blob: d5e114a5027974c0a056f4f33d6c7948a81d0098 [file] [log] [blame]
andrewonlaba548f962014-10-21 19:28:43 -04001#!/usr/bin/env python
2
kelvin-onlabedcff052015-01-16 12:53:55 -08003"""
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004Copyright 2014 Open Networking Foundation (ONF)
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07005
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
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000013 (at your option) any later version.
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -070014
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"""
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000038
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 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +000059
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 )
Devin Lim44075962017-08-11 10:56:37 -0700138 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800139 except Exception:
140 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700141 main.cleanAndExit()
andrewonlab52a31e02014-10-22 12:57:19 -0400142
kelvin-onlabd3b64892015-01-20 13:26:24 -0800143 def setInterfaceUp( self, intfs ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800144 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400145 Specify interface to bring up.
146 When Linc-OE is started, tap interfaces should
147 be created. They must be brought up manually
kelvin-onlabedcff052015-01-16 12:53:55 -0800148 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400149 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800150 self.handle.sendline( "ifconfig " + str( intfs ) + " up" )
151 self.handle.expect( "linc@" )
152
andrewonlabeb08b6f2014-10-21 21:23:15 -0400153 handle = self.handle.before
154
155 return handle
156
157 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800158 main.log.error( self.name + ": EOF exception" )
159 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700160 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800161 except Exception:
162 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700163 main.cleanAndExit()
andrewonlaba548f962014-10-21 19:28:43 -0400164
kelvin-onlabd3b64892015-01-20 13:26:24 -0800165 def startSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800166 """
andrewonlab52a31e02014-10-22 12:57:19 -0400167 Start a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800168 """
andrewonlab52a31e02014-10-22 12:57:19 -0400169 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800170 self.handle.sendline( "linc:start_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800171 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400172
173 handle = self.handle.before
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 )
Devin Lim44075962017-08-11 10:56:37 -0700178 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800179 except Exception:
180 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700181 main.cleanAndExit()
andrewonlab52a31e02014-10-22 12:57:19 -0400182
kelvin-onlabd3b64892015-01-20 13:26:24 -0800183 def stopSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800184 """
andrewonlab52a31e02014-10-22 12:57:19 -0400185 Stop a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800186 """
andrewonlab52a31e02014-10-22 12:57:19 -0400187 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800188 self.handle.sendline( "linc:stop_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800189 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400190
191 handle = self.handle.before
192
193 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800194 main.log.error( self.name + ": EOF exception" )
195 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700196 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800197 except Exception:
198 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700199 main.cleanAndExit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800200
kelvin-onlabd3b64892015-01-20 13:26:24 -0800201 def getDatapathId( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800202 """
andrewonlab52a31e02014-10-22 12:57:19 -0400203 Get datapath id of a specific switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800204 """
andrewonlab52a31e02014-10-22 12:57:19 -0400205 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800206 self.handle.sendline( "linc_logic:get_datapath_id(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800207 str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800208 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400209
210 handle = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800211
andrewonlab52a31e02014-10-22 12:57:19 -0400212 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800213 main.log.error( self.name + ": EOF exception" )
214 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700215 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800216 except Exception:
217 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700218 main.cleanAndExit()
andrewonlab52a31e02014-10-22 12:57:19 -0400219
shahshreya9ee36d52015-04-03 15:38:23 -0700220 def attachLincOESession( self ):
221 """
222 Since executing opticalTest.py will give you mininet
223 prompt, you would at some point require to get onto
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000224 console of LincOE ((linc@onosTestBench)1>) to execute
shahshreya9ee36d52015-04-03 15:38:23 -0700225 commands like bring a optical port up or down on a ROADM
226 You can attach to console of Linc-OE session by a cmd:
227 sudo ~/linc-oe/rel/linc/bin/linc attach
228 """
229 try:
230 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700231 self.handle.expect( self.prompt )
shahshreya9ee36d52015-04-03 15:38:23 -0700232 self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" )
233 self.handle.expect( ">" )
234 return main.TRUE
235 except pexpect.EOF:
236 main.log.error( self.name + ": EOF exception found" )
237 main.log.error( self.name + ": " + self.handle.before )
238 return main.FALSE
239
kelvin-onlabd3b64892015-01-20 13:26:24 -0800240 def listPorts( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800241 """
andrewonlab52a31e02014-10-22 12:57:19 -0400242 List all ports of a switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800243 """
andrewonlab52a31e02014-10-22 12:57:19 -0400244 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800245 self.handle.sendline( "linc:ports(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800246 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400247
248 handle = self.handle.before
249
250 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800251 main.log.error( self.name + ": EOF exception" )
252 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700253 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800254 except Exception:
255 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700256 main.cleanAndExit()
andrewonlab52a31e02014-10-22 12:57:19 -0400257
kelvin-onlabd3b64892015-01-20 13:26:24 -0800258 def portUp( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800259 """
andrewonlab52a31e02014-10-22 12:57:19 -0400260 Bring port up using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800261 """
andrewonlab52a31e02014-10-22 12:57:19 -0400262 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800263 self.handle.sendline( "linc:port_up(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800264 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800265 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400266
267 handle = self.handle.before
268
269 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800270 main.log.error( self.name + ": EOF exception" )
271 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700272 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800273 except Exception:
274 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700275 main.cleanAndExit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800276
kelvin-onlabd3b64892015-01-20 13:26:24 -0800277 def portDown( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800278 """
andrewonlab52a31e02014-10-22 12:57:19 -0400279 Bring port down using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800280 """
andrewonlab52a31e02014-10-22 12:57:19 -0400281 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800282 self.handle.sendline( "linc:port_down(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800283 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800284 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400285
286 handle = self.handle.before
287
288 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800289 main.log.error( self.name + ": EOF exception" )
290 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700291 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800292 except Exception:
293 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700294 main.cleanAndExit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800295
296 def stopLincOEConsole( self ):
297 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800298 This function is only used for packet optical testing
299 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800300 ( CTRL+C ) and kill the linc process
301 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800302 try:
303 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800304 self.handle.sendline( "pgrep -f linc" )
305 self.handle.expect( "linc" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800306 print "stophandle = ", self.handle.before
307 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800308 main.log.error( self.name + ": EOF exception" )
309 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba548f962014-10-21 19:28:43 -0400310
kelvin-onlabedcff052015-01-16 12:53:55 -0800311 def disconnect( self ):
312 """
andrewonlab0980f422014-10-21 21:28:39 -0400313 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800314 ( CTRL+C ) and kill the linc process
315 """
andrewonlab0980f422014-10-21 21:28:39 -0400316 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800317 # Send CTRL+C twice to exit CLI
318 self.handle.send( "\x03" )
319 self.handle.send( "\x03" )
Devin Limdc78e202017-06-09 18:30:07 -0700320 self.handle.expect( self.prompt )
shahshreyae6c7cf42014-11-26 16:39:01 -0800321 handle1 = self.handle.before
322 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800323 self.handle.sendline( cmd )
Devin Limdc78e202017-06-09 18:30:07 -0700324 self.handle.expect( self.prompt )
shahshreyae6c7cf42014-11-26 16:39:01 -0800325 handle2 = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800326 main.log.info( "pid's = " + handle2 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800327 cmd = "sudo kill -9 `pgrep -f linc`"
kelvin-onlabedcff052015-01-16 12:53:55 -0800328 self.handle.sendline( cmd )
Devin Limdc78e202017-06-09 18:30:07 -0700329 self.handle.expect( self.prompt )
shahshreya5a034842015-02-05 11:08:46 -0800330 # Close the ssh connection
331 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700332 self.handle.expect( self.prompt )
shahshreya5a034842015-02-05 11:08:46 -0800333 self.handle.sendline( "exit" )
334 self.handle.expect( "closed" )
andrewonlab0980f422014-10-21 21:28:39 -0400335 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800336 main.log.error( self.name + ": EOF exception" )
337 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700338 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800339 except Exception:
340 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700341 main.cleanAndExit()
andrewonlab0980f422014-10-21 21:28:39 -0400342
andrewonlaba548f962014-10-21 19:28:43 -0400343if __name__ != "__main__":
344 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800345 sys.modules[ __name__ ] = LincOEDriver()