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 | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 24 | import xml.etree.ElementTree as ET |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 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 | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 40 | def find_dbus_interfaces(conn, service, path, match): |
| 41 | class __FindInterfaces(object): |
| 42 | def __init__(self): |
| 43 | self.results = {} |
| 44 | |
| 45 | @staticmethod |
| 46 | def __introspect(service, path): |
| 47 | obj = conn.get_object(service, path, introspect=False) |
| 48 | iface = dbus.Interface(obj, dbus.INTROSPECTABLE_IFACE) |
| 49 | return iface.Introspect() |
| 50 | |
| 51 | @staticmethod |
| 52 | def __get_managed_objects(service, om): |
| 53 | obj = conn.get_object(service, om, introspect=False) |
| 54 | iface = dbus.Interface( |
| 55 | obj, dbus.BUS_DAEMON_IFACE + '.ObjectManager') |
| 56 | return iface.GetManagedObjects() |
| 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, service, path): |
| 67 | self.results = {} |
| 68 | self.__find_interfaces(service, path) |
| 69 | return self.results |
| 70 | |
| 71 | @staticmethod |
| 72 | def __match(iface): |
| 73 | return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' \ |
| 74 | or match(iface) |
| 75 | |
| 76 | def __find_interfaces(self, service, path): |
| 77 | path_elements = self.__to_path_elements(path) |
| 78 | path = self.__to_path(path_elements) |
| 79 | root = ET.fromstring(self.__introspect(service, path)) |
| 80 | |
| 81 | ifaces = filter( |
| 82 | self.__match, |
| 83 | [x.attrib.get('name') for x in root.findall('interface')]) |
| 84 | self.results[path] = ifaces |
| 85 | |
| 86 | if dbus.BUS_DAEMON_IFACE + '.ObjectManager' in ifaces: |
| 87 | objs = self.__get_managed_objects(service, path) |
| 88 | for k, v in objs.iteritems(): |
Brad Bishop | 59800b8 | 2016-06-24 13:07:26 -0400 | [diff] [blame^] | 89 | self.results[k] = v |
Brad Bishop | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 90 | else: |
| 91 | children = filter( |
| 92 | bool, |
| 93 | [x.attrib.get('name') for x in root.findall('node')]) |
| 94 | children = [ |
| 95 | self.__to_path( |
| 96 | path_elements + self.__to_path_elements(x)) |
Brad Bishop | 59800b8 | 2016-06-24 13:07:26 -0400 | [diff] [blame^] | 97 | for x in sorted(children)] |
Brad Bishop | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 98 | for child in children: |
| 99 | if child not in self.results: |
| 100 | self.__find_interfaces(service, child) |
| 101 | |
| 102 | return __FindInterfaces()(service, path) |
| 103 | |
| 104 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 105 | class Association(dbus.service.Object): |
| 106 | def __init__(self, bus, path, endpoints): |
| 107 | super(Association, self).__init__(bus, path) |
| 108 | self.endpoints = endpoints |
| 109 | |
| 110 | def __getattr__(self, name): |
| 111 | if name == 'properties': |
| 112 | return { |
| 113 | obmc.dbuslib.enums.OBMC_ASSOC_IFACE: { |
| 114 | 'endpoints': self.endpoints}} |
| 115 | return super(Association, self).__getattr__(name) |
| 116 | |
| 117 | def emit_signal(self, old): |
| 118 | if old != self.endpoints: |
| 119 | self.PropertiesChanged( |
| 120 | obmc.dbuslib.enums.OBMC_ASSOC_IFACE, |
| 121 | {'endpoints': self.endpoints}, ['endpoints']) |
| 122 | |
| 123 | def append(self, endpoints): |
| 124 | old = self.endpoints |
| 125 | self.endpoints = list(set(endpoints).union(self.endpoints)) |
| 126 | self.emit_signal(old) |
| 127 | |
| 128 | def remove(self, endpoints): |
| 129 | old = self.endpoints |
| 130 | self.endpoints = list(set(self.endpoints).difference(endpoints)) |
| 131 | self.emit_signal(old) |
| 132 | |
| 133 | @dbus.service.method(dbus.PROPERTIES_IFACE, 'ss', 'as') |
| 134 | def Get(self, interface_name, property_name): |
| 135 | if property_name != 'endpoints': |
| 136 | raise dbus.exceptions.DBusException(name=DBUS_UNKNOWN_PROPERTY) |
| 137 | return self.GetAll(interface_name)[property_name] |
| 138 | |
| 139 | @dbus.service.method(dbus.PROPERTIES_IFACE, 's', 'a{sas}') |
| 140 | def GetAll(self, interface_name): |
| 141 | if interface_name != obmc.dbuslib.enums.OBMC_ASSOC_IFACE: |
| 142 | raise dbus.exceptions.DBusException(DBUS_UNKNOWN_INTERFACE) |
| 143 | return {'endpoints': self.endpoints} |
| 144 | |
| 145 | @dbus.service.signal( |
| 146 | dbus.PROPERTIES_IFACE, signature='sa{sas}as') |
| 147 | def PropertiesChanged( |
| 148 | self, interface_name, changed_properties, invalidated_properties): |
| 149 | pass |
| 150 | |
| 151 | |
| 152 | class Manager(obmc.dbuslib.bindings.DbusObjectManager): |
| 153 | def __init__(self, bus, path): |
| 154 | obmc.dbuslib.bindings.DbusObjectManager.__init__(self) |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 155 | dbus.service.Object.__init__(self, bus, path) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 156 | |
| 157 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 158 | class ObjectMapper(dbus.service.Object): |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 159 | def __init__(self, bus, path, |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 160 | name_match=obmc.utils.misc.org_dot_openbmc_match, |
| 161 | intf_match=obmc.utils.misc.org_dot_openbmc_match): |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 162 | super(ObjectMapper, self).__init__(bus, path) |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 163 | self.cache = obmc.utils.pathtree.PathTree() |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 164 | self.bus = bus |
| 165 | self.name_match = name_match |
| 166 | self.intf_match = intf_match |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 167 | self.tag_match = obmc.utils.misc.ListMatch(['children', 'interface']) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 168 | self.service = None |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 169 | self.index = {} |
| 170 | self.manager = Manager(bus, obmc.dbuslib.bindings.OBJ_PREFIX) |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 171 | self.unique = bus.get_unique_name() |
| 172 | self.bus_map = {} |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 173 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 174 | gobject.idle_add(self.discover) |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 175 | self.bus.add_signal_receiver( |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 176 | self.bus_handler, |
Brad Bishop | 62a50da | 2016-06-24 13:06:01 -0400 | [diff] [blame] | 177 | dbus_interface=dbus.BUS_DAEMON_IFACE, |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 178 | signal_name='NameOwnerChanged') |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 179 | self.bus.add_signal_receiver( |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 180 | self.interfaces_added_handler, |
Brad Bishop | 62a50da | 2016-06-24 13:06:01 -0400 | [diff] [blame] | 181 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 182 | signal_name='InterfacesAdded', |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame] | 183 | sender_keyword='sender', |
| 184 | path_keyword='sender_path') |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 185 | self.bus.add_signal_receiver( |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 186 | self.interfaces_removed_handler, |
| 187 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 188 | signal_name='InterfacesRemoved', |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame] | 189 | sender_keyword='sender', |
| 190 | path_keyword='sender_path') |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 191 | self.bus.add_signal_receiver( |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 192 | self.properties_changed_handler, |
| 193 | dbus_interface=dbus.PROPERTIES_IFACE, |
| 194 | signal_name='PropertiesChanged', |
| 195 | path_keyword='path', |
| 196 | sender_keyword='sender') |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 197 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 198 | def bus_match(self, owner): |
| 199 | # Ignore my own signals |
| 200 | return owner != obmc.mapper.MAPPER_NAME and \ |
| 201 | self.name_match(owner) |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 202 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 203 | def discovery_pending(self): |
| 204 | return not bool(self.service) |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 205 | |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 206 | def cache_get(self, path): |
| 207 | cache_entry = self.cache.get(path, {}) |
| 208 | if cache_entry is None: |
| 209 | # hide path elements without any interfaces |
| 210 | cache_entry = {} |
| 211 | return cache_entry |
| 212 | |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame] | 213 | def add_new_objmgr(self, path, owner): |
| 214 | # We don't get a signal for the ObjectManager |
| 215 | # interface itself, so if we see a signal from |
| 216 | # make sure its in our cache, and add it if not. |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 217 | cache_entry = self.cache_get(path) |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame] | 218 | old = self.interfaces_get(cache_entry, owner) |
| 219 | new = list(set(old).union([dbus.BUS_DAEMON_IFACE + '.ObjectManager'])) |
| 220 | self.update_interfaces(path, owner, old, new) |
| 221 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 222 | def interfaces_added_handler(self, path, iprops, **kw): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 223 | path = str(path) |
| 224 | owner = str(kw['sender']) |
| 225 | interfaces = self.get_signal_interfaces(owner, iprops.iterkeys()) |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 226 | if interfaces: |
| 227 | self.add_new_objmgr(str(kw['sender_path']), owner) |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 228 | cache_entry = self.cache_get(path) |
| 229 | old = self.interfaces_get(cache_entry, owner) |
| 230 | new = list(set(interfaces).union(old)) |
| 231 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 232 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 233 | def interfaces_removed_handler(self, path, interfaces, **kw): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 234 | path = str(path) |
| 235 | owner = str(kw['sender']) |
| 236 | interfaces = self.get_signal_interfaces(owner, interfaces) |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 237 | if interfaces: |
| 238 | self.add_new_objmgr(str(kw['sender_path']), owner) |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 239 | cache_entry = self.cache_get(path) |
| 240 | old = self.interfaces_get(cache_entry, owner) |
| 241 | new = list(set(old).difference(interfaces)) |
| 242 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 243 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 244 | def properties_changed_handler(self, interface, new, old, **kw): |
| 245 | owner = str(kw['sender']) |
| 246 | path = str(kw['path']) |
| 247 | interfaces = self.get_signal_interfaces(owner, [interface]) |
| 248 | if not self.is_association(interfaces): |
| 249 | return |
| 250 | associations = new.get('associations', None) |
| 251 | if associations is None: |
| 252 | return |
| 253 | |
| 254 | associations = [ |
| 255 | (str(x), str(y), str(z)) for x, y, z in associations] |
| 256 | self.update_associations( |
| 257 | path, owner, |
| 258 | self.index_get_associations(path, [owner]), |
| 259 | associations) |
| 260 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 261 | def process_new_owner(self, owned_name, owner): |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 262 | # unique name |
Brad Bishop | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 263 | try: |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 264 | return self.discover([(owned_name, owner)]) |
Brad Bishop | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 265 | except dbus.exceptions.DBusException, e: |
| 266 | if obmc.dbuslib.enums.DBUS_UNKNOWN_SERVICE \ |
| 267 | not in e.get_dbus_name(): |
| 268 | raise |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 269 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 270 | def process_old_owner(self, owned_name, owner): |
| 271 | if owner in self.bus_map: |
| 272 | del self.bus_map[owner] |
| 273 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 274 | for path, item in self.cache.dataitems(): |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 275 | old = self.interfaces_get(item, owner) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 276 | # remove all interfaces for this service |
| 277 | self.update_interfaces( |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 278 | path, owner, old=old, new=[]) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 279 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 280 | def bus_handler(self, owned_name, old, new): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 281 | valid = False |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 282 | if not obmc.dbuslib.bindings.is_unique(owned_name): |
| 283 | valid = self.valid_signal(owned_name) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 284 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 285 | if valid and new: |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 286 | self.process_new_owner(owned_name, new) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 287 | if valid and old: |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 288 | self.process_old_owner(owned_name, old) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 289 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 290 | def update_interfaces(self, path, owner, old, new): |
Brad Bishop | 59800b8 | 2016-06-24 13:07:26 -0400 | [diff] [blame^] | 291 | # __xx -> intf list |
| 292 | # xx -> intf dict |
| 293 | if isinstance(old, dict): |
| 294 | __old = old.keys() |
| 295 | else: |
| 296 | __old = old |
| 297 | old = {x: {} for x in old} |
| 298 | if isinstance(new, dict): |
| 299 | __new = new.keys() |
| 300 | else: |
| 301 | __new = new |
| 302 | new = {x: {} for x in new} |
| 303 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 304 | cache_entry = self.cache.setdefault(path, {}) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 305 | created = [] if self.has_interfaces(cache_entry) else [path] |
Brad Bishop | 59800b8 | 2016-06-24 13:07:26 -0400 | [diff] [blame^] | 306 | added = list(set(__new).difference(__old)) |
| 307 | removed = list(set(__old).difference(__new)) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 308 | self.interfaces_append(cache_entry, owner, added) |
| 309 | self.interfaces_remove(cache_entry, owner, removed, path) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 310 | destroyed = [] if self.has_interfaces(cache_entry) else [path] |
| 311 | |
| 312 | # react to anything that requires association updates |
| 313 | new_assoc = [] |
| 314 | old_assoc = [] |
| 315 | if self.is_association(added): |
Brad Bishop | 59800b8 | 2016-06-24 13:07:26 -0400 | [diff] [blame^] | 316 | new_assoc = self.dbus_get_associations(path, owner, new) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 317 | if self.is_association(removed): |
| 318 | old_assoc = self.index_get_associations(path, [owner]) |
| 319 | self.update_associations( |
| 320 | path, owner, old_assoc, new_assoc, created, destroyed) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 321 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 322 | def add_items(self, owner, bus_items): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 323 | for path, items in bus_items.iteritems(): |
Brad Bishop | 59800b8 | 2016-06-24 13:07:26 -0400 | [diff] [blame^] | 324 | self.update_interfaces(path, str(owner), old=[], new=items) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 325 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 326 | def discover(self, owners=[]): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 327 | def match(iface): |
| 328 | return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' or \ |
| 329 | self.intf_match(iface) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 330 | if not owners: |
Brad Bishop | 62a50da | 2016-06-24 13:06:01 -0400 | [diff] [blame] | 331 | owned_names = [ |
| 332 | x for x in self.bus.list_names() if self.bus_match(x)] |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 333 | owners = [self.bus.get_name_owner(x) for x in owned_names] |
| 334 | owners = zip(owned_names, owners) |
| 335 | for owned_name, o in owners: |
Brad Bishop | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 336 | self.add_items( |
| 337 | o, |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 338 | find_dbus_interfaces(self.bus, o, '/', self.intf_match)) |
| 339 | self.bus_map[o] = owned_name |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 340 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 341 | if self.discovery_pending(): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 342 | # add my object mananger instance |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 343 | self.bus_map[self.unique] = obmc.mapper.MAPPER_NAME |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 344 | self.add_items( |
| 345 | self.unique, |
| 346 | {obmc.dbuslib.bindings.OBJ_PREFIX: |
| 347 | {'interfaces': |
| 348 | [dbus.BUS_DAEMON_IFACE + '.ObjectManager']}}) |
| 349 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 350 | print "ObjectMapper discovery complete..." |
| 351 | self.service = dbus.service.BusName( |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 352 | obmc.mapper.MAPPER_NAME, self.bus) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 353 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 354 | def valid_signal(self, name): |
| 355 | if self.discovery_pending(): |
| 356 | return False |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 357 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 358 | if obmc.dbuslib.bindings.is_unique(name): |
| 359 | name = self.bus_map.get(name) |
| 360 | |
| 361 | return name is not None and \ |
| 362 | self.bus_match(name) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 363 | |
| 364 | def get_signal_interfaces(self, owner, interfaces): |
| 365 | filtered = [] |
| 366 | if self.valid_signal(owner): |
| 367 | filtered = [str(x) for x in interfaces if self.intf_match(x)] |
| 368 | |
| 369 | return filtered |
| 370 | |
| 371 | @staticmethod |
| 372 | def interfaces_get(item, owner, default=[]): |
| 373 | return item.get(owner, default) |
| 374 | |
| 375 | @staticmethod |
| 376 | def interfaces_append(item, owner, append): |
| 377 | interfaces = item.setdefault(owner, []) |
| 378 | item[owner] = list(set(append).union(interfaces)) |
| 379 | |
| 380 | def interfaces_remove(self, item, owner, remove, path): |
| 381 | interfaces = item.get(owner, []) |
| 382 | item[owner] = list(set(interfaces).difference(remove)) |
| 383 | |
| 384 | if not item[owner]: |
| 385 | # remove the owner if there aren't any interfaces left |
| 386 | del item[owner] |
| 387 | |
| 388 | if item: |
| 389 | # other owners remain |
| 390 | return |
| 391 | |
| 392 | if self.cache.get_children(path): |
| 393 | # there are still references to this path |
| 394 | # from objects further down the tree. |
| 395 | # mark it for removal if that changes |
| 396 | self.cache.demote(path) |
| 397 | else: |
| 398 | # delete the entire path if everything is gone |
| 399 | del self.cache[path] |
| 400 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 401 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sas}') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 402 | def GetObject(self, path): |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 403 | o = self.cache_get(path) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 404 | if not o: |
| 405 | raise MapperNotFoundException(path) |
| 406 | return o |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 407 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 408 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'as') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 409 | def GetSubTreePaths(self, path, depth): |
| 410 | try: |
| 411 | return self.cache.iterkeys(path, depth) |
| 412 | except KeyError: |
| 413 | raise MapperNotFoundException(path) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 414 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 415 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'a{sa{sas}}') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 416 | def GetSubTree(self, path, depth): |
| 417 | try: |
| 418 | return {x: y for x, y in self.cache.dataitems(path, depth)} |
| 419 | except KeyError: |
| 420 | raise MapperNotFoundException(path) |
| 421 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 422 | @staticmethod |
| 423 | def has_interfaces(item): |
| 424 | for owner in item.iterkeys(): |
| 425 | if ObjectMapper.interfaces_get(item, owner): |
| 426 | return True |
| 427 | return False |
| 428 | |
| 429 | @staticmethod |
| 430 | def is_association(interfaces): |
| 431 | return obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE in interfaces |
| 432 | |
| 433 | def index_get(self, index, path, owners): |
| 434 | items = [] |
| 435 | item = self.index.get(index, {}) |
| 436 | item = item.get(path, {}) |
| 437 | for o in owners: |
| 438 | items.extend(item.get(o, [])) |
| 439 | return items |
| 440 | |
| 441 | def index_append(self, index, path, owner, assoc): |
| 442 | item = self.index.setdefault(index, {}) |
| 443 | item = item.setdefault(path, {}) |
| 444 | item = item.setdefault(owner, []) |
| 445 | item.append(assoc) |
| 446 | |
| 447 | def index_remove(self, index, path, owner, assoc): |
| 448 | index = self.index.get(index, {}) |
| 449 | owners = index.get(path, {}) |
| 450 | items = owners.get(owner, []) |
| 451 | if assoc in items: |
| 452 | items.remove(assoc) |
| 453 | if not items: |
| 454 | del owners[owner] |
| 455 | if not owners: |
| 456 | del index[path] |
| 457 | |
Brad Bishop | 59800b8 | 2016-06-24 13:07:26 -0400 | [diff] [blame^] | 458 | def dbus_get_associations(self, path, owner, obj): |
| 459 | iface = obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE |
| 460 | if 'associations' in obj[iface]: |
| 461 | return obj[iface]['associations'] |
| 462 | |
| 463 | # fallback to dbus |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 464 | obj = self.bus.get_object(owner, path, introspect=False) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 465 | iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE) |
| 466 | return [(str(f), str(r), str(e)) for f, r, e in iface.Get( |
| 467 | obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE, |
| 468 | 'associations')] |
| 469 | |
| 470 | def index_get_associations(self, path, owners=[], direction='forward'): |
| 471 | forward = 'forward' if direction == 'forward' else 'reverse' |
| 472 | reverse = 'reverse' if direction == 'forward' else 'forward' |
| 473 | |
| 474 | associations = [] |
| 475 | if not owners: |
| 476 | index = self.index.get(forward, {}) |
| 477 | owners = index.get(path, {}).keys() |
| 478 | |
| 479 | # f: forward |
| 480 | # r: reverse |
| 481 | for rassoc in self.index_get(forward, path, owners): |
| 482 | elements = rassoc.split('/') |
| 483 | rtype = ''.join(elements[-1:]) |
| 484 | fendpoint = '/'.join(elements[:-1]) |
| 485 | for fassoc in self.index_get(reverse, fendpoint, owners): |
| 486 | elements = fassoc.split('/') |
| 487 | ftype = ''.join(elements[-1:]) |
| 488 | rendpoint = '/'.join(elements[:-1]) |
| 489 | if rendpoint != path: |
| 490 | continue |
| 491 | associations.append((ftype, rtype, fendpoint)) |
| 492 | |
| 493 | return associations |
| 494 | |
| 495 | def update_association(self, path, removed, added): |
| 496 | iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE |
| 497 | create = [] if self.manager.get(path, False) else [iface] |
| 498 | |
| 499 | if added and create: |
| 500 | self.manager.add( |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 501 | path, Association(self.bus, path, added)) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 502 | elif added: |
| 503 | self.manager.get(path).append(added) |
| 504 | |
| 505 | obj = self.manager.get(path, None) |
| 506 | if obj and removed: |
| 507 | obj.remove(removed) |
| 508 | |
| 509 | if obj and not obj.endpoints: |
| 510 | self.manager.remove(path) |
| 511 | |
| 512 | delete = [] if self.manager.get(path, False) else [iface] |
| 513 | |
| 514 | if create != delete: |
| 515 | self.update_interfaces( |
| 516 | path, self.unique, delete, create) |
| 517 | |
| 518 | def update_associations( |
| 519 | self, path, owner, old, new, created=[], destroyed=[]): |
| 520 | added = list(set(new).difference(old)) |
| 521 | removed = list(set(old).difference(new)) |
| 522 | for forward, reverse, endpoint in added: |
| 523 | # update the index |
| 524 | forward_path = str(path + '/' + forward) |
| 525 | reverse_path = str(endpoint + '/' + reverse) |
| 526 | self.index_append( |
| 527 | 'forward', path, owner, reverse_path) |
| 528 | self.index_append( |
| 529 | 'reverse', endpoint, owner, forward_path) |
| 530 | |
| 531 | # create the association if the endpoint exists |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 532 | if not self.cache_get(endpoint): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 533 | continue |
| 534 | |
| 535 | self.update_association(forward_path, [], [endpoint]) |
| 536 | self.update_association(reverse_path, [], [path]) |
| 537 | |
| 538 | for forward, reverse, endpoint in removed: |
| 539 | # update the index |
| 540 | forward_path = str(path + '/' + forward) |
| 541 | reverse_path = str(endpoint + '/' + reverse) |
| 542 | self.index_remove( |
| 543 | 'forward', path, owner, reverse_path) |
| 544 | self.index_remove( |
| 545 | 'reverse', endpoint, owner, forward_path) |
| 546 | |
| 547 | # destroy the association if it exists |
| 548 | self.update_association(forward_path, [endpoint], []) |
| 549 | self.update_association(reverse_path, [path], []) |
| 550 | |
| 551 | # If the associations interface endpoint comes |
| 552 | # or goes create or destroy the appropriate |
| 553 | # associations |
| 554 | for path in created: |
| 555 | for forward, reverse, endpoint in \ |
| 556 | self.index_get_associations(path, direction='reverse'): |
| 557 | forward_path = str(path + '/' + forward) |
| 558 | reverse_path = str(endpoint + '/' + reverse) |
| 559 | self.update_association(forward_path, [], [endpoint]) |
| 560 | self.update_association(reverse_path, [], [path]) |
| 561 | |
| 562 | for path in destroyed: |
| 563 | for forward, reverse, endpoint in \ |
| 564 | self.index_get_associations(path, direction='reverse'): |
| 565 | forward_path = str(path + '/' + forward) |
| 566 | reverse_path = str(endpoint + '/' + reverse) |
| 567 | self.update_association(forward_path, [endpoint], []) |
| 568 | self.update_association(reverse_path, [path], []) |
| 569 | |
| 570 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sa{sas}}') |
| 571 | def GetAncestors(self, path): |
| 572 | elements = filter(bool, path.split('/')) |
| 573 | paths = [] |
| 574 | objs = {} |
| 575 | while elements: |
| 576 | elements.pop() |
| 577 | paths.append('/' + '/'.join(elements)) |
| 578 | if path != '/': |
| 579 | paths.append('/') |
| 580 | |
| 581 | for path in paths: |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 582 | obj = self.cache_get(path) |
| 583 | if not obj: |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 584 | continue |
| 585 | objs[path] = obj |
| 586 | |
| 587 | return objs |
| 588 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 589 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 590 | if __name__ == '__main__': |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 591 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 592 | bus = dbus.SystemBus() |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame] | 593 | o = ObjectMapper(bus, obmc.mapper.MAPPER_PATH) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 594 | loop = gobject.MainLoop() |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 595 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 596 | loop.run() |