1
2
3 """
4 This driver handles the optical switch emulator linc-oe.
5
6 Please follow the coding style demonstrated by existing
7 functions and document properly.
8
9 If you are a contributor to the driver, please
10 list your email here for future contact:
11
12 andrew@onlab.us
13 shreya@onlab.us
14
15 OCT 20 2014
16 """
17
18 import pexpect
19 import sys
20 from drivers.common.cli.emulatordriver import Emulator
21
22
24
25 """
26 LincOEDriver class will handle all emulator functions
27 """
29 super( Emulator, self ).__init__()
30 self.handle = self
31 self.wrapped = sys.modules[ __name__ ]
32 self.flag = 0
33
34 - def connect( self, **connectargs ):
35 """
36 Create ssh handle for Linc-OE cli
37 """
38
39 for key in connectargs:
40 vars( self )[ key ] = connectargs[ key ]
41
42 self.name = self.options[ 'name' ]
43 self.handle = \
44 super( LincOEDriver, self ).connect(
45 user_name=self.user_name,
46 ip_address=self.ip_address,
47 port=None,
48 pwd=self.pwd )
49
50 self.sshHandle = self.handle
51
52 if self.handle:
53 main.log.info( "Handle successfully created" )
54 self.home = "~/linc-oe"
55
56 self.handle.sendline( "cd " + self.home )
57 self.handle.expect( "oe$" )
58
59 print "handle = ", self.handle.before
60
61 return main.TRUE
62 else:
63 main.log.error( self.name +
64 ": Connection failed to the host " +
65 self.user_name + "@" + self.ip_address )
66 main.log.error( self.name +
67 ": Failed to connect to Linc-OE" )
68 return main.FALSE
69
71 import time
72 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 ] )
78 startResult = self.handle.before
79 if j == 0:
80 main.log.info( "Linc-OE CLI started" )
81 return main.TRUE
82 else:
83 main.log.error(
84 self.name +
85 ": Connection failed to the host " +
86 self.user_name +
87 "@" +
88 self.ip_address )
89 main.log.error( self.name +
90 ": Failed to connect to Linc-OE" )
91 return main.FALSE
92
94 """
95 Build Linc-OE with the specified settings
96 """
97 try:
98 self.handle.sendline( "make rel" )
99 i = self.handle.expect( [
100 "ERROR",
101 "\$" ] )
102
103 if i == 0:
104 self.handle.sendline( "sudo pkill -9 epmd" )
105 self.handle.sendline( "make rel" )
106 self.handle.expect( "\$" )
107
108 handle = self.handle.before
109 return handle
110
111 else:
112 return main.TRUE
113
114 except pexpect.EOF:
115 main.log.error( self.name + ": EOF exception" )
116 main.log.error( self.name + ": " + self.handle.before )
117 main.cleanup()
118 main.exit()
119 except Exception:
120 main.log.exception( self.name + ": Uncaught exception!" )
121 main.cleanup()
122 main.exit()
123
125 """
126 Specify interface to bring up.
127 When Linc-OE is started, tap interfaces should
128 be created. They must be brought up manually
129 """
130 try:
131 self.handle.sendline( "ifconfig " + str( intfs ) + " up" )
132 self.handle.expect( "linc@" )
133
134 handle = self.handle.before
135
136 return handle
137
138 except pexpect.EOF:
139 main.log.error( self.name + ": EOF exception" )
140 main.log.error( self.name + ": " + self.handle.before )
141 main.cleanup()
142 main.exit()
143 except Exception:
144 main.log.exception( self.name + ": Uncaught exception!" )
145 main.cleanup()
146 main.exit()
147
149 """
150 Start a logical switch using switch id
151 """
152 try:
153 self.handle.sendline( "linc:start_switch(" + str( swId ) + ")." )
154 self.handle.expect( "linc@" )
155
156 handle = self.handle.before
157
158 except pexpect.EOF:
159 main.log.error( self.name + ": EOF exception" )
160 main.log.error( self.name + ": " + self.handle.before )
161 main.cleanup()
162 main.exit()
163 except Exception:
164 main.log.exception( self.name + ": Uncaught exception!" )
165 main.cleanup()
166 main.exit()
167
169 """
170 Stop a logical switch using switch id
171 """
172 try:
173 self.handle.sendline( "linc:stop_switch(" + str( swId ) + ")." )
174 self.handle.expect( "linc@" )
175
176 handle = self.handle.before
177
178 except pexpect.EOF:
179 main.log.error( self.name + ": EOF exception" )
180 main.log.error( self.name + ": " + self.handle.before )
181 main.cleanup()
182 main.exit()
183 except Exception:
184 main.log.exception( self.name + ": Uncaught exception!" )
185 main.cleanup()
186 main.exit()
187
189 """
190 Get datapath id of a specific switch by switch id
191 """
192 try:
193 self.handle.sendline( "linc_logic:get_datapath_id(" +
194 str( swId ) + ")." )
195 self.handle.expect( "linc@" )
196
197 handle = self.handle.before
198
199 except pexpect.EOF:
200 main.log.error( self.name + ": EOF exception" )
201 main.log.error( self.name + ": " + self.handle.before )
202 main.cleanup()
203 main.exit()
204 except Exception:
205 main.log.exception( self.name + ": Uncaught exception!" )
206 main.cleanup()
207 main.exit()
208
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
230 """
231 List all ports of a switch by switch id
232 """
233 try:
234 self.handle.sendline( "linc:ports(" + str( swId ) + ")." )
235 self.handle.expect( "linc@" )
236
237 handle = self.handle.before
238
239 except pexpect.EOF:
240 main.log.error( self.name + ": EOF exception" )
241 main.log.error( self.name + ": " + self.handle.before )
242 main.cleanup()
243 main.exit()
244 except Exception:
245 main.log.exception( self.name + ": Uncaught exception!" )
246 main.cleanup()
247 main.exit()
248
249 - def portUp( self, swId, ptId ):
250 """
251 Bring port up using switch id and port id
252 """
253 try:
254 self.handle.sendline( "linc:port_up(" +
255 str( swId ) + ", " + str( ptId ) + ")." )
256 self.handle.expect( "linc@" )
257
258 handle = self.handle.before
259
260 except pexpect.EOF:
261 main.log.error( self.name + ": EOF exception" )
262 main.log.error( self.name + ": " + self.handle.before )
263 main.cleanup()
264 main.exit()
265 except Exception:
266 main.log.exception( self.name + ": Uncaught exception!" )
267 main.cleanup()
268 main.exit()
269
271 """
272 Bring port down using switch id and port id
273 """
274 try:
275 self.handle.sendline( "linc:port_down(" +
276 str( swId ) + ", " + str( ptId ) + ")." )
277 self.handle.expect( "linc@" )
278
279 handle = self.handle.before
280
281 except pexpect.EOF:
282 main.log.error( self.name + ": EOF exception" )
283 main.log.error( self.name + ": " + self.handle.before )
284 main.cleanup()
285 main.exit()
286 except Exception:
287 main.log.exception( self.name + ": Uncaught exception!" )
288 main.cleanup()
289 main.exit()
290
292 """
293 This function is only used for packet optical testing
294 Send disconnect prompt to Linc-OE CLI
295 ( CTRL+C ) and kill the linc process
296 """
297 try:
298 cmd = "pgrep -f linc"
299 self.handle.sendline( "pgrep -f linc" )
300 self.handle.expect( "linc" )
301 print "stophandle = ", self.handle.before
302 except pexpect.EOF:
303 main.log.error( self.name + ": EOF exception" )
304 main.log.error( self.name + ": " + self.handle.before )
305
307 """
308 Send disconnect prompt to Linc-OE CLI
309 ( CTRL+C ) and kill the linc process
310 """
311 try:
312
313 self.handle.send( "\x03" )
314 self.handle.send( "\x03" )
315 self.handle.expect( "\$" )
316 handle1 = self.handle.before
317 cmd = "pgrep -f linc"
318 self.handle.sendline( cmd )
319 self.handle.expect( "\$" )
320 handle2 = self.handle.before
321 main.log.info( "pid's = " + handle2 )
322 cmd = "sudo kill -9 `pgrep -f linc`"
323 self.handle.sendline( cmd )
324 self.handle.expect( "\$" )
325
326 self.handle.sendline( "" )
327 self.handle.expect( "\$" )
328 self.handle.sendline( "exit" )
329 self.handle.expect( "closed" )
330 except pexpect.EOF:
331 main.log.error( self.name + ": EOF exception" )
332 main.log.error( self.name + ": " + self.handle.before )
333 main.cleanup()
334 main.exit()
335 except Exception:
336 main.log.exception( self.name + ": Uncaught exception!" )
337 main.cleanup()
338 main.exit()
339
340 if __name__ != "__main__":
341 import sys
342 sys.modules[ __name__ ] = LincOEDriver()
343