blob: 4d2cc018b3f8b047ade9c932855e38b67896ba70 [file] [log] [blame]
andrewonlaba548f962014-10-21 19:28:43 -04001#!/usr/bin/env python
2
3'''
4This 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
13
14OCT 20 2014
15'''
16
17import traceback
18import pexpect
19import struct
20import fcntl
21import os
22import signal
23import re
24import sys
25import core.teston
26sys.path.append("../")
27from math import pow
28from drivers.common.cli.emulatordriver import Emulator
29from drivers.common.clidriver import CLI
30
31class LincOEDriver(Emulator):
32 '''
33 LincOEDriver class will handle all emulator functions
34 '''
35 def __init__(self):
36 super(Emulator, self).__init__()
37 self.handle = self
38 self.wrapped = sys.modules[__name__]
39 self.flag = 0
40
41 def connect(self, **connectargs):
42 '''
43 Create ssh handle for Linc-OE cli
44 '''
andrewonlaba85a7762014-10-22 18:05:52 -040045 import time
46
andrewonlaba548f962014-10-21 19:28:43 -040047 for key in connectargs:
48 vars(self)[key] = connectargs[key]
49
50 self.name = self.options['name']
51 self.handle = \
52 super(LincOEDriver, self).connect(\
53 user_name = self.user_name,
54 ip_address = self.ip_address,
55 port = None,
56 pwd = self.pwd)
57
58 self.ssh_handle = self.handle
59
60 if self.handle :
andrewonlab8d29f122014-10-22 17:15:04 -040061 main.log.info("Handle successfully created")
andrewonlab52a31e02014-10-22 12:57:19 -040062 self.home = "~/linc-oe"
63 self.handle.sendline("cd "+self.home)
andrewonlab8d29f122014-10-22 17:15:04 -040064 self.handle.expect("oe$")
andrewonlab52a31e02014-10-22 12:57:19 -040065
andrewonlab8d29f122014-10-22 17:15:04 -040066 main.log.info("Building Linc-OE")
67 self.handle.sendline("make rel")
andrewonlaba85a7762014-10-22 18:05:52 -040068 i = self.handle.expect(["ERROR","linc-oe\$"],timeout=60)
andrewonlab8d29f122014-10-22 17:15:04 -040069 if i == 0:
70 self.handle.sendline("sudo pkill -9 epmd")
71 self.handle.expect("\$")
72 self.handle.sendline("make rel")
andrewonlaba85a7762014-10-22 18:05:52 -040073 x = self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT])
andrewonlab8d29f122014-10-22 17:15:04 -040074 main.log.info("make rel returned: "+ str(x))
andrewonlaba85a7762014-10-22 18:05:52 -040075 else:
76 main.log.info(self.name+": Starting Linc-OE CLI.. This may take a while")
77 time.sleep(30)
78 self.handle.sendline("sudo ./rel/linc/bin/linc console")
79 j = self.handle.expect(["linc@",pexpect.EOF,pexpect.TIMEOUT])
80
81 if j == 0:
82 main.log.info("Linc-OE CLI started")
83 return main.TRUE
andrewonlaba548f962014-10-21 19:28:43 -040084
85 else:
86 main.log.error(self.name+
87 ": Connection failed to the host "+
88 self.user_name+"@"+self.ip_address)
89 main.log.error(self.name+
90 ": Failed to connect to Linc-OE")
91 return main.FALSE
92
andrewonlab52a31e02014-10-22 12:57:19 -040093 def build(self):
94 '''
95 Build Linc-OE with the specified settings
96 '''
97 try:
98 self.handle.sendline("make rel")
andrewonlabc6d1fa62014-10-22 16:28:04 -040099 i = self.handle.expect([
100 "ERROR",
101 "\$"])
102
103 if i == 0:
104 #If error, try to resolve the most common error
105 #(epmd running and cannot compile)
106 self.handle.sendline("sudo pkill -9 epmd")
107 self.handle.sendline("make rel")
108 self.handle.expect("\$")
109
110 handle = self.handle.before
111 return handle
112
113 else:
114 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400115
116 except pexpect.EOF:
117 main.log.error(self.name+ ": EOF exception")
118 main.log.error(self.name+ ": " + self.handle.before)
119 main.cleanup()
120 main.exit()
121 except:
122 main.log.info(self.name+" :::::::")
123 main.log.error( traceback.print_exc())
124 main.log.info(self.name+" :::::::")
125 main.cleanup()
126 main.exit()
127
andrewonlabeb08b6f2014-10-21 21:23:15 -0400128 def set_interface_up(self, intfs):
129 '''
130 Specify interface to bring up.
131 When Linc-OE is started, tap interfaces should
132 be created. They must be brought up manually
133 '''
134 try:
135 self.handle.sendline("ifconfig "+str(intf)+" up")
136 self.handle.expect("linc@")
137
138 handle = self.handle.before
139
140 return handle
141
142 except pexpect.EOF:
143 main.log.error(self.name+ ": EOF exception")
144 main.log.error(self.name+ ": " + self.handle.before)
145 main.cleanup()
146 main.exit()
147 except:
148 main.log.info(self.name+" :::::::")
149 main.log.error( traceback.print_exc())
150 main.log.info(self.name+" :::::::")
151 main.cleanup()
152 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400153
andrewonlab52a31e02014-10-22 12:57:19 -0400154 def start_switch(self, sw_id):
155 '''
156 Start a logical switch using switch id
157 '''
158 try:
159 self.handle.sendline("linc:start_switch("+str(sw_id)+").")
160 self.handle.expect("linc@")
161
162 handle = self.handle.before
163
164 except pexpect.EOF:
165 main.log.error(self.name+ ": EOF exception")
166 main.log.error(self.name+ ": " + self.handle.before)
167 main.cleanup()
168 main.exit()
169 except:
170 main.log.info(self.name+" :::::::")
171 main.log.error( traceback.print_exc())
172 main.log.info(self.name+" :::::::")
173 main.cleanup()
174 main.exit()
175
176 def stop_switch(self, sw_id):
177 '''
178 Stop a logical switch using switch id
179 '''
180 try:
181 self.handle.sendline("linc:stop_switch("+str(sw_id)+").")
182 self.handle.expect("linc@")
183
184 handle = self.handle.before
185
186 except pexpect.EOF:
187 main.log.error(self.name+ ": EOF exception")
188 main.log.error(self.name+ ": " + self.handle.before)
189 main.cleanup()
190 main.exit()
191 except:
192 main.log.info(self.name+" :::::::")
193 main.log.error( traceback.print_exc())
194 main.log.info(self.name+" :::::::")
195 main.cleanup()
196 main.exit()
197
198 def get_datapath_id(self, sw_id):
199 '''
200 Get datapath id of a specific switch by switch id
201 '''
202 try:
203 self.handle.sendline("linc_logic:get_datapath_id("+
204 str(sw_id)+").")
205 self.handle.expect("linc@")
206
207 handle = self.handle.before
208
209 except pexpect.EOF:
210 main.log.error(self.name+ ": EOF exception")
211 main.log.error(self.name+ ": " + self.handle.before)
212 main.cleanup()
213 main.exit()
214 except:
215 main.log.info(self.name+" :::::::")
216 main.log.error( traceback.print_exc())
217 main.log.info(self.name+" :::::::")
218 main.cleanup()
219 main.exit()
220
221 def list_ports(self, sw_id):
222 '''
223 List all ports of a switch by switch id
224 '''
225 try:
226 self.handle.sendline("linc:ports("+str(sw_id)+").")
227 self.handle.expect("linc@")
228
229 handle = self.handle.before
230
231 except pexpect.EOF:
232 main.log.error(self.name+ ": EOF exception")
233 main.log.error(self.name+ ": " + self.handle.before)
234 main.cleanup()
235 main.exit()
236 except:
237 main.log.info(self.name+" :::::::")
238 main.log.error( traceback.print_exc())
239 main.log.info(self.name+" :::::::")
240 main.cleanup()
241 main.exit()
242
243 def port_up(self, sw_id, pt_id):
244 '''
245 Bring port up using switch id and port id
246 '''
247 try:
248 self.handle.sendline("linc:port_up("+
249 str(sw_id)+", "+str(pt_id)+").")
250 self.handle.expect("linc@")
251
252 handle = self.handle.before
253
254 except pexpect.EOF:
255 main.log.error(self.name+ ": EOF exception")
256 main.log.error(self.name+ ": " + self.handle.before)
257 main.cleanup()
258 main.exit()
259 except:
260 main.log.info(self.name+" :::::::")
261 main.log.error( traceback.print_exc())
262 main.log.info(self.name+" :::::::")
263 main.cleanup()
264 main.exit()
265
266 def port_down(self, sw_id, pt_id):
267 '''
268 Bring port down using switch id and port id
269 '''
270 try:
271 self.handle.sendline("linc:port_down("+
272 str(sw_id)+", "+str(pt_id)+").")
273 self.handle.expect("linc@")
274
275 handle = self.handle.before
276
277 except pexpect.EOF:
278 main.log.error(self.name+ ": EOF exception")
279 main.log.error(self.name+ ": " + self.handle.before)
280 main.cleanup()
281 main.exit()
282 except:
283 main.log.info(self.name+" :::::::")
284 main.log.error( traceback.print_exc())
285 main.log.info(self.name+" :::::::")
286 main.cleanup()
287 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400288
andrewonlab0980f422014-10-21 21:28:39 -0400289 def disconnect(self):
290 '''
291 Send disconnect prompt to Linc-OE CLI
292 (CTRL+C)
293 '''
294 try:
295 #Send CTRL+C twice to exit CLI
296 self.handle.sendline("\x03")
297 self.handle.sendline("\x03")
298 self.handle.expect("\$")
299
300 except pexpect.EOF:
301 main.log.error(self.name+ ": EOF exception")
302 main.log.error(self.name+ ": " + self.handle.before)
303 main.cleanup()
304 main.exit()
305 except:
306 main.log.info(self.name+" :::::::")
307 main.log.error( traceback.print_exc())
308 main.log.info(self.name+" :::::::")
309 main.cleanup()
310 main.exit()
311
andrewonlaba548f962014-10-21 19:28:43 -0400312if __name__ != "__main__":
313 import sys
314 sys.modules[__name__] = LincOEDriver()
315