2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from __future__ import print_function
21 from nepi.util.netgraph import NetGraph, TopologyType
22 from nepi.util.timefuncs import stformat, tsformat
24 from xml.dom import minidom
36 if isinstance(s, str):
37 rv = s.decode("latin1")
38 if isinstance(s, datetime.datetime):
40 elif not isinstance(s, unicode):
44 return rv.replace(u'\x00',u'�')
46 def xmldecode(s, cast = str):
47 ret = s.replace(u'�',u'\x00').encode("ascii")
54 if isinstance(value, bool):
56 if isinstance(value, int):
58 if isinstance(value, float):
63 def to_type(type, value):
70 return value == "True"
76 class ECXMLParser(object):
79 doc = minidom.Document()
81 self._ec_to_xml(doc, ec)
84 xml = doc.toprettyxml(indent=" ", encoding="UTF-8")
86 print("Oops: generating XML from %s" % (data,), file=sys.stderr)
91 def _ec_to_xml(self, doc, ec):
92 ecnode = doc.createElement("experiment")
93 ecnode.setAttribute("exp_id", xmlencode(ec.exp_id))
94 ecnode.setAttribute("run_id", xmlencode(ec.run_id))
95 ecnode.setAttribute("nthreads", xmlencode(ec.nthreads))
96 ecnode.setAttribute("local_dir", xmlencode(ec.local_dir))
97 doc.appendChild(ecnode)
99 if ec.netgraph != None:
100 self._netgraph_to_xml(doc, ecnode, ec)
102 rmsnode = doc.createElement("rms")
103 ecnode.appendChild(rmsnode)
105 for guid, rm in ec._resources.items():
106 self._rm_to_xml(doc, rmsnode, ec, guid, rm)
110 def _netgraph_to_xml(self, doc, ecnode, ec):
111 ngnode = doc.createElement("topology")
112 ngnode.setAttribute("topo-type", xmlencode(ec.netgraph.topo_type))
113 ecnode.appendChild(ngnode)
115 self. _netgraph_nodes_to_xml(doc, ngnode, ec)
116 self. _netgraph_edges_to_xml(doc, ngnode, ec)
118 def _netgraph_nodes_to_xml(self, doc, ngnode, ec):
119 ngnsnode = doc.createElement("nodes")
120 ngnode.appendChild(ngnsnode)
122 for nid in ec.netgraph.nodes():
123 ngnnode = doc.createElement("node")
124 ngnnode.setAttribute("nid", xmlencode(nid))
125 ngnnode.setAttribute("nid-type", from_type(nid))
126 ngnsnode.appendChild(ngnnode)
128 # Mark ources and targets
129 if ec.netgraph.is_source(nid):
130 ngnnode.setAttribute("source", xmlencode(True))
132 if ec.netgraph.is_target(nid):
133 ngnnode.setAttribute("target", xmlencode(True))
136 annosnode = doc.createElement("node-annotations")
137 add_annotations = False
138 for name in ec.netgraph.node_annotations(nid):
139 add_annotations = True
140 value = ec.netgraph.node_annotation(nid, name)
141 annonode = doc.createElement("node-annotation")
142 annonode.setAttribute("name", xmlencode(name))
143 annonode.setAttribute("value", xmlencode(value))
144 annonode.setAttribute("type", from_type(value))
145 annosnode.appendChild(annonode)
148 ngnnode.appendChild(annosnode)
150 def _netgraph_edges_to_xml(self, doc, ngnode, ec):
151 ngesnode = doc.createElement("edges")
152 ngnode.appendChild(ngesnode)
154 for nid1, nid2 in ec.netgraph.edges():
155 ngenode = doc.createElement("edge")
156 ngenode.setAttribute("nid1", xmlencode(nid1))
157 ngenode.setAttribute("nid1-type", from_type(nid1))
158 ngenode.setAttribute("nid2", xmlencode(nid2))
159 ngenode.setAttribute("nid2-type", from_type(nid2))
160 ngesnode.appendChild(ngenode)
163 annosnode = doc.createElement("edge-annotations")
164 add_annotations = False
165 for name in ec.netgraph.edge_annotations(nid1, nid2):
166 add_annotations = True
167 value = ec.netgraph.edge_annotation(nid1, nid2, name)
168 annonode = doc.createElement("edge-annotation")
169 annonode.setAttribute("name", xmlencode(name))
170 annonode.setAttribute("value", xmlencode(value))
171 annonode.setAttribute("type", from_type(value))
172 annosnode.appendChild(annonode)
175 ngenode.appendChild(annosnode)
177 def _rm_to_xml(self, doc, rmsnode, ec, guid, rm):
178 rmnode = doc.createElement("rm")
179 rmnode.setAttribute("guid", xmlencode(guid))
180 rmnode.setAttribute("rtype", xmlencode(rm._rtype))
181 rmnode.setAttribute("state", xmlencode(rm._state))
183 rmnode.setAttribute("start_time", xmlencode(rm._start_time))
185 rmnode.setAttribute("stop_time", xmlencode(rm._stop_time))
186 if rm._discover_time:
187 rmnode.setAttribute("discover_time", xmlencode(rm._discover_time))
188 if rm._provision_time:
189 rmnode.setAttribute("provision_time", xmlencode(rm._provision_time))
191 rmnode.setAttribute("ready_time", xmlencode(rm._ready_time))
193 rmnode.setAttribute("release_time", xmlencode(rm._release_time))
195 rmnode.setAttribute("failed_time", xmlencode(rm._failed_time))
196 rmsnode.appendChild(rmnode)
198 anode = doc.createElement("attributes")
201 for attr in rm._attrs.values():
204 aanode = doc.createElement("attribute")
205 aanode.setAttribute("name", xmlencode(attr.name))
206 aanode.setAttribute("value", xmlencode(attr.value))
207 aanode.setAttribute("type", from_type(attr.value))
208 anode.appendChild(aanode)
211 rmnode.appendChild(anode)
213 cnode = doc.createElement("connections")
216 for guid in rm._connections:
218 ccnode = doc.createElement("connection")
219 ccnode.setAttribute("guid", xmlencode(guid))
220 cnode.appendChild(ccnode)
223 rmnode.appendChild(cnode)
225 cnnode = doc.createElement("conditions")
228 for action, conds in rm._conditions.items():
230 for (group, state, time) in conds:
231 ccnnode = doc.createElement("condition")
232 ccnnode.setAttribute("action", xmlencode(action))
233 ccnnode.setAttribute("group", xmlencode(group))
234 ccnnode.setAttribute("state", xmlencode(state))
235 ccnnode.setAttribute("time", xmlencode(time))
236 cnnode.appendChild(ccnnode)
239 rmnode.appendChild(cnnode)
241 tnode = doc.createElement("traces")
244 for trace in rm._trcs.values():
247 ttnode = doc.createElement("trace")
248 ttnode.setAttribute("name", xmlencode(trace.name))
249 tnode.appendChild(ttnode)
252 rmnode.appendChild(tnode)
254 def from_xml(self, xml):
255 doc = minidom.parseString(xml)
256 return self._ec_from_xml(doc)
258 def _ec_from_xml(self, doc):
259 from nepi.execution.ec import ExperimentController
262 ecnode_list = doc.getElementsByTagName("experiment")
263 for ecnode in ecnode_list:
264 if ecnode.nodeType == doc.ELEMENT_NODE:
265 exp_id = xmldecode(ecnode.getAttribute("exp_id"))
266 run_id = xmldecode(ecnode.getAttribute("run_id"))
267 local_dir = xmldecode(ecnode.getAttribute("local_dir"))
269 # Configure number of preocessing threads
270 nthreads = xmldecode(ecnode.getAttribute("nthreads"))
271 os.environ["NEPI_NTHREADS"] = nthreads
273 # Deserialize netgraph
277 netgraph = self._netgraph_from_xml(doc, ecnode)
280 topo_type = netgraph.topo_type
281 topology = netgraph.topology
284 ec = ExperimentController(exp_id = exp_id, local_dir = local_dir,
285 topology = topology, topo_type = topo_type)
289 rmsnode_list = ecnode.getElementsByTagName("rms")
291 rmnode_list = rmsnode_list[0].getElementsByTagName("rm")
292 for rmnode in rmnode_list:
293 if rmnode.nodeType == doc.ELEMENT_NODE:
294 self._rm_from_xml(doc, rmnode, ec, connections)
296 for (guid1, guid2) in connections:
297 ec.register_connection(guid1, guid2)
303 def _netgraph_from_xml(self, doc, ecnode):
306 topology = ecnode.getElementsByTagName("topology")
308 topology = topology[0]
309 topo_type = xmldecode(topology.getAttribute("topo-type"))
311 netgraph = NetGraph(topo_type = topo_type)
313 ngnsnode_list = topology.getElementsByTagName("nodes")
315 ngnsnode = ngnsnode_list[0].getElementsByTagName("node")
316 for ngnnode in ngnsnode:
317 nid = xmldecode(ngnnode.getAttribute("nid"))
318 tipe = xmldecode(ngnnode.getAttribute("nid-type"))
319 nid = to_type(tipe, nid)
320 netgraph.add_node(nid)
322 if ngnnode.hasAttribute("source"):
323 netgraph.set_source(nid)
324 if ngnnode.hasAttribute("target"):
325 netgraph.set_target(nid)
327 annosnode_list = ngnnode.getElementsByTagName("node-annotations")
330 annosnode = annosnode_list[0].getElementsByTagName("node-annotation")
331 for annonode in annosnode:
332 name = xmldecode(annonode.getAttribute("name"))
335 ips = xmldecode(annonode.getAttribute("value"), eval) # list
337 netgraph.annotate_node_ip(nid, ip)
339 value = xmldecode(annonode.getAttribute("value"))
340 tipe = xmldecode(annonode.getAttribute("type"))
341 value = to_type(tipe, value)
342 netgraph.annotate_node(nid, name, value)
344 ngesnode_list = topology.getElementsByTagName("edges")
346 ngesnode = ngesnode_list[0].getElementsByTagName("edge")
347 for ngenode in ngesnode:
348 nid1 = xmldecode(ngenode.getAttribute("nid1"))
349 tipe1 = xmldecode(ngenode.getAttribute("nid1-type"))
350 nid1 = to_type(tipe1, nid1)
352 nid2 = xmldecode(ngenode.getAttribute("nid2"))
353 tipe2 = xmldecode(ngenode.getAttribute("nid2-type"))
354 nid2 = to_type(tipe2, nid2)
356 netgraph.add_edge(nid1, nid2)
358 annosnode_list = ngenode.getElementsByTagName("edge-annotations")
360 annosnode = annosnode_list[0].getElementsByTagName("edge-annotation")
361 for annonode in annosnode:
362 name = xmldecode(annonode.getAttribute("name"))
365 net = xmldecode(annonode.getAttribute("value"), eval) # dict
366 netgraph.annotate_edge_net(nid1, nid2, net[nid1], net[nid2],
367 net["mask"], net["network"], net["prefix"])
369 value = xmldecode(annonode.getAttribute("value"))
370 tipe = xmldecode(annonode.getAttribute("type"))
371 value = to_type(tipe, value)
372 netgraph.annotate_edge(nid1, nid2, name, value)
375 def _rm_from_xml(self, doc, rmnode, ec, connections):
379 provision_time = None
384 guid = xmldecode(rmnode.getAttribute("guid"), int)
385 rtype = xmldecode(rmnode.getAttribute("rtype"))
387 # FOR NOW ONLY STATE NEW IS ALLOWED
390 state = xmldecode(rmnode.getAttribute("state"), int)
392 if rmnode.hasAttribute("start_time"):
393 start_time = xmldecode(rmnode.getAttribute("start_time"),
395 if rmnode.hasAttribute("stop_time"):
396 stop_time = xmldecode(rmnode.getAttribute("stop_time"),
398 if rmnode.hasAttribute("discover_time"):
399 dicover_time = xmldecode(rmnode.getAttribute("discover_time"),
401 if rmnode.hasAttribute("provision_time"):
402 provision_time = xmldecode(rmnode.getAttribute("provision_time"),
404 if rmnode.hasAttribute("ready_time"):
405 ready_time = xmldecode(rmnode.getAttribute("ready_time"),
407 if rmnode.hasAttribute("release_time"):
408 release_time = xmldecode(rmnode.getAttribute("release_time"),
410 if rmnode.hasAttribute("failed_time"):
411 failed_time = xmldecode(rmnode.getAttribute("failed_time"),
415 ec.register_resource(rtype, guid = guid)
416 rm = ec.get_resource(guid)
417 rm.set_state_time(state, "_start_time", start_time)
418 rm.set_state_time(state, "_stop_time", stop_time)
419 rm.set_state_time(state, "_discover_time", discover_time)
420 rm.set_state_time(state, "_provision_time", provision_time)
421 rm.set_state_time(state, "_ready_time", ready_time)
422 rm.set_state_time(state, "_release_time", release_time)
423 rm.set_state_time(state, "_failed_time", failed_time)
425 anode_list = rmnode.getElementsByTagName("attributes")
427 aanode_list = anode_list[0].getElementsByTagName("attribute")
428 for aanode in aanode_list:
429 name = xmldecode(aanode.getAttribute("name"))
430 value = xmldecode(aanode.getAttribute("value"))
431 tipe = xmldecode(aanode.getAttribute("type"))
432 value = to_type(tipe, value)
435 cnode_list = rmnode.getElementsByTagName("connections")
437 ccnode_list = cnode_list[0].getElementsByTagName("connection")
438 for ccnode in ccnode_list:
439 guid2 = xmldecode(ccnode.getAttribute("guid"), int)
440 connections.add((guid, guid2))
442 tnode_list = rmnode.getElementsByTagName("traces")
444 ttnode_list = tnode_list[0].getElementsByTagName("trace")
445 for ttnode in ttnode_list:
446 name = xmldecode(ttnode.getAttribute("name"))
447 ec.enable_trace(guid, name)
449 cnnode_list = rmnode.getElementsByTagName("conditions")
451 ccnnode_list = cnnode_list[0].getElementsByTagName("condition")
452 for ccnnode in ccnnode_list:
453 action = xmldecode(ccnnode.getAttribute("action"), int)
454 group = xmldecode(ccnnode.getAttribute("group"), eval) # list
455 state = xmldecode(ccnnode.getAttribute("state"), int)
456 time = xmldecode(ccnnode.getAttribute("time"))
457 time = to_type('STRING', time)
458 ec.register_condition(guid, action, group, state, time = time)