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