Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 1 | #! /usr/bin/perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
| 5 | use mrw::Targets; |
| 6 | use mrw::Inventory; |
| 7 | use Getopt::Long; # For parsing command line arguments |
Brad Bishop | 550075b | 2017-01-30 20:27:04 -0500 | [diff] [blame^] | 8 | use YAML::Tiny qw(LoadFile); |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 9 | |
| 10 | # Globals |
| 11 | my $serverwizFile = ""; |
| 12 | my $debug = 0; |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 13 | my $outputFile = ""; |
| 14 | my $metaDataFile = ""; |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 15 | |
| 16 | # Command line argument parsing |
| 17 | GetOptions( |
| 18 | "i=s" => \$serverwizFile, # string |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 19 | "m=s" => \$metaDataFile, # string |
| 20 | "o=s" => \$outputFile, # string |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 21 | "d" => \$debug, |
| 22 | ) |
| 23 | or printUsage(); |
| 24 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 25 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq "")) |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 26 | { |
| 27 | printUsage(); |
| 28 | } |
| 29 | |
| 30 | my $targetObj = Targets->new; |
| 31 | $targetObj->loadXML($serverwizFile); |
| 32 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 33 | #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 | |
| 38 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
| 39 | my $fruTypeConfig = LoadFile($metaDataFile); |
| 40 | |
| 41 | my @interestedTypes = keys %{$fruTypeConfig}; |
| 42 | my %types; |
| 43 | @types{@interestedTypes} = (); |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 44 | |
| 45 | my @inventory = Inventory::getInventory($targetObj); |
| 46 | for my $item (@inventory) { |
| 47 | my $isFru = 0, my $fruID = 0, my $fruType = ""; |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 48 | #Fetch the FRUID. |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 49 | if (!$targetObj->isBadAttribute($item->{TARGET}, "FRU_ID")) { |
| 50 | $fruID = $targetObj->getAttribute($item->{TARGET}, "FRU_ID"); |
| 51 | $isFru = 1; |
| 52 | } |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 53 | #Fetch the FRU Type. |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 54 | if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) { |
| 55 | $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE"); |
| 56 | } |
| 57 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 58 | #Skip if we're not interested |
| 59 | next if (not $isFru or not exists $types{$fruType}); |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 60 | |
| 61 | printDebug ("FRUID => $fruID, FRUType => $fruType, ObjectPath => $item->{OBMC_NAME}"); |
| 62 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 63 | print $fh $fruID.":"; |
| 64 | print $fh "\n"; |
| 65 | |
| 66 | writeToFile($fruType,$item->{OBMC_NAME},$fruTypeConfig,$fh); |
| 67 | |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 68 | # Fetch all the children for this inventory target,It might happen the child is fru or non fru |
| 69 | # Following condition to be true for fetching the associated non fru devices. |
| 70 | # -it should be non fru. |
| 71 | # -type of the fru is in the interested types. |
| 72 | # - the parent of the child should be same as inventory target. |
| 73 | |
| 74 | foreach my $child ($targetObj->getAllTargetChildren($item->{TARGET})) { |
| 75 | $fruType = $targetObj->getAttribute($child, "TYPE"); |
| 76 | |
| 77 | if (!$targetObj->isBadAttribute($child, "FRU_ID")) { |
| 78 | #i.e this child is a fru,we are interrested in non fru devices |
| 79 | next; |
| 80 | } |
| 81 | |
| 82 | #Fetch the Fru Type |
| 83 | if (!$targetObj->isBadAttribute($child, "TYPE")) { |
| 84 | $fruType = $targetObj->getAttribute($child, "TYPE"); |
| 85 | } |
| 86 | |
| 87 | # check whether this fru type is in interested fru types. |
| 88 | if (not exists $types{$fruType}) { |
| 89 | next; |
| 90 | } |
| 91 | |
| 92 | # find the parent fru of this child. |
| 93 | my $parent = $targetObj->getTargetParent($child); |
| 94 | while ($parent ne ($item->{TARGET})) { |
| 95 | $parent = $targetObj->getTargetParent($parent); |
| 96 | if (!$targetObj->isBadAttribute($parent, "FRU_ID")) { |
| 97 | last; |
| 98 | } |
| 99 | |
| 100 | } |
| 101 | #if parent of the child is not equal to the item->target |
| 102 | #i.e some other fru is parent of this child. |
| 103 | if ( $parent ne ($item->{TARGET}) ){ |
| 104 | next; |
| 105 | } |
| 106 | |
| 107 | printDebug(" ".$child); |
| 108 | printDebug(" Type:".$fruType ); |
| 109 | my $childObmcName = getObmcName(\@inventory, $child); |
| 110 | writeToFile($fruType, $childObmcName, $fruTypeConfig, $fh); |
| 111 | } |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 112 | } |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 113 | close $fh; |
| 114 | |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 115 | #------------------------------------END OF MAIN----------------------- |
| 116 | |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 117 | # Map an MRW name to corresponding OBMC name |
| 118 | sub getObmcName |
| 119 | { |
| 120 | my $inventory = $_[0]; # Inventory items |
| 121 | my $target = $_[1]; # MRW Target name |
| 122 | for my $item (@inventory) |
| 123 | { |
| 124 | if($item->{TARGET} eq $target) |
| 125 | { |
| 126 | return $item->{OBMC_NAME}; |
| 127 | } |
| 128 | } |
| 129 | return undef; |
| 130 | } |
| 131 | |
| 132 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 133 | #Get the metdata for the incoming frutype from the loaded config file. |
| 134 | #Write the FRU data into the output file |
| 135 | |
| 136 | sub writeToFile |
| 137 | { |
| 138 | my $fruType = $_[0];#fru type |
| 139 | my $instancePath = $_[1];#instance Path |
| 140 | my $fruTypeConfig = $_[2];#loaded config file (frutypes) |
| 141 | my $fh = $_[3];#file Handle |
| 142 | #walk over all the fru types and match for the incoming type |
| 143 | print $fh " ".$instancePath.":"; |
| 144 | print $fh "\n"; |
| 145 | my $interfaces = $fruTypeConfig->{$fruType}; |
| 146 | #Walk over all the interfaces as it needs to be written |
| 147 | while ( my ($interface,$properties) = each %{$interfaces}) { |
| 148 | print $fh " ".$interface.":"; |
| 149 | print $fh "\n"; |
| 150 | #walk over all the properties as it needs to be written |
| 151 | while ( my ($dbusProperty,$metadata) = each %{$properties}) { |
| 152 | #will write property named "Property" first then |
| 153 | #other properties. |
| 154 | print $fh " ".$dbusProperty.":"; |
| 155 | print $fh "\n"; |
| 156 | for my $key (sort keys %{$metadata}) { |
| 157 | print $fh " $key: "."$metadata->{$key}"; |
| 158 | print $fh "\n"; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 164 | # Usage |
| 165 | sub printUsage |
| 166 | { |
| 167 | print " |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 168 | $0 -i [MRW filename] -m [MetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 169 | Options: |
| 170 | -d = debug mode |
| 171 | \n"; |
| 172 | exit(1); |
| 173 | } |
| 174 | |
| 175 | # Helper function to put debug statements. |
| 176 | sub printDebug |
| 177 | { |
| 178 | my $str = shift; |
| 179 | print "DEBUG: ", $str, "\n" if $debug; |
| 180 | } |