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