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