added mock testbed for unit testing.
[nepi.git] / src / nepi / util / parser / base.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import sys
5
6 class ExperimentData(object):
7     def __init__(self):
8         self.data = dict()
9
10     @property
11     def guids(self):
12         return self.data.keys()
13
14     def add_testbed_data(self, guid, testbed_id, testbed_version):
15         testbed_data = dict()
16         testbed_data["testbed_id"] = testbed_id
17         testbed_data["testbed_version"] = testbed_version
18         self.data[guid] = testbed_data
19
20     def add_box_data(self, guid, testbed_guid, factory_id):
21         box_data = dict()
22         box_data["testbed_guid"] = testbed_guid
23         box_data["factory_id"] = factory_id
24         self.data[guid] = box_data
25
26     def add_graphical_info_data(self, guid, x, y, width, height, label):
27         data = self.data[guid]
28         if not "graphical_info" in data:
29             data["graphical_info"] = dict()
30         graphical_info_data = data["graphical_info"]
31         graphical_info_data["x"] = x
32         graphical_info_data["y"] = y
33         graphical_info_data["width"] = width
34         graphical_info_data["height"] = height
35         graphical_info_data["label"] = label
36
37     def add_factory_attribute_data(self, guid, name, value):
38         data = self.data[guid]
39         if not "factory_attributes" in data:
40             data["factory_attributes"] = dict()
41         factory_attributes_data = data["factory_attributes"]
42         factory_attributes_data[name] = value
43
44     def add_attribute_data(self, guid, name, value):
45         data = self.data[guid]
46         if not "attributes" in data:
47             data["attributes"] = dict()
48         attributes_data = data["attributes"]
49         attributes_data[name] = value
50
51     def add_trace_data(self, guid, trace_name):
52         data = self.data[guid]
53         if not "traces" in data:
54             data["traces"] = list()
55         traces_data = data["traces"]
56         traces_data.append(trace_name)
57
58     def add_connection_data(self, guid, connector_type_name, other_guid,
59             other_connector_type_name):
60         data = self.data[guid]
61         if not "connections" in data:
62             data["connections"] = dict()
63         connections_data = data["connections"]
64         if not connector_type_name in connections_data:
65             connections_data[connector_type_name] = dict()
66         connection_data = connections_data[connector_type_name]
67         connection_data[other_guid] = other_connector_type_name
68
69     def add_address_data(self, guid, autoconf, address, family, netprefix, 
70             broadcast):
71         data = self.data[guid]
72         if not "addresses" in data:
73             data["addresses"] = list()
74         addresses_data = data["addresses"]
75         address_data = dict()
76         if autoconf:
77             address_data["AutoConfigure"] = autoconf
78         if address:
79             address_data["Address"] = address
80         address_data["Family"] = family
81         address_data["NetPrefix"] = netprefix
82         if broadcast:
83             address_data["Broadcast"] = broadcast
84         addresses_data.append(address_data)
85
86     def add_route_data(self, guid, family, destination, netprefix, nexthop): 
87         data = self.data[guid]
88         if not "routes" in data:
89             data["routes"] = list()
90         routes_data = data["routes"]
91         route_data = dict({
92             "Family": family, 
93             "Destination": destination,
94             "NetPrefix": netprefix, 
95             "NextHop": nexthop 
96             })
97         routes_data.append(route_data)
98
99     def is_testbed_data(self, guid):
100         return True if "testbed_id" in self.data[guid] else None
101
102     def get_testbed_data(self, guid):
103         testbed_data = self.data[guid]
104         return (testbed_data["testbed_id"], testbed_data["testbed_version"])
105
106     def get_box_data(self, guid):
107         box_data = self.data[guid]
108         return (box_data["testbed_guid"], box_data["factory_id"])
109
110     def get_graphical_info_data(self, guid):
111         data = self.data[guid]
112         if not "graphical_info" in data:
113             return (0, 0, 0, 0, "") 
114         graphical_info_data = data["graphical_info"]
115         return (graphical_info_data["x"],
116                 graphical_info_data["y"],
117                 graphical_info_data["width"],
118                 graphical_info_data["height"],
119                 graphical_info_data["label"])
120
121     def get_factory_attribute_data(self, guid):
122         data = self.data[guid]
123         if not "factory_attributes" in data:
124             return []
125         factory_attributes_data = data["factory_attributes"]
126         return [(name, value) for name, value \
127                 in factory_attributes_data.iteritems()]
128
129     def get_attribute_data(self, guid):
130         data = self.data[guid]
131         if not "attributes" in data:
132             return []
133         attributes_data = data["attributes"]
134         return [(name, value) for name, value \
135                 in attributes_data.iteritems()]
136
137     def get_trace_data(self, guid):
138         data = self.data[guid]
139         if not "traces" in data:
140             return []
141         return [trace_id for trace_id in data["traces"]]
142
143     def get_connection_data(self, guid):
144         data = self.data[guid]
145         if not "connections" in data:
146             return []
147         connections_data = data["connections"]
148         return [(connector_type_name, other_guid, other_connector_type_name) \
149                     for connector_type_name, connection_data \
150                         in connections_data.iteritems() \
151                             for other_guid, other_connector_type_name \
152                                 in connection_data.iteritems()]
153
154     def get_address_data(self, guid):
155         data = self.data[guid]
156         if not "addresses" in data:
157             return []
158         addresses_data = data["addresses"]
159         return [(data["AutoConfigure"] if "AutoConfigure" in data else None,
160                  data["Address"] if "Address" in data else None,
161                  data["Family"] if "Family" in data else None,
162                  data["NetPrefix"] if "NetPrefix" in data else None,
163                  data["Broadcast"] if "Broadcast" in data else None) \
164                  for data in addresses_data]
165
166     def get_route_data(self, guid):
167         data = self.data[guid]
168         if not "routes" in data:
169             return []
170         routes_data = data["routes"]
171         return [(data["Family"],
172                  data["Destination"],
173                  data["NetPrefix"],
174                  data["NextHop"]) \
175                          for data in routes_data]
176
177 class ExperimentParser(object):
178     def to_data(self, experiment_description):
179         data = ExperimentData()
180         for testbed_description in experiment_description.testbed_descriptions:
181             guid = testbed_description.guid
182             testbed_id = testbed_description.provider.testbed_id
183             testbed_version = testbed_description.provider.testbed_version
184             data.add_testbed_data(guid, testbed_id, testbed_version)
185             self.graphical_info_to_data(data, guid, 
186                     testbed_description.graphical_info)
187             self.attributes_to_data(data, guid, testbed_description.attributes)
188             for box in testbed_description.boxes:
189                 data.add_box_data(box.guid, guid, box.factory_id)
190                 self.graphical_info_to_data(data, box.guid, box.graphical_info)
191                 self.factory_attributes_to_data(data, box.guid, 
192                         box.factory_attributes)
193                 self.attributes_to_data(data, box.guid, box.attributes)
194                 self.traces_to_data(data, box.guid, box.traces)
195                 self.connections_to_data(data, box.guid, box.connectors)
196                 self.addresses_to_data(data, box.guid, box.addresses)
197                 self.routes_to_data(data, box.guid, box.routes)
198         return data
199
200     def graphical_info_to_data(self, data, guid, g_info):
201         data.add_graphical_info_data(guid, g_info.x, g_info.y, g_info.width, 
202                 g_info.height, g_info.label)
203
204     def factory_attributes_to_data(self, data, guid, factory_attributes):
205         for name, value in factory_attributes.iteritems():
206             data.add_factory_attribute_data(guid, name, value)
207
208     def attributes_to_data(self, data, guid, attributes):
209         for attribute in attributes:
210             if attribute.modified or attribute.has_no_default_value:
211                 data.add_attribute_data(guid, attribute.name, attribute.value)
212
213     def traces_to_data(self, data, guid, traces):
214         for trace in traces:
215             if trace.enabled:
216                 data.add_trace_data(guid, trace.trace_id)
217
218     def connections_to_data(self, data, guid, connectors):
219         for connector in connectors:
220             connector_type_name = connector.connector_type.name
221             for other_connector in connector.connections:
222                 other_guid = other_connector.box.guid
223                 other_connector_type_name = other_connector.connector_type.name
224                 data.add_connection_data(guid, connector_type_name, other_guid,
225                         other_connector_type_name)
226
227     def addresses_to_data(self, data, guid, addresses):
228         for addr in addresses:
229              autoconf = addr.get_attribute_value("AutoConfigure")
230              address = addr.get_attribute_value("Address")
231              netprefix = addr.get_attribute_value("NetPrefix")
232              family = addr.get_attribute_value("Family")
233              broadcast = addr.get_attribute_value("Broadcast") \
234                     if addr.has_attribute("Broadcast") and \
235                     addr.is_attribute_modified("Broadcast") else None
236              data.add_address_data(guid, autoconf, address, family, netprefix, 
237                     broadcast)
238
239     def routes_to_data(self, data, guid, routes):
240         for route in routes:
241              family = route.get_attribute_value("Family")
242              destination = route.get_attribute_value("Destination")
243              netprefix = route.get_attribute_value("NetPrefix")
244              nexthop = route.get_attribute_value("NextHop")
245              data.add_route_data(guid, family, destination, netprefix, nexthop)
246
247     def from_data(self, experiment_description, data):
248         box_guids = list()
249         for guid in data.guids:
250             if data.is_testbed_data(guid):
251                 self.testbed_from_data(experiment_description, guid, data)
252             else:
253                 self.box_from_data(experiment_description, guid, data)
254                 box_guids.append(guid)
255         self.connections_from_data(experiment_description, box_guids, data)
256
257     def testbed_from_data(self, experiment_description, guid, data):
258         from nepi.core.design import FactoriesProvider
259         (testbed_id, testbed_version) = data.get_testbed_data(guid)
260         provider = FactoriesProvider(testbed_id, testbed_version)
261         experiment_description.add_testbed_description(provider)
262         testbed_description = experiment_description.testbed_description(guid)
263         self.attributes_from_data(testbed_description, data)
264
265     def box_from_data(self, experiment_description, guid, data):
266         (testbed_guid, factory_id) = data.get_box_data(guid)
267         testbed_description = experiment_description.testbed_description(
268                 testbed_guid)
269         self.factory_attributes_from_data(testbed_description, factory_id,
270                 guid, data)
271         box = testbed_description.create(factory_id)
272         self.graphical_info_from_data(box, data)
273         self.attributes_from_data(box, data)
274         self.traces_from_data(box, data)
275         self.addresses_from_data(box, data)
276         self.routes_from_data(box, data)
277
278     def graphical_info_from_data(self, element, data):
279         (x, y, width, height, label) =  data.get_graphical_info_data(
280                 element.guid)
281         element.graphical_info.x = x
282         element.graphical_info.y = y
283         element.graphical_info.width = width
284         element.graphical_info.height = height
285         element.graphical_info.label = label
286
287     def factory_attributes_from_data(self, testbed_description, factory_id, 
288             guid, data):
289         factory = testbed_description.provider.factory(factory_id)
290         for (name, value) in data.get_factory_attribute_data(guid):
291             factory.set_attribute_value(name, value)
292
293     def attributes_from_data(self, element, data):
294         for name, value in data.get_attribute_data(element.guid):
295             element.set_attribute_value(name, value)
296
297     def traces_from_data(self, box, data):
298         for name in data.get_trace_data(box.guid):
299             box.enable_trace(name)
300
301     def addresses_from_data(self, box, data):
302         for (autoconf, address, family, netprefix, broadcast) \
303                 in data.get_address_data(box.guid):
304             addr = box.add_address()
305             if autoconf:
306                 addr.set_attribute_value("AutoConfigure", autoconf)
307             if address:
308                 addr.set_attribute_value("Address", address)
309             if family != None:
310                 addr.set_attribute_value("Family", family)
311             if netprefix != None:
312                 addr.set_attribute_value("NetPrefix", netprefix)
313             if broadcast:
314                 addr.set_attribute_value("Broadcast", broadcast)
315
316     def routes_from_data(self, box, data):
317          for (family, destination, netprefix, nexthop) \
318                  in data.get_route_data(box.guid):
319             addr = box.add_route(family)
320             addr.set_attribute_value("Destination", destination)
321             addr.set_attribute_value("NetPrefix", netprefix)
322             addr.set_attribute_value("NextHop", nexthop)
323
324     def connections_from_data(self, experiment_description, guids, data):
325         for guid in guids:
326             box = experiment_description.box(guid)
327             for (connector_type_name, other_guid, other_connector_type_name) \
328                     in data.get_connection_data(guid):
329                     other_box = experiment_description.box(other_guid)
330                     connector = box.connector(connector_type_name)
331                     other_connector = other_box.connector(
332                             other_connector_type_name)
333                     if not connector.is_connected(other_connector):
334                         connector.connect(other_connector)
335