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(): |
| 89 | self.results[k] = v.keys() |
| 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)) |
| 97 | for x in children] |
| 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, |
| 177 | dbus_interface= |
| 178 | dbus.BUS_DAEMON_IFACE, |
| 179 | signal_name='NameOwnerChanged') |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 180 | self.bus.add_signal_receiver( |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 181 | self.interfaces_added_handler, |
| 182 | dbus_interface= |
| 183 | dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 184 | signal_name='InterfacesAdded', |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame] | 185 | sender_keyword='sender', |
| 186 | path_keyword='sender_path') |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 187 | self.bus.add_signal_receiver( |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 188 | self.interfaces_removed_handler, |
| 189 | dbus_interface=dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 190 | signal_name='InterfacesRemoved', |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame] | 191 | sender_keyword='sender', |
| 192 | path_keyword='sender_path') |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 193 | self.bus.add_signal_receiver( |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 194 | self.properties_changed_handler, |
| 195 | dbus_interface=dbus.PROPERTIES_IFACE, |
| 196 | signal_name='PropertiesChanged', |
| 197 | path_keyword='path', |
| 198 | sender_keyword='sender') |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 199 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 200 | def bus_match(self, owner): |
| 201 | # Ignore my own signals |
| 202 | return owner != obmc.mapper.MAPPER_NAME and \ |
| 203 | self.name_match(owner) |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 204 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 205 | def discovery_pending(self): |
| 206 | return not bool(self.service) |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 207 | |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 208 | def cache_get(self, path): |
| 209 | cache_entry = self.cache.get(path, {}) |
| 210 | if cache_entry is None: |
| 211 | # hide path elements without any interfaces |
| 212 | cache_entry = {} |
| 213 | return cache_entry |
| 214 | |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame] | 215 | def add_new_objmgr(self, path, owner): |
| 216 | # We don't get a signal for the ObjectManager |
| 217 | # interface itself, so if we see a signal from |
| 218 | # make sure its in our cache, and add it if not. |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 219 | cache_entry = self.cache_get(path) |
Brad Bishop | c568e02 | 2016-03-23 14:50:05 -0400 | [diff] [blame] | 220 | old = self.interfaces_get(cache_entry, owner) |
| 221 | new = list(set(old).union([dbus.BUS_DAEMON_IFACE + '.ObjectManager'])) |
| 222 | self.update_interfaces(path, owner, old, new) |
| 223 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 224 | def interfaces_added_handler(self, path, iprops, **kw): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 225 | path = str(path) |
| 226 | owner = str(kw['sender']) |
| 227 | interfaces = self.get_signal_interfaces(owner, iprops.iterkeys()) |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 228 | if interfaces: |
| 229 | self.add_new_objmgr(str(kw['sender_path']), owner) |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 230 | cache_entry = self.cache_get(path) |
| 231 | old = self.interfaces_get(cache_entry, owner) |
| 232 | new = list(set(interfaces).union(old)) |
| 233 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 234 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 235 | def interfaces_removed_handler(self, path, interfaces, **kw): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 236 | path = str(path) |
| 237 | owner = str(kw['sender']) |
| 238 | interfaces = self.get_signal_interfaces(owner, interfaces) |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 239 | if interfaces: |
| 240 | self.add_new_objmgr(str(kw['sender_path']), owner) |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 241 | cache_entry = self.cache_get(path) |
| 242 | old = self.interfaces_get(cache_entry, owner) |
| 243 | new = list(set(old).difference(interfaces)) |
| 244 | self.update_interfaces(path, owner, old, new) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 245 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 246 | def properties_changed_handler(self, interface, new, old, **kw): |
| 247 | owner = str(kw['sender']) |
| 248 | path = str(kw['path']) |
| 249 | interfaces = self.get_signal_interfaces(owner, [interface]) |
| 250 | if not self.is_association(interfaces): |
| 251 | return |
| 252 | associations = new.get('associations', None) |
| 253 | if associations is None: |
| 254 | return |
| 255 | |
| 256 | associations = [ |
| 257 | (str(x), str(y), str(z)) for x, y, z in associations] |
| 258 | self.update_associations( |
| 259 | path, owner, |
| 260 | self.index_get_associations(path, [owner]), |
| 261 | associations) |
| 262 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 263 | def process_new_owner(self, owned_name, owner): |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 264 | # unique name |
Brad Bishop | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 265 | try: |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 266 | return self.discover([(owned_name, owner)]) |
Brad Bishop | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 267 | except dbus.exceptions.DBusException, e: |
| 268 | if obmc.dbuslib.enums.DBUS_UNKNOWN_SERVICE \ |
| 269 | not in e.get_dbus_name(): |
| 270 | raise |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 271 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 272 | def process_old_owner(self, owned_name, owner): |
| 273 | if owner in self.bus_map: |
| 274 | del self.bus_map[owner] |
| 275 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 276 | for path, item in self.cache.dataitems(): |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 277 | old = self.interfaces_get(item, owner) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 278 | # remove all interfaces for this service |
| 279 | self.update_interfaces( |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 280 | path, owner, old=old, new=[]) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 281 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 282 | def bus_handler(self, owned_name, old, new): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 283 | valid = False |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 284 | if not obmc.dbuslib.bindings.is_unique(owned_name): |
| 285 | valid = self.valid_signal(owned_name) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 286 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 287 | if valid and new: |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 288 | self.process_new_owner(owned_name, new) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 289 | if valid and old: |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 290 | self.process_old_owner(owned_name, old) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 291 | |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 292 | def update_interfaces(self, path, owner, old, new): |
| 293 | cache_entry = self.cache.setdefault(path, {}) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 294 | created = [] if self.has_interfaces(cache_entry) else [path] |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 295 | added = list(set(new).difference(old)) |
| 296 | removed = list(set(old).difference(new)) |
| 297 | self.interfaces_append(cache_entry, owner, added) |
| 298 | self.interfaces_remove(cache_entry, owner, removed, path) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 299 | destroyed = [] if self.has_interfaces(cache_entry) else [path] |
| 300 | |
| 301 | # react to anything that requires association updates |
| 302 | new_assoc = [] |
| 303 | old_assoc = [] |
| 304 | if self.is_association(added): |
| 305 | new_assoc = self.dbus_get_associations(path, owner) |
| 306 | if self.is_association(removed): |
| 307 | old_assoc = self.index_get_associations(path, [owner]) |
| 308 | self.update_associations( |
| 309 | path, owner, old_assoc, new_assoc, created, destroyed) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 310 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 311 | def add_items(self, owner, bus_items): |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 312 | for path, items in bus_items.iteritems(): |
| 313 | # convert dbus types to native. |
Brad Bishop | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 314 | interfaces = [str(i) for i in items] |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 315 | self.update_interfaces(path, str(owner), old=[], new=interfaces) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 316 | |
Brad Bishop | 9cff26b | 2016-03-18 11:16:47 -0400 | [diff] [blame] | 317 | def discover(self, owners=[]): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 318 | def match(iface): |
| 319 | return iface == dbus.BUS_DAEMON_IFACE + '.ObjectManager' or \ |
| 320 | self.intf_match(iface) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 321 | if not owners: |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 322 | owned_names = [x for x in self.bus.list_names() |
| 323 | if self.bus_match(x)] |
| 324 | owners = [self.bus.get_name_owner(x) for x in owned_names] |
| 325 | owners = zip(owned_names, owners) |
| 326 | for owned_name, o in owners: |
Brad Bishop | ba86bc8 | 2016-05-05 22:09:27 -0400 | [diff] [blame] | 327 | self.add_items( |
| 328 | o, |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 329 | find_dbus_interfaces(self.bus, o, '/', self.intf_match)) |
| 330 | self.bus_map[o] = owned_name |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 331 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 332 | if self.discovery_pending(): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 333 | # add my object mananger instance |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 334 | self.bus_map[self.unique] = obmc.mapper.MAPPER_NAME |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 335 | self.add_items( |
| 336 | self.unique, |
| 337 | {obmc.dbuslib.bindings.OBJ_PREFIX: |
| 338 | {'interfaces': |
| 339 | [dbus.BUS_DAEMON_IFACE + '.ObjectManager']}}) |
| 340 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 341 | print "ObjectMapper discovery complete..." |
| 342 | self.service = dbus.service.BusName( |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 343 | obmc.mapper.MAPPER_NAME, self.bus) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 344 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 345 | def valid_signal(self, name): |
| 346 | if self.discovery_pending(): |
| 347 | return False |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 348 | |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 349 | if obmc.dbuslib.bindings.is_unique(name): |
| 350 | name = self.bus_map.get(name) |
| 351 | |
| 352 | return name is not None and \ |
| 353 | self.bus_match(name) |
Brad Bishop | fed968e | 2016-03-18 10:47:26 -0400 | [diff] [blame] | 354 | |
| 355 | def get_signal_interfaces(self, owner, interfaces): |
| 356 | filtered = [] |
| 357 | if self.valid_signal(owner): |
| 358 | filtered = [str(x) for x in interfaces if self.intf_match(x)] |
| 359 | |
| 360 | return filtered |
| 361 | |
| 362 | @staticmethod |
| 363 | def interfaces_get(item, owner, default=[]): |
| 364 | return item.get(owner, default) |
| 365 | |
| 366 | @staticmethod |
| 367 | def interfaces_append(item, owner, append): |
| 368 | interfaces = item.setdefault(owner, []) |
| 369 | item[owner] = list(set(append).union(interfaces)) |
| 370 | |
| 371 | def interfaces_remove(self, item, owner, remove, path): |
| 372 | interfaces = item.get(owner, []) |
| 373 | item[owner] = list(set(interfaces).difference(remove)) |
| 374 | |
| 375 | if not item[owner]: |
| 376 | # remove the owner if there aren't any interfaces left |
| 377 | del item[owner] |
| 378 | |
| 379 | if item: |
| 380 | # other owners remain |
| 381 | return |
| 382 | |
| 383 | if self.cache.get_children(path): |
| 384 | # there are still references to this path |
| 385 | # from objects further down the tree. |
| 386 | # mark it for removal if that changes |
| 387 | self.cache.demote(path) |
| 388 | else: |
| 389 | # delete the entire path if everything is gone |
| 390 | del self.cache[path] |
| 391 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 392 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sas}') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 393 | def GetObject(self, path): |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 394 | o = self.cache_get(path) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 395 | if not o: |
| 396 | raise MapperNotFoundException(path) |
| 397 | return o |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 398 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 399 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'as') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 400 | def GetSubTreePaths(self, path, depth): |
| 401 | try: |
| 402 | return self.cache.iterkeys(path, depth) |
| 403 | except KeyError: |
| 404 | raise MapperNotFoundException(path) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 405 | |
Brad Bishop | fb1f58e | 2016-03-04 15:19:13 -0500 | [diff] [blame] | 406 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 'si', 'a{sa{sas}}') |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 407 | def GetSubTree(self, path, depth): |
| 408 | try: |
| 409 | return {x: y for x, y in self.cache.dataitems(path, depth)} |
| 410 | except KeyError: |
| 411 | raise MapperNotFoundException(path) |
| 412 | |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 413 | @staticmethod |
| 414 | def has_interfaces(item): |
| 415 | for owner in item.iterkeys(): |
| 416 | if ObjectMapper.interfaces_get(item, owner): |
| 417 | return True |
| 418 | return False |
| 419 | |
| 420 | @staticmethod |
| 421 | def is_association(interfaces): |
| 422 | return obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE in interfaces |
| 423 | |
| 424 | def index_get(self, index, path, owners): |
| 425 | items = [] |
| 426 | item = self.index.get(index, {}) |
| 427 | item = item.get(path, {}) |
| 428 | for o in owners: |
| 429 | items.extend(item.get(o, [])) |
| 430 | return items |
| 431 | |
| 432 | def index_append(self, index, path, owner, assoc): |
| 433 | item = self.index.setdefault(index, {}) |
| 434 | item = item.setdefault(path, {}) |
| 435 | item = item.setdefault(owner, []) |
| 436 | item.append(assoc) |
| 437 | |
| 438 | def index_remove(self, index, path, owner, assoc): |
| 439 | index = self.index.get(index, {}) |
| 440 | owners = index.get(path, {}) |
| 441 | items = owners.get(owner, []) |
| 442 | if assoc in items: |
| 443 | items.remove(assoc) |
| 444 | if not items: |
| 445 | del owners[owner] |
| 446 | if not owners: |
| 447 | del index[path] |
| 448 | |
| 449 | def dbus_get_associations(self, path, owner): |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 450 | obj = self.bus.get_object(owner, path, introspect=False) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 451 | iface = dbus.Interface(obj, dbus.PROPERTIES_IFACE) |
| 452 | return [(str(f), str(r), str(e)) for f, r, e in iface.Get( |
| 453 | obmc.dbuslib.enums.OBMC_ASSOCIATIONS_IFACE, |
| 454 | 'associations')] |
| 455 | |
| 456 | def index_get_associations(self, path, owners=[], direction='forward'): |
| 457 | forward = 'forward' if direction == 'forward' else 'reverse' |
| 458 | reverse = 'reverse' if direction == 'forward' else 'forward' |
| 459 | |
| 460 | associations = [] |
| 461 | if not owners: |
| 462 | index = self.index.get(forward, {}) |
| 463 | owners = index.get(path, {}).keys() |
| 464 | |
| 465 | # f: forward |
| 466 | # r: reverse |
| 467 | for rassoc in self.index_get(forward, path, owners): |
| 468 | elements = rassoc.split('/') |
| 469 | rtype = ''.join(elements[-1:]) |
| 470 | fendpoint = '/'.join(elements[:-1]) |
| 471 | for fassoc in self.index_get(reverse, fendpoint, owners): |
| 472 | elements = fassoc.split('/') |
| 473 | ftype = ''.join(elements[-1:]) |
| 474 | rendpoint = '/'.join(elements[:-1]) |
| 475 | if rendpoint != path: |
| 476 | continue |
| 477 | associations.append((ftype, rtype, fendpoint)) |
| 478 | |
| 479 | return associations |
| 480 | |
| 481 | def update_association(self, path, removed, added): |
| 482 | iface = obmc.dbuslib.enums.OBMC_ASSOC_IFACE |
| 483 | create = [] if self.manager.get(path, False) else [iface] |
| 484 | |
| 485 | if added and create: |
| 486 | self.manager.add( |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 487 | path, Association(self.bus, path, added)) |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 488 | elif added: |
| 489 | self.manager.get(path).append(added) |
| 490 | |
| 491 | obj = self.manager.get(path, None) |
| 492 | if obj and removed: |
| 493 | obj.remove(removed) |
| 494 | |
| 495 | if obj and not obj.endpoints: |
| 496 | self.manager.remove(path) |
| 497 | |
| 498 | delete = [] if self.manager.get(path, False) else [iface] |
| 499 | |
| 500 | if create != delete: |
| 501 | self.update_interfaces( |
| 502 | path, self.unique, delete, create) |
| 503 | |
| 504 | def update_associations( |
| 505 | self, path, owner, old, new, created=[], destroyed=[]): |
| 506 | added = list(set(new).difference(old)) |
| 507 | removed = list(set(old).difference(new)) |
| 508 | for forward, reverse, endpoint in added: |
| 509 | # update the index |
| 510 | forward_path = str(path + '/' + forward) |
| 511 | reverse_path = str(endpoint + '/' + reverse) |
| 512 | self.index_append( |
| 513 | 'forward', path, owner, reverse_path) |
| 514 | self.index_append( |
| 515 | 'reverse', endpoint, owner, forward_path) |
| 516 | |
| 517 | # create the association if the endpoint exists |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 518 | if not self.cache_get(endpoint): |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 519 | continue |
| 520 | |
| 521 | self.update_association(forward_path, [], [endpoint]) |
| 522 | self.update_association(reverse_path, [], [path]) |
| 523 | |
| 524 | for forward, reverse, endpoint in removed: |
| 525 | # update the index |
| 526 | forward_path = str(path + '/' + forward) |
| 527 | reverse_path = str(endpoint + '/' + reverse) |
| 528 | self.index_remove( |
| 529 | 'forward', path, owner, reverse_path) |
| 530 | self.index_remove( |
| 531 | 'reverse', endpoint, owner, forward_path) |
| 532 | |
| 533 | # destroy the association if it exists |
| 534 | self.update_association(forward_path, [endpoint], []) |
| 535 | self.update_association(reverse_path, [path], []) |
| 536 | |
| 537 | # If the associations interface endpoint comes |
| 538 | # or goes create or destroy the appropriate |
| 539 | # associations |
| 540 | for path in created: |
| 541 | for forward, reverse, endpoint in \ |
| 542 | self.index_get_associations(path, direction='reverse'): |
| 543 | forward_path = str(path + '/' + forward) |
| 544 | reverse_path = str(endpoint + '/' + reverse) |
| 545 | self.update_association(forward_path, [], [endpoint]) |
| 546 | self.update_association(reverse_path, [], [path]) |
| 547 | |
| 548 | for path in destroyed: |
| 549 | for forward, reverse, endpoint in \ |
| 550 | self.index_get_associations(path, direction='reverse'): |
| 551 | forward_path = str(path + '/' + forward) |
| 552 | reverse_path = str(endpoint + '/' + reverse) |
| 553 | self.update_association(forward_path, [endpoint], []) |
| 554 | self.update_association(reverse_path, [path], []) |
| 555 | |
| 556 | @dbus.service.method(obmc.mapper.MAPPER_IFACE, 's', 'a{sa{sas}}') |
| 557 | def GetAncestors(self, path): |
| 558 | elements = filter(bool, path.split('/')) |
| 559 | paths = [] |
| 560 | objs = {} |
| 561 | while elements: |
| 562 | elements.pop() |
| 563 | paths.append('/' + '/'.join(elements)) |
| 564 | if path != '/': |
| 565 | paths.append('/') |
| 566 | |
| 567 | for path in paths: |
Brad Bishop | 10abe04 | 2016-05-02 23:11:19 -0400 | [diff] [blame] | 568 | obj = self.cache_get(path) |
| 569 | if not obj: |
Brad Bishop | d9fc21c | 2016-03-18 12:24:42 -0400 | [diff] [blame] | 570 | continue |
| 571 | objs[path] = obj |
| 572 | |
| 573 | return objs |
| 574 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 575 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 576 | if __name__ == '__main__': |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 577 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 578 | bus = dbus.SystemBus() |
Brad Bishop | 5693437 | 2016-06-05 18:24:41 -0400 | [diff] [blame^] | 579 | o = ObjectMapper(bus, obmc.mapper.MAPPER_PATH) |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 580 | loop = gobject.MainLoop() |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 581 | |
Brad Bishop | 4b84941 | 2016-02-04 15:40:26 -0500 | [diff] [blame] | 582 | loop.run() |