blob: a2d007ce70c0362ef43bf868d7bad453d1d5d8bd [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)
Brad Bishop56934372016-06-05 18:24:41 -0400155 dbus.service.Object.__init__(self, bus, path)
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400156
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 Bishop56934372016-06-05 18:24:41 -0400162 super(ObjectMapper, self).__init__(bus, 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)
Brad Bishop56934372016-06-05 18:24:41 -0400171 self.unique = bus.get_unique_name()
172 self.bus_map = {}
Brad Bishop732c6db2015-10-19 14:49:21 -0400173
Brad Bishop4b849412016-02-04 15:40:26 -0500174 gobject.idle_add(self.discover)
Brad Bishop56934372016-06-05 18:24:41 -0400175 self.bus.add_signal_receiver(
Brad Bishop4b849412016-02-04 15:40:26 -0500176 self.bus_handler,
Brad Bishop62a50da2016-06-24 13:06:01 -0400177 dbus_interface=dbus.BUS_DAEMON_IFACE,
Brad Bishop4b849412016-02-04 15:40:26 -0500178 signal_name='NameOwnerChanged')
Brad Bishop56934372016-06-05 18:24:41 -0400179 self.bus.add_signal_receiver(
Brad Bishop4b849412016-02-04 15:40:26 -0500180 self.interfaces_added_handler,
Brad Bishop62a50da2016-06-24 13:06:01 -0400181 dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager',
Brad Bishop4b849412016-02-04 15:40:26 -0500182 signal_name='InterfacesAdded',
Brad Bishopc568e022016-03-23 14:50:05 -0400183 sender_keyword='sender',
184 path_keyword='sender_path')
Brad Bishop56934372016-06-05 18:24:41 -0400185 self.bus.add_signal_receiver(
Brad Bishop4b849412016-02-04 15:40:26 -0500186 self.interfaces_removed_handler,
187 dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager',
188 signal_name='InterfacesRemoved',
Brad Bishopc568e022016-03-23 14:50:05 -0400189 sender_keyword='sender',
190 path_keyword='sender_path')
Brad Bishop56934372016-06-05 18:24:41 -0400191 self.bus.add_signal_receiver(
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400192 self.properties_changed_handler,
193 dbus_interface=dbus.PROPERTIES_IFACE,
194 signal_name='PropertiesChanged',
195 path_keyword='path',
196 sender_keyword='sender')
Brad Bishop732c6db2015-10-19 14:49:21 -0400197
Brad Bishop9cff26b2016-03-18 11:16:47 -0400198 def bus_match(self, owner):
199 # Ignore my own signals
200 return owner != obmc.mapper.MAPPER_NAME and \
201 self.name_match(owner)
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500202
Brad Bishop4b849412016-02-04 15:40:26 -0500203 def discovery_pending(self):
204 return not bool(self.service)
Brad Bishopcb7aa602015-11-03 10:40:17 -0500205
Brad Bishop10abe042016-05-02 23:11:19 -0400206 def cache_get(self, path):
207 cache_entry = self.cache.get(path, {})
208 if cache_entry is None:
209 # hide path elements without any interfaces
210 cache_entry = {}
211 return cache_entry
212
Brad Bishopc568e022016-03-23 14:50:05 -0400213 def add_new_objmgr(self, path, owner):
214 # We don't get a signal for the ObjectManager
215 # interface itself, so if we see a signal from
216 # make sure its in our cache, and add it if not.
Brad Bishop10abe042016-05-02 23:11:19 -0400217 cache_entry = self.cache_get(path)
Brad Bishopc568e022016-03-23 14:50:05 -0400218 old = self.interfaces_get(cache_entry, owner)
219 new = list(set(old).union([dbus.BUS_DAEMON_IFACE + '.ObjectManager']))
220 self.update_interfaces(path, owner, old, new)
221
Brad Bishop4b849412016-02-04 15:40:26 -0500222 def interfaces_added_handler(self, path, iprops, **kw):
Brad Bishopfed968e2016-03-18 10:47:26 -0400223 path = str(path)
224 owner = str(kw['sender'])
225 interfaces = self.get_signal_interfaces(owner, iprops.iterkeys())
Brad Bishop10abe042016-05-02 23:11:19 -0400226 if interfaces:
227 self.add_new_objmgr(str(kw['sender_path']), owner)
Brad Bishop56934372016-06-05 18:24:41 -0400228 cache_entry = self.cache_get(path)
229 old = self.interfaces_get(cache_entry, owner)
230 new = list(set(interfaces).union(old))
231 self.update_interfaces(path, owner, old, new)
Brad Bishop732c6db2015-10-19 14:49:21 -0400232
Brad Bishop4b849412016-02-04 15:40:26 -0500233 def interfaces_removed_handler(self, path, interfaces, **kw):
Brad Bishopfed968e2016-03-18 10:47:26 -0400234 path = str(path)
235 owner = str(kw['sender'])
236 interfaces = self.get_signal_interfaces(owner, interfaces)
Brad Bishop10abe042016-05-02 23:11:19 -0400237 if interfaces:
238 self.add_new_objmgr(str(kw['sender_path']), owner)
Brad Bishop56934372016-06-05 18:24:41 -0400239 cache_entry = self.cache_get(path)
240 old = self.interfaces_get(cache_entry, owner)
241 new = list(set(old).difference(interfaces))
242 self.update_interfaces(path, owner, old, new)
Brad Bishop732c6db2015-10-19 14:49:21 -0400243
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400244 def properties_changed_handler(self, interface, new, old, **kw):
245 owner = str(kw['sender'])
246 path = str(kw['path'])
247 interfaces = self.get_signal_interfaces(owner, [interface])
248 if not self.is_association(interfaces):
249 return
250 associations = new.get('associations', None)
251 if associations is None:
252 return
253
254 associations = [
255 (str(x), str(y), str(z)) for x, y, z in associations]
256 self.update_associations(
257 path, owner,
258 self.index_get_associations(path, [owner]),
259 associations)
260
Brad Bishop56934372016-06-05 18:24:41 -0400261 def process_new_owner(self, owned_name, owner):
Brad Bishop4b849412016-02-04 15:40:26 -0500262 # unique name
Brad Bishopba86bc82016-05-05 22:09:27 -0400263 try:
Brad Bishop56934372016-06-05 18:24:41 -0400264 return self.discover([(owned_name, owner)])
Brad Bishopba86bc82016-05-05 22:09:27 -0400265 except dbus.exceptions.DBusException, e:
266 if obmc.dbuslib.enums.DBUS_UNKNOWN_SERVICE \
267 not in e.get_dbus_name():
268 raise
Brad Bishop732c6db2015-10-19 14:49:21 -0400269
Brad Bishop56934372016-06-05 18:24:41 -0400270 def process_old_owner(self, owned_name, owner):
271 if owner in self.bus_map:
272 del self.bus_map[owner]
273
Brad Bishopfed968e2016-03-18 10:47:26 -0400274 for path, item in self.cache.dataitems():
Brad Bishop9cff26b2016-03-18 11:16:47 -0400275 old = self.interfaces_get(item, owner)
Brad Bishopfed968e2016-03-18 10:47:26 -0400276 # remove all interfaces for this service
277 self.update_interfaces(
Brad Bishop9cff26b2016-03-18 11:16:47 -0400278 path, owner, old=old, new=[])
Brad Bishop732c6db2015-10-19 14:49:21 -0400279
Brad Bishop56934372016-06-05 18:24:41 -0400280 def bus_handler(self, owned_name, old, new):
Brad Bishopfed968e2016-03-18 10:47:26 -0400281 valid = False
Brad Bishop56934372016-06-05 18:24:41 -0400282 if not obmc.dbuslib.bindings.is_unique(owned_name):
283 valid = self.valid_signal(owned_name)
Brad Bishop732c6db2015-10-19 14:49:21 -0400284
Brad Bishopfed968e2016-03-18 10:47:26 -0400285 if valid and new:
Brad Bishop56934372016-06-05 18:24:41 -0400286 self.process_new_owner(owned_name, new)
Brad Bishopfed968e2016-03-18 10:47:26 -0400287 if valid and old:
Brad Bishop56934372016-06-05 18:24:41 -0400288 self.process_old_owner(owned_name, old)
Brad Bishop732c6db2015-10-19 14:49:21 -0400289
Brad Bishopfed968e2016-03-18 10:47:26 -0400290 def update_interfaces(self, path, owner, old, new):
291 cache_entry = self.cache.setdefault(path, {})
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400292 created = [] if self.has_interfaces(cache_entry) else [path]
Brad Bishopfed968e2016-03-18 10:47:26 -0400293 added = list(set(new).difference(old))
294 removed = list(set(old).difference(new))
295 self.interfaces_append(cache_entry, owner, added)
296 self.interfaces_remove(cache_entry, owner, removed, path)
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400297 destroyed = [] if self.has_interfaces(cache_entry) else [path]
298
299 # react to anything that requires association updates
300 new_assoc = []
301 old_assoc = []
302 if self.is_association(added):
303 new_assoc = self.dbus_get_associations(path, owner)
304 if self.is_association(removed):
305 old_assoc = self.index_get_associations(path, [owner])
306 self.update_associations(
307 path, owner, old_assoc, new_assoc, created, destroyed)
Brad Bishop86398d22015-10-28 21:58:31 -0400308
Brad Bishop4b849412016-02-04 15:40:26 -0500309 def add_items(self, owner, bus_items):
Brad Bishopfed968e2016-03-18 10:47:26 -0400310 for path, items in bus_items.iteritems():
311 # convert dbus types to native.
Brad Bishopba86bc82016-05-05 22:09:27 -0400312 interfaces = [str(i) for i in items]
Brad Bishopfed968e2016-03-18 10:47:26 -0400313 self.update_interfaces(path, str(owner), old=[], new=interfaces)
Brad Bishop86398d22015-10-28 21:58:31 -0400314
Brad Bishop9cff26b2016-03-18 11:16:47 -0400315 def discover(self, owners=[]):
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400316 def match(iface):
317 return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' or \
318 self.intf_match(iface)
Brad Bishop4b849412016-02-04 15:40:26 -0500319 if not owners:
Brad Bishop62a50da2016-06-24 13:06:01 -0400320 owned_names = [
321 x for x in self.bus.list_names() if self.bus_match(x)]
Brad Bishop56934372016-06-05 18:24:41 -0400322 owners = [self.bus.get_name_owner(x) for x in owned_names]
323 owners = zip(owned_names, owners)
324 for owned_name, o in owners:
Brad Bishopba86bc82016-05-05 22:09:27 -0400325 self.add_items(
326 o,
Brad Bishop56934372016-06-05 18:24:41 -0400327 find_dbus_interfaces(self.bus, o, '/', self.intf_match))
328 self.bus_map[o] = owned_name
Brad Bishop732c6db2015-10-19 14:49:21 -0400329
Brad Bishop4b849412016-02-04 15:40:26 -0500330 if self.discovery_pending():
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400331 # add my object mananger instance
Brad Bishop56934372016-06-05 18:24:41 -0400332 self.bus_map[self.unique] = obmc.mapper.MAPPER_NAME
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400333 self.add_items(
334 self.unique,
335 {obmc.dbuslib.bindings.OBJ_PREFIX:
336 {'interfaces':
337 [dbus.BUS_DAEMON_IFACE + '.ObjectManager']}})
338
Brad Bishop4b849412016-02-04 15:40:26 -0500339 print "ObjectMapper discovery complete..."
340 self.service = dbus.service.BusName(
Brad Bishop56934372016-06-05 18:24:41 -0400341 obmc.mapper.MAPPER_NAME, self.bus)
Brad Bishop732c6db2015-10-19 14:49:21 -0400342
Brad Bishop56934372016-06-05 18:24:41 -0400343 def valid_signal(self, name):
344 if self.discovery_pending():
345 return False
Brad Bishopfed968e2016-03-18 10:47:26 -0400346
Brad Bishop56934372016-06-05 18:24:41 -0400347 if obmc.dbuslib.bindings.is_unique(name):
348 name = self.bus_map.get(name)
349
350 return name is not None and \
351 self.bus_match(name)
Brad Bishopfed968e2016-03-18 10:47:26 -0400352
353 def get_signal_interfaces(self, owner, interfaces):
354 filtered = []
355 if self.valid_signal(owner):
356 filtered = [str(x) for x in interfaces if self.intf_match(x)]
357
358 return filtered
359
360 @staticmethod
361 def interfaces_get(item, owner, default=[]):
362 return item.get(owner, default)
363
364 @staticmethod
365 def interfaces_append(item, owner, append):
366 interfaces = item.setdefault(owner, [])
367 item[owner] = list(set(append).union(interfaces))
368
369 def interfaces_remove(self, item, owner, remove, path):
370 interfaces = item.get(owner, [])
371 item[owner] = list(set(interfaces).difference(remove))
372
373 if not item[owner]:
374 # remove the owner if there aren't any interfaces left
375 del item[owner]
376
377 if item:
378 # other owners remain
379 return
380
381 if self.cache.get_children(path):
382 # there are still references to this path
383 # from objects further down the tree.
384 # mark it for removal if that changes
385 self.cache.demote(path)
386 else:
387 # delete the entire path if everything is gone
388 del self.cache[path]
389
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500390 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sas}')
Brad Bishop4b849412016-02-04 15:40:26 -0500391 def GetObject(self, path):
Brad Bishop10abe042016-05-02 23:11:19 -0400392 o = self.cache_get(path)
Brad Bishop4b849412016-02-04 15:40:26 -0500393 if not o:
394 raise MapperNotFoundException(path)
395 return o
Brad Bishop732c6db2015-10-19 14:49:21 -0400396
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500397 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'as')
Brad Bishop4b849412016-02-04 15:40:26 -0500398 def GetSubTreePaths(self, path, depth):
399 try:
400 return self.cache.iterkeys(path, depth)
401 except KeyError:
402 raise MapperNotFoundException(path)
Brad Bishop732c6db2015-10-19 14:49:21 -0400403
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500404 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'a{sa{sas}}')
Brad Bishop4b849412016-02-04 15:40:26 -0500405 def GetSubTree(self, path, depth):
406 try:
407 return {x: y for x, y in self.cache.dataitems(path, depth)}
408 except KeyError:
409 raise MapperNotFoundException(path)
410
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400411 @staticmethod
412 def has_interfaces(item):
413 for owner in item.iterkeys():
414 if ObjectMapper.interfaces_get(item, owner):
415 return True
416 return False
417
418 @staticmethod
419 def is_association(interfaces):
420 return obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE in interfaces
421
422 def index_get(self, index, path, owners):
423 items = []
424 item = self.index.get(index, {})
425 item = item.get(path, {})
426 for o in owners:
427 items.extend(item.get(o, []))
428 return items
429
430 def index_append(self, index, path, owner, assoc):
431 item = self.index.setdefault(index, {})
432 item = item.setdefault(path, {})
433 item = item.setdefault(owner, [])
434 item.append(assoc)
435
436 def index_remove(self, index, path, owner, assoc):
437 index = self.index.get(index, {})
438 owners = index.get(path, {})
439 items = owners.get(owner, [])
440 if assoc in items:
441 items.remove(assoc)
442 if not items:
443 del owners[owner]
444 if not owners:
445 del index[path]
446
447 def dbus_get_associations(self, path, owner):
Brad Bishop56934372016-06-05 18:24:41 -0400448 obj = self.bus.get_object(owner, path, introspect=False)
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400449 iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
450 return [(str(f), str(r), str(e)) for f, r, e in iface.Get(
451 obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE,
452 'associations')]
453
454 def index_get_associations(self, path, owners=[], direction='forward'):
455 forward = 'forward' if direction == 'forward' else 'reverse'
456 reverse = 'reverse' if direction == 'forward' else 'forward'
457
458 associations = []
459 if not owners:
460 index = self.index.get(forward, {})
461 owners = index.get(path, {}).keys()
462
463 # f: forward
464 # r: reverse
465 for rassoc in self.index_get(forward, path, owners):
466 elements = rassoc.split('/')
467 rtype = ''.join(elements[-1:])
468 fendpoint = '/'.join(elements[:-1])
469 for fassoc in self.index_get(reverse, fendpoint, owners):
470 elements = fassoc.split('/')
471 ftype = ''.join(elements[-1:])
472 rendpoint = '/'.join(elements[:-1])
473 if rendpoint != path:
474 continue
475 associations.append((ftype, rtype, fendpoint))
476
477 return associations
478
479 def update_association(self, path, removed, added):
480 iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE
481 create = [] if self.manager.get(path, False) else [iface]
482
483 if added and create:
484 self.manager.add(
Brad Bishop56934372016-06-05 18:24:41 -0400485 path, Association(self.bus, path, added))
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400486 elif added:
487 self.manager.get(path).append(added)
488
489 obj = self.manager.get(path, None)
490 if obj and removed:
491 obj.remove(removed)
492
493 if obj and not obj.endpoints:
494 self.manager.remove(path)
495
496 delete = [] if self.manager.get(path, False) else [iface]
497
498 if create != delete:
499 self.update_interfaces(
500 path, self.unique, delete, create)
501
502 def update_associations(
503 self, path, owner, old, new, created=[], destroyed=[]):
504 added = list(set(new).difference(old))
505 removed = list(set(old).difference(new))
506 for forward, reverse, endpoint in added:
507 # update the index
508 forward_path = str(path + '/' + forward)
509 reverse_path = str(endpoint + '/' + reverse)
510 self.index_append(
511 'forward', path, owner, reverse_path)
512 self.index_append(
513 'reverse', endpoint, owner, forward_path)
514
515 # create the association if the endpoint exists
Brad Bishop10abe042016-05-02 23:11:19 -0400516 if not self.cache_get(endpoint):
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400517 continue
518
519 self.update_association(forward_path, [], [endpoint])
520 self.update_association(reverse_path, [], [path])
521
522 for forward, reverse, endpoint in removed:
523 # update the index
524 forward_path = str(path + '/' + forward)
525 reverse_path = str(endpoint + '/' + reverse)
526 self.index_remove(
527 'forward', path, owner, reverse_path)
528 self.index_remove(
529 'reverse', endpoint, owner, forward_path)
530
531 # destroy the association if it exists
532 self.update_association(forward_path, [endpoint], [])
533 self.update_association(reverse_path, [path], [])
534
535 # If the associations interface endpoint comes
536 # or goes create or destroy the appropriate
537 # associations
538 for path in created:
539 for forward, reverse, endpoint in \
540 self.index_get_associations(path, direction='reverse'):
541 forward_path = str(path + '/' + forward)
542 reverse_path = str(endpoint + '/' + reverse)
543 self.update_association(forward_path, [], [endpoint])
544 self.update_association(reverse_path, [], [path])
545
546 for path in destroyed:
547 for forward, reverse, endpoint in \
548 self.index_get_associations(path, direction='reverse'):
549 forward_path = str(path + '/' + forward)
550 reverse_path = str(endpoint + '/' + reverse)
551 self.update_association(forward_path, [endpoint], [])
552 self.update_association(reverse_path, [path], [])
553
554 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sa{sas}}')
555 def GetAncestors(self, path):
556 elements = filter(bool, path.split('/'))
557 paths = []
558 objs = {}
559 while elements:
560 elements.pop()
561 paths.append('/' + '/'.join(elements))
562 if path != '/':
563 paths.append('/')
564
565 for path in paths:
Brad Bishop10abe042016-05-02 23:11:19 -0400566 obj = self.cache_get(path)
567 if not obj:
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400568 continue
569 objs[path] = obj
570
571 return objs
572
Brad Bishop732c6db2015-10-19 14:49:21 -0400573
Brad Bishop732c6db2015-10-19 14:49:21 -0400574if __name__ == '__main__':
Brad Bishop4b849412016-02-04 15:40:26 -0500575 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
576 bus = dbus.SystemBus()
Brad Bishop56934372016-06-05 18:24:41 -0400577 o = ObjectMapper(bus, obmc.mapper.MAPPER_PATH)
Brad Bishop4b849412016-02-04 15:40:26 -0500578 loop = gobject.MainLoop()
Brad Bishop732c6db2015-10-19 14:49:21 -0400579
Brad Bishop4b849412016-02-04 15:40:26 -0500580 loop.run()