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