xref: /openbmc/skeleton/pyinventorymgr/inventory_items.py (revision 14c8286a7e4e81481e1387238ce1efaf72ea3e14)
1#!/usr/bin/env python
2
3import gobject
4import dbus
5import dbus.service
6import dbus.mainloop.glib
7import obmc.dbuslib.propertycacher as PropertyCacher
8from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
9
10try:
11    import obmc_system_config as System
12    have_system = True
13except ImportError:
14    have_system = False
15
16INTF_NAME = 'org.openbmc.InventoryItem'
17DBUS_NAME = 'org.openbmc.Inventory'
18
19if have_system:
20    FRUS = System.FRU_INSTANCES
21else:
22    FRUS = {}
23
24
25class Inventory(DbusProperties, DbusObjectManager):
26    def __init__(self, bus, name):
27        DbusProperties.__init__(self)
28        DbusObjectManager.__init__(self)
29        dbus.service.Object.__init__(self, bus, name)
30
31
32class InventoryItem(DbusProperties):
33    def __init__(self, bus, name, data):
34        DbusProperties.__init__(self)
35        dbus.service.Object.__init__(self, bus, name)
36
37        self.name = name
38
39        if 'present' not in data:
40            data['present'] = 'False'
41        if 'fault' not in data:
42            data['fault'] = 'False'
43        if 'version' not in data:
44            data['version'] = ''
45
46        self.SetMultiple(INTF_NAME, data)
47
48        ## this will load properties from cache
49        PropertyCacher.load(name, INTF_NAME, self.properties)
50
51    @dbus.service.method(
52        INTF_NAME, in_signature='a{sv}', out_signature='')
53    def update(self, data):
54        self.SetMultiple(INTF_NAME, data)
55        PropertyCacher.save(self.name, INTF_NAME, self.properties)
56
57    @dbus.service.method(
58        INTF_NAME, in_signature='s', out_signature='')
59    def setPresent(self, present):
60        self.Set(INTF_NAME, 'present', present)
61        PropertyCacher.save(self.name, INTF_NAME, self.properties)
62
63    @dbus.service.method(
64        INTF_NAME, in_signature='s', out_signature='')
65    def setFault(self, fault):
66        self.Set(INTF_NAME, 'fault', fault)
67        PropertyCacher.save(self.name, INTF_NAME, self.properties)
68
69
70def getVersion():
71    version = "Error"
72    with open('/etc/os-release', 'r') as f:
73        for line in f:
74            p = line.rstrip('\n')
75            parts = line.rstrip('\n').split('=')
76            if (parts[0] == "VERSION_ID"):
77                version = parts[1]
78                version = version.strip('"')
79    return version
80
81
82if __name__ == '__main__':
83    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
84    bus = get_dbus()
85    mainloop = gobject.MainLoop()
86    obj_parent = Inventory(bus, '/org/openbmc/inventory')
87    INVENTORY_FILE = os.path.join(sys.prefix, 'share',
88                                  'inventory', 'inventory.json')
89
90    if os.path.exists(INVENTORY_FILE):
91        with open(INVENTORY_FILE, 'r') as f:
92            try:
93                inv = json.load(f)
94            except ValueError:
95                print "Invalid JSON detected in " + INVENTORY_FILE
96            else:
97                FRUS = inv
98
99    for f in FRUS.keys():
100        obj_path = f.replace("<inventory_root>", System.INVENTORY_ROOT)
101        obj = InventoryItem(bus, obj_path, FRUS[f])
102        obj_parent.add(obj_path, obj)
103
104        ## TODO:  this is a hack to update bmc inventory item with version
105        ## should be done by flash object
106        if (FRUS[f]['fru_type'] == "BMC"):
107            version = getVersion()
108            obj.update({'version': version})
109
110    obj_parent.unmask_signals()
111    name = dbus.service.BusName(DBUS_NAME, bus)
112    print "Running Inventory Manager"
113    mainloop.run()
114