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