2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID
5 from nepi.core import testbed_impl
8 class TestbedController(testbed_impl.TestbedController):
9 def __init__(self, testbed_version):
10 super(TestbedController, self).__init__(TESTBED_ID, testbed_version)
11 self._home_directory = None
15 import node, interfaces, application
17 self._interfaces = interfaces
18 self._app = application
21 def home_directory(self):
22 return self._home_directory
26 if not hasattr(self, '_plapi'):
30 self._plapi = plcapi.PLCAPI(
31 username = self.authUser,
32 password = self.authString)
34 # anonymous access - may not be enough for much
35 self._plapi = plcapi.PLCAPI()
40 if not hasattr(self, '_slice_id'):
41 slices = self.plapi.GetSlices(self.slicename, fields=('slice_id',))
43 self._slice_id = slices[0]['slice_id']
45 # If it wasn't found, don't remember this failure, keep trying
50 self._home_directory = self._attributes.\
51 get_attribute_value("homeDirectory")
52 self.slicename = self._attributes.\
53 get_attribute_value("slice")
54 self.authUser = self._attributes.\
55 get_attribute_value("authUser")
56 self.authString = self._attributes.\
57 get_attribute_value("authPass")
58 self.sliceSSHKey = self._attributes.\
59 get_attribute_value("sliceSSHKey")
62 # Create node elements per XML data
63 super(TestbedController, self).do_create()
65 # Perform resource discovery if we don't have
66 # specific resources assigned yet
67 self.do_resource_discovery()
69 # Create PlanetLab slivers
70 self.do_provisioning()
72 # Wait for all nodes to be ready
75 def do_resource_discovery(self):
79 def do_provisioning(self):
87 def set(self, time, guid, name, value):
88 super(TestbedController, self).set(time, guid, name, value)
89 # TODO: take on account schedule time for the task
90 element = self._elements[guid]
92 setattr(element, name, value)
94 def get(self, time, guid, name):
95 # TODO: take on account schedule time for the task
96 element = self._elements.get(guid)
99 if hasattr(element, name):
101 return getattr(element, name)
103 # Try design-time attributes
104 return self.box_get(time, guid, name)
105 except KeyError, AttributeError:
108 def get_route(self, guid, index, attribute):
109 # TODO: fetch real data from planetlab
111 return self.box_get_route(guid, int(index), attribute)
112 except KeyError, AttributeError:
115 def get_address(self, guid, index, attribute='Address'):
117 iface = self._elements.get(guid)
118 if iface and index == 0:
119 if attribute == 'Address':
121 elif attribute == 'NetPrefix':
122 return iface.netprefix
123 elif attribute == 'Broadcast':
124 return iface.broadcast
126 # if all else fails, query box
128 return self.box_get_address(guid, int(index), attribute)
129 except KeyError, AttributeError:
133 def action(self, time, guid, action):
134 raise NotImplementedError
137 for trace in self._traces.values():
139 for element in self._elements.values():
143 def trace(self, guid, trace_id, attribute='value'):
144 app = self._elements[guid]
146 if attribute == 'value':
147 path = app.sync_trace(self.home_directory, trace_id)
154 elif attribute == 'path':
155 content = app.remote_trace_path(trace_id)
160 def follow_trace(self, trace_id, trace):
161 self._traces[trace_id] = trace
163 def _make_node(self, parameters):
164 node = self._node.Node(self.plapi)
166 # Note: there is 1-to-1 correspondence between attribute names
167 # If that changes, this has to change as well
168 for attr,val in parameters.iteritems():
169 setattr(node, attr, val)
173 def _make_node_iface(self, parameters):
174 iface = self._interfaces.NodeIface(self.plapi)
176 # Note: there is 1-to-1 correspondence between attribute names
177 # If that changes, this has to change as well
178 for attr,val in parameters.iteritems():
179 setattr(iface, attr, val)
183 def _make_tun_iface(self, parameters):
184 iface = self._interfaces.TunIface(self.plapi)
186 # Note: there is 1-to-1 correspondence between attribute names
187 # If that changes, this has to change as well
188 for attr,val in parameters.iteritems():
189 setattr(iface, attr, val)
193 def _make_internet(self, parameters):
194 return self._interfaces.Internet(self.plapi)
196 def _make_application(self, parameters):
197 app = self._app.Application(self.plapi)
199 # Note: there is 1-to-1 correspondence between attribute names
200 # If that changes, this has to change as well
201 for attr,val in parameters.iteritems():
202 setattr(app, attr, val)