blob: 5631c9b5a0d986bca22ad1f1c4a86875fc14eb78 [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 ]
pingping-linc6b86fa2014-12-01 16:18:10 -080020
kelvin-onlabbbe46482015-01-16 10:44:28 -080021 self.name = self.options[ 'name' ]
22 # self.handle = super( QuaggaCliDriver,self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080023 # user_name=self.user_name, ip_address=self.ip_address,port=self.port,
kelvin-onlabbbe46482015-01-16 10:44:28 -080024 # pwd=self.pwd )
kelvin-onlab2c4342a2015-01-28 15:59:53 -080025 self.handle = super( QuaggaCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080026 user_name=self.user_name,
kelvin-onlab2c4342a2015-01-28 15:59:53 -080027 ip_address="1.1.1.1",
28 port=self.port,
29 pwd=self.pwd )
30 main.log.info( "connect parameters:" + str( self.user_name ) + ";"
31 + str( self.ip_address ) + ";" + str( self.port )
32 + ";" + str(self.pwd ) )
timlindbergef8d55d2013-09-27 12:50:13 -070033
pingping-linc6b86fa2014-12-01 16:18:10 -080034 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -080035 # self.handle.expect( "",timeout=10 )
36 # self.handle.expect( "\$",timeout=10 )
37 self.handle.sendline( "telnet localhost 2605" )
38 # self.handle.expect( "Password:", timeout=5 )
39 self.handle.expect( "Password:" )
40 self.handle.sendline( "hello" )
41 # self.handle.expect( "bgpd", timeout=5 )
42 self.handle.expect( "bgpd" )
43 self.handle.sendline( "enable" )
44 # self.handle.expect( "bgpd#", timeout=5 )
45 self.handle.expect( "bgpd#" )
timlindbergef8d55d2013-09-27 12:50:13 -070046 return self.handle
kelvin-onlabbbe46482015-01-16 10:44:28 -080047 else:
48 main.log.info( "NO HANDLE" )
timlindbergef8d55d2013-09-27 12:50:13 -070049 return main.FALSE
50
kelvin-onlab08679eb2015-01-21 16:11:48 -080051 def loginQuagga( self, ip_address ):
kelvin-onlabbbe46482015-01-16 10:44:28 -080052 self.name = self.options[ 'name' ]
53 self.handle = super( QuaggaCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080054 user_name=self.user_name, ip_address=ip_address,
kelvin-onlabbbe46482015-01-16 10:44:28 -080055 port=self.port, pwd=self.pwd )
kelvin-onlab2c4342a2015-01-28 15:59:53 -080056 main.log.info( "connect parameters:" + str( self.user_name ) + ";"
57 + str( self.ip_address ) + ";" + str( self.port )
58 + ";" + str( self.pwd ) )
pingping-lin8b306ac2014-11-17 18:13:51 -080059
60 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -080061 # self.handle.expect( "" )
62 # self.handle.expect( "\$" )
63 self.handle.sendline( "telnet localhost 2605" )
64 # self.handle.expect( "Password:", timeout=5 )
65 self.handle.expect( "Password:" )
66 self.handle.sendline( "hello" )
67 # self.handle.expect( "bgpd", timeout=5 )
68 self.handle.expect( "bgpd" )
69 self.handle.sendline( "enable" )
70 # self.handle.expect( "bgpd#", timeout=5 )
71 self.handle.expect( "bgpd#" )
kelvin-onlab2c4342a2015-01-28 15:59:53 -080072 main.log.info( "I am in quagga on host " + str( ip_address ) )
pingping-lin8b306ac2014-11-17 18:13:51 -080073
74 return self.handle
75 else:
kelvin-onlabbbe46482015-01-16 10:44:28 -080076 main.log.info( "NO HANDLE" )
pingping-lin8b306ac2014-11-17 18:13:51 -080077 return main.FALSE
78
kelvin-onlabd3b64892015-01-20 13:26:24 -080079 def enterConfig( self, asn ):
kelvin-onlabbbe46482015-01-16 10:44:28 -080080 main.log.info( "I am in enter_config method!" )
timlindbergef8d55d2013-09-27 12:50:13 -070081 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -080082 self.handle.sendline( "" )
83 self.handle.expect( "bgpd#" )
Jon Hallfebb1c72015-03-05 13:30:09 -080084 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -080085 main.log.warn( "Probably not currently in enable mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -070086 self.disconnect()
87 return main.FALSE
kelvin-onlabbbe46482015-01-16 10:44:28 -080088 self.handle.sendline( "configure terminal" )
89 self.handle.expect( "config", timeout=5 )
90 routerAS = "router bgp " + str( asn )
timlindbergef8d55d2013-09-27 12:50:13 -070091 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -080092 self.handle.sendline( routerAS )
93 self.handle.expect( "config-router", timeout=5 )
timlindbergef8d55d2013-09-27 12:50:13 -070094 return main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -080095 except Exception:
timlindbergef8d55d2013-09-27 12:50:13 -070096 return main.FALSE
pingping-lin8b306ac2014-11-17 18:13:51 -080097
kelvin-onlabd3b64892015-01-20 13:26:24 -080098 def generatePrefixes( self, net, numRoutes ):
kelvin-onlabbbe46482015-01-16 10:44:28 -080099 main.log.info( "I am in generate_prefixes method!" )
pingping-lin6f6332e2014-11-19 19:13:58 -0800100
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800101 # each IP prefix is composed by "net" + "." + m + "." + n + "." + x
pingping-lin8b306ac2014-11-17 18:13:51 -0800102 # the length of each IP prefix is 24
103 routes = []
kelvin-onlabd3b64892015-01-20 13:26:24 -0800104 routesGen = 0
pingping-lin8b306ac2014-11-17 18:13:51 -0800105 m = numRoutes / 256
106 n = numRoutes % 256
107
kelvin-onlabbbe46482015-01-16 10:44:28 -0800108 for i in range( 0, m ):
109 for j in range( 0, 256 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800110 network = str( net ) + "." + str( i ) + "." + str( j ) \
111 + ".0/24"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800112 routes.append( network )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800113 routesGen = routesGen + 1
pingping-lin8b306ac2014-11-17 18:13:51 -0800114
kelvin-onlabbbe46482015-01-16 10:44:28 -0800115 for j in range( 0, n ):
116 network = str( net ) + "." + str( m ) + "." + str( j ) + ".0/24"
117 routes.append( network )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800118 routesGen = routesGen + 1
pingping-lin8b306ac2014-11-17 18:13:51 -0800119
kelvin-onlabd3b64892015-01-20 13:26:24 -0800120 if routesGen == numRoutes:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800121 main.log.info( "Successfully generated " + str( numRoutes )
122 + " prefixes!" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800123 return routes
124 return main.FALSE
pingping-lin6f6332e2014-11-19 19:13:58 -0800125
kelvin-onlabbbe46482015-01-16 10:44:28 -0800126 # This method generates a multiple to single point intent(
127 # MultiPointToSinglePointIntent ) for a given route
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800128 def generateExpectedSingleRouteIntent( self, prefix, nextHop, nextHopMac,
129 sdnipData ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800130
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800131 ingresses = []
pingping-lin6f6332e2014-11-19 19:13:58 -0800132 egress = ""
kelvin-onlabd3b64892015-01-20 13:26:24 -0800133 for peer in sdnipData[ 'bgpPeers' ]:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800134 if peer[ 'ipAddress' ] == nextHop:
135 egress = "of:" + str(
136 peer[ 'attachmentDpid' ] ).replace( ":", "" ) + ":" \
137 + str( peer[ 'attachmentPort' ] )
138 for peer in sdnipData[ 'bgpPeers' ]:
139 if not peer[ 'ipAddress' ] == nextHop:
140 ingress = "of:" + str(
141 peer[ 'attachmentDpid' ] ).replace( ":", "" ) + ":" \
142 + str( peer[ 'attachmentPort' ] )
143 if not ingress == egress and ingress not in ingresses:
144 ingresses.append( ingress )
145 # ingresses.append( "of:" + str( peer[ 'attachmentDpid' ]
146 # ).replace( ":", "" ) + ":" + str( peer[ 'attachmentPort'
147 # ] ) )
pingping-lin6f6332e2014-11-19 19:13:58 -0800148
pingping-linc6b86fa2014-12-01 16:18:10 -0800149 selector = "ETH_TYPE{ethType=800},IPV4_DST{ip=" + prefix + "}"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800150 treatment = "[ETH_DST{mac=" + str( nextHopMac ) + "}]"
pingping-lin6f6332e2014-11-19 19:13:58 -0800151
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800152 intent = egress + "/" + str( sorted( ingresses ) ) + "/" + \
153 selector + "/" + treatment
pingping-lin6f6332e2014-11-19 19:13:58 -0800154 return intent
155
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800156 def generateExpectedOnePeerRouteIntents( self, prefixes, nextHop,
157 nextHopMac, sdnipJsonFilePath ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800158 intents = []
kelvin-onlabd3b64892015-01-20 13:26:24 -0800159 sdnipJsonFile = open( sdnipJsonFilePath ).read()
pingping-lin6f6332e2014-11-19 19:13:58 -0800160
kelvin-onlabd3b64892015-01-20 13:26:24 -0800161 sdnipData = json.loads( sdnipJsonFile )
pingping-lin6f6332e2014-11-19 19:13:58 -0800162
163 for prefix in prefixes:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800164 intents.append(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800165 self.generateExpectedSingleRouteIntent(
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800166 prefix, nextHop, nextHopMac, sdnipData ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800167 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800168
169 # TODO
170 # This method generates all expected route intents for all BGP peers
kelvin-onlabd3b64892015-01-20 13:26:24 -0800171 def generateExpectedRouteIntents( self ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800172 intents = []
173 return intents
174
175 # This method extracts all actual routes from ONOS CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800176 def extractActualRoutes( self, getRoutesResult ):
177 routesJsonObj = json.loads( getRoutesResult )
pingping-lin6f6332e2014-11-19 19:13:58 -0800178
kelvin-onlabd3b64892015-01-20 13:26:24 -0800179 allRoutesActual = []
180 for route in routesJsonObj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800181 if route[ 'prefix' ] == '172.16.10.0/24':
pingping-lin6f6332e2014-11-19 19:13:58 -0800182 continue
kelvin-onlabd3b64892015-01-20 13:26:24 -0800183 allRoutesActual.append(
kelvin-onlabbbe46482015-01-16 10:44:28 -0800184 route[ 'prefix' ] + "/" + route[ 'nextHop' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800185
kelvin-onlabd3b64892015-01-20 13:26:24 -0800186 return sorted( allRoutesActual )
pingping-lin6f6332e2014-11-19 19:13:58 -0800187
188 # This method extracts all actual route intents from ONOS CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800189 def extractActualRouteIntents( self, getIntentsResult ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800190 intents = []
191 # TODO: delete the line below when change to Mininet demo script
kelvin-onlabd3b64892015-01-20 13:26:24 -0800192 # getIntentsResult=open( "../tests/SdnIpTest/intents.json" ).read()
193 intentsJsonObj = json.loads( getIntentsResult )
pingping-lin6f6332e2014-11-19 19:13:58 -0800194
kelvin-onlabd3b64892015-01-20 13:26:24 -0800195 for intent in intentsJsonObj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800196 if intent[ 'appId' ] != "org.onosproject.sdnip":
pingping-lin6f6332e2014-11-19 19:13:58 -0800197 continue
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800198 if intent[ 'type' ] == "MultiPointToSinglePointIntent" \
199 and intent[ 'state' ] == 'INSTALLED':
200 egress = str( intent[ 'egress' ][ 'device' ] ) + ":" \
201 + str( intent[ 'egress' ][ 'port' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800202 ingress = []
kelvin-onlabbbe46482015-01-16 10:44:28 -0800203 for attachmentPoint in intent[ 'ingress' ]:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800204 ingress.append(
205 str( attachmentPoint[ 'device' ] ) + ":"
206 + str( attachmentPoint[ 'port' ] ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800207
kelvin-onlabbbe46482015-01-16 10:44:28 -0800208 selector = intent[ 'selector' ].replace(
209 "[", "" ).replace( "]", "" ).replace( " ", "" )
210 if str( selector ).startswith( "IPV4" ):
211 str1, str2 = str( selector ).split( "," )
pingping-linc6b86fa2014-12-01 16:18:10 -0800212 selector = str2 + "," + str1
213
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800214 intent = egress + "/" + str( sorted( ingress ) ) + "/" + \
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 selector + "/" + intent[ 'treatment' ]
kelvin-onlabbbe46482015-01-16 10:44:28 -0800216 intents.append( intent )
217 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800218
219 # This method extracts all actual BGP intents from ONOS CLI
kelvin-onlabd3b64892015-01-20 13:26:24 -0800220 def extractActualBgpIntents( self, getIntentsResult ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800221 intents = []
222 # TODO: delete the line below when change to Mininet demo script
kelvin-onlabd3b64892015-01-20 13:26:24 -0800223 # getIntentsResult=open( "../tests/SdnIpTest/intents.json" ).read()
224 intentsJsonObj = json.loads( getIntentsResult )
pingping-lin6f6332e2014-11-19 19:13:58 -0800225
kelvin-onlabd3b64892015-01-20 13:26:24 -0800226 for intent in intentsJsonObj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800227 if intent[ 'appId' ] != "org.onosproject.sdnip":
pingping-lin6f6332e2014-11-19 19:13:58 -0800228 continue
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800229 if intent[ 'type' ] == "PointToPointIntent" \
230 and "protocol=6" in str( intent[ 'selector' ] ):
231 ingress = str( intent[ 'ingress' ][ 'device' ] ) + ":" \
232 + str( intent[ 'ingress' ][ 'port' ] )
233 egress = str( intent[ 'egress' ][ 'device' ] ) + ":" + \
234 str( intent[ 'egress' ][ 'port' ] )
235 selector = str( intent[ 'selector' ] ).replace( " ", "" )\
236 .replace( "[", "" ).replace( "]", "" ).split( "," )
237 intent = ingress + "/" + egress + "/" + \
238 str( sorted( selector ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800239 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800240
kelvin-onlabbbe46482015-01-16 10:44:28 -0800241 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800242
kelvin-onlabbbe46482015-01-16 10:44:28 -0800243 # This method generates a single point to single point intent(
244 # PointToPointIntent ) for BGP path
kelvin-onlabd3b64892015-01-20 13:26:24 -0800245 def generateExpectedBgpIntents( self, sdnipJsonFilePath ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800246 from operator import eq
247
kelvin-onlabd3b64892015-01-20 13:26:24 -0800248 sdnipJsonFile = open( sdnipJsonFilePath ).read()
249 sdnipData = json.loads( sdnipJsonFile )
pingping-lin6f6332e2014-11-19 19:13:58 -0800250
251 intents = []
pingping-linc6b86fa2014-12-01 16:18:10 -0800252 bgpPeerAttachmentPoint = ""
kelvin-onlabbbe46482015-01-16 10:44:28 -0800253 bgpSpeakerAttachmentPoint = "of:" + str(
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800254 sdnipData[ 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] )\
255 .replace( ":", "" ) + ":" \
256 + str( sdnipData[ 'bgpSpeakers' ][ 0 ][ 'attachmentPort' ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800257 for peer in sdnipData[ 'bgpPeers' ]:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800258 bgpPeerAttachmentPoint = "of:" \
259 + str( peer[ 'attachmentDpid' ] ).replace( ":", "" ) \
260 + ":" + str( peer[ 'attachmentPort' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800261 # find out the BGP speaker IP address for this BGP peer
pingping-linc6b86fa2014-12-01 16:18:10 -0800262 bgpSpeakerIpAddress = ""
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800263 for interfaceAddress in \
264 sdnipData[ 'bgpSpeakers' ][ 0 ][ 'interfaceAddresses' ]:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800265 # if eq( interfaceAddress[ 'interfaceDpid' ],sdnipData[
kelvin-onlabbbe46482015-01-16 10:44:28 -0800266 # 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] ) and eq(
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800267 # interfaceAddress[ 'interfacePort' ], sdnipData[ 'bgpSpeakers'
268 # ][ 0 ][ 'attachmentPort' ] ):
269 if eq( interfaceAddress[ 'interfaceDpid' ],
270 peer[ 'attachmentDpid' ] ) \
271 and eq( interfaceAddress[ 'interfacePort' ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800272 peer[ 'attachmentPort' ] ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800273 bgpSpeakerIpAddress = interfaceAddress[ 'ipAddress' ]
pingping-lin6f6332e2014-11-19 19:13:58 -0800274 break
275 else:
276 continue
277
kelvin-onlabbbe46482015-01-16 10:44:28 -0800278 # from bgpSpeakerAttachmentPoint to bgpPeerAttachmentPoint
279 # direction
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800280 selectorStr = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," \
281 + "IPV4_DST{ip=" + peer[ 'ipAddress' ] + "/32}," \
282 + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \
283 TCP_DST{tcpPort=179}"
284 selector = selectorStr.replace( " ", "" ).replace("[", "" )\
285 .replace( "]", "" ).split( "," )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800286 intent = bgpSpeakerAttachmentPoint + "/" + \
287 bgpPeerAttachmentPoint + "/" + str( sorted( selector ) )
288 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800289
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800290 selectorStr = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," \
291 + "IPV4_DST{ip=" + peer[ 'ipAddress' ] + "/32}," \
292 + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \
293 TCP_SRC{tcpPort=179}"
294 selector = selectorStr.replace( " ", "" ).replace("[", "" )\
295 .replace( "]", "" ).split( "," )
296 intent = bgpSpeakerAttachmentPoint + "/" \
297 + bgpPeerAttachmentPoint + "/" + str( sorted( selector ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800298 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800299
kelvin-onlabbbe46482015-01-16 10:44:28 -0800300 # from bgpPeerAttachmentPoint to bgpSpeakerAttachmentPoint
301 # direction
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800302 selectorStr = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," \
303 + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," \
304 + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \
305 TCP_DST{tcpPort=179}"
306 selector = selectorStr.replace( " ", "" ).replace("[", "" )\
307 .replace( "]", "" ).split( "," )
308 intent = bgpPeerAttachmentPoint + "/" \
309 + bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800310 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800311
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800312 selectorStr = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," \
313 + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," \
314 + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, \
315 TCP_SRC{tcpPort=179}"
316 selector = selectorStr.replace( " ", "" ).replace( "[", "" )\
317 .replace( "]", "" ).split( "," )
318 intent = bgpPeerAttachmentPoint + "/" \
319 + bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800320 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800321
kelvin-onlabbbe46482015-01-16 10:44:28 -0800322 return sorted( intents )
pingping-linc6b86fa2014-12-01 16:18:10 -0800323
kelvin-onlabd3b64892015-01-20 13:26:24 -0800324 def addRoutes( self, routes, routeRate ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800325 main.log.info( "I am in add_routes method!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800326
kelvin-onlabd3b64892015-01-20 13:26:24 -0800327 routesAdded = 0
pingping-lin8b306ac2014-11-17 18:13:51 -0800328 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800329 self.handle.sendline( "" )
330 # self.handle.expect( "config-router" )
331 self.handle.expect( "config-router", timeout=5 )
Jon Hallfebb1c72015-03-05 13:30:09 -0800332 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800333 main.log.warn( "Probably not in config-router mode!" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800334 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800335 main.log.info( "Start to add routes" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800336
kelvin-onlabbbe46482015-01-16 10:44:28 -0800337 for i in range( 0, len( routes ) ):
338 routeCmd = "network " + routes[ i ]
pingping-lin8b306ac2014-11-17 18:13:51 -0800339 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800340 self.handle.sendline( routeCmd )
341 self.handle.expect( "bgpd", timeout=5 )
Jon Hallfebb1c72015-03-05 13:30:09 -0800342 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800343 main.log.warn( "Failed to add route" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800344 self.disconnect()
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800345 # waitTimer = 1.00 / routeRate
346 # time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800347 if routesAdded == len( routes ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800348 main.log.info( "Finished adding routes" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800349 return main.TRUE
350 return main.FALSE
pingping-linc6b86fa2014-12-01 16:18:10 -0800351
kelvin-onlabd3b64892015-01-20 13:26:24 -0800352 def deleteRoutes( self, routes, routeRate ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800353 main.log.info( "I am in delete_routes method!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800354
kelvin-onlabd3b64892015-01-20 13:26:24 -0800355 routesAdded = 0
pingping-linc6b86fa2014-12-01 16:18:10 -0800356 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800357 self.handle.sendline( "" )
358 # self.handle.expect( "config-router" )
359 self.handle.expect( "config-router", timeout=5 )
Jon Hallfebb1c72015-03-05 13:30:09 -0800360 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800361 main.log.warn( "Probably not in config-router mode!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800362 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800363 main.log.info( "Start to delete routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800364
kelvin-onlabbbe46482015-01-16 10:44:28 -0800365 for i in range( 0, len( routes ) ):
366 routeCmd = "no network " + routes[ i ]
pingping-linc6b86fa2014-12-01 16:18:10 -0800367 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800368 self.handle.sendline( routeCmd )
369 self.handle.expect( "bgpd", timeout=5 )
Jon Hallfebb1c72015-03-05 13:30:09 -0800370 except Exception:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800371 main.log.warn( "Failed to delete route" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800372 self.disconnect()
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800373 # waitTimer = 1.00 / routeRate
374 # time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800375 if routesAdded == len( routes ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800376 main.log.info( "Finished deleting routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800377 return main.TRUE
378 return main.FALSE
379
kelvin-onlab08679eb2015-01-21 16:11:48 -0800380 def pingTest( self, ip_address, pingTestFile, pingTestResultFile ):
381 main.log.info( "Start the ping test on host:" + str( ip_address ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800382
kelvin-onlabbbe46482015-01-16 10:44:28 -0800383 self.name = self.options[ 'name' ]
384 self.handle = super( QuaggaCliDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -0800385 user_name=self.user_name, ip_address=ip_address,
kelvin-onlabbbe46482015-01-16 10:44:28 -0800386 port=self.port, pwd=self.pwd )
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800387 main.log.info( "connect parameters:" + str( self.user_name ) + ";"
388 + str( self.ip_address ) + ";" + str( self.port )
389 + ";" + str( self.pwd ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800390
391 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800392 # self.handle.expect( "" )
393 # self.handle.expect( "\$" )
kelvin-onlab08679eb2015-01-21 16:11:48 -0800394 main.log.info( "I in host " + str( ip_address ) )
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800395 main.log.info( pingTestFile + " > " + pingTestResultFile + " &" )
kelvin-onlabbbe46482015-01-16 10:44:28 -0800396 self.handle.sendline(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800397 pingTestFile +
kelvin-onlabbbe46482015-01-16 10:44:28 -0800398 " > " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800399 pingTestResultFile +
kelvin-onlabbbe46482015-01-16 10:44:28 -0800400 " &" )
401 self.handle.expect( "\$", timeout=60 )
pingping-linc6b86fa2014-12-01 16:18:10 -0800402 handle = self.handle.before
403
404 return handle
405 else:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800406 main.log.info( "NO HANDLE" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800407 return main.FALSE
408
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800409 # Please use the generateRoutes plus addRoutes instead of this one!
kelvin-onlabd3b64892015-01-20 13:26:24 -0800410 def addRoute( self, net, numRoutes, routeRate ):
timlindbergef8d55d2013-09-27 12:50:13 -0700411 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800412 self.handle.sendline( "" )
413 self.handle.expect( "config-router" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800414 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800415 main.log.warn( "Probably not in config-router mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -0700416 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800417 main.log.info( "Adding Routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800418 j = 0
419 k = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700420 while numRoutes > 255:
421 numRoutes = numRoutes - 255
422 j = j + 1
423 k = numRoutes % 254
kelvin-onlabd3b64892015-01-20 13:26:24 -0800424 routesAdded = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700425 if numRoutes > 255:
426 numRoutes = 255
kelvin-onlabbbe46482015-01-16 10:44:28 -0800427 for m in range( 1, j + 1 ):
428 for n in range( 1, numRoutes + 1 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800429 network = str( net ) + "." + str( m ) + "." + str( n ) \
430 + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700431 routeCmd = "network " + network
432 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800433 self.handle.sendline( routeCmd )
434 self.handle.expect( "bgpd" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800435 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800436 main.log.warn( "failed to add route" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800437 self.disconnect()
438 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800439 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800440 routesAdded = routesAdded + 1
kelvin-onlabbbe46482015-01-16 10:44:28 -0800441 for d in range( j + 1, j + 2 ):
442 for e in range( 1, k + 1 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800443 network = str( net ) + "." + str( d ) + "." + str( e ) \
444 + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700445 routeCmd = "network " + network
446 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800447 self.handle.sendline( routeCmd )
448 self.handle.expect( "bgpd" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800449 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800450 main.log.warn( "failed to add route" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800451 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800452 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800453 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800454 routesAdded = routesAdded + 1
455 if routesAdded == numRoutes:
timlindbergef8d55d2013-09-27 12:50:13 -0700456 return main.TRUE
457 return main.FALSE
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800458
459 # Please use deleteRoutes method instead of this one!
kelvin-onlabd3b64892015-01-20 13:26:24 -0800460 def delRoute( self, net, numRoutes, routeRate ):
timlindbergef8d55d2013-09-27 12:50:13 -0700461 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800462 self.handle.sendline( "" )
463 self.handle.expect( "config-router" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800464 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800465 main.log.warn( "Probably not in config-router mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -0700466 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800467 main.log.info( "Deleting Routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800468 j = 0
469 k = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700470 while numRoutes > 255:
471 numRoutes = numRoutes - 255
472 j = j + 1
473 k = numRoutes % 254
kelvin-onlabd3b64892015-01-20 13:26:24 -0800474 routesDeleted = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700475 if numRoutes > 255:
476 numRoutes = 255
kelvin-onlabbbe46482015-01-16 10:44:28 -0800477 for m in range( 1, j + 1 ):
478 for n in range( 1, numRoutes + 1 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800479 network = str( net ) + "." + str( m ) + "." + str( n ) \
480 + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700481 routeCmd = "no network " + network
482 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800483 self.handle.sendline( routeCmd )
484 self.handle.expect( "bgpd" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800485 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800486 main.log.warn( "Failed to delete route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700487 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800488 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800489 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800490 routesDeleted = routesDeleted + 1
kelvin-onlabbbe46482015-01-16 10:44:28 -0800491 for d in range( j + 1, j + 2 ):
492 for e in range( 1, k + 1 ):
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800493 network = str( net ) + "." + str( d ) + "." + str( e ) \
494 + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700495 routeCmd = "no network " + network
496 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800497 self.handle.sendline( routeCmd )
498 self.handle.expect( "bgpd" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800499 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800500 main.log.warn( "Failed to delete route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700501 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800502 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800503 time.sleep( waitTimer )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800504 routesDeleted = routesDeleted + 1
505 if routesDeleted == numRoutes:
timlindbergef8d55d2013-09-27 12:50:13 -0700506 return main.TRUE
507 return main.FALSE
pingping-linc6b86fa2014-12-01 16:18:10 -0800508
kelvin-onlabd3b64892015-01-20 13:26:24 -0800509 def checkRoutes( self, brand, ip, user, pw ):
kelvin-onlabbbe46482015-01-16 10:44:28 -0800510 def pronto( ip, user, passwd ):
timlindbergef8d55d2013-09-27 12:50:13 -0700511 print "Connecting to Pronto switch"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800512 child = pexpect.spawn( "telnet " + ip )
513 i = child.expect( [ "login:", "CLI#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700514 if i == 0:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800515 print "user_name and password required. Passing login info."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800516 child.sendline( user )
517 child.expect( "Password:" )
518 child.sendline( passwd )
519 child.expect( "CLI#" )
timlindbergef8d55d2013-09-27 12:50:13 -0700520 print "Logged in, getting flowtable."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800521 child.sendline( "flowtable brief" )
522 for t in range( 9 ):
timlindbergef8d55d2013-09-27 12:50:13 -0700523 t2 = 9 - t
kelvin-onlabbbe46482015-01-16 10:44:28 -0800524 print "\r" + str( t2 )
525 sys.stdout.write( "\033[F" )
526 time.sleep( 1 )
timlindbergef8d55d2013-09-27 12:50:13 -0700527 print "Scanning flowtable"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800528 child.expect( "Flow table show" )
timlindbergef8d55d2013-09-27 12:50:13 -0700529 count = 0
kelvin-onlabbbe46482015-01-16 10:44:28 -0800530 while True:
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800531 i = child.expect( [ '17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}',
532 'CLI#', pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700533 if i == 0:
534 count = count + 1
535 elif i == 1:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800536 print "Pronto flows: " + str( count ) + "\nDone\n"
timlindbergef8d55d2013-09-27 12:50:13 -0700537 break
538 else:
539 break
kelvin-onlabbbe46482015-01-16 10:44:28 -0800540
541 def cisco( ip, user, passwd ):
timlindbergef8d55d2013-09-27 12:50:13 -0700542 print "Establishing Cisco switch connection"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800543 child = pexpect.spawn( "ssh " + user + "@" + ip )
544 i = child.expect( [ "Password:", "CLI#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700545 if i == 0:
546 print "Password required. Passing now."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800547 child.sendline( passwd )
548 child.expect( "#" )
timlindbergef8d55d2013-09-27 12:50:13 -0700549 print "Logged in. Retrieving flow table then counting flows."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800550 child.sendline( "show openflow switch all flows all" )
551 child.expect( "Logical Openflow Switch" )
timlindbergef8d55d2013-09-27 12:50:13 -0700552 print "Flow table retrieved. Counting flows"
553 count = 0
kelvin-onlabbbe46482015-01-16 10:44:28 -0800554 while True:
555 i = child.expect( [ "nw_src=17", "#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700556 if i == 0:
557 count = count + 1
558 elif i == 1:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800559 print "Cisco flows: " + str( count ) + "\nDone\n"
timlindbergef8d55d2013-09-27 12:50:13 -0700560 break
561 else:
562 break
563 if brand == "pronto" or brand == "PRONTO":
kelvin-onlabbbe46482015-01-16 10:44:28 -0800564 pronto( ip, user, passwd )
pingping-linc6b86fa2014-12-01 16:18:10 -0800565 # elif brand == "cisco" or brand == "CISCO":
kelvin-onlabbbe46482015-01-16 10:44:28 -0800566 # cisco( ip,user,passwd )
567
568 def disconnect( self ):
569 """
570 Called when Test is complete to disconnect the Quagga handle.
571 """
timlindbergef8d55d2013-09-27 12:50:13 -0700572 response = ''
573 try:
574 self.handle.close()
Jon Hallfebb1c72015-03-05 13:30:09 -0800575 except Exception:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800576 main.log.error( "Connection failed to the host" )
timlindbergef8d55d2013-09-27 12:50:13 -0700577 response = main.FALSE
578 return response
kelvin-onlab2c4342a2015-01-28 15:59:53 -0800579