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