blob: da4ae6390d5063c9300d501488993cfc322e600b [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',
Brad Bishop5ffc2a32015-11-25 08:46:01 -050051 signal_name = 'InterfacesAdded',
Brad Bishop732c6db2015-10-19 14:49:21 -040052 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 Bishop5ffc2a32015-11-25 08:46:01 -050067 name = self.bus.get_owned_name(self.bus_match, kw['sender'])
Brad Bishop65b7ceb2015-11-04 23:05:56 -050068 if self.discovery_pending() or \
Brad Bishop5ffc2a32015-11-25 08:46:01 -050069 not self.bus_match(name):
Brad Bishopcb7aa602015-11-03 10:40:17 -050070 return
Brad Bishop5ffc2a32015-11-25 08:46:01 -050071
Brad Bishop54d3ec92015-11-03 09:32:34 -050072 matches = [ x for x in iprops.iterkeys() if self.intf_match(x) ]
Brad Bishope07fb492016-02-04 11:43:28 -050073 self.add_interfaces(path, kw['sender'], matches)
Brad Bishop732c6db2015-10-19 14:49:21 -040074
75 def interfaces_removed_handler(self, path, interfaces, **kw):
Brad Bishop5ffc2a32015-11-25 08:46:01 -050076 name = self.bus.get_owned_name(self.bus_match, kw['sender'])
Brad Bishop65b7ceb2015-11-04 23:05:56 -050077 if self.discovery_pending() or \
Brad Bishop5ffc2a32015-11-25 08:46:01 -050078 not self.bus_match(name):
Brad Bishopcb7aa602015-11-03 10:40:17 -050079 return
Brad Bishop54d3ec92015-11-03 09:32:34 -050080 item = self.cache[path]
Brad Bishop5ffc2a32015-11-25 08:46:01 -050081 sender = kw['sender']
Brad Bishop732c6db2015-10-19 14:49:21 -040082 for x in interfaces:
Brad Bishop5ffc2a32015-11-25 08:46:01 -050083 if self.intf_match(x):
84 try:
85 item[sender].remove(x)
86 except ValueError:
87 pass
Brad Bishop54d3ec92015-11-03 09:32:34 -050088
89 # remove the owner if there aren't any interfaces left
Brad Bishop5ffc2a32015-11-25 08:46:01 -050090 if not item[sender]:
91 del item[sender]
Brad Bishop54d3ec92015-11-03 09:32:34 -050092
93 # update if interfaces remain
94 if item:
95 self.cache[path] = item
96 # mark for removal if no interfaces remain
Brad Bishop5ffc2a32015-11-25 08:46:01 -050097 elif self.cache.get_children(path):
98 self.cache.demote(path)
Brad Bishop54d3ec92015-11-03 09:32:34 -050099 # delete the entire path if everything is gone
100 else:
101 del self.cache[path]
Brad Bishop732c6db2015-10-19 14:49:21 -0400102
103 def process_new_owner(self, name):
104 # unique name
Brad Bishop6fb84612015-11-01 00:06:24 -0400105 return self.discover([ IntrospectionParser(name,
106 self.bus.dbus, self.tag_match, self.intf_match) ])
Brad Bishop732c6db2015-10-19 14:49:21 -0400107
108 def process_old_owner(self, name):
Brad Bishop54d3ec92015-11-03 09:32:34 -0500109 for x,y in self.cache.dataitems():
110 if name not in y:
111 continue
112 del y[name]
113 if y:
114 self.cache[x] = y
115 elif self.cache.get_children(x):
116 self.cache.demote(x)
117 else:
118 del self.cache[x]
Brad Bishop732c6db2015-10-19 14:49:21 -0400119
120 def bus_handler(self, service, old, new):
Brad Bishopcb7aa602015-11-03 10:40:17 -0500121 if self.discovery_pending() or \
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500122 not self.bus_match(service):
Brad Bishop732c6db2015-10-19 14:49:21 -0400123 return
124
125 if new:
126 self.process_new_owner(new)
127 if old:
128 self.process_old_owner(old)
129
Brad Bishop54d3ec92015-11-03 09:32:34 -0500130 def add_interfaces(self, path, owner, interfaces):
Brad Bishope07fb492016-02-04 11:43:28 -0500131 d = self.cache.setdefault(path, {})
132 d = d.setdefault(owner, [])
133 self.cache[path][owner] = list(set(d + interfaces))
Brad Bishop86398d22015-10-28 21:58:31 -0400134
Brad Bishop6fb84612015-11-01 00:06:24 -0400135 def add_items(self, owner, bus_items):
Brad Bishop86398d22015-10-28 21:58:31 -0400136 for x,y in bus_items.iteritems():
Brad Bishop54d3ec92015-11-03 09:32:34 -0500137 self.add_interfaces(x, owner, y['interfaces'])
Brad Bishop86398d22015-10-28 21:58:31 -0400138
Brad Bishop732c6db2015-10-19 14:49:21 -0400139 def discover(self, owners = None):
Brad Bishop732c6db2015-10-19 14:49:21 -0400140 if not owners:
Brad Bishop6fb84612015-11-01 00:06:24 -0400141 owners = [ IntrospectionParser(x, self.bus.dbus,
142 self.tag_match, self.intf_match) \
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500143 for x in self.bus.get_owner_names(self.bus_match) ]
Brad Bishop732c6db2015-10-19 14:49:21 -0400144 for o in owners:
Brad Bishop6fb84612015-11-01 00:06:24 -0400145 self.add_items(o.name, o.introspect())
Brad Bishop732c6db2015-10-19 14:49:21 -0400146
Brad Bishopcb7aa602015-11-03 10:40:17 -0500147 if self.discovery_pending():
Brad Bishop732c6db2015-10-19 14:49:21 -0400148 print "ObjectMapper discovery complete..."
Brad Bishopcb7aa602015-11-03 10:40:17 -0500149 self.service = dbus.service.BusName(
150 OpenBMCMapper.MAPPER_NAME, self.bus.dbus)
Brad Bishop732c6db2015-10-19 14:49:21 -0400151
Brad Bishop54d3ec92015-11-03 09:32:34 -0500152 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 's', 'a{sas}')
153 def GetObject(self, path):
Brad Bishop3640ecd2015-11-03 14:46:10 -0500154 o = self.cache.get(path)
155 if not o:
156 raise MapperNotFoundException(path)
157 return o
Brad Bishop732c6db2015-10-19 14:49:21 -0400158
Brad Bishop54d3ec92015-11-03 09:32:34 -0500159 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'as')
160 def GetSubTreePaths(self, path, depth):
Brad Bishop3640ecd2015-11-03 14:46:10 -0500161 try:
162 return self.cache.iterkeys(path, depth)
163 except KeyError:
164 raise MapperNotFoundException(path)
Brad Bishop732c6db2015-10-19 14:49:21 -0400165
Brad Bishop54d3ec92015-11-03 09:32:34 -0500166 @dbus.service.method(OpenBMCMapper.MAPPER_IFACE, 'si', 'a{sa{sas}}')
167 def GetSubTree(self, path, depth):
Brad Bishop3640ecd2015-11-03 14:46:10 -0500168 try:
169 return { x:y for x, y in self.cache.dataitems(path, depth) }
170 except KeyError:
171 raise MapperNotFoundException(path)
Brad Bishop732c6db2015-10-19 14:49:21 -0400172
Brad Bishop732c6db2015-10-19 14:49:21 -0400173class BusWrapper:
174 def __init__(self, bus):
175 self.dbus = bus
176
Brad Bishop5ffc2a32015-11-25 08:46:01 -0500177 def get_owned_name(self, match, bus):
178 for x in self.get_service_names(match):
179 if self.dbus.get_name_owner(x) == bus:
180 return x
181
Brad Bishop732c6db2015-10-19 14:49:21 -0400182 def get_service_names(self, match):
183 # these are well known names
184 return [ x for x in self.dbus.list_names() \
Brad Bishop65b7ceb2015-11-04 23:05:56 -0500185 if match(x) ]
Brad Bishop732c6db2015-10-19 14:49:21 -0400186
187 def get_owner_names(self, match):
188 # these are unique connection names
189 return list( set( [ self.dbus.get_name_owner(x) \
190 for x in self.get_service_names(match) ] ) )
191
Brad Bishop732c6db2015-10-19 14:49:21 -0400192if __name__ == '__main__':
193 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
194 bus = dbus.SystemBus()
Brad Bishop732c6db2015-10-19 14:49:21 -0400195 o = ObjectMapper(BusWrapper(bus), OpenBMCMapper.MAPPER_PATH)
196 loop = gobject.MainLoop()
197
198 loop.run()