1*b4edc27aSAndrew Geissler#!/usr/bin/env perl
265ca8e19SAndrew Geissler
358825916STom Josephuse strict;
458825916STom Josephuse warnings;
558825916STom Joseph
658825916STom Josephuse mrw::Targets;
758825916STom Josephuse mrw::Inventory;
858825916STom Josephuse mrw::Util;
958825916STom Josephuse Getopt::Long; # For parsing command line arguments
1058825916STom Josephuse YAML::Tiny qw(LoadFile);
1158825916STom Joseph
1258825916STom Joseph# Globals
1358825916STom Josephmy $serverwizFile  = "";
1458825916STom Josephmy $debug          = 0;
1558825916STom Josephmy $outputFile     = "";
1658825916STom Josephmy $metaDataFile   = "";
1751041fc8SSantosh Puranikmy $skipBrokenMrw  = 0;
1858825916STom Joseph
1958825916STom Joseph# Command line argument parsing
2058825916STom JosephGetOptions(
2158825916STom Joseph"i=s" => \$serverwizFile,    # string
2258825916STom Joseph"m=s" => \$metaDataFile,     # string
2358825916STom Joseph"o=s" => \$outputFile,       # string
2451041fc8SSantosh Puranik"skip-broken-mrw" => \$skipBrokenMrw,
2558825916STom Joseph"d"   => \$debug,
2658825916STom Joseph)
2758825916STom Josephor printUsage();
2858825916STom Joseph
2958825916STom Josephif (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
3058825916STom Joseph{
3158825916STom Joseph    printUsage();
3258825916STom Joseph}
3358825916STom Joseph
3458825916STom Josephmy $targetObj = Targets->new;
3558825916STom Joseph$targetObj->loadXML($serverwizFile);
3658825916STom Joseph
3758825916STom Joseph# Open the MRW xml and the Metadata file for the sensor.
3858825916STom Joseph# Get the IPMI sensor information based on the Entity ID and Sensor Type.
3958825916STom Joseph# Fetch the Sensor ID, Event/Reading Type and Object Path from MRW.
4058825916STom Joseph# Get the Sensor Type and Offset from the metadata file.
4158825916STom Joseph# Merge and generate an output YAML with inventory object path as the key.
4258825916STom Joseph
4358825916STom Josephopen(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
4458825916STom Josephmy $metaDataConfig = LoadFile($metaDataFile);
4558825916STom Joseph
4658825916STom Josephmy @interestedTypes = keys %{$metaDataConfig};
4758825916STom Josephmy %types;
4858825916STom Joseph
4958825916STom Joseph@types{@interestedTypes} = ();
5058825916STom Joseph
5158825916STom Josephmy @inventory = Inventory::getInventory($targetObj);
5258825916STom Joseph#Process all the targets in the XML
5358825916STom Josephforeach my $target (sort keys %{$targetObj->getAllTargets()})
5458825916STom Joseph{
5558825916STom Joseph    my $sensorID = '';
5658825916STom Joseph    my $sensorType = '';
5758825916STom Joseph    my $eventReadingType = '';
5858825916STom Joseph    my $path = '';
5958825916STom Joseph    my $obmcPath = '';
6058825916STom Joseph    my $entityID = '';
6158825916STom Joseph    my $base = "/xyz/openbmc_project/inventory";
6258825916STom Joseph
6358825916STom Joseph    if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
6458825916STom Joseph
655102e57bSBrad Bishop        $sensorID = getNumeric($targetObj, $target, "IPMI_SENSOR_ID");
665102e57bSBrad Bishop        $sensorType = getNumeric($targetObj, $target, "IPMI_SENSOR_TYPE");
675102e57bSBrad Bishop        $eventReadingType = getNumeric(
685102e57bSBrad Bishop            $targetObj, $target, "IPMI_SENSOR_READING_TYPE");
6958825916STom Joseph        $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
705102e57bSBrad Bishop        $entityID = getNumeric($targetObj, $target, "IPMI_ENTITY_ID");
7158825916STom Joseph
7258825916STom Joseph        # Look only for the interested Entity ID & Sensor Type
7358825916STom Joseph        next if (not exists $types{$entityID});
7458825916STom Joseph        next if ($sensorType ne $metaDataConfig->{$entityID}->{SensorType});
7558825916STom Joseph
7658825916STom Joseph        #if there is ipmi sensor without sensorid or sensorReadingType or
7758825916STom Joseph        #Instance path then die
7858825916STom Joseph
7958825916STom Joseph        if ($sensorID eq '' or $eventReadingType eq '' or $path eq '') {
8051041fc8SSantosh Puranik            next if $skipBrokenMrw;
8158825916STom Joseph            close $fh;
8258825916STom Joseph            die("sensor without info for target=$target");
8358825916STom Joseph        }
8458825916STom Joseph
8558825916STom Joseph        # Removing the string "instance:" from path
8658825916STom Joseph        $path =~ s/^instance:/\//;
8758825916STom Joseph        $obmcPath = Util::getObmcName(\@inventory, $path);
8858825916STom Joseph
8958825916STom Joseph        # If unable to get the obmc path then die
9058825916STom Joseph        if (not defined $obmcPath) {
9158825916STom Joseph            close $fh;
9258825916STom Joseph            die("Unable to get the obmc path for path=$path");
9358825916STom Joseph        }
9458825916STom Joseph
9558825916STom Joseph        $base .= $obmcPath;
9658825916STom Joseph
9758825916STom Joseph        print $fh $base.":"."\n";
9858825916STom Joseph        print $fh "  sensorID: ".$sensorID."\n";
9958825916STom Joseph        print $fh "  sensorType: ".$sensorType."\n";
10058825916STom Joseph        print $fh "  eventReadingType: ".$eventReadingType."\n";
10158825916STom Joseph        print $fh "  offset: ".$metaDataConfig->{$entityID}->{Offset}."\n";
10258825916STom Joseph
10358825916STom Joseph        printDebug("$sensorID : $sensorType : $eventReadingType : $entityID : $metaDataConfig->{$entityID}->{Offset}")
10458825916STom Joseph    }
10558825916STom Joseph}
10658825916STom Josephclose $fh;
10758825916STom Joseph
1085102e57bSBrad Bishopsub getNumeric
1095102e57bSBrad Bishop{
1105102e57bSBrad Bishop    my $obj = shift;
1115102e57bSBrad Bishop    my $target = shift;
1125102e57bSBrad Bishop    my $attr = shift;
1135102e57bSBrad Bishop    my $val = $obj->getAttribute($target, $attr);
1145102e57bSBrad Bishop    $val = oct($val) if $val =~ /^0/;
1155102e57bSBrad Bishop    return $val;
1165102e57bSBrad Bishop}
1175102e57bSBrad Bishop
11858825916STom Joseph# Usage
11958825916STom Josephsub printUsage
12058825916STom Joseph{
12158825916STom Joseph    print "
12258825916STom Joseph    $0 -i [MRW filename] -m [SensorMetaData filename] -o [Output filename] [OPTIONS]
12358825916STom JosephOptions:
12458825916STom Joseph    -d = debug mode
12551041fc8SSantosh Puranik    --skip-broken-mrw = Skip broken MRW targets
12658825916STom Joseph        \n";
12758825916STom Joseph    exit(1);
12858825916STom Joseph}
12958825916STom Joseph
13058825916STom Joseph# Helper function to put debug statements.
13158825916STom Josephsub printDebug
13258825916STom Joseph{
13358825916STom Joseph    my $str = shift;
13458825916STom Joseph    print "DEBUG: ", $str, "\n" if $debug;
13558825916STom Joseph}
136