Correct the tests for OMF Resources
[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 """
19
20 from nepi.execution.resource import ResourceManager, clsinit, ResourceState
21 from nepi.execution.attribute import Attribute, Flags 
22 from nepi.resources.omf.omf_api import OMFAPIFactory
23
24 reschedule_delay = "0.5s"
25
26 @clsinit
27 class OMFApplication(ResourceManager):
28     """
29     .. class:: Class Args :
30       
31         :param ec: The Experiment controller
32         :type ec: ExperimentController
33         :param guid: guid of the RM
34         :type guid: int
35         :param creds: Credentials to communicate with the rm (XmppClient)
36         :type creds: dict
37
38     .. note::
39
40        This class is used only by the Experiment Controller through the Resource Factory
41
42     """
43     _rtype = "OMFApplication"
44     _authorized_connections = ["OMFNode"]
45
46     @classmethod
47     def _register_attributes(cls):
48         """Register the attributes of an OMF application
49         """
50
51         appid = Attribute("appid", "Name of the application")
52         path = Attribute("path", "Path of the application")
53         args = Attribute("args", "Argument of the application")
54         env = Attribute("env", "Environnement variable of the application")
55         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = Flags.Credential)
56         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = Flags.Credential)
57         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = Flags.Credential)
58         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = Flags.Credential)
59         cls._register_attribute(appid)
60         cls._register_attribute(path)
61         cls._register_attribute(args)
62         cls._register_attribute(env)
63         cls._register_attribute(xmppSlice)
64         cls._register_attribute(xmppHost)
65         cls._register_attribute(xmppPort)
66         cls._register_attribute(xmppPassword)
67
68
69     def __init__(self, ec, guid):
70         """
71         :param ec: The Experiment controller
72         :type ec: ExperimentController
73         :param guid: guid of the RM
74         :type guid: int
75         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
76         :type creds: dict
77
78         """
79         
80         super(OMFApplication, self).__init__(ec, guid)
81
82         self.set('appid', "")
83         self.set('path', "")
84         self.set('args', "")
85         self.set('env', "")
86
87         self._node = None
88
89         self._omf_api = None
90
91     def _validate_connection(self, guid):
92         """Check if the connection is available.
93
94         :param guid: Guid of the current RM
95         :type guid: int
96         :rtype:  Boolean
97
98         """
99         rm = self.ec.get_resource(guid)
100         if rm.rtype() not in self._authorized_connections:
101             msg = "Connection between %s %s and %s %s refused : An Application can be connected only to a Node" % (self.rtype(), self._guid, rm.rtype(), guid)
102             self.debug(msg)
103             return False
104         elif len(self.connections) != 0 :
105             msg = "Connection between %s %s and %s %s refused : Already Connected" % (self.rtype(), self._guid, rm.rtype(), guid)
106             self.debug(msg)
107             return False
108         else :
109             msg = "Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid)
110             self.debug(msg)
111             return True
112
113     def deploy(self):
114         """Deploy the RM
115
116         """
117         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
118             self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
119         super(OMFApplication, self).deploy()
120
121     def start(self):
122         """Send Xmpp Message Using OMF protocol to execute the application
123
124         """
125         super(OMFApplication, self).start()
126         msg = " " + self.rtype() + " ( Guid : " + str(self._guid) +") : " + self.get('appid') + " : " + self.get('path') + " : " + self.get('args') + " : " + self.get('env')
127         self.info(msg)
128
129         if self.get('appid') and self.get('path') and self.get('args') and self.get('env'):
130             rm_list = self.get_connected("OMFNode")
131             for rm_node in rm_list:
132                 self._omf_api.execute(rm_node.get('hostname'),self.get('appid'), self.get('args'), self.get('path'), self.get('env'))
133         else :
134             msg = "Application's information are not initialized"
135             self.error(msg)
136
137     def stop(self):
138         """Send Xmpp Message Using OMF protocol to kill the application
139
140         """
141
142         rm_list = self.get_connected("OMFNode")
143         for rm_node in rm_list :
144             self._omf_api.exit(rm_node.get('hostname'),self.get('appid'))
145         super(OMFApplication, self).stop()
146         self._state = ResourceState.FINISHED
147         
148
149     def release(self):
150         """Clean the RM at the end of the experiment
151
152         """
153         OMFAPIFactory.release_api(self.get('xmppSlice'), 
154             self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
155