Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 1 | # Contributors Listed Below - COPYRIGHT 2016 |
| 2 | # [+] International Business Machines Corp. |
| 3 | # |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | |
| 17 | import dbus |
| 18 | import obmc.dbuslib.enums |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 19 | import obmc.utils.pathtree |
| 20 | |
Brad Bishop | 5fd9c47 | 2016-07-25 09:09:00 -0400 | [diff] [blame] | 21 | |
Brad Bishop | b301f2f | 2016-10-05 20:02:57 -0400 | [diff] [blame] | 22 | MAPPER_NAME = 'xyz.openbmc_project.ObjectMapper' |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 23 | MAPPER_IFACE = MAPPER_NAME |
Leonel Gonzalez | 7fb0fe9 | 2017-03-09 15:27:36 -0600 | [diff] [blame] | 24 | MAPPER_PATH = '/xyz/openbmc_project/object_mapper' |
Brad Bishop | fba8ea3 | 2016-09-20 15:45:18 -0400 | [diff] [blame] | 25 | MAPPER_NOT_FOUND = 'org.freedesktop.DBus.Error.FileNotFound' |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class Mapper: |
| 29 | def __init__(self, bus): |
| 30 | self.bus = bus |
| 31 | obj = bus.get_object(MAPPER_NAME, MAPPER_PATH, introspect=False) |
| 32 | self.iface = dbus.Interface( |
| 33 | obj, dbus_interface=MAPPER_IFACE) |
| 34 | |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 35 | @staticmethod |
Lei YU | 9e94fb6 | 2017-01-11 11:23:58 +0800 | [diff] [blame] | 36 | def retry(func, retries, interval): |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 37 | e = None |
| 38 | count = 0 |
| 39 | while count < retries: |
| 40 | try: |
| 41 | return func() |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 42 | except dbus.exceptions.DBusException as e: |
Matt Spinler | 3c18b9f | 2017-01-27 14:41:48 -0600 | [diff] [blame] | 43 | if e.get_dbus_name() not in \ |
| 44 | ['org.freedesktop.DBus.Error.ObjectPathInUse', |
| 45 | 'org.freedesktop.DBus.Error.LimitsExceeded']: |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 46 | raise |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 47 | |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 48 | count += 1 |
Lei YU | 9e94fb6 | 2017-01-11 11:23:58 +0800 | [diff] [blame] | 49 | if interval > 0: |
| 50 | from time import sleep |
| 51 | sleep(interval) |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 52 | if e: |
| 53 | raise e |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 54 | |
Cory Klokman | a5513ab | 2017-01-24 17:27:16 -0600 | [diff] [blame] | 55 | def get_object(self, path, retries=5, interfaces=[], interval=0.2): |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 56 | return self.retry( |
Brad Bishop | bf464f4 | 2016-11-02 00:37:06 -0400 | [diff] [blame] | 57 | lambda: self.iface.GetObject( |
| 58 | path, interfaces, signature='sas'), |
Lei YU | 9e94fb6 | 2017-01-11 11:23:58 +0800 | [diff] [blame] | 59 | retries, interval) |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 60 | |
Adriana Kobylak | a1f2422 | 2018-01-10 16:09:07 -0600 | [diff] [blame] | 61 | def get_subtree_paths( |
| 62 | self, path='/', depth=0, retries=5, interfaces=[], interval=0.2): |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 63 | return self.retry( |
Brad Bishop | bf464f4 | 2016-11-02 00:37:06 -0400 | [diff] [blame] | 64 | lambda: self.iface.GetSubTreePaths( |
| 65 | path, depth, interfaces, signature='sias'), |
Lei YU | 9e94fb6 | 2017-01-11 11:23:58 +0800 | [diff] [blame] | 66 | retries, interval) |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 67 | |
Adriana Kobylak | a1f2422 | 2018-01-10 16:09:07 -0600 | [diff] [blame] | 68 | def get_subtree( |
| 69 | self, path='/', depth=0, retries=5, interfaces=[], interval=0.2): |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 70 | return self.retry( |
Brad Bishop | bf464f4 | 2016-11-02 00:37:06 -0400 | [diff] [blame] | 71 | lambda: self.iface.GetSubTree( |
| 72 | path, depth, interfaces, signature='sias'), |
Lei YU | 9e94fb6 | 2017-01-11 11:23:58 +0800 | [diff] [blame] | 73 | retries, interval) |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 74 | |
Cory Klokman | a5513ab | 2017-01-24 17:27:16 -0600 | [diff] [blame] | 75 | def get_ancestors(self, path, retries=5, interfaces=[], interval=0.2): |
Brad Bishop | 1eba37d | 2016-09-20 08:12:25 -0400 | [diff] [blame] | 76 | return self.retry( |
Brad Bishop | bf464f4 | 2016-11-02 00:37:06 -0400 | [diff] [blame] | 77 | lambda: self.iface.GetAncestors( |
| 78 | path, interfaces, signature='sas'), |
Lei YU | 9e94fb6 | 2017-01-11 11:23:58 +0800 | [diff] [blame] | 79 | retries, interval) |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 80 | |
| 81 | @staticmethod |
| 82 | def __try_properties_interface(f, *a): |
| 83 | try: |
| 84 | return f(*a) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 85 | except dbus.exceptions.DBusException as e: |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 86 | if obmc.dbuslib.enums.DBUS_UNKNOWN_INTERFACE in \ |
Adriana Kobylak | a8f9765 | 2017-12-11 13:32:10 -0600 | [diff] [blame] | 87 | e.get_dbus_name(): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 88 | # interface doesn't have any properties |
| 89 | return None |
| 90 | if obmc.dbuslib.enums.DBUS_UNKNOWN_METHOD == e.get_dbus_name(): |
| 91 | # properties interface not implemented at all |
| 92 | return None |
| 93 | raise |
| 94 | |
| 95 | @staticmethod |
| 96 | def __get_properties_on_iface(properties_iface, iface): |
| 97 | properties = Mapper.__try_properties_interface( |
| 98 | properties_iface.GetAll, iface) |
| 99 | if properties is None: |
| 100 | return {} |
| 101 | return properties |
| 102 | |
| 103 | def __get_properties_on_bus(self, path, bus, interfaces, match): |
| 104 | properties = {} |
| 105 | obj = self.bus.get_object(bus, path, introspect=False) |
| 106 | properties_iface = dbus.Interface( |
| 107 | obj, dbus_interface=dbus.PROPERTIES_IFACE) |
| 108 | for i in interfaces: |
Brad Bishop | 8f30173 | 2017-09-11 20:09:48 -0400 | [diff] [blame] | 109 | if match and not match(i): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 110 | continue |
| 111 | properties.update(self.__get_properties_on_iface( |
| 112 | properties_iface, i)) |
| 113 | |
| 114 | return properties |
| 115 | |
| 116 | def enumerate_object( |
| 117 | self, path, |
Brad Bishop | 8f30173 | 2017-09-11 20:09:48 -0400 | [diff] [blame] | 118 | match=lambda x: x != dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 119 | mapper_data=None): |
| 120 | if mapper_data is None: |
| 121 | mapper_data = {path: self.get_object(path)} |
| 122 | |
| 123 | obj = {} |
| 124 | |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 125 | for owner, interfaces in mapper_data[path].items(): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 126 | obj.update( |
| 127 | self.__get_properties_on_bus( |
| 128 | path, owner, interfaces, match)) |
| 129 | |
| 130 | return obj |
| 131 | |
| 132 | def enumerate_subtree( |
| 133 | self, path='/', |
Brad Bishop | 8f30173 | 2017-09-11 20:09:48 -0400 | [diff] [blame] | 134 | match=lambda x: x != dbus.BUS_DAEMON_IFACE + '.ObjectManager', |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 135 | mapper_data=None): |
| 136 | if mapper_data is None: |
| 137 | mapper_data = self.get_subtree(path) |
| 138 | managers = {} |
| 139 | owners = [] |
| 140 | |
| 141 | # look for objectmanager implementations as they result |
| 142 | # in fewer dbus calls |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 143 | for path, bus_data in mapper_data.items(): |
| 144 | for owner, interfaces in bus_data.items(): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 145 | owners.append(owner) |
| 146 | if dbus.BUS_DAEMON_IFACE + '.ObjectManager' in interfaces: |
| 147 | managers[owner] = path |
| 148 | |
| 149 | # also look in the parent objects |
| 150 | ancestors = self.get_ancestors(path) |
| 151 | |
| 152 | # finally check the root for one too |
| 153 | try: |
| 154 | ancestors.update({path: self.get_object(path)}) |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 155 | except dbus.exceptions.DBusException as e: |
Brad Bishop | 5fd9c47 | 2016-07-25 09:09:00 -0400 | [diff] [blame] | 156 | if e.get_dbus_name() != MAPPER_NOT_FOUND: |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 157 | raise |
| 158 | |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 159 | for path, bus_data in ancestors.items(): |
| 160 | for owner, interfaces in bus_data.items(): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 161 | if dbus.BUS_DAEMON_IFACE + '.ObjectManager' in interfaces: |
| 162 | managers[owner] = path |
| 163 | |
| 164 | # make all the manager gmo (get managed objects) calls |
| 165 | results = {} |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 166 | for owner, path in managers.items(): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 167 | if owner not in owners: |
| 168 | continue |
| 169 | obj = self.bus.get_object(owner, path, introspect=False) |
| 170 | iface = dbus.Interface( |
| 171 | obj, dbus.BUS_DAEMON_IFACE + '.ObjectManager') |
| 172 | |
| 173 | # flatten (remove interface names) gmo results |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 174 | for path, interfaces in iface.GetManagedObjects().items(): |
| 175 | if path not in iter(mapper_data.keys()): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 176 | continue |
| 177 | properties = {} |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 178 | for iface, props in interfaces.items(): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 179 | properties.update(props) |
| 180 | results.setdefault(path, {}).setdefault(owner, properties) |
| 181 | |
| 182 | # make dbus calls for any remaining objects |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 183 | for path, bus_data in mapper_data.items(): |
| 184 | for owner, interfaces in bus_data.items(): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 185 | if results.setdefault(path, {}).setdefault(owner, {}): |
| 186 | continue |
| 187 | results[path][owner].update( |
| 188 | self.__get_properties_on_bus( |
| 189 | path, owner, interfaces, match)) |
| 190 | |
| 191 | objs = obmc.utils.pathtree.PathTree() |
Balaji B Rao | 84e331a | 2017-11-09 21:19:13 -0600 | [diff] [blame] | 192 | for path, owners in results.items(): |
| 193 | for owner, properties in owners.items(): |
Brad Bishop | ea2bc0c | 2016-07-25 09:05:39 -0400 | [diff] [blame] | 194 | objs.setdefault(path, {}).update(properties) |
| 195 | |
| 196 | return objs |