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