change bug of dict
[nepi.git] / src / nepi / resources / omf / interface.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
26 @clsinit
27 class OMFWifiInterface(ResourceManager):
28     """
29     .. class:: Class Args :
30       
31         :param ec: The Experiment controller
32         :type ec: ExperimentController
33         :param guid: guid of the RM
34         :type guid: int
35         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
36         :type creds: dict
37
38     .. note::
39
40        This class is used only by the Experiment Controller through the Resource Factory
41
42     """
43     _rtype = "OMFWifiInterface"
44     _authorized_connections = ["OMFNode" , "OMFChannel"]
45     _waiters = ["OMFNode"]
46
47     #alias2name = dict({'w0':'wlan0', 'w1':'wlan1'})
48
49     @classmethod
50     def _register_attributes(cls):
51         """Register the attributes of an OMF interface 
52
53         """
54         alias = Attribute("alias","Alias of the interface", default = "w0")
55         mode = Attribute("mode","Mode of the interface")
56         type = Attribute("type","Type of the interface")
57         essid = Attribute("essid","Essid of the interface")
58         ip = Attribute("ip","IP of the interface")
59         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = Flags.Credential)
60         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = Flags.Credential)
61         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = Flags.Credential)
62         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = Flags.Credential)
63         cls._register_attribute(alias)
64         cls._register_attribute(xmppSlice)
65         cls._register_attribute(xmppHost)
66         cls._register_attribute(xmppPort)
67         cls._register_attribute(xmppPassword)
68         cls._register_attribute(mode)
69         cls._register_attribute(type)
70         cls._register_attribute(essid)
71         cls._register_attribute(ip)
72
73     def __init__(self, ec, guid):
74         """
75         :param ec: The Experiment controller
76         :type ec: ExperimentController
77         :param guid: guid of the RM
78         :type guid: int
79         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
80         :type creds: dict
81
82         """
83         super(OMFWifiInterface, self).__init__(ec, guid)
84
85         self._omf_api = None
86         self._alias = self.get('alias')
87
88     def _validate_connection(self, guid):
89         """ Check if the connection is available.
90
91         :param guid: Guid of the current RM
92         :type guid: int
93         :rtype:  Boolean
94
95         """
96         rm = self.ec.get_resource(guid)
97         if rm.rtype() in self._authorized_connections:
98             msg = "Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid)
99             self.debug(msg)
100             return True
101         msg = "Connection between %s %s and %s %s refused" % (self.rtype(), self._guid, rm.rtype(), guid)
102         self.debug(msg)
103         return False
104
105     def _get_nodes(self, conn_set):
106         """ Get the RM of the node to which the application is connected
107
108         :param conn_set: Connections of the current Guid
109         :type conn_set: set
110         :rtype: ResourceManager
111
112         """
113         for elt in conn_set:
114             rm = self.ec.get_resource(elt)
115             if rm.rtype() == "OMFNode":
116                 return rm
117         return None
118
119     def deploy(self):
120         """Deploy the RM
121
122         """
123         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
124             self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
125
126         self._logger.debug(" " + self.rtype() + " ( Guid : " + str(self._guid) +") : " +
127             self.get('mode') + " : " + self.get('type') + " : " +
128             self.get('essid') + " : " + self.get('ip'))
129         #try:
130         if self.get('mode') and self.get('type') and self.get('essid') and self.get('ip'):
131             rm_node = self._get_nodes(self._connections)    
132             for attrname in ["mode", "type", "essid", "ip"]:
133                 attrval = self.get(attrname)
134                 attrname = "net/%s/%s" % (self._alias, attrname)
135                 #print "Send the configure message"
136                 self._omf_api.configure(rm_node.get('hostname'), attrname, attrval)
137
138         super(OMFWifiInterface, self).deploy()
139
140
141     def start(self):
142         """Send Xmpp Messages Using OMF protocol to configure Interface
143
144         """
145
146         super(OMFWifiInterface, self).start()
147
148     def stop(self):
149         """Send Xmpp Message Using OMF protocol to put down the interface
150
151         """
152         super(OMFWifiInterface, self).stop()
153
154     def release(self):
155         """Clean the RM at the end of the experiment
156
157         """
158         OMFAPIFactory.release_api(self.get('xmppSlice'), 
159             self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
160
161