Merge "Bgpls scripts updated with Links"
diff --git a/TestON/tests/FUNC/FUNCbgpls/FUNCbgpls.params b/TestON/tests/FUNC/FUNCbgpls/FUNCbgpls.params
index 34dba48..89202a5 100755
--- a/TestON/tests/FUNC/FUNCbgpls/FUNCbgpls.params
+++ b/TestON/tests/FUNC/FUNCbgpls/FUNCbgpls.params
@@ -5,14 +5,15 @@
 #CASE1: Compile ONOS and push it to the test machines
 #CASE2: Discovery the topology using BGPLS
 #CASE3: Addition of new Node to existing topology
-#CASE4: Deletion of Node
-#Case5: Uninstalling the app
+#CASE4: Verification of Links thats is discovered"
+#CASE5: Deletion of Links
+#Case6: Uninstalling the app
 
 
-    <testcases>1,2,3,4,5</testcases>
+    <testcases>1,2,3,4,5,6</testcases>
 
     <DEPENDENCY>
-        <path>/tests/FUNCbgpls/Dependencies/</path>
+        <path>/tests/FUNC/FUNCbgpls/Dependencies/</path>
     </DEPENDENCY>
 
     <ENV>
diff --git a/TestON/tests/FUNC/FUNCbgpls/FUNCbgpls.py b/TestON/tests/FUNC/FUNCbgpls/FUNCbgpls.py
index a56c5c4..db60aad 100755
--- a/TestON/tests/FUNC/FUNCbgpls/FUNCbgpls.py
+++ b/TestON/tests/FUNC/FUNCbgpls/FUNCbgpls.py
@@ -8,8 +8,9 @@
 CASE1: Compile ONOS and push it to the test machines
 CASE2: Discovery the topology using BGPLS
 CASE3: Addition of new Node to existing topology
-CASE4: Deletion of Node
-Case5: Uninstalling the app
+CASE4: Verification of Links thats is discovered"
+CASE5: Deletion of Links
+Case6: Uninstalling the app
 
 
 """
@@ -275,7 +276,7 @@
         bgplsConfig.Comments()
 
         main.Scapy1.handle.sendline( "\x03" )
-        time.sleep(90) #This Sleep time gives time for the socket to close.
+        time.sleep( 90 ) #This Sleep time gives time for the socket to close.
 
 
 
@@ -365,24 +366,23 @@
             stepResult = main.FALSE
         utilities.assert_equals( expect=main.TRUE,
                                  actual=stepResult,
-                                 onpass="Node " + str( Ne_id[1][3] ) + " update  sucess",
-                                 onfail="Node " + str( Ne_id[1][3]) + " update failed" )
+                                 onpass="Node " + str( Ne_id[ 1 ][ 3 ] ) + " update  sucess",
+                                 onfail="Node " + str( Ne_id[ 1 ][ 3 ] ) + " update failed" )
         bgplsConfig.Comments()
         main.log.info( "Kill Scapy process" )
         bgplsConfig.Comments()
         main.Scapy1.handle.sendline( "\x03" )
-
+        time.sleep( 90 ) #This Sleep time gives time for the socket to close.
 
 
     def CASE4( self, main ):
         """
