blob: 28f95afdfdb28f185bc401378a616532793b7e8e [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(
29 # user_name=self.user_name, ip_address=self.ip_address,port=self.port,
30 # pwd=self.pwd )
31 self.handle = super(
32 QuaggaCliDriver,
33 self ).connect(
34 user_name=self.user_name,
35 ip_address="1.1.1.1",
36 port=self.port,
37 pwd=self.pwd )
38 main.log.info(
39 "connect parameters:" + str(
40 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
kelvin-onlabbbe46482015-01-16 10:44:28 -080062 def loginQuagga( self, ip_address ):
63 self.name = self.options[ 'name' ]
64 self.handle = super( QuaggaCliDriver, self ).connect(
pingping-lin8b306ac2014-11-17 18:13:51 -080065 user_name=self.user_name, ip_address=ip_address,
kelvin-onlabbbe46482015-01-16 10:44:28 -080066 port=self.port, pwd=self.pwd )
67 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#" )
82 main.log.info( "I 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-onlabbbe46482015-01-16 10:44:28 -080089 def enter_config( self, asn ):
90 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-onlabbbe46482015-01-16 10:44:28 -0800108 def generate_prefixes( self, net, numRoutes ):
109 main.log.info( "I am in generate_prefixes method!" )
pingping-lin6f6332e2014-11-19 19:13:58 -0800110
111 # 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 = []
114 routes_gen = 0
115 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 )
pingping-lin8b306ac2014-11-17 18:13:51 -0800125 routes_gen = routes_gen + 1
126
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 )
pingping-lin8b306ac2014-11-17 18:13:51 -0800130 routes_gen = routes_gen + 1
131
132 if routes_gen == 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
140 def generate_expected_singleRouteIntent( self, prefix, nextHop, nextHopMac, sdnip_data ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800141
142 ingress = []
143 egress = ""
kelvin-onlabbbe46482015-01-16 10:44:28 -0800144 for peer in sdnip_data[ 'bgpPeers' ]:
145 if peer[ 'ipAddress' ] == nextHop:
146 egress = "of:" + str( peer[ 'attachmentDpid' ] ).replace( ":", "" ) + ":" + str( peer[ 'attachmentPort' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800147 else:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800148 ingress.append( "of:" + \
149 str( peer[ 'attachmentDpid' ] ).replace( ":",
150 "" ) + ":" + str( peer[ 'attachmentPort' ] ) )
pingping-lin6f6332e2014-11-19 19:13:58 -0800151
pingping-linc6b86fa2014-12-01 16:18:10 -0800152 selector = "ETH_TYPE{ethType=800},IPV4_DST{ip=" + prefix + "}"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800153 treatment = "[ETH_DST{mac=" + str( nextHopMac ) + "}]"
pingping-lin6f6332e2014-11-19 19:13:58 -0800154
kelvin-onlabbbe46482015-01-16 10:44:28 -0800155 intent = egress + "/" + \
156 str( sorted( ingress ) ) + "/" + selector + "/" + treatment
pingping-lin6f6332e2014-11-19 19:13:58 -0800157 return intent
158
kelvin-onlabbbe46482015-01-16 10:44:28 -0800159 def generate_expected_onePeerRouteIntents( self, prefixes, nextHop, nextHopMac, sdnip_json_file_path ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800160 intents = []
kelvin-onlabbbe46482015-01-16 10:44:28 -0800161 sdnip_json_file = open( sdnip_json_file_path ).read()
pingping-lin6f6332e2014-11-19 19:13:58 -0800162
kelvin-onlabbbe46482015-01-16 10:44:28 -0800163 sdnip_data = json.loads( sdnip_json_file )
pingping-lin6f6332e2014-11-19 19:13:58 -0800164
165 for prefix in prefixes:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800166 intents.append(
167 self.generate_expected_singleRouteIntent(
168 prefix,
169 nextHop,
170 nextHopMac,
171 sdnip_data ) )
172 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800173
174 # TODO
175 # This method generates all expected route intents for all BGP peers
kelvin-onlabbbe46482015-01-16 10:44:28 -0800176 def generate_expected_routeIntents( self ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800177 intents = []
178 return intents
179
180 # This method extracts all actual routes from ONOS CLI
kelvin-onlabbbe46482015-01-16 10:44:28 -0800181 def extract_actual_routes( self, get_routes_result ):
182 routes_json_obj = json.loads( get_routes_result )
pingping-lin6f6332e2014-11-19 19:13:58 -0800183
184 allRoutes_actual = []
185 for route in routes_json_obj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800186 if route[ 'prefix' ] == '172.16.10.0/24':
pingping-lin6f6332e2014-11-19 19:13:58 -0800187 continue
kelvin-onlabbbe46482015-01-16 10:44:28 -0800188 allRoutes_actual.append(
189 route[ 'prefix' ] + "/" + route[ 'nextHop' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800190
kelvin-onlabbbe46482015-01-16 10:44:28 -0800191 return sorted( allRoutes_actual )
pingping-lin6f6332e2014-11-19 19:13:58 -0800192
193 # This method extracts all actual route intents from ONOS CLI
kelvin-onlabbbe46482015-01-16 10:44:28 -0800194 def extract_actual_routeIntents( self, get_intents_result ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800195 intents = []
196 # TODO: delete the line below when change to Mininet demo script
kelvin-onlabbbe46482015-01-16 10:44:28 -0800197 # get_intents_result=open( "../tests/SdnIpTest/intents.json" ).read()
198 intents_json_obj = json.loads( get_intents_result )
pingping-lin6f6332e2014-11-19 19:13:58 -0800199
200 for intent in intents_json_obj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800201 if intent[ 'appId' ] != "org.onosproject.sdnip":
pingping-lin6f6332e2014-11-19 19:13:58 -0800202 continue
kelvin-onlabbbe46482015-01-16 10:44:28 -0800203 if intent[ 'type' ] == "MultiPointToSinglePointIntent" and intent[ 'state' ] == 'INSTALLED':
204 egress = str( intent[ 'egress' ][ 'device' ] ) + ":" + str(
205 intent[ 'egress' ][ 'port' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800206 ingress = []
kelvin-onlabbbe46482015-01-16 10:44:28 -0800207 for attachmentPoint in intent[ 'ingress' ]:
208 ingress.append(
209 str( attachmentPoint[ 'device' ] ) + ":" + str( attachmentPoint[ 'port' ] ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800210
kelvin-onlabbbe46482015-01-16 10:44:28 -0800211 selector = intent[ 'selector' ].replace(
212 "[", "" ).replace( "]", "" ).replace( " ", "" )
213 if str( selector ).startswith( "IPV4" ):
214 str1, str2 = str( selector ).split( "," )
pingping-linc6b86fa2014-12-01 16:18:10 -0800215 selector = str2 + "," + str1
216
kelvin-onlabbbe46482015-01-16 10:44:28 -0800217 intent = egress + "/" + \
218 str( sorted( ingress ) ) + "/" + \
219 selector + "/" + intent[ 'treatment' ]
220 intents.append( intent )
221 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800222
223 # This method extracts all actual BGP intents from ONOS CLI
kelvin-onlabbbe46482015-01-16 10:44:28 -0800224 def extract_actual_bgpIntents( self, get_intents_result ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800225 intents = []
226 # TODO: delete the line below when change to Mininet demo script
kelvin-onlabbbe46482015-01-16 10:44:28 -0800227 # get_intents_result=open( "../tests/SdnIpTest/intents.json" ).read()
228 intents_json_obj = json.loads( get_intents_result )
pingping-lin6f6332e2014-11-19 19:13:58 -0800229
230 for intent in intents_json_obj:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800231 if intent[ 'appId' ] != "org.onosproject.sdnip":
pingping-lin6f6332e2014-11-19 19:13:58 -0800232 continue
kelvin-onlabbbe46482015-01-16 10:44:28 -0800233 if intent[ 'type' ] == "PointToPointIntent" and "protocol=6" in str( intent[ 'selector' ] ):
234 ingress = str( intent[ 'ingress' ][ 'device' ] ) + ":" + str(
235 intent[ 'ingress' ][ 'port' ] )
236 egress = str( intent[ 'egress' ][ 'device' ] ) + ":" + str(
237 intent[ 'egress' ][ 'port' ] )
238 selector = str(
239 intent[ 'selector' ] ).replace( " ", "" ).replace( "[", "" ).replace( "]", "" ).split( "," )
240 intent = ingress + "/" + egress + \
241 "/" + str( sorted( selector ) )
242 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800243
kelvin-onlabbbe46482015-01-16 10:44:28 -0800244 return sorted( intents )
pingping-lin6f6332e2014-11-19 19:13:58 -0800245
kelvin-onlabbbe46482015-01-16 10:44:28 -0800246 # This method generates a single point to single point intent(
247 # PointToPointIntent ) for BGP path
248 def generate_expected_bgpIntents( self, sdnip_json_file_path ):
pingping-lin6f6332e2014-11-19 19:13:58 -0800249 from operator import eq
250
kelvin-onlabbbe46482015-01-16 10:44:28 -0800251 sdnip_json_file = open( sdnip_json_file_path ).read()
252 sdnip_data = json.loads( sdnip_json_file )
pingping-lin6f6332e2014-11-19 19:13:58 -0800253
254 intents = []
pingping-linc6b86fa2014-12-01 16:18:10 -0800255 bgpPeerAttachmentPoint = ""
kelvin-onlabbbe46482015-01-16 10:44:28 -0800256 bgpSpeakerAttachmentPoint = "of:" + str(
257 sdnip_data[ 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] ).replace( ":",
258 "" ) + ":" + str( sdnip_data[ 'bgpSpeakers' ][ 0 ][ 'attachmentPort' ] )
259 for peer in sdnip_data[ 'bgpPeers' ]:
260 bgpPeerAttachmentPoint = "of:" + str(
261 peer[ 'attachmentDpid' ] ).replace( ":", "" ) + ":" + str( peer[ 'attachmentPort' ] )
pingping-lin6f6332e2014-11-19 19:13:58 -0800262 # find out the BGP speaker IP address for this BGP peer
pingping-linc6b86fa2014-12-01 16:18:10 -0800263 bgpSpeakerIpAddress = ""
kelvin-onlabbbe46482015-01-16 10:44:28 -0800264 for interfaceAddress in sdnip_data[ 'bgpSpeakers' ][ 0 ][ 'interfaceAddresses' ]:
265 # if eq( interfaceAddress[ 'interfaceDpid' ],sdnip_data[
266 # 'bgpSpeakers' ][ 0 ][ 'attachmentDpid' ] ) and eq(
267 # interfaceAddress[ 'interfacePort' ], sdnip_data[
268 # 'bgpSpeakers' ][ 0 ][ 'attachmentPort' ] ):
269 if eq( interfaceAddress[ 'interfaceDpid' ], peer[ 'attachmentDpid' ] ) and eq( interfaceAddress[ 'interfacePort' ], peer[ 'attachmentPort' ] ):
270 bgpSpeakerIpAddress = interfaceAddress[ 'ipAddress' ]
pingping-lin6f6332e2014-11-19 19:13:58 -0800271 break
272 else:
273 continue
274
kelvin-onlabbbe46482015-01-16 10:44:28 -0800275 # from bgpSpeakerAttachmentPoint to bgpPeerAttachmentPoint
276 # direction
277 selector_str = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," + "IPV4_DST{ip=" + peer[
278 'ipAddress' ] + "/32}," + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_DST{tcpPort=179}"
279 selector = selector_str.replace( " ", "" ).replace(
280 "[", "" ).replace( "]", "" ).split( "," )
281 intent = bgpSpeakerAttachmentPoint + "/" + \
282 bgpPeerAttachmentPoint + "/" + str( sorted( selector ) )
283 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800284
kelvin-onlabbbe46482015-01-16 10:44:28 -0800285 selector_str = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," + "IPV4_DST{ip=" + peer[
286 'ipAddress' ] + "/32}," + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_SRC{tcpPort=179}"
287 selector = selector_str.replace( " ", "" ).replace(
288 "[", "" ).replace( "]", "" ).split( "," )
289 intent = bgpSpeakerAttachmentPoint + "/" + \
290 bgpPeerAttachmentPoint + "/" + str( sorted( selector ) )
291 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800292
kelvin-onlabbbe46482015-01-16 10:44:28 -0800293 # from bgpPeerAttachmentPoint to bgpSpeakerAttachmentPoint
294 # direction
295 selector_str = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," + \
296 "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_DST{tcpPort=179}"
297 selector = selector_str.replace( " ", "" ).replace(
298 "[", "" ).replace( "]", "" ).split( "," )
299 intent = bgpPeerAttachmentPoint + "/" + \
300 bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) )
301 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800302
kelvin-onlabbbe46482015-01-16 10:44:28 -0800303 selector_str = "IPV4_SRC{ip=" + peer[ 'ipAddress' ] + "/32}," + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," + \
304 "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_SRC{tcpPort=179}"
305 selector = selector_str.replace( " ", "" ).replace(
306 "[", "" ).replace( "]", "" ).split( "," )
307 intent = bgpPeerAttachmentPoint + "/" + \
308 bgpSpeakerAttachmentPoint + "/" + str( sorted( selector ) )
309 intents.append( intent )
pingping-lin6f6332e2014-11-19 19:13:58 -0800310
kelvin-onlabbbe46482015-01-16 10:44:28 -0800311 return sorted( intents )
pingping-linc6b86fa2014-12-01 16:18:10 -0800312
kelvin-onlabbbe46482015-01-16 10:44:28 -0800313 def add_routes( self, routes, routeRate ):
314 main.log.info( "I am in add_routes method!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800315
pingping-lin8b306ac2014-11-17 18:13:51 -0800316 routes_added = 0
317 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800318 self.handle.sendline( "" )
319 # self.handle.expect( "config-router" )
320 self.handle.expect( "config-router", timeout=5 )
pingping-lin8b306ac2014-11-17 18:13:51 -0800321 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800322 main.log.warn( "Probably not in config-router mode!" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800323 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800324 main.log.info( "Start to add routes" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800325
kelvin-onlabbbe46482015-01-16 10:44:28 -0800326 for i in range( 0, len( routes ) ):
327 routeCmd = "network " + routes[ i ]
pingping-lin8b306ac2014-11-17 18:13:51 -0800328 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800329 self.handle.sendline( routeCmd )
330 self.handle.expect( "bgpd", timeout=5 )
pingping-lin8b306ac2014-11-17 18:13:51 -0800331 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800332 main.log.warn( "Failed to add route" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800333 self.disconnect()
334 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800335 time.sleep( waitTimer )
336 if routes_added == len( routes ):
337 main.log.info( "Finished adding routes" )
pingping-lin8b306ac2014-11-17 18:13:51 -0800338 return main.TRUE
339 return main.FALSE
pingping-linc6b86fa2014-12-01 16:18:10 -0800340
kelvin-onlabbbe46482015-01-16 10:44:28 -0800341 def delete_routes( self, routes, routeRate ):
342 main.log.info( "I am in delete_routes method!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800343
344 routes_added = 0
345 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800346 self.handle.sendline( "" )
347 # self.handle.expect( "config-router" )
348 self.handle.expect( "config-router", timeout=5 )
pingping-linc6b86fa2014-12-01 16:18:10 -0800349 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800350 main.log.warn( "Probably not in config-router mode!" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800351 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800352 main.log.info( "Start to delete routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800353
kelvin-onlabbbe46482015-01-16 10:44:28 -0800354 for i in range( 0, len( routes ) ):
355 routeCmd = "no network " + routes[ i ]
pingping-linc6b86fa2014-12-01 16:18:10 -0800356 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800357 self.handle.sendline( routeCmd )
358 self.handle.expect( "bgpd", timeout=5 )
pingping-linc6b86fa2014-12-01 16:18:10 -0800359 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800360 main.log.warn( "Failed to add route" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800361 self.disconnect()
362 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800363 time.sleep( waitTimer )
364 if routes_added == len( routes ):
365 main.log.info( "Finished deleting routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800366 return main.TRUE
367 return main.FALSE
368
kelvin-onlabbbe46482015-01-16 10:44:28 -0800369 def ping_test( self, ip_address, ping_test_file, ping_test_result_file ):
370 main.log.info( "Start the ping test on host:" + str( ip_address ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800371
kelvin-onlabbbe46482015-01-16 10:44:28 -0800372 self.name = self.options[ 'name' ]
373 self.handle = super( QuaggaCliDriver, self ).connect(
pingping-linc6b86fa2014-12-01 16:18:10 -0800374 user_name=self.user_name, ip_address=ip_address,
kelvin-onlabbbe46482015-01-16 10:44:28 -0800375 port=self.port, pwd=self.pwd )
376 main.log.info( "connect parameters:" + str( self.user_name ) + ";"
377 + str( self.ip_address ) + ";" + str( self.port ) + ";" + str( self.pwd ) )
pingping-linc6b86fa2014-12-01 16:18:10 -0800378
379 if self.handle:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800380 # self.handle.expect( "" )
381 # self.handle.expect( "\$" )
382 main.log.info( "I in host " + str( ip_address ) )
383 main.log.info(
384 ping_test_file +
385 " > " +
386 ping_test_result_file +
387 " &" )
388 self.handle.sendline(
389 ping_test_file +
390 " > " +
391 ping_test_result_file +
392 " &" )
393 self.handle.expect( "\$", timeout=60 )
pingping-linc6b86fa2014-12-01 16:18:10 -0800394 handle = self.handle.before
395
396 return handle
397 else:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800398 main.log.info( "NO HANDLE" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800399 return main.FALSE
400
pingping-lin6f6332e2014-11-19 19:13:58 -0800401 # Please use the generate_routes plus add_routes instead of this one
kelvin-onlabbbe46482015-01-16 10:44:28 -0800402 def add_route( self, net, numRoutes, routeRate ):
timlindbergef8d55d2013-09-27 12:50:13 -0700403 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800404 self.handle.sendline( "" )
405 self.handle.expect( "config-router" )
timlindbergef8d55d2013-09-27 12:50:13 -0700406 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800407 main.log.warn( "Probably not in config-router mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -0700408 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800409 main.log.info( "Adding Routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800410 j = 0
411 k = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700412 while numRoutes > 255:
413 numRoutes = numRoutes - 255
414 j = j + 1
415 k = numRoutes % 254
416 routes_added = 0
417 if numRoutes > 255:
418 numRoutes = 255
kelvin-onlabbbe46482015-01-16 10:44:28 -0800419 for m in range( 1, j + 1 ):
420 for n in range( 1, numRoutes + 1 ):
421 network = str( net ) + "." + str( m ) + "." + str( n ) + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700422 routeCmd = "network " + network
423 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800424 self.handle.sendline( routeCmd )
425 self.handle.expect( "bgpd" )
timlindbergef8d55d2013-09-27 12:50:13 -0700426 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800427 main.log.warn( "failed to add route" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800428 self.disconnect()
429 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800430 time.sleep( waitTimer )
timlindbergef8d55d2013-09-27 12:50:13 -0700431 routes_added = routes_added + 1
kelvin-onlabbbe46482015-01-16 10:44:28 -0800432 for d in range( j + 1, j + 2 ):
433 for e in range( 1, k + 1 ):
434 network = str(
435 net ) + "." + str(
436 d ) + "." + str(
437 e ) + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700438 routeCmd = "network " + network
439 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800440 self.handle.sendline( routeCmd )
441 self.handle.expect( "bgpd" )
timlindbergef8d55d2013-09-27 12:50:13 -0700442 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800443 main.log.warn( "failed to add route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700444 self.disconnect
pingping-linc6b86fa2014-12-01 16:18:10 -0800445 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800446 time.sleep( waitTimer )
timlindbergef8d55d2013-09-27 12:50:13 -0700447 routes_added = routes_added + 1
448 if routes_added == numRoutes:
449 return main.TRUE
450 return main.FALSE
pingping-lin6f6332e2014-11-19 19:13:58 -0800451
kelvin-onlabbbe46482015-01-16 10:44:28 -0800452 def del_route( self, net, numRoutes, routeRate ):
timlindbergef8d55d2013-09-27 12:50:13 -0700453 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800454 self.handle.sendline( "" )
455 self.handle.expect( "config-router" )
timlindbergef8d55d2013-09-27 12:50:13 -0700456 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800457 main.log.warn( "Probably not in config-router mode!" )
timlindbergef8d55d2013-09-27 12:50:13 -0700458 self.disconnect()
kelvin-onlabbbe46482015-01-16 10:44:28 -0800459 main.log.info( "Deleting Routes" )
pingping-linc6b86fa2014-12-01 16:18:10 -0800460 j = 0
461 k = 0
timlindbergef8d55d2013-09-27 12:50:13 -0700462 while numRoutes > 255:
463 numRoutes = numRoutes - 255
464 j = j + 1
465 k = numRoutes % 254
466 routes_deleted = 0
467 if numRoutes > 255:
468 numRoutes = 255
kelvin-onlabbbe46482015-01-16 10:44:28 -0800469 for m in range( 1, j + 1 ):
470 for n in range( 1, numRoutes + 1 ):
471 network = str( net ) + "." + str( m ) + "." + str( n ) + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700472 routeCmd = "no network " + network
473 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800474 self.handle.sendline( routeCmd )
475 self.handle.expect( "bgpd" )
timlindbergef8d55d2013-09-27 12:50:13 -0700476 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800477 main.log.warn( "Failed to delete route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700478 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800479 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800480 time.sleep( waitTimer )
timlindbergef8d55d2013-09-27 12:50:13 -0700481 routes_deleted = routes_deleted + 1
kelvin-onlabbbe46482015-01-16 10:44:28 -0800482 for d in range( j + 1, j + 2 ):
483 for e in range( 1, k + 1 ):
484 network = str( net ) + "." + str( d ) + "." + str( e ) + ".0/24"
timlindbergef8d55d2013-09-27 12:50:13 -0700485 routeCmd = "no network " + network
486 try:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800487 self.handle.sendline( routeCmd )
488 self.handle.expect( "bgpd" )
timlindbergef8d55d2013-09-27 12:50:13 -0700489 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800490 main.log.warn( "Failed to delete route" )
timlindbergef8d55d2013-09-27 12:50:13 -0700491 self.disconnect()
pingping-linc6b86fa2014-12-01 16:18:10 -0800492 waitTimer = 1.00 / routeRate
kelvin-onlabbbe46482015-01-16 10:44:28 -0800493 time.sleep( waitTimer )
timlindbergef8d55d2013-09-27 12:50:13 -0700494 routes_deleted = routes_deleted + 1
495 if routes_deleted == numRoutes:
496 return main.TRUE
497 return main.FALSE
pingping-linc6b86fa2014-12-01 16:18:10 -0800498
kelvin-onlabbbe46482015-01-16 10:44:28 -0800499 def check_routes( self, brand, ip, user, pw ):
500 def pronto( ip, user, passwd ):
timlindbergef8d55d2013-09-27 12:50:13 -0700501 print "Connecting to Pronto switch"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800502 child = pexpect.spawn( "telnet " + ip )
503 i = child.expect( [ "login:", "CLI#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700504 if i == 0:
505 print "Username and password required. Passing login info."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800506 child.sendline( user )
507 child.expect( "Password:" )
508 child.sendline( passwd )
509 child.expect( "CLI#" )
timlindbergef8d55d2013-09-27 12:50:13 -0700510 print "Logged in, getting flowtable."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800511 child.sendline( "flowtable brief" )
512 for t in range( 9 ):
timlindbergef8d55d2013-09-27 12:50:13 -0700513 t2 = 9 - t
kelvin-onlabbbe46482015-01-16 10:44:28 -0800514 print "\r" + str( t2 )
515 sys.stdout.write( "\033[F" )
516 time.sleep( 1 )
timlindbergef8d55d2013-09-27 12:50:13 -0700517 print "Scanning flowtable"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800518 child.expect( "Flow table show" )
timlindbergef8d55d2013-09-27 12:50:13 -0700519 count = 0
kelvin-onlabbbe46482015-01-16 10:44:28 -0800520 while True:
521 i = child.expect(
522 [ '17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}',
523 'CLI#',
524 pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700525 if i == 0:
526 count = count + 1
527 elif i == 1:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800528 print "Pronto flows: " + str( count ) + "\nDone\n"
timlindbergef8d55d2013-09-27 12:50:13 -0700529 break
530 else:
531 break
kelvin-onlabbbe46482015-01-16 10:44:28 -0800532
533 def cisco( ip, user, passwd ):
timlindbergef8d55d2013-09-27 12:50:13 -0700534 print "Establishing Cisco switch connection"
kelvin-onlabbbe46482015-01-16 10:44:28 -0800535 child = pexpect.spawn( "ssh " + user + "@" + ip )
536 i = child.expect( [ "Password:", "CLI#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700537 if i == 0:
538 print "Password required. Passing now."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800539 child.sendline( passwd )
540 child.expect( "#" )
timlindbergef8d55d2013-09-27 12:50:13 -0700541 print "Logged in. Retrieving flow table then counting flows."
kelvin-onlabbbe46482015-01-16 10:44:28 -0800542 child.sendline( "show openflow switch all flows all" )
543 child.expect( "Logical Openflow Switch" )
timlindbergef8d55d2013-09-27 12:50:13 -0700544 print "Flow table retrieved. Counting flows"
545 count = 0
kelvin-onlabbbe46482015-01-16 10:44:28 -0800546 while True:
547 i = child.expect( [ "nw_src=17", "#", pexpect.TIMEOUT ] )
timlindbergef8d55d2013-09-27 12:50:13 -0700548 if i == 0:
549 count = count + 1
550 elif i == 1:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800551 print "Cisco flows: " + str( count ) + "\nDone\n"
timlindbergef8d55d2013-09-27 12:50:13 -0700552 break
553 else:
554 break
555 if brand == "pronto" or brand == "PRONTO":
kelvin-onlabbbe46482015-01-16 10:44:28 -0800556 pronto( ip, user, passwd )
pingping-linc6b86fa2014-12-01 16:18:10 -0800557 # elif brand == "cisco" or brand == "CISCO":
kelvin-onlabbbe46482015-01-16 10:44:28 -0800558 # cisco( ip,user,passwd )
559
560 def disconnect( self ):
561 """
562 Called when Test is complete to disconnect the Quagga handle.
563 """
timlindbergef8d55d2013-09-27 12:50:13 -0700564 response = ''
565 try:
566 self.handle.close()
567 except:
kelvin-onlabbbe46482015-01-16 10:44:28 -0800568 main.log.error( "Connection failed to the host" )
timlindbergef8d55d2013-09-27 12:50:13 -0700569 response = main.FALSE
570 return response
kelvin-onlabbbe46482015-01-16 10:44:28 -0800571