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