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
17 class ConnectorType(ConnectorTypeBase):
18 def __init__(self, testbed_id, factory_id, name, help, max = -1, min = 0):
19 super(ConnectorType, self).__init__(testbed_id, factory_id, name, max, min)
24 # allowed_connections -- keys in the dictionary correspond to the
25 # connector_type_id for possible connections. The value indicates if
26 # the connection is allowed accros different testbed instances
27 self._allowed_connections = dict()
33 def add_allowed_connection(self, testbed_id, factory_id, name, can_cross):
34 type_id = self.make_connector_type_id(testbed_id, factory_id, name)
35 self._allowed_connections[type_id] = can_cross
37 def can_connect(self, connector_type_id, testbed_guid1, testbed_guid2):
38 for lookup_type_id in self._type_resolution_order(connector_type_id):
39 if lookup_type_id in self._allowed_connections:
40 can_cross = self._allowed_connections[lookup_type_id]
41 if can_cross or (testbed_guid1 == testbed_guid2):
46 class Connector(object):
47 """A Connector sepcifies the connection points in an Object"""
48 def __init__(self, box, connector_type):
49 super(Connector, self).__init__()
51 self._connector_type = connector_type
52 self._connections = list()
55 return "Connector(%s, %s)" % (self.box, self.connector_type)
62 def connector_type(self):
63 return self._connector_type
66 def connections(self):
67 return self._connections
70 """Return True if the connector has the maximum number of connections
72 return len(self.connections) == self.connector_type.max
74 def is_complete(self):
75 """Return True if the connector has the minimum number of connections
77 return len(self.connections) >= self.connector_type.min
79 def is_connected(self, connector):
80 return connector in self._connections
82 def connect(self, connector):
83 if not self.can_connect(connector) or not connector.can_connect(self):
84 raise RuntimeError("Could not connect. %s to %s" % (self, connector))
85 self._connections.append(connector)
86 connector._connections.append(self)
88 def disconnect(self, connector):
89 if connector not in self._connections or\
90 self not in connector._connections:
91 raise RuntimeError("Could not disconnect.")
92 self._connections.remove(connector)
93 connector._connections.remove(self)
95 def can_connect(self, connector):
96 # can't connect with self
97 if self.box.guid == connector.box.guid:
99 if self.is_full() or connector.is_full():
101 if self.is_connected(connector):
103 connector_type_id = connector.connector_type.connector_type_id
104 testbed_guid1 = self.box.testbed_guid
105 testbed_guid2 = connector.box.testbed_guid
106 return self.connector_type.can_connect(connector_type_id,
107 testbed_guid1, testbed_guid2)
110 for connector in self.connections:
111 self.disconnect(connector)
112 self._box = self._connectors = None
114 class Trace(AttributesMap):
115 def __init__(self, trace_id, help, enabled = False):
116 super(Trace, self).__init__()
117 self._trace_id = trace_id
119 self._enabled = enabled
123 return self._trace_id
137 self._enabled = False
139 class Address(AttributesMap):
141 super(Address, self).__init__()
142 self.add_attribute(name = "Address",
143 help = "Address number",
144 type = Attribute.STRING,
145 flags = Attribute.HasNoDefaultValue,
146 validation_function = validation.is_ip_address)
147 self.add_attribute(name = "NetPrefix",
148 help = "Network prefix for the address",
149 type = Attribute.INTEGER,
152 flags = Attribute.HasNoDefaultValue,
153 validation_function = validation.is_integer)
154 self.add_attribute(name = "Broadcast",
155 help = "Broadcast address",
156 type = Attribute.STRING,
157 validation_function = validation.is_ip4_address)
159 class Route(AttributesMap):
161 super(Route, self).__init__()
162 self.add_attribute(name = "Destination",
163 help = "Network destintation",
164 type = Attribute.STRING,
165 validation_function = validation.is_ref_address)
166 self.add_attribute(name = "NetPrefix",
167 help = "Network destination prefix",
168 type = Attribute.INTEGER,
171 flags = Attribute.HasNoDefaultValue,
172 validation_function = validation.is_integer)
173 self.add_attribute(name = "NextHop",
174 help = "Address for the next hop",
175 type = Attribute.STRING,
176 flags = Attribute.HasNoDefaultValue,
177 validation_function = validation.is_ref_address)
178 self.add_attribute(name = "Metric",
179 help = "Routing metric",
180 type = Attribute.INTEGER,
182 flags = Attribute.HasNoDefaultValue,
183 validation_function = validation.is_integer)
185 class Box(AttributesMap):
186 def __init__(self, guid, factory, testbed_guid, container = None):
187 super(Box, self).__init__()
188 # guid -- global unique identifier
190 # factory_id -- factory identifier or name
191 self._factory_id = factory.factory_id
192 # testbed_guid -- parent testbed guid
193 self._testbed_guid = testbed_guid
194 # container -- boxes can be nested inside other 'container' boxes
195 self._container = container
196 # traces -- list of available traces for the box
197 self._traces = dict()
198 # tags -- list of tags for the box
200 # connectors -- list of available connectors for the box
201 self._connectors = dict()
202 # factory_attributes -- factory attributes for box construction
203 self._factory_attributes = dict()
204 # graphical_info -- GUI position information
205 self.graphical_info = GraphicalInfo()
207 for connector_type in factory.connector_types:
208 connector = Connector(self, connector_type)
209 self._connectors[connector_type.name] = connector
210 for trace in factory.traces:
211 tr = Trace(trace.trace_id, trace.help, trace.enabled)
212 self._traces[trace.trace_id] = tr
213 for tag_id in factory.tags:
214 self._tags.append(tag_id)
215 for attr in factory.box_attributes.attributes:
216 self.add_attribute(attr.name, attr.help, attr.type, attr.value,
217 attr.range, attr.allowed, attr.flags,
218 attr.validation_function, attr.category)
219 for attr in factory.attributes:
221 self._factory_attributes[attr.name] = attr.value
224 return "Box(%s, %s, %s)" % (self.guid, self.factory_id, self.testbed_guid)
231 def factory_id(self):
232 return self._factory_id
235 def testbed_guid(self):
236 return self._testbed_guid
240 return self._container
243 def connectors(self):
244 return self._connectors.values()
248 return self._traces.values()
251 def trace_names(self):
252 return self._traces.keys()
255 def factory_attributes(self):
256 return self._factory_attributes
262 def trace_help(self, trace_id):
263 return self._traces[trace_id].help
265 def enable_trace(self, trace_id):
266 self._traces[trace_id].enable()
268 def disable_trace(self, trace_id):
269 self._traces[trace_id].disable()
271 def is_trace_enabled(self, trace_id):
272 return self._traces[trace_id].enabled
274 def connector(self, name):
275 return self._connectors[name]
278 super(Box, self).destroy()
279 for c in self.connectors:
281 for t in self.traces:
283 self._connectors = self._traces = self._factory_attributes = None
285 class AddressableMixin(object):
286 def __init__(self, guid, factory, testbed_guid, container = None):
287 super(AddressableMixin, self).__init__(guid, factory, testbed_guid,
289 self._max_addresses = 1 # TODO: How to make this configurable!
290 self._addresses = list()
294 return self._addresses
297 def max_addresses(self):
298 return self._max_addresses
300 class UserAddressableMixin(AddressableMixin):
301 def __init__(self, guid, factory, testbed_guid, container = None):
302 super(UserAddressableMixin, self).__init__(guid, factory, testbed_guid,
305 def add_address(self):
306 if len(self._addresses) == self.max_addresses:
307 raise RuntimeError("Maximun number of addresses for this box reached.")
309 self._addresses.append(address)
312 def delete_address(self, address):
313 self._addresses.remove(address)
317 super(UserAddressableMixin, self).destroy()
318 for address in list(self.addresses):
319 self.delete_address(address)
320 self._addresses = None
322 class RoutableMixin(object):
323 def __init__(self, guid, factory, testbed_guid, container = None):
324 super(RoutableMixin, self).__init__(guid, factory, testbed_guid,
326 self._routes = list()
332 class UserRoutableMixin(RoutableMixin):
333 def __init__(self, guid, factory, testbed_guid, container = None):
334 super(UserRoutableMixin, self).__init__(guid, factory, testbed_guid,
339 self._routes.append(route)
342 def delete_route(self, route):
343 self._routes.remove(route)
347 super(UserRoutableMixin, self).destroy()
348 for route in list(self.routes):
349 self.delete_route(route)
352 def MixIn(MyClass, MixIn):
353 # Mixins are installed BEFORE "Box" because
354 # Box inherits from non-cooperative classes,
355 # so the MRO chain gets broken when it gets
359 MyClass.__bases__ = (MixIn,) + MyClass.__bases__
362 # Somehow it doesn't work automatically
363 for name in dir(MixIn):
364 prop = getattr(MixIn,name,None)
365 if isinstance(prop, property):
366 setattr(MyClass, name, prop)
369 MyClass.__name__ = MyClass.__name__.replace(
371 MixIn.__name__.replace('MixIn','')+'Box',
374 class Factory(AttributesMap):
375 _box_class_cache = {}
377 def __init__(self, factory_id,
378 allow_addresses = False, has_addresses = False,
379 allow_routes = False, has_routes = False,
380 Help = None, category = None):
381 super(Factory, self).__init__()
382 self._factory_id = factory_id
383 self._allow_addresses = bool(allow_addresses)
384 self._allow_routes = bool(allow_routes)
385 self._has_addresses = bool(allow_addresses) or self._allow_addresses
386 self._has_routes = bool(allow_routes) or self._allow_routes
388 self._category = category
389 self._connector_types = list()
390 self._traces = list()
392 self._box_attributes = AttributesMap()
394 if not self._has_addresses and not self._has_routes:
397 addresses = 'w' if self._allow_addresses else ('r' if self._has_addresses else '-')
398 routes = 'w' if self._allow_routes else ('r' if self._has_routes else '-')
399 key = addresses+routes
401 if key in self._box_class_cache:
402 self._factory = self._box_class_cache[key]
406 def __init__(self, guid, factory, testbed_guid, container = None):
407 super(_factory, self).__init__(guid, factory, testbed_guid, container)
409 # Add mixins, one by one
411 MixIn(_factory, UserAddressableMixin)
413 MixIn(_factory, AddressableMixin)
416 MixIn(_factory, UserRoutableMixin)
418 MixIn(_factory, RoutableMixin)
421 self._box_class_cache[key] = self._factory = _factory
424 def factory_id(self):
425 return self._factory_id
428 def allow_addresses(self):
429 return self._allow_addresses
432 def allow_routes(self):
433 return self._allow_routes
436 def has_addresses(self):
437 return self._has_addresses
440 def has_routes(self):
441 return self._has_routes
449 return self._category
452 def connector_types(self):
453 return self._connector_types
464 def box_attributes(self):
465 return self._box_attributes
467 def add_connector_type(self, connector_type):
468 self._connector_types.append(connector_type)
470 def add_trace(self, trace_id, help, enabled = False):
471 trace = Trace(trace_id, help, enabled)
472 self._traces.append(trace)
474 def add_tag(self, tag_id):
475 self._tags.append(tag_id)
477 def add_box_attribute(self, name, help, type, value = None, range = None,
478 allowed = None, flags = Attribute.NoFlags, validation_function = None,
480 self._box_attributes.add_attribute(name, help, type, value, range,
481 allowed, flags, validation_function, category)
483 def create(self, guid, testbed_description):
484 return self._factory(guid, self, testbed_description.guid)
487 super(Factory, self).destroy()
488 self._connector_types = None
490 class FactoriesProvider(object):
491 def __init__(self, testbed_id, testbed_version):
492 super(FactoriesProvider, self).__init__()
493 self._testbed_id = testbed_id
494 self._testbed_version = testbed_version
495 self._factories = dict()
497 metadata = Metadata(testbed_id, testbed_version)
498 for factory in metadata.build_design_factories():
499 self.add_factory(factory)
502 def testbed_id(self):
503 return self._testbed_id
506 def testbed_version(self):
507 return self._testbed_version
511 return self._factories.values()
513 def factory(self, factory_id):
514 return self._factories[factory_id]
516 def add_factory(self, factory):
517 self._factories[factory.factory_id] = factory
519 def remove_factory(self, factory_id):
520 del self._factories[factory_id]
522 class TestbedDescription(AttributesMap):
523 def __init__(self, guid_generator, provider, guid = None):
524 super(TestbedDescription, self).__init__()
525 self._guid_generator = guid_generator
526 self._guid = guid_generator.next(guid)
527 self._provider = provider
529 self.graphical_info = GraphicalInfo()
531 metadata = Metadata(provider.testbed_id, provider.testbed_version)
532 for attr in metadata.testbed_attributes().attributes:
533 self.add_attribute(attr.name, attr.help, attr.type, attr.value,
534 attr.range, attr.allowed, attr.flags,
535 attr.validation_function, attr.category)
543 return self._provider
547 return self._boxes.values()
550 return self._boxes[guid] if guid in self._boxes else None
552 def create(self, factory_id, guid = None):
553 guid = self._guid_generator.next(guid)
554 factory = self._provider.factory(factory_id)
555 box = factory.create(guid, self)
556 self._boxes[guid] = box
559 def delete(self, guid):
560 box = self._boxes[guid]
561 del self._boxes[guid]
565 for guid, box in self._boxes.iteritems():
569 class ExperimentDescription(object):
571 self._guid_generator = GuidGenerator()
572 self._testbed_descriptions = dict()
575 def testbed_descriptions(self):
576 return self._testbed_descriptions.values()
579 parser = XmlExperimentParser()
580 return parser.to_xml(self)
582 def from_xml(self, xml):
583 parser = XmlExperimentParser()
584 parser.from_xml(self, xml)
586 def testbed_description(self, guid):
587 return self._testbed_descriptions[guid] \
588 if guid in self._testbed_descriptions else None
591 for testbed_description in self._testbed_descriptions.values():
592 box = testbed_description.box(guid)
596 def get_element(self, guid):
597 if guid in self._testbed_descriptions:
598 return self._testbed_descriptions[guid]
599 for testbed_description in self._testbed_descriptions.values():
600 box = testbed_description.box(guid)
604 def add_testbed_description(self, provider, guid = None):
605 testbed_description = TestbedDescription(self._guid_generator,
607 guid = testbed_description.guid
608 self._testbed_descriptions[guid] = testbed_description
609 return testbed_description
611 def remove_testbed_description(self, guid):
612 testbed_description = self._testbed_descriptions[guid]
613 del self._testbed_descriptions[guid]
614 testbed_description.destroy()
617 for testbed_description in self.testbed_descriptions:
618 testbed_description.destroy()