blob: 011dc56e642f5a12661c22371e5ea1f976a184d0 [file] [log] [blame]
Brad Bishop732c6db2015-10-19 14:49:21 -04001#!/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
Brad Bishop732c6db2015-10-19 14:49:21 -040019import dbus
20import dbus.service
Brad Bishopae0c0af2015-11-09 18:41:28 -050021import dbus.exceptions
Brad Bishop732c6db2015-10-19 14:49:21 -040022import dbus.mainloop.glib
23import gobject
Brad Bishopae0c0af2015-11-09 18:41:28 -050024from OpenBMCMapper import IntrospectionParser, PathTree
Brad Bishop732c6db2015-10-19 14:49:21 -040025import OpenBMCMapper
26
Brad Bishopae0c0af2015-11-09 18:41:28 -050027class MapperNotFoundException(dbus.exceptions.DBusException):
28 _dbus_error_name = OpenBMCMapper.MAPPER_NOT_FOUND
29 def __init__(self, path):
30 super(MapperNotFoundException, self).__init__(
31 "path or object not found: %s" %(path))
32
Brad Bishop732c6db2015-10-19 14:49:21 -040033class ObjectMapper(dbus.service.Object):
34 def __init__(self, bus, path,
Brad Bishop6fb84612015-11-01 00:06:24 -040035 name_match = OpenBMCMapper.org_dot_openbmc_match,
36 intf_match = OpenBMCMapper.org_dot_openbmc_match):
Brad Bishop732c6db2015-10-19 14:49:21 -040037 super(ObjectMapper, self).__init__(bus.dbus, path)
Brad Bishop54d3ec92015-11-03 09:32:34 -050038 self.cache = PathTree()
Brad Bishop732c6db2015-10-19 14:49:21 -040039 self.bus = bus
40 self.name_match = name_match
41 self.intf_match = intf_match
Brad Bishopa8e752f2015-11-03 09:24:48 -050042 self.tag_match = OpenBMCMapper.ListMatch(['children', 'interface'])
Brad Bishopcb7aa602015-11-03 10:40:17 -050043 self.service = None
Brad Bishop732c6db2015-10-19 14:49:21 -040044
45 gobject.idle_add(self.discover)
46 self.bus.dbus.add_signal_receiver(self.bus_handler,
47 dbus_interface = dbus.BUS_DAEMON_IFACE,
48 signal_name = 'NameOwnerChanged')
49 self.bus.dbus.add_signal_receiver(self.interfaces_added_handler,
50 dbus_interface = dbus.BUS_DAEMON_IFACE + '.ObjectManager',
51 signal_name = 'InterfaceesAdded',
52 sender_keyword = 'sender')
53 self.bus.dbus.add_signal_receiver(self.interfaces_removed_handler,
54 dbus_interface = dbus.BUS_DAEMON_IFACE + '.ObjectManager',
55 signal_name = 'InterfacesRemoved',
56 sender_keyword = 'sender')
57
Brad Bishop65b7ceb2015-11-04 23:05:56 -050058 def bus_match(self, name):
59 if name == OpenBMCMapper.MAPPER_NAME:
60 return False
61 return self.name_match(name)
62
Brad Bishopcb7aa602015-11-03 10:40:17 -050063 def discovery_pending(self):
64 return not bool(self.service)
65
Brad Bishop732c6db2015-10-19 14:49:21 -040066 def interfaces_added_handler(self, path, iprops, **kw):
Brad Bishop65b7ceb2015-11-04 23:05:56 -050067 if self.discovery_pending() or \
68 not self.bus_match(kw['sender']):
Brad Bishopcb7aa602015-11-03 10:40:17 -050069 return
Brad Bishop54d3ec92015-11-03 09:32:34 -050070 matches = [ x for x in iprops.iterkeys() if self.intf_match(x) ]
71 d = self.cache.setdefault(path, {})
72 d[path].setdefault(kw['sender'], []).extend(matches)
73 self.cache[path] = d
Brad Bishop732c6db2015-10-19 14:49:21 -040074
75 def interfaces_removed_handler(self, path, interfaces, **kw):
Brad Bishop65b7ceb2015-11-04 23:05:56 -050076 if self.discovery_pending() or \
77 not self.bus_match(kw['sender']):
Brad Bishopcb7aa602015-11-03 10:40:17 -050078 return
Brad Bishop54d3ec92015-11-03 09:32:34 -050079 item = self.cache[path]
80 name = kw['sender']
Brad Bishop732c6db2015-10-19 14:49:21 -040081 for x in interfaces:
Brad Bishop54d3ec92015-11-03 09:32:34 -050082 item[name].remove(x)
83
84 # remove the owner if there aren't any interfaces left
85 if not item[name]:
86 del item[name]
87
88 # update if interfaces remain
89 if item:
90 self.cache[path] = item
91 # mark for removal if no interfaces remain
92 elif self.cache.get_children(item):
93 self.cache.demote(item)
94 # delete the entire path if everything is gone
95 else:
96 del self.cache[path]
Brad Bishop732c6db2015-10-19 14:49:21 -040097
98 def process_new_owner(self, name):
99 # unique name
Brad Bishop6fb84612015-11-01 00:06:24 -0400100 return self.discover([ IntrospectionParser(name,
101 self.bus.dbus, self.tag_match, self.intf_match) ])
Brad Bishop732c6db2015-10-19 14:49:21 -0400102
103 def process_old_owner(self, name):
Brad Bishop54d3ec92015-11-03 09:32:34 -0500104 for x,y in self.cache.dataitems():
105 if name not in y:
106 continue
107 del y[name]
108 if y:
109 self.cache[x] = y
110 elif self.cache.get_children(x):
111 self.cache.demote(x)
112 else:
113 del self.cache[x]
Brad Bishop732c6db2015-10-19 14:49:21 -0400114
115 def bus_handler(self, service, old, new):
Brad Bishopcb7aa602015-11-03 10:40:17 -0500116 if self.discovery_pending() or \
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500117 not self.bus_match(service):
Brad Bishop732c6db2015-10-19 14:49:21 -0400118 return
119
120 if new:
121 self.process_new_owner(new)
122 if old:
123 self.process_old_owner(old)
124
Brad Bishop54d3ec92015-11-03 09:32:34 -0500125 def add_interfaces(self, path, owner, interfaces):
126 d = self.cache.setdefault(path, { })
127 d.setdefault(owner, []).extend(interfaces)
128 self.cache[path] = d
Brad Bishop86398d22015-10-28 21:58:31 -0400129
Brad Bishop6fb84612015-11-01 00:06:24 -0400130 def add_items(self, owner, bus_items):
Brad Bishop86398d22015-10-28 21:58:31 -0400131 for x,y in bus_items.iteritems():
Brad Bishop54d3ec92015-11-03 09:32:34 -0500132 self.add_interfaces(x, owner, y['interfaces'])
Brad Bishop86398d22015-10-28 21:58:31 -0400133
Brad Bishop732c6db2015-10-19 14:49:21 -0400134 def discover(self, owners = None):
Brad Bishop732c6db2015-10-19 14:49:21 -0400135 if not owners:
Brad Bishop6fb84612015-11-01 00:06:24 -0400136 owners = [ IntrospectionParser(x, self.bus.dbus,
137 self.tag_match, self.intf_match) \
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500138 for x in self.bus.get_owner_names(self.bus_match) ]
Brad Bishop732c6db2015-10-19 14:49:21 -0400139 for o in owners:
Brad Bishop6fb84612015-11-01 00:06:24 -0400140 self.add_items(o.name, o.introspect())
Brad Bishop732c6db2015-10-19 14:49:21 -0400141
Brad Bishopcb7aa602015-11-03 10:40:17 -0500142 if self.discovery_pending():
Brad Bishop732c6db2015-10-19 14:49:21 -0400143 print "ObjectMapper discovery complete..."
Brad Bishopcb7aa602015-11-03 10:40:17 -0500144 self.service = dbus.service.BusName(
145 OpenBMCMapper.MAPPER_NAME, self.bus.dbus)
Brad Bishop732c6db2015-10-19 14:49:21 -0400146
Brad Bishop54d3ec92015-11-03 09:32:34 -0500147 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 's', 'a{sas}')
148 def GetObject(self, path):
Brad Bishop3640ecd2015-11-03 14:46:10 -0500149 o = self.cache.get(path)
150 if not o:
151 raise MapperNotFoundException(path)
152 return o
Brad Bishop732c6db2015-10-19 14:49:21 -0400153
Brad Bishop54d3ec92015-11-03 09:32:34 -0500154 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'as')
155 def GetSubTreePaths(self, path, depth):
Brad Bishop3640ecd2015-11-03 14:46:10 -0500156 try:
157 return self.cache.iterkeys(path, depth)
158 except KeyError:
159 raise MapperNotFoundException(path)
Brad Bishop732c6db2015-10-19 14:49:21 -0400160
Brad Bishop54d3ec92015-11-03 09:32:34 -0500161 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'a{sa{sas}}')
162 def GetSubTree(self, path, depth):
Brad Bishop3640ecd2015-11-03 14:46:10 -0500163 try:
164 return { x:y for x, y in self.cache.dataitems(path, depth) }
165 except KeyError:
166 raise MapperNotFoundException(path)
Brad Bishop732c6db2015-10-19 14:49:21 -0400167
Brad Bishop732c6db2015-10-19 14:49:21 -0400168class BusWrapper:
169 def __init__(self, bus):
170 self.dbus = bus
171
172 def get_service_names(self, match):
173 # these are well known names
174 return [ x for x in self.dbus.list_names() \
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500175 if match(x) ]
Brad Bishop732c6db2015-10-19 14:49:21 -0400176
177 def get_owner_names(self, match):
178 # these are unique connection names
179 return list( set( [ self.dbus.get_name_owner(x) \
180 for x in self.get_service_names(match) ] ) )
181
Brad Bishop732c6db2015-10-19 14:49:21 -0400182if __name__ == '__main__':
183 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
184 bus = dbus.SystemBus()
Brad Bishop732c6db2015-10-19 14:49:21 -0400185 o = ObjectMapper(BusWrapper(bus), OpenBMCMapper.MAPPER_PATH)
186 loop = gobject.MainLoop()
187
188 loop.run()