4e1ae2d916a1176f74a8fbee2b834a9159a42992
[nepi.git] / src / nepi / resources / omf / node.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.util.timefuncs import tnow
22 from nepi.execution.resource import ResourceManager, clsinit_copy, \
23         ResourceState, reschedule_delay
24 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
27
28 import time
29
30 reschedule_check = "0.5s"
31 confirmation_counter = 2000
32
33 @clsinit_copy
34 class OMFNode(OMFResource):
35     """
36     .. class:: Class Args :
37       
38         :param ec: The Experiment controller
39         :type ec: ExperimentController
40         :param guid: guid of the RM
41         :type guid: int
42         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
43         :type creds: dict
44
45     """
46     _rtype = "OMFNode"
47     _authorized_connections = ["OMFApplication" , "OMFWifiInterface"]
48
49     @classmethod
50     def _register_attributes(cls):
51         """Register the attributes of an OMF Node
52
53         """
54         hostname = Attribute("hostname", "Hostname of the machine")
55
56         cls._register_attribute(hostname)
57
58     # XXX: We don't necessary need to have the credentials at the 
59     # moment we create the RM
60     def __init__(self, ec, guid):
61         """
62         :param ec: The Experiment controller
63         :type ec: ExperimentController
64         :param guid: guid of the RM
65         :type guid: int
66
67         """
68         super(OMFNode, self).__init__(ec, guid)
69
70         self._omf_api = None 
71
72         # For performance tests
73         self.perf = True
74         self.begin_deploy_time = None
75
76
77     @property
78     def exp_id(self):
79         return self.ec.exp_id
80
81     def valid_connection(self, guid):
82         """ Check if the connection with the guid in parameter is possible. 
83         Only meaningful connections are allowed.
84
85         :param guid: Guid of the current RM
86         :type guid: int
87         :rtype:  Boolean
88
89         """
90         rm = self.ec.get_resource(guid)
91         if rm.get_rtype() in self._authorized_connections:
92             msg = "Connection between %s %s and %s %s accepted" % (
93                     self.get_rtype(), self._guid, rm.get_rtype(), guid)
94             self.debug(msg)
95             return True
96
97         msg = "Connection between %s %s and %s %s refused" % (
98                 self.get_rtype(), self._guid, rm.get_rtype(), guid)
99         self.error(msg)
100
101         return False
102
103     def do_deploy(self):
104         """ Deploy the RM. It means : Send Xmpp Message Using OMF protocol 
105             to enroll the node into the experiment.
106
107         """ 
108       ## For performance test
109         if self.perf:
110             self.begin_deploy_time = tnow()
111             self.perf = False
112
113         if not self.get('xmppServer'):
114             msg = "XmppServer is not initialzed. XMPP Connections impossible"
115             self.error(msg)
116             raise RuntimeError, msg
117
118         if not self.get('version'):
119             msg = "Version of OMF is not indicated"
120             self.error(msg)
121             raise RuntimeError, msg
122
123         if not (self.get('xmppUser') or self.get('xmppPort') 
124                    or self.get('xmppPassword')):
125             msg = "Credentials are not all initialzed. Default values will be used"
126             self.warn(msg)
127
128         if not self._omf_api :
129             self._omf_api = OMFAPIFactory.get_api(self.get('version'), 
130               self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
131                self.get('xmppPassword'), exp_id = self.exp_id)
132
133         if not self.get('hostname') :
134             msg = "Hostname's value is not initialized"
135             self.error(msg)
136             raise RuntimeError, msg
137
138         if self.get('version') == "5":
139             self._omf_api.enroll_host(self.get('hostname'))
140         else:
141             self._omf_api.enroll_topic(self.get('hostname'))
142
143         super(OMFNode, self).do_deploy()
144
145     def do_release(self):
146         """ Clean the RM at the end of the experiment by unenrolling
147         the node from the topic
148
149         """
150         from nepi.resources.omf.application import OMFApplication
151         rm_list = self.get_connected(OMFApplication.get_rtype())
152         if rm_list:
153             for rm in rm_list:
154                 if rm.state < ResourceState.RELEASED:
155                     self.ec.schedule(reschedule_delay, self.release)
156                     return 
157
158         from nepi.resources.omf.interface import OMFWifiInterface
159         rm_list = self.get_connected(OMFWifiInterface.get_rtype())
160         if rm_list:
161             for rm in rm_list:
162                 if rm.state < ResourceState.RELEASED:
163                     self.ec.schedule(reschedule_delay, self.release)
164                     return 
165
166         if self._omf_api:
167             if self.get('version') == "5":
168                 self._omf_api.release(self.get('hostname'))
169             else:
170                 self._omf_api.unenroll_topic(self.get('hostname'))
171
172             OMFAPIFactory.release_api(self.get('version'), 
173               self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
174                self.get('xmppPassword'), exp_id = self.exp_id)
175
176         super(OMFNode, self).do_release()
177