blob: 5bdcd8de707f4381682fe362cd2fdaeb4d6ef439 [file] [log] [blame]
Brad Bishop732c6db2015-10-19 14:49:21 -04001#!/usr/bin/env python
2
3# Contributors Listed Below - COPYRIGHT 2015
4# [+] International Business Machines Corp.
5#
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16# implied. See the License for the specific language governing
17# permissions and limitations under the License.
18
19import sys
20import dbus
21import dbus.service
22import dbus.mainloop.glib
23import gobject
Brad Bishop54d3ec92015-11-03 09:32:34 -050024from OpenBMCMapper import Path, IntrospectionParser, PathTree
Brad Bishop732c6db2015-10-19 14:49:21 -040025import OpenBMCMapper
26
Brad Bishop732c6db2015-10-19 14:49:21 -040027class ObjectMapper(dbus.service.Object):
28 def __init__(self, bus, path,
Brad Bishop6fb84612015-11-01 00:06:24 -040029 name_match = OpenBMCMapper.org_dot_openbmc_match,
30 intf_match = OpenBMCMapper.org_dot_openbmc_match):
Brad Bishop732c6db2015-10-19 14:49:21 -040031 super(ObjectMapper, self).__init__(bus.dbus, path)
Brad Bishop54d3ec92015-11-03 09:32:34 -050032 self.cache = PathTree()
Brad Bishop732c6db2015-10-19 14:49:21 -040033 self.bus = bus
34 self.name_match = name_match
35 self.intf_match = intf_match
Brad Bishopa8e752f2015-11-03 09:24:48 -050036 self.tag_match = OpenBMCMapper.ListMatch(['children', 'interface'])
Brad Bishopcb7aa602015-11-03 10:40:17 -050037 self.service = None
Brad Bishop732c6db2015-10-19 14:49:21 -040038
39 gobject.idle_add(self.discover)
40 self.bus.dbus.add_signal_receiver(self.bus_handler,
41 dbus_interface = dbus.BUS_DAEMON_IFACE,
42 signal_name = 'NameOwnerChanged')
43 self.bus.dbus.add_signal_receiver(self.interfaces_added_handler,
44 dbus_interface = dbus.BUS_DAEMON_IFACE + '.ObjectManager',
45 signal_name = 'InterfaceesAdded',
46 sender_keyword = 'sender')
47 self.bus.dbus.add_signal_receiver(self.interfaces_removed_handler,
48 dbus_interface = dbus.BUS_DAEMON_IFACE + '.ObjectManager',
49 signal_name = 'InterfacesRemoved',
50 sender_keyword = 'sender')
51
Brad Bishopcb7aa602015-11-03 10:40:17 -050052 def discovery_pending(self):
53 return not bool(self.service)
54
Brad Bishop732c6db2015-10-19 14:49:21 -040055 def interfaces_added_handler(self, path, iprops, **kw):
Brad Bishopcb7aa602015-11-03 10:40:17 -050056 if self.discovery_pending():
57 return
Brad Bishop54d3ec92015-11-03 09:32:34 -050058 matches = [ x for x in iprops.iterkeys() if self.intf_match(x) ]
59 d = self.cache.setdefault(path, {})
60 d[path].setdefault(kw['sender'], []).extend(matches)
61 self.cache[path] = d
Brad Bishop732c6db2015-10-19 14:49:21 -040062
63 def interfaces_removed_handler(self, path, interfaces, **kw):
Brad Bishopcb7aa602015-11-03 10:40:17 -050064 if self.discovery_pending():
65 return
Brad Bishop54d3ec92015-11-03 09:32:34 -050066 item = self.cache[path]
67 name = kw['sender']
Brad Bishop732c6db2015-10-19 14:49:21 -040068 for x in interfaces:
Brad Bishop54d3ec92015-11-03 09:32:34 -050069 item[name].remove(x)
70
71 # remove the owner if there aren't any interfaces left
72 if not item[name]:
73 del item[name]
74
75 # update if interfaces remain
76 if item:
77 self.cache[path] = item
78 # mark for removal if no interfaces remain
79 elif self.cache.get_children(item):
80 self.cache.demote(item)
81 # delete the entire path if everything is gone
82 else:
83 del self.cache[path]
Brad Bishop732c6db2015-10-19 14:49:21 -040084
85 def process_new_owner(self, name):
86 # unique name
Brad Bishop6fb84612015-11-01 00:06:24 -040087 return self.discover([ IntrospectionParser(name,
88 self.bus.dbus, self.tag_match, self.intf_match) ])
Brad Bishop732c6db2015-10-19 14:49:21 -040089
90 def process_old_owner(self, name):
Brad Bishop54d3ec92015-11-03 09:32:34 -050091 for x,y in self.cache.dataitems():
92 if name not in y:
93 continue
94 del y[name]
95 if y:
96 self.cache[x] = y
97 elif self.cache.get_children(x):
98 self.cache.demote(x)
99 else:
100 del self.cache[x]
Brad Bishop732c6db2015-10-19 14:49:21 -0400101
102 def bus_handler(self, service, old, new):
Brad Bishopcb7aa602015-11-03 10:40:17 -0500103 if self.discovery_pending() or \
Brad Bishop6fb84612015-11-01 00:06:24 -0400104 not self.name_match(service):
Brad Bishop732c6db2015-10-19 14:49:21 -0400105 return
106
107 if new:
108 self.process_new_owner(new)
109 if old:
110 self.process_old_owner(old)
111
Brad Bishop54d3ec92015-11-03 09:32:34 -0500112 def add_interfaces(self, path, owner, interfaces):
113 d = self.cache.setdefault(path, { })
114 d.setdefault(owner, []).extend(interfaces)
115 self.cache[path] = d
Brad Bishop86398d22015-10-28 21:58:31 -0400116
Brad Bishop6fb84612015-11-01 00:06:24 -0400117 def add_items(self, owner, bus_items):
Brad Bishop86398d22015-10-28 21:58:31 -0400118 for x,y in bus_items.iteritems():
Brad Bishop54d3ec92015-11-03 09:32:34 -0500119 self.add_interfaces(x, owner, y['interfaces'])
Brad Bishop86398d22015-10-28 21:58:31 -0400120
Brad Bishop732c6db2015-10-19 14:49:21 -0400121 def discover(self, owners = None):
Brad Bishop732c6db2015-10-19 14:49:21 -0400122 if not owners:
Brad Bishop6fb84612015-11-01 00:06:24 -0400123 owners = [ IntrospectionParser(x, self.bus.dbus,
124 self.tag_match, self.intf_match) \
125 for x in self.bus.get_owner_names(self.name_match) ]
Brad Bishop732c6db2015-10-19 14:49:21 -0400126 for o in owners:
Brad Bishop6fb84612015-11-01 00:06:24 -0400127 self.add_items(o.name, o.introspect())
Brad Bishop732c6db2015-10-19 14:49:21 -0400128
Brad Bishopcb7aa602015-11-03 10:40:17 -0500129 if self.discovery_pending():
Brad Bishop732c6db2015-10-19 14:49:21 -0400130 print "ObjectMapper discovery complete..."
Brad Bishopcb7aa602015-11-03 10:40:17 -0500131 self.service = dbus.service.BusName(
132 OpenBMCMapper.MAPPER_NAME, self.bus.dbus)
Brad Bishop732c6db2015-10-19 14:49:21 -0400133
Brad Bishop54d3ec92015-11-03 09:32:34 -0500134 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 's', 'a{sas}')
135 def GetObject(self, path):
136 return self.cache[path]
Brad Bishop732c6db2015-10-19 14:49:21 -0400137
Brad Bishop54d3ec92015-11-03 09:32:34 -0500138 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'as')
139 def GetSubTreePaths(self, path, depth):
140 return self.cache.iterkeys(path, depth)
Brad Bishop732c6db2015-10-19 14:49:21 -0400141
Brad Bishop54d3ec92015-11-03 09:32:34 -0500142 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'a{sa{sas}}')
143 def GetSubTree(self, path, depth):
144 return { x:y for x, y in self.cache.dataitems(path, depth) }
Brad Bishop732c6db2015-10-19 14:49:21 -0400145
Brad Bishop732c6db2015-10-19 14:49:21 -0400146class BusWrapper:
147 def __init__(self, bus):
148 self.dbus = bus
149
150 def get_service_names(self, match):
151 # these are well known names
152 return [ x for x in self.dbus.list_names() \
Brad Bishop6fb84612015-11-01 00:06:24 -0400153 if match(x) and x != OpenBMCMapper.MAPPER_NAME ]
Brad Bishop732c6db2015-10-19 14:49:21 -0400154
155 def get_owner_names(self, match):
156 # these are unique connection names
157 return list( set( [ self.dbus.get_name_owner(x) \
158 for x in self.get_service_names(match) ] ) )
159
Brad Bishop732c6db2015-10-19 14:49:21 -0400160if __name__ == '__main__':
161 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
162 bus = dbus.SystemBus()
Brad Bishop732c6db2015-10-19 14:49:21 -0400163 o = ObjectMapper(BusWrapper(bus), OpenBMCMapper.MAPPER_PATH)
164 loop = gobject.MainLoop()
165
166 loop.run()