test support added
[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_name for trace_name 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, attributes):
207         for attribute in attributes:
208             if attribute.modified:
209                 data.add_factory_attribute_data(guid, attribute.name, 
210                         attribute.value)
211
212     def attributes_to_data(self, data, guid, attributes):
213         for attribute in attributes:
214             if attribute.modified:
215                 data.add_attribute_data(guid, attribute.name, attribute.value)
216
217     def traces_to_data(self, data, guid, traces):
218         for trace in traces:
219             if trace.enabled:
220                 data.add_trace_data(guid, trace.name)
221
222     def connections_to_data(self, data, guid, connectors):
223         for connector in connectors:
224             connector_type_name = connector.connector_type.name
225             for other_connector in connector.connections:
226                 other_guid = other_connector.box.guid
227                 other_connector_type_name = other_connector.connector_type.name
228                 data.add_connection_data(guid, connector_type_name, other_guid,
229                         other_connector_type_name)
230
231     def addresses_to_data(self, data, guid, addresses):
232         for addr in addresses:
233              autoconf = addr.get_attribute_value("AutoConfigure") \
234                     if addr.is_attribute_modified("AutoConfigure") else None
235              address = addr.get_attribute_value("Address") \
236                     if addr.is_attribute_modified("Address") else None
237              netprefix = addr.get_attribute_value("NetPrefix") \
238                     if addr.is_attribute_modified("NetPrefix") else None
239              family = addr.get_attribute_value("Family") \
240                     if addr.is_attribute_modified("Family") else None
241              broadcast = addr.get_attribute_value("Broadcast") \
242                     if addr.has_attribute("Broadcast") and \
243                      addr.is_attribute_modified("Broadcast") else None
244              data.add_address_data(guid, autoconf, address, family, netprefix, 
245                     broadcast)
246
247     def routes_to_data(self, data, guid, routes):
248         for route in routes:
249              family = route.get_attribute_value("Family")
250              destination = route.get_attribute_value("Destination")
251              netprefix = route.get_attribute_value("NetPrefix")
252              nexthop = route.get_attribute_value("NextHop")
253              data.add_route_data(guid, family, destination, netprefix, nexthop)
254
255     def from_data(self, experiment_description, data):
256         box_guids = list()
257         for guid in data.guids:
258             if data.is_testbed_data(guid):
259                 self.testbed_from_data(experiment_description, guid, data)
260             else:
261                 self.box_from_data(experiment_description, guid, data)
262                 box_guids.append(guid)
263         self.connections_from_data(experiment_description, box_guids, data)
264
265     def testbed_from_data(self, experiment_description, guid, data):
266         from nepi.core.design import FactoriesProvider
267         (testbed_id, testbed_version) = data.get_testbed_data(guid)
268         provider = FactoriesProvider(testbed_id, testbed_version)
269         experiment_description.add_testbed_description(provider)
270         testbed_description = experiment_description.testbed_description(guid)
271         self.attributes_from_data(testbed_description, data)
272
273     def box_from_data(self, experiment_description, guid, data):
274         (testbed_guid, factory_id) = data.get_box_data(guid)
275         testbed_description = experiment_description.testbed_description(
276                 testbed_guid)
277         self.factory_attributes_from_data(testbed_description, factory_id,
278                 guid, data)
279         box = testbed_description.create(factory_id)
280         self.graphical_info_from_data(box, data)
281         self.attributes_from_data(box, data)
282         self.traces_from_data(box, data)
283         self.addresses_from_data(box, data)
284         self.routes_from_data(box, data)
285
286     def graphical_info_from_data(self, element, data):
287         (x, y, width, height, label) =  data.get_graphical_info_data(
288                 element.guid)
289         element.graphical_info.x = x
290         element.graphical_info.y = y
291         element.graphical_info.width = width
292         element.graphical_info.height = height
293         element.graphical_info.label = label
294
295     def factory_attributes_from_data(self, testbed_description, factory_id, 
296             guid, data):
297         factory = testbed_description.provider.factory(factory_id)
298         for (name, value) in data.get_factory_attribute_data(guid):
299             factory.set_attribute_value(name, value)
300
301     def attributes_from_data(self, element, data):
302         for name, value in data.get_attribute_data(element.guid):
303             element.set_attribute_value(name, value)
304
305     def traces_from_data(self, box, data):
306         for name in data.get_trace_data(box.guid):
307             box.trace(name).enable()
308
309     def addresses_from_data(self, box, data):
310         for (autoconf, address, family, netprefix, broadcast) \
311                 in data.get_address_data(box.guid):
312             addr = box.add_address()
313             if autoconf:
314                 addr.set_attribute_value("AutoConfigure", autoconf)
315             if address:
316                 addr.set_attribute_value("Address", address)
317             if family:
318                 addr.set_attribute_value("Family", family)
319             if netprefix:
320                 addr.set_attribute_value("NetPrefix", netprefix)
321             if broadcast:
322                 addr.set_attribute_value("Broadcast", broadcast)
323
324     def routes_from_data(self, box, data):
325          for (family, destination, netprefix, nexthop) \
326                  in data.get_route_data(box.guid):
327             addr = box.add_route(family)
328             addr.set_attribute_value("Destination", destination)
329             addr.set_attribute_value("NetPrefix", netprefix)
330             addr.set_attribute_value("NextHop", nexthop)
331
332     def connections_from_data(self, experiment_description, guids, data):
333         for guid in guids:
334             box = experiment_description.box(guid)
335             for (connector_type_name, other_guid, other_connector_type_name) \
336                     in data.get_connection_data(guid):
337                     other_box = experiment_description.box(other_guid)
338                     connector = box.connector(connector_type_name)
339                     other_connector = other_box.connector(
340                             other_connector_type_name)
341                     if not connector.is_connected(other_connector):
342                         connector.connect(other_connector)
343