summaryrefslogtreecommitdiffstats
path: root/inventory.pl
blob: 75c42410f549adf09b67011d1b9c352a3020f03e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#!/usr/bin/env perl

#Creates the OpenBMC inventory from ServerWiz output XML.
#Basically, the inventory includes anything with a FRU name,
#plus some other specific things we always look for since we
#need more than just FRUs.


use strict;
use XML::Simple;
use mrw::Targets;
use Getopt::Long;
use JSON;

my $serverwizFile;
my $outputFile, my $target;
my $fruName, my $type;
my @items, my %item, my %inventory;

#Ensure we never pick these up
my %skipFRUTypes = (OCC => 1);

#We always want the targets with these Types
my %includedTypes = ("CORE" => 1);

#We always want the targets with these MRW Types
my %includedTargetTypes = ("chip-sp-bmc" => 1,
                           "chip-apss-psoc" => 1);

#These are never considered FRUs
my %notFRUTypes = ("CORE" => 1);

GetOptions("x=s" => \$serverwizFile,
           "o=s" => \$outputFile)
or printUsage();

if ((not defined $serverwizFile) || (not defined $outputFile)) {
    printUsage();
}

my $targetObj = Targets->new;
$targetObj->loadXML($serverwizFile);

foreach $target (sort keys %{ $targetObj->getAllTargets() })
{
    $type = $targetObj->getType($target);
    if (exists $skipFRUTypes{$type}) {
        next;
    }

    $fruName = "";

    if (!$targetObj->isBadAttribute($target, "FRU_NAME")) {
        $fruName = $targetObj->getAttribute($target,"FRU_NAME");
    }

    my $targetType = $targetObj->getTargetType($target);

    #We're looking for FRUs, and a few other required parts
    if (($fruName ne "") || (exists $includedTargetTypes{$targetType}) ||
        (exists $includedTypes{$type}))
    {
        $item{name} = $target;
        $item{orig_name} = $target;
        $item{fru_type} = $type;
        $item{target_type} = $targetType;

        if (($fruName ne "") && (not exists $notFRUTypes{$type})) {
            $item{is_fru} = 1;
        } else {
            $item{is_fru} = 0;
        }
        push @items, { %item };
    }

}

#Hardcode the entries that will never be in the MRW
#TODO: openbmc/openbmc#596 Remove when BIOS version is stored elsewhere.
$inventory{'<inventory_root>/system/bios'} =
    {is_fru => 1, fru_type => 'SYSTEM'};

#TODO: openbmc/openbmc#597 Remove when misc FRU data is stored elsewhere.
$inventory{'<inventory_root>/system/misc'} =
    {is_fru => 0, fru_type => 'SYSTEM'};

transform(\@items, \%inventory);

#Encode in JSON and write it out
my $json = JSON->new;
$json->indent(1);
$json->canonical(1);
my $text = $json->encode(\%inventory);

open(FILE, ">$outputFile") or die "Unable to create $outputFile\n";
print FILE $text;
close FILE;

print "Created $outputFile\n";


#Apply OpenBMC naming conventions to the Serverwiz names
sub transform
{
    my $items = shift @_;
    my $inventory = shift @_;

    removeConnectors($items);

    removeProcModule($items);

    renameSegmentWithType("PROC", "cpu", $items);

    renameSegmentWithType("SYS", "system", $items);
    renameType("SYS", "SYSTEM", $items);

    renameSegmentWithType("NODE", "chassis", $items);
    renameType("NODE", "SYSTEM", $items);

    renameSegmentWithTargetType("card-motherboard", "motherboard", $items);
    renameTypeWithTargetType("card-motherboard", "MAIN_PLANAR", $items);

    renameType("MEMBUF", "MEMORY_BUFFER", $items);

    renameType("FSP", "BMC", $items);

    removeCoreParentChiplet($items);

    removeInstNumIfOneInstPresent($items);

    removeHyphensFromInstanceNum($items);

    for my $i (@$items) {
        my $name = "<inventory_root>".$i->{name};
        delete $i->{name};
        delete $i->{orig_name};
        delete $i->{target_type};
        $inventory{$name} = { %$i };
    }
}


#Renames a segment in all target names based on its type
#
#For example:
#    renameSegmentWithType("PROC", "foo", $items)
#  would change
#    /sys-0/node-0/motherboard-0/module-0/cpu/core0
#  to
#    /sys-0/node-0/motherboard-0/module-0/foo/core0
#  assuming /sys-0/.../cpu had type PROC.
sub renameSegmentWithType
{
    my $type = shift @_;
    my $newSegment = shift @_;
    my $items = shift @_;
    my %segmentsToRename;

    for my $item (@$items) {
        my @segments = split('/', $item->{orig_name});
        my $target = "";
        for my $s (@segments) {
            if (length($s) > 0) {
                $target .= "/$s";
                my $curType = "";
                if (!$targetObj->isBadAttribute($target, "TYPE")) {
                    $curType = $targetObj->getType($target);
                }
                if ($curType eq $type) {
                    if (not defined $segmentsToRename{$target}) {
                        my ($oldSegment) = $target =~ /\b(\w+)(-\d+)?$/;
                        $segmentsToRename{$target}{old} = $oldSegment;
                        $segmentsToRename{$target}{new} = $newSegment;
                    }
                }
             }
        }
    }

    for my $s (keys %segmentsToRename) {
        for my $item (@$items) {
            $item->{name} =~
                s/$segmentsToRename{$s}{old}/$segmentsToRename{$s}{new}/;
        }
    }
}


