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
37 def __init__(self, sliceid, expid ):
40 :param sliceid: Slice Name (= Xmpp Slice)
42 :param expid: Experiment ID (= Xmpp User)
46 self._slice_id = sliceid
50 def _id_element(self, parent, markup):
51 """ Insert a markup element with an id
53 :param parent: Parent element in an XML point of view
54 :type parent: ElementTree Element
55 :param markup: Name of the markup
59 elt = ET.SubElement(parent, markup)
60 elt.set("id", "\'omf-payload\'")
63 def _attr_element(self, parent, markup, text):
64 """ Insert a markup element with a text (value)
66 :param parent: Parent element in an XML point of view
67 :type parent: ElementTree Element
68 :param markup: Name of the markup
70 :param text: Value of the markup element
74 elt = ET.SubElement(parent, markup)
78 def execute_function(self, target, appid, cmdlineargs, path, env):
79 """ Build an Execute Message
81 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
83 :param appid: Application id
85 :param cmdlineargs: Arguments of the application
86 :type cmdlineargs: str
87 :param path: Path of the application
89 :param env: Environment variables
93 payload = ET.Element("omf-message")
94 execute = self._id_element(payload,"EXECUTE")
95 env = self._attr_element(execute, "ENV", env)
96 sliceid = self._attr_element(execute,"SLICEID",self._slice_id)
97 expid = self._attr_element(execute,"EXPID",self._exp_id)
98 target = self._attr_element(execute,"TARGET",target)
99 appid = self._attr_element(execute,"APPID",appid)
100 cmdlineargs = self._attr_element(execute,"CMDLINEARGS",cmdlineargs)
101 path = self._attr_element(execute,"PATH",path)
104 def stdin_function(self, target, value, appid):
105 """ Build an Execute Message
107 :param value: parameter that go in the stdin
109 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
111 :param appid: Application id
115 payload = ET.Element("omf-message")
116 stdin = self._id_element(payload,"STDIN")
117 value = self._attr_element(stdin,"VALUE",value)
118 sliceid = self._attr_element(stdin,"SLICEID",self._slice_id)
119 expid = self._attr_element(stdin,"EXPID",self._exp_id)
120 target = self._attr_element(stdin,"TARGET",target)
121 appid = self._attr_element(stdin,"APPID",appid)
124 def exit_function(self, target, appid):
125 """ Build an Exit Message
127 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
129 :param appid: Application id (ex : vlc#1)
133 payload = ET.Element("omf-message")
134 execute = self._id_element(payload,"EXIT")
135 sliceid = self._attr_element(execute,"SLICEID",self._slice_id)
136 expid = self._attr_element(execute,"EXPID",self._exp_id)
137 target = self._attr_element(execute,"TARGET",target)
138 appid = self._attr_element(execute,"APPID",appid)
141 def configure_function(self, target, value, path):
142 """ Build a Configure Message
144 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
146 :param value: guid of the RM
148 :param path: Path of the element to configure (ex : net/w0/channel)
152 payload = ET.Element("omf-message")
153 config = self._id_element(payload, "CONFIGURE")
154 sliceid = self._attr_element(config,"SLICEID",self._slice_id)
155 expid = self._attr_element(config,"EXPID",self._exp_id)
156 target = self._attr_element(config,"TARGET",target)
157 value = self._attr_element(config,"VALUE",value)
158 path = self._attr_element(config,"PATH",path)
161 def log_function(self,level, logger, level_name, data):
162 """ Build a Log Message
164 :param level: Level of logging
166 :param logger: Element publishing the log
168 :param level_name: Name of the level (ex : INFO)
169 :type level_name: str
170 :param data: Content to publish
174 payload = ET.Element("omf-message")
175 log = self._id_element(payload, "LOGGING")
176 level = self._attr_element(log,"LEVEL",level)
177 sliceid = self._attr_element(log,"SLICEID",self._slice_id)
178 logger = self._attr_element(log,"LOGGER",logger)
179 expid = self._attr_element(log,"EXPID",self._exp_id)
180 level_name = self._attr_element(log,"LEVEL_NAME",level_name)
181 data = self._attr_element(log,"DATA",data)
184 def alias_function(self, name, target):
185 """ Build an Alias Message
187 :param name: Name of the new alias
189 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
193 payload = ET.Element("omf-message")
194 alias = self._id_element(payload,"ALIAS")
195 sliceid = self._attr_element(alias,"SLICEID",self._slice_id)
196 expid = self._attr_element(alias,"EXPID",self._exp_id)
197 name = self._attr_element(alias,"NAME",name)
198 target = self._attr_element(alias,"TARGET",target)
201 def enroll_function(self, enrollkey, image, index, target ):
202 """ Build an Enroll Message
204 :param enrollkey: Type of enrollment (= 1)
206 :param image: Image (= * when all the nodes are concerned)
208 :param index: Index (= 1 in general)
210 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
214 payload = ET.Element("omf-message")
215 enroll = self._id_element(payload,"ENROLL")
216 enrollkey = self._attr_element(enroll,"ENROLLKEY",enrollkey)
217 sliceid = self._attr_element(enroll,"SLICEID",self._slice_id)
218 image = self._attr_element(enroll,"IMAGE",image)
219 expid = self._attr_element(enroll,"EXPID",self._exp_id)
220 index = self._attr_element(enroll,"INDEX",index)
221 target = self._attr_element(enroll,"TARGET",target)
224 def noop_function(self,target):
225 """ Build a Noop Message
227 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
231 payload = ET.Element("omf-message")
232 noop = self._id_element(payload,"NOOP")
233 sliceid = self._attr_element(noop,"SLICEID",self._slice_id)
234 expid = self._attr_element(noop,"EXPID",self._exp_id)
235 target = self._attr_element(noop,"TARGET",target)
238 def newexp_function(self, experimentid, address):
239 """ Build a NewExp Message
241 :param experimentid: Id of the new experiment
242 :type experimentid: str
243 :param address: Adress of the destination set of nodes
247 payload = ET.Element("omf-message")
248 newexp = self._id_element(payload,"EXPERIMENT_NEW")
249 experimentid = self._attr_element(newexp,"EXPERIMENT_ID",experimentid)
250 sliceid = self._attr_element(newexp,"SLICEID",self._slice_id)
251 expid = self._attr_element(newexp,"EXPID",self._exp_id)
252 address = self._attr_element(newexp,"ADDRESS",address)