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