Bug fixing and ordering openvswitch code
[nepi.git] / src / nepi / resources / planetlab / openvswitch / ovsport.py
index f4f7c7a..f2cfb3e 100644 (file)
 from nepi.execution.attribute import Attribute, Flags, Types
 from nepi.execution.resource import ResourceManager, clsinit_copy, \
         ResourceState
-from nepi.resources.planetlab.openvswitch.ovs import OVSSwitch        
+from nepi.resources.planetlab.openvswitch.ovs import PlanetlabOVSSwitch        
 from nepi.resources.planetlab.node import PlanetlabNode        
 from nepi.resources.linux.application import LinuxApplication
 
 import os
 
 @clsinit_copy                 
-class OVSPort(LinuxApplication):
+class PlanetlabOVSPort(LinuxApplication):
     """
     .. class:: Class Args :
       
@@ -72,39 +72,24 @@ class OVSPort(LinuxApplication):
         :type guid: int
     
         """
-        super(OVSPort, self).__init__(ec, guid)
-
-
+        super(PlanetlabOVSPort, self).__init__(ec, guid)
+        self._home = "ovsport-%s" % self.guid
         self._port_number = None
-        # in case of connection by tunnel       
-        self._remote_ip = None    
-
-    def log_message(self, msg):
-        return " guid %d - OVSPort - %s " % (self.guid, msg)
 
     @property
     def node(self):
         """ Node that run the switch and the ports
         """
-        rm_list = self.get_connected(OVSSwitch.get_rtype())
-        if rm_list:
-            for elt in rm_list:
-                node = elt.get_connected(PlanetlabNode.get_rtype())
-                if node: return node[0]
-        return node[0]
+        return self.ovsswitch.node
 
     @property
     def ovsswitch(self):
         """ Switch where the port is created
         """
-        ovsswitch = self.get_connected(OVSSwitch.get_rtype())
+        ovsswitch = self.get_connected(PlanetlabOVSSwitch.get_rtype())
         if ovsswitch: return ovsswitch[0]
         return None
         
-    @property
-    def remote_ip(self):
-        return self._remote_ip
-
     @property
     def port_number(self):
         return self._port_number
@@ -118,12 +103,10 @@ class OVSPort(LinuxApplication):
 
         """
         rm = self.ec.get_resource(guid)
-        if rm.get_rtype() in self._authorized_connections:
-            msg = "Connection between %s %s and %s %s accepted" % (self.get_rtype(), self._guid, rm.get_rtype(), guid)
-            self.debug(msg)
-            return True
-        msg = "Connection between %s %s and %s %s refused" % (self.get_rtype(), self._guid, rm.get_rtype(), guid)
-        self.debug(msg)
+        if rm.get_rtype() not in self._authorized_connections:
+            return False
+
+        return True
 
     def create_port(self):
         """ Create the desired port
@@ -141,48 +124,60 @@ class OVSPort(LinuxApplication):
             self.error(msg)
             raise AttributeError, msg
 
-        cmd = "sliver-ovs create-port %s %s" % (self.ovsswitch.get('bridge_name'),
-                                                self.get('port_name'))   
-        self.node.run(cmd, self.ovsswitch.ovs_checks, 
-                stderr = "stdout-%s" % self.get('port_name'), 
-                stdout = "stderr-%s" % self.get('port_name'),
-                sudo = True)
+        command = "sliver-ovs create-port %s %s" % (
+                self.ovsswitch.get('bridge_name'),
+                self.get('port_name'))   
+        
+        shfile = os.path.join(self.app_home, "create_port.sh")
+        try:
+            self.node.run_and_wait(command, self.run_home,
+                    shfile=shfile,
+                    sudo = True,
+                    stderr="port_stdout", 
+                    stdout="port_stderr",
+                    pidfile="port_pidfile",
+                    ecodefile="port_exitcode")
+        except RuntimeError:
+            msg = "Could not create ovs-port"           
+            self.debug(msg)
+            raise RuntimeError, msg
 
