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>
19 # Julien Tribino <julien.tribino@inria.fr>
21 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")
57 def init_mailbox(self):
58 self.mailbox['create'] = []
59 self.mailbox['started'] = []
60 self.mailbox['release'] = []
62 def _check_for_tag(self, root, namespaces, tag):
63 """ Check if an element markup is in the ElementTree
65 :param root: Root of the tree
66 :type root: ElementTree Element
67 :param namespaces: Namespaces of the element
69 :param tag: Tag that will search in the tree
73 for element in root.iter(namespaces+tag):
79 def _check_for_props(self, root, namespaces):
80 """ Check if an element markup is in the ElementTree
82 :param root: Root of the tree
83 :type root: ElementTree Element
84 :param namespaces: Namespaces of the element
89 for properties in root.iter(namespaces+'props'):
90 for element in properties.iter():
91 if element.tag and element.text:
92 props[element.tag] = element.text
95 def _check_for_membership(self, root, namespaces):
96 """ Check if an element markup is in the ElementTree
98 :param root: Root of the tree
99 :type root: ElementTree Element
100 :param namespaces: Namespaces of the element
101 :type namespaces: str
104 for element in root.iter(namespaces+'membership'):
105 for elt in element.iter(namespaces+'it'):
110 def _check_output(self, root, namespaces):
111 """ Check the significative element in the answer and display it
113 :param root: Root of the tree
114 :type root: ElementTree Element
115 :param namespaces: Namespaces of the tree
116 :type namespaces: str
119 fields = ["TARGET", "REASON", "PATH", "APPID", "VALUE"]
122 msg = self._check_for_tag(root, namespaces, elt)
124 response = response + " " + msg.text + " :"
125 deb = self._check_for_tag(root, namespaces, "MESSAGE")
127 msg = response + " " + deb.text
133 def _inform_creation_ok(self, root, namespaces):
135 uid = self._check_for_tag(root, namespaces, "uid")
136 cid = self._check_for_tag(root, namespaces, "cid")
137 member = self._check_for_membership(root, namespaces)
138 binary_path = self._check_for_tag(root, namespaces, "binary_path")
139 msg = "CREATION OK -- "
141 msg = msg + "The resource : '"+binary_path
143 msg = msg + "' is listening to the topics : '"+ uid
145 msg = msg + "' and '"+ member +"'"
148 self.mailbox['create'].append([cid, uid ])
150 def _inform_creation_failed(self, root, namespaces):
151 reason = self._check_for_tag(root, namespaces, "reason")
152 cid = self._check_for_tag(root, namespaces, "cid")
153 msg = "CREATION FAILED - The reason : "+reason
156 self.mailbox['create'].append([cid, uid ])
158 def _inform_status(self, root, namespaces):
159 props = self._check_for_props(root, namespaces)
160 uid = self._check_for_tag(root, namespaces, "uid")
162 for elt in props.keys():
163 ns, tag = elt.split('}')
165 msg = msg + "membership : " + props[elt]+" -- "
167 self.mailbox['started'].append(uid)
168 msg = msg + "event : " + props[elt]+" -- "
170 msg = msg + tag +" : " + props[elt]+" -- "
171 msg = msg + " STATUS "
174 def _inform_released(self, root, namespaces):
176 parent_id = self._check_for_tag(root, namespaces, "src")
177 child_id = self._check_for_tag(root, namespaces, "res_id")
178 cid = self._check_for_tag(root, namespaces, "cid")
180 msg = "RELEASED - The resource : '"+child_id+ \
181 "' has been released by : '"+ parent_id
183 self.mailbox['release'].append(cid)
185 def _inform_error(self, root, namespaces):
186 reason = self._check_for_tag(root, namespaces, "reason")
187 msg = "The reason : "+reason
190 def _inform_warn(self, root, namespaces):
191 reason = self._check_for_tag(root, namespaces, "reason")
192 msg = "The reason : "+reason
195 def _parse_inform(self, root, namespaces):
196 """ Check the significative element in the answer and display it
198 :param root: Root of the tree
199 :type root: ElementTree Element
200 :param namespaces: Namespaces of the tree
201 :type namespaces: str
204 itype = self._check_for_tag(root, namespaces, "itype")
206 method_name = '_inform_'+ itype.replace('.', '_').lower()
207 method = getattr(self, method_name)
209 method(root, namespaces)
211 msg = "There is no method to parse the response of the type " + itype
216 def check_mailbox(self, itype, attr):
217 if itype == "create":
218 for res in self.mailbox[itype]:
221 self.mailbox[itype].remove(res)
224 for res in self.mailbox[itype]:
226 self.mailbox[itype].remove(res)
230 def handle(self, iq):
231 namespaces = "{http://schema.mytestbed.net/omf/6.0/protocol}"
232 for i in iq['pubsub_event']['items']:
233 root = ET.fromstring(str(i))
235 self._parse_inform(root, namespaces)