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 xml.etree import cElementTree as ET
23 class MessageHandler():
25 .. class:: Class Args :
27 :param sliceid: Slice Name (= Xmpp Slice)
29 :param expid: Experiment ID (= Xmpp User)
34 This class is used only for OMF 5.4 Protocol and is going to become unused
44 def _type_element(self, type_elt, xmlns, msg_id):
45 """ Insert a markup element with an id
48 elt = ET.Element(type_elt)
49 elt.set("xmlns", xmlns)
50 elt.set("mid", msg_id)
55 def _attr_element(self, parent, markup, text, type_key=None, type_value = None):
56 """ Insert a markup element with a text (value)
58 :param parent: Parent element in an XML point of view
59 :type parent: ElementTree Element
60 :param markup: Name of the markup
62 :param text: Value of the markup element
66 elt = ET.SubElement(parent, markup)
67 if type_key and type_value:
68 elt.set(type_key, type_value)
72 def _id_element(self, parent, markup, key, value):
73 """ Insert a markup element with a text (value)
75 :param parent: Parent element in an XML point of view
76 :type parent: ElementTree Element
77 :param markup: Name of the markup
79 :param text: Value of the markup element
83 elt = ET.SubElement(parent, markup)
87 def create_function(self, msg_id, src, rtype, timestamp, props = None, guards = None):
88 """ Build a create message
90 payload = self._type_element("create", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
91 self._attr_element(payload,"src",src)
92 self._attr_element(payload,"ts",timestamp)
93 self._attr_element(payload,"rtype",rtype)
96 if rtype == "application" :
97 properties = self._id_element(payload,"props","xmlns:application",
98 "http://schema.mytestbed.net/omf/6.0/protocol/application")
99 elif rtype == "wlan" :
100 properties = self._id_element(payload,"props","xmlns:wlan",
101 "http://schema.mytestbed.net/omf/6.0/protocol/wlan")
103 properties = self._attr_element(payload,"props","")
105 for prop in props.keys():
106 if isinstance(props[prop],str):
107 self._attr_element(properties,prop,props[prop],type_key="type", type_value = "string")
108 elif isinstance(props[prop],dict):
109 key = self._attr_element(properties,prop,"",type_key="type", type_value = "hash")
110 for comp in props[prop].keys():
111 self._attr_element(key,comp,props[prop][comp],type_key="type", type_value = "string")
114 guardians = self._attr_element(payload,"guard","")
115 for guard in guards.keys():
116 self._attr_element(guardians,guard,guards[guard],type_key="type", type_value = "string")
120 def configure_function(self, msg_id, src, timestamp, props = None, guards = None):
121 """ Build a configure message
123 payload = self._type_element("configure", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
124 self._attr_element(payload,"src",src)
125 self._attr_element(payload,"ts",timestamp)
128 properties = self._attr_element(payload,"props","")
129 for prop in props.keys():
130 self._attr_element(properties,prop,props[prop],type_key="type", type_value = "symbol")
133 guardians = self._attr_element(payload,"guard","")
134 for guard in guards.keys():
135 self._attr_element(guardians,guard,guards[guard],type_key="type", type_value = "string")
139 def request_function(self, msg_id, src, timestamp, props = None, guards = None):
140 """ Build a request message
143 payload = self._type_element("request", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
144 self._attr_element(payload,"src",src)
145 self._attr_element(payload,"ts",timestamp)
148 properties = self._attr_element(payload,"props","")
149 for prop in props.keys():
150 self._attr_element(properties,prop,props[prop])
153 guardians = self._attr_element(payload,"guard","")
154 for guard in guards.keys():
155 self._attr_element(guardians,guard,guards[guard])
158 def inform_function(self, msg_id, src, timestamp, cid, itype):
159 """ Build an inform message
162 payload = self._type_element("inform", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
163 sliceid = self._attr_element(payload,"src",src)
164 expid = self._attr_element(config,"ts",timestamp)
165 target = self._attr_element(config,"cid",cid)
166 value = self._attr_element(config,"itype",value)
167 path = self._attr_element(config,"properties",path)
170 def release_function(self, msg_id, src, timestamp, res_id = None, props = None, guards = None):
171 """ Build a release message
174 payload = self._type_element("release", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
175 self._attr_element(payload,"src",src)
176 self._attr_element(payload,"ts",timestamp)
178 self._attr_element(payload,"res_id",res_id)
181 properties = self._id_element(payload,"props","xmlns:frcp",
182 "http://schema.mytestbed.net/omf/6.0/protocol")
183 for prop in props.keys():
184 self._attr_element(properties,prop,props[prop])
187 guardians = self._attr_element(payload,"guard","")
188 for guard in guards.keys():
189 self._attr_element(guardians,guard,guards[guard])