-        self.info("Created the port %s on switch %s" % (self.get('port_name'),
-                                             self.ovsswitch.get('bridge_name')))     
+        self.info("Created port %s on switch %s" % (
+            self.get('port_name'),
+            self.ovsswitch.get('bridge_name')))     
            
     def initiate_udp_connection(self, remote_endpoint, connection_app_home, 
             connection_run_home, cipher, cipher_key, bwlimit, txqueuelen):
         """ Get the local_endpoint of the port
         """
-
-        self._remote_ip = remote_endpoint.node.get("ip")
-
-        msg = "Discovering the number of the port %s" % self.get('port_name')
+        msg = "Discovering the port number for %s" % self.get('port_name')
         self.info(msg)
 
         command = "sliver-ovs get-local-endpoint %s" % self.get('port_name')
-        out = err = ""
-        (out, err), proc = self.node.run_and_wait(command, 
-                self.ovsswitch.ovs_checks,
-                shfile = "port_number-%s.sh" % self.get('port_name'),
-                pidfile = "port_number_pidfile-%s" % self.get('port_name'),
-                ecodefile = "port_number_exitcode-%s" % self.get('port_name'), 
-                sudo = True, 
-                stdout = "stdout-%s" % self.get('port_name'),    
-                stderr = "stderr-%s" % self.get('port_name'))
+
+        shfile = os.path.join(connection_app_home, "get_port.sh")
+        (out, err), proc = self.node.run_and_wait(command, connection_run_home,
+                shfile=shfile,
+                sudo=True, 
+                overwrite = True,
+                pidfile="get_port_pidfile",
+                ecodefile="get_port_exitcode", 
+                stdout="get_port_stdout",    
+                stderr="get_port_stderr")
 
         if err != "":
             msg = "Error retrieving the local endpoint of the port"
             self.error(msg)
-            raise AttributeError, msg
+            raise RuntimeError, msg
 
         if out:
-            self._port_number = int(out)
+            self._port_number = out.strip()
 
         self.info("The number of the %s is %s" % (self.get('port_name'), 
            self.port_number))
 
+        # Must set a routing rule in the ovs client nodes so they know
+        # that the LAN can be found through the switch
         if remote_endpoint.is_rm_instance("planetlab::Tap"):
             self._vroute = self.ec.register_resource("planetlab::Vroute")
             self.ec.set(self._vroute, "action", "add")
@@ -199,58 +194,38 @@ class OVSPort(LinuxApplication):
 
         return self.port_number
 
-
-    def establish_udp_connection(self,remote_endpoint, port):
-        establish_connection_command = self._establish_connection_command(port)
-
-        # upload command to connect.sh script
-        shfile = os.path.join(self.app_home, "sw-connect.sh")
-        self.node.upload_command(establish_connection_command,
-                shfile = shfile,
-                overwrite = False)
-
-        # invoke connect script
-        cmd = "bash %s" % shfile
-        (out, err), proc = self.node.run(cmd, self.run_home,
-                sudo  = True,
-                stdout = "sw_stdout",
-                stderr = "sw_stderr") 
-             
-        # check if execution errors occurred
-        msg = "Failed to connect endpoints "
-        if proc.poll():
-            self.error(msg, out, err)
-            raise RuntimeError, msg
-    
-        # Wait for pid file to be generated
-        self._pid, self._ppid = self.node.wait_pid(self.run_home)
-        
-        # If the process is not running, check for error information
-        # on the remote machine
-        if not self._pid or not self._ppid:
-            (out, err), proc = self.node.check_errors(self.run_home)
-            # Out is what was written in the stderr file
-            if err:
-                msg = " Failed to start command '%s' " % command
-                self.error(msg, out, err)
-                raise RuntimeError, msg
+    def establish_udp_connection(self, remote_endpoint,
+            connection_app_home,
+            connection_run_home, 
+            port):
+        remote_ip = remote_endpoint.node.get("ip")
+        command = self._establish_connection_command(port, remote_ip)
+
+        shfile = os.path.join(connection_app_home, "connect_port.sh")
+        (out, err), proc = self.node.run_and_wait(command, connection_run_home,
+                shfile=shfile,
+                sudo=True, 
+                overwrite = True,
+                pidfile="connect_port_pidfile",
+                ecodefile="connect_port_exitcode", 
+                stdout="connect_port_stdout",    
+                stderr="connect_port_stderr")
 
         # For debugging
         msg = "Connection on port configured"
         self.debug(msg)
 
