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("") |
| 87 | self.handle.expect("onos>") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 88 | self.handle.sendline("system:shutdown") |
| 89 | self.handle.expect("Confirm") |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 90 | self.handle.sendline("yes") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 91 | self.handle.expect("\$") |
| 92 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 93 | except pexpect.EOF: |
| 94 | main.log.error(self.name + ": EOF exception found") |
| 95 | main.log.error(self.name + ": " + self.handle.before) |
| 96 | except: |
| 97 | main.log.error(self.name + ": Connection failed to the host") |
| 98 | response = main.FALSE |
| 99 | return response |
| 100 | |
| 101 | def set_cell(self, cellname): |
| 102 | ''' |
| 103 | Calls 'cell <name>' to set the environment variables on ONOSbench |
| 104 | |
| 105 | Before issuing any cli commands, set the environment variable first. |
| 106 | ''' |
| 107 | try: |
| 108 | if not cellname: |
| 109 | main.log.error("Must define cellname") |
| 110 | main.cleanup() |
| 111 | main.exit() |
| 112 | else: |
| 113 | self.handle.sendline("cell "+str(cellname)) |
| 114 | #Expect the cellname in the ONOS_CELL variable. |
| 115 | #Note that this variable name is subject to change |
| 116 | # and that this driver will have to change accordingly |
| 117 | self.handle.expect("ONOS_CELL="+str(cellname)) |
| 118 | handle_before = self.handle.before |
| 119 | handle_after = self.handle.after |
| 120 | #Get the rest of the handle |
| 121 | self.handle.sendline("") |
| 122 | self.handle.expect("\$") |
| 123 | handle_more = self.handle.before |
| 124 | |
| 125 | main.log.info("Cell call returned: "+handle_before+ |
| 126 | handle_after + handle_more) |
| 127 | |
| 128 | return main.TRUE |
| 129 | |
| 130 | except pexpect.EOF: |
| 131 | main.log.error(self.name + ": EOF exception found") |
| 132 | main.log.error(self.name + ": " + self.handle.before) |
| 133 | main.cleanup() |
| 134 | main.exit() |
| 135 | except: |
| 136 | main.log.info(self.name+" ::::::") |
| 137 | main.log.error( traceback.print_exc()) |
| 138 | main.log.info(self.name+" ::::::") |
| 139 | main.cleanup() |
| 140 | main.exit() |
| 141 | |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 142 | def start_onos_cli(self, ONOS_ip): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 143 | try: |
| 144 | self.handle.sendline("") |
| 145 | self.handle.expect("\$") |
| 146 | |
| 147 | #Wait for onos start (-w) and enter onos cli |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 148 | self.handle.sendline("onos -w "+str(ONOS_ip)) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 149 | self.handle.expect("onos>") |
| 150 | |
| 151 | except pexpect.EOF: |
| 152 | main.log.error(self.name + ": EOF exception found") |
| 153 | main.log.error(self.name + ": " + self.handle.before) |
| 154 | main.cleanup() |
| 155 | main.exit() |
| 156 | except: |
| 157 | main.log.info(self.name+" ::::::") |
| 158 | main.log.error( traceback.print_exc()) |
| 159 | main.log.info(self.name+" ::::::") |
| 160 | main.cleanup() |
| 161 | main.exit() |
| 162 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 163 | def sendline(self, cmd_str): |
| 164 | ''' |
| 165 | Send a completely user specified string to |
| 166 | the onos> prompt. Use this function if you have |
| 167 | a very specific command to send. |
| 168 | |
| 169 | Warning: There are no sanity checking to commands |
| 170 | sent using this method. |
| 171 | ''' |
| 172 | try: |
| 173 | self.handle.sendline("") |
| 174 | self.handle.expect("onos>") |
| 175 | |
| 176 | self.handle.sendline(cmd_str) |
| 177 | self.handle.expect("onos>") |
| 178 | |
| 179 | handle = self.handle.before |
| 180 | |
| 181 | self.handle.sendline("") |
| 182 | self.handle.expect("onos>") |
| 183 | |
| 184 | handle += self.handle.before |
| 185 | handle += self.handle.after |
| 186 | |
| 187 | main.log.info("Command sent.") |
| 188 | |
| 189 | return handle |
| 190 | except pexpect.EOF: |
| 191 | main.log.error(self.name + ": EOF exception found") |
| 192 | main.log.error(self.name + ": " + self.handle.before) |
| 193 | main.cleanup() |
| 194 | main.exit() |
| 195 | except: |
| 196 | main.log.info(self.name+" ::::::") |
| 197 | main.log.error( traceback.print_exc()) |
| 198 | main.log.info(self.name+" ::::::") |
| 199 | main.cleanup() |
| 200 | main.exit() |
| 201 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 202 | #IMPORTANT NOTE: |
| 203 | #For all cli commands, naming convention should match |
| 204 | #the cli command replacing ':' with '_'. |
| 205 | #Ex) onos:topology > onos_topology |
| 206 | # onos:links > onos_links |
| 207 | # feature:list > feature_list |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 208 | |
| 209 | def add_node(self, node_id, ONOS_ip, tcp_port=""): |
| 210 | ''' |
| 211 | Adds a new cluster node by ID and address information. |
| 212 | Required: |
| 213 | * node_id |
| 214 | * ONOS_ip |
| 215 | Optional: |
| 216 | * tcp_port |
| 217 | ''' |
| 218 | try: |
| 219 | self.handle.sendline("") |
| 220 | self.handle.expect("onos>") |
| 221 | |
| 222 | self.handle.sendline("add-node "+ |
| 223 | str(node_id)+" "+ |
| 224 | str(ONOS_ip)+" "+ |
| 225 | str(tcp_port)) |
| 226 | |
| 227 | i = self.handle.expect([ |
| 228 | "Error", |
| 229 | "onos>" ]) |
| 230 | |
| 231 | #Clear handle to get previous output |
| 232 | self.handle.sendline("") |
| 233 | self.handle.expect("onos>") |
| 234 | |
| 235 | handle = self.handle.before |
| 236 | |
| 237 | if i == 0: |
| 238 | main.log.error("Error in adding node") |
| 239 | main.log.error(handle) |
| 240 | return main.FALSE |
| 241 | else: |
| 242 | main.log.info("Node "+str(ONOS_ip)+" added") |
| 243 | return main.TRUE |
| 244 | |
| 245 | except pexpect.EOF: |
| 246 | main.log.error(self.name + ": EOF exception found") |
| 247 | main.log.error(self.name + ": " + self.handle.before) |
| 248 | main.cleanup() |
| 249 | main.exit() |
| 250 | except: |
| 251 | main.log.info(self.name+" ::::::") |
| 252 | main.log.error( traceback.print_exc()) |
| 253 | main.log.info(self.name+" ::::::") |
| 254 | main.cleanup() |
| 255 | main.exit() |
| 256 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 257 | def remove_node(self, node_id): |
| 258 | ''' |
| 259 | Removes a cluster by ID |
| 260 | Issues command: 'remove-node [<node-id>]' |
| 261 | Required: |
| 262 | * node_id |
| 263 | ''' |
| 264 | try: |
| 265 | self.handle.sendline("") |
| 266 | self.handle.expect("onos>") |
| 267 | |
| 268 | self.handle.sendline("remove-node "+str(node_id)) |
| 269 | self.handle.expect("onos>") |
| 270 | |
| 271 | return main.TRUE |
| 272 | |
| 273 | except pexpect.EOF: |
| 274 | main.log.error(self.name + ": EOF exception found") |
| 275 | main.log.error(self.name + ": " + self.handle.before) |
| 276 | main.cleanup() |
| 277 | main.exit() |
| 278 | except: |
| 279 | main.log.info(self.name+" ::::::") |
| 280 | main.log.error( traceback.print_exc()) |
| 281 | main.log.info(self.name+" ::::::") |
| 282 | main.cleanup() |
| 283 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 284 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 285 | def nodes(self): |
| 286 | ''' |
| 287 | List the nodes currently visible |
| 288 | Issues command: 'nodes' |
| 289 | Returns: entire handle of list of nodes |
| 290 | ''' |
| 291 | try: |
| 292 | self.handle.sendline("") |
| 293 | self.handle.expect("onos>") |
| 294 | |
| 295 | self.handle.sendline("nodes") |
| 296 | self.handle.expect("onos>") |
| 297 | |
| 298 | self.handle.sendline("") |
| 299 | self.handle.expect("onos>") |
| 300 | |
| 301 | handle = self.handle.before |
| 302 | |
| 303 | return handle |
| 304 | |
| 305 | except pexpect.EOF: |
| 306 | main.log.error(self.name + ": EOF exception found") |
| 307 | main.log.error(self.name + ": " + self.handle.before) |
| 308 | main.cleanup() |
| 309 | main.exit() |
| 310 | except: |
| 311 | main.log.info(self.name+" ::::::") |
| 312 | main.log.error( traceback.print_exc()) |
| 313 | main.log.info(self.name+" ::::::") |
| 314 | main.cleanup() |
| 315 | main.exit() |
| 316 | |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 317 | def topology(self): |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 318 | ''' |
| 319 | Shows the current state of the topology |
| 320 | by issusing command: 'onos> onos:topology' |
| 321 | ''' |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 322 | try: |
| 323 | self.handle.sendline("") |
| 324 | self.handle.expect("onos>") |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 325 | #either onos:topology or 'topology' will work in CLI |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 326 | self.handle.sendline("onos:topology") |
| 327 | self.handle.expect("onos>") |
| 328 | |
| 329 | handle = self.handle.before |
| 330 | |
| 331 | main.log.info("onos:topology returned: " + |
| 332 | str(handle)) |
| 333 | |
| 334 | return handle |
| 335 | |
| 336 | except pexpect.EOF: |
| 337 | main.log.error(self.name + ": EOF exception found") |
| 338 | main.log.error(self.name + ": " + self.handle.before) |
| 339 | main.cleanup() |
| 340 | main.exit() |
| 341 | except: |
| 342 | main.log.info(self.name+" ::::::") |
| 343 | main.log.error( traceback.print_exc()) |
| 344 | main.log.info(self.name+" ::::::") |
| 345 | main.cleanup() |
| 346 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 347 | |
| 348 | def feature_install(self, feature_str): |
| 349 | ''' |
| 350 | Installs a specified feature |
| 351 | by issuing command: 'onos> feature:install <feature_str>' |
| 352 | ''' |
| 353 | try: |
| 354 | self.handle.sendline("") |
| 355 | self.handle.expect("onos>") |
| 356 | |
| 357 | self.handle.sendline("feature:install "+str(feature_str)) |
| 358 | self.handle.expect("onos>") |
| 359 | |
| 360 | return main.TRUE |
| 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() |
| 373 | |
| 374 | def feature_uninstall(self, feature_str): |
| 375 | ''' |
| 376 | Uninstalls a specified feature |
| 377 | by issuing command: 'onos> feature:uninstall <feature_str>' |
| 378 | ''' |
| 379 | try: |
| 380 | self.handle.sendline("") |
| 381 | self.handle.expect("onos>") |
| 382 | |
| 383 | self.handle.sendline("feature:uninstall "+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() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 399 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 400 | def devices(self, json_format=True, grep_str=""): |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 401 | ''' |
| 402 | Lists all infrastructure devices |
| 403 | Optional argument: |
| 404 | * grep_str - pass in a string to grep |
| 405 | ''' |
| 406 | try: |
| 407 | self.handle.sendline("") |
| 408 | self.handle.expect("onos>") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 409 | |
| 410 | if json_format: |
| 411 | if not grep_str: |
| 412 | self.handle.sendline("devices -j") |
| 413 | self.handle.expect("devices -j") |
| 414 | self.handle.expect("onos>") |
| 415 | else: |
| 416 | self.handle.sendline("devices -j | grep '"+ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 417 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 418 | self.handle.expect("devices -j | grep '"+str(grep_str)+"'") |
| 419 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 420 | handle = self.handle.before |
| 421 | print "repr(handle) =", repr(handle) |
| 422 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 423 | handle1 = ansi_escape.sub('', handle) |
| 424 | print "repr(handle1) = ", repr(handle1) |
| 425 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 426 | else: |
| 427 | if not grep_str: |
| 428 | self.handle.sendline("devices") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 429 | self.handle.expect("onos>") |
| 430 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 431 | self.handle.expect("onos>") |
| 432 | else: |
| 433 | self.handle.sendline("devices | grep '"+ |
| 434 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 435 | self.handle.expect("onos>") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 436 | self.handle.sendline("") |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 437 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame^] | 438 | handle = self.handle.before |
| 439 | print "handle =",handle |
| 440 | return handle |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 441 | except pexpect.EOF: |
| 442 | main.log.error(self.name + ": EOF exception found") |
| 443 | main.log.error(self.name + ": " + self.handle.before) |
| 444 | main.cleanup() |
| 445 | main.exit() |
| 446 | except: |
| 447 | main.log.info(self.name+" ::::::") |
| 448 | main.log.error( traceback.print_exc()) |
| 449 | main.log.info(self.name+" ::::::") |
| 450 | main.cleanup() |
| 451 | main.exit() |
| 452 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 453 | def links(self, json_format=True, grep_str=""): |
| 454 | ''' |
| 455 | Lists all core links |
| 456 | Optional argument: |
| 457 | * grep_str - pass in a string to grep |
| 458 | ''' |
| 459 | try: |
| 460 | self.handle.sendline("") |
| 461 | self.handle.expect("onos>") |
| 462 | |
| 463 | if json_format: |
| 464 | if not grep_str: |
| 465 | self.handle.sendline("links -j") |
| 466 | self.handle.expect("links -j") |
| 467 | self.handle.expect("onos>") |
| 468 | else: |
| 469 | self.handle.sendline("links -j | grep '"+ |
| 470 | str(grep_str)+"'") |
| 471 | self.handle.expect("links -j | grep '"+str(grep_str)+"'") |
| 472 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame^] | 473 | handle = self.handle.before |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 474 | print "repr(handle) =", repr(handle) |
| 475 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 476 | handle1 = ansi_escape.sub('', handle) |
| 477 | print "repr(handle1) = ", repr(handle1) |
| 478 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 479 | else: |
| 480 | if not grep_str: |
| 481 | self.handle.sendline("links") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 482 | self.handle.expect("onos>") |
| 483 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 484 | self.handle.expect("onos>") |
| 485 | else: |
| 486 | self.handle.sendline("links | grep '"+ |
| 487 | str(grep_str)+"'") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 488 | self.handle.expect("onos>") |
| 489 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 490 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 491 | handle = self.handle.before |
| 492 | print "handle =",handle |
| 493 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 494 | except pexpect.EOF: |
| 495 | main.log.error(self.name + ": EOF exception found") |
| 496 | main.log.error(self.name + ": " + self.handle.before) |
| 497 | main.cleanup() |
| 498 | main.exit() |
| 499 | except: |
| 500 | main.log.info(self.name+" ::::::") |
| 501 | main.log.error( traceback.print_exc()) |
| 502 | main.log.info(self.name+" ::::::") |
| 503 | main.cleanup() |
| 504 | main.exit() |
| 505 | |
| 506 | |
| 507 | def ports(self, json_format=True, grep_str=""): |
| 508 | ''' |
| 509 | Lists all ports |
| 510 | Optional argument: |
| 511 | * grep_str - pass in a string to grep |
| 512 | ''' |
| 513 | try: |
| 514 | self.handle.sendline("") |
| 515 | self.handle.expect("onos>") |
| 516 | |
| 517 | if json_format: |
| 518 | if not grep_str: |
| 519 | self.handle.sendline("ports -j") |
| 520 | self.handle.expect("ports -j") |
| 521 | self.handle.expect("onos>") |
| 522 | else: |
| 523 | self.handle.sendline("ports -j | grep '"+ |
| 524 | str(grep_str)+"'") |
| 525 | self.handle.expect("ports -j | grep '"+str(grep_str)+"'") |
| 526 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame^] | 527 | handle = self.handle.before |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 528 | print "repr(handle) =", repr(handle) |
| 529 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 530 | handle1 = ansi_escape.sub('', handle) |
| 531 | print "repr(handle1) = ", repr(handle1) |
| 532 | return handle1 |
| 533 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 534 | else: |
| 535 | if not grep_str: |
| 536 | self.handle.sendline("ports") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 537 | self.handle.expect("onos>") |
| 538 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 539 | self.handle.expect("onos>") |
| 540 | else: |
| 541 | self.handle.sendline("ports | grep '"+ |
| 542 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 543 | self.handle.expect("onos>") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 544 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 545 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 546 | handle = self.handle.before |
| 547 | print "handle =",handle |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame^] | 548 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 549 | except pexpect.EOF: |
| 550 | main.log.error(self.name + ": EOF exception found") |
| 551 | main.log.error(self.name + ": " + self.handle.before) |
| 552 | main.cleanup() |
| 553 | main.exit() |
| 554 | except: |
| 555 | main.log.info(self.name+" ::::::") |
| 556 | main.log.error( traceback.print_exc()) |
| 557 | main.log.info(self.name+" ::::::") |
| 558 | main.cleanup() |
| 559 | main.exit() |
| 560 | |
| 561 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 562 | def device_role(self, device_id, node_id, role): |
| 563 | ''' |
| 564 | Set device role for specified device and node with role |
| 565 | Required: |
| 566 | * device_id : may be obtained by function get_all_devices_id |
| 567 | * node_id : may be obtained by function get_all_nodes_id |
| 568 | * role: specify one of the following roles: |
| 569 | - master |
| 570 | - standby |
| 571 | - none |
| 572 | ''' |
| 573 | try: |
| 574 | self.handle.sendline("") |
| 575 | self.handle.expect("onos>") |
| 576 | |
| 577 | self.handle.sendline("device-role "+ |
| 578 | str(device_id) + " " + |
| 579 | str(node_id) + " " + |
| 580 | str(role)) |
| 581 | i = self.handle.expect([ |
| 582 | "Error", |
| 583 | "onos>"]) |
| 584 | |
| 585 | self.handle.sendline("") |
| 586 | self.handle.expect("onos>") |
| 587 | |
| 588 | handle = self.handle.before |
| 589 | |
| 590 | if i == 0: |
| 591 | main.log.error("device-role command returned error") |
| 592 | return handle |
| 593 | else: |
| 594 | return main.TRUE |
| 595 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 596 | except pexpect.EOF: |
| 597 | main.log.error(self.name + ": EOF exception found") |
| 598 | main.log.error(self.name + ": " + self.handle.before) |
| 599 | main.cleanup() |
| 600 | main.exit() |
| 601 | except: |
| 602 | main.log.info(self.name+" ::::::") |
| 603 | main.log.error( traceback.print_exc()) |
| 604 | main.log.info(self.name+" ::::::") |
| 605 | main.cleanup() |
| 606 | main.exit() |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 607 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 608 | def paths(self, src_id, dst_id): |
| 609 | ''' |
| 610 | Returns string of paths, and the cost. |
| 611 | Issues command: onos:paths <src> <dst> |
| 612 | ''' |
| 613 | try: |
| 614 | self.handle.sendline("") |
| 615 | self.handle.expect("onos>") |
| 616 | |
| 617 | self.handle.sendline("onos:paths "+ |
| 618 | str(src_id) + " " + str(dst_id)) |
| 619 | i = self.handle.expect([ |
| 620 | "Error", |
| 621 | "onos>"]) |
| 622 | |
| 623 | self.handle.sendline("") |
| 624 | self.handle.expect("onos>") |
| 625 | |
| 626 | handle = self.handle.before |
| 627 | |
| 628 | if i == 0: |
| 629 | main.log.error("Error in getting paths") |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 630 | return (handle, "Error") |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 631 | else: |
| 632 | path = handle.split(";")[0] |
| 633 | cost = handle.split(";")[1] |
| 634 | return (path, cost) |
| 635 | |
| 636 | except pexpect.EOF: |
| 637 | main.log.error(self.name + ": EOF exception found") |
| 638 | main.log.error(self.name + ": " + self.handle.before) |
| 639 | main.cleanup() |
| 640 | main.exit() |
| 641 | except: |
| 642 | main.log.info(self.name+" ::::::") |
| 643 | main.log.error( traceback.print_exc()) |
| 644 | main.log.info(self.name+" ::::::") |
| 645 | main.cleanup() |
| 646 | main.exit() |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 647 | |
| 648 | #TODO: |
| 649 | #def hosts(self): |
| 650 | |
| 651 | def get_hosts_id(self, host_list): |
| 652 | ''' |
| 653 | Obtain list of hosts |
| 654 | Issues command: 'onos> hosts' |
| 655 | |
| 656 | Required: |
| 657 | * host_list: List of hosts obtained by Mininet |
| 658 | IMPORTANT: |
| 659 | This function assumes that you started your |
| 660 | topology with the option '--mac'. |
| 661 | Furthermore, it assumes that value of VLAN is '-1' |
| 662 | Description: |
| 663 | Converts mininet hosts (h1, h2, h3...) into |
| 664 | ONOS format (00:00:00:00:00:01/-1 , ...) |
| 665 | ''' |
| 666 | |
| 667 | try: |
| 668 | self.handle.sendline("") |
| 669 | self.handle.expect("onos>") |
| 670 | |
| 671 | onos_host_list = [] |
| 672 | |
| 673 | for host in host_list: |
| 674 | host = host.replace("h", "") |
| 675 | host_hex = hex(int(host)).zfill(12) |
| 676 | host_hex = str(host_hex).replace('x','0') |
| 677 | i = iter(str(host_hex)) |
| 678 | host_hex = ":".join(a+b for a,b in zip(i,i)) |
| 679 | host_hex = host_hex + "/-1" |
| 680 | onos_host_list.append(host_hex) |
| 681 | |
| 682 | return onos_host_list |
| 683 | |
| 684 | except pexpect.EOF: |
| 685 | main.log.error(self.name + ": EOF exception found") |
| 686 | main.log.error(self.name + ": " + self.handle.before) |
| 687 | main.cleanup() |
| 688 | main.exit() |
| 689 | except: |
| 690 | main.log.info(self.name+" ::::::") |
| 691 | main.log.error( traceback.print_exc()) |
| 692 | main.log.info(self.name+" ::::::") |
| 693 | main.cleanup() |
| 694 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 695 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 696 | def add_host_intent(self, host_id_one, host_id_two): |
| 697 | ''' |
| 698 | Required: |
| 699 | * host_id_one: ONOS host id for host1 |
| 700 | * host_id_two: ONOS host id for host2 |
| 701 | Description: |
| 702 | Adds a host-to-host intent (bidrectional) by |
| 703 | specifying the two hosts. |
| 704 | ''' |
| 705 | try: |
| 706 | self.handle.sendline("") |
| 707 | self.handle.expect("onos>") |
| 708 | |
| 709 | self.handle.sendline("add-host-intent "+ |
| 710 | str(host_id_one) + " " + str(host_id_two)) |
| 711 | self.handle.expect("onos>") |
| 712 | |
| 713 | self.handle.sendline("") |
| 714 | self.handle.expect("onos>") |
| 715 | |
| 716 | handle = self.handle.before |
| 717 | |
| 718 | main.log.info("Intent installed between "+ |
| 719 | str(host_id_one) + " and " + str(host_id_two)) |
| 720 | |
| 721 | return handle |
| 722 | |
| 723 | except pexpect.EOF: |
| 724 | main.log.error(self.name + ": EOF exception found") |
| 725 | main.log.error(self.name + ": " + self.handle.before) |
| 726 | main.cleanup() |
| 727 | main.exit() |
| 728 | except: |
| 729 | main.log.info(self.name+" ::::::") |
| 730 | main.log.error( traceback.print_exc()) |
| 731 | main.log.info(self.name+" ::::::") |
| 732 | main.cleanup() |
| 733 | main.exit() |
| 734 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 735 | def add_point_intent(self, ingress_device, port_ingress, |
| 736 | egress_device, port_egress): |
| 737 | ''' |
| 738 | Required: |
| 739 | * ingress_device: device id of ingress device |
| 740 | * egress_device: device id of egress device |
| 741 | Description: |
| 742 | Adds a point-to-point intent (uni-directional) by |
| 743 | specifying device id's |
| 744 | |
| 745 | NOTE: This function may change depending on the |
| 746 | options developers provide for point-to-point |
| 747 | intent via cli |
| 748 | ''' |
| 749 | try: |
| 750 | self.handle.sendline("") |
| 751 | self.handle.expect("onos>") |
| 752 | |
| 753 | self.handle.sendline("add-point-intent "+ |
| 754 | str(ingress_device) + "/" + str(port_ingress) + " " + |
| 755 | str(egress_device) + "/" + str(port_egress)) |
| 756 | i = self.handle.expect([ |
| 757 | "Error", |
| 758 | "onos>"]) |
| 759 | |
| 760 | self.handle.sendline("") |
| 761 | self.handle.expect("onos>") |
| 762 | |
| 763 | handle = self.handle.before |
| 764 | |
| 765 | if i == 0: |
| 766 | main.log.error("Error in adding point-to-point intent") |
| 767 | return handle |
| 768 | else: |
| 769 | return main.TRUE |
| 770 | |
| 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 | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 783 | def remove_intent(self, intent_id): |
| 784 | ''' |
| 785 | Remove intent for specified intent id |
| 786 | ''' |
| 787 | try: |
| 788 | self.handle.sendline("") |
| 789 | self.handle.expect("onos>") |
| 790 | |
| 791 | self.handle.sendline("remove-intent "+str(intent_id)) |
| 792 | i = self.handle.expect([ |
| 793 | "Error", |
| 794 | "onos>"]) |
| 795 | |
| 796 | handle = self.handle.before |
| 797 | |
| 798 | if i == 0: |
| 799 | main.log.error("Error in removing intent") |
| 800 | return handle |
| 801 | else: |
| 802 | return handle |
| 803 | |
| 804 | except pexpect.EOF: |
| 805 | main.log.error(self.name + ": EOF exception found") |
| 806 | main.log.error(self.name + ": " + self.handle.before) |
| 807 | main.cleanup() |
| 808 | main.exit() |
| 809 | except: |
| 810 | main.log.info(self.name+" ::::::") |
| 811 | main.log.error( traceback.print_exc()) |
| 812 | main.log.info(self.name+" ::::::") |
| 813 | main.cleanup() |
| 814 | main.exit() |
| 815 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 816 | def intents(self): |
| 817 | ''' |
| 818 | Description: |
| 819 | Obtain intents currently installed |
| 820 | ''' |
| 821 | try: |
| 822 | self.handle.sendline("") |
| 823 | self.handle.expect("onos>") |
| 824 | |
| 825 | self.handle.sendline("intents") |
| 826 | self.handle.expect("onos>") |
| 827 | |
| 828 | self.handle.sendline("") |
| 829 | self.handle.expect("onos>") |
| 830 | |
| 831 | handle = self.handle.before |
| 832 | |
| 833 | return handle |
| 834 | |
| 835 | except pexpect.EOF: |
| 836 | main.log.error(self.name + ": EOF exception found") |
| 837 | main.log.error(self.name + ": " + self.handle.before) |
| 838 | main.cleanup() |
| 839 | main.exit() |
| 840 | except: |
| 841 | main.log.info(self.name+" ::::::") |
| 842 | main.log.error( traceback.print_exc()) |
| 843 | main.log.info(self.name+" ::::::") |
| 844 | main.cleanup() |
| 845 | main.exit() |
| 846 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 847 | #Wrapper functions **************** |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 848 | #Wrapper functions use existing driver |
| 849 | #functions and extends their use case. |
| 850 | #For example, we may use the output of |
| 851 | #a normal driver function, and parse it |
| 852 | #using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 853 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 854 | def get_all_intents_id(self): |
| 855 | ''' |
| 856 | Description: |
| 857 | Obtain all intent id's in a list |
| 858 | ''' |
| 859 | try: |
| 860 | #Obtain output of intents function |
| 861 | intents_str = self.intents() |
| 862 | all_intent_list = [] |
| 863 | intent_id_list = [] |
| 864 | |
| 865 | #Parse the intents output for ID's |
| 866 | intents_list = [s.strip() for s in intents_str.splitlines()] |
| 867 | for intents in intents_list: |
| 868 | if "onos>" in intents: |
| 869 | continue |
| 870 | elif "intents" in intents: |
| 871 | continue |
| 872 | else: |
| 873 | line_list = intents.split(" ") |
| 874 | all_intent_list.append(line_list[0]) |
| 875 | |
| 876 | all_intent_list = all_intent_list[1:-2] |
| 877 | |
| 878 | for intents in all_intent_list: |
| 879 | if not intents: |
| 880 | continue |
| 881 | else: |
| 882 | intent_id_list.append(intents) |
| 883 | |
| 884 | return intent_id_list |
| 885 | |
| 886 | except pexpect.EOF: |
| 887 | main.log.error(self.name + ": EOF exception found") |
| 888 | main.log.error(self.name + ": " + self.handle.before) |
| 889 | main.cleanup() |
| 890 | main.exit() |
| 891 | except: |
| 892 | main.log.info(self.name+" ::::::") |
| 893 | main.log.error( traceback.print_exc()) |
| 894 | main.log.info(self.name+" ::::::") |
| 895 | main.cleanup() |
| 896 | main.exit() |
| 897 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 898 | def get_all_devices_id(self): |
| 899 | ''' |
| 900 | Use 'devices' function to obtain list of all devices |
| 901 | and parse the result to obtain a list of all device |
| 902 | id's. Returns this list. Returns empty list if no |
| 903 | devices exist |
| 904 | List is ordered sequentially |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 905 | |
| 906 | This function may be useful if you are not sure of the |
| 907 | device id, and wish to execute other commands using |
| 908 | the ids. By obtaining the list of device ids on the fly, |
| 909 | you can iterate through the list to get mastership, etc. |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 910 | ''' |
| 911 | try: |
| 912 | #Call devices and store result string |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 913 | devices_str = self.devices(json_format=False) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 914 | id_list = [] |
| 915 | |
| 916 | if not devices_str: |
| 917 | main.log.info("There are no devices to get id from") |
| 918 | return id_list |
| 919 | |
| 920 | #Split the string into list by comma |
| 921 | device_list = devices_str.split(",") |
| 922 | #Get temporary list of all arguments with string 'id=' |
| 923 | temp_list = [dev for dev in device_list if "id=" in dev] |
| 924 | #Split list further into arguments before and after string |
| 925 | # 'id='. Get the latter portion (the actual device id) and |
| 926 | # append to id_list |
| 927 | for arg in temp_list: |
| 928 | id_list.append(arg.split("id=")[1]) |
| 929 | |
| 930 | return id_list |
| 931 | |
| 932 | except pexpect.EOF: |
| 933 | main.log.error(self.name + ": EOF exception found") |
| 934 | main.log.error(self.name + ": " + self.handle.before) |
| 935 | main.cleanup() |
| 936 | main.exit() |
| 937 | except: |
| 938 | main.log.info(self.name+" ::::::") |
| 939 | main.log.error( traceback.print_exc()) |
| 940 | main.log.info(self.name+" ::::::") |
| 941 | main.cleanup() |
| 942 | main.exit() |
| 943 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 944 | def get_all_nodes_id(self): |
| 945 | ''' |
| 946 | Uses 'nodes' function to obtain list of all nodes |
| 947 | and parse the result of nodes to obtain just the |
| 948 | node id's. |
| 949 | Returns: |
| 950 | list of node id's |
| 951 | ''' |
| 952 | try: |
| 953 | nodes_str = self.nodes() |
| 954 | id_list = [] |
| 955 | |
| 956 | if not nodes_str: |
| 957 | main.log.info("There are no nodes to get id from") |
| 958 | return id_list |
| 959 | |
| 960 | #Sample nodes_str output |
| 961 | #id=local, address=127.0.0.1:9876, state=ACTIVE * |
| 962 | |
| 963 | #Split the string into list by comma |
| 964 | nodes_list = nodes_str.split(",") |
| 965 | temp_list = [node for node in nodes_list if "id=" in node] |
| 966 | for arg in temp_list: |
| 967 | id_list.append(arg.split("id=")[1]) |
| 968 | |
| 969 | return id_list |
| 970 | |
| 971 | except pexpect.EOF: |
| 972 | main.log.error(self.name + ": EOF exception found") |
| 973 | main.log.error(self.name + ": " + self.handle.before) |
| 974 | main.cleanup() |
| 975 | main.exit() |
| 976 | except: |
| 977 | main.log.info(self.name+" ::::::") |
| 978 | main.log.error( traceback.print_exc()) |
| 979 | main.log.info(self.name+" ::::::") |
| 980 | main.cleanup() |
| 981 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 982 | |
| 983 | #*********************************** |