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 |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 33 | |
| 34 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 35 | class MapperBusyException(dbus.exceptions.DBusException): |
| 36 | _dbus_error_name = 'org.freedesktop.DBus.Error.ObjectPathInUse' |
| 37 | |
| 38 | def __init__(self): |
| 39 | super(MapperBusyException, self).__init__( |
| 40 | 'busy processing bus traffic') |
| 41 | |
| 42 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 43 | class MapperNotFoundException(dbus.exceptions.DBusException): |
| 44 | _dbus_error_name = obmc.mapper.MAPPER_NOT_FOUND |
| 45 | |
| 46 | def __init__(self, path): |
| 47 | super(MapperNotFoundException, self).__init__( |
| 48 | "path or object not found: %s" % path) |
| 49 | |
| 50 | |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 51 | def find_dbus_interfaces(conn, service, path, callback, error_callback, **kw): |
Brad Bishop | bd8aa05 | 2016-09-19 09:30:06 -0400 | [diff] [blame] | 52 | iface_match = kw.pop('iface_match', bool) |
Brad Bishop | 6a0320b | 2016-09-19 11:03:06 -0400 | [diff] [blame] | 53 | subtree_match = kw.pop('subtree_match', bool) |
Brad Bishop | bd8aa05 | 2016-09-19 09:30:06 -0400 | [diff] [blame] | 54 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 55 | class _FindInterfaces(object): |
| 56 | def __init__(self): |
| 57 | self.results = {} |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 58 | self.introspect_pending = [] |
| 59 | self.gmo_pending = [] |
| 60 | self.assoc_pending = [] |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 61 | |
| 62 | @staticmethod |
| 63 | def _to_path(elements): |
| 64 | return '/' + '/'.join(elements) |
| 65 | |
| 66 | @staticmethod |
| 67 | def _to_path_elements(path): |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 68 | return list(filter(bool, path.split('/'))) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 69 | |
| 70 | def __call__(self, path): |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 71 | try: |
| 72 | self._find_interfaces(path) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 73 | except Exception as e: |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 74 | error_callback(service, path, e) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 75 | |
| 76 | @staticmethod |
| 77 | def _match(iface): |
| 78 | return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' \ |
Brad Bishop | bd8aa05 | 2016-09-19 09:30:06 -0400 | [diff] [blame] | 79 | or iface_match(iface) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 80 | |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 81 | def check_done(self): |
| 82 | if any([ |
| 83 | self.introspect_pending, |
| 84 | self.gmo_pending, |
| 85 | self.assoc_pending]): |
| 86 | return |
| 87 | |
| 88 | callback(service, self.results) |
| 89 | |
| 90 | def _assoc_callback(self, path, associations): |
| 91 | try: |
| 92 | iface = obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE |
| 93 | self.assoc_pending.remove(path) |
Gunnar Mills | 296395c | 2017-09-06 13:56:43 -0500 | [diff] [blame] | 94 | self.results[path][iface]['associations'] = associations |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 95 | except Exception as e: |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 96 | error_callback(service, path, e) |
| 97 | return None |
| 98 | |
| 99 | self.check_done() |
| 100 | |
| 101 | def _gmo_callback(self, path, objs): |
| 102 | try: |
| 103 | self.gmo_pending.remove(path) |
CamVan Nguyen | 2fd4b1f | 2018-03-05 12:19:46 -0600 | [diff] [blame] | 104 | for k, v in list(objs.items()): |
Brad Bishop | f9b24bf | 2018-04-02 16:46:32 -0400 | [diff] [blame^] | 105 | ifaces = {iface: properties for iface, properties in list( |
| 106 | filter(lambda x: iface_match(x[0]), v.items()))} |
| 107 | self.results[k] = ifaces |
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) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 123 | ifaces = list(filter( |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 124 | self._match, |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 125 | [x.attrib.get('name') for x in root.findall('interface')])) |
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: |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 152 | children = list(filter( |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 153 | bool, |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [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 = [ |
| 156 | self._to_path( |
| 157 | path_elements + self._to_path_elements(x)) |
| 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): |
| 169 | path_elements = self._to_path_elements(path) |
| 170 | path = self._to_path(path_elements) |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 171 | obj = conn.get_object(service, path, introspect=False) |
| 172 | iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE) |
| 173 | self.introspect_pending.append(path) |
| 174 | iface.Introspect.call_async( |
| 175 | reply_handler=lambda x: self._introspect_callback(path, x), |
| 176 | error_handler=lambda x: error_callback(service, path, x)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 177 | |
| 178 | return _FindInterfaces()(path) |
| 179 | |
| 180 | |
Brad Bishop | c33ae65 | 2017-11-02 22:23:09 -0400 | [diff] [blame] | 181 | @obmc.dbuslib.bindings.add_interfaces([obmc.dbuslib.enums.OBMC_ASSOC_IFACE]) |
| 182 | class Association(obmc.dbuslib.bindings.DbusProperties): |
Brad Bishop | 734b2c3 | 2017-11-01 15:40:07 -0400 | [diff] [blame] | 183 | """Implementation of org.openbmc.Association.""" |
| 184 | |
Brad Bishop | b9b3ed5 | 2017-11-01 21:40:31 -0400 | [diff] [blame] | 185 | iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE |
| 186 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 187 | def __init__(self, bus, path, endpoints): |
Brad Bishop | 734b2c3 | 2017-11-01 15:40:07 -0400 | [diff] [blame] | 188 | """Construct an Association. |
| 189 | |
| 190 | Arguments: |
| 191 | bus -- The python-dbus connection to host the interface |
| 192 | path -- The D-Bus object path on which to implement the interface |
| 193 | endpoints -- A list of the initial association endpoints |
| 194 | """ |
Brad Bishop | 70dd595 | 2016-09-08 22:33:33 -0400 | [diff] [blame] | 195 | super(Association, self).__init__(conn=bus, object_path=path) |
Brad Bishop | b9b3ed5 | 2017-11-01 21:40:31 -0400 | [diff] [blame] | 196 | self.properties = {self.iface: {'endpoints': endpoints}} |
Brad Bishop | bcc0644 | 2018-01-29 14:54:51 -0500 | [diff] [blame] | 197 | self.unmask_signals() |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 198 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 199 | |
| 200 | class Manager(obmc.dbuslib.bindings.DbusObjectManager): |
| 201 | def __init__(self, bus, path): |
Brad Bishop | 70dd595 | 2016-09-08 22:33:33 -0400 | [diff] [blame] | 202 | super(Manager, self).__init__(conn=bus, object_path=path) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 203 | |
| 204 | |
| 205 | class ObjectMapper(dbus.service.Object): |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 206 | def __init__( |
| 207 | self, bus, path, namespaces, interface_namespaces, |
| 208 | blacklist, interface_blacklist): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 209 | super(ObjectMapper, self).__init__(bus, path) |
| 210 | self.cache = obmc.utils.pathtree.PathTree() |
| 211 | self.bus = bus |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 212 | self.service = None |
| 213 | self.index = {} |
| 214 | self.manager = Manager(bus, obmc.dbuslib.bindings.OBJ_PREFIX) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 215 | self.bus_map = {} |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 216 | self.defer_signals = {} |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 217 | self.namespaces = namespaces |
| 218 | self.interface_namespaces = interface_namespaces |
| 219 | self.blacklist = blacklist |
| 220 | self.blacklist.append(obmc.mapper.MAPPER_PATH) |
| 221 | self.interface_blacklist = interface_blacklist |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 222 | |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 223 | # add my object mananger instance |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 224 | self.add_new_objmgr( |
| 225 | obmc.dbuslib.bindings.OBJ_PREFIX, obmc.mapper.MAPPER_NAME) |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 226 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 227 | self.bus.add_signal_receiver( |
| 228 | self.bus_handler, |
| 229 | dbus_interface=dbus.BUS_DAEMON_IFACE, |
| 230 | signal_name='NameOwnerChanged') |
| 231 | self.bus.add_signal_receiver( |
| 232 | self.interfaces_added_handler, |
| 233 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 234 | signal_name='InterfacesAdded', |
| 235 | sender_keyword='sender', |
| 236 | path_keyword='sender_path') |
| 237 | self.bus.add_signal_receiver( |
| 238 | self.interfaces_removed_handler, |
| 239 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 240 | signal_name='InterfacesRemoved', |
| 241 | sender_keyword='sender', |
| 242 | path_keyword='sender_path') |
| 243 | self.bus.add_signal_receiver( |
| 244 | self.properties_changed_handler, |
| 245 | dbus_interface=dbus.PROPERTIES_IFACE, |
| 246 | signal_name='PropertiesChanged', |
Brad Bishop | b270adc | 2017-11-14 23:32:59 -0500 | [diff] [blame] | 247 | arg0=obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE, |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 248 | path_keyword='path', |
| 249 | sender_keyword='sender') |
| 250 | |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 251 | print("ObjectMapper startup complete. Discovery in progress...") |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 252 | self.discover() |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 253 | gobject.idle_add(self.claim_name) |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 254 | |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 255 | def claim_name(self): |
| 256 | if len(self.defer_signals): |
| 257 | return True |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 258 | print("ObjectMapper discovery complete") |
Brad Bishop | 5d4890c | 2016-09-19 11:28:47 -0400 | [diff] [blame] | 259 | self.service = dbus.service.BusName( |
| 260 | obmc.mapper.MAPPER_NAME, self.bus) |
Brad Bishop | 55b89cd | 2016-09-19 23:02:48 -0400 | [diff] [blame] | 261 | self.manager.unmask_signals() |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 262 | return False |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 263 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 264 | def discovery_callback(self, owner, items): |
| 265 | if owner in self.defer_signals: |
| 266 | self.add_items(owner, items) |
| 267 | pending = self.defer_signals[owner] |
| 268 | del self.defer_signals[owner] |
| 269 | |
| 270 | for x in pending: |
| 271 | x() |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 272 | self.IntrospectionComplete(owner) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 273 | |
| 274 | def discovery_error(self, owner, path, e): |
Brad Bishop | 99b8bc8 | 2017-07-29 21:39:52 -0400 | [diff] [blame] | 275 | '''Log a message and remove all traces of the service |
| 276 | we were attempting to introspect.''' |
| 277 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 278 | if owner in self.defer_signals: |
Brad Bishop | 7a79027 | 2017-12-14 21:25:24 -0500 | [diff] [blame] | 279 | |
| 280 | # Safe to add a reference to the traceback here, |
| 281 | # since it cannot contain the discovery_error frame. |
| 282 | exctype, value, tb = sys.exc_info() |
Brad Bishop | 99b8bc8 | 2017-07-29 21:39:52 -0400 | [diff] [blame] | 283 | sys.stderr.write( |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 284 | '{} discovery failure on {}\n'.format(owner, path)) |
Brad Bishop | 7a79027 | 2017-12-14 21:25:24 -0500 | [diff] [blame] | 285 | if tb: |
| 286 | traceback.print_exception(exctype, value, tb, file=sys.stderr) |
| 287 | else: |
| 288 | sys.stderr.write('{}: {}\n'.format(e.__class__.__name__, e)) |
| 289 | |
Brad Bishop | 99b8bc8 | 2017-07-29 21:39:52 -0400 | [diff] [blame] | 290 | del self.defer_signals[owner] |
| 291 | del self.bus_map[owner] |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 292 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 293 | def cache_get(self, path): |
| 294 | cache_entry = self.cache.get(path, {}) |
| 295 | if cache_entry is None: |
| 296 | # hide path elements without any interfaces |
| 297 | cache_entry = {} |
| 298 | return cache_entry |
| 299 | |
| 300 | def add_new_objmgr(self, path, owner): |
| 301 | # We don't get a signal for the ObjectManager |
| 302 | # interface itself, so if we see a signal from |
| 303 | # make sure its in our cache, and add it if not. |
| 304 | cache_entry = self.cache_get(path) |
| 305 | old = self.interfaces_get(cache_entry, owner) |
| 306 | new = list(set(old).union([dbus.BUS_DAEMON_IFACE + '.ObjectManager'])) |
| 307 | self.update_interfaces(path, owner, old, new) |
| 308 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 309 | def defer_signal(self, owner, callback): |
| 310 | self.defer_signals.setdefault(owner, []).append(callback) |
| 311 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 312 | def interfaces_added_handler(self, path, iprops, **kw): |
| 313 | path = str(path) |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 314 | owner = self.bus_normalize(str(kw['sender'])) |
| 315 | if not owner: |
Brad Bishop | 787aa81 | 2018-01-28 23:42:03 -0500 | [diff] [blame] | 316 | return |
CamVan Nguyen | 2fd4b1f | 2018-03-05 12:19:46 -0600 | [diff] [blame] | 317 | interfaces = self.filter_signal_interfaces(iter(list(iprops.keys()))) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 318 | if not interfaces: |
| 319 | return |
| 320 | |
| 321 | if owner not in self.defer_signals: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 322 | self.add_new_objmgr(str(kw['sender_path']), owner) |
| 323 | cache_entry = self.cache_get(path) |
| 324 | old = self.interfaces_get(cache_entry, owner) |
| 325 | new = list(set(interfaces).union(old)) |
Brad Bishop | a623596 | 2017-06-07 23:56:54 -0400 | [diff] [blame] | 326 | new = {x: iprops.get(x, {}) for x in new} |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 327 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 328 | else: |
| 329 | self.defer_signal( |
| 330 | owner, |
| 331 | lambda: self.interfaces_added_handler( |
| 332 | path, iprops, **kw)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 333 | |
| 334 | def interfaces_removed_handler(self, path, interfaces, **kw): |
| 335 | path = str(path) |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 336 | owner = self.bus_normalize(str(kw['sender'])) |
| 337 | if not owner: |
Brad Bishop | 787aa81 | 2018-01-28 23:42:03 -0500 | [diff] [blame] | 338 | return |
| 339 | interfaces = self.filter_signal_interfaces(interfaces) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 340 | if not interfaces: |
| 341 | return |
| 342 | |
| 343 | if owner not in self.defer_signals: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 344 | self.add_new_objmgr(str(kw['sender_path']), owner) |
| 345 | cache_entry = self.cache_get(path) |
| 346 | old = self.interfaces_get(cache_entry, owner) |
| 347 | new = list(set(old).difference(interfaces)) |
| 348 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 349 | else: |
| 350 | self.defer_signal( |
| 351 | owner, |
| 352 | lambda: self.interfaces_removed_handler( |
| 353 | path, interfaces, **kw)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 354 | |
| 355 | def properties_changed_handler(self, interface, new, old, **kw): |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 356 | owner = self.bus_normalize(str(kw['sender'])) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 357 | path = str(kw['path']) |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 358 | if not owner: |
Brad Bishop | 787aa81 | 2018-01-28 23:42:03 -0500 | [diff] [blame] | 359 | return |
| 360 | interfaces = self.filter_signal_interfaces([interface]) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 361 | if not self.is_association(interfaces): |
| 362 | return |
| 363 | associations = new.get('associations', None) |
| 364 | if associations is None: |
| 365 | return |
| 366 | |
Brad Bishop | 2e0436c | 2016-09-19 18:02:19 -0400 | [diff] [blame] | 367 | if owner not in self.defer_signals: |
| 368 | associations = [ |
| 369 | (str(x), str(y), str(z)) for x, y, z in associations] |
| 370 | self.update_associations( |
| 371 | path, owner, |
| 372 | self.index_get_associations(path, [owner]), |
| 373 | associations) |
| 374 | else: |
| 375 | self.defer_signal( |
| 376 | owner, |
| 377 | lambda: self.properties_changed_handler( |
| 378 | interface, new, old, **kw)) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 379 | |
| 380 | def process_new_owner(self, owned_name, owner): |
| 381 | # unique name |
| 382 | try: |
| 383 | return self.discover([(owned_name, owner)]) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 384 | except dbus.exceptions.DBusException as e: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 385 | if obmc.dbuslib.enums.DBUS_UNKNOWN_SERVICE \ |
| 386 | not in e.get_dbus_name(): |
| 387 | raise |
| 388 | |
| 389 | def process_old_owner(self, owned_name, owner): |
| 390 | if owner in self.bus_map: |
| 391 | del self.bus_map[owner] |
| 392 | |
| 393 | for path, item in self.cache.dataitems(): |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 394 | old = self.interfaces_get(item, owned_name) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 395 | # remove all interfaces for this service |
| 396 | self.update_interfaces( |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 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): |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 418 | __old = list(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): |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 423 | __new = list(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] |
| 430 | added = list(set(__new).difference(__old)) |
| 431 | removed = list(set(__old).difference(__new)) |
| 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): |
CamVan Nguyen | 2fd4b1f | 2018-03-05 12:19:46 -0600 | [diff] [blame] | 448 | for path, items in list(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 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 487 | def discover(self, owners=[]): |
Brad Bishop | 062403d | 2017-07-29 22:43:40 -0400 | [diff] [blame] | 488 | def get_owner(name): |
| 489 | try: |
| 490 | return (name, self.bus.get_name_owner(name)) |
Adriana Kobylak | a1f2422 | 2018-01-10 16:09:07 -0600 | [diff] [blame] | 491 | except Exception: |
Brad Bishop | 062403d | 2017-07-29 22:43:40 -0400 | [diff] [blame] | 492 | traceback.print_exception(*sys.exc_info()) |
| 493 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 494 | if not owners: |
Brad Bishop | cabc638 | 2018-01-29 15:39:07 -0500 | [diff] [blame] | 495 | owned_names = [ |
| 496 | x for x in self.bus.list_names() |
| 497 | if not obmc.dbuslib.bindings.is_unique(x)] |
| 498 | owners = list( |
| 499 | filter(bool, [get_owner(name) for name in owned_names])) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 500 | for owned_name, o in owners: |
Brad Bishop | 5c5e13e | 2018-01-28 23:34:54 -0500 | [diff] [blame] | 501 | if not self.bus_normalize(owned_name): |
Brad Bishop | aeac98b | 2017-07-29 22:56:48 -0400 | [diff] [blame] | 502 | continue |
Andrew Geissler | 1246924 | 2018-01-02 09:41:37 -0600 | [diff] [blame] | 503 | self.bus_map[o] = owned_name |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 504 | self.defer_signals[owned_name] = [] |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 505 | find_dbus_interfaces( |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 506 | self.bus, owned_name, '/', |
Brad Bishop | 520473f | 2016-09-19 21:46:36 -0400 | [diff] [blame] | 507 | self.discovery_callback, |
Andrew Geissler | 140f410 | 2018-02-19 08:31:05 -0800 | [diff] [blame] | 508 | self.discovery_error_retry, |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 509 | subtree_match=self.path_match, |
| 510 | iface_match=self.interface_match) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 511 | |
Brad Bishop | 5c5e13e | 2018-01-28 23:34:54 -0500 | [diff] [blame] | 512 | def bus_normalize(self, name): |
| 513 | ''' |
| 514 | Normalize on well-known names and filter signals |
| 515 | originating from the mapper. |
| 516 | ''' |
| 517 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 518 | if obmc.dbuslib.bindings.is_unique(name): |
| 519 | name = self.bus_map.get(name) |
| 520 | |
Brad Bishop | 5c5e13e | 2018-01-28 23:34:54 -0500 | [diff] [blame] | 521 | if name == obmc.mapper.MAPPER_NAME: |
| 522 | return None |
| 523 | |
| 524 | return name |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 525 | |
Brad Bishop | 787aa81 | 2018-01-28 23:42:03 -0500 | [diff] [blame] | 526 | def filter_signal_interfaces(self, interfaces): |
| 527 | return [str(x) for x in interfaces if self.interface_match(x)] |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 528 | |
| 529 | @staticmethod |
| 530 | def interfaces_get(item, owner, default=[]): |
| 531 | return item.get(owner, default) |
| 532 | |
| 533 | @staticmethod |
| 534 | def interfaces_append(item, owner, append): |
| 535 | interfaces = item.setdefault(owner, []) |
| 536 | item[owner] = list(set(append).union(interfaces)) |
| 537 | |
| 538 | def interfaces_remove(self, item, owner, remove, path): |
| 539 | interfaces = item.get(owner, []) |
| 540 | item[owner] = list(set(interfaces).difference(remove)) |
| 541 | |
| 542 | if not item[owner]: |
| 543 | # remove the owner if there aren't any interfaces left |
| 544 | del item[owner] |
| 545 | |
| 546 | if item: |
| 547 | # other owners remain |
| 548 | return |
| 549 | |
| 550 | if self.cache.get_children(path): |
| 551 | # there are still references to this path |
| 552 | # from objects further down the tree. |
| 553 | # mark it for removal if that changes |
| 554 | self.cache.demote(path) |
| 555 | else: |
| 556 | # delete the entire path if everything is gone |
| 557 | del self.cache[path] |
| 558 | |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 559 | @staticmethod |
| 560 | def filter_interfaces(item, ifaces): |
| 561 | if isinstance(item, dict): |
| 562 | # Called with a single object. |
| 563 | if not ifaces: |
| 564 | return item |
| 565 | |
| 566 | # Remove interfaces from a service that |
| 567 | # aren't in a filter. |
Adriana Kobylak | 7f42ad2 | 2018-01-16 12:15:23 -0600 | [diff] [blame] | 568 | svc_map = lambda svc: ( |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 569 | svc[0], |
| 570 | list(set(ifaces).intersection(svc[1]))) |
| 571 | |
| 572 | # Remove services where no interfaces remain after mapping. |
Adriana Kobylak | 7f42ad2 | 2018-01-16 12:15:23 -0600 | [diff] [blame] | 573 | svc_filter = lambda svc: svc[1] |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 574 | |
Adriana Kobylak | 7f42ad2 | 2018-01-16 12:15:23 -0600 | [diff] [blame] | 575 | obj_map = lambda o: ( |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 576 | tuple(*list(filter(svc_filter, list(map(svc_map, [o])))))) |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 577 | |
CamVan Nguyen | 2fd4b1f | 2018-03-05 12:19:46 -0600 | [diff] [blame] | 578 | return dict( |
| 579 | [x for x in map(obj_map, iter(list(item.items()))) if x]) |
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 | |
Adriana Kobylak | 7f42ad2 | 2018-01-16 12:15:23 -0600 | [diff] [blame] | 585 | obj_map = lambda x: ( |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 586 | x[0], |
| 587 | ObjectMapper.filter_interfaces( |
| 588 | x[1], |
| 589 | ifaces)) |
| 590 | |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 591 | return dict([x for x in map(obj_map, iter(item or [])) if x[1]]) |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 592 | |
| 593 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'sas', 'a{sas}') |
| 594 | def GetObject(self, path, interfaces): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 595 | o = self.cache_get(path) |
| 596 | if not o: |
| 597 | raise MapperNotFoundException(path) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 598 | |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 599 | return self.filter_interfaces(o, interfaces) |
| 600 | |
| 601 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'sias', 'as') |
| 602 | def GetSubTreePaths(self, path, depth, interfaces): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 603 | try: |
Brad Bishop | 2430197 | 2017-06-23 13:40:07 -0400 | [diff] [blame] | 604 | return self.filter_interfaces( |
| 605 | self.cache.iteritems(path, depth), |
| 606 | interfaces) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 607 | except KeyError: |
| 608 | raise MapperNotFoundException(path) |
| 609 | |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 610 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'sias', 'a{sa{sas}}') |
| 611 | def GetSubTree(self, path, depth, interfaces): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 612 | try: |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 613 | return self.filter_interfaces( |
| 614 | self.cache.dataitems(path, depth), |
| 615 | interfaces) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 616 | except KeyError: |
| 617 | raise MapperNotFoundException(path) |
| 618 | |
| 619 | @staticmethod |
| 620 | def has_interfaces(item): |
CamVan Nguyen | 2fd4b1f | 2018-03-05 12:19:46 -0600 | [diff] [blame] | 621 | for owner in list(item.keys()): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 622 | if ObjectMapper.interfaces_get(item, owner): |
| 623 | return True |
| 624 | return False |
| 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, {}) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 662 | owners = list(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 | |
Brad Bishop | c33ae65 | 2017-11-02 22:23:09 -0400 | [diff] [blame] | 684 | old_endpoints = assoc.Get(iface, 'endpoints') if assoc else [] |
Brad Bishop | 84041e3 | 2017-11-02 21:48:57 -0400 | [diff] [blame] | 685 | new_endpoints = list( |
| 686 | set(old_endpoints).union(added).difference(removed)) |
| 687 | |
| 688 | if old_endpoints == new_endpoints: |
| 689 | return |
| 690 | |
| 691 | create = [] if old_endpoints else [iface] |
| 692 | delete = [] if new_endpoints else [iface] |
| 693 | |
| 694 | if create: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 695 | self.manager.add( |
Brad Bishop | 84041e3 | 2017-11-02 21:48:57 -0400 | [diff] [blame] | 696 | path, Association(self.bus, path, new_endpoints)) |
| 697 | elif delete: |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 698 | self.manager.remove(path) |
Brad Bishop | 84041e3 | 2017-11-02 21:48:57 -0400 | [diff] [blame] | 699 | else: |
Brad Bishop | c33ae65 | 2017-11-02 22:23:09 -0400 | [diff] [blame] | 700 | assoc.Set(iface, 'endpoints', new_endpoints) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 701 | |
| 702 | if create != delete: |
| 703 | self.update_interfaces( |
Brad Bishop | 57255f6 | 2018-01-29 15:26:06 -0500 | [diff] [blame] | 704 | path, obmc.mapper.MAPPER_NAME, delete, create) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 705 | |
| 706 | def update_associations( |
| 707 | self, path, owner, old, new, created=[], destroyed=[]): |
| 708 | added = list(set(new).difference(old)) |
| 709 | removed = list(set(old).difference(new)) |
| 710 | for forward, reverse, endpoint in added: |
Brad Bishop | b15b631 | 2017-11-01 16:34:13 -0400 | [diff] [blame] | 711 | if not endpoint: |
| 712 | # skip associations without an endpoint |
| 713 | continue |
| 714 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 715 | # update the index |
| 716 | forward_path = str(path + '/' + forward) |
| 717 | reverse_path = str(endpoint + '/' + reverse) |
| 718 | self.index_append( |
| 719 | 'forward', path, owner, reverse_path) |
| 720 | self.index_append( |
| 721 | 'reverse', endpoint, owner, forward_path) |
| 722 | |
| 723 | # create the association if the endpoint exists |
| 724 | if not self.cache_get(endpoint): |
| 725 | continue |
| 726 | |
| 727 | self.update_association(forward_path, [], [endpoint]) |
| 728 | self.update_association(reverse_path, [], [path]) |
| 729 | |
| 730 | for forward, reverse, endpoint in removed: |
| 731 | # update the index |
| 732 | forward_path = str(path + '/' + forward) |
| 733 | reverse_path = str(endpoint + '/' + reverse) |
| 734 | self.index_remove( |
| 735 | 'forward', path, owner, reverse_path) |
| 736 | self.index_remove( |
| 737 | 'reverse', endpoint, owner, forward_path) |
| 738 | |
| 739 | # destroy the association if it exists |
| 740 | self.update_association(forward_path, [endpoint], []) |
| 741 | self.update_association(reverse_path, [path], []) |
| 742 | |
| 743 | # If the associations interface endpoint comes |
| 744 | # or goes create or destroy the appropriate |
| 745 | # associations |
| 746 | for path in created: |
| 747 | for forward, reverse, endpoint in \ |
| 748 | self.index_get_associations(path, direction='reverse'): |
| 749 | forward_path = str(path + '/' + forward) |
| 750 | reverse_path = str(endpoint + '/' + reverse) |
| 751 | self.update_association(forward_path, [], [endpoint]) |
| 752 | self.update_association(reverse_path, [], [path]) |
| 753 | |
| 754 | for path in destroyed: |
| 755 | for forward, reverse, endpoint in \ |
| 756 | self.index_get_associations(path, direction='reverse'): |
| 757 | forward_path = str(path + '/' + forward) |
| 758 | reverse_path = str(endpoint + '/' + reverse) |
| 759 | self.update_association(forward_path, [endpoint], []) |
| 760 | self.update_association(reverse_path, [path], []) |
| 761 | |
Brad Bishop | 1c33c22 | 2016-11-02 00:08:46 -0400 | [diff] [blame] | 762 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'sas', 'a{sa{sas}}') |
| 763 | def GetAncestors(self, path, interfaces): |
Brad Bishop | 495ee09 | 2016-11-02 00:11:11 -0400 | [diff] [blame] | 764 | if not self.cache_get(path): |
| 765 | raise MapperNotFoundException(path) |
| 766 | |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 767 | elements = list(filter(bool, path.split('/'))) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 768 | paths = [] |
| 769 | objs = {} |
| 770 | while elements: |
| 771 | elements.pop() |
| 772 | paths.append('/' + '/'.join(elements)) |
| 773 | if path != '/': |
| 774 | paths.append('/') |
| 775 | |
| 776 | for path in paths: |
| 777 | obj = self.cache_get(path) |
| 778 | if not obj: |
| 779 | continue |
| 780 | objs[path] = obj |
| 781 | |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 782 | return self.filter_interfaces(list(objs.items()), interfaces) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 783 | |
Brad Bishop | 829181d | 2017-02-24 09:49:14 -0500 | [diff] [blame] | 784 | @dbus.service.signal(obmc.mapper.MAPPER_IFACE + '.Private', 's') |
| 785 | def IntrospectionComplete(self, name): |
| 786 | pass |
| 787 | |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 788 | |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 789 | def server_main( |
| 790 | path_namespaces, |
| 791 | interface_namespaces, |
| 792 | blacklists, |
| 793 | interface_blacklists): |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 794 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 795 | bus = dbus.SystemBus() |
Brad Bishop | cb2e1b3 | 2017-07-09 20:11:35 -0400 | [diff] [blame] | 796 | o = ObjectMapper( |
| 797 | bus, |
| 798 | obmc.mapper.MAPPER_PATH, |
| 799 | path_namespaces, |
| 800 | interface_namespaces, |
| 801 | blacklists, |
| 802 | interface_blacklists) |
Brad Bishop | 63f59a7 | 2016-07-25 12:05:57 -0400 | [diff] [blame] | 803 | loop = gobject.MainLoop() |
| 804 | |
| 805 | loop.run() |