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("\$") |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 93 | self.handle.sendline("") |
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...") |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 197 | self.handle.send("\x03") |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 198 | self.handle.sendline("onos -w "+str(ONOS_ip)) |
| 199 | i = self.handle.expect(["onos>",pexpect.TIMEOUT], |
| 200 | timeout=30) |
| 201 | if i == 0: |
andrewonlab | 28ca4b2 | 2014-11-20 13:15:59 -0500 | [diff] [blame] | 202 | main.log.info(str(ONOS_ip)+" CLI Started "+ |
| 203 | "successfully after retry attempt") |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 204 | return main.TRUE |
| 205 | else: |
| 206 | main.log.error("Connection to CLI "+\ |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 207 | str(ONOS_ip)+" timeout") |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 208 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 209 | |
| 210 | except pexpect.EOF: |
| 211 | main.log.error(self.name + ": EOF exception found") |
| 212 | main.log.error(self.name + ": " + self.handle.before) |
| 213 | main.cleanup() |
| 214 | main.exit() |
| 215 | except: |
| 216 | main.log.info(self.name+" ::::::") |
| 217 | main.log.error( traceback.print_exc()) |
| 218 | main.log.info(self.name+" ::::::") |
| 219 | main.cleanup() |
| 220 | main.exit() |
| 221 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 222 | def sendline(self, cmd_str): |
| 223 | ''' |
| 224 | Send a completely user specified string to |
| 225 | the onos> prompt. Use this function if you have |
| 226 | a very specific command to send. |
| 227 | |
| 228 | Warning: There are no sanity checking to commands |
| 229 | sent using this method. |
| 230 | ''' |
| 231 | try: |
| 232 | self.handle.sendline("") |
| 233 | self.handle.expect("onos>") |
| 234 | |
| 235 | self.handle.sendline(cmd_str) |
| 236 | self.handle.expect("onos>") |
| 237 | |
| 238 | handle = self.handle.before |
| 239 | |
| 240 | self.handle.sendline("") |
| 241 | self.handle.expect("onos>") |
| 242 | |
| 243 | handle += self.handle.before |
| 244 | handle += self.handle.after |
| 245 | |
| 246 | main.log.info("Command sent.") |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 247 | ansi_escape = re.compile(r'\x1b[^m]*m') |
| 248 | handle = ansi_escape.sub('', handle) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 249 | |
| 250 | return handle |
| 251 | except pexpect.EOF: |
| 252 | main.log.error(self.name + ": EOF exception found") |
| 253 | main.log.error(self.name + ": " + self.handle.before) |
| 254 | main.cleanup() |
| 255 | main.exit() |
| 256 | except: |
| 257 | main.log.info(self.name+" ::::::") |
| 258 | main.log.error( traceback.print_exc()) |
| 259 | main.log.info(self.name+" ::::::") |
| 260 | main.cleanup() |
| 261 | main.exit() |
| 262 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 263 | #IMPORTANT NOTE: |
| 264 | #For all cli commands, naming convention should match |
| 265 | #the cli command replacing ':' with '_'. |
| 266 | #Ex) onos:topology > onos_topology |
| 267 | # onos:links > onos_links |
| 268 | # feature:list > feature_list |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 269 | |
| 270 | def add_node(self, node_id, ONOS_ip, tcp_port=""): |
| 271 | ''' |
| 272 | Adds a new cluster node by ID and address information. |
| 273 | Required: |
| 274 | * node_id |
| 275 | * ONOS_ip |
| 276 | Optional: |
| 277 | * tcp_port |
| 278 | ''' |
| 279 | try: |
| 280 | self.handle.sendline("") |
| 281 | self.handle.expect("onos>") |
| 282 | |
| 283 | self.handle.sendline("add-node "+ |
| 284 | str(node_id)+" "+ |
| 285 | str(ONOS_ip)+" "+ |
| 286 | str(tcp_port)) |
| 287 | |
| 288 | i = self.handle.expect([ |
| 289 | "Error", |
| 290 | "onos>" ]) |
| 291 | |
| 292 | #Clear handle to get previous output |
| 293 | self.handle.sendline("") |
| 294 | self.handle.expect("onos>") |
| 295 | |
| 296 | handle = self.handle.before |
| 297 | |
| 298 | if i == 0: |
| 299 | main.log.error("Error in adding node") |
| 300 | main.log.error(handle) |
| 301 | return main.FALSE |
| 302 | else: |
| 303 | main.log.info("Node "+str(ONOS_ip)+" added") |
| 304 | return main.TRUE |
| 305 | |
| 306 | except pexpect.EOF: |
| 307 | main.log.error(self.name + ": EOF exception found") |
| 308 | main.log.error(self.name + ": " + self.handle.before) |
| 309 | main.cleanup() |
| 310 | main.exit() |
| 311 | except: |
| 312 | main.log.info(self.name+" ::::::") |
| 313 | main.log.error( traceback.print_exc()) |
| 314 | main.log.info(self.name+" ::::::") |
| 315 | main.cleanup() |
| 316 | main.exit() |
| 317 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 318 | def remove_node(self, node_id): |
| 319 | ''' |
| 320 | Removes a cluster by ID |
| 321 | Issues command: 'remove-node [<node-id>]' |
| 322 | Required: |
| 323 | * node_id |
| 324 | ''' |
| 325 | try: |
| 326 | self.handle.sendline("") |
| 327 | self.handle.expect("onos>") |
| 328 | |
| 329 | self.handle.sendline("remove-node "+str(node_id)) |
| 330 | self.handle.expect("onos>") |
| 331 | |
| 332 | return main.TRUE |
| 333 | |
| 334 | except pexpect.EOF: |
| 335 | main.log.error(self.name + ": EOF exception found") |
| 336 | main.log.error(self.name + ": " + self.handle.before) |
| 337 | main.cleanup() |
| 338 | main.exit() |
| 339 | except: |
| 340 | main.log.info(self.name+" ::::::") |
| 341 | main.log.error( traceback.print_exc()) |
| 342 | main.log.info(self.name+" ::::::") |
| 343 | main.cleanup() |
| 344 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 345 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 346 | def nodes(self): |
| 347 | ''' |
| 348 | List the nodes currently visible |
| 349 | Issues command: 'nodes' |
| 350 | Returns: entire handle of list of nodes |
| 351 | ''' |
| 352 | try: |
| 353 | self.handle.sendline("") |
| 354 | self.handle.expect("onos>") |
| 355 | |
| 356 | self.handle.sendline("nodes") |
| 357 | self.handle.expect("onos>") |
| 358 | |
| 359 | self.handle.sendline("") |
| 360 | self.handle.expect("onos>") |
| 361 | |
| 362 | handle = self.handle.before |
| 363 | |
| 364 | return handle |
| 365 | |
| 366 | except pexpect.EOF: |
| 367 | main.log.error(self.name + ": EOF exception found") |
| 368 | main.log.error(self.name + ": " + self.handle.before) |
| 369 | main.cleanup() |
| 370 | main.exit() |
| 371 | except: |
| 372 | main.log.info(self.name+" ::::::") |
| 373 | main.log.error( traceback.print_exc()) |
| 374 | main.log.info(self.name+" ::::::") |
| 375 | main.cleanup() |
| 376 | main.exit() |
| 377 | |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 378 | def topology(self): |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 379 | ''' |
| 380 | Shows the current state of the topology |
| 381 | by issusing command: 'onos> onos:topology' |
| 382 | ''' |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 383 | try: |
| 384 | self.handle.sendline("") |
| 385 | self.handle.expect("onos>") |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 386 | #either onos:topology or 'topology' will work in CLI |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 387 | self.handle.sendline("onos:topology") |
| 388 | self.handle.expect("onos>") |
| 389 | |
| 390 | handle = self.handle.before |
| 391 | |
| 392 | main.log.info("onos:topology returned: " + |
| 393 | str(handle)) |
| 394 | |
| 395 | return handle |
| 396 | |
| 397 | except pexpect.EOF: |
| 398 | main.log.error(self.name + ": EOF exception found") |
| 399 | main.log.error(self.name + ": " + self.handle.before) |
| 400 | main.cleanup() |
| 401 | main.exit() |
| 402 | except: |
| 403 | main.log.info(self.name+" ::::::") |
| 404 | main.log.error( traceback.print_exc()) |
| 405 | main.log.info(self.name+" ::::::") |
| 406 | main.cleanup() |
| 407 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 408 | |
| 409 | def feature_install(self, feature_str): |
| 410 | ''' |
| 411 | Installs a specified feature |
| 412 | by issuing command: 'onos> feature:install <feature_str>' |
| 413 | ''' |
| 414 | try: |
| 415 | self.handle.sendline("") |
| 416 | self.handle.expect("onos>") |
| 417 | |
| 418 | self.handle.sendline("feature:install "+str(feature_str)) |
| 419 | self.handle.expect("onos>") |
| 420 | |
| 421 | return main.TRUE |
| 422 | |
| 423 | except pexpect.EOF: |
| 424 | main.log.error(self.name + ": EOF exception found") |
| 425 | main.log.error(self.name + ": " + self.handle.before) |
andrewonlab | bf225b0 | 2014-11-12 12:14:05 -0500 | [diff] [blame] | 426 | main.log.report("Failed to install feature") |
| 427 | main.log.report("Exiting test") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 428 | main.cleanup() |
| 429 | main.exit() |
| 430 | except: |
| 431 | main.log.info(self.name+" ::::::") |
| 432 | main.log.error( traceback.print_exc()) |
andrewonlab | bf225b0 | 2014-11-12 12:14:05 -0500 | [diff] [blame] | 433 | main.log.report("Failed to install feature") |
| 434 | main.log.report("Exiting test") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 435 | main.log.info(self.name+" ::::::") |
| 436 | main.cleanup() |
| 437 | main.exit() |
| 438 | |
| 439 | def feature_uninstall(self, feature_str): |
| 440 | ''' |
| 441 | Uninstalls a specified feature |
| 442 | by issuing command: 'onos> feature:uninstall <feature_str>' |
| 443 | ''' |
| 444 | try: |
| 445 | self.handle.sendline("") |
| 446 | self.handle.expect("onos>") |
| 447 | |
| 448 | self.handle.sendline("feature:uninstall "+str(feature_str)) |
| 449 | self.handle.expect("onos>") |
| 450 | |
| 451 | return main.TRUE |
| 452 | |
| 453 | except pexpect.EOF: |
| 454 | main.log.error(self.name + ": EOF exception found") |
| 455 | main.log.error(self.name + ": " + self.handle.before) |
| 456 | main.cleanup() |
| 457 | main.exit() |
| 458 | except: |
| 459 | main.log.info(self.name+" ::::::") |
| 460 | main.log.error( traceback.print_exc()) |
| 461 | main.log.info(self.name+" ::::::") |
| 462 | main.cleanup() |
| 463 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 464 | |
| 465 | def devices(self, json_format=True): |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 466 | ''' |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 467 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 468 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 469 | * json_format - boolean indicating if you want output in json |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 470 | ''' |
| 471 | try: |
| 472 | self.handle.sendline("") |
| 473 | self.handle.expect("onos>") |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 474 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 475 | if json_format: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 476 | self.handle.sendline("devices -j") |
| 477 | self.handle.expect("devices -j") |
| 478 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 479 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 480 | ''' |
| 481 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 482 | 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] | 483 | 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] | 484 | So we take off that escape sequence using |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 485 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 486 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 487 | ''' |
| 488 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 489 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 490 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 491 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 492 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 493 | else: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 494 | self.handle.sendline("devices") |
| 495 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 496 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 497 | #print "handle =",handle |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 498 | return handle |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 499 | except pexpect.EOF: |
| 500 | main.log.error(self.name + ": EOF exception found") |
| 501 | main.log.error(self.name + ": " + self.handle.before) |
| 502 | main.cleanup() |
| 503 | main.exit() |
| 504 | except: |
| 505 | main.log.info(self.name+" ::::::") |
| 506 | main.log.error( traceback.print_exc()) |
| 507 | main.log.info(self.name+" ::::::") |
| 508 | main.cleanup() |
| 509 | main.exit() |
| 510 | |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 511 | |
| 512 | def balance_masters(self): |
| 513 | ''' |
| 514 | This balances the devices across all controllers |
| 515 | by issuing command: 'onos> onos:balance-masters' |
| 516 | If required this could be extended to return devices balanced output. |
| 517 | ''' |
| 518 | try: |
| 519 | self.handle.sendline("") |
| 520 | self.handle.expect("onos>") |
| 521 | |
| 522 | self.handle.sendline("onos:balance-masters") |
| 523 | self.handle.expect("onos>") |
| 524 | return main.TRUE |
| 525 | |
| 526 | except pexpect.EOF: |
| 527 | main.log.error(self.name + ": EOF exception found") |
| 528 | main.log.error(self.name + ": " + self.handle.before) |
| 529 | main.cleanup() |
| 530 | main.exit() |
| 531 | except: |
| 532 | main.log.info(self.name+" ::::::") |
| 533 | main.log.error( traceback.print_exc()) |
| 534 | main.log.info(self.name+" ::::::") |
| 535 | main.cleanup() |
| 536 | main.exit() |
| 537 | |
Hari Krishna | 2bbaa70 | 2014-12-19 14:46:12 -0800 | [diff] [blame^] | 538 | def links(self, json_format=True): |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 539 | ''' |
| 540 | Lists all core links |
| 541 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 542 | * json_format - boolean indicating if you want output in json |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 543 | ''' |
| 544 | try: |
| 545 | self.handle.sendline("") |
| 546 | self.handle.expect("onos>") |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 547 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 548 | if json_format: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 549 | self.handle.sendline("links -j") |
| 550 | self.handle.expect("links -j") |
| 551 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 552 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 553 | ''' |
| 554 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 555 | 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] | 556 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 557 | So we take off that escape sequence using |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 558 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 559 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 560 | ''' |
| 561 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 562 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 563 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 564 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 565 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 566 | else: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 567 | self.handle.sendline("links") |
| 568 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 569 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 570 | #print "handle =",handle |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 571 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 572 | except pexpect.EOF: |
| 573 | main.log.error(self.name + ": EOF exception found") |
| 574 | main.log.error(self.name + ": " + self.handle.before) |
| 575 | main.cleanup() |
| 576 | main.exit() |
| 577 | except: |
| 578 | main.log.info(self.name+" ::::::") |
| 579 | main.log.error( traceback.print_exc()) |
| 580 | main.log.info(self.name+" ::::::") |
| 581 | main.cleanup() |
| 582 | main.exit() |
| 583 | |
| 584 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 585 | def ports(self, json_format=True): |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 586 | ''' |
| 587 | Lists all ports |
| 588 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 589 | * json_format - boolean indicating if you want output in json |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 590 | ''' |
| 591 | try: |
| 592 | self.handle.sendline("") |
| 593 | self.handle.expect("onos>") |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 594 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 595 | if json_format: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 596 | self.handle.sendline("ports -j") |
| 597 | self.handle.expect("ports -j") |
| 598 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 599 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 600 | ''' |
| 601 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 602 | 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] | 603 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 604 | So we take off that escape sequence using the following commads: |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 605 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 606 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 607 | ''' |
| 608 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 609 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 610 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 611 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 612 | return handle1 |
| 613 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 614 | else: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 615 | self.handle.sendline("ports") |
| 616 | self.handle.expect("onos>") |
| 617 | self.handle.sendline("") |
| 618 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 619 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 620 | #print "handle =",handle |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 621 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 622 | except pexpect.EOF: |
| 623 | main.log.error(self.name + ": EOF exception found") |
| 624 | main.log.error(self.name + ": " + self.handle.before) |
| 625 | main.cleanup() |
| 626 | main.exit() |
| 627 | except: |
| 628 | main.log.info(self.name+" ::::::") |
| 629 | main.log.error( traceback.print_exc()) |
| 630 | main.log.info(self.name+" ::::::") |
| 631 | main.cleanup() |
| 632 | main.exit() |
| 633 | |
| 634 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 635 | def roles(self, json_format=True): |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 636 | ''' |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 637 | Lists all devices and the controllers with roles assigned to them |
| 638 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 639 | * json_format - boolean indicating if you want output in json |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 640 | ''' |
| 641 | try: |
| 642 | self.handle.sendline("") |
| 643 | self.handle.expect("onos>") |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 644 | |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 645 | if json_format: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 646 | self.handle.sendline("roles -j") |
| 647 | self.handle.expect("roles -j") |
| 648 | self.handle.expect("onos>") |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 649 | handle = self.handle.before |
| 650 | ''' |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 651 | handle variable here contains some ANSI escape color code sequences at the |
| 652 | end which are invisible in the print command output. To make that escape |
| 653 | sequence visible, use repr() function. The repr(handle) output when printed |
| 654 | shows the ANSI escape sequences. In json.loads(somestring), this somestring |
| 655 | variable is actually repr(somestring) and json.loads would fail with the escape |
| 656 | sequence. |
| 657 | |
| 658 | So we take off that escape sequence using the following commads: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 659 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 660 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 661 | ''' |
| 662 | #print "repr(handle) =", repr(handle) |
| 663 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 664 | handle1 = ansi_escape.sub('', handle) |
| 665 | #print "repr(handle1) = ", repr(handle1) |
| 666 | return handle1 |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 667 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 668 | else: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 669 | self.handle.sendline("roles") |
| 670 | self.handle.expect("onos>") |
| 671 | self.handle.sendline("") |
| 672 | self.handle.expect("onos>") |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 673 | handle = self.handle.before |
| 674 | #print "handle =",handle |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 675 | return handle |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 676 | except pexpect.EOF: |
| 677 | main.log.error(self.name + ": EOF exception found") |
| 678 | main.log.error(self.name + ": " + self.handle.before) |
| 679 | main.cleanup() |
| 680 | main.exit() |
| 681 | except: |
| 682 | main.log.info(self.name+" ::::::") |
| 683 | main.log.error( traceback.print_exc()) |
| 684 | main.log.info(self.name+" ::::::") |
| 685 | main.cleanup() |
| 686 | main.exit() |
| 687 | |
| 688 | def get_role(self, device_id): |
| 689 | ''' |
| 690 | Given the a string containing the json representation of the "roles" cli command and a |
| 691 | partial or whole device id, returns a json object containing the |
| 692 | roles output for the first device whose id contains "device_id" |
| 693 | |
| 694 | Returns: |
| 695 | Dict of the role assignments for the given device or |
| 696 | None if not match |
| 697 | ''' |
| 698 | try: |
| 699 | import json |
| 700 | if device_id == None: |
| 701 | return None |
| 702 | else: |
| 703 | raw_roles = self.roles() |
| 704 | roles_json = json.loads(raw_roles) |
| 705 | #search json for the device with id then return the device |
| 706 | for device in roles_json: |
Jon Hall | 720653a | 2014-10-28 19:02:37 -0400 | [diff] [blame] | 707 | #print device |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 708 | if str(device_id) in device['id']: |
| 709 | return device |
| 710 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 711 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 712 | except pexpect.EOF: |
| 713 | main.log.error(self.name + ": EOF exception found") |
| 714 | main.log.error(self.name + ": " + self.handle.before) |
| 715 | main.cleanup() |
| 716 | main.exit() |
| 717 | except: |
| 718 | main.log.info(self.name+" ::::::") |
| 719 | main.log.error( traceback.print_exc()) |
| 720 | main.log.info(self.name+" ::::::") |
| 721 | main.cleanup() |
| 722 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 723 | |
| 724 | def roles_not_null(self): |
| 725 | ''' |
| 726 | Iterates through each device and checks if there is a master assigned |
| 727 | Returns: main.TRUE if each device has a master |
| 728 | main.FALSE any device has no master |
| 729 | ''' |
| 730 | try: |
| 731 | import json |
| 732 | raw_roles = self.roles() |
| 733 | roles_json = json.loads(raw_roles) |
| 734 | #search json for the device with id then return the device |
| 735 | for device in roles_json: |
| 736 | #print device |
| 737 | if device['master'] == "none": |
| 738 | main.log.warn("Device has no master: " + str(device) ) |
| 739 | return main.FALSE |
| 740 | return main.TRUE |
| 741 | |
| 742 | except pexpect.EOF: |
| 743 | main.log.error(self.name + ": EOF exception found") |
| 744 | main.log.error(self.name + ": " + self.handle.before) |
| 745 | main.cleanup() |
| 746 | main.exit() |
| 747 | except: |
| 748 | main.log.info(self.name+" ::::::") |
| 749 | main.log.error( traceback.print_exc()) |
| 750 | main.log.info(self.name+" ::::::") |
| 751 | main.cleanup() |
| 752 | main.exit() |
| 753 | |
| 754 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 755 | def paths(self, src_id, dst_id): |
| 756 | ''' |
| 757 | Returns string of paths, and the cost. |
| 758 | Issues command: onos:paths <src> <dst> |
| 759 | ''' |
| 760 | try: |
| 761 | self.handle.sendline("") |
| 762 | self.handle.expect("onos>") |
| 763 | |
| 764 | self.handle.sendline("onos:paths "+ |
| 765 | str(src_id) + " " + str(dst_id)) |
| 766 | i = self.handle.expect([ |
| 767 | "Error", |
| 768 | "onos>"]) |
| 769 | |
| 770 | self.handle.sendline("") |
| 771 | self.handle.expect("onos>") |
| 772 | |
| 773 | handle = self.handle.before |
| 774 | |
| 775 | if i == 0: |
| 776 | main.log.error("Error in getting paths") |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 777 | return (handle, "Error") |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 778 | else: |
| 779 | path = handle.split(";")[0] |
| 780 | cost = handle.split(";")[1] |
| 781 | return (path, cost) |
| 782 | |
| 783 | except pexpect.EOF: |
| 784 | main.log.error(self.name + ": EOF exception found") |
| 785 | main.log.error(self.name + ": " + self.handle.before) |
| 786 | main.cleanup() |
| 787 | main.exit() |
| 788 | except: |
| 789 | main.log.info(self.name+" ::::::") |
| 790 | main.log.error( traceback.print_exc()) |
| 791 | main.log.info(self.name+" ::::::") |
| 792 | main.cleanup() |
| 793 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 794 | |
| 795 | def hosts(self, json_format=True): |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 796 | ''' |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 797 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 798 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 799 | * json_format - boolean indicating if you want output in json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 800 | ''' |
| 801 | try: |
| 802 | self.handle.sendline("") |
| 803 | self.handle.expect("onos>") |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 804 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 805 | if json_format: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 806 | self.handle.sendline("hosts -j") |
| 807 | self.handle.expect("hosts -j") |
| 808 | self.handle.expect("onos>") |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 809 | handle = self.handle.before |
| 810 | ''' |
| 811 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 812 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
| 813 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 814 | So we take off that escape sequence using |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 815 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 816 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 817 | ''' |
| 818 | #print "repr(handle) =", repr(handle) |
| 819 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 820 | handle1 = ansi_escape.sub('', handle) |
| 821 | #print "repr(handle1) = ", repr(handle1) |
| 822 | return handle1 |
| 823 | else: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 824 | self.handle.sendline("hosts") |
| 825 | self.handle.expect("onos>") |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 826 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 827 | #print "handle =",handle |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 828 | return handle |
| 829 | except pexpect.EOF: |
| 830 | main.log.error(self.name + ": EOF exception found") |
| 831 | main.log.error(self.name + ": " + self.handle.before) |
| 832 | main.cleanup() |
| 833 | main.exit() |
| 834 | except: |
| 835 | main.log.info(self.name+" ::::::") |
| 836 | main.log.error( traceback.print_exc()) |
| 837 | main.log.info(self.name+" ::::::") |
| 838 | main.cleanup() |
| 839 | main.exit() |
| 840 | |
| 841 | def get_host(self, mac): |
| 842 | ''' |
| 843 | Return the first host from the hosts api whose 'id' contains 'mac' |
| 844 | Note: mac must be a colon seperated mac address, but could be a partial mac address |
| 845 | Return None if there is no match |
| 846 | ''' |
| 847 | import json |
| 848 | try: |
| 849 | if mac == None: |
| 850 | return None |
| 851 | else: |
| 852 | mac = mac |
| 853 | raw_hosts = self.hosts() |
| 854 | hosts_json = json.loads(raw_hosts) |
| 855 | #search json for the host with mac then return the device |
| 856 | for host in hosts_json: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 857 | #print "%s in %s?" % (mac, host['id']) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 858 | if mac in host['id']: |
| 859 | return host |
| 860 | return None |
| 861 | except pexpect.EOF: |
| 862 | main.log.error(self.name + ": EOF exception found") |
| 863 | main.log.error(self.name + ": " + self.handle.before) |
| 864 | main.cleanup() |
| 865 | main.exit() |
| 866 | except: |
| 867 | main.log.info(self.name+" ::::::") |
| 868 | main.log.error( traceback.print_exc()) |
| 869 | main.log.info(self.name+" ::::::") |
| 870 | main.cleanup() |
| 871 | main.exit() |
| 872 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 873 | |
| 874 | def get_hosts_id(self, host_list): |
| 875 | ''' |
| 876 | Obtain list of hosts |
| 877 | Issues command: 'onos> hosts' |
| 878 | |
| 879 | Required: |
| 880 | * host_list: List of hosts obtained by Mininet |
| 881 | IMPORTANT: |
| 882 | This function assumes that you started your |
| 883 | topology with the option '--mac'. |
| 884 | Furthermore, it assumes that value of VLAN is '-1' |
| 885 | Description: |
| 886 | Converts mininet hosts (h1, h2, h3...) into |
| 887 | ONOS format (00:00:00:00:00:01/-1 , ...) |
| 888 | ''' |
| 889 | |
| 890 | try: |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 891 | onos_host_list = [] |
| 892 | |
| 893 | for host in host_list: |
| 894 | host = host.replace("h", "") |
| 895 | host_hex = hex(int(host)).zfill(12) |
| 896 | host_hex = str(host_hex).replace('x','0') |
| 897 | i = iter(str(host_hex)) |
| 898 | host_hex = ":".join(a+b for a,b in zip(i,i)) |
| 899 | host_hex = host_hex + "/-1" |
| 900 | onos_host_list.append(host_hex) |
| 901 | |
| 902 | return onos_host_list |
| 903 | |
| 904 | except pexpect.EOF: |
| 905 | main.log.error(self.name + ": EOF exception found") |
| 906 | main.log.error(self.name + ": " + self.handle.before) |
| 907 | main.cleanup() |
| 908 | main.exit() |
| 909 | except: |
| 910 | main.log.info(self.name+" ::::::") |
| 911 | main.log.error( traceback.print_exc()) |
| 912 | main.log.info(self.name+" ::::::") |
| 913 | main.cleanup() |
| 914 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 915 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 916 | def add_host_intent(self, host_id_one, host_id_two): |
| 917 | ''' |
| 918 | Required: |
| 919 | * host_id_one: ONOS host id for host1 |
| 920 | * host_id_two: ONOS host id for host2 |
| 921 | Description: |
| 922 | Adds a host-to-host intent (bidrectional) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 923 | specifying the two hosts. |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 924 | ''' |
| 925 | try: |
| 926 | self.handle.sendline("") |
| 927 | self.handle.expect("onos>") |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 928 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 929 | self.handle.sendline("add-host-intent "+ |
| 930 | str(host_id_one) + " " + str(host_id_two)) |
| 931 | self.handle.expect("onos>") |
| 932 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 933 | handle = self.handle.before |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 934 | #print "handle =", handle |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 935 | |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 936 | main.log.info("Host intent installed between "+ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 937 | str(host_id_one) + " and " + str(host_id_two)) |
| 938 | |
| 939 | return handle |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 940 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 941 | except pexpect.EOF: |
| 942 | main.log.error(self.name + ": EOF exception found") |
| 943 | main.log.error(self.name + ": " + self.handle.before) |
| 944 | main.cleanup() |
| 945 | main.exit() |
| 946 | except: |
| 947 | main.log.info(self.name+" ::::::") |
| 948 | main.log.error( traceback.print_exc()) |
| 949 | main.log.info(self.name+" ::::::") |
| 950 | main.cleanup() |
| 951 | main.exit() |
| 952 | |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 953 | def add_optical_intent(self, ingress_device, egress_device): |
| 954 | ''' |
| 955 | Required: |
| 956 | * ingress_device: device id of ingress device |
| 957 | * egress_device: device id of egress device |
| 958 | Optional: |
| 959 | TODO: Still needs to be implemented via dev side |
| 960 | ''' |
| 961 | try: |
| 962 | self.handle.sendline("add-optical-intent "+ |
| 963 | str(ingress_device) + " " + str(egress_device)) |
| 964 | self.handle.expect("add-optical-intent") |
| 965 | i = self.handle.expect([ |
| 966 | "Error", |
| 967 | "onos>"]) |
| 968 | |
| 969 | handle = self.handle.before |
| 970 | |
| 971 | #If error, return error message |
| 972 | if i == 0: |
| 973 | return handle |
| 974 | else: |
| 975 | return main.TRUE |
| 976 | |
| 977 | except pexpect.EOF: |
| 978 | main.log.error(self.name + ": EOF exception found") |
| 979 | main.log.error(self.name + ": " + self.handle.before) |
| 980 | main.cleanup() |
| 981 | main.exit() |
| 982 | except: |
| 983 | main.log.info(self.name+" ::::::") |
| 984 | main.log.error( traceback.print_exc()) |
| 985 | main.log.info(self.name+" ::::::") |
| 986 | main.cleanup() |
| 987 | main.exit() |
| 988 | |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 989 | def add_point_intent(self, ingress_device, egress_device, |
| 990 | port_ingress="", port_egress="", ethType="", ethSrc="", |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 991 | ethDst="", bandwidth="", lambda_alloc=False, |
| 992 | ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""): |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 993 | ''' |
| 994 | Required: |
| 995 | * ingress_device: device id of ingress device |
| 996 | * egress_device: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 997 | Optional: |
| 998 | * ethType: specify ethType |
| 999 | * ethSrc: specify ethSrc (i.e. src mac addr) |
| 1000 | * ethDst: specify ethDst (i.e. dst mac addr) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1001 | * bandwidth: specify bandwidth capacity of link |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1002 | * lambda_alloc: if True, intent will allocate lambda |
| 1003 | for the specified intent |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1004 | * ipProto: specify ip protocol |
| 1005 | * ipSrc: specify ip source address |
| 1006 | * ipDst: specify ip destination address |
| 1007 | * tcpSrc: specify tcp source port |
| 1008 | * tcpDst: specify tcp destination port |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1009 | Description: |
| 1010 | Adds a point-to-point intent (uni-directional) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1011 | specifying device id's and optional fields |
| 1012 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1013 | NOTE: This function may change depending on the |
| 1014 | options developers provide for point-to-point |
| 1015 | intent via cli |
| 1016 | ''' |
| 1017 | try: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1018 | cmd = "" |
| 1019 | |
| 1020 | #If there are no optional arguments |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1021 | if not ethType and not ethSrc and not ethDst\ |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1022 | and not bandwidth and not lambda_alloc \ |
| 1023 | and not ipProto and not ipSrc and not ipDst \ |
| 1024 | and not tcpSrc and not tcpDst: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1025 | cmd = "add-point-intent" |
| 1026 | |
| 1027 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1028 | else: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1029 | cmd = "add-point-intent" |
andrewonlab | 9a130be | 2014-10-22 12:44:56 -0400 | [diff] [blame] | 1030 | |
andrewonlab | 0c0a677 | 2014-10-22 12:31:18 -0400 | [diff] [blame] | 1031 | if ethType: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1032 | cmd += " --ethType " + str(ethType) |
| 1033 | if ethSrc: |
| 1034 | cmd += " --ethSrc " + str(ethSrc) |
| 1035 | if ethDst: |
| 1036 | cmd += " --ethDst " + str(ethDst) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1037 | if bandwidth: |
| 1038 | cmd += " --bandwidth " + str(bandwidth) |
| 1039 | if lambda_alloc: |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1040 | cmd += " --lambda " |
| 1041 | if ipProto: |
| 1042 | cmd += " --ipProto " + str(ipProto) |
| 1043 | if ipSrc: |
| 1044 | cmd += " --ipSrc " + str(ipSrc) |
| 1045 | if ipDst: |
| 1046 | cmd += " --ipDst " + str(ipDst) |
| 1047 | if tcpSrc: |
| 1048 | cmd += " --tcpSrc " + str(tcpSrc) |
| 1049 | if tcpDst: |
| 1050 | cmd += " --tcpDst " + str(tcpDst) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1051 | |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1052 | #Check whether the user appended the port |
| 1053 | #or provided it as an input |
| 1054 | if "/" in ingress_device: |
| 1055 | cmd += " "+str(ingress_device) |
| 1056 | else: |
| 1057 | if not port_ingress: |
| 1058 | main.log.error("You must specify "+ |
| 1059 | "the ingress port") |
| 1060 | #TODO: perhaps more meaningful return |
| 1061 | return main.FALSE |
| 1062 | |
| 1063 | cmd += " "+ \ |
| 1064 | str(ingress_device) + "/" +\ |
| 1065 | str(port_ingress) + " " |
| 1066 | |
| 1067 | if "/" in egress_device: |
| 1068 | cmd += " "+str(egress_device) |
| 1069 | else: |
| 1070 | if not port_egress: |
| 1071 | main.log.error("You must specify "+ |
| 1072 | "the egress port") |
| 1073 | return main.FALSE |
| 1074 | |
| 1075 | cmd += " "+\ |
| 1076 | str(egress_device) + "/" +\ |
| 1077 | str(port_egress) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1078 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1079 | self.handle.sendline(cmd) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1080 | |
| 1081 | main.log.info(cmd + " sent") |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1082 | i = self.handle.expect([ |
| 1083 | "Error", |
| 1084 | "onos>"]) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1085 | |
| 1086 | if i == 0: |
| 1087 | main.log.error("Error in adding point-to-point intent") |
| 1088 | return handle |
| 1089 | else: |
| 1090 | return main.TRUE |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1091 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1092 | except pexpect.EOF: |
| 1093 | main.log.error(self.name + ": EOF exception found") |
| 1094 | main.log.error(self.name + ": " + self.handle.before) |
| 1095 | main.cleanup() |
| 1096 | main.exit() |
| 1097 | except: |
| 1098 | main.log.info(self.name+" ::::::") |
| 1099 | main.log.error( traceback.print_exc()) |
| 1100 | main.log.info(self.name+" ::::::") |
| 1101 | main.cleanup() |
| 1102 | main.exit() |
| 1103 | |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1104 | |
| 1105 | def add_multipoint_to_singlepoint_intent(self, ingress_device1, ingress_device2,egress_device, |
| 1106 | port_ingress="", port_egress="", ethType="", ethSrc="", |
| 1107 | ethDst="", bandwidth="", lambda_alloc=False, |
| 1108 | ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst=""): |
| 1109 | ''' |
| 1110 | Note: |
| 1111 | This function assumes that there would be 2 ingress devices and one egress device |
| 1112 | For more number of ingress devices, this function needs to be modified |
| 1113 | Required: |
| 1114 | * ingress_device1: device id of ingress device1 |
| 1115 | * ingress_device2: device id of ingress device2 |
| 1116 | * egress_device: device id of egress device |
| 1117 | Optional: |
| 1118 | * ethType: specify ethType |
| 1119 | * ethSrc: specify ethSrc (i.e. src mac addr) |
| 1120 | * ethDst: specify ethDst (i.e. dst mac addr) |
| 1121 | * bandwidth: specify bandwidth capacity of link |
| 1122 | * lambda_alloc: if True, intent will allocate lambda |
| 1123 | for the specified intent |
| 1124 | * ipProto: specify ip protocol |
| 1125 | * ipSrc: specify ip source address |
| 1126 | * ipDst: specify ip destination address |
| 1127 | * tcpSrc: specify tcp source port |
| 1128 | * tcpDst: specify tcp destination port |
| 1129 | * setEthSrc: action to Rewrite Source MAC Address |
| 1130 | * setEthDst: action to Rewrite Destination MAC Address |
| 1131 | Description: |
| 1132 | Adds a multipoint-to-singlepoint intent (uni-directional) by |
| 1133 | specifying device id's and optional fields |
| 1134 | |
| 1135 | NOTE: This function may change depending on the |
| 1136 | options developers provide for multipointpoint-to-singlepoint |
| 1137 | intent via cli |
| 1138 | ''' |
| 1139 | try: |
| 1140 | cmd = "" |
| 1141 | |
| 1142 | #If there are no optional arguments |
| 1143 | if not ethType and not ethSrc and not ethDst\ |
| 1144 | and not bandwidth and not lambda_alloc \ |
| 1145 | and not ipProto and not ipSrc and not ipDst \ |
| 1146 | and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst: |
| 1147 | cmd = "add-multi-to-single-intent" |
| 1148 | |
| 1149 | |
| 1150 | else: |
| 1151 | cmd = "add-multi-to-single-intent" |
| 1152 | |
| 1153 | if ethType: |
| 1154 | cmd += " --ethType " + str(ethType) |
| 1155 | if ethSrc: |
| 1156 | cmd += " --ethSrc " + str(ethSrc) |
| 1157 | if ethDst: |
| 1158 | cmd += " --ethDst " + str(ethDst) |
| 1159 | if bandwidth: |
| 1160 | cmd += " --bandwidth " + str(bandwidth) |
| 1161 | if lambda_alloc: |
| 1162 | cmd += " --lambda " |
| 1163 | if ipProto: |
| 1164 | cmd += " --ipProto " + str(ipProto) |
| 1165 | if ipSrc: |
| 1166 | cmd += " --ipSrc " + str(ipSrc) |
| 1167 | if ipDst: |
| 1168 | cmd += " --ipDst " + str(ipDst) |
| 1169 | if tcpSrc: |
| 1170 | cmd += " --tcpSrc " + str(tcpSrc) |
| 1171 | if tcpDst: |
| 1172 | cmd += " --tcpDst " + str(tcpDst) |
| 1173 | if setEthSrc: |
| 1174 | cmd += " --setEthSrc "+ str(setEthSrc) |
| 1175 | if setEthDst: |
| 1176 | cmd += " --setEthDst "+ str(setEthDst) |
| 1177 | |
| 1178 | #Check whether the user appended the port |
| 1179 | #or provided it as an input |
| 1180 | if "/" in ingress_device1: |
| 1181 | cmd += " "+str(ingress_device1) |
| 1182 | else: |
| 1183 | if not port_ingress1: |
| 1184 | main.log.error("You must specify "+ |
| 1185 | "the ingress port1") |
| 1186 | #TODO: perhaps more meaningful return |
| 1187 | return main.FALSE |
| 1188 | |
| 1189 | cmd += " "+ \ |
| 1190 | str(ingress_device1) + "/" +\ |
| 1191 | str(port_ingress1) + " " |
| 1192 | |
| 1193 | if "/" in ingress_device2: |
| 1194 | cmd += " "+str(ingress_device2) |
| 1195 | else: |
| 1196 | if not port_ingress2: |
| 1197 | main.log.error("You must specify "+ |
| 1198 | "the ingress port2") |
| 1199 | #TODO: perhaps more meaningful return |
| 1200 | return main.FALSE |
| 1201 | |
| 1202 | cmd += " "+ \ |
| 1203 | str(ingress_device2) + "/" +\ |
| 1204 | str(port_ingress2) + " " |
| 1205 | |
| 1206 | if "/" in egress_device: |
| 1207 | cmd += " "+str(egress_device) |
| 1208 | else: |
| 1209 | if not port_egress: |
| 1210 | main.log.error("You must specify "+ |
| 1211 | "the egress port") |
| 1212 | return main.FALSE |
| 1213 | |
| 1214 | cmd += " "+\ |
| 1215 | str(egress_device) + "/" +\ |
| 1216 | str(port_egress) |
| 1217 | print "cmd= ",cmd |
| 1218 | self.handle.sendline(cmd) |
| 1219 | |
| 1220 | main.log.info(cmd + " sent") |
| 1221 | i = self.handle.expect([ |
| 1222 | "Error", |
| 1223 | "onos>"]) |
| 1224 | |
| 1225 | if i == 0: |
| 1226 | main.log.error("Error in adding point-to-point intent") |
| 1227 | return self.handle |
| 1228 | else: |
| 1229 | return main.TRUE |
| 1230 | |
| 1231 | except pexpect.EOF: |
| 1232 | main.log.error(self.name + ": EOF exception found") |
| 1233 | main.log.error(self.name + ": " + self.handle.before) |
| 1234 | main.cleanup() |
| 1235 | main.exit() |
| 1236 | except: |
| 1237 | main.log.info(self.name+" ::::::") |
| 1238 | main.log.error( traceback.print_exc()) |
| 1239 | main.log.info(self.name+" ::::::") |
| 1240 | main.cleanup() |
| 1241 | main.exit() |
| 1242 | |
| 1243 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1244 | def remove_intent(self, intent_id): |
| 1245 | ''' |
| 1246 | Remove intent for specified intent id |
| 1247 | ''' |
| 1248 | try: |
| 1249 | self.handle.sendline("") |
| 1250 | self.handle.expect("onos>") |
| 1251 | |
| 1252 | self.handle.sendline("remove-intent "+str(intent_id)) |
| 1253 | i = self.handle.expect([ |
| 1254 | "Error", |
| 1255 | "onos>"]) |
| 1256 | |
| 1257 | handle = self.handle.before |
| 1258 | |
| 1259 | if i == 0: |
| 1260 | main.log.error("Error in removing intent") |
| 1261 | return handle |
| 1262 | else: |
| 1263 | return handle |
| 1264 | |
| 1265 | except pexpect.EOF: |
| 1266 | main.log.error(self.name + ": EOF exception found") |
| 1267 | main.log.error(self.name + ": " + self.handle.before) |
| 1268 | main.cleanup() |
| 1269 | main.exit() |
| 1270 | except: |
| 1271 | main.log.info(self.name+" ::::::") |
| 1272 | main.log.error( traceback.print_exc()) |
| 1273 | main.log.info(self.name+" ::::::") |
| 1274 | main.cleanup() |
| 1275 | main.exit() |
| 1276 | |
pingping-lin | dabe797 | 2014-11-17 19:29:44 -0800 | [diff] [blame] | 1277 | # This method should be used after installing application: onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1278 | def routes(self, json_format=False): |
| 1279 | ''' |
| 1280 | Optional: |
| 1281 | * json_format: enable output formatting in json |
| 1282 | Description: |
| 1283 | Obtain all routes in the system |
| 1284 | ''' |
| 1285 | try: |
| 1286 | if json_format: |
| 1287 | self.handle.sendline("routes -j") |
| 1288 | self.handle.expect("routes -j") |
| 1289 | self.handle.expect("onos>") |
| 1290 | handle_tmp = self.handle.before |
| 1291 | |
| 1292 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1293 | handle = ansi_escape.sub('', handle_tmp) |
| 1294 | |
| 1295 | else: |
| 1296 | self.handle.sendline("") |
| 1297 | self.handle.expect("onos>") |
| 1298 | |
| 1299 | self.handle.sendline("routes") |
| 1300 | self.handle.expect("onos>") |
| 1301 | handle = self.handle.before |
| 1302 | |
| 1303 | return handle |
| 1304 | |
| 1305 | except pexpect.EOF: |
| 1306 | main.log.error(self.name + ": EOF exception found") |
| 1307 | main.log.error(self.name + ": " + self.handle.before) |
| 1308 | main.cleanup() |
| 1309 | main.exit() |
| 1310 | except: |
| 1311 | main.log.info(self.name + " ::::::") |
| 1312 | main.log.error(traceback.print_exc()) |
| 1313 | main.log.info(self.name + " ::::::") |
| 1314 | main.cleanup() |
| 1315 | main.exit() |
| 1316 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1317 | def intents(self, json_format = True): |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1318 | ''' |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1319 | Optional: |
| 1320 | * json_format: enable output formatting in json |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1321 | Description: |
| 1322 | Obtain intents currently installed |
| 1323 | ''' |
| 1324 | try: |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1325 | if json_format: |
| 1326 | self.handle.sendline("intents -j") |
| 1327 | self.handle.expect("intents -j") |
| 1328 | self.handle.expect("onos>") |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1329 | handle = self.handle.before |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1330 | |
| 1331 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1332 | handle = ansi_escape.sub('', handle) |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1333 | else: |
| 1334 | self.handle.sendline("") |
| 1335 | self.handle.expect("onos>") |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1336 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1337 | self.handle.sendline("intents") |
| 1338 | self.handle.expect("onos>") |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1339 | handle = self.handle.before |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1340 | |
| 1341 | return handle |
| 1342 | |
| 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() |
| 1354 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1355 | def flows(self, json_format = True): |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1356 | ''' |
| 1357 | Optional: |
| 1358 | * json_format: enable output formatting in json |
| 1359 | Description: |
| 1360 | Obtain flows currently installed |
| 1361 | ''' |
| 1362 | try: |
| 1363 | if json_format: |
| 1364 | self.handle.sendline("flows -j") |
| 1365 | self.handle.expect("flows -j") |
| 1366 | self.handle.expect("onos>") |
| 1367 | handle = self.handle.before |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1368 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1369 | handle = ansi_escape.sub('', handle) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1370 | |
| 1371 | else: |
| 1372 | self.handle.sendline("") |
| 1373 | self.handle.expect("onos>") |
| 1374 | self.handle.sendline("flows") |
| 1375 | self.handle.expect("onos>") |
| 1376 | handle = self.handle.before |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1377 | if re.search("Error\sexecuting\scommand:", handle): |
| 1378 | main.log.error(self.name + ".flows() response: " + str(handle)) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1379 | |
| 1380 | return handle |
| 1381 | |
| 1382 | except pexpect.EOF: |
| 1383 | main.log.error(self.name + ": EOF exception found") |
| 1384 | main.log.error(self.name + ": " + self.handle.before) |
| 1385 | main.cleanup() |
| 1386 | main.exit() |
| 1387 | except: |
| 1388 | main.log.info(self.name+" ::::::") |
| 1389 | main.log.error( traceback.print_exc()) |
| 1390 | main.log.info(self.name+" ::::::") |
| 1391 | main.cleanup() |
| 1392 | main.exit() |
| 1393 | |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1394 | def push_test_intents(self, dpid_src, dpid_dst, num_intents, |
| 1395 | num_mult="", app_id="", report=True): |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1396 | ''' |
| 1397 | Description: |
| 1398 | Push a number of intents in a batch format to |
| 1399 | a specific point-to-point intent definition |
| 1400 | Required: |
| 1401 | * dpid_src: specify source dpid |
| 1402 | * dpid_dst: specify destination dpid |
| 1403 | * num_intents: specify number of intents to push |
| 1404 | Optional: |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1405 | * num_mult: number multiplier for multiplying |
| 1406 | the number of intents specified |
| 1407 | * app_id: specify the application id init to further |
| 1408 | modularize the intents |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1409 | * report: default True, returns latency information |
| 1410 | ''' |
| 1411 | try: |
| 1412 | cmd = "push-test-intents "+\ |
| 1413 | str(dpid_src)+" "+str(dpid_dst)+" "+\ |
| 1414 | str(num_intents) |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1415 | |
| 1416 | if num_mult: |
| 1417 | cmd += " " + str(num_mult) |
andrewonlab | 042b391 | 2014-12-10 16:40:50 -0500 | [diff] [blame] | 1418 | #If app id is specified, then num_mult |
| 1419 | #must exist because of the way this command |
| 1420 | #takes in arguments |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1421 | if app_id: |
| 1422 | cmd += " " + str(app_id) |
| 1423 | |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1424 | self.handle.sendline(cmd) |
| 1425 | self.handle.expect(cmd) |
| 1426 | self.handle.expect("onos>") |
| 1427 | |
| 1428 | handle = self.handle.before |
| 1429 | |
| 1430 | #Some color thing that we want to escape |
| 1431 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1432 | handle = ansi_escape.sub('', handle) |
| 1433 | |
| 1434 | if report: |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1435 | lat_result = [] |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1436 | main.log.info(handle) |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1437 | #Split result by newline |
| 1438 | newline = handle.split("\r\r\n") |
| 1439 | #Ignore the first object of list, which is empty |
| 1440 | newline = newline[1:] |
| 1441 | #Some sloppy parsing method to get the latency |
| 1442 | for result in newline: |
| 1443 | result = result.split(": ") |
| 1444 | #Append the first result of second parse |
| 1445 | lat_result.append(result[1].split(" ")[0]) |
| 1446 | |
andrewonlab | 042b391 | 2014-12-10 16:40:50 -0500 | [diff] [blame] | 1447 | main.log.info(lat_result) |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1448 | return lat_result |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1449 | else: |
| 1450 | return main.TRUE |
| 1451 | |
| 1452 | except pexpect.EOF: |
| 1453 | main.log.error(self.name + ": EOF exception found") |
| 1454 | main.log.error(self.name + ": " + self.handle.before) |
| 1455 | main.cleanup() |
| 1456 | main.exit() |
| 1457 | except: |
| 1458 | main.log.info(self.name+" ::::::") |
| 1459 | main.log.error( traceback.print_exc()) |
| 1460 | main.log.info(self.name+" ::::::") |
| 1461 | main.cleanup() |
| 1462 | main.exit() |
| 1463 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1464 | def intents_events_metrics(self, json_format=True): |
| 1465 | ''' |
| 1466 | Description:Returns topology metrics |
| 1467 | Optional: |
| 1468 | * json_format: enable json formatting of output |
| 1469 | ''' |
| 1470 | try: |
| 1471 | if json_format: |
| 1472 | self.handle.sendline("intents-events-metrics -j") |
| 1473 | self.handle.expect("intents-events-metrics -j") |
| 1474 | self.handle.expect("onos>") |
| 1475 | |
| 1476 | handle = self.handle.before |
| 1477 | |
| 1478 | #Some color thing that we want to escape |
| 1479 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1480 | handle = ansi_escape.sub('', handle) |
| 1481 | |
| 1482 | else: |
| 1483 | self.handle.sendline("intents-events-metrics") |
| 1484 | self.handle.expect("intents-events-metrics") |
| 1485 | self.handle.expect("onos>") |
| 1486 | |
| 1487 | handle = self.handle.before |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1488 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1489 | return handle |
| 1490 | |
| 1491 | except pexpect.EOF: |
| 1492 | main.log.error(self.name + ": EOF exception found") |
| 1493 | main.log.error(self.name + ": " + self.handle.before) |
| 1494 | main.cleanup() |
| 1495 | main.exit() |
| 1496 | except: |
| 1497 | main.log.info(self.name+" ::::::") |
| 1498 | main.log.error( traceback.print_exc()) |
| 1499 | main.log.info(self.name+" ::::::") |
| 1500 | main.cleanup() |
| 1501 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1502 | |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1503 | def topology_events_metrics(self, json_format=True): |
| 1504 | ''' |
| 1505 | Description:Returns topology metrics |
| 1506 | Optional: |
| 1507 | * json_format: enable json formatting of output |
| 1508 | ''' |
| 1509 | try: |
| 1510 | if json_format: |
| 1511 | self.handle.sendline("topology-events-metrics -j") |
| 1512 | self.handle.expect("topology-events-metrics -j") |
| 1513 | self.handle.expect("onos>") |
| 1514 | |
| 1515 | handle = self.handle.before |
| 1516 | |
| 1517 | #Some color thing that we want to escape |
| 1518 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1519 | handle = ansi_escape.sub('', handle) |
| 1520 | |
| 1521 | else: |
| 1522 | self.handle.sendline("topology-events-metrics") |
| 1523 | self.handle.expect("topology-events-metrics") |
| 1524 | self.handle.expect("onos>") |
| 1525 | |
| 1526 | handle = self.handle.before |
| 1527 | |
| 1528 | return handle |
| 1529 | |
| 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 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1542 | #Wrapper functions **************** |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1543 | #Wrapper functions use existing driver |
| 1544 | #functions and extends their use case. |
| 1545 | #For example, we may use the output of |
| 1546 | #a normal driver function, and parse it |
| 1547 | #using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 1548 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1549 | def get_all_intents_id(self): |
| 1550 | ''' |
| 1551 | Description: |
| 1552 | Obtain all intent id's in a list |
| 1553 | ''' |
| 1554 | try: |
| 1555 | #Obtain output of intents function |
| 1556 | intents_str = self.intents() |
| 1557 | all_intent_list = [] |
| 1558 | intent_id_list = [] |
| 1559 | |
| 1560 | #Parse the intents output for ID's |
| 1561 | intents_list = [s.strip() for s in intents_str.splitlines()] |
| 1562 | for intents in intents_list: |
| 1563 | if "onos>" in intents: |
| 1564 | continue |
| 1565 | elif "intents" in intents: |
| 1566 | continue |
| 1567 | else: |
| 1568 | line_list = intents.split(" ") |
| 1569 | all_intent_list.append(line_list[0]) |
| 1570 | |
| 1571 | all_intent_list = all_intent_list[1:-2] |
| 1572 | |
| 1573 | for intents in all_intent_list: |
| 1574 | if not intents: |
| 1575 | continue |
| 1576 | else: |
| 1577 | intent_id_list.append(intents) |
| 1578 | |
| 1579 | return intent_id_list |
| 1580 | |
| 1581 | except pexpect.EOF: |
| 1582 | main.log.error(self.name + ": EOF exception found") |
| 1583 | main.log.error(self.name + ": " + self.handle.before) |
| 1584 | main.cleanup() |
| 1585 | main.exit() |
| 1586 | except: |
| 1587 | main.log.info(self.name+" ::::::") |
| 1588 | main.log.error( traceback.print_exc()) |
| 1589 | main.log.info(self.name+" ::::::") |
| 1590 | main.cleanup() |
| 1591 | main.exit() |
| 1592 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1593 | def get_all_devices_id(self): |
| 1594 | ''' |
| 1595 | Use 'devices' function to obtain list of all devices |
| 1596 | and parse the result to obtain a list of all device |
| 1597 | id's. Returns this list. Returns empty list if no |
| 1598 | devices exist |
| 1599 | List is ordered sequentially |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1600 | |
| 1601 | This function may be useful if you are not sure of the |
| 1602 | device id, and wish to execute other commands using |
| 1603 | the ids. By obtaining the list of device ids on the fly, |
| 1604 | you can iterate through the list to get mastership, etc. |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1605 | ''' |
| 1606 | try: |
| 1607 | #Call devices and store result string |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1608 | devices_str = self.devices(json_format=False) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1609 | id_list = [] |
| 1610 | |
| 1611 | if not devices_str: |
| 1612 | main.log.info("There are no devices to get id from") |
| 1613 | return id_list |
| 1614 | |
| 1615 | #Split the string into list by comma |
| 1616 | device_list = devices_str.split(",") |
| 1617 | #Get temporary list of all arguments with string 'id=' |
| 1618 | temp_list = [dev for dev in device_list if "id=" in dev] |
| 1619 | #Split list further into arguments before and after string |
| 1620 | # 'id='. Get the latter portion (the actual device id) and |
| 1621 | # append to id_list |
| 1622 | for arg in temp_list: |
| 1623 | id_list.append(arg.split("id=")[1]) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1624 | return id_list |
| 1625 | |
| 1626 | except pexpect.EOF: |
| 1627 | main.log.error(self.name + ": EOF exception found") |
| 1628 | main.log.error(self.name + ": " + self.handle.before) |
| 1629 | main.cleanup() |
| 1630 | main.exit() |
| 1631 | except: |
| 1632 | main.log.info(self.name+" ::::::") |
| 1633 | main.log.error( traceback.print_exc()) |
| 1634 | main.log.info(self.name+" ::::::") |
| 1635 | main.cleanup() |
| 1636 | main.exit() |
| 1637 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1638 | def get_all_nodes_id(self): |
| 1639 | ''' |
| 1640 | Uses 'nodes' function to obtain list of all nodes |
| 1641 | and parse the result of nodes to obtain just the |
| 1642 | node id's. |
| 1643 | Returns: |
| 1644 | list of node id's |
| 1645 | ''' |
| 1646 | try: |
| 1647 | nodes_str = self.nodes() |
| 1648 | id_list = [] |
| 1649 | |
| 1650 | if not nodes_str: |
| 1651 | main.log.info("There are no nodes to get id from") |
| 1652 | return id_list |
| 1653 | |
| 1654 | #Sample nodes_str output |
| 1655 | #id=local, address=127.0.0.1:9876, state=ACTIVE * |
| 1656 | |
| 1657 | #Split the string into list by comma |
| 1658 | nodes_list = nodes_str.split(",") |
| 1659 | temp_list = [node for node in nodes_list if "id=" in node] |
| 1660 | for arg in temp_list: |
| 1661 | id_list.append(arg.split("id=")[1]) |
| 1662 | |
| 1663 | return id_list |
| 1664 | |
| 1665 | except pexpect.EOF: |
| 1666 | main.log.error(self.name + ": EOF exception found") |
| 1667 | main.log.error(self.name + ": " + self.handle.before) |
| 1668 | main.cleanup() |
| 1669 | main.exit() |
| 1670 | except: |
| 1671 | main.log.info(self.name+" ::::::") |
| 1672 | main.log.error( traceback.print_exc()) |
| 1673 | main.log.info(self.name+" ::::::") |
| 1674 | main.cleanup() |
| 1675 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1676 | |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1677 | def get_device(self, dpid=None): |
| 1678 | ''' |
| 1679 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 1680 | Return None if there is no match |
| 1681 | ''' |
| 1682 | import json |
| 1683 | try: |
| 1684 | if dpid == None: |
| 1685 | return None |
| 1686 | else: |
| 1687 | dpid = dpid.replace(':', '') |
| 1688 | raw_devices = self.devices() |
| 1689 | devices_json = json.loads(raw_devices) |
| 1690 | #search json for the device with dpid then return the device |
| 1691 | for device in devices_json: |
| 1692 | #print "%s in %s?" % (dpid, device['id']) |
| 1693 | if dpid in device['id']: |
| 1694 | return device |
| 1695 | return None |
| 1696 | except pexpect.EOF: |
| 1697 | main.log.error(self.name + ": EOF exception found") |
| 1698 | main.log.error(self.name + ": " + self.handle.before) |
| 1699 | main.cleanup() |
| 1700 | main.exit() |
| 1701 | except: |
| 1702 | main.log.info(self.name+" ::::::") |
| 1703 | main.log.error( traceback.print_exc()) |
| 1704 | main.log.info(self.name+" ::::::") |
| 1705 | main.cleanup() |
| 1706 | main.exit() |
| 1707 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1708 | def check_status(self, ip, numoswitch, numolink, log_level="info"): |
| 1709 | ''' |
| 1710 | Checks the number of swithes & links that ONOS sees against the |
| 1711 | supplied values. By default this will report to main.log, but the |
| 1712 | log level can be specifid. |
| 1713 | |
| 1714 | Params: ip = ip used for the onos cli |
| 1715 | numoswitch = expected number of switches |
| 1716 | numlink = expected number of links |
| 1717 | log_level = level to log to. Currently accepts 'info', 'warn' and 'report' |
| 1718 | |
| 1719 | |
| 1720 | log_level can |
| 1721 | |
| 1722 | Returns: main.TRUE if the number of switchs and links are correct, |
| 1723 | main.FALSE if the numer of switches and links is incorrect, |
| 1724 | and main.ERROR otherwise |
| 1725 | ''' |
| 1726 | |
| 1727 | try: |
| 1728 | topology = self.get_topology(ip) |
| 1729 | if topology == {}: |
| 1730 | return main.ERROR |
| 1731 | output = "" |
| 1732 | #Is the number of switches is what we expected |
| 1733 | devices = topology.get('devices',False) |
| 1734 | links = topology.get('links',False) |
| 1735 | if devices == False or links == False: |
| 1736 | return main.ERROR |
| 1737 | switch_check = ( int(devices) == int(numoswitch) ) |
| 1738 | #Is the number of links is what we expected |
| 1739 | link_check = ( int(links) == int(numolink) ) |
| 1740 | if (switch_check and link_check): |
| 1741 | #We expected the correct numbers |
| 1742 | output = output + "The number of links and switches match "\ |
| 1743 | + "what was expected" |
| 1744 | result = main.TRUE |
| 1745 | else: |
| 1746 | output = output + \ |
| 1747 | "The number of links and switches does not match what was expected" |
| 1748 | result = main.FALSE |
| 1749 | output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\ |
| 1750 | % ( int(devices), int(numoswitch), int(links), int(numolink) ) |
| 1751 | if log_level == "report": |
| 1752 | main.log.report(output) |
| 1753 | elif log_level == "warn": |
| 1754 | main.log.warn(output) |
| 1755 | else: |
| 1756 | main.log.info(output) |
| 1757 | return result |
| 1758 | except pexpect.EOF: |
| 1759 | main.log.error(self.name + ": EOF exception found") |
| 1760 | main.log.error(self.name + ": " + self.handle.before) |
| 1761 | main.cleanup() |
| 1762 | main.exit() |
| 1763 | except: |
| 1764 | main.log.info(self.name+" ::::::") |
| 1765 | main.log.error( traceback.print_exc()) |
| 1766 | main.log.info(self.name+" ::::::") |
| 1767 | main.cleanup() |
| 1768 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1769 | |
| 1770 | def device_role(self, device_id, onos_node, role="master"): |
| 1771 | ''' |
| 1772 | Calls the device-role cli command. |
| 1773 | device_id must be the id of a device as seen in the onos devices command |
| 1774 | onos_node is the ip of one of the onos nodes in the cluster |
| 1775 | role must be either master, standby, or none |
| 1776 | |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1777 | Returns main.TRUE or main.FALSE based on argument verification. |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1778 | When device-role supports errors this should be extended to |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1779 | support that output |
| 1780 | ''' |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1781 | try: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1782 | #print "beginning device_role... \n\tdevice_id:" + device_id |
| 1783 | #print "\tonos_node: " + onos_node |
| 1784 | #print "\trole: "+ role |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1785 | if role.lower() == "master" or \ |
| 1786 | role.lower() == "standby" or \ |
| 1787 | role.lower() == "none": |
| 1788 | self.handle.sendline("") |
| 1789 | self.handle.expect("onos>") |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1790 | self.handle.sendline("device-role " + |
| 1791 | str(device_id) + " " + |
| 1792 | str(onos_node) + " " + |
| 1793 | str(role)) |
| 1794 | i= self.handle.expect(["Error","onos>"]) |
| 1795 | if i == 0: |
| 1796 | output = str(self.handle.before) |
| 1797 | self.handle.expect("onos>") |
| 1798 | output = output + str(self.handle.before) |
| 1799 | main.log.error(self.name + ": " + |
| 1800 | output + '\033[0m')#end color output to escape any colours from the cli |
| 1801 | return main.ERROR |
| 1802 | self.handle.sendline("") |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1803 | self.handle.expect("onos>") |
| 1804 | return main.TRUE |
| 1805 | else: |
| 1806 | return main.FALSE |
| 1807 | |
| 1808 | except pexpect.EOF: |
| 1809 | main.log.error(self.name + ": EOF exception found") |
| 1810 | main.log.error(self.name + ": " + self.handle.before) |
| 1811 | main.cleanup() |
| 1812 | main.exit() |
| 1813 | except: |
| 1814 | main.log.info(self.name+" ::::::") |
| 1815 | main.log.error( traceback.print_exc()) |
| 1816 | main.log.info(self.name+" ::::::") |
| 1817 | main.cleanup() |
| 1818 | main.exit() |
| 1819 | |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1820 | def clusters(self, json_format=True): |
| 1821 | ''' |
| 1822 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1823 | Optional argument: |
| 1824 | * json_format - boolean indicating if you want output in json |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1825 | ''' |
| 1826 | try: |
| 1827 | self.handle.sendline("") |
| 1828 | self.handle.expect("onos>") |
| 1829 | |
| 1830 | if json_format: |
| 1831 | self.handle.sendline("clusters -j") |
| 1832 | self.handle.expect("clusters -j") |
| 1833 | self.handle.expect("onos>") |
| 1834 | handle = self.handle.before |
| 1835 | ''' |
| 1836 | handle variable here contains some ANSI escape color code |
| 1837 | sequences at the end which are invisible in the print command |
| 1838 | output. To make that escape sequence visible, use repr() function. |
| 1839 | The repr(handle) output when printed shows the ANSI escape sequences. |
| 1840 | In json.loads(somestring), this somestring variable is actually |
| 1841 | repr(somestring) and json.loads would fail with the escape sequence. |
| 1842 | So we take off that escape sequence using |
| 1843 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1844 | handle1 = ansi_escape.sub('', handle) |
| 1845 | ''' |
| 1846 | #print "repr(handle) =", repr(handle) |
| 1847 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1848 | handle1 = ansi_escape.sub('', handle) |
| 1849 | #print "repr(handle1) = ", repr(handle1) |
| 1850 | return handle1 |
| 1851 | else: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1852 | self.handle.sendline("clusters") |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1853 | self.handle.expect("onos>") |
| 1854 | handle = self.handle.before |
| 1855 | #print "handle =",handle |
| 1856 | return handle |
| 1857 | except pexpect.EOF: |
| 1858 | main.log.error(self.name + ": EOF exception found") |
| 1859 | main.log.error(self.name + ": " + self.handle.before) |
| 1860 | main.cleanup() |
| 1861 | main.exit() |
| 1862 | except: |
| 1863 | main.log.info(self.name+" ::::::") |
| 1864 | main.log.error( traceback.print_exc()) |
| 1865 | main.log.info(self.name+" ::::::") |
| 1866 | main.cleanup() |
| 1867 | main.exit() |
| 1868 | |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1869 | def election_test_leader(self): |
| 1870 | ''' |
| 1871 | * CLI command to get the current leader for the Election test application. |
| 1872 | #NOTE: Requires installation of the onos-app-election feature |
| 1873 | Returns: Node IP of the leader if one exists |
| 1874 | None if none exists |
| 1875 | Main.FALSE on error |
| 1876 | ''' |
| 1877 | try: |
| 1878 | self.handle.sendline("election-test-leader") |
| 1879 | self.handle.expect("election-test-leader") |
| 1880 | self.handle.expect("onos>") |
| 1881 | response = self.handle.before |
| 1882 | #Leader |
| 1883 | node_search = re.search("The\scurrent\sleader\sfor\sthe\sElection\sapp\sis\s(?P<node>.+)\.", response) |
| 1884 | if node_search: |
| 1885 | node = node_search.group('node') |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1886 | main.log.info("Election-test-leader on "+str(self.name)+" found " + node + " as the leader") |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1887 | return node |
| 1888 | #no leader |
| 1889 | null_search = re.search("There\sis\scurrently\sno\sleader\selected\sfor\sthe\sElection\sapp", response) |
| 1890 | if null_search: |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1891 | main.log.info("Election-test-leader found no leader on " + self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1892 | return None |
| 1893 | #error |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1894 | if re.search("Command\snot\sfound", response): |
| 1895 | main.log.error("Election app is not loaded on " + self.name) |
| 1896 | return main.FALSE |
| 1897 | else: |
| 1898 | main.log.error("Error in election_test_leader: unexpected response") |
| 1899 | main.log.error( repr(response) ) |
| 1900 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1901 | |
| 1902 | except pexpect.EOF: |
| 1903 | main.log.error(self.name + ": EOF exception found") |
| 1904 | main.log.error(self.name + ": " + self.handle.before) |
| 1905 | main.cleanup() |
| 1906 | main.exit() |
| 1907 | except: |
| 1908 | main.log.info(self.name+" ::::::") |
| 1909 | main.log.error( traceback.print_exc()) |
| 1910 | main.log.info(self.name+" ::::::") |
| 1911 | main.cleanup() |
| 1912 | main.exit() |
| 1913 | |
| 1914 | def election_test_run(self): |
| 1915 | ''' |
| 1916 | * CLI command to run for leadership of the Election test application. |
| 1917 | #NOTE: Requires installation of the onos-app-election feature |
| 1918 | Returns: Main.TRUE on success |
| 1919 | Main.FALSE on error |
| 1920 | ''' |
| 1921 | try: |
| 1922 | self.handle.sendline("election-test-run") |
| 1923 | self.handle.expect("election-test-run") |
| 1924 | self.handle.expect("onos>") |
| 1925 | response = self.handle.before |
| 1926 | #success |
| 1927 | search = re.search("Entering\sleadership\selections\sfor\sthe\sElection\sapp.", response) |
| 1928 | if search: |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1929 | main.log.info(self.name + " entering leadership elections for the Election app.") |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1930 | return main.TRUE |
| 1931 | #error |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1932 | if re.search("Command\snot\sfound", response): |
| 1933 | main.log.error("Election app is not loaded on " + self.name) |
| 1934 | return main.FALSE |
| 1935 | else: |
| 1936 | main.log.error("Error in election_test_run: unexpected response") |
| 1937 | main.log.error( repr(response) ) |
| 1938 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1939 | |
| 1940 | except pexpect.EOF: |
| 1941 | main.log.error(self.name + ": EOF exception found") |
| 1942 | main.log.error(self.name + ": " + self.handle.before) |
| 1943 | main.cleanup() |
| 1944 | main.exit() |
| 1945 | except: |
| 1946 | main.log.info(self.name+" ::::::") |
| 1947 | main.log.error( traceback.print_exc()) |
| 1948 | main.log.info(self.name+" ::::::") |
| 1949 | main.cleanup() |
| 1950 | main.exit() |
| 1951 | |
| 1952 | def election_test_withdraw(self): |
| 1953 | ''' |
| 1954 | * CLI command to withdraw the local node from leadership election for |
| 1955 | * the Election test application. |
| 1956 | #NOTE: Requires installation of the onos-app-election feature |
| 1957 | Returns: Main.TRUE on success |
| 1958 | Main.FALSE on error |
| 1959 | ''' |
| 1960 | try: |
| 1961 | self.handle.sendline("election-test-withdraw") |
| 1962 | self.handle.expect("election-test-withdraw") |
| 1963 | self.handle.expect("onos>") |
| 1964 | response = self.handle.before |
| 1965 | #success |
| 1966 | search = re.search("Withdrawing\sfrom\sleadership\selections\sfor\sthe\sElection\sapp.", response) |
| 1967 | if search: |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1968 | main.log.info(self.name + " withdrawing from leadership elections for the Election app.") |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1969 | return main.TRUE |
| 1970 | #error |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1971 | if re.search("Command\snot\sfound", response): |
| 1972 | main.log.error("Election app is not loaded on " + self.name) |
| 1973 | return main.FALSE |
| 1974 | else: |
| 1975 | main.log.error("Error in election_test_withdraw: unexpected response") |
| 1976 | main.log.error( repr(response) ) |
| 1977 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1978 | |
| 1979 | |
| 1980 | except pexpect.EOF: |
| 1981 | main.log.error(self.name + ": EOF exception found") |
| 1982 | main.log.error(self.name + ": " + self.handle.before) |
| 1983 | main.cleanup() |
| 1984 | main.exit() |
| 1985 | except: |
| 1986 | main.log.info(self.name+" ::::::") |
| 1987 | main.log.error( traceback.print_exc()) |
| 1988 | main.log.info(self.name+" ::::::") |
| 1989 | main.cleanup() |
| 1990 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1991 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1992 | #*********************************** |
Hari Krishna | 2bbaa70 | 2014-12-19 14:46:12 -0800 | [diff] [blame^] | 1993 | def getDevicePortsEnabledCount(self,str(dpid)): |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 1994 | ''' |
| 1995 | Get the count of all enabled ports on a particular device/switch |
| 1996 | ''' |
| 1997 | try: |
Hari Krishna | 2bbaa70 | 2014-12-19 14:46:12 -0800 | [diff] [blame^] | 1998 | dpid = str(dpid) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 1999 | self.handle.sendline("") |
| 2000 | self.handle.expect("onos>") |
| 2001 | |
| 2002 | self.handle.sendline("onos:ports -e "+dpid+" | wc -l") |
| 2003 | i = self.handle.expect([ |
| 2004 | "No such device", |
| 2005 | "onos>"]) |
| 2006 | |
| 2007 | #self.handle.sendline("") |
| 2008 | #self.handle.expect("onos>") |
| 2009 | |
| 2010 | output = self.handle.before |
| 2011 | |
| 2012 | if i == 0: |
| 2013 | main.log.error("Error in getting ports") |
| 2014 | return (ouput, "Error") |
| 2015 | else: |
| 2016 | result = output |
| 2017 | return result |
| 2018 | |
| 2019 | except pexpect.EOF: |
| 2020 | main.log.error(self.name + ": EOF exception found") |
| 2021 | main.log.error(self.name + ": " + self.handle.before) |
| 2022 | main.cleanup() |
| 2023 | main.exit() |
| 2024 | except: |
| 2025 | main.log.info(self.name+" ::::::") |
| 2026 | main.log.error( traceback.print_exc()) |
| 2027 | main.log.info(self.name+" ::::::") |
| 2028 | main.cleanup() |
| 2029 | main.exit() |
| 2030 | |
| 2031 | def getDeviceLinksActiveCount(self,dpid): |
| 2032 | ''' |
| 2033 | Get the count of all enabled ports on a particular device/switch |
| 2034 | ''' |
| 2035 | try: |
Hari Krishna | 2bbaa70 | 2014-12-19 14:46:12 -0800 | [diff] [blame^] | 2036 | dpid = str(dpid) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2037 | self.handle.sendline("") |
| 2038 | self.handle.expect("onos>") |
| 2039 | |
| 2040 | self.handle.sendline("onos:links "+dpid+" | grep ACTIVE | wc -l") |
| 2041 | i = self.handle.expect([ |
| 2042 | "No such device", |
| 2043 | "onos>"]) |
| 2044 | |
| 2045 | output = self.handle.before |
| 2046 | |
| 2047 | if i == 0: |
| 2048 | main.log.error("Error in getting ports") |
| 2049 | return (ouput, "Error") |
| 2050 | else: |
| 2051 | result = output |
| 2052 | return result |
| 2053 | |
| 2054 | except pexpect.EOF: |
| 2055 | main.log.error(self.name + ": EOF exception found") |
| 2056 | main.log.error(self.name + ": " + self.handle.before) |
| 2057 | main.cleanup() |
| 2058 | main.exit() |
| 2059 | except: |
| 2060 | main.log.info(self.name+" ::::::") |
| 2061 | main.log.error( traceback.print_exc()) |
| 2062 | main.log.info(self.name+" ::::::") |
| 2063 | main.cleanup() |
| 2064 | main.exit() |
| 2065 | |
| 2066 | def getAllIntentIds(self): |
| 2067 | ''' |
| 2068 | Return a list of all Intent IDs |
| 2069 | ''' |
| 2070 | try: |
| 2071 | self.handle.sendline("") |
| 2072 | self.handle.expect("onos>") |
| 2073 | |
| 2074 | self.handle.sendline("onos:intents | grep id=") |
| 2075 | i = self.handle.expect([ |
| 2076 | "Error", |
| 2077 | "onos>"]) |
| 2078 | |
| 2079 | output = self.handle.before |
| 2080 | |
| 2081 | if i == 0: |
| 2082 | main.log.error("Error in getting ports") |
| 2083 | return (ouput, "Error") |
| 2084 | else: |
| 2085 | result = output |
| 2086 | return result |
| 2087 | |
| 2088 | except pexpect.EOF: |
| 2089 | main.log.error(self.name + ": EOF exception found") |
| 2090 | main.log.error(self.name + ": " + self.handle.before) |
| 2091 | main.cleanup() |
| 2092 | main.exit() |
| 2093 | except: |
| 2094 | main.log.info(self.name+" ::::::") |
| 2095 | main.log.error( traceback.print_exc()) |
| 2096 | main.log.info(self.name+" ::::::") |
| 2097 | main.cleanup() |
| 2098 | main.exit() |