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)) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 149 | self.handle.expect("onos>", timeout = 60) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 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 |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 421 | ''' |
| 422 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 423 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
| 424 | In json.loads(somestring), this somestring variable is a actually repr(somestring) and json.loads would fail with the escape sequence. |
| 425 | So we take off that escape sequence using |
| 426 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 427 | handle1 = ansi_escape.sub('', handle) |
| 428 | ''' |
| 429 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 430 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 431 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 432 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 433 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 434 | else: |
| 435 | if not grep_str: |
| 436 | self.handle.sendline("devices") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 437 | self.handle.expect("onos>") |
| 438 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 439 | self.handle.expect("onos>") |
| 440 | else: |
| 441 | self.handle.sendline("devices | grep '"+ |
| 442 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 443 | self.handle.expect("onos>") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 444 | self.handle.sendline("") |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 445 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 446 | handle = self.handle.before |
| 447 | print "handle =",handle |
| 448 | return handle |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 449 | except pexpect.EOF: |
| 450 | main.log.error(self.name + ": EOF exception found") |
| 451 | main.log.error(self.name + ": " + self.handle.before) |
| 452 | main.cleanup() |
| 453 | main.exit() |
| 454 | except: |
| 455 | main.log.info(self.name+" ::::::") |
| 456 | main.log.error( traceback.print_exc()) |
| 457 | main.log.info(self.name+" ::::::") |
| 458 | main.cleanup() |
| 459 | main.exit() |
| 460 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 461 | def links(self, json_format=True, grep_str=""): |
| 462 | ''' |
| 463 | Lists all core links |
| 464 | Optional argument: |
| 465 | * grep_str - pass in a string to grep |
| 466 | ''' |
| 467 | try: |
| 468 | self.handle.sendline("") |
| 469 | self.handle.expect("onos>") |
| 470 | |
| 471 | if json_format: |
| 472 | if not grep_str: |
| 473 | self.handle.sendline("links -j") |
| 474 | self.handle.expect("links -j") |
| 475 | self.handle.expect("onos>") |
| 476 | else: |
| 477 | self.handle.sendline("links -j | grep '"+ |
| 478 | str(grep_str)+"'") |
| 479 | self.handle.expect("links -j | grep '"+str(grep_str)+"'") |
| 480 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 481 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 482 | ''' |
| 483 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 484 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
| 485 | In json.loads(somestring), this somestring variable is a actually repr(somestring) and json.loads would fail with the escape sequence. |
| 486 | So we take off that escape sequence using |
| 487 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 488 | handle1 = ansi_escape.sub('', handle) |
| 489 | ''' |
| 490 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 491 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 492 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 493 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 494 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 495 | else: |
| 496 | if not grep_str: |
| 497 | self.handle.sendline("links") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 498 | self.handle.expect("onos>") |
| 499 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 500 | self.handle.expect("onos>") |
| 501 | else: |
| 502 | self.handle.sendline("links | grep '"+ |
| 503 | str(grep_str)+"'") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 504 | self.handle.expect("onos>") |
| 505 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 506 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 507 | handle = self.handle.before |
| 508 | print "handle =",handle |
| 509 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 510 | except pexpect.EOF: |
| 511 | main.log.error(self.name + ": EOF exception found") |
| 512 | main.log.error(self.name + ": " + self.handle.before) |
| 513 | main.cleanup() |
| 514 | main.exit() |
| 515 | except: |
| 516 | main.log.info(self.name+" ::::::") |
| 517 | main.log.error( traceback.print_exc()) |
| 518 | main.log.info(self.name+" ::::::") |
| 519 | main.cleanup() |
| 520 | main.exit() |
| 521 | |
| 522 | |
| 523 | def ports(self, json_format=True, grep_str=""): |
| 524 | ''' |
| 525 | Lists all ports |
| 526 | Optional argument: |
| 527 | * grep_str - pass in a string to grep |
| 528 | ''' |
| 529 | try: |
| 530 | self.handle.sendline("") |
| 531 | self.handle.expect("onos>") |
| 532 | |
| 533 | if json_format: |
| 534 | if not grep_str: |
| 535 | self.handle.sendline("ports -j") |
| 536 | self.handle.expect("ports -j") |
| 537 | self.handle.expect("onos>") |
| 538 | else: |
| 539 | self.handle.sendline("ports -j | grep '"+ |
| 540 | str(grep_str)+"'") |
| 541 | self.handle.expect("ports -j | grep '"+str(grep_str)+"'") |
| 542 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 543 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 544 | ''' |
| 545 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 546 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
| 547 | In json.loads(somestring), this somestring variable is a actually repr(somestring) and json.loads would fail with the escape sequence. |
| 548 | So we take off that escape sequence using |
| 549 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 550 | handle1 = ansi_escape.sub('', handle) |
| 551 | ''' |
| 552 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 553 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 554 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 555 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 556 | return handle1 |
| 557 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 558 | else: |
| 559 | if not grep_str: |
| 560 | self.handle.sendline("ports") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 561 | self.handle.expect("onos>") |
| 562 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 563 | self.handle.expect("onos>") |
| 564 | else: |
| 565 | self.handle.sendline("ports | grep '"+ |
| 566 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 567 | self.handle.expect("onos>") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 568 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 569 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 570 | handle = self.handle.before |
| 571 | print "handle =",handle |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 572 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 573 | except pexpect.EOF: |
| 574 | main.log.error(self.name + ": EOF exception found") |
| 575 | main.log.error(self.name + ": " + self.handle.before) |
| 576 | main.cleanup() |
| 577 | main.exit() |
| 578 | except: |
| 579 | main.log.info(self.name+" ::::::") |
| 580 | main.log.error( traceback.print_exc()) |
| 581 | main.log.info(self.name+" ::::::") |
| 582 | main.cleanup() |
| 583 | main.exit() |
| 584 | |
| 585 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 586 | def device_role(self, device_id, node_id, role): |
| 587 | ''' |
| 588 | Set device role for specified device and node with role |
| 589 | Required: |
| 590 | * device_id : may be obtained by function get_all_devices_id |
| 591 | * node_id : may be obtained by function get_all_nodes_id |
| 592 | * role: specify one of the following roles: |
| 593 | - master |
| 594 | - standby |
| 595 | - none |
| 596 | ''' |
| 597 | try: |
| 598 | self.handle.sendline("") |
| 599 | self.handle.expect("onos>") |
| 600 | |
| 601 | self.handle.sendline("device-role "+ |
| 602 | str(device_id) + " " + |
| 603 | str(node_id) + " " + |
| 604 | str(role)) |
| 605 | i = self.handle.expect([ |
| 606 | "Error", |
| 607 | "onos>"]) |
| 608 | |
| 609 | self.handle.sendline("") |
| 610 | self.handle.expect("onos>") |
| 611 | |
| 612 | handle = self.handle.before |
| 613 | |
| 614 | if i == 0: |
| 615 | main.log.error("device-role command returned error") |
| 616 | return handle |
| 617 | else: |
| 618 | return main.TRUE |
| 619 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 620 | except pexpect.EOF: |
| 621 | main.log.error(self.name + ": EOF exception found") |
| 622 | main.log.error(self.name + ": " + self.handle.before) |
| 623 | main.cleanup() |
| 624 | main.exit() |
| 625 | except: |
| 626 | main.log.info(self.name+" ::::::") |
| 627 | main.log.error( traceback.print_exc()) |
| 628 | main.log.info(self.name+" ::::::") |
| 629 | main.cleanup() |
| 630 | main.exit() |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 631 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 632 | def paths(self, src_id, dst_id): |
| 633 | ''' |
| 634 | Returns string of paths, and the cost. |
| 635 | Issues command: onos:paths <src> <dst> |
| 636 | ''' |
| 637 | try: |
| 638 | self.handle.sendline("") |
| 639 | self.handle.expect("onos>") |
| 640 | |
| 641 | self.handle.sendline("onos:paths "+ |
| 642 | str(src_id) + " " + str(dst_id)) |
| 643 | i = self.handle.expect([ |
| 644 | "Error", |
| 645 | "onos>"]) |
| 646 | |
| 647 | self.handle.sendline("") |
| 648 | self.handle.expect("onos>") |
| 649 | |
| 650 | handle = self.handle.before |
| 651 | |
| 652 | if i == 0: |
| 653 | main.log.error("Error in getting paths") |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 654 | return (handle, "Error") |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 655 | else: |
| 656 | path = handle.split(";")[0] |
| 657 | cost = handle.split(";")[1] |
| 658 | return (path, cost) |
| 659 | |
| 660 | except pexpect.EOF: |
| 661 | main.log.error(self.name + ": EOF exception found") |
| 662 | main.log.error(self.name + ": " + self.handle.before) |
| 663 | main.cleanup() |
| 664 | main.exit() |
| 665 | except: |
| 666 | main.log.info(self.name+" ::::::") |
| 667 | main.log.error( traceback.print_exc()) |
| 668 | main.log.info(self.name+" ::::::") |
| 669 | main.cleanup() |
| 670 | main.exit() |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 671 | |
| 672 | #TODO: |
| 673 | #def hosts(self): |
| 674 | |
| 675 | def get_hosts_id(self, host_list): |
| 676 | ''' |
| 677 | Obtain list of hosts |
| 678 | Issues command: 'onos> hosts' |
| 679 | |
| 680 | Required: |
| 681 | * host_list: List of hosts obtained by Mininet |
| 682 | IMPORTANT: |
| 683 | This function assumes that you started your |
| 684 | topology with the option '--mac'. |
| 685 | Furthermore, it assumes that value of VLAN is '-1' |
| 686 | Description: |
| 687 | Converts mininet hosts (h1, h2, h3...) into |
| 688 | ONOS format (00:00:00:00:00:01/-1 , ...) |
| 689 | ''' |
| 690 | |
| 691 | try: |
| 692 | self.handle.sendline("") |
| 693 | self.handle.expect("onos>") |
| 694 | |
| 695 | onos_host_list = [] |
| 696 | |
| 697 | for host in host_list: |
| 698 | host = host.replace("h", "") |
| 699 | host_hex = hex(int(host)).zfill(12) |
| 700 | host_hex = str(host_hex).replace('x','0') |
| 701 | i = iter(str(host_hex)) |
| 702 | host_hex = ":".join(a+b for a,b in zip(i,i)) |
| 703 | host_hex = host_hex + "/-1" |
| 704 | onos_host_list.append(host_hex) |
| 705 | |
| 706 | return onos_host_list |
| 707 | |
| 708 | except pexpect.EOF: |
| 709 | main.log.error(self.name + ": EOF exception found") |
| 710 | main.log.error(self.name + ": " + self.handle.before) |
| 711 | main.cleanup() |
| 712 | main.exit() |
| 713 | except: |
| 714 | main.log.info(self.name+" ::::::") |
| 715 | main.log.error( traceback.print_exc()) |
| 716 | main.log.info(self.name+" ::::::") |
| 717 | main.cleanup() |
| 718 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 719 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 720 | def add_host_intent(self, host_id_one, host_id_two): |
| 721 | ''' |
| 722 | Required: |
| 723 | * host_id_one: ONOS host id for host1 |
| 724 | * host_id_two: ONOS host id for host2 |
| 725 | Description: |
| 726 | Adds a host-to-host intent (bidrectional) by |
| 727 | specifying the two hosts. |
| 728 | ''' |
| 729 | try: |
| 730 | self.handle.sendline("") |
| 731 | self.handle.expect("onos>") |
| 732 | |
| 733 | self.handle.sendline("add-host-intent "+ |
| 734 | str(host_id_one) + " " + str(host_id_two)) |
| 735 | self.handle.expect("onos>") |
| 736 | |
| 737 | self.handle.sendline("") |
| 738 | self.handle.expect("onos>") |
| 739 | |
| 740 | handle = self.handle.before |
| 741 | |
| 742 | main.log.info("Intent installed between "+ |
| 743 | str(host_id_one) + " and " + str(host_id_two)) |
| 744 | |
| 745 | return handle |
| 746 | |
| 747 | except pexpect.EOF: |
| 748 | main.log.error(self.name + ": EOF exception found") |
| 749 | main.log.error(self.name + ": " + self.handle.before) |
| 750 | main.cleanup() |
| 751 | main.exit() |
| 752 | except: |
| 753 | main.log.info(self.name+" ::::::") |
| 754 | main.log.error( traceback.print_exc()) |
| 755 | main.log.info(self.name+" ::::::") |
| 756 | main.cleanup() |
| 757 | main.exit() |
| 758 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 759 | def add_point_intent(self, ingress_device, port_ingress, |
| 760 | egress_device, port_egress): |
| 761 | ''' |
| 762 | Required: |
| 763 | * ingress_device: device id of ingress device |
| 764 | * egress_device: device id of egress device |
| 765 | Description: |
| 766 | Adds a point-to-point intent (uni-directional) by |
| 767 | specifying device id's |
| 768 | |
| 769 | NOTE: This function may change depending on the |
| 770 | options developers provide for point-to-point |
| 771 | intent via cli |
| 772 | ''' |
| 773 | try: |
| 774 | self.handle.sendline("") |
| 775 | self.handle.expect("onos>") |
| 776 | |
| 777 | self.handle.sendline("add-point-intent "+ |
| 778 | str(ingress_device) + "/" + str(port_ingress) + " " + |
| 779 | str(egress_device) + "/" + str(port_egress)) |
| 780 | i = self.handle.expect([ |
| 781 | "Error", |
| 782 | "onos>"]) |
| 783 | |
| 784 | self.handle.sendline("") |
| 785 | self.handle.expect("onos>") |
| 786 | |
| 787 | handle = self.handle.before |
| 788 | |
| 789 | if i == 0: |
| 790 | main.log.error("Error in adding point-to-point intent") |
| 791 | return handle |
| 792 | else: |
| 793 | return main.TRUE |
| 794 | |
| 795 | except pexpect.EOF: |
| 796 | main.log.error(self.name + ": EOF exception found") |
| 797 | main.log.error(self.name + ": " + self.handle.before) |
| 798 | main.cleanup() |
| 799 | main.exit() |
| 800 | except: |
| 801 | main.log.info(self.name+" ::::::") |
| 802 | main.log.error( traceback.print_exc()) |
| 803 | main.log.info(self.name+" ::::::") |
| 804 | main.cleanup() |
| 805 | main.exit() |
| 806 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 807 | def remove_intent(self, intent_id): |
| 808 | ''' |
| 809 | Remove intent for specified intent id |
| 810 | ''' |
| 811 | try: |
| 812 | self.handle.sendline("") |
| 813 | self.handle.expect("onos>") |
| 814 | |
| 815 | self.handle.sendline("remove-intent "+str(intent_id)) |
| 816 | i = self.handle.expect([ |
| 817 | "Error", |
| 818 | "onos>"]) |
| 819 | |
| 820 | handle = self.handle.before |
| 821 | |
| 822 | if i == 0: |
| 823 | main.log.error("Error in removing intent") |
| 824 | return handle |
| 825 | else: |
| 826 | return handle |
| 827 | |
| 828 | except pexpect.EOF: |
| 829 | main.log.error(self.name + ": EOF exception found") |
| 830 | main.log.error(self.name + ": " + self.handle.before) |
| 831 | main.cleanup() |
| 832 | main.exit() |
| 833 | except: |
| 834 | main.log.info(self.name+" ::::::") |
| 835 | main.log.error( traceback.print_exc()) |
| 836 | main.log.info(self.name+" ::::::") |
| 837 | main.cleanup() |
| 838 | main.exit() |
| 839 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 840 | def intents(self): |
| 841 | ''' |
| 842 | Description: |
| 843 | Obtain intents currently installed |
| 844 | ''' |
| 845 | try: |
| 846 | self.handle.sendline("") |
| 847 | self.handle.expect("onos>") |
| 848 | |
| 849 | self.handle.sendline("intents") |
| 850 | self.handle.expect("onos>") |
| 851 | |
| 852 | self.handle.sendline("") |
| 853 | self.handle.expect("onos>") |
| 854 | |
| 855 | handle = self.handle.before |
| 856 | |
| 857 | return handle |
| 858 | |
| 859 | except pexpect.EOF: |
| 860 | main.log.error(self.name + ": EOF exception found") |
| 861 | main.log.error(self.name + ": " + self.handle.before) |
| 862 | main.cleanup() |
| 863 | main.exit() |
| 864 | except: |
| 865 | main.log.info(self.name+" ::::::") |
| 866 | main.log.error( traceback.print_exc()) |
| 867 | main.log.info(self.name+" ::::::") |
| 868 | main.cleanup() |
| 869 | main.exit() |
| 870 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 871 | #Wrapper functions **************** |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 872 | #Wrapper functions use existing driver |
| 873 | #functions and extends their use case. |
| 874 | #For example, we may use the output of |
| 875 | #a normal driver function, and parse it |
| 876 | #using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 877 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 878 | def get_all_intents_id(self): |
| 879 | ''' |
| 880 | Description: |
| 881 | Obtain all intent id's in a list |
| 882 | ''' |
| 883 | try: |
| 884 | #Obtain output of intents function |
| 885 | intents_str = self.intents() |
| 886 | all_intent_list = [] |
| 887 | intent_id_list = [] |
| 888 | |
| 889 | #Parse the intents output for ID's |
| 890 | intents_list = [s.strip() for s in intents_str.splitlines()] |
| 891 | for intents in intents_list: |
| 892 | if "onos>" in intents: |
| 893 | continue |
| 894 | elif "intents" in intents: |
| 895 | continue |
| 896 | else: |
| 897 | line_list = intents.split(" ") |
| 898 | all_intent_list.append(line_list[0]) |
| 899 | |
| 900 | all_intent_list = all_intent_list[1:-2] |
| 901 | |
| 902 | for intents in all_intent_list: |
| 903 | if not intents: |
| 904 | continue |
| 905 | else: |
| 906 | intent_id_list.append(intents) |
| 907 | |
| 908 | return intent_id_list |
| 909 | |
| 910 | except pexpect.EOF: |
| 911 | main.log.error(self.name + ": EOF exception found") |
| 912 | main.log.error(self.name + ": " + self.handle.before) |
| 913 | main.cleanup() |
| 914 | main.exit() |
| 915 | except: |
| 916 | main.log.info(self.name+" ::::::") |
| 917 | main.log.error( traceback.print_exc()) |
| 918 | main.log.info(self.name+" ::::::") |
| 919 | main.cleanup() |
| 920 | main.exit() |
| 921 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 922 | def get_all_devices_id(self): |
| 923 | ''' |
| 924 | Use 'devices' function to obtain list of all devices |
| 925 | and parse the result to obtain a list of all device |
| 926 | id's. Returns this list. Returns empty list if no |
| 927 | devices exist |
| 928 | List is ordered sequentially |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 929 | |
| 930 | This function may be useful if you are not sure of the |
| 931 | device id, and wish to execute other commands using |
| 932 | the ids. By obtaining the list of device ids on the fly, |
| 933 | you can iterate through the list to get mastership, etc. |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 934 | ''' |
| 935 | try: |
| 936 | #Call devices and store result string |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 937 | devices_str = self.devices(json_format=False) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 938 | id_list = [] |
| 939 | |
| 940 | if not devices_str: |
| 941 | main.log.info("There are no devices to get id from") |
| 942 | return id_list |
| 943 | |
| 944 | #Split the string into list by comma |
| 945 | device_list = devices_str.split(",") |
| 946 | #Get temporary list of all arguments with string 'id=' |
| 947 | temp_list = [dev for dev in device_list if "id=" in dev] |
| 948 | #Split list further into arguments before and after string |
| 949 | # 'id='. Get the latter portion (the actual device id) and |
| 950 | # append to id_list |
| 951 | for arg in temp_list: |
| 952 | id_list.append(arg.split("id=")[1]) |
| 953 | |
| 954 | return id_list |
| 955 | |
| 956 | except pexpect.EOF: |
| 957 | main.log.error(self.name + ": EOF exception found") |
| 958 | main.log.error(self.name + ": " + self.handle.before) |
| 959 | main.cleanup() |
| 960 | main.exit() |
| 961 | except: |
| 962 | main.log.info(self.name+" ::::::") |
| 963 | main.log.error( traceback.print_exc()) |
| 964 | main.log.info(self.name+" ::::::") |
| 965 | main.cleanup() |
| 966 | main.exit() |
| 967 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 968 | def get_all_nodes_id(self): |
| 969 | ''' |
| 970 | Uses 'nodes' function to obtain list of all nodes |
| 971 | and parse the result of nodes to obtain just the |
| 972 | node id's. |
| 973 | Returns: |
| 974 | list of node id's |
| 975 | ''' |
| 976 | try: |
| 977 | nodes_str = self.nodes() |
| 978 | id_list = [] |
| 979 | |
| 980 | if not nodes_str: |
| 981 | main.log.info("There are no nodes to get id from") |
| 982 | return id_list |
| 983 | |
| 984 | #Sample nodes_str output |
| 985 | #id=local, address=127.0.0.1:9876, state=ACTIVE * |
| 986 | |
| 987 | #Split the string into list by comma |
| 988 | nodes_list = nodes_str.split(",") |
| 989 | temp_list = [node for node in nodes_list if "id=" in node] |
| 990 | for arg in temp_list: |
| 991 | id_list.append(arg.split("id=")[1]) |
| 992 | |
| 993 | return id_list |
| 994 | |
| 995 | except pexpect.EOF: |
| 996 | main.log.error(self.name + ": EOF exception found") |
| 997 | main.log.error(self.name + ": " + self.handle.before) |
| 998 | main.cleanup() |
| 999 | main.exit() |
| 1000 | except: |
| 1001 | main.log.info(self.name+" ::::::") |
| 1002 | main.log.error( traceback.print_exc()) |
| 1003 | main.log.info(self.name+" ::::::") |
| 1004 | main.cleanup() |
| 1005 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1006 | |
| 1007 | #*********************************** |