update omf part with last changes for the demo
[nepi.git] / src / nepi / resources / omf / channel.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, ResourceState, \
22         reschedule_delay
23 from nepi.execution.attribute import Attribute, Flags 
24
25 from nepi.resources.omf.omf_resource import ResourceGateway, OMFResource
26 from nepi.resources.omf.omf_api import OMFAPIFactory
27
28
29 @clsinit_copy
30 class OMFChannel(OMFResource):
31     """
32     .. class:: Class Args :
33       
34         :param ec: The Experiment controller
35         :type ec: ExperimentController
36         :param guid: guid of the RM
37         :type guid: int
38         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
39         :type creds: dict
40
41     .. note::
42
43        This class is used only by the Experiment Controller through the Resource Factory
44
45     """
46     _rtype = "OMFChannel"
47     _authorized_connections = ["OMFWifiInterface", "OMFNode"]
48
49     @classmethod
50     def _register_attributes(cls):
51         """Register the attributes of an OMF channel
52         
53         """
54         channel = Attribute("channel", "Name of the application")
55         cls._register_attribute(channel)
56
57     def __init__(self, ec, guid):
58         """
59         :param ec: The Experiment controller
60         :type ec: ExperimentController
61         :param guid: guid of the RM
62         :type guid: int
63         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
64         :type creds: dict
65
66         """
67         super(OMFChannel, self).__init__(ec, guid)
68
69         self._nodes_guid = list()
70
71         self._omf_api = None
72
73     @property
74     def exp_id(self):
75         return self.ec.exp_id
76
77     def valid_connection(self, guid):
78         """ Check if the connection with the guid in parameter is possible.
79         Only meaningful connections are allowed.
80
81         :param guid: Guid of the current RM
82         :type guid: int
83         :rtype:  Boolean
84
85         """
86         rm = self.ec.get_resource(guid)
87         
88         if rm.rtype() in self._authorized_connections:
89             msg = "Connection between %s %s and %s %s accepted" % (
90                     self.rtype(), self._guid, rm.rtype(), guid)
91             self.debug(msg)
92             return True
93
94         msg = "Connection between %s %s and %s %s refused" % (
95                 self.rtype(), self._guid, rm.rtype(), guid)
96         self.debug(msg)
97         
98         return False
99
100     def _get_target(self, conn_set):
101         """
102         Get the couples (host, interface) that uses this channel
103
104         :param conn_set: Connections of the current Guid
105         :type conn_set: set
106         :rtype: list
107         :return: self._nodes_guid
108
109         """
110         res = []
111         for elt in conn_set:
112             rm_iface = self.ec.get_resource(elt)
113             for conn in rm_iface.connections:
114                 rm_node = self.ec.get_resource(conn)
115                 if rm_node.rtype() == "OMFNode" and rm_node.get('hostname'):
116                     if rm_iface.state < ResourceState.PROVISIONED or \
117                             rm_node.state < ResourceState.READY:
118                         return "reschedule"
119                     couple = [rm_node.get('hostname'), rm_iface.get('alias')]
120                     #print couple
121                     res.append(couple)
122         return res
123
124     def discover(self):
125         """ Discover the availables channels
126
127         """
128         pass
129      
130     def provision(self):
131         """ Provision some availables channels
132
133         """
134         pass
135
136     def deploy(self):
137         """ Deploy the RM. It means : Get the xmpp client and send messages 
138         using OMF 5.4 protocol to configure the channel.
139         It becomes DEPLOYED after sending messages to configure the channel
140
141         """
142         if not self._omf_api :
143             self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
144                 self.get('xmppHost'), self.get('xmppPort'), 
145                 self.get('xmppPassword'), exp_id = self.exp_id)
146
147         if not self._omf_api :
148             msg = "Credentials are not initialzed. XMPP Connections impossible"
149             self.error(msg)
150             self.fail()
151             return
152
153         if not self.get('channel'):
154             msg = "Channel's value is not initialized"
155             self.error(msg)
156             self.fail()
157             return
158
159         self._nodes_guid = self._get_target(self._connections) 
160         if self._nodes_guid == "reschedule" :
161             self.ec.schedule("2s", self.deploy)
162             return False
163
164         try:
165             for couple in self._nodes_guid:
166                 #print "Couple node/alias : " + couple[0] + "  ,  " + couple[1]
167                 attrval = self.get('channel')
168                 attrname = "net/%s/%s" % (couple[1], 'channel')
169                 self._omf_api.configure(couple[0], attrname, attrval)
170         except AttributeError:
171             msg = "Credentials are not initialzed. XMPP Connections impossible"
172             self.error(msg)
173             self.fail()
174             return
175
176         super(OMFChannel, self).deploy()
177
178     def start(self):
179         """ Start the RM. It means nothing special for a channel for now
180         It becomes STARTED as soon as this method starts.
181
182         """
183
184         super(OMFChannel, self).start()
185
186     def stop(self):
187         """ Stop the RM. It means nothing special for a channel for now
188         It becomes STOPPED as soon as this method is called
189
190         """
191         super(OMFChannel, self).stop()
192         self.set_finished()
193
194     def release(self):
195         """ Clean the RM at the end of the experiment and release the API
196
197         """
198         if self._omf_api :
199             OMFAPIFactory.release_api(self.get('xmppSlice'), 
200                 self.get('xmppHost'), self.get('xmppPort'), 
201                 self.get('xmppPassword'), exp_id = self.exp_id)
202
203         super(OMFChannel, self).release()
204