ec_shutdown
[nepi.git] / src / nepi / resources / omf / application.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, ResourceState, \
22         reschedule_delay
23 from nepi.execution.attribute import Attribute, Flags 
24 from nepi.resources.omf.node import OMFNode
25 from nepi.resources.omf.omf_api import OMFAPIFactory
26
27
28 @clsinit
29 class OMFApplication(ResourceManager):
30     """
31     .. class:: Class Args :
32       
33         :param ec: The Experiment controller
34         :type ec: ExperimentController
35         :param guid: guid of the RM
36         :type guid: int
37         :param creds: Credentials to communicate with the rm (XmppClient)
38         :type creds: dict
39
40     .. note::
41
42        This class is used only by the Experiment Controller through the 
43        Resource Factory
44
45     """
46     _rtype = "OMFApplication"
47     _authorized_connections = ["OMFNode"]
48
49     @classmethod
50     def _register_attributes(cls):
51         """ Register the attributes of an OMF application
52
53         """
54
55         appid = Attribute("appid", "Name of the application")
56         path = Attribute("path", "Path of the application")
57         args = Attribute("args", "Argument of the application")
58         env = Attribute("env", "Environnement variable of the application")
59         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = Flags.Credential)
60         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = Flags.Credential)
61         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = Flags.Credential)
62         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = Flags.Credential)
63         cls._register_attribute(appid)
64         cls._register_attribute(path)
65         cls._register_attribute(args)
66         cls._register_attribute(env)
67         cls._register_attribute(xmppSlice)
68         cls._register_attribute(xmppHost)
69         cls._register_attribute(xmppPort)
70         cls._register_attribute(xmppPassword)
71
72
73     def __init__(self, ec, guid):
74         """
75         :param ec: The Experiment controller
76         :type ec: ExperimentController
77         :param guid: guid of the RM
78         :type guid: int
79         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
80         :type creds: dict
81
82         """
83         super(OMFApplication, self).__init__(ec, guid)
84
85         self.set('appid', "")
86         self.set('path', "")
87         self.set('args', "")
88         self.set('env', "")
89
90         self._node = None
91
92         self._omf_api = None
93
94     @property
95     def node(self):
96         rm_list = self.get_connected(OMFNode.rtype())
97         if rm_list: return rm_list[0]
98         return None
99
100     def valid_connection(self, guid):
101         """ Check if the connection with the guid in parameter is possible. 
102         Only meaningful connections are allowed.
103
104         :param guid: Guid of RM it will be connected
105         :type guid: int
106         :rtype:  Boolean
107
108         """
109         rm = self.ec.get_resource(guid)
110         if rm.rtype() not in self._authorized_connections:
111             msg = ("Connection between %s %s and %s %s refused: "
112                     "An Application can be connected only to a Node" ) % \
113                 (self.rtype(), self._guid, rm.rtype(), guid)
114             self.debug(msg)
115
116             return False
117
118         elif len(self.connections) != 0 :
119             msg = ("Connection between %s %s and %s %s refused: "
120                     "This Application is already connected" ) % \
121                 (self.rtype(), self._guid, rm.rtype(), guid)
122             self.debug(msg)
123
124             return False
125
126         else :
127             msg = "Connection between %s %s and %s %s accepted" % (
128                     self.rtype(), self._guid, rm.rtype(), guid)
129             self.debug(msg)
130
131             return True
132
133     def deploy(self):
134         """ Deploy the RM. It means nothing special for an application 
135         for now (later it will be upload sources, ...)
136         It becomes DEPLOYED after getting the xmpp client.
137
138         """
139         if not self._omf_api :
140             self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
141                 self.get('xmppHost'), self.get('xmppPort'), 
142                 self.get('xmppPassword'), exp_id = self.ec.exp_id)
143
144         if not self._omf_api :
145             msg = "Credentials are not initialzed. XMPP Connections impossible"
146             self.error(msg)
147             self.fail()
148             return
149
150         super(OMFApplication, self).deploy()
151
152     def start(self):
153         """ Start the RM. It means : Send Xmpp Message Using OMF protocol 
154          to execute the application. 
155          It becomes STARTED before the messages are sent (for coordination)
156
157         """
158         if not (self.get('appid') and self.get('path')) :
159             msg = "Application's information are not initialized"
160             self.error(msg)
161             self.fail()
162             return
163
164         if not self.get('args'):
165             self.set('args', " ")
166         if not self.get('env'):
167             self.set('env', " ")
168
169         # Some information to check the information in parameter
170         msg = " " + self.rtype() + " ( Guid : " + str(self._guid) +") : " + \
171             self.get('appid') + " : " + self.get('path') + " : " + \
172             self.get('args') + " : " + self.get('env')
173         self.info(msg)
174
175         try:
176             self._omf_api.execute(self.node.get('hostname'),self.get('appid'), \
177                 self.get('args'), self.get('path'), self.get('env'))
178         except AttributeError:
179             msg = "Credentials are not initialzed. XMPP Connections impossible"
180             self.error(msg)
181             self.fail()
182             raise
183
184         super(OMFApplication, self).start()
185
186     def stop(self):
187         """ Stop the RM. It means : Send Xmpp Message Using OMF protocol to 
188         kill the application. 
189         State is set to STOPPED after the message is sent.
190
191         """
192         try:
193             self._omf_api.exit(self.node.get('hostname'),self.get('appid'))
194         except AttributeError:
195             msg = "Credentials were not initialzed. XMPP Connections impossible"
196             self.error(msg)
197             self.fail()
198             return
199
200         super(OMFApplication, self).stop()
201
202     def release(self):
203         """ Clean the RM at the end of the experiment and release the API.
204
205         """
206         if self._omf_api :
207             OMFAPIFactory.release_api(self.get('xmppSlice'), 
208                 self.get('xmppHost'), self.get('xmppPort'), 
209                 self.get('xmppPassword'), exp_id = self.ec.exp_id)
210
211         super(OMFApplication, self).release()
212