blob: 36bf09bf9e04d83049bc65f0908568191dd3ed12 [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
19import sys
20import dbus
21import dbus.service
22import dbus.mainloop.glib
23import gobject
Brad Bishop54d3ec92015-11-03 09:32:34 -050024from OpenBMCMapper import Path, IntrospectionParser, PathTree
Brad Bishop732c6db2015-10-19 14:49:21 -040025import OpenBMCMapper
26
Brad Bishop732c6db2015-10-19 14:49:21 -040027class ObjectMapper(dbus.service.Object):
28 def __init__(self, bus, path,
Brad Bishop6fb84612015-11-01 00:06:24 -040029 name_match = OpenBMCMapper.org_dot_openbmc_match,
30 intf_match = OpenBMCMapper.org_dot_openbmc_match):
Brad Bishop732c6db2015-10-19 14:49:21 -040031 super(ObjectMapper, self).__init__(bus.dbus, path)
Brad Bishop54d3ec92015-11-03 09:32:34 -050032 self.cache = PathTree()
Brad Bishop732c6db2015-10-19 14:49:21 -040033 self.bus = bus
34 self.name_match = name_match
35 self.intf_match = intf_match
Brad Bishopa8e752f2015-11-03 09:24:48 -050036 self.tag_match = OpenBMCMapper.ListMatch(['children', 'interface'])
Brad Bishopcb7aa602015-11-03 10:40:17 -050037 self.service = None
Brad Bishop732c6db2015-10-19 14:49:21 -040038
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 Bishop65b7ceb2015-11-04 23:05:56 -050052 def bus_match(self, name):
53 if name == OpenBMCMapper.MAPPER_NAME:
54 return False
55 return self.name_match(name)
56
Brad Bishopcb7aa602015-11-03 10:40:17 -050057 def discovery_pending(self):
58 return not bool(self.service)
59
Brad Bishop732c6db2015-10-19 14:49:21 -040060 def interfaces_added_handler(self, path, iprops, **kw):
Brad Bishop65b7ceb2015-11-04 23:05:56 -050061 if self.discovery_pending() or \
62 not self.bus_match(kw['sender']):
Brad Bishopcb7aa602015-11-03 10:40:17 -050063 return
Brad Bishop54d3ec92015-11-03 09:32:34 -050064 matches = [ x for x in iprops.iterkeys() if self.intf_match(x) ]
65 d = self.cache.setdefault(path, {})
66 d[path].setdefault(kw['sender'], []).extend(matches)
67 self.cache[path] = d
Brad Bishop732c6db2015-10-19 14:49:21 -040068
69 def interfaces_removed_handler(self, path, interfaces, **kw):
Brad Bishop65b7ceb2015-11-04 23:05:56 -050070 if self.discovery_pending() or \
71 not self.bus_match(kw['sender']):
Brad Bishopcb7aa602015-11-03 10:40:17 -050072 return
Brad Bishop54d3ec92015-11-03 09:32:34 -050073 item = self.cache[path]
74 name = kw['sender']
Brad Bishop732c6db2015-10-19 14:49:21 -040075 for x in interfaces:
Brad Bishop54d3ec92015-11-03 09:32:34 -050076 item[name].remove(x)
77
78 # remove the owner if there aren't any interfaces left
79 if not item[name]:
80 del item[name]
81
82 # update if interfaces remain
83 if item:
84 self.cache[path] = item
85 # mark for removal if no interfaces remain
86 elif self.cache.get_children(item):
87 self.cache.demote(item)
88 # delete the entire path if everything is gone
89 else:
90 del self.cache[path]
Brad Bishop732c6db2015-10-19 14:49:21 -040091
92 def process_new_owner(self, name):
93 # unique name
Brad Bishop6fb84612015-11-01 00:06:24 -040094 return self.discover([ IntrospectionParser(name,
95 self.bus.dbus, self.tag_match, self.intf_match) ])
Brad Bishop732c6db2015-10-19 14:49:21 -040096
97 def process_old_owner(self, name):
Brad Bishop54d3ec92015-11-03 09:32:34 -050098 for x,y in self.cache.dataitems():
99 if name not in y:
100 continue
101 del y[name]
102 if y:
103 self.cache[x] = y
104 elif self.cache.get_children(x):
105 self.cache.demote(x)
106 else:
107 del self.cache[x]
Brad Bishop732c6db2015-10-19 14:49:21 -0400108
109 def bus_handler(self, service, old, new):
Brad Bishopcb7aa602015-11-03 10:40:17 -0500110 if self.discovery_pending() or \
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500111 not self.bus_match(service):
Brad Bishop732c6db2015-10-19 14:49:21 -0400112 return
113
114 if new:
115 self.process_new_owner(new)
116 if old:
117 self.process_old_owner(old)
118
Brad Bishop54d3ec92015-11-03 09:32:34 -0500119 def add_interfaces(self, path, owner, interfaces):
120 d = self.cache.setdefault(path, { })
121 d.setdefault(owner, []).extend(interfaces)
122 self.cache[path] = d
Brad Bishop86398d22015-10-28 21:58:31 -0400123
Brad Bishop6fb84612015-11-01 00:06:24 -0400124 def add_items(self, owner, bus_items):
Brad Bishop86398d22015-10-28 21:58:31 -0400125 for x,y in bus_items.iteritems():
Brad Bishop54d3ec92015-11-03 09:32:34 -0500126 self.add_interfaces(x, owner, y['interfaces'])
Brad Bishop86398d22015-10-28 21:58:31 -0400127
Brad Bishop732c6db2015-10-19 14:49:21 -0400128 def discover(self, owners = None):
Brad Bishop732c6db2015-10-19 14:49:21 -0400129 if not owners:
Brad Bishop6fb84612015-11-01 00:06:24 -0400130 owners = [ IntrospectionParser(x, self.bus.dbus,
131 self.tag_match, self.intf_match) \
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500132 for x in self.bus.get_owner_names(self.bus_match) ]
Brad Bishop732c6db2015-10-19 14:49:21 -0400133 for o in owners:
Brad Bishop6fb84612015-11-01 00:06:24 -0400134 self.add_items(o.name, o.introspect())
Brad Bishop732c6db2015-10-19 14:49:21 -0400135
Brad Bishopcb7aa602015-11-03 10:40:17 -0500136 if self.discovery_pending():
Brad Bishop732c6db2015-10-19 14:49:21 -0400137 print "ObjectMapper discovery complete..."
Brad Bishopcb7aa602015-11-03 10:40:17 -0500138 self.service = dbus.service.BusName(
139 OpenBMCMapper.MAPPER_NAME, self.bus.dbus)
Brad Bishop732c6db2015-10-19 14:49:21 -0400140
Brad Bishop54d3ec92015-11-03 09:32:34 -0500141 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 's', 'a{sas}')
142 def GetObject(self, path):
Brad Bishop3640ecd2015-11-03 14:46:10 -0500143 o = self.cache.get(path)
144 if not o:
145 raise MapperNotFoundException(path)
146 return o
Brad Bishop732c6db2015-10-19 14:49:21 -0400147
Brad Bishop54d3ec92015-11-03 09:32:34 -0500148 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'as')
149 def GetSubTreePaths(self, path, depth):
Brad Bishop3640ecd2015-11-03 14:46:10 -0500150 try:
151 return self.cache.iterkeys(path, depth)
152 except KeyError:
153 raise MapperNotFoundException(path)
Brad Bishop732c6db2015-10-19 14:49:21 -0400154
Brad Bishop54d3ec92015-11-03 09:32:34 -0500155 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'a{sa{sas}}')
156 def GetSubTree(self, path, depth):
Brad Bishop3640ecd2015-11-03 14:46:10 -0500157 try:
158 return { x:y for x, y in self.cache.dataitems(path, depth) }
159 except KeyError:
160 raise MapperNotFoundException(path)
Brad Bishop732c6db2015-10-19 14:49:21 -0400161
Brad Bishop732c6db2015-10-19 14:49:21 -0400162class BusWrapper:
163 def __init__(self, bus):
164 self.dbus = bus
165
166 def get_service_names(self, match):
167 # these are well known names
168 return [ x for x in self.dbus.list_names() \
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500169 if match(x) ]
Brad Bishop732c6db2015-10-19 14:49:21 -0400170
171 def get_owner_names(self, match):
172 # these are unique connection names
173 return list( set( [ self.dbus.get_name_owner(x) \
174 for x in self.get_service_names(match) ] ) )
175
Brad Bishop732c6db2015-10-19 14:49:21 -0400176if __name__ == '__main__':
177 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
178 bus = dbus.SystemBus()
Brad Bishop732c6db2015-10-19 14:49:21 -0400179 o = ObjectMapper(BusWrapper(bus), OpenBMCMapper.MAPPER_PATH)
180 loop = gobject.MainLoop()
181
182 loop.run()