blob: ea30f1bbe078fe0483b1221b6bf72a642d180a8b [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,
177 dbus_interface=
178 dbus.BUS_DAEMON_IFACE,
179 signal_name='NameOwnerChanged')
Brad Bishop56934372016-06-05 18:24:41 -0400180 self.bus.add_signal_receiver(
Brad Bishop4b849412016-02-04 15:40:26 -0500181 self.interfaces_added_handler,
182 dbus_interface=
183 dbus.BUS_DAEMON_IFACE + '.ObjectManager',
184 signal_name='InterfacesAdded',
Brad Bishopc568e022016-03-23 14:50:05 -0400185 sender_keyword='sender',
186 path_keyword='sender_path')
Brad Bishop56934372016-06-05 18:24:41 -0400187 self.bus.add_signal_receiver(
Brad Bishop4b849412016-02-04 15:40:26 -0500188 self.interfaces_removed_handler,
189 dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager',
190 signal_name='InterfacesRemoved',
Brad Bishopc568e022016-03-23 14:50:05 -0400191 sender_keyword='sender',
192 path_keyword='sender_path')
Brad Bishop56934372016-06-05 18:24:41 -0400193 self.bus.add_signal_receiver(
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400194 self.properties_changed_handler,
195 dbus_interface=dbus.PROPERTIES_IFACE,
196 signal_name='PropertiesChanged',
197 path_keyword='path',
198 sender_keyword='sender')
Brad Bishop732c6db2015-10-19 14:49:21 -0400199
Brad Bishop9cff26b2016-03-18 11:16:47 -0400200 def bus_match(self, owner):
201 # Ignore my own signals
202 return owner != obmc.mapper.MAPPER_NAME and \
203 self.name_match(owner)
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500204
Brad Bishop4b849412016-02-04 15:40:26 -0500205 def discovery_pending(self):
206 return not bool(self.service)
Brad Bishopcb7aa602015-11-03 10:40:17 -0500207
Brad Bishop10abe042016-05-02 23:11:19 -0400208 def cache_get(self, path):
209 cache_entry = self.cache.get(path, {})
210 if cache_entry is None:
211 # hide path elements without any interfaces
212 cache_entry = {}
213 return cache_entry
214
Brad Bishopc568e022016-03-23 14:50:05 -0400215 def add_new_objmgr(self, path, owner):
216 # We don't get a signal for the ObjectManager
217 # interface itself, so if we see a signal from
218 # make sure its in our cache, and add it if not.
Brad Bishop10abe042016-05-02 23:11:19 -0400219 cache_entry = self.cache_get(path)
Brad Bishopc568e022016-03-23 14:50:05 -0400220 old = self.interfaces_get(cache_entry, owner)
221 new = list(set(old).union([dbus.BUS_DAEMON_IFACE + '.ObjectManager']))
222 self.update_interfaces(path, owner, old, new)
223
Brad Bishop4b849412016-02-04 15:40:26 -0500224 def interfaces_added_handler(self, path, iprops, **kw):
Brad Bishopfed968e2016-03-18 10:47:26 -0400225 path = str(path)
226 owner = str(kw['sender'])
227 interfaces = self.get_signal_interfaces(owner, iprops.iterkeys())
Brad Bishop10abe042016-05-02 23:11:19 -0400228 if interfaces:
229 self.add_new_objmgr(str(kw['sender_path']), owner)
Brad Bishop56934372016-06-05 18:24:41 -0400230 cache_entry = self.cache_get(path)
231 old = self.interfaces_get(cache_entry, owner)
232 new = list(set(interfaces).union(old))
233 self.update_interfaces(path, owner, old, new)
Brad Bishop732c6db2015-10-19 14:49:21 -0400234
Brad Bishop4b849412016-02-04 15:40:26 -0500235 def interfaces_removed_handler(self, path, interfaces, **kw):
Brad Bishopfed968e2016-03-18 10:47:26 -0400236 path = str(path)
237 owner = str(kw['sender'])
238 interfaces = self.get_signal_interfaces(owner, interfaces)
Brad Bishop10abe042016-05-02 23:11:19 -0400239 if interfaces:
240 self.add_new_objmgr(str(kw['sender_path']), owner)
Brad Bishop56934372016-06-05 18:24:41 -0400241 cache_entry = self.cache_get(path)
242 old = self.interfaces_get(cache_entry, owner)
243 new = list(set(old).difference(interfaces))
244 self.update_interfaces(path, owner, old, new)
Brad Bishop732c6db2015-10-19 14:49:21 -0400245
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400246 def properties_changed_handler(self, interface, new, old, **kw):
247 owner = str(kw['sender'])
248 path = str(kw['path'])
249 interfaces = self.get_signal_interfaces(owner, [interface])
250 if not self.is_association(interfaces):
251 return
252 associations = new.get('associations', None)
253 if associations is None:
254 return
255
256 associations = [
257 (str(x), str(y), str(z)) for x, y, z in associations]
258 self.update_associations(
259 path, owner,
260 self.index_get_associations(path, [owner]),
261 associations)
262
Brad Bishop56934372016-06-05 18:24:41 -0400263 def process_new_owner(self, owned_name, owner):
Brad Bishop4b849412016-02-04 15:40:26 -0500264 # unique name
Brad Bishopba86bc82016-05-05 22:09:27 -0400265 try:
Brad Bishop56934372016-06-05 18:24:41 -0400266 return self.discover([(owned_name, owner)])
Brad Bishopba86bc82016-05-05 22:09:27 -0400267 except dbus.exceptions.DBusException, e:
268 if obmc.dbuslib.enums.DBUS_UNKNOWN_SERVICE \
269 not in e.get_dbus_name():
270 raise
Brad Bishop732c6db2015-10-19 14:49:21 -0400271
Brad Bishop56934372016-06-05 18:24:41 -0400272 def process_old_owner(self, owned_name, owner):
273 if owner in self.bus_map:
274 del self.bus_map[owner]
275
Brad Bishopfed968e2016-03-18 10:47:26 -0400276 for path, item in self.cache.dataitems():
Brad Bishop9cff26b2016-03-18 11:16:47 -0400277 old = self.interfaces_get(item, owner)
Brad Bishopfed968e2016-03-18 10:47:26 -0400278 # remove all interfaces for this service
279 self.update_interfaces(
Brad Bishop9cff26b2016-03-18 11:16:47 -0400280 path, owner, old=old, new=[])
Brad Bishop732c6db2015-10-19 14:49:21 -0400281
Brad Bishop56934372016-06-05 18:24:41 -0400282 def bus_handler(self, owned_name, old, new):
Brad Bishopfed968e2016-03-18 10:47:26 -0400283 valid = False
Brad Bishop56934372016-06-05 18:24:41 -0400284 if not obmc.dbuslib.bindings.is_unique(owned_name):
285 valid = self.valid_signal(owned_name)
Brad Bishop732c6db2015-10-19 14:49:21 -0400286
Brad Bishopfed968e2016-03-18 10:47:26 -0400287 if valid and new:
Brad Bishop56934372016-06-05 18:24:41 -0400288 self.process_new_owner(owned_name, new)
Brad Bishopfed968e2016-03-18 10:47:26 -0400289 if valid and old:
Brad Bishop56934372016-06-05 18:24:41 -0400290 self.process_old_owner(owned_name, old)
Brad Bishop732c6db2015-10-19 14:49:21 -0400291
Brad Bishopfed968e2016-03-18 10:47:26 -0400292 def update_interfaces(self, path, owner, old, new):
293 cache_entry = self.cache.setdefault(path, {})
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400294 created = [] if self.has_interfaces(cache_entry) else [path]
Brad Bishopfed968e2016-03-18 10:47:26 -0400295 added = list(set(new).difference(old))
296 removed = list(set(old).difference(new))
297 self.interfaces_append(cache_entry, owner, added)
298 self.interfaces_remove(cache_entry, owner, removed, path)
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400299 destroyed = [] if self.has_interfaces(cache_entry) else [path]
300
301 # react to anything that requires association updates
302 new_assoc = []
303 old_assoc = []
304 if self.is_association(added):
305 new_assoc = self.dbus_get_associations(path, owner)
306 if self.is_association(removed):
307 old_assoc = self.index_get_associations(path, [owner])
308 self.update_associations(
309 path, owner, old_assoc, new_assoc, created, destroyed)
Brad Bishop86398d22015-10-28 21:58:31 -0400310
Brad Bishop4b849412016-02-04 15:40:26 -0500311 def add_items(self, owner, bus_items):
Brad Bishopfed968e2016-03-18 10:47:26 -0400312 for path, items in bus_items.iteritems():
313 # convert dbus types to native.
Brad Bishopba86bc82016-05-05 22:09:27 -0400314 interfaces = [str(i) for i in items]
Brad Bishopfed968e2016-03-18 10:47:26 -0400315 self.update_interfaces(path, str(owner), old=[], new=interfaces)
Brad Bishop86398d22015-10-28 21:58:31 -0400316
Brad Bishop9cff26b2016-03-18 11:16:47 -0400317 def discover(self, owners=[]):
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400318 def match(iface):
319 return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' or \
320 self.intf_match(iface)
Brad Bishop4b849412016-02-04 15:40:26 -0500321 if not owners:
Brad Bishop56934372016-06-05 18:24:41 -0400322 owned_names = [x for x in self.bus.list_names()
323 if self.bus_match(x)]
324 owners = [self.bus.get_name_owner(x) for x in owned_names]
325 owners = zip(owned_names, owners)
326 for owned_name, o in owners:
Brad Bishopba86bc82016-05-05 22:09:27 -0400327 self.add_items(
328 o,
Brad Bishop56934372016-06-05 18:24:41 -0400329 find_dbus_interfaces(self.bus, o, '/', self.intf_match))
330 self.bus_map[o] = owned_name
Brad Bishop732c6db2015-10-19 14:49:21 -0400331
Brad Bishop4b849412016-02-04 15:40:26 -0500332 if self.discovery_pending():
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400333 # add my object mananger instance
Brad Bishop56934372016-06-05 18:24:41 -0400334 self.bus_map[self.unique] = obmc.mapper.MAPPER_NAME
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400335 self.add_items(
336 self.unique,
337 {obmc.dbuslib.bindings.OBJ_PREFIX:
338 {'interfaces':
339 [dbus.BUS_DAEMON_IFACE + '.ObjectManager']}})
340
Brad Bishop4b849412016-02-04 15:40:26 -0500341 print "ObjectMapper discovery complete..."
342 self.service = dbus.service.BusName(
Brad Bishop56934372016-06-05 18:24:41 -0400343 obmc.mapper.MAPPER_NAME, self.bus)
Brad Bishop732c6db2015-10-19 14:49:21 -0400344
Brad Bishop56934372016-06-05 18:24:41 -0400345 def valid_signal(self, name):
346 if self.discovery_pending():
347 return False
Brad Bishopfed968e2016-03-18 10:47:26 -0400348
Brad Bishop56934372016-06-05 18:24:41 -0400349 if obmc.dbuslib.bindings.is_unique(name):
350 name = self.bus_map.get(name)
351
352 return name is not None and \
353 self.bus_match(name)
Brad Bishopfed968e2016-03-18 10:47:26 -0400354
355 def get_signal_interfaces(self, owner, interfaces):
356 filtered = []
357 if self.valid_signal(owner):
358 filtered = [str(x) for x in interfaces if self.intf_match(x)]
359
360 return filtered
361
362 @staticmethod
363 def interfaces_get(item, owner, default=[]):
364 return item.get(owner, default)
365
366 @staticmethod
367 def interfaces_append(item, owner, append):
368 interfaces = item.setdefault(owner, [])
369 item[owner] = list(set(append).union(interfaces))
370
371 def interfaces_remove(self, item, owner, remove, path):
372 interfaces = item.get(owner, [])
373 item[owner] = list(set(interfaces).difference(remove))
374
375 if not item[owner]:
376 # remove the owner if there aren't any interfaces left
377 del item[owner]
378
379 if item:
380 # other owners remain
381 return
382
383 if self.cache.get_children(path):
384 # there are still references to this path
385 # from objects further down the tree.
386 # mark it for removal if that changes
387 self.cache.demote(path)
388 else:
389 # delete the entire path if everything is gone
390 del self.cache[path]
391
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500392 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sas}')
Brad Bishop4b849412016-02-04 15:40:26 -0500393 def GetObject(self, path):
Brad Bishop10abe042016-05-02 23:11:19 -0400394 o = self.cache_get(path)
Brad Bishop4b849412016-02-04 15:40:26 -0500395 if not o:
396 raise MapperNotFoundException(path)
397 return o
Brad Bishop732c6db2015-10-19 14:49:21 -0400398
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500399 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'as')
Brad Bishop4b849412016-02-04 15:40:26 -0500400 def GetSubTreePaths(self, path, depth):
401 try:
402 return self.cache.iterkeys(path, depth)
403 except KeyError:
404 raise MapperNotFoundException(path)
Brad Bishop732c6db2015-10-19 14:49:21 -0400405
Brad Bishopfb1f58e2016-03-04 15:19:13 -0500406 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'a{sa{sas}}')
Brad Bishop4b849412016-02-04 15:40:26 -0500407 def GetSubTree(self, path, depth):
408 try:
409 return {x: y for x, y in self.cache.dataitems(path, depth)}
410 except KeyError:
411 raise MapperNotFoundException(path)
412
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400413 @staticmethod
414 def has_interfaces(item):
415 for owner in item.iterkeys():
416 if ObjectMapper.interfaces_get(item, owner):
417 return True
418 return False
419
420 @staticmethod
421 def is_association(interfaces):
422 return obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE in interfaces
423
424 def index_get(self, index, path, owners):
425 items = []
426 item = self.index.get(index, {})
427 item = item.get(path, {})
428 for o in owners:
429 items.extend(item.get(o, []))
430 return items
431
432 def index_append(self, index, path, owner, assoc):
433 item = self.index.setdefault(index, {})
434 item = item.setdefault(path, {})
435 item = item.setdefault(owner, [])
436 item.append(assoc)
437
438 def index_remove(self, index, path, owner, assoc):
439 index = self.index.get(index, {})
440 owners = index.get(path, {})
441 items = owners.get(owner, [])
442 if assoc in items:
443 items.remove(assoc)
444 if not items:
445 del owners[owner]
446 if not owners:
447 del index[path]
448
449 def dbus_get_associations(self, path, owner):
Brad Bishop56934372016-06-05 18:24:41 -0400450 obj = self.bus.get_object(owner, path, introspect=False)
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400451 iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
452 return [(str(f), str(r), str(e)) for f, r, e in iface.Get(
453 obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE,
454 'associations')]
455
456 def index_get_associations(self, path, owners=[], direction='forward'):
457 forward = 'forward' if direction == 'forward' else 'reverse'
458 reverse = 'reverse' if direction == 'forward' else 'forward'
459
460 associations = []
461 if not owners:
462 index = self.index.get(forward, {})
463 owners = index.get(path, {}).keys()
464
465 # f: forward
466 # r: reverse
467 for rassoc in self.index_get(forward, path, owners):
468 elements = rassoc.split('/')
469 rtype = ''.join(elements[-1:])
470 fendpoint = '/'.join(elements[:-1])
471 for fassoc in self.index_get(reverse, fendpoint, owners):
472 elements = fassoc.split('/')
473 ftype = ''.join(elements[-1:])
474 rendpoint = '/'.join(elements[:-1])
475 if rendpoint != path:
476 continue
477 associations.append((ftype, rtype, fendpoint))
478
479 return associations
480
481 def update_association(self, path, removed, added):
482 iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE
483 create = [] if self.manager.get(path, False) else [iface]
484
485 if added and create:
486 self.manager.add(
Brad Bishop56934372016-06-05 18:24:41 -0400487 path, Association(self.bus, path, added))
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400488 elif added:
489 self.manager.get(path).append(added)
490
491 obj = self.manager.get(path, None)
492 if obj and removed:
493 obj.remove(removed)
494
495 if obj and not obj.endpoints:
496 self.manager.remove(path)
497
498 delete = [] if self.manager.get(path, False) else [iface]
499
500 if create != delete:
501 self.update_interfaces(
502 path, self.unique, delete, create)
503
504 def update_associations(
505 self, path, owner, old, new, created=[], destroyed=[]):
506 added = list(set(new).difference(old))
507 removed = list(set(old).difference(new))
508 for forward, reverse, endpoint in added:
509 # update the index
510 forward_path = str(path + '/' + forward)
511 reverse_path = str(endpoint + '/' + reverse)
512 self.index_append(
513 'forward', path, owner, reverse_path)
514 self.index_append(
515 'reverse', endpoint, owner, forward_path)
516
517 # create the association if the endpoint exists
Brad Bishop10abe042016-05-02 23:11:19 -0400518 if not self.cache_get(endpoint):
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400519 continue
520
521 self.update_association(forward_path, [], [endpoint])
522 self.update_association(reverse_path, [], [path])
523
524 for forward, reverse, endpoint in removed:
525 # update the index
526 forward_path = str(path + '/' + forward)
527 reverse_path = str(endpoint + '/' + reverse)
528 self.index_remove(
529 'forward', path, owner, reverse_path)
530 self.index_remove(
531 'reverse', endpoint, owner, forward_path)
532
533 # destroy the association if it exists
534 self.update_association(forward_path, [endpoint], [])
535 self.update_association(reverse_path, [path], [])
536
537 # If the associations interface endpoint comes
538 # or goes create or destroy the appropriate
539 # associations
540 for path in created:
541 for forward, reverse, endpoint in \
542 self.index_get_associations(path, direction='reverse'):
543 forward_path = str(path + '/' + forward)
544 reverse_path = str(endpoint + '/' + reverse)
545 self.update_association(forward_path, [], [endpoint])
546 self.update_association(reverse_path, [], [path])
547
548 for path in destroyed:
549 for forward, reverse, endpoint in \
550 self.index_get_associations(path, direction='reverse'):
551 forward_path = str(path + '/' + forward)
552 reverse_path = str(endpoint + '/' + reverse)
553 self.update_association(forward_path, [endpoint], [])
554 self.update_association(reverse_path, [path], [])
555
556 @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sa{sas}}')
557 def GetAncestors(self, path):
558 elements = filter(bool, path.split('/'))
559 paths = []
560 objs = {}
561 while elements:
562 elements.pop()
563 paths.append('/' + '/'.join(elements))
564 if path != '/':
565 paths.append('/')
566
567 for path in paths:
Brad Bishop10abe042016-05-02 23:11:19 -0400568 obj = self.cache_get(path)
569 if not obj:
Brad Bishopd9fc21c2016-03-18 12:24:42 -0400570 continue
571 objs[path] = obj
572
573 return objs
574
Brad Bishop732c6db2015-10-19 14:49:21 -0400575
Brad Bishop732c6db2015-10-19 14:49:21 -0400576if __name__ == '__main__':
Brad Bishop4b849412016-02-04 15:40:26 -0500577 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
578 bus = dbus.SystemBus()
Brad Bishop56934372016-06-05 18:24:41 -0400579 o = ObjectMapper(bus, obmc.mapper.MAPPER_PATH)
Brad Bishop4b849412016-02-04 15:40:26 -0500580 loop = gobject.MainLoop()
Brad Bishop732c6db2015-10-19 14:49:21 -0400581
Brad Bishop4b849412016-02-04 15:40:26 -0500582 loop.run()