Brad Bishop | eded8f3 | 2017-11-01 11:22:38 -0400 | [diff] [blame] | 1 | # Contributors Listed Below - COPYRIGHT 2017 |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 2 | # [+] International Business Machines Corp. |
| 3 | # |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | |
| 17 | import dbus |
| 18 | import dbus.service |
| 19 | import dbus.exceptions |
| 20 | import dbus.mainloop.glib |
CamVan Nguyen | 2fd4b1f | 2018-03-05 12:19:46 -0600 | [diff] [blame] | 21 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 22 | try: # python 2 |
| 23 | import gobject |
| 24 | except ImportError: # python 3 |
| 25 | from gi.repository import GObject as gobject |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 26 | import xml.etree.ElementTree as ET |
| 27 | import obmc.utils.pathtree |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 28 | import obmc.mapper |
| 29 | import obmc.dbuslib.bindings |
| 30 | import obmc.dbuslib.enums |
Brad Bishop | 99b8bc8 | 2017-07-29 21:39:52 -0400 | [diff] [blame] | 31 | import sys |
| 32 | import traceback |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 33 | from itertools import chain |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 34 | |
| 35 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 36 | class MapperBusyException(dbus.exceptions.DBusException): |
| 37 | _dbus_error_name = 'org.freedesktop.DBus.Error.ObjectPathInUse' |
| 38 | |
| 39 | def __init__(self): |
| 40 | super(MapperBusyException, self).__init__( |
| 41 | 'busy processing bus traffic') |
| 42 | |
| 43 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 44 | class MapperNotFoundException(dbus.exceptions.DBusException): |
| 45 | _dbus_error_name = obmc.mapper.MAPPER_NOT_FOUND |
| 46 | |
| 47 | def __init__(self, path): |
| 48 | super(MapperNotFoundException, self).__init__( |
| 49 | "path or object not found: %s" % path) |
| 50 | |
| 51 | |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 52 | def find_dbus_interfaces(conn, service, path, callback, error_callback, **kw): |
Brad Bishop | bd8aa05 | 2016-09-19 09:30:06 -0400 | [diff] [blame] | 53 | iface_match = kw.pop('iface_match', bool) |
Brad Bishop | 6a0320b | 2016-09-19 11:03:06 -0400 | [diff] [blame] | 54 | subtree_match = kw.pop('subtree_match', bool) |
Brad Bishop | bd8aa05 | 2016-09-19 09:30:06 -0400 | [diff] [blame] | 55 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 56 | class _FindInterfaces(object): |
| 57 | def __init__(self): |
| 58 | self.results = {} |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 59 | self.introspect_pending = [] |
| 60 | self.gmo_pending = [] |
| 61 | self.assoc_pending = [] |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 62 | |
| 63 | @staticmethod |
| 64 | def _to_path(elements): |
| 65 | return '/' + '/'.join(elements) |
| 66 | |
| 67 | @staticmethod |
| 68 | def _to_path_elements(path): |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 69 | return filter(bool, path.split('/')) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 70 | |
| 71 | def __call__(self, path): |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 72 | try: |
| 73 | self._find_interfaces(path) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 74 | except Exception as e: |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 75 | error_callback(service, path, e) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 76 | |
| 77 | @staticmethod |
| 78 | def _match(iface): |
| 79 | return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' \ |
Brad Bishop | bd8aa05 | 2016-09-19 09:30:06 -0400 | [diff] [blame] | 80 | or iface_match(iface) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 81 | |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 82 | def check_done(self): |
| 83 | if any([ |
| 84 | self.introspect_pending, |
| 85 | self.gmo_pending, |
| 86 | self.assoc_pending]): |
| 87 | return |
| 88 | |
| 89 | callback(service, self.results) |
| 90 | |
| 91 | def _assoc_callback(self, path, associations): |
| 92 | try: |
| 93 | iface = obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE |
| 94 | self.assoc_pending.remove(path) |
Gunnar Mills | 296395c | 2017-09-06 13:56:43 -0500 | [diff] [blame] | 95 | self.results[path][iface]['associations'] = associations |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 96 | except Exception as e: |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 97 | error_callback(service, path, e) |
| 98 | return None |
| 99 | |
| 100 | self.check_done() |
| 101 | |
| 102 | def _gmo_callback(self, path, objs): |
| 103 | try: |
| 104 | self.gmo_pending.remove(path) |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 105 | for k, v in objs.items(): |
| 106 | self.results[k] = dict(x for x in v.items() |
| 107 | if iface_match(x[0])) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 108 | except Exception as e: |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 109 | error_callback(service, path, e) |
| 110 | return None |
| 111 | |
| 112 | self.check_done() |
| 113 | |
| 114 | def _introspect_callback(self, path, data): |
| 115 | self.introspect_pending.remove(path) |
| 116 | if data is None: |
| 117 | self.check_done() |
| 118 | return |
| 119 | |
| 120 | try: |
| 121 | path_elements = self._to_path_elements(path) |
| 122 | root = ET.fromstring(data) |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 123 | all_ifaces = root.findall('interface') |
| 124 | ifaces = filter(self._match, |
| 125 | [x.attrib.get('name') for x in all_ifaces]) |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 126 | ifaces = {x: {} for x in ifaces} |
| 127 | self.results[path] = ifaces |
| 128 | |
| 129 | if obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE in ifaces: |
| 130 | obj = conn.get_object(service, path, introspect=False) |
| 131 | iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE) |
| 132 | self.assoc_pending.append(path) |
| 133 | iface.Get.call_async( |
| 134 | obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE, |
| 135 | 'associations', |
| 136 | reply_handler=lambda x: self._assoc_callback( |
| 137 | path, x), |
| 138 | error_handler=lambda e: error_callback( |
| 139 | service, path, e)) |
| 140 | |
| 141 | if dbus.BUS_DAEMON_IFACE + '.ObjectManager' in ifaces: |
| 142 | obj = conn.get_object(service, path, introspect=False) |
| 143 | iface = dbus.Interface( |
| 144 | obj, dbus.BUS_DAEMON_IFACE + '.ObjectManager') |
| 145 | self.gmo_pending.append(path) |
| 146 | iface.GetManagedObjects.call_async( |
| 147 | reply_handler=lambda x: self._gmo_callback( |
| 148 | path, x), |
| 149 | error_handler=lambda e: error_callback( |
| 150 | service, path, e)) |
| 151 | else: |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 152 | children = filter( |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 153 | bool, |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 154 | [x.attrib.get('name') for x in root.findall('node')]) |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 155 | children = [ |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 156 | self._to_path(chain(path_elements, |
| 157 | self._to_path_elements(x))) |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 158 | for x in sorted(children)] |
| 159 | for child in filter(subtree_match, children): |
| 160 | if child not in self.results: |
| 161 | self._find_interfaces(child) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 162 | except Exception as e: |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 163 | error_callback(service, path, e) |
| 164 | return None |
| 165 | |
| 166 | self.check_done() |
| 167 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 168 | def _find_interfaces(self, path): |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 169 | path = self._to_path(self._to_path_elements(path)) |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 170 | obj = conn.get_object(service, path, introspect=False) |
| 171 | iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE) |
| 172 | self.introspect_pending.append(path) |
| 173 | iface.Introspect.call_async( |
| 174 | reply_handler=lambda x: self._introspect_callback(path, x), |
| 175 | error_handler=lambda x: error_callback(service, path, x)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 176 | |
| 177 | return _FindInterfaces()(path) |
| 178 | |
| 179 | |
Brad Bishop | c33ae65 | 2017-11-02 22:23:09 -0400 | [diff] [blame] | 180 | @obmc.dbuslib.bindings.add_interfaces([obmc.dbuslib.enums.OBMC_ASSOC_IFACE]) |
| 181 | class Association(obmc.dbuslib.bindings.DbusProperties): |
Brad Bishop | 734b2c3 | 2017-11-01 15:40:07 -0400 | [diff] [blame] | 182 | """Implementation of org.openbmc.Association.""" |
| 183 | |
Brad Bishop | b9b3ed5 | 2017-11-01 21:40:31 -0400 | [diff] [blame] | 184 | iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE |
| 185 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 186 | def __init__(self, bus, path, endpoints): |
Brad Bishop | 734b2c3 | 2017-11-01 15:40:07 -0400 | [diff] [blame] | 187 | """Construct an Association. |
| 188 | |
| 189 | Arguments: |
| 190 | bus -- The python-dbus connection to host the interface |
| 191 | path -- The D-Bus object path on which to implement the interface |
| 192 | endpoints -- A list of the initial association endpoints |
| 193 | """ |
Brad Bishop | 70dd595 | 2016-09-08 22:33:33 -0400 | [diff] [blame] | 194 | super(Association, self).__init__(conn=bus, object_path=path) |
Brad Bishop | b9b3ed5 | 2017-11-01 21:40:31 -0400 | [diff] [blame] | 195 | self.properties = {self.iface: {'endpoints': endpoints}} |
Brad Bishop | bcc0644 | 2018-01-29 14:54:51 -0500 | [diff] [blame] | 196 | self.unmask_signals() |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 197 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 198 | |
| 199 | class Manager(obmc.dbuslib.bindings.DbusObjectManager): |
| 200 | def __init__(self, bus, path): |
Brad Bishop | 70dd595 | 2016-09-08 22:33:33 -0400 | [diff] [blame] | 201 | super(Manager, self).__init__(conn=bus, object_path=path) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 202 | |
| 203 | |
| 204 | class ObjectMapper(dbus.service.Object): |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 205 | def __init__( |
| 206 | self, bus, path, namespaces, interface_namespaces, |
| 207 | blacklist, interface_blacklist): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 208 | super(ObjectMapper, self).__init__(bus, path) |
| 209 | self.cache = obmc.utils.pathtree.PathTree() |
| 210 | self.bus = bus |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 211 | self.service = None |
| 212 | self.index = {} |
| 213 | self.manager = Manager(bus, obmc.dbuslib.bindings.OBJ_PREFIX) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 214 | self.bus_map = {} |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 215 | self.defer_signals = {} |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 216 | self.namespaces = namespaces |
| 217 | self.interface_namespaces = interface_namespaces |
| 218 | self.blacklist = blacklist |
| 219 | self.blacklist.append(obmc.mapper.MAPPER_PATH) |
| 220 | self.interface_blacklist = interface_blacklist |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 221 | |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 222 | # add my object mananger instance |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 223 | self.add_new_objmgr( |
| 224 | obmc.dbuslib.bindings.OBJ_PREFIX, obmc.mapper.MAPPER_NAME) |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 225 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 226 | self.bus.add_signal_receiver( |
| 227 | self.bus_handler, |
| 228 | dbus_interface=dbus.BUS_DAEMON_IFACE, |
| 229 | signal_name='NameOwnerChanged') |
| 230 | self.bus.add_signal_receiver( |
| 231 | self.interfaces_added_handler, |
| 232 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 233 | signal_name='InterfacesAdded', |
| 234 | sender_keyword='sender', |
| 235 | path_keyword='sender_path') |
| 236 | self.bus.add_signal_receiver( |
| 237 | self.interfaces_removed_handler, |
| 238 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 239 | signal_name='InterfacesRemoved', |
| 240 | sender_keyword='sender', |
| 241 | path_keyword='sender_path') |
| 242 | self.bus.add_signal_receiver( |
| 243 | self.properties_changed_handler, |
| 244 | dbus_interface=dbus.PROPERTIES_IFACE, |
| 245 | signal_name='PropertiesChanged', |
Brad Bishop | b270adc | 2017-11-14 23:32:59 -0500 | [diff] [blame] | 246 | arg0=obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE, |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 247 | path_keyword='path', |
| 248 | sender_keyword='sender') |
| 249 | |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 250 | print("ObjectMapper startup complete. Discovery in progress...") |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 251 | self.discover() |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 252 | gobject.idle_add(self.claim_name) |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 253 | |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 254 | def claim_name(self): |
| 255 | if len(self.defer_signals): |
| 256 | return True |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 257 | print("ObjectMapper discovery complete") |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 258 | self.service = dbus.service.BusName( |
| 259 | obmc.mapper.MAPPER_NAME, self.bus) |
Brad Bishop | 55b89cd | 2016-09-19 23:02:48 -0400 | [diff] [blame] | 260 | self.manager.unmask_signals() |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 261 | return False |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 262 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 263 | def discovery_callback(self, owner, items): |
| 264 | if owner in self.defer_signals: |
| 265 | self.add_items(owner, items) |
| 266 | pending = self.defer_signals[owner] |
| 267 | del self.defer_signals[owner] |
| 268 | |
| 269 | for x in pending: |
| 270 | x() |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 271 | self.IntrospectionComplete(owner) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 272 | |
| 273 | def discovery_error(self, owner, path, e): |
Brad Bishop | 99b8bc8 | 2017-07-29 21:39:52 -0400 | [diff] [blame] | 274 | '''Log a message and remove all traces of the service |
| 275 | we were attempting to introspect.''' |
| 276 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 277 | if owner in self.defer_signals: |
Brad Bishop | 7a79027 | 2017-12-14 21:25:24 -0500 | [diff] [blame] | 278 | |
| 279 | # Safe to add a reference to the traceback here, |
| 280 | # since it cannot contain the discovery_error frame. |
| 281 | exctype, value, tb = sys.exc_info() |
Brad Bishop | 99b8bc8 | 2017-07-29 21:39:52 -0400 | [diff] [blame] | 282 | sys.stderr.write( |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 283 | '{} discovery failure on {}\n'.format(owner, path)) |
Brad Bishop | 7a79027 | 2017-12-14 21:25:24 -0500 | [diff] [blame] | 284 | if tb: |
| 285 | traceback.print_exception(exctype, value, tb, file=sys.stderr) |
| 286 | else: |
| 287 | sys.stderr.write('{}: {}\n'.format(e.__class__.__name__, e)) |
| 288 | |
Brad Bishop | 99b8bc8 | 2017-07-29 21:39:52 -0400 | [diff] [blame] | 289 | del self.defer_signals[owner] |
| 290 | del self.bus_map[owner] |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 291 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 292 | def cache_get(self, path): |
| 293 | cache_entry = self.cache.get(path, {}) |
| 294 | if cache_entry is None: |
| 295 | # hide path elements without any interfaces |
| 296 | cache_entry = {} |
| 297 | return cache_entry |
| 298 | |
| 299 | def add_new_objmgr(self, path, owner): |
| 300 | # We don't get a signal for the ObjectManager |
| 301 | # interface itself, so if we see a signal from |
| 302 | # make sure its in our cache, and add it if not. |
| 303 | cache_entry = self.cache_get(path) |
| 304 | old = self.interfaces_get(cache_entry, owner) |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 305 | new = set(old).union([dbus.BUS_DAEMON_IFACE + '.ObjectManager']) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 306 | self.update_interfaces(path, owner, old, new) |
| 307 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 308 | def defer_signal(self, owner, callback): |
| 309 | self.defer_signals.setdefault(owner, []).append(callback) |
| 310 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 311 | def interfaces_added_handler(self, path, iprops, **kw): |
| 312 | path = str(path) |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 313 | owner = self.bus_normalize(str(kw['sender'])) |
| 314 | if not owner: |
Brad Bishop | 787aa81 | 2018-01-28 23:42:03 -0500 | [diff] [blame] | 315 | return |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 316 | interfaces = self.filter_signal_interfaces(iprops.keys()) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 317 | if not interfaces: |
| 318 | return |
| 319 | |
| 320 | if owner not in self.defer_signals: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 321 | self.add_new_objmgr(str(kw['sender_path']), owner) |
| 322 | cache_entry = self.cache_get(path) |
| 323 | old = self.interfaces_get(cache_entry, owner) |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 324 | new = set(interfaces).union(old) |
Brad Bishop | a623596 | 2017-06-07 23:56:54 -0400 | [diff] [blame] | 325 | new = {x: iprops.get(x, {}) for x in new} |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 326 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 327 | else: |
| 328 | self.defer_signal( |
| 329 | owner, |
| 330 | lambda: self.interfaces_added_handler( |
| 331 | path, iprops, **kw)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 332 | |
| 333 | def interfaces_removed_handler(self, path, interfaces, **kw): |
| 334 | path = str(path) |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 335 | owner = self.bus_normalize(str(kw['sender'])) |
| 336 | if not owner: |
Brad Bishop | 787aa81 | 2018-01-28 23:42:03 -0500 | [diff] [blame] | 337 | return |
| 338 | interfaces = self.filter_signal_interfaces(interfaces) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 339 | if not interfaces: |
| 340 | return |
| 341 | |
| 342 | if owner not in self.defer_signals: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 343 | self.add_new_objmgr(str(kw['sender_path']), owner) |
| 344 | cache_entry = self.cache_get(path) |
| 345 | old = self.interfaces_get(cache_entry, owner) |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 346 | new = set(old).difference(interfaces) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 347 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 348 | else: |
| 349 | self.defer_signal( |
| 350 | owner, |
| 351 | lambda: self.interfaces_removed_handler( |
| 352 | path, interfaces, **kw)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 353 | |
| 354 | def properties_changed_handler(self, interface, new, old, **kw): |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 355 | owner = self.bus_normalize(str(kw['sender'])) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 356 | path = str(kw['path']) |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 357 | if not owner: |
Brad Bishop | 787aa81 | 2018-01-28 23:42:03 -0500 | [diff] [blame] | 358 | return |
| 359 | interfaces = self.filter_signal_interfaces([interface]) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 360 | if not self.is_association(interfaces): |
| 361 | return |
| 362 | associations = new.get('associations', None) |
| 363 | if associations is None: |
| 364 | return |
| 365 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 366 | if owner not in self.defer_signals: |
| 367 | associations = [ |
| 368 | (str(x), str(y), str(z)) for x, y, z in associations] |
| 369 | self.update_associations( |
| 370 | path, owner, |
| 371 | self.index_get_associations(path, [owner]), |
| 372 | associations) |
| 373 | else: |
| 374 | self.defer_signal( |
| 375 | owner, |
| 376 | lambda: self.properties_changed_handler( |
| 377 | interface, new, old, **kw)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 378 | |
| 379 | def process_new_owner(self, owned_name, owner): |
| 380 | # unique name |
| 381 | try: |
| 382 | return self.discover([(owned_name, owner)]) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 383 | except dbus.exceptions.DBusException as e: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 384 | if obmc.dbuslib.enums.DBUS_UNKNOWN_SERVICE \ |
| 385 | not in e.get_dbus_name(): |
| 386 | raise |
| 387 | |
| 388 | def process_old_owner(self, owned_name, owner): |
| 389 | if owner in self.bus_map: |
| 390 | del self.bus_map[owner] |
| 391 | |
| 392 | for path, item in self.cache.dataitems(): |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 393 | old = self.interfaces_get(item, owned_name) |
Andrew Jeffery | 005e47a | 2018-05-10 02:17:34 +0930 | [diff] [blame^] | 394 | if old: |
| 395 | # remove all interfaces for this service |
| 396 | self.update_interfaces( |
| 397 | path, owned_name, old=old, new=[]) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 398 | |
| 399 | def bus_handler(self, owned_name, old, new): |
Brad Bishop | 45271cb | 2018-01-28 23:56:05 -0500 | [diff] [blame] | 400 | if obmc.dbuslib.bindings.is_unique(owned_name) or \ |
| 401 | owned_name == obmc.mapper.MAPPER_NAME: |
| 402 | return |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 403 | |
Brad Bishop | 45271cb | 2018-01-28 23:56:05 -0500 | [diff] [blame] | 404 | if new: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 405 | self.process_new_owner(owned_name, new) |
Brad Bishop | 45271cb | 2018-01-28 23:56:05 -0500 | [diff] [blame] | 406 | if old: |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 407 | # discard any unhandled signals |
| 408 | # or in progress discovery |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 409 | if owned_name in self.defer_signals: |
| 410 | del self.defer_signals[owned_name] |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 411 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 412 | self.process_old_owner(owned_name, old) |
| 413 | |
| 414 | def update_interfaces(self, path, owner, old, new): |
| 415 | # __xx -> intf list |
| 416 | # xx -> intf dict |
| 417 | if isinstance(old, dict): |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 418 | __old = old.keys() |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 419 | else: |
| 420 | __old = old |
| 421 | old = {x: {} for x in old} |
| 422 | if isinstance(new, dict): |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 423 | __new = new.keys() |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 424 | else: |
| 425 | __new = new |
| 426 | new = {x: {} for x in new} |
| 427 | |
| 428 | cache_entry = self.cache.setdefault(path, {}) |
| 429 | created = [] if self.has_interfaces(cache_entry) else [path] |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 430 | added = set(__new).difference(__old) |
| 431 | removed = set(__old).difference(__new) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 432 | self.interfaces_append(cache_entry, owner, added) |
| 433 | self.interfaces_remove(cache_entry, owner, removed, path) |
| 434 | destroyed = [] if self.has_interfaces(cache_entry) else [path] |
| 435 | |
| 436 | # react to anything that requires association updates |
| 437 | new_assoc = [] |
| 438 | old_assoc = [] |
| 439 | if self.is_association(added): |
Brad Bishop | 926b35d | 2016-09-19 14:20:04 -0400 | [diff] [blame] | 440 | iface = obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE |
| 441 | new_assoc = new[iface]['associations'] |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 442 | if self.is_association(removed): |
| 443 | old_assoc = self.index_get_associations(path, [owner]) |
| 444 | self.update_associations( |
| 445 | path, owner, old_assoc, new_assoc, created, destroyed) |
| 446 | |
| 447 | def add_items(self, owner, bus_items): |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 448 | for path, items in bus_items.items(): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 449 | self.update_interfaces(path, str(owner), old=[], new=items) |
| 450 | |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 451 | def path_match(self, path): |
| 452 | match = False |
| 453 | |
| 454 | if not any([x for x in self.blacklist if x in path]): |
| 455 | # not blacklisted |
| 456 | |
| 457 | if any([x for x in self.namespaces if x in path]): |
| 458 | # a watched namespace contains the path |
| 459 | match = True |
| 460 | elif any([path for x in self.namespaces if path in x]): |
| 461 | # the path contains a watched namespace |
| 462 | match = True |
| 463 | |
| 464 | return match |
| 465 | |
| 466 | def interface_match(self, interface): |
| 467 | match = True |
| 468 | |
| 469 | if any([x for x in self.interface_blacklist if x in interface]): |
| 470 | # not blacklisted |
| 471 | match = False |
| 472 | elif not any([x for x in self.interface_namespaces if x in interface]): |
| 473 | # the interface contains a watched interface namespace |
| 474 | match = False |
| 475 | |
| 476 | return match |
| 477 | |
Andrew Geissler | 140f410 | 2018-02-19 08:31:05 -0800 | [diff] [blame] | 478 | def discovery_error_retry(self, owner, path, e): |
| 479 | sys.stderr.write( |
| 480 | '{} discovery failure on {} - retry\n'.format(owner, path)) |
| 481 | find_dbus_interfaces(self.bus, owner, '/', |
| 482 | self.discovery_callback, |
| 483 | self.discovery_error, |
| 484 | subtree_match=self.path_match, |
| 485 | iface_match=self.interface_match) |
| 486 | |
Andrew Jeffery | 60e0bb0 | 2018-05-15 09:21:10 +0930 | [diff] [blame] | 487 | def discover(self, owners=None): |
| 488 | if owners is None: |
| 489 | owners = [] |
| 490 | |
Brad Bishop | 062403d | 2017-07-29 22:43:40 -0400 | [diff] [blame] | 491 | def get_owner(name): |
| 492 | try: |
| 493 | return (name, self.bus.get_name_owner(name)) |
Adriana Kobylak | a1f2422 | 2018-01-10 16:09:07 -0600 | [diff] [blame] | 494 | except Exception: |
Brad Bishop | 062403d | 2017-07-29 22:43:40 -0400 | [diff] [blame] | 495 | traceback.print_exception(*sys.exc_info()) |
| 496 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 497 | if not owners: |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 498 | owned_names = [x for x in self.bus.list_names() |
| 499 | if not obmc.dbuslib.bindings.is_unique(x)] |
| 500 | owners = filter(bool, (get_owner(name) for name in owned_names)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 501 | for owned_name, o in owners: |
Brad Bishop | 5c5e13e | 2018-01-28 23:34:54 -0500 | [diff] [blame] | 502 | if not self.bus_normalize(owned_name): |
Brad Bishop | aeac98b | 2017-07-29 22:56:48 -0400 | [diff] [blame] | 503 | continue |
Andrew Geissler | 1246924 | 2018-01-02 09:41:37 -0600 | [diff] [blame] | 504 | self.bus_map[o] = owned_name |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 505 | self.defer_signals[owned_name] = [] |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 506 | find_dbus_interfaces( |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 507 | self.bus, owned_name, '/', |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 508 | self.discovery_callback, |
Andrew Geissler | 140f410 | 2018-02-19 08:31:05 -0800 | [diff] [blame] | 509 | self.discovery_error_retry, |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 510 | subtree_match=self.path_match, |
| 511 | iface_match=self.interface_match) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 512 | |
Brad Bishop | 5c5e13e | 2018-01-28 23:34:54 -0500 | [diff] [blame] | 513 | def bus_normalize(self, name): |
| 514 | ''' |
| 515 | Normalize on well-known names and filter signals |
| 516 | originating from the mapper. |
| 517 | ''' |
| 518 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 519 | if obmc.dbuslib.bindings.is_unique(name): |
| 520 | name = self.bus_map.get(name) |
| 521 | |
Brad Bishop | 5c5e13e | 2018-01-28 23:34:54 -0500 | [diff] [blame] | 522 | if name == obmc.mapper.MAPPER_NAME: |
| 523 | return None |
| 524 | |
| 525 | return name |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 526 | |
Brad Bishop | 787aa81 | 2018-01-28 23:42:03 -0500 | [diff] [blame] | 527 | def filter_signal_interfaces(self, interfaces): |
| 528 | return [str(x) for x in interfaces if self.interface_match(x)] |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 529 | |
| 530 | @staticmethod |
Andrew Jeffery | 60e0bb0 | 2018-05-15 09:21:10 +0930 | [diff] [blame] | 531 | def interfaces_get(item, owner, default=None): |
| 532 | if default is None: |
| 533 | default = [] |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 534 | return item.get(owner, default) |
| 535 | |
| 536 | @staticmethod |
| 537 | def interfaces_append(item, owner, append): |
| 538 | interfaces = item.setdefault(owner, []) |
| 539 | item[owner] = list(set(append).union(interfaces)) |
| 540 | |
| 541 | def interfaces_remove(self, item, owner, remove, path): |
| 542 | interfaces = item.get(owner, []) |
| 543 | item[owner] = list(set(interfaces).difference(remove)) |
| 544 | |
| 545 | if not item[owner]: |
| 546 | # remove the owner if there aren't any interfaces left |
| 547 | del item[owner] |
| 548 | |
| 549 | if item: |
| 550 | # other owners remain |
| 551 | return |
| 552 | |
| 553 | if self.cache.get_children(path): |
| 554 | # there are still references to this path |
| 555 | # from objects further down the tree. |
| 556 | # mark it for removal if that changes |
| 557 | self.cache.demote(path) |
| 558 | else: |
| 559 | # delete the entire path if everything is gone |
| 560 | del self.cache[path] |
| 561 | |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 562 | @staticmethod |
| 563 | def filter_interfaces(item, ifaces): |
Andrew Jeffery | 4d49f95 | 2018-05-09 17:29:53 +0930 | [diff] [blame] | 564 | return ObjectMapper._filter_interfaces(item, set(ifaces)) |
| 565 | |
| 566 | @staticmethod |
| 567 | def _filter_interfaces(item, ifaces): |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 568 | if isinstance(item, dict): |
| 569 | # Called with a single object. |
| 570 | if not ifaces: |
| 571 | return item |
| 572 | |
Andrew Jeffery | 4d49f95 | 2018-05-09 17:29:53 +0930 | [diff] [blame] | 573 | filtered = dict() |
| 574 | for k, v in item.items(): |
| 575 | isec = ifaces.intersection(v) |
| 576 | if isec: |
| 577 | filtered[k] = isec |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 578 | |
Andrew Jeffery | 4d49f95 | 2018-05-09 17:29:53 +0930 | [diff] [blame] | 579 | return filtered |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 580 | |
| 581 | # Called with a list of path/object tuples. |
| 582 | if not ifaces: |
| 583 | return dict(item) |
| 584 | |
Andrew Jeffery | 4d49f95 | 2018-05-09 17:29:53 +0930 | [diff] [blame] | 585 | if not item: |
| 586 | return dict() |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 587 | |
Andrew Jeffery | 4d49f95 | 2018-05-09 17:29:53 +0930 | [diff] [blame] | 588 | filtered = dict() |
| 589 | for i in item: |
| 590 | children = ObjectMapper._filter_interfaces(i[1], ifaces) |
| 591 | if children: |
| 592 | filtered[i[0]] = children |
| 593 | |
| 594 | return filtered |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 595 | |
| 596 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'sas', 'a{sas}') |
| 597 | def GetObject(self, path, interfaces): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 598 | o = self.cache_get(path) |
| 599 | if not o: |
| 600 | raise MapperNotFoundException(path) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 601 | |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 602 | return self.filter_interfaces(o, interfaces) |
| 603 | |
| 604 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'sias', 'as') |
| 605 | def GetSubTreePaths(self, path, depth, interfaces): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 606 | try: |
Brad Bishop | 2430197 | 2017-06-23 13:40:07 -0400 | [diff] [blame] | 607 | return self.filter_interfaces( |
| 608 | self.cache.iteritems(path, depth), |
| 609 | interfaces) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 610 | except KeyError: |
| 611 | raise MapperNotFoundException(path) |
| 612 | |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 613 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'sias', 'a{sa{sas}}') |
| 614 | def GetSubTree(self, path, depth, interfaces): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 615 | try: |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 616 | return self.filter_interfaces( |
| 617 | self.cache.dataitems(path, depth), |
| 618 | interfaces) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 619 | except KeyError: |
| 620 | raise MapperNotFoundException(path) |
| 621 | |
| 622 | @staticmethod |
| 623 | def has_interfaces(item): |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 624 | return any(item.values()) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 625 | |
| 626 | @staticmethod |
| 627 | def is_association(interfaces): |
| 628 | return obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE in interfaces |
| 629 | |
| 630 | def index_get(self, index, path, owners): |
| 631 | items = [] |
| 632 | item = self.index.get(index, {}) |
| 633 | item = item.get(path, {}) |
| 634 | for o in owners: |
| 635 | items.extend(item.get(o, [])) |
| 636 | return items |
| 637 | |
| 638 | def index_append(self, index, path, owner, assoc): |
| 639 | item = self.index.setdefault(index, {}) |
| 640 | item = item.setdefault(path, {}) |
| 641 | item = item.setdefault(owner, []) |
| 642 | item.append(assoc) |
| 643 | |
| 644 | def index_remove(self, index, path, owner, assoc): |
| 645 | index = self.index.get(index, {}) |
| 646 | owners = index.get(path, {}) |
| 647 | items = owners.get(owner, []) |
| 648 | if assoc in items: |
| 649 | items.remove(assoc) |
| 650 | if not items: |
| 651 | del owners[owner] |
| 652 | if not owners: |
| 653 | del index[path] |
| 654 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 655 | def index_get_associations(self, path, owners=[], direction='forward'): |
| 656 | forward = 'forward' if direction == 'forward' else 'reverse' |
| 657 | reverse = 'reverse' if direction == 'forward' else 'forward' |
| 658 | |
| 659 | associations = [] |
| 660 | if not owners: |
| 661 | index = self.index.get(forward, {}) |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 662 | owners = index.get(path, {}).keys() |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 663 | |
| 664 | # f: forward |
| 665 | # r: reverse |
| 666 | for rassoc in self.index_get(forward, path, owners): |
| 667 | elements = rassoc.split('/') |
| 668 | rtype = ''.join(elements[-1:]) |
| 669 | fendpoint = '/'.join(elements[:-1]) |
| 670 | for fassoc in self.index_get(reverse, fendpoint, owners): |
| 671 | elements = fassoc.split('/') |
| 672 | ftype = ''.join(elements[-1:]) |
| 673 | rendpoint = '/'.join(elements[:-1]) |
| 674 | if rendpoint != path: |
| 675 | continue |
| 676 | associations.append((ftype, rtype, fendpoint)) |
| 677 | |
| 678 | return associations |
| 679 | |
| 680 | def update_association(self, path, removed, added): |
| 681 | iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE |
Brad Bishop | 8e1f4ab | 2017-11-02 20:44:17 -0400 | [diff] [blame] | 682 | assoc = self.manager.get(path, None) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 683 | |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 684 | old_endpoints = set(assoc.Get(iface, 'endpoints') if assoc else []) |
| 685 | new_endpoints = old_endpoints.union(added).difference(removed) |
Brad Bishop | 84041e3 | 2017-11-02 21:48:57 -0400 | [diff] [blame] | 686 | |
| 687 | if old_endpoints == new_endpoints: |
| 688 | return |
| 689 | |
| 690 | create = [] if old_endpoints else [iface] |
| 691 | delete = [] if new_endpoints else [iface] |
| 692 | |
| 693 | if create: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 694 | self.manager.add( |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 695 | path, Association(self.bus, path, list(new_endpoints))) |
Brad Bishop | 84041e3 | 2017-11-02 21:48:57 -0400 | [diff] [blame] | 696 | elif delete: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 697 | self.manager.remove(path) |
Brad Bishop | 84041e3 | 2017-11-02 21:48:57 -0400 | [diff] [blame] | 698 | else: |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 699 | assoc.Set(iface, 'endpoints', list(new_endpoints)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 700 | |
| 701 | if create != delete: |
| 702 | self.update_interfaces( |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 703 | path, obmc.mapper.MAPPER_NAME, delete, create) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 704 | |
| 705 | def update_associations( |
| 706 | self, path, owner, old, new, created=[], destroyed=[]): |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 707 | added = set(new).difference(old) |
| 708 | removed = set(old).difference(new) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 709 | for forward, reverse, endpoint in added: |
Brad Bishop | b15b631 | 2017-11-01 16:34:13 -0400 | [diff] [blame] | 710 | if not endpoint: |
| 711 | # skip associations without an endpoint |
| 712 | continue |
| 713 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 714 | # update the index |
| 715 | forward_path = str(path + '/' + forward) |
| 716 | reverse_path = str(endpoint + '/' + reverse) |
| 717 | self.index_append( |
| 718 | 'forward', path, owner, reverse_path) |
| 719 | self.index_append( |
| 720 | 'reverse', endpoint, owner, forward_path) |
| 721 | |
| 722 | # create the association if the endpoint exists |
| 723 | if not self.cache_get(endpoint): |
| 724 | continue |
| 725 | |
| 726 | self.update_association(forward_path, [], [endpoint]) |
| 727 | self.update_association(reverse_path, [], [path]) |
| 728 | |
| 729 | for forward, reverse, endpoint in removed: |
| 730 | # update the index |
| 731 | forward_path = str(path + '/' + forward) |
| 732 | reverse_path = str(endpoint + '/' + reverse) |
| 733 | self.index_remove( |
| 734 | 'forward', path, owner, reverse_path) |
| 735 | self.index_remove( |
| 736 | 'reverse', endpoint, owner, forward_path) |
| 737 | |
| 738 | # destroy the association if it exists |
| 739 | self.update_association(forward_path, [endpoint], []) |
| 740 | self.update_association(reverse_path, [path], []) |
| 741 | |
| 742 | # If the associations interface endpoint comes |
| 743 | # or goes create or destroy the appropriate |
| 744 | # associations |
| 745 | for path in created: |
| 746 | for forward, reverse, endpoint in \ |
| 747 | self.index_get_associations(path, direction='reverse'): |
| 748 | forward_path = str(path + '/' + forward) |
| 749 | reverse_path = str(endpoint + '/' + reverse) |
| 750 | self.update_association(forward_path, [], [endpoint]) |
| 751 | self.update_association(reverse_path, [], [path]) |
| 752 | |
| 753 | for path in destroyed: |
| 754 | for forward, reverse, endpoint in \ |
| 755 | self.index_get_associations(path, direction='reverse'): |
| 756 | forward_path = str(path + '/' + forward) |
| 757 | reverse_path = str(endpoint + '/' + reverse) |
| 758 | self.update_association(forward_path, [endpoint], []) |
| 759 | self.update_association(reverse_path, [path], []) |
| 760 | |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 761 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'sas', 'a{sa{sas}}') |
| 762 | def GetAncestors(self, path, interfaces): |
Brad Bishop | 495ee09 | 2016-11-02 00:11:11 -0400 | [diff] [blame] | 763 | if not self.cache_get(path): |
| 764 | raise MapperNotFoundException(path) |
| 765 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 766 | objs = {} |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 767 | |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 768 | def parents(path): |
| 769 | yield "/" |
| 770 | parent = "" |
| 771 | for elem in (x for x in list(filter(bool, path.split('/')))[:-1]): |
| 772 | parent += "/" + elem |
| 773 | yield parent |
| 774 | |
| 775 | for parent in parents(path): |
| 776 | obj = self.cache_get(parent) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 777 | if not obj: |
| 778 | continue |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 779 | objs[parent] = obj |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 780 | |
Andrew Jeffery | b86b63a | 2018-05-09 16:10:46 +0930 | [diff] [blame] | 781 | return self.filter_interfaces(objs.items(), interfaces) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 782 | |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 783 | @dbus.service.signal(obmc.mapper.MAPPER_IFACE + '.Private', 's') |
| 784 | def IntrospectionComplete(self, name): |
| 785 | pass |
| 786 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 787 | |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 788 | def server_main( |
| 789 | path_namespaces, |
| 790 | interface_namespaces, |
| 791 | blacklists, |
| 792 | interface_blacklists): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 793 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 794 | bus = dbus.SystemBus() |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 795 | o = ObjectMapper( |
| 796 | bus, |
| 797 | obmc.mapper.MAPPER_PATH, |
| 798 | path_namespaces, |
| 799 | interface_namespaces, |
| 800 | blacklists, |
| 801 | interface_blacklists) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 802 | loop = gobject.MainLoop() |
| 803 | |
| 804 | loop.run() |