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 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 511 | def links(self, json_format=True): |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 512 | ''' |
| 513 | Lists all core links |
| 514 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 515 | * json_format - boolean indicating if you want output in json |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 516 | ''' |
| 517 | try: |
| 518 | self.handle.sendline("") |
| 519 | self.handle.expect("onos>") |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 520 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 521 | if json_format: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 522 | self.handle.sendline("links -j") |
| 523 | self.handle.expect("links -j") |
| 524 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 525 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 526 | ''' |
| 527 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 528 | 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] | 529 | 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] | 530 | So we take off that escape sequence using |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 531 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 532 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 533 | ''' |
| 534 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 535 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 536 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 537 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 538 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 539 | else: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 540 | self.handle.sendline("links") |
| 541 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 542 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 543 | #print "handle =",handle |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 544 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 545 | except pexpect.EOF: |
| 546 | main.log.error(self.name + ": EOF exception found") |
| 547 | main.log.error(self.name + ": " + self.handle.before) |
| 548 | main.cleanup() |
| 549 | main.exit() |
| 550 | except: |
| 551 | main.log.info(self.name+" ::::::") |
| 552 | main.log.error( traceback.print_exc()) |
| 553 | main.log.info(self.name+" ::::::") |
| 554 | main.cleanup() |
| 555 | main.exit() |
| 556 | |
| 557 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 558 | def ports(self, json_format=True): |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 559 | ''' |
| 560 | Lists all ports |
| 561 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 562 | * json_format - boolean indicating if you want output in json |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 563 | ''' |
| 564 | try: |
| 565 | self.handle.sendline("") |
| 566 | self.handle.expect("onos>") |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 567 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 568 | if json_format: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 569 | self.handle.sendline("ports -j") |
| 570 | self.handle.expect("ports -j") |
| 571 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 572 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 573 | ''' |
| 574 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 575 | 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] | 576 | 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] | 577 | So we take off that escape sequence using the following commads: |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 578 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 579 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 580 | ''' |
| 581 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 582 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 583 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 584 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 585 | return handle1 |
| 586 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 587 | else: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 588 | self.handle.sendline("ports") |
| 589 | self.handle.expect("onos>") |
| 590 | self.handle.sendline("") |
| 591 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 592 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 593 | #print "handle =",handle |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 594 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 595 | except pexpect.EOF: |
| 596 | main.log.error(self.name + ": EOF exception found") |
| 597 | main.log.error(self.name + ": " + self.handle.before) |
| 598 | main.cleanup() |
| 599 | main.exit() |
| 600 | except: |
| 601 | main.log.info(self.name+" ::::::") |
| 602 | main.log.error( traceback.print_exc()) |
| 603 | main.log.info(self.name+" ::::::") |
| 604 | main.cleanup() |
| 605 | main.exit() |
| 606 | |
| 607 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 608 | def roles(self, json_format=True): |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 609 | ''' |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 610 | Lists all devices and the controllers with roles assigned to them |
| 611 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 612 | * json_format - boolean indicating if you want output in json |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 613 | ''' |
| 614 | try: |
| 615 | self.handle.sendline("") |
| 616 | self.handle.expect("onos>") |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 617 | |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 618 | if json_format: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 619 | self.handle.sendline("roles -j") |
| 620 | self.handle.expect("roles -j") |
| 621 | self.handle.expect("onos>") |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 622 | handle = self.handle.before |
| 623 | ''' |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 624 | handle variable here contains some ANSI escape color code sequences at the |
| 625 | end which are invisible in the print command output. To make that escape |
| 626 | sequence visible, use repr() function. The repr(handle) output when printed |
| 627 | shows the ANSI escape sequences. In json.loads(somestring), this somestring |
| 628 | variable is actually repr(somestring) and json.loads would fail with the escape |
| 629 | sequence. |
| 630 | |
| 631 | So we take off that escape sequence using the following commads: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 632 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 633 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 634 | ''' |
| 635 | #print "repr(handle) =", repr(handle) |
| 636 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 637 | handle1 = ansi_escape.sub('', handle) |
| 638 | #print "repr(handle1) = ", repr(handle1) |
| 639 | return handle1 |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 640 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 641 | else: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 642 | self.handle.sendline("roles") |
| 643 | self.handle.expect("onos>") |
| 644 | self.handle.sendline("") |
| 645 | self.handle.expect("onos>") |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 646 | handle = self.handle.before |
| 647 | #print "handle =",handle |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 648 | return handle |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 649 | except pexpect.EOF: |
| 650 | main.log.error(self.name + ": EOF exception found") |
| 651 | main.log.error(self.name + ": " + self.handle.before) |
| 652 | main.cleanup() |
| 653 | main.exit() |
| 654 | except: |
| 655 | main.log.info(self.name+" ::::::") |
| 656 | main.log.error( traceback.print_exc()) |
| 657 | main.log.info(self.name+" ::::::") |
| 658 | main.cleanup() |
| 659 | main.exit() |
| 660 | |
| 661 | def get_role(self, device_id): |
| 662 | ''' |
| 663 | Given the a string containing the json representation of the "roles" cli command and a |
| 664 | partial or whole device id, returns a json object containing the |
| 665 | roles output for the first device whose id contains "device_id" |
| 666 | |
| 667 | Returns: |
| 668 | Dict of the role assignments for the given device or |
| 669 | None if not match |
| 670 | ''' |
| 671 | try: |
| 672 | import json |
| 673 | if device_id == None: |
| 674 | return None |
| 675 | else: |
| 676 | raw_roles = self.roles() |
| 677 | roles_json = json.loads(raw_roles) |
| 678 | #search json for the device with id then return the device |
| 679 | for device in roles_json: |
Jon Hall | 720653a | 2014-10-28 19:02:37 -0400 | [diff] [blame] | 680 | #print device |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 681 | if str(device_id) in device['id']: |
| 682 | return device |
| 683 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 684 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 685 | except pexpect.EOF: |
| 686 | main.log.error(self.name + ": EOF exception found") |
| 687 | main.log.error(self.name + ": " + self.handle.before) |
| 688 | main.cleanup() |
| 689 | main.exit() |
| 690 | except: |
| 691 | main.log.info(self.name+" ::::::") |
| 692 | main.log.error( traceback.print_exc()) |
| 693 | main.log.info(self.name+" ::::::") |
| 694 | main.cleanup() |
| 695 | main.exit() |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 696 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 697 | def paths(self, src_id, dst_id): |
| 698 | ''' |
| 699 | Returns string of paths, and the cost. |
| 700 | Issues command: onos:paths <src> <dst> |
| 701 | ''' |
| 702 | try: |
| 703 | self.handle.sendline("") |
| 704 | self.handle.expect("onos>") |
| 705 | |
| 706 | self.handle.sendline("onos:paths "+ |
| 707 | str(src_id) + " " + str(dst_id)) |
| 708 | i = self.handle.expect([ |
| 709 | "Error", |
| 710 | "onos>"]) |
| 711 | |
| 712 | self.handle.sendline("") |
| 713 | self.handle.expect("onos>") |
| 714 | |
| 715 | handle = self.handle.before |
| 716 | |
| 717 | if i == 0: |
| 718 | main.log.error("Error in getting paths") |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 719 | return (handle, "Error") |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 720 | else: |
| 721 | path = handle.split(";")[0] |
| 722 | cost = handle.split(";")[1] |
| 723 | return (path, cost) |
| 724 | |
| 725 | except pexpect.EOF: |
| 726 | main.log.error(self.name + ": EOF exception found") |
| 727 | main.log.error(self.name + ": " + self.handle.before) |
| 728 | main.cleanup() |
| 729 | main.exit() |
| 730 | except: |
| 731 | main.log.info(self.name+" ::::::") |
| 732 | main.log.error( traceback.print_exc()) |
| 733 | main.log.info(self.name+" ::::::") |
| 734 | main.cleanup() |
| 735 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 736 | |
| 737 | def hosts(self, json_format=True): |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 738 | ''' |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 739 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 740 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 741 | * json_format - boolean indicating if you want output in json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 742 | ''' |
| 743 | try: |
| 744 | self.handle.sendline("") |
| 745 | self.handle.expect("onos>") |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 746 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 747 | if json_format: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 748 | self.handle.sendline("hosts -j") |
| 749 | self.handle.expect("hosts -j") |
| 750 | self.handle.expect("onos>") |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 751 | handle = self.handle.before |
| 752 | ''' |
| 753 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 754 | To make that escape sequence visible, use repr() function. The repr(handle) output when printed shows the ANSI escape sequences. |
| 755 | 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] | 756 | So we take off that escape sequence using |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 757 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 758 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 759 | ''' |
| 760 | #print "repr(handle) =", repr(handle) |
| 761 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 762 | handle1 = ansi_escape.sub('', handle) |
| 763 | #print "repr(handle1) = ", repr(handle1) |
| 764 | return handle1 |
| 765 | else: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 766 | self.handle.sendline("hosts") |
| 767 | self.handle.expect("onos>") |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 768 | handle = self.handle.before |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 769 | #print "handle =",handle |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 770 | return handle |
| 771 | except pexpect.EOF: |
| 772 | main.log.error(self.name + ": EOF exception found") |
| 773 | main.log.error(self.name + ": " + self.handle.before) |
| 774 | main.cleanup() |
| 775 | main.exit() |
| 776 | except: |
| 777 | main.log.info(self.name+" ::::::") |
| 778 | main.log.error( traceback.print_exc()) |
| 779 | main.log.info(self.name+" ::::::") |
| 780 | main.cleanup() |
| 781 | main.exit() |
| 782 | |
| 783 | def get_host(self, mac): |
| 784 | ''' |
| 785 | Return the first host from the hosts api whose 'id' contains 'mac' |
| 786 | Note: mac must be a colon seperated mac address, but could be a partial mac address |
| 787 | Return None if there is no match |
| 788 | ''' |
| 789 | import json |
| 790 | try: |
| 791 | if mac == None: |
| 792 | return None |
| 793 | else: |
| 794 | mac = mac |
| 795 | raw_hosts = self.hosts() |
| 796 | hosts_json = json.loads(raw_hosts) |
| 797 | #search json for the host with mac then return the device |
| 798 | for host in hosts_json: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 799 | #print "%s in %s?" % (mac, host['id']) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 800 | if mac in host['id']: |
| 801 | return host |
| 802 | return None |
| 803 | except pexpect.EOF: |
| 804 | main.log.error(self.name + ": EOF exception found") |
| 805 | main.log.error(self.name + ": " + self.handle.before) |
| 806 | main.cleanup() |
| 807 | main.exit() |
| 808 | except: |
| 809 | main.log.info(self.name+" ::::::") |
| 810 | main.log.error( traceback.print_exc()) |
| 811 | main.log.info(self.name+" ::::::") |
| 812 | main.cleanup() |
| 813 | main.exit() |
| 814 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 815 | |
| 816 | def get_hosts_id(self, host_list): |
| 817 | ''' |
| 818 | Obtain list of hosts |
| 819 | Issues command: 'onos> hosts' |
| 820 | |
| 821 | Required: |
| 822 | * host_list: List of hosts obtained by Mininet |
| 823 | IMPORTANT: |
| 824 | This function assumes that you started your |
| 825 | topology with the option '--mac'. |
| 826 | Furthermore, it assumes that value of VLAN is '-1' |
| 827 | Description: |
| 828 | Converts mininet hosts (h1, h2, h3...) into |
| 829 | ONOS format (00:00:00:00:00:01/-1 , ...) |
| 830 | ''' |
| 831 | |
| 832 | try: |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 833 | onos_host_list = [] |
| 834 | |
| 835 | for host in host_list: |
| 836 | host = host.replace("h", "") |
| 837 | host_hex = hex(int(host)).zfill(12) |
| 838 | host_hex = str(host_hex).replace('x','0') |
| 839 | i = iter(str(host_hex)) |
| 840 | host_hex = ":".join(a+b for a,b in zip(i,i)) |
| 841 | host_hex = host_hex + "/-1" |
| 842 | onos_host_list.append(host_hex) |
| 843 | |
| 844 | return onos_host_list |
| 845 | |
| 846 | except pexpect.EOF: |
| 847 | main.log.error(self.name + ": EOF exception found") |
| 848 | main.log.error(self.name + ": " + self.handle.before) |
| 849 | main.cleanup() |
| 850 | main.exit() |
| 851 | except: |
| 852 | main.log.info(self.name+" ::::::") |
| 853 | main.log.error( traceback.print_exc()) |
| 854 | main.log.info(self.name+" ::::::") |
| 855 | main.cleanup() |
| 856 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 857 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 858 | def add_host_intent(self, host_id_one, host_id_two): |
| 859 | ''' |
| 860 | Required: |
| 861 | * host_id_one: ONOS host id for host1 |
| 862 | * host_id_two: ONOS host id for host2 |
| 863 | Description: |
| 864 | Adds a host-to-host intent (bidrectional) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 865 | specifying the two hosts. |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 866 | ''' |
| 867 | try: |
| 868 | self.handle.sendline("") |
| 869 | self.handle.expect("onos>") |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 870 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 871 | self.handle.sendline("add-host-intent "+ |
| 872 | str(host_id_one) + " " + str(host_id_two)) |
| 873 | self.handle.expect("onos>") |
| 874 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 875 | handle = self.handle.before |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 876 | #print "handle =", handle |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 877 | |
| 878 | main.log.info("Intent installed between "+ |
| 879 | str(host_id_one) + " and " + str(host_id_two)) |
| 880 | |
| 881 | return handle |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 882 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 883 | except pexpect.EOF: |
| 884 | main.log.error(self.name + ": EOF exception found") |
| 885 | main.log.error(self.name + ": " + self.handle.before) |
| 886 | main.cleanup() |
| 887 | main.exit() |
| 888 | except: |
| 889 | main.log.info(self.name+" ::::::") |
| 890 | main.log.error( traceback.print_exc()) |
| 891 | main.log.info(self.name+" ::::::") |
| 892 | main.cleanup() |
| 893 | main.exit() |
| 894 | |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 895 | def add_optical_intent(self, ingress_device, egress_device): |
| 896 | ''' |
| 897 | Required: |
| 898 | * ingress_device: device id of ingress device |
| 899 | * egress_device: device id of egress device |
| 900 | Optional: |
| 901 | TODO: Still needs to be implemented via dev side |
| 902 | ''' |
| 903 | try: |
| 904 | self.handle.sendline("add-optical-intent "+ |
| 905 | str(ingress_device) + " " + str(egress_device)) |
| 906 | self.handle.expect("add-optical-intent") |
| 907 | i = self.handle.expect([ |
| 908 | "Error", |
| 909 | "onos>"]) |
| 910 | |
| 911 | handle = self.handle.before |
| 912 | |
| 913 | #If error, return error message |
| 914 | if i == 0: |
| 915 | return handle |
| 916 | else: |
| 917 | return main.TRUE |
| 918 | |
| 919 | except pexpect.EOF: |
| 920 | main.log.error(self.name + ": EOF exception found") |
| 921 | main.log.error(self.name + ": " + self.handle.before) |
| 922 | main.cleanup() |
| 923 | main.exit() |
| 924 | except: |
| 925 | main.log.info(self.name+" ::::::") |
| 926 | main.log.error( traceback.print_exc()) |
| 927 | main.log.info(self.name+" ::::::") |
| 928 | main.cleanup() |
| 929 | main.exit() |
| 930 | |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 931 | def add_point_intent(self, ingress_device, egress_device, |
| 932 | port_ingress="", port_egress="", ethType="", ethSrc="", |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 933 | ethDst="", bandwidth="", lambda_alloc=False, |
| 934 | ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst=""): |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 935 | ''' |
| 936 | Required: |
| 937 | * ingress_device: device id of ingress device |
| 938 | * egress_device: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 939 | Optional: |
| 940 | * ethType: specify ethType |
| 941 | * ethSrc: specify ethSrc (i.e. src mac addr) |
| 942 | * ethDst: specify ethDst (i.e. dst mac addr) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 943 | * bandwidth: specify bandwidth capacity of link |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 944 | * lambda_alloc: if True, intent will allocate lambda |
| 945 | for the specified intent |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 946 | * ipProto: specify ip protocol |
| 947 | * ipSrc: specify ip source address |
| 948 | * ipDst: specify ip destination address |
| 949 | * tcpSrc: specify tcp source port |
| 950 | * tcpDst: specify tcp destination port |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 951 | Description: |
| 952 | Adds a point-to-point intent (uni-directional) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 953 | specifying device id's and optional fields |
| 954 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 955 | NOTE: This function may change depending on the |
| 956 | options developers provide for point-to-point |
| 957 | intent via cli |
| 958 | ''' |
| 959 | try: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 960 | cmd = "" |
| 961 | |
| 962 | #If there are no optional arguments |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 963 | if not ethType and not ethSrc and not ethDst\ |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 964 | and not bandwidth and not lambda_alloc \ |
| 965 | and not ipProto and not ipSrc and not ipDst \ |
| 966 | and not tcpSrc and not tcpDst: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 967 | cmd = "add-point-intent" |
| 968 | |
| 969 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 970 | else: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 971 | cmd = "add-point-intent" |
andrewonlab | 9a130be | 2014-10-22 12:44:56 -0400 | [diff] [blame] | 972 | |
andrewonlab | 0c0a677 | 2014-10-22 12:31:18 -0400 | [diff] [blame] | 973 | if ethType: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 974 | cmd += " --ethType " + str(ethType) |
| 975 | if ethSrc: |
| 976 | cmd += " --ethSrc " + str(ethSrc) |
| 977 | if ethDst: |
| 978 | cmd += " --ethDst " + str(ethDst) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 979 | if bandwidth: |
| 980 | cmd += " --bandwidth " + str(bandwidth) |
| 981 | if lambda_alloc: |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 982 | cmd += " --lambda " |
| 983 | if ipProto: |
| 984 | cmd += " --ipProto " + str(ipProto) |
| 985 | if ipSrc: |
| 986 | cmd += " --ipSrc " + str(ipSrc) |
| 987 | if ipDst: |
| 988 | cmd += " --ipDst " + str(ipDst) |
| 989 | if tcpSrc: |
| 990 | cmd += " --tcpSrc " + str(tcpSrc) |
| 991 | if tcpDst: |
| 992 | cmd += " --tcpDst " + str(tcpDst) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 993 | |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 994 | #Check whether the user appended the port |
| 995 | #or provided it as an input |
| 996 | if "/" in ingress_device: |
| 997 | cmd += " "+str(ingress_device) |
| 998 | else: |
| 999 | if not port_ingress: |
| 1000 | main.log.error("You must specify "+ |
| 1001 | "the ingress port") |
| 1002 | #TODO: perhaps more meaningful return |
| 1003 | return main.FALSE |
| 1004 | |
| 1005 | cmd += " "+ \ |
| 1006 | str(ingress_device) + "/" +\ |
| 1007 | str(port_ingress) + " " |
| 1008 | |
| 1009 | if "/" in egress_device: |
| 1010 | cmd += " "+str(egress_device) |
| 1011 | else: |
| 1012 | if not port_egress: |
| 1013 | main.log.error("You must specify "+ |
| 1014 | "the egress port") |
| 1015 | return main.FALSE |
| 1016 | |
| 1017 | cmd += " "+\ |
| 1018 | str(egress_device) + "/" +\ |
| 1019 | str(port_egress) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1020 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1021 | self.handle.sendline(cmd) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1022 | |
| 1023 | main.log.info(cmd + " sent") |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1024 | i = self.handle.expect([ |
| 1025 | "Error", |
| 1026 | "onos>"]) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1027 | |
| 1028 | if i == 0: |
| 1029 | main.log.error("Error in adding point-to-point intent") |
| 1030 | return handle |
| 1031 | else: |
| 1032 | return main.TRUE |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1033 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1034 | except pexpect.EOF: |
| 1035 | main.log.error(self.name + ": EOF exception found") |
| 1036 | main.log.error(self.name + ": " + self.handle.before) |
| 1037 | main.cleanup() |
| 1038 | main.exit() |
| 1039 | except: |
| 1040 | main.log.info(self.name+" ::::::") |
| 1041 | main.log.error( traceback.print_exc()) |
| 1042 | main.log.info(self.name+" ::::::") |
| 1043 | main.cleanup() |
| 1044 | main.exit() |
| 1045 | |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1046 | |
| 1047 | def add_multipoint_to_singlepoint_intent(self, ingress_device1, ingress_device2,egress_device, |
| 1048 | port_ingress="", port_egress="", ethType="", ethSrc="", |
| 1049 | ethDst="", bandwidth="", lambda_alloc=False, |
| 1050 | ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst=""): |
| 1051 | ''' |
| 1052 | Note: |
| 1053 | This function assumes that there would be 2 ingress devices and one egress device |
| 1054 | For more number of ingress devices, this function needs to be modified |
| 1055 | Required: |
| 1056 | * ingress_device1: device id of ingress device1 |
| 1057 | * ingress_device2: device id of ingress device2 |
| 1058 | * egress_device: device id of egress device |
| 1059 | Optional: |
| 1060 | * ethType: specify ethType |
| 1061 | * ethSrc: specify ethSrc (i.e. src mac addr) |
| 1062 | * ethDst: specify ethDst (i.e. dst mac addr) |
| 1063 | * bandwidth: specify bandwidth capacity of link |
| 1064 | * lambda_alloc: if True, intent will allocate lambda |
| 1065 | for the specified intent |
| 1066 | * ipProto: specify ip protocol |
| 1067 | * ipSrc: specify ip source address |
| 1068 | * ipDst: specify ip destination address |
| 1069 | * tcpSrc: specify tcp source port |
| 1070 | * tcpDst: specify tcp destination port |
| 1071 | * setEthSrc: action to Rewrite Source MAC Address |
| 1072 | * setEthDst: action to Rewrite Destination MAC Address |
| 1073 | Description: |
| 1074 | Adds a multipoint-to-singlepoint intent (uni-directional) by |
| 1075 | specifying device id's and optional fields |
| 1076 | |
| 1077 | NOTE: This function may change depending on the |
| 1078 | options developers provide for multipointpoint-to-singlepoint |
| 1079 | intent via cli |
| 1080 | ''' |
| 1081 | try: |
| 1082 | cmd = "" |
| 1083 | |
| 1084 | #If there are no optional arguments |
| 1085 | if not ethType and not ethSrc and not ethDst\ |
| 1086 | and not bandwidth and not lambda_alloc \ |
| 1087 | and not ipProto and not ipSrc and not ipDst \ |
| 1088 | and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst: |
| 1089 | cmd = "add-multi-to-single-intent" |
| 1090 | |
| 1091 | |
| 1092 | else: |
| 1093 | cmd = "add-multi-to-single-intent" |
| 1094 | |
| 1095 | if ethType: |
| 1096 | cmd += " --ethType " + str(ethType) |
| 1097 | if ethSrc: |
| 1098 | cmd += " --ethSrc " + str(ethSrc) |
| 1099 | if ethDst: |
| 1100 | cmd += " --ethDst " + str(ethDst) |
| 1101 | if bandwidth: |
| 1102 | cmd += " --bandwidth " + str(bandwidth) |
| 1103 | if lambda_alloc: |
| 1104 | cmd += " --lambda " |
| 1105 | if ipProto: |
| 1106 | cmd += " --ipProto " + str(ipProto) |
| 1107 | if ipSrc: |
| 1108 | cmd += " --ipSrc " + str(ipSrc) |
| 1109 | if ipDst: |
| 1110 | cmd += " --ipDst " + str(ipDst) |
| 1111 | if tcpSrc: |
| 1112 | cmd += " --tcpSrc " + str(tcpSrc) |
| 1113 | if tcpDst: |
| 1114 | cmd += " --tcpDst " + str(tcpDst) |
| 1115 | if setEthSrc: |
| 1116 | cmd += " --setEthSrc "+ str(setEthSrc) |
| 1117 | if setEthDst: |
| 1118 | cmd += " --setEthDst "+ str(setEthDst) |
| 1119 | |
| 1120 | #Check whether the user appended the port |
| 1121 | #or provided it as an input |
| 1122 | if "/" in ingress_device1: |
| 1123 | cmd += " "+str(ingress_device1) |
| 1124 | else: |
| 1125 | if not port_ingress1: |
| 1126 | main.log.error("You must specify "+ |
| 1127 | "the ingress port1") |
| 1128 | #TODO: perhaps more meaningful return |
| 1129 | return main.FALSE |
| 1130 | |
| 1131 | cmd += " "+ \ |
| 1132 | str(ingress_device1) + "/" +\ |
| 1133 | str(port_ingress1) + " " |
| 1134 | |
| 1135 | if "/" in ingress_device2: |
| 1136 | cmd += " "+str(ingress_device2) |
| 1137 | else: |
| 1138 | if not port_ingress2: |
| 1139 | main.log.error("You must specify "+ |
| 1140 | "the ingress port2") |
| 1141 | #TODO: perhaps more meaningful return |
| 1142 | return main.FALSE |
| 1143 | |
| 1144 | cmd += " "+ \ |
| 1145 | str(ingress_device2) + "/" +\ |
| 1146 | str(port_ingress2) + " " |
| 1147 | |
| 1148 | if "/" in egress_device: |
| 1149 | cmd += " "+str(egress_device) |
| 1150 | else: |
| 1151 | if not port_egress: |
| 1152 | main.log.error("You must specify "+ |
| 1153 | "the egress port") |
| 1154 | return main.FALSE |
| 1155 | |
| 1156 | cmd += " "+\ |
| 1157 | str(egress_device) + "/" +\ |
| 1158 | str(port_egress) |
| 1159 | print "cmd= ",cmd |
| 1160 | self.handle.sendline(cmd) |
| 1161 | |
| 1162 | main.log.info(cmd + " sent") |
| 1163 | i = self.handle.expect([ |
| 1164 | "Error", |
| 1165 | "onos>"]) |
| 1166 | |
| 1167 | if i == 0: |
| 1168 | main.log.error("Error in adding point-to-point intent") |
| 1169 | return self.handle |
| 1170 | else: |
| 1171 | return main.TRUE |
| 1172 | |
| 1173 | except pexpect.EOF: |
| 1174 | main.log.error(self.name + ": EOF exception found") |
| 1175 | main.log.error(self.name + ": " + self.handle.before) |
| 1176 | main.cleanup() |
| 1177 | main.exit() |
| 1178 | except: |
| 1179 | main.log.info(self.name+" ::::::") |
| 1180 | main.log.error( traceback.print_exc()) |
| 1181 | main.log.info(self.name+" ::::::") |
| 1182 | main.cleanup() |
| 1183 | main.exit() |
| 1184 | |
| 1185 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1186 | def remove_intent(self, intent_id): |
| 1187 | ''' |
| 1188 | Remove intent for specified intent id |
| 1189 | ''' |
| 1190 | try: |
| 1191 | self.handle.sendline("") |
| 1192 | self.handle.expect("onos>") |
| 1193 | |
| 1194 | self.handle.sendline("remove-intent "+str(intent_id)) |
| 1195 | i = self.handle.expect([ |
| 1196 | "Error", |
| 1197 | "onos>"]) |
| 1198 | |
| 1199 | handle = self.handle.before |
| 1200 | |
| 1201 | if i == 0: |
| 1202 | main.log.error("Error in removing intent") |
| 1203 | return handle |
| 1204 | else: |
| 1205 | return handle |
| 1206 | |
| 1207 | except pexpect.EOF: |
| 1208 | main.log.error(self.name + ": EOF exception found") |
| 1209 | main.log.error(self.name + ": " + self.handle.before) |
| 1210 | main.cleanup() |
| 1211 | main.exit() |
| 1212 | except: |
| 1213 | main.log.info(self.name+" ::::::") |
| 1214 | main.log.error( traceback.print_exc()) |
| 1215 | main.log.info(self.name+" ::::::") |
| 1216 | main.cleanup() |
| 1217 | main.exit() |
| 1218 | |
pingping-lin | dabe797 | 2014-11-17 19:29:44 -0800 | [diff] [blame] | 1219 | # This method should be used after installing application: onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1220 | def routes(self, json_format=False): |
| 1221 | ''' |
| 1222 | Optional: |
| 1223 | * json_format: enable output formatting in json |
| 1224 | Description: |
| 1225 | Obtain all routes in the system |
| 1226 | ''' |
| 1227 | try: |
| 1228 | if json_format: |
| 1229 | self.handle.sendline("routes -j") |
| 1230 | self.handle.expect("routes -j") |
| 1231 | self.handle.expect("onos>") |
| 1232 | handle_tmp = self.handle.before |
| 1233 | |
| 1234 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1235 | handle = ansi_escape.sub('', handle_tmp) |
| 1236 | |
| 1237 | else: |
| 1238 | self.handle.sendline("") |
| 1239 | self.handle.expect("onos>") |
| 1240 | |
| 1241 | self.handle.sendline("routes") |
| 1242 | self.handle.expect("onos>") |
| 1243 | handle = self.handle.before |
| 1244 | |
| 1245 | return handle |
| 1246 | |
| 1247 | except pexpect.EOF: |
| 1248 | main.log.error(self.name + ": EOF exception found") |
| 1249 | main.log.error(self.name + ": " + self.handle.before) |
| 1250 | main.cleanup() |
| 1251 | main.exit() |
| 1252 | except: |
| 1253 | main.log.info(self.name + " ::::::") |
| 1254 | main.log.error(traceback.print_exc()) |
| 1255 | main.log.info(self.name + " ::::::") |
| 1256 | main.cleanup() |
| 1257 | main.exit() |
| 1258 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1259 | def intents(self, json_format = True): |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1260 | ''' |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1261 | Optional: |
| 1262 | * json_format: enable output formatting in json |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1263 | Description: |
| 1264 | Obtain intents currently installed |
| 1265 | ''' |
| 1266 | try: |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1267 | if json_format: |
| 1268 | self.handle.sendline("intents -j") |
| 1269 | self.handle.expect("intents -j") |
| 1270 | self.handle.expect("onos>") |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1271 | handle = self.handle.before |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1272 | |
| 1273 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1274 | handle = ansi_escape.sub('', handle) |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1275 | else: |
| 1276 | self.handle.sendline("") |
| 1277 | self.handle.expect("onos>") |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1278 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1279 | self.handle.sendline("intents") |
| 1280 | self.handle.expect("onos>") |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1281 | handle = self.handle.before |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1282 | |
| 1283 | return handle |
| 1284 | |
| 1285 | except pexpect.EOF: |
| 1286 | main.log.error(self.name + ": EOF exception found") |
| 1287 | main.log.error(self.name + ": " + self.handle.before) |
| 1288 | main.cleanup() |
| 1289 | main.exit() |
| 1290 | except: |
| 1291 | main.log.info(self.name+" ::::::") |
| 1292 | main.log.error( traceback.print_exc()) |
| 1293 | main.log.info(self.name+" ::::::") |
| 1294 | main.cleanup() |
| 1295 | main.exit() |
| 1296 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1297 | def flows(self, json_format = True): |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1298 | ''' |
| 1299 | Optional: |
| 1300 | * json_format: enable output formatting in json |
| 1301 | Description: |
| 1302 | Obtain flows currently installed |
| 1303 | ''' |
| 1304 | try: |
| 1305 | if json_format: |
| 1306 | self.handle.sendline("flows -j") |
| 1307 | self.handle.expect("flows -j") |
| 1308 | self.handle.expect("onos>") |
| 1309 | handle = self.handle.before |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1310 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1311 | handle = ansi_escape.sub('', handle) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1312 | |
| 1313 | else: |
| 1314 | self.handle.sendline("") |
| 1315 | self.handle.expect("onos>") |
| 1316 | self.handle.sendline("flows") |
| 1317 | self.handle.expect("onos>") |
| 1318 | handle = self.handle.before |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1319 | if re.search("Error\sexecuting\scommand:", handle): |
| 1320 | main.log.error(self.name + ".flows() response: " + str(handle)) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1321 | |
| 1322 | return handle |
| 1323 | |
| 1324 | except pexpect.EOF: |
| 1325 | main.log.error(self.name + ": EOF exception found") |
| 1326 | main.log.error(self.name + ": " + self.handle.before) |
| 1327 | main.cleanup() |
| 1328 | main.exit() |
| 1329 | except: |
| 1330 | main.log.info(self.name+" ::::::") |
| 1331 | main.log.error( traceback.print_exc()) |
| 1332 | main.log.info(self.name+" ::::::") |
| 1333 | main.cleanup() |
| 1334 | main.exit() |
| 1335 | |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1336 | def push_test_intents(self, dpid_src, dpid_dst, num_intents, |
| 1337 | num_mult="", app_id="", report=True): |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1338 | ''' |
| 1339 | Description: |
| 1340 | Push a number of intents in a batch format to |
| 1341 | a specific point-to-point intent definition |
| 1342 | Required: |
| 1343 | * dpid_src: specify source dpid |
| 1344 | * dpid_dst: specify destination dpid |
| 1345 | * num_intents: specify number of intents to push |
| 1346 | Optional: |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1347 | * num_mult: number multiplier for multiplying |
| 1348 | the number of intents specified |
| 1349 | * app_id: specify the application id init to further |
| 1350 | modularize the intents |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1351 | * report: default True, returns latency information |
| 1352 | ''' |
| 1353 | try: |
| 1354 | cmd = "push-test-intents "+\ |
| 1355 | str(dpid_src)+" "+str(dpid_dst)+" "+\ |
| 1356 | str(num_intents) |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1357 | |
| 1358 | if num_mult: |
| 1359 | cmd += " " + str(num_mult) |
| 1360 | if app_id: |
| 1361 | cmd += " " + str(app_id) |
| 1362 | |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1363 | self.handle.sendline(cmd) |
| 1364 | self.handle.expect(cmd) |
| 1365 | self.handle.expect("onos>") |
| 1366 | |
| 1367 | handle = self.handle.before |
| 1368 | |
| 1369 | #Some color thing that we want to escape |
| 1370 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1371 | handle = ansi_escape.sub('', handle) |
| 1372 | |
| 1373 | if report: |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1374 | lat_result = [] |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1375 | main.log.info(handle) |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1376 | #Split result by newline |
| 1377 | newline = handle.split("\r\r\n") |
| 1378 | #Ignore the first object of list, which is empty |
| 1379 | newline = newline[1:] |
| 1380 | #Some sloppy parsing method to get the latency |
| 1381 | for result in newline: |
| 1382 | result = result.split(": ") |
| 1383 | #Append the first result of second parse |
| 1384 | lat_result.append(result[1].split(" ")[0]) |
| 1385 | |
| 1386 | print lat_result |
| 1387 | return lat_result |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1388 | else: |
| 1389 | return main.TRUE |
| 1390 | |
| 1391 | except pexpect.EOF: |
| 1392 | main.log.error(self.name + ": EOF exception found") |
| 1393 | main.log.error(self.name + ": " + self.handle.before) |
| 1394 | main.cleanup() |
| 1395 | main.exit() |
| 1396 | except: |
| 1397 | main.log.info(self.name+" ::::::") |
| 1398 | main.log.error( traceback.print_exc()) |
| 1399 | main.log.info(self.name+" ::::::") |
| 1400 | main.cleanup() |
| 1401 | main.exit() |
| 1402 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1403 | def intents_events_metrics(self, json_format=True): |
| 1404 | ''' |
| 1405 | Description:Returns topology metrics |
| 1406 | Optional: |
| 1407 | * json_format: enable json formatting of output |
| 1408 | ''' |
| 1409 | try: |
| 1410 | if json_format: |
| 1411 | self.handle.sendline("intents-events-metrics -j") |
| 1412 | self.handle.expect("intents-events-metrics -j") |
| 1413 | self.handle.expect("onos>") |
| 1414 | |
| 1415 | handle = self.handle.before |
| 1416 | |
| 1417 | #Some color thing that we want to escape |
| 1418 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1419 | handle = ansi_escape.sub('', handle) |
| 1420 | |
| 1421 | else: |
| 1422 | self.handle.sendline("intents-events-metrics") |
| 1423 | self.handle.expect("intents-events-metrics") |
| 1424 | self.handle.expect("onos>") |
| 1425 | |
| 1426 | handle = self.handle.before |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1427 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1428 | return handle |
| 1429 | |
| 1430 | except pexpect.EOF: |
| 1431 | main.log.error(self.name + ": EOF exception found") |
| 1432 | main.log.error(self.name + ": " + self.handle.before) |
| 1433 | main.cleanup() |
| 1434 | main.exit() |
| 1435 | except: |
| 1436 | main.log.info(self.name+" ::::::") |
| 1437 | main.log.error( traceback.print_exc()) |
| 1438 | main.log.info(self.name+" ::::::") |
| 1439 | main.cleanup() |
| 1440 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1441 | |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1442 | def topology_events_metrics(self, json_format=True): |
| 1443 | ''' |
| 1444 | Description:Returns topology metrics |
| 1445 | Optional: |
| 1446 | * json_format: enable json formatting of output |
| 1447 | ''' |
| 1448 | try: |
| 1449 | if json_format: |
| 1450 | self.handle.sendline("topology-events-metrics -j") |
| 1451 | self.handle.expect("topology-events-metrics -j") |
| 1452 | self.handle.expect("onos>") |
| 1453 | |
| 1454 | handle = self.handle.before |
| 1455 | |
| 1456 | #Some color thing that we want to escape |
| 1457 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1458 | handle = ansi_escape.sub('', handle) |
| 1459 | |
| 1460 | else: |
| 1461 | self.handle.sendline("topology-events-metrics") |
| 1462 | self.handle.expect("topology-events-metrics") |
| 1463 | self.handle.expect("onos>") |
| 1464 | |
| 1465 | handle = self.handle.before |
| 1466 | |
| 1467 | return handle |
| 1468 | |
| 1469 | except pexpect.EOF: |
| 1470 | main.log.error(self.name + ": EOF exception found") |
| 1471 | main.log.error(self.name + ": " + self.handle.before) |
| 1472 | main.cleanup() |
| 1473 | main.exit() |
| 1474 | except: |
| 1475 | main.log.info(self.name+" ::::::") |
| 1476 | main.log.error( traceback.print_exc()) |
| 1477 | main.log.info(self.name+" ::::::") |
| 1478 | main.cleanup() |
| 1479 | main.exit() |
| 1480 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1481 | #Wrapper functions **************** |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1482 | #Wrapper functions use existing driver |
| 1483 | #functions and extends their use case. |
| 1484 | #For example, we may use the output of |
| 1485 | #a normal driver function, and parse it |
| 1486 | #using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 1487 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1488 | def get_all_intents_id(self): |
| 1489 | ''' |
| 1490 | Description: |
| 1491 | Obtain all intent id's in a list |
| 1492 | ''' |
| 1493 | try: |
| 1494 | #Obtain output of intents function |
| 1495 | intents_str = self.intents() |
| 1496 | all_intent_list = [] |
| 1497 | intent_id_list = [] |
| 1498 | |
| 1499 | #Parse the intents output for ID's |
| 1500 | intents_list = [s.strip() for s in intents_str.splitlines()] |
| 1501 | for intents in intents_list: |
| 1502 | if "onos>" in intents: |
| 1503 | continue |
| 1504 | elif "intents" in intents: |
| 1505 | continue |
| 1506 | else: |
| 1507 | line_list = intents.split(" ") |
| 1508 | all_intent_list.append(line_list[0]) |
| 1509 | |
| 1510 | all_intent_list = all_intent_list[1:-2] |
| 1511 | |
| 1512 | for intents in all_intent_list: |
| 1513 | if not intents: |
| 1514 | continue |
| 1515 | else: |
| 1516 | intent_id_list.append(intents) |
| 1517 | |
| 1518 | return intent_id_list |
| 1519 | |
| 1520 | except pexpect.EOF: |
| 1521 | main.log.error(self.name + ": EOF exception found") |
| 1522 | main.log.error(self.name + ": " + self.handle.before) |
| 1523 | main.cleanup() |
| 1524 | main.exit() |
| 1525 | except: |
| 1526 | main.log.info(self.name+" ::::::") |
| 1527 | main.log.error( traceback.print_exc()) |
| 1528 | main.log.info(self.name+" ::::::") |
| 1529 | main.cleanup() |
| 1530 | main.exit() |
| 1531 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1532 | def get_all_devices_id(self): |
| 1533 | ''' |
| 1534 | Use 'devices' function to obtain list of all devices |
| 1535 | and parse the result to obtain a list of all device |
| 1536 | id's. Returns this list. Returns empty list if no |
| 1537 | devices exist |
| 1538 | List is ordered sequentially |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1539 | |
| 1540 | This function may be useful if you are not sure of the |
| 1541 | device id, and wish to execute other commands using |
| 1542 | the ids. By obtaining the list of device ids on the fly, |
| 1543 | you can iterate through the list to get mastership, etc. |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1544 | ''' |
| 1545 | try: |
| 1546 | #Call devices and store result string |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1547 | devices_str = self.devices(json_format=False) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1548 | id_list = [] |
| 1549 | |
| 1550 | if not devices_str: |
| 1551 | main.log.info("There are no devices to get id from") |
| 1552 | return id_list |
| 1553 | |
| 1554 | #Split the string into list by comma |
| 1555 | device_list = devices_str.split(",") |
| 1556 | #Get temporary list of all arguments with string 'id=' |
| 1557 | temp_list = [dev for dev in device_list if "id=" in dev] |
| 1558 | #Split list further into arguments before and after string |
| 1559 | # 'id='. Get the latter portion (the actual device id) and |
| 1560 | # append to id_list |
| 1561 | for arg in temp_list: |
| 1562 | id_list.append(arg.split("id=")[1]) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1563 | return id_list |
| 1564 | |
| 1565 | except pexpect.EOF: |
| 1566 | main.log.error(self.name + ": EOF exception found") |
| 1567 | main.log.error(self.name + ": " + self.handle.before) |
| 1568 | main.cleanup() |
| 1569 | main.exit() |
| 1570 | except: |
| 1571 | main.log.info(self.name+" ::::::") |
| 1572 | main.log.error( traceback.print_exc()) |
| 1573 | main.log.info(self.name+" ::::::") |
| 1574 | main.cleanup() |
| 1575 | main.exit() |
| 1576 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1577 | def get_all_nodes_id(self): |
| 1578 | ''' |
| 1579 | Uses 'nodes' function to obtain list of all nodes |
| 1580 | and parse the result of nodes to obtain just the |
| 1581 | node id's. |
| 1582 | Returns: |
| 1583 | list of node id's |
| 1584 | ''' |
| 1585 | try: |
| 1586 | nodes_str = self.nodes() |
| 1587 | id_list = [] |
| 1588 | |
| 1589 | if not nodes_str: |
| 1590 | main.log.info("There are no nodes to get id from") |
| 1591 | return id_list |
| 1592 | |
| 1593 | #Sample nodes_str output |
| 1594 | #id=local, address=127.0.0.1:9876, state=ACTIVE * |
| 1595 | |
| 1596 | #Split the string into list by comma |
| 1597 | nodes_list = nodes_str.split(",") |
| 1598 | temp_list = [node for node in nodes_list if "id=" in node] |
| 1599 | for arg in temp_list: |
| 1600 | id_list.append(arg.split("id=")[1]) |
| 1601 | |
| 1602 | return id_list |
| 1603 | |
| 1604 | except pexpect.EOF: |
| 1605 | main.log.error(self.name + ": EOF exception found") |
| 1606 | main.log.error(self.name + ": " + self.handle.before) |
| 1607 | main.cleanup() |
| 1608 | main.exit() |
| 1609 | except: |
| 1610 | main.log.info(self.name+" ::::::") |
| 1611 | main.log.error( traceback.print_exc()) |
| 1612 | main.log.info(self.name+" ::::::") |
| 1613 | main.cleanup() |
| 1614 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1615 | |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1616 | def get_device(self, dpid=None): |
| 1617 | ''' |
| 1618 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 1619 | Return None if there is no match |
| 1620 | ''' |
| 1621 | import json |
| 1622 | try: |
| 1623 | if dpid == None: |
| 1624 | return None |
| 1625 | else: |
| 1626 | dpid = dpid.replace(':', '') |
| 1627 | raw_devices = self.devices() |
| 1628 | devices_json = json.loads(raw_devices) |
| 1629 | #search json for the device with dpid then return the device |
| 1630 | for device in devices_json: |
| 1631 | #print "%s in %s?" % (dpid, device['id']) |
| 1632 | if dpid in device['id']: |
| 1633 | return device |
| 1634 | return None |
| 1635 | except pexpect.EOF: |
| 1636 | main.log.error(self.name + ": EOF exception found") |
| 1637 | main.log.error(self.name + ": " + self.handle.before) |
| 1638 | main.cleanup() |
| 1639 | main.exit() |
| 1640 | except: |
| 1641 | main.log.info(self.name+" ::::::") |
| 1642 | main.log.error( traceback.print_exc()) |
| 1643 | main.log.info(self.name+" ::::::") |
| 1644 | main.cleanup() |
| 1645 | main.exit() |
| 1646 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1647 | def check_status(self, ip, numoswitch, numolink, log_level="info"): |
| 1648 | ''' |
| 1649 | Checks the number of swithes & links that ONOS sees against the |
| 1650 | supplied values. By default this will report to main.log, but the |
| 1651 | log level can be specifid. |
| 1652 | |
| 1653 | Params: ip = ip used for the onos cli |
| 1654 | numoswitch = expected number of switches |
| 1655 | numlink = expected number of links |
| 1656 | log_level = level to log to. Currently accepts 'info', 'warn' and 'report' |
| 1657 | |
| 1658 | |
| 1659 | log_level can |
| 1660 | |
| 1661 | Returns: main.TRUE if the number of switchs and links are correct, |
| 1662 | main.FALSE if the numer of switches and links is incorrect, |
| 1663 | and main.ERROR otherwise |
| 1664 | ''' |
| 1665 | |
| 1666 | try: |
| 1667 | topology = self.get_topology(ip) |
| 1668 | if topology == {}: |
| 1669 | return main.ERROR |
| 1670 | output = "" |
| 1671 | #Is the number of switches is what we expected |
| 1672 | devices = topology.get('devices',False) |
| 1673 | links = topology.get('links',False) |
| 1674 | if devices == False or links == False: |
| 1675 | return main.ERROR |
| 1676 | switch_check = ( int(devices) == int(numoswitch) ) |
| 1677 | #Is the number of links is what we expected |
| 1678 | link_check = ( int(links) == int(numolink) ) |
| 1679 | if (switch_check and link_check): |
| 1680 | #We expected the correct numbers |
| 1681 | output = output + "The number of links and switches match "\ |
| 1682 | + "what was expected" |
| 1683 | result = main.TRUE |
| 1684 | else: |
| 1685 | output = output + \ |
| 1686 | "The number of links and switches does not match what was expected" |
| 1687 | result = main.FALSE |
| 1688 | output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\ |
| 1689 | % ( int(devices), int(numoswitch), int(links), int(numolink) ) |
| 1690 | if log_level == "report": |
| 1691 | main.log.report(output) |
| 1692 | elif log_level == "warn": |
| 1693 | main.log.warn(output) |
| 1694 | else: |
| 1695 | main.log.info(output) |
| 1696 | return result |
| 1697 | except pexpect.EOF: |
| 1698 | main.log.error(self.name + ": EOF exception found") |
| 1699 | main.log.error(self.name + ": " + self.handle.before) |
| 1700 | main.cleanup() |
| 1701 | main.exit() |
| 1702 | except: |
| 1703 | main.log.info(self.name+" ::::::") |
| 1704 | main.log.error( traceback.print_exc()) |
| 1705 | main.log.info(self.name+" ::::::") |
| 1706 | main.cleanup() |
| 1707 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1708 | |
| 1709 | def device_role(self, device_id, onos_node, role="master"): |
| 1710 | ''' |
| 1711 | Calls the device-role cli command. |
| 1712 | device_id must be the id of a device as seen in the onos devices command |
| 1713 | onos_node is the ip of one of the onos nodes in the cluster |
| 1714 | role must be either master, standby, or none |
| 1715 | |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1716 | Returns main.TRUE or main.FALSE based argument varification. |
| 1717 | When device-role supports errors this should be extended to |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1718 | support that output |
| 1719 | ''' |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1720 | try: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1721 | #print "beginning device_role... \n\tdevice_id:" + device_id |
| 1722 | #print "\tonos_node: " + onos_node |
| 1723 | #print "\trole: "+ role |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1724 | if role.lower() == "master" or \ |
| 1725 | role.lower() == "standby" or \ |
| 1726 | role.lower() == "none": |
| 1727 | self.handle.sendline("") |
| 1728 | self.handle.expect("onos>") |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1729 | self.handle.sendline("device-role " + |
| 1730 | str(device_id) + " " + |
| 1731 | str(onos_node) + " " + |
| 1732 | str(role)) |
| 1733 | i= self.handle.expect(["Error","onos>"]) |
| 1734 | if i == 0: |
| 1735 | output = str(self.handle.before) |
| 1736 | self.handle.expect("onos>") |
| 1737 | output = output + str(self.handle.before) |
| 1738 | main.log.error(self.name + ": " + |
| 1739 | output + '\033[0m')#end color output to escape any colours from the cli |
| 1740 | return main.ERROR |
| 1741 | self.handle.sendline("") |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1742 | self.handle.expect("onos>") |
| 1743 | return main.TRUE |
| 1744 | else: |
| 1745 | return main.FALSE |
| 1746 | |
| 1747 | except pexpect.EOF: |
| 1748 | main.log.error(self.name + ": EOF exception found") |
| 1749 | main.log.error(self.name + ": " + self.handle.before) |
| 1750 | main.cleanup() |
| 1751 | main.exit() |
| 1752 | except: |
| 1753 | main.log.info(self.name+" ::::::") |
| 1754 | main.log.error( traceback.print_exc()) |
| 1755 | main.log.info(self.name+" ::::::") |
| 1756 | main.cleanup() |
| 1757 | main.exit() |
| 1758 | |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1759 | def clusters(self, json_format=True): |
| 1760 | ''' |
| 1761 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1762 | Optional argument: |
| 1763 | * json_format - boolean indicating if you want output in json |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1764 | ''' |
| 1765 | try: |
| 1766 | self.handle.sendline("") |
| 1767 | self.handle.expect("onos>") |
| 1768 | |
| 1769 | if json_format: |
| 1770 | self.handle.sendline("clusters -j") |
| 1771 | self.handle.expect("clusters -j") |
| 1772 | self.handle.expect("onos>") |
| 1773 | handle = self.handle.before |
| 1774 | ''' |
| 1775 | handle variable here contains some ANSI escape color code |
| 1776 | sequences at the end which are invisible in the print command |
| 1777 | output. To make that escape sequence visible, use repr() function. |
| 1778 | The repr(handle) output when printed shows the ANSI escape sequences. |
| 1779 | In json.loads(somestring), this somestring variable is actually |
| 1780 | repr(somestring) and json.loads would fail with the escape sequence. |
| 1781 | So we take off that escape sequence using |
| 1782 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1783 | handle1 = ansi_escape.sub('', handle) |
| 1784 | ''' |
| 1785 | #print "repr(handle) =", repr(handle) |
| 1786 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 1787 | handle1 = ansi_escape.sub('', handle) |
| 1788 | #print "repr(handle1) = ", repr(handle1) |
| 1789 | return handle1 |
| 1790 | else: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1791 | self.handle.sendline("clusters") |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1792 | self.handle.expect("onos>") |
| 1793 | handle = self.handle.before |
| 1794 | #print "handle =",handle |
| 1795 | return handle |
| 1796 | except pexpect.EOF: |
| 1797 | main.log.error(self.name + ": EOF exception found") |
| 1798 | main.log.error(self.name + ": " + self.handle.before) |
| 1799 | main.cleanup() |
| 1800 | main.exit() |
| 1801 | except: |
| 1802 | main.log.info(self.name+" ::::::") |
| 1803 | main.log.error( traceback.print_exc()) |
| 1804 | main.log.info(self.name+" ::::::") |
| 1805 | main.cleanup() |
| 1806 | main.exit() |
| 1807 | |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1808 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1809 | #*********************************** |