Nets integration test (design + execute) == integration running OK. Still some BUGS.
[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         if family:
81             address_data["Family"] = family
82         if netprefix:
83             address_data["NetPrefix"] = netprefix
84         if broadcast:
85             address_data["Broadcast"] = broadcast
86         addresses_data.append(address_data)
87
88     def add_route_data(self, guid, family, destination, netprefix, nexthop): 
89         data = self.data[guid]
90         if not "routes" in data:
91             data["routes"] = list()
92         routes_data = data["routes"]
93         route_data = dict({
94             "Family": family, 
95             "Destination": destination,
96             "NetPrefix": netprefix, 
97             "NextHop": nexthop 
98             })
99         routes_data.append(route_data)
100
101     def is_testbed_data(self, guid):
102         return True if "testbed_id" in self.data[guid] else None
103
104     def get_testbed_data(self, guid):
105         testbed_data = self.data[guid]
106         return (testbed_data["testbed_id"], testbed_data["testbed_version"])
107
108     def get_box_data(self, guid):
109         box_data = self.data[guid]
110         return (box_data["testbed_guid"], box_data["factory_id"])
111
112     def get_graphical_info_data(self, guid):
113         data = self.data[guid]
114         if not "graphical_info" in data:
115             return (0, 0, 0, 0, "") 
116         graphical_info_data = data["graphical_info"]
117         return (graphical_info_data["x"],
118                 graphical_info_data["y"],
119                 graphical_info_data["width"],
120                 graphical_info_data["height"],
121                 graphical_info_data["label"])
122
123     def get_factory_attribute_data(self, guid):
124         data = self.data[guid]
125         if not "factory_attributes" in data:
126             return []
127         factory_attributes_data = data["factory_attributes"]
128         return [(name, value) for name, value \
129                 in factory_attributes_data.iteritems()]
130
131     def get_attribute_data(self, guid):
132         data = self.data[guid]
133         if not "attributes" in data:
134             return []
135         attributes_data = data["attributes"]
136         return [(name, value) for name, value \
137                 in attributes_data.iteritems()]
138
139     def get_trace_data(self, guid):
140         data = self.data[guid]
141         if not "traces" in data:
142             return []
143         return [trace_id for trace_id in data["traces"]]
144
145     def get_connection_data(self, guid):
146         data = self.data[guid]
147         if not "connections" in data:
148             return []
149         connections_data = data["connections"]
150         return [(connector_type_name, other_guid, other_connector_type_name) \
151                     for connector_type_name, connection_data \
152                         in connections_data.iteritems() \
153                             for other_guid, other_connector_type_name \
154                                 in connection_data.iteritems()]
155
156     def get_address_data(self, guid):
157         data = self.data[guid]
158         if not "addresses" in data:
159             return []
160         addresses_data = data["addresses"]
161         return [(data["AutoConfigure"] if "AutoConfigure" in data else None,
162                  data["Address"] if "Address" in data else None,
163                  data["Family"] if "Family" in data else None,
164                  data["NetPrefix"] if "NetPrefix" in data else None,
165                  data["Broadcast"] if "Broadcast" in data else None) \
166                  for data in addresses_data]
167
168     def get_route_data(self, guid):
169         data = self.data[guid]
170         if not "routes" in data:
171             return []
172         routes_data = data["routes"]
173         return [(data["Family"],
174                  data["Destination"],
175                  data["NetPrefix"],
176                  data["NextHop"]) \
177                          for data in routes_data]
178
179 class ExperimentParser(object):
180     def to_data(self, experiment_description):
181         data = ExperimentData()
182         for testbed_description in experiment_description.testbed_descriptions:
183             guid = testbed_description.guid
184             testbed_id = testbed_description.provider.testbed_id
185             testbed_version = testbed_description.provider.testbed_version
186             data.add_testbed_data(guid, testbed_id, testbed_version)
187             self.graphical_info_to_data(data, guid, 
188                     testbed_description.graphical_info)
189             self.attributes_to_data(data, guid, testbed_description.attributes)
190             for box in testbed_description.boxes:
191                 data.add_box_data(box.guid, guid, box.factory_id)
192                 self.graphical_info_to_data(data, box.guid, box.graphical_info)
193                 self.factory_attributes_to_data(data, box.guid, 
194                         box.factory_attributes)
195                 self.attributes_to_data(data, box.guid, box.attributes)
196                 self.traces_to_data(data, box.guid, box.traces)
197                 self.connections_to_data(data, box.guid, box.connectors)
198                 self.addresses_to_data(data, box.guid, box.addresses)
199                 self.routes_to_data(data, box.guid, box.routes)
200         return data
201
202     def graphical_info_to_data(self, data, guid, g_info):
203         data.add_graphical_info_data(guid, g_info.x, g_info.y, g_info.width, 
204                 g_info.height, g_info.label)
205
206     def factory_attributes_to_data(self, data, guid, factory_attributes):
207         for name, value in factory_attributes.iteritems():
208             data.add_factory_attribute_data(guid, name, value)
209
210     def attributes_to_data(self, data, guid, attributes):
211         for attribute in attributes:
212             if attribute.modified:
213                 data.add_attribute_data(guid, attribute.name, attribute.value)
214
215     def traces_to_data(self, data, guid, traces):
216         for trace in traces:
217             if trace.enabled:
218                 data.add_trace_data(guid, trace.trace_id)
219
220     def connections_to_data(self, data, guid, connectors):
221         for connector in connectors:
222             connector_type_name = connector.connector_type.name
223             for other_connector in connector.connections:
224                 other_guid = other_connector.box.guid
225                 other_connector_type_name = other_connector.connector_type.name
226                 data.add_connection_data(guid, connector_type_name, other_guid,
227                         other_connector_type_name)
228
229     def addresses_to_data(self, data, guid, addresses):
230         for addr in addresses:
231              autoconf = addr.get_attribute_value("AutoConfigure") \
232                     if addr.is_attribute_modified("AutoConfigure") else None
233              address = addr.get_attribute_value("Address") \
234                     if addr.is_attribute_modified("Address") else None
235              netprefix = addr.get_attribute_value("NetPrefix") \
236                     if addr.is_attribute_modified("NetPrefix") else None
237              family = addr.get_attribute_value("Family") \
238                     if addr.is_attribute_modified("Family") else None
239              broadcast = addr.get_attribute_value("Broadcast") \
240                     if addr.has_attribute("Broadcast") and \
241                      addr.is_attribute_modified("Broadcast") else None
242              data.add_address_data(guid, autoconf, address, family, netprefix, 
243                     broadcast)
244
245     def routes_to_data(self, data, guid, routes):
246         for route in routes:
247              family = route.get_attribute_value("Family")
248              destination = route.get_attribute_value("Destination")
249              netprefix = route.get_attribute_value("NetPrefix")
250              nexthop = route.get_attribute_value("NextHop")
251              data.add_route_data(guid, family, destination, netprefix, nexthop)
252
253     def from_data(self, experiment_description, data):
254         box_guids = list()
255         for guid in data.guids:
256             if data.is_testbed_data(guid):
257                 self.testbed_from_data(experiment_description, guid, data)
258             else:
259                 self.box_from_data(experiment_description, guid, data)
260                 box_guids.append(guid)
261         self.connections_from_data(experiment_description, box_guids, data)
262
263     def testbed_from_data(self, experiment_description, guid, data):
264         from nepi.core.design import FactoriesProvider
265         (testbed_id, testbed_version) = data.get_testbed_data(guid)
266         provider = FactoriesProvider(testbed_id, testbed_version)
267         experiment_description.add_testbed_description(provider)
268         testbed_description = experiment_description.testbed_description(guid)
269         self.attributes_from_data(testbed_description, data)
270
271     def box_from_data(self, experiment_description, guid, data):
272         (testbed_guid, factory_id) = data.get_box_data(guid)
273         testbed_description = experiment_description.testbed_description(
274                 testbed_guid)
275         self.factory_attributes_from_data(testbed_description, factory_id,
276                 guid, data)
277         box = testbed_description.create(factory_id)
278         self.graphical_info_from_data(box, data)
279         self.attributes_from_data(box, data)
280         self.traces_from_data(box, data)
281         self.addresses_from_data(box, data)
282         self.routes_from_data(box, data)
283
284     def graphical_info_from_data(self, element, data):
285         (x, y, width, height, label) =  data.get_graphical_info_data(
286                 element.guid)
287         element.graphical_info.x = x
288         element.graphical_info.y = y
289         element.graphical_info.width = width
290         element.graphical_info.height = height
291         element.graphical_info.label = label
292
293     def factory_attributes_from_data(self, testbed_description, factory_id, 
294             guid, data):
295         factory = testbed_description.provider.factory(factory_id)
296         for (name, value) in data.get_factory_attribute_data(guid):
297             factory.set_attribute_value(name, value)
298
299     def attributes_from_data(self, element, data):
300         for name, value in data.get_attribute_data(element.guid):
301             element.set_attribute_value(name, value)
302
303     def traces_from_data(self, box, data):
304         for name in data.get_trace_data(box.guid):
305             box.trace(name).enable()
306
307     def addresses_from_data(self, box, data):
308         for (autoconf, address, family, netprefix, broadcast) \
309                 in data.get_address_data(box.guid):
310             addr = box.add_address()
311             if autoconf:
312                 addr.set_attribute_value("AutoConfigure", autoconf)
313             if address:
314                 addr.set_attribute_value("Address", address)
315             if family:
316                 addr.set_attribute_value("Family", family)
317             if netprefix:
318                 addr.set_attribute_value("NetPrefix", netprefix)
319             if broadcast:
320                 addr.set_attribute_value("Broadcast", broadcast)
321
322     def routes_from_data(self, box, data):
323          for (family, destination, netprefix, nexthop) \
324                  in data.get_route_data(box.guid):
325             addr = box.add_route(family)
326             addr.set_attribute_value("Destination", destination)
327             addr.set_attribute_value("NetPrefix", netprefix)
328             addr.set_attribute_value("NextHop", nexthop)
329
330     def connections_from_data(self, experiment_description, guids, data):
331         for guid in guids:
332             box = experiment_description.box(guid)
333             for (connector_type_name, other_guid, other_connector_type_name) \
334                     in data.get_connection_data(guid):
335                     other_box = experiment_description.box(other_guid)
336                     connector = box.connector(connector_type_name)
337                     other_connector = other_box.connector(
338                             other_connector_type_name)
339                     if not connector.is_connected(other_connector):
340                         connector.connect(other_connector)
341