2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 # Julien Tribino <julien.tribino@inria.fr>
21 from nepi.execution.resource import ResourceManager, clsinit_copy, \
22 ResourceState, reschedule_delay
23 from nepi.execution.attribute import Attribute, Flags
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
31 class OMFWifiInterface(OMFResource):
33 .. class:: Class Args :
35 :param ec: The Experiment controller
36 :type ec: ExperimentController
37 :param guid: guid of the RM
39 :param creds: Credentials to communicate with the rm (XmppClient for OMF)
43 _rtype = "OMFWifiInterface"
44 _authorized_connections = ["OMFNode" , "OMFChannel"]
46 #alias2name = dict({'w0':'wlan0', 'w1':'wlan1'})
49 def _register_attributes(cls):
50 """Register the attributes of an OMF interface
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)
64 def __init__(self, ec, guid):
66 :param ec: The Experiment controller
67 :type ec: ExperimentController
68 :param guid: guid of the RM
70 :param creds: Credentials to communicate with the rm (XmppClient for OMF)
74 super(OMFWifiInterface, self).__init__(ec, guid)
79 self._alias = self.get('alias')
81 def valid_connection(self, guid):
82 """ Check if the connection with the guid in parameter is possible.
83 Only meaningful connections are allowed.
85 :param guid: Guid of the current RM
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)
98 msg = "Connection between %s %s and %s %s refused" % \
99 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
106 return self.ec.exp_id
110 rm_list = self.get_connected(OMFNode.get_rtype())
111 if rm_list: return rm_list[0]
116 rm_list = self.get_connected(OMFChannel.get_rtype())
117 if rm_list: return rm_list[0]
120 def configure_iface(self):
121 """ Configure the interface without the ip
124 if self.node.state < ResourceState.READY:
125 self.ec.schedule(reschedule_delay, self.deploy)
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,
134 super(OMFWifiInterface, self).do_provision()
137 def configure_ip(self):
138 """ Configure the ip of the interface
141 if self.channel.state < ResourceState.READY:
142 self.ec.schedule(reschedule_delay, self.deploy)
145 attrval = self.get("ip")
146 attrname = "net/%s/%s" % (self._alias, "ip")
147 self._omf_api.configure(self.node.get('hostname'), attrname,
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
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'))
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"
166 raise RuntimeError, msg
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)
173 if not (self.get('mode') and self.get('type') and self.get('essid') \
175 msg = "Interface's variable are not initialized"
177 raise RuntimeError, msg
179 if not self.node.get('hostname') :
180 msg = "The channel is connected with an undefined node"
182 raise RuntimeError, msg
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'))
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:
196 super(OMFWifiInterface, self).do_deploy()
198 def do_release(self):
199 """ Clean the RM at the end of the experiment and release the API
203 OMFAPIFactory.release_api(self.get('xmppSlice'),
204 self.get('xmppHost'), self.get('xmppPort'),
205 self.get('xmppPassword'), exp_id = self.exp_id)
207 super(OMFWifiInterface, self).do_release()