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 |
| 8 | use YAML::XS 'LoadFile'; # For loading and reading of YAML file |
| 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 | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame^] | 48 | #Fetch the FRU ID. |
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 | a149ba1 | 2017-01-17 00:32:32 +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 | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 68 | } |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame^] | 69 | close $fh; |
| 70 | |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 71 | #------------------------------------END OF MAIN----------------------- |
| 72 | |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame^] | 73 | #Get the metdata for the incoming frutype from the loaded config file. |
| 74 | #Write the FRU data into the output file |
| 75 | |
| 76 | sub writeToFile |
| 77 | { |
| 78 | my $fruType = $_[0];#fru type |
| 79 | my $instancePath = $_[1];#instance Path |
| 80 | my $fruTypeConfig = $_[2];#loaded config file (frutypes) |
| 81 | my $fh = $_[3];#file Handle |
| 82 | #walk over all the fru types and match for the incoming type |
| 83 | print $fh " ".$instancePath.":"; |
| 84 | print $fh "\n"; |
| 85 | my $interfaces = $fruTypeConfig->{$fruType}; |
| 86 | #Walk over all the interfaces as it needs to be written |
| 87 | while ( my ($interface,$properties) = each %{$interfaces}) { |
| 88 | print $fh " ".$interface.":"; |
| 89 | print $fh "\n"; |
| 90 | #walk over all the properties as it needs to be written |
| 91 | while ( my ($dbusProperty,$metadata) = each %{$properties}) { |
| 92 | #will write property named "Property" first then |
| 93 | #other properties. |
| 94 | print $fh " ".$dbusProperty.":"; |
| 95 | print $fh "\n"; |
| 96 | for my $key (sort keys %{$metadata}) { |
| 97 | print $fh " $key: "."$metadata->{$key}"; |
| 98 | print $fh "\n"; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 104 | # Usage |
| 105 | sub printUsage |
| 106 | { |
| 107 | print " |
Ratan Gupta | a149ba1 | 2017-01-17 00:32:32 +0530 | [diff] [blame^] | 108 | $0 -i [MRW filename] -m [MetaData filename] -o [Output filename] [OPTIONS] |
Ratan Gupta | fa70dc9 | 2017-01-17 00:09:04 +0530 | [diff] [blame] | 109 | Options: |
| 110 | -d = debug mode |
| 111 | \n"; |
| 112 | exit(1); |
| 113 | } |
| 114 | |
| 115 | # Helper function to put debug statements. |
| 116 | sub printDebug |
| 117 | { |
| 118 | my $str = shift; |
| 119 | print "DEBUG: ", $str, "\n" if $debug; |
| 120 | } |