README moves to markdown
[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 version 2 as
7 #    published by the Free Software Foundation;
8 #
9 #    This program is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #    GNU General Public License for more details.
13 #
14 #    You should have received a copy of the GNU General Public License
15 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18 #         Julien Tribino <julien.tribino@inria.fr>
19
20 from nepi.util.timefuncs import tnow
21 from nepi.execution.resource import ResourceManager, clsinit_copy, \
22         ResourceState
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_factory 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 = "omf::Channel"
43     _authorized_connections = ["omf::WifiInterface", "omf::Node"]
44
45     ChannelToFreq = dict({
46              "1" : "2412",
47              "2" : "2417",
48              "3" : "2422",
49              "4" : "2427",
50              "5" : "2432",
51              "6" : "2437",
52              "7" : "2442",
53              "8" : "2447",
54              "9" : "2452",
55              "10" : "2457",
56              "11" : "2462",
57              "12" : "2467",
58              "13" : "2472",
59     })
60
61     @classmethod
62     def _register_attributes(cls):
63         """Register the attributes of an OMF channel
64         
65         """
66         channel = Attribute("channel", "Name of the application")
67         cls._register_attribute(channel)
68
69     def __init__(self, ec, guid):
70         """
71         :param ec: The Experiment controller
72         :type ec: ExperimentController
73         :param guid: guid of the RM
74         :type guid: int
75
76         """
77         super(OMFChannel, self).__init__(ec, guid)
78
79         self._nodes_guid = list()
80         self.frequency = None
81
82         self._omf_api = None
83
84         # For performance tests
85         self.perf = True
86         self.begin_deploy_time = None
87
88
89     @property
90     def exp_id(self):
91         return self.ec.exp_id
92
93     def valid_connection(self, guid):
94         """ Check if the connection with the guid in parameter is possible.
95         Only meaningful connections are allowed.
96
97         :param guid: Guid of the current RM
98         :type guid: int
99         :rtype:  Boolean
100
101         """
102         rm = self.ec.get_resource(guid)
103         if rm.get_rtype() in self._authorized_connections:
104             msg = "Connection between %s %s and %s %s accepted" % (
105                     self.get_rtype(), self._guid, rm.get_rtype(), guid)
106             self.debug(msg)
107             return True
108         msg = "Connection between %s %s and %s %s refused" % (
109                 self.get_rtype(), self._guid, rm.get_rtype(), guid)
110         self.debug(msg)
111         return False
112
113     def _get_target(self, conn_set):
114         """
115         Get the couples (host, interface) that uses this channel
116
117         :param conn_set: Connections of the current Guid
118         :type conn_set: set
119         :rtype: list
120         :return: self._nodes_guid
121
122         """
123         res = []
124         for elt in conn_set:
125             rm_iface = self.ec.get_resource(elt)
126             for conn in rm_iface.connections:
127                 rm_node = self.ec.get_resource(conn)
128                 if rm_node.get_rtype() == "omf::Node" and rm_node.get('hostname'):
129                     if rm_iface.state < ResourceState.PROVISIONED or \
130                             rm_node.state < ResourceState.READY:
131                         return "reschedule"
132                     couple = [rm_node.get('hostname'), rm_iface.alias]
133                     res.append(couple)
134         return res
135
136     def get_frequency(self, channel):
137         """ Returns the frequency of a specific channel number
138
139         """           
140         return OMFChannel.ChannelToFreq[channel]
141
142     def do_deploy(self):
143         """ Deploy the RM. It means : Get the xmpp client and send messages 
144         using OMF 5.4 or 6 protocol to configure the channel.
145
146         """   
147
148       ## For performance test
149         if self.perf:
150             self.begin_deploy_time = tnow()
151             self.perf = False
152
153         if not self.get('channel'):
154             msg = "Channel's value is not initialized"
155             self.error(msg)
156             raise RuntimeError(msg)
157
158         if self.get('version') == "6":
159             self.frequency = self.get_frequency(self.get('channel'))
160             super(OMFChannel, self).do_deploy()
161             return
162
163         if not self.get('xmppServer'):
164             msg = "XmppServer is not initialzed. XMPP Connections impossible"
165             self.error(msg)
166             raise RuntimeError(msg)
167
168         if not (self.get('xmppUser') or self.get('xmppPort') 
169                    or self.get('xmppPassword')):
170             msg = "Credentials are not all initialzed. Default values will be used"
171             self.warn(msg)
172
173         if not self._omf_api :
174             self._omf_api = OMFAPIFactory.get_api(self.get('version'), 
175               self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
176                self.get('xmppPassword'), exp_id = self.exp_id)
177
178         self._nodes_guid = self._get_target(self._connections)
179
180         if self._nodes_guid == "reschedule" :
181             self.ec.schedule("1s", self.deploy)
182         else:
183             for couple in self._nodes_guid:
184                 attrval = self.get('channel')
185                 attrname = "net/%s/%s" % (couple[1], 'channel')
186                 self._omf_api.configure(couple[0], attrname, attrval)
187
188         super(OMFChannel, self).do_deploy()
189
190     def do_release(self):
191         """ Clean the RM at the end of the experiment and release the API
192
193         """
194         if self._omf_api :
195             OMFAPIFactory.release_api(self.get('version'), 
196               self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
197                self.get('xmppPassword'), exp_id = self.exp_id)
198
199         super(OMFChannel, self).do_release()
200