andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | ''' |
| 4 | This driver enters the onos> prompt to issue commands. |
| 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 | jhall@onlab.us |
| 13 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 14 | shreya@onlab.us |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 15 | |
| 16 | OCT 13 2014 |
| 17 | |
| 18 | ''' |
| 19 | |
| 20 | import sys |
| 21 | import time |
| 22 | import pexpect |
| 23 | import re |
| 24 | import traceback |
| 25 | import os.path |
| 26 | import pydoc |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 27 | import re |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 28 | sys.path.append("../") |
| 29 | from drivers.common.clidriver import CLI |
| 30 | |
| 31 | class OnosCliDriver(CLI): |
| 32 | |
| 33 | def __init__(self): |
| 34 | ''' |
| 35 | Initialize client |
| 36 | ''' |
| 37 | super(CLI, self).__init__() |
| 38 | |
| 39 | def connect(self,**connectargs): |
| 40 | ''' |
| 41 | Creates ssh handle for ONOS cli. |
| 42 | ''' |
| 43 | try: |
| 44 | for key in connectargs: |
| 45 | vars(self)[key] = connectargs[key] |
| 46 | self.home = "~/ONOS" |
| 47 | for key in self.options: |
| 48 | if key == "home": |
| 49 | self.home = self.options['home'] |
| 50 | break |
| 51 | |
| 52 | |
| 53 | self.name = self.options['name'] |
| 54 | self.handle = super(OnosCliDriver,self).connect( |
| 55 | user_name = self.user_name, |
| 56 | ip_address = self.ip_address, |
| 57 | port = self.port, |
| 58 | pwd = self.pwd, |
| 59 | home = self.home) |
| 60 | |
| 61 | self.handle.sendline("cd "+ self.home) |
| 62 | self.handle.expect("\$") |
| 63 | if self.handle: |
| 64 | return self.handle |
| 65 | else : |
| 66 | main.log.info("NO ONOS HANDLE") |
| 67 | return main.FALSE |
| 68 | except pexpect.EOF: |
| 69 | main.log.error(self.name + ": EOF exception found") |
| 70 | main.log.error(self.name + ": " + self.handle.before) |
| 71 | main.cleanup() |
| 72 | main.exit() |
| 73 | except: |
| 74 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 75 | main.log.error( traceback.print_exc() ) |
| 76 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 77 | main.cleanup() |
| 78 | main.exit() |
| 79 | |
| 80 | def disconnect(self): |
| 81 | ''' |
| 82 | Called when Test is complete to disconnect the ONOS handle. |
| 83 | ''' |
| 84 | response = '' |
| 85 | try: |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 86 | self.handle.sendline("") |
Jon Hall | 7e5b917 | 2014-10-22 12:32:47 -0400 | [diff] [blame] | 87 | i = self.handle.expect(["onos>","\$"]) |
| 88 | if i == 0: |
| 89 | self.handle.sendline("system:shutdown") |
| 90 | self.handle.expect("Confirm") |
| 91 | self.handle.sendline("yes") |
| 92 | self.handle.expect("\$") |
| 93 | self.handle.sendline("\n") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 94 | self.handle.expect("\$") |
Jon Hall | 7e5b917 | 2014-10-22 12:32:47 -0400 | [diff] [blame] | 95 | self.handle.sendline("exit") |
| 96 | self.handle.expect("closed") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 97 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 98 | except pexpect.EOF: |
| 99 | main.log.error(self.name + ": EOF exception found") |
| 100 | main.log.error(self.name + ": " + self.handle.before) |
| 101 | except: |
| 102 | main.log.error(self.name + ": Connection failed to the host") |
| 103 | response = main.FALSE |
| 104 | return response |
| 105 | |
| 106 | def set_cell(self, cellname): |
| 107 | ''' |
| 108 | Calls 'cell <name>' to set the environment variables on ONOSbench |
| 109 | |
| 110 | Before issuing any cli commands, set the environment variable first. |
| 111 | ''' |
| 112 | try: |
| 113 | if not cellname: |
| 114 | main.log.error("Must define cellname") |
| 115 | main.cleanup() |
| 116 | main.exit() |
| 117 | else: |
| 118 | self.handle.sendline("cell "+str(cellname)) |
| 119 | #Expect the cellname in the ONOS_CELL variable. |
| 120 | #Note that this variable name is subject to change |
| 121 | # and that this driver will have to change accordingly |
| 122 | self.handle.expect("ONOS_CELL="+str(cellname)) |
| 123 | handle_before = self.handle.before |
| 124 | handle_after = self.handle.after |
| 125 | #Get the rest of the handle |
| 126 | self.handle.sendline("") |
| 127 | self.handle.expect("\$") |
| 128 | handle_more = self.handle.before |
| 129 | |
| 130 | main.log.info("Cell call returned: "+handle_before+ |
| 131 | handle_after + handle_more) |
| 132 | |
| 133 | return main.TRUE |
| 134 | |
| 135 | except pexpect.EOF: |
| 136 | main.log.error(self.name + ": EOF exception found") |
| 137 | main.log.error(self.name + ": " + self.handle.before) |
| 138 | main.cleanup() |
| 139 | main.exit() |
| 140 | except: |
| 141 | main.log.info(self.name+" ::::::") |
| 142 | main.log.error( traceback.print_exc()) |
| 143 | main.log.info(self.name+" ::::::") |
| 144 | main.cleanup() |
| 145 | main.exit() |
| 146 | |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 147 | def start_onos_cli(self, ONOS_ip): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 148 | try: |
| 149 | self.handle.sendline("") |
| 150 | self.handle.expect("\$") |
| 151 | |
| 152 | #Wait for onos start (-w) and enter onos cli |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 153 | self.handle.sendline("onos -w "+str(ONOS_ip)) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 154 | i = self.handle.expect([ |
| 155 | "onos>", |
| 156 | pexpect.TIMEOUT],timeout=60) |
| 157 | |
| 158 | if i == 0: |
| 159 | main.log.info(str(ONOS_ip)+" CLI Started successfully") |
| 160 | return main.TRUE |
| 161 | else: |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 162 | #If failed, send ctrl+c to process and try again |
andrewonlab | f47993a | 2014-10-24 17:56:01 -0400 | [diff] [blame] | 163 | main.log.info("Starting CLI failed. Retrying...") |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 164 | self.handle.sendline("\x03") |
| 165 | self.handle.sendline("onos -w "+str(ONOS_ip)) |
| 166 | i = self.handle.expect(["onos>",pexpect.TIMEOUT], |
| 167 | timeout=30) |
| 168 | if i == 0: |
| 169 | return main.TRUE |
| 170 | else: |
| 171 | main.log.error("Connection to CLI "+\ |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 172 | str(ONOS_ip)+" timeout") |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 173 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 174 | |
| 175 | except pexpect.EOF: |
| 176 | main.log.error(self.name + ": EOF exception found") |
| 177 | main.log.error(self.name + ": " + self.handle.before) |
| 178 | main.cleanup() |
| 179 | main.exit() |
| 180 | except: |
| 181 | main.log.info(self.name+" ::::::") |
| 182 | main.log.error( traceback.print_exc()) |
| 183 | main.log.info(self.name+" ::::::") |
| 184 | main.cleanup() |
| 185 | main.exit() |
| 186 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 187 | def sendline(self, cmd_str): |
| 188 | ''' |
| 189 | Send a completely user specified string to |
| 190 | the onos> prompt. Use this function if you have |
| 191 | a very specific command to send. |
| 192 | |
| 193 | Warning: There are no sanity checking to commands |
| 194 | sent using this method. |
| 195 | ''' |
| 196 | try: |
| 197 | self.handle.sendline("") |
| 198 | self.handle.expect("onos>") |
| 199 | |
| 200 | self.handle.sendline(cmd_str) |
| 201 | self.handle.expect("onos>") |
| 202 | |
| 203 | handle = self.handle.before |
| 204 | |
| 205 | self.handle.sendline("") |
| 206 | self.handle.expect("onos>") |
| 207 | |
| 208 | handle += self.handle.before |
| 209 | handle += self.handle.after |
| 210 | |
| 211 | main.log.info("Command sent.") |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 212 | ansi_escape = re.compile(r'\x1b[^m]*m') |
| 213 | handle = ansi_escape.sub('', handle) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 214 | |
| 215 | return handle |
| 216 | except pexpect.EOF: |
| 217 | main.log.error(self.name + ": EOF exception found") |
| 218 | main.log.error(self.name + ": " + self.handle.before) |
| 219 | main.cleanup() |
| 220 | main.exit() |
| 221 | except: |
| 222 | main.log.info(self.name+" ::::::") |
| 223 | main.log.error( traceback.print_exc()) |
| 224 | main.log.info(self.name+" ::::::") |
| 225 | main.cleanup() |
| 226 | main.exit() |
| 227 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 228 | #IMPORTANT NOTE: |
| 229 | #For all cli commands, naming convention should match |
| 230 | #the cli command replacing ':' with '_'. |
| 231 | #Ex) onos:topology > onos_topology |
| 232 | # onos:links > onos_links |
| 233 | # feature:list > feature_list |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 234 | |
| 235 | def add_node(self, node_id, ONOS_ip, tcp_port=""): |
| 236 | ''' |
| 237 | Adds a new cluster node by ID and address information. |
| 238 | Required: |
| 239 | * node_id |
| 240 | * ONOS_ip |
| 241 | Optional: |
| 242 | * tcp_port |
| 243 | ''' |
| 244 | try: |
| 245 | self.handle.sendline("") |
| 246 | self.handle.expect("onos>") |
| 247 | |
| 248 | self.handle.sendline("add-node "+ |
| 249 | str(node_id)+" "+ |
| 250 | str(ONOS_ip)+" "+ |
| 251 | str(tcp_port)) |
| 252 | |
| 253 | i = self.handle.expect([ |
| 254 | "Error", |
| 255 | "onos>" ]) |
| 256 | |
| 257 | #Clear handle to get previous output |
| 258 | self.handle.sendline("") |
| 259 | self.handle.expect("onos>") |
| 260 | |
| 261 | handle = self.handle.before |
| 262 | |
| 263 | if i == 0: |
| 264 | main.log.error("Error in adding node") |
| 265 | main.log.error(handle) |
| 266 | return main.FALSE |
| 267 | else: |
| 268 | main.log.info("Node "+str(ONOS_ip)+" added") |
| 269 | return main.TRUE |
| 270 | |
| 271 | except pexpect.EOF: |
| 272 | main.log.error(self.name + ": EOF exception found") |
| 273 | main.log.error(self.name + ": " + self.handle.before) |
| 274 | main.cleanup() |
| 275 | main.exit() |
| 276 | except: |
| 277 | main.log.info(self.name+" ::::::") |
| 278 | main.log.error( traceback.print_exc()) |
| 279 | main.log.info(self.name+" ::::::") |
| 280 | main.cleanup() |
| 281 | main.exit() |
| 282 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 283 | def remove_node(self, node_id): |
| 284 | ''' |
| 285 | Removes a cluster by ID |
| 286 | Issues command: 'remove-node [<node-id>]' |
| 287 | Required: |
| 288 | * node_id |
| 289 | ''' |
| 290 | try: |
| 291 | self.handle.sendline("") |
| 292 | self.handle.expect("onos>") |
| 293 | |
| 294 | self.handle.sendline("remove-node "+str(node_id)) |
| 295 | self.handle.expect("onos>") |
| 296 | |
| 297 | return main.TRUE |
| 298 | |
| 299 | except pexpect.EOF: |
| 300 | main.log.error(self.name + ": EOF exception found") |
| 301 | main.log.error(self.name + ": " + self.handle.before) |
| 302 | main.cleanup() |
| 303 | main.exit() |
| 304 | except: |
| 305 | main.log.info(self.name+" ::::::") |
| 306 | main.log.error( traceback.print_exc()) |
| 307 | main.log.info(self.name+" ::::::") |
| 308 | main.cleanup() |
| 309 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 310 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 311 | def nodes(self): |
| 312 | ''' |
| 313 | List the nodes currently visible |
| 314 | Issues command: 'nodes' |
| 315 | Returns: entire handle of list of nodes |
| 316 | ''' |
| 317 | try: |
| 318 | self.handle.sendline("") |
| 319 | self.handle.expect("onos>") |
| 320 | |
| 321 | self.handle.sendline("nodes") |
| 322 | self.handle.expect("onos>") |
| 323 | |
| 324 | self.handle.sendline("") |
| 325 | self.handle.expect("onos>") |
| 326 | |
| 327 | handle = self.handle.before |
| 328 | |
| 329 | return handle |
| 330 | |
| 331 | except pexpect.EOF: |
| 332 | main.log.error(self.name + ": EOF exception found") |
| 333 | main.log.error(self.name + ": " + self.handle.before) |
| 334 | main.cleanup() |
| 335 | main.exit() |
| 336 | except: |
| 337 | main.log.info(self.name+" ::::::") |
| 338 | main.log.error( traceback.print_exc()) |
| 339 | main.log.info(self.name+" ::::::") |
| 340 | main.cleanup() |
| 341 | main.exit() |
| 342 | |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 343 | def topology(self): |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 344 | ''' |
| 345 | Shows the current state of the topology |
| 346 | by issusing command: 'onos> onos:topology' |
| 347 | ''' |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 348 | try: |
| 349 | self.handle.sendline("") |
| 350 | self.handle.expect("onos>") |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 351 | #either onos:topology or 'topology' will work in CLI |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 352 | self.handle.sendline("onos:topology") |
| 353 | self.handle.expect("onos>") |
| 354 | |
| 355 | handle = self.handle.before |
| 356 | |
| 357 | main.log.info("onos:topology returned: " + |
| 358 | str(handle)) |
| 359 | |
| 360 | return handle |
| 361 | |
| 362 | except pexpect.EOF: |
| 363 | main.log.error(self.name + ": EOF exception found") |
| 364 | main.log.error(self.name + ": " + self.handle.before) |
| 365 | main.cleanup() |
| 366 | main.exit() |
| 367 | except: |
| 368 | main.log.info(self.name+" ::::::") |
| 369 | main.log.error( traceback.print_exc()) |
| 370 | main.log.info(self.name+" ::::::") |
| 371 | main.cleanup() |
| 372 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 373 | |
| 374 | def feature_install(self, feature_str): |
| 375 | ''' |
| 376 | Installs a specified feature |
| 377 | by issuing command: 'onos> feature:install <feature_str>' |
| 378 | ''' |
| 379 | try: |
| 380 | self.handle.sendline("") |
| 381 | self.handle.expect("onos>") |
| 382 | |
| 383 | self.handle.sendline("feature:install "+str(feature_str)) |
| 384 | self.handle.expect("onos>") |
| 385 | |
| 386 | return main.TRUE |
| 387 | |
| 388 | except pexpect.EOF: |
| 389 | main.log.error(self.name + ": EOF exception found") |
| 390 | main.log.error(self.name + ": " + self.handle.before) |
| 391 | main.cleanup() |
| 392 | main.exit() |
| 393 | except: |
| 394 | main.log.info(self.name+" ::::::") |
| 395 | main.log.error( traceback.print_exc()) |
| 396 | main.log.info(self.name+" ::::::") |
| 397 | main.cleanup() |
| 398 | main.exit() |
| 399 | |
| 400 | def feature_uninstall(self, feature_str): |
| 401 | ''' |
| 402 | Uninstalls a specified feature |
| 403 | by issuing command: 'onos> feature:uninstall <feature_str>' |
| 404 | ''' |
| 405 | try: |
| 406 | self.handle.sendline("") |
| 407 | self.handle.expect("onos>") |
| 408 | |
| 409 | self.handle.sendline("feature:uninstall "+str(feature_str)) |
| 410 | self.handle.expect("onos>") |
| 411 | |
| 412 | return main.TRUE |
| 413 | |
| 414 | except pexpect.EOF: |
| 415 | main.log.error(self.name + ": EOF exception found") |
| 416 | main.log.error(self.name + ": " + self.handle.before) |
| 417 | main.cleanup() |
| 418 | main.exit() |
| 419 | except: |
| 420 | main.log.info(self.name+" ::::::") |
| 421 | main.log.error( traceback.print_exc()) |
| 422 | main.log.info(self.name+" ::::::") |
| 423 | main.cleanup() |
| 424 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 425 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 426 | def devices(self, json_format=True, grep_str=""): |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 427 | ''' |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 428 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 429 | Optional argument: |
| 430 | * grep_str - pass in a string to grep |
| 431 | ''' |
| 432 | try: |
| 433 | self.handle.sendline("") |
| 434 | self.handle.expect("onos>") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 435 | |
| 436 | if json_format: |
| 437 | if not grep_str: |
| 438 | self.handle.sendline("devices -j") |
| 439 | self.handle.expect("devices -j") |
| 440 | self.handle.expect("onos>") |
| 441 | else: |
| 442 | self.handle.sendline("devices -j | grep '"+ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 443 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 444 | self.handle.expect("devices -j | grep '"+str(grep_str)+"'") |
| 445 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 446 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 447 | ''' |
| 448 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 449 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
Jon Hall | 5227ce8 | 2014-10-17 20:09:51 -0400 | [diff] [blame] | 450 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 451 | So we take off that escape sequence using |
| 452 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 453 | handle1 = ansi_escape.sub('', handle) |
| 454 | ''' |
| 455 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 456 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 457 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 458 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 459 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 460 | else: |
| 461 | if not grep_str: |
| 462 | self.handle.sendline("devices") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 463 | self.handle.expect("onos>") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 464 | else: |
| 465 | self.handle.sendline("devices | grep '"+ |
| 466 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 467 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 468 | handle = self.handle.before |
| 469 | print "handle =",handle |
| 470 | return handle |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 471 | except pexpect.EOF: |
| 472 | main.log.error(self.name + ": EOF exception found") |
| 473 | main.log.error(self.name + ": " + self.handle.before) |
| 474 | main.cleanup() |
| 475 | main.exit() |
| 476 | except: |
| 477 | main.log.info(self.name+" ::::::") |
| 478 | main.log.error( traceback.print_exc()) |
| 479 | main.log.info(self.name+" ::::::") |
| 480 | main.cleanup() |
| 481 | main.exit() |
| 482 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 483 | def links(self, json_format=True, grep_str=""): |
| 484 | ''' |
| 485 | Lists all core links |
| 486 | Optional argument: |
| 487 | * grep_str - pass in a string to grep |
| 488 | ''' |
| 489 | try: |
| 490 | self.handle.sendline("") |
| 491 | self.handle.expect("onos>") |
| 492 | |
| 493 | if json_format: |
| 494 | if not grep_str: |
| 495 | self.handle.sendline("links -j") |
| 496 | self.handle.expect("links -j") |
| 497 | self.handle.expect("onos>") |
| 498 | else: |
| 499 | self.handle.sendline("links -j | grep '"+ |
| 500 | str(grep_str)+"'") |
| 501 | self.handle.expect("links -j | grep '"+str(grep_str)+"'") |
| 502 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 503 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 504 | ''' |
| 505 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 506 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
Jon Hall | 5227ce8 | 2014-10-17 20:09:51 -0400 | [diff] [blame] | 507 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 508 | So we take off that escape sequence using |
| 509 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 510 | handle1 = ansi_escape.sub('', handle) |
| 511 | ''' |
| 512 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 513 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 514 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 515 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 516 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 517 | else: |
| 518 | if not grep_str: |
| 519 | self.handle.sendline("links") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 520 | self.handle.expect("onos>") |
| 521 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 522 | self.handle.expect("onos>") |
| 523 | else: |
| 524 | self.handle.sendline("links | grep '"+ |
| 525 | str(grep_str)+"'") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 526 | self.handle.expect("onos>") |
| 527 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 528 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 529 | handle = self.handle.before |
| 530 | print "handle =",handle |
| 531 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 532 | except pexpect.EOF: |
| 533 | main.log.error(self.name + ": EOF exception found") |
| 534 | main.log.error(self.name + ": " + self.handle.before) |
| 535 | main.cleanup() |
| 536 | main.exit() |
| 537 | except: |
| 538 | main.log.info(self.name+" ::::::") |
| 539 | main.log.error( traceback.print_exc()) |
| 540 | main.log.info(self.name+" ::::::") |
| 541 | main.cleanup() |
| 542 | main.exit() |
| 543 | |
| 544 | |
| 545 | def ports(self, json_format=True, grep_str=""): |
| 546 | ''' |
| 547 | Lists all ports |
| 548 | Optional argument: |
| 549 | * grep_str - pass in a string to grep |
| 550 | ''' |
| 551 | try: |
| 552 | self.handle.sendline("") |
| 553 | self.handle.expect("onos>") |
| 554 | |
| 555 | if json_format: |
| 556 | if not grep_str: |
| 557 | self.handle.sendline("ports -j") |
| 558 | self.handle.expect("ports -j") |
| 559 | self.handle.expect("onos>") |
| 560 | else: |
| 561 | self.handle.sendline("ports -j | grep '"+ |
| 562 | str(grep_str)+"'") |
| 563 | self.handle.expect("ports -j | grep '"+str(grep_str)+"'") |
| 564 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 565 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 566 | ''' |
| 567 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 568 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
Jon Hall | 5227ce8 | 2014-10-17 20:09:51 -0400 | [diff] [blame] | 569 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Shreya Shah | 0c525cc | 2014-10-17 20:20:24 -0400 | [diff] [blame] | 570 | So we take off that escape sequence using the following commads: |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 571 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 572 | handle1 = ansi_escape.sub('', handle) |
| 573 | ''' |
| 574 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 575 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 576 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 577 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 578 | return handle1 |
| 579 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 580 | else: |
| 581 | if not grep_str: |
| 582 | self.handle.sendline("ports") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 583 | self.handle.expect("onos>") |
| 584 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 585 | self.handle.expect("onos>") |
| 586 | else: |
| 587 | self.handle.sendline("ports | grep '"+ |
| 588 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 589 | self.handle.expect("onos>") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 590 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 591 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 592 | handle = self.handle.before |
| 593 | print "handle =",handle |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 594 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 595 | except pexpect.EOF: |
| 596 | main.log.error(self.name + ": EOF exception found") |
| 597 | main.log.error(self.name + ": " + self.handle.before) |
| 598 | main.cleanup() |
| 599 | main.exit() |
| 600 | except: |
| 601 | main.log.info(self.name+" ::::::") |
| 602 | main.log.error( traceback.print_exc()) |
| 603 | main.log.info(self.name+" ::::::") |
| 604 | main.cleanup() |
| 605 | main.exit() |
| 606 | |
| 607 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 608 | def device_role(self, device_id, node_id, role): |
| 609 | ''' |
| 610 | Set device role for specified device and node with role |
| 611 | Required: |
| 612 | * device_id : may be obtained by function get_all_devices_id |
| 613 | * node_id : may be obtained by function get_all_nodes_id |
| 614 | * role: specify one of the following roles: |
| 615 | - master |
| 616 | - standby |
| 617 | - none |
| 618 | ''' |
| 619 | try: |
| 620 | self.handle.sendline("") |
| 621 | self.handle.expect("onos>") |
| 622 | |
| 623 | self.handle.sendline("device-role "+ |
| 624 | str(device_id) + " " + |
| 625 | str(node_id) + " " + |
| 626 | str(role)) |
| 627 | i = self.handle.expect([ |
| 628 | "Error", |
| 629 | "onos>"]) |
| 630 | |
| 631 | self.handle.sendline("") |
| 632 | self.handle.expect("onos>") |
| 633 | |
| 634 | handle = self.handle.before |
| 635 | |
| 636 | if i == 0: |
| 637 | main.log.error("device-role command returned error") |
| 638 | return handle |
| 639 | else: |
| 640 | return main.TRUE |
| 641 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 642 | except pexpect.EOF: |
| 643 | main.log.error(self.name + ": EOF exception found") |
| 644 | main.log.error(self.name + ": " + self.handle.before) |
| 645 | main.cleanup() |
| 646 | main.exit() |
| 647 | except: |
| 648 | main.log.info(self.name+" ::::::") |
| 649 | main.log.error( traceback.print_exc()) |
| 650 | main.log.info(self.name+" ::::::") |
| 651 | main.cleanup() |
| 652 | main.exit() |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 653 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 654 | def paths(self, src_id, dst_id): |
| 655 | ''' |
| 656 | Returns string of paths, and the cost. |
| 657 | Issues command: onos:paths <src> <dst> |
| 658 | ''' |
| 659 | try: |
| 660 | self.handle.sendline("") |
| 661 | self.handle.expect("onos>") |
| 662 | |
| 663 | self.handle.sendline("onos:paths "+ |
| 664 | str(src_id) + " " + str(dst_id)) |
| 665 | i = self.handle.expect([ |
| 666 | "Error", |
| 667 | "onos>"]) |
| 668 | |
| 669 | self.handle.sendline("") |
| 670 | self.handle.expect("onos>") |
| 671 | |
| 672 | handle = self.handle.before |
| 673 | |
| 674 | if i == 0: |
| 675 | main.log.error("Error in getting paths") |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 676 | return (handle, "Error") |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 677 | else: |
| 678 | path = handle.split(";")[0] |
| 679 | cost = handle.split(";")[1] |
| 680 | return (path, cost) |
| 681 | |
| 682 | except pexpect.EOF: |
| 683 | main.log.error(self.name + ": EOF exception found") |
| 684 | main.log.error(self.name + ": " + self.handle.before) |
| 685 | main.cleanup() |
| 686 | main.exit() |
| 687 | except: |
| 688 | main.log.info(self.name+" ::::::") |
| 689 | main.log.error( traceback.print_exc()) |
| 690 | main.log.info(self.name+" ::::::") |
| 691 | main.cleanup() |
| 692 | main.exit() |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 693 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 694 | def hosts(self, json_format=True, grep_str=""): |
| 695 | ''' |
| 696 | Lists all discovered hosts |
| 697 | Optional argument: |
| 698 | * grep_str - pass in a string to grep |
| 699 | ''' |
| 700 | try: |
| 701 | self.handle.sendline("") |
| 702 | self.handle.expect("onos>") |
| 703 | |
| 704 | if json_format: |
| 705 | if not grep_str: |
| 706 | self.handle.sendline("hosts -j") |
| 707 | self.handle.expect("hosts -j") |
| 708 | self.handle.expect("onos>") |
| 709 | else: |
| 710 | self.handle.sendline("hosts -j | grep '"+ |
| 711 | str(grep_str)+"'") |
| 712 | self.handle.expect("hosts -j | grep '"+str(grep_str)+"'") |
| 713 | self.handle.expect("onos>") |
| 714 | handle = self.handle.before |
| 715 | ''' |
| 716 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 717 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
| 718 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
| 719 | So we take off that escape sequence using |
| 720 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 721 | handle1 = ansi_escape.sub('', handle) |
| 722 | ''' |
| 723 | #print "repr(handle) =", repr(handle) |
| 724 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 725 | handle1 = ansi_escape.sub('', handle) |
| 726 | #print "repr(handle1) = ", repr(handle1) |
| 727 | return handle1 |
| 728 | else: |
| 729 | if not grep_str: |
| 730 | self.handle.sendline("hosts") |
| 731 | self.handle.expect("onos>") |
| 732 | else: |
| 733 | self.handle.sendline("hosts | grep '"+ |
| 734 | str(grep_str)+"'") |
| 735 | self.handle.expect("onos>") |
| 736 | handle = self.handle.before |
| 737 | print "handle =",handle |
| 738 | return handle |
| 739 | except pexpect.EOF: |
| 740 | main.log.error(self.name + ": EOF exception found") |
| 741 | main.log.error(self.name + ": " + self.handle.before) |
| 742 | main.cleanup() |
| 743 | main.exit() |
| 744 | except: |
| 745 | main.log.info(self.name+" ::::::") |
| 746 | main.log.error( traceback.print_exc()) |
| 747 | main.log.info(self.name+" ::::::") |
| 748 | main.cleanup() |
| 749 | main.exit() |
| 750 | |
| 751 | def get_host(self, mac): |
| 752 | ''' |
| 753 | Return the first host from the hosts api whose 'id' contains 'mac' |
| 754 | Note: mac must be a colon seperated mac address, but could be a partial mac address |
| 755 | Return None if there is no match |
| 756 | ''' |
| 757 | import json |
| 758 | try: |
| 759 | if mac == None: |
| 760 | return None |
| 761 | else: |
| 762 | mac = mac |
| 763 | raw_hosts = self.hosts() |
| 764 | hosts_json = json.loads(raw_hosts) |
| 765 | #search json for the host with mac then return the device |
| 766 | for host in hosts_json: |
| 767 | print "%s in %s?" % (mac, host['id']) |
| 768 | if mac in host['id']: |
| 769 | return host |
| 770 | return None |
| 771 | except pexpect.EOF: |
| 772 | main.log.error(self.name + ": EOF exception found") |
| 773 | main.log.error(self.name + ": " + self.handle.before) |
| 774 | main.cleanup() |
| 775 | main.exit() |
| 776 | except: |
| 777 | main.log.info(self.name+" ::::::") |
| 778 | main.log.error( traceback.print_exc()) |
| 779 | main.log.info(self.name+" ::::::") |
| 780 | main.cleanup() |
| 781 | main.exit() |
| 782 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 783 | |
| 784 | def get_hosts_id(self, host_list): |
| 785 | ''' |
| 786 | Obtain list of hosts |
| 787 | Issues command: 'onos> hosts' |
| 788 | |
| 789 | Required: |
| 790 | * host_list: List of hosts obtained by Mininet |
| 791 | IMPORTANT: |
| 792 | This function assumes that you started your |
| 793 | topology with the option '--mac'. |
| 794 | Furthermore, it assumes that value of VLAN is '-1' |
| 795 | Description: |
| 796 | Converts mininet hosts (h1, h2, h3...) into |
| 797 | ONOS format (00:00:00:00:00:01/-1 , ...) |
| 798 | ''' |
| 799 | |
| 800 | try: |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 801 | onos_host_list = [] |
| 802 | |
| 803 | for host in host_list: |
| 804 | host = host.replace("h", "") |
| 805 | host_hex = hex(int(host)).zfill(12) |
| 806 | host_hex = str(host_hex).replace('x','0') |
| 807 | i = iter(str(host_hex)) |
| 808 | host_hex = ":".join(a+b for a,b in zip(i,i)) |
| 809 | host_hex = host_hex + "/-1" |
| 810 | onos_host_list.append(host_hex) |
| 811 | |
| 812 | return onos_host_list |
| 813 | |
| 814 | except pexpect.EOF: |
| 815 | main.log.error(self.name + ": EOF exception found") |
| 816 | main.log.error(self.name + ": " + self.handle.before) |
| 817 | main.cleanup() |
| 818 | main.exit() |
| 819 | except: |
| 820 | main.log.info(self.name+" ::::::") |
| 821 | main.log.error( traceback.print_exc()) |
| 822 | main.log.info(self.name+" ::::::") |
| 823 | main.cleanup() |
| 824 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 825 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 826 | def add_host_intent(self, host_id_one, host_id_two): |
| 827 | ''' |
| 828 | Required: |
| 829 | * host_id_one: ONOS host id for host1 |
| 830 | * host_id_two: ONOS host id for host2 |
| 831 | Description: |
| 832 | Adds a host-to-host intent (bidrectional) by |
| 833 | specifying the two hosts. |
| 834 | ''' |
| 835 | try: |
| 836 | self.handle.sendline("") |
| 837 | self.handle.expect("onos>") |
| 838 | |
| 839 | self.handle.sendline("add-host-intent "+ |
| 840 | str(host_id_one) + " " + str(host_id_two)) |
| 841 | self.handle.expect("onos>") |
| 842 | |
| 843 | self.handle.sendline("") |
| 844 | self.handle.expect("onos>") |
| 845 | |
| 846 | handle = self.handle.before |
| 847 | |
| 848 | main.log.info("Intent installed between "+ |
| 849 | str(host_id_one) + " and " + str(host_id_two)) |
| 850 | |
| 851 | return handle |
| 852 | |
| 853 | except pexpect.EOF: |
| 854 | main.log.error(self.name + ": EOF exception found") |
| 855 | main.log.error(self.name + ": " + self.handle.before) |
| 856 | main.cleanup() |
| 857 | main.exit() |
| 858 | except: |
| 859 | main.log.info(self.name+" ::::::") |
| 860 | main.log.error( traceback.print_exc()) |
| 861 | main.log.info(self.name+" ::::::") |
| 862 | main.cleanup() |
| 863 | main.exit() |
| 864 | |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 865 | def add_optical_intent(self, ingress_device, egress_device): |
| 866 | ''' |
| 867 | Required: |
| 868 | * ingress_device: device id of ingress device |
| 869 | * egress_device: device id of egress device |
| 870 | Optional: |
| 871 | TODO: Still needs to be implemented via dev side |
| 872 | ''' |
| 873 | try: |
| 874 | self.handle.sendline("add-optical-intent "+ |
| 875 | str(ingress_device) + " " + str(egress_device)) |
| 876 | self.handle.expect("add-optical-intent") |
| 877 | i = self.handle.expect([ |
| 878 | "Error", |
| 879 | "onos>"]) |
| 880 | |
| 881 | handle = self.handle.before |
| 882 | |
| 883 | #If error, return error message |
| 884 | if i == 0: |
| 885 | return handle |
| 886 | else: |
| 887 | return main.TRUE |
| 888 | |
| 889 | except pexpect.EOF: |
| 890 | main.log.error(self.name + ": EOF exception found") |
| 891 | main.log.error(self.name + ": " + self.handle.before) |
| 892 | main.cleanup() |
| 893 | main.exit() |
| 894 | except: |
| 895 | main.log.info(self.name+" ::::::") |
| 896 | main.log.error( traceback.print_exc()) |
| 897 | main.log.info(self.name+" ::::::") |
| 898 | main.cleanup() |
| 899 | main.exit() |
| 900 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 901 | def add_point_intent(self, ingress_device, port_ingress, |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 902 | egress_device, port_egress, ethType="", ethSrc="", |
| 903 | ethDst=""): |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 904 | ''' |
| 905 | Required: |
| 906 | * ingress_device: device id of ingress device |
| 907 | * egress_device: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 908 | Optional: |
| 909 | * ethType: specify ethType |
| 910 | * ethSrc: specify ethSrc (i.e. src mac addr) |
| 911 | * ethDst: specify ethDst (i.e. dst mac addr) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 912 | Description: |
| 913 | Adds a point-to-point intent (uni-directional) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 914 | specifying device id's and optional fields |
| 915 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 916 | NOTE: This function may change depending on the |
| 917 | options developers provide for point-to-point |
| 918 | intent via cli |
| 919 | ''' |
| 920 | try: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 921 | cmd = "" |
| 922 | |
| 923 | #If there are no optional arguments |
| 924 | if not ethType and not ethSrc and not ethDst: |
| 925 | cmd = "add-point-intent "+\ |
| 926 | str(ingress_device) + "/" + str(port_ingress) + " " +\ |
| 927 | str(egress_device) + "/" + str(port_egress) |
| 928 | |
| 929 | else: |
andrewonlab | 9a130be | 2014-10-22 12:44:56 -0400 | [diff] [blame] | 930 | cmd = "add-point-intent " |
| 931 | |
andrewonlab | 0c0a677 | 2014-10-22 12:31:18 -0400 | [diff] [blame] | 932 | if ethType: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 933 | cmd += " --ethType " + str(ethType) |
| 934 | if ethSrc: |
| 935 | cmd += " --ethSrc " + str(ethSrc) |
| 936 | if ethDst: |
| 937 | cmd += " --ethDst " + str(ethDst) |
andrewonlab | 9a130be | 2014-10-22 12:44:56 -0400 | [diff] [blame] | 938 | |
| 939 | cmd += " "+str(ingress_device) + "/" + str(port_ingress) + " " +\ |
| 940 | str(egress_device) + "/" + str(port_egress) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 941 | |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 942 | print "cmd = ", cmd |
| 943 | #self.handle.sendline("") |
| 944 | #self.handle.expect("onos>") |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 945 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 946 | self.handle.sendline(cmd) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 947 | i = self.handle.expect([ |
| 948 | "Error", |
| 949 | "onos>"]) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 950 | |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 951 | self.handle.sendline("intents") |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 952 | self.handle.expect("onos>") |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 953 | Intenthandle = self.handle.before |
| 954 | #print "Intenthandle = ", Intenthandle |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 955 | |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 956 | #self.handle.sendline("flows") |
| 957 | #self.handle.expect("onos>") |
| 958 | #Flowhandle = self.handle.before |
| 959 | #print "Flowhandle = ", Flowhandle |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 960 | |
| 961 | if i == 0: |
| 962 | main.log.error("Error in adding point-to-point intent") |
| 963 | return handle |
| 964 | else: |
| 965 | return main.TRUE |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 966 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 967 | except pexpect.EOF: |
| 968 | main.log.error(self.name + ": EOF exception found") |
| 969 | main.log.error(self.name + ": " + self.handle.before) |
| 970 | main.cleanup() |
| 971 | main.exit() |
| 972 | except: |
| 973 | main.log.info(self.name+" ::::::") |
| 974 | main.log.error( traceback.print_exc()) |
| 975 | main.log.info(self.name+" ::::::") |
| 976 | main.cleanup() |
| 977 | main.exit() |
| 978 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 979 | def remove_intent(self, intent_id): |
| 980 | ''' |
| 981 | Remove intent for specified intent id |
| 982 | ''' |
| 983 | try: |
| 984 | self.handle.sendline("") |
| 985 | self.handle.expect("onos>") |
| 986 | |
| 987 | self.handle.sendline("remove-intent "+str(intent_id)) |
| 988 | i = self.handle.expect([ |
| 989 | "Error", |
| 990 | "onos>"]) |
| 991 | |
| 992 | handle = self.handle.before |
| 993 | |
| 994 | if i == 0: |
| 995 | main.log.error("Error in removing intent") |
| 996 | return handle |
| 997 | else: |
| 998 | return handle |
| 999 | |
| 1000 | except pexpect.EOF: |
| 1001 | main.log.error(self.name + ": EOF exception found") |
| 1002 | main.log.error(self.name + ": " + self.handle.before) |
| 1003 | main.cleanup() |
| 1004 | main.exit() |
| 1005 | except: |
| 1006 | main.log.info(self.name+" ::::::") |
| 1007 | main.log.error( traceback.print_exc()) |
| 1008 | main.log.info(self.name+" ::::::") |
| 1009 | main.cleanup() |
| 1010 | main.exit() |
| 1011 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1012 | def intents(self, json_format = False): |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1013 | ''' |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1014 | Optional: |
| 1015 | * json_format: enable output formatting in json |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1016 | Description: |
| 1017 | Obtain intents currently installed |
| 1018 | ''' |
| 1019 | try: |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1020 | if json_format: |
| 1021 | self.handle.sendline("intents -j") |
| 1022 | self.handle.expect("intents -j") |
| 1023 | self.handle.expect("onos>") |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1024 | handle = self.handle.before |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1025 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1026 | else: |
| 1027 | self.handle.sendline("") |
| 1028 | self.handle.expect("onos>") |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1029 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1030 | self.handle.sendline("intents") |
| 1031 | self.handle.expect("onos>") |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1032 | handle = self.handle.before |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1033 | |
| 1034 | return handle |
| 1035 | |
| 1036 | except pexpect.EOF: |
| 1037 | main.log.error(self.name + ": EOF exception found") |
| 1038 | main.log.error(self.name + ": " + self.handle.before) |
| 1039 | main.cleanup() |
| 1040 | main.exit() |
| 1041 | except: |
| 1042 | main.log.info(self.name+" ::::::") |
| 1043 | main.log.error( traceback.print_exc()) |
| 1044 | main.log.info(self.name+" ::::::") |
| 1045 | main.cleanup() |
| 1046 | main.exit() |
| 1047 | |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1048 | def flows(self, json_format = False): |
| 1049 | ''' |
| 1050 | Optional: |
| 1051 | * json_format: enable output formatting in json |
| 1052 | Description: |
| 1053 | Obtain flows currently installed |
| 1054 | ''' |
| 1055 | try: |
| 1056 | if json_format: |
| 1057 | self.handle.sendline("flows -j") |
| 1058 | self.handle.expect("flows -j") |
| 1059 | self.handle.expect("onos>") |
| 1060 | handle = self.handle.before |
| 1061 | |
| 1062 | else: |
| 1063 | self.handle.sendline("") |
| 1064 | self.handle.expect("onos>") |
| 1065 | self.handle.sendline("flows") |
| 1066 | self.handle.expect("onos>") |
| 1067 | handle = self.handle.before |
| 1068 | |
| 1069 | return handle |
| 1070 | |
| 1071 | except pexpect.EOF: |
| 1072 | main.log.error(self.name + ": EOF exception found") |
| 1073 | main.log.error(self.name + ": " + self.handle.before) |
| 1074 | main.cleanup() |
| 1075 | main.exit() |
| 1076 | except: |
| 1077 | main.log.info(self.name+" ::::::") |
| 1078 | main.log.error( traceback.print_exc()) |
| 1079 | main.log.info(self.name+" ::::::") |
| 1080 | main.cleanup() |
| 1081 | main.exit() |
| 1082 | |
| 1083 | |
| 1084 | |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1085 | def topology_events_metrics(self, json_format=True): |
| 1086 | ''' |
| 1087 | Description:Returns topology metrics |
| 1088 | Optional: |
| 1089 | * json_format: enable json formatting of output |
| 1090 | ''' |
| 1091 | try: |
| 1092 | if json_format: |
| 1093 | self.handle.sendline("topology-events-metrics -j") |
| 1094 | self.handle.expect("topology-events-metrics -j") |
| 1095 | self.handle.expect("onos>") |
| 1096 | |
| 1097 | handle = self.handle.before |
| 1098 | |
| 1099 | #Some color thing that we want to escape |
| 1100 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1101 | handle = ansi_escape.sub('', handle) |
| 1102 | |
| 1103 | else: |
| 1104 | self.handle.sendline("topology-events-metrics") |
| 1105 | self.handle.expect("topology-events-metrics") |
| 1106 | self.handle.expect("onos>") |
| 1107 | |
| 1108 | handle = self.handle.before |
| 1109 | |
| 1110 | return handle |
| 1111 | |
| 1112 | except pexpect.EOF: |
| 1113 | main.log.error(self.name + ": EOF exception found") |
| 1114 | main.log.error(self.name + ": " + self.handle.before) |
| 1115 | main.cleanup() |
| 1116 | main.exit() |
| 1117 | except: |
| 1118 | main.log.info(self.name+" ::::::") |
| 1119 | main.log.error( traceback.print_exc()) |
| 1120 | main.log.info(self.name+" ::::::") |
| 1121 | main.cleanup() |
| 1122 | main.exit() |
| 1123 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1124 | #Wrapper functions **************** |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1125 | #Wrapper functions use existing driver |
| 1126 | #functions and extends their use case. |
| 1127 | #For example, we may use the output of |
| 1128 | #a normal driver function, and parse it |
| 1129 | #using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 1130 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1131 | def get_all_intents_id(self): |
| 1132 | ''' |
| 1133 | Description: |
| 1134 | Obtain all intent id's in a list |
| 1135 | ''' |
| 1136 | try: |
| 1137 | #Obtain output of intents function |
| 1138 | intents_str = self.intents() |
| 1139 | all_intent_list = [] |
| 1140 | intent_id_list = [] |
| 1141 | |
| 1142 | #Parse the intents output for ID's |
| 1143 | intents_list = [s.strip() for s in intents_str.splitlines()] |
| 1144 | for intents in intents_list: |
| 1145 | if "onos>" in intents: |
| 1146 | continue |
| 1147 | elif "intents" in intents: |
| 1148 | continue |
| 1149 | else: |
| 1150 | line_list = intents.split(" ") |
| 1151 | all_intent_list.append(line_list[0]) |
| 1152 | |
| 1153 | all_intent_list = all_intent_list[1:-2] |
| 1154 | |
| 1155 | for intents in all_intent_list: |
| 1156 | if not intents: |
| 1157 | continue |
| 1158 | else: |
| 1159 | intent_id_list.append(intents) |
| 1160 | |
| 1161 | return intent_id_list |
| 1162 | |
| 1163 | except pexpect.EOF: |
| 1164 | main.log.error(self.name + ": EOF exception found") |
| 1165 | main.log.error(self.name + ": " + self.handle.before) |
| 1166 | main.cleanup() |
| 1167 | main.exit() |
| 1168 | except: |
| 1169 | main.log.info(self.name+" ::::::") |
| 1170 | main.log.error( traceback.print_exc()) |
| 1171 | main.log.info(self.name+" ::::::") |
| 1172 | main.cleanup() |
| 1173 | main.exit() |
| 1174 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1175 | def get_all_devices_id(self): |
| 1176 | ''' |
| 1177 | Use 'devices' function to obtain list of all devices |
| 1178 | and parse the result to obtain a list of all device |
| 1179 | id's. Returns this list. Returns empty list if no |
| 1180 | devices exist |
| 1181 | List is ordered sequentially |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1182 | |
| 1183 | This function may be useful if you are not sure of the |
| 1184 | device id, and wish to execute other commands using |
| 1185 | the ids. By obtaining the list of device ids on the fly, |
| 1186 | you can iterate through the list to get mastership, etc. |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1187 | ''' |
| 1188 | try: |
| 1189 | #Call devices and store result string |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1190 | devices_str = self.devices(json_format=False) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1191 | id_list = [] |
| 1192 | |
| 1193 | if not devices_str: |
| 1194 | main.log.info("There are no devices to get id from") |
| 1195 | return id_list |
| 1196 | |
| 1197 | #Split the string into list by comma |
| 1198 | device_list = devices_str.split(",") |
| 1199 | #Get temporary list of all arguments with string 'id=' |
| 1200 | temp_list = [dev for dev in device_list if "id=" in dev] |
| 1201 | #Split list further into arguments before and after string |
| 1202 | # 'id='. Get the latter portion (the actual device id) and |
| 1203 | # append to id_list |
| 1204 | for arg in temp_list: |
| 1205 | id_list.append(arg.split("id=")[1]) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1206 | return id_list |
| 1207 | |
| 1208 | except pexpect.EOF: |
| 1209 | main.log.error(self.name + ": EOF exception found") |
| 1210 | main.log.error(self.name + ": " + self.handle.before) |
| 1211 | main.cleanup() |
| 1212 | main.exit() |
| 1213 | except: |
| 1214 | main.log.info(self.name+" ::::::") |
| 1215 | main.log.error( traceback.print_exc()) |
| 1216 | main.log.info(self.name+" ::::::") |
| 1217 | main.cleanup() |
| 1218 | main.exit() |
| 1219 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1220 | def get_all_nodes_id(self): |
| 1221 | ''' |
| 1222 | Uses 'nodes' function to obtain list of all nodes |
| 1223 | and parse the result of nodes to obtain just the |
| 1224 | node id's. |
| 1225 | Returns: |
| 1226 | list of node id's |
| 1227 | ''' |
| 1228 | try: |
| 1229 | nodes_str = self.nodes() |
| 1230 | id_list = [] |
| 1231 | |
| 1232 | if not nodes_str: |
| 1233 | main.log.info("There are no nodes to get id from") |
| 1234 | return id_list |
| 1235 | |
| 1236 | #Sample nodes_str output |
| 1237 | #id=local, address=127.0.0.1:9876, state=ACTIVE * |
| 1238 | |
| 1239 | #Split the string into list by comma |
| 1240 | nodes_list = nodes_str.split(",") |
| 1241 | temp_list = [node for node in nodes_list if "id=" in node] |
| 1242 | for arg in temp_list: |
| 1243 | id_list.append(arg.split("id=")[1]) |
| 1244 | |
| 1245 | return id_list |
| 1246 | |
| 1247 | except pexpect.EOF: |
| 1248 | main.log.error(self.name + ": EOF exception found") |
| 1249 | main.log.error(self.name + ": " + self.handle.before) |
| 1250 | main.cleanup() |
| 1251 | main.exit() |
| 1252 | except: |
| 1253 | main.log.info(self.name+" ::::::") |
| 1254 | main.log.error( traceback.print_exc()) |
| 1255 | main.log.info(self.name+" ::::::") |
| 1256 | main.cleanup() |
| 1257 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1258 | |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1259 | def get_device(self, dpid=None): |
| 1260 | ''' |
| 1261 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 1262 | Return None if there is no match |
| 1263 | ''' |
| 1264 | import json |
| 1265 | try: |
| 1266 | if dpid == None: |
| 1267 | return None |
| 1268 | else: |
| 1269 | dpid = dpid.replace(':', '') |
| 1270 | raw_devices = self.devices() |
| 1271 | devices_json = json.loads(raw_devices) |
| 1272 | #search json for the device with dpid then return the device |
| 1273 | for device in devices_json: |
| 1274 | #print "%s in %s?" % (dpid, device['id']) |
| 1275 | if dpid in device['id']: |
| 1276 | return device |
| 1277 | return None |
| 1278 | except pexpect.EOF: |
| 1279 | main.log.error(self.name + ": EOF exception found") |
| 1280 | main.log.error(self.name + ": " + self.handle.before) |
| 1281 | main.cleanup() |
| 1282 | main.exit() |
| 1283 | except: |
| 1284 | main.log.info(self.name+" ::::::") |
| 1285 | main.log.error( traceback.print_exc()) |
| 1286 | main.log.info(self.name+" ::::::") |
| 1287 | main.cleanup() |
| 1288 | main.exit() |
| 1289 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1290 | def check_status(self, ip, numoswitch, numolink, log_level="info"): |
| 1291 | ''' |
| 1292 | Checks the number of swithes & links that ONOS sees against the |
| 1293 | supplied values. By default this will report to main.log, but the |
| 1294 | log level can be specifid. |
| 1295 | |
| 1296 | Params: ip = ip used for the onos cli |
| 1297 | numoswitch = expected number of switches |
| 1298 | numlink = expected number of links |
| 1299 | log_level = level to log to. Currently accepts 'info', 'warn' and 'report' |
| 1300 | |
| 1301 | |
| 1302 | log_level can |
| 1303 | |
| 1304 | Returns: main.TRUE if the number of switchs and links are correct, |
| 1305 | main.FALSE if the numer of switches and links is incorrect, |
| 1306 | and main.ERROR otherwise |
| 1307 | ''' |
| 1308 | |
| 1309 | try: |
| 1310 | topology = self.get_topology(ip) |
| 1311 | if topology == {}: |
| 1312 | return main.ERROR |
| 1313 | output = "" |
| 1314 | #Is the number of switches is what we expected |
| 1315 | devices = topology.get('devices',False) |
| 1316 | links = topology.get('links',False) |
| 1317 | if devices == False or links == False: |
| 1318 | return main.ERROR |
| 1319 | switch_check = ( int(devices) == int(numoswitch) ) |
| 1320 | #Is the number of links is what we expected |
| 1321 | link_check = ( int(links) == int(numolink) ) |
| 1322 | if (switch_check and link_check): |
| 1323 | #We expected the correct numbers |
| 1324 | output = output + "The number of links and switches match "\ |
| 1325 | + "what was expected" |
| 1326 | result = main.TRUE |
| 1327 | else: |
| 1328 | output = output + \ |
| 1329 | "The number of links and switches does not match what was expected" |
| 1330 | result = main.FALSE |
| 1331 | output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\ |
| 1332 | % ( int(devices), int(numoswitch), int(links), int(numolink) ) |
| 1333 | if log_level == "report": |
| 1334 | main.log.report(output) |
| 1335 | elif log_level == "warn": |
| 1336 | main.log.warn(output) |
| 1337 | else: |
| 1338 | main.log.info(output) |
| 1339 | return result |
| 1340 | except pexpect.EOF: |
| 1341 | main.log.error(self.name + ": EOF exception found") |
| 1342 | main.log.error(self.name + ": " + self.handle.before) |
| 1343 | main.cleanup() |
| 1344 | main.exit() |
| 1345 | except: |
| 1346 | main.log.info(self.name+" ::::::") |
| 1347 | main.log.error( traceback.print_exc()) |
| 1348 | main.log.info(self.name+" ::::::") |
| 1349 | main.cleanup() |
| 1350 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1351 | #*********************************** |