Remove cards from inventory when part of a module

Cards, even if listed as FRUs, aren't need in the inventory
separately when they have a child chip, like a CPU or GPU,
that is also in the inventory.

So, don't need motherboard/cpu-module-card
when we have motherboard/cpu-module-card/cpu

Change-Id: Ia0445eee38b379685bf78cd2f0cbf68307071d1e
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/Inventory.pm b/Inventory.pm
index beaaaee..8015f66 100644
--- a/Inventory.pm
+++ b/Inventory.pm
@@ -10,6 +10,9 @@
 #FRU = field replaceable unit, CRU = customer replaceable unit
 my %RU_TYPES = (FRU => 1, CRU => 1);
 
+#Chips that are modeled as modules (card-chip together)
+my %MODULE_TYPES = (PROC => 1, GPU => 1);
+
 #Returns an array of hashes that represents the inventory
 #for a system.  The hash elements are:
 #TARGET:  The MRW target of the item
@@ -26,6 +29,8 @@
 
     findItems($targetObj, \@inventory);
 
+    pruneModuleCards($targetObj, \@inventory);
+
     return @inventory;
 }
 
@@ -60,6 +65,40 @@
 }
 
 
+#Removes entries from the inventory for the card target of a module.
+#Needed because processors and GPUs are modeled as a package which
+#is a card-chip instance that plugs into a connector on the
+#backplane/processor card.  Since we already include the chip target
+#in the inventory (that's how we can identify what it is), we don't
+#need the entry for the card target.
+#
+#For example, we'll already have .../module-0/proc-0 so we don't
+#need a separate .../module-0 entry.
+sub pruneModuleCards
+{
+    my ($targetObj, $inventory) = @_;
+    my @toRemove;
+
+    #Find the parent (a card) of items of type %type
+    for my $item (@$inventory) {
+
+        if (exists $MODULE_TYPES{$targetObj->getType($item->{TARGET})}) {
+            my $card = $targetObj->getTargetParent($item->{TARGET});
+            push @toRemove, $card;
+        }
+    }
+
+    #Remove these parent cards
+    for my $c (@toRemove) {
+        for my $i (0 .. (scalar @$inventory) - 1) {
+            if ($c eq $inventory->[$i]{TARGET}) {
+                splice(@$inventory, $i, 1);
+                last;
+            }
+        }
+    }
+}
+
 1;
 
 =head1 NAME