Modified FailureManager to abort only when critical resources fail
[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, \
23         ResourceState, reschedule_delay, failtrap
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 @clsinit_copy
31 class OMFNode(OMFResource):
32     """
33     .. class:: Class Args :
34       
35         :param ec: The Experiment controller
36         :type ec: ExperimentController
37         :param guid: guid of the RM
38         :type guid: int
39         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
40         :type creds: dict
41
42     .. note::
43
44        This class is used only by the Experiment Controller through the Resource Factory
45
46     """
47     _rtype = "OMFNode"
48     _authorized_connections = ["OMFApplication" , "OMFWifiInterface"]
49
50     @classmethod
51     def _register_attributes(cls):
52         """Register the attributes of an OMF Node
53
54         """
55         hostname = Attribute("hostname", "Hostname of the machine")
56
57         cls._register_attribute(hostname)
58
59     # XXX: We don't necessary need to have the credentials at the 
60     # moment we create the RM
61     def __init__(self, ec, guid):
62         """
63         :param ec: The Experiment controller
64         :type ec: ExperimentController
65         :param guid: guid of the RM
66         :type guid: int
67
68         """
69         super(OMFNode, self).__init__(ec, guid)
70
71         self._omf_api = None 
72
73     @property
74     def exp_id(self):
75         return self.ec.exp_id
76
77     def valid_connection(self, guid):
78         """ Check if the connection with the guid in parameter is possible. 
79         Only meaningful connections are allowed.
80
81         :param guid: Guid of the current RM
82         :type guid: int
83         :rtype:  Boolean
84
85         """
86         rm = self.ec.get_resource(guid)
87         if rm.rtype() in self._authorized_connections:
88             msg = "Connection between %s %s and %s %s accepted" % (
89                     self.rtype(), self._guid, rm.rtype(), guid)
90             self.debug(msg)
91
92             return True
93
94         msg = "Connection between %s %s and %s %s refused" % (
95                 self.rtype(), self._guid, rm.rtype(), guid)
96         self.debug(msg)
97
98         return False
99
100     @failtrap
101     def deploy(self):
102         """ Deploy the RM. It means : Send Xmpp Message Using OMF protocol 
103             to enroll the node into the experiment.
104             It becomes DEPLOYED after sending messages to enroll the node
105
106         """ 
107         if not self._omf_api :
108             self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
109                 self.get('xmppHost'), self.get('xmppPort'), 
110                 self.get('xmppPassword'), exp_id = self.exp_id)
111
112         if not self._omf_api :
113             msg = "Credentials are not initialzed. XMPP Connections impossible"
114             self.error(msg)
115             raise RuntimeError, msg
116
117         if not self.get('hostname') :
118             msg = "Hostname's value is not initialized"
119             self.error(msg)
120             raise RuntimeError, msg
121
122         try:
123             self._omf_api.enroll_host(self.get('hostname'))
124         except AttributeError:
125             msg = "Credentials are not initialzed. XMPP Connections impossible"
126             self.error(msg)
127             raise 
128
129         super(OMFNode, self).deploy()
130
131     def release(self):
132         """Clean the RM at the end of the experiment
133
134         """
135         try:
136             if self._omf_api :
137                 self._omf_api.release(self.get('hostname'))
138
139                 OMFAPIFactory.release_api(self.get('xmppSlice'), 
140                     self.get('xmppHost'), self.get('xmppPort'), 
141                     self.get('xmppPassword'), exp_id = self.exp_id)
142         except:
143             import traceback
144             err = traceback.format_exc()
145             self.error(err)
146
147         super(OMFNode, self).release()
148