blob: 0f22b288f732c673137bee829eb5ab48972ac5b2 [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
shahshreya9ee36d52015-04-03 15:38:23 -0700210 def attachLincOESession( self ):
211 """
212 Since executing opticalTest.py will give you mininet
213 prompt, you would at some point require to get onto
214 console of LincOE ((linc@onosTestBench)1>) to execute
215 commands like bring a optical port up or down on a ROADM
216 You can attach to console of Linc-OE session by a cmd:
217 sudo ~/linc-oe/rel/linc/bin/linc attach
218 """
219 try:
220 self.handle.sendline( "" )
221 self.handle.expect( "\$" )
222 self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" )
223 self.handle.expect( ">" )
224 return main.TRUE
225 except pexpect.EOF:
226 main.log.error( self.name + ": EOF exception found" )
227 main.log.error( self.name + ": " + self.handle.before )
228 return main.FALSE
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()
Jon Hallfebb1c72015-03-05 13:30:09 -0800245 except Exception:
246 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400247 main.cleanup()
248 main.exit()
249
kelvin-onlabd3b64892015-01-20 13:26:24 -0800250 def portUp( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800251 """
andrewonlab52a31e02014-10-22 12:57:19 -0400252 Bring port up using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800253 """
andrewonlab52a31e02014-10-22 12:57:19 -0400254 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800255 self.handle.sendline( "linc:port_up(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800256 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800257 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400258
259 handle = self.handle.before
260
261 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800262 main.log.error( self.name + ": EOF exception" )
263 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400264 main.cleanup()
265 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800266 except Exception:
267 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400268 main.cleanup()
269 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800270
kelvin-onlabd3b64892015-01-20 13:26:24 -0800271 def portDown( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800272 """
andrewonlab52a31e02014-10-22 12:57:19 -0400273 Bring port down using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800274 """
andrewonlab52a31e02014-10-22 12:57:19 -0400275 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800276 self.handle.sendline( "linc:port_down(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800277 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800278 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400279
280 handle = self.handle.before
281
282 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800283 main.log.error( self.name + ": EOF exception" )
284 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400285 main.cleanup()
286 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800287 except Exception:
288 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400289 main.cleanup()
290 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800291
292 def stopLincOEConsole( self ):
293 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800294 This function is only used for packet optical testing
295 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800296 ( CTRL+C ) and kill the linc process
297 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800298 try:
299 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800300 self.handle.sendline( "pgrep -f linc" )
301 self.handle.expect( "linc" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800302 print "stophandle = ", self.handle.before
303 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800304 main.log.error( self.name + ": EOF exception" )
305 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba548f962014-10-21 19:28:43 -0400306
kelvin-onlabedcff052015-01-16 12:53:55 -0800307 def disconnect( self ):
308 """
andrewonlab0980f422014-10-21 21:28:39 -0400309 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800310 ( CTRL+C ) and kill the linc process
311 """
andrewonlab0980f422014-10-21 21:28:39 -0400312 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800313 # Send CTRL+C twice to exit CLI
314 self.handle.send( "\x03" )
315 self.handle.send( "\x03" )
316 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800317 handle1 = self.handle.before
318 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800319 self.handle.sendline( cmd )
320 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800321 handle2 = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800322 main.log.info( "pid's = " + handle2 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800323 cmd = "sudo kill -9 `pgrep -f linc`"
kelvin-onlabedcff052015-01-16 12:53:55 -0800324 self.handle.sendline( cmd )
325 self.handle.expect( "\$" )
shahshreya5a034842015-02-05 11:08:46 -0800326 # Close the ssh connection
327 self.handle.sendline( "" )
328 self.handle.expect( "\$" )
329 self.handle.sendline( "exit" )
330 self.handle.expect( "closed" )
andrewonlab0980f422014-10-21 21:28:39 -0400331 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800332 main.log.error( self.name + ": EOF exception" )
333 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0980f422014-10-21 21:28:39 -0400334 main.cleanup()
335 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800336 except Exception:
337 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0980f422014-10-21 21:28:39 -0400338 main.cleanup()
339 main.exit()
340
andrewonlaba548f962014-10-21 19:28:43 -0400341if __name__ != "__main__":
342 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800343 sys.modules[ __name__ ] = LincOEDriver()