blob: ae3eef9fdd569775edca0fcb495a274abd64c306 [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
andrewonlaba548f962014-10-21 19:28:43 -040020from drivers.common.cli.emulatordriver import Emulator
andrewonlaba548f962014-10-21 19:28:43 -040021
kelvin-onlabedcff052015-01-16 12:53:55 -080022
23class LincOEDriver( Emulator ):
24
25 """
26 LincOEDriver class will handle all emulator functions
27 """
28 def __init__( self ):
29 super( Emulator, self ).__init__()
andrewonlaba548f962014-10-21 19:28:43 -040030 self.handle = self
kelvin-onlabedcff052015-01-16 12:53:55 -080031 self.wrapped = sys.modules[ __name__ ]
andrewonlaba548f962014-10-21 19:28:43 -040032 self.flag = 0
33
kelvin-onlabedcff052015-01-16 12:53:55 -080034 def connect( self, **connectargs ):
35 """
andrewonlaba548f962014-10-21 19:28:43 -040036 Create ssh handle for Linc-OE cli
kelvin-onlabedcff052015-01-16 12:53:55 -080037 """
andrewonlaba85a7762014-10-22 18:05:52 -040038
andrewonlaba548f962014-10-21 19:28:43 -040039 for key in connectargs:
kelvin-onlabedcff052015-01-16 12:53:55 -080040 vars( self )[ key ] = connectargs[ key ]
41
42 self.name = self.options[ 'name' ]
andrewonlaba548f962014-10-21 19:28:43 -040043 self.handle = \
kelvin-onlabedcff052015-01-16 12:53:55 -080044 super( LincOEDriver, self ).connect(
kelvin-onlab08679eb2015-01-21 16:11:48 -080045 user_name=self.user_name,
46 ip_address=self.ip_address,
kelvin-onlabedcff052015-01-16 12:53:55 -080047 port=None,
48 pwd=self.pwd )
49
kelvin-onlabd3b64892015-01-20 13:26:24 -080050 self.sshHandle = self.handle
kelvin-onlabedcff052015-01-16 12:53:55 -080051
52 if self.handle:
53 main.log.info( "Handle successfully created" )
andrewonlab52a31e02014-10-22 12:57:19 -040054 self.home = "~/linc-oe"
kelvin-onlabedcff052015-01-16 12:53:55 -080055
56 self.handle.sendline( "cd " + self.home )
57 self.handle.expect( "oe$" )
58
kelvin-onlabedcff052015-01-16 12:53:55 -080059 print "handle = ", self.handle.before
60
61 return main.TRUE
andrewonlaba548f962014-10-21 19:28:43 -040062 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080063 main.log.error( self.name +
64 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080065 self.user_name + "@" + self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -080066 main.log.error( self.name +
67 ": Failed to connect to Linc-OE" )
andrewonlaba548f962014-10-21 19:28:43 -040068 return main.FALSE
69
kelvin-onlabd3b64892015-01-20 13:26:24 -080070 def startConsole( self ):
shahshreyae6c7cf42014-11-26 16:39:01 -080071 import time
kelvin-onlabedcff052015-01-16 12:53:55 -080072 main.log.info(
73 self.name +
74 ": Starting Linc-OE CLI.. This may take a while" )
75 time.sleep( 30 )
76 self.handle.sendline( "sudo ./rel/linc/bin/linc console" )
77 j = self.handle.expect( [ "linc@", pexpect.EOF, pexpect.TIMEOUT ] )
kelvin-onlabd3b64892015-01-20 13:26:24 -080078 startResult = self.handle.before
shahshreyae6c7cf42014-11-26 16:39:01 -080079 if j == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -080080 main.log.info( "Linc-OE CLI started" )
shahshreyae6c7cf42014-11-26 16:39:01 -080081 return main.TRUE
82 else:
kelvin-onlabedcff052015-01-16 12:53:55 -080083 main.log.error(
84 self.name +
85 ": Connection failed to the host " +
kelvin-onlab08679eb2015-01-21 16:11:48 -080086 self.user_name +
kelvin-onlabedcff052015-01-16 12:53:55 -080087 "@" +
kelvin-onlab08679eb2015-01-21 16:11:48 -080088 self.ip_address )
kelvin-onlabedcff052015-01-16 12:53:55 -080089 main.log.error( self.name +
90 ": Failed to connect to Linc-OE" )
shahshreyae6c7cf42014-11-26 16:39:01 -080091 return main.FALSE
92
kelvin-onlabedcff052015-01-16 12:53:55 -080093 def build( self ):
94 """
andrewonlab52a31e02014-10-22 12:57:19 -040095 Build Linc-OE with the specified settings
kelvin-onlabedcff052015-01-16 12:53:55 -080096 """
andrewonlab52a31e02014-10-22 12:57:19 -040097 try:
kelvin-onlabedcff052015-01-16 12:53:55 -080098 self.handle.sendline( "make rel" )
99 i = self.handle.expect( [
andrewonlabc6d1fa62014-10-22 16:28:04 -0400100 "ERROR",
kelvin-onlabedcff052015-01-16 12:53:55 -0800101 "\$" ] )
andrewonlabc6d1fa62014-10-22 16:28:04 -0400102
103 if i == 0:
kelvin-onlabedcff052015-01-16 12:53:55 -0800104 self.handle.sendline( "sudo pkill -9 epmd" )
105 self.handle.sendline( "make rel" )
106 self.handle.expect( "\$" )
107
andrewonlabc6d1fa62014-10-22 16:28:04 -0400108 handle = self.handle.before
109 return handle
110
111 else:
112 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400113
114 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800115 main.log.error( self.name + ": EOF exception" )
116 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400117 main.cleanup()
118 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800119 except Exception:
120 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400121 main.cleanup()
122 main.exit()
123
kelvin-onlabd3b64892015-01-20 13:26:24 -0800124 def setInterfaceUp( self, intfs ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800125 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400126 Specify interface to bring up.
127 When Linc-OE is started, tap interfaces should
128 be created. They must be brought up manually
kelvin-onlabedcff052015-01-16 12:53:55 -0800129 """
andrewonlabeb08b6f2014-10-21 21:23:15 -0400130 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800131 self.handle.sendline( "ifconfig " + str( intfs ) + " up" )
132 self.handle.expect( "linc@" )
133
andrewonlabeb08b6f2014-10-21 21:23:15 -0400134 handle = self.handle.before
135
136 return handle
137
138 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800139 main.log.error( self.name + ": EOF exception" )
140 main.log.error( self.name + ": " + self.handle.before )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400141 main.cleanup()
142 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800143 except Exception:
144 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlabeb08b6f2014-10-21 21:23:15 -0400145 main.cleanup()
146 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400147
kelvin-onlabd3b64892015-01-20 13:26:24 -0800148 def startSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800149 """
andrewonlab52a31e02014-10-22 12:57:19 -0400150 Start a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800151 """
andrewonlab52a31e02014-10-22 12:57:19 -0400152 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800153 self.handle.sendline( "linc:start_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800154 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400155
156 handle = self.handle.before
157
158 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800159 main.log.error( self.name + ": EOF exception" )
160 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400161 main.cleanup()
162 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800163 except Exception:
164 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400165 main.cleanup()
166 main.exit()
167
kelvin-onlabd3b64892015-01-20 13:26:24 -0800168 def stopSwitch( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800169 """
andrewonlab52a31e02014-10-22 12:57:19 -0400170 Stop a logical switch using switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800171 """
andrewonlab52a31e02014-10-22 12:57:19 -0400172 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800173 self.handle.sendline( "linc:stop_switch(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800174 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400175
176 handle = self.handle.before
177
178 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800179 main.log.error( self.name + ": EOF exception" )
180 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400181 main.cleanup()
182 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800183 except Exception:
184 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400185 main.cleanup()
186 main.exit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800187
kelvin-onlabd3b64892015-01-20 13:26:24 -0800188 def getDatapathId( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800189 """
andrewonlab52a31e02014-10-22 12:57:19 -0400190 Get datapath id of a specific switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800191 """
andrewonlab52a31e02014-10-22 12:57:19 -0400192 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800193 self.handle.sendline( "linc_logic:get_datapath_id(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800194 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
kelvin-onlabedcff052015-01-16 12:53:55 -0800198
andrewonlab52a31e02014-10-22 12:57:19 -0400199 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()
208
shahshreya9ee36d52015-04-03 15:38:23 -0700209 def attachLincOESession( self ):
210 """
211 Since executing opticalTest.py will give you mininet
212 prompt, you would at some point require to get onto
213 console of LincOE ((linc@onosTestBench)1>) to execute
214 commands like bring a optical port up or down on a ROADM
215 You can attach to console of Linc-OE session by a cmd:
216 sudo ~/linc-oe/rel/linc/bin/linc attach
217 """
218 try:
219 self.handle.sendline( "" )
220 self.handle.expect( "\$" )
221 self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" )
222 self.handle.expect( ">" )
223 return main.TRUE
224 except pexpect.EOF:
225 main.log.error( self.name + ": EOF exception found" )
226 main.log.error( self.name + ": " + self.handle.before )
227 return main.FALSE
228
kelvin-onlabd3b64892015-01-20 13:26:24 -0800229 def listPorts( self, swId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800230 """
andrewonlab52a31e02014-10-22 12:57:19 -0400231 List all ports of a switch by switch id
kelvin-onlabedcff052015-01-16 12:53:55 -0800232 """
andrewonlab52a31e02014-10-22 12:57:19 -0400233 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800234 self.handle.sendline( "linc:ports(" + str( swId ) + ")." )
kelvin-onlabedcff052015-01-16 12:53:55 -0800235 self.handle.expect( "linc@" )
andrewonlab52a31e02014-10-22 12:57:19 -0400236
237 handle = self.handle.before
238
239 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800240 main.log.error( self.name + ": EOF exception" )
241 main.log.error( self.name + ": " + self.handle.before )
andrewonlab52a31e02014-10-22 12:57:19 -0400242 main.cleanup()
243 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800244 except Exception:
245 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab52a31e02014-10-22 12:57:19 -0400246 main.cleanup()
247 main.exit()
248
kelvin-onlabd3b64892015-01-20 13:26:24 -0800249 def portUp( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800250 """
andrewonlab52a31e02014-10-22 12:57:19 -0400251 Bring port up using switch id and port id
kelvin-onlabedcff052015-01-16 12:53:55 -0800252 """
andrewonlab52a31e02014-10-22 12:57:19 -0400253 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800254 self.handle.sendline( "linc:port_up(" +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800255 str( swId ) + ", " + str( ptId ) + ")." )
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()
kelvin-onlabedcff052015-01-16 12:53:55 -0800269
kelvin-onlabd3b64892015-01-20 13:26:24 -0800270 def portDown( self, swId, ptId ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800271 """
andrewonlab52a31e02014-10-22 12:57:19 -0400272 Bring port down 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_down(" +
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
291 def stopLincOEConsole( self ):
292 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800293 This function is only used for packet optical testing
294 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800295 ( CTRL+C ) and kill the linc process
296 """
shahshreyae6c7cf42014-11-26 16:39:01 -0800297 try:
298 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800299 self.handle.sendline( "pgrep -f linc" )
300 self.handle.expect( "linc" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800301 print "stophandle = ", self.handle.before
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 )
andrewonlaba548f962014-10-21 19:28:43 -0400305
kelvin-onlabedcff052015-01-16 12:53:55 -0800306 def disconnect( self ):
307 """
andrewonlab0980f422014-10-21 21:28:39 -0400308 Send disconnect prompt to Linc-OE CLI
kelvin-onlabedcff052015-01-16 12:53:55 -0800309 ( CTRL+C ) and kill the linc process
310 """
andrewonlab0980f422014-10-21 21:28:39 -0400311 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800312 # Send CTRL+C twice to exit CLI
313 self.handle.send( "\x03" )
314 self.handle.send( "\x03" )
315 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800316 handle1 = self.handle.before
317 cmd = "pgrep -f linc"
kelvin-onlabedcff052015-01-16 12:53:55 -0800318 self.handle.sendline( cmd )
319 self.handle.expect( "\$" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800320 handle2 = self.handle.before
kelvin-onlabedcff052015-01-16 12:53:55 -0800321 main.log.info( "pid's = " + handle2 )
shahshreyae6c7cf42014-11-26 16:39:01 -0800322 cmd = "sudo kill -9 `pgrep -f linc`"
kelvin-onlabedcff052015-01-16 12:53:55 -0800323 self.handle.sendline( cmd )
324 self.handle.expect( "\$" )
shahshreya5a034842015-02-05 11:08:46 -0800325 # Close the ssh connection
326 self.handle.sendline( "" )
327 self.handle.expect( "\$" )
328 self.handle.sendline( "exit" )
329 self.handle.expect( "closed" )
andrewonlab0980f422014-10-21 21:28:39 -0400330 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800331 main.log.error( self.name + ": EOF exception" )
332 main.log.error( self.name + ": " + self.handle.before )
andrewonlab0980f422014-10-21 21:28:39 -0400333 main.cleanup()
334 main.exit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800335 except Exception:
336 main.log.exception( self.name + ": Uncaught exception!" )
andrewonlab0980f422014-10-21 21:28:39 -0400337 main.cleanup()
338 main.exit()
339
andrewonlaba548f962014-10-21 19:28:43 -0400340if __name__ != "__main__":
341 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800342 sys.modules[ __name__ ] = LincOEDriver()