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: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 74 | main.log.info(self.name + ":::::::::::::::::::::::") |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 75 | main.log.error( traceback.print_exc() ) |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 76 | main.log.info(":::::::::::::::::::::::") |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 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 | |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 106 | def logout(self): |
| 107 | ''' |
| 108 | Sends 'logout' command to ONOS cli |
| 109 | ''' |
| 110 | try: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 111 | self.handle.sendline("") |
| 112 | i = self.handle.expect([ |
| 113 | "onos>", |
| 114 | "\$"], timeout=10) |
| 115 | if i == 0: |
| 116 | self.handle.sendline("logout") |
| 117 | self.handle.expect("\$") |
| 118 | elif i == 1: |
| 119 | return main.TRUE |
| 120 | |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 121 | except pexpect.EOF: |
| 122 | main.log.error(self.name + ": eof exception found") |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 123 | main.log.error(self.name + ": " + |
| 124 | self.handle.before) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 125 | main.cleanup() |
| 126 | main.exit() |
| 127 | except: |
| 128 | main.log.info(self.name+" ::::::") |
| 129 | main.log.error( traceback.print_exc()) |
| 130 | main.log.info(self.name+" ::::::") |
| 131 | main.cleanup() |
| 132 | main.exit() |
| 133 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | def set_cell(self, cellname): |
| 135 | ''' |
| 136 | Calls 'cell <name>' to set the environment variables on ONOSbench |
| 137 | |
| 138 | Before issuing any cli commands, set the environment variable first. |
| 139 | ''' |
| 140 | try: |
| 141 | if not cellname: |
| 142 | main.log.error("Must define cellname") |
| 143 | main.cleanup() |
| 144 | main.exit() |
| 145 | else: |
| 146 | self.handle.sendline("cell "+str(cellname)) |
| 147 | #Expect the cellname in the ONOS_CELL variable. |
| 148 | #Note that this variable name is subject to change |
| 149 | # and that this driver will have to change accordingly |
| 150 | self.handle.expect("ONOS_CELL="+str(cellname)) |
| 151 | handle_before = self.handle.before |
| 152 | handle_after = self.handle.after |
| 153 | #Get the rest of the handle |
| 154 | self.handle.sendline("") |
| 155 | self.handle.expect("\$") |
| 156 | handle_more = self.handle.before |
| 157 | |
| 158 | main.log.info("Cell call returned: "+handle_before+ |
| 159 | handle_after + handle_more) |
| 160 | |
| 161 | return main.TRUE |
| 162 | |
| 163 | except pexpect.EOF: |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 164 | main.log.error(self.name + ": eof exception found") |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 165 | main.log.error(self.name + ": " + self.handle.before) |
| 166 | main.cleanup() |
| 167 | main.exit() |
| 168 | except: |
| 169 | main.log.info(self.name+" ::::::") |
| 170 | main.log.error( traceback.print_exc()) |
| 171 | main.log.info(self.name+" ::::::") |
| 172 | main.cleanup() |
| 173 | main.exit() |
| 174 | |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 175 | def start_onos_cli(self, ONOS_ip): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 176 | try: |
| 177 | self.handle.sendline("") |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 178 | x = self.handle.expect([ |
| 179 | "\$", "onos>"], timeout=10) |
| 180 | |
| 181 | if x == 1: |
| 182 | main.log.info("ONOS cli is already running") |
| 183 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 184 | |
| 185 | #Wait for onos start (-w) and enter onos cli |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 186 | self.handle.sendline("onos -w "+str(ONOS_ip)) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 187 | i = self.handle.expect([ |
| 188 | "onos>", |
| 189 | pexpect.TIMEOUT],timeout=60) |
| 190 | |
| 191 | if i == 0: |
| 192 | main.log.info(str(ONOS_ip)+" CLI Started successfully") |
| 193 | return main.TRUE |
| 194 | else: |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 195 | #If failed, send ctrl+c to process and try again |
andrewonlab | f47993a | 2014-10-24 17:56:01 -0400 | [diff] [blame] | 196 | main.log.info("Starting CLI failed. Retrying...") |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 197 | self.handle.sendline("\x03") |
andrewonlab | 367b513 | 2014-11-20 14:20:16 -0500 | [diff] [blame] | 198 | i = self.handle.expect(["onos>",pexpect.TIMEOUT], |
| 199 | timeout=30) |
| 200 | #Send ctrl+d to exit the onos> prompt that was |
| 201 | #not successful |
| 202 | self.handle.sendline("\x04") |
| 203 | self.handle.expect("\$") |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 204 | self.handle.sendline("onos -w "+str(ONOS_ip)) |
| 205 | i = self.handle.expect(["onos>",pexpect.TIMEOUT], |
| 206 | timeout=30) |
| 207 | if i == 0: |
andrewonlab | 28ca4b2 | 2014-11-20 13:15:59 -0500 | [diff] [blame] | 208 | main.log.info(str(ONOS_ip)+" CLI Started "+ |
| 209 | "successfully after retry attempt") |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 210 | return main.TRUE |
| 211 | else: |
| 212 | main.log.error("Connection to CLI "+\ |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 213 | str(ONOS_ip)+" timeout") |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 214 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 215 | |
| 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 | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 228 | def sendline(self, cmd_str): |
| 229 | ''' |
| 230 | Send a completely user specified string to |
| 231 | the onos> prompt. Use this function if you have |
| 232 | a very specific command to send. |
| 233 | |
| 234 | Warning: There are no sanity checking to commands |
| 235 | sent using this method. |
| 236 | ''' |
| 237 | try: |
| 238 | self.handle.sendline("") |
| 239 | self.handle.expect("onos>") |
| 240 | |
| 241 | self.handle.sendline(cmd_str) |
| 242 | self.handle.expect("onos>") |
| 243 | |
| 244 | handle = self.handle.before |
| 245 | |
| 246 | self.handle.sendline("") |
| 247 | self.handle.expect("onos>") |
| 248 | |
| 249 | handle += self.handle.before |
| 250 | handle += self.handle.after |
| 251 | |
| 252 | main.log.info("Command sent.") |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 253 | ansi_escape = re.compile(r'\x1b[^m]*m') |
| 254 | handle = ansi_escape.sub('', handle) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 255 | |
| 256 | return handle |
| 257 | except pexpect.EOF: |
| 258 | main.log.error(self.name + ": EOF exception found") |
| 259 | main.log.error(self.name + ": " + self.handle.before) |
| 260 | main.cleanup() |
| 261 | main.exit() |
| 262 | except: |
| 263 | main.log.info(self.name+" ::::::") |
| 264 | main.log.error( traceback.print_exc()) |
| 265 | main.log.info(self.name+" ::::::") |
| 266 | main.cleanup() |
| 267 | main.exit() |
| 268 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 269 | #IMPORTANT NOTE: |
| 270 | #For all cli commands, naming convention should match |
| 271 | #the cli command replacing ':' with '_'. |
| 272 | #Ex) onos:topology > onos_topology |
| 273 | # onos:links > onos_links |
| 274 | # feature:list > feature_list |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 275 | |
| 276 | def add_node(self, node_id, ONOS_ip, tcp_port=""): |
| 277 | ''' |
| 278 | Adds a new cluster node by ID and address information. |
| 279 | Required: |
| 280 | * node_id |
| 281 | * ONOS_ip |
| 282 | Optional: |
| 283 | * tcp_port |
| 284 | ''' |
| 285 | try: |
| 286 | self.handle.sendline("") |
| 287 | self.handle.expect("onos>") |
| 288 | |
| 289 | self.handle.sendline("add-node "+ |
| 290 | str(node_id)+" "+ |
| 291 | str(ONOS_ip)+" "+ |
| 292 | str(tcp_port)) |
| 293 | |
| 294 | i = self.handle.expect([ |
| 295 | "Error", |
| 296 | "onos>" ]) |
| 297 | |
| 298 | #Clear handle to get previous output |
| 299 | self.handle.sendline("") |
| 300 | self.handle.expect("onos>") |
| 301 | |
| 302 | handle = self.handle.before |
| 303 | |
| 304 | if i == 0: |
| 305 | main.log.error("Error in adding node") |
| 306 | main.log.error(handle) |
| 307 | return main.FALSE |
| 308 | else: |
| 309 | main.log.info("Node "+str(ONOS_ip)+" added") |
| 310 | return main.TRUE |
| 311 | |
| 312 | except pexpect.EOF: |
| 313 | main.log.error(self.name + ": EOF exception found") |
| 314 | main.log.error(self.name + ": " + self.handle.before) |
| 315 | main.cleanup() |
| 316 | main.exit() |
| 317 | except: |
| 318 | main.log.info(self.name+" ::::::") |
| 319 | main.log.error( traceback.print_exc()) |
| 320 | main.log.info(self.name+" ::::::") |
| 321 | main.cleanup() |
| 322 | main.exit() |
| 323 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 324 | def remove_node(self, node_id): |
| 325 | ''' |
| 326 | Removes a cluster by ID |
| 327 | Issues command: 'remove-node [<node-id>]' |
| 328 | Required: |
| 329 | * node_id |
| 330 | ''' |
| 331 | try: |
| 332 | self.handle.sendline("") |
| 333 | self.handle.expect("onos>") |
| 334 | |
| 335 | self.handle.sendline("remove-node "+str(node_id)) |
| 336 | self.handle.expect("onos>") |
| 337 | |
| 338 | return main.TRUE |
| 339 | |
| 340 | except pexpect.EOF: |
| 341 | main.log.error(self.name + ": EOF exception found") |
| 342 | main.log.error(self.name + ": " + self.handle.before) |
| 343 | main.cleanup() |
| 344 | main.exit() |
| 345 | except: |
| 346 | main.log.info(self.name+" ::::::") |
| 347 | main.log.error( traceback.print_exc()) |
| 348 | main.log.info(self.name+" ::::::") |
| 349 | main.cleanup() |
| 350 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 351 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 352 | def nodes(self): |
| 353 | ''' |
| 354 | List the nodes currently visible |
| 355 | Issues command: 'nodes' |
| 356 | Returns: entire handle of list of nodes |
| 357 | ''' |
| 358 | try: |
| 359 | self.handle.sendline("") |
| 360 | self.handle.expect("onos>") |
| 361 | |
| 362 | self.handle.sendline("nodes") |
| 363 | self.handle.expect("onos>") |
| 364 | |
| 365 | self.handle.sendline("") |
| 366 | self.handle.expect("onos>") |
| 367 | |
| 368 | handle = self.handle.before |
| 369 | |
| 370 | return handle |
| 371 | |
| 372 | except pexpect.EOF: |
| 373 | main.log.error(self.name + ": EOF exception found") |
| 374 | main.log.error(self.name + ": " + self.handle.before) |
| 375 | main.cleanup() |
| 376 | main.exit() |
| 377 | except: |
| 378 | main.log.info(self.name+" ::::::") |
| 379 | main.log.error( traceback.print_exc()) |
| 380 | main.log.info(self.name+" ::::::") |
| 381 | main.cleanup() |
| 382 | main.exit() |
| 383 | |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 384 | def topology(self): |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 385 | ''' |
| 386 | Shows the current state of the topology |
| 387 | by issusing command: 'onos> onos:topology' |
| 388 | ''' |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 389 | try: |
| 390 | self.handle.sendline("") |
| 391 | self.handle.expect("onos>") |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 392 | #either onos:topology or 'topology' will work in CLI |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 393 | self.handle.sendline("onos:topology") |
| 394 | self.handle.expect("onos>") |
| 395 | |
| 396 | handle = self.handle.before |
| 397 | |
| 398 | main.log.info("onos:topology returned: " + |
| 399 | str(handle)) |
| 400 | |
| 401 | return handle |
| 402 | |
| 403 | except pexpect.EOF: |
| 404 | main.log.error(self.name + ": EOF exception found") |
| 405 | main.log.error(self.name + ": " + self.handle.before) |
| 406 | main.cleanup() |
| 407 | main.exit() |
| 408 | except: |
| 409 | main.log.info(self.name+" ::::::") |
| 410 | main.log.error( traceback.print_exc()) |
| 411 | main.log.info(self.name+" ::::::") |
| 412 | main.cleanup() |
| 413 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 414 | |
| 415 | def feature_install(self, feature_str): |
| 416 | ''' |
| 417 | Installs a specified feature |
| 418 | by issuing command: 'onos> feature:install <feature_str>' |
| 419 | ''' |
| 420 | try: |
| 421 | self.handle.sendline("") |
| 422 | self.handle.expect("onos>") |
| 423 | |
| 424 | self.handle.sendline("feature:install "+str(feature_str)) |
| 425 | self.handle.expect("onos>") |
| 426 | |
| 427 | return main.TRUE |
| 428 | |
| 429 | except pexpect.EOF: |
| 430 | main.log.error(self.name + ": EOF exception found") |
| 431 | main.log.error(self.name + ": " + self.handle.before) |
andrewonlab | bf225b0 | 2014-11-12 12:14:05 -0500 | [diff] [blame] | 432 | main.log.report("Failed to install feature") |
| 433 | main.log.report("Exiting test") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 434 | main.cleanup() |
| 435 | main.exit() |
| 436 | except: |
| 437 | main.log.info(self.name+" ::::::") |
| 438 | main.log.error( traceback.print_exc()) |
andrewonlab | bf225b0 | 2014-11-12 12:14:05 -0500 | [diff] [blame] | 439 | main.log.report("Failed to install feature") |
| 440 | main.log.report("Exiting test") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 441 | main.log.info(self.name+" ::::::") |
| 442 | main.cleanup() |
| 443 | main.exit() |
| 444 | |
| 445 | def feature_uninstall(self, feature_str): |
| 446 | ''' |
| 447 | Uninstalls a specified feature |
| 448 | by issuing command: 'onos> feature:uninstall <feature_str>' |
| 449 | ''' |
| 450 | try: |
| 451 | self.handle.sendline("") |
| 452 | self.handle.expect("onos>") |
| 453 | |
| 454 | self.handle.sendline("feature:uninstall "+str(feature_str)) |
| 455 | self.handle.expect("onos>") |
| 456 | |
| 457 | return main.TRUE |
| 458 | |
| 459 | except pexpect.EOF: |
| 460 | main.log.error(self.name + ": EOF exception found") |
| 461 | main.log.error(self.name + ": " + self.handle.before) |
| 462 | main.cleanup() |
| 463 | main.exit() |
| 464 | except: |
| 465 | main.log.info(self.name+" ::::::") |
| 466 | main.log.error( traceback.print_exc()) |
| 467 | main.log.info(self.name+" ::::::") |
| 468 | main.cleanup() |
| 469 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 470 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 471 | def devices(self, json_format=True, grep_str=""): |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 472 | ''' |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 473 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 474 | Optional argument: |
| 475 | * grep_str - pass in a string to grep |
| 476 | ''' |
| 477 | try: |
| 478 | self.handle.sendline("") |
| 479 | self.handle.expect("onos>") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 480 | |
| 481 | if json_format: |
| 482 | if not grep_str: |
| 483 | self.handle.sendline("devices -j") |
| 484 | self.handle.expect("devices -j") |
| 485 | self.handle.expect("onos>") |
| 486 | else: |
| 487 | self.handle.sendline("devices -j | grep '"+ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 488 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 489 | self.handle.expect("devices -j | grep '"+str(grep_str)+"'") |
| 490 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 491 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 492 | ''' |
| 493 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 494 | 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] | 495 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 496 | So we take off that escape sequence using |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 497 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 498 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 499 | ''' |
| 500 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 501 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 502 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 503 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 504 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 505 | else: |
| 506 | if not grep_str: |
| 507 | self.handle.sendline("devices") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 508 | self.handle.expect("onos>") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 509 | else: |
| 510 | self.handle.sendline("devices | grep '"+ |
| 511 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 512 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 513 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 514 | #print "handle =",handle |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 515 | return handle |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 516 | except pexpect.EOF: |
| 517 | main.log.error(self.name + ": EOF exception found") |
| 518 | main.log.error(self.name + ": " + self.handle.before) |
| 519 | main.cleanup() |
| 520 | main.exit() |
| 521 | except: |
| 522 | main.log.info(self.name+" ::::::") |
| 523 | main.log.error( traceback.print_exc()) |
| 524 | main.log.info(self.name+" ::::::") |
| 525 | main.cleanup() |
| 526 | main.exit() |
| 527 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 528 | def links(self, json_format=True, grep_str=""): |
| 529 | ''' |
| 530 | Lists all core links |
| 531 | Optional argument: |
| 532 | * grep_str - pass in a string to grep |
| 533 | ''' |
| 534 | try: |
| 535 | self.handle.sendline("") |
| 536 | self.handle.expect("onos>") |
| 537 | |
| 538 | if json_format: |
| 539 | if not grep_str: |
| 540 | self.handle.sendline("links -j") |
| 541 | self.handle.expect("links -j") |
| 542 | self.handle.expect("onos>") |
| 543 | else: |
| 544 | self.handle.sendline("links -j | grep '"+ |
| 545 | str(grep_str)+"'") |
| 546 | self.handle.expect("links -j | grep '"+str(grep_str)+"'") |
| 547 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 548 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 549 | ''' |
| 550 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 551 | 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] | 552 | 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] | 553 | So we take off that escape sequence using |
| 554 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 555 | handle1 = ansi_escape.sub('', handle) |
| 556 | ''' |
| 557 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 558 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 559 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 560 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 561 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 562 | else: |
| 563 | if not grep_str: |
| 564 | self.handle.sendline("links") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 565 | self.handle.expect("onos>") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 566 | else: |
| 567 | self.handle.sendline("links | grep '"+ |
| 568 | str(grep_str)+"'") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -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 |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 571 | #print "handle =",handle |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -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 | |
| 586 | def ports(self, json_format=True, grep_str=""): |
| 587 | ''' |
| 588 | Lists all ports |
| 589 | Optional argument: |
| 590 | * grep_str - pass in a string to grep |
| 591 | ''' |
| 592 | try: |
| 593 | self.handle.sendline("") |
| 594 | self.handle.expect("onos>") |
| 595 | |
| 596 | if json_format: |
| 597 | if not grep_str: |
| 598 | self.handle.sendline("ports -j") |
| 599 | self.handle.expect("ports -j") |
| 600 | self.handle.expect("onos>") |
| 601 | else: |
| 602 | self.handle.sendline("ports -j | grep '"+ |
| 603 | str(grep_str)+"'") |
| 604 | self.handle.expect("ports -j | grep '"+str(grep_str)+"'") |
| 605 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 606 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 607 | ''' |
| 608 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 609 | 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] | 610 | 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] | 611 | So we take off that escape sequence using the following commads: |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 612 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 613 | handle1 = ansi_escape.sub('', handle) |
| 614 | ''' |
| 615 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 616 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 617 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 618 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 619 | return handle1 |
| 620 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 621 | else: |
| 622 | if not grep_str: |
| 623 | self.handle.sendline("ports") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 624 | self.handle.expect("onos>") |
| 625 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 626 | self.handle.expect("onos>") |
| 627 | else: |
| 628 | self.handle.sendline("ports | grep '"+ |
| 629 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 630 | self.handle.expect("onos>") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 631 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 632 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 633 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 634 | #print "handle =",handle |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 635 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 636 | except pexpect.EOF: |
| 637 | main.log.error(self.name + ": EOF exception found") |
| 638 | main.log.error(self.name + ": " + self.handle.before) |
| 639 | main.cleanup() |
| 640 | main.exit() |
| 641 | except: |
| 642 | main.log.info(self.name+" ::::::") |
| 643 | main.log.error( traceback.print_exc()) |
| 644 | main.log.info(self.name+" ::::::") |
| 645 | main.cleanup() |
| 646 | main.exit() |
| 647 | |
| 648 | |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 649 | def roles(self, json_format=True, grep_str=""): |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 650 | ''' |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 651 | Lists all devices and the controllers with roles assigned to them |
| 652 | Optional argument: |
| 653 | * grep_str - pass in a string to grep |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 654 | ''' |
| 655 | try: |
| 656 | self.handle.sendline("") |
| 657 | self.handle.expect("onos>") |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 658 | |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 659 | if json_format: |
| 660 | if not grep_str: |
| 661 | self.handle.sendline("roles -j") |
| 662 | self.handle.expect("roles -j") |
| 663 | self.handle.expect("onos>") |
| 664 | else: |
| 665 | self.handle.sendline("roles -j | grep '"+ |
| 666 | str(grep_str)+"'") |
| 667 | self.handle.expect("roles -j | grep '"+str(grep_str)+"'") |
| 668 | self.handle.expect("onos>") |
| 669 | handle = self.handle.before |
| 670 | ''' |
| 671 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 672 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
| 673 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
| 674 | So we take off that escape sequence using the following commads: |
| 675 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 676 | handle1 = ansi_escape.sub('', handle) |
| 677 | ''' |
| 678 | #print "repr(handle) =", repr(handle) |
| 679 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 680 | handle1 = ansi_escape.sub('', handle) |
| 681 | #print "repr(handle1) = ", repr(handle1) |
| 682 | return handle1 |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 683 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 684 | else: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 685 | if not grep_str: |
| 686 | self.handle.sendline("roles") |
| 687 | self.handle.expect("onos>") |
| 688 | self.handle.sendline("") |
| 689 | self.handle.expect("onos>") |
| 690 | else: |
| 691 | self.handle.sendline("roles | grep '"+ |
| 692 | str(grep_str)+"'") |
| 693 | self.handle.expect("onos>") |
| 694 | self.handle.sendline("") |
| 695 | self.handle.expect("onos>") |
| 696 | handle = self.handle.before |
| 697 | #print "handle =",handle |
| 698 | return handle |
| 699 | except pexpect.EOF: |
| 700 | main.log.error(self.name + ": EOF exception found") |
| 701 | main.log.error(self.name + ": " + self.handle.before) |
| 702 | main.cleanup() |
| 703 | main.exit() |
| 704 | except: |
| 705 | main.log.info(self.name+" ::::::") |
| 706 | main.log.error( traceback.print_exc()) |
| 707 | main.log.info(self.name+" ::::::") |
| 708 | main.cleanup() |
| 709 | main.exit() |
| 710 | |
| 711 | def get_role(self, device_id): |
| 712 | ''' |
| 713 | Given the a string containing the json representation of the "roles" cli command and a |
| 714 | partial or whole device id, returns a json object containing the |
| 715 | roles output for the first device whose id contains "device_id" |
| 716 | |
| 717 | Returns: |
| 718 | Dict of the role assignments for the given device or |
| 719 | None if not match |
| 720 | ''' |
| 721 | try: |
| 722 | import json |
| 723 | if device_id == None: |
| 724 | return None |
| 725 | else: |
| 726 | raw_roles = self.roles() |
| 727 | roles_json = json.loads(raw_roles) |
| 728 | #search json for the device with id then return the device |
| 729 | for device in roles_json: |
Jon Hall | 720653a | 2014-10-28 19:02:37 -0400 | [diff] [blame] | 730 | #print device |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 731 | if str(device_id) in device['id']: |
| 732 | return device |
| 733 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 734 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 735 | except pexpect.EOF: |
| 736 | main.log.error(self.name + ": EOF exception found") |
| 737 | main.log.error(self.name + ": " + self.handle.before) |
| 738 | main.cleanup() |
| 739 | main.exit() |
| 740 | except: |
| 741 | main.log.info(self.name+" ::::::") |
| 742 | main.log.error( traceback.print_exc()) |
| 743 | main.log.info(self.name+" ::::::") |
| 744 | main.cleanup() |
| 745 | main.exit() |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 746 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 747 | def paths(self, src_id, dst_id): |
| 748 | ''' |
| 749 | Returns string of paths, and the cost. |
| 750 | Issues command: onos:paths <src> <dst> |
| 751 | ''' |
| 752 | try: |
| 753 | self.handle.sendline("") |
| 754 | self.handle.expect("onos>") |
| 755 | |
| 756 | self.handle.sendline("onos:paths "+ |
| 757 | str(src_id) + " " + str(dst_id)) |
| 758 | i = self.handle.expect([ |
| 759 | "Error", |
| 760 | "onos>"]) |
| 761 | |
| 762 | self.handle.sendline("") |
| 763 | self.handle.expect("onos>") |
| 764 | |
| 765 | handle = self.handle.before |
| 766 | |
| 767 | if i == 0: |
| 768 | main.log.error("Error in getting paths") |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 769 | return (handle, "Error") |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 770 | else: |
| 771 | path = handle.split(";")[0] |
| 772 | cost = handle.split(";")[1] |
| 773 | return (path, cost) |
| 774 | |
| 775 | except pexpect.EOF: |
| 776 | main.log.error(self.name + ": EOF exception found") |
| 777 | main.log.error(self.name + ": " + self.handle.before) |
| 778 | main.cleanup() |
| 779 | main.exit() |
| 780 | except: |
| 781 | main.log.info(self.name+" ::::::") |
| 782 | main.log.error( traceback.print_exc()) |
| 783 | main.log.info(self.name+" ::::::") |
| 784 | main.cleanup() |
| 785 | main.exit() |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 786 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 787 | def hosts(self, json_format=True, grep_str=""): |
| 788 | ''' |
| 789 | Lists all discovered hosts |
| 790 | Optional argument: |
| 791 | * grep_str - pass in a string to grep |
| 792 | ''' |
| 793 | try: |
| 794 | self.handle.sendline("") |
| 795 | self.handle.expect("onos>") |
| 796 | |
| 797 | if json_format: |
| 798 | if not grep_str: |
| 799 | self.handle.sendline("hosts -j") |
| 800 | self.handle.expect("hosts -j") |
| 801 | self.handle.expect("onos>") |
| 802 | else: |
| 803 | self.handle.sendline("hosts -j | grep '"+ |
| 804 | str(grep_str)+"'") |
| 805 | self.handle.expect("hosts -j | grep '"+str(grep_str)+"'") |
| 806 | self.handle.expect("onos>") |
| 807 | handle = self.handle.before |
| 808 | ''' |
| 809 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 810 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
| 811 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
| 812 | So we take off that escape sequence using |
| 813 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 814 | handle1 = ansi_escape.sub('', handle) |
| 815 | ''' |
| 816 | #print "repr(handle) =", repr(handle) |
| 817 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 818 | handle1 = ansi_escape.sub('', handle) |
| 819 | #print "repr(handle1) = ", repr(handle1) |
| 820 | return handle1 |
| 821 | else: |
| 822 | if not grep_str: |
| 823 | self.handle.sendline("hosts") |
| 824 | self.handle.expect("onos>") |
| 825 | else: |
| 826 | self.handle.sendline("hosts | grep '"+ |
| 827 | str(grep_str)+"'") |
| 828 | self.handle.expect("onos>") |
| 829 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 830 | #print "handle =",handle |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 831 | return handle |
| 832 | except pexpect.EOF: |
| 833 | main.log.error(self.name + ": EOF exception found") |
| 834 | main.log.error(self.name + ": " + self.handle.before) |
| 835 | main.cleanup() |
| 836 | main.exit() |
| 837 | except: |
| 838 | main.log.info(self.name+" ::::::") |
| 839 | main.log.error( traceback.print_exc()) |
| 840 | main.log.info(self.name+" ::::::") |
| 841 | main.cleanup() |
| 842 | main.exit() |
| 843 | |
| 844 | def get_host(self, mac): |
| 845 | ''' |
| 846 | Return the first host from the hosts api whose 'id' contains 'mac' |
| 847 | Note: mac must be a colon seperated mac address, but could be a partial mac address |
| 848 | Return None if there is no match |
| 849 | ''' |
| 850 | import json |
| 851 | try: |
| 852 | if mac == None: |
| 853 | return None |
| 854 | else: |
| 855 | mac = mac |
| 856 | raw_hosts = self.hosts() |
| 857 | hosts_json = json.loads(raw_hosts) |
| 858 | #search json for the host with mac then return the device |
| 859 | for host in hosts_json: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 860 | #print "%s in %s?" % (mac, host['id']) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 861 | if mac in host['id']: |
| 862 | return host |
| 863 | return None |
| 864 | except pexpect.EOF: |
| 865 | main.log.error(self.name + ": EOF exception found") |
| 866 | main.log.error(self.name + ": " + self.handle.before) |
| 867 | main.cleanup() |
| 868 | main.exit() |
| 869 | except: |
| 870 | main.log.info(self.name+" ::::::") |
| 871 | main.log.error( traceback.print_exc()) |
| 872 | main.log.info(self.name+" ::::::") |
| 873 | main.cleanup() |
| 874 | main.exit() |
| 875 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 876 | |
| 877 | def get_hosts_id(self, host_list): |
| 878 | ''' |
| 879 | Obtain list of hosts |
| 880 | Issues command: 'onos> hosts' |
| 881 | |
| 882 | Required: |
| 883 | * host_list: List of hosts obtained by Mininet |
| 884 | IMPORTANT: |
| 885 | This function assumes that you started your |
| 886 | topology with the option '--mac'. |
| 887 | Furthermore, it assumes that value of VLAN is '-1' |
| 888 | Description: |
| 889 | Converts mininet hosts (h1, h2, h3...) into |
| 890 | ONOS format (00:00:00:00:00:01/-1 , ...) |
| 891 | ''' |
| 892 | |
| 893 | try: |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 894 | onos_host_list = [] |
| 895 | |
| 896 | for host in host_list: |
| 897 | host = host.replace("h", "") |
| 898 | host_hex = hex(int(host)).zfill(12) |
| 899 | host_hex = str(host_hex).replace('x','0') |
| 900 | i = iter(str(host_hex)) |
| 901 | host_hex = ":".join(a+b for a,b in zip(i,i)) |
| 902 | host_hex = host_hex + "/-1" |
| 903 | onos_host_list.append(host_hex) |
| 904 | |
| 905 | return onos_host_list |
| 906 | |
| 907 | except pexpect.EOF: |
| 908 | main.log.error(self.name + ": EOF exception found") |
| 909 | main.log.error(self.name + ": " + self.handle.before) |
| 910 | main.cleanup() |
| 911 | main.exit() |
| 912 | except: |
| 913 | main.log.info(self.name+" ::::::") |
| 914 | main.log.error( traceback.print_exc()) |
| 915 | main.log.info(self.name+" ::::::") |
| 916 | main.cleanup() |
| 917 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 918 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 919 | def add_host_intent(self, host_id_one, host_id_two): |
| 920 | ''' |
| 921 | Required: |
| 922 | * host_id_one: ONOS host id for host1 |
| 923 | * host_id_two: ONOS host id for host2 |
| 924 | Description: |
| 925 | Adds a host-to-host intent (bidrectional) by |
| 926 | specifying the two hosts. |
| 927 | ''' |
| 928 | try: |
| 929 | self.handle.sendline("") |
| 930 | self.handle.expect("onos>") |
| 931 | |
| 932 | self.handle.sendline("add-host-intent "+ |
| 933 | str(host_id_one) + " " + str(host_id_two)) |
| 934 | self.handle.expect("onos>") |
| 935 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 936 | handle = self.handle.before |
Shreya Shah | 4f25fdf | 2014-10-29 19:55:35 -0400 | [diff] [blame] | 937 | print "handle =", handle |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 938 | |
| 939 | main.log.info("Intent installed between "+ |
| 940 | str(host_id_one) + " and " + str(host_id_two)) |
| 941 | |
| 942 | return handle |
| 943 | |
| 944 | except pexpect.EOF: |
| 945 | main.log.error(self.name + ": EOF exception found") |
| 946 | main.log.error(self.name + ": " + self.handle.before) |
| 947 | main.cleanup() |
| 948 | main.exit() |
| 949 | except: |
| 950 | main.log.info(self.name+" ::::::") |
| 951 | main.log.error( traceback.print_exc()) |
| 952 | main.log.info(self.name+" ::::::") |
| 953 | main.cleanup() |
| 954 | main.exit() |
| 955 | |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 956 | def add_optical_intent(self, ingress_device, egress_device): |
| 957 | ''' |
| 958 | Required: |
| 959 | * ingress_device: device id of ingress device |
| 960 | * egress_device: device id of egress device |
| 961 | Optional: |
| 962 | TODO: Still needs to be implemented via dev side |
| 963 | ''' |
| 964 | try: |
| 965 | self.handle.sendline("add-optical-intent "+ |
| 966 | str(ingress_device) + " " + str(egress_device)) |
| 967 | self.handle.expect("add-optical-intent") |
| 968 | i = self.handle.expect([ |
| 969 | "Error", |
| 970 | "onos>"]) |
| 971 | |
| 972 | handle = self.handle.before |
| 973 | |
| 974 | #If error, return error message |
| 975 | if i == 0: |
| 976 | return handle |
| 977 | else: |
| 978 | return main.TRUE |
| 979 | |
| 980 | except pexpect.EOF: |
| 981 | main.log.error(self.name + ": EOF exception found") |
| 982 | main.log.error(self.name + ": " + self.handle.before) |
| 983 | main.cleanup() |
| 984 | main.exit() |
| 985 | except: |
| 986 | main.log.info(self.name+" ::::::") |
| 987 | main.log.error( traceback.print_exc()) |
| 988 | main.log.info(self.name+" ::::::") |
| 989 | main.cleanup() |
| 990 | main.exit() |
| 991 | |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 992 | def add_point_intent(self, ingress_device, egress_device, |
| 993 | port_ingress="", port_egress="", ethType="", ethSrc="", |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 994 | ethDst="", bandwidth="", lambda_alloc=False, |
| 995 | ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""): |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 996 | ''' |
| 997 | Required: |
| 998 | * ingress_device: device id of ingress device |
| 999 | * egress_device: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1000 | Optional: |
| 1001 | * ethType: specify ethType |
| 1002 | * ethSrc: specify ethSrc (i.e. src mac addr) |
| 1003 | * ethDst: specify ethDst (i.e. dst mac addr) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1004 | * bandwidth: specify bandwidth capacity of link |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1005 | * lambda_alloc: if True, intent will allocate lambda |
| 1006 | for the specified intent |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1007 | * ipProto: specify ip protocol |
| 1008 | * ipSrc: specify ip source address |
| 1009 | * ipDst: specify ip destination address |
| 1010 | * tcpSrc: specify tcp source port |
| 1011 | * tcpDst: specify tcp destination port |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1012 | Description: |
| 1013 | Adds a point-to-point intent (uni-directional) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1014 | specifying device id's and optional fields |
| 1015 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1016 | NOTE: This function may change depending on the |
| 1017 | options developers provide for point-to-point |
| 1018 | intent via cli |
| 1019 | ''' |
| 1020 | try: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1021 | cmd = "" |
| 1022 | |
| 1023 | #If there are no optional arguments |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1024 | if not ethType and not ethSrc and not ethDst\ |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1025 | and not bandwidth and not lambda_alloc \ |
| 1026 | and not ipProto and not ipSrc and not ipDst \ |
| 1027 | and not tcpSrc and not tcpDst: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1028 | cmd = "add-point-intent" |
| 1029 | |
| 1030 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1031 | else: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1032 | cmd = "add-point-intent" |
andrewonlab | 9a130be | 2014-10-22 12:44:56 -0400 | [diff] [blame] | 1033 | |
andrewonlab | 0c0a677 | 2014-10-22 12:31:18 -0400 | [diff] [blame] | 1034 | if ethType: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1035 | cmd += " --ethType " + str(ethType) |
| 1036 | if ethSrc: |
| 1037 | cmd += " --ethSrc " + str(ethSrc) |
| 1038 | if ethDst: |
| 1039 | cmd += " --ethDst " + str(ethDst) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1040 | if bandwidth: |
| 1041 | cmd += " --bandwidth " + str(bandwidth) |
| 1042 | if lambda_alloc: |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1043 | cmd += " --lambda " |
| 1044 | if ipProto: |
| 1045 | cmd += " --ipProto " + str(ipProto) |
| 1046 | if ipSrc: |
| 1047 | cmd += " --ipSrc " + str(ipSrc) |
| 1048 | if ipDst: |
| 1049 | cmd += " --ipDst " + str(ipDst) |
| 1050 | if tcpSrc: |
| 1051 | cmd += " --tcpSrc " + str(tcpSrc) |
| 1052 | if tcpDst: |
| 1053 | cmd += " --tcpDst " + str(tcpDst) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1054 | |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1055 | #Check whether the user appended the port |
| 1056 | #or provided it as an input |
| 1057 | if "/" in ingress_device: |
| 1058 | cmd += " "+str(ingress_device) |
| 1059 | else: |
| 1060 | if not port_ingress: |
| 1061 | main.log.error("You must specify "+ |
| 1062 | "the ingress port") |
| 1063 | #TODO: perhaps more meaningful return |
| 1064 | return main.FALSE |
| 1065 | |
| 1066 | cmd += " "+ \ |
| 1067 | str(ingress_device) + "/" +\ |
| 1068 | str(port_ingress) + " " |
| 1069 | |
| 1070 | if "/" in egress_device: |
| 1071 | cmd += " "+str(egress_device) |
| 1072 | else: |
| 1073 | if not port_egress: |
| 1074 | main.log.error("You must specify "+ |
| 1075 | "the egress port") |
| 1076 | return main.FALSE |
| 1077 | |
| 1078 | cmd += " "+\ |
| 1079 | str(egress_device) + "/" +\ |
| 1080 | str(port_egress) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1081 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1082 | self.handle.sendline(cmd) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1083 | |
| 1084 | main.log.info(cmd + " sent") |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1085 | i = self.handle.expect([ |
| 1086 | "Error", |
| 1087 | "onos>"]) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1088 | |
| 1089 | if i == 0: |
| 1090 | main.log.error("Error in adding point-to-point intent") |
| 1091 | return handle |
| 1092 | else: |
| 1093 | return main.TRUE |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1094 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1095 | except pexpect.EOF: |
| 1096 | main.log.error(self.name + ": EOF exception found") |
| 1097 | main.log.error(self.name + ": " + self.handle.before) |
| 1098 | main.cleanup() |
| 1099 | main.exit() |
| 1100 | except: |
| 1101 | main.log.info(self.name+" ::::::") |
| 1102 | main.log.error( traceback.print_exc()) |
| 1103 | main.log.info(self.name+" ::::::") |
| 1104 | main.cleanup() |
| 1105 | main.exit() |
| 1106 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1107 | def remove_intent(self, intent_id): |
| 1108 | ''' |
| 1109 | Remove intent for specified intent id |
| 1110 | ''' |
| 1111 | try: |
| 1112 | self.handle.sendline("") |
| 1113 | self.handle.expect("onos>") |
| 1114 | |
| 1115 | self.handle.sendline("remove-intent "+str(intent_id)) |
| 1116 | i = self.handle.expect([ |
| 1117 | "Error", |
| 1118 | "onos>"]) |
| 1119 | |
| 1120 | handle = self.handle.before |
| 1121 | |
| 1122 | if i == 0: |
| 1123 | main.log.error("Error in removing intent") |
| 1124 | return handle |
| 1125 | else: |
| 1126 | return handle |
| 1127 | |
| 1128 | except pexpect.EOF: |
| 1129 | main.log.error(self.name + ": EOF exception found") |
| 1130 | main.log.error(self.name + ": " + self.handle.before) |
| 1131 | main.cleanup() |
| 1132 | main.exit() |
| 1133 | except: |
| 1134 | main.log.info(self.name+" ::::::") |
| 1135 | main.log.error( traceback.print_exc()) |
| 1136 | main.log.info(self.name+" ::::::") |
| 1137 | main.cleanup() |
| 1138 | main.exit() |
| 1139 | |
pingping-lin | dabe797 | 2014-11-17 19:29:44 -0800 | [diff] [blame] | 1140 | # This method should be used after installing application: onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1141 | def routes(self, json_format=False): |
| 1142 | ''' |
| 1143 | Optional: |
| 1144 | * json_format: enable output formatting in json |
| 1145 | Description: |
| 1146 | Obtain all routes in the system |
| 1147 | ''' |
| 1148 | try: |
| 1149 | if json_format: |
| 1150 | self.handle.sendline("routes -j") |
| 1151 | self.handle.expect("routes -j") |
| 1152 | self.handle.expect("onos>") |
| 1153 | handle_tmp = self.handle.before |
| 1154 | |
| 1155 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1156 | handle = ansi_escape.sub('', handle_tmp) |
| 1157 | |
| 1158 | else: |
| 1159 | self.handle.sendline("") |
| 1160 | self.handle.expect("onos>") |
| 1161 | |
| 1162 | self.handle.sendline("routes") |
| 1163 | self.handle.expect("onos>") |
| 1164 | handle = self.handle.before |
| 1165 | |
| 1166 | return handle |
| 1167 | |
| 1168 | except pexpect.EOF: |
| 1169 | main.log.error(self.name + ": EOF exception found") |
| 1170 | main.log.error(self.name + ": " + self.handle.before) |
| 1171 | main.cleanup() |
| 1172 | main.exit() |
| 1173 | except: |
| 1174 | main.log.info(self.name + " ::::::") |
| 1175 | main.log.error(traceback.print_exc()) |
| 1176 | main.log.info(self.name + " ::::::") |
| 1177 | main.cleanup() |
| 1178 | main.exit() |
| 1179 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1180 | def intents(self, json_format = False): |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1181 | ''' |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1182 | Optional: |
| 1183 | * json_format: enable output formatting in json |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1184 | Description: |
| 1185 | Obtain intents currently installed |
| 1186 | ''' |
| 1187 | try: |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1188 | if json_format: |
| 1189 | self.handle.sendline("intents -j") |
| 1190 | self.handle.expect("intents -j") |
| 1191 | self.handle.expect("onos>") |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1192 | handle = self.handle.before |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1193 | |
| 1194 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1195 | handle = ansi_escape.sub('', handle) |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1196 | else: |
| 1197 | self.handle.sendline("") |
| 1198 | self.handle.expect("onos>") |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1199 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1200 | self.handle.sendline("intents") |
| 1201 | self.handle.expect("onos>") |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1202 | handle = self.handle.before |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1203 | |
| 1204 | return handle |
| 1205 | |
| 1206 | except pexpect.EOF: |
| 1207 | main.log.error(self.name + ": EOF exception found") |
| 1208 | main.log.error(self.name + ": " + self.handle.before) |
| 1209 | main.cleanup() |
| 1210 | main.exit() |
| 1211 | except: |
| 1212 | main.log.info(self.name+" ::::::") |
| 1213 | main.log.error( traceback.print_exc()) |
| 1214 | main.log.info(self.name+" ::::::") |
| 1215 | main.cleanup() |
| 1216 | main.exit() |
| 1217 | |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1218 | def flows(self, json_format = False): |
| 1219 | ''' |
| 1220 | Optional: |
| 1221 | * json_format: enable output formatting in json |
| 1222 | Description: |
| 1223 | Obtain flows currently installed |
| 1224 | ''' |
| 1225 | try: |
| 1226 | if json_format: |
| 1227 | self.handle.sendline("flows -j") |
| 1228 | self.handle.expect("flows -j") |
| 1229 | self.handle.expect("onos>") |
| 1230 | handle = self.handle.before |
| 1231 | |
| 1232 | else: |
| 1233 | self.handle.sendline("") |
| 1234 | self.handle.expect("onos>") |
| 1235 | self.handle.sendline("flows") |
| 1236 | self.handle.expect("onos>") |
| 1237 | handle = self.handle.before |
| 1238 | |
| 1239 | return handle |
| 1240 | |
| 1241 | except pexpect.EOF: |
| 1242 | main.log.error(self.name + ": EOF exception found") |
| 1243 | main.log.error(self.name + ": " + self.handle.before) |
| 1244 | main.cleanup() |
| 1245 | main.exit() |
| 1246 | except: |
| 1247 | main.log.info(self.name+" ::::::") |
| 1248 | main.log.error( traceback.print_exc()) |
| 1249 | main.log.info(self.name+" ::::::") |
| 1250 | main.cleanup() |
| 1251 | main.exit() |
| 1252 | |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1253 | def push_test_intents(self, dpid_src, dpid_dst, num_intents, |
| 1254 | report=True): |
| 1255 | ''' |
| 1256 | Description: |
| 1257 | Push a number of intents in a batch format to |
| 1258 | a specific point-to-point intent definition |
| 1259 | Required: |
| 1260 | * dpid_src: specify source dpid |
| 1261 | * dpid_dst: specify destination dpid |
| 1262 | * num_intents: specify number of intents to push |
| 1263 | Optional: |
| 1264 | * report: default True, returns latency information |
| 1265 | ''' |
| 1266 | try: |
| 1267 | cmd = "push-test-intents "+\ |
| 1268 | str(dpid_src)+" "+str(dpid_dst)+" "+\ |
| 1269 | str(num_intents) |
| 1270 | self.handle.sendline(cmd) |
| 1271 | self.handle.expect(cmd) |
| 1272 | self.handle.expect("onos>") |
| 1273 | |
| 1274 | handle = self.handle.before |
| 1275 | |
| 1276 | #Some color thing that we want to escape |
| 1277 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1278 | handle = ansi_escape.sub('', handle) |
| 1279 | |
| 1280 | if report: |
| 1281 | main.log.info(handle) |
| 1282 | return handle |
| 1283 | else: |
| 1284 | return main.TRUE |
| 1285 | |
| 1286 | except pexpect.EOF: |
| 1287 | main.log.error(self.name + ": EOF exception found") |
| 1288 | main.log.error(self.name + ": " + self.handle.before) |
| 1289 | main.cleanup() |
| 1290 | main.exit() |
| 1291 | except: |
| 1292 | main.log.info(self.name+" ::::::") |
| 1293 | main.log.error( traceback.print_exc()) |
| 1294 | main.log.info(self.name+" ::::::") |
| 1295 | main.cleanup() |
| 1296 | main.exit() |
| 1297 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1298 | def intents_events_metrics(self, json_format=True): |
| 1299 | ''' |
| 1300 | Description:Returns topology metrics |
| 1301 | Optional: |
| 1302 | * json_format: enable json formatting of output |
| 1303 | ''' |
| 1304 | try: |
| 1305 | if json_format: |
| 1306 | self.handle.sendline("intents-events-metrics -j") |
| 1307 | self.handle.expect("intents-events-metrics -j") |
| 1308 | self.handle.expect("onos>") |
| 1309 | |
| 1310 | handle = self.handle.before |
| 1311 | |
| 1312 | #Some color thing that we want to escape |
| 1313 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1314 | handle = ansi_escape.sub('', handle) |
| 1315 | |
| 1316 | else: |
| 1317 | self.handle.sendline("intents-events-metrics") |
| 1318 | self.handle.expect("intents-events-metrics") |
| 1319 | self.handle.expect("onos>") |
| 1320 | |
| 1321 | handle = self.handle.before |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1322 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1323 | return handle |
| 1324 | |
| 1325 | except pexpect.EOF: |
| 1326 | main.log.error(self.name + ": EOF exception found") |
| 1327 | main.log.error(self.name + ": " + self.handle.before) |
| 1328 | main.cleanup() |
| 1329 | main.exit() |
| 1330 | except: |
| 1331 | main.log.info(self.name+" ::::::") |
| 1332 | main.log.error( traceback.print_exc()) |
| 1333 | main.log.info(self.name+" ::::::") |
| 1334 | main.cleanup() |
| 1335 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1336 | |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1337 | def topology_events_metrics(self, json_format=True): |
| 1338 | ''' |
| 1339 | Description:Returns topology metrics |
| 1340 | Optional: |
| 1341 | * json_format: enable json formatting of output |
| 1342 | ''' |
| 1343 | try: |
| 1344 | if json_format: |
| 1345 | self.handle.sendline("topology-events-metrics -j") |
| 1346 | self.handle.expect("topology-events-metrics -j") |
| 1347 | self.handle.expect("onos>") |
| 1348 | |
| 1349 | handle = self.handle.before |
| 1350 | |
| 1351 | #Some color thing that we want to escape |
| 1352 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1353 | handle = ansi_escape.sub('', handle) |
| 1354 | |
| 1355 | else: |
| 1356 | self.handle.sendline("topology-events-metrics") |
| 1357 | self.handle.expect("topology-events-metrics") |
| 1358 | self.handle.expect("onos>") |
| 1359 | |
| 1360 | handle = self.handle.before |
| 1361 | |
| 1362 | return handle |
| 1363 | |
| 1364 | except pexpect.EOF: |
| 1365 | main.log.error(self.name + ": EOF exception found") |
| 1366 | main.log.error(self.name + ": " + self.handle.before) |
| 1367 | main.cleanup() |
| 1368 | main.exit() |
| 1369 | except: |
| 1370 | main.log.info(self.name+" ::::::") |
| 1371 | main.log.error( traceback.print_exc()) |
| 1372 | main.log.info(self.name+" ::::::") |
| 1373 | main.cleanup() |
| 1374 | main.exit() |
| 1375 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1376 | #Wrapper functions **************** |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1377 | #Wrapper functions use existing driver |
| 1378 | #functions and extends their use case. |
| 1379 | #For example, we may use the output of |
| 1380 | #a normal driver function, and parse it |
| 1381 | #using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 1382 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1383 | def get_all_intents_id(self): |
| 1384 | ''' |
| 1385 | Description: |
| 1386 | Obtain all intent id's in a list |
| 1387 | ''' |
| 1388 | try: |
| 1389 | #Obtain output of intents function |
| 1390 | intents_str = self.intents() |
| 1391 | all_intent_list = [] |
| 1392 | intent_id_list = [] |
| 1393 | |
| 1394 | #Parse the intents output for ID's |
| 1395 | intents_list = [s.strip() for s in intents_str.splitlines()] |
| 1396 | for intents in intents_list: |
| 1397 | if "onos>" in intents: |
| 1398 | continue |
| 1399 | elif "intents" in intents: |
| 1400 | continue |
| 1401 | else: |
| 1402 | line_list = intents.split(" ") |
| 1403 | all_intent_list.append(line_list[0]) |
| 1404 | |
| 1405 | all_intent_list = all_intent_list[1:-2] |
| 1406 | |
| 1407 | for intents in all_intent_list: |
| 1408 | if not intents: |
| 1409 | continue |
| 1410 | else: |
| 1411 | intent_id_list.append(intents) |
| 1412 | |
| 1413 | return intent_id_list |
| 1414 | |
| 1415 | except pexpect.EOF: |
| 1416 | main.log.error(self.name + ": EOF exception found") |
| 1417 | main.log.error(self.name + ": " + self.handle.before) |
| 1418 | main.cleanup() |
| 1419 | main.exit() |
| 1420 | except: |
| 1421 | main.log.info(self.name+" ::::::") |
| 1422 | main.log.error( traceback.print_exc()) |
| 1423 | main.log.info(self.name+" ::::::") |
| 1424 | main.cleanup() |
| 1425 | main.exit() |
| 1426 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1427 | def get_all_devices_id(self): |
| 1428 | ''' |
| 1429 | Use 'devices' function to obtain list of all devices |
| 1430 | and parse the result to obtain a list of all device |
| 1431 | id's. Returns this list. Returns empty list if no |
| 1432 | devices exist |
| 1433 | List is ordered sequentially |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1434 | |
| 1435 | This function may be useful if you are not sure of the |
| 1436 | device id, and wish to execute other commands using |
| 1437 | the ids. By obtaining the list of device ids on the fly, |
| 1438 | you can iterate through the list to get mastership, etc. |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1439 | ''' |
| 1440 | try: |
| 1441 | #Call devices and store result string |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1442 | devices_str = self.devices(json_format=False) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1443 | id_list = [] |
| 1444 | |
| 1445 | if not devices_str: |
| 1446 | main.log.info("There are no devices to get id from") |
| 1447 | return id_list |
| 1448 | |
| 1449 | #Split the string into list by comma |
| 1450 | device_list = devices_str.split(",") |
| 1451 | #Get temporary list of all arguments with string 'id=' |
| 1452 | temp_list = [dev for dev in device_list if "id=" in dev] |
| 1453 | #Split list further into arguments before and after string |
| 1454 | # 'id='. Get the latter portion (the actual device id) and |
| 1455 | # append to id_list |
| 1456 | for arg in temp_list: |
| 1457 | id_list.append(arg.split("id=")[1]) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1458 | return id_list |
| 1459 | |
| 1460 | except pexpect.EOF: |
| 1461 | main.log.error(self.name + ": EOF exception found") |
| 1462 | main.log.error(self.name + ": " + self.handle.before) |
| 1463 | main.cleanup() |
| 1464 | main.exit() |
| 1465 | except: |
| 1466 | main.log.info(self.name+" ::::::") |
| 1467 | main.log.error( traceback.print_exc()) |
| 1468 | main.log.info(self.name+" ::::::") |
| 1469 | main.cleanup() |
| 1470 | main.exit() |
| 1471 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1472 | def get_all_nodes_id(self): |
| 1473 | ''' |
| 1474 | Uses 'nodes' function to obtain list of all nodes |
| 1475 | and parse the result of nodes to obtain just the |
| 1476 | node id's. |
| 1477 | Returns: |
| 1478 | list of node id's |
| 1479 | ''' |
| 1480 | try: |
| 1481 | nodes_str = self.nodes() |
| 1482 | id_list = [] |
| 1483 | |
| 1484 | if not nodes_str: |
| 1485 | main.log.info("There are no nodes to get id from") |
| 1486 | return id_list |
| 1487 | |
| 1488 | #Sample nodes_str output |
| 1489 | #id=local, address=127.0.0.1:9876, state=ACTIVE * |
| 1490 | |
| 1491 | #Split the string into list by comma |
| 1492 | nodes_list = nodes_str.split(",") |
| 1493 | temp_list = [node for node in nodes_list if "id=" in node] |
| 1494 | for arg in temp_list: |
| 1495 | id_list.append(arg.split("id=")[1]) |
| 1496 | |
| 1497 | return id_list |
| 1498 | |
| 1499 | except pexpect.EOF: |
| 1500 | main.log.error(self.name + ": EOF exception found") |
| 1501 | main.log.error(self.name + ": " + self.handle.before) |
| 1502 | main.cleanup() |
| 1503 | main.exit() |
| 1504 | except: |
| 1505 | main.log.info(self.name+" ::::::") |
| 1506 | main.log.error( traceback.print_exc()) |
| 1507 | main.log.info(self.name+" ::::::") |
| 1508 | main.cleanup() |
| 1509 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1510 | |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1511 | def get_device(self, dpid=None): |
| 1512 | ''' |
| 1513 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 1514 | Return None if there is no match |
| 1515 | ''' |
| 1516 | import json |
| 1517 | try: |
| 1518 | if dpid == None: |
| 1519 | return None |
| 1520 | else: |
| 1521 | dpid = dpid.replace(':', '') |
| 1522 | raw_devices = self.devices() |
| 1523 | devices_json = json.loads(raw_devices) |
| 1524 | #search json for the device with dpid then return the device |
| 1525 | for device in devices_json: |
| 1526 | #print "%s in %s?" % (dpid, device['id']) |
| 1527 | if dpid in device['id']: |
| 1528 | return device |
| 1529 | return None |
| 1530 | except pexpect.EOF: |
| 1531 | main.log.error(self.name + ": EOF exception found") |
| 1532 | main.log.error(self.name + ": " + self.handle.before) |
| 1533 | main.cleanup() |
| 1534 | main.exit() |
| 1535 | except: |
| 1536 | main.log.info(self.name+" ::::::") |
| 1537 | main.log.error( traceback.print_exc()) |
| 1538 | main.log.info(self.name+" ::::::") |
| 1539 | main.cleanup() |
| 1540 | main.exit() |
| 1541 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1542 | def check_status(self, ip, numoswitch, numolink, log_level="info"): |
| 1543 | ''' |
| 1544 | Checks the number of swithes & links that ONOS sees against the |
| 1545 | supplied values. By default this will report to main.log, but the |
| 1546 | log level can be specifid. |
| 1547 | |
| 1548 | Params: ip = ip used for the onos cli |
| 1549 | numoswitch = expected number of switches |
| 1550 | numlink = expected number of links |
| 1551 | log_level = level to log to. Currently accepts 'info', 'warn' and 'report' |
| 1552 | |
| 1553 | |
| 1554 | log_level can |
| 1555 | |
| 1556 | Returns: main.TRUE if the number of switchs and links are correct, |
| 1557 | main.FALSE if the numer of switches and links is incorrect, |
| 1558 | and main.ERROR otherwise |
| 1559 | ''' |
| 1560 | |
| 1561 | try: |
| 1562 | topology = self.get_topology(ip) |
| 1563 | if topology == {}: |
| 1564 | return main.ERROR |
| 1565 | output = "" |
| 1566 | #Is the number of switches is what we expected |
| 1567 | devices = topology.get('devices',False) |
| 1568 | links = topology.get('links',False) |
| 1569 | if devices == False or links == False: |
| 1570 | return main.ERROR |
| 1571 | switch_check = ( int(devices) == int(numoswitch) ) |
| 1572 | #Is the number of links is what we expected |
| 1573 | link_check = ( int(links) == int(numolink) ) |
| 1574 | if (switch_check and link_check): |
| 1575 | #We expected the correct numbers |
| 1576 | output = output + "The number of links and switches match "\ |
| 1577 | + "what was expected" |
| 1578 | result = main.TRUE |
| 1579 | else: |
| 1580 | output = output + \ |
| 1581 | "The number of links and switches does not match what was expected" |
| 1582 | result = main.FALSE |
| 1583 | output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\ |
| 1584 | % ( int(devices), int(numoswitch), int(links), int(numolink) ) |
| 1585 | if log_level == "report": |
| 1586 | main.log.report(output) |
| 1587 | elif log_level == "warn": |
| 1588 | main.log.warn(output) |
| 1589 | else: |
| 1590 | main.log.info(output) |
| 1591 | return result |
| 1592 | except pexpect.EOF: |
| 1593 | main.log.error(self.name + ": EOF exception found") |
| 1594 | main.log.error(self.name + ": " + self.handle.before) |
| 1595 | main.cleanup() |
| 1596 | main.exit() |
| 1597 | except: |
| 1598 | main.log.info(self.name+" ::::::") |
| 1599 | main.log.error( traceback.print_exc()) |
| 1600 | main.log.info(self.name+" ::::::") |
| 1601 | main.cleanup() |
| 1602 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1603 | |
| 1604 | def device_role(self, device_id, onos_node, role="master"): |
| 1605 | ''' |
| 1606 | Calls the device-role cli command. |
| 1607 | device_id must be the id of a device as seen in the onos devices command |
| 1608 | onos_node is the ip of one of the onos nodes in the cluster |
| 1609 | role must be either master, standby, or none |
| 1610 | |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1611 | Returns main.TRUE or main.FALSE based argument varification. |
| 1612 | When device-role supports errors this should be extended to |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1613 | support that output |
| 1614 | ''' |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1615 | try: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1616 | #print "beginning device_role... \n\tdevice_id:" + device_id |
| 1617 | #print "\tonos_node: " + onos_node |
| 1618 | #print "\trole: "+ role |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1619 | if role.lower() == "master" or \ |
| 1620 | role.lower() == "standby" or \ |
| 1621 | role.lower() == "none": |
| 1622 | self.handle.sendline("") |
| 1623 | self.handle.expect("onos>") |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1624 | self.handle.sendline("device-role " + |
| 1625 | str(device_id) + " " + |
| 1626 | str(onos_node) + " " + |
| 1627 | str(role)) |
| 1628 | i= self.handle.expect(["Error","onos>"]) |
| 1629 | if i == 0: |
| 1630 | output = str(self.handle.before) |
| 1631 | self.handle.expect("onos>") |
| 1632 | output = output + str(self.handle.before) |
| 1633 | main.log.error(self.name + ": " + |
| 1634 | output + '\033[0m')#end color output to escape any colours from the cli |
| 1635 | return main.ERROR |
| 1636 | self.handle.sendline("") |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1637 | self.handle.expect("onos>") |
| 1638 | return main.TRUE |
| 1639 | else: |
| 1640 | return main.FALSE |
| 1641 | |
| 1642 | except pexpect.EOF: |
| 1643 | main.log.error(self.name + ": EOF exception found") |
| 1644 | main.log.error(self.name + ": " + self.handle.before) |
| 1645 | main.cleanup() |
| 1646 | main.exit() |
| 1647 | except: |
| 1648 | main.log.info(self.name+" ::::::") |
| 1649 | main.log.error( traceback.print_exc()) |
| 1650 | main.log.info(self.name+" ::::::") |
| 1651 | main.cleanup() |
| 1652 | main.exit() |
| 1653 | |
| 1654 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1655 | #*********************************** |