Fixes for mapping ipmi sensors to d-bus objects

- The input sensor yaml now uses the sensor instance name as the key,
  instead of the sensor type. This was required because two sensors with
  the same sensor type can map to different d-bus objects.
- The script has a work-around to map MRW occ paths to d-bus occ paths.
  Simplify this workaround and add a TODO to remove the workaround.

Change-Id: I155fbcfe11a0dcc456415d70a026fe00de84051b
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/gen_ipmi_sensor.pl b/gen_ipmi_sensor.pl
index 1d3a1dd..053cf33 100755
--- a/gen_ipmi_sensor.pl
+++ b/gen_ipmi_sensor.pl
@@ -53,9 +53,14 @@
     my $sensorReadingType = '';
     my $path = '';
     my $obmcPath = '';
+    my $sensorName = '';
 
     if ($targetObj->getTargetType($target) eq "unit-ipmi-sensor") {
 
+        $sensorName = $targetObj->getInstanceName($target);
+        #not interested in this sensortype
+        next if (not exists $types{$sensorName});
+
         $sensorID = $targetObj->getAttribute($target, "IPMI_SENSOR_ID");
 
         $sensorType = hex($targetObj->getAttribute($target,
@@ -66,9 +71,6 @@
 
         $path = $targetObj->getAttribute($target, "INSTANCE_PATH");
 
-        #not interested in this sensortype
-        next if (not exists $types{$sensorType});
-
         #if there is ipmi sensor without sensorid or sensorReadingType or
         #Instance path then die
 
@@ -77,32 +79,17 @@
             die("sensor without info for target=$target");
         }
 
-        #removing the string "instance:" from path
-        $path =~ s/^instance:/\//;
-
-        #get the last word from the path to check whether it is an occ or
-        #something without a proper instance path.
-        #if instance path is sys0 then get the path value from the yaml
-        #if it is a occ path, get the path from yaml and add the occ instance
-        #number to it.
-        $obmcPath = Util::getObmcName(\@inventory,$path);
-        #if unable to get the obmc path then get from yaml
-        if ((not defined $obmcPath) or ($obmcPath eq "/system")){
-            if ($path eq "/sys-0") {
-                $obmcPath = $sensorTypeConfig->{$sensorType}->{"path"};
-            }
-            else {
-                my @pathelements =split(/\//,$path);
-                foreach my $elem (@pathelements) {
-                    #split element-instance_number
-                    my ($elemName,$elemNum) = split(/-([^-]+)$/,$elem);
-                    if ((defined $elemName) and ($elemName eq "proc_socket")) {
-                        $obmcPath = $sensorTypeConfig->{$sensorType}->{"path"}."occ".$elemNum;
-                        last;
-                    }
-                }
-            }
+        if (exists $sensorTypeConfig->{$sensorName}{"path"}) {
+            $obmcPath = $sensorTypeConfig->{$sensorName}->{"path"};
         }
+        else {
+            #removing the string "instance:" from path
+            $path =~ s/^instance:/\//;
+            $obmcPath = Util::getObmcName(\@inventory,$path);
+        }
+
+        # TODO via openbmc/openbmc#2144 - this fixup shouldn't be needed.
+        $obmcPath = checkOccPathFixup($obmcPath);
 
         if (not defined $obmcPath) {
             close $fh;
@@ -112,12 +99,12 @@
         print $fh $sensorID.":\n";
 
         my $serviceInterface =
-            $sensorTypeConfig->{$sensorType}->{"serviceInterface"};
-        my $readingType = $sensorTypeConfig->{$sensorType}->{"readingType"};
+            $sensorTypeConfig->{$sensorName}->{"serviceInterface"};
+        my $readingType = $sensorTypeConfig->{$sensorName}->{"readingType"};
 
-        printDebug("$sensorID : $sensorType : $sensorReadingType :$obmcPath \n");
+        printDebug("$sensorID : $sensorName : $sensorType : $sensorReadingType :$obmcPath \n");
 
-        writeToFile($sensorType,$sensorReadingType,$obmcPath,$serviceInterface,
+        writeToFile($sensorName,$sensorType,$sensorReadingType,$obmcPath,$serviceInterface,
             $readingType,$sensorTypeConfig,$fh);
 
     }
@@ -131,7 +118,7 @@
 
 sub writeToFile
 {
-    my ($sensorType,$sensorReadingType,$path,$serviceInterface,
+    my ($sensorName,$sensorType,$sensorReadingType,$path,$serviceInterface,
         $readingType,$sensorTypeConfig,$fh) = @_;
 
     print $fh "  sensorType: ".$sensorType."\n";
@@ -142,7 +129,7 @@
     print $fh "  readingType: ".$readingType."\n";
     print $fh "  interfaces:"."\n";
 
-    my $interfaces = $sensorTypeConfig->{$sensorType}->{"interfaces"};
+    my $interfaces = $sensorTypeConfig->{$sensorName}->{"interfaces"};
     #Walk over all the interfces as it needs to be written
     while (my ($interface,$properties) = each %{$interfaces}) {
         print $fh "    ".$interface.":\n";
@@ -161,6 +148,19 @@
     }
 }
 
+# Convert MRW OCC inventory path to application d-bus path
+sub checkOccPathFixup
+{
+    my ($path) = @_;
+    if ("/system/chassis/motherboard/cpu0/occ" eq $path) {
+        return "/org/open_power/control/occ0";
+    }
+    if ("/system/chassis/motherboard/cpu1/occ" eq $path) {
+        return "/org/open_power/control/occ1";
+    }
+    return $path;
+}
+
 # Usage
 sub printUsage
 {