Deepak Kodihalli | 9f3e1c1 | 2017-02-19 23:39:15 -0600 | [diff] [blame^] | 1 | #! /usr/bin/perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | |
| 5 | |
| 6 | use mrw::Targets; |
| 7 | use mrw::Inventory; |
| 8 | use mrw::Util; |
| 9 | use Getopt::Long; |
| 10 | use YAML::Tiny qw(LoadFile); |
| 11 | |
| 12 | |
| 13 | my $mrwFile = ""; |
| 14 | my $outFile = ""; |
| 15 | my $configFile = ""; |
| 16 | |
| 17 | |
| 18 | GetOptions( |
| 19 | "m=s" => \$mrwFile, |
| 20 | "c=s" => \$configFile, |
| 21 | "o=s" => \$outFile, |
| 22 | ) |
| 23 | or printUsage(); |
| 24 | |
| 25 | |
| 26 | if (($mrwFile eq "") or ($configFile eq "") or ($outFile eq "")) |
| 27 | { |
| 28 | printUsage(); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | # Load system MRW |
| 33 | my $targets = Targets->new; |
| 34 | $targets->loadXML($mrwFile); |
| 35 | my @inventory = Inventory::getInventory($targets); |
| 36 | |
| 37 | |
| 38 | # Parse config YAML |
| 39 | my $targetItems = LoadFile($configFile); |
| 40 | |
| 41 | |
| 42 | # Targets we're interested in, from the config YAML |
| 43 | my @targetNames = keys %{$targetItems}; |
| 44 | my %targetHash; |
| 45 | @targetHash{@targetNames} = (); |
| 46 | |
| 47 | |
| 48 | # Target Type : Target inventory path |
| 49 | my %defaultPaths = ( |
| 50 | "ETHERNET", |
| 51 | Util::getObmcName( |
| 52 | \@inventory, |
| 53 | Util::getBMCTarget($targets))."/ethernet" |
| 54 | ); |
| 55 | |
| 56 | |
| 57 | open(my $fh, '>', $outFile) or die "Could not open file '$outFile' $!"; |
| 58 | # Retrieve OBMC path of targets we're interested in |
| 59 | for my $item (@inventory) { |
| 60 | my $targetType = ""; |
| 61 | my $path = ""; |
| 62 | |
| 63 | if (!$targets->isBadAttribute($item->{TARGET}, "TYPE")) { |
| 64 | $targetType = $targets->getAttribute($item->{TARGET}, "TYPE"); |
| 65 | } |
| 66 | next if (not exists $targetItems->{$targetType}); |
| 67 | |
| 68 | writeOutput($targetType, |
| 69 | $item, |
| 70 | $targetItems); |
| 71 | delete($targetHash{$targetType}); |
| 72 | } |
| 73 | writeRemaining($targetItems); |
| 74 | close $fh; |
| 75 | |
| 76 | |
| 77 | sub writeRemaining |
| 78 | { |
| 79 | my ($yamlDict) = @_; |
| 80 | for my $type (keys %targetHash) |
| 81 | { |
| 82 | print $fh $defaultPaths{$type}.":"; |
| 83 | print $fh "\n"; |
| 84 | while (my ($interface,$propertyMap) = each %{$yamlDict->{$type}}) |
| 85 | { |
| 86 | print $fh " ".$interface.":"; |
| 87 | print $fh "\n"; |
| 88 | while (my ($property,$value) = each %{$propertyMap}) |
| 89 | { |
| 90 | $value = "'".$value."'"; |
| 91 | print $fh " ".$property.": ".$value; |
| 92 | print $fh "\n"; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | sub writeOutput |
| 100 | { |
| 101 | my ($type, $item, $yamlDict) = @_; |
| 102 | print $fh $item->{OBMC_NAME}.":"; |
| 103 | print $fh "\n"; |
| 104 | while (my ($interface,$propertyMap) = each %{$yamlDict->{$type}}) |
| 105 | { |
| 106 | print $fh " ".$interface.":"; |
| 107 | print $fh "\n"; |
| 108 | while (my ($property,$value) = each %{$propertyMap}) |
| 109 | { |
| 110 | $value = getValue($item, $property, $value); |
| 111 | print $fh " ".$property.": ".$value; |
| 112 | print $fh "\n"; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | sub getValue |
| 119 | { |
| 120 | my ($item, $property, $value) = @_; |
| 121 | $value = "'".$value."'"; |
| 122 | |
| 123 | if ($property eq "FieldReplaceable") |
| 124 | { |
| 125 | $value = "'false'"; |
| 126 | if (!$targets->isBadAttribute($item->{TARGET}, "RU_TYPE")) |
| 127 | { |
| 128 | my $ruType = $targets->getAttribute($item->{TARGET}, "RU_TYPE"); |
| 129 | if (($ruType eq "FRU") || ($ruType eq "CRU")) |
| 130 | { |
| 131 | $value = "'true'"; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return $value; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | sub printUsage |
| 141 | { |
| 142 | print " |
| 143 | $0 -m [MRW file] -c [Config yaml] -o [Output filename]\n"; |
| 144 | exit(1); |
| 145 | } |