Add OMF Classes using XMPP and Protocol 5.4
[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     _rtype = "OMFWifiInterface"
13     _authorized_connections = ["OMFNode" , "OMFChannel"]
14
15     #alias2name = dict({'w0':'wlan0', 'w1':'wlan1'})
16
17     @classmethod
18     def _register_attributes(cls):
19         alias = Attribute("alias","Alias of the interface", default_value = "w0")  
20         mode = Attribute("mode","Mode of the interface")
21         type = Attribute("type","Type of the interface")
22         essid = Attribute("essid","Essid of the interface")
23         ip = Attribute("ip","IP of the interface")
24         xmppSlice = Attribute("xmppSlice","Name of the slice", flags = "0x02")
25         xmppHost = Attribute("xmppHost", "Xmpp Server",flags = "0x02")
26         xmppPort = Attribute("xmppPort", "Xmpp Port",flags = "0x02")
27         xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = "0x02")
28         cls._register_attribute(alias)
29         cls._register_attribute(xmppSlice)
30         cls._register_attribute(xmppHost)
31         cls._register_attribute(xmppPort)
32         cls._register_attribute(xmppPassword)
33         cls._register_attribute(mode)
34         cls._register_attribute(type)
35         cls._register_attribute(essid)
36         cls._register_attribute(ip)
37
38     def __init__(self, ec, guid, creds):
39         super(OMFWifiInterface, self).__init__(ec, guid)
40         self.set('xmppSlice', creds['xmppSlice'])
41         self.set('xmppHost', creds['xmppHost'])
42         self.set('xmppPort', creds['xmppPort'])
43         self.set('xmppPassword', creds['xmppPassword'])
44
45         self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
46         self._alias = self.get('alias')
47
48         self._logger = logging.getLogger("neco.omf.omfIface  ")
49         self._logger.setLevel(neco.LOGLEVEL)
50
51     def _validate_connection(self, guid):
52         rm = self.ec.resource(guid)
53         if rm.rtype() in self._authorized_connections:
54             self._logger.debug("Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid))
55             return True
56         self._logger.debug("Connection between %s %s and %s %s refused" % (self.rtype(), self._guid, rm.rtype(), guid))
57         return False
58
59     def _get_nodes(self, conn_set):
60         for elt in conn_set:
61             rm = self.ec.resource(elt)
62             if rm.rtype() == "OMFNode":
63                 return rm
64         return None
65
66
67     def start(self):
68         self._logger.debug(self.rtype() + " ( Guid : " + str(self._guid) +") : " + self.get('mode') + " : " + self.get('type') + " : " + self.get('essid') + " : " + self.get('ip'))
69         #try:
70         if self.get('mode') and self.get('type') and self.get('essid') and self.get('ip'):
71             rm_node = self._get_nodes(self._connections)    
72             for attrname in ["mode", "type", "essid", "ip"]:
73                 attrval = self.get(attrname)
74                 attrname = "net/%s/%s" % (self._alias, attrname)
75                 #print "Send the configure message"
76                 self._omf_api.configure(rm_node.get('hostname'), attrname, attrval)
77
78     def stop(self):
79         self._omf_api.disconnect()
80
81
82