bug fixes
[nepi.git] / src / nepi / util / parser / _xml.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from nepi.core.attributes import Attribute
5 from nepi.util.parser.base import ExperimentData, ExperimentParser
6 from xml.dom import minidom
7
8 import sys
9
10 class XmlExperimentParser(ExperimentParser):
11     def to_xml(self, experiment_description):
12         data = self.to_data(experiment_description)
13         doc = minidom.Document()        
14         exp_tag = doc.createElement("experiment")
15         testbeds_tag = doc.createElement("testbeds")
16         exp_tag.appendChild(testbeds_tag)
17
18         elements_tags = dict()
19         for guid in sorted(data.guids):
20             if data.is_testbed_data(guid):
21                 elements_tag = self.testbed_data_to_xml(doc, testbeds_tag, guid, data)
22                 elements_tags[guid] = elements_tag
23             else:
24                 self.box_data_to_xml(doc, elements_tags, guid, data)
25         doc.appendChild(exp_tag)
26         
27         try:
28             xml = doc.toprettyxml(indent="    ", encoding="UTF-8")
29         except:
30             print >>sys.stderr, "Oops: generating XML from %s" % (data,)
31             raise
32         
33         return xml
34
35     def testbed_data_to_xml(self, doc, parent_tag, guid, data):
36         testbed_tag = doc.createElement("testbed") 
37         testbed_tag.setAttribute("guid", str(guid))
38         (testbed_id, testbed_version) = data.get_testbed_data(guid)
39         testbed_tag.setAttribute("testbed_id", str(testbed_id))
40         testbed_tag.setAttribute("testbed_version", str(testbed_version))
41         parent_tag.appendChild(testbed_tag)
42         self.graphical_info_data_to_xml(doc, testbed_tag, guid, data)
43         self.attributes_data_to_xml(doc, testbed_tag, guid, data)
44         elements_tag = doc.createElement("elements")
45         testbed_tag.appendChild(elements_tag)
46         return elements_tag
47
48     def box_data_to_xml(self, doc, elements_tags, guid, data):
49         (testbed_guid, factory_id) = data.get_box_data(guid)
50         element_tag = doc.createElement("element")
51         parent_tag = elements_tags[testbed_guid]
52         parent_tag.appendChild(element_tag)
53         element_tag.setAttribute("factory_id", factory_id)
54         element_tag.setAttribute("guid", str(guid))
55         self.graphical_info_data_to_xml(doc, element_tag, guid, data)
56         self.factory_attributes_data_to_xml(doc, element_tag, guid, data)
57         self.attributes_data_to_xml(doc, element_tag, guid, data)
58         self.traces_data_to_xml(doc, element_tag, guid, data)
59         self.addresses_data_to_xml(doc, element_tag, guid, data)
60         self.routes_data_to_xml(doc, element_tag, guid, data)
61         self.connections_data_to_xml(doc, element_tag, guid, data)
62
63     def graphical_info_data_to_xml(self, doc, parent_tag, guid, data):
64         graphical_info_tag = doc.createElement("graphical_info") 
65         parent_tag.appendChild(graphical_info_tag)
66         (x, y, width, height) = data.get_graphical_info_data(guid)
67         graphical_info_tag.setAttribute("x", str(x))
68         graphical_info_tag.setAttribute("y", str(y))
69         graphical_info_tag.setAttribute("width", str(width))
70         graphical_info_tag.setAttribute("height", str(height))
71
72     def factory_attributes_data_to_xml(self, doc, parent_tag, guid, data):
73         factory_attributes_tag = doc.createElement("factory_attributes")
74         for (name, value) in data.get_factory_attribute_data(guid):
75             if value is not None:
76                 factory_attribute_tag = doc.createElement("factory_attribute") 
77                 factory_attributes_tag.appendChild(factory_attribute_tag)
78                 factory_attribute_tag.setAttribute("name", name)
79                 factory_attribute_tag.setAttribute("value", str(value))
80                 factory_attribute_tag.setAttribute("type", self.type_to_standard(value))
81         if factory_attributes_tag.hasChildNodes():
82             parent_tag.appendChild(factory_attributes_tag)
83
84     def attributes_data_to_xml(self, doc, parent_tag, guid, data):
85         attributes_tag = doc.createElement("attributes") 
86         for name, value in data.get_attribute_data(guid):
87             if value is not None:
88                 attribute_tag = doc.createElement("attribute") 
89                 attributes_tag.appendChild(attribute_tag)
90                 attribute_tag.setAttribute("name", name)
91                 attribute_tag.setAttribute("value", str(value))
92                 attribute_tag.setAttribute("type", self.type_to_standard(value))
93         if attributes_tag.hasChildNodes():
94             parent_tag.appendChild(attributes_tag)
95
96     def traces_data_to_xml(self, doc, parent_tag, guid, data):
97         traces_tag = doc.createElement("traces") 
98         for name in data.get_trace_data(guid):
99             trace_tag = doc.createElement("trace") 
100             traces_tag.appendChild(trace_tag)
101             trace_tag.setAttribute("name", name)
102         if traces_tag.hasChildNodes():
103             parent_tag.appendChild(traces_tag)
104
105     def addresses_data_to_xml(self, doc, parent_tag, guid, data):
106         addresses_tag = doc.createElement("addresses") 
107         for (autoconfigure, address, netprefix, broadcast) \
108                 in data.get_address_data(guid):
109             address_tag = doc.createElement("address") 
110             addresses_tag.appendChild(address_tag)
111             if autoconfigure:
112                 address_tag.setAttribute("AutoConfigure", str(autoconf))
113             if address:
114                 address_tag.setAttribute("Address", str(address))
115             address_tag.setAttribute("NetPrefix", str(netprefix))
116             if broadcast:
117                 address_tag.setAttribute("Broadcast", str(broadcast))
118         if addresses_tag.hasChildNodes():
119             parent_tag.appendChild(addresses_tag)
120
121     def routes_data_to_xml(self, doc, parent_tag, guid, data):
122         routes_tag = doc.createElement("routes") 
123         for (destination, netprefix, nexthop) \
124                 in data.get_route_data(guid):
125             route_tag = doc.createElement("route") 
126             routes_tag.appendChild(route_tag)
127             route_tag.setAttribute("Destination", str(destination))
128             route_tag.setAttribute("NetPrefix", str(netprefix))
129             route_tag.setAttribute("NextHop", str(nexthop))
130         if routes_tag.hasChildNodes():
131             parent_tag.appendChild(routes_tag)
132
133     def connections_data_to_xml(self, doc, parent_tag, guid, data):
134         connections_tag = doc.createElement("connections") 
135         for (connector_type_name, other_guid, other_connector_type_name) \
136                 in data.get_connection_data(guid):
137                 connection_tag = doc.createElement("connection") 
138                 connections_tag.appendChild(connection_tag)
139                 connection_tag.setAttribute("connector", connector_type_name)
140                 connection_tag.setAttribute("other_guid", str(other_guid))
141                 connection_tag.setAttribute("other_connector",
142                         other_connector_type_name)
143         if connections_tag.hasChildNodes():
144             parent_tag.appendChild(connections_tag)
145
146     def from_xml_to_data(self, xml):
147         data = ExperimentData()
148         doc = minidom.parseString(xml)
149         testbeds_tag = doc.getElementsByTagName("testbeds")[0] 
150         testbed_tag_list = testbeds_tag.getElementsByTagName("testbed")
151         for testbed_tag in testbed_tag_list:
152             if testbed_tag.nodeType == doc.ELEMENT_NODE:
153                 testbed_guid = int(testbed_tag.getAttribute("guid"))
154                 elements_tag = testbed_tag.getElementsByTagName("elements")[0] 
155                 elements_tag = testbed_tag.removeChild(elements_tag)
156                 self.testbed_data_from_xml(testbed_tag, data)
157                 element_tag_list = elements_tag.getElementsByTagName("element")
158                 for element_tag in element_tag_list:
159                     if element_tag.nodeType == doc.ELEMENT_NODE:
160                         self.box_data_from_xml(element_tag, testbed_guid, data)
161         return data
162
163     def from_xml(self, experiment_description, xml):
164         data = self.from_xml_to_data(xml)
165         self.from_data(experiment_description, data)
166
167     def testbed_data_from_xml(self, tag, data):
168         testbed_guid = int(tag.getAttribute("guid"))
169         testbed_id = str(tag.getAttribute("testbed_id"))
170         testbed_version = str(tag.getAttribute("testbed_version"))
171         data.add_testbed_data(testbed_guid, testbed_id, testbed_version)
172         self.graphical_info_data_from_xml(tag, testbed_guid, data)
173         self.attributes_data_from_xml(tag, testbed_guid, data)
174
175     def box_data_from_xml(self, tag, testbed_guid, data):
176         guid = int(tag.getAttribute("guid"))
177         factory_id = str(tag.getAttribute("factory_id"))
178         data.add_box_data(guid, testbed_guid, factory_id)
179         self.graphical_info_data_from_xml(tag, guid, data)
180         self.factory_attributes_data_from_xml(tag, guid, data)
181         self.attributes_data_from_xml(tag, guid, data)
182         self.traces_data_from_xml(tag, guid, data)
183         self.addresses_data_from_xml(tag, guid, data)
184         self.routes_data_from_xml(tag, guid, data)
185         self.connections_data_from_xml(tag, guid, data)
186
187     def graphical_info_data_from_xml(self, tag, guid, data):
188         graphical_info_tag_list = tag.getElementsByTagName(
189                 "graphical_info")
190         if len(graphical_info_tag_list) == 0:
191             return
192
193         graphical_info_tag = graphical_info_tag_list[0]
194         if graphical_info_tag.nodeType == tag.ELEMENT_NODE:
195             x = float(graphical_info_tag.getAttribute("x"))
196             y = float(graphical_info_tag.getAttribute("y"))
197             width = float(graphical_info_tag.getAttribute("width"))
198             height = float(graphical_info_tag.getAttribute("height"))
199             data.add_graphical_info_data(guid, x, y, width, height)
200
201     def factory_attributes_data_from_xml(self, tag, guid, data):
202         factory_attributes_tag_list = tag.getElementsByTagName(
203                 "factory_attributes")
204         if len(factory_attributes_tag_list) == 0:
205             return
206
207         factory_attribute_tag_list = factory_attributes_tag_list[0].\
208                 getElementsByTagName("factory_attribute")
209         for factory_attribute_tag in factory_attribute_tag_list:
210              if factory_attribute_tag.nodeType == tag.ELEMENT_NODE:
211                 name = str(factory_attribute_tag.getAttribute("name"))
212                 value = factory_attribute_tag.getAttribute("value")
213                 std_type = factory_attribute_tag.getAttribute("type")
214                 value = self.type_from_standard(std_type, value)
215                 data.add_factory_attribute_data(guid, name, value)
216
217     def attributes_data_from_xml(self, tag, guid, data):
218         attributes_tag_list= tag.getElementsByTagName("attributes")
219         if len(attributes_tag_list) == 0:
220             return
221
222         attribute_tag_list = attributes_tag_list[0].\
223                 getElementsByTagName("attribute")
224         for attribute_tag in attribute_tag_list:
225              if attribute_tag.nodeType == tag.ELEMENT_NODE:
226                 name = str(attribute_tag.getAttribute("name"))
227                 value = attribute_tag.getAttribute("value")
228                 std_type = attribute_tag.getAttribute("type")
229                 value = self.type_from_standard(std_type, value)
230                 data.add_attribute_data(guid, name, value)
231
232     def traces_data_from_xml(self, tag, guid, data):
233         traces_tag_list = tag.getElementsByTagName("traces")
234         if len(traces_tag_list) == 0:
235             return
236
237         trace_tag_list = traces_tag_list[0].getElementsByTagName(
238                 "trace")
239         for trace_tag in trace_tag_list:
240              if trace_tag.nodeType == tag.ELEMENT_NODE:
241                 name = str(trace_tag.getAttribute("name"))
242                 data.add_trace_data(guid, name)
243
244     def addresses_data_from_xml(self, tag, guid, data):
245         addresses_tag_list = tag.getElementsByTagName("addresses")
246         if len(addresses_tag_list) == 0:
247             return
248
249         address_tag_list = addresses_tag_list[0].\
250                 getElementsByTagName("address")
251         for address_tag in address_tag_list:
252             if address_tag.nodeType == tag.ELEMENT_NODE:
253                 autoconf = address_tag.getAttribute("AutoConfigure") == "True" \
254                        if address_tag.hasAttribute("AutoConfigure") else None
255                 address = str(address_tag.getAttribute("Address")) \
256                        if address_tag.hasAttribute("Address") else None
257                 netprefix = int(address_tag.getAttribute("NetPrefix")) \
258                        if address_tag.hasAttribute("NetPrefix") else None
259                 broadcast = str(address_tag.getAttribute("Broadcast")) \
260                        if address_tag.hasAttribute("Broadcast") else None
261                 data.add_address_data(guid, autoconf, address, netprefix, 
262                         broadcast)
263
264     def routes_data_from_xml(self, tag, guid, data):
265         routes_tag_list = tag.getElementsByTagName("routes")
266         if len(routes_tag_list) == 0:
267             return
268
269         route_tag_list = routes_tag_list[0].getElementsByTagName("route")
270         for route_tag in route_tag_list:
271             if route_tag.nodeType == tag.ELEMENT_NODE:
272                 destination = str(route_tag.getAttribute("Destination"))
273                 netprefix = int(route_tag.getAttribute("NetPrefix"))
274                 nexthop = str(route_tag.getAttribute("NextHop"))
275                 data.add_route_data(guid, destination, netprefix, 
276                         nexthop)
277
278     def connections_data_from_xml(self, tag, guid, data):
279         connections_tag_list = tag.getElementsByTagName("connections")
280         if len(connections_tag_list) == 0:
281             return
282
283         connection_tag_list = connections_tag_list[0].getElementsByTagName(
284                 "connection")
285         for connection_tag in connection_tag_list:
286              if connection_tag.nodeType == tag.ELEMENT_NODE:
287                  connector_type_name = str(connection_tag.getAttribute(
288                      "connector"))
289                  other_connector_type_name = str(connection_tag.getAttribute(
290                          "other_connector"))
291                  other_guid = int(connection_tag.getAttribute("other_guid"))
292                  data.add_connection_data(guid, connector_type_name, 
293                          other_guid, other_connector_type_name)
294
295     def type_to_standard(self, value):
296         if isinstance(value, str):
297             return Attribute.STRING
298         if isinstance(value, bool):
299             return Attribute.BOOL
300         if isinstance(value, int):
301             return Attribute.INTEGER
302         if isinstance(value, float):
303             return Attribute.DOUBLE
304     
305     def type_from_standard(self, type, value):
306         if type == Attribute.STRING:
307             return str(value)
308         if type == Attribute.BOOL:
309             return value == "True"
310         if type == Attribute.INTEGER:
311             return int(value)
312         if type == Attribute.DOUBLE:
313             return float(value)
314