blob: 2cb7c9848222494418eb7a0db4e4e707d423aaf1 [file] [log] [blame]
andrewonlaba548f962014-10-21 19:28:43 -04001#!/usr/bin/env python
2
3'''
4This driver handles the optical switch emulator linc-oe.
5
6Please follow the coding style demonstrated by existing
7functions and document properly.
8
9If you are a contributor to the driver, please
10list your email here for future contact:
11
12 andrew@onlab.us
shahshreyae6c7cf42014-11-26 16:39:01 -080013 shreya@onlab.us
andrewonlaba548f962014-10-21 19:28:43 -040014
15OCT 20 2014
16'''
17
18import traceback
19import pexpect
20import struct
21import fcntl
22import os
23import signal
24import re
25import sys
26import core.teston
shahshreyae6c7cf42014-11-26 16:39:01 -080027import time
andrewonlaba548f962014-10-21 19:28:43 -040028sys.path.append("../")
29from math import pow
30from drivers.common.cli.emulatordriver import Emulator
31from drivers.common.clidriver import CLI
32
33class LincOEDriver(Emulator):
34 '''
35 LincOEDriver class will handle all emulator functions
36 '''
37 def __init__(self):
38 super(Emulator, self).__init__()
39 self.handle = self
40 self.wrapped = sys.modules[__name__]
41 self.flag = 0
42
43 def connect(self, **connectargs):
44 '''
45 Create ssh handle for Linc-OE cli
46 '''
andrewonlaba85a7762014-10-22 18:05:52 -040047 import time
48
andrewonlaba548f962014-10-21 19:28:43 -040049 for key in connectargs:
50 vars(self)[key] = connectargs[key]
51
52 self.name = self.options['name']
53 self.handle = \
54 super(LincOEDriver, self).connect(\
55 user_name = self.user_name,
56 ip_address = self.ip_address,
57 port = None,
58 pwd = self.pwd)
59
60 self.ssh_handle = self.handle
61
62 if self.handle :
andrewonlab8d29f122014-10-22 17:15:04 -040063 main.log.info("Handle successfully created")
andrewonlab52a31e02014-10-22 12:57:19 -040064 self.home = "~/linc-oe"
shahshreyae6c7cf42014-11-26 16:39:01 -080065
andrewonlab52a31e02014-10-22 12:57:19 -040066 self.handle.sendline("cd "+self.home)
andrewonlab8d29f122014-10-22 17:15:04 -040067 self.handle.expect("oe$")
shahshreyae6c7cf42014-11-26 16:39:01 -080068
69 #self.handle.sendline("pgrep -g linc")
70 #self.handle.expect("\$")
71 print "handle = ", self.handle.before
72
73 return main.TRUE
74 '''
andrewonlab8d29f122014-10-22 17:15:04 -040075 main.log.info("Building Linc-OE")
76 self.handle.sendline("make rel")
andrewonlaba85a7762014-10-22 18:05:52 -040077 i = self.handle.expect(["ERROR","linc-oe\$"],timeout=60)
andrewonlab8d29f122014-10-22 17:15:04 -040078 if i == 0:
79 self.handle.sendline("sudo pkill -9 epmd")
80 self.handle.expect("\$")
81 self.handle.sendline("make rel")
andrewonlaba85a7762014-10-22 18:05:52 -040082 x = self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
andrewonlab8d29f122014-10-22 17:15:04 -040083 main.log.info("make rel returned: "+ str(x))
andrewonlaba85a7762014-10-22 18:05:52 -040084 else:
shahshreyae6c7cf42014-11-26 16:39:01 -080085
86 main.log.info(self.name+": Starting Linc-OE CLI.. This may take a while")
87 time.sleep(30)
88 self.handle.sendline("sudo ./rel/linc/bin/linc console")
89 j = self.handle.expect(["linc@",pexpect.EOF,pexpect.TIMEOUT])
andrewonlaba85a7762014-10-22 18:05:52 -040090 if j == 0:
91 main.log.info("Linc-OE CLI started")
92 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -080093 '''
andrewonlaba548f962014-10-21 19:28:43 -040094 else:
95 main.log.error(self.name+
96 ": Connection failed to the host "+
97 self.user_name+"@"+self.ip_address)
98 main.log.error(self.name+
99 ": Failed to connect to Linc-OE")
100 return main.FALSE
101
shahshreyae6c7cf42014-11-26 16:39:01 -0800102
103 def start_console(self):
104 import time
105 main.log.info(self.name+": Starting Linc-OE CLI.. This may take a while")
106 time.sleep(30)
107 self.handle.sendline("sudo ./rel/linc/bin/linc console")
108 j = self.handle.expect(["linc@",pexpect.EOF,pexpect.TIMEOUT])
109 start_result = self.handle.before
110 if j == 0:
111 main.log.info("Linc-OE CLI started")
112 return main.TRUE
113 else:
114 main.log.error(self.name+
115 ": Connection failed to the host "+self.user_name+"@"+self.ip_address)
116 main.log.error(self.name+
117 ": Failed to connect to Linc-OE")
118 return main.FALSE
119
120
121
andrewonlab52a31e02014-10-22 12:57:19 -0400122 def build(self):
123 '''
124 Build Linc-OE with the specified settings
125 '''
126 try:
127 self.handle.sendline("make rel")
andrewonlabc6d1fa62014-10-22 16:28:04 -0400128 i = self.handle.expect([
129 "ERROR",
130 "\$"])
131
132 if i == 0:
133 #If error, try to resolve the most common error
134 #(epmd running and cannot compile)
135 self.handle.sendline("sudo pkill -9 epmd")
136 self.handle.sendline("make rel")
137 self.handle.expect("\$")
138
139 handle = self.handle.before
140 return handle
141
142 else:
143 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400144
145 except pexpect.EOF:
146 main.log.error(self.name+ ": EOF exception")
147 main.log.error(self.name+ ": " + self.handle.before)
148 main.cleanup()
149 main.exit()
150 except:
151 main.log.info(self.name+" :::::::")
152 main.log.error( traceback.print_exc())
153 main.log.info(self.name+" :::::::")
154 main.cleanup()
155 main.exit()
156
andrewonlabeb08b6f2014-10-21 21:23:15 -0400157 def set_interface_up(self, intfs):
158 '''
159 Specify interface to bring up.
160 When Linc-OE is started, tap interfaces should
161 be created. They must be brought up manually
162 '''
163 try:
Jon Hallfbc828e2015-01-06 17:30:19 -0800164 self.handle.sendline("ifconfig "+str(intfs)+" up")
andrewonlabeb08b6f2014-10-21 21:23:15 -0400165 self.handle.expect("linc@")
166
167 handle = self.handle.before
168
169 return handle
170
171 except pexpect.EOF:
172 main.log.error(self.name+ ": EOF exception")
173 main.log.error(self.name+ ": " + self.handle.before)
174 main.cleanup()
175 main.exit()
176 except:
177 main.log.info(self.name+" :::::::")
178 main.log.error( traceback.print_exc())
179 main.log.info(self.name+" :::::::")
180 main.cleanup()
181 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400182
andrewonlab52a31e02014-10-22 12:57:19 -0400183 def start_switch(self, sw_id):
184 '''
185 Start a logical switch using switch id
186 '''
187 try:
188 self.handle.sendline("linc:start_switch("+str(sw_id)+").")
189 self.handle.expect("linc@")
190
191 handle = self.handle.before
192
193 except pexpect.EOF:
194 main.log.error(self.name+ ": EOF exception")
195 main.log.error(self.name+ ": " + self.handle.before)
196 main.cleanup()
197 main.exit()
198 except:
199 main.log.info(self.name+" :::::::")
200 main.log.error( traceback.print_exc())
201 main.log.info(self.name+" :::::::")
202 main.cleanup()
203 main.exit()
204
205 def stop_switch(self, sw_id):
206 '''
207 Stop a logical switch using switch id
208 '''
209 try:
210 self.handle.sendline("linc:stop_switch("+str(sw_id)+").")
211 self.handle.expect("linc@")
212
213 handle = self.handle.before
214
215 except pexpect.EOF:
216 main.log.error(self.name+ ": EOF exception")
217 main.log.error(self.name+ ": " + self.handle.before)
218 main.cleanup()
219 main.exit()
220 except:
221 main.log.info(self.name+" :::::::")
222 main.log.error( traceback.print_exc())
223 main.log.info(self.name+" :::::::")
224 main.cleanup()
225 main.exit()
226
227 def get_datapath_id(self, sw_id):
228 '''
229 Get datapath id of a specific switch by switch id
230 '''
231 try:
232 self.handle.sendline("linc_logic:get_datapath_id("+
233 str(sw_id)+").")
234 self.handle.expect("linc@")
235
236 handle = self.handle.before
237
238 except pexpect.EOF:
239 main.log.error(self.name+ ": EOF exception")
240 main.log.error(self.name+ ": " + self.handle.before)
241 main.cleanup()
242 main.exit()
243 except:
244 main.log.info(self.name+" :::::::")
245 main.log.error( traceback.print_exc())
246 main.log.info(self.name+" :::::::")
247 main.cleanup()
248 main.exit()
249
250 def list_ports(self, sw_id):
251 '''
252 List all ports of a switch by switch id
253 '''
254 try:
255 self.handle.sendline("linc:ports("+str(sw_id)+").")
256 self.handle.expect("linc@")
257
258 handle = self.handle.before
259
260 except pexpect.EOF:
261 main.log.error(self.name+ ": EOF exception")
262 main.log.error(self.name+ ": " + self.handle.before)
263 main.cleanup()
264 main.exit()
265 except:
266 main.log.info(self.name+" :::::::")
267 main.log.error( traceback.print_exc())
268 main.log.info(self.name+" :::::::")
269 main.cleanup()
270 main.exit()
271
272 def port_up(self, sw_id, pt_id):
273 '''
274 Bring port up using switch id and port id
275 '''
276 try:
277 self.handle.sendline("linc:port_up("+
278 str(sw_id)+", "+str(pt_id)+").")
279 self.handle.expect("linc@")
280
281 handle = self.handle.before
282
283 except pexpect.EOF:
284 main.log.error(self.name+ ": EOF exception")
285 main.log.error(self.name+ ": " + self.handle.before)
286 main.cleanup()
287 main.exit()
288 except:
289 main.log.info(self.name+" :::::::")
290 main.log.error( traceback.print_exc())
291 main.log.info(self.name+" :::::::")
292 main.cleanup()
293 main.exit()
294
295 def port_down(self, sw_id, pt_id):
296 '''
297 Bring port down using switch id and port id
298 '''
299 try:
300 self.handle.sendline("linc:port_down("+
301 str(sw_id)+", "+str(pt_id)+").")
302 self.handle.expect("linc@")
303
304 handle = self.handle.before
305
306 except pexpect.EOF:
307 main.log.error(self.name+ ": EOF exception")
308 main.log.error(self.name+ ": " + self.handle.before)
309 main.cleanup()
310 main.exit()
311 except:
312 main.log.info(self.name+" :::::::")
313 main.log.error( traceback.print_exc())
314 main.log.info(self.name+" :::::::")
315 main.cleanup()
316 main.exit()
shahshreyae6c7cf42014-11-26 16:39:01 -0800317
318 def stopLincOEConsole(self):
319 '''
320 This function is only used for packet optical testing
321 Send disconnect prompt to Linc-OE CLI
322 (CTRL+C) and kill the linc process
323 '''
324 try:
325 cmd = "pgrep -f linc"
326 self.handle.sendline("pgrep -f linc")
327 self.handle.expect("linc")
328 print "stophandle = ", self.handle.before
329 except pexpect.EOF:
330 main.log.error(self.name+ ": EOF exception")
331 main.log.error(self.name+ ": " + self.handle.before)
andrewonlaba548f962014-10-21 19:28:43 -0400332
andrewonlab0980f422014-10-21 21:28:39 -0400333 def disconnect(self):
334 '''
335 Send disconnect prompt to Linc-OE CLI
shahshreyae6c7cf42014-11-26 16:39:01 -0800336 (CTRL+C) and kill the linc process
andrewonlab0980f422014-10-21 21:28:39 -0400337 '''
338 try:
339 #Send CTRL+C twice to exit CLI
Jon Hallffb386d2014-11-21 13:43:38 -0800340 self.handle.send("\x03")
341 self.handle.send("\x03")
andrewonlab0980f422014-10-21 21:28:39 -0400342 self.handle.expect("\$")
shahshreyae6c7cf42014-11-26 16:39:01 -0800343 handle1 = self.handle.before
344 cmd = "pgrep -f linc"
345 self.handle.sendline(cmd)
346 self.handle.expect("\$")
347 handle2 = self.handle.before
348 main.log.info("pid's = "+handle2)
349 cmd = "sudo kill -9 `pgrep -f linc`"
350 self.handle.sendline(cmd)
351 self.handle.expect("\$")
352
andrewonlab0980f422014-10-21 21:28:39 -0400353 except pexpect.EOF:
354 main.log.error(self.name+ ": EOF exception")
355 main.log.error(self.name+ ": " + self.handle.before)
356 main.cleanup()
357 main.exit()
358 except:
359 main.log.info(self.name+" :::::::")
360 main.log.error( traceback.print_exc())
361 main.log.info(self.name+" :::::::")
362 main.cleanup()
363 main.exit()
364
andrewonlaba548f962014-10-21 19:28:43 -0400365if __name__ != "__main__":
366 import sys
367 sys.modules[__name__] = LincOEDriver()
368