Merge pull request #25 from pingping-lin/ONOS-Next

Add SDN-IP test && update Quagga and ONOS-cli drivers
diff --git a/TestON/drivers/common/cli/onosclidriver.py b/TestON/drivers/common/cli/onosclidriver.py
index 615bf2a..1ca0a76 100644
--- a/TestON/drivers/common/cli/onosclidriver.py
+++ b/TestON/drivers/common/cli/onosclidriver.py
@@ -1129,6 +1129,46 @@
             main.cleanup()
             main.exit()
 
+    # This method should be used after installing application: onos-app-sdnip
+    def routes(self, json_format=False):
+        '''
+        Optional:
+            * json_format: enable output formatting in json
+        Description:
+            Obtain all routes in the system
+        '''
+        try:
+            if json_format:
+                self.handle.sendline("routes -j")
+                self.handle.expect("routes -j")
+                self.handle.expect("onos>")
+                handle_tmp = self.handle.before
+                
+                ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m')
+                handle = ansi_escape.sub('', handle_tmp)
+
+            else:
+                self.handle.sendline("")
+                self.handle.expect("onos>")
+
+                self.handle.sendline("routes")
+                self.handle.expect("onos>")
+                handle = self.handle.before
+
+            return handle
+
+        except pexpect.EOF:
+            main.log.error(self.name + ": EOF exception found")
+            main.log.error(self.name + ":    " + self.handle.before)
+            main.cleanup()
+            main.exit()
+        except:
+            main.log.info(self.name + " ::::::")
+            main.log.error(traceback.print_exc())
+            main.log.info(self.name + " ::::::")
+            main.cleanup()
+            main.exit()
+
     def intents(self, json_format = False):
         '''
         Optional:
diff --git a/TestON/drivers/common/cli/quaggaclidriver.py b/TestON/drivers/common/cli/quaggaclidriver.py
index 744d284..7851d13 100644
--- a/TestON/drivers/common/cli/quaggaclidriver.py
+++ b/TestON/drivers/common/cli/quaggaclidriver.py
@@ -14,12 +14,15 @@
     def __init__(self):
         super(CLI, self).__init__()
 
+    #TODO: simplify this method
     def connect(self, **connectargs):
         for key in connectargs:
             vars(self)[key] = connectargs[key]
         
         self.name = self.options['name']
-        self.handle = super(QuaggaCliDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd)
+        #self.handle = super(QuaggaCliDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd)
+        self.handle = super(QuaggaCliDriver, self).connect(user_name=self.user_name, ip_address="1.1.1.1", port=self.port, pwd=self.pwd)
+        main.log.info("connect parameters:" + str(self.user_name) + ";" + str(self.ip_address) + ";" + str(self.port) + ";" + str(self.pwd))
 
         if self.handle: 
             self.handle.expect("")
@@ -35,7 +38,33 @@
             main.log.info("NO HANDLE")
             return main.FALSE
 
+    def loginQuagga(self, ip_address):
+
+        self.name = self.options['name']
+        self.handle = super(QuaggaCliDriver, self).connect(
+            user_name=self.user_name, ip_address=ip_address,
+            port=self.port, pwd=self.pwd)
+        main.log.info("connect parameters:" + str(self.user_name) + ";"
+        + str(self.ip_address) + ";" + str(self.port) + ";" + str(self.pwd))
+
+        if self.handle:
+            self.handle.expect("")
+            self.handle.expect("\$")
+            self.handle.sendline("telnet localhost 2605")
+            self.handle.expect("Password:", timeout=5)
+            self.handle.sendline("hello")
+            self.handle.expect("bgpd", timeout=5)
+            self.handle.sendline("enable")
+            self.handle.expect("bgpd#", timeout=5)
+            main.log.info("I am inside BGP peer Quagga!")
+
+            return self.handle
+        else:
+            main.log.info("NO HANDLE")
+            return main.FALSE
+
     def enter_config(self, asn):
+        main.log.info("I am in enter_config method!")
         try:
             self.handle.sendline("")
             self.handle.expect("bgpd#")
@@ -52,6 +81,63 @@
             return main.TRUE
         except:
             return main.FALSE
+
+    def generate_routes(self, net, numRoutes):
+        main.log.info("I am in generate_routes method!")
+        
+        # a IP prefix will be composed by "net" + "." + m + "." + n + "." + x 
+        # the length of each IP prefix is 24
+        routes = []
+        routes_gen = 0
+        m = numRoutes / 256
+        n = numRoutes % 256
+
+        for i in range(0, m):
+            for j in range(0, 256):
+                network = str(net) + "." + str(i) + "." + str(j) + ".0/24"
+                routes.append(network)
+                routes_gen = routes_gen + 1
+
+        for j in range(0, n):
+            network = str(net) + "." + str(m) + "." + str(j) + ".0/24"
+            routes.append(network)
+            routes_gen = routes_gen + 1
+
+        if routes_gen == numRoutes:
+            main.log.info("Successfully generated " + str(numRoutes)
+            + " routes!")
+            return routes
+        return main.FALSE
+    
+    def add_routes(self, routes, routeRate):
+        main.log.info("I am in add_routes method!")
+        
+        routes_added = 0
+        try:
+            self.handle.sendline("")
+            #self.handle.expect("config-router")
+            self.handle.expect("config-router", timeout=5)
+        except:
+            main.log.warn("Probably not in config-router mode!")
+            self.disconnect()
+        main.log.info("Start to add routes")
+
+        for i in range(0, len(routes)):
+            routeCmd = "network " + routes[i]
+            try:
+                self.handle.sendline(routeCmd)
+                self.handle.expect("bgpd", timeout=5)
+            except:
+                main.log.warn("Failed to add route")
+                self.disconnect()
+            waitTimer = 1.00 / routeRate
+            time.sleep(waitTimer)
+        if routes_added == len(routes):
+            main.log.info("Finished adding routes")
+            return main.TRUE
+        return main.FALSE
+    
+    # please use the generate_routes plus add_routes instead of this one
     def add_route(self, net, numRoutes, routeRate):
         try:
             self.handle.sendline("")
diff --git a/TestON/tests/SdnIpTest/SdnIpTest.params b/TestON/tests/SdnIpTest/SdnIpTest.params
new file mode 100755
index 0000000..9d3627a
--- /dev/null
+++ b/TestON/tests/SdnIpTest/SdnIpTest.params
@@ -0,0 +1,25 @@
+<PARAMS>
+    
+    <testcases>1</testcases>
+
+    #Environment variables
+    <ENV>
+        <cellName>driver_test</cellName>
+    </ENV>
+
+    <CTRL>
+        <ip1>127.0.0.1</ip1>
+        <port1>6633</port1>
+    </CTRL>
+
+    <GIT>
+        <autoPull>off</autoPull>
+        <checkout>master</checkout>
+    </GIT>
+
+    <JSON>
+        <prefix>prefix</prefix>
+        <nextHop>nextHop</nextHop>
+    </JSON>
+
+</PARAMS>
diff --git a/TestON/tests/SdnIpTest/SdnIpTest.py b/TestON/tests/SdnIpTest/SdnIpTest.py
new file mode 100755
index 0000000..18b23af
--- /dev/null
+++ b/TestON/tests/SdnIpTest/SdnIpTest.py
@@ -0,0 +1,90 @@
+
+#Testing the basic functionality of SDN-IP
+
+class SdnIpTest:
+    def __init__(self):
+        self.default = ''
+
+    def CASE1(self, main):
+
+        '''
+        Test the SDN-IP functionality
+        '''
+        import time
+        import json
+        from operator import eq
+        
+        # all generated routes for all BGP peers
+        allRoutes = []
+
+        main.step("Start to generate routes for BGP peer on host1")
+        routes = main.Quaggacli.generate_routes(1, 3)
+        main.log.info(routes)
+        
+        for route in routes:
+            allRoutes.append(route + "/" + "1.1.1.1")
+
+        # start to insert routes into the bgp peer
+        main.step("Start Quagga-cli on host1")
+        main.Quaggacli.loginQuagga("1.1.1.1")
+
+        main.step("Start to configure Quagga on host1")
+        main.Quaggacli.enter_config(64513)
+    
+        main.step("Start to generate and add routes for BGP peer on host1")    
+        routes = main.Quaggacli.generate_routes(8, 3)
+        main.Quaggacli.add_routes(routes, 1)
+             
+        # add generates routes to allRoutes
+        for route in routes:
+            allRoutes.append(route + "/" + "1.1.1.1")
+
+        cell_name = main.params['ENV']['cellName']
+        ONOS1_ip = main.params['CTRL']['ip1']
+
+        main.step("Starting ONOS service")
+        # TODO: start ONOS from Mininet Script
+        # start_result = main.ONOSbench.onos_start("127.0.0.1")
+
+        main.step("Set cell for ONOS-cli environment")
+        main.ONOScli.set_cell(cell_name)
+   
+        main.step("Start ONOS-cli")
+        # TODO: change the hardcode in start_onos_cli method in ONOS CLI driver
+        time.sleep(5)
+        main.ONOScli.start_onos_cli(ONOS1_ip)    
+
+        main.step("Get devices in the network")
+        list_result = main.ONOScli.devices(json_format=False)
+        main.log.info(list_result)
+
+        #get all routes inside SDN-IP
+        get_routes_result = main.ONOScli.routes(json_format=True)  
+                     
+        # parse routes from ONOS CLI
+        routes_list = []
+        routes_json_obj = json.loads(get_routes_result)   
+        for route in routes_json_obj:
+            if route['prefix'] == '172.16.10.0/24':
+                continue
+            routes_list.append(route['prefix'] + "/" + route['nextHop'])
+      
+        main.log.info("Start to checking routes")
+        main.log.info("Routes inserted into the system:")
+        main.log.info(sorted(allRoutes))
+        main.log.info("Routes get from ONOS CLI:")
+        main.log.info(sorted(routes_list))
+        utilities.assert_equals(expect=sorted(allRoutes), actual=sorted(routes_list),
+                onpass="***Routes in SDN-IP are correct!***",
+                onfail="***Routes in SDN-IP are wrong!***")
+        
+        time.sleep(2)
+        main.step("Get intents installed on ONOS")
+        get_intents_result = main.ONOScli.intents(json_format=True)
+        main.log.info(get_intents_result)
+
+
+        #main.step("Test whether Mininet is started")
+        #main.Mininet2.handle.sendline("xterm host1")
+        #main.Mininet2.handle.expect("mininet>")
+
diff --git a/TestON/tests/SdnIpTest/SdnIpTest.topo b/TestON/tests/SdnIpTest/SdnIpTest.topo
new file mode 100755
index 0000000..1d23878
--- /dev/null
+++ b/TestON/tests/SdnIpTest/SdnIpTest.topo
@@ -0,0 +1,41 @@
+<TOPOLOGY>
+    <COMPONENT>
+
+        <ONOSbench>
+            <host>127.0.0.1</host>
+            <user>sdn</user>
+            <password>rocks</password>
+            <type>OnosDriver</type>
+            <connect_order>1</connect_order>
+            <COMPONENTS> </COMPONENTS>
+        </ONOSbench>
+
+        <ONOScli>
+            <host>127.0.0.1</host>
+            <user>sdn</user>
+            <password>rocks</password>
+            <type>OnosCliDriver</type>
+            <connect_order>2</connect_order>
+            <COMPONENTS> </COMPONENTS>
+        </ONOScli>
+
+        <ONOS1>
+            <host>127.0.0.1</host>
+            <user>sdn</user>
+            <password>rocks</password>
+            <type>OnosDriver</type>
+            <connect_order>3</connect_order>
+            <COMPONENTS> </COMPONENTS>
+        </ONOS1>
+
+      	<Quaggacli>
+            <host>127.0.0.1</host>
+            <user>username</user>
+            <password>password</password>
+            <type>QuaggaCliDriver</type>
+            <connect_order>5</connect_order>
+            <COMPONENTS> </COMPONENTS>
+        </Quaggacli>
+
+    </COMPONENT>
+</TOPOLOGY>
diff --git a/TestON/tests/SdnIpTest/__init__.py b/TestON/tests/SdnIpTest/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/TestON/tests/SdnIpTest/__init__.py