1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6
7use mrw::Targets;
8use mrw::Inventory;
9use mrw::Util;
10use Getopt::Long;
11use YAML::Tiny qw(LoadFile);
12
13
14my $mrwFile = "";
15my $outFile = "";
16my $configFile = "";
17my $skipBrokenMrw = 0;
18
19
20GetOptions(
21"m=s" => \$mrwFile,
22"c=s" => \$configFile,
23"o=s" => \$outFile,
24"skip-broken-mrw" => \$skipBrokenMrw
25)
26or printUsage();
27
28
29if (($mrwFile eq "") or ($configFile eq "") or ($outFile eq ""))
30{
31    printUsage();
32}
33
34
35# Load system MRW
36my $targets = Targets->new;
37$targets->loadXML($mrwFile);
38my @inventory = Inventory::getInventory($targets);
39
40
41# Parse config YAML
42my $targetItems = LoadFile($configFile);
43
44
45# Targets we're interested in, from the config YAML
46my @targetNames = keys %{$targetItems};
47my %targetHash;
48@targetHash{@targetNames} = ();
49
50
51# Target Type : Target inventory path
52my %defaultPaths = (
53    "ETHERNET",
54     Util::getObmcName(
55         \@inventory,
56         Util::getBMCTarget($targets))."/ethernet"
57);
58
59
60open(my $fh, '>', $outFile) or die "Could not open file '$outFile' $!";
61# Retrieve OBMC path of targets we're interested in
62for 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}
76writeRemaining($targetItems);
77close $fh;
78
79
80sub writeRemaining
81{
82    my ($yamlDict) = @_;
83    for my $type (keys %targetHash)
84    {
85        if($skipBrokenMrw and !exists $defaultPaths{$type})
86        {
87            next;
88        }
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
106sub 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
125sub 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
147sub printUsage
148{
149    print "
150    $0 -m [MRW file] -c [Config yaml] -o [Output filename] [OPTIONS]
151Options:
152    --skip-broken-mrw = Skip broken MRW targets
153    \n";
154    exit(1);
155}
156