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