aa8b8af2adeed98174bc17d067bd3c9cad859388
[nepi.git] / src / nepi / core / execute.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from nepi.core.attributes import AttributesMap
5 import sys
6
7 class ConnectorType(object):
8     def __init__(self, testbed_id, factory_id, name, max = -1, min = 0):
9         super(ConnectorType, self).__init__()
10         if max == -1:
11             max = sys.maxint
12         elif max <= 0:
13                 raise RuntimeError(
14              "The maximum number of connections allowed need to be more than 0")
15         if min < 0:
16             raise RuntimeError(
17              "The minimum number of connections allowed needs to be at least 0")
18         # connector_type_id -- univoquely identifies a connector type 
19         # across testbeds
20         self._connector_type_id = (testbed_id.lower(), factory_id.lower(), 
21                 name.lower())
22         # name -- display name for the connector type
23         self._name = name
24         # max -- maximum amount of connections that this type support, 
25         # -1 for no limit
26         self._max = max
27         # min -- minimum amount of connections required by this type of connector
28         self._min = min
29         # from_connections -- connections where the other connector is the "From"
30         # to_connections -- connections where the other connector is the "To"
31         # keys in the dictionary correspond to the 
32         # connector_type_id for possible connections. The value is a tuple:
33         # (can_cross, connect)
34         # can_cross: indicates if the connection is allowed accros different
35         #    testbed instances
36         # code: is the connection function to be invoked when the elements
37         #    are connected
38         self._from_connections = dict()
39         self._to_connections = dict()
40
41     @property
42     def connector_type_id(self):
43         return self._connector_type_id
44
45     @property
46     def name(self):
47         return self._name
48
49     @property
50     def max(self):
51         return self._max
52
53     @property
54     def min(self):
55         return self._min
56
57     def add_from_connection(self, testbed_id, factory_id, name, can_cross, code):
58         self._from_connections[(testbed_id.lower(), factory_id.lower(),
59             name.lower())] = (can_cross, code)
60
61     def add_to_connection(self, testbed_id, factory_id, name, can_cross, code):
62         self._to_connections[(testbed_id.lower(), factory_id.lower(), 
63             name.lower())] = (can_cross, code)
64
65     def can_connect(self, testbed_id, factory_id, name, count, 
66             must_cross = False):
67         connector_type_id = (testbed_id.lower(), factory_id.lower(),
68             name.lower())
69         if connector_type_id in self._from_connections:
70             (can_cross, code) = self._from_connections[connector_type_id]
71         elif connector_type_id in self._to_connections:
72             (can_cross, code) = self._to_connections[connector_type_id]
73         else:
74             return False
75         return not must_cross or can_cross
76
77     def code_to_connect(self, testbed_id, factory_id, name):
78         connector_type_id = (testbed_id.lower(), factory_id.lower(), 
79             name.lower())        
80         if not connector_type_id in self._to_connections.keys():
81             return False
82         (can_cross, code) = self._to_connections[connector_type_id]
83         return code
84
85 # TODO: create_function, start_function, stop_function, status_function 
86 # need a definition!
87 class Factory(AttributesMap):
88     def __init__(self, factory_id, create_function, start_function, 
89             stop_function, status_function, allow_addresses = False, 
90             allow_routes = False):
91         super(Factory, self).__init__()
92         self._factory_id = factory_id
93         self._allow_addresses = (allow_addresses == True)
94         self._allow_routes = (allow_routes == True)
95         self._create_function = create_function
96         self._start_function = start_function
97         self._stop_function = stop_function
98         self._status_function = status_function
99         self._connector_types = dict()
100         self._traces = list()
101
102     @property
103     def factory_id(self):
104         return self._factory_id
105
106     @property
107     def allow_addresses(self):
108         return self._allow_addresses
109
110     @property
111     def allow_routes(self):
112         return self._allow_routes
113
114     @property
115     def create_function(self):
116         return self._create_function
117
118     @property
119     def start_function(self):
120         return self._start_function
121
122     @property
123     def stop_function(self):
124         return self._stop_function
125
126     @property
127     def status_function(self):
128         return self._status_function
129
130     @property
131     def traces(self):
132         return self._traces
133
134     def connector_type(self, name):
135         return self._connector_types[name]
136
137     def add_connector_type(self, connector_type):
138         self._connector_types[connector_type.name] = connector_type
139
140     def add_trace(self, trace_id):
141         self._traces.append(trace_id)
142
143 class TestbedConfiguration(AttributesMap):
144     pass
145
146 class TestbedInstance(object):
147     def __init__(self, testbed_version, configuration):
148         pass
149
150     def create(self, guid, factory_id):
151         """Instructs creation of element """
152         raise NotImplementedError
153
154     def create_set(self, guid, name, value):
155         """Instructs setting an attribute on an element"""
156         raise NotImplementedError
157
158     def do_create(self):
159         """After do_create all instructed elements are created and 
160         attributes setted"""
161         raise NotImplementedError
162
163     def connect(self, guid1, connector_type_name1, guid2, 
164             connector_type_name2): 
165         raise NotImplementedError
166
167     def cross_connect(self, guid, connector_type_name, cross_guid, 
168             cross_testbed_id, cross_factory_id, cross_connector_type_name):
169         raise NotImplementedError
170
171     def do_connect(self):
172         raise NotImplementedError
173
174     def add_trace(self, guid, trace_id):
175         raise NotImplementedError
176
177     def add_adddress(self, guid, family, address, netprefix, broadcast): 
178         raise NotImplementedError
179
180     def add_route(self, guid, family, destination, netprefix, nexthop, 
181             interface):
182         raise NotImplementedError
183
184     def do_configure(self):
185         raise NotImplementedError
186
187     def do_cross_connect(self):
188         raise NotImplementedError
189
190     def set(self, time, guid, name, value):
191         raise NotImplementedError
192
193     def get(self, time, guid, name):
194         raise NotImplementedError
195
196     def start(self, time):
197         raise NotImplementedError
198
199     def action(self, time, guid, action):
200         raise NotImplementedError
201
202     def stop(self, time):
203         raise NotImplementedError
204
205     def status(self, guid):
206         raise NotImplementedError
207
208     def trace(self, guid, trace_id):
209         raise NotImplementedError
210
211     def shutdown(self):
212         raise NotImplementedError
213