blob: f41b6d2dd5aea68bffc5126c2d339b25d95d2475 [file] [log] [blame]
andrewonlaba548f962014-10-21 19:28:43 -04001#!/usr/bin/env python
2
kelvin-onlabedcff052015-01-16 12:53:55 -08003"""
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07004Copyright 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 Ronquillo23fb2162017-09-15 14:59:57 -070013 ( 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"""
andrewonlaba548f962014-10-21 19:28:43 -040038import pexpect
andrewonlaba548f962014-10-21 19:28:43 -040039import sys
andrewonlaba548f962014-10-21 19:28:43 -040040from drivers.common.cli.emulatordriver import Emulator
andrewonlaba548f962014-10-21 19:28:43 -040041
kelvin-onlabedcff052015-01-16 12:53:55 -080042
43class LincOEDriver( Emulator ):
44
45 """
46 LincOEDriver class will handle all emulator functions
47 """
48 def __init__( self ):
Devin Limdc78e202017-06-09 18:30:07 -070049 super( LincOEDriver, self ).__init__()
andrewonlaba548f962014-10-21 19:28:43 -040050 self.handle = self
kelvin-onlabedcff052015-01-16 12:53:55 -080051 self.wrapped = sys.modules[ __name__ ]
andrewonlaba548f962014-10-21 19:28:43 -040052 self.flag = 0
53
kelvin-onlabedcff052015-01-16 12:53:55 -080054 def connect( self, **connectargs ):
55 """
andrewonlaba548f962014-10-21 19:28:43 -040056 Create ssh handle for Linc-OE cli
kelvin-onlabedcff052015-01-16 12:53:55 -080057 """
andrewonlaba548f962014-10-21 19:28:43 -040058 for key in connectargs:
kelvin-onlabedcff052015-01-16 12:53:55 -080059 vars( self )[ key ] = connectargs[ key ]
60
61 self.name = self.options[ 'name' ]
andrewonlaba548f962014-10-21 19:28:43 -040062 self.handle = \
kelvin-onlabedcff052015-01-16 12:53:55 -080063 super( LincOEDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080064 user_name=self.user_name,
65 ip_address=self.ip_address,
kelvin-onlabedcff052015-01-16 12:53:55 -080066 port=None,
67 pwd=self.pwd )
68
kelvin-onlabd3b64892015-01-20 13:26:24 -080069 self.sshHandle = self.handle
kelvin-onlabedcff052015-01-16 12:53:55 -080070
71 if self.handle:
72 main.log.info( "Handle successfully created" )
andrewonlab52a31e02014-10-22 12:57:19 -040073 self.home = "~/linc-oe"
kelvin-onlabedcff052015-01-16 12:53:55 -080074
75 self.handle.sendline( "cd " + self.home )
76 self.handle.expect( "oe$" )
77
kelvin-onlabedcff052015-01-16 12:53:55 -080078 print "handle = ", self.handle.before
79
80 return main.TRUE
andrewonlaba548f962014-10-21 19:28:43 -040081 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080082 main.log.error( self.name +
83 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080084 self.user_name + "@" + self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -080085 main.log.error( self.name +
86 ": Failed to connect to Linc-OE" )
andrewonlaba548f962014-10-21 19:28:43 -040087 return main.FALSE
88
kelvin-onlabd3b64892015-01-20 13:26:24 -080089 def startConsole( self ):
shahshreyae6c7cf42014-11-26 16:39:01 -080090 import time
kelvin-onlabedcff052015-01-16 12:53:55 -080091 main.log.info(
92 self.name +
93 ": Starting Linc-OE CLI.. This may take a while" )
94 time.sleep( 30 )
95 self.handle.sendline( "sudo ./rel/linc/bin/linc console" )
96 j = self.handle.expect( [ "linc@", pexpect.EOF, pexpect.TIMEOUT ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -080097 startResult = self.handle.before
shahshreyae6c7cf42014-11-26 16:39:01 -080098 if j == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -080099 main.log.info( "Linc-OE CLI started" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800100 return main.TRUE
101 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800102 main.log.error(
103 self.name +
104 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -0800105 self.user_name +
kelvin-onlabedcff052015-01-16 12:53:55 -0800106 "@" +
kelvin-onlab08679eb2015-01-21 16:11:48 -0800107 self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -0800108 main.log.error( self.name +
109 ": Failed to connect to Linc-OE" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800110 return main.FALSE
111
kelvin-onlabedcff052015-01-16 12:53:55 -0800112 def build( self ):
113 """
andrewonlab52a31e02014-10-22 12:57:19 -0400114 Build Linc-OE with the specified settings
kelvin-onlabedcff052015-01-16 12:53:55 -0800115 """
andrewonlab52a31e02014-10-22 12:57:19 -0400116 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800117 self.handle.sendline( "make rel" )
118 i = self.handle.expect( [
andrewonlabc6d1fa62014-10-22 16:28:04 -0400119 "ERROR",
Devin Limdc78e202017-06-09 18:30:07 -0700120 self.prompt ] )
andrewonlabc6d1fa62014-10-22 16:28:04 -0400121
122 if i == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -0800123 self.handle.sendline( "sudo pkill -9 epmd" )
124 self.handle.sendline( "make rel" )
Devin Limdc78e202017-06-09 18:30:07 -0700125 self.handle.expect( self.prompt )
kelvin-onlabedcff052015-01-16 12:53:55 -0800126
andrewonlabc6d1fa62014-10-22 16:28:04 -0400127 handle = self.handle.before
128 return handle
129
130 else:
131 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400132
133 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800134 main.log.error( self.name + ": EOF exception" )
135 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700136 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800137 except Exception:
138 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700139 main.cleanAndExit()
andrewonlab52a31e02014-10-22 12:57:19 -0400140
kelvin-onlabd3b64892015-01-20 13:26:24 -0800141 def setInterfaceUp( self, intfs ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800142 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400143 Specify interface to bring up.
144 When Linc-OE is started, tap interfaces should
145 be created. They must be brought up manually
kelvin-onlabedcff052015-01-16 12:53:55 -0800146 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400147 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800148 self.handle.sendline( "ifconfig " + str( intfs ) + " up" )
149 self.handle.expect( "linc@" )
150
andrewonlabeb08b6f2014-10-21 21:23:15 -0400151 handle = self.handle.before
152
153 return handle
154
155 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800156 main.log.error( self.name + ": EOF exception" )
157 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700158 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800159 except Exception:
160 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700161 main.cleanAndExit()
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 )
Devin Lim44075962017-08-11 10:56:37 -0700176 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800177 except Exception:
178 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700179 main.cleanAndExit()
andrewonlab52a31e02014-10-22 12:57:19 -0400180
kelvin-onlabd3b64892015-01-20 13:26:24 -0800181 def stopSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800182 """
andrewonlab52a31e02014-10-22 12:57:19 -0400183 Stop a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800184 """
andrewonlab52a31e02014-10-22 12:57:19 -0400185 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800186 self.handle.sendline( "linc:stop_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800187 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400188
189 handle = self.handle.before
190
191 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800192 main.log.error( self.name + ": EOF exception" )
193 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700194 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800195 except Exception:
196 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700197 main.cleanAndExit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800198
kelvin-onlabd3b64892015-01-20 13:26:24 -0800199 def getDatapathId( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800200 """
andrewonlab52a31e02014-10-22 12:57:19 -0400201 Get datapath id of a specific switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800202 """
andrewonlab52a31e02014-10-22 12:57:19 -0400203 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800204 self.handle.sendline( "linc_logic:get_datapath_id(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800205 str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800206 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400207
208 handle = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800209
andrewonlab52a31e02014-10-22 12:57:19 -0400210 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800211 main.log.error( self.name + ": EOF exception" )
212 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700213 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800214 except Exception:
215 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700216 main.cleanAndExit()
andrewonlab52a31e02014-10-22 12:57:19 -0400217
shahshreya9ee36d52015-04-03 15:38:23 -0700218 def attachLincOESession( self ):
219 """
220 Since executing opticalTest.py will give you mininet
221 prompt, you would at some point require to get onto
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700222 console of LincOE ( ( linc@onosTestBench )1> ) to execute
shahshreya9ee36d52015-04-03 15:38:23 -0700223 commands like bring a optical port up or down on a ROADM
224 You can attach to console of Linc-OE session by a cmd:
225 sudo ~/linc-oe/rel/linc/bin/linc attach
226 """
227 try:
228 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700229 self.handle.expect( self.prompt )
shahshreya9ee36d52015-04-03 15:38:23 -0700230 self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" )
231 self.handle.expect( ">" )
232 return main.TRUE
233 except pexpect.EOF:
234 main.log.error( self.name + ": EOF exception found" )
235 main.log.error( self.name + ": " + self.handle.before )
236 return main.FALSE
237
kelvin-onlabd3b64892015-01-20 13:26:24 -0800238 def listPorts( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800239 """
andrewonlab52a31e02014-10-22 12:57:19 -0400240 List all ports of a switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800241 """
andrewonlab52a31e02014-10-22 12:57:19 -0400242 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800243 self.handle.sendline( "linc:ports(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800244 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400245
246 handle = self.handle.before
247
248 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800249 main.log.error( self.name + ": EOF exception" )
250 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700251 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800252 except Exception:
253 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700254 main.cleanAndExit()
andrewonlab52a31e02014-10-22 12:57:19 -0400255
kelvin-onlabd3b64892015-01-20 13:26:24 -0800256 def portUp( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800257 """
andrewonlab52a31e02014-10-22 12:57:19 -0400258 Bring port up using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800259 """
andrewonlab52a31e02014-10-22 12:57:19 -0400260 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800261 self.handle.sendline( "linc:port_up(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800262 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800263 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400264
265 handle = self.handle.before
266
267 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800268 main.log.error( self.name + ": EOF exception" )
269 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700270 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800271 except Exception:
272 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700273 main.cleanAndExit()
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 )
Devin Lim44075962017-08-11 10:56:37 -0700289 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800290 except Exception:
291 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700292 main.cleanAndExit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800293
294 def stopLincOEConsole( self ):
295 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800296 This function is only used for packet optical testing
297 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800298 ( CTRL+C ) and kill the linc process
299 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800300 try:
301 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800302 self.handle.sendline( "pgrep -f linc" )
303 self.handle.expect( "linc" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800304 print "stophandle = ", self.handle.before
305 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800306 main.log.error( self.name + ": EOF exception" )
307 main.log.error( self.name + ": " + self.handle.before )
andrewonlaba548f962014-10-21 19:28:43 -0400308
kelvin-onlabedcff052015-01-16 12:53:55 -0800309 def disconnect( self ):
310 """
andrewonlab0980f422014-10-21 21:28:39 -0400311 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800312 ( CTRL+C ) and kill the linc process
313 """
andrewonlab0980f422014-10-21 21:28:39 -0400314 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800315 # Send CTRL+C twice to exit CLI
316 self.handle.send( "\x03" )
317 self.handle.send( "\x03" )
Devin Limdc78e202017-06-09 18:30:07 -0700318 self.handle.expect( self.prompt )
shahshreyae6c7cf42014-11-26 16:39:01 -0800319 handle1 = self.handle.before
320 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800321 self.handle.sendline( cmd )
Devin Limdc78e202017-06-09 18:30:07 -0700322 self.handle.expect( self.prompt )
shahshreyae6c7cf42014-11-26 16:39:01 -0800323 handle2 = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800324 main.log.info( "pid's = " + handle2 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800325 cmd = "sudo kill -9 `pgrep -f linc`"
kelvin-onlabedcff052015-01-16 12:53:55 -0800326 self.handle.sendline( cmd )
Devin Limdc78e202017-06-09 18:30:07 -0700327 self.handle.expect( self.prompt )
shahshreya5a034842015-02-05 11:08:46 -0800328 # Close the ssh connection
329 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700330 self.handle.expect( self.prompt )
shahshreya5a034842015-02-05 11:08:46 -0800331 self.handle.sendline( "exit" )
332 self.handle.expect( "closed" )
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 )
Devin Lim44075962017-08-11 10:56:37 -0700336 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800337 except Exception:
338 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700339 main.cleanAndExit()
andrewonlab0980f422014-10-21 21:28:39 -0400340
andrewonlaba548f962014-10-21 19:28:43 -0400341if __name__ != "__main__":
342 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800343 sys.modules[ __name__ ] = LincOEDriver()