Adding linux ns3 server unit test
[nepi.git] / src / nepi / resources / planetlab / openvswitch / ovs.py
index e8b8011..f4b50c6 100644 (file)
@@ -19,7 +19,8 @@
 #         Alexandros Kouvakas <alexandros.kouvakas@inria.fr>
 
 
-from nepi.execution.resource import ResourceManager, clsinit_copy, ResourceState
+from nepi.execution.resource import ResourceManager, clsinit_copy, \
+        ResourceState
 from nepi.execution.attribute import Attribute, Flags
 from nepi.resources.planetlab.node import PlanetlabNode        
 from nepi.resources.linux.application import LinuxApplication
@@ -31,6 +32,9 @@ reschedule_delay = "0.5s"
 class OVSWitch(LinuxApplication):
     
     _rtype = "OVSWitch"
+    _help = "Runs an OpenVSwitch on a PlanetLab host"
+    _backend = "planetlab"
+
     _authorized_connections = ["PlanetlabNode", "OVSPort", "LinuxNode"]       
 
     @classmethod
@@ -39,13 +43,13 @@ class OVSWitch(LinuxApplication):
 
         """
         bridge_name = Attribute("bridge_name", "Name of the switch/bridge",
-                flags = Flags.ExecReadOnly)    
+                flags = Flags.Design)  
         virtual_ip_pref = Attribute("virtual_ip_pref", "Virtual IP/PREFIX of the switch",
-                flags = Flags.ExecReadOnly)       
+                flags = Flags.Design)  
         controller_ip = Attribute("controller_ip", "IP of the controller",
-                flags = Flags.ExecReadOnly)
+                flags = Flags.Design)  
         controller_port = Attribute("controller_port", "Port of the controller",
-                flags = Flags.ExecReadOnly)
+                flags = Flags.Design)  
 
         cls._register_attribute(bridge_name)
         cls._register_attribute(virtual_ip_pref)
@@ -58,8 +62,6 @@ class OVSWitch(LinuxApplication):
         :type ec: ExperimentController
         :param guid: guid of the RM
         :type guid: int
-        :param creds: Credentials to communicate with the rm 
-        :type creds: dict
     
         """
         super(OVSWitch, self).__init__(ec, guid)
@@ -70,7 +72,7 @@ class OVSWitch(LinuxApplication):
 
     @property
     def node(self):
-        node = self.get_connected(PlanetlabNode.rtype())
+        node = self.get_connected(PlanetlabNode.get_rtype())
         if node: return node[0]
         return None
 
@@ -99,13 +101,13 @@ class OVSWitch(LinuxApplication):
 
 #        """
 #        rm = self.ec.get_resource(guid)
-#        if rm.rtype() in self._authorized_connections:
+#        if rm.get_rtype() in self._authorized_connections:
 #            msg = "Connection between %s %s and %s %s accepted" % \
-#                (self.rtype(), self._guid, rm.rtype(), guid)
+#                (self.get_rtype(), self._guid, rm.get_rtype(), guid)
 #            self.debug(msg)
 #            return True
 #        msg = "Connection between %s %s and %s %s refused" % \
-#             (self.rtype(), self._guid, rm.rtype(), guid)
+#             (self.get_rtype(), self._guid, rm.get_rtype(), guid)
 #        self.debug(msg)
 #        return False
 
@@ -113,11 +115,13 @@ class OVSWitch(LinuxApplication):
         # TODO: Validate!
         return True
 
-    def provision(self):
+    def do_provision(self):
         # create home dir for ovs
         self.node.mkdir(self.ovs_home)
         # create dir for ovs checks
         self.node.mkdir(self.ovs_checks)
+        
+        super(OVSWitch, self).do_provision()
 
     def check_sliver_ovs(self):  
         """ Check if sliver-ovs exists. If it does not exist, we interrupt
@@ -135,35 +139,33 @@ class OVSWitch(LinuxApplication):
                 stderr = "check_cmd_stderr")
 
         (out, err), proc = self.node.check_output(self.ovs_checks, 'check_cmd_exitcode')
+        
         if out != "0\n":
             msg = "Command sliver-ovs does not exist on the VM"         
             self.debug(msg)
             raise RuntimeError, msg
+
         msg = "Command sliver-ovs exists" 
         self.debug(msg)                                                
 
-    def deploy(self):
+    def do_deploy(self):
         """ Wait until node is associated and deployed
         """
         node = self.node
         if not node or node.state < ResourceState.READY:
-            self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
+            #self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
             self.ec.schedule(reschedule_delay, self.deploy)
-
-        else:
-            try:
-                self.discover()
-                self.provision()
-                self.check_sliver_ovs()
-                self.servers_on()
-                self.create_bridge()
-                self.assign_contr()
-                self.ovs_status()
-            except:
-                self._state = ResourceState.FAILED
-                raise
-                
-            self._state = ResourceState.READY
+            return
+
+        self.do_discover()
+        self.do_provision()
+        self.check_sliver_ovs()
+        self.servers_on()
+        self.create_bridge()
+        self.assign_controller()
+        self.ovs_status()
+            
+        super(OVSWitch, self).do_deploy()
 
     def servers_on(self):
         """ Start the openvswitch servers and also checking 
