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; |
Deepak Kodihalli | 6225e93 | 2017-02-07 12:14:55 -0600 | [diff] [blame] | 7 | use mrw::Util; |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 8 | use Getopt::Long; # For parsing command line arguments |
Brad Bishop | 550075b | 2017-01-30 20:27:04 -0500 | [diff] [blame] | 9 | use YAML::Tiny qw(LoadFile); |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 10 | |
| 11 | # Globals |
| 12 | my $serverwizFile = ""; |
| 13 | my $debug = 0; |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 14 | my $outputFile = ""; |
| 15 | my $metaDataFile = ""; |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 16 | |
| 17 | # Command line argument parsing |
| 18 | GetOptions( |
| 19 | "i=s" => \$serverwizFile, # string |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 20 | "m=s" => \$metaDataFile, # string |
| 21 | "o=s" => \$outputFile, # string |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 22 | "d" => \$debug, |
| 23 | ) |
| 24 | or printUsage(); |
| 25 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 26 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq "")) |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 27 | { |
| 28 | printUsage(); |
| 29 | } |
| 30 | |
| 31 | my $targetObj = Targets->new; |
| 32 | $targetObj->loadXML($serverwizFile); |
| 33 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 34 | #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 | |
| 39 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
| 40 | my $fruTypeConfig = LoadFile($metaDataFile); |
| 41 | |
| 42 | my @interestedTypes = keys %{$fruTypeConfig}; |
| 43 | my %types; |
| 44 | @types{@interestedTypes} = (); |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 45 | |
| 46 | my @inventory = Inventory::getInventory($targetObj); |
| 47 | for my $item (@inventory) { |
| 48 | my $isFru = 0, my $fruID = 0, my $fruType = ""; |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 49 | #Fetch the FRUID. |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 50 | if (!$targetObj->isBadAttribute($item->{TARGET}, "FRU_ID")) { |
| 51 | $fruID = $targetObj->getAttribute($item->{TARGET}, "FRU_ID"); |
| 52 | $isFru = 1; |
| 53 | } |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 54 | #Fetch the FRU Type. |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 55 | if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) { |
| 56 | $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE"); |
| 57 | } |
| 58 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 59 | #Skip if we're not interested |
| 60 | next if (not $isFru or not exists $types{$fruType}); |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 61 | |
| 62 | printDebug ("FRUID => $fruID, FRUType => $fruType, ObjectPath => $item->{OBMC_NAME}"); |
| 63 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 64 | print $fh $fruID.":"; |
| 65 | print $fh "\n"; |
| 66 | |
| 67 | writeToFile($fruType,$item->{OBMC_NAME},$fruTypeConfig,$fh); |
| 68 | |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 69 | # 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 Kodihalli | 6225e93 | 2017-02-07 12:14:55 -0600 | [diff] [blame] | 110 | my $childObmcName = Util::getObmcName(\@inventory, $child); |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 111 | writeToFile($fruType, $childObmcName, $fruTypeConfig, $fh); |
| 112 | } |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 113 | } |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 114 | close $fh; |
| 115 | |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 116 | #------------------------------------END OF MAIN----------------------- |
| 117 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 118 | #Get the metdata for the incoming frutype from the loaded config file. |
| 119 | #Write the FRU data into the output file |
| 120 | |
| 121 | sub 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 Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 149 | # Usage |
| 150 | sub printUsage |
| 151 | { |
| 152 | print " |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 153 | $0 -i [MRW filename] -m [MetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 154 | Options: |
| 155 | -d = debug mode |
| 156 | \n"; |
| 157 | exit(1); |
| 158 | } |
| 159 | |
| 160 | # Helper function to put debug statements. |
| 161 | sub printDebug |
| 162 | { |
| 163 | my $str = shift; |
| 164 | print "DEBUG: ", $str, "\n" if $debug; |
| 165 | } |