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', |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame^] | 119 | sender_keyword='sender', |
| 120 | path_keyword='sender_path') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 121 | self.bus.dbus.add_signal_receiver( |
| 122 | self.interfaces_removed_handler, |
| 123 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 124 | signal_name='InterfacesRemoved', |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame^] | 125 | sender_keyword='sender', |
| 126 | path_keyword='sender_path') |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 127 | self.bus.dbus.add_signal_receiver( |
| 128 | self.properties_changed_handler, |
| 129 | dbus_interface=dbus.PROPERTIES_IFACE, |
| 130 | signal_name='PropertiesChanged', |
| 131 | path_keyword='path', |
| 132 | sender_keyword='sender') |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 133 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 134 | def bus_match(self, owner): |
| 135 | # Ignore my own signals |
| 136 | return owner != obmc.mapper.MAPPER_NAME and \ |
| 137 | self.name_match(owner) |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 138 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 139 | def discovery_pending(self): |
| 140 | return not bool(self.service) |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 141 | |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame^] | 142 | def add_new_objmgr(self, path, owner): |
| 143 | # We don't get a signal for the ObjectManager |
| 144 | # interface itself, so if we see a signal from |
| 145 | # make sure its in our cache, and add it if not. |
| 146 | cache_entry = self.cache.get(path, {}) |
| 147 | cache_entry = cache_entry if cache_entry is not None else {} |
| 148 | old = self.interfaces_get(cache_entry, owner) |
| 149 | new = list(set(old).union([dbus.BUS_DAEMON_IFACE + '.ObjectManager'])) |
| 150 | self.update_interfaces(path, owner, old, new) |
| 151 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 152 | def interfaces_added_handler(self, path, iprops, **kw): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 153 | path = str(path) |
| 154 | owner = str(kw['sender']) |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame^] | 155 | self.add_new_objmgr(str(kw['sender_path']), owner) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 156 | interfaces = self.get_signal_interfaces(owner, iprops.iterkeys()) |
| 157 | cache_entry = self.cache.get(path, {}) |
| 158 | old = self.interfaces_get(cache_entry, owner) |
| 159 | new = list(set(interfaces).union(old)) |
| 160 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 161 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 162 | def interfaces_removed_handler(self, path, interfaces, **kw): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 163 | path = str(path) |
| 164 | owner = str(kw['sender']) |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame^] | 165 | self.add_new_objmgr(str(kw['sender_path']), owner) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 166 | interfaces = self.get_signal_interfaces(owner, interfaces) |
| 167 | cache_entry = self.cache.get(path, {}) |
| 168 | old = self.interfaces_get(cache_entry, owner) |
| 169 | new = list(set(old).difference(interfaces)) |
| 170 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 171 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 172 | def properties_changed_handler(self, interface, new, old, **kw): |
| 173 | owner = str(kw['sender']) |
| 174 | path = str(kw['path']) |
| 175 | interfaces = self.get_signal_interfaces(owner, [interface]) |
| 176 | if not self.is_association(interfaces): |
| 177 | return |
| 178 | associations = new.get('associations', None) |
| 179 | if associations is None: |
| 180 | return |
| 181 | |
| 182 | associations = [ |
| 183 | (str(x), str(y), str(z)) for x, y, z in associations] |
| 184 | self.update_associations( |
| 185 | path, owner, |
| 186 | self.index_get_associations(path, [owner]), |
| 187 | associations) |
| 188 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 189 | def process_new_owner(self, owner): |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 190 | # unique name |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 191 | return self.discover([IntrospectionParser(owner, |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 192 | self.bus.dbus, |
| 193 | self.tag_match, |
| 194 | self.intf_match)]) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 195 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 196 | def process_old_owner(self, owner): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 197 | for path, item in self.cache.dataitems(): |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 198 | old = self.interfaces_get(item, owner) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 199 | # remove all interfaces for this service |
| 200 | self.update_interfaces( |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 201 | path, owner, old=old, new=[]) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 202 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 203 | def bus_handler(self, owner, old, new): |
| 204 | valid = False |
| 205 | if not obmc.dbuslib.bindings.is_unique(owner): |
| 206 | valid = self.valid_signal(owner) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 207 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 208 | if valid and new: |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 209 | self.process_new_owner(new) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 210 | if valid and old: |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 211 | self.process_old_owner(old) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 212 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 213 | def update_interfaces(self, path, owner, old, new): |
| 214 | cache_entry = self.cache.setdefault(path, {}) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 215 | created = [] if self.has_interfaces(cache_entry) else [path] |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 216 | added = list(set(new).difference(old)) |
| 217 | removed = list(set(old).difference(new)) |
| 218 | self.interfaces_append(cache_entry, owner, added) |
| 219 | self.interfaces_remove(cache_entry, owner, removed, path) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 220 | destroyed = [] if self.has_interfaces(cache_entry) else [path] |
| 221 | |
| 222 | # react to anything that requires association updates |
| 223 | new_assoc = [] |
| 224 | old_assoc = [] |
| 225 | if self.is_association(added): |
| 226 | new_assoc = self.dbus_get_associations(path, owner) |
| 227 | if self.is_association(removed): |
| 228 | old_assoc = self.index_get_associations(path, [owner]) |
| 229 | self.update_associations( |
| 230 | path, owner, old_assoc, new_assoc, created, destroyed) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 231 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 232 | def add_items(self, owner, bus_items): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 233 | for path, items in bus_items.iteritems(): |
| 234 | # convert dbus types to native. |
| 235 | interfaces = [str(i) for i in items.get('interfaces', [])] |
| 236 | self.update_interfaces(path, str(owner), old=[], new=interfaces) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 237 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 238 | def discover(self, owners=[]): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 239 | def match(iface): |
| 240 | return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' or \ |
| 241 | self.intf_match(iface) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 242 | if not owners: |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 243 | owners = [ |
| 244 | IntrospectionParser( |
| 245 | x, self.bus.dbus, |
| 246 | self.tag_match, |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 247 | match) |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 248 | for x in self.bus.get_owner_names(self.bus_match)] |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 249 | for o in owners: |
| 250 | self.add_items(o.name, o.introspect()) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 251 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 252 | if self.discovery_pending(): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 253 | # add my object mananger instance |
| 254 | self.add_items( |
| 255 | self.unique, |
| 256 | {obmc.dbuslib.bindings.OBJ_PREFIX: |
| 257 | {'interfaces': |
| 258 | [dbus.BUS_DAEMON_IFACE + '.ObjectManager']}}) |
| 259 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 260 | print "ObjectMapper discovery complete..." |
| 261 | self.service = dbus.service.BusName( |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 262 | obmc.mapper.MAPPER_NAME, self.bus.dbus) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 263 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 264 | def valid_signal(self, owner): |
| 265 | if obmc.dbuslib.bindings.is_unique(owner): |
| 266 | owner = self.bus.get_owned_name(self.bus_match, owner) |
| 267 | |
| 268 | return owner is not None and not self.discovery_pending() and \ |
| 269 | self.bus_match(owner) |
| 270 | |
| 271 | def get_signal_interfaces(self, owner, interfaces): |
| 272 | filtered = [] |
| 273 | if self.valid_signal(owner): |
| 274 | filtered = [str(x) for x in interfaces if self.intf_match(x)] |
| 275 | |
| 276 | return filtered |
| 277 | |
| 278 | @staticmethod |
| 279 | def interfaces_get(item, owner, default=[]): |
| 280 | return item.get(owner, default) |
| 281 | |
| 282 | @staticmethod |
| 283 | def interfaces_append(item, owner, append): |
| 284 | interfaces = item.setdefault(owner, []) |
| 285 | item[owner] = list(set(append).union(interfaces)) |
| 286 | |
| 287 | def interfaces_remove(self, item, owner, remove, path): |
| 288 | interfaces = item.get(owner, []) |
| 289 | item[owner] = list(set(interfaces).difference(remove)) |
| 290 | |
| 291 | if not item[owner]: |
| 292 | # remove the owner if there aren't any interfaces left |
| 293 | del item[owner] |
| 294 | |
| 295 | if item: |
| 296 | # other owners remain |
| 297 | return |
| 298 | |
| 299 | if self.cache.get_children(path): |
| 300 | # there are still references to this path |
| 301 | # from objects further down the tree. |
| 302 | # mark it for removal if that changes |
| 303 | self.cache.demote(path) |
| 304 | else: |
| 305 | # delete the entire path if everything is gone |
| 306 | del self.cache[path] |
| 307 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 308 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sas}') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 309 | def GetObject(self, path): |
| 310 | o = self.cache.get(path) |
| 311 | if not o: |
| 312 | raise MapperNotFoundException(path) |
| 313 | return o |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 314 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 315 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'as') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 316 | def GetSubTreePaths(self, path, depth): |
| 317 | try: |
| 318 | return self.cache.iterkeys(path, depth) |
| 319 | except KeyError: |
| 320 | raise MapperNotFoundException(path) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 321 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 322 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'a{sa{sas}}') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 323 | def GetSubTree(self, path, depth): |
| 324 | try: |
| 325 | return {x: y for x, y in self.cache.dataitems(path, depth)} |
| 326 | except KeyError: |
| 327 | raise MapperNotFoundException(path) |
| 328 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 329 | @staticmethod |
| 330 | def has_interfaces(item): |
| 331 | for owner in item.iterkeys(): |
| 332 | if ObjectMapper.interfaces_get(item, owner): |
| 333 | return True |
| 334 | return False |
| 335 | |
| 336 | @staticmethod |
| 337 | def is_association(interfaces): |
| 338 | return obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE in interfaces |
| 339 | |
| 340 | def index_get(self, index, path, owners): |
| 341 | items = [] |
| 342 | item = self.index.get(index, {}) |
| 343 | item = item.get(path, {}) |
| 344 | for o in owners: |
| 345 | items.extend(item.get(o, [])) |
| 346 | return items |
| 347 | |
| 348 | def index_append(self, index, path, owner, assoc): |
| 349 | item = self.index.setdefault(index, {}) |
| 350 | item = item.setdefault(path, {}) |
| 351 | item = item.setdefault(owner, []) |
| 352 | item.append(assoc) |
| 353 | |
| 354 | def index_remove(self, index, path, owner, assoc): |
| 355 | index = self.index.get(index, {}) |
| 356 | owners = index.get(path, {}) |
| 357 | items = owners.get(owner, []) |
| 358 | if assoc in items: |
| 359 | items.remove(assoc) |
| 360 | if not items: |
| 361 | del owners[owner] |
| 362 | if not owners: |
| 363 | del index[path] |
| 364 | |
| 365 | def dbus_get_associations(self, path, owner): |
| 366 | obj = self.bus.dbus.get_object(owner, path, introspect=False) |
| 367 | iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE) |
| 368 | return [(str(f), str(r), str(e)) for f, r, e in iface.Get( |
| 369 | obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE, |
| 370 | 'associations')] |
| 371 | |
| 372 | def index_get_associations(self, path, owners=[], direction='forward'): |
| 373 | forward = 'forward' if direction == 'forward' else 'reverse' |
| 374 | reverse = 'reverse' if direction == 'forward' else 'forward' |
| 375 | |
| 376 | associations = [] |
| 377 | if not owners: |
| 378 | index = self.index.get(forward, {}) |
| 379 | owners = index.get(path, {}).keys() |
| 380 | |
| 381 | # f: forward |
| 382 | # r: reverse |
| 383 | for rassoc in self.index_get(forward, path, owners): |
| 384 | elements = rassoc.split('/') |
| 385 | rtype = ''.join(elements[-1:]) |
| 386 | fendpoint = '/'.join(elements[:-1]) |
| 387 | for fassoc in self.index_get(reverse, fendpoint, owners): |
| 388 | elements = fassoc.split('/') |
| 389 | ftype = ''.join(elements[-1:]) |
| 390 | rendpoint = '/'.join(elements[:-1]) |
| 391 | if rendpoint != path: |
| 392 | continue |
| 393 | associations.append((ftype, rtype, fendpoint)) |
| 394 | |
| 395 | return associations |
| 396 | |
| 397 | def update_association(self, path, removed, added): |
| 398 | iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE |
| 399 | create = [] if self.manager.get(path, False) else [iface] |
| 400 | |
| 401 | if added and create: |
| 402 | self.manager.add( |
| 403 | path, Association(self.bus.dbus, path, added)) |
| 404 | elif added: |
| 405 | self.manager.get(path).append(added) |
| 406 | |
| 407 | obj = self.manager.get(path, None) |
| 408 | if obj and removed: |
| 409 | obj.remove(removed) |
| 410 | |
| 411 | if obj and not obj.endpoints: |
| 412 | self.manager.remove(path) |
| 413 | |
| 414 | delete = [] if self.manager.get(path, False) else [iface] |
| 415 | |
| 416 | if create != delete: |
| 417 | self.update_interfaces( |
| 418 | path, self.unique, delete, create) |
| 419 | |
| 420 | def update_associations( |
| 421 | self, path, owner, old, new, created=[], destroyed=[]): |
| 422 | added = list(set(new).difference(old)) |
| 423 | removed = list(set(old).difference(new)) |
| 424 | for forward, reverse, endpoint in added: |
| 425 | # update the index |
| 426 | forward_path = str(path + '/' + forward) |
| 427 | reverse_path = str(endpoint + '/' + reverse) |
| 428 | self.index_append( |
| 429 | 'forward', path, owner, reverse_path) |
| 430 | self.index_append( |
| 431 | 'reverse', endpoint, owner, forward_path) |
| 432 | |
| 433 | # create the association if the endpoint exists |
| 434 | if self.cache.get(endpoint, None) is None: |
| 435 | continue |
| 436 | |
| 437 | self.update_association(forward_path, [], [endpoint]) |
| 438 | self.update_association(reverse_path, [], [path]) |
| 439 | |
| 440 | for forward, reverse, endpoint in removed: |
| 441 | # update the index |
| 442 | forward_path = str(path + '/' + forward) |
| 443 | reverse_path = str(endpoint + '/' + reverse) |
| 444 | self.index_remove( |
| 445 | 'forward', path, owner, reverse_path) |
| 446 | self.index_remove( |
| 447 | 'reverse', endpoint, owner, forward_path) |
| 448 | |
| 449 | # destroy the association if it exists |
| 450 | self.update_association(forward_path, [endpoint], []) |
| 451 | self.update_association(reverse_path, [path], []) |
| 452 | |
| 453 | # If the associations interface endpoint comes |
| 454 | # or goes create or destroy the appropriate |
| 455 | # associations |
| 456 | for path in created: |
| 457 | for forward, reverse, endpoint in \ |
| 458 | self.index_get_associations(path, direction='reverse'): |
| 459 | forward_path = str(path + '/' + forward) |
| 460 | reverse_path = str(endpoint + '/' + reverse) |
| 461 | self.update_association(forward_path, [], [endpoint]) |
| 462 | self.update_association(reverse_path, [], [path]) |
| 463 | |
| 464 | for path in destroyed: |
| 465 | for forward, reverse, endpoint in \ |
| 466 | self.index_get_associations(path, direction='reverse'): |
| 467 | forward_path = str(path + '/' + forward) |
| 468 | reverse_path = str(endpoint + '/' + reverse) |
| 469 | self.update_association(forward_path, [endpoint], []) |
| 470 | self.update_association(reverse_path, [path], []) |
| 471 | |
| 472 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sa{sas}}') |
| 473 | def GetAncestors(self, path): |
| 474 | elements = filter(bool, path.split('/')) |
| 475 | paths = [] |
| 476 | objs = {} |
| 477 | while elements: |
| 478 | elements.pop() |
| 479 | paths.append('/' + '/'.join(elements)) |
| 480 | if path != '/': |
| 481 | paths.append('/') |
| 482 | |
| 483 | for path in paths: |
| 484 | obj = self.cache.get(path, None) |
| 485 | if obj is None: |
| 486 | continue |
| 487 | objs[path] = obj |
| 488 | |
| 489 | return objs |
| 490 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 491 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 492 | class BusWrapper: |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 493 | def __init__(self, bus): |
| 494 | self.dbus = bus |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 495 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 496 | def get_owned_name(self, match, bus): |
| 497 | for x in self.get_service_names(match): |
| 498 | if self.dbus.get_name_owner(x) == bus: |
| 499 | return x |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame] | 500 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 501 | def get_service_names(self, match): |
| 502 | # these are well known names |
| 503 | return [x for x in self.dbus.list_names() |
| 504 | if match(x)] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 505 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 506 | def get_owner_names(self, match): |
| 507 | # these are unique connection names |
| 508 | return list(set( |
| 509 | [self.dbus.get_name_owner(x) |
| 510 | for x in self.get_service_names(match)])) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 511 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 512 | if __name__ == '__main__': |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 513 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 514 | bus = dbus.SystemBus() |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 515 | o = ObjectMapper(BusWrapper(bus), obmc.mapper.MAPPER_PATH) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 516 | loop = gobject.MainLoop() |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 517 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 518 | loop.run() |