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