blob: ab6a7bc8d21e3867fa8447aba95a4c19f229a64b [file] [log] [blame]
andrewonlaba548f962014-10-21 19:28:43 -04001#!/usr/bin/env python
2
kelvin-onlabedcff052015-01-16 12:53:55 -08003"""
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07004Copyright 2014 Open Networking Foundation (ONF)
5
6Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
9
10 TestON is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
13 (at your option) any later version.
14
15 TestON is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with TestON. If not, see <http://www.gnu.org/licenses/>.
22
23
24
andrewonlaba548f962014-10-21 19:28:43 -040025This driver handles the optical switch emulator linc-oe.
26
27Please follow the coding style demonstrated by existing
28functions and document properly.
29
30If you are a contributor to the driver, please
31list your email here for future contact:
32
33 andrew@onlab.us
shahshreyae6c7cf42014-11-26 16:39:01 -080034 shreya@onlab.us
andrewonlaba548f962014-10-21 19:28:43 -040035
36OCT 20 2014
kelvin-onlabedcff052015-01-16 12:53:55 -080037"""
Jon Hallfebb1c72015-03-05 13:30:09 -080038
andrewonlaba548f962014-10-21 19:28:43 -040039import pexpect
andrewonlaba548f962014-10-21 19:28:43 -040040import sys
andrewonlaba548f962014-10-21 19:28:43 -040041from drivers.common.cli.emulatordriver import Emulator
andrewonlaba548f962014-10-21 19:28:43 -040042
kelvin-onlabedcff052015-01-16 12:53:55 -080043
44class LincOEDriver( Emulator ):
45
46 """
47 LincOEDriver class will handle all emulator functions
48 """
49 def __init__( self ):
Devin Limdc78e202017-06-09 18:30:07 -070050 super( LincOEDriver, self ).__init__()
andrewonlaba548f962014-10-21 19:28:43 -040051 self.handle = self
kelvin-onlabedcff052015-01-16 12:53:55 -080052 self.wrapped = sys.modules[ __name__ ]
andrewonlaba548f962014-10-21 19:28:43 -040053 self.flag = 0
54
kelvin-onlabedcff052015-01-16 12:53:55 -080055 def connect( self, **connectargs ):
56 """
andrewonlaba548f962014-10-21 19:28:43 -040057 Create ssh handle for Linc-OE cli
kelvin-onlabedcff052015-01-16 12:53:55 -080058 """
andrewonlaba85a7762014-10-22 18:05:52 -040059
andrewonlaba548f962014-10-21 19:28:43 -040060 for key in connectargs:
kelvin-onlabedcff052015-01-16 12:53:55 -080061 vars( self )[ key ] = connectargs[ key ]
62
63 self.name = self.options[ 'name' ]
andrewonlaba548f962014-10-21 19:28:43 -040064 self.handle = \
kelvin-onlabedcff052015-01-16 12:53:55 -080065 super( LincOEDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080066 user_name=self.user_name,
67 ip_address=self.ip_address,
kelvin-onlabedcff052015-01-16 12:53:55 -080068 port=None,
69 pwd=self.pwd )
70
kelvin-onlabd3b64892015-01-20 13:26:24 -080071 self.sshHandle = self.handle
kelvin-onlabedcff052015-01-16 12:53:55 -080072
73 if self.handle:
74 main.log.info( "Handle successfully created" )
andrewonlab52a31e02014-10-22 12:57:19 -040075 self.home = "~/linc-oe"
kelvin-onlabedcff052015-01-16 12:53:55 -080076
77 self.handle.sendline( "cd " + self.home )
78 self.handle.expect( "oe$" )
79
kelvin-onlabedcff052015-01-16 12:53:55 -080080 print "handle = ", self.handle.before
81
82 return main.TRUE
andrewonlaba548f962014-10-21 19:28:43 -040083 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080084 main.log.error( self.name +
85 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080086 self.user_name + "@" + self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -080087 main.log.error( self.name +
88 ": Failed to connect to Linc-OE" )
andrewonlaba548f962014-10-21 19:28:43 -040089 return main.FALSE
90
kelvin-onlabd3b64892015-01-20 13:26:24 -080091 def startConsole( self ):
shahshreyae6c7cf42014-11-26 16:39:01 -080092 import time
kelvin-onlabedcff052015-01-16 12:53:55 -080093 main.log.info(
94 self.name +
95 ": Starting Linc-OE CLI.. This may take a while" )
96 time.sleep( 30 )
97 self.handle.sendline( "sudo ./rel/linc/bin/linc console" )
98 j = self.handle.expect( [ "linc@", pexpect.EOF, pexpect.TIMEOUT ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -080099 startResult = self.handle.before
shahshreyae6c7cf42014-11-26 16:39:01 -0800100 if j == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -0800101 main.log.info( "Linc-OE CLI started" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800102 return main.TRUE
103 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800104 main.log.error(
105 self.name +
106 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -0800107 self.user_name +
kelvin-onlabedcff052015-01-16 12:53:55 -0800108 "@" +
kelvin-onlab08679eb2015-01-21 16:11:48 -0800109 self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -0800110 main.log.error( self.name +
111 ": Failed to connect to Linc-OE" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800112 return main.FALSE
113
kelvin-onlabedcff052015-01-16 12:53:55 -0800114 def build( self ):
115 """
andrewonlab52a31e02014-10-22 12:57:19 -0400116 Build Linc-OE with the specified settings
kelvin-onlabedcff052015-01-16 12:53:55 -0800117 """
andrewonlab52a31e02014-10-22 12:57:19 -0400118 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800119 self.handle.sendline( "make rel" )
120 i = self.handle.expect( [
andrewonlabc6d1fa62014-10-22 16:28:04 -0400121 "ERROR",
Devin Limdc78e202017-06-09 18:30:07 -0700122 self.prompt ] )
andrewonlabc6d1fa62014-10-22 16:28:04 -0400123
124 if i == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -0800125 self.handle.sendline( "sudo pkill -9 epmd" )
126 self.handle.sendline( "make rel" )
Devin Limdc78e202017-06-09 18:30:07 -0700127 self.handle.expect( self.prompt )
kelvin-onlabedcff052015-01-16 12:53:55 -0800128
andrewonlabc6d1fa62014-10-22 16:28:04 -0400129 handle = self.handle.before
130 return handle
131
132 else:
133 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400134
135 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800136 main.log.error( self.name + ": EOF exception" )
137 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400138 main.cleanup()
139 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800140 except Exception:
141 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400142 main.cleanup()
143 main.exit()
144
kelvin-onlabd3b64892015-01-20 13:26:24 -0800145 def setInterfaceUp( self, intfs ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800146 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400147 Specify interface to bring up.
148 When Linc-OE is started, tap interfaces should
149 be created. They must be brought up manually
kelvin-onlabedcff052015-01-16 12:53:55 -0800150 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400151 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800152 self.handle.sendline( "ifconfig " + str( intfs ) + " up" )
153 self.handle.expect( "linc@" )
154
andrewonlabeb08b6f2014-10-21 21:23:15 -0400155 handle = self.handle.before
156
157 return handle
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 )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400162 main.cleanup()
163 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800164 except Exception:
165 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400166 main.cleanup()
167 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400168
kelvin-onlabd3b64892015-01-20 13:26:24 -0800169 def startSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800170 """
andrewonlab52a31e02014-10-22 12:57:19 -0400171 Start 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:start_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()
188
kelvin-onlabd3b64892015-01-20 13:26:24 -0800189 def stopSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800190 """
andrewonlab52a31e02014-10-22 12:57:19 -0400191 Stop a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800192 """
andrewonlab52a31e02014-10-22 12:57:19 -0400193 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800194 self.handle.sendline( "linc:stop_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800195 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400196
197 handle = self.handle.before
198
199 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800200 main.log.error( self.name + ": EOF exception" )
201 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400202 main.cleanup()
203 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800204 except Exception:
205 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400206 main.cleanup()
207 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800208
kelvin-onlabd3b64892015-01-20 13:26:24 -0800209 def getDatapathId( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800210 """
andrewonlab52a31e02014-10-22 12:57:19 -0400211 Get datapath id of a specific switch by 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_logic:get_datapath_id(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800215 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
kelvin-onlabedcff052015-01-16 12:53:55 -0800219
andrewonlab52a31e02014-10-22 12:57:19 -0400220 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
shahshreya9ee36d52015-04-03 15:38:23 -0700230 def attachLincOESession( self ):
231 """
232 Since executing opticalTest.py will give you mininet
233 prompt, you would at some point require to get onto
234 console of LincOE ((linc@onosTestBench)1>) to execute
235 commands like bring a optical port up or down on a ROADM
236 You can attach to console of Linc-OE session by a cmd:
237 sudo ~/linc-oe/rel/linc/bin/linc attach
238 """
239 try:
240 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700241 self.handle.expect( self.prompt )
shahshreya9ee36d52015-04-03 15:38:23 -0700242 self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" )
243 self.handle.expect( ">" )
244 return main.TRUE
245 except pexpect.EOF:
246 main.log.error( self.name + ": EOF exception found" )
247 main.log.error( self.name + ": " + self.handle.before )
248 return main.FALSE
249
kelvin-onlabd3b64892015-01-20 13:26:24 -0800250 def listPorts( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800251 """
andrewonlab52a31e02014-10-22 12:57:19 -0400252 List all ports of a switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800253 """
andrewonlab52a31e02014-10-22 12:57:19 -0400254 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800255 self.handle.sendline( "linc:ports(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800256 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400257
258 handle = self.handle.before
259
260 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800261 main.log.error( self.name + ": EOF exception" )
262 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400263 main.cleanup()
264 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800265 except Exception:
266 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400267 main.cleanup()
268 main.exit()
269
kelvin-onlabd3b64892015-01-20 13:26:24 -0800270 def portUp( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800271 """
andrewonlab52a31e02014-10-22 12:57:19 -0400272 Bring port up using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800273 """
andrewonlab52a31e02014-10-22 12:57:19 -0400274 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800275 self.handle.sendline( "linc:port_up(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800276 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800277 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400278
279 handle = self.handle.before
280
281 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800282 main.log.error( self.name + ": EOF exception" )
283 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400284 main.cleanup()
285 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800286 except Exception:
287 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400288 main.cleanup()
289 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800290
kelvin-onlabd3b64892015-01-20 13:26:24 -0800291 def portDown( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800292 """
andrewonlab52a31e02014-10-22 12:57:19 -0400293 Bring port down using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800294 """
andrewonlab52a31e02014-10-22 12:57:19 -0400295 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800296 self.handle.sendline( "linc:port_down(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800297 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800298 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400299
300 handle = self.handle.before
301
302 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800303 main.log.error( self.name + ": EOF exception" )
304 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400305 main.cleanup()
306 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800307 except Exception:
308 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400309 main.cleanup()
310 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800311
312 def stopLincOEConsole( self ):
313 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800314 This function is only used for packet optical testing
315 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800316 ( CTRL+C ) and kill the linc process
317 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800318 try:
319 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800320 self.handle.sendline( "pgrep -f linc" )
321 self.handle.expect( "linc" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800322 print "stophandle = ", self.handle.before
323 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800324 main.log.error( self.name + ": EOF exception" )
325 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba548f962014-10-21 19:28:43 -0400326
kelvin-onlabedcff052015-01-16 12:53:55 -0800327 def disconnect( self ):
328 """
andrewonlab0980f422014-10-21 21:28:39 -0400329 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800330 ( CTRL+C ) and kill the linc process
331 """
andrewonlab0980f422014-10-21 21:28:39 -0400332 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800333 # Send CTRL+C twice to exit CLI
334 self.handle.send( "\x03" )
335 self.handle.send( "\x03" )
Devin Limdc78e202017-06-09 18:30:07 -0700336 self.handle.expect( self.prompt )
shahshreyae6c7cf42014-11-26 16:39:01 -0800337 handle1 = self.handle.before
338 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800339 self.handle.sendline( cmd )
Devin Limdc78e202017-06-09 18:30:07 -0700340 self.handle.expect( self.prompt )
shahshreyae6c7cf42014-11-26 16:39:01 -0800341 handle2 = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800342 main.log.info( "pid's = " + handle2 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800343 cmd = "sudo kill -9 `pgrep -f linc`"
kelvin-onlabedcff052015-01-16 12:53:55 -0800344 self.handle.sendline( cmd )
Devin Limdc78e202017-06-09 18:30:07 -0700345 self.handle.expect( self.prompt )
shahshreya5a034842015-02-05 11:08:46 -0800346 # Close the ssh connection
347 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700348 self.handle.expect( self.prompt )
shahshreya5a034842015-02-05 11:08:46 -0800349 self.handle.sendline( "exit" )
350 self.handle.expect( "closed" )
andrewonlab0980f422014-10-21 21:28:39 -0400351 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800352 main.log.error( self.name + ": EOF exception" )
353 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0980f422014-10-21 21:28:39 -0400354 main.cleanup()
355 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800356 except Exception:
357 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0980f422014-10-21 21:28:39 -0400358 main.cleanup()
359 main.exit()
360
andrewonlaba548f962014-10-21 19:28:43 -0400361if __name__ != "__main__":
362 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800363 sys.modules[ __name__ ] = LincOEDriver()