blob: 944eded5334e886219a053d4a798b855f0bdb00b [file] [log] [blame]
andrewonlaba548f962014-10-21 19:28:43 -04001#!/usr/bin/env python
2
kelvin-onlabedcff052015-01-16 12:53:55 -08003"""
andrewonlaba548f962014-10-21 19:28:43 -04004This 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
kelvin-onlabedcff052015-01-16 12:53:55 -080016"""
andrewonlaba548f962014-10-21 19:28:43 -040017import traceback
18import pexpect
19import struct
20import fcntl
21import os
22import signal
23import re
24import sys
25import core.teston
shahshreyae6c7cf42014-11-26 16:39:01 -080026import time
kelvin-onlabedcff052015-01-16 12:53:55 -080027sys.path.append( "../" )
andrewonlaba548f962014-10-21 19:28:43 -040028from math import pow
29from drivers.common.cli.emulatordriver import Emulator
30from drivers.common.clidriver import CLI
31
kelvin-onlabedcff052015-01-16 12:53:55 -080032
33class LincOEDriver( Emulator ):
34
35 """
36 LincOEDriver class will handle all emulator functions
37 """
38 def __init__( self ):
39 super( Emulator, self ).__init__()
andrewonlaba548f962014-10-21 19:28:43 -040040 self.handle = self
kelvin-onlabedcff052015-01-16 12:53:55 -080041 self.wrapped = sys.modules[ __name__ ]
andrewonlaba548f962014-10-21 19:28:43 -040042 self.flag = 0
43
kelvin-onlabedcff052015-01-16 12:53:55 -080044 def connect( self, **connectargs ):
45 """
andrewonlaba548f962014-10-21 19:28:43 -040046 Create ssh handle for Linc-OE cli
kelvin-onlabedcff052015-01-16 12:53:55 -080047 """
andrewonlaba85a7762014-10-22 18:05:52 -040048 import time
49
andrewonlaba548f962014-10-21 19:28:43 -040050 for key in connectargs:
kelvin-onlabedcff052015-01-16 12:53:55 -080051 vars( self )[ key ] = connectargs[ key ]
52
53 self.name = self.options[ 'name' ]
andrewonlaba548f962014-10-21 19:28:43 -040054 self.handle = \
kelvin-onlabedcff052015-01-16 12:53:55 -080055 super( LincOEDriver, self ).connect(
56 user_name=self.user_name,
57 ip_address=self.ip_address,
58 port=None,
59 pwd=self.pwd )
60
andrewonlaba548f962014-10-21 19:28:43 -040061 self.ssh_handle = self.handle
kelvin-onlabedcff052015-01-16 12:53:55 -080062
63 if self.handle:
64 main.log.info( "Handle successfully created" )
andrewonlab52a31e02014-10-22 12:57:19 -040065 self.home = "~/linc-oe"
kelvin-onlabedcff052015-01-16 12:53:55 -080066
67 self.handle.sendline( "cd " + self.home )
68 self.handle.expect( "oe$" )
69
70 #self.handle.sendline( "pgrep -g linc" )
71 # self.handle.expect( "\$" )
72 print "handle = ", self.handle.before
73
74 return main.TRUE
75 """
76 main.log.info( "Building Linc-OE" )
77 self.handle.sendline( "make rel" )
78 i = self.handle.expect( [ "ERROR","linc-oe\$" ],timeout=60 )
andrewonlab8d29f122014-10-22 17:15:04 -040079 if i == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -080080 self.handle.sendline( "sudo pkill -9 epmd" )
81 self.handle.expect( "\$" )
82 self.handle.sendline( "make rel" )
83 x = self.handle.expect( [ "\$",pexpect.EOF,pexpect.TIMEOUT ] )
84 main.log.info( "make rel returned: "+ str( x ) )
85 else:
86
87 main.log.info( self.name+": Starting Linc-OE CLI.. This may take a while" )
88 time.sleep( 30 )
89 self.handle.sendline( "sudo ./rel/linc/bin/linc console" )
90 j = self.handle.expect( [ "linc@",pexpect.EOF,pexpect.TIMEOUT ] )
andrewonlaba85a7762014-10-22 18:05:52 -040091 if j == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -080092 main.log.info( "Linc-OE CLI started" )
andrewonlaba85a7762014-10-22 18:05:52 -040093 return main.TRUE
kelvin-onlabedcff052015-01-16 12:53:55 -080094 """
andrewonlaba548f962014-10-21 19:28:43 -040095 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080096 main.log.error( self.name +
97 ": Connection failed to the host " +
98 self.user_name + "@" + self.ip_address )
99 main.log.error( self.name +
100 ": Failed to connect to Linc-OE" )
andrewonlaba548f962014-10-21 19:28:43 -0400101 return main.FALSE
102
kelvin-onlabedcff052015-01-16 12:53:55 -0800103 def start_console( self ):
shahshreyae6c7cf42014-11-26 16:39:01 -0800104 import time
kelvin-onlabedcff052015-01-16 12:53:55 -0800105 main.log.info(
106 self.name +
107 ": Starting Linc-OE CLI.. This may take a while" )
108 time.sleep( 30 )
109 self.handle.sendline( "sudo ./rel/linc/bin/linc console" )
110 j = self.handle.expect( [ "linc@", pexpect.EOF, pexpect.TIMEOUT ] )
shahshreyae6c7cf42014-11-26 16:39:01 -0800111 start_result = self.handle.before
112 if j == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -0800113 main.log.info( "Linc-OE CLI started" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800114 return main.TRUE
115 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800116 main.log.error(
117 self.name +
118 ": Connection failed to the host " +
119 self.user_name +
120 "@" +
121 self.ip_address )
122 main.log.error( self.name +
123 ": Failed to connect to Linc-OE" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800124 return main.FALSE
125
kelvin-onlabedcff052015-01-16 12:53:55 -0800126 def build( self ):
127 """
andrewonlab52a31e02014-10-22 12:57:19 -0400128 Build Linc-OE with the specified settings
kelvin-onlabedcff052015-01-16 12:53:55 -0800129 """
andrewonlab52a31e02014-10-22 12:57:19 -0400130 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800131 self.handle.sendline( "make rel" )
132 i = self.handle.expect( [
andrewonlabc6d1fa62014-10-22 16:28:04 -0400133 "ERROR",
kelvin-onlabedcff052015-01-16 12:53:55 -0800134 "\$" ] )
andrewonlabc6d1fa62014-10-22 16:28:04 -0400135
136 if i == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -0800137 # If error, try to resolve the most common error
138 #( epmd running and cannot compile )
139 self.handle.sendline( "sudo pkill -9 epmd" )
140 self.handle.sendline( "make rel" )
141 self.handle.expect( "\$" )
142
andrewonlabc6d1fa62014-10-22 16:28:04 -0400143 handle = self.handle.before
144 return handle
145
146 else:
147 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400148
149 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800150 main.log.error( self.name + ": EOF exception" )
151 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400152 main.cleanup()
153 main.exit()
154 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800155 main.log.info( self.name + " :::::::" )
156 main.log.error( traceback.print_exc() )
157 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400158 main.cleanup()
159 main.exit()
160
kelvin-onlabedcff052015-01-16 12:53:55 -0800161 def set_interface_up( self, intfs ):
162 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400163 Specify interface to bring up.
164 When Linc-OE is started, tap interfaces should
165 be created. They must be brought up manually
kelvin-onlabedcff052015-01-16 12:53:55 -0800166 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400167 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800168 self.handle.sendline( "ifconfig " + str( intfs ) + " up" )
169 self.handle.expect( "linc@" )
170
andrewonlabeb08b6f2014-10-21 21:23:15 -0400171 handle = self.handle.before
172
173 return handle
174
175 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800176 main.log.error( self.name + ": EOF exception" )
177 main.log.error( self.name + ": " + self.handle.before )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400178 main.cleanup()
179 main.exit()
180 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800181 main.log.info( self.name + " :::::::" )
182 main.log.error( traceback.print_exc() )
183 main.log.info( self.name + " :::::::" )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400184 main.cleanup()
185 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400186
kelvin-onlabedcff052015-01-16 12:53:55 -0800187 def start_switch( self, sw_id ):
188 """
andrewonlab52a31e02014-10-22 12:57:19 -0400189 Start a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800190 """
andrewonlab52a31e02014-10-22 12:57:19 -0400191 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800192 self.handle.sendline( "linc:start_switch(" + str( sw_id ) + ")." )
193 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400194
195 handle = self.handle.before
196
197 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800198 main.log.error( self.name + ": EOF exception" )
199 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400200 main.cleanup()
201 main.exit()
202 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800203 main.log.info( self.name + " :::::::" )
204 main.log.error( traceback.print_exc() )
205 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400206 main.cleanup()
207 main.exit()
208
kelvin-onlabedcff052015-01-16 12:53:55 -0800209 def stop_switch( self, sw_id ):
210 """
andrewonlab52a31e02014-10-22 12:57:19 -0400211 Stop a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800212 """
andrewonlab52a31e02014-10-22 12:57:19 -0400213 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800214 self.handle.sendline( "linc:stop_switch(" + str( sw_id ) + ")." )
215 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400216
217 handle = self.handle.before
218
219 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800220 main.log.error( self.name + ": EOF exception" )
221 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400222 main.cleanup()
223 main.exit()
224 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800225 main.log.info( self.name + " :::::::" )
226 main.log.error( traceback.print_exc() )
227 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400228 main.cleanup()
229 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800230
231 def get_datapath_id( self, sw_id ):
232 """
andrewonlab52a31e02014-10-22 12:57:19 -0400233 Get datapath id of a specific switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800234 """
andrewonlab52a31e02014-10-22 12:57:19 -0400235 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800236 self.handle.sendline( "linc_logic:get_datapath_id(" +
237 str( sw_id ) + ")." )
238 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400239
240 handle = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800241
andrewonlab52a31e02014-10-22 12:57:19 -0400242 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800243 main.log.error( self.name + ": EOF exception" )
244 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400245 main.cleanup()
246 main.exit()
247 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800248 main.log.info( self.name + " :::::::" )
249 main.log.error( traceback.print_exc() )
250 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400251 main.cleanup()
252 main.exit()
253
kelvin-onlabedcff052015-01-16 12:53:55 -0800254 def list_ports( self, sw_id ):
255 """
andrewonlab52a31e02014-10-22 12:57:19 -0400256 List all ports of a switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800257 """
andrewonlab52a31e02014-10-22 12:57:19 -0400258 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800259 self.handle.sendline( "linc:ports(" + str( sw_id ) + ")." )
260 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400261
262 handle = self.handle.before
263
264 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800265 main.log.error( self.name + ": EOF exception" )
266 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400267 main.cleanup()
268 main.exit()
269 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800270 main.log.info( self.name + " :::::::" )
271 main.log.error( traceback.print_exc() )
272 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400273 main.cleanup()
274 main.exit()
275
kelvin-onlabedcff052015-01-16 12:53:55 -0800276 def port_up( self, sw_id, pt_id ):
277 """
andrewonlab52a31e02014-10-22 12:57:19 -0400278 Bring port up using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800279 """
andrewonlab52a31e02014-10-22 12:57:19 -0400280 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800281 self.handle.sendline( "linc:port_up(" +
282 str( sw_id ) + ", " + str( pt_id ) + ")." )
283 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400284
285 handle = self.handle.before
286
287 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800288 main.log.error( self.name + ": EOF exception" )
289 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400290 main.cleanup()
291 main.exit()
292 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800293 main.log.info( self.name + " :::::::" )
294 main.log.error( traceback.print_exc() )
295 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400296 main.cleanup()
297 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800298
299 def port_down( self, sw_id, pt_id ):
300 """
andrewonlab52a31e02014-10-22 12:57:19 -0400301 Bring port down using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800302 """
andrewonlab52a31e02014-10-22 12:57:19 -0400303 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800304 self.handle.sendline( "linc:port_down(" +
305 str( sw_id ) + ", " + str( pt_id ) + ")." )
306 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400307
308 handle = self.handle.before
309
310 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800311 main.log.error( self.name + ": EOF exception" )
312 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400313 main.cleanup()
314 main.exit()
315 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800316 main.log.info( self.name + " :::::::" )
317 main.log.error( traceback.print_exc() )
318 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400319 main.cleanup()
320 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800321
322 def stopLincOEConsole( self ):
323 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800324 This function is only used for packet optical testing
325 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800326 ( CTRL+C ) and kill the linc process
327 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800328 try:
329 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800330 self.handle.sendline( "pgrep -f linc" )
331 self.handle.expect( "linc" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800332 print "stophandle = ", self.handle.before
333 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800334 main.log.error( self.name + ": EOF exception" )
335 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba548f962014-10-21 19:28:43 -0400336
kelvin-onlabedcff052015-01-16 12:53:55 -0800337 def disconnect( self ):
338 """
andrewonlab0980f422014-10-21 21:28:39 -0400339 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800340 ( CTRL+C ) and kill the linc process
341 """
andrewonlab0980f422014-10-21 21:28:39 -0400342 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800343 # Send CTRL+C twice to exit CLI
344 self.handle.send( "\x03" )
345 self.handle.send( "\x03" )
346 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800347 handle1 = self.handle.before
348 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800349 self.handle.sendline( cmd )
350 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800351 handle2 = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800352 main.log.info( "pid's = " + handle2 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800353 cmd = "sudo kill -9 `pgrep -f linc`"
kelvin-onlabedcff052015-01-16 12:53:55 -0800354 self.handle.sendline( cmd )
355 self.handle.expect( "\$" )
356
andrewonlab0980f422014-10-21 21:28:39 -0400357 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800358 main.log.error( self.name + ": EOF exception" )
359 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0980f422014-10-21 21:28:39 -0400360 main.cleanup()
361 main.exit()
362 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800363 main.log.info( self.name + " :::::::" )
364 main.log.error( traceback.print_exc() )
365 main.log.info( self.name + " :::::::" )
andrewonlab0980f422014-10-21 21:28:39 -0400366 main.cleanup()
367 main.exit()
368
andrewonlaba548f962014-10-21 19:28:43 -0400369if __name__ != "__main__":
370 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800371 sys.modules[ __name__ ] = LincOEDriver()