blob: bd19f1972d8705299ee63ba606113242eeacbf5f [file] [log] [blame]
Brad Bishopea2bc0c2016-07-25 09:05:39 -04001# 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
17import dbus
18import obmc.dbuslib.enums
Brad Bishopea2bc0c2016-07-25 09:05:39 -040019import obmc.utils.pathtree
20
Brad Bishop5fd9c472016-07-25 09:09:00 -040021
Brad Bishopb301f2f2016-10-05 20:02:57 -040022MAPPER_NAME = 'xyz.openbmc_project.ObjectMapper'
Brad Bishopea2bc0c2016-07-25 09:05:39 -040023MAPPER_IFACE = MAPPER_NAME
Leonel Gonzalez7fb0fe92017-03-09 15:27:36 -060024MAPPER_PATH = '/xyz/openbmc_project/object_mapper'
Brad Bishopfba8ea32016-09-20 15:45:18 -040025MAPPER_NOT_FOUND = 'org.freedesktop.DBus.Error.FileNotFound'
Brad Bishopea2bc0c2016-07-25 09:05:39 -040026
27
28class 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 Bishop1eba37d2016-09-20 08:12:25 -040035 @staticmethod
Lei YU9e94fb62017-01-11 11:23:58 +080036 def retry(func, retries, interval):
Brad Bishop1eba37d2016-09-20 08:12:25 -040037 e = None
38 count = 0
39 while count < retries:
40 try:
41 return func()
Balaji B Rao84e331a2017-11-09 21:19:13 -060042 except dbus.exceptions.DBusException as e:
Matt Spinler3c18b9f2017-01-27 14:41:48 -060043 if e.get_dbus_name() not in \
44 ['org.freedesktop.DBus.Error.ObjectPathInUse',
45 'org.freedesktop.DBus.Error.LimitsExceeded']:
Brad Bishop1eba37d2016-09-20 08:12:25 -040046 raise
Brad Bishopea2bc0c2016-07-25 09:05:39 -040047
Brad Bishop1eba37d2016-09-20 08:12:25 -040048 count += 1
Lei YU9e94fb62017-01-11 11:23:58 +080049 if interval > 0:
50 from time import sleep
51 sleep(interval)
Brad Bishop1eba37d2016-09-20 08:12:25 -040052 if e:
53 raise e
Brad Bishopea2bc0c2016-07-25 09:05:39 -040054
Cory Klokmana5513ab2017-01-24 17:27:16 -060055 def get_object(self, path, retries=5, interfaces=[], interval=0.2):
Brad Bishop1eba37d2016-09-20 08:12:25 -040056 return self.retry(
Brad Bishopbf464f42016-11-02 00:37:06 -040057 lambda: self.iface.GetObject(
58 path, interfaces, signature='sas'),
Lei YU9e94fb62017-01-11 11:23:58 +080059 retries, interval)
Brad Bishopea2bc0c2016-07-25 09:05:39 -040060
Adriana Kobylaka1f24222018-01-10 16:09:07 -060061 def get_subtree_paths(
62 self, path='/', depth=0, retries=5, interfaces=[], interval=0.2):
Brad Bishop1eba37d2016-09-20 08:12:25 -040063 return self.retry(
Brad Bishopbf464f42016-11-02 00:37:06 -040064 lambda: self.iface.GetSubTreePaths(
65 path, depth, interfaces, signature='sias'),
Lei YU9e94fb62017-01-11 11:23:58 +080066 retries, interval)
Brad Bishop1eba37d2016-09-20 08:12:25 -040067
Adriana Kobylaka1f24222018-01-10 16:09:07 -060068 def get_subtree(
69 self, path='/', depth=0, retries=5, interfaces=[], interval=0.2):
Brad Bishop1eba37d2016-09-20 08:12:25 -040070 return self.retry(
Brad Bishopbf464f42016-11-02 00:37:06 -040071 lambda: self.iface.GetSubTree(
72 path, depth, interfaces, signature='sias'),
Lei YU9e94fb62017-01-11 11:23:58 +080073 retries, interval)
Brad Bishop1eba37d2016-09-20 08:12:25 -040074
Cory Klokmana5513ab2017-01-24 17:27:16 -060075 def get_ancestors(self, path, retries=5, interfaces=[], interval=0.2):
Brad Bishop1eba37d2016-09-20 08:12:25 -040076 return self.retry(
Brad Bishopbf464f42016-11-02 00:37:06 -040077 lambda: self.iface.GetAncestors(
78 path, interfaces, signature='sas'),
Lei YU9e94fb62017-01-11 11:23:58 +080079 retries, interval)
Brad Bishopea2bc0c2016-07-25 09:05:39 -040080
81 @staticmethod
82 def __try_properties_interface(f, *a):
83 try:
84 return f(*a)
Balaji B Rao84e331a2017-11-09 21:19:13 -060085 except dbus.exceptions.DBusException as e:
Brad Bishopea2bc0c2016-07-25 09:05:39 -040086 if obmc.dbuslib.enums.DBUS_UNKNOWN_INTERFACE in \
Adriana Kobylaka8f97652017-12-11 13:32:10 -060087 e.get_dbus_name():
Brad Bishopea2bc0c2016-07-25 09:05:39 -040088 # 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 Bishop8f301732017-09-11 20:09:48 -0400109 if match and not match(i):
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400110 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 Bishop8f301732017-09-11 20:09:48 -0400118 match=lambda x: x != dbus.BUS_DAEMON_IFACE + '.ObjectManager',
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400119 mapper_data=None):
120 if mapper_data is None:
121 mapper_data = {path: self.get_object(path)}
122
123 obj = {}
124
Balaji B Rao84e331a2017-11-09 21:19:13 -0600125 for owner, interfaces in mapper_data[path].items():
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400126 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 Bishop8f301732017-09-11 20:09:48 -0400134 match=lambda x: x != dbus.BUS_DAEMON_IFACE + '.ObjectManager',
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400135 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 Rao84e331a2017-11-09 21:19:13 -0600143 for path, bus_data in mapper_data.items():
144 for owner, interfaces in bus_data.items():
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400145 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 Rao84e331a2017-11-09 21:19:13 -0600155 except dbus.exceptions.DBusException as e:
Brad Bishop5fd9c472016-07-25 09:09:00 -0400156 if e.get_dbus_name() != MAPPER_NOT_FOUND:
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400157 raise
158
Balaji B Rao84e331a2017-11-09 21:19:13 -0600159 for path, bus_data in ancestors.items():
160 for owner, interfaces in bus_data.items():
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400161 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 Rao84e331a2017-11-09 21:19:13 -0600166 for owner, path in managers.items():
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400167 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 Rao84e331a2017-11-09 21:19:13 -0600174 for path, interfaces in iface.GetManagedObjects().items():
175 if path not in iter(mapper_data.keys()):
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400176 continue
177 properties = {}
Balaji B Rao84e331a2017-11-09 21:19:13 -0600178 for iface, props in interfaces.items():
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400179 properties.update(props)
180 results.setdefault(path, {}).setdefault(owner, properties)
181
182 # make dbus calls for any remaining objects
Balaji B Rao84e331a2017-11-09 21:19:13 -0600183 for path, bus_data in mapper_data.items():
184 for owner, interfaces in bus_data.items():
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400185 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 Rao84e331a2017-11-09 21:19:13 -0600192 for path, owners in results.items():
193 for owner, properties in owners.items():
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400194 objs.setdefault(path, {}).update(properties)
195
196 return objs