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