2 # -*- coding: utf-8 -*-
8 from nepi.core.attributes import AttributesMap, Attribute
9 from nepi.core.connector import ConnectorTypeBase
10 from nepi.core.metadata import Metadata
11 from nepi.util import validation
12 from nepi.util.guid import GuidGenerator
13 from nepi.util.graphical_info import GraphicalInfo
14 from nepi.util.parser._xml import XmlExperimentParser
19 class ConnectorType(ConnectorTypeBase):
20 def __init__(self, testbed_id, factory_id, name, help, max = -1, min = 0):
21 super(ConnectorType, self).__init__(testbed_id, factory_id, name, max, min)
26 # allowed_connections -- keys in the dictionary correspond to the
27 # connector_type_id for possible connections. The value indicates if
28 # the connection is allowed accros different testbed instances
29 self._allowed_connections = dict()
35 def add_allowed_connection(self, testbed_id, factory_id, name, can_cross):
36 type_id = self.make_connector_type_id(testbed_id, factory_id, name)
37 self._allowed_connections[type_id] = can_cross
39 def can_connect(self, connector_type_id, testbed_guid1, testbed_guid2):
40 for lookup_type_id in self._type_resolution_order(connector_type_id):
41 if lookup_type_id in self._allowed_connections:
42 can_cross = self._allowed_connections[lookup_type_id]
43 if can_cross or (testbed_guid1 == testbed_guid2):
48 class Connector(object):
49 """A Connector sepcifies the connection points in an Object"""
50 def __init__(self, box, connector_type):
51 super(Connector, self).__init__()
53 self._connector_type = connector_type
54 self._connections = list()
57 return "Connector(%s, %s)" % (self.box, self.connector_type)
64 def connector_type(self):
65 return self._connector_type
68 def connections(self):
69 return self._connections
72 """Return True if the connector has the maximum number of connections
74 return len(self.connections) == self.connector_type.max
76 def is_complete(self):
77 """Return True if the connector has the minimum number of connections
79 return len(self.connections) >= self.connector_type.min
81 def is_connected(self, connector):
82 return connector in self._connections
84 def connect(self, connector):
85 if not self.can_connect(connector) or not connector.can_connect(self):
86 raise RuntimeError("Could not connect. %s to %s" % (self, connector))
87 self._connections.append(connector)
88 connector._connections.append(self)
90 def disconnect(self, connector):
91 if connector not in self._connections or\
92 self not in connector._connections:
93 raise RuntimeError("Could not disconnect.")
94 self._connections.remove(connector)
95 connector._connections.remove(self)
97 def can_connect(self, connector):
98 if self.is_full() or connector.is_full():
100 if self.is_connected(connector):
102 connector_type_id = connector.connector_type.connector_type_id
103 testbed_guid1 = self.box.testbed_guid
104 testbed_guid2 = connector.box.testbed_guid
105 return self.connector_type.can_connect(connector_type_id,
106 testbed_guid1, testbed_guid2)
109 for connector in self.connections:
110 self.disconnect(connector)
111 self._box = self._connectors = None
113 class Trace(AttributesMap):
114 def __init__(self, trace_id, help, enabled = False):
115 super(Trace, self).__init__()
116 self._trace_id = trace_id
118 self.enabled = enabled
122 return self._trace_id
128 class Address(AttributesMap):
130 super(Address, self).__init__()
131 self.add_attribute(name = "AutoConfigure",
132 help = "If set, this address will automatically be assigned",
133 type = Attribute.BOOL,
135 flags = Attribute.DesignOnly,
136 validation_function = validation.is_bool)
137 self.add_attribute(name = "Address",
138 help = "Address number",
139 type = Attribute.STRING,
140 flags = Attribute.HasNoDefaultValue,
141 validation_function = validation.is_ip_address)
142 self.add_attribute(name = "NetPrefix",
143 help = "Network prefix for the address",
144 type = Attribute.INTEGER,
147 flags = Attribute.HasNoDefaultValue,
148 validation_function = validation.is_integer)
149 self.add_attribute(name = "Broadcast",
150 help = "Broadcast address",
151 type = Attribute.STRING,
152 validation_function = validation.is_ip4_address)
154 class Route(AttributesMap):
156 super(Route, self).__init__()
157 self.add_attribute(name = "Destination",
158 help = "Network destintation",
159 type = Attribute.STRING,
160 validation_function = validation.is_ip_address)
161 self.add_attribute(name = "NetPrefix",
162 help = "Network destination prefix",
163 type = Attribute.INTEGER,
166 flags = Attribute.HasNoDefaultValue,
167 validation_function = validation.is_integer)
168 self.add_attribute(name = "NextHop",
169 help = "Address for the next hop",
170 type = Attribute.STRING,
171 flags = Attribute.HasNoDefaultValue,
172 validation_function = validation.is_ip_address)
174 class Box(AttributesMap):
175 def __init__(self, guid, factory, testbed_guid, container = None):
176 super(Box, self).__init__()
177 # guid -- global unique identifier
179 # factory_id -- factory identifier or name
180 self._factory_id = factory.factory_id
181 # testbed_guid -- parent testbed guid
182 self._testbed_guid = testbed_guid
183 # container -- boxes can be nested inside other 'container' boxes
184 self._container = container
185 # traces -- list of available traces for the box
186 self._traces = dict()
187 # connectors -- list of available connectors for the box
188 self._connectors = dict()
189 # factory_attributes -- factory attributes for box construction
190 self._factory_attributes = dict()
191 # graphical_info -- GUI position information
192 self.graphical_info = GraphicalInfo(str(self._guid))
194 for connector_type in factory.connector_types:
195 connector = Connector(self, connector_type)
196 self._connectors[connector_type.name] = connector
197 for trace in factory.traces:
198 tr = Trace(trace.trace_id, trace.help, trace.enabled)
199 self._traces[trace.trace_id] = tr
200 for attr in factory.box_attributes.attributes:
201 self.add_attribute(attr.name, attr.help, attr.type, attr.value,
202 attr.range, attr.allowed, attr.flags,
203 attr.validation_function, attr.category)
204 for attr in factory.attributes:
206 self._factory_attributes[attr.name] = attr.value
209 return "Box(%s, %s, %s)" % (self.guid, self.factory_id, self.testbed_guid)
216 def factory_id(self):
217 return self._factory_id
220 def testbed_guid(self):
221 return self._testbed_guid
225 return self._container
228 def connectors(self):
229 return self._connectors.values()
233 return self._traces.values()
236 def traces_name(self):
237 return self._traces.keys()
240 def factory_attributes(self):
241 return self._factory_attributes
251 def trace_help(self, trace_id):
252 return self._traces[trace_id].help
254 def enable_trace(self, trace_id):
255 self._traces[trace_id].enabled = True
257 def disable_trace(self, trace_id):
258 self._traces[trace_id].enabled = False
260 def connector(self, name):
261 return self._connectors[name]
264 super(Box, self).destroy()
265 for c in self.connectors:
267 for t in self.traces:
269 self._connectors = self._traces = self._factory_attributes = None
271 class AddressableMixin(object):
272 def __init__(self, guid, factory, testbed_guid, container = None):
273 super(AddressableMixin, self).__init__(guid, factory, testbed_guid,
275 self._max_addresses = 1 # TODO: How to make this configurable!
276 self._addresses = list()
280 return self._addresses
283 def max_addresses(self):
284 return self._max_addresses
286 class UserAddressableMixin(AddressableMixin):
287 def __init__(self, guid, factory, testbed_guid, container = None):
288 super(UserAddressableMixin, self).__init__(guid, factory, testbed_guid,
291 def add_address(self):
292 if len(self._addresses) == self.max_addresses:
293 raise RuntimeError("Maximun number of addresses for this box reached.")
295 self._addresses.append(address)
298 def delete_address(self, address):
299 self._addresses.remove(address)
303 super(UserAddressableMixin, self).destroy()
304 for address in list(self.addresses):
305 self.delete_address(address)
306 self._addresses = None
308 class RoutableMixin(object):
309 def __init__(self, guid, factory, testbed_guid, container = None):
310 super(RoutableMixin, self).__init__(guid, factory, testbed_guid,
312 self._routes = list()
318 class UserRoutableMixin(RoutableMixin):
319 def __init__(self, guid, factory, testbed_guid, container = None):
320 super(UserRoutableMixin, self).__init__(guid, factory, testbed_guid,
325 self._routes.append(route)
328 def delete_route(self, route):
329 self._routes.remove(route)
333 super(UserRoutableMixin, self).destroy()
334 for route in list(self.routes):
335 self.delete_route(route)
338 def MixIn(MyClass, MixIn):
339 # Mixins are installed BEFORE "Box" because
340 # Box inherits from non-cooperative classes,
341 # so the MRO chain gets broken when it gets
345 MyClass.__bases__ = (MixIn,) + MyClass.__bases__
348 # Somehow it doesn't work automatically
349 for name in dir(MixIn):
350 prop = getattr(MixIn,name,None)
351 if isinstance(prop, property):
352 setattr(MyClass, name, prop)
355 MyClass.__name__ = MyClass.__name__.replace(
357 MixIn.__name__.replace('MixIn','')+'Box',
360 class Factory(AttributesMap):
361 _box_class_cache = {}
363 def __init__(self, factory_id,
364 allow_addresses = False, has_addresses = False,
365 allow_routes = False, has_routes = False,
366 Help = None, category = None):
367 super(Factory, self).__init__()
368 self._factory_id = factory_id
369 self._allow_addresses = bool(allow_addresses)
370 self._allow_routes = bool(allow_routes)
371 self._has_addresses = bool(allow_addresses) or self._allow_addresses
372 self._has_routes = bool(allow_routes) or self._allow_routes
374 self._category = category
375 self._connector_types = list()
376 self._traces = list()
377 self._box_attributes = AttributesMap()
379 if not self._has_addresses and not self._has_routes:
382 addresses = 'w' if self._allow_addresses else ('r' if self._has_addresses else '-')
383 routes = 'w' if self._allow_routes else ('r' if self._has_routes else '-')
384 key = addresses+routes
386 if key in self._box_class_cache:
387 self._factory = self._box_class_cache[key]
391 def __init__(self, guid, factory, testbed_guid, container = None):
392 super(_factory, self).__init__(guid, factory, testbed_guid, container)
394 # Add mixins, one by one
396 MixIn(_factory, UserAddressableMixin)
398 MixIn(_factory, AddressableMixin)
401 MixIn(_factory, UserRoutableMixin)
403 MixIn(_factory, RoutableMixin)
406 self._box_class_cache[key] = self._factory = _factory
409 def factory_id(self):
410 return self._factory_id
413 def allow_addresses(self):
414 return self._allow_addresses
417 def allow_routes(self):
418 return self._allow_routes
421 def has_addresses(self):
422 return self._has_addresses
425 def has_routes(self):
426 return self._has_routes
434 return self._category
437 def connector_types(self):
438 return self._connector_types
445 def box_attributes(self):
446 return self._box_attributes
448 def add_connector_type(self, connector_type):
449 self._connector_types.append(connector_type)
451 def add_trace(self, trace_id, help, enabled = False):
452 trace = Trace(trace_id, help, enabled)
453 self._traces.append(trace)
455 def add_box_attribute(self, name, help, type, value = None, range = None,
456 allowed = None, flags = Attribute.NoFlags, validation_function = None,
458 self._box_attributes.add_attribute(name, help, type, value, range,
459 allowed, flags, validation_function, category)
461 def create(self, guid, testbed_description):
462 return self._factory(guid, self, testbed_description.guid)
465 super(Factory, self).destroy()
466 self._connector_types = None
468 class FactoriesProvider(object):
469 def __init__(self, testbed_id, testbed_version):
470 super(FactoriesProvider, self).__init__()
471 self._testbed_id = testbed_id
472 self._testbed_version = testbed_version
473 self._factories = dict()
475 metadata = Metadata(testbed_id, testbed_version)
476 for factory in metadata.build_design_factories():
477 self.add_factory(factory)
480 def testbed_id(self):
481 return self._testbed_id
484 def testbed_version(self):
485 return self._testbed_version
489 return self._factories.values()
491 def factory(self, factory_id):
492 return self._factories[factory_id]
494 def add_factory(self, factory):
495 self._factories[factory.factory_id] = factory
497 def remove_factory(self, factory_id):
498 del self._factories[factory_id]
500 class TestbedDescription(AttributesMap):
501 def __init__(self, guid_generator, provider):
502 super(TestbedDescription, self).__init__()
503 self._guid_generator = guid_generator
504 self._guid = guid_generator.next()
505 self._provider = provider
507 self.graphical_info = GraphicalInfo(str(self._guid))
509 metadata = Metadata(provider.testbed_id, provider.testbed_version)
510 for attr in metadata.testbed_attributes().attributes:
511 self.add_attribute(attr.name, attr.help, attr.type, attr.value,
512 attr.range, attr.allowed, attr.flags,
513 attr.validation_function, attr.category)
521 return self._provider
525 return self._boxes.values()
528 return self._boxes[guid] if guid in self._boxes else None
530 def create(self, factory_id):
531 guid = self._guid_generator.next()
532 factory = self._provider.factory(factory_id)
533 box = factory.create(guid, self)
534 self._boxes[guid] = box
537 def delete(self, guid):
538 box = self._boxes[guid]
539 del self._boxes[guid]
543 for guid, box in self._boxes.iteritems():
547 class ExperimentDescription(object):
548 def __init__(self, guid = 0):
549 self._guid_generator = GuidGenerator(guid)
550 self._testbed_descriptions = dict()
553 def testbed_descriptions(self):
554 return self._testbed_descriptions.values()
557 parser = XmlExperimentParser()
558 return parser.to_xml(self)
560 def from_xml(self, xml):
561 parser = XmlExperimentParser()
562 parser.from_xml(self, xml)
564 def testbed_description(self, guid):
565 return self._testbed_descriptions[guid] \
566 if guid in self._testbed_descriptions else None
569 for testbed_description in self._testbed_descriptions.values():
570 box = testbed_description.box(guid)
574 def add_testbed_description(self, provider):
575 testbed_description = TestbedDescription(self._guid_generator,
577 guid = testbed_description.guid
578 self._testbed_descriptions[guid] = testbed_description
579 return testbed_description
581 def remove_testbed_description(self, testbed_description):
582 guid = testbed_description.guid
583 del self._testbed_descriptions[guid]
586 for testbed_description in self.testbed_descriptions:
587 testbed_description.destroy()
589 # TODO: When the experiment xml is passed to the controller to execute it
590 # NetReferences in the xml need to be solved
592 #targets = re.findall(r"%target:(.*?)%", command)
593 #for target in targets:
595 # (family, address, port) = resolve_netref(target, AF_INET,
596 # self.server.experiment )
597 # command = command.replace("%%target:%s%%" % target, address.address)