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