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
41 _rtype = "OMFWifiInterface"
42 _authorized_connections = ["OMFNode" , "OMFChannel"]
44 #alias2name = dict({'w0':'wlan0', 'w1':'wlan1'})
47 def _register_attributes(cls):
48 """Register the attributes of an OMF interface
51 alias = Attribute("alias","Alias of the interface", default = "w0")
52 mode = Attribute("mode","Mode of the interface")
53 type = Attribute("type","Type of the interface")
54 essid = Attribute("essid","Essid of the interface")
55 ip = Attribute("ip","IP of the interface")
56 cls._register_attribute(alias)
57 cls._register_attribute(mode)
58 cls._register_attribute(type)
59 cls._register_attribute(essid)
60 cls._register_attribute(ip)
62 def __init__(self, ec, guid):
64 :param ec: The Experiment controller
65 :type ec: ExperimentController
66 :param guid: guid of the RM
68 :param creds: Credentials to communicate with the rm (XmppClient for OMF)
72 super(OMFWifiInterface, self).__init__(ec, guid)
77 self._alias = self.get('alias')
79 def valid_connection(self, guid):
80 """ Check if the connection with the guid in parameter is possible.
81 Only meaningful connections are allowed.
83 :param guid: Guid of the current RM
88 rm = self.ec.get_resource(guid)
89 if rm.get_rtype() in self._authorized_connections:
90 msg = "Connection between %s %s and %s %s accepted" % \
91 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
96 msg = "Connection between %s %s and %s %s refused" % \
97 (self.get_rtype(), self._guid, rm.get_rtype(), guid)
104 return self.ec.exp_id
108 rm_list = self.get_connected(OMFNode.get_rtype())
109 if rm_list: return rm_list[0]
114 rm_list = self.get_connected(OMFChannel.get_rtype())
115 if rm_list: return rm_list[0]
118 def configure_iface(self):
119 """ Configure the interface without the ip
122 if self.node.state < ResourceState.READY:
123 self.ec.schedule(reschedule_delay, self.deploy)
126 for attrname in ["mode", "type", "essid"]:
127 attrval = self.get(attrname)
128 attrname = "net/%s/%s" % (self._alias, attrname)
129 self._omf_api.configure(self.node.get('hostname'), attrname,
132 super(OMFWifiInterface, self).do_provision()
135 def configure_ip(self):
136 """ Configure the ip of the interface
139 if self.channel.state < ResourceState.READY:
140 self.ec.schedule(reschedule_delay, self.deploy)
143 attrval = self.get("ip")
144 attrname = "net/%s/%s" % (self._alias, "ip")
145 self._omf_api.configure(self.node.get('hostname'), attrname,
151 """ Deploy the RM. It means : Get the xmpp client and send messages
152 using OMF 5.4 protocol to configure the interface.
153 It becomes DEPLOYED after sending messages to configure the interface
155 self.set('xmppSlice',self.node.get('xmppSlice'))
156 self.set('xmppHost',self.node.get('xmppHost'))
157 self.set('xmppPort',self.node.get('xmppPort'))
158 self.set('xmppPassword',self.node.get('xmppPassword'))
160 if not (self.get('xmppSlice') and self.get('xmppHost')
161 and self.get('xmppPort') and self.get('xmppPassword')):
162 msg = "Credentials are not initialzed. XMPP Connections impossible"
164 raise RuntimeError, msg
166 if not self._omf_api :
167 self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'),
168 self.get('xmppHost'), self.get('xmppPort'),
169 self.get('xmppPassword'), exp_id = self.exp_id)
171 if not (self.get('mode') and self.get('type') and self.get('essid') \
173 msg = "Interface's variable are not initialized"
175 raise RuntimeError, msg
177 if not self.node.get('hostname') :
178 msg = "The channel is connected with an undefined node"
180 raise RuntimeError, msg
182 # Just for information
183 self.debug(" " + self.get_rtype() + " ( Guid : " + str(self._guid) +") : " + \
184 self.get('mode') + " : " + self.get('type') + " : " + \
185 self.get('essid') + " : " + self.get('ip'))
187 # Check if the node is already deployed
188 if self.state < ResourceState.PROVISIONED:
189 if self._conf == False:
190 self._conf = self.configure_iface()
191 if self._conf == True:
194 super(OMFWifiInterface, self).do_deploy()
196 def do_release(self):
197 """ Clean the RM at the end of the experiment and release the API
201 OMFAPIFactory.release_api(self.get('xmppSlice'),
202 self.get('xmppHost'), self.get('xmppPort'),
203 self.get('xmppPassword'), exp_id = self.exp_id)
205 super(OMFWifiInterface, self).do_release()