Added unit tests for linux application
[nepi.git] / src / neco / resources / omf / omf_interface.py
index 40a650c..1bc0fc4 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 from neco.execution.resource import ResourceManager, clsinit
-from neco.execution.attribute import Attribute
+from neco.execution.attribute import Attribute, Flags 
 
 from neco.resources.omf.omf_api import OMFAPIFactory
 
@@ -9,22 +9,41 @@ import logging
 
 @clsinit
 class OMFWifiInterface(ResourceManager):
+    """
+    .. class:: Class Args :
+      
+        :param ec: The Experiment controller
+        :type ec: ExperimentController
+        :param guid: guid of the RM
+        :type guid: int
+        :param creds: Credentials to communicate with the rm (XmppClient for OMF)
+        :type creds: dict
+
+    .. note::
+
+       This class is used only by the Experiment Controller through the Resource Factory
+
+    """
     _rtype = "OMFWifiInterface"
     _authorized_connections = ["OMFNode" , "OMFChannel"]
+    _waiters = ["OMFNode"]
 
     #alias2name = dict({'w0':'wlan0', 'w1':'wlan1'})
 
     @classmethod
     def _register_attributes(cls):
-        alias = Attribute("alias","Alias of the interface", default_value = "w0")  
+        """Register the attributes of an OMF interface 
+
+        """
+        alias = Attribute("alias","Alias of the interface", default = "w0")
         mode = Attribute("mode","Mode of the interface")
         type = Attribute("type","Type of the interface")
         essid = Attribute("essid","Essid of the interface")
         ip = Attribute("ip","IP of the interface")
-        xmppSlice = Attribute("xmppSlice","Name of the slice", flags = "0x02")
-        xmppHost = Attribute("xmppHost", "Xmpp Server",flags = "0x02")
-        xmppPort = Attribute("xmppPort", "Xmpp Port",flags = "0x02")
-        xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = "0x02")
+        xmppSlice = Attribute("xmppSlice","Name of the slice", flags = Flags.Credential)
+        xmppHost = Attribute("xmppHost", "Xmpp Server",flags = Flags.Credential)
+        xmppPort = Attribute("xmppPort", "Xmpp Port",flags = Flags.Credential)
+        xmppPassword = Attribute("xmppPassword", "Xmpp Port",flags = Flags.Credential)
         cls._register_attribute(alias)
         cls._register_attribute(xmppSlice)
         cls._register_attribute(xmppHost)
@@ -35,37 +54,65 @@ class OMFWifiInterface(ResourceManager):
         cls._register_attribute(essid)
         cls._register_attribute(ip)
 
-    def __init__(self, ec, guid, creds):
+    def __init__(self, ec, guid):
+        """
+        :param ec: The Experiment controller
+        :type ec: ExperimentController
+        :param guid: guid of the RM
+        :type guid: int
+        :param creds: Credentials to communicate with the rm (XmppClient for OMF)
+        :type creds: dict
+
+        """
         super(OMFWifiInterface, self).__init__(ec, guid)
-        self.set('xmppSlice', creds['xmppSlice'])
-        self.set('xmppHost', creds['xmppHost'])
-        self.set('xmppPort', creds['xmppPort'])
-        self.set('xmppPassword', creds['xmppPassword'])
 
-        self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
+        self._omf_api = None
         self._alias = self.get('alias')
 
         self._logger = logging.getLogger("neco.omf.omfIface  ")
         self._logger.setLevel(neco.LOGLEVEL)
 
     def _validate_connection(self, guid):
-        rm = self.ec.resource(guid)
+        """ Check if the connection is available.
+
+        :param guid: Guid of the current RM
+        :type guid: int
+        :rtype:  Boolean
+
+        """
+        rm = self.ec.get_resource(guid)
         if rm.rtype() in self._authorized_connections:
-            self._logger.debug("Connection between %s %s and %s %s accepted" % (self.rtype(), self._guid, rm.rtype(), guid))
+            self._logger.debug("Connection between %s %s and %s %s accepted" %
+                (self.rtype(), self._guid, rm.rtype(), guid))
             return True
-        self._logger.debug("Connection between %s %s and %s %s refused" % (self.rtype(), self._guid, rm.rtype(), guid))
+        self._logger.debug("Connection between %s %s and %s %s refused" % 
+            (self.rtype(), self._guid, rm.rtype(), guid))
         return False
 
     def _get_nodes(self, conn_set):
+        """ Get the RM of the node to which the application is connected
+
+        :param conn_set: Connections of the current Guid
+        :type conn_set: set
+        :rtype: ResourceManager
+
+        """
         for elt in conn_set:
-            rm = self.ec.resource(elt)
+            rm = self.ec.get_resource(elt)
             if rm.rtype() == "OMFNode":
                 return rm
         return None
 
+    def deploy_action(self):
+        """Deploy the RM
 
-    def start(self):
-        self._logger.debug(self.rtype() + " ( Guid : " + str(self._guid) +") : " + self.get('mode') + " : " + self.get('type') + " : " + self.get('essid') + " : " + self.get('ip'))
+        """
+        self._omf_api = OMFAPIFactory.get_api(self.get('xmppSlice'), 
+            self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))
+
+        self._logger.debug(" " + self.rtype() + " ( Guid : " + str(self._guid) +") : " +
+            self.get('mode') + " : " + self.get('type') + " : " +
+            self.get('essid') + " : " + self.get('ip'))
         #try:
         if self.get('mode') and self.get('type') and self.get('essid') and self.get('ip'):
             rm_node = self._get_nodes(self._connections)    
@@ -75,8 +122,27 @@ class OMFWifiInterface(ResourceManager):
                 #print "Send the configure message"
                 self._omf_api.configure(rm_node.get('hostname'), attrname, attrval)
 
+        super(OMFWifiInterface, self).deploy_action()
+
+
+    def start(self):
+        """Send Xmpp Messages Using OMF protocol to configure Interface
+
+        """
+
+        super(OMFWifiInterface, self).start()
+
     def stop(self):
-        self._omf_api.disconnect()
+        """Send Xmpp Message Using OMF protocol to put down the interface
+
+        """
+        super(OMFWifiInterface, self).stop()
+
+    def release(self):
+        """Clean the RM at the end of the experiment
 
+        """
+        OMFAPIFactory.release_api(self.get('xmppSlice'), 
+            self.get('xmppHost'), self.get('xmppPort'), self.get('xmppPassword'))