Merging with jtribino omf stuff
[nepi.git] / src / neco / resources / omf / omf_interface.py
1 #!/usr/bin/env python
2 from neco.execution.resource import Resource, clsinit
3 from neco.execution.attribute import Attribute
4
5 from neco.resources.omf.omf_api import OMFAPIFactory
6
7 import neco
8 import logging
9
10 @clsinit
11 class OMFWifiInterface(Resource):
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         alias = Attribute("alias","Alias of the interface", default_value = "w0")  
37         mode = Attribute("mode","Mode of the interface")
38         type = Attribute("type","Type of the interface")
39         essid = Attribute("essid","Essid of the interface")
40         ip = Attribute("ip","IP of the interface")
41         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = "0x02")
42         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = "0x02")
43         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = "0x02")
44         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = "0x02")
45         cls._register_attribute(alias)
46         cls._register_attribute(xmppSlice)
47         cls._register_attribute(xmppHost)
48         cls._register_attribute(xmppPort)
49         cls._register_attribute(xmppPassword)
50         cls._register_attribute(mode)
51         cls._register_attribute(type)
52         cls._register_attribute(essid)
53         cls._register_attribute(ip)
54
55     def __init__(self, ec, guid, creds):
56         """
57         :param ec: The Experiment controller
58         :type ec: ExperimentController
59         :param guid: guid of the RM
60         :type guid: int
61         :param creds: Credentials to communicate with the rm (XmppClient for OMF)
62         :type creds: dict
63
64         """
65         super(OMFWifiInterface, self).__init__(ec, guid)
66         self.set('xmppSlice', creds['xmppSlice'])
67         self.set('xmppHost', creds['xmppHost'])
68         self.set('xmppPort', creds['xmppPort'])
69         self.set('xmppPassword', creds['xmppPassword'])
70
71         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
72         self._alias = self.get('alias')
73
74         self._logger = logging.getLogger("neco.omf.omfIface  ")
75         self._logger.setLevel(neco.LOGLEVEL)
76
77     def _validate_connection(self, guid):
78         """Check if the connection is available.
79
80         :param guid: Guid of the current RM
81         :type guid: int
82         :rtype:  Boolean
83
84         """
85         rm = self.ec.resource(guid)
86         if rm.rtype() in self._authorized_connections:
87             self._logger.debug("Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid))
88             return True
89         self._logger.debug("Connection between %s %s and %s %s refused" % (self.rtype(), self._guid, rm.rtype(), guid))
90         return False
91
92     def _get_nodes(self, conn_set):
93         """
94         Get the RM of the node to which the application is connected
95
96         :param conn_set: Connections of the current Guid
97         :type conn_set: set
98         :rtype: ResourceManager
99         """
100         for elt in conn_set:
101             rm = self.ec.resource(elt)
102             if rm.rtype() == "OMFNode":
103                 return rm
104         return None
105
106
107     def start(self):
108         """Send Xmpp Messages Using OMF protocol to configure Interface
109
110         """
111         self._logger.debug(self.rtype() + " ( Guid : " + str(self._guid) +") : " + self.get('mode') + " : " + self.get('type') + " : " + self.get('essid') + " : " + self.get('ip'))
112         #try:
113         if self.get('mode') and self.get('type') and self.get('essid') and self.get('ip'):
114             rm_node = self._get_nodes(self._connections)    
115             for attrname in ["mode", "type", "essid", "ip"]:
116                 attrval = self.get(attrname)
117                 attrname = "net/%s/%s" % (self._alias, attrname)
118                 #print "Send the configure message"
119                 self._omf_api.configure(rm_node.get('hostname'), attrname, attrval)
120
121     def stop(self):
122         """Send Xmpp Message Using OMF protocol to put down the interface
123
124         """
125         self._omf_api.disconnect()
126
127
128