blob: 16252bf1bbca3a46636b6e4719101e539e3e6d41 [file] [log] [blame]
Ratan Guptafa70dc92017-01-17 00:09:04 +05301#! /usr/bin/perl
2use 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 = "";
Ratan Guptafa70dc92017-01-17 00:09:04 +053015
16# Command line argument parsing
17GetOptions(
18"i=s" => \$serverwizFile, # string
Ratan Guptaa149ba12017-01-17 00:32:32 +053019"m=s" => \$metaDataFile, # string
20"o=s" => \$outputFile, # string
Ratan Guptafa70dc92017-01-17 00:09:04 +053021"d" => \$debug,
22)
23or printUsage();
24
Ratan Guptaa149ba12017-01-17 00:32:32 +053025if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
Ratan Guptafa70dc92017-01-17 00:09:04 +053026{
27 printUsage();
28}
29
30my $targetObj = Targets->new;
31$targetObj->loadXML($serverwizFile);
32
Ratan Guptaa149ba12017-01-17 00:32:32 +053033#open the mrw xml and the metaData file for the fru.
34#Fetch the FRU id,type,object path from the mrw.
35#Get the metadata for that fru from the metadata file.
36#Merge the data into the outputfile
37
38open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
39my $fruTypeConfig = LoadFile($metaDataFile);
40
41my @interestedTypes = keys %{$fruTypeConfig};
42my %types;
43@types{@interestedTypes} = ();
Ratan Guptafa70dc92017-01-17 00:09:04 +053044
Ratan Guptad92101d2017-02-15 19:40:39 +053045my @allAssoTypes = getAllAssociatedTypes($fruTypeConfig);
46my %allAssoTypesHash;
47@allAssoTypesHash{@allAssoTypes} = ();
48
Ratan Guptafa70dc92017-01-17 00:09:04 +053049my @inventory = Inventory::getInventory($targetObj);
50for my $item (@inventory) {
51 my $isFru = 0, my $fruID = 0, my $fruType = "";
Ratan Gupta26cc0552017-01-17 00:44:17 +053052 #Fetch the FRUID.
Ratan Guptafa70dc92017-01-17 00:09:04 +053053 if (!$targetObj->isBadAttribute($item->{TARGET}, "FRU_ID")) {
54 $fruID = $targetObj->getAttribute($item->{TARGET}, "FRU_ID");
55 $isFru = 1;
56 }
Ratan Gupta26cc0552017-01-17 00:44:17 +053057 #Fetch the FRU Type.
Ratan Guptafa70dc92017-01-17 00:09:04 +053058 if (!$targetObj->isBadAttribute($item->{TARGET}, "TYPE")) {
59 $fruType = $targetObj->getAttribute($item->{TARGET}, "TYPE");
60 }
61
Ratan Guptad92101d2017-02-15 19:40:39 +053062 #Skip if any one is true
63 #1) If not fru
64 #2) if the fru type is not there in the config file.
65 #3) if the fru type is in associated types.
66
67 next if (not $isFru or not exists $types{$fruType} or exists $allAssoTypesHash{$fruType});
Ratan Guptafa70dc92017-01-17 00:09:04 +053068
69 printDebug ("FRUID => $fruID, FRUType => $fruType, ObjectPath => $item->{OBMC_NAME}");
70
Ratan Guptaa149ba12017-01-17 00:32:32 +053071 print $fh $fruID.":";
72 print $fh "\n";
73
74 writeToFile($fruType,$item->{OBMC_NAME},$fruTypeConfig,$fh);
75
Ratan Guptad92101d2017-02-15 19:40:39 +053076 #if the key(AssociatedTypes) exists and it is defined
77 #then make the association.
Ratan Gupta26cc0552017-01-17 00:44:17 +053078
Ratan Guptad92101d2017-02-15 19:40:39 +053079 if (!defined $fruTypeConfig->{$fruType}->{'AssociatedTypes'}) {
80 next;
Ratan Gupta26cc0552017-01-17 00:44:17 +053081 }
Ratan Guptad92101d2017-02-15 19:40:39 +053082
83 my $assoTypes = $fruTypeConfig->{$fruType}->{'AssociatedTypes'};
84 for my $type (@$assoTypes) {
85 my @devices = Util::getDevicePath(\@inventory,$targetObj,$type);
86 for my $device (@devices) {
87 writeToFile($type,$device,$fruTypeConfig,$fh);
88 }
89
90 }
91
Ratan Guptafa70dc92017-01-17 00:09:04 +053092}
Ratan Guptaa149ba12017-01-17 00:32:32 +053093close $fh;
94
Ratan Guptafa70dc92017-01-17 00:09:04 +053095#------------------------------------END OF MAIN-----------------------
96
Ratan Guptad92101d2017-02-15 19:40:39 +053097# Get all the associated types
98sub getAllAssociatedTypes
99{
100 my $fruTypeConfig = $_[0];
101 my @assoTypes;
102 while (my($key, $value) = each %$fruTypeConfig) {
103 #if the key exist and value is also there
104 if (defined $value->{'AssociatedTypes'}) {
105 my $assoTypes = $value->{'AssociatedTypes'};
106 for my $type (@$assoTypes) {
107 push(@assoTypes,$type);
108 }
109 }
110 }
111 return @assoTypes;
112}
113
Ratan Guptaa149ba12017-01-17 00:32:32 +0530114#Get the metdata for the incoming frutype from the loaded config file.
115#Write the FRU data into the output file
116
117sub writeToFile
118{
119 my $fruType = $_[0];#fru type
120 my $instancePath = $_[1];#instance Path
121 my $fruTypeConfig = $_[2];#loaded config file (frutypes)
122 my $fh = $_[3];#file Handle
123 #walk over all the fru types and match for the incoming type
124 print $fh " ".$instancePath.":";
125 print $fh "\n";
Ratan Guptad92101d2017-02-15 19:40:39 +0530126 my $interfaces = $fruTypeConfig->{$fruType}->{'Interfaces'};
Ratan Guptaa149ba12017-01-17 00:32:32 +0530127 #Walk over all the interfaces as it needs to be written
128 while ( my ($interface,$properties) = each %{$interfaces}) {
129 print $fh " ".$interface.":";
130 print $fh "\n";
131 #walk over all the properties as it needs to be written
132 while ( my ($dbusProperty,$metadata) = each %{$properties}) {
133 #will write property named "Property" first then
134 #other properties.
135 print $fh " ".$dbusProperty.":";
136 print $fh "\n";
137 for my $key (sort keys %{$metadata}) {
138 print $fh " $key: "."$metadata->{$key}";
139 print $fh "\n";
140 }
141 }
142 }
143}
144
Ratan Guptafa70dc92017-01-17 00:09:04 +0530145# Usage
146sub printUsage
147{
148 print "
Ratan Guptaa149ba12017-01-17 00:32:32 +0530149 $0 -i [MRW filename] -m [MetaData filename] -o [Output filename] [OPTIONS]
Ratan Guptafa70dc92017-01-17 00:09:04 +0530150Options:
151 -d = debug mode
152 \n";
153 exit(1);
154}
155
156# Helper function to put debug statements.
157sub printDebug
158{
159 my $str = shift;
160 print "DEBUG: ", $str, "\n" if $debug;
161}
Ratan Guptad92101d2017-02-15 19:40:39 +0530162