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/>.
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 exit_function(self, target, appid):
105 """ Build an Exit Message
107 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
109 :param appid: Application id (ex : vlc#1)
113 payload = ET.Element("omf-message")
114 execute = self._id_element(payload,"EXIT")
115 sliceid = self._attr_element(execute,"SLICEID",self._slice_id)
116 expid = self._attr_element(execute,"EXPID",self._exp_id)
117 target = self._attr_element(execute,"TARGET",target)
118 appid = self._attr_element(execute,"APPID",appid)
121 def configure_function(self, target, value, path):
122 """ Build a Configure Message
124 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
126 :param value: guid of the RM
128 :param path: Path of the element to configure (ex : net/w0/channel)
132 payload = ET.Element("omf-message")
133 config = self._id_element(payload, "CONFIGURE")
134 sliceid = self._attr_element(config,"SLICEID",self._slice_id)
135 expid = self._attr_element(config,"EXPID",self._exp_id)
136 target = self._attr_element(config,"TARGET",target)
137 value = self._attr_element(config,"VALUE",value)
138 path = self._attr_element(config,"PATH",path)
141 def log_function(self,level, logger, level_name, data):
142 """ Build a Log Message
144 :param level: Level of logging
146 :param logger: Element publishing the log
148 :param level_name: Name of the level (ex : INFO)
149 :type level_name: str
150 :param data: Content to publish
154 payload = ET.Element("omf-message")
155 log = self._id_element(payload, "LOGGING")
156 level = self._attr_element(log,"LEVEL",level)
157 sliceid = self._attr_element(log,"SLICEID",self._slice_id)
158 logger = self._attr_element(log,"LOGGER",logger)
159 expid = self._attr_element(log,"EXPID",self._exp_id)
160 level_name = self._attr_element(log,"LEVEL_NAME",level_name)
161 data = self._attr_element(log,"DATA",data)
164 def alias_function(self, name, target):
165 """ Build a Alias Message
167 :param name: Name of the new alias
169 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
173 payload = ET.Element("omf-message")
174 alias = self._id_element(payload,"ALIAS")
175 sliceid = self._attr_element(alias,"SLICEID",self._slice_id)
176 expid = self._attr_element(alias,"EXPID",self._exp_id)
177 name = self._attr_element(alias,"NAME",name)
178 target = self._attr_element(alias,"TARGET",target)
181 def enroll_function(self, enrollkey, image, index, target ):
182 """ Build an Enroll Message
184 :param enrollkey: Type of enrollment (= 1)
186 :param image: Image (= * when all the nodes are concerned)
188 :param index: Index (= 1 in general)
190 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
194 payload = ET.Element("omf-message")
195 enroll = self._id_element(payload,"ENROLL")
196 enrollkey = self._attr_element(enroll,"ENROLLKEY",enrollkey)
197 sliceid = self._attr_element(enroll,"SLICEID",self._slice_id)
198 image = self._attr_element(enroll,"IMAGE",image)
199 expid = self._attr_element(enroll,"EXPID",self._exp_id)
200 index = self._attr_element(enroll,"INDEX",index)
201 target = self._attr_element(enroll,"TARGET",target)
204 def noop_function(self,target):
205 """ Build a Noop Message
207 :param target: Hrn of the target node (ex : omf.plexus.wlab17)
211 payload = ET.Element("omf-message")
212 noop = self._id_element(payload,"NOOP")
213 sliceid = self._attr_element(noop,"SLICEID",self._slice_id)
214 expid = self._attr_element(noop,"EXPID",self._exp_id)
215 target = self._attr_element(noop,"TARGET",target)
218 def newexp_function(self, experimentid, address):
219 """ Build a NewExp Message
221 :param experimentid: Id of the new experiment
222 :type experimentid: str
223 :param address: Adress of the destination set of nodes
227 payload = ET.Element("omf-message")
228 newexp = self._id_element(payload,"EXPERIMENT_NEW")
229 experimentid = self._attr_element(newexp,"EXPERIMENT_ID",experimentid)
230 sliceid = self._attr_element(newexp,"SLICEID",self._slice_id)
231 expid = self._attr_element(newexp,"EXPID",self._exp_id)
232 address = self._attr_element(newexp,"ADDRESS",address)