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")
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 with open(filename,'a+') as f:
189 # XXX: Adding fake \n for visual formatting
190 msg = props[elt] # + "\n"
192 elif event == "STDERR" :
193 filename = os.path.join("/tmp", "%s.err" % uid)
194 with open(filename,'a+') as f:
195 # XXX: Adding fake \n for visual formatting
196 msg = props[elt] # + "\n"
198 log = log + tag +" : " + props[elt]+" -- "
200 log = log + tag +" : " + props[elt]+" -- "
201 log = log + " STATUS "
204 def _inform_released(self, root, namespaces):
205 """ Parse and Display RELEASED message
209 parent_id = self._check_for_tag(root, namespaces, "src")
210 child_id = self._check_for_tag(root, namespaces, "res_id")
211 cid = self._check_for_tag(root, namespaces, "cid")
213 msg = "RELEASED - The resource : '"+child_id+ \
214 "' has been released by : '"+ parent_id
216 self.mailbox['release'].append(cid)
218 def _inform_error(self, root, namespaces):
219 """ Parse and Display ERROR message
222 reason = self._check_for_tag(root, namespaces, "reason")
223 msg = "The reason : "+reason
226 def _inform_warn(self, root, namespaces):
227 """ Parse and Display WARN message
230 reason = self._check_for_tag(root, namespaces, "reason")
231 msg = "The reason : "+reason
234 def _parse_inform(self, root, namespaces):
235 """ Check the significative element in the answer
236 Then Parse it and display using specific method
238 :param root: Root of the tree
239 :type root: ElementTree Element
240 :param namespaces: Namespaces of the tree
241 :type namespaces: str
244 itype = self._check_for_tag(root, namespaces, "itype")
246 method_name = '_inform_'+ itype.replace('.', '_').lower()
247 method = getattr(self, method_name)
249 method(root, namespaces)
251 msg = "There is no method to parse the response of the type " + itype
256 def check_mailbox(self, itype, attr):
257 """ Check the mail box
259 :param itype: type of mail
261 :param attr: value wanted
265 if itype == "create":
266 for res in self.mailbox[itype]:
269 self.mailbox[itype].remove(res)
272 for res in self.mailbox[itype]:
274 self.mailbox[itype].remove(res)
278 def handle(self, iq):
279 """ Check the mail box
281 :param iq: message received
284 namespaces = "{http://schema.mytestbed.net/omf/6.0/protocol}"
285 for i in iq['pubsub_event']['items']:
286 root = ET.fromstring(str(i))
288 self._parse_inform(root, namespaces)