-        Deletion of  Node
+        Verification of Links in existing topology
         """
-        import os , sys
-        import re
+        import json
         import time
-        main.case( "TestCase 4: Deletion of Node from existing Topology" )
-
+        import os
+        main.case( "Testcase 4: Verification of Links thats is discovered" )
         try:
             from tests.FUNC.FUNCbgpls.dependencies.Nbdata import BgpLs
         except ImportError:
@@ -392,30 +392,14 @@
             main.exit()
 
         bgplsConfig = BgpLs()
-        Ne_id = bgplsConfig.Constants()
         app = bgplsConfig.apps()
         main.CLIs = []
         main.nodes = []
         main.numCtrls= 1
-
-        bgplsConfig.Comments()
-        main.log.info( "Blocked due to this defect : ONOS-3920 " )
-        bgplsConfig.Comments()
-
-        '''
         ipList = os.getenv( main.params[ 'CTRL' ][ 'ip1' ] )
         scapy_ip = os.getenv(main.params ['SCAPY'] ['HOSTNAMES'] )
-        cellName = main.params[ 'ENV' ][ 'cellName' ]
-        cellAppString= main.params[ 'ENV' ][ 'cellApps' ]
-        httpport = main.params['HTTP']['port']
-        path = main.params['HTTP']['path']
-        bgplsConfig = BgpLs()
         bgplsConfig.ipValue(ipList,scapy_ip)
 
-        main.ONOSbench.createCellFile( main.ONOSbench.ip_address, cellName,
-                                       scapy_ip,
-                                       cellAppString, ipList , onosUser="karaf" )
-
         for i in range( 1, main.numCtrls + 1 ):
             try:
                 main.CLIs.append( getattr( main, 'ONOScli' + str( i ) ) )
@@ -424,93 +408,120 @@
             except AttributeError:
                 break
 
-        main.step( "Apply cell to environment" )
         bgplsConfig.Comments()
-        cellResult = main.ONOSbench.setCell( cellName )
-
-        bgplsConfig.Comments()
-        main.log.info( "Sending BGPLS information " )
+        main.log.info( "Sending BGPLS Link information Packet " )
         bgplsConfig.Comments()
 
-
-        main.Scapy1.handle.sendline( "sudo python  OnosSystemTest/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Deletion_Node.py" )
-        #main.Scapy1.handle.expect( "sdn:" )
-        #main.Scapy1.handle.sendline( "rocks" )
-
-
+        main.Scapy1.handle.sendline( "sudo python  OnosSystemTest/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Link_Update_Node.py" )
         bgplsConfig.Comments()
         main.log.info( "Enable BGPlS plugin in ONOS" )
         bgplsConfig.Comments()
 
+        main.step( "UnInstall onos-app-bgp" )
+        installResults = main.ONOScli1.deactivateApp( app[ 0 ] )
+        utilities.assert_equals( expect=main.TRUE, actual=installResults,
+                                 onpass="Uninstall  onos-app-bgp successful",
+                                 onfail="Uninstall  onos-app-bgp failed" )
 
-        cliResults = main.ONOScli1.startOnosCli( main.nodes[0].ip_address)
-
-        main.step( "Getting connected to ONOS" )
-        utilities.assert_equals( expect=main.TRUE, actual=cliResults,
-                                 onpass="ONOS cli startup successful",
-                                 onfail="ONOS cli startup failed" )
-        installResults = main.ONOScli1.activateApp( app[0])
-
+        installResults = main.ONOScli1.activateApp( app[ 0 ])
         main.step( "Install onos-app-bgp" )
         utilities.assert_equals( expect=main.TRUE, actual=installResults,
                                  onpass="Install onos-app-bgp successful",
                                  onfail="Install onos-app-bgp failed" )
-
-        main.step( "Install onos-app-bgpflow" )
-        installResults = main.ONOScli1.activateApp( app[1] )
-        utilities.assert_equals( expect=main.TRUE, actual=installResults,
-                                 onpass="Install onos-app-bgpflow successful",
-                                 onfail="Install onos-app-bgpflow failed" )
-
-
-        bgpls_post = bgplsConfig.DictoJson()
-
         bgplsConfig.Comments()
-        main.log.info( "BGPLS RestConf input" )
+        main.log.info( "Checking the Link Discovery Status" )
         bgplsConfig.Comments()
+        time.sleep( 120 )   # Time taken to discovery the links
+        response = main.ONOScli1.links()
+        linksResp = json.loads( response )
+        check_link = bgplsConfig.checkLinks( linksResp )
 
-        print (bgpls_post)
-        main.ONOSrest.user_name = "karaf"
-        main.ONOSrest.pwd = "karaf"
-        Poststatus, result = main.ONOSrest.send( ipList,httpport,'', path + 'v1/network/configuration/',
-                                                 'POST', None, bgpls_post, debug=True )
-
-        main.step( "Configure BGP through RESTCONF" )
-        utilities.assert_equals(
-                expect='200',
-                actual=Poststatus,
-                onpass="Post Port Success",
-                onfail="Post Port Failed " + str( Poststatus ) + "," + str( result ) )
-
-
-        bgplsConfig.Comments()
-        main.log.info( "Check Network devices is deleted from  ONOS " )
-        bgplsConfig.Comments()
-        time.sleep(15)
-
-        response = main.ONOScli1.devices()
-
-        main.step( "Check whehther Node is deleted successfully" )
-
-        if response.find(Ne_id[3]) != -1:
-            stepResult = main.TRUE
-        else:
-            stepResult = main.FALSE
-        utilities.assert_equals( expect=main.FALSE,
-                                 actual=stepResult,
-                                 onpass="Node " + str( Ne_id[3] ) + " Deletion sucess",
-                                 onfail="Node " + str( Ne_id[3] ) + " Deletion  failed" )
-
+        if check_link == True:
+            reply_Check_Link = main.TRUE
+        utilities.assert_equals( expect= main.TRUE,
+                                     actual=reply_Check_Link ,
+                                     onpass="Link  Discovery Success.",
+                                     onfail="Link  Discovery Failed." )
         bgplsConfig.Comments()
         main.log.info( "Kill Scapy process" )
         bgplsConfig.Comments()
-
         main.Scapy1.handle.sendline( "\x03" )
-        '''
-
+        time.sleep( 90 )
 
     def CASE5( self, main ):
         """
