Added scheduler and task processing thread to ec. Completed deploy and release methods.
[nepi.git] / src / neco / resources / omf / omf_channel.py
1 #!/usr/bin/env python
2 from neco.execution.resource import ResourceManager, clsinit
3 from neco.execution.attribute import Attribute
4
5 from neco.resources.omf.omf_api import OMFAPIFactory
6
7 import neco
8 import logging
9
10 @clsinit
11 class OMFChannel(ResourceManager):
12     _rtype = "OMFChannel"
13     _authorized_connections = ["OMFWifiInterface"]
14
15     @classmethod
16     def _register_attributes(cls):
17         channel = Attribute("channel", "Name of the application")
18         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = "0x02")
19         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = "0x02")
20         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = "0x02")
21         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = "0x02")
22         cls._register_attribute(channel)
23         cls._register_attribute(xmppSlice)
24         cls._register_attribute(xmppHost)
25         cls._register_attribute(xmppPort)
26         cls._register_attribute(xmppPassword)
27
28     def __init__(self, ec, guid, creds):
29         super(OMFChannel, self).__init__(ec, guid)
30         self.set('xmppSlice', creds['xmppSlice'])
31         self.set('xmppHost', creds['xmppHost'])
32         self.set('xmppPort', creds['xmppPort'])
33         self.set('xmppPassword', creds['xmppPassword'])
34
35         self._nodes_guid = list()
36
37         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
38
39         self._logger = logging.getLogger("neco.omf.omfChannel")
40         self._logger.setLevel(neco.LOGLEVEL)
41
42     def _validate_connection(self, guid):
43         rm = self.ec.resource(guid)
44         if rm.rtype() in self._authorized_connections:
45             self._logger.debug("Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid))
46             return True
47         self._logger.debug("Connection between %s %s and %s %s refused" % (self.rtype(), self._guid, rm.rtype(), guid))
48         return False
49
50     def _get_nodes(self, conn_set):
51         for elt in conn_set:
52             rm_iface = self.ec.resource(elt)
53             for conn in rm_iface._connections:
54                 rm_node = self.ec.resource(conn)
55                 if rm_node.rtype() == "OMFNode":
56                     couple = [rm_node.get('hostname'), rm_iface.get('alias')]
57                     #print couple
58                     self._nodes_guid.append(couple)
59         return self._nodes_guid
60
61     def discover(self):
62         pass
63      
64     def provision(self, credential):
65         pass
66
67     def start(self):
68         if self.get('channel'):
69             set_nodes = self._get_nodes(self._connections) 
70             #print set_nodes
71             for couple in set_nodes:
72                 #print "Couple node/alias : " + couple[0] + "  ,  " + couple[1]
73                 attrval = self.get('channel')
74                 attrname = "net/%s/%s" % (couple[1], 'channel')
75                 #print "Send the configure message"
76                 self._omf_api.configure(couple[0], attrname, attrval)
77
78     def xstart(self):
79         try:
80             if self.get('channel'):
81                 node = self.tc.elements.get(self._node_guid)    
82                 attrval = self.get('channel')
83                 attrname = "net/%s/%s" % (self._alias, 'channel')
84                 self._omf_api.configure('omf.plexus.wlab17', attrname, attrval)
85         except AttributeError:
86             # If the attribute is not yet defined, ignore the error
87             pass
88
89