117257f9597fdf22e67218e7ce97417662ca7881
[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 """
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 exit_function(self, target, appid):
105         """ Build an Exit Message
106
107         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
108         :type target: str
109         :param appid: Application id (ex : vlc#1)
110         :type appid: str
111
112         """
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)
119         return payload
120
121     def configure_function(self, target, value, path):
122         """ Build a Configure Message
123
124         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
125         :type target: str
126         :param value: guid of the RM
127         :type value: int
128         :param path: Path of the element to configure (ex : net/w0/channel)
129         :type path: dict
130
131         """
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)
139         return payload
140
141     def log_function(self,level, logger, level_name, data):
142         """ Build a Log Message
143
144         :param level: Level of logging
145         :type level: str
146         :param logger: Element publishing the log
147         :type logger: str
148         :param level_name: Name of the level (ex : INFO)
149         :type level_name: str
150         :param data: Content to publish
151         :type data: str
152
153         """
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)
162         return payload
163
164     def alias_function(self, name, target):
165         """ Build a Alias Message
166
167         :param name: Name of the new alias
168         :type name: str
169         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
170         :type target: str
171
172         """
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)
179         return payload
180
181     def enroll_function(self, enrollkey, image, index, target ):
182         """ Build an Enroll Message
183
184         :param enrollkey: Type of enrollment (= 1)
185         :type enrollkey: str
186         :param image: Image (= * when all the nodes are concerned)
187         :type image: str
188         :param index: Index (= 1 in general)
189         :type index: str
190         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
191         :type target: str
192
193         """
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)
202         return payload
203
204     def noop_function(self,target):
205         """ Build a Noop Message
206
207         :param target: Hrn of the target node (ex : omf.plexus.wlab17)
208         :type target: str
209
210         """
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)
216         return payload
217
218     def newexp_function(self, experimentid, address):
219         """ Build a NewExp Message
220
221         :param experimentid: Id of the new experiment
222         :type experimentid: str
223         :param address: Adress of the destination set of nodes
224         :type address: str
225
226         """
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)
233         return payload
234