@@ -185,7 +187,7 @@ class OVSWitch(LinuxApplication):
         (out, err), proc = self.node.check_output(self.ovs_checks, 'start_srv_exitcode')
 
         if out != "0\n":
-            self.debug("Servers have not started")
+            self.error("Servers have not started")
             raise RuntimeError, msg    
                                
         cmd = "ps -A | grep ovsdb-server"
@@ -200,9 +202,11 @@ class OVSWitch(LinuxApplication):
 
         # Check if the servers are running or not
         (out, err), proc = self.node.check_output(self.ovs_checks, 'status_srv_exitcode')
+        
         if out != "0\n":
-            self.debug("Servers are not running")
+            self.error("Servers are not running")
             raise RuntimeError, msg
+        
         self.info("Servers started")  
 
     def del_old_br(self):
@@ -218,36 +222,38 @@ class OVSWitch(LinuxApplication):
         # TODO: Add check for virtual_ip belonging to vsys_tag
         self.del_old_br()
        
-        if self.get("bridge_name") and self.get("virtual_ip_pref"):    
-            bridge_name = self.get("bridge_name")
-            virtual_ip_pref = self.get("virtual_ip_pref")
-            self.info(" Creating the bridge %s and assigning %s" %\
-                (bridge_name, virtual_ip_pref) )
-            cmd = "sliver-ovs create-bridge '%s' '%s'" %\
-                (bridge_name, virtual_ip_pref) 
-            out = err = ""
-            (out, err), proc = self.node.run_and_wait(cmd, self.ovs_checks,
-                    shfile = "create_br.sh",
-                    pidfile = "create_br_pidfile",
-                    ecodefile = "create_br_exitcode", 
-                    sudo = True, 
-                    stdout = "create_br_stdout", 
-                    stderr = "create_br_stderr") 
-            (out, err), proc = self.node.check_output(self.ovs_checks, 'create_br_exitcode')
-            if out != "0\n":
-                msg = "No such pltap netdev\novs-appctl: ovs-vswitchd: server returned an error"
-                self.debug("Check again the virtual IP")                       
-                raise RuntimeError, msg
-            self.info("Bridge %s created" % bridge_name)
-          
-        else:  
+        if not (self.get("bridge_name") and self.get("virtual_ip_pref")):
             msg = "No assignment in one or both attributes"
             self.error(msg)
             self.debug("Bridge name is %s and virtual_ip_pref is %s" %\
                 (self.get("bridge_name"), self.get("virtual_ip_pref")) )
             raise AttributeError, msg
+       
+        bridge_name = self.get("bridge_name")
+        virtual_ip_pref = self.get("virtual_ip_pref")
+        self.info(" Creating the bridge %s and assigning %s" %\
+            (bridge_name, virtual_ip_pref) )
+        cmd = "sliver-ovs create-bridge '%s' '%s'" %\
+            (bridge_name, virtual_ip_pref) 
+        out = err = ""
+        (out, err), proc = self.node.run_and_wait(cmd, self.ovs_checks,
+                shfile = "create_br.sh",
+                pidfile = "create_br_pidfile",
+                ecodefile = "create_br_exitcode", 
+                sudo = True, 
+                stdout = "create_br_stdout", 
+                stderr = "create_br_stderr") 
 
-    def assign_contr(self):
+        (out, err), proc = self.node.check_output(self.ovs_checks, 'create_br_exitcode')
+        if out != "0\n":
+            msg = "No such pltap netdev\novs-appctl: ovs-vswitchd: server returned an error"
+            self.debug("Check again the virtual IP")                   
+            raise RuntimeError, msg
+
+        self.info("Bridge %s created" % bridge_name)
+          
+
+    def assign_controller(self):
         """ Set the controller IP
         """
         if self.get("controller_ip") and self.get("controller_port"):
@@ -278,28 +284,16 @@ class OVSWitch(LinuxApplication):
         (out, err), proc = self.node.check_output(self.ovs_home, 'show_stdout')
         self.info(out)
 
-    def start(self):
-        """ Start the RM. It means nothing special for 
-            ovswitch for now.  
-        """
-        pass
-
-    def stop(self):
-        """ Stop the RM.It means nothing 
-            for ovswitch for now.
-        """
-        pass
-
-    def release(self):
+    def do_release(self):
         """ Delete the bridge and 
             close the servers
         """
         # Node needs to wait until all associated RMs are released
         # to be released
         from nepi.resources.planetlab.openvswitch.ovsport import OVSPort
-        rm = self.get_connected(OVSPort.rtype())
+        rm = self.get_connected(OVSPort.get_rtype())
 
-        if rm[0].state < ResourceState.FINISHED:
+        if rm[0].state < ResourceState.STOPPED:
             self.ec.schedule(reschedule_delay, self.release)
             return 
             
@@ -313,9 +307,8 @@ class OVSWitch(LinuxApplication):
                 sudo = True)
         
         if proc.poll():
-            self.fail()
             self.error(msg, out, err)
             raise RuntimeError, msg
-     
-        self._state = ResourceState.RELEASED
-        
+
+        super(OVSWitch, self).do_release()
+