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