blob: 1c63206fae3a5fe1cb4a6fdeef90dbb49f16e6b8 [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 sys
timlindbergef8d55d2013-09-27 12:50:13 -07006import json
kelvin-onlabbbe46482015-01-16 10:44:28 -08007sys.path.append( "../" )
timlindbergef8d55d2013-09-27 12:50:13 -07008from drivers.common.clidriver import CLI
9
timlindbergef8d55d2013-09-27 12:50:13 -070010
kelvin-onlabbbe46482015-01-16 10:44:28 -080011class QuaggaCliDriver( CLI ):
12
13 def __init__( self ):
14 super( CLI, self ).__init__()
timlindbergef8d55d2013-09-27 12:50:13 -070015
pingping-linc6b86fa2014-12-01 16:18:10 -080016 # TODO: simplify this method
kelvin-onlabbbe46482015-01-16 10:44:28 -080017 def connect( self, **connectargs ):
timlindbergef8d55d2013-09-27 12:50:13 -070018 for key in connectargs:
kelvin-onlabbbe46482015-01-16 10:44:28 -080019 vars( self )[ key ] = connectargs[ key ]
Srikanth Vavilapalli68a1a2b2015-02-27 10:29:14 -080020 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-linc6b86fa2014-12-01 16:18:10 -080032
Srikanth Vavilapalli68a1a2b2015-02-27 10:29:14 -080033 def connectQuagga( self ):
kelvin-onlabbbe46482015-01-16 10:44:28 -080034 self.name = self.options[ 'name' ]
35 # self.handle = super( QuaggaCliDriver,self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080036 # user_name=self.user_name, ip_address=self.ip_address,port=self.port,
kelvin-onlabbbe46482015-01-16 10:44:28 -080037 # pwd=self.pwd )
kelvin-onlab2c4342a2015-01-28 15:59:53 -080038 self.handle = super( QuaggaCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080039 user_name=self.user_name,
kelvin-onlab2c4342a2015-01-28 15:59:53 -080040 ip_address="1.1.1.1",
41 port=self.port,
42 pwd=self.pwd )
43 main.log.info( "connect parameters:" + str( self.user_name ) + ";"
44 + str( self.ip_address ) + ";" + str( self.port )
45 + ";" + str(self.pwd ) )
timlindbergef8d55d2013-09-27 12:50:13 -070046
pingping-linc6b86fa2014-12-01 16:18:10 -080047 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -080048 # 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#" )
timlindbergef8d55d2013-09-27 12:50:13 -070059 return self.handle
kelvin-onlabbbe46482015-01-16 10:44:28 -080060 else:
61 main.log.info( "NO HANDLE" )
timlindbergef8d55d2013-09-27 12:50:13 -070062 return main.FALSE
63
kelvin-onlab08679eb2015-01-21 16:11:48 -080064 def loginQuagga( self, ip_address ):
kelvin-onlabbbe46482015-01-16 10:44:28 -080065 self.name = self.options[ 'name' ]
66 self.handle = super( QuaggaCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080067 user_name=self.user_name, ip_address=ip_address,
kelvin-onlabbbe46482015-01-16 10:44:28 -080068 port=self.port, pwd=self.pwd )
kelvin-onlab2c4342a2015-01-28 15:59:53 -080069 main.log.info( "connect parameters:" + str( self.user_name ) + ";"
70 + str( self.ip_address ) + ";" + str( self.port )
71 + ";" + str( self.pwd ) )
pingping-lin8b306ac2014-11-17 18:13:51 -080072
73 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -080074 # 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#" )
kelvin-onlab2c4342a2015-01-28 15:59:53 -080085 main.log.info( "I am in quagga on host " + str( ip_address ) )
pingping-lin8b306ac2014-11-17 18:13:51 -080086
87 return self.handle
88 else:
kelvin-onlabbbe46482015-01-16 10:44:28 -080089 main.log.info( "NO HANDLE" )
pingping-lin8b306ac2014-11-17 18:13:51 -080090 return main.FALSE
91
kelvin-onlabd3b64892015-01-20 13:26:24 -080092 def enterConfig( self, asn ):
kelvin-onlabbbe46482015-01-16 10:44:28 -080093 main.log.info( "I am in enter_config method!" )
timlindbergef8d55d2013-09-27 12:50:13 -070094 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -080095 self.handle.sendline( "" )
96 self.handle.expect( "bgpd#" )
Jon Hallfebb1c72015-03-05 13:30:09 -080097 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -080098 main.log.warn( "Probably not currently in enable mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -070099 self.disconnect()
100 return main.FALSE
kelvin-onlabbbe46482015-01-16 10:44:28 -0800101 self.handle.sendline( "configure terminal" )
102 self.handle.expect( "config", timeout=5 )
103 routerAS = "router bgp " + str( asn )
timlindbergef8d55d2013-09-27 12:50:13 -0700104 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800105 self.handle.sendline( routerAS )
106 self.handle.expect( "config-router", timeout=5 )
timlindbergef8d55d2013-09-27 12:50:13 -0700107 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800108 except Exception:
timlindbergef8d55d2013-09-27 12:50:13 -0700109 return main.FALSE
pingping-lin8b306ac2014-11-17 18:13:51 -0800110
kelvin-onlabd3b64892015-01-20 13:26:24 -0800111 def generatePrefixes( self, net, numRoutes ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800112 main.log.info( "I am in generate_prefixes method!" )
pingping-lin6f6332e2014-11-19 19:13:58 -0800113
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800114 # each IP prefix is composed by "net" + "." + m + "." + n + "." + x
pingping-lin8b306ac2014-11-17 18:13:51 -0800115 # the length of each IP prefix is 24
116 routes = []
kelvin-onlabd3b64892015-01-20 13:26:24 -0800117 routesGen = 0
pingping-lin8b306ac2014-11-17 18:13:51 -0800118 m = numRoutes / 256
119 n = numRoutes % 256
120
kelvin-onlabbbe46482015-01-16 10:44:28 -0800121 for i in range( 0, m ):
122 for j in range( 0, 256 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800123 network = str( net ) + "." + str( i ) + "." + str( j ) \
124 + ".0/24"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800125 routes.append( network )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800126 routesGen = routesGen + 1
pingping-lin8b306ac2014-11-17 18:13:51 -0800127
kelvin-onlabbbe46482015-01-16 10:44:28 -0800128 for j in range( 0, n ):
129 network = str( net ) + "." + str( m ) + "." + str( j ) + ".0/24"
130 routes.append( network )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800131 routesGen = routesGen + 1
pingping-lin8b306ac2014-11-17 18:13:51 -0800132
kelvin-onlabd3b64892015-01-20 13:26:24 -0800133 if routesGen == numRoutes:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800134 main.log.info( "Successfully generated " + str( numRoutes )
135 + " prefixes!" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800136 return routes
137 return main.FALSE
pingping-lin6f6332e2014-11-19 19:13:58 -0800138
kelvin-onlabbbe46482015-01-16 10:44:28 -0800139 # This method generates a multiple to single point intent(
140 # MultiPointToSinglePointIntent ) for a given route
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800141 def generateExpectedSingleRouteIntent( self, prefix, nextHop, nextHopMac,
142 sdnipData ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800143
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800144 ingresses = []
pingping-lin6f6332e2014-11-19 19:13:58 -0800145 egress = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800146 for peer in sdnipData[ 'bgpPeers' ]:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800147 if peer[ 'ipAddress' ] == nextHop:
148 egress = "of:" + str(
149 peer[ 'attachmentDpid' ] ).replace( ":", "" ) + ":" \
150 + str( peer[ 'attachmentPort' ] )
151 for peer in sdnipData[ 'bgpPeers' ]:
152 if not peer[ 'ipAddress' ] == nextHop:
153 ingress = "of:" + str(
154 peer[ 'attachmentDpid' ] ).replace( ":", "" ) + ":" \
155 + str( peer[ 'attachmentPort' ] )
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-lin6f6332e2014-11-19 19:13:58 -0800161
pingping-linc6b86fa2014-12-01 16:18:10 -0800162 selector = "ETH_TYPE{ethType=800},IPV4_DST{ip=" + prefix + "}"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800163 treatment = "[ETH_DST{mac=" + str( nextHopMac ) + "}]"
pingping-lin6f6332e2014-11-19 19:13:58 -0800164
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800165 intent = egress + "/" + str( sorted( ingresses ) ) + "/" + \
166 selector + "/" + treatment
pingping-lin6f6332e2014-11-19 19:13:58 -0800167 return intent
168
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800169 def generateExpectedOnePeerRouteIntents( self, prefixes, nextHop,
170 nextHopMac, sdnipJsonFilePath ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800171 intents = []
kelvin-onlabd3b64892015-01-20 13:26:24 -0800172 sdnipJsonFile = open( sdnipJsonFilePath ).read()
pingping-lin6f6332e2014-11-19 19:13:58 -0800173
kelvin-onlabd3b64892015-01-20 13:26:24 -0800174 sdnipData = json.loads( sdnipJsonFile )
pingping-lin6f6332e2014-11-19 19:13:58 -0800175
176 for prefix in prefixes:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800177 intents.append(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800178 self.generateExpectedSingleRouteIntent(
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800179 prefix, nextHop, nextHopMac, sdnipData ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800180 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800181
182 # TODO
183 # This method generates all expected route intents for all BGP peers
kelvin-onlabd3b64892015-01-20 13:26:24 -0800184 def generateExpectedRouteIntents( self ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800185 intents = []
186 return intents
187
188 # This method extracts all actual routes from ONOS CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800189 def extractActualRoutes( self, getRoutesResult ):
190 routesJsonObj = json.loads( getRoutesResult )
pingping-lin6f6332e2014-11-19 19:13:58 -0800191
kelvin-onlabd3b64892015-01-20 13:26:24 -0800192 allRoutesActual = []
Srikanth Vavilapalli68a1a2b2015-02-27 10:29:14 -0800193 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-lin6f6332e2014-11-19 19:13:58 -0800199
kelvin-onlabd3b64892015-01-20 13:26:24 -0800200 return sorted( allRoutesActual )
pingping-lin6f6332e2014-11-19 19:13:58 -0800201
202 # This method extracts all actual route intents from ONOS CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800203 def extractActualRouteIntents( self, getIntentsResult ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800204 intents = []
205 # TODO: delete the line below when change to Mininet demo script
kelvin-onlabd3b64892015-01-20 13:26:24 -0800206 # getIntentsResult=open( "../tests/SdnIpTest/intents.json" ).read()
207 intentsJsonObj = json.loads( getIntentsResult )
pingping-lin6f6332e2014-11-19 19:13:58 -0800208
kelvin-onlabd3b64892015-01-20 13:26:24 -0800209 for intent in intentsJsonObj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800210 if intent[ 'appId' ] != "org.onosproject.sdnip":
pingping-lin6f6332e2014-11-19 19:13:58 -0800211 continue
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800212 if intent[ 'type' ] == "MultiPointToSinglePointIntent" \
213 and intent[ 'state' ] == 'INSTALLED':
214 egress = str( intent[ 'egress' ][ 'device' ] ) + ":" \
215 + str( intent[ 'egress' ][ 'port' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800216 ingress = []
kelvin-onlabbbe46482015-01-16 10:44:28 -0800217 for attachmentPoint in intent[ 'ingress' ]:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800218 ingress.append(
219 str( attachmentPoint[ 'device' ] ) + ":"
220 + str( attachmentPoint[ 'port' ] ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800221
kelvin-onlabbbe46482015-01-16 10:44:28 -0800222 selector = intent[ 'selector' ].replace(
223 "[", "" ).replace( "]", "" ).replace( " ", "" )
224 if str( selector ).startswith( "IPV4" ):
225 str1, str2 = str( selector ).split( "," )
pingping-linc6b86fa2014-12-01 16:18:10 -0800226 selector = str2 + "," + str1
227
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800228 intent = egress + "/" + str( sorted( ingress ) ) + "/" + \
kelvin-onlabd3b64892015-01-20 13:26:24 -0800229 selector + "/" + intent[ 'treatment' ]
kelvin-onlabbbe46482015-01-16 10:44:28 -0800230 intents.append( intent )
231 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800232
233 # This method extracts all actual BGP intents from ONOS CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800234 def extractActualBgpIntents( self, getIntentsResult ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800235 intents = []
236 # TODO: delete the line below when change to Mininet demo script
kelvin-onlabd3b64892015-01-20 13:26:24 -0800237 # getIntentsResult=open( "../tests/SdnIpTest/intents.json" ).read()
238 intentsJsonObj = json.loads( getIntentsResult )
pingping-lin6f6332e2014-11-19 19:13:58 -0800239
kelvin-onlabd3b64892015-01-20 13:26:24 -0800240 for intent in intentsJsonObj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800241 if intent[ 'appId' ] != "org.onosproject.sdnip":
pingping-lin6f6332e2014-11-19 19:13:58 -0800242 continue
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800243 if intent[ 'type' ] == "PointToPointIntent" \
244 and "protocol=6" in str( intent[ 'selector' ] ):
245 ingress = str( intent[ 'ingress' ][ 'device' ] ) + ":" \
246 + str( intent[ 'ingress' ][ 'port' ] )
247 egress = str( intent[ 'egress' ][ 'device' ] ) + ":" + \
248 str( intent[ 'egress' ][ 'port' ] )
249 selector = str( intent[ 'selector' ] ).replace( " ", "" )\
250 .replace( "[", "" ).replace( "]", "" ).split( "," )
251 intent = ingress + "/" + egress + "/" + \
252 str( sorted( selector ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800253 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800254
kelvin-onlabbbe46482015-01-16 10:44:28 -0800255 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800256
kelvin-onlabbbe46482015-01-16 10:44:28 -0800257 # This method generates a single point to single point intent(
258 # PointToPointIntent ) for BGP path
kelvin-onlabd3b64892015-01-20 13:26:24 -0800259 def generateExpectedBgpIntents( self, sdnipJsonFilePath ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800260 from operator import eq
261
kelvin-onlabd3b64892015-01-20 13:26:24 -0800262 sdnipJsonFile = open( sdnipJsonFilePath ).read()
263 sdnipData = json.loads( sdnipJsonFile )
pingping-lin6f6332e2014-11-19 19:13:58 -0800264
265 intents = []
pingping-linc6b86fa2014-12-01 16:18:10 -0800266 bgpPeerAttachmentPoint = ""
kelvin-onlabbbe46482015-01-16 10:44:28 -0800267 bgpSpeakerAttachmentPoint = "of:" + str(
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800268 sdnipData[ 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] )\
269 .replace( ":", "" ) + ":" \
270 + str( sdnipData[ 'bgpSpeakers' ][ 0 ][ 'attachmentPort' ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800271 for peer in sdnipData[ 'bgpPeers' ]:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800272 bgpPeerAttachmentPoint = "of:" \
273 + str( peer[ 'attachmentDpid' ] ).replace( ":", "" ) \
274 + ":" + str( peer[ 'attachmentPort' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800275 # find out the BGP speaker IP address for this BGP peer
pingping-linc6b86fa2014-12-01 16:18:10 -0800276 bgpSpeakerIpAddress = ""
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800277 for interfaceAddress in \
278 sdnipData[ 'bgpSpeakers' ][ 0 ][ 'interfaceAddresses' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800279 # if eq( interfaceAddress[ 'interfaceDpid' ],sdnipData[
kelvin-onlabbbe46482015-01-16 10:44:28 -0800280 # 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] ) and eq(
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800281 # interfaceAddress[ 'interfacePort' ], sdnipData[ 'bgpSpeakers'
282 # ][ 0 ][ 'attachmentPort' ] ):
283 if eq( interfaceAddress[ 'interfaceDpid' ],
284 peer[ 'attachmentDpid' ] ) \
285 and eq( interfaceAddress[ 'interfacePort' ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800286 peer[ 'attachmentPort' ] ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800287 bgpSpeakerIpAddress = interfaceAddress[ 'ipAddress' ]
pingping-lin6f6332e2014-11-19 19:13:58 -0800288 break
289 else:
290 continue
291
kelvin-onlabbbe46482015-01-16 10:44:28 -0800292 # from bgpSpeakerAttachmentPoint to bgpPeerAttachmentPoint
293 # direction
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800294 selectorStr = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," \
295 + "IPV4_DST{ip=" + peer[ 'ipAddress' ] + "/32}," \
296 + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \
297 TCP_DST{tcpPort=179}"
298 selector = selectorStr.replace( " ", "" ).replace("[", "" )\
299 .replace( "]", "" ).split( "," )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800300 intent = bgpSpeakerAttachmentPoint + "/" + \
301 bgpPeerAttachmentPoint + "/" + str( sorted( selector ) )
302 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800303
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800304 selectorStr = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," \
305 + "IPV4_DST{ip=" + peer[ 'ipAddress' ] + "/32}," \
306 + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \
307 TCP_SRC{tcpPort=179}"
308 selector = selectorStr.replace( " ", "" ).replace("[", "" )\
309 .replace( "]", "" ).split( "," )
310 intent = bgpSpeakerAttachmentPoint + "/" \
311 + bgpPeerAttachmentPoint + "/" + str( sorted( selector ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800312 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800313
kelvin-onlabbbe46482015-01-16 10:44:28 -0800314 # from bgpPeerAttachmentPoint to bgpSpeakerAttachmentPoint
315 # direction
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800316 selectorStr = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," \
317 + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," \
318 + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \
319 TCP_DST{tcpPort=179}"
320 selector = selectorStr.replace( " ", "" ).replace("[", "" )\
321 .replace( "]", "" ).split( "," )
322 intent = bgpPeerAttachmentPoint + "/" \
323 + bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800324 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800325
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800326 selectorStr = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," \
327 + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," \
328 + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \
329 TCP_SRC{tcpPort=179}"
330 selector = selectorStr.replace( " ", "" ).replace( "[", "" )\
331 .replace( "]", "" ).split( "," )
332 intent = bgpPeerAttachmentPoint + "/" \
333 + bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800334 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800335
kelvin-onlabbbe46482015-01-16 10:44:28 -0800336 return sorted( intents )
pingping-linc6b86fa2014-12-01 16:18:10 -0800337
kelvin-onlabd3b64892015-01-20 13:26:24 -0800338 def addRoutes( self, routes, routeRate ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800339 main.log.info( "I am in add_routes method!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800340
kelvin-onlabd3b64892015-01-20 13:26:24 -0800341 routesAdded = 0
pingping-lin8b306ac2014-11-17 18:13:51 -0800342 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800343 self.handle.sendline( "" )
344 # self.handle.expect( "config-router" )
345 self.handle.expect( "config-router", timeout=5 )
Jon Hallfebb1c72015-03-05 13:30:09 -0800346 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800347 main.log.warn( "Probably not in config-router mode!" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800348 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800349 main.log.info( "Start to add routes" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800350
sanghoshin70502002015-03-11 10:21:14 -0700351 chunk_size = 20
352
353 if len(routes) > chunk_size:
354 num_iter = (int) (len(routes) / chunk_size)
355 else:
356 num_iter = 1;
357
358 total = 0
359 for n in range( 0, num_iter + 1):
360 routeCmd = ""
361 if (len( routes ) - (n * chunk_size)) >= chunk_size:
362 m = (n + 1) * chunk_size
363 else:
364 m = len( routes )
365 for i in range( n * chunk_size, m ):
366 routeCmd = routeCmd + "network " + routes[ i ] + "\n"
367 total = total + 1
368
369 main.log.info(routeCmd)
pingping-lin8b306ac2014-11-17 18:13:51 -0800370 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800371 self.handle.sendline( routeCmd )
372 self.handle.expect( "bgpd", timeout=5 )
Jon Hallfebb1c72015-03-05 13:30:09 -0800373 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800374 main.log.warn( "Failed to add route" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800375 self.disconnect()
sanghoshin70502002015-03-11 10:21:14 -0700376
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800377 # waitTimer = 1.00 / routeRate
sanghoshin70502002015-03-11 10:21:14 -0700378 main.log.info("Total routes so far " + ((str) (total)) + " wait for 0 sec")
379 #time.sleep( 1 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800380 if routesAdded == len( routes ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800381 main.log.info( "Finished adding routes" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800382 return main.TRUE
383 return main.FALSE
pingping-linc6b86fa2014-12-01 16:18:10 -0800384
kelvin-onlabd3b64892015-01-20 13:26:24 -0800385 def deleteRoutes( self, routes, routeRate ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800386 main.log.info( "I am in delete_routes method!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800387
kelvin-onlabd3b64892015-01-20 13:26:24 -0800388 routesAdded = 0
pingping-linc6b86fa2014-12-01 16:18:10 -0800389 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800390 self.handle.sendline( "" )
391 # self.handle.expect( "config-router" )
392 self.handle.expect( "config-router", timeout=5 )
Jon Hallfebb1c72015-03-05 13:30:09 -0800393 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800394 main.log.warn( "Probably not in config-router mode!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800395 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800396 main.log.info( "Start to delete routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800397
kelvin-onlabbbe46482015-01-16 10:44:28 -0800398 for i in range( 0, len( routes ) ):
399 routeCmd = "no network " + routes[ i ]
pingping-linc6b86fa2014-12-01 16:18:10 -0800400 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800401 self.handle.sendline( routeCmd )
402 self.handle.expect( "bgpd", timeout=5 )
Jon Hallfebb1c72015-03-05 13:30:09 -0800403 except Exception:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800404 main.log.warn( "Failed to delete route" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800405 self.disconnect()
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800406 # waitTimer = 1.00 / routeRate
407 # time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800408 if routesAdded == len( routes ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800409 main.log.info( "Finished deleting routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800410 return main.TRUE
411 return main.FALSE
412
kelvin-onlab08679eb2015-01-21 16:11:48 -0800413 def pingTest( self, ip_address, pingTestFile, pingTestResultFile ):
414 main.log.info( "Start the ping test on host:" + str( ip_address ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800415
kelvin-onlabbbe46482015-01-16 10:44:28 -0800416 self.name = self.options[ 'name' ]
417 self.handle = super( QuaggaCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -0800418 user_name=self.user_name, ip_address=ip_address,
kelvin-onlabbbe46482015-01-16 10:44:28 -0800419 port=self.port, pwd=self.pwd )
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800420 main.log.info( "connect parameters:" + str( self.user_name ) + ";"
421 + str( self.ip_address ) + ";" + str( self.port )
422 + ";" + str( self.pwd ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800423
424 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800425 # self.handle.expect( "" )
426 # self.handle.expect( "\$" )
kelvin-onlab08679eb2015-01-21 16:11:48 -0800427 main.log.info( "I in host " + str( ip_address ) )
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800428 main.log.info( pingTestFile + " > " + pingTestResultFile + " &" )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800429 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800430 pingTestFile +
kelvin-onlabbbe46482015-01-16 10:44:28 -0800431 " > " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800432 pingTestResultFile +
kelvin-onlabbbe46482015-01-16 10:44:28 -0800433 " &" )
434 self.handle.expect( "\$", timeout=60 )
pingping-linc6b86fa2014-12-01 16:18:10 -0800435 handle = self.handle.before
436
437 return handle
438 else:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800439 main.log.info( "NO HANDLE" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800440 return main.FALSE
441
sanghoshin29e6a472015-02-27 12:55:06 -0800442
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800443 # Please use the generateRoutes plus addRoutes instead of this one!
kelvin-onlabd3b64892015-01-20 13:26:24 -0800444 def addRoute( self, net, numRoutes, routeRate ):
timlindbergef8d55d2013-09-27 12:50:13 -0700445 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800446 self.handle.sendline( "" )
447 self.handle.expect( "config-router" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800448 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800449 main.log.warn( "Probably not in config-router mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -0700450 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800451 main.log.info( "Adding Routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800452 j = 0
453 k = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700454 while numRoutes > 255:
455 numRoutes = numRoutes - 255
456 j = j + 1
457 k = numRoutes % 254
kelvin-onlabd3b64892015-01-20 13:26:24 -0800458 routesAdded = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700459 if numRoutes > 255:
460 numRoutes = 255
kelvin-onlabbbe46482015-01-16 10:44:28 -0800461 for m in range( 1, j + 1 ):
462 for n in range( 1, numRoutes + 1 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800463 network = str( net ) + "." + str( m ) + "." + str( n ) \
464 + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700465 routeCmd = "network " + network
466 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800467 self.handle.sendline( routeCmd )
468 self.handle.expect( "bgpd" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800469 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800470 main.log.warn( "failed to add route" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800471 self.disconnect()
472 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800473 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800474 routesAdded = routesAdded + 1
kelvin-onlabbbe46482015-01-16 10:44:28 -0800475 for d in range( j + 1, j + 2 ):
476 for e in range( 1, k + 1 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800477 network = str( net ) + "." + str( d ) + "." + str( e ) \
478 + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700479 routeCmd = "network " + network
480 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800481 self.handle.sendline( routeCmd )
482 self.handle.expect( "bgpd" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800483 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800484 main.log.warn( "failed to add route" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800485 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800486 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800487 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800488 routesAdded = routesAdded + 1
489 if routesAdded == numRoutes:
timlindbergef8d55d2013-09-27 12:50:13 -0700490 return main.TRUE
491 return main.FALSE
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800492
493 # Please use deleteRoutes method instead of this one!
kelvin-onlabd3b64892015-01-20 13:26:24 -0800494 def delRoute( self, net, numRoutes, routeRate ):
timlindbergef8d55d2013-09-27 12:50:13 -0700495 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800496 self.handle.sendline( "" )
497 self.handle.expect( "config-router" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800498 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800499 main.log.warn( "Probably not in config-router mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -0700500 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800501 main.log.info( "Deleting Routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800502 j = 0
503 k = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700504 while numRoutes > 255:
505 numRoutes = numRoutes - 255
506 j = j + 1
507 k = numRoutes % 254
kelvin-onlabd3b64892015-01-20 13:26:24 -0800508 routesDeleted = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700509 if numRoutes > 255:
510 numRoutes = 255
kelvin-onlabbbe46482015-01-16 10:44:28 -0800511 for m in range( 1, j + 1 ):
512 for n in range( 1, numRoutes + 1 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800513 network = str( net ) + "." + str( m ) + "." + str( n ) \
514 + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700515 routeCmd = "no network " + network
516 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800517 self.handle.sendline( routeCmd )
518 self.handle.expect( "bgpd" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800519 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800520 main.log.warn( "Failed to delete route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700521 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800522 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800523 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800524 routesDeleted = routesDeleted + 1
kelvin-onlabbbe46482015-01-16 10:44:28 -0800525 for d in range( j + 1, j + 2 ):
526 for e in range( 1, k + 1 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800527 network = str( net ) + "." + str( d ) + "." + str( e ) \
528 + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700529 routeCmd = "no network " + network
530 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800531 self.handle.sendline( routeCmd )
532 self.handle.expect( "bgpd" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800533 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800534 main.log.warn( "Failed to delete route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700535 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800536 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800537 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800538 routesDeleted = routesDeleted + 1
539 if routesDeleted == numRoutes:
timlindbergef8d55d2013-09-27 12:50:13 -0700540 return main.TRUE
541 return main.FALSE
pingping-linc6b86fa2014-12-01 16:18:10 -0800542
kelvin-onlabd3b64892015-01-20 13:26:24 -0800543 def checkRoutes( self, brand, ip, user, pw ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800544 def pronto( ip, user, passwd ):
timlindbergef8d55d2013-09-27 12:50:13 -0700545 print "Connecting to Pronto switch"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800546 child = pexpect.spawn( "telnet " + ip )
547 i = child.expect( [ "login:", "CLI#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700548 if i == 0:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800549 print "user_name and password required. Passing login info."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800550 child.sendline( user )
551 child.expect( "Password:" )
552 child.sendline( passwd )
553 child.expect( "CLI#" )
timlindbergef8d55d2013-09-27 12:50:13 -0700554 print "Logged in, getting flowtable."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800555 child.sendline( "flowtable brief" )
556 for t in range( 9 ):
timlindbergef8d55d2013-09-27 12:50:13 -0700557 t2 = 9 - t
kelvin-onlabbbe46482015-01-16 10:44:28 -0800558 print "\r" + str( t2 )
559 sys.stdout.write( "\033[F" )
560 time.sleep( 1 )
timlindbergef8d55d2013-09-27 12:50:13 -0700561 print "Scanning flowtable"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800562 child.expect( "Flow table show" )
timlindbergef8d55d2013-09-27 12:50:13 -0700563 count = 0
kelvin-onlabbbe46482015-01-16 10:44:28 -0800564 while True:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800565 i = child.expect( [ '17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}',
566 'CLI#', pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700567 if i == 0:
568 count = count + 1
569 elif i == 1:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800570 print "Pronto flows: " + str( count ) + "\nDone\n"
timlindbergef8d55d2013-09-27 12:50:13 -0700571 break
572 else:
573 break
kelvin-onlabbbe46482015-01-16 10:44:28 -0800574
575 def cisco( ip, user, passwd ):
timlindbergef8d55d2013-09-27 12:50:13 -0700576 print "Establishing Cisco switch connection"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800577 child = pexpect.spawn( "ssh " + user + "@" + ip )
578 i = child.expect( [ "Password:", "CLI#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700579 if i == 0:
580 print "Password required. Passing now."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800581 child.sendline( passwd )
582 child.expect( "#" )
timlindbergef8d55d2013-09-27 12:50:13 -0700583 print "Logged in. Retrieving flow table then counting flows."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800584 child.sendline( "show openflow switch all flows all" )
585 child.expect( "Logical Openflow Switch" )
timlindbergef8d55d2013-09-27 12:50:13 -0700586 print "Flow table retrieved. Counting flows"
587 count = 0
kelvin-onlabbbe46482015-01-16 10:44:28 -0800588 while True:
589 i = child.expect( [ "nw_src=17", "#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700590 if i == 0:
591 count = count + 1
592 elif i == 1:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800593 print "Cisco flows: " + str( count ) + "\nDone\n"
timlindbergef8d55d2013-09-27 12:50:13 -0700594 break
595 else:
596 break
597 if brand == "pronto" or brand == "PRONTO":
kelvin-onlabbbe46482015-01-16 10:44:28 -0800598 pronto( ip, user, passwd )
pingping-linc6b86fa2014-12-01 16:18:10 -0800599 # elif brand == "cisco" or brand == "CISCO":
kelvin-onlabbbe46482015-01-16 10:44:28 -0800600 # cisco( ip,user,passwd )
601
Srikanth Vavilapalli68a1a2b2015-02-27 10:29:14 -0800602 def disable_bgp_peer( self, peer, peer_as ):
603 main.log.info( "I am in disconnect_peer_session method!" )
604
605 try:
606 self.handle.sendline( "" )
607 # self.handle.expect( "config-router" )
608 self.handle.expect( "config-router", timeout=5 )
Jon Hall77ba41c2015-04-06 10:25:40 -0700609 except Exception:
Srikanth Vavilapalli68a1a2b2015-02-27 10:29:14 -0800610 main.log.warn( "Probably not in config-router mode!" )
611 self.disconnect()
612 main.log.info( "Start to disable peer" )
613
614 cmd = "no neighbor " + peer + " remote-as " + peer_as
615 try:
616 self.handle.sendline( cmd )
617 self.handle.expect( "bgpd", timeout=5 )
Jon Hall77ba41c2015-04-06 10:25:40 -0700618 except Exception:
Srikanth Vavilapalli68a1a2b2015-02-27 10:29:14 -0800619 main.log.warn( "Failed to disable peer" )
620 self.disconnect()
621
Srikanth Vavilapalli43f3a1d2015-03-04 11:09:44 -0800622 def enable_bgp_peer( self, peer, peer_as ):
623 main.log.info( "I am in enable_bgp_peer method!" )
624
625 try:
626 self.handle.sendline( "" )
627 # self.handle.expect( "config-router" )
628 self.handle.expect( "config-router", timeout=5 )
Jon Hall77ba41c2015-04-06 10:25:40 -0700629 except Exception:
Srikanth Vavilapalli43f3a1d2015-03-04 11:09:44 -0800630 main.log.warn( "Probably not in config-router mode!" )
631 self.disconnect()
632 main.log.info( "Start to disable peer" )
633
634 cmd = "neighbor " + peer + " remote-as " + peer_as
635 try:
636 self.handle.sendline( cmd )
637 self.handle.expect( "bgpd", timeout=5 )
Jon Hall77ba41c2015-04-06 10:25:40 -0700638 except Exception:
Srikanth Vavilapalli43f3a1d2015-03-04 11:09:44 -0800639 main.log.warn( "Failed to enable peer" )
640 self.disconnect()
641
kelvin-onlabbbe46482015-01-16 10:44:28 -0800642 def disconnect( self ):
643 """
644 Called when Test is complete to disconnect the Quagga handle.
645 """
timlindbergef8d55d2013-09-27 12:50:13 -0700646 response = ''
647 try:
648 self.handle.close()
Jon Hallfebb1c72015-03-05 13:30:09 -0800649 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800650 main.log.error( "Connection failed to the host" )
timlindbergef8d55d2013-09-27 12:50:13 -0700651 response = main.FALSE
652 return response
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800653