blob: 94286f214b09fe06f3a5f1fe9cd17dc4f9238454 [file] [log] [blame]
Andrew Geisslerb4edc272022-07-19 11:24:45 -04001#!/usr/bin/env perl
Andrew Geissler65ca8e12022-07-16 11:36:14 -04002
Ratan Guptafa70dc92017-01-17 00:09:04 +05303use strict;
4use warnings;
5
6use mrw::Targets;
7use mrw::Inventory;
Deepak Kodihalli6225e932017-02-07 12:14:55 -06008use mrw::Util;
Ratan Guptafa70dc92017-01-17 00:09:04 +05309use Getopt::Long; # For parsing command line arguments
Brad Bishop550075b2017-01-30 20:27:04 -050010use YAML::Tiny qw(LoadFile);
Ratan Guptafa70dc92017-01-17 00:09:04 +053011# Globals
12my $serverwizFile = "";
13my $debug = 0;
Ratan Guptaa149ba12017-01-17 00:32:32 +053014my $outputFile = "";
15my $metaDataFile = "";
Santosh Puranik51041fc2019-10-08 09:45:15 -050016my $skipBrokenMrw = 0;
Ratan Guptafa70dc92017-01-17 00:09:04 +053017
18# Command line argument parsing
19GetOptions(
20"i=s" => \$serverwizFile, # string
Ratan Guptaa149ba12017-01-17 00:32:32 +053021"m=s" => \$metaDataFile, # string
22"o=s" => \$outputFile, # string
Santosh Puranik51041fc2019-10-08 09:45:15 -050023"skip-broken-mrw" => \$skipBrokenMrw,
Ratan Guptafa70dc92017-01-17 00:09:04 +053024"d" => \$debug,
25)
26or printUsage();
27
Ratan Guptaa149ba12017-01-17 00:32:32 +053028if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptafa70dc92017-01-17 00:09:04 +053029{
30 printUsage();
31}
32
33my $targetObj = Targets->new;
34$targetObj->loadXML($serverwizFile);
35
Ratan Guptaa149ba12017-01-17 00:32:32 +053036#open the mrw xml and the metaData file for the fru.
37#Fetch the FRU id,type,object path from the mrw.
38#Get the metadata for that fru from the metadata file.
39#Merge the data into the outputfile
40
41open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
42my $fruTypeConfig = LoadFile($metaDataFile);
43
44my @interestedTypes = keys %{$fruTypeConfig};
45my %types;
46@types{@interestedTypes} = ();
Ratan Guptafa70dc92017-01-17 00:09:04 +053047
Ratan Gupta8b34b332018-01-24 16:42:06 +053048my %entityInfoDict;
49
Ratan Guptad92101d2017-02-15 19:40:39 +053050my @allAssoTypes = getAllAssociatedTypes($fruTypeConfig);
51my %allAssoTypesHash;
52@allAssoTypesHash{@allAssoTypes} = ();
53
Ratan Guptafa70dc92017-01-17 00:09:04 +053054my @inventory = Inventory::getInventory($targetObj);
55for my $item (@inventory) {
56 my $isFru = 0, my $fruID = 0, my $fruType = "";
Ratan Gupta8b34b332018-01-24 16:42:06 +053057 my $entityInstance = 1, my $entityID = 0;
58
Ratan Gupta26cc0552017-01-17 00:44:17 +053059 #Fetch the FRUID.
Ratan Guptafa70dc92017-01-17 00:09:04 +053060 if (!$targetObj->isBadAttribute($item->{TARGET}, "FRU_ID")) {
61 $fruID = $targetObj->getAttribute($item->{TARGET}, "FRU_ID");
62 $isFru = 1;
63 }
Ratan Gupta26cc0552017-01-17 00:44:17 +053064 #Fetch the FRU Type.
Ratan Guptafa70dc92017-01-17 00:09:04 +053065 if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) {
66 $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE");
67 }
68
Ratan Guptad92101d2017-02-15 19:40:39 +053069 #Skip if any one is true
70 #1) If not fru
71 #2) if the fru type is not there in the config file.
72 #3) if the fru type is in associated types.
Santosh Puranik51041fc2019-10-08 09:45:15 -050073 #4) if FRU_ID is not defined for the target and we are asked to ignore such
74 # targets.
75
76 next if ($skipBrokenMrw and ($fruID eq ""));
Ratan Guptad92101d2017-02-15 19:40:39 +053077
78 next if (not $isFru or not exists $types{$fruType} or exists $allAssoTypesHash{$fruType});
Ratan Guptafa70dc92017-01-17 00:09:04 +053079
80 printDebug ("FRUID => $fruID, FRUType => $fruType, ObjectPath => $item->{OBMC_NAME}");
81
Ratan Guptaa149ba12017-01-17 00:32:32 +053082 print $fh $fruID.":";
83 print $fh "\n";
84
Ratan Gupta8b34b332018-01-24 16:42:06 +053085 $entityID = $fruTypeConfig->{$fruType}->{'EntityID'};
86
87 if (exists $entityInfoDict{$entityID}) {
88 $entityInstance = $entityInfoDict{$entityID} + 1;
89 }
90
91 printDebug("entityID => $entityID , entityInstance => $entityInstance");
92
93 $entityInfoDict{$entityID} = $entityInstance;
94
Ratan Guptaa149ba12017-01-17 00:32:32 +053095 writeToFile($fruType,$item->{OBMC_NAME},$fruTypeConfig,$fh);
96
Ratan Guptad92101d2017-02-15 19:40:39 +053097 #if the key(AssociatedTypes) exists and it is defined
98 #then make the association.
Ratan Gupta26cc0552017-01-17 00:44:17 +053099
Ratan Guptad92101d2017-02-15 19:40:39 +0530100 if (!defined $fruTypeConfig->{$fruType}->{'AssociatedTypes'}) {
101 next;
Ratan Gupta26cc0552017-01-17 00:44:17 +0530102 }
Ratan Guptad92101d2017-02-15 19:40:39 +0530103
104 my $assoTypes = $fruTypeConfig->{$fruType}->{'AssociatedTypes'};
105 for my $type (@$assoTypes) {
106 my @devices = Util::getDevicePath(\@inventory,$targetObj,$type);
107 for my $device (@devices) {
108 writeToFile($type,$device,$fruTypeConfig,$fh);
109 }
110
111 }
112
Ratan Guptafa70dc92017-01-17 00:09:04 +0530113}
Ratan Guptaa149ba12017-01-17 00:32:32 +0530114close $fh;
115
Ratan Guptafa70dc92017-01-17 00:09:04 +0530116#------------------------------------END OF MAIN-----------------------
117
Ratan Guptad92101d2017-02-15 19:40:39 +0530118# Get all the associated types
119sub getAllAssociatedTypes
120{
121 my $fruTypeConfig = $_[0];
122 my @assoTypes;
123 while (my($key, $value) = each %$fruTypeConfig) {
124 #if the key exist and value is also there
125 if (defined $value->{'AssociatedTypes'}) {
126 my $assoTypes = $value->{'AssociatedTypes'};
127 for my $type (@$assoTypes) {
128 push(@assoTypes,$type);
129 }
130 }
131 }
132 return @assoTypes;
133}
134
Ratan Guptaa149ba12017-01-17 00:32:32 +0530135#Get the metdata for the incoming frutype from the loaded config file.
136#Write the FRU data into the output file
137
138sub writeToFile
139{
140 my $fruType = $_[0];#fru type
141 my $instancePath = $_[1];#instance Path
142 my $fruTypeConfig = $_[2];#loaded config file (frutypes)
143 my $fh = $_[3];#file Handle
144 #walk over all the fru types and match for the incoming type
Ratan Gupta8b34b332018-01-24 16:42:06 +0530145
Ratan Guptaa149ba12017-01-17 00:32:32 +0530146 print $fh " ".$instancePath.":";
147 print $fh "\n";
Ratan Gupta8b34b332018-01-24 16:42:06 +0530148 print $fh " "."entityID: ".$fruTypeConfig->{$fruType}->{'EntityID'};
149 print $fh "\n";
150 print $fh " "."entityInstance: ";
151 print $fh $entityInfoDict{$fruTypeConfig->{$fruType}->{'EntityID'}};
152 print $fh "\n";
153 print $fh " "."interfaces:";
154 print $fh "\n";
155
Ratan Guptad92101d2017-02-15 19:40:39 +0530156 my $interfaces = $fruTypeConfig->{$fruType}->{'Interfaces'};
Ratan Gupta8b34b332018-01-24 16:42:06 +0530157
Ratan Guptaa149ba12017-01-17 00:32:32 +0530158 #Walk over all the interfaces as it needs to be written
159 while ( my ($interface,$properties) = each %{$interfaces}) {
Ratan Gupta8b34b332018-01-24 16:42:06 +0530160 print $fh " ".$interface.":";
Ratan Guptaa149ba12017-01-17 00:32:32 +0530161 print $fh "\n";
162 #walk over all the properties as it needs to be written
163 while ( my ($dbusProperty,$metadata) = each %{$properties}) {
164 #will write property named "Property" first then
165 #other properties.
Ratan Gupta8b34b332018-01-24 16:42:06 +0530166 print $fh " ".$dbusProperty.":";
Ratan Guptaa149ba12017-01-17 00:32:32 +0530167 print $fh "\n";
168 for my $key (sort keys %{$metadata}) {
Ratan Gupta8b34b332018-01-24 16:42:06 +0530169 print $fh " $key: "."$metadata->{$key}";
Ratan Guptaa149ba12017-01-17 00:32:32 +0530170 print $fh "\n";
171 }
172 }
173 }
174}
175
Ratan Guptafa70dc92017-01-17 00:09:04 +0530176# Usage
177sub printUsage
178{
179 print "
Ratan Guptaa149ba12017-01-17 00:32:32 +0530180 $0 -i [MRW filename] -m [MetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptafa70dc92017-01-17 00:09:04 +0530181Options:
182 -d = debug mode
Santosh Puranik51041fc2019-10-08 09:45:15 -0500183 --skip-broken-mrw = Skip broken MRW targets
Ratan Guptafa70dc92017-01-17 00:09:04 +0530184 \n";
185 exit(1);
186}
187
188# Helper function to put debug statements.
189sub printDebug
190{
191 my $str = shift;
192 print "DEBUG: ", $str, "\n" if $debug;
193}
Ratan Guptad92101d2017-02-15 19:40:39 +0530194