gen_path_callouts: Write callouts to JSON file

Save all of the Callout objects to a JSON file.  This is where the FRU
callout has its duplicates removed and is sorted based on priority.

This also adds a new Util module function getLocationCode to find the
location code of a target.

The JSON file is organized into sections for each callout type,
with keys based on the type.  I2c uses a bus and address, FSI uses a
link, and SPI uses a bus number. If FSI is combined with I2C or SPI,
then the link plus the I2C/SPI keys is used.  Multi-hop FSI links are
indicated by a dash in between the links, eg "0-1".  A contrived example
of an entry in the FSI section, on link 5, is:

"FSI":
{
   "5":
   {
      "Callouts":[
        {
           "Priority":"H"
           "LocationCode": "P1-C50",
           "MRU":"/sys-0/node-0/motherboard/proc_socket-0/module-0/power9-0",
           "Name":"/sys-0/node-0/motherboard/cpu0"
        },
        {
           "Priority":"M",
           "LocationCode": "P1-C42",
           "MRU":"/sys-0/node-0/motherboard/ebmc-card/BMC-0",
           "Name":"/sys-0/node-0/motherboard/ebmc-card"
        },
        {
           "Priority":"L",
           "LocationCode": "P1",
           "Name":"/sys-0/node-0/motherboard"
        }
     ],
     "Dest":"/sys-0/node-0/motherboard-0/proc_socket-0/module-0/power9-0",
     "Source":"/sys-0/node-0/motherboard-0/ebmc-card-connector-0/card-0/bmc-0"
   }
}

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: Iee3493834d5045c553a52c80bbb62f7aa71140e0
diff --git a/Util.pm b/Util.pm
index ea6b914..d555637 100644
--- a/Util.pm
+++ b/Util.pm
@@ -149,6 +149,61 @@
     return $port - 1;
 }
 
+# Get the location code for the target passed in, like P1-C5.
+#  $targets = the targets object
+#  $target = target to get the location code of
+sub getLocationCode
+{
+    my ($targets, $passedInTarget) = @_;
+    my $locCode = undef;
+    my $target = $passedInTarget;
+    my $done = 0;
+
+    # Walk up the parent chain prepending segments until an absolute
+    # location code is found.
+    while (!$done)
+    {
+        if (!$targets->isBadAttribute($target, "LOCATION_CODE"))
+        {
+            my $loc = $targets->getAttribute($target, "LOCATION_CODE");
+            my $type = $targets->getAttribute($target, "LOCATION_CODE_TYPE");
+
+            if (length($loc) > 0 )
+            {
+                if (defined $locCode)
+                {
+                    $locCode = $loc . '-' . $locCode;
+                }
+                else
+                {
+                    $locCode = $loc;
+                }
+
+                if ($type eq "ABSOLUTE")
+                {
+                    $done = 1;
+                }
+            }
+            else
+            {
+                die "Missing location code on $target\n" if $type eq "ABSOLUTE";
+            }
+        }
+
+        if (!$done)
+        {
+            $target = $targets->getTargetParent($target);
+            if (not defined $target)
+            {
+                die "Did not find complete location " .
+                    "code for $passedInTarget\n";
+            }
+        }
+    }
+
+    return $locCode;
+}
+
 1;
 
 =head1 NAME
@@ -200,6 +255,10 @@
 
 Returns C<I2CPort> converted from MRW numbering scheme to Linux numbering scheme.
 
+=item getLocationCode(C<TargetsObj, C<Target>)
+
+Gets the location code for the input Target.
+
 =back
 
 =cut