blob: 1f135844b7c16686b937b83e045ebca9a59b8c3b [file] [log] [blame]
Santosh Puranik51041fc2019-10-08 09:45:15 -05001#!/usr/bin/perl
Ratan Guptafa70dc92017-01-17 00:09:04 +05302use strict;
3use warnings;
4
5use mrw::Targets;
6use mrw::Inventory;
Deepak Kodihalli6225e932017-02-07 12:14:55 -06007use mrw::Util;
Ratan Guptafa70dc92017-01-17 00:09:04 +05308use Getopt::Long; # For parsing command line arguments
Brad Bishop550075b2017-01-30 20:27:04 -05009use YAML::Tiny qw(LoadFile);
Ratan Guptafa70dc92017-01-17 00:09:04 +053010# Globals
11my $serverwizFile = "";
12my $debug = 0;
Ratan Guptaa149ba12017-01-17 00:32:32 +053013my $outputFile = "";
14my $metaDataFile = "";
Santosh Puranik51041fc2019-10-08 09:45:15 -050015my $skipBrokenMrw = 0;
Ratan Guptafa70dc92017-01-17 00:09:04 +053016
17# Command line argument parsing
18GetOptions(
19"i=s" => \$serverwizFile, # string
Ratan Guptaa149ba12017-01-17 00:32:32 +053020"m=s" => \$metaDataFile, # string
21"o=s" => \$outputFile, # string
Santosh Puranik51041fc2019-10-08 09:45:15 -050022"skip-broken-mrw" => \$skipBrokenMrw,
Ratan Guptafa70dc92017-01-17 00:09:04 +053023"d" => \$debug,
24)
25or printUsage();
26
Ratan Guptaa149ba12017-01-17 00:32:32 +053027if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptafa70dc92017-01-17 00:09:04 +053028{
29 printUsage();
30}
31
32my $targetObj = Targets->new;
33$targetObj->loadXML($serverwizFile);
34
Ratan Guptaa149ba12017-01-17 00:32:32 +053035#open the mrw xml and the metaData file for the fru.
36#Fetch the FRU id,type,object path from the mrw.
37#Get the metadata for that fru from the metadata file.
38#Merge the data into the outputfile
39
40open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
41my $fruTypeConfig = LoadFile($metaDataFile);
42
43my @interestedTypes = keys %{$fruTypeConfig};
44my %types;
45@types{@interestedTypes} = ();
Ratan Guptafa70dc92017-01-17 00:09:04 +053046
Ratan Gupta8b34b332018-01-24 16:42:06 +053047my %entityInfoDict;
48
Ratan Guptad92101d2017-02-15 19:40:39 +053049my @allAssoTypes = getAllAssociatedTypes($fruTypeConfig);
50my %allAssoTypesHash;
51@allAssoTypesHash{@allAssoTypes} = ();
52
Ratan Guptafa70dc92017-01-17 00:09:04 +053053my @inventory = Inventory::getInventory($targetObj);
54for my $item (@inventory) {
55 my $isFru = 0, my $fruID = 0, my $fruType = "";
Ratan Gupta8b34b332018-01-24 16:42:06 +053056 my $entityInstance = 1, my $entityID = 0;
57
Ratan Gupta26cc0552017-01-17 00:44:17 +053058 #Fetch the FRUID.
Ratan Guptafa70dc92017-01-17 00:09:04 +053059 if (!$targetObj->isBadAttribute($item->{TARGET}, "FRU_ID")) {
60 $fruID = $targetObj->getAttribute($item->{TARGET}, "FRU_ID");
61 $isFru = 1;
62 }
Ratan Gupta26cc0552017-01-17 00:44:17 +053063 #Fetch the FRU Type.
Ratan Guptafa70dc92017-01-17 00:09:04 +053064 if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) {
65 $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE");
66 }
67
Ratan Guptad92101d2017-02-15 19:40:39 +053068 #Skip if any one is true
69 #1) If not fru
70 #2) if the fru type is not there in the config file.
71 #3) if the fru type is in associated types.
Santosh Puranik51041fc2019-10-08 09:45:15 -050072 #4) if FRU_ID is not defined for the target and we are asked to ignore such
73 # targets.
74
75 next if ($skipBrokenMrw and ($fruID eq ""));
Ratan Guptad92101d2017-02-15 19:40:39 +053076
77 next if (not $isFru or not exists $types{$fruType} or exists $allAssoTypesHash{$fruType});
Ratan Guptafa70dc92017-01-17 00:09:04 +053078
79 printDebug ("FRUID => $fruID, FRUType => $fruType, ObjectPath => $item->{OBMC_NAME}");
80
Ratan Guptaa149ba12017-01-17 00:32:32 +053081 print $fh $fruID.":";
82 print $fh "\n";
83
Ratan Gupta8b34b332018-01-24 16:42:06 +053084 $entityID = $fruTypeConfig->{$fruType}->{'EntityID'};
85
86 if (exists $entityInfoDict{$entityID}) {
87 $entityInstance = $entityInfoDict{$entityID} + 1;
88 }
89
90 printDebug("entityID => $entityID , entityInstance => $entityInstance");
91
92 $entityInfoDict{$entityID} = $entityInstance;
93
Ratan Guptaa149ba12017-01-17 00:32:32 +053094 writeToFile($fruType,$item->{OBMC_NAME},$fruTypeConfig,$fh);
95
Ratan Guptad92101d2017-02-15 19:40:39 +053096 #if the key(AssociatedTypes) exists and it is defined
97 #then make the association.
Ratan Gupta26cc0552017-01-17 00:44:17 +053098
Ratan Guptad92101d2017-02-15 19:40:39 +053099 if (!defined $fruTypeConfig->{$fruType}->{'AssociatedTypes'}) {
100 next;
Ratan Gupta26cc0552017-01-17 00:44:17 +0530101 }
Ratan Guptad92101d2017-02-15 19:40:39 +0530102
103 my $assoTypes = $fruTypeConfig->{$fruType}->{'AssociatedTypes'};
104 for my $type (@$assoTypes) {
105 my @devices = Util::getDevicePath(\@inventory,$targetObj,$type);
106 for my $device (@devices) {
107 writeToFile($type,$device,$fruTypeConfig,$fh);
108 }
109
110 }
111
Ratan Guptafa70dc92017-01-17 00:09:04 +0530112}
Ratan Guptaa149ba12017-01-17 00:32:32 +0530113close $fh;
114
Ratan Guptafa70dc92017-01-17 00:09:04 +0530115#------------------------------------END OF MAIN-----------------------
116
Ratan Guptad92101d2017-02-15 19:40:39 +0530117# Get all the associated types
118sub getAllAssociatedTypes
119{
120 my $fruTypeConfig = $_[0];
121 my @assoTypes;
122 while (my($key, $value) = each %$fruTypeConfig) {
123 #if the key exist and value is also there
124 if (defined $value->{'AssociatedTypes'}) {
125 my $assoTypes = $value->{'AssociatedTypes'};
126 for my $type (@$assoTypes) {
127 push(@assoTypes,$type);
128 }
129 }
130 }
131 return @assoTypes;
132}
133
Ratan Guptaa149ba12017-01-17 00:32:32 +0530134#Get the metdata for the incoming frutype from the loaded config file.
135#Write the FRU data into the output file
136
137sub writeToFile
138{
139 my $fruType = $_[0];#fru type
140 my $instancePath = $_[1];#instance Path
141 my $fruTypeConfig = $_[2];#loaded config file (frutypes)
142 my $fh = $_[3];#file Handle
143 #walk over all the fru types and match for the incoming type
Ratan Gupta8b34b332018-01-24 16:42:06 +0530144
Ratan Guptaa149ba12017-01-17 00:32:32 +0530145 print $fh " ".$instancePath.":";
146 print $fh "\n";
Ratan Gupta8b34b332018-01-24 16:42:06 +0530147 print $fh " "."entityID: ".$fruTypeConfig->{$fruType}->{'EntityID'};
148 print $fh "\n";
149 print $fh " "."entityInstance: ";
150 print $fh $entityInfoDict{$fruTypeConfig->{$fruType}->{'EntityID'}};
151 print $fh "\n";
152 print $fh " "."interfaces:";
153 print $fh "\n";
154
Ratan Guptad92101d2017-02-15 19:40:39 +0530155 my $interfaces = $fruTypeConfig->{$fruType}->{'Interfaces'};
Ratan Gupta8b34b332018-01-24 16:42:06 +0530156
Ratan Guptaa149ba12017-01-17 00:32:32 +0530157 #Walk over all the interfaces as it needs to be written
158 while ( my ($interface,$properties) = each %{$interfaces}) {
Ratan Gupta8b34b332018-01-24 16:42:06 +0530159 print $fh " ".$interface.":";
Ratan Guptaa149ba12017-01-17 00:32:32 +0530160 print $fh "\n";
161 #walk over all the properties as it needs to be written
162 while ( my ($dbusProperty,$metadata) = each %{$properties}) {
163 #will write property named "Property" first then
164 #other properties.
Ratan Gupta8b34b332018-01-24 16:42:06 +0530165 print $fh " ".$dbusProperty.":";
Ratan Guptaa149ba12017-01-17 00:32:32 +0530166 print $fh "\n";
167 for my $key (sort keys %{$metadata}) {
Ratan Gupta8b34b332018-01-24 16:42:06 +0530168 print $fh " $key: "."$metadata->{$key}";
Ratan Guptaa149ba12017-01-17 00:32:32 +0530169 print $fh "\n";
170 }
171 }
172 }
173}
174
Ratan Guptafa70dc92017-01-17 00:09:04 +0530175# Usage
176sub printUsage
177{
178 print "
Ratan Guptaa149ba12017-01-17 00:32:32 +0530179 $0 -i [MRW filename] -m [MetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptafa70dc92017-01-17 00:09:04 +0530180Options:
181 -d = debug mode
Santosh Puranik51041fc2019-10-08 09:45:15 -0500182 --skip-broken-mrw = Skip broken MRW targets
Ratan Guptafa70dc92017-01-17 00:09:04 +0530183 \n";
184 exit(1);
185}
186
187# Helper function to put debug statements.
188sub printDebug
189{
190 my $str = shift;
191 print "DEBUG: ", $str, "\n" if $debug;
192}
Ratan Guptad92101d2017-02-15 19:40:39 +0530193