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