blob: 7558077f7a2782840dc3607567ac13c0fd33483a [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 :
andrewonlab52a31e02014-10-22 12:57:19 -040059 self.home = "~/linc-oe"
60 self.handle.sendline("cd "+self.home)
61
andrewonlaba548f962014-10-21 19:28:43 -040062 main.log.info(self.name+": Starting Linc-OE CLI")
63 cmdStr = "sudo ./rel/linc/bin/linc console"
64
65 self.handle.sendline(cmdStr)
andrewonlabeb08b6f2014-10-21 21:23:15 -040066 #Sending blank lines "shows" the CLI
67 self.handle.sendline("")
68 self.handle.sendline("")
andrewonlaba548f962014-10-21 19:28:43 -040069 self.handle.expect(["linc@",pexpect.EOF,pexpect.TIMEOUT])
70
71 else:
72 main.log.error(self.name+
73 ": Connection failed to the host "+
74 self.user_name+"@"+self.ip_address)
75 main.log.error(self.name+
76 ": Failed to connect to Linc-OE")
77 return main.FALSE
78
andrewonlab52a31e02014-10-22 12:57:19 -040079 def build(self):
80 '''
81 Build Linc-OE with the specified settings
82 '''
83 try:
84 self.handle.sendline("make rel")
andrewonlabc6d1fa62014-10-22 16:28:04 -040085 i = self.handle.expect([
86 "ERROR",
87 "\$"])
88
89 if i == 0:
90 #If error, try to resolve the most common error
91 #(epmd running and cannot compile)
92 self.handle.sendline("sudo pkill -9 epmd")
93 self.handle.sendline("make rel")
94 self.handle.expect("\$")
95
96 handle = self.handle.before
97 return handle
98
99 else:
100 return main.TRUE
andrewonlab52a31e02014-10-22 12:57:19 -0400101
102 except pexpect.EOF:
103 main.log.error(self.name+ ": EOF exception")
104 main.log.error(self.name+ ": " + self.handle.before)
105 main.cleanup()
106 main.exit()
107 except:
108 main.log.info(self.name+" :::::::")
109 main.log.error( traceback.print_exc())
110 main.log.info(self.name+" :::::::")
111 main.cleanup()
112 main.exit()
113
andrewonlabeb08b6f2014-10-21 21:23:15 -0400114 def set_interface_up(self, intfs):
115 '''
116 Specify interface to bring up.
117 When Linc-OE is started, tap interfaces should
118 be created. They must be brought up manually
119 '''
120 try:
121 self.handle.sendline("ifconfig "+str(intf)+" up")
122 self.handle.expect("linc@")
123
124 handle = self.handle.before
125
126 return handle
127
128 except pexpect.EOF:
129 main.log.error(self.name+ ": EOF exception")
130 main.log.error(self.name+ ": " + self.handle.before)
131 main.cleanup()
132 main.exit()
133 except:
134 main.log.info(self.name+" :::::::")
135 main.log.error( traceback.print_exc())
136 main.log.info(self.name+" :::::::")
137 main.cleanup()
138 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400139
andrewonlab52a31e02014-10-22 12:57:19 -0400140 def start_switch(self, sw_id):
141 '''
142 Start a logical switch using switch id
143 '''
144 try:
145 self.handle.sendline("linc:start_switch("+str(sw_id)+").")
146 self.handle.expect("linc@")
147
148 handle = self.handle.before
149
150 except pexpect.EOF:
151 main.log.error(self.name+ ": EOF exception")
152 main.log.error(self.name+ ": " + self.handle.before)
153 main.cleanup()
154 main.exit()
155 except:
156 main.log.info(self.name+" :::::::")
157 main.log.error( traceback.print_exc())
158 main.log.info(self.name+" :::::::")
159 main.cleanup()
160 main.exit()
161
162 def stop_switch(self, sw_id):
163 '''
164 Stop a logical switch using switch id
165 '''
166 try:
167 self.handle.sendline("linc:stop_switch("+str(sw_id)+").")
168 self.handle.expect("linc@")
169
170 handle = self.handle.before
171
172 except pexpect.EOF:
173 main.log.error(self.name+ ": EOF exception")
174 main.log.error(self.name+ ": " + self.handle.before)
175 main.cleanup()
176 main.exit()
177 except:
178 main.log.info(self.name+" :::::::")
179 main.log.error( traceback.print_exc())
180 main.log.info(self.name+" :::::::")
181 main.cleanup()
182 main.exit()
183
184 def get_datapath_id(self, sw_id):
185 '''
186 Get datapath id of a specific switch by switch id
187 '''
188 try:
189 self.handle.sendline("linc_logic:get_datapath_id("+
190 str(sw_id)+").")
191 self.handle.expect("linc@")
192
193 handle = self.handle.before
194
195 except pexpect.EOF:
196 main.log.error(self.name+ ": EOF exception")
197 main.log.error(self.name+ ": " + self.handle.before)
198 main.cleanup()
199 main.exit()
200 except:
201 main.log.info(self.name+" :::::::")
202 main.log.error( traceback.print_exc())
203 main.log.info(self.name+" :::::::")
204 main.cleanup()
205 main.exit()
206
207 def list_ports(self, sw_id):
208 '''
209 List all ports of a switch by switch id
210 '''
211 try:
212 self.handle.sendline("linc:ports("+str(sw_id)+").")
213 self.handle.expect("linc@")
214
215 handle = self.handle.before
216
217 except pexpect.EOF:
218 main.log.error(self.name+ ": EOF exception")
219 main.log.error(self.name+ ": " + self.handle.before)
220 main.cleanup()
221 main.exit()
222 except:
223 main.log.info(self.name+" :::::::")
224 main.log.error( traceback.print_exc())
225 main.log.info(self.name+" :::::::")
226 main.cleanup()
227 main.exit()
228
229 def port_up(self, sw_id, pt_id):
230 '''
231 Bring port up using switch id and port id
232 '''
233 try:
234 self.handle.sendline("linc:port_up("+
235 str(sw_id)+", "+str(pt_id)+").")
236 self.handle.expect("linc@")
237
238 handle = self.handle.before
239
240 except pexpect.EOF:
241 main.log.error(self.name+ ": EOF exception")
242 main.log.error(self.name+ ": " + self.handle.before)
243 main.cleanup()
244 main.exit()
245 except:
246 main.log.info(self.name+" :::::::")
247 main.log.error( traceback.print_exc())
248 main.log.info(self.name+" :::::::")
249 main.cleanup()
250 main.exit()
251
252 def port_down(self, sw_id, pt_id):
253 '''
254 Bring port down using switch id and port id
255 '''
256 try:
257 self.handle.sendline("linc:port_down("+
258 str(sw_id)+", "+str(pt_id)+").")
259 self.handle.expect("linc@")
260
261 handle = self.handle.before
262
263 except pexpect.EOF:
264 main.log.error(self.name+ ": EOF exception")
265 main.log.error(self.name+ ": " + self.handle.before)
266 main.cleanup()
267 main.exit()
268 except:
269 main.log.info(self.name+" :::::::")
270 main.log.error( traceback.print_exc())
271 main.log.info(self.name+" :::::::")
272 main.cleanup()
273 main.exit()
andrewonlaba548f962014-10-21 19:28:43 -0400274
andrewonlab0980f422014-10-21 21:28:39 -0400275 def disconnect(self):
276 '''
277 Send disconnect prompt to Linc-OE CLI
278 (CTRL+C)
279 '''
280 try:
281 #Send CTRL+C twice to exit CLI
282 self.handle.sendline("\x03")
283 self.handle.sendline("\x03")
284 self.handle.expect("\$")
285
286 except pexpect.EOF:
287 main.log.error(self.name+ ": EOF exception")
288 main.log.error(self.name+ ": " + self.handle.before)
289 main.cleanup()
290 main.exit()
291 except:
292 main.log.info(self.name+" :::::::")
293 main.log.error( traceback.print_exc())
294 main.log.info(self.name+" :::::::")
295 main.cleanup()
296 main.exit()
297
andrewonlaba548f962014-10-21 19:28:43 -0400298if __name__ != "__main__":
299 import sys
300 sys.modules[__name__] = LincOEDriver()
301