Minor typos
[nepi.git] / src / neco / resources / omf / omf_application.py
1 #!/usr/bin/env python
2
3 from neco.execution.resource import ResourceManager, clsinit
4 from neco.execution.attribute import Attribute, Flags 
5 from neco.resources.omf.omf_api import OMFAPIFactory
6
7 import neco
8 import logging
9
10 @clsinit
11 class OMFApplication(ResourceManager):
12     """
13     .. class:: Class Args :
14       
15         :param ec: The Experiment controller
16         :type ec: ExperimentController
17         :param guid: guid of the RM
18         :type guid: int
19         :param creds: Credentials to communicate with the rm (XmppClient)
20         :type creds: dict
21
22     .. note::
23
24        This class is used only by the Experiment Controller through the Resource Factory
25
26     """
27     _rtype = "OMFApplication"
28     _authorized_connections = ["OMFNode"]
29     _waiters = ["OMFNode", "OMFChannel", "OMFWifiInterface"]
30
31     @classmethod
32     def _register_attributes(cls):
33         """Register the attributes of an OMF application
34         """
35
36         appid = Attribute("appid", "Name of the application")
37         path = Attribute("path", "Path of the application")
38         args = Attribute("args", "Argument of the application")
39         env = Attribute("env", "Environnement variable of the application")
40         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = Flags.Credential)
41         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = Flags.Credential)
42         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = Flags.Credential)
43         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = Flags.Credential)
44         cls._register_attribute(appid)
45         cls._register_attribute(path)
46         cls._register_attribute(args)
47         cls._register_attribute(env)
48         cls._register_attribute(xmppSlice)
49         cls._register_attribute(xmppHost)
50         cls._register_attribute(xmppPort)
51         cls._register_attribute(xmppPassword)
52
53
54     def __init__(self, ec, guid):
55         """
56         :param ec: The Experiment controller
57         :type ec: ExperimentController
58         :param guid: guid of the RM
59         :type guid: int
60         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
61         :type creds: dict
62
63         """
64         
65         super(OMFApplication, self).__init__(ec, guid)
66
67         self.set('appid', "")
68         self.set('path', "")
69         self.set('args', "")
70         self.set('env', "")
71
72         self._node = None
73
74         self._omf_api = None
75
76         self._logger = logging.getLogger("neco.omf.omfApp    ")
77         self._logger.setLevel(neco.LOGLEVEL)
78
79
80     def _validate_connection(self, guid):
81         """Check if the connection is available.
82
83         :param guid: Guid of the current RM
84         :type guid: int
85         :rtype:  Boolean
86
87         """
88         rm = self.ec.get_resource(guid)
89         if rm.rtype() not in self._authorized_connections:
90             self._logger.debug("Connection between %s %s and %s %s refused : An Application can be connected only to a Node" % (self.rtype(), self._guid, rm.rtype(), guid))
91             return False
92         elif len(self.connections) != 0 :
93             self._logger.debug("Connection between %s %s and %s %s refused : Already Connected" % (self.rtype(), self._guid, rm.rtype(), guid))
94             return False
95         else :
96             self._logger.debug("Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid))
97             return True
98
99     def _get_nodes(self, conn_set):
100         """Get the RM of the node to which the application is connected
101
102         :param conn_set: Connections of the current Guid
103         :type conn_set: set
104         :rtype: ResourceManager
105         """
106
107         for elt in conn_set:
108             rm = self.ec.get_resource(elt)
109             if rm.rtype() == "OMFNode":
110                 return rm
111         return None
112
113     def deploy_action(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_action()
120
121     def start(self):
122         """Send Xmpp Message Using OMF protocol to execute the application
123
124         """
125         super(OMFApplication, self).start()
126         self._logger.debug(" " + self.rtype() + " ( Guid : " + str(self._guid) +") : " + self.get('appid') + " : " + self.get('path') + " : " + self.get('args') + " : " + self.get('env'))
127
128         if self.get('appid') and self.get('path') and self.get('args') and self.get('env'):
129             rm_node = self._get_nodes(self._connections)
130             self._omf_api.execute(rm_node.get('hostname'),self.get('appid'), self.get('args'), self.get('path'), self.get('env'))
131
132     def stop(self):
133         """Send Xmpp Message Using OMF protocol to kill the application
134
135         """
136
137         rm_node = self._get_nodes(self._connections)
138         self._omf_api.exit(rm_node.get('hostname'),self.get('appid'))
139         super(OMFApplication, self).stop()
140
141     def release(self):
142         """Clean the RM at the end of the experiment
143
144         """
145         OMFAPIFactory.release_api(self.get('xmppSlice'), 
146             self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
147