Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Brad Bishop | b338560 | 2016-03-04 15:32:01 -0500 | [diff] [blame] | 3 | # Contributors Listed Below - COPYRIGHT 2016 |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 4 | # [+] International Business Machines Corp. |
| 5 | # |
| 6 | # |
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | # you may not use this file except in compliance with the License. |
| 9 | # You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, software |
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 16 | # implied. See the License for the specific language governing |
| 17 | # permissions and limitations under the License. |
| 18 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 19 | import dbus |
| 20 | import dbus.service |
Brad Bishop | ae0c0af | 2015-11-09 18:41:28 -0500 | [diff] [blame] | 21 | import dbus.exceptions |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 22 | import dbus.mainloop.glib |
| 23 | import gobject |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 24 | from obmc.dbuslib.introspection import IntrospectionParser |
| 25 | import obmc.utils.pathtree |
| 26 | import obmc.utils.misc |
| 27 | import obmc.mapper |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 28 | import obmc.dbuslib.bindings |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 29 | import obmc.dbuslib.enums |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 30 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 31 | |
Brad Bishop | ae0c0af | 2015-11-09 18:41:28 -0500 | [diff] [blame] | 32 | class MapperNotFoundException(dbus.exceptions.DBusException): |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 33 | _dbus_error_name = obmc.mapper.MAPPER_NOT_FOUND |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 34 | |
| 35 | def __init__(self, path): |
| 36 | super(MapperNotFoundException, self).__init__( |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 37 | "path or object not found: %s" % path) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 38 | |
Brad Bishop | ae0c0af | 2015-11-09 18:41:28 -0500 | [diff] [blame] | 39 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 40 | class Association(dbus.service.Object): |
| 41 | def __init__(self, bus, path, endpoints): |
| 42 | super(Association, self).__init__(bus, path) |
| 43 | self.endpoints = endpoints |
| 44 | |
| 45 | def __getattr__(self, name): |
| 46 | if name == 'properties': |
| 47 | return { |
| 48 | obmc.dbuslib.enums.OBMC_ASSOC_IFACE: { |
| 49 | 'endpoints': self.endpoints}} |
| 50 | return super(Association, self).__getattr__(name) |
| 51 | |
| 52 | def emit_signal(self, old): |
| 53 | if old != self.endpoints: |
| 54 | self.PropertiesChanged( |
| 55 | obmc.dbuslib.enums.OBMC_ASSOC_IFACE, |
| 56 | {'endpoints': self.endpoints}, ['endpoints']) |
| 57 | |
| 58 | def append(self, endpoints): |
| 59 | old = self.endpoints |
| 60 | self.endpoints = list(set(endpoints).union(self.endpoints)) |
| 61 | self.emit_signal(old) |
| 62 | |
| 63 | def remove(self, endpoints): |
| 64 | old = self.endpoints |
| 65 | self.endpoints = list(set(self.endpoints).difference(endpoints)) |
| 66 | self.emit_signal(old) |
| 67 | |
| 68 | @dbus.service.method(dbus.PROPERTIES_IFACE, 'ss', 'as') |
| 69 | def Get(self, interface_name, property_name): |
| 70 | if property_name != 'endpoints': |
| 71 | raise dbus.exceptions.DBusException(name=DBUS_UNKNOWN_PROPERTY) |
| 72 | return self.GetAll(interface_name)[property_name] |
| 73 | |
| 74 | @dbus.service.method(dbus.PROPERTIES_IFACE, 's', 'a{sas}') |
| 75 | def GetAll(self, interface_name): |
| 76 | if interface_name != obmc.dbuslib.enums.OBMC_ASSOC_IFACE: |
| 77 | raise dbus.exceptions.DBusException(DBUS_UNKNOWN_INTERFACE) |
| 78 | return {'endpoints': self.endpoints} |
| 79 | |
| 80 | @dbus.service.signal( |
| 81 | dbus.PROPERTIES_IFACE, signature='sa{sas}as') |
| 82 | def PropertiesChanged( |
| 83 | self, interface_name, changed_properties, invalidated_properties): |
| 84 | pass |
| 85 | |
| 86 | |
| 87 | class Manager(obmc.dbuslib.bindings.DbusObjectManager): |
| 88 | def __init__(self, bus, path): |
| 89 | obmc.dbuslib.bindings.DbusObjectManager.__init__(self) |
| 90 | dbus.service.Object.__init__(self, bus.dbus, path) |
| 91 | |
| 92 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 93 | class ObjectMapper(dbus.service.Object): |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 94 | def __init__(self, bus, path, |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 95 | name_match=obmc.utils.misc.org_dot_openbmc_match, |
| 96 | intf_match=obmc.utils.misc.org_dot_openbmc_match): |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 97 | super(ObjectMapper, self).__init__(bus.dbus, path) |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 98 | self.cache = obmc.utils.pathtree.PathTree() |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 99 | self.bus = bus |
| 100 | self.name_match = name_match |
| 101 | self.intf_match = intf_match |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 102 | self.tag_match = obmc.utils.misc.ListMatch(['children', 'interface']) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 103 | self.service = None |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 104 | self.index = {} |
| 105 | self.manager = Manager(bus, obmc.dbuslib.bindings.OBJ_PREFIX) |
| 106 | self.unique = bus.dbus.get_unique_name() |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 107 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 108 | gobject.idle_add(self.discover) |
| 109 | self.bus.dbus.add_signal_receiver( |
| 110 | self.bus_handler, |
| 111 | dbus_interface= |
| 112 | dbus.BUS_DAEMON_IFACE, |
| 113 | signal_name='NameOwnerChanged') |
| 114 | self.bus.dbus.add_signal_receiver( |
| 115 | self.interfaces_added_handler, |
| 116 | dbus_interface= |
| 117 | dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 118 | signal_name='InterfacesAdded', |
| 119 | sender_keyword='sender') |
| 120 | self.bus.dbus.add_signal_receiver( |
| 121 | self.interfaces_removed_handler, |
| 122 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 123 | signal_name='InterfacesRemoved', |
| 124 | sender_keyword='sender') |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 125 | self.bus.dbus.add_signal_receiver( |
| 126 | self.properties_changed_handler, |
| 127 | dbus_interface=dbus.PROPERTIES_IFACE, |
| 128 | signal_name='PropertiesChanged', |
| 129 | path_keyword='path', |
| 130 | sender_keyword='sender') |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 131 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 132 | def bus_match(self, owner): |
| 133 | # Ignore my own signals |
| 134 | return owner != obmc.mapper.MAPPER_NAME and \ |
| 135 | self.name_match(owner) |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 136 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 137 | def discovery_pending(self): |
| 138 | return not bool(self.service) |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 139 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 140 | def interfaces_added_handler(self, path, iprops, **kw): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 141 | path = str(path) |
| 142 | owner = str(kw['sender']) |
| 143 | interfaces = self.get_signal_interfaces(owner, iprops.iterkeys()) |
| 144 | cache_entry = self.cache.get(path, {}) |
| 145 | old = self.interfaces_get(cache_entry, owner) |
| 146 | new = list(set(interfaces).union(old)) |
| 147 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 148 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 149 | def interfaces_removed_handler(self, path, interfaces, **kw): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 150 | path = str(path) |
| 151 | owner = str(kw['sender']) |
| 152 | interfaces = self.get_signal_interfaces(owner, interfaces) |
| 153 | cache_entry = self.cache.get(path, {}) |
| 154 | old = self.interfaces_get(cache_entry, owner) |
| 155 | new = list(set(old).difference(interfaces)) |
| 156 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 157 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 158 | def properties_changed_handler(self, interface, new, old, **kw): |
| 159 | owner = str(kw['sender']) |
| 160 | path = str(kw['path']) |
| 161 | interfaces = self.get_signal_interfaces(owner, [interface]) |
| 162 | if not self.is_association(interfaces): |
| 163 | return |
| 164 | associations = new.get('associations', None) |
| 165 | if associations is None: |
| 166 | return |
| 167 | |
| 168 | associations = [ |
| 169 | (str(x), str(y), str(z)) for x, y, z in associations] |
| 170 | self.update_associations( |
| 171 | path, owner, |
| 172 | self.index_get_associations(path, [owner]), |
| 173 | associations) |
| 174 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 175 | def process_new_owner(self, owner): |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 176 | # unique name |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 177 | return self.discover([IntrospectionParser(owner, |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 178 | self.bus.dbus, |
| 179 | self.tag_match, |
| 180 | self.intf_match)]) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 181 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 182 | def process_old_owner(self, owner): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 183 | for path, item in self.cache.dataitems(): |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 184 | old = self.interfaces_get(item, owner) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 185 | # remove all interfaces for this service |
| 186 | self.update_interfaces( |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 187 | path, owner, old=old, new=[]) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 188 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 189 | def bus_handler(self, owner, old, new): |
| 190 | valid = False |
| 191 | if not obmc.dbuslib.bindings.is_unique(owner): |
| 192 | valid = self.valid_signal(owner) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 193 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 194 | if valid and new: |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 195 | self.process_new_owner(new) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 196 | if valid and old: |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 197 | self.process_old_owner(old) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 198 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 199 | def update_interfaces(self, path, owner, old, new): |
| 200 | cache_entry = self.cache.setdefault(path, {}) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 201 | created = [] if self.has_interfaces(cache_entry) else [path] |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 202 | added = list(set(new).difference(old)) |
| 203 | removed = list(set(old).difference(new)) |
| 204 | self.interfaces_append(cache_entry, owner, added) |
| 205 | self.interfaces_remove(cache_entry, owner, removed, path) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 206 | destroyed = [] if self.has_interfaces(cache_entry) else [path] |
| 207 | |
| 208 | # react to anything that requires association updates |
| 209 | new_assoc = [] |
| 210 | old_assoc = [] |
| 211 | if self.is_association(added): |
| 212 | new_assoc = self.dbus_get_associations(path, owner) |
| 213 | if self.is_association(removed): |
| 214 | old_assoc = self.index_get_associations(path, [owner]) |
| 215 | self.update_associations( |
| 216 | path, owner, old_assoc, new_assoc, created, destroyed) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 217 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 218 | def add_items(self, owner, bus_items): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 219 | for path, items in bus_items.iteritems(): |
| 220 | # convert dbus types to native. |
| 221 | interfaces = [str(i) for i in items.get('interfaces', [])] |
| 222 | self.update_interfaces(path, str(owner), old=[], new=interfaces) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 223 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 224 | def discover(self, owners=[]): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 225 | def match(iface): |
| 226 | return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' or \ |
| 227 | self.intf_match(iface) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 228 | if not owners: |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 229 | owners = [ |
| 230 | IntrospectionParser( |
| 231 | x, self.bus.dbus, |
| 232 | self.tag_match, |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 233 | match) |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 234 | for x in self.bus.get_owner_names(self.bus_match)] |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 235 | for o in owners: |
| 236 | self.add_items(o.name, o.introspect()) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 237 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 238 | if self.discovery_pending(): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 239 | # add my object mananger instance |
| 240 | self.add_items( |
| 241 | self.unique, |
| 242 | {obmc.dbuslib.bindings.OBJ_PREFIX: |
| 243 | {'interfaces': |
| 244 | [dbus.BUS_DAEMON_IFACE + '.ObjectManager']}}) |
| 245 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 246 | print "ObjectMapper discovery complete..." |
| 247 | self.service = dbus.service.BusName( |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 248 | obmc.mapper.MAPPER_NAME, self.bus.dbus) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 249 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 250 | def valid_signal(self, owner): |
| 251 | if obmc.dbuslib.bindings.is_unique(owner): |
| 252 | owner = self.bus.get_owned_name(self.bus_match, owner) |
| 253 | |
| 254 | return owner is not None and not self.discovery_pending() and \ |
| 255 | self.bus_match(owner) |
| 256 | |
| 257 | def get_signal_interfaces(self, owner, interfaces): |
| 258 | filtered = [] |
| 259 | if self.valid_signal(owner): |
| 260 | filtered = [str(x) for x in interfaces if self.intf_match(x)] |
| 261 | |
| 262 | return filtered |
| 263 | |
| 264 | @staticmethod |
| 265 | def interfaces_get(item, owner, default=[]): |
| 266 | return item.get(owner, default) |
| 267 | |
| 268 | @staticmethod |
| 269 | def interfaces_append(item, owner, append): |
| 270 | interfaces = item.setdefault(owner, []) |
| 271 | item[owner] = list(set(append).union(interfaces)) |
| 272 | |
| 273 | def interfaces_remove(self, item, owner, remove, path): |
| 274 | interfaces = item.get(owner, []) |
| 275 | item[owner] = list(set(interfaces).difference(remove)) |
| 276 | |
| 277 | if not item[owner]: |
| 278 | # remove the owner if there aren't any interfaces left |
| 279 | del item[owner] |
| 280 | |
| 281 | if item: |
| 282 | # other owners remain |
| 283 | return |
| 284 | |
| 285 | if self.cache.get_children(path): |
| 286 | # there are still references to this path |
| 287 | # from objects further down the tree. |
| 288 | # mark it for removal if that changes |
| 289 | self.cache.demote(path) |
| 290 | else: |
| 291 | # delete the entire path if everything is gone |
| 292 | del self.cache[path] |
| 293 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 294 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sas}') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 295 | def GetObject(self, path): |
| 296 | o = self.cache.get(path) |
| 297 | if not o: |
| 298 | raise MapperNotFoundException(path) |
| 299 | return o |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 300 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 301 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'as') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 302 | def GetSubTreePaths(self, path, depth): |
| 303 | try: |
| 304 | return self.cache.iterkeys(path, depth) |
| 305 | except KeyError: |
| 306 | raise MapperNotFoundException(path) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 307 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 308 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'a{sa{sas}}') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 309 | def GetSubTree(self, path, depth): |
| 310 | try: |
| 311 | return {x: y for x, y in self.cache.dataitems(path, depth)} |
| 312 | except KeyError: |
| 313 | raise MapperNotFoundException(path) |
| 314 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame^] | 315 | @staticmethod |
| 316 | def has_interfaces(item): |
| 317 | for owner in item.iterkeys(): |
| 318 | if ObjectMapper.interfaces_get(item, owner): |
| 319 | return True |
| 320 | return False |
| 321 | |
| 322 | @staticmethod |
| 323 | def is_association(interfaces): |
| 324 | return obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE in interfaces |
| 325 | |
| 326 | def index_get(self, index, path, owners): |
| 327 | items = [] |
| 328 | item = self.index.get(index, {}) |
| 329 | item = item.get(path, {}) |
| 330 | for o in owners: |
| 331 | items.extend(item.get(o, [])) |
| 332 | return items |
| 333 | |
| 334 | def index_append(self, index, path, owner, assoc): |
| 335 | item = self.index.setdefault(index, {}) |
| 336 | item = item.setdefault(path, {}) |
| 337 | item = item.setdefault(owner, []) |
| 338 | item.append(assoc) |
| 339 | |
| 340 | def index_remove(self, index, path, owner, assoc): |
| 341 | index = self.index.get(index, {}) |
| 342 | owners = index.get(path, {}) |
| 343 | items = owners.get(owner, []) |
| 344 | if assoc in items: |
| 345 | items.remove(assoc) |
| 346 | if not items: |
| 347 | del owners[owner] |
| 348 | if not owners: |
| 349 | del index[path] |
| 350 | |
| 351 | def dbus_get_associations(self, path, owner): |
| 352 | obj = self.bus.dbus.get_object(owner, path, introspect=False) |
| 353 | iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE) |
| 354 | return [(str(f), str(r), str(e)) for f, r, e in iface.Get( |
| 355 | obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE, |
| 356 | 'associations')] |
| 357 | |
| 358 | def index_get_associations(self, path, owners=[], direction='forward'): |
| 359 | forward = 'forward' if direction == 'forward' else 'reverse' |
| 360 | reverse = 'reverse' if direction == 'forward' else 'forward' |
| 361 | |
| 362 | associations = [] |
| 363 | if not owners: |
| 364 | index = self.index.get(forward, {}) |
| 365 | owners = index.get(path, {}).keys() |
| 366 | |
| 367 | # f: forward |
| 368 | # r: reverse |
| 369 | for rassoc in self.index_get(forward, path, owners): |
| 370 | elements = rassoc.split('/') |
| 371 | rtype = ''.join(elements[-1:]) |
| 372 | fendpoint = '/'.join(elements[:-1]) |
| 373 | for fassoc in self.index_get(reverse, fendpoint, owners): |
| 374 | elements = fassoc.split('/') |
| 375 | ftype = ''.join(elements[-1:]) |
| 376 | rendpoint = '/'.join(elements[:-1]) |
| 377 | if rendpoint != path: |
| 378 | continue |
| 379 | associations.append((ftype, rtype, fendpoint)) |
| 380 | |
| 381 | return associations |
| 382 | |
| 383 | def update_association(self, path, removed, added): |
| 384 | iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE |
| 385 | create = [] if self.manager.get(path, False) else [iface] |
| 386 | |
| 387 | if added and create: |
| 388 | self.manager.add( |
| 389 | path, Association(self.bus.dbus, path, added)) |
| 390 | elif added: |
| 391 | self.manager.get(path).append(added) |
| 392 | |
| 393 | obj = self.manager.get(path, None) |
| 394 | if obj and removed: |
| 395 | obj.remove(removed) |
| 396 | |
| 397 | if obj and not obj.endpoints: |
| 398 | self.manager.remove(path) |
| 399 | |
| 400 | delete = [] if self.manager.get(path, False) else [iface] |
| 401 | |
| 402 | if create != delete: |
| 403 | self.update_interfaces( |
| 404 | path, self.unique, delete, create) |
| 405 | |
| 406 | def update_associations( |
| 407 | self, path, owner, old, new, created=[], destroyed=[]): |
| 408 | added = list(set(new).difference(old)) |
| 409 | removed = list(set(old).difference(new)) |
| 410 | for forward, reverse, endpoint in added: |
| 411 | # update the index |
| 412 | forward_path = str(path + '/' + forward) |
| 413 | reverse_path = str(endpoint + '/' + reverse) |
| 414 | self.index_append( |
| 415 | 'forward', path, owner, reverse_path) |
| 416 | self.index_append( |
| 417 | 'reverse', endpoint, owner, forward_path) |
| 418 | |
| 419 | # create the association if the endpoint exists |
| 420 | if self.cache.get(endpoint, None) is None: |
| 421 | continue |
| 422 | |
| 423 | self.update_association(forward_path, [], [endpoint]) |
| 424 | self.update_association(reverse_path, [], [path]) |
| 425 | |
| 426 | for forward, reverse, endpoint in removed: |
| 427 | # update the index |
| 428 | forward_path = str(path + '/' + forward) |
| 429 | reverse_path = str(endpoint + '/' + reverse) |
| 430 | self.index_remove( |
| 431 | 'forward', path, owner, reverse_path) |
| 432 | self.index_remove( |
| 433 | 'reverse', endpoint, owner, forward_path) |
| 434 | |
| 435 | # destroy the association if it exists |
| 436 | self.update_association(forward_path, [endpoint], []) |
| 437 | self.update_association(reverse_path, [path], []) |
| 438 | |
| 439 | # If the associations interface endpoint comes |
| 440 | # or goes create or destroy the appropriate |
| 441 | # associations |
| 442 | for path in created: |
| 443 | for forward, reverse, endpoint in \ |
| 444 | self.index_get_associations(path, direction='reverse'): |
| 445 | forward_path = str(path + '/' + forward) |
| 446 | reverse_path = str(endpoint + '/' + reverse) |
| 447 | self.update_association(forward_path, [], [endpoint]) |
| 448 | self.update_association(reverse_path, [], [path]) |
| 449 | |
| 450 | for path in destroyed: |
| 451 | for forward, reverse, endpoint in \ |
| 452 | self.index_get_associations(path, direction='reverse'): |
| 453 | forward_path = str(path + '/' + forward) |
| 454 | reverse_path = str(endpoint + '/' + reverse) |
| 455 | self.update_association(forward_path, [endpoint], []) |
| 456 | self.update_association(reverse_path, [path], []) |
| 457 | |
| 458 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sa{sas}}') |
| 459 | def GetAncestors(self, path): |
| 460 | elements = filter(bool, path.split('/')) |
| 461 | paths = [] |
| 462 | objs = {} |
| 463 | while elements: |
| 464 | elements.pop() |
| 465 | paths.append('/' + '/'.join(elements)) |
| 466 | if path != '/': |
| 467 | paths.append('/') |
| 468 | |
| 469 | for path in paths: |
| 470 | obj = self.cache.get(path, None) |
| 471 | if obj is None: |
| 472 | continue |
| 473 | objs[path] = obj |
| 474 | |
| 475 | return objs |
| 476 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 477 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 478 | class BusWrapper: |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 479 | def __init__(self, bus): |
| 480 | self.dbus = bus |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 481 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 482 | def get_owned_name(self, match, bus): |
| 483 | for x in self.get_service_names(match): |
| 484 | if self.dbus.get_name_owner(x) == bus: |
| 485 | return x |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame] | 486 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 487 | def get_service_names(self, match): |
| 488 | # these are well known names |
| 489 | return [x for x in self.dbus.list_names() |
| 490 | if match(x)] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 491 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 492 | def get_owner_names(self, match): |
| 493 | # these are unique connection names |
| 494 | return list(set( |
| 495 | [self.dbus.get_name_owner(x) |
| 496 | for x in self.get_service_names(match)])) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 497 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 498 | if __name__ == '__main__': |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 499 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 500 | bus = dbus.SystemBus() |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 501 | o = ObjectMapper(BusWrapper(bus), obmc.mapper.MAPPER_PATH) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 502 | loop = gobject.MainLoop() |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 503 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 504 | loop.run() |