1*58825916STom Joseph#! /usr/bin/perl
2*58825916STom Josephuse strict;
3*58825916STom Josephuse warnings;
4*58825916STom Joseph
5*58825916STom Josephuse mrw::Targets;
6*58825916STom Josephuse mrw::Inventory;
7*58825916STom Josephuse mrw::Util;
8*58825916STom Josephuse Getopt::Long; # For parsing command line arguments
9*58825916STom Josephuse YAML::Tiny qw(LoadFile);
10*58825916STom Joseph
11*58825916STom Joseph# Globals
12*58825916STom Josephmy $serverwizFile  = "";
13*58825916STom Josephmy $debug          = 0;
14*58825916STom Josephmy $outputFile     = "";
15*58825916STom Josephmy $metaDataFile   = "";
16*58825916STom Joseph
17*58825916STom Joseph# Command line argument parsing
18*58825916STom JosephGetOptions(
19*58825916STom Joseph"i=s" => \$serverwizFile,    # string
20*58825916STom Joseph"m=s" => \$metaDataFile,     # string
21*58825916STom Joseph"o=s" => \$outputFile,       # string
22*58825916STom Joseph"d"   => \$debug,
23*58825916STom Joseph)
24*58825916STom Josephor printUsage();
25*58825916STom Joseph
26*58825916STom Josephif (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
27*58825916STom Joseph{
28*58825916STom Joseph    printUsage();
29*58825916STom Joseph}
30*58825916STom Joseph
31*58825916STom Josephmy $targetObj = Targets->new;
32*58825916STom Joseph$targetObj->loadXML($serverwizFile);
33*58825916STom Joseph
34*58825916STom Joseph# Open the MRW xml and the Metadata file for the sensor.
35*58825916STom Joseph# Get the IPMI sensor information based on the Entity ID and Sensor Type.
36*58825916STom Joseph# Fetch the Sensor ID, Event/Reading Type and Object Path from MRW.
37*58825916STom Joseph# Get the Sensor Type and Offset from the metadata file.
38*58825916STom Joseph# Merge and generate an output YAML with inventory object path as the key.
39*58825916STom Joseph
40*58825916STom Josephopen(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
41*58825916STom Josephmy $metaDataConfig = LoadFile($metaDataFile);
42*58825916STom Joseph
43*58825916STom Josephmy @interestedTypes = keys %{$metaDataConfig};
44*58825916STom Josephmy %types;
45*58825916STom Joseph
46*58825916STom Joseph@types{@interestedTypes} = ();
47*58825916STom Joseph
48*58825916STom Josephmy @inventory = Inventory::getInventory($targetObj);
49*58825916STom Joseph#Process all the targets in the XML
50*58825916STom Josephforeach my $target (sort keys %{$targetObj->getAllTargets()})
51*58825916STom Joseph{
52*58825916STom Joseph    my $sensorID = '';
53*58825916STom Joseph    my $sensorType = '';
54*58825916STom Joseph    my $eventReadingType = '';
55*58825916STom Joseph    my $path = '';
56*58825916STom Joseph    my $obmcPath = '';
57*58825916STom Joseph    my $entityID = '';
58*58825916STom Joseph    my $base = "/xyz/openbmc_project/inventory";
59*58825916STom Joseph
60*58825916STom Joseph    if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
61*58825916STom Joseph
62*58825916STom Joseph        $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
63*58825916STom Joseph        $sensorType = $targetObj->getAttribute($target, "IPMI_SENSOR_TYPE");
64*58825916STom Joseph        $eventReadingType = $targetObj->getAttribute($target,
65*58825916STom Joseph                             "IPMI_SENSOR_READING_TYPE");
66*58825916STom Joseph        $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
67*58825916STom Joseph        $entityID = $targetObj->getAttribute($target, "IPMI_ENTITY_ID");
68*58825916STom Joseph
69*58825916STom Joseph        # Look only for the interested Entity ID & Sensor Type
70*58825916STom Joseph        next if (not exists $types{$entityID});
71*58825916STom Joseph        next if ($sensorType ne $metaDataConfig->{$entityID}->{SensorType});
72*58825916STom Joseph
73*58825916STom Joseph        #if there is ipmi sensor without sensorid or sensorReadingType or
74*58825916STom Joseph        #Instance path then die
75*58825916STom Joseph
76*58825916STom Joseph        if ($sensorID eq '' or $eventReadingType eq '' or $path eq '') {
77*58825916STom Joseph            close $fh;
78*58825916STom Joseph            die("sensor without info for target=$target");
79*58825916STom Joseph        }
80*58825916STom Joseph
81*58825916STom Joseph        # Removing the string "instance:" from path
82*58825916STom Joseph        $path =~ s/^instance:/\//;
83*58825916STom Joseph        $obmcPath = Util::getObmcName(\@inventory, $path);
84*58825916STom Joseph
85*58825916STom Joseph        # If unable to get the obmc path then die
86*58825916STom Joseph        if (not defined $obmcPath) {
87*58825916STom Joseph            close $fh;
88*58825916STom Joseph            die("Unable to get the obmc path for path=$path");
89*58825916STom Joseph        }
90*58825916STom Joseph
91*58825916STom Joseph        $base .= $obmcPath;
92*58825916STom Joseph
93*58825916STom Joseph        print $fh $base.":"."\n";
94*58825916STom Joseph        print $fh "  sensorID: ".$sensorID."\n";
95*58825916STom Joseph        print $fh "  sensorType: ".$sensorType."\n";
96*58825916STom Joseph        print $fh "  eventReadingType: ".$eventReadingType."\n";
97*58825916STom Joseph        print $fh "  offset: ".$metaDataConfig->{$entityID}->{Offset}."\n";
98*58825916STom Joseph
99*58825916STom Joseph        printDebug("$sensorID : $sensorType : $eventReadingType : $entityID : $metaDataConfig->{$entityID}->{Offset}")
100*58825916STom Joseph    }
101*58825916STom Joseph}
102*58825916STom Josephclose $fh;
103*58825916STom Joseph
104*58825916STom Joseph# Usage
105*58825916STom Josephsub printUsage
106*58825916STom Joseph{
107*58825916STom Joseph    print "
108*58825916STom Joseph    $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
109*58825916STom JosephOptions:
110*58825916STom Joseph    -d = debug mode
111*58825916STom Joseph        \n";
112*58825916STom Joseph    exit(1);
113*58825916STom Joseph}
114*58825916STom Joseph
115*58825916STom Joseph# Helper function to put debug statements.
116*58825916STom Josephsub printDebug
117*58825916STom Joseph{
118*58825916STom Joseph    my $str = shift;
119*58825916STom Joseph    print "DEBUG: ", $str, "\n" if $debug;
120*58825916STom Joseph}
121