blob: a82cfa42723adbeb83c28996b6bb50a4614fba8e [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(
kelvin-onlab08679eb2015-01-21 16:11:48 -080056 user_name=self.user_name,
57 ip_address=self.ip_address,
kelvin-onlabedcff052015-01-16 12:53:55 -080058 port=None,
59 pwd=self.pwd )
60
kelvin-onlabd3b64892015-01-20 13:26:24 -080061 self.sshHandle = 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
kelvin-onlabedcff052015-01-16 12:53:55 -080070 print "handle = ", self.handle.before
71
72 return main.TRUE
andrewonlaba548f962014-10-21 19:28:43 -040073 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080074 main.log.error( self.name +
75 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080076 self.user_name + "@" + self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -080077 main.log.error( self.name +
78 ": Failed to connect to Linc-OE" )
andrewonlaba548f962014-10-21 19:28:43 -040079 return main.FALSE
80
kelvin-onlabd3b64892015-01-20 13:26:24 -080081 def startConsole( self ):
shahshreyae6c7cf42014-11-26 16:39:01 -080082 import time
kelvin-onlabedcff052015-01-16 12:53:55 -080083 main.log.info(
84 self.name +
85 ": Starting Linc-OE CLI.. This may take a while" )
86 time.sleep( 30 )
87 self.handle.sendline( "sudo ./rel/linc/bin/linc console" )
88 j = self.handle.expect( [ "linc@", pexpect.EOF, pexpect.TIMEOUT ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -080089 startResult = self.handle.before
shahshreyae6c7cf42014-11-26 16:39:01 -080090 if j == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -080091 main.log.info( "Linc-OE CLI started" )
shahshreyae6c7cf42014-11-26 16:39:01 -080092 return main.TRUE
93 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080094 main.log.error(
95 self.name +
96 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080097 self.user_name +
kelvin-onlabedcff052015-01-16 12:53:55 -080098 "@" +
kelvin-onlab08679eb2015-01-21 16:11:48 -080099 self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -0800100 main.log.error( self.name +
101 ": Failed to connect to Linc-OE" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800102 return main.FALSE
103
kelvin-onlabedcff052015-01-16 12:53:55 -0800104 def build( self ):
105 """
andrewonlab52a31e02014-10-22 12:57:19 -0400106 Build Linc-OE with the specified settings
kelvin-onlabedcff052015-01-16 12:53:55 -0800107 """
andrewonlab52a31e02014-10-22 12:57:19 -0400108 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800109 self.handle.sendline( "make rel" )
110 i = self.handle.expect( [
andrewonlabc6d1fa62014-10-22 16:28:04 -0400111 "ERROR",
kelvin-onlabedcff052015-01-16 12:53:55 -0800112 "\$" ] )
andrewonlabc6d1fa62014-10-22 16:28:04 -0400113
114 if i == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -0800115 self.handle.sendline( "sudo pkill -9 epmd" )
116 self.handle.sendline( "make rel" )
117 self.handle.expect( "\$" )
118
andrewonlabc6d1fa62014-10-22 16:28:04 -0400119 handle = self.handle.before
120 return handle
121
122 else:
123 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400124
125 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800126 main.log.error( self.name + ": EOF exception" )
127 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400128 main.cleanup()
129 main.exit()
130 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800131 main.log.info( self.name + " :::::::" )
kelvin-onlab09aabaf2015-01-22 13:32:33 -0800132 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800133 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400134 main.cleanup()
135 main.exit()
136
kelvin-onlabd3b64892015-01-20 13:26:24 -0800137 def setInterfaceUp( self, intfs ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800138 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400139 Specify interface to bring up.
140 When Linc-OE is started, tap interfaces should
141 be created. They must be brought up manually
kelvin-onlabedcff052015-01-16 12:53:55 -0800142 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400143 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800144 self.handle.sendline( "ifconfig " + str( intfs ) + " up" )
145 self.handle.expect( "linc@" )
146
andrewonlabeb08b6f2014-10-21 21:23:15 -0400147 handle = self.handle.before
148
149 return handle
150
151 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800152 main.log.error( self.name + ": EOF exception" )
153 main.log.error( self.name + ": " + self.handle.before )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400154 main.cleanup()
155 main.exit()
156 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800157 main.log.info( self.name + " :::::::" )
kelvin-onlab09aabaf2015-01-22 13:32:33 -0800158 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800159 main.log.info( self.name + " :::::::" )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400160 main.cleanup()
161 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400162
kelvin-onlabd3b64892015-01-20 13:26:24 -0800163 def startSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800164 """
andrewonlab52a31e02014-10-22 12:57:19 -0400165 Start a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800166 """
andrewonlab52a31e02014-10-22 12:57:19 -0400167 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800168 self.handle.sendline( "linc:start_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800169 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400170
171 handle = self.handle.before
172
173 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800174 main.log.error( self.name + ": EOF exception" )
175 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400176 main.cleanup()
177 main.exit()
178 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800179 main.log.info( self.name + " :::::::" )
kelvin-onlab09aabaf2015-01-22 13:32:33 -0800180 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800181 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400182 main.cleanup()
183 main.exit()
184
kelvin-onlabd3b64892015-01-20 13:26:24 -0800185 def stopSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800186 """
andrewonlab52a31e02014-10-22 12:57:19 -0400187 Stop a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800188 """
andrewonlab52a31e02014-10-22 12:57:19 -0400189 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800190 self.handle.sendline( "linc:stop_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800191 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400192
193 handle = self.handle.before
194
195 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800196 main.log.error( self.name + ": EOF exception" )
197 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400198 main.cleanup()
199 main.exit()
200 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800201 main.log.info( self.name + " :::::::" )
kelvin-onlab09aabaf2015-01-22 13:32:33 -0800202 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800203 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400204 main.cleanup()
205 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800206
kelvin-onlabd3b64892015-01-20 13:26:24 -0800207 def getDatapathId( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800208 """
andrewonlab52a31e02014-10-22 12:57:19 -0400209 Get datapath id of a specific switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800210 """
andrewonlab52a31e02014-10-22 12:57:19 -0400211 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800212 self.handle.sendline( "linc_logic:get_datapath_id(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800213 str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800214 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400215
216 handle = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800217
andrewonlab52a31e02014-10-22 12:57:19 -0400218 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800219 main.log.error( self.name + ": EOF exception" )
220 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400221 main.cleanup()
222 main.exit()
223 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800224 main.log.info( self.name + " :::::::" )
kelvin-onlab09aabaf2015-01-22 13:32:33 -0800225 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800226 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400227 main.cleanup()
228 main.exit()
229
kelvin-onlabd3b64892015-01-20 13:26:24 -0800230 def listPorts( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800231 """
andrewonlab52a31e02014-10-22 12:57:19 -0400232 List all ports of a switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800233 """
andrewonlab52a31e02014-10-22 12:57:19 -0400234 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800235 self.handle.sendline( "linc:ports(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800236 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400237
238 handle = self.handle.before
239
240 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800241 main.log.error( self.name + ": EOF exception" )
242 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400243 main.cleanup()
244 main.exit()
245 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800246 main.log.info( self.name + " :::::::" )
kelvin-onlab09aabaf2015-01-22 13:32:33 -0800247 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800248 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400249 main.cleanup()
250 main.exit()
251
kelvin-onlabd3b64892015-01-20 13:26:24 -0800252 def portUp( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800253 """
andrewonlab52a31e02014-10-22 12:57:19 -0400254 Bring port up using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800255 """
andrewonlab52a31e02014-10-22 12:57:19 -0400256 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800257 self.handle.sendline( "linc:port_up(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800258 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800259 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400260
261 handle = self.handle.before
262
263 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800264 main.log.error( self.name + ": EOF exception" )
265 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400266 main.cleanup()
267 main.exit()
268 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800269 main.log.info( self.name + " :::::::" )
kelvin-onlab09aabaf2015-01-22 13:32:33 -0800270 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800271 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400272 main.cleanup()
273 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800274
kelvin-onlabd3b64892015-01-20 13:26:24 -0800275 def portDown( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800276 """
andrewonlab52a31e02014-10-22 12:57:19 -0400277 Bring port down using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800278 """
andrewonlab52a31e02014-10-22 12:57:19 -0400279 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800280 self.handle.sendline( "linc:port_down(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800281 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800282 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400283
284 handle = self.handle.before
285
286 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800287 main.log.error( self.name + ": EOF exception" )
288 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400289 main.cleanup()
290 main.exit()
291 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800292 main.log.info( self.name + " :::::::" )
kelvin-onlab09aabaf2015-01-22 13:32:33 -0800293 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800294 main.log.info( self.name + " :::::::" )
andrewonlab52a31e02014-10-22 12:57:19 -0400295 main.cleanup()
296 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800297
298 def stopLincOEConsole( self ):
299 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800300 This function is only used for packet optical testing
301 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800302 ( CTRL+C ) and kill the linc process
303 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800304 try:
305 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800306 self.handle.sendline( "pgrep -f linc" )
307 self.handle.expect( "linc" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800308 print "stophandle = ", self.handle.before
309 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800310 main.log.error( self.name + ": EOF exception" )
311 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba548f962014-10-21 19:28:43 -0400312
kelvin-onlabedcff052015-01-16 12:53:55 -0800313 def disconnect( self ):
314 """
andrewonlab0980f422014-10-21 21:28:39 -0400315 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800316 ( CTRL+C ) and kill the linc process
317 """
andrewonlab0980f422014-10-21 21:28:39 -0400318 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800319 # Send CTRL+C twice to exit CLI
320 self.handle.send( "\x03" )
321 self.handle.send( "\x03" )
322 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800323 handle1 = self.handle.before
324 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800325 self.handle.sendline( cmd )
326 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800327 handle2 = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800328 main.log.info( "pid's = " + handle2 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800329 cmd = "sudo kill -9 `pgrep -f linc`"
kelvin-onlabedcff052015-01-16 12:53:55 -0800330 self.handle.sendline( cmd )
331 self.handle.expect( "\$" )
332
andrewonlab0980f422014-10-21 21:28:39 -0400333 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 )
andrewonlab0980f422014-10-21 21:28:39 -0400336 main.cleanup()
337 main.exit()
338 except:
kelvin-onlabedcff052015-01-16 12:53:55 -0800339 main.log.info( self.name + " :::::::" )
kelvin-onlab09aabaf2015-01-22 13:32:33 -0800340 main.log.error( traceback.print_exc() )
kelvin-onlabedcff052015-01-16 12:53:55 -0800341 main.log.info( self.name + " :::::::" )
andrewonlab0980f422014-10-21 21:28:39 -0400342 main.cleanup()
343 main.exit()
344
andrewonlaba548f962014-10-21 19:28:43 -0400345if __name__ != "__main__":
346 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800347 sys.modules[ __name__ ] = LincOEDriver()