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 xml.etree import cElementTree as ET
22 class MessageHandler():
24 .. class:: Class Args :
26 :param sliceid: Slice Name (= Xmpp Slice)
28 :param expid: Experiment ID (= Xmpp User)
33 This class is used only for OMF 5.4 Protocol and is going to become unused
43 def _type_element(self, type_elt, xmlns, msg_id):
44 """ Insert a markup element with an id
47 elt = ET.Element(type_elt)
48 elt.set("xmlns", xmlns)
49 elt.set("mid", msg_id)
54 def _attr_element(self, parent, markup, text, type_key=None, type_value = None):
55 """ Insert a markup element with a text (value)
57 :param parent: Parent element in an XML point of view
58 :type parent: ElementTree Element
59 :param markup: Name of the markup
61 :param text: Value of the markup element
65 elt = ET.SubElement(parent, markup)
66 if type_key and type_value:
67 elt.set(type_key, type_value)
71 def _id_element(self, parent, markup, key, value):
72 """ Insert a markup element with a text (value)
74 :param parent: Parent element in an XML point of view
75 :type parent: ElementTree Element
76 :param markup: Name of the markup
78 :param text: Value of the markup element
82 elt = ET.SubElement(parent, markup)
86 def create_function(self, msg_id, src, rtype, timestamp, props = None, guards = None):
87 """ Build a create message
89 :param msg_id: Id of the message
91 :param src: Src node that send the message (jabber source)
93 :param rtype: Type of the object
95 :param timestamp: Unix Timestamp
97 :param props: List of properties
99 :param guards: list of guards (assertions for properties)
102 payload = self._type_element("create", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
103 self._attr_element(payload,"src",src)
104 self._attr_element(payload,"ts",timestamp)
105 self._attr_element(payload,"rtype",rtype)
108 if rtype == "application" :
109 properties = self._id_element(payload,"props","xmlns:application",
110 "http://schema.mytestbed.net/omf/6.0/protocol/application")
111 elif rtype == "wlan" :
112 properties = self._id_element(payload,"props","xmlns:wlan",
113 "http://schema.mytestbed.net/omf/6.0/protocol/wlan")
115 properties = self._attr_element(payload,"props","")
117 for prop in list(props.keys()):
118 if isinstance(props[prop],str):
119 self._attr_element(properties,prop,props[prop],type_key="type", type_value = "string")
120 elif isinstance(props[prop],dict):
121 key = self._attr_element(properties,prop,"",type_key="type", type_value = "hash")
122 for comp in list(props[prop].keys()):
123 self._attr_element(key,comp,props[prop][comp],type_key="type", type_value = "string")
126 guardians = self._attr_element(payload,"guard","")
127 for guard in list(guards.keys()):
128 self._attr_element(guardians,guard,guards[guard],type_key="type", type_value = "string")
132 def configure_function(self, msg_id, src, timestamp, props = None, guards = None):
133 """ Build a configure message
135 :param msg_id: Id of the message
137 :param src: Src node that send the message (jabber source)
139 :param timestamp: Unix Timestamp
141 :param props: List of properties
143 :param guards: list of guards (assertions for properties)
146 payload = self._type_element("configure", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
147 self._attr_element(payload,"src",src)
148 self._attr_element(payload,"ts",timestamp)
151 properties = self._attr_element(payload,"props","")
152 for prop in list(props.keys()):
153 self._attr_element(properties,prop,props[prop],type_key="type", type_value = "symbol")
156 guardians = self._attr_element(payload,"guard","")
157 for guard in list(guards.keys()):
158 self._attr_element(guardians,guard,guards[guard],type_key="type", type_value = "string")
162 def request_function(self, msg_id, src, timestamp, props = None, guards = None):
163 """ Build a request message
165 :param msg_id: Id of the message
167 :param src: Src node that send the message (jabber source)
169 :param timestamp: Unix Timestamp
171 :param props: List of properties
173 :param guards: list of guards (assertions for properties)
176 payload = self._type_element("request", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
177 self._attr_element(payload,"src",src)
178 self._attr_element(payload,"ts",timestamp)
181 properties = self._attr_element(payload,"props","")
182 for prop in list(props.keys()):
183 self._attr_element(properties,prop,props[prop])
186 guardians = self._attr_element(payload,"guard","")
187 for guard in list(guards.keys()):
188 self._attr_element(guardians,guard,guards[guard])
191 # For now, we don't need the inform message since it is ht RC that send them.
193 # def inform_function(self, msg_id, src, timestamp, cid, itype):
194 # """ Build an inform message
196 # :param msg_id: Id of the message
198 # :param src: Src node that send the message (jabber source)
200 # :param rtype: Type of the object
202 # :param timestamp: Unix Timestamp
203 # :type timestamp: str
204 # :param cid: Id of the orignial message
206 # :param itype: type of the object created
210 # payload = self._type_element("inform", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
211 # sliceid = self._attr_element(payload,"src",src)
212 # expid = self._attr_element(config,"ts",timestamp)
213 # target = self._attr_element(config,"cid",cid)
214 # value = self._attr_element(config,"itype",value)
215 # path = self._attr_element(config,"properties",path)
218 def release_function(self, msg_id, src, timestamp, res_id = None, props = None, guards = None):
219 """ Build a release message
221 :param msg_id: Id of the message
223 :param src: Src node that send the message (jabber source)
225 :param timestamp: Unix Timestamp
227 :param res_id: Id of the release resource
229 :param props: List of properties
231 :param guards: list of guards (assertions for properties)
234 payload = self._type_element("release", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
235 self._attr_element(payload,"src",src)
236 self._attr_element(payload,"ts",timestamp)
238 self._attr_element(payload,"res_id",res_id)
241 properties = self._id_element(payload,"props","xmlns:frcp",
242 "http://schema.mytestbed.net/omf/6.0/protocol")
243 for prop in list(props.keys()):
244 self._attr_element(properties,prop,props[prop])
247 guardians = self._attr_element(payload,"guard","")
248 for guard in list(guards.keys()):
249 self._attr_element(guardians,guard,guards[guard])