blob: 0885d9ea0deb173618237557500ebc631369debd [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 '''
45 for key in connectargs:
46 vars(self)[key] = connectargs[key]
47
48 self.name = self.options['name']
49 self.handle = \
50 super(LincOEDriver, self).connect(\
51 user_name = self.user_name,
52 ip_address = self.ip_address,
53 port = None,
54 pwd = self.pwd)
55
56 self.ssh_handle = self.handle
57
58 if self.handle :
andrewonlab8d29f122014-10-22 17:15:04 -040059 main.log.info("Handle successfully created")
andrewonlab52a31e02014-10-22 12:57:19 -040060 self.home = "~/linc-oe"
61 self.handle.sendline("cd "+self.home)
andrewonlab8d29f122014-10-22 17:15:04 -040062 self.handle.expect("oe$")
andrewonlab52a31e02014-10-22 12:57:19 -040063
andrewonlab8d29f122014-10-22 17:15:04 -040064 main.log.info("Building Linc-OE")
65 self.handle.sendline("make rel")
66 i = self.handle.expect(["ERROR","\$"])
67 if i == 0:
68 self.handle.sendline("sudo pkill -9 epmd")
69 self.handle.expect("\$")
70 self.handle.sendline("make rel")
71 x = self.handle.expect(["\$",pespect.EOF,pexpect.TIMEOUT])
72 main.log.info("make rel returned: "+ str(x))
73
andrewonlaba548f962014-10-21 19:28:43 -040074 main.log.info(self.name+": Starting Linc-OE CLI")
75 cmdStr = "sudo ./rel/linc/bin/linc console"
76
77 self.handle.sendline(cmdStr)
andrewonlab8d29f122014-10-22 17:15:04 -040078 self.handle.expect([">",pexpect.EOF,pexpect.TIMEOUT])
79
80 return main.TRUE
andrewonlaba548f962014-10-21 19:28:43 -040081
82 else:
83 main.log.error(self.name+
84 ": Connection failed to the host "+
85 self.user_name+"@"+self.ip_address)
86 main.log.error(self.name+
87 ": Failed to connect to Linc-OE")
88 return main.FALSE
89
andrewonlab52a31e02014-10-22 12:57:19 -040090 def build(self):
91 '''
92 Build Linc-OE with the specified settings
93 '''
94 try:
95 self.handle.sendline("make rel")
andrewonlabc6d1fa62014-10-22 16:28:04 -040096 i = self.handle.expect([
97 "ERROR",
98 "\$"])
99
100 if i == 0:
101 #If error, try to resolve the most common error
102 #(epmd running and cannot compile)
103 self.handle.sendline("sudo pkill -9 epmd")
104 self.handle.sendline("make rel")
105 self.handle.expect("\$")
106
107 handle = self.handle.before
108 return handle
109
110 else:
111 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400112
113 except pexpect.EOF:
114 main.log.error(self.name+ ": EOF exception")
115 main.log.error(self.name+ ": " + self.handle.before)
116 main.cleanup()
117 main.exit()
118 except:
119 main.log.info(self.name+" :::::::")
120 main.log.error( traceback.print_exc())
121 main.log.info(self.name+" :::::::")
122 main.cleanup()
123 main.exit()
124
andrewonlabeb08b6f2014-10-21 21:23:15 -0400125 def set_interface_up(self, intfs):
126 '''
127 Specify interface to bring up.
128 When Linc-OE is started, tap interfaces should
129 be created. They must be brought up manually
130 '''
131 try:
132 self.handle.sendline("ifconfig "+str(intf)+" up")
133 self.handle.expect("linc@")
134
135 handle = self.handle.before
136
137 return handle
138
139 except pexpect.EOF:
140 main.log.error(self.name+ ": EOF exception")
141 main.log.error(self.name+ ": " + self.handle.before)
142 main.cleanup()
143 main.exit()
144 except:
145 main.log.info(self.name+" :::::::")
146 main.log.error( traceback.print_exc())
147 main.log.info(self.name+" :::::::")
148 main.cleanup()
149 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400150
andrewonlab52a31e02014-10-22 12:57:19 -0400151 def start_switch(self, sw_id):
152 '''
153 Start a logical switch using switch id
154 '''
155 try:
156 self.handle.sendline("linc:start_switch("+str(sw_id)+").")
157 self.handle.expect("linc@")
158
159 handle = self.handle.before
160
161 except pexpect.EOF:
162 main.log.error(self.name+ ": EOF exception")
163 main.log.error(self.name+ ": " + self.handle.before)
164 main.cleanup()
165 main.exit()
166 except:
167 main.log.info(self.name+" :::::::")
168 main.log.error( traceback.print_exc())
169 main.log.info(self.name+" :::::::")
170 main.cleanup()
171 main.exit()
172
173 def stop_switch(self, sw_id):
174 '''
175 Stop a logical switch using switch id
176 '''
177 try:
178 self.handle.sendline("linc:stop_switch("+str(sw_id)+").")
179 self.handle.expect("linc@")
180
181 handle = self.handle.before
182
183 except pexpect.EOF:
184 main.log.error(self.name+ ": EOF exception")
185 main.log.error(self.name+ ": " + self.handle.before)
186 main.cleanup()
187 main.exit()
188 except:
189 main.log.info(self.name+" :::::::")
190 main.log.error( traceback.print_exc())
191 main.log.info(self.name+" :::::::")
192 main.cleanup()
193 main.exit()
194
195 def get_datapath_id(self, sw_id):
196 '''
197 Get datapath id of a specific switch by switch id
198 '''
199 try:
200 self.handle.sendline("linc_logic:get_datapath_id("+
201 str(sw_id)+").")
202 self.handle.expect("linc@")
203
204 handle = self.handle.before
205
206 except pexpect.EOF:
207 main.log.error(self.name+ ": EOF exception")
208 main.log.error(self.name+ ": " + self.handle.before)
209 main.cleanup()
210 main.exit()
211 except:
212 main.log.info(self.name+" :::::::")
213 main.log.error( traceback.print_exc())
214 main.log.info(self.name+" :::::::")
215 main.cleanup()
216 main.exit()
217
218 def list_ports(self, sw_id):
219 '''
220 List all ports of a switch by switch id
221 '''
222 try:
223 self.handle.sendline("linc:ports("+str(sw_id)+").")
224 self.handle.expect("linc@")
225
226 handle = self.handle.before
227
228 except pexpect.EOF:
229 main.log.error(self.name+ ": EOF exception")
230 main.log.error(self.name+ ": " + self.handle.before)
231 main.cleanup()
232 main.exit()
233 except:
234 main.log.info(self.name+" :::::::")
235 main.log.error( traceback.print_exc())
236 main.log.info(self.name+" :::::::")
237 main.cleanup()
238 main.exit()
239
240 def port_up(self, sw_id, pt_id):
241 '''
242 Bring port up using switch id and port id
243 '''
244 try:
245 self.handle.sendline("linc:port_up("+
246 str(sw_id)+", "+str(pt_id)+").")
247 self.handle.expect("linc@")
248
249 handle = self.handle.before
250
251 except pexpect.EOF:
252 main.log.error(self.name+ ": EOF exception")
253 main.log.error(self.name+ ": " + self.handle.before)
254 main.cleanup()
255 main.exit()
256 except:
257 main.log.info(self.name+" :::::::")
258 main.log.error( traceback.print_exc())
259 main.log.info(self.name+" :::::::")
260 main.cleanup()
261 main.exit()
262
263 def port_down(self, sw_id, pt_id):
264 '''
265 Bring port down using switch id and port id
266 '''
267 try:
268 self.handle.sendline("linc:port_down("+
269 str(sw_id)+", "+str(pt_id)+").")
270 self.handle.expect("linc@")
271
272 handle = self.handle.before
273
274 except pexpect.EOF:
275 main.log.error(self.name+ ": EOF exception")
276 main.log.error(self.name+ ": " + self.handle.before)
277 main.cleanup()
278 main.exit()
279 except:
280 main.log.info(self.name+" :::::::")
281 main.log.error( traceback.print_exc())
282 main.log.info(self.name+" :::::::")
283 main.cleanup()
284 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400285
andrewonlab0980f422014-10-21 21:28:39 -0400286 def disconnect(self):
287 '''
288 Send disconnect prompt to Linc-OE CLI
289 (CTRL+C)
290 '''
291 try:
292 #Send CTRL+C twice to exit CLI
293 self.handle.sendline("\x03")
294 self.handle.sendline("\x03")
295 self.handle.expect("\$")
296
297 except pexpect.EOF:
298 main.log.error(self.name+ ": EOF exception")
299 main.log.error(self.name+ ": " + self.handle.before)
300 main.cleanup()
301 main.exit()
302 except:
303 main.log.info(self.name+" :::::::")
304 main.log.error( traceback.print_exc())
305 main.log.info(self.name+" :::::::")
306 main.cleanup()
307 main.exit()
308
andrewonlaba548f962014-10-21 19:28:43 -0400309if __name__ != "__main__":
310 import sys
311 sys.modules[__name__] = LincOEDriver()
312