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