+        Deletion of  links
+        """
+        import json
+        import time
+        import os
+        main.case( "Testcase 5: Deletion of Link in existing topology" )
+        try:
+            from tests.FUNC.FUNCbgpls.dependencies.Nbdata import BgpLs
+        except ImportError:
+            main.log.exception( "Something wrong with import file or code error." )
+            main.log.info( "Import Error, please check!" )
+            main.cleanup()
+            main.exit()
+
+        bgplsConfig = BgpLs()
+        app = bgplsConfig.apps()
+        main.CLIs = []
+        main.nodes = []
+        main.numCtrls= 1
+        ipList = os.getenv( main.params[ 'CTRL' ][ 'ip1' ] )
+        scapy_ip = os.getenv(main.params [ 'SCAPY' ] [ 'HOSTNAMES' ] )
+        bgplsConfig.ipValue(ipList,scapy_ip)
+
+        for i in range( 1, main.numCtrls + 1 ):
+            try:
+                main.CLIs.append( getattr( main, 'ONOScli' + str( i ) ) )
+                main.nodes.append( getattr( main, 'ONOS' + str( i ) ) )
+                ipList.append( main.nodes[ -1 ].ip_address )
+            except AttributeError:
+                break
+
+        bgplsConfig.Comments()
+        main.log.info( "Sending BGPLS Delete Link Packet " )
+        bgplsConfig.Comments()
+
+        main.Scapy1.handle.sendline( "sudo python  OnosSystemTest/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Deletion_Node.py" )
+        bgplsConfig.Comments()
+        main.log.info( "Enable BGPlS plugin in ONOS" )
+        bgplsConfig.Comments()
+
+        main.step( "UnInstall onos-app-bgp" )
+        installResults = main.ONOScli1.deactivateApp( app[ 0 ] )
+        utilities.assert_equals( expect=main.TRUE, actual=installResults,
+                                 onpass="Uninstall  onos-app-bgp successful",
+                                 onfail="Uninstall  onos-app-bgp failed" )
+
+        installResults = main.ONOScli1.activateApp( app[ 0 ])
+        main.step( "Install onos-app-bgp" )
+        utilities.assert_equals( expect=main.TRUE, actual=installResults,
+                                 onpass="Install onos-app-bgp successful",
+                                 onfail="Install onos-app-bgp failed" )
+        bgplsConfig.Comments()
+        main.log.info( "Checking whether the links is deleted" )
+        bgplsConfig.Comments()
+        time.sleep( 120 )  # Time taken to discovery the links
+        response = main.ONOScli1.links()
+        linksResp = json.loads( response )
+        check_link = bgplsConfig.checkLinks( linksResp )
+        if check_link == False:
+            reply_Check_Link = main.TRUE
+        utilities.assert_equals( expect= main.TRUE,
+                                     actual=reply_Check_Link ,
+                                     onpass="Link  is Deleted Successfully.",
+                                     onfail="Link  is Deletion Failed." )
+        bgplsConfig.Comments()
+        main.log.info( "Kill Scapy process" )
+        bgplsConfig.Comments()
+        main.Scapy1.handle.sendline( "\x03" )
+        time.sleep( 90 )
+
+    def CASE6( self, main ):
+        """
         Uninstalling the app
         """
         import os,sys
@@ -542,7 +553,7 @@
         bgplsConfig.ipValue(ipList,scapy_ip)
         main.ONOSbench.createCellFile( main.ONOSbench.ip_address, cellName,
                                        scapy_ip,
-                                       cellAppString, ipList , onosUser="karaf" )
+                                       cellAppString, ipList )
 
         for i in range( 1, main.numCtrls + 1 ):
             try:
@@ -574,5 +585,9 @@
                                  onpass="Uninstall  onos-app-bgp successful",
                                  onfail="Uninstall  onos-app-bgp failed" )
 
-
-
+        main.log.info( "Check for Errors or Exception End of the Script" )
+        errorLog = main.ONOSbench.logReport( main.nodes[0].ip_address, ["ERROR",\
+                                            "EXCEPT"] )
+        utilities.assert_equals( expect= 0, actual=errorLog,
+                                 onpass="No Exception or Error occured",
+                                 onfail="Exception or Error occured" )
\ No newline at end of file
diff --git a/TestON/tests/FUNC/FUNCbgpls/README b/TestON/tests/FUNC/FUNCbgpls/README
index 2ecb554..8974506 100755
--- a/TestON/tests/FUNC/FUNCbgpls/README
+++ b/TestON/tests/FUNC/FUNCbgpls/README
@@ -28,7 +28,7 @@
 1] We use Scapy tool, to send the BGPLS packet to ONOS.
 2] The packets are already pre-captured.
 3] ONOS reads those packet and displays the topology.
-4] We query the devices information in ONOS to check the devices/links are learn
+4] We query the devices information in ONOS to check the devices/links are learned
 
 
 Steps :
diff --git a/TestON/tests/FUNC/FUNCbgpls/dependencies/Bgpls_packets/Topo_pkts.pcap b/TestON/tests/FUNC/FUNCbgpls/dependencies/Bgpls_packets/Topo_pkts.pcap
deleted file mode 100755
index 2021f22..0000000
--- a/TestON/tests/FUNC/FUNCbgpls/dependencies/Bgpls_packets/Topo_pkts.pcap
+++ /dev/null
Binary files differ
diff --git a/TestON/tests/FUNC/FUNCbgpls/dependencies/Bgpls_packets/bgpls.pcap b/TestON/tests/FUNC/FUNCbgpls/dependencies/Bgpls_packets/bgpls.pcap
new file mode 100644
index 0000000..8e3db7d
--- /dev/null
+++ b/TestON/tests/FUNC/FUNCbgpls/dependencies/Bgpls_packets/bgpls.pcap
Binary files differ
diff --git a/TestON/tests/FUNC/FUNCbgpls/dependencies/Nbdata.py b/TestON/tests/FUNC/FUNCbgpls/dependencies/Nbdata.py
index 8a83748..5661cb8 100755
--- a/TestON/tests/FUNC/FUNCbgpls/dependencies/Nbdata.py
+++ b/TestON/tests/FUNC/FUNCbgpls/dependencies/Nbdata.py
@@ -78,3 +78,47 @@
         self.app_bgpflow = 'org.onosproject.bgpflow'
         self.list1 = [self.app_bgp,self.app_bgpflow]
         return self.list1
+
+    def checkLinks(self,linksResp):
+        #Declaring the links values
+        links = {'link1_src' : "1650.5555.0055", 'link1_dst' : "1660.6666.0066",
+             'link2_src' : "1630.3333.0033", 'link2_dst' : "1620.2222.0022",
+             'link3_src' : "1660.6666.0066", 'link3_dst' : "1650.5555.0055",
+             'link4_src' : "1630.3333.0033", 'link4_dst' : "1650.5555.0055",
+             'link5_src' : "1640.4444.0044", 'link5_dst' : "1610.1111.0011",
+             'link6_src' : "1650.5555.0055", 'link4_dst' : "1630.3333.0033",
+             'link7_src' : "1620.2222.0022", 'link4_dst' : "1630.3333.0033",
+             'link8_src' : "1620.2222.0022", 'link4_dst' : "1610.1111.0011",
+             'link9_src' : "1630.3333.0033", 'link4_dst' : "1640.4444.0044",
+             'link10_src' : "1650.5555.0055", 'link4_dst' : "1640.4444.0044",
+             'link11_src' : "1610.1111.0011", 'link4_dst' : "1640.4444.0044",
+             'link12_src' : "1640.4444.0044", 'link4_dst' : "1620.2222.0022",
+             'link13_src' : "1660.6666.0066", 'link4_dst' : "1630.3333.0033",
+             'link14_src' : "1640.4444.0044", 'link4_dst' : "1660.6666.0066",
+             'link15_src' : "1640.4444.0044", 'link4_dst' : "1630.3333.0033",
+             'link16_src' : "1610.1111.0011", 'link4_dst' : "1630.3333.0033",
+             'link17_src' : "1630.3333.0033", 'link4_dst' : "1610.1111.0011",
+             'link18_src' : "1610.1111.0011", 'link4_dst' : "1620.2222.0022",
+             'link19_src' : "1620.2222.0022", 'link4_dst' : "1640.4444.0044",
+             'link20_src' : "1630.3333.0033", 'link4_dst' : "1660.6666.0066",
+             'link21_src' : "1640.4444.0044", 'link4_dst' : "1650.5555.0055",
+             'link22_src' : "1660.6666.0066", 'link4_dst' : "1640.4444.0044"
+        }
+
+        #Comparing the Links
+        for x in xrange(22):
+            link_src_info = linksResp[x]['src']['device']
+            link_dst_info = linksResp[x]['dst']['device']
+            link_src_split = link_src_info.split("=")
+            link_src = link_src_split[4]
+            link_dst_split = link_dst_info.split("=")
+            link_dst = link_dst_split[4]
+            y = x+1
+            link_src_ref = links['link'+str(y)+'_src']
+            link_dst_ref = links['link'+str(y)+'_dst']
+            if (link_src == link_src_ref) and (link_dst == (link_dst_ref) and \
+                linksResp[x]['type'] == 'DIRECT' and linksResp[x]['state'] == \
+                'ACTIVE'):
+                return True
+            else:
+                return False
diff --git a/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Deletion_Node.py b/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Deletion_Node.py
index abd1cfc..259f846 100755
--- a/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Deletion_Node.py
+++ b/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Deletion_Node.py
@@ -18,36 +18,35 @@
 host = peerIp # Get local machine name
 port = 179                # Reserve a port for your service.
 s.bind((host, port))        # Bind to the port
-pkts = rdpcap(path + "/OnosSystemTest/TestON/tests/FUNC/FUNCbgpls/dependencies/Bgpls_packets/bgpls_all.pcap")
-pkts[69][BGPOpen].bgp_id = peerIp
+pkts = rdpcap(path + "/OnosSystemTest/TestON/tests/FUNC/FUNCbgpls/dependencies/Bgpls_packets/bgpls.pcap")
+time.sleep(15)
+pkts[759][BGPOpen].bgp_id = peerIp
 
 s.listen(5)                   # Now wait for client connection.
 
 print("starting Connecting to ONOS peer")
 c, addr = s.accept()     # Establish connection with client.
 print 'Got connection from ONOS :', addr
-c.send(str(pkts[69][BGPHeader])) # OPEN MESSAGE
+c.send(str(pkts[759][BGPHeader])) # OPEN MESSAGE
 c.recv(4096)
-c.send(str(pkts[71][BGPHeader]))# KEEPALIVE MESSAGE
+c.send(str(pkts[765][BGPHeader]))# KEEPALIVE MESSAGE
 c.recv(4096)
-c.send(str(pkts[72][BGPHeader]))   # UPDATE MESSAGES
-c.send(str(pkts[74][BGPHeader]))
-c.send(str(pkts[71][BGPHeader]))
+c.send(str(pkts[768][BGPHeader]))   # UPDATE MESSAGES
+c.send(str(pkts[771][BGPHeader]))
+c.send(str(pkts[773][BGPHeader]))
+c.send(str(pkts[775][BGPHeader]))
+c.send(str(pkts[778][BGPHeader]))
+c.send(str(pkts[765][BGPHeader]))
 
-time.sleep(10)               # Waiting for 120 seconds for update message.
-
-c.send(str(pkts[298][BGPHeader]))
-c.send(str(pkts[860][BGPHeader]))   # UPDATE MESSAGES with new node
-c.send(str(pkts[865][BGPHeader]))
-print ("New Node update msg sent")
-
-time.sleep(5)
-c.send(str(pkts[890][BGPHeader]))
+time.sleep(15)
+c.send(str(pkts[1168][BGPHeader]))
+c.send(str(pkts[1250][BGPHeader]))
+c.send(str(pkts[1354][BGPHeader]))
 print ("Node Delete msg sent")
 
 
 while True:
     c.recv(4096)
-    c.send(str(pkts[71][BGPHeader]))
+    c.send(str(pkts[765][BGPHeader]))
 
   # c.close()                # Close the connection
diff --git a/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Link_Update_Node.py b/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Link_Update_Node.py
new file mode 100755
index 0000000..03a5653
--- /dev/null
+++ b/TestON/tests/FUNC/FUNCbgpls/dependencies/Scapyfiles/Link_Update_Node.py
@@ -0,0 +1,45 @@
+#!/usr/bin/python           # This is server.py file
+from scapy.all import *
+import socket               # Import socket module
+import time                 # Import Time module
+import sys
+import os
+
+path = os.getcwd()
+sys.path.append('OnosSystemTest/TestON/tests/FUNC/FUNCbgpls/dependencies')   #Setting the path for BgpLS
+from Nbdata import BgpLs
+
+obj = BgpLs()
+returnlist = obj.Constants()
+peerIp = returnlist[0][0]
+
+load_contrib('bgp')
+s = socket.socket()         # Create a socket object
+host = peerIp # Get local machine name
+port = 179                # Reserve a port for your service.
+s.bind((host, port))        # Bind to the port
+pkts = rdpcap(path + "/OnosSystemTest/TestON/tests/FUNC/FUNCbgpls/dependencies/Bgpls_packets/bgpls.pcap")
+time.sleep(15)
+pkts[759][BGPOpen].bgp_id = peerIp
+
+s.listen(5)                   # Now wait for client connection.
+
+print("starting Connecting to ONOS peer")
+c, addr = s.accept()     # Establish connection with client.
+print 'Got connection from ONOS :', addr
+c.send(str(pkts[759][BGPHeader])) # OPEN MESSAGE
+c.recv(4096)
+c.send(str(pkts[765][BGPHeader]))# KEEPALIVE MESSAGE
+c.recv(4096)
+c.send(str(pkts[768][BGPHeader]))   # UPDATE MESSAGES
+c.send(str(pkts[771][BGPHeader]))
+c.send(str(pkts[773][BGPHeader]))
+c.send(str(pkts[775][BGPHeader]))
+c.send(str(pkts[778][BGPHeader]))
+c.send(str(pkts[765][BGPHeader]))
+
+while True:
+    c.recv(4096)
+    c.send(str(pkts[765][BGPHeader]))
+
+  # c.close()                # Close the connection