-
-    def _establish_connection_command(self, port):
+    def _establish_connection_command(self, port, remote_ip):
         """ Script to create the connection from a switch to a 
              remote endpoint
         """
         local_port_name = self.get('port_name')
 
         command = ["sliver-ovs"]
-        command.append("set-remote-endpoint ")
-        command.append("%s " % local_port_name)
-        command.append("%s " % self.remote_ip)
-        command.append("%s " % port)
+        command.append("set-remote-endpoint")
+        command.append(local_port_name)
+        command.append(remote_ip)
+        command.append(port)
         command = " ".join(command)
         command = self.replace_paths(command)
         return command
@@ -264,17 +239,8 @@ class OVSPort(LinuxApplication):
     def check_status(self):
         return self.node.status(self._pid, self._ppid)
 
-    def do_deploy(self):
-        """ Deploy the OVS port after the OVS Switch
-        """
-
-        if not self.ovsswitch or self.ovsswitch.state < ResourceState.READY:       
-            self.debug("---- RESCHEDULING DEPLOY ---- OVSwitch state %s " % self.ovsswitch.state )  
-            self.ec.schedule(self.reschedule_delay, self.deploy)
-            return
-
-        self.do_discover()
-        self.do_provision()
+    def do_provision(self):
+        self.node.mkdir(self.run_home)
 
         self.create_port()
         end_ip = self.ovsswitch.get('virtual_ip_pref').split('/')
@@ -282,8 +248,20 @@ class OVSPort(LinuxApplication):
 
         #Check the status of the OVS Switch
         self.ovsswitch.ovs_status()
+    
+        self.set_provisioned()
+               
+    def do_deploy(self):
+        """ Deploy the OVS port after the OVS Switch
+        """
+        if not self.ovsswitch or self.ovsswitch.state < ResourceState.READY:       
+            self.debug("---- RESCHEDULING DEPLOY ---- OVSwitch state %s " % self.ovsswitch.state )  
+            self.ec.schedule(self.reschedule_delay, self.deploy)
+        else:
+            self.do_discover()
+            self.do_provision()
 
-        super(OVSPort, self).do_deploy()
+            self.set_ready()
 
     def do_release(self):
         """ Delete the port on the OVSwitch. It needs to wait for the tunnel
@@ -296,16 +274,19 @@ class OVSPort(LinuxApplication):
             self.ec.schedule(self.reschedule_delay, self.release)
             return 
             
-        cmd = "sliver-ovs del_port %s" % self.get('port_name')
-        (out, err), proc = self.node.run(cmd, self.ovsswitch.ovs_checks,
-                sudo = True)
-
         msg = "Deleting the port %s" % self.get('port_name')
         self.info(msg)
 
-        if proc.poll():
-            self.error(msg, out, err)
-            raise RuntimeError, msg
+        command = "sliver-ovs del_port %s" % self.get('port_name')
+
+        shfile = os.path.join(self.app_home, "stop.sh")
+        self.node.run_and_wait(command, self.run_home,
+                shfile=shfile,
+                sudo=True, 
+                pidfile="stop_pidfile",
+                ecodefile="stop_exitcode", 
+                stdout="stop_stdout", 
+                stderr="stop_stderr")
 
-        super(OVSPort, self).do_release()
+        super(PlanetlabOVSPort, self).do_release()