111af67a661dfad0c21198575bd9f31292c00681
[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, Flags 
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     """
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 for OMF)
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 = "OMFChannel"
28     _authorized_connections = ["OMFWifiInterface", "OMFNode"]
29     _waiters = ["OMFNode", "OMFWifiInterface"]
30
31
32     @classmethod
33     def _register_attributes(cls):
34         """Register the attributes of an OMF channel
35         """
36         channel = Attribute("channel", "Name of the application")
37         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = Flags.Credential)
38         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = Flags.Credential)
39         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = Flags.Credential)
40         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = Flags.Credential)
41         cls._register_attribute(channel)
42         cls._register_attribute(xmppSlice)
43         cls._register_attribute(xmppHost)
44         cls._register_attribute(xmppPort)
45         cls._register_attribute(xmppPassword)
46
47     def __init__(self, ec, guid):
48         """
49         :param ec: The Experiment controller
50         :type ec: ExperimentController
51         :param guid: guid of the RM
52         :type guid: int
53         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
54         :type creds: dict
55
56         """
57         super(OMFChannel, self).__init__(ec, guid)
58
59         self._nodes_guid = list()
60
61         self._omf_api = None
62
63         self._logger = logging.getLogger("neco.omf.omfChannel")
64         self._logger.setLevel(neco.LOGLEVEL)
65
66     def _validate_connection(self, guid):
67         """Check if the connection is available.
68
69         :param guid: Guid of the current RM
70         :type guid: int
71         :rtype:  Boolean
72
73         """
74         rm = self.ec.get_resource(guid)
75         if rm.rtype() in self._authorized_connections:
76             self._logger.debug("Connection between %s %s and %s %s accepted" %
77                 (self.rtype(), self._guid, rm.rtype(), guid))
78             return True
79         self._logger.debug("Connection between %s %s and %s %s refused" % (self.rtype(), self._guid, rm.rtype(), guid))
80         return False
81
82     def _get_target(self, conn_set):
83         """
84         Get the couples (host, interface) that used this channel
85
86         :param conn_set: Connections of the current Guid
87         :type conn_set: set
88         :rtype: list
89         :return: self._nodes_guid
90
91         """
92         for elt in conn_set:
93             rm_iface = self.ec.get_resource(elt)
94             for conn in rm_iface.connections:
95                 rm_node = self.ec.get_resource(conn)
96                 if rm_node.rtype() == "OMFNode":
97                     couple = [rm_node.get('hostname'), rm_iface.get('alias')]
98                     #print couple
99                     self._nodes_guid.append(couple)
100         return self._nodes_guid
101
102     def deploy_action(self):
103         """Deploy the RM
104
105         """
106         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
107             self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
108
109         if self.get('channel'):
110             set_nodes = self._get_target(self._connections) 
111             print set_nodes
112             for couple in set_nodes:
113                 #print "Couple node/alias : " + couple[0] + "  ,  " + couple[1]
114                 attrval = self.get('channel')
115                 attrname = "net/%s/%s" % (couple[1], 'channel')
116                 #print "Send the configure message"
117                 self._omf_api.configure(couple[0], attrname, attrval)
118
119         super(OMFChannel, self).deploy_action()
120
121     def discover(self):
122         """ Discover the availables channels
123
124         """
125         pass
126      
127     def provision(self):
128         """ Provision some availables channels
129
130         """
131         pass
132
133     def start(self):
134         """Send Xmpp Message Using OMF protocol to configure Channel
135
136         """
137
138         super(OMFChannel, self).start()
139
140     def stop(self):
141         """Send Xmpp Message Using OMF protocol to put down the interface
142
143         """
144         super(OMFChannel, self).stop()
145
146     def release(self):
147         """Clean the RM at the end of the experiment
148
149         """
150         OMFAPIFactory.release_api(self.get('xmppSlice'), 
151             self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
152