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