#Renames a segment in all target names based on its target type
#
#For example:
#    renameSegmentWithType("PROC", "foo", $items)
#  would change
#    /sys-0/node-0/motherboard-0/module-0/cpu/core0
#  to
#    /sys-0/node-0/motherboard-0/module-0/foo/core0
#  assuming /sys-0/.../cpu had target type PROC.
sub renameSegmentWithTargetType
{
    my $type = shift @_;
    my $newSegment = shift @_;
    my $items = shift @_;
    my %segmentsToRename;

    for my $item (@$items) {
        my @segments = split('/', $item->{orig_name});
        my $target = "";
        for my $s (@segments) {
            if (length($s) > 0) {
                $target .= "/$s";
                my $curType = $targetObj->getTargetType($target);
                if ($curType eq $type) {
                    if (not defined $segmentsToRename{$target}) {
                        my ($oldSegment) = $target =~ /\b(\w+)(-\d+)?$/;
                        $segmentsToRename{$target}{old} = $oldSegment;
                        $segmentsToRename{$target}{new} = $newSegment;
                    }
                }
             }
        }
    }

    for my $s (keys %segmentsToRename) {
        for my $item (@$items) {
            $item->{name} =~
                s/$segmentsToRename{$s}{old}/$segmentsToRename{$s}{new}/;
        }
    }
}


#Remove the core's parent chiplet, after moving
#the chiplet's instance number to the core.
#Note: Serverwiz always puts the core on a chiplet
sub removeCoreParentChiplet
{
    my $items = shift @_;

    for my $item (@$items) {
        if ($item->{fru_type} eq "CORE") {
            $item->{name} =~ s/\w+-(\d+)\/(\w+)-\d+$/$2-$1/;
        }
    }
}


#Remove path segments that are connectors
sub removeConnectors
{
    my $items = shift @_;
    my %connectors;
    my $item;

    for $item (@$items) {
        my @segments = split('/', $item->{name});
        my $target = "";
        for my $s (@segments) {
            if (length($s) > 0) {
                $target .= "/$s";
                my $class = $targetObj->getAttribute($target, "CLASS");
                if ($class eq "CONNECTOR") {
                    if (not exists $connectors{$target}) {
                        $connectors{$target} = 1;
                    }
                }
            }
        }
    }

    #remove the connector segments out of the path
    #Reverse sort so we start with connectors further out
    for my $connector (sort {$b cmp $a} keys %connectors) {
        for $item (@$items) {
            if ($item->{name} =~ /$connector\b/) {
                my ($inst) = $connector =~ /-(\d+)$/;
                my ($card) = $item->{name};
                $card =~ s/^$connector\///;

                #add the connector instance to the child card
                $card =~ s/^(\w+)-\d+/$1-$inst/;

                #remove the connector segment from the path
                my $base = $connector;
                $base =~ s/\w+-\d+$//;
                $item->{name} = $base . $card;
            }
        }
    }
}


#Remove the processor module card from the path name.
#Note: Serverwiz always outputs proc_socket-X/module-Y/proc.
#      where proc_socket, module, and proc can be any name
#      We already transormed it to module-X/proc.
#      Our use requires proc-X.
#      Note: No multichip modules in plan for OpenPower systems.
sub removeProcModule
{
    my $items = shift @_;
    my $procName = "";

    #Find the name of the processor used in this model
    for my $item (@$items) {
        if ($item->{fru_type} eq "PROC") {
            ($procName) = $item->{name} =~ /\b(\w+)$/;
            last;
        }
    }

    #Now remove it from every instance that it's a part of
    if ($procName eq "") {
        print "Could not find the name of the processor in this system\n";
    } else {
        for my $item (@$items) {
            $item->{name} =~ s/\w+-(\d+)\/$procName/$procName-$1/;
        }
    }
}


sub renameType
{
    my $old = shift @_;
    my $new = shift @_;
    my $items = shift @_;

    for my $item (@$items) {
        $item->{fru_type} =~ s/$old/$new/;
    }
}


sub renameTypeWithTargetType
{
    my $targetType = shift@_;
    my $newType = shift @_;
    my $items = shift @_;

    for my $item (@$items) {
        if ($item->{target_type} eq $targetType) {
            $item->{fru_type} = $newType;
        }
    }
}


sub removeHyphensFromInstanceNum
{
    my $items = shift @_;

    for my $item (@$items) {
        $item->{name} =~ s/-(\d+)\b/$1/g;
    }
}


sub renameSegment
{
    my $old = shift @_;
    my $new = shift @_;
    my $items = shift @_;

    for my $item (@$items) {
      $item->{name} =~ s/\b$old\b/$new/;
    }
}


sub removeInstNumIfOneInstPresent
{
    my $items = shift @_;
    my %instanceHash;
    my $segment, my $item;

    for $item (@$items) {
        my @segments = split('/', $item->{name});

        for $segment (@segments) {
            my ($s, $inst) = $segment =~ /(\w+)-(\d+)/;
            if (defined $s) {
                if (not exists $instanceHash{$s}) {
                    $instanceHash{$s}{inst} = $inst;
                }
                else {
                    if ($instanceHash{$s}{inst} ne $inst) {
                        $instanceHash{$s}{keep} = 1;
                    }
                }
            }
        }
    }

    for my $segment (keys %instanceHash) {

        if (not exists $instanceHash{$segment}{keep}) {
            for $item (@$items) {
               $item->{name} =~ s/$segment-\d+/$segment/;
            }
        }
    }
}


sub printUsage
{
    print "inventory.pl -x [XML filename] -o [output filename]\n";
    exit(1);
}
OpenPOWER on IntegriCloud