blob: dfc75113b8e98142bfcff4abce3df37ae8f3825b [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
Jeremy Songsterae01bba2016-07-11 15:39:17 -070016Modified 2015 by ON.Lab
17
18Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
19the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
20or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
kelvin-onlabedcff052015-01-16 12:53:55 -080021"""
Jon Hallfebb1c72015-03-05 13:30:09 -080022
andrewonlaba548f962014-10-21 19:28:43 -040023import pexpect
andrewonlaba548f962014-10-21 19:28:43 -040024import sys
andrewonlaba548f962014-10-21 19:28:43 -040025from drivers.common.cli.emulatordriver import Emulator
andrewonlaba548f962014-10-21 19:28:43 -040026
kelvin-onlabedcff052015-01-16 12:53:55 -080027
28class LincOEDriver( Emulator ):
29
30 """
31 LincOEDriver class will handle all emulator functions
32 """
33 def __init__( self ):
Devin Limdc78e202017-06-09 18:30:07 -070034 super( LincOEDriver, self ).__init__()
andrewonlaba548f962014-10-21 19:28:43 -040035 self.handle = self
kelvin-onlabedcff052015-01-16 12:53:55 -080036 self.wrapped = sys.modules[ __name__ ]
andrewonlaba548f962014-10-21 19:28:43 -040037 self.flag = 0
38
kelvin-onlabedcff052015-01-16 12:53:55 -080039 def connect( self, **connectargs ):
40 """
andrewonlaba548f962014-10-21 19:28:43 -040041 Create ssh handle for Linc-OE cli
kelvin-onlabedcff052015-01-16 12:53:55 -080042 """
andrewonlaba85a7762014-10-22 18:05:52 -040043
andrewonlaba548f962014-10-21 19:28:43 -040044 for key in connectargs:
kelvin-onlabedcff052015-01-16 12:53:55 -080045 vars( self )[ key ] = connectargs[ key ]
46
47 self.name = self.options[ 'name' ]
andrewonlaba548f962014-10-21 19:28:43 -040048 self.handle = \
kelvin-onlabedcff052015-01-16 12:53:55 -080049 super( LincOEDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080050 user_name=self.user_name,
51 ip_address=self.ip_address,
kelvin-onlabedcff052015-01-16 12:53:55 -080052 port=None,
53 pwd=self.pwd )
54
kelvin-onlabd3b64892015-01-20 13:26:24 -080055 self.sshHandle = self.handle
kelvin-onlabedcff052015-01-16 12:53:55 -080056
57 if self.handle:
58 main.log.info( "Handle successfully created" )
andrewonlab52a31e02014-10-22 12:57:19 -040059 self.home = "~/linc-oe"
kelvin-onlabedcff052015-01-16 12:53:55 -080060
61 self.handle.sendline( "cd " + self.home )
62 self.handle.expect( "oe$" )
63
kelvin-onlabedcff052015-01-16 12:53:55 -080064 print "handle = ", self.handle.before
65
66 return main.TRUE
andrewonlaba548f962014-10-21 19:28:43 -040067 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080068 main.log.error( self.name +
69 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080070 self.user_name + "@" + self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -080071 main.log.error( self.name +
72 ": Failed to connect to Linc-OE" )
andrewonlaba548f962014-10-21 19:28:43 -040073 return main.FALSE
74
kelvin-onlabd3b64892015-01-20 13:26:24 -080075 def startConsole( self ):
shahshreyae6c7cf42014-11-26 16:39:01 -080076 import time
kelvin-onlabedcff052015-01-16 12:53:55 -080077 main.log.info(
78 self.name +
79 ": Starting Linc-OE CLI.. This may take a while" )
80 time.sleep( 30 )
81 self.handle.sendline( "sudo ./rel/linc/bin/linc console" )
82 j = self.handle.expect( [ "linc@", pexpect.EOF, pexpect.TIMEOUT ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -080083 startResult = self.handle.before
shahshreyae6c7cf42014-11-26 16:39:01 -080084 if j == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -080085 main.log.info( "Linc-OE CLI started" )
shahshreyae6c7cf42014-11-26 16:39:01 -080086 return main.TRUE
87 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080088 main.log.error(
89 self.name +
90 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080091 self.user_name +
kelvin-onlabedcff052015-01-16 12:53:55 -080092 "@" +
kelvin-onlab08679eb2015-01-21 16:11:48 -080093 self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -080094 main.log.error( self.name +
95 ": Failed to connect to Linc-OE" )
shahshreyae6c7cf42014-11-26 16:39:01 -080096 return main.FALSE
97
kelvin-onlabedcff052015-01-16 12:53:55 -080098 def build( self ):
99 """
andrewonlab52a31e02014-10-22 12:57:19 -0400100 Build Linc-OE with the specified settings
kelvin-onlabedcff052015-01-16 12:53:55 -0800101 """
andrewonlab52a31e02014-10-22 12:57:19 -0400102 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800103 self.handle.sendline( "make rel" )
104 i = self.handle.expect( [
andrewonlabc6d1fa62014-10-22 16:28:04 -0400105 "ERROR",
Devin Limdc78e202017-06-09 18:30:07 -0700106 self.prompt ] )
andrewonlabc6d1fa62014-10-22 16:28:04 -0400107
108 if i == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -0800109 self.handle.sendline( "sudo pkill -9 epmd" )
110 self.handle.sendline( "make rel" )
Devin Limdc78e202017-06-09 18:30:07 -0700111 self.handle.expect( self.prompt )
kelvin-onlabedcff052015-01-16 12:53:55 -0800112
andrewonlabc6d1fa62014-10-22 16:28:04 -0400113 handle = self.handle.before
114 return handle
115
116 else:
117 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400118
119 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800120 main.log.error( self.name + ": EOF exception" )
121 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400122 main.cleanup()
123 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800124 except Exception:
125 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400126 main.cleanup()
127 main.exit()
128
kelvin-onlabd3b64892015-01-20 13:26:24 -0800129 def setInterfaceUp( self, intfs ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800130 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400131 Specify interface to bring up.
132 When Linc-OE is started, tap interfaces should
133 be created. They must be brought up manually
kelvin-onlabedcff052015-01-16 12:53:55 -0800134 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400135 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800136 self.handle.sendline( "ifconfig " + str( intfs ) + " up" )
137 self.handle.expect( "linc@" )
138
andrewonlabeb08b6f2014-10-21 21:23:15 -0400139 handle = self.handle.before
140
141 return handle
142
143 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800144 main.log.error( self.name + ": EOF exception" )
145 main.log.error( self.name + ": " + self.handle.before )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400146 main.cleanup()
147 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800148 except Exception:
149 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400150 main.cleanup()
151 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400152
kelvin-onlabd3b64892015-01-20 13:26:24 -0800153 def startSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800154 """
andrewonlab52a31e02014-10-22 12:57:19 -0400155 Start a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800156 """
andrewonlab52a31e02014-10-22 12:57:19 -0400157 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800158 self.handle.sendline( "linc:start_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800159 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400160
161 handle = self.handle.before
162
163 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800164 main.log.error( self.name + ": EOF exception" )
165 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400166 main.cleanup()
167 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800168 except Exception:
169 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400170 main.cleanup()
171 main.exit()
172
kelvin-onlabd3b64892015-01-20 13:26:24 -0800173 def stopSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800174 """
andrewonlab52a31e02014-10-22 12:57:19 -0400175 Stop a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800176 """
andrewonlab52a31e02014-10-22 12:57:19 -0400177 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800178 self.handle.sendline( "linc:stop_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800179 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400180
181 handle = self.handle.before
182
183 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800184 main.log.error( self.name + ": EOF exception" )
185 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400186 main.cleanup()
187 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800188 except Exception:
189 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400190 main.cleanup()
191 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800192
kelvin-onlabd3b64892015-01-20 13:26:24 -0800193 def getDatapathId( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800194 """
andrewonlab52a31e02014-10-22 12:57:19 -0400195 Get datapath id of a specific switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800196 """
andrewonlab52a31e02014-10-22 12:57:19 -0400197 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800198 self.handle.sendline( "linc_logic:get_datapath_id(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800199 str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800200 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400201
202 handle = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800203
andrewonlab52a31e02014-10-22 12:57:19 -0400204 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800205 main.log.error( self.name + ": EOF exception" )
206 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400207 main.cleanup()
208 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800209 except Exception:
210 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400211 main.cleanup()
212 main.exit()
213
shahshreya9ee36d52015-04-03 15:38:23 -0700214 def attachLincOESession( self ):
215 """
216 Since executing opticalTest.py will give you mininet
217 prompt, you would at some point require to get onto
218 console of LincOE ((linc@onosTestBench)1>) to execute
219 commands like bring a optical port up or down on a ROADM
220 You can attach to console of Linc-OE session by a cmd:
221 sudo ~/linc-oe/rel/linc/bin/linc attach
222 """
223 try:
224 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700225 self.handle.expect( self.prompt )
shahshreya9ee36d52015-04-03 15:38:23 -0700226 self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" )
227 self.handle.expect( ">" )
228 return main.TRUE
229 except pexpect.EOF:
230 main.log.error( self.name + ": EOF exception found" )
231 main.log.error( self.name + ": " + self.handle.before )
232 return main.FALSE
233
kelvin-onlabd3b64892015-01-20 13:26:24 -0800234 def listPorts( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800235 """
andrewonlab52a31e02014-10-22 12:57:19 -0400236 List all ports of a switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800237 """
andrewonlab52a31e02014-10-22 12:57:19 -0400238 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800239 self.handle.sendline( "linc:ports(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800240 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400241
242 handle = self.handle.before
243
244 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800245 main.log.error( self.name + ": EOF exception" )
246 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400247 main.cleanup()
248 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800249 except Exception:
250 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400251 main.cleanup()
252 main.exit()
253
kelvin-onlabd3b64892015-01-20 13:26:24 -0800254 def portUp( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800255 """
andrewonlab52a31e02014-10-22 12:57:19 -0400256 Bring port up using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800257 """
andrewonlab52a31e02014-10-22 12:57:19 -0400258 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800259 self.handle.sendline( "linc:port_up(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800260 str( swId ) + ", " + str( ptId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800261 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400262
263 handle = self.handle.before
264
265 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800266 main.log.error( self.name + ": EOF exception" )
267 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400268 main.cleanup()
269 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800270 except Exception:
271 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400272 main.cleanup()
273 main.exit()
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 )
andrewonlab52a31e02014-10-22 12:57:19 -0400289 main.cleanup()
290 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800291 except Exception:
292 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400293 main.cleanup()
294 main.exit()
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 )
andrewonlab0980f422014-10-21 21:28:39 -0400338 main.cleanup()
339 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800340 except Exception:
341 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0980f422014-10-21 21:28:39 -0400342 main.cleanup()
343 main.exit()
344
andrewonlaba548f962014-10-21 19:28:43 -0400345if __name__ != "__main__":
346 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800347 sys.modules[ __name__ ] = LincOEDriver()