e333940c52222f74b9d0cef1ca55cb7ecfefc1dc
[nepi.git] / src / nepi / core / execute.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from nepi.core.attributes import Attribute, AttributesMap
5 from nepi.util.constants import STATUS_FINISHED
6 from nepi.util.parser._xml import XmlExperimentParser
7 from nepi.util import validation
8 import sys
9
10 class ConnectorType(object):
11     def __init__(self, testbed_id, factory_id, name, max = -1, min = 0):
12         super(ConnectorType, self).__init__()
13         if max == -1:
14             max = sys.maxint
15         elif max <= 0:
16                 raise RuntimeError(
17              "The maximum number of connections allowed need to be more than 0")
18         if min < 0:
19             raise RuntimeError(
20              "The minimum number of connections allowed needs to be at least 0")
21         # connector_type_id -- univoquely identifies a connector type 
22         # across testbeds
23         self._connector_type_id = (testbed_id.lower(), factory_id.lower(), 
24                 name.lower())
25         # name -- display name for the connector type
26         self._name = name
27         # max -- maximum amount of connections that this type support, 
28         # -1 for no limit
29         self._max = max
30         # min -- minimum amount of connections required by this type of connector
31         self._min = min
32         # from_connections -- connections where the other connector is the "From"
33         # to_connections -- connections where the other connector is the "To"
34         # keys in the dictionary correspond to the 
35         # connector_type_id for possible connections. The value is a tuple:
36         # (can_cross, connect)
37         # can_cross: indicates if the connection is allowed accros different
38         #    testbed instances
39         # code: is the connection function to be invoked when the elements
40         #    are connected
41         self._from_connections = dict()
42         self._to_connections = dict()
43
44     @property
45     def connector_type_id(self):
46         return self._connector_type_id
47
48     @property
49     def name(self):
50         return self._name
51
52     @property
53     def max(self):
54         return self._max
55
56     @property
57     def min(self):
58         return self._min
59
60     def add_from_connection(self, testbed_id, factory_id, name, can_cross, code):
61         self._from_connections[(testbed_id.lower(), factory_id.lower(),
62             name.lower())] = (can_cross, code)
63
64     def add_to_connection(self, testbed_id, factory_id, name, can_cross, code):
65         self._to_connections[(testbed_id.lower(), factory_id.lower(), 
66             name.lower())] = (can_cross, code)
67
68     def can_connect(self, testbed_id, factory_id, name, count, 
69             must_cross = False):
70         connector_type_id = (testbed_id.lower(), factory_id.lower(),
71             name.lower())
72         if connector_type_id in self._from_connections:
73             (can_cross, code) = self._from_connections[connector_type_id]
74         elif connector_type_id in self._to_connections:
75             (can_cross, code) = self._to_connections[connector_type_id]
76         else:
77             return False
78         return not must_cross or can_cross
79
80     def code_to_connect(self, testbed_id, factory_id, name):
81         connector_type_id = (testbed_id.lower(), factory_id.lower(), 
82             name.lower())        
83         if not connector_type_id in self._to_connections.keys():
84             return False
85         (can_cross, code) = self._to_connections[connector_type_id]
86         return code
87
88 # TODO: create_function, start_function, stop_function, status_function 
89 # need a definition!
90 class Factory(AttributesMap):
91     def __init__(self, factory_id, create_function, start_function, 
92             stop_function, status_function, allow_addresses = False, 
93             allow_routes = False):
94         super(Factory, self).__init__()
95         self._factory_id = factory_id
96         self._allow_addresses = (allow_addresses == True)
97         self._allow_routes = (allow_routes == True)
98         self._create_function = create_function
99         self._start_function = start_function
100         self._stop_function = stop_function
101         self._status_function = status_function
102         self._connector_types = dict()
103         self._traces = list()
104         self._box_attributes = AttributesMap()
105
106     @property
107     def factory_id(self):
108         return self._factory_id
109
110     @property
111     def allow_addresses(self):
112         return self._allow_addresses
113
114     @property
115     def allow_routes(self):
116         return self._allow_routes
117
118     @property
119     def box_attributes(self):
120         return self._box_attributes
121
122     @property
123     def create_function(self):
124         return self._create_function
125
126     @property
127     def start_function(self):
128         return self._start_function
129
130     @property
131     def stop_function(self):
132         return self._stop_function
133
134     @property
135     def status_function(self):
136         return self._status_function
137
138     @property
139     def traces(self):
140         return self._traces
141
142     def connector_type(self, name):
143         return self._connector_types[name]
144
145     def add_connector_type(self, connector_type):
146         self._connector_types[connector_type.name] = connector_type
147
148     def add_trace(self, trace_id):
149         self._traces.append(trace_id)
150
151     def add_box_attribute(self, name, help, type, value = None, range = None,
152         allowed = None, flags = Attribute.NoFlags, validation_function = None):
153         self._box_attributes.add_attribute(name, help, type, value, range, 
154                 allowed, flags, validation_function)
155
156 class TestbedInstance(object):
157     def __init__(self, testbed_id, testbed_version):
158         self._testbed_id = testbed_id
159         self._testbed_version = testbed_version
160
161     @property
162     def guids(self):
163         raise NotImplementedError
164
165     def configure(self, name, value):
166         """Set a configuartion attribute for the testbed instance"""
167         raise NotImplementedError
168
169     def create(self, guid, factory_id):
170         """Instructs creation of element """
171         raise NotImplementedError
172
173     def create_set(self, guid, name, value):
174         """Instructs setting an initial attribute on an element"""
175         raise NotImplementedError
176
177     def factory_set(self, guid, name, value):
178         """Instructs setting an attribute on a factory"""
179         raise NotImplementedError
180
181     def connect(self, guid1, connector_type_name1, guid2, 
182             connector_type_name2): 
183         raise NotImplementedError
184
185     def cross_connect(self, guid, connector_type_name, cross_guid, 
186             cross_testbed_id, cross_factory_id, cross_connector_type_name):
187         raise NotImplementedError
188
189     def add_trace(self, guid, trace_id):
190         raise NotImplementedError
191
192     def add_adddress(self, guid, family, address, netprefix, broadcast): 
193         raise NotImplementedError
194
195     def add_route(self, guid, destination, netprefix, nexthop):
196         raise NotImplementedError
197
198     def do_setup(self):
199         """After do_setup the testbed initial configuration is done"""
200         raise NotImplementedError
201
202     def do_create(self):
203         """After do_create all instructed elements are created and 
204         attributes setted"""
205         raise NotImplementedError
206
207     def do_connect(self):
208         """After do_connect all internal connections between testbed elements
209         are done"""
210         raise NotImplementedError
211
212     def do_configure(self):
213         """After do_configure elements are configured"""
214         raise NotImplementedError
215
216     def do_cross_connect(self):
217         """After do_cross_connect all external connections between different testbed 
218         elements are done"""
219         raise NotImplementedError
220
221     def start(self, time):
222         raise NotImplementedError
223
224     def stop(self, time):
225         raise NotImplementedError
226
227     def set(self, time, guid, name, value):
228         raise NotImplementedError
229
230     def get(self, time, guid, name):
231         raise NotImplementedError
232
233     def action(self, time, guid, action):
234         raise NotImplementedError
235
236     def status(self, guid):
237         raise NotImplementedError
238
239     def trace(self, guid, trace_id):
240         raise NotImplementedError
241
242     def shutdown(self):
243         raise NotImplementedError
244
245 class ExperimentController(object):
246     def __init__(self, experiment_xml):
247         self._experiment_xml = experiment_xml
248         self._testbeds = dict()
249         self._access_config = dict()
250
251     @property
252     def experiment_xml(self):
253         return self._experiment_xml
254
255     def testbed_instance(self, guid):
256         return self._testbeds[guid]
257
258     def set_testbed_access_config(self, guid, access_config):
259         self._access_config[guid] = access_config
260
261     def trace(self, testbed_guid, guid, trace_id):
262         return self._testbeds[testbed_guid].trace(guid, trace_id)
263
264     def start(self):
265         self._create_testbed_instances()
266         for instance in self._testbeds.values():
267             instance.do_setup()
268         for instance in self._testbeds.values():
269             instance.do_create()
270             instance.do_connect()
271             instance.do_configure()
272         for instances in self._testbeds.values():
273             instance.do_cross_connect()
274         for instances in self._testbeds.values():
275             instance.start()
276
277     def stop(self):
278        for instance in self._testbeds.values():
279            instance.stop()
280
281     def is_finished(self, guid):
282         for instance in self._testbeds.values():
283             for guid_ in instance.guids:
284                 if guid_ == guid:
285                     return instance.status(guid) == STATUS_FINISHED
286         raise RuntimeError("No element exists with guid %d" % guid)    
287
288     def shutdown(self):
289        for instance in self._testbeds.values():
290            instance.shutdown()
291
292     def _build_testbed_instance(self, testbed_id, testbed_version):
293         mod_name = "nepi.testbeds.%s" % (testbed_id.lower())
294         if not mod_name in sys.modules:
295             __import__(mod_name)
296         module = sys.modules[mod_name]
297         return module.TestbedInstance(testbed_version)
298
299     def _create_testbed_instances(self):
300         parser = XmlExperimentParser()
301         data = parser.from_xml_to_data(self._experiment_xml)
302         element_guids = list()
303         for guid in data.guids:
304             if data.is_testbed_data(guid):
305                 (testbed_id, testbed_version) = data.get_testbed_data(guid)
306                 instance = self._build_testbed_instance(testbed_id, 
307                         testbed_version)
308                 for (name, value) in data.get_attribute_data(guid):
309                     instance.configure(name, value)
310                 self._testbeds[guid] = instance
311             else:
312                 element_guids.append(guid)
313         self._program_testbed_instances(element_guids, data)
314
315     def _program_testbed_instances(self, element_guids, data):
316         for guid in element_guids:
317             (testbed_guid, factory_id) = data.get_box_data(guid)
318             instance = self._testbeds[testbed_guid]
319             instance.create(guid, factory_id)
320             for (name, value) in data.get_attribute_data(guid):
321                 instance.create_set(guid, name, value)
322
323         for guid in element_guids: 
324             (testbed_guid, factory_id) = data.get_box_data(guid)
325             instance = self._testbeds[testbed_guid]
326             for (connector_type_name, other_guid, other_connector_type_name) \
327                     in data.get_connection_data(guid):
328                 (testbed_guid, factory_id) = data.get_box_data(guid)
329                 (other_testbed_guid, other_factory_id) = data.get_box_data(
330                         other_guid)
331                 if testbed_guid == other_testbed_guid:
332                     instance.connect(guid, connector_type_name, other_guid, 
333                         other_connector_type_name)
334                 else:
335                     instance.cross_connect(guid, connector_type_name, other_guid, 
336                         other_testbed_id, other_factory_id, other_connector_type_name)
337             for trace_id in data.get_trace_data(guid):
338                 instance.add_trace(guid, trace_id)
339             for (autoconf, address, family, netprefix, broadcast) in \
340                     data.get_address_data(guid):
341                 if address != None:
342                     instance.add_adddress(guid, family, address, netprefix,
343                         broadcast)
344             for (family, destination, netprefix, nexthop) in \
345                     data.get_route_data(guid):
346                 instance.add_route(guid, destination, netprefix, nexthop)
347