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