2 # -*- coding: utf-8 -*-
10 class ConnectorType(object):
11 def __init__(self, testbed_id, factory_id, name, help, max = -1, min = 0):
12 super(ConnectorType, self).__init__()
17 raise RuntimeError, "The maximum number of connections allowed need to be more than 0"
19 raise RuntimeError, "The minimum number of connections allowed needs to be at least 0"
20 # max -- maximum amount of connections that this type support,
23 # min -- minimum amount of connections required by this type of connector
26 # connector_type_id -- univoquely identifies a connector type
28 self._connector_type_id = self.make_connector_type_id(
29 testbed_id, factory_id, name)
31 # name -- display name for the connector type
37 # from_connections -- connections where the other connector is the "From"
38 # to_connections -- connections where the other connector is the "To"
39 # keys in the dictionary correspond to the
40 # connector_type_id for possible connections. The value is a tuple:
41 # (can_cross, connect)
42 # can_cross: indicates if the connection is allowed accros different
44 # code: is the connection function to be invoked when the elements
46 self._from_connections = dict()
47 self._to_connections = dict()
50 return "ConnectorType%r" % (self._connector_type_id,)
53 def connector_type_id(self):
54 return self._connector_type_id
73 def make_connector_type_id(testbed_id, factory_id, name):
74 testbed_id = testbed_id.lower() if testbed_id else None
75 factory_id = factory_id.lower() if factory_id else None
76 name = name.lower() if name else None
77 return (testbed_id, factory_id, name)
80 def _type_resolution_order(connector_type_id):
81 testbed_id, factory_id, name = connector_type_id
83 # the key is always a candidate
84 yield connector_type_id
86 # Try wildcard combinations
87 if (testbed_id, None, name) != connector_type_id:
88 yield (testbed_id, None, name)
89 if (None, factory_id, name) != connector_type_id:
90 yield (None, factory_id, name)
91 if (None, None, name) != connector_type_id:
92 yield (None, None, name)
94 def add_from_connection(self, testbed_id, factory_id, name, can_cross,
95 init_code, compl_code):
96 type_id = self.make_connector_type_id(testbed_id, factory_id, name)
97 self._from_connections[type_id] = (can_cross, init_code, compl_code)
99 def add_to_connection(self, testbed_id, factory_id, name, can_cross,
100 init_code, compl_code):
101 type_id = self.make_connector_type_id(testbed_id, factory_id, name)
102 self._to_connections[type_id] = (can_cross, init_code, compl_code)
104 def connect_to_init_code(self, testbed_id, factory_id, name, must_cross):
105 return self._connect_to_code(testbed_id, factory_id, name, must_cross)[0]
107 def connect_to_compl_code(self, testbed_id, factory_id, name, must_cross):
108 return self._connect_to_code(testbed_id, factory_id, name, must_cross)[1]
110 def _connect_to_code(self, testbed_id, factory_id, name,
112 connector_type_id = self.make_connector_type_id(testbed_id, factory_id, name)
113 for lookup_type_id in self._type_resolution_order(connector_type_id):
114 if lookup_type_id in self._to_connections:
115 (can_cross, init_code, compl_code) = self._to_connections[lookup_type_id]
116 if must_cross == can_cross:
117 return (init_code, compl_code)
119 return (False, False)
121 def can_connect(self, testbed_id, factory_id, name, must_cross):
122 connector_type_id = self.make_connector_type_id(testbed_id, factory_id, name)
123 for lookup_type_id in self._type_resolution_order(connector_type_id):
124 if lookup_type_id in self._from_connections:
125 (can_cross, init_code, compl_code) = self._from_connections[lookup_type_id]
126 elif lookup_type_id in self._to_connections:
127 (can_cross, init_code, compl_code) = self._to_connections[lookup_type_id]
131 if must_cross == can_cross: