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 |
| 13 | |
| 14 | OCT 20 2014 |
| 15 | ''' |
| 16 | |
| 17 | import traceback |
| 18 | import pexpect |
| 19 | import struct |
| 20 | import fcntl |
| 21 | import os |
| 22 | import signal |
| 23 | import re |
| 24 | import sys |
| 25 | import core.teston |
| 26 | sys.path.append("../") |
| 27 | from math import pow |
| 28 | from drivers.common.cli.emulatordriver import Emulator |
| 29 | from drivers.common.clidriver import CLI |
| 30 | |
| 31 | class 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 : |
andrewonlab | 52a31e0 | 2014-10-22 12:57:19 -0400 | [diff] [blame] | 59 | self.home = "~/linc-oe" |
| 60 | self.handle.sendline("cd "+self.home) |
| 61 | |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 62 | main.log.info(self.name+": Starting Linc-OE CLI") |
| 63 | cmdStr = "sudo ./rel/linc/bin/linc console" |
| 64 | |
| 65 | self.handle.sendline(cmdStr) |
andrewonlab | eb08b6f | 2014-10-21 21:23:15 -0400 | [diff] [blame] | 66 | #Sending blank lines "shows" the CLI |
| 67 | self.handle.sendline("") |
| 68 | self.handle.sendline("") |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 69 | 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 | |
andrewonlab | 52a31e0 | 2014-10-22 12:57:19 -0400 | [diff] [blame] | 79 | def build(self): |
| 80 | ''' |
| 81 | Build Linc-OE with the specified settings |
| 82 | ''' |
| 83 | try: |
| 84 | self.handle.sendline("make rel") |
| 85 | |
| 86 | return main.TRUE |
| 87 | |
| 88 | except pexpect.EOF: |
| 89 | main.log.error(self.name+ ": EOF exception") |
| 90 | main.log.error(self.name+ ": " + self.handle.before) |
| 91 | main.cleanup() |
| 92 | main.exit() |
| 93 | except: |
| 94 | main.log.info(self.name+" :::::::") |
| 95 | main.log.error( traceback.print_exc()) |
| 96 | main.log.info(self.name+" :::::::") |
| 97 | main.cleanup() |
| 98 | main.exit() |
| 99 | |
andrewonlab | eb08b6f | 2014-10-21 21:23:15 -0400 | [diff] [blame] | 100 | def set_interface_up(self, intfs): |
| 101 | ''' |
| 102 | Specify interface to bring up. |
| 103 | When Linc-OE is started, tap interfaces should |
| 104 | be created. They must be brought up manually |
| 105 | ''' |
| 106 | try: |
| 107 | self.handle.sendline("ifconfig "+str(intf)+" up") |
| 108 | self.handle.expect("linc@") |
| 109 | |
| 110 | handle = self.handle.before |
| 111 | |
| 112 | return handle |
| 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: |
| 120 | main.log.info(self.name+" :::::::") |
| 121 | main.log.error( traceback.print_exc()) |
| 122 | main.log.info(self.name+" :::::::") |
| 123 | main.cleanup() |
| 124 | main.exit() |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 125 | |
andrewonlab | 52a31e0 | 2014-10-22 12:57:19 -0400 | [diff] [blame] | 126 | def start_switch(self, sw_id): |
| 127 | ''' |
| 128 | Start a logical switch using switch id |
| 129 | ''' |
| 130 | try: |
| 131 | self.handle.sendline("linc:start_switch("+str(sw_id)+").") |
| 132 | self.handle.expect("linc@") |
| 133 | |
| 134 | handle = self.handle.before |
| 135 | |
| 136 | except pexpect.EOF: |
| 137 | main.log.error(self.name+ ": EOF exception") |
| 138 | main.log.error(self.name+ ": " + self.handle.before) |
| 139 | main.cleanup() |
| 140 | main.exit() |
| 141 | except: |
| 142 | main.log.info(self.name+" :::::::") |
| 143 | main.log.error( traceback.print_exc()) |
| 144 | main.log.info(self.name+" :::::::") |
| 145 | main.cleanup() |
| 146 | main.exit() |
| 147 | |
| 148 | def stop_switch(self, sw_id): |
| 149 | ''' |
| 150 | Stop a logical switch using switch id |
| 151 | ''' |
| 152 | try: |
| 153 | self.handle.sendline("linc:stop_switch("+str(sw_id)+").") |
| 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: |
| 164 | main.log.info(self.name+" :::::::") |
| 165 | main.log.error( traceback.print_exc()) |
| 166 | main.log.info(self.name+" :::::::") |
| 167 | main.cleanup() |
| 168 | main.exit() |
| 169 | |
| 170 | def get_datapath_id(self, sw_id): |
| 171 | ''' |
| 172 | Get datapath id of a specific switch by switch id |
| 173 | ''' |
| 174 | try: |
| 175 | self.handle.sendline("linc_logic:get_datapath_id("+ |
| 176 | str(sw_id)+").") |
| 177 | self.handle.expect("linc@") |
| 178 | |
| 179 | handle = self.handle.before |
| 180 | |
| 181 | except pexpect.EOF: |
| 182 | main.log.error(self.name+ ": EOF exception") |
| 183 | main.log.error(self.name+ ": " + self.handle.before) |
| 184 | main.cleanup() |
| 185 | main.exit() |
| 186 | except: |
| 187 | main.log.info(self.name+" :::::::") |
| 188 | main.log.error( traceback.print_exc()) |
| 189 | main.log.info(self.name+" :::::::") |
| 190 | main.cleanup() |
| 191 | main.exit() |
| 192 | |
| 193 | def list_ports(self, sw_id): |
| 194 | ''' |
| 195 | List all ports of a switch by switch id |
| 196 | ''' |
| 197 | try: |
| 198 | self.handle.sendline("linc:ports("+str(sw_id)+").") |
| 199 | self.handle.expect("linc@") |
| 200 | |
| 201 | handle = self.handle.before |
| 202 | |
| 203 | except pexpect.EOF: |
| 204 | main.log.error(self.name+ ": EOF exception") |
| 205 | main.log.error(self.name+ ": " + self.handle.before) |
| 206 | main.cleanup() |
| 207 | main.exit() |
| 208 | except: |
| 209 | main.log.info(self.name+" :::::::") |
| 210 | main.log.error( traceback.print_exc()) |
| 211 | main.log.info(self.name+" :::::::") |
| 212 | main.cleanup() |
| 213 | main.exit() |
| 214 | |
| 215 | def port_up(self, sw_id, pt_id): |
| 216 | ''' |
| 217 | Bring port up using switch id and port id |
| 218 | ''' |
| 219 | try: |
| 220 | self.handle.sendline("linc:port_up("+ |
| 221 | str(sw_id)+", "+str(pt_id)+").") |
| 222 | self.handle.expect("linc@") |
| 223 | |
| 224 | handle = self.handle.before |
| 225 | |
| 226 | except pexpect.EOF: |
| 227 | main.log.error(self.name+ ": EOF exception") |
| 228 | main.log.error(self.name+ ": " + self.handle.before) |
| 229 | main.cleanup() |
| 230 | main.exit() |
| 231 | except: |
| 232 | main.log.info(self.name+" :::::::") |
| 233 | main.log.error( traceback.print_exc()) |
| 234 | main.log.info(self.name+" :::::::") |
| 235 | main.cleanup() |
| 236 | main.exit() |
| 237 | |
| 238 | def port_down(self, sw_id, pt_id): |
| 239 | ''' |
| 240 | Bring port down using switch id and port id |
| 241 | ''' |
| 242 | try: |
| 243 | self.handle.sendline("linc:port_down("+ |
| 244 | str(sw_id)+", "+str(pt_id)+").") |
| 245 | self.handle.expect("linc@") |
| 246 | |
| 247 | handle = self.handle.before |
| 248 | |
| 249 | except pexpect.EOF: |
| 250 | main.log.error(self.name+ ": EOF exception") |
| 251 | main.log.error(self.name+ ": " + self.handle.before) |
| 252 | main.cleanup() |
| 253 | main.exit() |
| 254 | except: |
| 255 | main.log.info(self.name+" :::::::") |
| 256 | main.log.error( traceback.print_exc()) |
| 257 | main.log.info(self.name+" :::::::") |
| 258 | main.cleanup() |
| 259 | main.exit() |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 260 | |
andrewonlab | 0980f42 | 2014-10-21 21:28:39 -0400 | [diff] [blame] | 261 | def disconnect(self): |
| 262 | ''' |
| 263 | Send disconnect prompt to Linc-OE CLI |
| 264 | (CTRL+C) |
| 265 | ''' |
| 266 | try: |
| 267 | #Send CTRL+C twice to exit CLI |
| 268 | self.handle.sendline("\x03") |
| 269 | self.handle.sendline("\x03") |
| 270 | self.handle.expect("\$") |
| 271 | |
| 272 | except pexpect.EOF: |
| 273 | main.log.error(self.name+ ": EOF exception") |
| 274 | main.log.error(self.name+ ": " + self.handle.before) |
| 275 | main.cleanup() |
| 276 | main.exit() |
| 277 | except: |
| 278 | main.log.info(self.name+" :::::::") |
| 279 | main.log.error( traceback.print_exc()) |
| 280 | main.log.info(self.name+" :::::::") |
| 281 | main.cleanup() |
| 282 | main.exit() |
| 283 | |
andrewonlab | a548f96 | 2014-10-21 19:28:43 -0400 | [diff] [blame] | 284 | if __name__ != "__main__": |
| 285 | import sys |
| 286 | sys.modules[__name__] = LincOEDriver() |
| 287 | |