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', |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 51 | signal_name = 'InterfacesAdded', |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 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 | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 67 | name = self.bus.get_owned_name(self.bus_match, kw['sender']) |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 68 | if self.discovery_pending() or \ |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 69 | not self.bus_match(name): |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 70 | return |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 71 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 72 | matches = [ x for x in iprops.iterkeys() if self.intf_match(x) ] |
| 73 | d = self.cache.setdefault(path, {}) |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 74 | d.setdefault(kw['sender'], []).extend(matches) |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 75 | self.cache[path] = d |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 76 | |
| 77 | def interfaces_removed_handler(self, path, interfaces, **kw): |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 78 | name = self.bus.get_owned_name(self.bus_match, kw['sender']) |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 79 | if self.discovery_pending() or \ |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 80 | not self.bus_match(name): |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 81 | return |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 82 | item = self.cache[path] |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 83 | sender = kw['sender'] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 84 | for x in interfaces: |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 85 | if self.intf_match(x): |
| 86 | try: |
| 87 | item[sender].remove(x) |
| 88 | except ValueError: |
| 89 | pass |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 90 | |
| 91 | # remove the owner if there aren't any interfaces left |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 92 | if not item[sender]: |
| 93 | del item[sender] |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 94 | |
| 95 | # update if interfaces remain |
| 96 | if item: |
| 97 | self.cache[path] = item |
| 98 | # mark for removal if no interfaces remain |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 99 | elif self.cache.get_children(path): |
| 100 | self.cache.demote(path) |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 101 | # delete the entire path if everything is gone |
| 102 | else: |
| 103 | del self.cache[path] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 104 | |
| 105 | def process_new_owner(self, name): |
| 106 | # unique name |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 107 | return self.discover([ IntrospectionParser(name, |
| 108 | self.bus.dbus, self.tag_match, self.intf_match) ]) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 109 | |
| 110 | def process_old_owner(self, name): |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 111 | for x,y in self.cache.dataitems(): |
| 112 | if name not in y: |
| 113 | continue |
| 114 | del y[name] |
| 115 | if y: |
| 116 | self.cache[x] = y |
| 117 | elif self.cache.get_children(x): |
| 118 | self.cache.demote(x) |
| 119 | else: |
| 120 | del self.cache[x] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 121 | |
| 122 | def bus_handler(self, service, old, new): |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 123 | if self.discovery_pending() or \ |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 124 | not self.bus_match(service): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 125 | return |
| 126 | |
| 127 | if new: |
| 128 | self.process_new_owner(new) |
| 129 | if old: |
| 130 | self.process_old_owner(old) |
| 131 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 132 | def add_interfaces(self, path, owner, interfaces): |
| 133 | d = self.cache.setdefault(path, { }) |
| 134 | d.setdefault(owner, []).extend(interfaces) |
| 135 | self.cache[path] = d |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 136 | |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 137 | def add_items(self, owner, bus_items): |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 138 | for x,y in bus_items.iteritems(): |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 139 | self.add_interfaces(x, owner, y['interfaces']) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 140 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 141 | def discover(self, owners = None): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 142 | if not owners: |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 143 | owners = [ IntrospectionParser(x, self.bus.dbus, |
| 144 | self.tag_match, self.intf_match) \ |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 145 | for x in self.bus.get_owner_names(self.bus_match) ] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 146 | for o in owners: |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 147 | self.add_items(o.name, o.introspect()) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 148 | |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 149 | if self.discovery_pending(): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 150 | print "ObjectMapper discovery complete..." |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame] | 151 | self.service = dbus.service.BusName( |
| 152 | OpenBMCMapper.MAPPER_NAME, self.bus.dbus) |
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, 's', 'a{sas}') |
| 155 | def GetObject(self, path): |
Brad Bishop | 3640ecd | 2015-11-03 14:46:10 -0500 | [diff] [blame] | 156 | o = self.cache.get(path) |
| 157 | if not o: |
| 158 | raise MapperNotFoundException(path) |
| 159 | return o |
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', 'as') |
| 162 | def GetSubTreePaths(self, path, depth): |
Brad Bishop | 3640ecd | 2015-11-03 14:46:10 -0500 | [diff] [blame] | 163 | try: |
| 164 | return self.cache.iterkeys(path, depth) |
| 165 | except KeyError: |
| 166 | raise MapperNotFoundException(path) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 167 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 168 | @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'a{sa{sas}}') |
| 169 | def GetSubTree(self, path, depth): |
Brad Bishop | 3640ecd | 2015-11-03 14:46:10 -0500 | [diff] [blame] | 170 | try: |
| 171 | return { x:y for x, y in self.cache.dataitems(path, depth) } |
| 172 | except KeyError: |
| 173 | raise MapperNotFoundException(path) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 174 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 175 | class BusWrapper: |
| 176 | def __init__(self, bus): |
| 177 | self.dbus = bus |
| 178 | |
Brad Bishop | 5ffc2a3 | 2015-11-25 08:46:01 -0500 | [diff] [blame^] | 179 | def get_owned_name(self, match, bus): |
| 180 | for x in self.get_service_names(match): |
| 181 | if self.dbus.get_name_owner(x) == bus: |
| 182 | return x |
| 183 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 184 | def get_service_names(self, match): |
| 185 | # these are well known names |
| 186 | return [ x for x in self.dbus.list_names() \ |
Brad Bishop | 65b7ceb | 2015-11-04 23:05:56 -0500 | [diff] [blame] | 187 | if match(x) ] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 188 | |
| 189 | def get_owner_names(self, match): |
| 190 | # these are unique connection names |
| 191 | return list( set( [ self.dbus.get_name_owner(x) \ |
| 192 | for x in self.get_service_names(match) ] ) ) |
| 193 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 194 | if __name__ == '__main__': |
| 195 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 196 | bus = dbus.SystemBus() |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 197 | o = ObjectMapper(BusWrapper(bus), OpenBMCMapper.MAPPER_PATH) |
| 198 | loop = gobject.MainLoop() |
| 199 | |
| 200 | loop.run() |