Change some comments about OMF RM
[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, \
22         ResourceState, 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     """
42     _rtype = "OMFChannel"
43     _authorized_connections = ["OMFWifiInterface", "OMFNode"]
44
45     @classmethod
46     def _register_attributes(cls):
47         """Register the attributes of an OMF channel
48         
49         """
50         channel = Attribute("channel", "Name of the application")
51         cls._register_attribute(channel)
52
53     def __init__(self, ec, guid):
54         """
55         :param ec: The Experiment controller
56         :type ec: ExperimentController
57         :param guid: guid of the RM
58         :type guid: int
59         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
60         :type creds: dict
61
62         """
63         super(OMFChannel, self).__init__(ec, guid)
64
65         self._nodes_guid = list()
66
67         self._omf_api = None
68
69     @property
70     def exp_id(self):
71         return self.ec.exp_id
72
73     def valid_connection(self, guid):
74         """ Check if the connection with the guid in parameter is possible.
75         Only meaningful connections are allowed.
76
77         :param guid: Guid of the current RM
78         :type guid: int
79         :rtype:  Boolean
80
81         """
82         rm = self.ec.get_resource(guid)
83         
84         if rm.get_rtype() in self._authorized_connections:
85             msg = "Connection between %s %s and %s %s accepted" % (
86                     self.get_rtype(), self._guid, rm.get_rtype(), guid)
87             self.debug(msg)
88             return True
89
90         msg = "Connection between %s %s and %s %s refused" % (
91                 self.get_rtype(), self._guid, rm.get_rtype(), guid)
92         self.debug(msg)
93         
94         return False
95
96     def _get_target(self, conn_set):
97         """
98         Get the couples (host, interface) that uses this channel
99
100         :param conn_set: Connections of the current Guid
101         :type conn_set: set
102         :rtype: list
103         :return: self._nodes_guid
104
105         """
106         res = []
107         for elt in conn_set:
108             rm_iface = self.ec.get_resource(elt)
109             for conn in rm_iface.connections:
110                 rm_node = self.ec.get_resource(conn)
111                 if rm_node.get_rtype() == "OMFNode" and rm_node.get('hostname'):
112                     if rm_iface.state < ResourceState.PROVISIONED or \
113                             rm_node.state < ResourceState.READY:
114                         return "reschedule"
115                     couple = [rm_node.get('hostname'), rm_iface.get('alias')]
116                     #print couple
117                     res.append(couple)
118         return res
119
120     def do_deploy(self):
121         """ Deploy the RM. It means : Get the xmpp client and send messages 
122         using OMF 5.4 protocol to configure the channel.
123         It becomes DEPLOYED after sending messages to configure the channel
124
125         """
126         if not (self.get('xmppSlice') and self.get('xmppHost')
127               and self.get('xmppPort') and self.get('xmppPassword')):
128             msg = "Credentials are not initialzed. XMPP Connections impossible"
129             self.error(msg)
130             raise RuntimeError, msg
131
132         if not self._omf_api :
133             self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
134                 self.get('xmppHost'), self.get('xmppPort'), 
135                 self.get('xmppPassword'), exp_id = self.exp_id)
136
137         if not self.get('channel'):
138             msg = "Channel's value is not initialized"
139             self.error(msg)
140             raise RuntimeError, msg
141
142         self._nodes_guid = self._get_target(self._connections)
143
144         if self._nodes_guid == "reschedule" :
145             self.ec.schedule("2s", self.deploy)
146         else:
147             for couple in self._nodes_guid:
148                 attrval = self.get('channel')
149                 attrname = "net/%s/%s" % (couple[1], 'channel')
150                 self._omf_api.configure(couple[0], attrname, attrval)
151
152             super(OMFChannel, self).do_deploy()
153
154     def do_release(self):
155         """ Clean the RM at the end of the experiment and release the API
156
157         """
158         if self._omf_api :
159             OMFAPIFactory.release_api(self.get('xmppSlice'), 
160                 self.get('xmppHost'), self.get('xmppPort'), 
161                 self.get('xmppPassword'), exp_id = self.exp_id)
162
163         super(OMFChannel, self).do_release()
164