timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import time |
| 4 | import pexpect |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 5 | import sys |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 6 | import json |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 7 | sys.path.append( "../" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 8 | from drivers.common.clidriver import CLI |
| 9 | |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 10 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 11 | class QuaggaCliDriver( CLI ): |
| 12 | |
| 13 | def __init__( self ): |
| 14 | super( CLI, self ).__init__() |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 15 | |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 16 | # TODO: simplify this method |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 17 | def connect( self, **connectargs ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 18 | for key in connectargs: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 19 | vars( self )[ key ] = connectargs[ key ] |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 20 | self.name = self.options[ 'name' ] |
| 21 | self.handle = super( QuaggaCliDriver, self ).connect( |
| 22 | user_name=self.user_name, |
| 23 | ip_address="127.0.0.1", |
| 24 | port=self.port, |
| 25 | pwd=self.pwd ) |
| 26 | if self.handle: |
| 27 | return self.handle |
| 28 | else: |
| 29 | main.log.info( "NO HANDLE" ) |
| 30 | return main.FALSE |
| 31 | |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 32 | |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 33 | def connectQuagga( self ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 34 | self.name = self.options[ 'name' ] |
| 35 | # self.handle = super( QuaggaCliDriver,self ).connect( |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 36 | # user_name=self.user_name, ip_address=self.ip_address,port=self.port, |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 37 | # pwd=self.pwd ) |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 38 | self.handle = super( QuaggaCliDriver, self ).connect( |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 39 | user_name=self.user_name, |
| 40 | ip_address="1.1.1.1", |
| 41 | port=self.port, |
| 42 | pwd=self.pwd ) |
pingping-lin | a600d9b | 2015-01-30 13:57:26 -0800 | [diff] [blame] | 43 | #main.log.info( "connect parameters:" + str( self.user_name ) + ";" |
| 44 | # + str( self.ip_address ) + ";" + str( self.port ) |
| 45 | # + ";" + str(self.pwd ) ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 46 | |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 47 | if self.handle: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 48 | # self.handle.expect( "",timeout=10 ) |
| 49 | # self.handle.expect( "\$",timeout=10 ) |
| 50 | self.handle.sendline( "telnet localhost 2605" ) |
| 51 | # self.handle.expect( "Password:", timeout=5 ) |
| 52 | self.handle.expect( "Password:" ) |
| 53 | self.handle.sendline( "hello" ) |
| 54 | # self.handle.expect( "bgpd", timeout=5 ) |
| 55 | self.handle.expect( "bgpd" ) |
| 56 | self.handle.sendline( "enable" ) |
| 57 | # self.handle.expect( "bgpd#", timeout=5 ) |
| 58 | self.handle.expect( "bgpd#" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 59 | return self.handle |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 60 | else: |
| 61 | main.log.info( "NO HANDLE" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 62 | return main.FALSE |
| 63 | |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 64 | def loginQuagga( self, ip_address ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 65 | self.name = self.options[ 'name' ] |
| 66 | self.handle = super( QuaggaCliDriver, self ).connect( |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 67 | user_name=self.user_name, ip_address=ip_address, |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 68 | port=self.port, pwd=self.pwd ) |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 69 | main.log.info( "connect parameters:" + str( self.user_name ) + ";" |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 70 | + str( self.ip_address ) + ";" + str( self.port ) |
| 71 | + ";" + str( self.pwd ) ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 72 | |
| 73 | if self.handle: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 74 | # self.handle.expect( "" ) |
| 75 | # self.handle.expect( "\$" ) |
| 76 | self.handle.sendline( "telnet localhost 2605" ) |
| 77 | # self.handle.expect( "Password:", timeout=5 ) |
| 78 | self.handle.expect( "Password:" ) |
| 79 | self.handle.sendline( "hello" ) |
| 80 | # self.handle.expect( "bgpd", timeout=5 ) |
| 81 | self.handle.expect( "bgpd" ) |
| 82 | self.handle.sendline( "enable" ) |
| 83 | # self.handle.expect( "bgpd#", timeout=5 ) |
| 84 | self.handle.expect( "bgpd#" ) |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 85 | main.log.info( "I am in quagga on host " + str( ip_address ) ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 86 | |
| 87 | return self.handle |
| 88 | else: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 89 | main.log.info( "NO HANDLE" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 90 | return main.FALSE |
| 91 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 92 | def enterConfig( self, asn ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 93 | main.log.info( "I am in enter_config method!" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 94 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 95 | self.handle.sendline( "" ) |
| 96 | self.handle.expect( "bgpd#" ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 97 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 98 | main.log.warn( "Probably not currently in enable mode!" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 99 | self.disconnect() |
| 100 | return main.FALSE |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 101 | self.handle.sendline( "configure terminal" ) |
| 102 | self.handle.expect( "config", timeout=5 ) |
| 103 | routerAS = "router bgp " + str( asn ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 104 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 105 | self.handle.sendline( routerAS ) |
| 106 | self.handle.expect( "config-router", timeout=5 ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 107 | return main.TRUE |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 108 | except Exception: |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 109 | return main.FALSE |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 110 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 111 | def generatePrefixes( self, net, numRoutes ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 112 | main.log.info( "I am in generate_prefixes method!" ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 113 | |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 114 | # each IP prefix is composed by "net" + "." + m + "." + n + "." + x |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 115 | # the length of each IP prefix is 24 |
| 116 | routes = [] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 117 | routesGen = 0 |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 118 | m = numRoutes / 256 |
| 119 | n = numRoutes % 256 |
| 120 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 121 | for i in range( 0, m ): |
| 122 | for j in range( 0, 256 ): |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 123 | network = str( net ) + "." + str( i ) + "." + str( j ) \ |
| 124 | + ".0/24" |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 125 | routes.append( network ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 126 | routesGen = routesGen + 1 |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 127 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 128 | for j in range( 0, n ): |
| 129 | network = str( net ) + "." + str( m ) + "." + str( j ) + ".0/24" |
| 130 | routes.append( network ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 131 | routesGen = routesGen + 1 |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 132 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 133 | if routesGen == numRoutes: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 134 | main.log.info( "Successfully generated " + str( numRoutes ) |
| 135 | + " prefixes!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 136 | return routes |
| 137 | return main.FALSE |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 138 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 139 | # This method generates a multiple to single point intent( |
| 140 | # MultiPointToSinglePointIntent ) for a given route |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 141 | def generateExpectedSingleRouteIntent( self, prefix, nextHop, nextHopMac, |
| 142 | sdnipData ): |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 143 | |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 144 | ingresses = [] |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 145 | egress = "" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 146 | for peer in sdnipData[ 'bgpPeers' ]: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 147 | if peer[ 'ipAddress' ] == nextHop: |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 148 | egress = "of:" + str( |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 149 | peer[ 'attachmentDpid' ] ).replace( ":", "" ) + ":" \ |
| 150 | + str( peer[ 'attachmentPort' ] ) |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 151 | for peer in sdnipData[ 'bgpPeers' ]: |
| 152 | if not peer[ 'ipAddress' ] == nextHop: |
| 153 | ingress = "of:" + str( |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 154 | peer[ 'attachmentDpid' ] ).replace( ":", "" ) + ":" \ |
| 155 | + str( peer[ 'attachmentPort' ] ) |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 156 | if not ingress == egress and ingress not in ingresses: |
| 157 | ingresses.append( ingress ) |
| 158 | # ingresses.append( "of:" + str( peer[ 'attachmentDpid' ] |
| 159 | # ).replace( ":", "" ) + ":" + str( peer[ 'attachmentPort' |
| 160 | # ] ) ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 161 | |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 162 | selector = "ETH_TYPE{ethType=800},IPV4_DST{ip=" + prefix + "}" |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 163 | treatment = "[ETH_DST{mac=" + str( nextHopMac ) + "}]" |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 164 | |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 165 | intent = egress + "/" + str( sorted( ingresses ) ) + "/" + \ |
| 166 | selector + "/" + treatment |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 167 | return intent |
| 168 | |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 169 | def generateExpectedOnePeerRouteIntents( self, prefixes, nextHop, |
| 170 | nextHopMac, sdnipJsonFilePath ): |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 171 | intents = [] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 172 | sdnipJsonFile = open( sdnipJsonFilePath ).read() |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 173 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 174 | sdnipData = json.loads( sdnipJsonFile ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 175 | |
| 176 | for prefix in prefixes: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 177 | intents.append( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 178 | self.generateExpectedSingleRouteIntent( |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 179 | prefix, nextHop, nextHopMac, sdnipData ) ) |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 180 | return sorted( intents ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 181 | |
| 182 | # TODO |
| 183 | # This method generates all expected route intents for all BGP peers |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 184 | def generateExpectedRouteIntents( self ): |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 185 | intents = [] |
| 186 | return intents |
| 187 | |
| 188 | # This method extracts all actual routes from ONOS CLI |
pingping-lin | b2a8658 | 2015-02-02 16:18:59 -0800 | [diff] [blame] | 189 | def extractActualRoutesOneDotZero( self, getRoutesResult ): |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 190 | routesJsonObj = json.loads( getRoutesResult ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 191 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 192 | allRoutesActual = [] |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 193 | for route in routesJsonObj['routes4']: |
| 194 | if 'prefix' in route: |
| 195 | if route[ 'prefix' ] == '172.16.10.0/24': |
| 196 | continue |
| 197 | allRoutesActual.append( |
| 198 | route[ 'prefix' ] + "/" + route[ 'nextHop' ] ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 199 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 200 | return sorted( allRoutesActual ) |
pingping-lin | b2a8658 | 2015-02-02 16:18:59 -0800 | [diff] [blame] | 201 | |
| 202 | def extractActualRoutesMaster( self, getRoutesResult ): |
pingping-lin | a600d9b | 2015-01-30 13:57:26 -0800 | [diff] [blame] | 203 | routesJsonObj = json.loads( getRoutesResult ) |
| 204 | |
| 205 | allRoutesActual = [] |
| 206 | for route in routesJsonObj['routes4']: |
| 207 | if route[ 'prefix' ] == '172.16.10.0/24': |
| 208 | continue |
| 209 | allRoutesActual.append( |
| 210 | route[ 'prefix' ] + "/" + route[ 'nextHop' ] ) |
| 211 | |
| 212 | return sorted( allRoutesActual ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 213 | |
| 214 | # This method extracts all actual route intents from ONOS CLI |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 215 | def extractActualRouteIntents( self, getIntentsResult ): |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 216 | intents = [] |
| 217 | # TODO: delete the line below when change to Mininet demo script |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 218 | # getIntentsResult=open( "../tests/SdnIpTest/intents.json" ).read() |
| 219 | intentsJsonObj = json.loads( getIntentsResult ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 220 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 221 | for intent in intentsJsonObj: |
pingping-lin | f30cf27 | 2015-05-29 15:54:07 -0700 | [diff] [blame] | 222 | #if intent[ 'appId' ] != "org.onosproject.sdnip": |
| 223 | # continue |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 224 | if intent[ 'type' ] == "MultiPointToSinglePointIntent" \ |
| 225 | and intent[ 'state' ] == 'INSTALLED': |
| 226 | egress = str( intent[ 'egress' ][ 'device' ] ) + ":" \ |
| 227 | + str( intent[ 'egress' ][ 'port' ] ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 228 | ingress = [] |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 229 | for attachmentPoint in intent[ 'ingress' ]: |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 230 | ingress.append( |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 231 | str( attachmentPoint[ 'device' ] ) + ":" |
| 232 | + str( attachmentPoint[ 'port' ] ) ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 233 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 234 | selector = intent[ 'selector' ].replace( |
| 235 | "[", "" ).replace( "]", "" ).replace( " ", "" ) |
| 236 | if str( selector ).startswith( "IPV4" ): |
| 237 | str1, str2 = str( selector ).split( "," ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 238 | selector = str2 + "," + str1 |
| 239 | |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 240 | intent = egress + "/" + str( sorted( ingress ) ) + "/" + \ |
| 241 | selector + "/" + intent[ 'treatment' ] |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 242 | intents.append( intent ) |
| 243 | return sorted( intents ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 244 | |
pingping-lin | f30cf27 | 2015-05-29 15:54:07 -0700 | [diff] [blame] | 245 | # This method calculates the MultiPointToSinglePointIntent number installed |
| 246 | def extractActualRouteIntentNum( self, getIntentsResult ): |
| 247 | intentsJsonObj = json.loads( getIntentsResult ) |
| 248 | num = 0 |
| 249 | for intent in intentsJsonObj: |
| 250 | if intent[ 'type' ] == "MultiPointToSinglePointIntent" \ |
| 251 | and intent[ 'state' ] == 'INSTALLED': |
| 252 | num = num + 1 |
| 253 | return num |
| 254 | |
| 255 | # This method calculates the PointToPointIntent number installed |
| 256 | def extractActualBgpIntentNum( self, getIntentsResult ): |
| 257 | intentsJsonObj = json.loads( getIntentsResult ) |
| 258 | num = 0 |
| 259 | for intent in intentsJsonObj: |
| 260 | if intent[ 'type' ] == "PointToPointIntent" \ |
| 261 | and intent[ 'state' ] == 'INSTALLED': |
| 262 | num = num + 1 |
| 263 | return num |
| 264 | |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 265 | # This method extracts all actual BGP intents from ONOS CLI |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 266 | def extractActualBgpIntents( self, getIntentsResult ): |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 267 | intents = [] |
| 268 | # TODO: delete the line below when change to Mininet demo script |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 269 | # getIntentsResult=open( "../tests/SdnIpTest/intents.json" ).read() |
| 270 | intentsJsonObj = json.loads( getIntentsResult ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 271 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 272 | for intent in intentsJsonObj: |
pingping-lin | f30cf27 | 2015-05-29 15:54:07 -0700 | [diff] [blame] | 273 | #if intent[ 'appId' ] != "org.onosproject.sdnip": |
| 274 | # continue |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 275 | if intent[ 'type' ] == "PointToPointIntent" \ |
| 276 | and "protocol=6" in str( intent[ 'selector' ] ): |
| 277 | ingress = str( intent[ 'ingress' ][ 'device' ] ) + ":" \ |
| 278 | + str( intent[ 'ingress' ][ 'port' ] ) |
| 279 | egress = str( intent[ 'egress' ][ 'device' ] ) + ":" + \ |
| 280 | str( intent[ 'egress' ][ 'port' ] ) |
| 281 | selector = str( intent[ 'selector' ] ).replace( " ", "" )\ |
| 282 | .replace( "[", "" ).replace( "]", "" ).split( "," ) |
| 283 | intent = ingress + "/" + egress + "/" + \ |
| 284 | str( sorted( selector ) ) |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 285 | intents.append( intent ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 286 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 287 | return sorted( intents ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 288 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 289 | # This method generates a single point to single point intent( |
| 290 | # PointToPointIntent ) for BGP path |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 291 | def generateExpectedBgpIntents( self, sdnipJsonFilePath ): |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 292 | from operator import eq |
| 293 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 294 | sdnipJsonFile = open( sdnipJsonFilePath ).read() |
| 295 | sdnipData = json.loads( sdnipJsonFile ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 296 | |
| 297 | intents = [] |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 298 | bgpPeerAttachmentPoint = "" |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 299 | bgpSpeakerAttachmentPoint = "of:" + str( |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 300 | sdnipData[ 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] )\ |
| 301 | .replace( ":", "" ) + ":" \ |
| 302 | + str( sdnipData[ 'bgpSpeakers' ][ 0 ][ 'attachmentPort' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 303 | for peer in sdnipData[ 'bgpPeers' ]: |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 304 | bgpPeerAttachmentPoint = "of:" \ |
| 305 | + str( peer[ 'attachmentDpid' ] ).replace( ":", "" ) \ |
| 306 | + ":" + str( peer[ 'attachmentPort' ] ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 307 | # find out the BGP speaker IP address for this BGP peer |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 308 | bgpSpeakerIpAddress = "" |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 309 | for interfaceAddress in \ |
| 310 | sdnipData[ 'bgpSpeakers' ][ 0 ][ 'interfaceAddresses' ]: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 311 | # if eq( interfaceAddress[ 'interfaceDpid' ],sdnipData[ |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 312 | # 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] ) and eq( |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 313 | # interfaceAddress[ 'interfacePort' ], sdnipData[ 'bgpSpeakers' |
| 314 | # ][ 0 ][ 'attachmentPort' ] ): |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 315 | if eq( interfaceAddress[ 'interfaceDpid' ], |
| 316 | peer[ 'attachmentDpid' ] ) \ |
| 317 | and eq( interfaceAddress[ 'interfacePort' ], |
| 318 | peer[ 'attachmentPort' ] ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 319 | bgpSpeakerIpAddress = interfaceAddress[ 'ipAddress' ] |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 320 | break |
| 321 | else: |
| 322 | continue |
| 323 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 324 | # from bgpSpeakerAttachmentPoint to bgpPeerAttachmentPoint |
| 325 | # direction |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 326 | selectorStr = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," \ |
| 327 | + "IPV4_DST{ip=" + peer[ 'ipAddress' ] + "/32}," \ |
| 328 | + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \ |
| 329 | TCP_DST{tcpPort=179}" |
| 330 | selector = selectorStr.replace( " ", "" ).replace("[", "" )\ |
| 331 | .replace( "]", "" ).split( "," ) |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 332 | intent = bgpSpeakerAttachmentPoint + "/" + \ |
| 333 | bgpPeerAttachmentPoint + "/" + str( sorted( selector ) ) |
| 334 | intents.append( intent ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 335 | |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 336 | selectorStr = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," \ |
| 337 | + "IPV4_DST{ip=" + peer[ 'ipAddress' ] + "/32}," \ |
| 338 | + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \ |
| 339 | TCP_SRC{tcpPort=179}" |
| 340 | selector = selectorStr.replace( " ", "" ).replace("[", "" )\ |
| 341 | .replace( "]", "" ).split( "," ) |
| 342 | intent = bgpSpeakerAttachmentPoint + "/" \ |
| 343 | + bgpPeerAttachmentPoint + "/" + str( sorted( selector ) ) |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 344 | intents.append( intent ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 345 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 346 | # from bgpPeerAttachmentPoint to bgpSpeakerAttachmentPoint |
| 347 | # direction |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 348 | selectorStr = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," \ |
| 349 | + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," \ |
| 350 | + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \ |
| 351 | TCP_DST{tcpPort=179}" |
| 352 | selector = selectorStr.replace( " ", "" ).replace("[", "" )\ |
| 353 | .replace( "]", "" ).split( "," ) |
| 354 | intent = bgpPeerAttachmentPoint + "/" \ |
| 355 | + bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) ) |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 356 | intents.append( intent ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 357 | |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 358 | selectorStr = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," \ |
| 359 | + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," \ |
| 360 | + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \ |
| 361 | TCP_SRC{tcpPort=179}" |
| 362 | selector = selectorStr.replace( " ", "" ).replace( "[", "" )\ |
| 363 | .replace( "]", "" ).split( "," ) |
| 364 | intent = bgpPeerAttachmentPoint + "/" \ |
| 365 | + bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) ) |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 366 | intents.append( intent ) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 367 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 368 | return sorted( intents ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 369 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 370 | def addRoutes( self, routes, routeRate ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 371 | main.log.info( "I am in add_routes method!" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 372 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 373 | routesAdded = 0 |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 374 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 375 | self.handle.sendline( "" ) |
| 376 | # self.handle.expect( "config-router" ) |
| 377 | self.handle.expect( "config-router", timeout=5 ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 378 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 379 | main.log.warn( "Probably not in config-router mode!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 380 | self.disconnect() |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 381 | main.log.info( "Start to add routes" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 382 | |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 383 | chunk_size = 20 |
| 384 | |
| 385 | if len(routes) > chunk_size: |
| 386 | num_iter = (int) (len(routes) / chunk_size) |
| 387 | else: |
| 388 | num_iter = 1; |
| 389 | |
| 390 | total = 0 |
| 391 | for n in range( 0, num_iter + 1): |
| 392 | routeCmd = "" |
| 393 | if (len( routes ) - (n * chunk_size)) >= chunk_size: |
| 394 | m = (n + 1) * chunk_size |
| 395 | else: |
| 396 | m = len( routes ) |
| 397 | for i in range( n * chunk_size, m ): |
| 398 | routeCmd = routeCmd + "network " + routes[ i ] + "\n" |
| 399 | total = total + 1 |
| 400 | |
| 401 | main.log.info(routeCmd) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 402 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 403 | self.handle.sendline( routeCmd ) |
| 404 | self.handle.expect( "bgpd", timeout=5 ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 405 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 406 | main.log.warn( "Failed to add route" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 407 | self.disconnect() |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 408 | |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 409 | # waitTimer = 1.00 / routeRate |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 410 | main.log.info("Total routes so far " + ((str) (total)) + " wait for 0 sec") |
| 411 | #time.sleep( 1 ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 412 | if routesAdded == len( routes ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 413 | main.log.info( "Finished adding routes" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 414 | return main.TRUE |
| 415 | return main.FALSE |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 416 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 417 | def deleteRoutes( self, routes, routeRate ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 418 | main.log.info( "I am in delete_routes method!" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 419 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 420 | routesAdded = 0 |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 421 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 422 | self.handle.sendline( "" ) |
| 423 | # self.handle.expect( "config-router" ) |
| 424 | self.handle.expect( "config-router", timeout=5 ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 425 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 426 | main.log.warn( "Probably not in config-router mode!" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 427 | self.disconnect() |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 428 | main.log.info( "Start to delete routes" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 429 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 430 | for i in range( 0, len( routes ) ): |
| 431 | routeCmd = "no network " + routes[ i ] |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 432 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 433 | self.handle.sendline( routeCmd ) |
| 434 | self.handle.expect( "bgpd", timeout=5 ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 435 | except Exception: |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 436 | main.log.warn( "Failed to delete route" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 437 | self.disconnect() |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 438 | # waitTimer = 1.00 / routeRate |
| 439 | # time.sleep( waitTimer ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 440 | if routesAdded == len( routes ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 441 | main.log.info( "Finished deleting routes" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 442 | return main.TRUE |
| 443 | return main.FALSE |
| 444 | |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 445 | def pingTest( self, ip_address, pingTestFile, pingTestResultFile ): |
| 446 | main.log.info( "Start the ping test on host:" + str( ip_address ) ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 447 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 448 | self.name = self.options[ 'name' ] |
| 449 | self.handle = super( QuaggaCliDriver, self ).connect( |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 450 | user_name=self.user_name, ip_address=ip_address, |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 451 | port=self.port, pwd=self.pwd ) |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 452 | main.log.info( "connect parameters:" + str( self.user_name ) + ";" |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 453 | + str( self.ip_address ) + ";" + str( self.port ) |
| 454 | + ";" + str( self.pwd ) ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 455 | |
| 456 | if self.handle: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 457 | # self.handle.expect( "" ) |
| 458 | # self.handle.expect( "\$" ) |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 459 | main.log.info( "I in host " + str( ip_address ) ) |
| 460 | main.log.info( pingTestFile + " > " + pingTestResultFile + " &" ) |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 461 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 462 | pingTestFile + |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 463 | " > " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 464 | pingTestResultFile + |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 465 | " &" ) |
| 466 | self.handle.expect( "\$", timeout=60 ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 467 | handle = self.handle.before |
| 468 | |
| 469 | return handle |
| 470 | else: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 471 | main.log.info( "NO HANDLE" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 472 | return main.FALSE |
| 473 | |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 474 | |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 475 | # Please use the generateRoutes plus addRoutes instead of this one! |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 476 | def addRoute( self, net, numRoutes, routeRate ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 477 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 478 | self.handle.sendline( "" ) |
| 479 | self.handle.expect( "config-router" ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 480 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 481 | main.log.warn( "Probably not in config-router mode!" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 482 | self.disconnect() |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 483 | main.log.info( "Adding Routes" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 484 | j = 0 |
| 485 | k = 0 |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 486 | while numRoutes > 255: |
| 487 | numRoutes = numRoutes - 255 |
| 488 | j = j + 1 |
| 489 | k = numRoutes % 254 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 490 | routesAdded = 0 |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 491 | if numRoutes > 255: |
| 492 | numRoutes = 255 |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 493 | for m in range( 1, j + 1 ): |
| 494 | for n in range( 1, numRoutes + 1 ): |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 495 | network = str( net ) + "." + str( m ) + "." + str( n ) \ |
| 496 | + ".0/24" |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 497 | routeCmd = "network " + network |
| 498 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 499 | self.handle.sendline( routeCmd ) |
| 500 | self.handle.expect( "bgpd" ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 501 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 502 | main.log.warn( "failed to add route" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 503 | self.disconnect() |
| 504 | waitTimer = 1.00 / routeRate |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 505 | time.sleep( waitTimer ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 506 | routesAdded = routesAdded + 1 |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 507 | for d in range( j + 1, j + 2 ): |
| 508 | for e in range( 1, k + 1 ): |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 509 | network = str( net ) + "." + str( d ) + "." + str( e ) \ |
| 510 | + ".0/24" |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 511 | routeCmd = "network " + network |
| 512 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 513 | self.handle.sendline( routeCmd ) |
| 514 | self.handle.expect( "bgpd" ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 515 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 516 | main.log.warn( "failed to add route" ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 517 | self.disconnect() |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 518 | waitTimer = 1.00 / routeRate |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 519 | time.sleep( waitTimer ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 520 | routesAdded = routesAdded + 1 |
| 521 | if routesAdded == numRoutes: |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 522 | return main.TRUE |
| 523 | return main.FALSE |
pingping-lin | 0904ad0 | 2015-01-30 15:18:14 -0800 | [diff] [blame] | 524 | |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 525 | # Please use deleteRoutes method instead of this one! |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 526 | def delRoute( self, net, numRoutes, routeRate ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 527 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 528 | self.handle.sendline( "" ) |
| 529 | self.handle.expect( "config-router" ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 530 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 531 | main.log.warn( "Probably not in config-router mode!" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 532 | self.disconnect() |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 533 | main.log.info( "Deleting Routes" ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 534 | j = 0 |
| 535 | k = 0 |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 536 | while numRoutes > 255: |
| 537 | numRoutes = numRoutes - 255 |
| 538 | j = j + 1 |
| 539 | k = numRoutes % 254 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 540 | routesDeleted = 0 |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 541 | if numRoutes > 255: |
| 542 | numRoutes = 255 |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 543 | for m in range( 1, j + 1 ): |
| 544 | for n in range( 1, numRoutes + 1 ): |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 545 | network = str( net ) + "." + str( m ) + "." + str( n ) \ |
| 546 | + ".0/24" |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 547 | routeCmd = "no network " + network |
| 548 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 549 | self.handle.sendline( routeCmd ) |
| 550 | self.handle.expect( "bgpd" ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 551 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 552 | main.log.warn( "Failed to delete route" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 553 | self.disconnect() |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 554 | waitTimer = 1.00 / routeRate |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 555 | time.sleep( waitTimer ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 556 | routesDeleted = routesDeleted + 1 |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 557 | for d in range( j + 1, j + 2 ): |
| 558 | for e in range( 1, k + 1 ): |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 559 | network = str( net ) + "." + str( d ) + "." + str( e ) \ |
| 560 | + ".0/24" |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 561 | routeCmd = "no network " + network |
| 562 | try: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 563 | self.handle.sendline( routeCmd ) |
| 564 | self.handle.expect( "bgpd" ) |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 565 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 566 | main.log.warn( "Failed to delete route" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 567 | self.disconnect() |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 568 | waitTimer = 1.00 / routeRate |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 569 | time.sleep( waitTimer ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 570 | routesDeleted = routesDeleted + 1 |
| 571 | if routesDeleted == numRoutes: |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 572 | return main.TRUE |
| 573 | return main.FALSE |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 574 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 575 | def checkRoutes( self, brand, ip, user, pw ): |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 576 | def pronto( ip, user, passwd ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 577 | print "Connecting to Pronto switch" |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 578 | child = pexpect.spawn( "telnet " + ip ) |
| 579 | i = child.expect( [ "login:", "CLI#", pexpect.TIMEOUT ] ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 580 | if i == 0: |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 581 | print "user_name and password required. Passing login info." |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 582 | child.sendline( user ) |
| 583 | child.expect( "Password:" ) |
| 584 | child.sendline( passwd ) |
| 585 | child.expect( "CLI#" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 586 | print "Logged in, getting flowtable." |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 587 | child.sendline( "flowtable brief" ) |
| 588 | for t in range( 9 ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 589 | t2 = 9 - t |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 590 | print "\r" + str( t2 ) |
| 591 | sys.stdout.write( "\033[F" ) |
| 592 | time.sleep( 1 ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 593 | print "Scanning flowtable" |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 594 | child.expect( "Flow table show" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 595 | count = 0 |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 596 | while True: |
pingping-lin | 0904ad0 | 2015-01-30 15:18:14 -0800 | [diff] [blame] | 597 | i = child.expect( [ '17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}', |
pingping-lin | 4e7b0d3 | 2015-01-27 18:06:22 -0800 | [diff] [blame] | 598 | 'CLI#', pexpect.TIMEOUT ] ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 599 | if i == 0: |
| 600 | count = count + 1 |
| 601 | elif i == 1: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 602 | print "Pronto flows: " + str( count ) + "\nDone\n" |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 603 | break |
| 604 | else: |
| 605 | break |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 606 | |
| 607 | def cisco( ip, user, passwd ): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 608 | print "Establishing Cisco switch connection" |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 609 | child = pexpect.spawn( "ssh " + user + "@" + ip ) |
| 610 | i = child.expect( [ "Password:", "CLI#", pexpect.TIMEOUT ] ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 611 | if i == 0: |
| 612 | print "Password required. Passing now." |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 613 | child.sendline( passwd ) |
| 614 | child.expect( "#" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 615 | print "Logged in. Retrieving flow table then counting flows." |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 616 | child.sendline( "show openflow switch all flows all" ) |
| 617 | child.expect( "Logical Openflow Switch" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 618 | print "Flow table retrieved. Counting flows" |
| 619 | count = 0 |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 620 | while True: |
| 621 | i = child.expect( [ "nw_src=17", "#", pexpect.TIMEOUT ] ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 622 | if i == 0: |
| 623 | count = count + 1 |
| 624 | elif i == 1: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 625 | print "Cisco flows: " + str( count ) + "\nDone\n" |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 626 | break |
| 627 | else: |
| 628 | break |
| 629 | if brand == "pronto" or brand == "PRONTO": |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 630 | pronto( ip, user, passwd ) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 631 | # elif brand == "cisco" or brand == "CISCO": |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 632 | # cisco( ip,user,passwd ) |
| 633 | |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 634 | def disable_bgp_peer( self, peer, peer_as ): |
| 635 | main.log.info( "I am in disconnect_peer_session method!" ) |
| 636 | |
| 637 | try: |
| 638 | self.handle.sendline( "" ) |
| 639 | # self.handle.expect( "config-router" ) |
| 640 | self.handle.expect( "config-router", timeout=5 ) |
| 641 | except Exception: |
| 642 | main.log.warn( "Probably not in config-router mode!" ) |
| 643 | self.disconnect() |
| 644 | main.log.info( "Start to disable peer" ) |
| 645 | |
| 646 | cmd = "no neighbor " + peer + " remote-as " + peer_as |
| 647 | try: |
| 648 | self.handle.sendline( cmd ) |
| 649 | self.handle.expect( "bgpd", timeout=5 ) |
| 650 | except Exception: |
| 651 | main.log.warn( "Failed to disable peer" ) |
| 652 | self.disconnect() |
| 653 | |
| 654 | def enable_bgp_peer( self, peer, peer_as ): |
| 655 | main.log.info( "I am in enable_bgp_peer method!" ) |
| 656 | |
| 657 | try: |
| 658 | self.handle.sendline( "" ) |
| 659 | # self.handle.expect( "config-router" ) |
| 660 | self.handle.expect( "config-router", timeout=5 ) |
| 661 | except Exception: |
| 662 | main.log.warn( "Probably not in config-router mode!" ) |
| 663 | self.disconnect() |
| 664 | main.log.info( "Start to disable peer" ) |
| 665 | |
| 666 | cmd = "neighbor " + peer + " remote-as " + peer_as |
| 667 | try: |
| 668 | self.handle.sendline( cmd ) |
| 669 | self.handle.expect( "bgpd", timeout=5 ) |
| 670 | except Exception: |
| 671 | main.log.warn( "Failed to enable peer" ) |
| 672 | self.disconnect() |
| 673 | |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 674 | def disconnect( self ): |
| 675 | """ |
| 676 | Called when Test is complete to disconnect the Quagga handle. |
| 677 | """ |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 678 | response = '' |
| 679 | try: |
| 680 | self.handle.close() |
pingping-lin | 763ee04 | 2015-05-20 17:45:30 -0700 | [diff] [blame] | 681 | except Exception: |
kelvin-onlab | bbe4648 | 2015-01-16 10:44:28 -0800 | [diff] [blame] | 682 | main.log.error( "Connection failed to the host" ) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 683 | response = main.FALSE |
| 684 | return response |
pingping-lin | c1c696e | 2015-01-27 13:46:44 -0800 | [diff] [blame] | 685 | |