blob: ecd1c520f27931064c6c1cab01497fd0c93aae93 [file] [log] [blame]
timlindbergef8d55d2013-09-27 12:50:13 -07001#!/usr/bin/env python
2
3import time
4import pexpect
kelvin-onlabbbe46482015-01-16 10:44:28 -08005import struct
6import fcntl
7import os
8import sys
9import signal
timlindbergef8d55d2013-09-27 12:50:13 -070010import sys
11import re
12import json
kelvin-onlabbbe46482015-01-16 10:44:28 -080013sys.path.append( "../" )
timlindbergef8d55d2013-09-27 12:50:13 -070014from drivers.common.clidriver import CLI
15
timlindbergef8d55d2013-09-27 12:50:13 -070016
kelvin-onlabbbe46482015-01-16 10:44:28 -080017class QuaggaCliDriver( CLI ):
18
19 def __init__( self ):
20 super( CLI, self ).__init__()
timlindbergef8d55d2013-09-27 12:50:13 -070021
pingping-linc6b86fa2014-12-01 16:18:10 -080022 # TODO: simplify this method
kelvin-onlabbbe46482015-01-16 10:44:28 -080023 def connect( self, **connectargs ):
timlindbergef8d55d2013-09-27 12:50:13 -070024 for key in connectargs:
kelvin-onlabbbe46482015-01-16 10:44:28 -080025 vars( self )[ key ] = connectargs[ key ]
pingping-linc6b86fa2014-12-01 16:18:10 -080026
kelvin-onlabbbe46482015-01-16 10:44:28 -080027 self.name = self.options[ 'name' ]
28 # self.handle = super( QuaggaCliDriver,self ).connect(
pingping-linc1c696e2015-01-27 13:46:44 -080029 # user_name=self.user_name, ip_address=self.ip_address,port=self.port,
kelvin-onlabbbe46482015-01-16 10:44:28 -080030 # pwd=self.pwd )
31 self.handle = super(
32 QuaggaCliDriver,
33 self ).connect(
pingping-linc1c696e2015-01-27 13:46:44 -080034 user_name=self.user_name,
35 ip_address="1.1.1.1",
36 port=self.port,
37 pwd=self.pwd )
kelvin-onlabbbe46482015-01-16 10:44:28 -080038 main.log.info(
39 "connect parameters:" + str(
pingping-linc1c696e2015-01-27 13:46:44 -080040 self.user_name ) + ";" + str(
41 self.ip_address ) + ";" + str(
42 self.port ) + ";" + str(
43 self.pwd ) )
timlindbergef8d55d2013-09-27 12:50:13 -070044
pingping-linc6b86fa2014-12-01 16:18:10 -080045 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -080046 # self.handle.expect( "",timeout=10 )
47 # self.handle.expect( "\$",timeout=10 )
48 self.handle.sendline( "telnet localhost 2605" )
49 # self.handle.expect( "Password:", timeout=5 )
50 self.handle.expect( "Password:" )
51 self.handle.sendline( "hello" )
52 # self.handle.expect( "bgpd", timeout=5 )
53 self.handle.expect( "bgpd" )
54 self.handle.sendline( "enable" )
55 # self.handle.expect( "bgpd#", timeout=5 )
56 self.handle.expect( "bgpd#" )
timlindbergef8d55d2013-09-27 12:50:13 -070057 return self.handle
kelvin-onlabbbe46482015-01-16 10:44:28 -080058 else:
59 main.log.info( "NO HANDLE" )
timlindbergef8d55d2013-09-27 12:50:13 -070060 return main.FALSE
61
pingping-linc1c696e2015-01-27 13:46:44 -080062 def loginQuagga( self, ip_address ):
kelvin-onlabbbe46482015-01-16 10:44:28 -080063 self.name = self.options[ 'name' ]
64 self.handle = super( QuaggaCliDriver, self ).connect(
pingping-linc1c696e2015-01-27 13:46:44 -080065 user_name=self.user_name, ip_address=ip_address,
kelvin-onlabbbe46482015-01-16 10:44:28 -080066 port=self.port, pwd=self.pwd )
pingping-linc1c696e2015-01-27 13:46:44 -080067 main.log.info( "connect parameters:" + str( self.user_name ) + ";"
68 + str( self.ip_address ) + ";" + str( self.port ) + ";" + str( self.pwd ) )
pingping-lin8b306ac2014-11-17 18:13:51 -080069
70 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -080071 # self.handle.expect( "" )
72 # self.handle.expect( "\$" )
73 self.handle.sendline( "telnet localhost 2605" )
74 # self.handle.expect( "Password:", timeout=5 )
75 self.handle.expect( "Password:" )
76 self.handle.sendline( "hello" )
77 # self.handle.expect( "bgpd", timeout=5 )
78 self.handle.expect( "bgpd" )
79 self.handle.sendline( "enable" )
80 # self.handle.expect( "bgpd#", timeout=5 )
81 self.handle.expect( "bgpd#" )
pingping-linc1c696e2015-01-27 13:46:44 -080082 main.log.info( "I am in quagga on host " + str( ip_address ) )
pingping-lin8b306ac2014-11-17 18:13:51 -080083
84 return self.handle
85 else:
kelvin-onlabbbe46482015-01-16 10:44:28 -080086 main.log.info( "NO HANDLE" )
pingping-lin8b306ac2014-11-17 18:13:51 -080087 return main.FALSE
88
kelvin-onlabd3b64892015-01-20 13:26:24 -080089 def enterConfig( self, asn ):
kelvin-onlabbbe46482015-01-16 10:44:28 -080090 main.log.info( "I am in enter_config method!" )
timlindbergef8d55d2013-09-27 12:50:13 -070091 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -080092 self.handle.sendline( "" )
93 self.handle.expect( "bgpd#" )
timlindbergef8d55d2013-09-27 12:50:13 -070094 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -080095 main.log.warn( "Probably not currently in enable mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -070096 self.disconnect()
97 return main.FALSE
kelvin-onlabbbe46482015-01-16 10:44:28 -080098 self.handle.sendline( "configure terminal" )
99 self.handle.expect( "config", timeout=5 )
100 routerAS = "router bgp " + str( asn )
timlindbergef8d55d2013-09-27 12:50:13 -0700101 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800102 self.handle.sendline( routerAS )
103 self.handle.expect( "config-router", timeout=5 )
timlindbergef8d55d2013-09-27 12:50:13 -0700104 return main.TRUE
105 except:
106 return main.FALSE
pingping-lin8b306ac2014-11-17 18:13:51 -0800107
kelvin-onlabd3b64892015-01-20 13:26:24 -0800108 def generatePrefixes( self, net, numRoutes ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800109 main.log.info( "I am in generate_prefixes method!" )
pingping-lin6f6332e2014-11-19 19:13:58 -0800110
pingping-linc1c696e2015-01-27 13:46:44 -0800111 # each IP prefix will be composed by "net" + "." + m + "." + n + "." + x
pingping-lin8b306ac2014-11-17 18:13:51 -0800112 # the length of each IP prefix is 24
113 routes = []
kelvin-onlabd3b64892015-01-20 13:26:24 -0800114 routesGen = 0
pingping-lin8b306ac2014-11-17 18:13:51 -0800115 m = numRoutes / 256
116 n = numRoutes % 256
117
kelvin-onlabbbe46482015-01-16 10:44:28 -0800118 for i in range( 0, m ):
119 for j in range( 0, 256 ):
120 network = str(
121 net ) + "." + str(
122 i ) + "." + str(
123 j ) + ".0/24"
124 routes.append( network )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800125 routesGen = routesGen + 1
pingping-lin8b306ac2014-11-17 18:13:51 -0800126
kelvin-onlabbbe46482015-01-16 10:44:28 -0800127 for j in range( 0, n ):
128 network = str( net ) + "." + str( m ) + "." + str( j ) + ".0/24"
129 routes.append( network )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800130 routesGen = routesGen + 1
pingping-lin8b306ac2014-11-17 18:13:51 -0800131
kelvin-onlabd3b64892015-01-20 13:26:24 -0800132 if routesGen == numRoutes:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800133 main.log.info( "Successfully generated " + str( numRoutes )
134 + " prefixes!" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800135 return routes
136 return main.FALSE
pingping-lin6f6332e2014-11-19 19:13:58 -0800137
kelvin-onlabbbe46482015-01-16 10:44:28 -0800138 # This method generates a multiple to single point intent(
139 # MultiPointToSinglePointIntent ) for a given route
kelvin-onlabd3b64892015-01-20 13:26:24 -0800140 def generateExpectedSingleRouteIntent(
pingping-linc1c696e2015-01-27 13:46:44 -0800141 self,
142 prefix,
143 nextHop,
144 nextHopMac,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800145 sdnipData ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800146
pingping-linc1c696e2015-01-27 13:46:44 -0800147 ingresses = []
pingping-lin6f6332e2014-11-19 19:13:58 -0800148 egress = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800149 for peer in sdnipData[ 'bgpPeers' ]:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800150 if peer[ 'ipAddress' ] == nextHop:
pingping-linc1c696e2015-01-27 13:46:44 -0800151 egress = "of:" + str(
152 peer[ 'attachmentDpid' ] ).replace( ":",
153 "" ) + ":" + str( peer[ 'attachmentPort' ] )
154 for peer in sdnipData[ 'bgpPeers' ]:
155 if not peer[ 'ipAddress' ] == nextHop:
156 ingress = "of:" + str(
157 peer[ 'attachmentDpid' ] ).replace( ":",
158 "" ) + ":" + str( peer[ 'attachmentPort' ] )
159 if not ingress == egress and ingress not in ingresses:
160 ingresses.append( ingress )
161 # ingresses.append( "of:" + str( peer[ 'attachmentDpid' ]
162 # ).replace( ":", "" ) + ":" + str( peer[ 'attachmentPort'
163 # ] ) )
pingping-lin6f6332e2014-11-19 19:13:58 -0800164
pingping-linc6b86fa2014-12-01 16:18:10 -0800165 selector = "ETH_TYPE{ethType=800},IPV4_DST{ip=" + prefix + "}"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800166 treatment = "[ETH_DST{mac=" + str( nextHopMac ) + "}]"
pingping-lin6f6332e2014-11-19 19:13:58 -0800167
kelvin-onlabbbe46482015-01-16 10:44:28 -0800168 intent = egress + "/" + \
pingping-linc1c696e2015-01-27 13:46:44 -0800169 str( sorted( ingresses ) ) + "/" + selector + "/" + treatment
pingping-lin6f6332e2014-11-19 19:13:58 -0800170 return intent
171
kelvin-onlabd3b64892015-01-20 13:26:24 -0800172 def generateExpectedOnePeerRouteIntents(
pingping-linc1c696e2015-01-27 13:46:44 -0800173 self,
174 prefixes,
175 nextHop,
176 nextHopMac,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800177 sdnipJsonFilePath ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800178 intents = []
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 sdnipJsonFile = open( sdnipJsonFilePath ).read()
pingping-lin6f6332e2014-11-19 19:13:58 -0800180
kelvin-onlabd3b64892015-01-20 13:26:24 -0800181 sdnipData = json.loads( sdnipJsonFile )
pingping-lin6f6332e2014-11-19 19:13:58 -0800182
183 for prefix in prefixes:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800184 intents.append(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800185 self.generateExpectedSingleRouteIntent(
kelvin-onlabbbe46482015-01-16 10:44:28 -0800186 prefix,
187 nextHop,
188 nextHopMac,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800189 sdnipData ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800190 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800191
192 # TODO
193 # This method generates all expected route intents for all BGP peers
kelvin-onlabd3b64892015-01-20 13:26:24 -0800194 def generateExpectedRouteIntents( self ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800195 intents = []
196 return intents
197
198 # This method extracts all actual routes from ONOS CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800199 def extractActualRoutes( self, getRoutesResult ):
200 routesJsonObj = json.loads( getRoutesResult )
pingping-lin6f6332e2014-11-19 19:13:58 -0800201
kelvin-onlabd3b64892015-01-20 13:26:24 -0800202 allRoutesActual = []
203 for route in routesJsonObj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800204 if route[ 'prefix' ] == '172.16.10.0/24':
pingping-lin6f6332e2014-11-19 19:13:58 -0800205 continue
kelvin-onlabd3b64892015-01-20 13:26:24 -0800206 allRoutesActual.append(
kelvin-onlabbbe46482015-01-16 10:44:28 -0800207 route[ 'prefix' ] + "/" + route[ 'nextHop' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800208
kelvin-onlabd3b64892015-01-20 13:26:24 -0800209 return sorted( allRoutesActual )
pingping-lin6f6332e2014-11-19 19:13:58 -0800210
211 # This method extracts all actual route intents from ONOS CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800212 def extractActualRouteIntents( self, getIntentsResult ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800213 intents = []
214 # TODO: delete the line below when change to Mininet demo script
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 # getIntentsResult=open( "../tests/SdnIpTest/intents.json" ).read()
216 intentsJsonObj = json.loads( getIntentsResult )
pingping-lin6f6332e2014-11-19 19:13:58 -0800217
kelvin-onlabd3b64892015-01-20 13:26:24 -0800218 for intent in intentsJsonObj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800219 if intent[ 'appId' ] != "org.onosproject.sdnip":
pingping-lin6f6332e2014-11-19 19:13:58 -0800220 continue
pingping-linc1c696e2015-01-27 13:46:44 -0800221 if intent[ 'type' ] == "MultiPointToSinglePointIntent" and intent[ 'state' ] == 'INSTALLED':
kelvin-onlabbbe46482015-01-16 10:44:28 -0800222 egress = str( intent[ 'egress' ][ 'device' ] ) + ":" + str(
223 intent[ 'egress' ][ 'port' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800224 ingress = []
kelvin-onlabbbe46482015-01-16 10:44:28 -0800225 for attachmentPoint in intent[ 'ingress' ]:
pingping-linc1c696e2015-01-27 13:46:44 -0800226 ingress.append(
227 str( attachmentPoint[ 'device' ] ) + ":" + str( attachmentPoint[ 'port' ] ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800228
kelvin-onlabbbe46482015-01-16 10:44:28 -0800229 selector = intent[ 'selector' ].replace(
230 "[", "" ).replace( "]", "" ).replace( " ", "" )
231 if str( selector ).startswith( "IPV4" ):
232 str1, str2 = str( selector ).split( "," )
pingping-linc6b86fa2014-12-01 16:18:10 -0800233 selector = str2 + "," + str1
234
kelvin-onlabbbe46482015-01-16 10:44:28 -0800235 intent = egress + "/" + \
236 str( sorted( ingress ) ) + "/" + \
pingping-linc1c696e2015-01-27 13:46:44 -0800237 selector + "/" + intent[ 'treatment' ]
kelvin-onlabbbe46482015-01-16 10:44:28 -0800238 intents.append( intent )
239 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800240
241 # This method extracts all actual BGP intents from ONOS CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800242 def extractActualBgpIntents( self, getIntentsResult ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800243 intents = []
244 # TODO: delete the line below when change to Mininet demo script
kelvin-onlabd3b64892015-01-20 13:26:24 -0800245 # getIntentsResult=open( "../tests/SdnIpTest/intents.json" ).read()
246 intentsJsonObj = json.loads( getIntentsResult )
pingping-lin6f6332e2014-11-19 19:13:58 -0800247
kelvin-onlabd3b64892015-01-20 13:26:24 -0800248 for intent in intentsJsonObj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800249 if intent[ 'appId' ] != "org.onosproject.sdnip":
pingping-lin6f6332e2014-11-19 19:13:58 -0800250 continue
pingping-linc1c696e2015-01-27 13:46:44 -0800251 if intent[ 'type' ] == "PointToPointIntent" and "protocol=6" in str( intent[ 'selector' ] ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800252 ingress = str( intent[ 'ingress' ][ 'device' ] ) + ":" + str(
253 intent[ 'ingress' ][ 'port' ] )
254 egress = str( intent[ 'egress' ][ 'device' ] ) + ":" + str(
255 intent[ 'egress' ][ 'port' ] )
256 selector = str(
pingping-linc1c696e2015-01-27 13:46:44 -0800257 intent[ 'selector' ] ).replace( " ",
258 "" ).replace( "[",
259 "" ).replace( "]",
260 "" ).split( "," )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800261 intent = ingress + "/" + egress + \
262 "/" + str( sorted( selector ) )
263 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800264
kelvin-onlabbbe46482015-01-16 10:44:28 -0800265 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800266
kelvin-onlabbbe46482015-01-16 10:44:28 -0800267 # This method generates a single point to single point intent(
268 # PointToPointIntent ) for BGP path
kelvin-onlabd3b64892015-01-20 13:26:24 -0800269 def generateExpectedBgpIntents( self, sdnipJsonFilePath ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800270 from operator import eq
271
kelvin-onlabd3b64892015-01-20 13:26:24 -0800272 sdnipJsonFile = open( sdnipJsonFilePath ).read()
273 sdnipData = json.loads( sdnipJsonFile )
pingping-lin6f6332e2014-11-19 19:13:58 -0800274
275 intents = []
pingping-linc6b86fa2014-12-01 16:18:10 -0800276 bgpPeerAttachmentPoint = ""
kelvin-onlabbbe46482015-01-16 10:44:28 -0800277 bgpSpeakerAttachmentPoint = "of:" + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800278 sdnipData[ 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] ).replace( ":",
pingping-linc1c696e2015-01-27 13:46:44 -0800279 "" ) + ":" + str( sdnipData[ 'bgpSpeakers' ][ 0 ][ 'attachmentPort' ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800280 for peer in sdnipData[ 'bgpPeers' ]:
pingping-linc1c696e2015-01-27 13:46:44 -0800281 bgpPeerAttachmentPoint = "of:" + str(
282 peer[ 'attachmentDpid' ] ).replace( ":",
283 "" ) + ":" + str( peer[ 'attachmentPort' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800284 # find out the BGP speaker IP address for this BGP peer
pingping-linc6b86fa2014-12-01 16:18:10 -0800285 bgpSpeakerIpAddress = ""
pingping-linc1c696e2015-01-27 13:46:44 -0800286 for interfaceAddress in sdnipData[ 'bgpSpeakers' ][ 0 ][ 'interfaceAddresses' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800287 # if eq( interfaceAddress[ 'interfaceDpid' ],sdnipData[
kelvin-onlabbbe46482015-01-16 10:44:28 -0800288 # 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] ) and eq(
pingping-linc1c696e2015-01-27 13:46:44 -0800289 # interfaceAddress[ 'interfacePort' ], sdnipData[ 'bgpSpeakers'
290 # ][ 0 ][ 'attachmentPort' ] ):
291 if eq( interfaceAddress[ 'interfaceDpid' ], peer[ 'attachmentDpid' ] ) and eq( interfaceAddress[ 'interfacePort' ], peer[ 'attachmentPort' ] ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800292 bgpSpeakerIpAddress = interfaceAddress[ 'ipAddress' ]
pingping-lin6f6332e2014-11-19 19:13:58 -0800293 break
294 else:
295 continue
296
kelvin-onlabbbe46482015-01-16 10:44:28 -0800297 # from bgpSpeakerAttachmentPoint to bgpPeerAttachmentPoint
298 # direction
pingping-linc1c696e2015-01-27 13:46:44 -0800299 selectorStr = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," + "IPV4_DST{ip=" + peer[
300 'ipAddress' ] + "/32}," + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_DST{tcpPort=179}"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800301 selector = selectorStr.replace( " ", "" ).replace(
kelvin-onlabbbe46482015-01-16 10:44:28 -0800302 "[", "" ).replace( "]", "" ).split( "," )
303 intent = bgpSpeakerAttachmentPoint + "/" + \
304 bgpPeerAttachmentPoint + "/" + str( sorted( selector ) )
305 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800306
pingping-linc1c696e2015-01-27 13:46:44 -0800307 selectorStr = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," + "IPV4_DST{ip=" + peer[
308 'ipAddress' ] + "/32}," + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_SRC{tcpPort=179}"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800309 selector = selectorStr.replace( " ", "" ).replace(
kelvin-onlabbbe46482015-01-16 10:44:28 -0800310 "[", "" ).replace( "]", "" ).split( "," )
311 intent = bgpSpeakerAttachmentPoint + "/" + \
312 bgpPeerAttachmentPoint + "/" + str( sorted( selector ) )
313 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800314
kelvin-onlabbbe46482015-01-16 10:44:28 -0800315 # from bgpPeerAttachmentPoint to bgpSpeakerAttachmentPoint
316 # direction
pingping-linc1c696e2015-01-27 13:46:44 -0800317 selectorStr = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," + \
318 "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_DST{tcpPort=179}"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800319 selector = selectorStr.replace( " ", "" ).replace(
kelvin-onlabbbe46482015-01-16 10:44:28 -0800320 "[", "" ).replace( "]", "" ).split( "," )
321 intent = bgpPeerAttachmentPoint + "/" + \
322 bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) )
323 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800324
pingping-linc1c696e2015-01-27 13:46:44 -0800325 selectorStr = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," + \
326 "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_SRC{tcpPort=179}"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800327 selector = selectorStr.replace( " ", "" ).replace(
kelvin-onlabbbe46482015-01-16 10:44:28 -0800328 "[", "" ).replace( "]", "" ).split( "," )
329 intent = bgpPeerAttachmentPoint + "/" + \
330 bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) )
331 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800332
kelvin-onlabbbe46482015-01-16 10:44:28 -0800333 return sorted( intents )
pingping-linc6b86fa2014-12-01 16:18:10 -0800334
kelvin-onlabd3b64892015-01-20 13:26:24 -0800335 def addRoutes( self, routes, routeRate ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800336 main.log.info( "I am in add_routes method!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800337
kelvin-onlabd3b64892015-01-20 13:26:24 -0800338 routesAdded = 0
pingping-lin8b306ac2014-11-17 18:13:51 -0800339 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800340 self.handle.sendline( "" )
341 # self.handle.expect( "config-router" )
342 self.handle.expect( "config-router", timeout=5 )
pingping-lin8b306ac2014-11-17 18:13:51 -0800343 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800344 main.log.warn( "Probably not in config-router mode!" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800345 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800346 main.log.info( "Start to add routes" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800347
kelvin-onlabbbe46482015-01-16 10:44:28 -0800348 for i in range( 0, len( routes ) ):
349 routeCmd = "network " + routes[ i ]
pingping-lin8b306ac2014-11-17 18:13:51 -0800350 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800351 self.handle.sendline( routeCmd )
352 self.handle.expect( "bgpd", timeout=5 )
pingping-lin8b306ac2014-11-17 18:13:51 -0800353 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800354 main.log.warn( "Failed to add route" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800355 self.disconnect()
pingping-linc1c696e2015-01-27 13:46:44 -0800356 # waitTimer = 1.00 / routeRate
357 # time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800358 if routesAdded == len( routes ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800359 main.log.info( "Finished adding routes" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800360 return main.TRUE
361 return main.FALSE
pingping-linc6b86fa2014-12-01 16:18:10 -0800362
kelvin-onlabd3b64892015-01-20 13:26:24 -0800363 def deleteRoutes( self, routes, routeRate ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800364 main.log.info( "I am in delete_routes method!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800365
kelvin-onlabd3b64892015-01-20 13:26:24 -0800366 routesAdded = 0
pingping-linc6b86fa2014-12-01 16:18:10 -0800367 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800368 self.handle.sendline( "" )
369 # self.handle.expect( "config-router" )
370 self.handle.expect( "config-router", timeout=5 )
pingping-linc6b86fa2014-12-01 16:18:10 -0800371 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800372 main.log.warn( "Probably not in config-router mode!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800373 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800374 main.log.info( "Start to delete routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800375
kelvin-onlabbbe46482015-01-16 10:44:28 -0800376 for i in range( 0, len( routes ) ):
377 routeCmd = "no network " + routes[ i ]
pingping-linc6b86fa2014-12-01 16:18:10 -0800378 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800379 self.handle.sendline( routeCmd )
380 self.handle.expect( "bgpd", timeout=5 )
pingping-linc6b86fa2014-12-01 16:18:10 -0800381 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800382 main.log.warn( "Failed to add route" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800383 self.disconnect()
pingping-linc1c696e2015-01-27 13:46:44 -0800384 # waitTimer = 1.00 / routeRate
385 # time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800386 if routesAdded == len( routes ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800387 main.log.info( "Finished deleting routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800388 return main.TRUE
389 return main.FALSE
390
pingping-linc1c696e2015-01-27 13:46:44 -0800391 def pingTest( self, ip_address, pingTestFile, pingTestResultFile ):
392 main.log.info( "Start the ping test on host:" + str( ip_address ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800393
kelvin-onlabbbe46482015-01-16 10:44:28 -0800394 self.name = self.options[ 'name' ]
395 self.handle = super( QuaggaCliDriver, self ).connect(
pingping-linc1c696e2015-01-27 13:46:44 -0800396 user_name=self.user_name, ip_address=ip_address,
kelvin-onlabbbe46482015-01-16 10:44:28 -0800397 port=self.port, pwd=self.pwd )
pingping-linc1c696e2015-01-27 13:46:44 -0800398 main.log.info( "connect parameters:" + str( self.user_name ) + ";"
399 + str( self.ip_address ) + ";" + str( self.port ) + ";" + str( self.pwd ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800400
401 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800402 # self.handle.expect( "" )
403 # self.handle.expect( "\$" )
pingping-linc1c696e2015-01-27 13:46:44 -0800404 main.log.info( "I in host " + str( ip_address ) )
405 main.log.info( pingTestFile + " > " + pingTestResultFile + " &" )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800406 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800407 pingTestFile +
kelvin-onlabbbe46482015-01-16 10:44:28 -0800408 " > " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800409 pingTestResultFile +
kelvin-onlabbbe46482015-01-16 10:44:28 -0800410 " &" )
411 self.handle.expect( "\$", timeout=60 )
pingping-linc6b86fa2014-12-01 16:18:10 -0800412 handle = self.handle.before
413
414 return handle
415 else:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800416 main.log.info( "NO HANDLE" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800417 return main.FALSE
418
kelvin-onlabd3b64892015-01-20 13:26:24 -0800419 # Please use the generateRoutes plus addRoutes instead of this one
420 def addRoute( self, net, numRoutes, routeRate ):
timlindbergef8d55d2013-09-27 12:50:13 -0700421 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800422 self.handle.sendline( "" )
423 self.handle.expect( "config-router" )
timlindbergef8d55d2013-09-27 12:50:13 -0700424 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800425 main.log.warn( "Probably not in config-router mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -0700426 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800427 main.log.info( "Adding Routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800428 j = 0
429 k = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700430 while numRoutes > 255:
431 numRoutes = numRoutes - 255
432 j = j + 1
433 k = numRoutes % 254
kelvin-onlabd3b64892015-01-20 13:26:24 -0800434 routesAdded = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700435 if numRoutes > 255:
436 numRoutes = 255
kelvin-onlabbbe46482015-01-16 10:44:28 -0800437 for m in range( 1, j + 1 ):
438 for n in range( 1, numRoutes + 1 ):
pingping-linc1c696e2015-01-27 13:46:44 -0800439 network = str(
440 net ) + "." + str(
441 m ) + "." + str(
442 n ) + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700443 routeCmd = "network " + network
444 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800445 self.handle.sendline( routeCmd )
446 self.handle.expect( "bgpd" )
timlindbergef8d55d2013-09-27 12:50:13 -0700447 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800448 main.log.warn( "failed to add route" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800449 self.disconnect()
450 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800451 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800452 routesAdded = routesAdded + 1
kelvin-onlabbbe46482015-01-16 10:44:28 -0800453 for d in range( j + 1, j + 2 ):
454 for e in range( 1, k + 1 ):
455 network = str(
456 net ) + "." + str(
457 d ) + "." + str(
458 e ) + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700459 routeCmd = "network " + network
460 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800461 self.handle.sendline( routeCmd )
462 self.handle.expect( "bgpd" )
timlindbergef8d55d2013-09-27 12:50:13 -0700463 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800464 main.log.warn( "failed to add route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700465 self.disconnect
pingping-linc6b86fa2014-12-01 16:18:10 -0800466 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800467 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800468 routesAdded = routesAdded + 1
469 if routesAdded == numRoutes:
timlindbergef8d55d2013-09-27 12:50:13 -0700470 return main.TRUE
471 return main.FALSE
pingping-lin6f6332e2014-11-19 19:13:58 -0800472
kelvin-onlabd3b64892015-01-20 13:26:24 -0800473 def delRoute( self, net, numRoutes, routeRate ):
timlindbergef8d55d2013-09-27 12:50:13 -0700474 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800475 self.handle.sendline( "" )
476 self.handle.expect( "config-router" )
timlindbergef8d55d2013-09-27 12:50:13 -0700477 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800478 main.log.warn( "Probably not in config-router mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -0700479 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800480 main.log.info( "Deleting Routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800481 j = 0
482 k = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700483 while numRoutes > 255:
484 numRoutes = numRoutes - 255
485 j = j + 1
486 k = numRoutes % 254
kelvin-onlabd3b64892015-01-20 13:26:24 -0800487 routesDeleted = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700488 if numRoutes > 255:
489 numRoutes = 255
kelvin-onlabbbe46482015-01-16 10:44:28 -0800490 for m in range( 1, j + 1 ):
491 for n in range( 1, numRoutes + 1 ):
pingping-linc1c696e2015-01-27 13:46:44 -0800492 network = str(
493 net ) + "." + str(
494 m ) + "." + str(
495 n ) + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700496 routeCmd = "no network " + network
497 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800498 self.handle.sendline( routeCmd )
499 self.handle.expect( "bgpd" )
timlindbergef8d55d2013-09-27 12:50:13 -0700500 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800501 main.log.warn( "Failed to delete route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700502 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800503 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800504 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800505 routesDeleted = routesDeleted + 1
kelvin-onlabbbe46482015-01-16 10:44:28 -0800506 for d in range( j + 1, j + 2 ):
507 for e in range( 1, k + 1 ):
pingping-linc1c696e2015-01-27 13:46:44 -0800508 network = str(
509 net ) + "." + str(
510 d ) + "." + str(
511 e ) + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700512 routeCmd = "no network " + network
513 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800514 self.handle.sendline( routeCmd )
515 self.handle.expect( "bgpd" )
timlindbergef8d55d2013-09-27 12:50:13 -0700516 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800517 main.log.warn( "Failed to delete route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700518 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800519 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800520 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800521 routesDeleted = routesDeleted + 1
522 if routesDeleted == numRoutes:
timlindbergef8d55d2013-09-27 12:50:13 -0700523 return main.TRUE
524 return main.FALSE
pingping-linc6b86fa2014-12-01 16:18:10 -0800525
kelvin-onlabd3b64892015-01-20 13:26:24 -0800526 def checkRoutes( self, brand, ip, user, pw ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800527 def pronto( ip, user, passwd ):
timlindbergef8d55d2013-09-27 12:50:13 -0700528 print "Connecting to Pronto switch"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800529 child = pexpect.spawn( "telnet " + ip )
530 i = child.expect( [ "login:", "CLI#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700531 if i == 0:
pingping-linc1c696e2015-01-27 13:46:44 -0800532 print "user_name and password required. Passing login info."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800533 child.sendline( user )
534 child.expect( "Password:" )
535 child.sendline( passwd )
536 child.expect( "CLI#" )
timlindbergef8d55d2013-09-27 12:50:13 -0700537 print "Logged in, getting flowtable."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800538 child.sendline( "flowtable brief" )
539 for t in range( 9 ):
timlindbergef8d55d2013-09-27 12:50:13 -0700540 t2 = 9 - t
kelvin-onlabbbe46482015-01-16 10:44:28 -0800541 print "\r" + str( t2 )
542 sys.stdout.write( "\033[F" )
543 time.sleep( 1 )
timlindbergef8d55d2013-09-27 12:50:13 -0700544 print "Scanning flowtable"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800545 child.expect( "Flow table show" )
timlindbergef8d55d2013-09-27 12:50:13 -0700546 count = 0
kelvin-onlabbbe46482015-01-16 10:44:28 -0800547 while True:
548 i = child.expect(
549 [ '17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}',
pingping-linc1c696e2015-01-27 13:46:44 -0800550 'CLI#',
551 pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700552 if i == 0:
553 count = count + 1
554 elif i == 1:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800555 print "Pronto flows: " + str( count ) + "\nDone\n"
timlindbergef8d55d2013-09-27 12:50:13 -0700556 break
557 else:
558 break
kelvin-onlabbbe46482015-01-16 10:44:28 -0800559
560 def cisco( ip, user, passwd ):
timlindbergef8d55d2013-09-27 12:50:13 -0700561 print "Establishing Cisco switch connection"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800562 child = pexpect.spawn( "ssh " + user + "@" + ip )
563 i = child.expect( [ "Password:", "CLI#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700564 if i == 0:
565 print "Password required. Passing now."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800566 child.sendline( passwd )
567 child.expect( "#" )
timlindbergef8d55d2013-09-27 12:50:13 -0700568 print "Logged in. Retrieving flow table then counting flows."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800569 child.sendline( "show openflow switch all flows all" )
570 child.expect( "Logical Openflow Switch" )
timlindbergef8d55d2013-09-27 12:50:13 -0700571 print "Flow table retrieved. Counting flows"
572 count = 0
kelvin-onlabbbe46482015-01-16 10:44:28 -0800573 while True:
574 i = child.expect( [ "nw_src=17", "#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700575 if i == 0:
576 count = count + 1
577 elif i == 1:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800578 print "Cisco flows: " + str( count ) + "\nDone\n"
timlindbergef8d55d2013-09-27 12:50:13 -0700579 break
580 else:
581 break
582 if brand == "pronto" or brand == "PRONTO":
kelvin-onlabbbe46482015-01-16 10:44:28 -0800583 pronto( ip, user, passwd )
pingping-linc6b86fa2014-12-01 16:18:10 -0800584 # elif brand == "cisco" or brand == "CISCO":
kelvin-onlabbbe46482015-01-16 10:44:28 -0800585 # cisco( ip,user,passwd )
586
587 def disconnect( self ):
588 """
589 Called when Test is complete to disconnect the Quagga handle.
590 """
timlindbergef8d55d2013-09-27 12:50:13 -0700591 response = ''
592 try:
593 self.handle.close()
594 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800595 main.log.error( "Connection failed to the host" )
timlindbergef8d55d2013-09-27 12:50:13 -0700596 response = main.FALSE
597 return response
pingping-linc1c696e2015-01-27 13:46:44 -0800598