Rename some inventory path segments based on type

Certain segments have standard naming, like one
with a type of NODE will always be called chassis.

Change-Id: I30eee555aeae010715000a6f521300793908e78c
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/Inventory.pm b/Inventory.pm
index 705e3e1..19dccdb 100644
--- a/Inventory.pm
+++ b/Inventory.pm
@@ -118,6 +118,17 @@
 
     #Don't need card segments for non-FRUs
     removeNonFRUCardSegments($targetObj, $inventory);
+
+    #Certain parts have standard naming
+    renameSegmentWithType("PROC", "cpu", $targetObj, $inventory);
+    renameSegmentWithType("SYS", "system", $targetObj, $inventory);
+    renameSegmentWithType("NODE", "chassis", $targetObj, $inventory);
+
+    #Make sure the motherboard is called 'motherboard' in the OBMC_NAME.
+    #The only way to identify it is with its TARGET_TYPE,
+    #which is different than the regular type.
+    renameSegmentWithTargetType("card-motherboard", "motherboard",
+                                $targetObj, $inventory);
 }
 
 
@@ -191,6 +202,41 @@
 }
 
 
+#Renames segments of the paths to the name passed in
+#based on the type of the segment.
+#For example:
+# renameSegmentWithType("PROC", "cpu", ...);
+# With a target of:
+#   motherboard-0/myp9proc-5/core-3
+# Where target motherboard-0/myp9proc-5 has a type
+# of PROC, gives:
+#   motherboard-0/cpu-5/core-3
+sub renameSegmentWithType
+{
+    my ($type, $newSegment, $targetObj, $inventory) = @_;
+
+    #Find the targets for all the segments, and
+    #if their type matches what we're looking for, then
+    #save it so we can rename them later.
+    for my $item (@$inventory) {
+
+        my @segments = split('/', $item->{TARGET});
+        my $target = "";
+
+        for my $s (@segments) {
+            next if (length($s) == 0);
+
+            $target .= "/$s";
+            my $curType = $targetObj->getType($target);
+            next unless ($curType eq $type);
+
+            my $oldSegment = $targetObj->getInstanceName($target);
+            $item->{OBMC_NAME} =~ s/$oldSegment/$newSegment/;
+        }
+    }
+}
+
+
 #Removes the card portion of a module from OBMC_NAME.
 #For example, .../motherboard-0/module-1/proc-0 ->
 #.../motherboard-0/proc-1.
@@ -218,6 +264,32 @@
     }
 }
 
+
+#The same as renameSegmentWithType, but finds the segment
+#to rename by calling Targets::getTargetType() on it
+#instead of Targets::getType().
+sub renameSegmentWithTargetType
+{
+    my ($type, $newSegment, $targetObj, $inventory) = @_;
+
+    for my $item (@$inventory) {
+
+        my @segments = split('/', $item->{TARGET});
+        my $target = "";
+
+        for my $s (@segments) {
+            next if (length($s) == 0);
+
+            $target .= "/$s";
+            my $curType = $targetObj->getTargetType($target);
+            next unless ($curType eq $type);
+
+            my $oldSegment = $targetObj->getInstanceName($target);
+            $item->{OBMC_NAME} =~ s/$oldSegment/$newSegment/;
+        }
+    }
+}
+
 1;
 
 =head1 NAME