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