blob: 944f6d462e618d363887db01391cb34707b4cffe [file] [log] [blame]
Brad Bishop732c6db2015-10-19 14:49:21 -04001#!/usr/bin/env python
2
Brad Bishopb3385602016-03-04 15:32:01 -05003# Contributors Listed Below - COPYRIGHT 2016
Brad Bishop732c6db2015-10-19 14:49:21 -04004# [+] 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
Brad Bishop732c6db2015-10-19 14:49:21 -040019import dbus
20import dbus.service
Brad Bishopae0c0af2015-11-09 18:41:28 -050021import dbus.exceptions
Brad Bishop732c6db2015-10-19 14:49:21 -040022import dbus.mainloop.glib
23import gobject
Brad Bishopba86bc82016-05-05 22:09:27 -040024import xml.etree.ElementTree as ET
Brad Bishopfb1f58e2016-03-04 15:19:13 -050025import obmc.utils.pathtree
26import obmc.utils.misc
27import obmc.mapper
Brad Bishopfed968e2016-03-18 10:47:26 -040028import obmc.dbuslib.bindings
Brad Bishopd9fc21c2016-03-18 12:24:42 -040029import obmc.dbuslib.enums
Brad Bishop732c6db2015-10-19 14:49:21 -040030
Brad Bishop4b849412016-02-04 15:40:26 -050031
Brad Bishopae0c0af2015-11-09 18:41:28 -050032class MapperNotFoundException(dbus.exceptions.DBusException):
Brad Bishopfb1f58e2016-03-04 15:19:13 -050033 _dbus_error_name = obmc.mapper.MAPPER_NOT_FOUND
Brad Bishop4b849412016-02-04 15:40:26 -050034
35 def __init__(self, path):
36 super(MapperNotFoundException, self).__init__(
Brad Bishop9cff26b2016-03-18 11:16:47 -040037 "path or object not found: %s" % path)
Brad Bishop4b849412016-02-04 15:40:26 -050038
Brad Bishopae0c0af2015-11-09 18:41:28 -050039
Brad Bishopba86bc82016-05-05 22:09:27 -040040def find_dbus_interfaces(conn, service, path, match):
41 class __FindInterfaces(object):
42 def __init__(self):
43 self.results = {}
44
45 @staticmethod
46 def __introspect(service, path):
47 obj = conn.get_object(service, path, introspect=False)
48 iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE)
49 return iface.Introspect()
50
51 @staticmethod
52 def __get_managed_objects(service, om):
53 obj = conn.get_object(service, om, introspect=False)
54 iface = dbus.Interface(
55 obj, dbus.BUS_DAEMON_IFACE + '.ObjectManager')
56 return iface.GetManagedObjects()
57
58 @staticmethod
59 def __to_path(elements):
60 return '/' + '/'.join(elements)
61
62 @staticmethod
63 def __to_path_elements(path):
64 return filter(bool, path.split('/'))
65
66 def __call__(self, service, path):
67 self.results = {}
68 self.__find_interfaces(service, path)
69 return self.results
70
71 @staticmethod
72 def __match(iface):
73 return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' \
74 or match(iface)
75
76 def __find_interfaces(self, service, path):
77 path_elements = self.__to_path_elements(path)
78 path = self.__to_path(path_elements)
79 root = ET.fromstring(self.__introspect(service, path))
80
81 ifaces = filter(
82 self.__match,
83 [x.attrib.get('name') for x in root.findall('interface')])
84 self.results[path] = ifaces
85
86 if dbus.BUS_DAEMON_IFACE + '.ObjectManager' in ifaces:
87 objs = self.__get_managed_objects(service, path)
88 for k, v in objs.iteritems():
89 self.results[k] = v.keys()
90 else:
91 children = filter(
92 bool,
93 [x.attrib.get('name') for x in root.findall('node')])
94 children = [
95 self.__to_path(
96 path_elements + self.__to_path_elements(x))
97 for x in children]
98 for child in children:
99 if child not in self.results:
100 self.__find_interfaces(service, child)
101
102 return __FindInterfaces()(service, path)
103
104
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400105class Association(dbus.service.Object):
106 def __init__(self, bus, path, endpoints):
107 super(Association, self).__init__(bus, path)
108 self.endpoints = endpoints
109
110 def __getattr__(self, name):
111 if name == 'properties':
112 return {
113 obmc.dbuslib.enums.OBMC_ASSOC_IFACE: {
114 'endpoints': self.endpoints}}
115 return super(Association, self).__getattr__(name)
116
117 def emit_signal(self, old):
118 if old != self.endpoints:
119 self.PropertiesChanged(
120 obmc.dbuslib.enums.OBMC_ASSOC_IFACE,
121 {'endpoints': self.endpoints}, ['endpoints'])
122
123 def append(self, endpoints):
124 old = self.endpoints
125 self.endpoints = list(set(endpoints).union(self.endpoints))
126 self.emit_signal(old)
127
128 def remove(self, endpoints):
129 old = self.endpoints
130 self.endpoints = list(set(self.endpoints).difference(endpoints))
131 self.emit_signal(old)
132
133 @dbus.service.method(dbus.PROPERTIES_IFACE, 'ss', 'as')
134 def Get(self, interface_name, property_name):
135 if property_name != 'endpoints':
136 raise dbus.exceptions.DBusException(name=DBUS_UNKNOWN_PROPERTY)
137 return self.GetAll(interface_name)[property_name]
138
139 @dbus.service.method(dbus.PROPERTIES_IFACE, 's', 'a{sas}')
140 def GetAll(self, interface_name):
141 if interface_name != obmc.dbuslib.enums.OBMC_ASSOC_IFACE:
142 raise dbus.exceptions.DBusException(DBUS_UNKNOWN_INTERFACE)
143 return {'endpoints': self.endpoints}
144
145 @dbus.service.signal(
146 dbus.PROPERTIES_IFACE, signature='sa{sas}as')
147 def PropertiesChanged(
148 self, interface_name, changed_properties, invalidated_properties):
149 pass
150
151
152class Manager(obmc.dbuslib.bindings.DbusObjectManager):
153 def __init__(self, bus, path):
154 obmc.dbuslib.bindings.DbusObjectManager.__init__(self)
155 dbus.service.Object.__init__(self, bus.dbus, path)
156
157
Brad Bishop732c6db2015-10-19 14:49:21 -0400158class ObjectMapper(dbus.service.Object):
Brad Bishop4b849412016-02-04 15:40:26 -0500159 def __init__(self, bus, path,
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500160 name_match=obmc.utils.misc.org_dot_openbmc_match,
161 intf_match=obmc.utils.misc.org_dot_openbmc_match):
Brad Bishop4b849412016-02-04 15:40:26 -0500162 super(ObjectMapper, self).__init__(bus.dbus, path)
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500163 self.cache = obmc.utils.pathtree.PathTree()
Brad Bishop4b849412016-02-04 15:40:26 -0500164 self.bus = bus
165 self.name_match = name_match
166 self.intf_match = intf_match
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500167 self.tag_match = obmc.utils.misc.ListMatch(['children', 'interface'])
Brad Bishop4b849412016-02-04 15:40:26 -0500168 self.service = None
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400169 self.index = {}
170 self.manager = Manager(bus, obmc.dbuslib.bindings.OBJ_PREFIX)
171 self.unique = bus.dbus.get_unique_name()
Brad Bishop732c6db2015-10-19 14:49:21 -0400172
Brad Bishop4b849412016-02-04 15:40:26 -0500173 gobject.idle_add(self.discover)
174 self.bus.dbus.add_signal_receiver(
175 self.bus_handler,
176 dbus_interface=
177 dbus.BUS_DAEMON_IFACE,
178 signal_name='NameOwnerChanged')
179 self.bus.dbus.add_signal_receiver(
180 self.interfaces_added_handler,
181 dbus_interface=
182 dbus.BUS_DAEMON_IFACE + '.ObjectManager',
183 signal_name='InterfacesAdded',
Brad Bishopc568e022016-03-23 14:50:05 -0400184 sender_keyword='sender',
185 path_keyword='sender_path')
Brad Bishop4b849412016-02-04 15:40:26 -0500186 self.bus.dbus.add_signal_receiver(
187 self.interfaces_removed_handler,
188 dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager',
189 signal_name='InterfacesRemoved',
Brad Bishopc568e022016-03-23 14:50:05 -0400190 sender_keyword='sender',
191 path_keyword='sender_path')
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400192 self.bus.dbus.add_signal_receiver(
193 self.properties_changed_handler,
194 dbus_interface=dbus.PROPERTIES_IFACE,
195 signal_name='PropertiesChanged',
196 path_keyword='path',
197 sender_keyword='sender')
Brad Bishop732c6db2015-10-19 14:49:21 -0400198
Brad Bishop9cff26b2016-03-18 11:16:47 -0400199 def bus_match(self, owner):
200 # Ignore my own signals
201 return owner != obmc.mapper.MAPPER_NAME and \
202 self.name_match(owner)
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500203
Brad Bishop4b849412016-02-04 15:40:26 -0500204 def discovery_pending(self):
205 return not bool(self.service)
Brad Bishopcb7aa602015-11-03 10:40:17 -0500206
Brad Bishop10abe042016-05-02 23:11:19 -0400207 def cache_get(self, path):
208 cache_entry = self.cache.get(path, {})
209 if cache_entry is None:
210 # hide path elements without any interfaces
211 cache_entry = {}
212 return cache_entry
213
Brad Bishopc568e022016-03-23 14:50:05 -0400214 def add_new_objmgr(self, path, owner):
215 # We don't get a signal for the ObjectManager
216 # interface itself, so if we see a signal from
217 # make sure its in our cache, and add it if not.
Brad Bishop10abe042016-05-02 23:11:19 -0400218 cache_entry = self.cache_get(path)
Brad Bishopc568e022016-03-23 14:50:05 -0400219 old = self.interfaces_get(cache_entry, owner)
220 new = list(set(old).union([dbus.BUS_DAEMON_IFACE + '.ObjectManager']))
221 self.update_interfaces(path, owner, old, new)
222
Brad Bishop4b849412016-02-04 15:40:26 -0500223 def interfaces_added_handler(self, path, iprops, **kw):
Brad Bishopfed968e2016-03-18 10:47:26 -0400224 path = str(path)
225 owner = str(kw['sender'])
226 interfaces = self.get_signal_interfaces(owner, iprops.iterkeys())
Brad Bishop10abe042016-05-02 23:11:19 -0400227 if interfaces:
228 self.add_new_objmgr(str(kw['sender_path']), owner)
229 cache_entry = self.cache_get(path)
Brad Bishopfed968e2016-03-18 10:47:26 -0400230 old = self.interfaces_get(cache_entry, owner)
231 new = list(set(interfaces).union(old))
232 self.update_interfaces(path, owner, old, new)
Brad Bishop732c6db2015-10-19 14:49:21 -0400233
Brad Bishop4b849412016-02-04 15:40:26 -0500234 def interfaces_removed_handler(self, path, interfaces, **kw):
Brad Bishopfed968e2016-03-18 10:47:26 -0400235 path = str(path)
236 owner = str(kw['sender'])
237 interfaces = self.get_signal_interfaces(owner, interfaces)
Brad Bishop10abe042016-05-02 23:11:19 -0400238 if interfaces:
239 self.add_new_objmgr(str(kw['sender_path']), owner)
240 cache_entry = self.cache_get(path)
Brad Bishopfed968e2016-03-18 10:47:26 -0400241 old = self.interfaces_get(cache_entry, owner)
242 new = list(set(old).difference(interfaces))
243 self.update_interfaces(path, owner, old, new)
Brad Bishop732c6db2015-10-19 14:49:21 -0400244
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400245 def properties_changed_handler(self, interface, new, old, **kw):
246 owner = str(kw['sender'])
247 path = str(kw['path'])
248 interfaces = self.get_signal_interfaces(owner, [interface])
249 if not self.is_association(interfaces):
250 return
251 associations = new.get('associations', None)
252 if associations is None:
253 return
254
255 associations = [
256 (str(x), str(y), str(z)) for x, y, z in associations]
257 self.update_associations(
258 path, owner,
259 self.index_get_associations(path, [owner]),
260 associations)
261
Brad Bishop9cff26b2016-03-18 11:16:47 -0400262 def process_new_owner(self, owner):
Brad Bishop4b849412016-02-04 15:40:26 -0500263 # unique name
Brad Bishopba86bc82016-05-05 22:09:27 -0400264 try:
265 return self.discover([owner])
266 except dbus.exceptions.DBusException, e:
267 if obmc.dbuslib.enums.DBUS_UNKNOWN_SERVICE \
268 not in e.get_dbus_name():
269 raise
Brad Bishop732c6db2015-10-19 14:49:21 -0400270
Brad Bishop9cff26b2016-03-18 11:16:47 -0400271 def process_old_owner(self, owner):
Brad Bishopfed968e2016-03-18 10:47:26 -0400272 for path, item in self.cache.dataitems():
Brad Bishop9cff26b2016-03-18 11:16:47 -0400273 old = self.interfaces_get(item, owner)
Brad Bishopfed968e2016-03-18 10:47:26 -0400274 # remove all interfaces for this service
275 self.update_interfaces(
Brad Bishop9cff26b2016-03-18 11:16:47 -0400276 path, owner, old=old, new=[])
Brad Bishop732c6db2015-10-19 14:49:21 -0400277
Brad Bishopfed968e2016-03-18 10:47:26 -0400278 def bus_handler(self, owner, old, new):
279 valid = False
280 if not obmc.dbuslib.bindings.is_unique(owner):
281 valid = self.valid_signal(owner)
Brad Bishop732c6db2015-10-19 14:49:21 -0400282
Brad Bishopfed968e2016-03-18 10:47:26 -0400283 if valid and new:
Brad Bishop4b849412016-02-04 15:40:26 -0500284 self.process_new_owner(new)
Brad Bishopfed968e2016-03-18 10:47:26 -0400285 if valid and old:
Brad Bishop4b849412016-02-04 15:40:26 -0500286 self.process_old_owner(old)
Brad Bishop732c6db2015-10-19 14:49:21 -0400287
Brad Bishopfed968e2016-03-18 10:47:26 -0400288 def update_interfaces(self, path, owner, old, new):
289 cache_entry = self.cache.setdefault(path, {})
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400290 created = [] if self.has_interfaces(cache_entry) else [path]
Brad Bishopfed968e2016-03-18 10:47:26 -0400291 added = list(set(new).difference(old))
292 removed = list(set(old).difference(new))
293 self.interfaces_append(cache_entry, owner, added)
294 self.interfaces_remove(cache_entry, owner, removed, path)
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400295 destroyed = [] if self.has_interfaces(cache_entry) else [path]
296
297 # react to anything that requires association updates
298 new_assoc = []
299 old_assoc = []
300 if self.is_association(added):
301 new_assoc = self.dbus_get_associations(path, owner)
302 if self.is_association(removed):
303 old_assoc = self.index_get_associations(path, [owner])
304 self.update_associations(
305 path, owner, old_assoc, new_assoc, created, destroyed)
Brad Bishop86398d22015-10-28 21:58:31 -0400306
Brad Bishop4b849412016-02-04 15:40:26 -0500307 def add_items(self, owner, bus_items):
Brad Bishopfed968e2016-03-18 10:47:26 -0400308 for path, items in bus_items.iteritems():
309 # convert dbus types to native.
Brad Bishopba86bc82016-05-05 22:09:27 -0400310 interfaces = [str(i) for i in items]
Brad Bishopfed968e2016-03-18 10:47:26 -0400311 self.update_interfaces(path, str(owner), old=[], new=interfaces)
Brad Bishop86398d22015-10-28 21:58:31 -0400312
Brad Bishop9cff26b2016-03-18 11:16:47 -0400313 def discover(self, owners=[]):
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400314 def match(iface):
315 return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' or \
316 self.intf_match(iface)
Brad Bishop4b849412016-02-04 15:40:26 -0500317 if not owners:
Brad Bishopba86bc82016-05-05 22:09:27 -0400318 owners = self.bus.get_owner_names(self.bus_match)
Brad Bishop4b849412016-02-04 15:40:26 -0500319 for o in owners:
Brad Bishopba86bc82016-05-05 22:09:27 -0400320 self.add_items(
321 o,
322 find_dbus_interfaces(self.bus.dbus, o, '/', self.intf_match))
Brad Bishop732c6db2015-10-19 14:49:21 -0400323
Brad Bishop4b849412016-02-04 15:40:26 -0500324 if self.discovery_pending():
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400325 # add my object mananger instance
326 self.add_items(
327 self.unique,
328 {obmc.dbuslib.bindings.OBJ_PREFIX:
329 {'interfaces':
330 [dbus.BUS_DAEMON_IFACE + '.ObjectManager']}})
331
Brad Bishop4b849412016-02-04 15:40:26 -0500332 print "ObjectMapper discovery complete..."
333 self.service = dbus.service.BusName(
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500334 obmc.mapper.MAPPER_NAME, self.bus.dbus)
Brad Bishop732c6db2015-10-19 14:49:21 -0400335
Brad Bishopfed968e2016-03-18 10:47:26 -0400336 def valid_signal(self, owner):
337 if obmc.dbuslib.bindings.is_unique(owner):
338 owner = self.bus.get_owned_name(self.bus_match, owner)
339
340 return owner is not None and not self.discovery_pending() and \
341 self.bus_match(owner)
342
343 def get_signal_interfaces(self, owner, interfaces):
344 filtered = []
345 if self.valid_signal(owner):
346 filtered = [str(x) for x in interfaces if self.intf_match(x)]
347
348 return filtered
349
350 @staticmethod
351 def interfaces_get(item, owner, default=[]):
352 return item.get(owner, default)
353
354 @staticmethod
355 def interfaces_append(item, owner, append):
356 interfaces = item.setdefault(owner, [])
357 item[owner] = list(set(append).union(interfaces))
358
359 def interfaces_remove(self, item, owner, remove, path):
360 interfaces = item.get(owner, [])
361 item[owner] = list(set(interfaces).difference(remove))
362
363 if not item[owner]:
364 # remove the owner if there aren't any interfaces left
365 del item[owner]
366
367 if item:
368 # other owners remain
369 return
370
371 if self.cache.get_children(path):
372 # there are still references to this path
373 # from objects further down the tree.
374 # mark it for removal if that changes
375 self.cache.demote(path)
376 else:
377 # delete the entire path if everything is gone
378 del self.cache[path]
379
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500380 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sas}')
Brad Bishop4b849412016-02-04 15:40:26 -0500381 def GetObject(self, path):
Brad Bishop10abe042016-05-02 23:11:19 -0400382 o = self.cache_get(path)
Brad Bishop4b849412016-02-04 15:40:26 -0500383 if not o:
384 raise MapperNotFoundException(path)
385 return o
Brad Bishop732c6db2015-10-19 14:49:21 -0400386
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500387 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'as')
Brad Bishop4b849412016-02-04 15:40:26 -0500388 def GetSubTreePaths(self, path, depth):
389 try:
390 return self.cache.iterkeys(path, depth)
391 except KeyError:
392 raise MapperNotFoundException(path)
Brad Bishop732c6db2015-10-19 14:49:21 -0400393
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500394 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'a{sa{sas}}')
Brad Bishop4b849412016-02-04 15:40:26 -0500395 def GetSubTree(self, path, depth):
396 try:
397 return {x: y for x, y in self.cache.dataitems(path, depth)}
398 except KeyError:
399 raise MapperNotFoundException(path)
400
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400401 @staticmethod
402 def has_interfaces(item):
403 for owner in item.iterkeys():
404 if ObjectMapper.interfaces_get(item, owner):
405 return True
406 return False
407
408 @staticmethod
409 def is_association(interfaces):
410 return obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE in interfaces
411
412 def index_get(self, index, path, owners):
413 items = []
414 item = self.index.get(index, {})
415 item = item.get(path, {})
416 for o in owners:
417 items.extend(item.get(o, []))
418 return items
419
420 def index_append(self, index, path, owner, assoc):
421 item = self.index.setdefault(index, {})
422 item = item.setdefault(path, {})
423 item = item.setdefault(owner, [])
424 item.append(assoc)
425
426 def index_remove(self, index, path, owner, assoc):
427 index = self.index.get(index, {})
428 owners = index.get(path, {})
429 items = owners.get(owner, [])
430 if assoc in items:
431 items.remove(assoc)
432 if not items:
433 del owners[owner]
434 if not owners:
435 del index[path]
436
437 def dbus_get_associations(self, path, owner):
438 obj = self.bus.dbus.get_object(owner, path, introspect=False)
439 iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
440 return [(str(f), str(r), str(e)) for f, r, e in iface.Get(
441 obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE,
442 'associations')]
443
444 def index_get_associations(self, path, owners=[], direction='forward'):
445 forward = 'forward' if direction == 'forward' else 'reverse'
446 reverse = 'reverse' if direction == 'forward' else 'forward'
447
448 associations = []
449 if not owners:
450 index = self.index.get(forward, {})
451 owners = index.get(path, {}).keys()
452
453 # f: forward
454 # r: reverse
455 for rassoc in self.index_get(forward, path, owners):
456 elements = rassoc.split('/')
457 rtype = ''.join(elements[-1:])
458 fendpoint = '/'.join(elements[:-1])
459 for fassoc in self.index_get(reverse, fendpoint, owners):
460 elements = fassoc.split('/')
461 ftype = ''.join(elements[-1:])
462 rendpoint = '/'.join(elements[:-1])
463 if rendpoint != path:
464 continue
465 associations.append((ftype, rtype, fendpoint))
466
467 return associations
468
469 def update_association(self, path, removed, added):
470 iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE
471 create = [] if self.manager.get(path, False) else [iface]
472
473 if added and create:
474 self.manager.add(
475 path, Association(self.bus.dbus, path, added))
476 elif added:
477 self.manager.get(path).append(added)
478
479 obj = self.manager.get(path, None)
480 if obj and removed:
481 obj.remove(removed)
482
483 if obj and not obj.endpoints:
484 self.manager.remove(path)
485
486 delete = [] if self.manager.get(path, False) else [iface]
487
488 if create != delete:
489 self.update_interfaces(
490 path, self.unique, delete, create)
491
492 def update_associations(
493 self, path, owner, old, new, created=[], destroyed=[]):
494 added = list(set(new).difference(old))
495 removed = list(set(old).difference(new))
496 for forward, reverse, endpoint in added:
497 # update the index
498 forward_path = str(path + '/' + forward)
499 reverse_path = str(endpoint + '/' + reverse)
500 self.index_append(
501 'forward', path, owner, reverse_path)
502 self.index_append(
503 'reverse', endpoint, owner, forward_path)
504
505 # create the association if the endpoint exists
Brad Bishop10abe042016-05-02 23:11:19 -0400506 if not self.cache_get(endpoint):
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400507 continue
508
509 self.update_association(forward_path, [], [endpoint])
510 self.update_association(reverse_path, [], [path])
511
512 for forward, reverse, endpoint in removed:
513 # update the index
514 forward_path = str(path + '/' + forward)
515 reverse_path = str(endpoint + '/' + reverse)
516 self.index_remove(
517 'forward', path, owner, reverse_path)
518 self.index_remove(
519 'reverse', endpoint, owner, forward_path)
520
521 # destroy the association if it exists
522 self.update_association(forward_path, [endpoint], [])
523 self.update_association(reverse_path, [path], [])
524
525 # If the associations interface endpoint comes
526 # or goes create or destroy the appropriate
527 # associations
528 for path in created:
529 for forward, reverse, endpoint in \
530 self.index_get_associations(path, direction='reverse'):
531 forward_path = str(path + '/' + forward)
532 reverse_path = str(endpoint + '/' + reverse)
533 self.update_association(forward_path, [], [endpoint])
534 self.update_association(reverse_path, [], [path])
535
536 for path in destroyed:
537 for forward, reverse, endpoint in \
538 self.index_get_associations(path, direction='reverse'):
539 forward_path = str(path + '/' + forward)
540 reverse_path = str(endpoint + '/' + reverse)
541 self.update_association(forward_path, [endpoint], [])
542 self.update_association(reverse_path, [path], [])
543
544 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sa{sas}}')
545 def GetAncestors(self, path):
546 elements = filter(bool, path.split('/'))
547 paths = []
548 objs = {}
549 while elements:
550 elements.pop()
551 paths.append('/' + '/'.join(elements))
552 if path != '/':
553 paths.append('/')
554
555 for path in paths:
Brad Bishop10abe042016-05-02 23:11:19 -0400556 obj = self.cache_get(path)
557 if not obj:
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400558 continue
559 objs[path] = obj
560
561 return objs
562
Brad Bishop732c6db2015-10-19 14:49:21 -0400563
Brad Bishop732c6db2015-10-19 14:49:21 -0400564class BusWrapper:
Brad Bishop4b849412016-02-04 15:40:26 -0500565 def __init__(self, bus):
566 self.dbus = bus
Brad Bishop732c6db2015-10-19 14:49:21 -0400567
Brad Bishop4b849412016-02-04 15:40:26 -0500568 def get_owned_name(self, match, bus):
569 for x in self.get_service_names(match):
570 if self.dbus.get_name_owner(x) == bus:
571 return x
Brad Bishop5ffc2a32015-11-25 08:46:01 -0500572
Brad Bishop4b849412016-02-04 15:40:26 -0500573 def get_service_names(self, match):
574 # these are well known names
575 return [x for x in self.dbus.list_names()
576 if match(x)]
Brad Bishop732c6db2015-10-19 14:49:21 -0400577
Brad Bishop4b849412016-02-04 15:40:26 -0500578 def get_owner_names(self, match):
579 # these are unique connection names
580 return list(set(
581 [self.dbus.get_name_owner(x)
582 for x in self.get_service_names(match)]))
Brad Bishop732c6db2015-10-19 14:49:21 -0400583
Brad Bishop732c6db2015-10-19 14:49:21 -0400584if __name__ == '__main__':
Brad Bishop4b849412016-02-04 15:40:26 -0500585 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
586 bus = dbus.SystemBus()
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500587 o = ObjectMapper(BusWrapper(bus), obmc.mapper.MAPPER_PATH)
Brad Bishop4b849412016-02-04 15:40:26 -0500588 loop = gobject.MainLoop()
Brad Bishop732c6db2015-10-19 14:49:21 -0400589
Brad Bishop4b849412016-02-04 15:40:26 -0500590 loop.run()