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.omf_resource import ResourceGateway, OMFResource
26 from nepi.resources.omf.omf_api_factory import OMFAPIFactory
30 class OMFChannel(OMFResource):
32 .. class:: Class Args :
34 :param ec: The Experiment controller
35 :type ec: ExperimentController
36 :param guid: guid of the RM
38 :param creds: Credentials to communicate with the rm (XmppClient for OMF)
43 _authorized_connections = ["OMFWifiInterface", "OMFNode"]
45 ChannelToFreq = dict({
62 def _register_attributes(cls):
63 """Register the attributes of an OMF channel
66 channel = Attribute("channel", "Name of the application")
67 cls._register_attribute(channel)
69 def __init__(self, ec, guid):
71 :param ec: The Experiment controller
72 :type ec: ExperimentController
73 :param guid: guid of the RM
77 super(OMFChannel, self).__init__(ec, guid)
79 self._nodes_guid = list()
88 def valid_connection(self, guid):
89 """ Check if the connection with the guid in parameter is possible.
90 Only meaningful connections are allowed.
92 :param guid: Guid of the current RM
97 rm = self.ec.get_resource(guid)
98 if rm.get_rtype() in self._authorized_connections:
99 msg = "Connection between %s %s and %s %s accepted" % (
100 self.get_rtype(), self._guid, rm.get_rtype(), guid)
103 msg = "Connection between %s %s and %s %s refused" % (
104 self.get_rtype(), self._guid, rm.get_rtype(), guid)
108 def _get_target(self, conn_set):
110 Get the couples (host, interface) that uses this channel
112 :param conn_set: Connections of the current Guid
115 :return: self._nodes_guid
120 rm_iface = self.ec.get_resource(elt)
121 for conn in rm_iface.connections:
122 rm_node = self.ec.get_resource(conn)
123 if rm_node.get_rtype() == "OMFNode" and rm_node.get('hostname'):
124 if rm_iface.state < ResourceState.PROVISIONED or \
125 rm_node.state < ResourceState.READY:
127 couple = [rm_node.get('hostname'), rm_iface.alias]
131 def get_frequency(self, channel):
132 """ Returns the frequency of a specific channel number
135 return OMFChannel.ChannelToFreq[channel]
138 """ Deploy the RM. It means : Get the xmpp client and send messages
139 using OMF 5.4 or 6 protocol to configure the channel.
142 if not self.get('channel'):
143 msg = "Channel's value is not initialized"
145 raise RuntimeError, msg
147 if self.get('version') == "6":
148 self.frequency = self.get_frequency(self.get('channel'))
149 super(OMFChannel, self).do_deploy()
153 if not self.get('xmppServer'):
154 msg = "XmppServer is not initialzed. XMPP Connections impossible"
156 raise RuntimeError, msg
158 if not (self.get('xmppUser') or self.get('xmppPort')
159 or self.get('xmppPassword')):
160 msg = "Credentials are not all initialzed. Default values will be used"
163 if not self._omf_api :
164 self._omf_api = OMFAPIFactory.get_api(self.get('version'),
165 self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
166 self.get('xmppPassword'), exp_id = self.exp_id)
168 self._nodes_guid = self._get_target(self._connections)
172 if self._nodes_guid == "reschedule" :
173 self.ec.schedule("1s", self.deploy)
175 for couple in self._nodes_guid:
176 attrval = self.get('channel')
177 attrname = "net/%s/%s" % (couple[1], 'channel')
178 self._omf_api.configure(couple[0], attrname, attrval)
180 super(OMFChannel, self).do_deploy()
182 def do_release(self):
183 """ Clean the RM at the end of the experiment and release the API
187 OMFAPIFactory.release_api(self.get('version'),
188 self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
189 self.get('xmppPassword'), exp_id = self.exp_id)
191 super(OMFChannel, self).do_release()