Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame^] | 1 | #!/usr/bin/perl |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 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 | # 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 = ""; |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame^] | 15 | my $skipBrokenMrw = 0; |
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 |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame^] | 22 | "skip-broken-mrw" => \$skipBrokenMrw, |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 23 | "d" => \$debug, |
| 24 | ) |
| 25 | or printUsage(); |
| 26 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 27 | if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq "")) |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 28 | { |
| 29 | printUsage(); |
| 30 | } |
| 31 | |
| 32 | my $targetObj = Targets->new; |
| 33 | $targetObj->loadXML($serverwizFile); |
| 34 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 35 | #open the mrw xml and the metaData file for the fru. |
| 36 | #Fetch the FRU id,type,object path from the mrw. |
| 37 | #Get the metadata for that fru from the metadata file. |
| 38 | #Merge the data into the outputfile |
| 39 | |
| 40 | open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!"; |
| 41 | my $fruTypeConfig = LoadFile($metaDataFile); |
| 42 | |
| 43 | my @interestedTypes = keys %{$fruTypeConfig}; |
| 44 | my %types; |
| 45 | @types{@interestedTypes} = (); |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 46 | |
Ratan Gupta | 8b34b33 | 2018-01-24 16:42:06 +0530 | [diff] [blame] | 47 | my %entityInfoDict; |
| 48 | |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 49 | my @allAssoTypes = getAllAssociatedTypes($fruTypeConfig); |
| 50 | my %allAssoTypesHash; |
| 51 | @allAssoTypesHash{@allAssoTypes} = (); |
| 52 | |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 53 | my @inventory = Inventory::getInventory($targetObj); |
| 54 | for my $item (@inventory) { |
| 55 | my $isFru = 0, my $fruID = 0, my $fruType = ""; |
Ratan Gupta | 8b34b33 | 2018-01-24 16:42:06 +0530 | [diff] [blame] | 56 | my $entityInstance = 1, my $entityID = 0; |
| 57 | |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 58 | #Fetch the FRUID. |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 59 | if (!$targetObj->isBadAttribute($item->{TARGET}, "FRU_ID")) { |
| 60 | $fruID = $targetObj->getAttribute($item->{TARGET}, "FRU_ID"); |
| 61 | $isFru = 1; |
| 62 | } |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 63 | #Fetch the FRU Type. |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 64 | if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) { |
| 65 | $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE"); |
| 66 | } |
| 67 | |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 68 | #Skip if any one is true |
| 69 | #1) If not fru |
| 70 | #2) if the fru type is not there in the config file. |
| 71 | #3) if the fru type is in associated types. |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame^] | 72 | #4) if FRU_ID is not defined for the target and we are asked to ignore such |
| 73 | # targets. |
| 74 | |
| 75 | next if ($skipBrokenMrw and ($fruID eq "")); |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 76 | |
| 77 | next if (not $isFru or not exists $types{$fruType} or exists $allAssoTypesHash{$fruType}); |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 78 | |
| 79 | printDebug ("FRUID => $fruID, FRUType => $fruType, ObjectPath => $item->{OBMC_NAME}"); |
| 80 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 81 | print $fh $fruID.":"; |
| 82 | print $fh "\n"; |
| 83 | |
Ratan Gupta | 8b34b33 | 2018-01-24 16:42:06 +0530 | [diff] [blame] | 84 | $entityID = $fruTypeConfig->{$fruType}->{'EntityID'}; |
| 85 | |
| 86 | if (exists $entityInfoDict{$entityID}) { |
| 87 | $entityInstance = $entityInfoDict{$entityID} + 1; |
| 88 | } |
| 89 | |
| 90 | printDebug("entityID => $entityID , entityInstance => $entityInstance"); |
| 91 | |
| 92 | $entityInfoDict{$entityID} = $entityInstance; |
| 93 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 94 | writeToFile($fruType,$item->{OBMC_NAME},$fruTypeConfig,$fh); |
| 95 | |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 96 | #if the key(AssociatedTypes) exists and it is defined |
| 97 | #then make the association. |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 98 | |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 99 | if (!defined $fruTypeConfig->{$fruType}->{'AssociatedTypes'}) { |
| 100 | next; |
Ratan Gupta | 26cc055 | 2017-01-17 00:44:17 +0530 | [diff] [blame] | 101 | } |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 102 | |
| 103 | my $assoTypes = $fruTypeConfig->{$fruType}->{'AssociatedTypes'}; |
| 104 | for my $type (@$assoTypes) { |
| 105 | my @devices = Util::getDevicePath(\@inventory,$targetObj,$type); |
| 106 | for my $device (@devices) { |
| 107 | writeToFile($type,$device,$fruTypeConfig,$fh); |
| 108 | } |
| 109 | |
| 110 | } |
| 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 | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 117 | # Get all the associated types |
| 118 | sub getAllAssociatedTypes |
| 119 | { |
| 120 | my $fruTypeConfig = $_[0]; |
| 121 | my @assoTypes; |
| 122 | while (my($key, $value) = each %$fruTypeConfig) { |
| 123 | #if the key exist and value is also there |
| 124 | if (defined $value->{'AssociatedTypes'}) { |
| 125 | my $assoTypes = $value->{'AssociatedTypes'}; |
| 126 | for my $type (@$assoTypes) { |
| 127 | push(@assoTypes,$type); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | return @assoTypes; |
| 132 | } |
| 133 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 134 | #Get the metdata for the incoming frutype from the loaded config file. |
| 135 | #Write the FRU data into the output file |
| 136 | |
| 137 | sub writeToFile |
| 138 | { |
| 139 | my $fruType = $_[0];#fru type |
| 140 | my $instancePath = $_[1];#instance Path |
| 141 | my $fruTypeConfig = $_[2];#loaded config file (frutypes) |
| 142 | my $fh = $_[3];#file Handle |
| 143 | #walk over all the fru types and match for the incoming type |
Ratan Gupta | 8b34b33 | 2018-01-24 16:42:06 +0530 | [diff] [blame] | 144 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 145 | print $fh " ".$instancePath.":"; |
| 146 | print $fh "\n"; |
Ratan Gupta | 8b34b33 | 2018-01-24 16:42:06 +0530 | [diff] [blame] | 147 | print $fh " "."entityID: ".$fruTypeConfig->{$fruType}->{'EntityID'}; |
| 148 | print $fh "\n"; |
| 149 | print $fh " "."entityInstance: "; |
| 150 | print $fh $entityInfoDict{$fruTypeConfig->{$fruType}->{'EntityID'}}; |
| 151 | print $fh "\n"; |
| 152 | print $fh " "."interfaces:"; |
| 153 | print $fh "\n"; |
| 154 | |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 155 | my $interfaces = $fruTypeConfig->{$fruType}->{'Interfaces'}; |
Ratan Gupta | 8b34b33 | 2018-01-24 16:42:06 +0530 | [diff] [blame] | 156 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 157 | #Walk over all the interfaces as it needs to be written |
| 158 | while ( my ($interface,$properties) = each %{$interfaces}) { |
Ratan Gupta | 8b34b33 | 2018-01-24 16:42:06 +0530 | [diff] [blame] | 159 | print $fh " ".$interface.":"; |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 160 | print $fh "\n"; |
| 161 | #walk over all the properties as it needs to be written |
| 162 | while ( my ($dbusProperty,$metadata) = each %{$properties}) { |
| 163 | #will write property named "Property" first then |
| 164 | #other properties. |
Ratan Gupta | 8b34b33 | 2018-01-24 16:42:06 +0530 | [diff] [blame] | 165 | print $fh " ".$dbusProperty.":"; |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 166 | print $fh "\n"; |
| 167 | for my $key (sort keys %{$metadata}) { |
Ratan Gupta | 8b34b33 | 2018-01-24 16:42:06 +0530 | [diff] [blame] | 168 | print $fh " $key: "."$metadata->{$key}"; |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 169 | print $fh "\n"; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 175 | # Usage |
| 176 | sub printUsage |
| 177 | { |
| 178 | print " |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame] | 179 | $0 -i [MRW filename] -m [MetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 180 | Options: |
| 181 | -d = debug mode |
Santosh Puranik | 51041fc | 2019-10-08 09:45:15 -0500 | [diff] [blame^] | 182 | --skip-broken-mrw = Skip broken MRW targets |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 183 | \n"; |
| 184 | exit(1); |
| 185 | } |
| 186 | |
| 187 | # Helper function to put debug statements. |
| 188 | sub printDebug |
| 189 | { |
| 190 | my $str = shift; |
| 191 | print "DEBUG: ", $str, "\n" if $debug; |
| 192 | } |
Ratan Gupta | d92101d | 2017-02-15 19:40:39 +0530 | [diff] [blame] | 193 | |