First version of OMF6 working. Just problem of wifi driver are still there
[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 @clsinit_copy
30 class OMFNode(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 = "OMFNode"
43     _authorized_connections = ["OMFApplication" , "OMFWifiInterface"]
44
45     @classmethod
46     def _register_attributes(cls):
47         """Register the attributes of an OMF Node
48
49         """
50         hostname = Attribute("hostname", "Hostname of the machine")
51
52         cls._register_attribute(hostname)
53
54     # XXX: We don't necessary need to have the credentials at the 
55     # moment we create the RM
56     def __init__(self, ec, guid):
57         """
58         :param ec: The Experiment controller
59         :type ec: ExperimentController
60         :param guid: guid of the RM
61         :type guid: int
62
63         """
64         super(OMFNode, self).__init__(ec, guid)
65
66         self._omf_api = None 
67
68     @property
69     def exp_id(self):
70         return self.ec.exp_id
71
72     def valid_connection(self, guid):
73         """ Check if the connection with the guid in parameter is possible. 
74         Only meaningful connections are allowed.
75
76         :param guid: Guid of the current RM
77         :type guid: int
78         :rtype:  Boolean
79
80         """
81         rm = self.ec.get_resource(guid)
82         if rm.get_rtype() in self._authorized_connections:
83             msg = "Connection between %s %s and %s %s accepted" % (
84                     self.get_rtype(), self._guid, rm.get_rtype(), guid)
85             self.debug(msg)
86             return True
87
88         msg = "Connection between %s %s and %s %s refused" % (
89                 self.get_rtype(), self._guid, rm.get_rtype(), guid)
90         self.error(msg)
91
92         return False
93
94     def do_deploy(self):
95         """ Deploy the RM. It means : Send Xmpp Message Using OMF protocol 
96             to enroll the node into the experiment.
97             It becomes DEPLOYED after sending messages to enroll the node
98
99         """ 
100         if not self.get('xmppServer'):
101             msg = "XmppServer is not initialzed. XMPP Connections impossible"
102             self.error(msg)
103             raise RuntimeError, msg
104
105         if not self.get('version'):
106             msg = "Version of OMF is not indicated"
107             self.error(msg)
108             raise RuntimeError, msg
109
110         if not (self.get('xmppUser') or self.get('xmppPort') 
111                    or self.get('xmppPassword')):
112             msg = "Credentials are not all initialzed. Default values will be used"
113             self.warn(msg)
114
115         if not self._omf_api :
116             self._omf_api = OMFAPIFactory.get_api(self.get('version'), 
117               self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
118                self.get('xmppPassword'), exp_id = self.exp_id)
119
120         if not self.get('hostname') :
121             msg = "Hostname's value is not initialized"
122             self.error(msg)
123             raise RuntimeError, msg
124
125         if self.get('version') == "5":
126             self._omf_api.enroll_host(self.get('hostname'))
127         else:
128             self._omf_api.enroll_topic(self.get('hostname'))
129
130         super(OMFNode, self).do_deploy()
131
132     def do_release(self):
133         """ Clean the RM at the end of the experiment
134
135         """
136         from nepi.resources.omf.application import OMFApplication
137         rm_list = self.get_connected(OMFApplication.get_rtype())
138         if rm_list:
139             for rm in rm_list:
140                 if rm.state < ResourceState.RELEASED:
141                     self.ec.schedule(reschedule_delay, self.release)
142                     return 
143
144         if self._omf_api:
145             if self.get('version') == "5":
146                 self._omf_api.release(self.get('hostname'))
147             else:
148                 self._omf_api.unenroll_topic(self.get('hostname'))
149
150             OMFAPIFactory.release_api(self.get('version'), 
151               self.get('xmppServer'), self.get('xmppUser'), self.get('xmppPort'),
152                self.get('xmppPassword'), exp_id = self.exp_id)
153
154         super(OMFNode, self).do_release()
155