andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 13 | shreya@onlab.us |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 14 | |
| 15 | OCT 20 2014 |
| 16 | ''' |
| 17 | |
| 18 | import traceback |
| 19 | import pexpect |
| 20 | import struct |
| 21 | import fcntl |
| 22 | import os |
| 23 | import signal |
| 24 | import re |
| 25 | import sys |
| 26 | import core.teston |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 27 | import time |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 28 | sys.path.append("../") |
| 29 | from math import pow |
| 30 | from drivers.common.cli.emulatordriver import Emulator |
| 31 | from drivers.common.clidriver import CLI |
| 32 | |
| 33 | class LincOEDriver(Emulator): |
| 34 | ''' |
| 35 | LincOEDriver class will handle all emulator functions |
| 36 | ''' |
| 37 | def __init__(self): |
| 38 | super(Emulator, self).__init__() |
| 39 | self.handle = self |
| 40 | self.wrapped = sys.modules[__name__] |
| 41 | self.flag = 0 |
| 42 | |
| 43 | def connect(self, **connectargs): |
| 44 | ''' |
| 45 | Create ssh handle for Linc-OE cli |
| 46 | ''' |
andrewonlab | a85a776 | 2014-10-22 18:05:52 -0400 | [diff] [blame] | 47 | import time |
| 48 | |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 49 | for key in connectargs: |
| 50 | vars(self)[key] = connectargs[key] |
| 51 | |
| 52 | self.name = self.options['name'] |
| 53 | self.handle = \ |
| 54 | super(LincOEDriver, self).connect(\ |
| 55 | user_name = self.user_name, |
| 56 | ip_address = self.ip_address, |
| 57 | port = None, |
| 58 | pwd = self.pwd) |
| 59 | |
| 60 | self.ssh_handle = self.handle |
| 61 | |
| 62 | if self.handle : |
andrewonlab | 8d29f12 | 2014-10-22 17:15:04 -0400 | [diff] [blame] | 63 | main.log.info("Handle successfully created") |
andrewonlab | 52a31e0 | 2014-10-22 12:57:19 -0400 | [diff] [blame] | 64 | self.home = "~/linc-oe" |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 65 | |
andrewonlab | 52a31e0 | 2014-10-22 12:57:19 -0400 | [diff] [blame] | 66 | self.handle.sendline("cd "+self.home) |
andrewonlab | 8d29f12 | 2014-10-22 17:15:04 -0400 | [diff] [blame] | 67 | self.handle.expect("oe$") |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 68 | |
| 69 | #self.handle.sendline("pgrep -g linc") |
| 70 | #self.handle.expect("\$") |
| 71 | print "handle = ", self.handle.before |
| 72 | |
| 73 | return main.TRUE |
| 74 | ''' |
andrewonlab | 8d29f12 | 2014-10-22 17:15:04 -0400 | [diff] [blame] | 75 | main.log.info("Building Linc-OE") |
| 76 | self.handle.sendline("make rel") |
andrewonlab | a85a776 | 2014-10-22 18:05:52 -0400 | [diff] [blame] | 77 | i = self.handle.expect(["ERROR","linc-oe\$"],timeout=60) |
andrewonlab | 8d29f12 | 2014-10-22 17:15:04 -0400 | [diff] [blame] | 78 | if i == 0: |
| 79 | self.handle.sendline("sudo pkill -9 epmd") |
| 80 | self.handle.expect("\$") |
| 81 | self.handle.sendline("make rel") |
andrewonlab | a85a776 | 2014-10-22 18:05:52 -0400 | [diff] [blame] | 82 | x = self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT]) |
andrewonlab | 8d29f12 | 2014-10-22 17:15:04 -0400 | [diff] [blame] | 83 | main.log.info("make rel returned: "+ str(x)) |
andrewonlab | a85a776 | 2014-10-22 18:05:52 -0400 | [diff] [blame] | 84 | else: |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 85 | |
| 86 | main.log.info(self.name+": Starting Linc-OE CLI.. This may take a while") |
| 87 | time.sleep(30) |
| 88 | self.handle.sendline("sudo ./rel/linc/bin/linc console") |
| 89 | j = self.handle.expect(["linc@",pexpect.EOF,pexpect.TIMEOUT]) |
andrewonlab | a85a776 | 2014-10-22 18:05:52 -0400 | [diff] [blame] | 90 | if j == 0: |
| 91 | main.log.info("Linc-OE CLI started") |
| 92 | return main.TRUE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 93 | ''' |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 94 | else: |
| 95 | main.log.error(self.name+ |
| 96 | ": Connection failed to the host "+ |
| 97 | self.user_name+"@"+self.ip_address) |
| 98 | main.log.error(self.name+ |
| 99 | ": Failed to connect to Linc-OE") |
| 100 | return main.FALSE |
| 101 | |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 102 | |
| 103 | def start_console(self): |
| 104 | import time |
| 105 | main.log.info(self.name+": Starting Linc-OE CLI.. This may take a while") |
| 106 | time.sleep(30) |
| 107 | self.handle.sendline("sudo ./rel/linc/bin/linc console") |
| 108 | j = self.handle.expect(["linc@",pexpect.EOF,pexpect.TIMEOUT]) |
| 109 | start_result = self.handle.before |
| 110 | if j == 0: |
| 111 | main.log.info("Linc-OE CLI started") |
| 112 | return main.TRUE |
| 113 | else: |
| 114 | main.log.error(self.name+ |
| 115 | ": Connection failed to the host "+self.user_name+"@"+self.ip_address) |
| 116 | main.log.error(self.name+ |
| 117 | ": Failed to connect to Linc-OE") |
| 118 | return main.FALSE |
| 119 | |
| 120 | |
| 121 | |
andrewonlab | 52a31e0 | 2014-10-22 12:57:19 -0400 | [diff] [blame] | 122 | def build(self): |
| 123 | ''' |
| 124 | Build Linc-OE with the specified settings |
| 125 | ''' |
| 126 | try: |
| 127 | self.handle.sendline("make rel") |
andrewonlab | c6d1fa6 | 2014-10-22 16:28:04 -0400 | [diff] [blame] | 128 | i = self.handle.expect([ |
| 129 | "ERROR", |
| 130 | "\$"]) |
| 131 | |
| 132 | if i == 0: |
| 133 | #If error, try to resolve the most common error |
| 134 | #(epmd running and cannot compile) |
| 135 | self.handle.sendline("sudo pkill -9 epmd") |
| 136 | self.handle.sendline("make rel") |
| 137 | self.handle.expect("\$") |
| 138 | |
| 139 | handle = self.handle.before |
| 140 | return handle |
| 141 | |
| 142 | else: |
| 143 | return main.TRUE |
andrewonlab | 52a31e0 | 2014-10-22 12:57:19 -0400 | [diff] [blame] | 144 | |
| 145 | except pexpect.EOF: |
| 146 | main.log.error(self.name+ ": EOF exception") |
| 147 | main.log.error(self.name+ ": " + self.handle.before) |
| 148 | main.cleanup() |
| 149 | main.exit() |
| 150 | except: |
| 151 | main.log.info(self.name+" :::::::") |
| 152 | main.log.error( traceback.print_exc()) |
| 153 | main.log.info(self.name+" :::::::") |
| 154 | main.cleanup() |
| 155 | main.exit() |
| 156 | |
andrewonlab | eb08b6f | 2014-10-21 21:23:15 -0400 | [diff] [blame] | 157 | def set_interface_up(self, intfs): |
| 158 | ''' |
| 159 | Specify interface to bring up. |
| 160 | When Linc-OE is started, tap interfaces should |
| 161 | be created. They must be brought up manually |
| 162 | ''' |
| 163 | try: |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 164 | self.handle.sendline("ifconfig "+str(intfs)+" up") |
andrewonlab | eb08b6f | 2014-10-21 21:23:15 -0400 | [diff] [blame] | 165 | self.handle.expect("linc@") |
| 166 | |
| 167 | handle = self.handle.before |
| 168 | |
| 169 | return handle |
| 170 | |
| 171 | except pexpect.EOF: |
| 172 | main.log.error(self.name+ ": EOF exception") |
| 173 | main.log.error(self.name+ ": " + self.handle.before) |
| 174 | main.cleanup() |
| 175 | main.exit() |
| 176 | except: |
| 177 | main.log.info(self.name+" :::::::") |
| 178 | main.log.error( traceback.print_exc()) |
| 179 | main.log.info(self.name+" :::::::") |
| 180 | main.cleanup() |
| 181 | main.exit() |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 182 | |
andrewonlab | 52a31e0 | 2014-10-22 12:57:19 -0400 | [diff] [blame] | 183 | def start_switch(self, sw_id): |
| 184 | ''' |
| 185 | Start a logical switch using switch id |
| 186 | ''' |
| 187 | try: |
| 188 | self.handle.sendline("linc:start_switch("+str(sw_id)+").") |
| 189 | self.handle.expect("linc@") |
| 190 | |
| 191 | handle = self.handle.before |
| 192 | |
| 193 | except pexpect.EOF: |
| 194 | main.log.error(self.name+ ": EOF exception") |
| 195 | main.log.error(self.name+ ": " + self.handle.before) |
| 196 | main.cleanup() |
| 197 | main.exit() |
| 198 | except: |
| 199 | main.log.info(self.name+" :::::::") |
| 200 | main.log.error( traceback.print_exc()) |
| 201 | main.log.info(self.name+" :::::::") |
| 202 | main.cleanup() |
| 203 | main.exit() |
| 204 | |
| 205 | def stop_switch(self, sw_id): |
| 206 | ''' |
| 207 | Stop a logical switch using switch id |
| 208 | ''' |
| 209 | try: |
| 210 | self.handle.sendline("linc:stop_switch("+str(sw_id)+").") |
| 211 | self.handle.expect("linc@") |
| 212 | |
| 213 | handle = self.handle.before |
| 214 | |
| 215 | except pexpect.EOF: |
| 216 | main.log.error(self.name+ ": EOF exception") |
| 217 | main.log.error(self.name+ ": " + self.handle.before) |
| 218 | main.cleanup() |
| 219 | main.exit() |
| 220 | except: |
| 221 | main.log.info(self.name+" :::::::") |
| 222 | main.log.error( traceback.print_exc()) |
| 223 | main.log.info(self.name+" :::::::") |
| 224 | main.cleanup() |
| 225 | main.exit() |
| 226 | |
| 227 | def get_datapath_id(self, sw_id): |
| 228 | ''' |
| 229 | Get datapath id of a specific switch by switch id |
| 230 | ''' |
| 231 | try: |
| 232 | self.handle.sendline("linc_logic:get_datapath_id("+ |
| 233 | str(sw_id)+").") |
| 234 | self.handle.expect("linc@") |
| 235 | |
| 236 | handle = self.handle.before |
| 237 | |
| 238 | except pexpect.EOF: |
| 239 | main.log.error(self.name+ ": EOF exception") |
| 240 | main.log.error(self.name+ ": " + self.handle.before) |
| 241 | main.cleanup() |
| 242 | main.exit() |
| 243 | except: |
| 244 | main.log.info(self.name+" :::::::") |
| 245 | main.log.error( traceback.print_exc()) |
| 246 | main.log.info(self.name+" :::::::") |
| 247 | main.cleanup() |
| 248 | main.exit() |
| 249 | |
| 250 | def list_ports(self, sw_id): |
| 251 | ''' |
| 252 | List all ports of a switch by switch id |
| 253 | ''' |
| 254 | try: |
| 255 | self.handle.sendline("linc:ports("+str(sw_id)+").") |
| 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: |
| 266 | main.log.info(self.name+" :::::::") |
| 267 | main.log.error( traceback.print_exc()) |
| 268 | main.log.info(self.name+" :::::::") |
| 269 | main.cleanup() |
| 270 | main.exit() |
| 271 | |
| 272 | def port_up(self, sw_id, pt_id): |
| 273 | ''' |
| 274 | Bring port up using switch id and port id |
| 275 | ''' |
| 276 | try: |
| 277 | self.handle.sendline("linc:port_up("+ |
| 278 | str(sw_id)+", "+str(pt_id)+").") |
| 279 | self.handle.expect("linc@") |
| 280 | |
| 281 | handle = self.handle.before |
| 282 | |
| 283 | except pexpect.EOF: |
| 284 | main.log.error(self.name+ ": EOF exception") |
| 285 | main.log.error(self.name+ ": " + self.handle.before) |
| 286 | main.cleanup() |
| 287 | main.exit() |
| 288 | except: |
| 289 | main.log.info(self.name+" :::::::") |
| 290 | main.log.error( traceback.print_exc()) |
| 291 | main.log.info(self.name+" :::::::") |
| 292 | main.cleanup() |
| 293 | main.exit() |
| 294 | |
| 295 | def port_down(self, sw_id, pt_id): |
| 296 | ''' |
| 297 | Bring port down using switch id and port id |
| 298 | ''' |
| 299 | try: |
| 300 | self.handle.sendline("linc:port_down("+ |
| 301 | str(sw_id)+", "+str(pt_id)+").") |
| 302 | self.handle.expect("linc@") |
| 303 | |
| 304 | handle = self.handle.before |
| 305 | |
| 306 | except pexpect.EOF: |
| 307 | main.log.error(self.name+ ": EOF exception") |
| 308 | main.log.error(self.name+ ": " + self.handle.before) |
| 309 | main.cleanup() |
| 310 | main.exit() |
| 311 | except: |
| 312 | main.log.info(self.name+" :::::::") |
| 313 | main.log.error( traceback.print_exc()) |
| 314 | main.log.info(self.name+" :::::::") |
| 315 | main.cleanup() |
| 316 | main.exit() |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 317 | |
| 318 | def stopLincOEConsole(self): |
| 319 | ''' |
| 320 | This function is only used for packet optical testing |
| 321 | Send disconnect prompt to Linc-OE CLI |
| 322 | (CTRL+C) and kill the linc process |
| 323 | ''' |
| 324 | try: |
| 325 | cmd = "pgrep -f linc" |
| 326 | self.handle.sendline("pgrep -f linc") |
| 327 | self.handle.expect("linc") |
| 328 | print "stophandle = ", self.handle.before |
| 329 | except pexpect.EOF: |
| 330 | main.log.error(self.name+ ": EOF exception") |
| 331 | main.log.error(self.name+ ": " + self.handle.before) |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 332 | |
andrewonlab | 0980f42 | 2014-10-21 21:28:39 -0400 | [diff] [blame] | 333 | def disconnect(self): |
| 334 | ''' |
| 335 | Send disconnect prompt to Linc-OE CLI |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 336 | (CTRL+C) and kill the linc process |
andrewonlab | 0980f42 | 2014-10-21 21:28:39 -0400 | [diff] [blame] | 337 | ''' |
| 338 | try: |
| 339 | #Send CTRL+C twice to exit CLI |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 340 | self.handle.send("\x03") |
| 341 | self.handle.send("\x03") |
andrewonlab | 0980f42 | 2014-10-21 21:28:39 -0400 | [diff] [blame] | 342 | self.handle.expect("\$") |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 343 | handle1 = self.handle.before |
| 344 | cmd = "pgrep -f linc" |
| 345 | self.handle.sendline(cmd) |
| 346 | self.handle.expect("\$") |
| 347 | handle2 = self.handle.before |
| 348 | main.log.info("pid's = "+handle2) |
| 349 | cmd = "sudo kill -9 `pgrep -f linc`" |
| 350 | self.handle.sendline(cmd) |
| 351 | self.handle.expect("\$") |
| 352 | |
andrewonlab | 0980f42 | 2014-10-21 21:28:39 -0400 | [diff] [blame] | 353 | except pexpect.EOF: |
| 354 | main.log.error(self.name+ ": EOF exception") |
| 355 | main.log.error(self.name+ ": " + self.handle.before) |
| 356 | main.cleanup() |
| 357 | main.exit() |
| 358 | except: |
| 359 | main.log.info(self.name+" :::::::") |
| 360 | main.log.error( traceback.print_exc()) |
| 361 | main.log.info(self.name+" :::::::") |
| 362 | main.cleanup() |
| 363 | main.exit() |
| 364 | |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 365 | if __name__ != "__main__": |
| 366 | import sys |
| 367 | sys.modules[__name__] = LincOEDriver() |
| 368 | |