4229f4403d0f5b4c899480cd54da3348a75e792e
[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 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     .. note::
42
43        This class is used only by the Experiment Controller through the Resource Factory
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     @property
73     def exp_id(self):
74         return self.ec.exp_id
75
76     def valid_connection(self, guid):
77         """ Check if the connection with the guid in parameter is possible. 
78         Only meaningful connections are allowed.
79
80         :param guid: Guid of the current RM
81         :type guid: int
82         :rtype:  Boolean
83
84         """
85         rm = self.ec.get_resource(guid)
86         if rm.rtype() in self._authorized_connections:
87             msg = "Connection between %s %s and %s %s accepted" % (
88                     self.rtype(), self._guid, rm.rtype(), guid)
89             self.debug(msg)
90
91             return True
92
93         msg = "Connection between %s %s and %s %s refused" % (
94                 self.rtype(), self._guid, rm.rtype(), guid)
95         self.debug(msg)
96
97         return False
98
99     def do_deploy(self):
100         """ Deploy the RM. It means : Send Xmpp Message Using OMF protocol 
101             to enroll the node into the experiment.
102             It becomes DEPLOYED after sending messages to enroll the node
103
104         """ 
105         if not self._omf_api :
106             self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
107                 self.get('xmppHost'), self.get('xmppPort'), 
108                 self.get('xmppPassword'), exp_id = self.exp_id)
109
110         if not self._omf_api :
111             msg = "Credentials are not initialzed. XMPP Connections impossible"
112             self.error(msg)
113             raise RuntimeError, msg
114
115         if not self.get('hostname') :
116             msg = "Hostname's value is not initialized"
117             self.error(msg)
118             raise RuntimeError, msg
119
120         try:
121             self._omf_api.enroll_host(self.get('hostname'))
122         except AttributeError:
123             msg = "Credentials are not initialzed. XMPP Connections impossible"
124             self.error(msg)
125             raise
126
127         super(OMFNode, self).do_deploy()
128
129     def do_release(self):
130         """ Clean the RM at the end of the experiment
131
132         """
133         if self._omf_api:
134             self._omf_api.release(self.get('hostname'))
135
136             OMFAPIFactory.release_api(self.get('xmppSlice'), 
137                 self.get('xmppHost'), self.get('xmppPort'), 
138                 self.get('xmppPassword'), exp_id = self.exp_id)
139
140         super(OMFNode, self).do_release()
141