Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Contributors Listed Below - COPYRIGHT 2015 |
| 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 | ae0c0af | 2015-11-09 18:41:28 -0500 | [diff] [blame^] | 24 | from OpenBMCMapper import IntrospectionParser, PathTree |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 25 | import OpenBMCMapper |
| 26 | |
Brad Bishop | ae0c0af | 2015-11-09 18:41:28 -0500 | [diff] [blame^] | 27 | class MapperNotFoundException(dbus.exceptions.DBusException): |
| 28 | _dbus_error_name = OpenBMCMapper.MAPPER_NOT_FOUND |
| 29 | def __init__(self, path): |
| 30 | super(MapperNotFoundException, self).__init__( |
| 31 | "path or object not found: %s" %(path)) |
| 32 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 33 | class ObjectMapper(dbus.service.Object): |
| 34 | def __init__(self, bus, path, |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 35 | name_match = OpenBMCMapper.org_dot_openbmc_match, |
| 36 | intf_match = OpenBMCMapper.org_dot_openbmc_match): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 37 | super(ObjectMapper, self).__init__(bus.dbus, path) |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 38 | self.cache = PathTree() |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 39 | self.bus = bus |
| 40 | self.name_match = name_match |
| 41 | self.intf_match = intf_match |
Brad Bishop | a8e752f | 2015-11-03 09:24:48 -0500 | [diff] [blame] | 42 | self.tag_match = OpenBMCMapper.ListMatch(['children', 'interface']) |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 43 | self.service = None |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 44 | |
| 45 | gobject.idle_add(self.discover) |
| 46 | self.bus.dbus.add_signal_receiver(self.bus_handler, |
| 47 | dbus_interface = dbus.BUS_DAEMON_IFACE, |
| 48 | signal_name = 'NameOwnerChanged') |
| 49 | self.bus.dbus.add_signal_receiver(self.interfaces_added_handler, |
| 50 | dbus_interface = dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 51 | signal_name = 'InterfaceesAdded', |
| 52 | sender_keyword = 'sender') |
| 53 | self.bus.dbus.add_signal_receiver(self.interfaces_removed_handler, |
| 54 | dbus_interface = dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
| 55 | signal_name = 'InterfacesRemoved', |
| 56 | sender_keyword = 'sender') |
| 57 | |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 58 | def bus_match(self, name): |
| 59 | if name == OpenBMCMapper.MAPPER_NAME: |
| 60 | return False |
| 61 | return self.name_match(name) |
| 62 | |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 63 | def discovery_pending(self): |
| 64 | return not bool(self.service) |
| 65 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 66 | def interfaces_added_handler(self, path, iprops, **kw): |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 67 | if self.discovery_pending() or \ |
| 68 | not self.bus_match(kw['sender']): |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 69 | return |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 70 | matches = [ x for x in iprops.iterkeys() if self.intf_match(x) ] |
| 71 | d = self.cache.setdefault(path, {}) |
| 72 | d[path].setdefault(kw['sender'], []).extend(matches) |
| 73 | self.cache[path] = d |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 74 | |
| 75 | def interfaces_removed_handler(self, path, interfaces, **kw): |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 76 | if self.discovery_pending() or \ |
| 77 | not self.bus_match(kw['sender']): |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 78 | return |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 79 | item = self.cache[path] |
| 80 | name = kw['sender'] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 81 | for x in interfaces: |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 82 | item[name].remove(x) |
| 83 | |
| 84 | # remove the owner if there aren't any interfaces left |
| 85 | if not item[name]: |
| 86 | del item[name] |
| 87 | |
| 88 | # update if interfaces remain |
| 89 | if item: |
| 90 | self.cache[path] = item |
| 91 | # mark for removal if no interfaces remain |
| 92 | elif self.cache.get_children(item): |
| 93 | self.cache.demote(item) |
| 94 | # delete the entire path if everything is gone |
| 95 | else: |
| 96 | del self.cache[path] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 97 | |
| 98 | def process_new_owner(self, name): |
| 99 | # unique name |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 100 | return self.discover([ IntrospectionParser(name, |
| 101 | self.bus.dbus, self.tag_match, self.intf_match) ]) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 102 | |
| 103 | def process_old_owner(self, name): |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 104 | for x,y in self.cache.dataitems(): |
| 105 | if name not in y: |
| 106 | continue |
| 107 | del y[name] |
| 108 | if y: |
| 109 | self.cache[x] = y |
| 110 | elif self.cache.get_children(x): |
| 111 | self.cache.demote(x) |
| 112 | else: |
| 113 | del self.cache[x] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 114 | |
| 115 | def bus_handler(self, service, old, new): |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 116 | if self.discovery_pending() or \ |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 117 | not self.bus_match(service): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 118 | return |
| 119 | |
| 120 | if new: |
| 121 | self.process_new_owner(new) |
| 122 | if old: |
| 123 | self.process_old_owner(old) |
| 124 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 125 | def add_interfaces(self, path, owner, interfaces): |
| 126 | d = self.cache.setdefault(path, { }) |
| 127 | d.setdefault(owner, []).extend(interfaces) |
| 128 | self.cache[path] = d |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 129 | |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 130 | def add_items(self, owner, bus_items): |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 131 | for x,y in bus_items.iteritems(): |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 132 | self.add_interfaces(x, owner, y['interfaces']) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 133 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 134 | def discover(self, owners = None): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 135 | if not owners: |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 136 | owners = [ IntrospectionParser(x, self.bus.dbus, |
| 137 | self.tag_match, self.intf_match) \ |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 138 | for x in self.bus.get_owner_names(self.bus_match) ] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 139 | for o in owners: |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 140 | self.add_items(o.name, o.introspect()) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 141 | |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 142 | if self.discovery_pending(): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 143 | print "ObjectMapper discovery complete..." |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 144 | self.service = dbus.service.BusName( |
| 145 | OpenBMCMapper.MAPPER_NAME, self.bus.dbus) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 146 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 147 | @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 's', 'a{sas}') |
| 148 | def GetObject(self, path): |
Brad Bishop | 3640ecd | 2015-11-03 14:46:10 -0500 | [diff] [blame] | 149 | o = self.cache.get(path) |
| 150 | if not o: |
| 151 | raise MapperNotFoundException(path) |
| 152 | return o |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 153 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 154 | @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'as') |
| 155 | def GetSubTreePaths(self, path, depth): |
Brad Bishop | 3640ecd | 2015-11-03 14:46:10 -0500 | [diff] [blame] | 156 | try: |
| 157 | return self.cache.iterkeys(path, depth) |
| 158 | except KeyError: |
| 159 | raise MapperNotFoundException(path) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 160 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 161 | @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'a{sa{sas}}') |
| 162 | def GetSubTree(self, path, depth): |
Brad Bishop | 3640ecd | 2015-11-03 14:46:10 -0500 | [diff] [blame] | 163 | try: |
| 164 | return { x:y for x, y in self.cache.dataitems(path, depth) } |
| 165 | except KeyError: |
| 166 | raise MapperNotFoundException(path) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 167 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 168 | class BusWrapper: |
| 169 | def __init__(self, bus): |
| 170 | self.dbus = bus |
| 171 | |
| 172 | def get_service_names(self, match): |
| 173 | # these are well known names |
| 174 | return [ x for x in self.dbus.list_names() \ |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 175 | if match(x) ] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 176 | |
| 177 | def get_owner_names(self, match): |
| 178 | # these are unique connection names |
| 179 | return list( set( [ self.dbus.get_name_owner(x) \ |
| 180 | for x in self.get_service_names(match) ] ) ) |
| 181 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 182 | if __name__ == '__main__': |
| 183 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 184 | bus = dbus.SystemBus() |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 185 | o = ObjectMapper(BusWrapper(bus), OpenBMCMapper.MAPPER_PATH) |
| 186 | loop = gobject.MainLoop() |
| 187 | |
| 188 | loop.run() |