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