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