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 | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame^] | 52 | def discovery_pending(self): |
| 53 | return not bool(self.service) |
| 54 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 55 | def interfaces_added_handler(self, path, iprops, **kw): |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame^] | 56 | if self.discovery_pending(): |
| 57 | return |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 58 | matches = [ x for x in iprops.iterkeys() if self.intf_match(x) ] |
| 59 | d = self.cache.setdefault(path, {}) |
| 60 | d[path].setdefault(kw['sender'], []).extend(matches) |
| 61 | self.cache[path] = d |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 62 | |
| 63 | def interfaces_removed_handler(self, path, interfaces, **kw): |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame^] | 64 | if self.discovery_pending(): |
| 65 | return |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 66 | item = self.cache[path] |
| 67 | name = kw['sender'] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 68 | for x in interfaces: |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 69 | item[name].remove(x) |
| 70 | |
| 71 | # remove the owner if there aren't any interfaces left |
| 72 | if not item[name]: |
| 73 | del item[name] |
| 74 | |
| 75 | # update if interfaces remain |
| 76 | if item: |
| 77 | self.cache[path] = item |
| 78 | # mark for removal if no interfaces remain |
| 79 | elif self.cache.get_children(item): |
| 80 | self.cache.demote(item) |
| 81 | # delete the entire path if everything is gone |
| 82 | else: |
| 83 | del self.cache[path] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 84 | |
| 85 | def process_new_owner(self, name): |
| 86 | # unique name |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 87 | return self.discover([ IntrospectionParser(name, |
| 88 | self.bus.dbus, self.tag_match, self.intf_match) ]) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 89 | |
| 90 | def process_old_owner(self, name): |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 91 | for x,y in self.cache.dataitems(): |
| 92 | if name not in y: |
| 93 | continue |
| 94 | del y[name] |
| 95 | if y: |
| 96 | self.cache[x] = y |
| 97 | elif self.cache.get_children(x): |
| 98 | self.cache.demote(x) |
| 99 | else: |
| 100 | del self.cache[x] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 101 | |
| 102 | def bus_handler(self, service, old, new): |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame^] | 103 | if self.discovery_pending() or \ |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 104 | not self.name_match(service): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 105 | return |
| 106 | |
| 107 | if new: |
| 108 | self.process_new_owner(new) |
| 109 | if old: |
| 110 | self.process_old_owner(old) |
| 111 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 112 | def add_interfaces(self, path, owner, interfaces): |
| 113 | d = self.cache.setdefault(path, { }) |
| 114 | d.setdefault(owner, []).extend(interfaces) |
| 115 | self.cache[path] = d |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 116 | |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 117 | def add_items(self, owner, bus_items): |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 118 | for x,y in bus_items.iteritems(): |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 119 | self.add_interfaces(x, owner, y['interfaces']) |
Brad Bishop | 86398d2 | 2015-10-28 21:58:31 -0400 | [diff] [blame] | 120 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 121 | def discover(self, owners = None): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 122 | if not owners: |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 123 | owners = [ IntrospectionParser(x, self.bus.dbus, |
| 124 | self.tag_match, self.intf_match) \ |
| 125 | for x in self.bus.get_owner_names(self.name_match) ] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 126 | for o in owners: |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 127 | self.add_items(o.name, o.introspect()) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 128 | |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame^] | 129 | if self.discovery_pending(): |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 130 | print "ObjectMapper discovery complete..." |
Brad Bishop | cb7aa60 | 2015-11-03 10:40:17 -0500 | [diff] [blame^] | 131 | self.service = dbus.service.BusName( |
| 132 | OpenBMCMapper.MAPPER_NAME, self.bus.dbus) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 133 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 134 | @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 's', 'a{sas}') |
| 135 | def GetObject(self, path): |
| 136 | return self.cache[path] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 137 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 138 | @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'as') |
| 139 | def GetSubTreePaths(self, path, depth): |
| 140 | return self.cache.iterkeys(path, depth) |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 141 | |
Brad Bishop | 54d3ec9 | 2015-11-03 09:32:34 -0500 | [diff] [blame] | 142 | @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'a{sa{sas}}') |
| 143 | def GetSubTree(self, path, depth): |
| 144 | return { x:y for x, y in self.cache.dataitems(path, depth) } |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 145 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 146 | class BusWrapper: |
| 147 | def __init__(self, bus): |
| 148 | self.dbus = bus |
| 149 | |
| 150 | def get_service_names(self, match): |
| 151 | # these are well known names |
| 152 | return [ x for x in self.dbus.list_names() \ |
Brad Bishop | 6fb8461 | 2015-11-01 00:06:24 -0400 | [diff] [blame] | 153 | if match(x) and x != OpenBMCMapper.MAPPER_NAME ] |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 154 | |
| 155 | def get_owner_names(self, match): |
| 156 | # these are unique connection names |
| 157 | return list( set( [ self.dbus.get_name_owner(x) \ |
| 158 | for x in self.get_service_names(match) ] ) ) |
| 159 | |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 160 | if __name__ == '__main__': |
| 161 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 162 | bus = dbus.SystemBus() |
Brad Bishop | 732c6db | 2015-10-19 14:49:21 -0400 | [diff] [blame] | 163 | o = ObjectMapper(BusWrapper(bus), OpenBMCMapper.MAPPER_PATH) |
| 164 | loop = gobject.MainLoop() |
| 165 | |
| 166 | loop.run() |