OCC Active: MRW parser updates for accepting multiple yamls

The MRW parser for IPMI sensor is updated to accept
multiple yamls and generate output sensor yaml.

Change-Id: I623071fe491b098d005215045acbf155ad5077a1
Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
diff --git a/gen_ipmi_sensor.pl b/gen_ipmi_sensor.pl
index 742da9b..8dceaf4 100755
--- a/gen_ipmi_sensor.pl
+++ b/gen_ipmi_sensor.pl
@@ -12,18 +12,18 @@
 my $serverwizFile  = "";
 my $debug           = 0;
 my $outputFile     = "";
-my $metaDataFile   = "";
+my $metaDir   = "";
 
 # Command line argument parsing
 GetOptions(
 "i=s" => \$serverwizFile,    # string
-"m=s" => \$metaDataFile,     # string
+"m=s" => \$metaDir,     # string
 "o=s" => \$outputFile,       # string
 "d"   => \$debug,
 )
 or printUsage();
 
-if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDataFile eq ""))
+if (($serverwizFile eq "") or ($outputFile eq "") or ($metaDir eq ""))
 {
     printUsage();
 }
@@ -36,8 +36,26 @@
 #Get the metadata for that sensor from the metadata file.
 #Merge the data into the outputfile
 
+my $sensorTypeConfig;
+my $tmpSensor;
+opendir my $dir,$metaDir or die "Cannot open directory: $!";
+my @files = readdir $dir;
+foreach my $file (@files){
+    my ($filename,$extn) = split(/\.([^\.]+)$/,$file);
+    if((defined($extn)) and ( $extn eq "yaml")) {
+        my $metaDataFile = $metaDir."/".$file;
+        my $tmpSensor = LoadFile($metaDataFile);
+        if(!keys %{$sensorTypeConfig}) {
+            %{$sensorTypeConfig} = %{$tmpSensor};
+        }
+        else {
+            %{$sensorTypeConfig} = (%{$sensorTypeConfig},%{$tmpSensor});
+        }
+    }
+}
+
+
 open(my $fh, '>', $outputFile) or die "Could not open file '$outputFile' $!";
-my $sensorTypeConfig = LoadFile($metaDataFile);
 
 my @interestedTypes = keys %{$sensorTypeConfig};
 my %types;
@@ -79,9 +97,25 @@
         #removing the string "instance:" from path
         $path =~ s/^instance:/\//;
 
-        $obmcPath = Util::getObmcName(\@inventory, $path);
+        #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) {
+            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 unable to get the obmc path then die
         if (not defined $obmcPath) {
             close $fh;
             die("Unable to get the obmc path for path=$path");
@@ -107,10 +141,12 @@
     my ($sensorType,$sensorReadingType,$path,$sensorTypeConfig,$fh) = @_;
     print $fh "  sensorType: ".$sensorType."\n";
     print $fh "  path: ".$path."\n";
+
     print $fh "  sensorReadingType: ".$sensorReadingType."\n";
+    print $fh "  updateInterface: ".$sensorTypeConfig->{$sensorType}->{"updateInterface"}."\n";
     print $fh "  interfaces:"."\n";
 
-    my $interfaces = $sensorTypeConfig->{$sensorType};
+    my $interfaces = $sensorTypeConfig->{$sensorType}->{"interfaces"};
     #Walk over all the interfces as it needs to be written
     while (my ($interface,$properties) = each %{$interfaces}) {
         print $fh "    ".$interface.":\n";
@@ -120,9 +156,9 @@
                     #other properties.
             print $fh "      ".$dbusProperty.":\n";
             while (my ( $offset,$values) = each %{$dbusPropertyValue}) {
-                print $fh "        $offset:\n";
+                print $fh "          $offset:\n";
                 while (my ( $key,$value) = each %{$values})  {
-                    print $fh "          $key: ". $value."\n";
+                    print $fh "            $key: ". $value."\n";
                 }
             }
         }