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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.util.netgraph import NetGraph, TopologyType
21 from nepi.util.timefuncs import stformat, tsformat
23 from xml.dom import minidom
35 if isinstance(s, str):
36 rv = s.decode("latin1")
37 if isinstance(s, datetime.datetime):
39 elif not isinstance(s, unicode):
43 return rv.replace(u'\x00',u'�')
45 def xmldecode(s, cast = str):
46 ret = s.replace(u'�',u'\x00').encode("ascii")
53 if isinstance(value, bool):
55 if isinstance(value, int):
57 if isinstance(value, float):
62 def to_type(type, value):
69 return value == "True"
75 class ECXMLParser(object):
78 doc = minidom.Document()
80 self._ec_to_xml(doc, ec)
83 xml = doc.toprettyxml(indent=" ", encoding="UTF-8")
85 print >>sys.stderr, "Oops: generating XML from %s" % (data,)
90 def _ec_to_xml(self, doc, ec):
91 ecnode = doc.createElement("experiment")
92 ecnode.setAttribute("exp_id", xmlencode(ec.exp_id))
93 ecnode.setAttribute("run_id", xmlencode(ec.run_id))
94 ecnode.setAttribute("nthreads", xmlencode(ec.nthreads))
95 ecnode.setAttribute("local_dir", xmlencode(ec.local_dir))
96 doc.appendChild(ecnode)
98 if ec.netgraph != None:
99 self._netgraph_to_xml(doc, ecnode, ec)
101 rmsnode = doc.createElement("rms")
102 ecnode.appendChild(rmsnode)
104 for guid, rm in ec._resources.iteritems():
105 self._rm_to_xml(doc, rmsnode, ec, guid, rm)
109 def _netgraph_to_xml(self, doc, ecnode, ec):
110 ngnode = doc.createElement("topology")
111 ngnode.setAttribute("topo-type", xmlencode(ec.netgraph.topo_type))
112 ecnode.appendChild(ngnode)
114 self. _netgraph_nodes_to_xml(doc, ngnode, ec)
115 self. _netgraph_edges_to_xml(doc, ngnode, ec)
117 def _netgraph_nodes_to_xml(self, doc, ngnode, ec):
118 ngnsnode = doc.createElement("nodes")
119 ngnode.appendChild(ngnsnode)
121 for nid in ec.netgraph.nodes():
122 ngnnode = doc.createElement("node")
123 ngnnode.setAttribute("nid", xmlencode(nid))
124 ngnnode.setAttribute("nid-type", from_type(nid))
125 ngnsnode.appendChild(ngnnode)
127 # Mark ources and targets
128 if ec.netgraph.is_source(nid):
129 ngnnode.setAttribute("source", xmlencode(True))
131 if ec.netgraph.is_target(nid):
132 ngnnode.setAttribute("target", xmlencode(True))
135 annosnode = doc.createElement("node-annotations")
136 add_annotations = False
137 for name in ec.netgraph.node_annotations(nid):
138 add_annotations = True
139 value = ec.netgraph.node_annotation(nid, name)
140 annonode = doc.createElement("node-annotation")
141 annonode.setAttribute("name", xmlencode(name))
142 annonode.setAttribute("value", xmlencode(value))
143 annonode.setAttribute("type", from_type(value))
144 annosnode.appendChild(annonode)
147 ngnnode.appendChild(annosnode)
149 def _netgraph_edges_to_xml(self, doc, ngnode, ec):
150 ngesnode = doc.createElement("edges")
151 ngnode.appendChild(ngesnode)
153 for nid1, nid2 in ec.netgraph.edges():
154 ngenode = doc.createElement("edge")
155 ngenode.setAttribute("nid1", xmlencode(nid1))
156 ngenode.setAttribute("nid1-type", from_type(nid1))
157 ngenode.setAttribute("nid2", xmlencode(nid2))
158 ngenode.setAttribute("nid2-type", from_type(nid2))
159 ngesnode.appendChild(ngenode)
162 annosnode = doc.createElement("edge-annotations")
163 add_annotations = False
164 for name in ec.netgraph.edge_annotations(nid1, nid2):
165 add_annotations = True
166 value = ec.netgraph.edge_annotation(nid1, nid2, name)
167 annonode = doc.createElement("edge-annotation")
168 annonode.setAttribute("name", xmlencode(name))
169 annonode.setAttribute("value", xmlencode(value))
170 annonode.setAttribute("type", from_type(value))
171 annosnode.appendChild(annonode)
174 ngenode.appendChild(annosnode)
176 def _rm_to_xml(self, doc, rmsnode, ec, guid, rm):
177 rmnode = doc.createElement("rm")
178 rmnode.setAttribute("guid", xmlencode(guid))
179 rmnode.setAttribute("rtype", xmlencode(rm._rtype))
180 rmnode.setAttribute("state", xmlencode(rm._state))
182 rmnode.setAttribute("start_time", xmlencode(rm._start_time))
184 rmnode.setAttribute("stop_time", xmlencode(rm._stop_time))
185 if rm._discover_time:
186 rmnode.setAttribute("discover_time", xmlencode(rm._discover_time))
187 if rm._provision_time:
188 rmnode.setAttribute("provision_time", xmlencode(rm._provision_time))
190 rmnode.setAttribute("ready_time", xmlencode(rm._ready_time))
192 rmnode.setAttribute("release_time", xmlencode(rm._release_time))
194 rmnode.setAttribute("failed_time", xmlencode(rm._failed_time))
195 rmsnode.appendChild(rmnode)
197 anode = doc.createElement("attributes")
200 for attr in rm._attrs.values():
203 aanode = doc.createElement("attribute")
204 aanode.setAttribute("name", xmlencode(attr.name))
205 aanode.setAttribute("value", xmlencode(attr.value))
206 aanode.setAttribute("type", from_type(attr.value))
207 anode.appendChild(aanode)
210 rmnode.appendChild(anode)
212 cnode = doc.createElement("connections")
215 for guid in rm._connections:
217 ccnode = doc.createElement("connection")
218 ccnode.setAttribute("guid", xmlencode(guid))
219 cnode.appendChild(ccnode)
222 rmnode.appendChild(cnode)
224 cnnode = doc.createElement("conditions")
227 for action, conds in rm._conditions.iteritems():
229 for (group, state, time) in conds:
230 ccnnode = doc.createElement("condition")
231 ccnnode.setAttribute("action", xmlencode(action))
232 ccnnode.setAttribute("group", xmlencode(group))
233 ccnnode.setAttribute("state", xmlencode(state))
234 ccnnode.setAttribute("time", xmlencode(time))
235 cnnode.appendChild(ccnnode)
238 rmnode.appendChild(cnnode)
240 tnode = doc.createElement("traces")
243 for trace in rm._trcs.values():
246 ttnode = doc.createElement("trace")
247 ttnode.setAttribute("name", xmlencode(trace.name))
248 tnode.appendChild(ttnode)
251 rmnode.appendChild(tnode)
253 def from_xml(self, xml):
254 doc = minidom.parseString(xml)
255 return self._ec_from_xml(doc)
257 def _ec_from_xml(self, doc):
258 from nepi.execution.ec import ExperimentController
261 ecnode_list = doc.getElementsByTagName("experiment")
262 for ecnode in ecnode_list:
263 if ecnode.nodeType == doc.ELEMENT_NODE:
264 exp_id = xmldecode(ecnode.getAttribute("exp_id"))
265 run_id = xmldecode(ecnode.getAttribute("run_id"))
266 local_dir = xmldecode(ecnode.getAttribute("local_dir"))
268 # Configure number of preocessing threads
269 nthreads = xmldecode(ecnode.getAttribute("nthreads"))
270 os.environ["NEPI_NTHREADS"] = nthreads
272 # Deserialize netgraph
276 netgraph = self._netgraph_from_xml(doc, ecnode)
279 topo_type = netgraph.topo_type
280 topology = netgraph.topology
283 ec = ExperimentController(exp_id = exp_id, local_dir = local_dir,
284 topology = topology, topo_type = topo_type)
288 rmsnode_list = ecnode.getElementsByTagName("rms")
290 rmnode_list = rmsnode_list[0].getElementsByTagName("rm")
291 for rmnode in rmnode_list:
292 if rmnode.nodeType == doc.ELEMENT_NODE:
293 self._rm_from_xml(doc, rmnode, ec, connections)
295 for (guid1, guid2) in connections:
296 ec.register_connection(guid1, guid2)
302 def _netgraph_from_xml(self, doc, ecnode):
305 topology = ecnode.getElementsByTagName("topology")
307 topology = topology[0]
308 topo_type = xmldecode(topology.getAttribute("topo-type"))
310 netgraph = NetGraph(topo_type = topo_type)
312 ngnsnode_list = topology.getElementsByTagName("nodes")
314 ngnsnode = ngnsnode_list[0].getElementsByTagName("node")
315 for ngnnode in ngnsnode:
316 nid = xmldecode(ngnnode.getAttribute("nid"))
317 tipe = xmldecode(ngnnode.getAttribute("nid-type"))
318 nid = to_type(tipe, nid)
319 netgraph.add_node(nid)
321 if ngnnode.hasAttribute("source"):
322 netgraph.set_source(nid)
323 if ngnnode.hasAttribute("target"):
324 netgraph.set_target(nid)
326 annosnode_list = ngnnode.getElementsByTagName("node-annotations")
329 annosnode = annosnode_list[0].getElementsByTagName("node-annotation")
330 for annonode in annosnode:
331 name = xmldecode(annonode.getAttribute("name"))
334 ips = xmldecode(annonode.getAttribute("value"), eval) # list
336 netgraph.annotate_node_ip(nid, ip)
338 value = xmldecode(annonode.getAttribute("value"))
339 tipe = xmldecode(annonode.getAttribute("type"))
340 value = to_type(tipe, value)
341 netgraph.annotate_node(nid, name, value)
343 ngesnode_list = topology.getElementsByTagName("edges")
345 ngesnode = ngesnode_list[0].getElementsByTagName("edge")
346 for ngenode in ngesnode:
347 nid1 = xmldecode(ngenode.getAttribute("nid1"))
348 tipe1 = xmldecode(ngenode.getAttribute("nid1-type"))
349 nid1 = to_type(tipe1, nid1)
351 nid2 = xmldecode(ngenode.getAttribute("nid2"))
352 tipe2 = xmldecode(ngenode.getAttribute("nid2-type"))
353 nid2 = to_type(tipe2, nid2)
355 netgraph.add_edge(nid1, nid2)
357 annosnode_list = ngenode.getElementsByTagName("edge-annotations")
359 annosnode = annosnode_list[0].getElementsByTagName("edge-annotation")
360 for annonode in annosnode:
361 name = xmldecode(annonode.getAttribute("name"))
364 net = xmldecode(annonode.getAttribute("value"), eval) # dict
365 netgraph.annotate_edge_net(nid1, nid2, net[nid1], net[nid2],
366 net["mask"], net["network"], net["prefix"])
368 value = xmldecode(annonode.getAttribute("value"))
369 tipe = xmldecode(annonode.getAttribute("type"))
370 value = to_type(tipe, value)
371 netgraph.annotate_edge(nid1, nid2, name, value)
374 def _rm_from_xml(self, doc, rmnode, ec, connections):
378 provision_time = None
383 guid = xmldecode(rmnode.getAttribute("guid"), int)
384 rtype = xmldecode(rmnode.getAttribute("rtype"))
386 # FOR NOW ONLY STATE NEW IS ALLOWED
389 state = xmldecode(rmnode.getAttribute("state"), int)
391 if rmnode.hasAttribute("start_time"):
392 start_time = xmldecode(rmnode.getAttribute("start_time"),
394 if rmnode.hasAttribute("stop_time"):
395 stop_time = xmldecode(rmnode.getAttribute("stop_time"),
397 if rmnode.hasAttribute("discover_time"):
398 dicover_time = xmldecode(rmnode.getAttribute("discover_time"),
400 if rmnode.hasAttribute("provision_time"):
401 provision_time = xmldecode(rmnode.getAttribute("provision_time"),
403 if rmnode.hasAttribute("ready_time"):
404 ready_time = xmldecode(rmnode.getAttribute("ready_time"),
406 if rmnode.hasAttribute("release_time"):
407 release_time = xmldecode(rmnode.getAttribute("release_time"),
409 if rmnode.hasAttribute("failed_time"):
410 failed_time = xmldecode(rmnode.getAttribute("failed_time"),
414 ec.register_resource(rtype, guid = guid)
415 rm = ec.get_resource(guid)
416 rm.set_state_time(state, "_start_time", start_time)
417 rm.set_state_time(state, "_stop_time", stop_time)
418 rm.set_state_time(state, "_discover_time", discover_time)
419 rm.set_state_time(state, "_provision_time", provision_time)
420 rm.set_state_time(state, "_ready_time", ready_time)
421 rm.set_state_time(state, "_release_time", release_time)
422 rm.set_state_time(state, "_failed_time", failed_time)
424 anode_list = rmnode.getElementsByTagName("attributes")
426 aanode_list = anode_list[0].getElementsByTagName("attribute")
427 for aanode in aanode_list:
428 name = xmldecode(aanode.getAttribute("name"))
429 value = xmldecode(aanode.getAttribute("value"))
430 tipe = xmldecode(aanode.getAttribute("type"))
431 value = to_type(tipe, value)
434 cnode_list = rmnode.getElementsByTagName("connections")
436 ccnode_list = cnode_list[0].getElementsByTagName("connection")
437 for ccnode in ccnode_list:
438 guid2 = xmldecode(ccnode.getAttribute("guid"), int)
439 connections.add((guid, guid2))
441 tnode_list = rmnode.getElementsByTagName("traces")
443 ttnode_list = tnode_list[0].getElementsByTagName("trace")
444 for ttnode in ttnode_list:
445 name = xmldecode(ttnode.getAttribute("name"))
446 ec.enable_trace(guid, name)
448 cnnode_list = rmnode.getElementsByTagName("conditions")
450 ccnnode_list = cnnode_list[0].getElementsByTagName("condition")
451 for ccnnode in ccnnode_list:
452 action = xmldecode(ccnnode.getAttribute("action"), int)
453 group = xmldecode(ccnnode.getAttribute("group"), eval) # list
454 state = xmldecode(ccnnode.getAttribute("state"), int)
455 time = xmldecode(ccnnode.getAttribute("time"))
456 time = to_type('STRING', time)
457 ec.register_condition(guid, action, group, state, time = time)