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