blob: 760082fe1b98d38877b2c408dc3a8246feaa6726 [file] [log] [blame]
Ratan Guptafa70dc92017-01-17 00:09:04 +05301#! /usr/bin/perl
2use strict;
3use warnings;
4
5use mrw::Targets;
6use mrw::Inventory;
7use Getopt::Long; # For parsing command line arguments
8use YAML::XS 'LoadFile'; # For loading and reading of YAML file
9
10# Globals
11my $serverwizFile = "";
12my $debug = 0;
Ratan Guptaa149ba12017-01-17 00:32:32 +053013my $outputFile = "";
14my $metaDataFile = "";
Ratan Guptafa70dc92017-01-17 00:09:04 +053015
16# Command line argument parsing
17GetOptions(
18"i=s" => \$serverwizFile, # string
Ratan Guptaa149ba12017-01-17 00:32:32 +053019"m=s" => \$metaDataFile, # string
20"o=s" => \$outputFile, # string
Ratan Guptafa70dc92017-01-17 00:09:04 +053021"d" => \$debug,
22)
23or printUsage();
24
Ratan Guptaa149ba12017-01-17 00:32:32 +053025if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptafa70dc92017-01-17 00:09:04 +053026{
27 printUsage();
28}
29
30my $targetObj = Targets->new;
31$targetObj->loadXML($serverwizFile);
32
Ratan Guptaa149ba12017-01-17 00:32:32 +053033#open the mrw xml and the metaData file for the fru.
34#Fetch the FRU id,type,object path from the mrw.
35#Get the metadata for that fru from the metadata file.
36#Merge the data into the outputfile
37
38open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
39my $fruTypeConfig = LoadFile($metaDataFile);
40
41my @interestedTypes = keys %{$fruTypeConfig};
42my %types;
43@types{@interestedTypes} = ();
Ratan Guptafa70dc92017-01-17 00:09:04 +053044
45my @inventory = Inventory::getInventory($targetObj);
46for my $item (@inventory) {
47 my $isFru = 0, my $fruID = 0, my $fruType = "";
Ratan Guptaa149ba12017-01-17 00:32:32 +053048 #Fetch the FRU ID.
Ratan Guptafa70dc92017-01-17 00:09:04 +053049 if (!$targetObj->isBadAttribute($item->{TARGET}, "FRU_ID")) {
50 $fruID = $targetObj->getAttribute($item->{TARGET}, "FRU_ID");
51 $isFru = 1;
52 }
Ratan Guptaa149ba12017-01-17 00:32:32 +053053 # Fetch the FRU Type.
Ratan Guptafa70dc92017-01-17 00:09:04 +053054 if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) {
55 $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE");
56 }
57
Ratan Guptaa149ba12017-01-17 00:32:32 +053058 #Skip if we're not interested
59 next if (not $isFru or not exists $types{$fruType});
Ratan Guptafa70dc92017-01-17 00:09:04 +053060
61 printDebug ("FRUID => $fruID, FRUType => $fruType, ObjectPath => $item->{OBMC_NAME}");
62
Ratan Guptaa149ba12017-01-17 00:32:32 +053063 print $fh $fruID.":";
64 print $fh "\n";
65
66 writeToFile($fruType,$item->{OBMC_NAME},$fruTypeConfig,$fh);
67
Ratan Guptafa70dc92017-01-17 00:09:04 +053068}
Ratan Guptaa149ba12017-01-17 00:32:32 +053069close $fh;
70
Ratan Guptafa70dc92017-01-17 00:09:04 +053071#------------------------------------END OF MAIN-----------------------
72
Ratan Guptaa149ba12017-01-17 00:32:32 +053073#Get the metdata for the incoming frutype from the loaded config file.
74#Write the FRU data into the output file
75
76sub writeToFile
77{
78 my $fruType = $_[0];#fru type
79 my $instancePath = $_[1];#instance Path
80 my $fruTypeConfig = $_[2];#loaded config file (frutypes)
81 my $fh = $_[3];#file Handle
82 #walk over all the fru types and match for the incoming type
83 print $fh " ".$instancePath.":";
84 print $fh "\n";
85 my $interfaces = $fruTypeConfig->{$fruType};
86 #Walk over all the interfaces as it needs to be written
87 while ( my ($interface,$properties) = each %{$interfaces}) {
88 print $fh " ".$interface.":";
89 print $fh "\n";
90 #walk over all the properties as it needs to be written
91 while ( my ($dbusProperty,$metadata) = each %{$properties}) {
92 #will write property named "Property" first then
93 #other properties.
94 print $fh " ".$dbusProperty.":";
95 print $fh "\n";
96 for my $key (sort keys %{$metadata}) {
97 print $fh " $key: "."$metadata->{$key}";
98 print $fh "\n";
99 }
100 }
101 }
102}
103
Ratan Guptafa70dc92017-01-17 00:09:04 +0530104# Usage
105sub printUsage
106{
107 print "
Ratan Guptaa149ba12017-01-17 00:32:32 +0530108 $0 -i [MRW filename] -m [MetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptafa70dc92017-01-17 00:09:04 +0530109Options:
110 -d = debug mode
111 \n";
112 exit(1);
113}
114
115# Helper function to put debug statements.
116sub printDebug
117{
118 my $str = shift;
119 print "DEBUG: ", $str, "\n" if $debug;
120}