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