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>
18 # Julien Tribino <julien.tribino@inria.fr>
20 from nepi.util.logger import Logger
24 import xml.etree.ElementTree as ET
26 # inherit from BaseXmpp and XMLstream classes
27 class OMF6Parser(Logger):
29 .. class:: Class Args :
31 :param jid: Jabber Id (= Xmpp Slice + Date)
33 :param password: Jabber Password (= Xmpp Password)
38 This class is an XMPP Client with customized method
45 :param jid: Jabber Id (= Xmpp Slice + Date)
47 :param password: Jabber Password (= Xmpp Password)
52 super(OMF6Parser, self).__init__("OMF6API")
60 def init_mailbox(self):
61 self.mailbox['create'] = []
62 self.mailbox['started'] = []
63 self.mailbox['release'] = []
65 def _check_for_tag(self, root, namespaces, tag):
66 """ Check if an element markup is in the ElementTree
68 :param root: Root of the tree
69 :type root: ElementTree Element
70 :param namespaces: Namespaces of the element
72 :param tag: Tag that will search in the tree
76 for element in root.iter(namespaces+tag):
82 def _check_for_props(self, root, namespaces):
83 """ Check if an element markup is in the ElementTree
85 :param root: Root of the tree
86 :type root: ElementTree Element
87 :param namespaces: Namespaces of the element
92 for properties in root.iter(namespaces+'props'):
93 for element in properties.iter():
94 if element.tag and element.text:
95 props[element.tag] = element.text
98 def _check_for_membership(self, root, namespaces):
99 """ Check if an element markup is in the ElementTree
101 :param root: Root of the tree
102 :type root: ElementTree Element
103 :param namespaces: Namespaces of the element
104 :type namespaces: str
107 for element in root.iter(namespaces+'membership'):
108 for elt in element.iter(namespaces+'it'):
113 def _check_output(self, root, namespaces):
114 """ Check the significative element in the answer and display it
116 :param root: Root of the tree
117 :type root: ElementTree Element
118 :param namespaces: Namespaces of the tree
119 :type namespaces: str
122 fields = ["TARGET", "REASON", "PATH", "APPID", "VALUE"]
125 msg = self._check_for_tag(root, namespaces, elt)
127 response = response + " " + msg.text + " :"
128 deb = self._check_for_tag(root, namespaces, "MESSAGE")
130 msg = response + " " + deb.text
136 def _inform_creation_ok(self, root, namespaces):
137 """ Parse and Display CREATION OK message
141 uid = self._check_for_tag(root, namespaces, "uid")
142 cid = self._check_for_tag(root, namespaces, "cid")
143 member = self._check_for_membership(root, namespaces)
144 binary_path = self._check_for_tag(root, namespaces, "binary_path")
145 msg = "CREATION OK -- "
147 msg = msg + "The resource : '"+binary_path
149 msg = msg + "The interface"
151 msg = msg + "' is listening to the topics : '"+ uid
153 msg = msg + "' and '"+ member +"'"
156 self.mailbox['create'].append([cid, uid ])
158 def _inform_creation_failed(self, root, namespaces):
159 """ Parse and Display CREATION FAILED message
162 reason = self._check_for_tag(root, namespaces, "reason")
163 cid = self._check_for_tag(root, namespaces, "cid")
164 msg = "CREATION FAILED - The reason : "+reason
167 self.mailbox['create'].append([cid, uid ])
169 def _inform_status(self, root, namespaces):
170 """ Parse and Display STATUS message
173 props = self._check_for_props(root, namespaces)
174 uid = self._check_for_tag(root, namespaces, "uid")
175 event = self._check_for_tag(root, namespaces, "event")
178 for elt in props.keys():
179 ns, tag = elt.split('}')
181 log = log + "membership : " + props[elt]+" -- "
183 self.mailbox['started'].append(uid)
184 log = log + "event : " + props[elt]+" -- "
186 if event == "STDOUT" :
187 filename = os.path.join("/tmp", "%s.out" % uid)
188 f = open(filename,'a+')
189 # XXX: Adding fake \n for visual formatting
190 msg = props[elt] # + "\n"
193 elif event == "STDERR" :
194 filename = os.path.join("/tmp", "%s.err" % uid)
195 f = open(filename,'a+')
196 # XXX: Adding fake \n for visual formatting
197 msg = props[elt] # + "\n"
200 log = log + tag +" : " + props[elt]+" -- "
202 log = log + tag +" : " + props[elt]+" -- "
203 log = log + " STATUS "
206 def _inform_released(self, root, namespaces):
207 """ Parse and Display RELEASED message
211 parent_id = self._check_for_tag(root, namespaces, "src")
212 child_id = self._check_for_tag(root, namespaces, "res_id")
213 cid = self._check_for_tag(root, namespaces, "cid")
215 msg = "RELEASED - The resource : '"+child_id+ \
216 "' has been released by : '"+ parent_id
218 self.mailbox['release'].append(cid)
220 def _inform_error(self, root, namespaces):
221 """ Parse and Display ERROR message
224 reason = self._check_for_tag(root, namespaces, "reason")
225 msg = "The reason : "+reason
228 def _inform_warn(self, root, namespaces):
229 """ Parse and Display WARN message
232 reason = self._check_for_tag(root, namespaces, "reason")
233 msg = "The reason : "+reason
236 def _parse_inform(self, root, namespaces):
237 """ Check the significative element in the answer
238 Then Parse it and display using specific method
240 :param root: Root of the tree
241 :type root: ElementTree Element
242 :param namespaces: Namespaces of the tree
243 :type namespaces: str
246 itype = self._check_for_tag(root, namespaces, "itype")
248 method_name = '_inform_'+ itype.replace('.', '_').lower()
249 method = getattr(self, method_name)
251 method(root, namespaces)
253 msg = "There is no method to parse the response of the type " + itype
258 def check_mailbox(self, itype, attr):
259 """ Check the mail box
261 :param itype: type of mail
263 :param attr: value wanted
267 if itype == "create":
268 for res in self.mailbox[itype]:
271 self.mailbox[itype].remove(res)
274 for res in self.mailbox[itype]:
276 self.mailbox[itype].remove(res)
280 def handle(self, iq):
281 """ Check the mail box
283 :param iq: message received
286 namespaces = "{http://schema.mytestbed.net/omf/6.0/protocol}"
287 for i in iq['pubsub_event']['items']:
288 root = ET.fromstring(str(i))
290 self._parse_inform(root, namespaces)