Remove non-FRU card segments from Inventory path.

If a card in the middle of the name hierarchy
isn't a FRU, we can remove it.

For example, motherboard/non-fru-card/fru-card
will be motherboard/fru-card.

Change-Id: Idf7348401f787c7ce2a58270b50c117714899a85
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/Inventory.pm b/Inventory.pm
index 91ed2b6..705e3e1 100644
--- a/Inventory.pm
+++ b/Inventory.pm
@@ -115,6 +115,44 @@
 
     #Don't need the card instance of a PROC/GPU module
     removeModuleFromPath($targetObj, $inventory);
+
+    #Don't need card segments for non-FRUs
+    removeNonFRUCardSegments($targetObj, $inventory);
+}
+
+
+#Removes non-FRU cards in the middle of a hierarchy from OBMC_NAME.
+#For example, .../motherboard/fanriser-0/fan-0 ->
+# .../motherboard/fan-0 when fanriser-0 isn't a FRU.
+sub removeNonFRUCardSegments
+{
+    my ($targetObj, $inventory) = @_;
+
+    for my $item (@$inventory) {
+
+        #Split the target into segments, then start
+        #adding segments in to make new targets so we can
+        #make API calls on the segment instances.
+        my @segments = split('/', $item->{TARGET});
+        my $target = "";
+        for my $s (@segments) {
+            next if (length($s) == 0);
+
+            $target .= "/$s";
+
+            my $class = $targetObj->getAttribute($target, "CLASS");
+            next if ($class ne "CARD");
+
+            my $ruType = $targetObj->getAttribute($target, "RU_TYPE");
+
+            #If this segment is a card but not a FRU,
+            #remove it from the path.
+            if (not exists $RU_TYPES{$ruType}) {
+                my $segment = $targetObj->getInstanceName($target);
+                $item->{OBMC_NAME} =~ s/\b$segment-\d+\b\///;
+            }
+        }
+    }
 }