f8709bf4fa7137b1cd3ed74641a91141ab460b94
[nepi.git] / src / nepi / resources / omf / interface.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 nepi.execution.resource import ResourceManager, clsinit_copy, \
22         ResourceState, reschedule_delay
23 from nepi.execution.attribute import Attribute, Flags 
24
25 from nepi.resources.omf.node import OMFNode
26 from nepi.resources.omf.omf_resource import ResourceGateway, OMFResource
27 from nepi.resources.omf.channel import OMFChannel
28 from nepi.resources.omf.omf_api import OMFAPIFactory
29
30 @clsinit_copy
31 class OMFWifiInterface(OMFResource):
32     """
33     .. class:: Class Args :
34       
35         :param ec: The Experiment controller
36         :type ec: ExperimentController
37         :param guid: guid of the RM
38         :type guid: int
39         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
40         :type creds: dict
41
42     """
43     _rtype = "OMFWifiInterface"
44     _authorized_connections = ["OMFNode" , "OMFChannel"]
45
46     #alias2name = dict({'w0':'wlan0', 'w1':'wlan1'})
47
48     @classmethod
49     def _register_attributes(cls):
50         """Register the attributes of an OMF interface 
51
52         """
53         alias = Attribute("alias","Alias of the interface", default = "w0")
54         mode = Attribute("mode","Mode of the interface")
55         type = Attribute("type","Type of the interface")
56         essid = Attribute("essid","Essid of the interface")
57         ip = Attribute("ip","IP of the interface")
58         cls._register_attribute(alias)
59         cls._register_attribute(mode)
60         cls._register_attribute(type)
61         cls._register_attribute(essid)
62         cls._register_attribute(ip)
63
64     def __init__(self, ec, guid):
65         """
66         :param ec: The Experiment controller
67         :type ec: ExperimentController
68         :param guid: guid of the RM
69         :type guid: int
70         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
71         :type creds: dict
72
73         """
74         super(OMFWifiInterface, self).__init__(ec, guid)
75
76         self._conf = False
77
78         self._omf_api = None
79         self._alias = self.get('alias')
80
81     def valid_connection(self, guid):
82         """ Check if the connection with the guid in parameter is possible. 
83         Only meaningful connections are allowed.
84
85         :param guid: Guid of the current RM
86         :type guid: int
87         :rtype:  Boolean
88
89         """
90         rm = self.ec.get_resource(guid)
91         if rm.get_rtype() in self._authorized_connections:
92             msg = "Connection between %s %s and %s %s accepted" % \
93                 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
94             self.debug(msg)
95
96             return True
97
98         msg = "Connection between %s %s and %s %s refused" % \
99              (self.get_rtype(), self._guid, rm.get_rtype(), guid)
100         self.debug(msg)
101
102         return False
103
104     @property
105     def exp_id(self):
106         return self.ec.exp_id
107
108     @property
109     def node(self):
110         rm_list = self.get_connected(OMFNode.get_rtype())
111         if rm_list: return rm_list[0]
112         return None
113
114     @property
115     def channel(self):
116         rm_list = self.get_connected(OMFChannel.get_rtype())
117         if rm_list: return rm_list[0]
118         return None
119
120     def configure_iface(self):
121         """ Configure the interface without the ip
122
123         """
124         if self.node.state < ResourceState.READY:
125             self.ec.schedule(reschedule_delay, self.deploy)
126             return False
127
128         for attrname in ["mode", "type", "essid"]:
129             attrval = self.get(attrname)
130             attrname = "net/%s/%s" % (self._alias, attrname)
131             self._omf_api.configure(self.node.get('hostname'), attrname, 
132                         attrval)
133         
134         super(OMFWifiInterface, self).do_provision()
135         return True
136
137     def configure_ip(self):
138         """ Configure the ip of the interface
139
140         """
141         if self.channel.state < ResourceState.READY:
142             self.ec.schedule(reschedule_delay, self.deploy)
143             return False
144
145         attrval = self.get("ip")
146         attrname = "net/%s/%s" % (self._alias, "ip")
147         self._omf_api.configure(self.node.get('hostname'), attrname, 
148                     attrval)
149
150         return True
151
152     def do_deploy(self):
153         """ Deploy the RM. It means : Get the xmpp client and send messages 
154         using OMF 5.4 protocol to configure the interface.
155         It becomes DEPLOYED after sending messages to configure the interface
156         """
157         self.set('xmppSlice',self.node.get('xmppSlice'))
158         self.set('xmppHost',self.node.get('xmppHost'))
159         self.set('xmppPort',self.node.get('xmppPort'))
160         self.set('xmppPassword',self.node.get('xmppPassword'))
161
162         if not (self.get('xmppSlice') and self.get('xmppHost')
163               and self.get('xmppPort') and self.get('xmppPassword')):
164             msg = "Credentials are not initialzed. XMPP Connections impossible"
165             self.error(msg)
166             raise RuntimeError, msg
167
168         if not self._omf_api :
169             self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
170                 self.get('xmppHost'), self.get('xmppPort'), 
171                 self.get('xmppPassword'), exp_id = self.exp_id)
172
173         if not (self.get('mode') and self.get('type') and self.get('essid') \
174                 and self.get('ip')):
175             msg = "Interface's variable are not initialized"
176             self.error(msg)
177             raise RuntimeError, msg
178
179         if not self.node.get('hostname') :
180             msg = "The channel is connected with an undefined node"
181             self.error(msg)
182             raise RuntimeError, msg
183
184         # Just for information
185         self.debug(" " + self.get_rtype() + " ( Guid : " + str(self._guid) +") : " + \
186             self.get('mode') + " : " + self.get('type') + " : " + \
187             self.get('essid') + " : " + self.get('ip'))
188     
189         # Check if the node is already deployed
190         if self.state < ResourceState.PROVISIONED:
191             if self._conf == False:
192                 self._conf = self.configure_iface()
193         if self._conf == True:
194             self.configure_ip()
195
196         super(OMFWifiInterface, self).do_deploy()
197
198     def do_release(self):
199         """ Clean the RM at the end of the experiment and release the API
200
201         """
202         if self._omf_api:
203             OMFAPIFactory.release_api(self.get('xmppSlice'), 
204                 self.get('xmppHost'), self.get('xmppPort'), 
205                 self.get('xmppPassword'), exp_id = self.exp_id)
206
207         super(OMFWifiInterface, self).do_release()
208