Adding authors and correcting licence information
[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 as published by
7 #    the Free Software Foundation, either version 3 of the License, or
8 #    (at your option) any later version.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 #         Julien Tribino <julien.tribino@inria.fr>
20
21 from xml.etree import cElementTree as ET
22
23 class MessageHandler():
24     """
25     .. class:: Class Args :
26       
27         :param sliceid: Slice Name (= Xmpp Slice)
28         :type expid: Str
29         :param expid: Experiment ID (= Xmpp User)
30         :type expid: Str
31
32     .. note::
33
34        This class is used only for OMF 5.4 Protocol and is going to become unused
35
36     """
37
38     def __init__(self, sliceid, expid ):
39         """
40
41         :param sliceid: Slice Name (= Xmpp Slice)
42         :type expid: Str
43         :param expid: Experiment ID (= Xmpp User)
44         :type expid: Str
45
46         """
47         self._slice_id = sliceid
48         self._exp_id = expid
49
50
51     def _id_element(self, parent, markup):
52         """ Insert a markup element with an id
53
54         :param parent: Parent element in an XML point of view
55         :type parent: ElementTree Element
56         :param markup: Name of the markup
57         :type markup: str
58
59         """
60         elt = ET.SubElement(parent, markup)
61         elt.set("id", "\'omf-payload\'")
62         return elt
63
64     def _attr_element(self, parent, markup, text):
65         """ Insert a markup element with a text (value)
66
67         :param parent: Parent element in an XML point of view
68         :type parent: ElementTree Element
69         :param markup: Name of the markup
70         :type markup: str
71         :param text: Value of the markup element
72         :type text: str
73
74         """
75         elt = ET.SubElement(parent, markup)
76         elt.text = text
77         return elt
78
79     def execute_function(self, target, appid, cmdlineargs, path, env):
80         """ Build an Execute Message
81
82         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
83         :type target: str
84         :param appid: Application id
85         :type appid: str
86         :param cmdlineargs: Arguments of the application
87         :type cmdlineargs: str
88         :param path: Path of the application
89         :type path: str
90         :param env: Environment variables
91         :type env: str
92
93         """
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)
103         return payload
104
105     def exit_function(self, target, appid):
106         """ Build an Exit Message
107
108         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
109         :type target: str
110         :param appid: Application id (ex : vlc#1)
111         :type appid: str
112
113         """
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)
120         return payload
121
122     def configure_function(self, target, value, path):
123         """ Build a Configure Message
124
125         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
126         :type target: str
127         :param value: guid of the RM
128         :type value: int
129         :param path: Path of the element to configure (ex : net/w0/channel)
130         :type path: dict
131
132         """
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)
140         return payload
141
142     def log_function(self,level, logger, level_name, data):
143         """ Build a Log Message
144
145         :param level: Level of logging
146         :type level: str
147         :param logger: Element publishing the log
148         :type logger: str
149         :param level_name: Name of the level (ex : INFO)
150         :type level_name: str
151         :param data: Content to publish
152         :type data: str
153
154         """
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)
163         return payload
164
165     def alias_function(self, name, target):
166         """ Build a Alias Message
167
168         :param name: Name of the new alias
169         :type name: str
170         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
171         :type target: str
172
173         """
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)
180         return payload
181
182     def enroll_function(self, enrollkey, image, index, target ):
183         """ Build an Enroll Message
184
185         :param enrollkey: Type of enrollment (= 1)
186         :type enrollkey: str
187         :param image: Image (= * when all the nodes are concerned)
188         :type image: str
189         :param index: Index (= 1 in general)
190         :type index: str
191         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
192         :type target: str
193
194         """
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)
203         return payload
204
205     def noop_function(self,target):
206         """ Build a Noop Message
207
208         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
209         :type target: str
210
211         """
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)
217         return payload
218
219     def newexp_function(self, experimentid, address):
220         """ Build a NewExp Message
221
222         :param experimentid: Id of the new experiment
223         :type experimentid: str
224         :param address: Adress of the destination set of nodes
225         :type address: str
226
227         """
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)
234         return payload
235