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