blob: 7851d138e58b87f11f9f1e51de9df5130d46e4bf [file] [log] [blame]
timlindbergef8d55d2013-09-27 12:50:13 -07001#!/usr/bin/env python
2
3import time
4import pexpect
5import struct, fcntl, os, sys, signal
6import sys
7import re
8import json
9sys.path.append("../")
10from drivers.common.clidriver import CLI
11
12class QuaggaCliDriver(CLI):
13
14 def __init__(self):
15 super(CLI, self).__init__()
16
pingping-lin8b306ac2014-11-17 18:13:51 -080017 #TODO: simplify this method
timlindbergef8d55d2013-09-27 12:50:13 -070018 def connect(self, **connectargs):
19 for key in connectargs:
20 vars(self)[key] = connectargs[key]
21
22 self.name = self.options['name']
pingping-lin8b306ac2014-11-17 18:13:51 -080023 #self.handle = super(QuaggaCliDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd)
24 self.handle = super(QuaggaCliDriver, self).connect(user_name=self.user_name, ip_address="1.1.1.1", port=self.port, pwd=self.pwd)
25 main.log.info("connect parameters:" + str(self.user_name) + ";" + str(self.ip_address) + ";" + str(self.port) + ";" + str(self.pwd))
timlindbergef8d55d2013-09-27 12:50:13 -070026
27 if self.handle:
28 self.handle.expect("")
29 self.handle.expect("\$")
30 self.handle.sendline("telnet localhost 2605")
31 self.handle.expect("Password:", timeout = 5)
32 self.handle.sendline("hello")
33 self.handle.expect("bgpd",timeout = 5)
34 self.handle.sendline("enable")
35 self.handle.expect("bgpd#", timeout = 5)
36 return self.handle
37 else :
38 main.log.info("NO HANDLE")
39 return main.FALSE
40
pingping-lin8b306ac2014-11-17 18:13:51 -080041 def loginQuagga(self, ip_address):
42
43 self.name = self.options['name']
44 self.handle = super(QuaggaCliDriver, self).connect(
45 user_name=self.user_name, ip_address=ip_address,
46 port=self.port, pwd=self.pwd)
47 main.log.info("connect parameters:" + str(self.user_name) + ";"
48 + str(self.ip_address) + ";" + str(self.port) + ";" + str(self.pwd))
49
50 if self.handle:
51 self.handle.expect("")
52 self.handle.expect("\$")
53 self.handle.sendline("telnet localhost 2605")
54 self.handle.expect("Password:", timeout=5)
55 self.handle.sendline("hello")
56 self.handle.expect("bgpd", timeout=5)
57 self.handle.sendline("enable")
58 self.handle.expect("bgpd#", timeout=5)
59 main.log.info("I am inside BGP peer Quagga!")
60
61 return self.handle
62 else:
63 main.log.info("NO HANDLE")
64 return main.FALSE
65
timlindbergef8d55d2013-09-27 12:50:13 -070066 def enter_config(self, asn):
pingping-lin8b306ac2014-11-17 18:13:51 -080067 main.log.info("I am in enter_config method!")
timlindbergef8d55d2013-09-27 12:50:13 -070068 try:
69 self.handle.sendline("")
70 self.handle.expect("bgpd#")
71 except:
72 main.log.warn("Probably not currently in enable mode!")
73 self.disconnect()
74 return main.FALSE
75 self.handle.sendline("configure terminal")
76 self.handle.expect("config", timeout = 5)
77 routerAS = "router bgp " + str(asn)
78 try:
79 self.handle.sendline(routerAS)
80 self.handle.expect("config-router", timeout = 5)
81 return main.TRUE
82 except:
83 return main.FALSE
pingping-lin8b306ac2014-11-17 18:13:51 -080084
85 def generate_routes(self, net, numRoutes):
86 main.log.info("I am in generate_routes method!")
87
88 # a IP prefix will be composed by "net" + "." + m + "." + n + "." + x
89 # the length of each IP prefix is 24
90 routes = []
91 routes_gen = 0
92 m = numRoutes / 256
93 n = numRoutes % 256
94
95 for i in range(0, m):
96 for j in range(0, 256):
97 network = str(net) + "." + str(i) + "." + str(j) + ".0/24"
98 routes.append(network)
99 routes_gen = routes_gen + 1
100
101 for j in range(0, n):
102 network = str(net) + "." + str(m) + "." + str(j) + ".0/24"
103 routes.append(network)
104 routes_gen = routes_gen + 1
105
106 if routes_gen == numRoutes:
107 main.log.info("Successfully generated " + str(numRoutes)
108 + " routes!")
109 return routes
110 return main.FALSE
111
112 def add_routes(self, routes, routeRate):
113 main.log.info("I am in add_routes method!")
114
115 routes_added = 0
116 try:
117 self.handle.sendline("")
118 #self.handle.expect("config-router")
119 self.handle.expect("config-router", timeout=5)
120 except:
121 main.log.warn("Probably not in config-router mode!")
122 self.disconnect()
123 main.log.info("Start to add routes")
124
125 for i in range(0, len(routes)):
126 routeCmd = "network " + routes[i]
127 try:
128 self.handle.sendline(routeCmd)
129 self.handle.expect("bgpd", timeout=5)
130 except:
131 main.log.warn("Failed to add route")
132 self.disconnect()
133 waitTimer = 1.00 / routeRate
134 time.sleep(waitTimer)
135 if routes_added == len(routes):
136 main.log.info("Finished adding routes")
137 return main.TRUE
138 return main.FALSE
139
140 # please use the generate_routes plus add_routes instead of this one
timlindbergef8d55d2013-09-27 12:50:13 -0700141 def add_route(self, net, numRoutes, routeRate):
142 try:
143 self.handle.sendline("")
144 self.handle.expect("config-router")
145 except:
146 main.log.warn("Probably not in config-router mode!")
147 self.disconnect()
148 main.log.report("Adding Routes")
149 j=0
150 k=0
151 while numRoutes > 255:
152 numRoutes = numRoutes - 255
153 j = j + 1
154 k = numRoutes % 254
155 routes_added = 0
156 if numRoutes > 255:
157 numRoutes = 255
158 for m in range(1,j+1):
159 for n in range(1, numRoutes+1):
160 network = str(net) + "." + str(m) + "." + str(n) + ".0/24"
161 routeCmd = "network " + network
162 try:
163 self.handle.sendline(routeCmd)
164 self.handle.expect("bgpd")
165 except:
166 main.log.warn("failed to add route")
167 self.disconnect()
168 waitTimer = 1.00/routeRate
169 time.sleep(waitTimer)
170 routes_added = routes_added + 1
171 for d in range(j+1,j+2):
172 for e in range(1,k+1):
173 network = str(net) + "." + str(d) + "." + str(e) + ".0/24"
174 routeCmd = "network " + network
175 try:
176 self.handle.sendline(routeCmd)
177 self.handle.expect("bgpd")
178 except:
179 main.log.warn("failed to add route")
180 self.disconnect
181 waitTimer = 1.00/routeRate
182 time.sleep(waitTimer)
183 routes_added = routes_added + 1
184 if routes_added == numRoutes:
185 return main.TRUE
186 return main.FALSE
187 def del_route(self, net, numRoutes, routeRate):
188 try:
189 self.handle.sendline("")
190 self.handle.expect("config-router")
191 except:
192 main.log.warn("Probably not in config-router mode!")
193 self.disconnect()
194 main.log.report("Deleting Routes")
195 j=0
196 k=0
197 while numRoutes > 255:
198 numRoutes = numRoutes - 255
199 j = j + 1
200 k = numRoutes % 254
201 routes_deleted = 0
202 if numRoutes > 255:
203 numRoutes = 255
204 for m in range(1,j+1):
205 for n in range(1, numRoutes+1):
206 network = str(net) + "." + str(m) + "." + str(n) + ".0/24"
207 routeCmd = "no network " + network
208 try:
209 self.handle.sendline(routeCmd)
210 self.handle.expect("bgpd")
211 except:
212 main.log.warn("Failed to delete route")
213 self.disconnect()
214 waitTimer = 1.00/routeRate
215 time.sleep(waitTimer)
216 routes_deleted = routes_deleted + 1
217 for d in range(j+1,j+2):
218 for e in range(1,k+1):
219 network = str(net) + "." + str(d) + "." + str(e) + ".0/24"
220 routeCmd = "no network " + network
221 try:
222 self.handle.sendline(routeCmd)
223 self.handle.expect("bgpd")
224 except:
225 main.log.warn("Failed to delete route")
226 self.disconnect()
227 waitTimer = 1.00/routeRate
228 time.sleep(waitTimer)
229 routes_deleted = routes_deleted + 1
230 if routes_deleted == numRoutes:
231 return main.TRUE
232 return main.FALSE
233 def check_routes(self, brand, ip, user, pw):
234 def pronto(ip, user, passwd):
235 print "Connecting to Pronto switch"
236 child = pexpect.spawn("telnet " + ip)
237 i = child.expect(["login:", "CLI#",pexpect.TIMEOUT])
238 if i == 0:
239 print "Username and password required. Passing login info."
240 child.sendline(user)
241 child.expect("Password:")
242 child.sendline(passwd)
243 child.expect("CLI#")
244 print "Logged in, getting flowtable."
245 child.sendline("flowtable brief")
246 for t in range (9):
247 t2 = 9 - t
248 print "\r" + str(t2)
249 sys.stdout.write("\033[F")
250 time.sleep(1)
251 print "Scanning flowtable"
252 child.expect("Flow table show")
253 count = 0
254 while 1:
255 i = child.expect(['17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}','CLI#',pexpect.TIMEOUT])
256 if i == 0:
257 count = count + 1
258 elif i == 1:
259 print "Pronto flows: " + str(count) + "\nDone\n"
260 break
261 else:
262 break
263 def cisco(ip,user,passwd):
264 print "Establishing Cisco switch connection"
265 child = pexpect.spawn("ssh " + user + "@" + ip)
266 i = child.expect(["Password:", "CLI#",pexpect.TIMEOUT])
267 if i == 0:
268 print "Password required. Passing now."
269 child.sendline(passwd)
270 child.expect("#")
271 print "Logged in. Retrieving flow table then counting flows."
272 child.sendline("show openflow switch all flows all")
273 child.expect("Logical Openflow Switch")
274 print "Flow table retrieved. Counting flows"
275 count = 0
276 while 1:
277 i = child.expect(["nw_src=17","#",pexpect.TIMEOUT])
278 if i == 0:
279 count = count + 1
280 elif i == 1:
281 print "Cisco flows: " + str(count) + "\nDone\n"
282 break
283 else:
284 break
285 if brand == "pronto" or brand == "PRONTO":
286 pronto(ip,user,passwd)
287 #elif brand == "cisco" or brand == "CISCO":
288 # cisco(ip,user,passwd)
289 def disconnect(self):
290 '''
291 Called when Test is complete to disconnect the Quagga handle.
292 '''
293 response = ''
294 try:
295 self.handle.close()
296 except:
297 main.log.error("Connection failed to the host")
298 response = main.FALSE
299 return response