U-boot script update with new inventory object

Updated new inventory objects in u-boot fw_env scripts.

Change-Id: I9545f29f603e2fc46424d44df45ca82379df5ecb
Signed-off-by: Dinesh Chinari <chinari@us.ibm.com>
diff --git a/pyinventorymgr/sync_inventory_items.py b/pyinventorymgr/sync_inventory_items.py
index d92c3df..5fcca42 100644
--- a/pyinventorymgr/sync_inventory_items.py
+++ b/pyinventorymgr/sync_inventory_items.py
@@ -15,21 +15,23 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-
 import sys
 import os
 import dbus
+import uuid
 import argparse
 import subprocess
+import obmc.mapper
+import shutil
 
-
-INV_DBUS_NAME = 'org.openbmc.Inventory'
-INV_INTF_NAME = 'org.openbmc.InventoryItem'
+INV_INTF_NAME = 'xyz.openbmc_project.Inventory.Item.NetworkInterface'
 NET_DBUS_NAME = 'org.openbmc.NetworkManager'
 NET_OBJ_NAME = '/org/openbmc/NetworkManager/Interface'
 CHS_DBUS_NAME = 'org.openbmc.control.Chassis'
+CHS_INTF_NAME = 'xyz.openbmc_project.Common.UUID'
 CHS_OBJ_NAME = '/org/openbmc/control/chassis0'
 PROP_INTF_NAME = 'org.freedesktop.DBus.Properties'
+INVENTORY_ROOT = '/xyz/openbmc_project/inventory'
 
 FRUS = {}
 
@@ -39,22 +41,61 @@
 MAC_LOCALLY_ADMIN_MASK = 0x20000000000
 
 
-# Get the inventory dbus path based on the requested fru
-def get_inv_obj_path(fru_type, fru_name):
-    obj_path = ''
-    for f in FRUS.keys():
-        import obmc.inventory
-        if (FRUS[f]['fru_type'] == fru_type and f.endswith(fru_name)):
-            obj_path = f.replace("<inventory_root>", obmc.inventory.INVENTORY_ROOT)
-    return obj_path
+# Get inventory MACAddress value.
+def get_bmc_mac_address(bus, prop):
+    mapper = obmc.mapper.Mapper(bus)
+
+    # Get the inventory subtree, limited
+    # to objects that implement NetworkInterface.
+    for path, info in \
+        mapper.get_subtree(
+            path=INVENTORY_ROOT,
+            interfaces=[INV_INTF_NAME]).iteritems():
+
+            # Find a NetworkInterface with 'bmc' in the path.
+            if 'bmc' not in path:
+                continue
+
+            # Only expecting a single service to implement
+            # NetworkInterface.  Get the service connection
+            # from the mapper response
+            conn = info.keys()[0]
+
+            # Get the inventory object implementing NetworkInterface.
+            obj = bus.get_object(conn, path)
+
+            # Get the MAC address
+            mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME)
+            return mproxy(INV_INTF_NAME, prop)
 
 
-# Get the inventory property value
-def get_inv_value(obj, prop_name):
-    value = ''
-    dbus_method = obj.get_dbus_method("Get", PROP_INTF_NAME)
-    value = dbus_method(INV_INTF_NAME, prop_name)
-    return value
+# Get inventory UUID value.
+def get_uuid(bus, prop):
+    mapper = obmc.mapper.Mapper(bus)
+
+    # Get the inventory subtree, limited
+    # to objects that implement UUID.
+    resp = mapper.get_subtree(
+        path=INVENTORY_ROOT,
+        interfaces=[CHS_INTF_NAME])
+
+    # Only expecting a single object to implement UUID.
+    try:
+        path, info = resp.items()[0]
+    except IndexError as e:
+        return None
+
+    # Only expecting a single service to implement
+    # UUID.  Get the service connection
+    # from the mapper response
+    conn = info.keys()[0]
+
+    # Get the inventory object implementing UUID.
+    obj = bus.get_object(conn, path)
+
+    # Get the uuid
+    mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME)
+    return mproxy(CHS_INTF_NAME, prop)
 
 
 # Get the value of the mac on the system (from u-boot) without ':' separators
@@ -88,14 +129,6 @@
         dbus_method("eth0", mac_str)
 
 
-# Get sys uuid
-def get_sys_uuid(obj):
-    sys_uuid = ''
-    dbus_method = obj.get_dbus_method("Get", PROP_INTF_NAME)
-    sys_uuid = dbus_method(CHS_DBUS_NAME, "uuid")
-    return sys_uuid
-
-
 # Set sys uuid, this reboots the BMC for the value to take effect
 def set_sys_uuid(uuid):
     rc = subprocess.call(["fw_setenv", "uuid", uuid])
@@ -109,48 +142,28 @@
 
 if __name__ == '__main__':
     arg = argparse.ArgumentParser()
-    arg.add_argument('-t')
-    arg.add_argument('-n')
     arg.add_argument('-p')
     arg.add_argument('-s')
 
     opt = arg.parse_args()
-    fru_type = opt.t
-    fru_name = opt.n
     prop_name = opt.p
     sync_type = opt.s
 
-    inventory = os.path.join(
-        sys.prefix, 'share', 'inventory', 'inventory.json')
-    if os.path.exists(inventory):
-        import json
-        with open(inventory, 'r') as f:
-            try:
-                inv = json.load(f)
-            except ValueError:
-                print "Invalid JSON detected in " + inventory
-            else:
-                FRUS = inv
-    else:
-        import obmc_system_config as System
-        FRUS = System.FRU_INSTANCES
-
     bus = dbus.SystemBus()
-    inv_obj_path = get_inv_obj_path(fru_type, fru_name)
-    inv_obj = bus.get_object(INV_DBUS_NAME, inv_obj_path)
-    net_obj = bus.get_object(NET_DBUS_NAME, NET_OBJ_NAME)
-    chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME)
-
-    # Get the value of the requested inventory property
-    inv_value = get_inv_value(inv_obj, prop_name)
-
     if sync_type == "mac":
-        sys_mac = get_sys_mac(net_obj)
-        if inv_value != sys_mac:
-            sync_mac(net_obj, inv_value, sys_mac)
+            inv_mac = get_bmc_mac_address(bus, prop_name)
+            if inv_mac:
+                net_obj = bus.get_object(NET_DBUS_NAME, NET_OBJ_NAME)
+                sys_mac = get_sys_mac(net_obj)
+                if inv_mac != sys_mac:
+                    sync_mac(net_obj, inv_mac, sys_mac)
     elif sync_type == "uuid":
-        sys_uuid = get_sys_uuid(chs_obj)
-        if inv_value != sys_uuid:
-            set_sys_uuid(inv_value)
+            inv_uuid = get_uuid(bus, prop_name)
+            if inv_uuid:
+                inv_uuid = uuid.UUID(inv_uuid)
+                chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME)
+                chs_uuid = get_sys_uuid(chs_obj)
+                if inv_uuid != sys_uuid:
+                    set_sys_uuid(inv_uuid)
 
 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4