README moves to markdown
[nepi.git] / nepi / resources / omf / messages_6.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):
38         """
39         
40         """
41         pass
42
43     def _type_element(self, type_elt, xmlns, msg_id):
44         """ Insert a markup element with an id
45
46         """
47         elt = ET.Element(type_elt)
48         elt.set("xmlns", xmlns)
49         elt.set("mid", msg_id)
50         return elt
51
52     
53
54     def _attr_element(self, parent, markup, text, type_key=None, type_value = None):
55         """ Insert a markup element with a text (value)
56
57         :param parent: Parent element in an XML point of view
58         :type parent: ElementTree Element
59         :param markup: Name of the markup
60         :type markup: str
61         :param text: Value of the markup element
62         :type text: str
63
64         """
65         elt = ET.SubElement(parent, markup)
66         if type_key and type_value:
67             elt.set(type_key, type_value)
68         elt.text = text
69         return elt
70
71     def _id_element(self, parent, markup, key, value):
72         """ Insert a markup element with a text (value)
73
74         :param parent: Parent element in an XML point of view
75         :type parent: ElementTree Element
76         :param markup: Name of the markup
77         :type markup: str
78         :param text: Value of the markup element
79         :type text: str
80
81         """
82         elt = ET.SubElement(parent, markup)
83         elt.set(key, value)
84         return elt
85
86     def create_function(self, msg_id, src, rtype, timestamp, props = None, guards = None):
87         """ Build a create message
88
89         :param msg_id: Id of the message
90         :type msg_id: str
91         :param src: Src node that send the message (jabber source)
92         :type src: str
93         :param rtype: Type of the object
94         :type rtype: str
95         :param timestamp: Unix Timestamp
96         :type timestamp: str
97         :param props: List of properties
98         :type props: list
99         :param guards: list of guards (assertions for properties)
100         :type guards: list
101         """
102         payload = self._type_element("create", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
103         self._attr_element(payload,"src",src)
104         self._attr_element(payload,"ts",timestamp)
105         self._attr_element(payload,"rtype",rtype)
106
107         if props :
108             if rtype == "application" :
109                 properties = self._id_element(payload,"props","xmlns:application",
110                       "http://schema.mytestbed.net/omf/6.0/protocol/application")
111             elif rtype == "wlan" :
112                 properties = self._id_element(payload,"props","xmlns:wlan",
113                       "http://schema.mytestbed.net/omf/6.0/protocol/wlan")
114             else:
115                 properties = self._attr_element(payload,"props","")
116
117             for prop in props:
118                 if isinstance(props[prop],str):
119                     self._attr_element(properties,prop,props[prop],type_key="type", type_value = "string")
120                 elif isinstance(props[prop],dict):
121                     key = self._attr_element(properties,prop,"",type_key="type", type_value = "hash")
122                     for comp in props[prop]:
123                         self._attr_element(key,comp,props[prop][comp],type_key="type", type_value = "string")
124
125         if guards :
126             guardians = self._attr_element(payload,"guard","")
127             for guard in guards:
128                 self._attr_element(guardians,guard,guards[guard],type_key="type", type_value = "string")
129
130         return payload
131
132     def configure_function(self, msg_id, src, timestamp, props = None, guards = None):
133         """ Build a configure message
134
135         :param msg_id: Id of the message
136         :type msg_id: str
137         :param src: Src node that send the message (jabber source)
138         :type src: str
139         :param timestamp: Unix Timestamp
140         :type timestamp: str
141         :param props: List of properties
142         :type props: list
143         :param guards: list of guards (assertions for properties)
144         :type guards: list
145         """
146         payload = self._type_element("configure", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
147         self._attr_element(payload,"src",src)
148         self._attr_element(payload,"ts",timestamp)
149
150         if props :
151             properties = self._attr_element(payload,"props","")
152             for prop in props:
153                 self._attr_element(properties,prop,props[prop],type_key="type", type_value = "symbol")
154            
155         if guards :
156             guardians = self._attr_element(payload,"guard","")
157             for guard in guards:
158                 self._attr_element(guardians,guard,guards[guard],type_key="type", type_value = "string")
159
160         return payload
161
162     def request_function(self, msg_id, src, timestamp,  props = None, guards = None):
163         """ Build a request message
164
165         :param msg_id: Id of the message
166         :type msg_id: str
167         :param src: Src node that send the message (jabber source)
168         :type src: str
169         :param timestamp: Unix Timestamp
170         :type timestamp: str
171         :param props: List of properties
172         :type props: list
173         :param guards: list of guards (assertions for properties)
174         :type guards: list
175         """
176         payload = self._type_element("request", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
177         self._attr_element(payload,"src",src)
178         self._attr_element(payload,"ts",timestamp)
179
180         if props :
181             properties = self._attr_element(payload,"props","")
182             for prop in list(props.keys()):
183                 self._attr_element(properties,prop,props[prop])
184
185         if guards :
186             guardians = self._attr_element(payload,"guard","")
187             for guard in list(guards.keys()):
188                 self._attr_element(guardians,guard,guards[guard])
189         return payload
190
191 #  For now, we don't need the inform message since it is ht RC that send them.
192
193 #    def inform_function(self, msg_id, src, timestamp, cid, itype):
194 #        """ Build an inform message
195
196 #        :param msg_id: Id of the message
197 #        :type msg_id: str
198 #        :param src: Src node that send the message (jabber source)
199 #        :type src: str
200 #        :param rtype: Type of the object
201 #        :type rtype: str
202 #        :param timestamp: Unix Timestamp
203 #        :type timestamp: str
204 #        :param cid: Id of the orignial message
205 #        :type cid: str
206 #        :param itype: type of the object created
207 #        :type itype: str
208 #        """
209
210 #        payload = self._type_element("inform", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
211 #        sliceid = self._attr_element(payload,"src",src)
212 #        expid = self._attr_element(config,"ts",timestamp)
213 #        target = self._attr_element(config,"cid",cid)
214 #        value = self._attr_element(config,"itype",value)
215 #        path = self._attr_element(config,"properties",path)
216 #        return payload
217
218     def release_function(self, msg_id, src, timestamp, res_id = None, props = None, guards = None):
219         """ Build a release message
220
221         :param msg_id: Id of the message
222         :type msg_id: str
223         :param src: Src node that send the message (jabber source)
224         :type src: str
225         :param timestamp: Unix Timestamp
226         :type timestamp: str
227         :param res_id: Id of the release resource
228         :type res_id: str
229         :param props: List of properties
230         :type props: list
231         :param guards: list of guards (assertions for properties)
232         :type guards: list
233         """
234         payload = self._type_element("release", "http://schema.mytestbed.net/omf/6.0/protocol", msg_id )
235         self._attr_element(payload,"src",src)
236         self._attr_element(payload,"ts",timestamp)
237         if res_id :
238             self._attr_element(payload,"res_id",res_id)
239  
240         if props :
241             properties = self._id_element(payload,"props","xmlns:frcp",
242                       "http://schema.mytestbed.net/omf/6.0/protocol")
243             for prop in list(props.keys()):
244                 self._attr_element(properties,prop,props[prop])
245
246         if guards :
247             guardians = self._attr_element(payload,"guard","")
248             for guard in list(guards.keys()):
249                 self._attr_element(guardians,guard,guards[guard])
250
251         return payload
252