README moves to markdown
[nepi.git] / src / nepi / resources / omf / messages_5_4.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
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;
8 #
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.
13 #
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/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18 #         Julien Tribino <julien.tribino@inria.fr>
19
20 from xml.etree import cElementTree as ET
21
22 class MessageHandler():
23     """
24     .. class:: Class Args :
25       
26         :param sliceid: Slice Name (= Xmpp Slice)
27         :type expid: str
28         :param expid: Experiment ID (= Xmpp User)
29         :type expid: str
30
31     .. note::
32
33        This class is used only for OMF 5.4 Protocol and is going to become unused
34
35     """
36
37     def __init__(self, sliceid, expid ):
38         """
39
40         :param sliceid: Slice Name (= Xmpp Slice)
41         :type expid: str
42         :param expid: Experiment ID (= Xmpp User)
43         :type expid: str
44
45         """
46         self._slice_id = sliceid
47         self._exp_id = expid
48
49
50     def _id_element(self, parent, markup):
51         """ Insert a markup element with an id
52
53         :param parent: Parent element in an XML point of view
54         :type parent: ElementTree Element
55         :param markup: Name of the markup
56         :type markup: str
57
58         """
59         elt = ET.SubElement(parent, markup)
60         elt.set("id", "\'omf-payload\'")
61         return elt
62
63     def _attr_element(self, parent, markup, text):
64         """ Insert a markup element with a text (value)
65
66         :param parent: Parent element in an XML point of view
67         :type parent: ElementTree Element
68         :param markup: Name of the markup
69         :type markup: str
70         :param text: Value of the markup element
71         :type text: str
72
73         """
74         elt = ET.SubElement(parent, markup)
75         elt.text = text
76         return elt
77
78     def execute_function(self, target, appid, cmdlineargs, path, env):
79         """ Build an Execute Message
80
81         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
82         :type target: str
83         :param appid: Application id
84         :type appid: str
85         :param cmdlineargs: Arguments of the application
86         :type cmdlineargs: str
87         :param path: Path of the application
88         :type path: str
89         :param env: Environment variables
90         :type env: str
91
92         """
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)
102         return payload
103
104     def stdin_function(self, target, value, appid):
105         """ Build an Execute Message
106
107         :param value: parameter that go in the stdin
108         :type value: str
109         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
110         :type target: str
111         :param appid: Application id
112         :type appid: str
113
114         """
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)
122         return payload
123
124     def exit_function(self, target, appid):
125         """ Build an Exit Message
126
127         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
128         :type target: str
129         :param appid: Application id (ex : vlc#1)
130         :type appid: str
131
132         """
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)
139         return payload
140
141     def configure_function(self, target, value, path):
142         """ Build a Configure Message
143
144         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
145         :type target: str
146         :param value: guid of the RM
147         :type value: int
148         :param path: Path of the element to configure (ex : net/w0/channel)
149         :type path: dict
150
151         """
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)
159         return payload
160
161     def log_function(self,level, logger, level_name, data):
162         """ Build a Log Message
163
164         :param level: Level of logging
165         :type level: str
166         :param logger: Element publishing the log
167         :type logger: str
168         :param level_name: Name of the level (ex : INFO)
169         :type level_name: str
170         :param data: Content to publish
171         :type data: str
172
173         """
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)
182         return payload
183
184     def alias_function(self, name, target):
185         """ Build an Alias Message
186
187         :param name: Name of the new alias
188         :type name: str
189         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
190         :type target: str
191
192         """
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)
199         return payload
200
201     def enroll_function(self, enrollkey, image, index, target ):
202         """ Build an Enroll Message
203
204         :param enrollkey: Type of enrollment (= 1)
205         :type enrollkey: str
206         :param image: Image (= * when all the nodes are concerned)
207         :type image: str
208         :param index: Index (= 1 in general)
209         :type index: str
210         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
211         :type target: str
212
213         """
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)
222         return payload
223
224     def noop_function(self,target):
225         """ Build a Noop Message
226
227         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
228         :type target: str
229
230         """
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)
236         return payload
237
238     def newexp_function(self, experimentid, address):
239         """ Build a NewExp Message
240
241         :param experimentid: Id of the new experiment
242         :type experimentid: str
243         :param address: Adress of the destination set of nodes
244         :type address: str
245
246         """
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)
253         return payload
254