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