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, ResourceState, \
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 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 This class is used only by the Experiment Controller through the Resource Factory
47 _authorized_connections = ["OMFWifiInterface", "OMFNode"]
50 def _register_attributes(cls):
51 """Register the attributes of an OMF channel
54 channel = Attribute("channel", "Name of the application")
55 cls._register_attribute(channel)
57 def __init__(self, ec, guid):
59 :param ec: The Experiment controller
60 :type ec: ExperimentController
61 :param guid: guid of the RM
63 :param creds: Credentials to communicate with the rm (XmppClient for OMF)
67 super(OMFChannel, self).__init__(ec, guid)
69 self._nodes_guid = list()
75 if self.ec.exp_id.startswith('exp-'):
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)
90 if rm.rtype() in self._authorized_connections:
91 msg = "Connection between %s %s and %s %s accepted" % (
92 self.rtype(), self._guid, rm.rtype(), guid)
96 msg = "Connection between %s %s and %s %s refused" % (
97 self.rtype(), self._guid, rm.rtype(), guid)
102 def _get_target(self, conn_set):
104 Get the couples (host, interface) that uses this channel
106 :param conn_set: Connections of the current Guid
109 :return: self._nodes_guid
114 rm_iface = self.ec.get_resource(elt)
115 for conn in rm_iface.connections:
116 rm_node = self.ec.get_resource(conn)
117 if rm_node.rtype() == "OMFNode" and rm_node.get('hostname'):
118 if rm_iface.state < ResourceState.PROVISIONED or \
119 rm_node.state < ResourceState.READY:
121 couple = [rm_node.get('hostname'), rm_iface.get('alias')]
127 """ Discover the availables channels
133 """ Provision some availables channels
139 """ Deploy the RM. It means : Get the xmpp client and send messages
140 using OMF 5.4 protocol to configure the channel.
141 It becomes DEPLOYED after sending messages to configure the channel
144 if not self._omf_api :
145 self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'),
146 self.get('xmppHost'), self.get('xmppPort'),
147 self.get('xmppPassword'), exp_id = self.exp_id)
149 if not self._omf_api :
150 msg = "Credentials are not initialzed. XMPP Connections impossible"
155 if not self.get('channel'):
156 msg = "Channel's value is not initialized"
161 self._nodes_guid = self._get_target(self._connections)
162 if self._nodes_guid == "reschedule" :
163 self.ec.schedule("2s", self.deploy)
167 for couple in self._nodes_guid:
168 #print "Couple node/alias : " + couple[0] + " , " + couple[1]
169 attrval = self.get('channel')
170 attrname = "net/%s/%s" % (couple[1], 'channel')
171 self._omf_api.configure(couple[0], attrname, attrval)
172 except AttributeError:
173 msg = "Credentials are not initialzed. XMPP Connections impossible"
178 super(OMFChannel, self).deploy()
181 """ Start the RM. It means nothing special for a channel for now
182 It becomes STARTED as soon as this method starts.
186 super(OMFChannel, self).start()
189 """ Stop the RM. It means nothing special for a channel for now
190 It becomes STOPPED as soon as this method is called
193 super(OMFChannel, self).stop()
196 """ Clean the RM at the end of the experiment and release the API
200 OMFAPIFactory.release_api(self.get('xmppSlice'),
201 self.get('xmppHost'), self.get('xmppPort'),
202 self.get('xmppPassword'), exp_id = self.exp_id)
204 super(OMFChannel, self).release()