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
38 def __init__(self, sliceid, expid ):
41 :param sliceid: Slice Name (= Xmpp Slice)
43 :param expid: Experiment ID (= Xmpp User)
47 self._slice_id = sliceid
51 def _id_element(self, parent, markup):
52 """ Insert a markup element with an id
54 :param parent: Parent element in an XML point of view
55 :type parent: ElementTree Element
56 :param markup: Name of the markup
60 elt = ET.SubElement(parent, markup)
61 elt.set("id", "\'omf-payload\'")
64 def _attr_element(self, parent, markup, text):
65 """ Insert a markup element with a text (value)
67 :param parent: Parent element in an XML point of view
68 :type parent: ElementTree Element
69 :param markup: Name of the markup
71 :param text: Value of the markup element
75 elt = ET.SubElement(parent, markup)
79 def execute_function(self, target, appid, cmdlineargs, path, env):
80 """ Build an Execute Message
82 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
84 :param appid: Application id
86 :param cmdlineargs: Arguments of the application
87 :type cmdlineargs: str
88 :param path: Path of the application
90 :param env: Environment variables
94 payload = ET.Element("omf-message")
95 execute = self._id_element(payload,"EXECUTE")
96 env = self._attr_element(execute, "ENV", env)
97 sliceid = self._attr_element(execute,"SLICEID",self._slice_id)
98 expid = self._attr_element(execute,"EXPID",self._exp_id)
99 target = self._attr_element(execute,"TARGET",target)
100 appid = self._attr_element(execute,"APPID",appid)
101 cmdlineargs = self._attr_element(execute,"CMDLINEARGS",cmdlineargs)
102 path = self._attr_element(execute,"PATH",path)
105 def exit_function(self, target, appid):
106 """ Build an Exit Message
108 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
110 :param appid: Application id (ex : vlc#1)
114 payload = ET.Element("omf-message")
115 execute = self._id_element(payload,"EXIT")
116 sliceid = self._attr_element(execute,"SLICEID",self._slice_id)
117 expid = self._attr_element(execute,"EXPID",self._exp_id)
118 target = self._attr_element(execute,"TARGET",target)
119 appid = self._attr_element(execute,"APPID",appid)
122 def configure_function(self, target, value, path):
123 """ Build a Configure Message
125 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
127 :param value: guid of the RM
129 :param path: Path of the element to configure (ex : net/w0/channel)
133 payload = ET.Element("omf-message")
134 config = self._id_element(payload, "CONFIGURE")
135 sliceid = self._attr_element(config,"SLICEID",self._slice_id)
136 expid = self._attr_element(config,"EXPID",self._exp_id)
137 target = self._attr_element(config,"TARGET",target)
138 value = self._attr_element(config,"VALUE",value)
139 path = self._attr_element(config,"PATH",path)
142 def log_function(self,level, logger, level_name, data):
143 """ Build a Log Message
145 :param level: Level of logging
147 :param logger: Element publishing the log
149 :param level_name: Name of the level (ex : INFO)
150 :type level_name: str
151 :param data: Content to publish
155 payload = ET.Element("omf-message")
156 log = self._id_element(payload, "LOGGING")
157 level = self._attr_element(log,"LEVEL",level)
158 sliceid = self._attr_element(log,"SLICEID",self._slice_id)
159 logger = self._attr_element(log,"LOGGER",logger)
160 expid = self._attr_element(log,"EXPID",self._exp_id)
161 level_name = self._attr_element(log,"LEVEL_NAME",level_name)
162 data = self._attr_element(log,"DATA",data)
165 def alias_function(self, name, target):
166 """ Build a Alias Message
168 :param name: Name of the new alias
170 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
174 payload = ET.Element("omf-message")
175 alias = self._id_element(payload,"ALIAS")
176 sliceid = self._attr_element(alias,"SLICEID",self._slice_id)
177 expid = self._attr_element(alias,"EXPID",self._exp_id)
178 name = self._attr_element(alias,"NAME",name)
179 target = self._attr_element(alias,"TARGET",target)
182 def enroll_function(self, enrollkey, image, index, target ):
183 """ Build an Enroll Message
185 :param enrollkey: Type of enrollment (= 1)
187 :param image: Image (= * when all the nodes are concerned)
189 :param index: Index (= 1 in general)
191 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
195 payload = ET.Element("omf-message")
196 enroll = self._id_element(payload,"ENROLL")
197 enrollkey = self._attr_element(enroll,"ENROLLKEY",enrollkey)
198 sliceid = self._attr_element(enroll,"SLICEID",self._slice_id)
199 image = self._attr_element(enroll,"IMAGE",image)
200 expid = self._attr_element(enroll,"EXPID",self._exp_id)
201 index = self._attr_element(enroll,"INDEX",index)
202 target = self._attr_element(enroll,"TARGET",target)
205 def noop_function(self,target):
206 """ Build a Noop Message
208 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
212 payload = ET.Element("omf-message")
213 noop = self._id_element(payload,"NOOP")
214 sliceid = self._attr_element(noop,"SLICEID",self._slice_id)
215 expid = self._attr_element(noop,"EXPID",self._exp_id)
216 target = self._attr_element(noop,"TARGET",target)
219 def newexp_function(self, experimentid, address):
220 """ Build a NewExp Message
222 :param experimentid: Id of the new experiment
223 :type experimentid: str
224 :param address: Adress of the destination set of nodes
228 payload = ET.Element("omf-message")
229 newexp = self._id_element(payload,"EXPERIMENT_NEW")
230 experimentid = self._attr_element(newexp,"EXPERIMENT_ID",experimentid)
231 sliceid = self._attr_element(newexp,"SLICEID",self._slice_id)
232 expid = self._attr_element(newexp,"EXPID",self._exp_id)
233 address = self._attr_element(newexp,"ADDRESS",address)