blob: f77ba46629c12538ee428e820ba2cfe5bd62548d [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
19import obmc.utils.misc
20import obmc.utils.pathtree
21
Brad Bishop5fd9c472016-07-25 09:09:00 -040022
Brad Bishopea2bc0c2016-07-25 09:05:39 -040023MAPPER_NAME = 'org.openbmc.ObjectMapper'
24MAPPER_IFACE = MAPPER_NAME
25MAPPER_PATH = '/org/openbmc/ObjectMapper'
Brad Bishopfba8ea32016-09-20 15:45:18 -040026MAPPER_NOT_FOUND = 'org.freedesktop.DBus.Error.FileNotFound'
Brad Bishopea2bc0c2016-07-25 09:05:39 -040027
28
29class Mapper:
30 def __init__(self, bus):
31 self.bus = bus
32 obj = bus.get_object(MAPPER_NAME, MAPPER_PATH, introspect=False)
33 self.iface = dbus.Interface(
34 obj, dbus_interface=MAPPER_IFACE)
35
Brad Bishop1eba37d2016-09-20 08:12:25 -040036 @staticmethod
37 def retry(func, retries):
38 e = None
39 count = 0
40 while count < retries:
41 try:
42 return func()
43 except dbus.exceptions.DBusException, e:
44 if e.get_dbus_name() is not \
45 'org.freedesktop.DBus.Error.ObjectPathInUse':
46 raise
Brad Bishopea2bc0c2016-07-25 09:05:39 -040047
Brad Bishop1eba37d2016-09-20 08:12:25 -040048 count += 1
49 if e:
50 raise e
Brad Bishopea2bc0c2016-07-25 09:05:39 -040051
Brad Bishop1eba37d2016-09-20 08:12:25 -040052 def get_object(self, path, retries=5):
53 return self.retry(
54 lambda: self.iface.GetObject(path),
55 retries)
Brad Bishopea2bc0c2016-07-25 09:05:39 -040056
Brad Bishop1eba37d2016-09-20 08:12:25 -040057 def get_subtree_paths(self, path='/', depth=0, retries=5):
58 return self.retry(
59 lambda: self.iface.GetSubTreePaths(path, depth),
60 retries)
61
62 def get_subtree(self, path='/', depth=0, retries=5):
63 return self.retry(
64 lambda: self.iface.GetSubTree(path, depth),
65 retries)
66
67 def get_ancestors(self, path, retries=5):
68 return self.retry(
69 lambda: self.iface.GetAncestors(path),
70 retries)
Brad Bishopea2bc0c2016-07-25 09:05:39 -040071
72 @staticmethod
73 def __try_properties_interface(f, *a):
74 try:
75 return f(*a)
76 except dbus.exceptions.DBusException, e:
77 if obmc.dbuslib.enums.DBUS_UNKNOWN_INTERFACE in \
78 e.get_dbus_message():
79 # interface doesn't have any properties
80 return None
81 if obmc.dbuslib.enums.DBUS_UNKNOWN_METHOD == e.get_dbus_name():
82 # properties interface not implemented at all
83 return None
84 raise
85
86 @staticmethod
87 def __get_properties_on_iface(properties_iface, iface):
88 properties = Mapper.__try_properties_interface(
89 properties_iface.GetAll, iface)
90 if properties is None:
91 return {}
92 return properties
93
94 def __get_properties_on_bus(self, path, bus, interfaces, match):
95 properties = {}
96 obj = self.bus.get_object(bus, path, introspect=False)
97 properties_iface = dbus.Interface(
98 obj, dbus_interface=dbus.PROPERTIES_IFACE)
99 for i in interfaces:
100 if not match(i):
101 continue
102 properties.update(self.__get_properties_on_iface(
103 properties_iface, i))
104
105 return properties
106
107 def enumerate_object(
108 self, path,
109 match=obmc.utils.misc.org_dot_openbmc_match,
110 mapper_data=None):
111 if mapper_data is None:
112 mapper_data = {path: self.get_object(path)}
113
114 obj = {}
115
116 for owner, interfaces in mapper_data[path].iteritems():
117 obj.update(
118 self.__get_properties_on_bus(
119 path, owner, interfaces, match))
120
121 return obj
122
123 def enumerate_subtree(
124 self, path='/',
125 match=obmc.utils.misc.org_dot_openbmc_match,
126 mapper_data=None):
127 if mapper_data is None:
128 mapper_data = self.get_subtree(path)
129 managers = {}
130 owners = []
131
132 # look for objectmanager implementations as they result
133 # in fewer dbus calls
134 for path, bus_data in mapper_data.iteritems():
135 for owner, interfaces in bus_data.iteritems():
136 owners.append(owner)
137 if dbus.BUS_DAEMON_IFACE + '.ObjectManager' in interfaces:
138 managers[owner] = path
139
140 # also look in the parent objects
141 ancestors = self.get_ancestors(path)
142
143 # finally check the root for one too
144 try:
145 ancestors.update({path: self.get_object(path)})
146 except dbus.exceptions.DBusException, e:
Brad Bishop5fd9c472016-07-25 09:09:00 -0400147 if e.get_dbus_name() != MAPPER_NOT_FOUND:
Brad Bishopea2bc0c2016-07-25 09:05:39 -0400148 raise
149
150 for path, bus_data in ancestors.iteritems():
151 for owner, interfaces in bus_data.iteritems():
152 if dbus.BUS_DAEMON_IFACE + '.ObjectManager' in interfaces:
153 managers[owner] = path
154
155 # make all the manager gmo (get managed objects) calls
156 results = {}
157 for owner, path in managers.iteritems():
158 if owner not in owners:
159 continue
160 obj = self.bus.get_object(owner, path, introspect=False)
161 iface = dbus.Interface(
162 obj, dbus.BUS_DAEMON_IFACE + '.ObjectManager')
163
164 # flatten (remove interface names) gmo results
165 for path, interfaces in iface.GetManagedObjects().iteritems():
166 if path not in mapper_data.iterkeys():
167 continue
168 properties = {}
169 for iface, props in interfaces.iteritems():
170 properties.update(props)
171 results.setdefault(path, {}).setdefault(owner, properties)
172
173 # make dbus calls for any remaining objects
174 for path, bus_data in mapper_data.iteritems():
175 for owner, interfaces in bus_data.iteritems():
176 if results.setdefault(path, {}).setdefault(owner, {}):
177 continue
178 results[path][owner].update(
179 self.__get_properties_on_bus(
180 path, owner, interfaces, match))
181
182 objs = obmc.utils.pathtree.PathTree()
183 for path, owners in results.iteritems():
184 for owner, properties in owners.iteritems():
185 objs.setdefault(path, {}).update(properties)
186
187 return objs