8b6625afc91599a2f9f7e54a2e9eca99d4707074
[nepi.git] / src / neco / resources / omf / omf_application.py
1 #!/usr/bin/env python
2 from neco.execution.resource import Resource, clsinit
3 from neco.execution.attribute import Attribute
4 from neco.resources.omf.omf_api import OMFAPIFactory
5
6 import neco
7 import logging
8
9 @clsinit
10 class OMFApplication(Resource):
11     _rtype = "OMFApplication"
12     _authorized_connections = ["OMFNode"]
13
14     @classmethod
15     def _register_attributes(cls):
16         appid = Attribute("appid", "Name of the application")
17         path = Attribute("path", "Path of the application")
18         args = Attribute("args", "Argument of the application")
19         env = Attribute("env", "Environnement variable of the application")
20         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = "0x02")
21         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = "0x02")
22         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = "0x02")
23         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = "0x02")
24         cls._register_attribute(appid)
25         cls._register_attribute(path)
26         cls._register_attribute(args)
27         cls._register_attribute(env)
28         cls._register_attribute(xmppSlice)
29         cls._register_attribute(xmppHost)
30         cls._register_attribute(xmppPort)
31         cls._register_attribute(xmppPassword)
32
33
34     def __init__(self, ec, guid, creds):
35         super(OMFApplication, self).__init__(ec, guid)
36         self.set('xmppSlice', creds['xmppSlice'])
37         self.set('xmppHost', creds['xmppHost'])
38         self.set('xmppPort', creds['xmppPort'])
39         self.set('xmppPassword', creds['xmppPassword'])
40
41         self.set('appid', "")
42         self.set('path', "")
43         self.set('args', "")
44         self.set('env', "")
45
46         self._node = None
47
48         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
49
50         self._logger = logging.getLogger("neco.omf.omfApp    ")
51         self._logger.setLevel(neco.LOGLEVEL)
52
53     def _validate_connection(self, guid):
54         rm = self.ec.resource(guid)
55         if rm.rtype() not in self._authorized_connections:
56             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))
57             return False
58         elif len(self.connections) != 0 :
59             self._logger.debug("Connection between %s %s and %s %s refused : Already Connected" % (self.rtype(), self._guid, rm.rtype(), guid))
60             return False
61         else :
62             self._logger.debug("Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid))
63             return True
64
65     def _get_nodes(self, conn_set):
66         for elt in conn_set:
67             rm = self.ec.resource(elt)
68             if rm.rtype() == "OMFNode":
69                 return rm
70         return None
71
72     def start(self):
73         self._logger.debug(" " + self.rtype() + " ( Guid : " + str(self._guid) +") : " + self.get('appid') + " : " + self.get('path') + " : " + self.get('args') + " : " + self.get('env'))
74         #try:
75         if self.get('appid') and self.get('path') and self.get('args') and self.get('env'):
76             rm_node = self._get_nodes(self._connections)
77             self._omf_api.execute(rm_node.get('hostname'),self.get('appid'), self.get('args'), self.get('path'), self.get('env'))
78
79     def stop(self):
80         rm_node = self._get_nodes(self._connections)
81         self._omf_api.exit(rm_node.get('hostname'),self.get('appid'))
82
83
84