blob: 5465d846ab736520e66dd5d0e3c04d53f1c7e92b [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;
Deepak Kodihalli6225e932017-02-07 12:14:55 -06007use mrw::Util;
Ratan Guptafa70dc92017-01-17 00:09:04 +05308use Getopt::Long; # For parsing command line arguments
Brad Bishop550075b2017-01-30 20:27:04 -05009use YAML::Tiny qw(LoadFile);
Ratan Guptafa70dc92017-01-17 00:09:04 +053010
11# Globals
12my $serverwizFile = "";
13my $debug = 0;
Ratan Guptaa149ba12017-01-17 00:32:32 +053014my $outputFile = "";
15my $metaDataFile = "";
Ratan Guptafa70dc92017-01-17 00:09:04 +053016
17# Command line argument parsing
18GetOptions(
19"i=s" => \$serverwizFile, # string
Ratan Guptaa149ba12017-01-17 00:32:32 +053020"m=s" => \$metaDataFile, # string
21"o=s" => \$outputFile, # string
Ratan Guptafa70dc92017-01-17 00:09:04 +053022"d" => \$debug,
23)
24or printUsage();
25
Ratan Guptaa149ba12017-01-17 00:32:32 +053026if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptafa70dc92017-01-17 00:09:04 +053027{
28 printUsage();
29}
30
31my $targetObj = Targets->new;
32$targetObj->loadXML($serverwizFile);
33
Ratan Guptaa149ba12017-01-17 00:32:32 +053034#open the mrw xml and the metaData file for the fru.
35#Fetch the FRU id,type,object path from the mrw.
36#Get the metadata for that fru from the metadata file.
37#Merge the data into the outputfile
38
39open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
40my $fruTypeConfig = LoadFile($metaDataFile);
41
42my @interestedTypes = keys %{$fruTypeConfig};
43my %types;
44@types{@interestedTypes} = ();
Ratan Guptafa70dc92017-01-17 00:09:04 +053045
46my @inventory = Inventory::getInventory($targetObj);
47for my $item (@inventory) {
48 my $isFru = 0, my $fruID = 0, my $fruType = "";
Ratan Gupta26cc0552017-01-17 00:44:17 +053049 #Fetch the FRUID.
Ratan Guptafa70dc92017-01-17 00:09:04 +053050 if (!$targetObj->isBadAttribute($item->{TARGET}, "FRU_ID")) {
51 $fruID = $targetObj->getAttribute($item->{TARGET}, "FRU_ID");
52 $isFru = 1;
53 }
Ratan Gupta26cc0552017-01-17 00:44:17 +053054 #Fetch the FRU Type.
Ratan Guptafa70dc92017-01-17 00:09:04 +053055 if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) {
56 $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE");
57 }
58
Ratan Guptaa149ba12017-01-17 00:32:32 +053059 #Skip if we're not interested
60 next if (not $isFru or not exists $types{$fruType});
Ratan Guptafa70dc92017-01-17 00:09:04 +053061
62 printDebug ("FRUID => $fruID, FRUType => $fruType, ObjectPath => $item->{OBMC_NAME}");
63
Ratan Guptaa149ba12017-01-17 00:32:32 +053064 print $fh $fruID.":";
65 print $fh "\n";
66
67 writeToFile($fruType,$item->{OBMC_NAME},$fruTypeConfig,$fh);
68
Ratan Gupta26cc0552017-01-17 00:44:17 +053069 # Fetch all the children for this inventory target,It might happen the child is fru or non fru
70 # Following condition to be true for fetching the associated non fru devices.
71 # -it should be non fru.
72 # -type of the fru is in the interested types.
73 # - the parent of the child should be same as inventory target.
74
75 foreach my $child ($targetObj->getAllTargetChildren($item->{TARGET})) {
76 $fruType = $targetObj->getAttribute($child, "TYPE");
77
78 if (!$targetObj->isBadAttribute($child, "FRU_ID")) {
79 #i.e this child is a fru,we are interrested in non fru devices
80 next;
81 }
82
83 #Fetch the Fru Type
84 if (!$targetObj->isBadAttribute($child, "TYPE")) {
85 $fruType = $targetObj->getAttribute($child, "TYPE");
86 }
87
88 # check whether this fru type is in interested fru types.
89 if (not exists $types{$fruType}) {
90 next;
91 }
92
93 # find the parent fru of this child.
94 my $parent = $targetObj->getTargetParent($child);
95 while ($parent ne ($item->{TARGET})) {
96 $parent = $targetObj->getTargetParent($parent);
97 if (!$targetObj->isBadAttribute($parent, "FRU_ID")) {
98 last;
99 }
100
101 }
102 #if parent of the child is not equal to the item->target
103 #i.e some other fru is parent of this child.
104 if ( $parent ne ($item->{TARGET}) ){
105 next;
106 }
107
108 printDebug(" ".$child);
109 printDebug(" Type:".$fruType );
Deepak Kodihalli6225e932017-02-07 12:14:55 -0600110 my $childObmcName = Util::getObmcName(\@inventory, $child);
Ratan Gupta26cc0552017-01-17 00:44:17 +0530111 writeToFile($fruType, $childObmcName, $fruTypeConfig, $fh);
112 }
Ratan Guptafa70dc92017-01-17 00:09:04 +0530113}
Ratan Guptaa149ba12017-01-17 00:32:32 +0530114close $fh;
115
Ratan Guptafa70dc92017-01-17 00:09:04 +0530116#------------------------------------END OF MAIN-----------------------
117
Ratan Guptaa149ba12017-01-17 00:32:32 +0530118#Get the metdata for the incoming frutype from the loaded config file.
119#Write the FRU data into the output file
120
121sub writeToFile
122{
123 my $fruType = $_[0];#fru type
124 my $instancePath = $_[1];#instance Path
125 my $fruTypeConfig = $_[2];#loaded config file (frutypes)
126 my $fh = $_[3];#file Handle
127 #walk over all the fru types and match for the incoming type
128 print $fh " ".$instancePath.":";
129 print $fh "\n";
130 my $interfaces = $fruTypeConfig->{$fruType};
131 #Walk over all the interfaces as it needs to be written
132 while ( my ($interface,$properties) = each %{$interfaces}) {
133 print $fh " ".$interface.":";
134 print $fh "\n";
135 #walk over all the properties as it needs to be written
136 while ( my ($dbusProperty,$metadata) = each %{$properties}) {
137 #will write property named "Property" first then
138 #other properties.
139 print $fh " ".$dbusProperty.":";
140 print $fh "\n";
141 for my $key (sort keys %{$metadata}) {
142 print $fh " $key: "."$metadata->{$key}";
143 print $fh "\n";
144 }
145 }
146 }
147}
148
Ratan Guptafa70dc92017-01-17 00:09:04 +0530149# Usage
150sub printUsage
151{
152 print "
Ratan Guptaa149ba12017-01-17 00:32:32 +0530153 $0 -i [MRW filename] -m [MetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptafa70dc92017-01-17 00:09:04 +0530154Options:
155 -d = debug mode
156 \n";
157 exit(1);
158}
159
160# Helper function to put debug statements.
161sub printDebug
162{
163 my $str = shift;
164 print "DEBUG: ", $str, "\n" if $debug;
165}