blob: b1bc05aa3e33dd61201b21aace70c3d220d509eb [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"""
Jon Hallfebb1c72015-03-05 13:30:09 -080017
andrewonlaba548f962014-10-21 19:28:43 -040018import pexpect
andrewonlaba548f962014-10-21 19:28:43 -040019import sys
kelvin-onlabedcff052015-01-16 12:53:55 -080020sys.path.append( "../" )
andrewonlaba548f962014-10-21 19:28:43 -040021from drivers.common.cli.emulatordriver import Emulator
andrewonlaba548f962014-10-21 19:28:43 -040022
kelvin-onlabedcff052015-01-16 12:53:55 -080023
24class LincOEDriver( Emulator ):
25
26 """
27 LincOEDriver class will handle all emulator functions
28 """
29 def __init__( self ):
30 super( Emulator, self ).__init__()
andrewonlaba548f962014-10-21 19:28:43 -040031 self.handle = self
kelvin-onlabedcff052015-01-16 12:53:55 -080032 self.wrapped = sys.modules[ __name__ ]
andrewonlaba548f962014-10-21 19:28:43 -040033 self.flag = 0
34
kelvin-onlabedcff052015-01-16 12:53:55 -080035 def connect( self, **connectargs ):
36 """
andrewonlaba548f962014-10-21 19:28:43 -040037 Create ssh handle for Linc-OE cli
kelvin-onlabedcff052015-01-16 12:53:55 -080038 """
andrewonlaba85a7762014-10-22 18:05:52 -040039
andrewonlaba548f962014-10-21 19:28:43 -040040 for key in connectargs:
kelvin-onlabedcff052015-01-16 12:53:55 -080041 vars( self )[ key ] = connectargs[ key ]
42
43 self.name = self.options[ 'name' ]
andrewonlaba548f962014-10-21 19:28:43 -040044 self.handle = \
kelvin-onlabedcff052015-01-16 12:53:55 -080045 super( LincOEDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080046 user_name=self.user_name,
47 ip_address=self.ip_address,
kelvin-onlabedcff052015-01-16 12:53:55 -080048 port=None,
49 pwd=self.pwd )
50
kelvin-onlabd3b64892015-01-20 13:26:24 -080051 self.sshHandle = self.handle
kelvin-onlabedcff052015-01-16 12:53:55 -080052
53 if self.handle:
54 main.log.info( "Handle successfully created" )
andrewonlab52a31e02014-10-22 12:57:19 -040055 self.home = "~/linc-oe"
kelvin-onlabedcff052015-01-16 12:53:55 -080056
57 self.handle.sendline( "cd " + self.home )
58 self.handle.expect( "oe$" )
59
kelvin-onlabedcff052015-01-16 12:53:55 -080060 print "handle = ", self.handle.before
61
62 return main.TRUE
andrewonlaba548f962014-10-21 19:28:43 -040063 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080064 main.log.error( self.name +
65 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080066 self.user_name + "@" + self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -080067 main.log.error( self.name +
68 ": Failed to connect to Linc-OE" )
andrewonlaba548f962014-10-21 19:28:43 -040069 return main.FALSE
70
kelvin-onlabd3b64892015-01-20 13:26:24 -080071 def startConsole( self ):
shahshreyae6c7cf42014-11-26 16:39:01 -080072 import time
kelvin-onlabedcff052015-01-16 12:53:55 -080073 main.log.info(
74 self.name +
75 ": Starting Linc-OE CLI.. This may take a while" )
76 time.sleep( 30 )
77 self.handle.sendline( "sudo ./rel/linc/bin/linc console" )
78 j = self.handle.expect( [ "linc@", pexpect.EOF, pexpect.TIMEOUT ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -080079 startResult = self.handle.before
shahshreyae6c7cf42014-11-26 16:39:01 -080080 if j == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -080081 main.log.info( "Linc-OE CLI started" )
shahshreyae6c7cf42014-11-26 16:39:01 -080082 return main.TRUE
83 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080084 main.log.error(
85 self.name +
86 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080087 self.user_name +
kelvin-onlabedcff052015-01-16 12:53:55 -080088 "@" +
kelvin-onlab08679eb2015-01-21 16:11:48 -080089 self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -080090 main.log.error( self.name +
91 ": Failed to connect to Linc-OE" )
shahshreyae6c7cf42014-11-26 16:39:01 -080092 return main.FALSE
93
kelvin-onlabedcff052015-01-16 12:53:55 -080094 def build( self ):
95 """
andrewonlab52a31e02014-10-22 12:57:19 -040096 Build Linc-OE with the specified settings
kelvin-onlabedcff052015-01-16 12:53:55 -080097 """
andrewonlab52a31e02014-10-22 12:57:19 -040098 try:
kelvin-onlabedcff052015-01-16 12:53:55 -080099 self.handle.sendline( "make rel" )
100 i = self.handle.expect( [
andrewonlabc6d1fa62014-10-22 16:28:04 -0400101 "ERROR",
kelvin-onlabedcff052015-01-16 12:53:55 -0800102 "\$" ] )
andrewonlabc6d1fa62014-10-22 16:28:04 -0400103
104 if i == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -0800105 self.handle.sendline( "sudo pkill -9 epmd" )
106 self.handle.sendline( "make rel" )
107 self.handle.expect( "\$" )
108
andrewonlabc6d1fa62014-10-22 16:28:04 -0400109 handle = self.handle.before
110 return handle
111
112 else:
113 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400114
115 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800116 main.log.error( self.name + ": EOF exception" )
117 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400118 main.cleanup()
119 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800120 except Exception:
121 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400122 main.cleanup()
123 main.exit()
124
kelvin-onlabd3b64892015-01-20 13:26:24 -0800125 def setInterfaceUp( self, intfs ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800126 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400127 Specify interface to bring up.
128 When Linc-OE is started, tap interfaces should
129 be created. They must be brought up manually
kelvin-onlabedcff052015-01-16 12:53:55 -0800130 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400131 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800132 self.handle.sendline( "ifconfig " + str( intfs ) + " up" )
133 self.handle.expect( "linc@" )
134
andrewonlabeb08b6f2014-10-21 21:23:15 -0400135 handle = self.handle.before
136
137 return handle
138
139 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800140 main.log.error( self.name + ": EOF exception" )
141 main.log.error( self.name + ": " + self.handle.before )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400142 main.cleanup()
143 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800144 except Exception:
145 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400146 main.cleanup()
147 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400148
kelvin-onlabd3b64892015-01-20 13:26:24 -0800149 def startSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800150 """
andrewonlab52a31e02014-10-22 12:57:19 -0400151 Start a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800152 """
andrewonlab52a31e02014-10-22 12:57:19 -0400153 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800154 self.handle.sendline( "linc:start_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800155 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400156
157 handle = self.handle.before
158
159 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800160 main.log.error( self.name + ": EOF exception" )
161 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400162 main.cleanup()
163 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800164 except Exception:
165 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400166 main.cleanup()
167 main.exit()
168
kelvin-onlabd3b64892015-01-20 13:26:24 -0800169 def stopSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800170 """
andrewonlab52a31e02014-10-22 12:57:19 -0400171 Stop a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800172 """
andrewonlab52a31e02014-10-22 12:57:19 -0400173 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800174 self.handle.sendline( "linc:stop_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800175 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400176
177 handle = self.handle.before
178
179 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800180 main.log.error( self.name + ": EOF exception" )
181 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400182 main.cleanup()
183 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800184 except Exception:
185 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400186 main.cleanup()
187 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800188
kelvin-onlabd3b64892015-01-20 13:26:24 -0800189 def getDatapathId( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800190 """
andrewonlab52a31e02014-10-22 12:57:19 -0400191 Get datapath id of a specific switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800192 """
andrewonlab52a31e02014-10-22 12:57:19 -0400193 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800194 self.handle.sendline( "linc_logic:get_datapath_id(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800195 str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800196 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400197
198 handle = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800199
andrewonlab52a31e02014-10-22 12:57:19 -0400200 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800201 main.log.error( self.name + ": EOF exception" )
202 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400203 main.cleanup()
204 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800205 except Exception:
206 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400207 main.cleanup()
208 main.exit()
209
kelvin-onlabd3b64892015-01-20 13:26:24 -0800210 def listPorts( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800211 """
andrewonlab52a31e02014-10-22 12:57:19 -0400212 List all ports of a switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800213 """
andrewonlab52a31e02014-10-22 12:57:19 -0400214 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 self.handle.sendline( "linc:ports(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800216 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400217
218 handle = self.handle.before
219
220 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800221 main.log.error( self.name + ": EOF exception" )
222 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400223 main.cleanup()
224 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800225 except Exception:
226 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400227 main.cleanup()
228 main.exit()
229
kelvin-onlabd3b64892015-01-20 13:26:24 -0800230 def portUp( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800231 """
andrewonlab52a31e02014-10-22 12:57:19 -0400232 Bring port up using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800233 """
andrewonlab52a31e02014-10-22 12:57:19 -0400234 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800235 self.handle.sendline( "linc:port_up(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800236 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800237 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400238
239 handle = self.handle.before
240
241 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800242 main.log.error( self.name + ": EOF exception" )
243 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400244 main.cleanup()
245 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800246 except Exception:
247 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400248 main.cleanup()
249 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800250
kelvin-onlabd3b64892015-01-20 13:26:24 -0800251 def portDown( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800252 """
andrewonlab52a31e02014-10-22 12:57:19 -0400253 Bring port down using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800254 """
andrewonlab52a31e02014-10-22 12:57:19 -0400255 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800256 self.handle.sendline( "linc:port_down(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800257 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800258 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400259
260 handle = self.handle.before
261
262 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800263 main.log.error( self.name + ": EOF exception" )
264 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400265 main.cleanup()
266 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800267 except Exception:
268 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400269 main.cleanup()
270 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800271
272 def stopLincOEConsole( self ):
273 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800274 This function is only used for packet optical testing
275 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800276 ( CTRL+C ) and kill the linc process
277 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800278 try:
279 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800280 self.handle.sendline( "pgrep -f linc" )
281 self.handle.expect( "linc" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800282 print "stophandle = ", self.handle.before
283 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800284 main.log.error( self.name + ": EOF exception" )
285 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba548f962014-10-21 19:28:43 -0400286
kelvin-onlabedcff052015-01-16 12:53:55 -0800287 def disconnect( self ):
288 """
andrewonlab0980f422014-10-21 21:28:39 -0400289 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800290 ( CTRL+C ) and kill the linc process
291 """
andrewonlab0980f422014-10-21 21:28:39 -0400292 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800293 # Send CTRL+C twice to exit CLI
294 self.handle.send( "\x03" )
295 self.handle.send( "\x03" )
296 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800297 handle1 = self.handle.before
298 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800299 self.handle.sendline( cmd )
300 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800301 handle2 = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800302 main.log.info( "pid's = " + handle2 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800303 cmd = "sudo kill -9 `pgrep -f linc`"
kelvin-onlabedcff052015-01-16 12:53:55 -0800304 self.handle.sendline( cmd )
305 self.handle.expect( "\$" )
shahshreya5a034842015-02-05 11:08:46 -0800306 # Close the ssh connection
307 self.handle.sendline( "" )
308 self.handle.expect( "\$" )
309 self.handle.sendline( "exit" )
310 self.handle.expect( "closed" )
andrewonlab0980f422014-10-21 21:28:39 -0400311 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800312 main.log.error( self.name + ": EOF exception" )
313 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0980f422014-10-21 21:28:39 -0400314 main.cleanup()
315 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800316 except Exception:
317 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0980f422014-10-21 21:28:39 -0400318 main.cleanup()
319 main.exit()
320
andrewonlaba548f962014-10-21 19:28:43 -0400321if __name__ != "__main__":
322 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800323 sys.modules[ __name__ ] = LincOEDriver()