blob: 818f2c48da4cdf775612a65e440a52f84a5af70a [file] [log] [blame]
Vishwanatha Subbannad0039432017-03-30 00:10:47 +05301#!/usr/bin/env perl
Vishwanatha Subbanna37066462016-12-13 17:06:18 +05302use strict;
3use warnings;
4
5use mrw::Targets; # Set of APIs allowing access to parsed ServerWiz2 XML output
6use mrw::Inventory; # To get list of Inventory targets
7use Getopt::Long; # For parsing command line arguments
8use Data::Dumper qw(Dumper); # Dumping blob
Vishwanatha Subbannad0039432017-03-30 00:10:47 +05309use List::Util qw(first);
Vishwanatha Subbanna37066462016-12-13 17:06:18 +053010
11# Globals
12my $force = 0;
13my $serverwizFile = "";
14my $debug = 0;
15my $outputFile = "";
16my $verbose = 0;
17
18# Command line argument parsing
19GetOptions(
20"f" => \$force, # numeric
21"i=s" => \$serverwizFile, # string
22"o=s" => \$outputFile, # string
23"d" => \$debug,
24"v" => \$verbose,
25)
26or printUsage();
27
28if (($serverwizFile eq "") or ($outputFile eq ""))
29{
30 printUsage();
31}
32
33# Hashmap of all the LED groups with the properties
34my %hashGroup;
35
36# hash of targets to Names that have the FRU Inventory instances
37my %invHash;
38
Vishwanatha Subbannae36476e2017-07-19 16:36:02 +053039# Hash of Enclosure Fault LED names and their properties
40# These are generally front-fault-led and rear-fault-led
41my %encFaults;
Vishwanatha Subbanna37066462016-12-13 17:06:18 +053042
Vishwanatha Subbannad0039432017-03-30 00:10:47 +053043# These groups are a must in all the systems.
44# Its fine if they don't map to any physical LED
45my @defaultGroup = ("BmcBooted", "PowerOn");
46
Vishwanatha Subbannac5e127d2017-05-15 12:08:42 +053047# This group contains all the LEDs with the action Blink
48my $lampTest = "LampTest";
49
Vishwanatha Subbanna37066462016-12-13 17:06:18 +053050# API used to access parsed XML data
51my $targetObj = Targets->new;
52if($verbose == 1)
53{
54 $targetObj->{debug} = 1;
55}
56
57if($force == 1)
58{
59 $targetObj->{force} = 1;
60}
61
62$targetObj->loadXML($serverwizFile);
63print "Loaded MRW XML: $serverwizFile \n";
64
65# Iterate over Inventory and get all the Inventory targets.
66my @inventory = Inventory::getInventory($targetObj);
67for my $item (@inventory)
68{
69 # Target to Obmc_Name hash.
70 $invHash{$item->{TARGET}} = $item->{OBMC_NAME};
71}
72
73# For debugging purpose.
74printDebug("\nList of Inventory targets\n");
75foreach my $key (sort keys %invHash)
76{
77 printDebug("$invHash{$key}\n");
78}
79
80# Process all the targets in the XML. If the target is associated with a FRU,
81# then remember it so that when we do the FRU inventory lookup, we know if
82# that Inventory has a LED associated with it or not.
83foreach my $target (sort keys %{$targetObj->getAllTargets()})
84{
85 # Some the target instances may *not* have this MRW_TYPE attribute.
86 if($targetObj->isBadAttribute($target, "MRW_TYPE"))
87 {
88 next;
89 }
90
91 # Return true if not populated -or- not present
92 if("LED" eq $targetObj->getMrwType($target))
93 {
94 # Just for clarity.
95 my $ledTarget = $target;
96
97 # OBMC_NAME field of the FRU
98 # fruPath ex /system/chassis/motherboard/dimm1
99 # device "dimm1"
100 my $fruPath = '';
101 my $device = '';
102
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530103 # Find if this LED is associated with a FRU.
104 # Example, FAN will have LED on that assembly.
105 my $conns = $targetObj->findConnections($ledTarget, "LOGICAL_ASSOCIATION");
106 if ($conns ne "")
107 {
108 # This LED is associated with a FRU
109 for my $conn (@{$conns->{CONN}})
110 {
111 my $destTarget = $conn->{DEST_PARENT};
112 # If we have found this, then that means, we do not need to
113 # hand cook a group name. delete this value from the inventory
114 # array
115 if(exists($invHash{$destTarget}))
116 {
117 # This will remove a particular {key, value} pair
118 $fruPath = $invHash{$destTarget};
119 printDebug("$destTarget : $fruPath is having associated LED\n");
120 delete ($invHash{$destTarget});
121 }
122 }
123 # fetch FruName from the device path
124 $device = getFruName($fruPath);
125 printDebug("$target; $device has device\n");
126 }
127
128 if($targetObj->isBadAttribute($ledTarget, "CONTROL_GROUPS"))
129 {
130 next;
131 }
132
Vishwanatha Subbannaa9660422017-05-05 16:06:21 +0530133 # By default, Blink takes higher priority
134 my $priority = "'Blink'";
135
136 # Get the priority. Since rest everything is populated,
137 # default to Blink than err'ing out. Not checking for
138 # validity of this since it must be present.
139 if($targetObj->getAttribute($ledTarget, "LED_PRIORITY") eq "ON")
140 {
141 $priority = "'On'";
142 }
143
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530144 # Need this to populate the table incase the device is empty
145 my $instance = $targetObj->getInstanceName($ledTarget);
146
Vishwanatha Subbannae36476e2017-07-19 16:36:02 +0530147 # All the fan instances have fan-fault-led and need to extract the
148 # real name. If not, then what's in the instance holds good
149 my $name = ($device eq '') ? $instance : $device;
150
151 # Get if this LED is a ENC-FAULT type.
152 if(!$targetObj->isBadAttribute($target, "LED_TYPE"))
153 {
154 if("ENC-FAULT" eq $targetObj->getAttribute($ledTarget, "LED_TYPE"))
155 {
156 $encFaults{$name} = $priority;
157 }
158 }
159
160 # Defines the LEDs and the Groups that they belong to
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530161 my $controlGroup = $targetObj->getAttribute($ledTarget, "CONTROL_GROUPS");
162
163 #remove spaces, because serverwiz isn't good at removing them itself
164 $controlGroup =~ s/\s//g;
165 my @groups= split(',', $controlGroup); #just a long 16x3 = 48 element list
166
167 for (my $i = 0; $i < scalar @groups; $i += 3)
168 {
169 if (($groups[$i] ne "NA") && ($groups[$i] ne ""))
170 {
171 my $groupName = $groups[$i];
172 printDebug("$groupName\n");
173
174 my $blinkFreq = $groups[$i+1];
175 my $action = "'On'";
176 my $period = 0;
177
178 # Period in milli seconds
179 my $dutyCycle = $groups[$i+2];
180 if($blinkFreq > 0)
181 {
182 $action = "'Blink'";
183 $period = (1 / $blinkFreq) * 1000;
184 }
185
186 # Insert into hash map;
Vishwanatha Subbannae36476e2017-07-19 16:36:02 +0530187 $hashGroup{$groupName}{$name}{"Action"} = $action;
188 $hashGroup{$groupName}{$name}{"Period"} = $period;
189 $hashGroup{$groupName}{$name}{"DutyOn"} = $dutyCycle;
190 $hashGroup{$groupName}{$name}{"Priority"} = $priority;
Vishwanatha Subbannac5e127d2017-05-15 12:08:42 +0530191
192 # Need to update the LampTest group.
Vishwanatha Subbannae36476e2017-07-19 16:36:02 +0530193 $hashGroup{$lampTest}{$name}{"Action"} = "'Blink'";
194 $hashGroup{$lampTest}{$name}{"Period"} = 1000;
195 $hashGroup{$lampTest}{$name}{"DutyOn"} = 50;
196
197 # Priority of a particular LED needs to stay SAME across
198 # all groups
199 $hashGroup{$lampTest}{$name}{"Priority"} = $priority;
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530200 }
201 } # Walk CONTROL_GROUP
202 } # Has LED target
203} # All the targets
204
205
206# These are the FRUs that do not have associated LEDs. All of these need to be
207# mapped to some group, which will be named after this target name and the
208# elements of the group are EnclosureFaults Front and Back
209printDebug("\n======================================================================\n");
210printDebug("\nFRUs that do not have associated LEDs\n");
211foreach my $key (sort keys %invHash)
212{
213 my $device = getFruName($invHash{$key});
214
215 # For each of these device, the Group record would be this :
216 my $groupName = $device . "Fault";
217 printDebug("$device :: $groupName\n");
218
219 # Setup roll-up LEDs to the ones that are of type ENC-FAULT
Vishwanatha Subbannae36476e2017-07-19 16:36:02 +0530220 foreach my $led (sort keys %encFaults)
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530221 {
Vishwanatha Subbannae36476e2017-07-19 16:36:02 +0530222 $hashGroup{$groupName}{$led}{"Action"} = "'On'";
223 $hashGroup{$groupName}{$led}{"Period"} = 0;
224 $hashGroup{$groupName}{$led}{"DutyOn"} = 50;
225
226 # Priority of a particular LED needs to stay SAME across
227 # all groups
228 $hashGroup{$groupName}{$led}{"Priority"} = $encFaults{$led};
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530229 }
230}
231printDebug("\n======================================================================\n");
232
233# Generate the yaml file
234generateYamlFile();
235#------------------------------------END OF MAIN-----------------------
236
237# Gven a '/' separated string, returns the leaf.
238# Ex: /a/b/c/d returns device=d
239sub getFruName
240{
241 my $path = shift;
242 my $device = '';
243 my $lastSlash=rindex($path, '/');
244 $device=substr($path, $lastSlash+1);
245}
246
247sub generateYamlFile
248{
249 my $fileName = $outputFile;
250 my $groupCopy = '';
251 my $ledCopy = '';
252 open(my $fh, '>', $fileName) or die "Could not open file '$fileName' $!";
253
254 foreach my $group (sort keys %hashGroup)
255 {
256 if($group ne $groupCopy)
257 {
Vishwanatha Subbannad0039432017-03-30 00:10:47 +0530258 # If one of these is a default group, then delete it from the array
259 # that is being maintained to create one by hand if all default ones
260 # are not defined
261 my $index = first {$defaultGroup[$_] eq $group} 0..$#defaultGroup;
262 if (defined $index)
263 {
264 splice @defaultGroup, $index, 1;
265 }
266
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530267 $groupCopy = '';
268 $ledCopy = '';
269 }
270
Vishwanatha Subbannac5e127d2017-05-15 12:08:42 +0530271 foreach my $led (sort keys %{ $hashGroup{$group} })
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530272 {
Vishwanatha Subbannac5e127d2017-05-15 12:08:42 +0530273 foreach my $property (sort keys %{ $hashGroup{$group}{$led}})
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530274 {
275 if($group ne $groupCopy)
276 {
277 $groupCopy = $group;
278 print $fh "$group:\n";
279 }
280 print $fh " ";
281 if($led ne $ledCopy)
282 {
283 $ledCopy = $led;
284 print $fh "$led:\n";
285 print $fh " ";
286 }
287 print $fh " ";
288 print $fh "$property:";
289 print $fh " $hashGroup{$group}{$led}{$property}\n";
290 }
291 }
292 }
Vishwanatha Subbannad0039432017-03-30 00:10:47 +0530293 # If we need to hand create some of the groups, do so now.
294 foreach my $name (@defaultGroup)
295 {
296 print $fh "$name:\n";
297 }
Vishwanatha Subbanna37066462016-12-13 17:06:18 +0530298 close $fh;
299}
300
301# Helper function to put debug statements.
302sub printDebug
303{
304 my $str = shift;
305 print "DEBUG: ", $str, "\n" if $debug;
306}
307
308# Usage
309sub printUsage
310{
311 print "
312 $0 -i [XML filename] -o [Output filename] [OPTIONS]
313Options:
314 -f = force output file creation even when errors
315 -d = debug mode
316 -v = verbose mode - for verbose o/p from Targets.pm
317
318PS: mrw::Targets can be found in https://github.com/open-power/serverwiz/
319 mrw::Inventory can be found in https://github.com/openbmc/phosphor-mrw-tools/
320 \n";
321 exit(1);
322}
323#------------------------------------END OF SUB-----------------------