add set_hook test and add the OMFResource mother classes
[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
22 from nepi.execution.resource import ResourceManager, clsinit_copy, ResourceState, \
23         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 import OMFAPIFactory
27
28 import time
29
30
31 @clsinit_copy
32 class OMFNode(OMFResource):
33     """
34     .. class:: Class Args :
35       
36         :param ec: The Experiment controller
37         :type ec: ExperimentController
38         :param guid: guid of the RM
39         :type guid: int
40         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
41         :type creds: dict
42
43     .. note::
44
45        This class is used only by the Experiment Controller through the Resource Factory
46
47     """
48     _rtype = "OMFNode"
49     _authorized_connections = ["OMFApplication" , "OMFWifiInterface"]
50
51     @classmethod
52     def _register_attributes(cls):
53         """Register the attributes of an OMF Node
54
55         """
56         hostname = Attribute("hostname", "Hostname of the machine")
57
58         cls._register_attribute(hostname)
59
60     # XXX: We don't necessary need to have the credentials at the 
61     # moment we create the RM
62     def __init__(self, ec, guid):
63         """
64         :param ec: The Experiment controller
65         :type ec: ExperimentController
66         :param guid: guid of the RM
67         :type guid: int
68
69         """
70         super(OMFNode, self).__init__(ec, guid)
71
72         self._omf_api = None 
73
74     @property
75     def exp_id(self):
76         if self.ec.exp_id.startswith('exp-'):
77             return None
78         return self.ec.exp_id
79
80     def valid_connection(self, guid):
81         """ Check if the connection with the guid in parameter is possible. 
82         Only meaningful connections are allowed.
83
84         :param guid: Guid of the current RM
85         :type guid: int
86         :rtype:  Boolean
87
88         """
89         rm = self.ec.get_resource(guid)
90         if rm.rtype() in self._authorized_connections:
91             msg = "Connection between %s %s and %s %s accepted" % (
92                     self.rtype(), self._guid, rm.rtype(), guid)
93             self.debug(msg)
94
95             return True
96
97         msg = "Connection between %s %s and %s %s refused" % (
98                 self.rtype(), self._guid, rm.rtype(), guid)
99         self.debug(msg)
100
101         return False
102
103     def deploy(self):
104         """ Deploy the RM. It means : Send Xmpp Message Using OMF protocol 
105             to enroll the node into the experiment.
106             It becomes DEPLOYED after sending messages to enroll the node
107
108         """ 
109         if not self._omf_api :
110             self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
111                 self.get('xmppHost'), self.get('xmppPort'), 
112                 self.get('xmppPassword'), exp_id = self.exp_id)
113
114         if not self._omf_api :
115             msg = "Credentials are not initialzed. XMPP Connections impossible"
116             self.error(msg)
117             self.fail()
118             return
119
120         if not self.get('hostname') :
121             msg = "Hostname's value is not initialized"
122             self.error(msg)
123             self.fail()
124             return False
125
126         try:
127             self._omf_api.enroll_host(self.get('hostname'))
128         except AttributeError:
129             msg = "Credentials are not initialzed. XMPP Connections impossible"
130             self.error(msg)
131             self.fail()
132             #raise AttributeError, msg
133
134         super(OMFNode, self).deploy()
135
136     def discover(self):
137         """ Discover the availables nodes
138
139         """
140         pass
141      
142     def provision(self):
143         """ Provision some availables nodes
144
145         """
146         pass
147
148     def start(self):
149         """Start the RM. It means nothing special for an interface for now
150            It becomes STARTED as soon as this method starts.
151
152         """
153
154         super(OMFNode, self).start()
155
156     def stop(self):
157         """Stop the RM. It means nothing special for an interface for now
158            It becomes STOPPED as soon as this method stops
159
160         """
161         super(OMFNode, self).stop()
162
163     def release(self):
164         """Clean the RM at the end of the experiment
165
166         """
167         if self._omf_api :
168             self._omf_api.release(self.get('hostname'))
169
170             OMFAPIFactory.release_api(self.get('xmppSlice'), 
171                 self.get('xmppHost'), self.get('xmppPort'), 
172                 self.get('xmppPassword'), exp_id = self.exp_id)
173
174         super(OMFNode, self).release()
175