blob: 744d284cce179939dddeeb5aae1e8cb4475c0afe [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
17 def connect(self, **connectargs):
18 for key in connectargs:
19 vars(self)[key] = connectargs[key]
20
21 self.name = self.options['name']
22 self.handle = super(QuaggaCliDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd)
23
24 if self.handle:
25 self.handle.expect("")
26 self.handle.expect("\$")
27 self.handle.sendline("telnet localhost 2605")
28 self.handle.expect("Password:", timeout = 5)
29 self.handle.sendline("hello")
30 self.handle.expect("bgpd",timeout = 5)
31 self.handle.sendline("enable")
32 self.handle.expect("bgpd#", timeout = 5)
33 return self.handle
34 else :
35 main.log.info("NO HANDLE")
36 return main.FALSE
37
38 def enter_config(self, asn):
39 try:
40 self.handle.sendline("")
41 self.handle.expect("bgpd#")
42 except:
43 main.log.warn("Probably not currently in enable mode!")
44 self.disconnect()
45 return main.FALSE
46 self.handle.sendline("configure terminal")
47 self.handle.expect("config", timeout = 5)
48 routerAS = "router bgp " + str(asn)
49 try:
50 self.handle.sendline(routerAS)
51 self.handle.expect("config-router", timeout = 5)
52 return main.TRUE
53 except:
54 return main.FALSE
55 def add_route(self, net, numRoutes, routeRate):
56 try:
57 self.handle.sendline("")
58 self.handle.expect("config-router")
59 except:
60 main.log.warn("Probably not in config-router mode!")
61 self.disconnect()
62 main.log.report("Adding Routes")
63 j=0
64 k=0
65 while numRoutes > 255:
66 numRoutes = numRoutes - 255
67 j = j + 1
68 k = numRoutes % 254
69 routes_added = 0
70 if numRoutes > 255:
71 numRoutes = 255
72 for m in range(1,j+1):
73 for n in range(1, numRoutes+1):
74 network = str(net) + "." + str(m) + "." + str(n) + ".0/24"
75 routeCmd = "network " + network
76 try:
77 self.handle.sendline(routeCmd)
78 self.handle.expect("bgpd")
79 except:
80 main.log.warn("failed to add route")
81 self.disconnect()
82 waitTimer = 1.00/routeRate
83 time.sleep(waitTimer)
84 routes_added = routes_added + 1
85 for d in range(j+1,j+2):
86 for e in range(1,k+1):
87 network = str(net) + "." + str(d) + "." + str(e) + ".0/24"
88 routeCmd = "network " + network
89 try:
90 self.handle.sendline(routeCmd)
91 self.handle.expect("bgpd")
92 except:
93 main.log.warn("failed to add route")
94 self.disconnect
95 waitTimer = 1.00/routeRate
96 time.sleep(waitTimer)
97 routes_added = routes_added + 1
98 if routes_added == numRoutes:
99 return main.TRUE
100 return main.FALSE
101 def del_route(self, net, numRoutes, routeRate):
102 try:
103 self.handle.sendline("")
104 self.handle.expect("config-router")
105 except:
106 main.log.warn("Probably not in config-router mode!")
107 self.disconnect()
108 main.log.report("Deleting Routes")
109 j=0
110 k=0
111 while numRoutes > 255:
112 numRoutes = numRoutes - 255
113 j = j + 1
114 k = numRoutes % 254
115 routes_deleted = 0
116 if numRoutes > 255:
117 numRoutes = 255
118 for m in range(1,j+1):
119 for n in range(1, numRoutes+1):
120 network = str(net) + "." + str(m) + "." + str(n) + ".0/24"
121 routeCmd = "no network " + network
122 try:
123 self.handle.sendline(routeCmd)
124 self.handle.expect("bgpd")
125 except:
126 main.log.warn("Failed to delete route")
127 self.disconnect()
128 waitTimer = 1.00/routeRate
129 time.sleep(waitTimer)
130 routes_deleted = routes_deleted + 1
131 for d in range(j+1,j+2):
132 for e in range(1,k+1):
133 network = str(net) + "." + str(d) + "." + str(e) + ".0/24"
134 routeCmd = "no network " + network
135 try:
136 self.handle.sendline(routeCmd)
137 self.handle.expect("bgpd")
138 except:
139 main.log.warn("Failed to delete route")
140 self.disconnect()
141 waitTimer = 1.00/routeRate
142 time.sleep(waitTimer)
143 routes_deleted = routes_deleted + 1
144 if routes_deleted == numRoutes:
145 return main.TRUE
146 return main.FALSE
147 def check_routes(self, brand, ip, user, pw):
148 def pronto(ip, user, passwd):
149 print "Connecting to Pronto switch"
150 child = pexpect.spawn("telnet " + ip)
151 i = child.expect(["login:", "CLI#",pexpect.TIMEOUT])
152 if i == 0:
153 print "Username and password required. Passing login info."
154 child.sendline(user)
155 child.expect("Password:")
156 child.sendline(passwd)
157 child.expect("CLI#")
158 print "Logged in, getting flowtable."
159 child.sendline("flowtable brief")
160 for t in range (9):
161 t2 = 9 - t
162 print "\r" + str(t2)
163 sys.stdout.write("\033[F")
164 time.sleep(1)
165 print "Scanning flowtable"
166 child.expect("Flow table show")
167 count = 0
168 while 1:
169 i = child.expect(['17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}','CLI#',pexpect.TIMEOUT])
170 if i == 0:
171 count = count + 1
172 elif i == 1:
173 print "Pronto flows: " + str(count) + "\nDone\n"
174 break
175 else:
176 break
177 def cisco(ip,user,passwd):
178 print "Establishing Cisco switch connection"
179 child = pexpect.spawn("ssh " + user + "@" + ip)
180 i = child.expect(["Password:", "CLI#",pexpect.TIMEOUT])
181 if i == 0:
182 print "Password required. Passing now."
183 child.sendline(passwd)
184 child.expect("#")
185 print "Logged in. Retrieving flow table then counting flows."
186 child.sendline("show openflow switch all flows all")
187 child.expect("Logical Openflow Switch")
188 print "Flow table retrieved. Counting flows"
189 count = 0
190 while 1:
191 i = child.expect(["nw_src=17","#",pexpect.TIMEOUT])
192 if i == 0:
193 count = count + 1
194 elif i == 1:
195 print "Cisco flows: " + str(count) + "\nDone\n"
196 break
197 else:
198 break
199 if brand == "pronto" or brand == "PRONTO":
200 pronto(ip,user,passwd)
201 #elif brand == "cisco" or brand == "CISCO":
202 # cisco(ip,user,passwd)
203 def disconnect(self):
204 '''
205 Called when Test is complete to disconnect the Quagga handle.
206 '''
207 response = ''
208 try:
209 self.handle.close()
210 except:
211 main.log.error("Connection failed to the host")
212 response = main.FALSE
213 return response