Delete the OVSTunnel to use the UdpTunnel instead. Just need to fix an issue with...
[nepi.git] / src / nepi / resources / planetlab / openvswitch / ovsport.py
index 6571358..8275249 100644 (file)
@@ -26,6 +26,8 @@ from nepi.resources.planetlab.openvswitch.ovs import OVSSwitch
 from nepi.resources.planetlab.node import PlanetlabNode        
 from nepi.resources.linux.application import LinuxApplication
 
+import os
+
 reschedule_delay = "0.5s"
 
 @clsinit_copy                 
@@ -44,7 +46,7 @@ class OVSPort(LinuxApplication):
     _help = "Runs an OpenVSwitch on a PlanetLab host"
     _backend = "planetlab"
 
-    _authorized_connections = ["OVSSwitch", "OVSTunnel"]      
+    _authorized_connections = ["OVSSwitch", "LinuxUdpTunnel", "LinuxTunnel"]      
 
     @classmethod
     def _register_attributes(cls):
@@ -53,8 +55,16 @@ class OVSPort(LinuxApplication):
         """
         port_name = Attribute("port_name", "Name of the port",
             flags = Flags.Design)                      
+        endpoint_ip = Attribute("endpoint_ip", "IP of the endpoint. This is the attribute " 
+                                "you should use to establish a tunnel or a remote "
+                                "connection between endpoint",
+            flags = Flags.Design)
+        network = Attribute("network", "Network used by the port",
+            flags = Flags.Design)      
 
         cls._register_attribute(port_name)
+        cls._register_attribute(endpoint_ip)
+        cls._register_attribute(network)
 
     def __init__(self, ec, guid):
         """
@@ -65,8 +75,11 @@ class OVSPort(LinuxApplication):
     
         """
         super(OVSPort, self).__init__(ec, guid)
+
+
         self._port_number = None
-        self.port_info = []         
+        # in case of connection by tunnel       
+        self._remote_ip = None    
 
     def log_message(self, msg):
         return " guid %d - OVSPort - %s " % (self.guid, msg)
@@ -90,6 +103,10 @@ class OVSPort(LinuxApplication):
         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
@@ -136,12 +153,15 @@ class OVSPort(LinuxApplication):
         self.info("Created the port %s on switch %s" % (self.get('port_name'),
                                              self.ovsswitch.get('bridge_name')))     
            
-    def get_local_end(self):
+    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')
-        self.debug(msg)
+        self.info(msg)
 
         command = "sliver-ovs get-local-endpoint %s" % self.get('port_name')
         out = err = ""
@@ -164,39 +184,88 @@ class OVSPort(LinuxApplication):
 
         self.info("The number of the %s is %s" % (self.get('port_name'), 
            self.port_number))
-   
-    def set_port_info(self):
-        """ Set all the information about the port inside a list
-        """
 
-        info = []
-        info.append(self.node.get('hostname'))
+        if remote_endpoint.is_rm_instance("PlanetlabTap"):
+            self._vroute = self.ec.register_resource("PlanetlabVroute")
+            self.ec.set(self._vroute, "action", "add")
+            self.ec.set(self._vroute, "network", self.get("network"))
+
+            print "Vroute Guid :" + str(self._vroute)
+
+            self.ec.register_connection(self._vroute, remote_endpoint.guid)
+            self.ec.deploy(guids=[self._vroute], group = self.deployment_group)
+
+            # For debugging
+            msg = "Route for the tap configured"
+            self.debug(msg)
+
+        return self.port_number
+
 
-        #Return the ip of the node
-        import socket
-        ip = socket.gethostbyname(self.node.get('hostname'))
-        info.append(ip)
+    def establish_udp_connection(self,remote_endpoint, port):
+        establish_connection_command = self._establish_connection_command(port)
 
-        info.append(self.get('port_name'))
-        info.append(self.ovsswitch.get('virtual_ip_pref'))
-        info.append(self.port_number)
-        return info
+        # 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)
 
-    def switch_connect_command(self, local_port_name, 
-            remote_ip, remote_port_num):
+        # 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
+
+        # For debugging
+        msg = "Connection on port configured"
+        self.debug(msg)
+
+
+    def _establish_connection_command(self, port):
         """ 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 " % remote_ip)
-        command.append("%s " % remote_port_num)
+        command.append("%s " % self.remote_ip)
+        command.append("%s " % port)
         command = " ".join(command)
         command = self.replace_paths(command)
         return command
         
+    def verify_connection(self):
+        self.ovsswitch.ovs_status()
+
+    def terminate_connection(self):
+        return True
+
+    def check_status(self):
+        return self.node.status(self._pid, self._ppid)
+
     def do_deploy(self):
         """ Deploy the OVS port after the OVS Switch
         """
@@ -210,25 +279,22 @@ class OVSPort(LinuxApplication):
         self.do_provision()
 
         self.create_port()
-        self.get_local_end()
+        end_ip = self.ovsswitch.get('virtual_ip_pref').split('/')
+        self.set("endpoint_ip", end_ip[0])
 
         #Check the status of the OVS Switch
         self.ovsswitch.ovs_status()
 
-        # Save all the information inside a list
-        self.port_info = self.set_port_info()
-
         super(OVSPort, self).do_deploy()
 
     def do_release(self):
         """ Delete the port on the OVSwitch. It needs to wait for the tunnel
         to be released.
         """
+        from nepi.resources.linux.udptunnel import LinuxUdpTunnel
+        rm = self.get_connected(LinuxUdpTunnel.get_rtype())
 
-        from nepi.resources.planetlab.openvswitch.tunnel import OVSTunnel
-        rm = self.get_connected(OVSTunnel.get_rtype())
-
-        if rm and rm[0].state < ResourceState.RELEASED:
+        if rm and rm[0].state < ResourceState.STOPPED:
             self.ec.schedule(reschedule_delay, self.release)
             return