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: |
| 74 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 75 | main.log.error( traceback.print_exc() ) |
| 76 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 77 | main.cleanup() |
| 78 | main.exit() |
| 79 | |
| 80 | def disconnect(self): |
| 81 | ''' |
| 82 | Called when Test is complete to disconnect the ONOS handle. |
| 83 | ''' |
| 84 | response = '' |
| 85 | try: |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 86 | self.handle.sendline("") |
Jon Hall | 7e5b917 | 2014-10-22 12:32:47 -0400 | [diff] [blame] | 87 | i = self.handle.expect(["onos>","\$"]) |
| 88 | if i == 0: |
| 89 | self.handle.sendline("system:shutdown") |
| 90 | self.handle.expect("Confirm") |
| 91 | self.handle.sendline("yes") |
| 92 | self.handle.expect("\$") |
| 93 | self.handle.sendline("\n") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 94 | self.handle.expect("\$") |
Jon Hall | 7e5b917 | 2014-10-22 12:32:47 -0400 | [diff] [blame] | 95 | self.handle.sendline("exit") |
| 96 | self.handle.expect("closed") |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 97 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 98 | except pexpect.EOF: |
| 99 | main.log.error(self.name + ": EOF exception found") |
| 100 | main.log.error(self.name + ": " + self.handle.before) |
| 101 | except: |
| 102 | main.log.error(self.name + ": Connection failed to the host") |
| 103 | response = main.FALSE |
| 104 | return response |
| 105 | |
| 106 | def set_cell(self, cellname): |
| 107 | ''' |
| 108 | Calls 'cell <name>' to set the environment variables on ONOSbench |
| 109 | |
| 110 | Before issuing any cli commands, set the environment variable first. |
| 111 | ''' |
| 112 | try: |
| 113 | if not cellname: |
| 114 | main.log.error("Must define cellname") |
| 115 | main.cleanup() |
| 116 | main.exit() |
| 117 | else: |
| 118 | self.handle.sendline("cell "+str(cellname)) |
| 119 | #Expect the cellname in the ONOS_CELL variable. |
| 120 | #Note that this variable name is subject to change |
| 121 | # and that this driver will have to change accordingly |
| 122 | self.handle.expect("ONOS_CELL="+str(cellname)) |
| 123 | handle_before = self.handle.before |
| 124 | handle_after = self.handle.after |
| 125 | #Get the rest of the handle |
| 126 | self.handle.sendline("") |
| 127 | self.handle.expect("\$") |
| 128 | handle_more = self.handle.before |
| 129 | |
| 130 | main.log.info("Cell call returned: "+handle_before+ |
| 131 | handle_after + handle_more) |
| 132 | |
| 133 | return main.TRUE |
| 134 | |
| 135 | except pexpect.EOF: |
| 136 | main.log.error(self.name + ": EOF exception found") |
| 137 | main.log.error(self.name + ": " + self.handle.before) |
| 138 | main.cleanup() |
| 139 | main.exit() |
| 140 | except: |
| 141 | main.log.info(self.name+" ::::::") |
| 142 | main.log.error( traceback.print_exc()) |
| 143 | main.log.info(self.name+" ::::::") |
| 144 | main.cleanup() |
| 145 | main.exit() |
| 146 | |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 147 | def start_onos_cli(self, ONOS_ip): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 148 | try: |
| 149 | self.handle.sendline("") |
| 150 | self.handle.expect("\$") |
| 151 | |
| 152 | #Wait for onos start (-w) and enter onos cli |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 153 | self.handle.sendline("onos -w "+str(ONOS_ip)) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 154 | self.handle.expect("onos>", timeout = 60) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 155 | |
| 156 | except pexpect.EOF: |
| 157 | main.log.error(self.name + ": EOF exception found") |
| 158 | main.log.error(self.name + ": " + self.handle.before) |
| 159 | main.cleanup() |
| 160 | main.exit() |
| 161 | except: |
| 162 | main.log.info(self.name+" ::::::") |
| 163 | main.log.error( traceback.print_exc()) |
| 164 | main.log.info(self.name+" ::::::") |
| 165 | main.cleanup() |
| 166 | main.exit() |
| 167 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 168 | def sendline(self, cmd_str): |
| 169 | ''' |
| 170 | Send a completely user specified string to |
| 171 | the onos> prompt. Use this function if you have |
| 172 | a very specific command to send. |
| 173 | |
| 174 | Warning: There are no sanity checking to commands |
| 175 | sent using this method. |
| 176 | ''' |
| 177 | try: |
| 178 | self.handle.sendline("") |
| 179 | self.handle.expect("onos>") |
| 180 | |
| 181 | self.handle.sendline(cmd_str) |
| 182 | self.handle.expect("onos>") |
| 183 | |
| 184 | handle = self.handle.before |
| 185 | |
| 186 | self.handle.sendline("") |
| 187 | self.handle.expect("onos>") |
| 188 | |
| 189 | handle += self.handle.before |
| 190 | handle += self.handle.after |
| 191 | |
| 192 | main.log.info("Command sent.") |
| 193 | |
| 194 | return handle |
| 195 | except pexpect.EOF: |
| 196 | main.log.error(self.name + ": EOF exception found") |
| 197 | main.log.error(self.name + ": " + self.handle.before) |
| 198 | main.cleanup() |
| 199 | main.exit() |
| 200 | except: |
| 201 | main.log.info(self.name+" ::::::") |
| 202 | main.log.error( traceback.print_exc()) |
| 203 | main.log.info(self.name+" ::::::") |
| 204 | main.cleanup() |
| 205 | main.exit() |
| 206 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 207 | #IMPORTANT NOTE: |
| 208 | #For all cli commands, naming convention should match |
| 209 | #the cli command replacing ':' with '_'. |
| 210 | #Ex) onos:topology > onos_topology |
| 211 | # onos:links > onos_links |
| 212 | # feature:list > feature_list |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 213 | |
| 214 | def add_node(self, node_id, ONOS_ip, tcp_port=""): |
| 215 | ''' |
| 216 | Adds a new cluster node by ID and address information. |
| 217 | Required: |
| 218 | * node_id |
| 219 | * ONOS_ip |
| 220 | Optional: |
| 221 | * tcp_port |
| 222 | ''' |
| 223 | try: |
| 224 | self.handle.sendline("") |
| 225 | self.handle.expect("onos>") |
| 226 | |
| 227 | self.handle.sendline("add-node "+ |
| 228 | str(node_id)+" "+ |
| 229 | str(ONOS_ip)+" "+ |
| 230 | str(tcp_port)) |
| 231 | |
| 232 | i = self.handle.expect([ |
| 233 | "Error", |
| 234 | "onos>" ]) |
| 235 | |
| 236 | #Clear handle to get previous output |
| 237 | self.handle.sendline("") |
| 238 | self.handle.expect("onos>") |
| 239 | |
| 240 | handle = self.handle.before |
| 241 | |
| 242 | if i == 0: |
| 243 | main.log.error("Error in adding node") |
| 244 | main.log.error(handle) |
| 245 | return main.FALSE |
| 246 | else: |
| 247 | main.log.info("Node "+str(ONOS_ip)+" added") |
| 248 | return main.TRUE |
| 249 | |
| 250 | except pexpect.EOF: |
| 251 | main.log.error(self.name + ": EOF exception found") |
| 252 | main.log.error(self.name + ": " + self.handle.before) |
| 253 | main.cleanup() |
| 254 | main.exit() |
| 255 | except: |
| 256 | main.log.info(self.name+" ::::::") |
| 257 | main.log.error( traceback.print_exc()) |
| 258 | main.log.info(self.name+" ::::::") |
| 259 | main.cleanup() |
| 260 | main.exit() |
| 261 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 262 | def remove_node(self, node_id): |
| 263 | ''' |
| 264 | Removes a cluster by ID |
| 265 | Issues command: 'remove-node [<node-id>]' |
| 266 | Required: |
| 267 | * node_id |
| 268 | ''' |
| 269 | try: |
| 270 | self.handle.sendline("") |
| 271 | self.handle.expect("onos>") |
| 272 | |
| 273 | self.handle.sendline("remove-node "+str(node_id)) |
| 274 | self.handle.expect("onos>") |
| 275 | |
| 276 | return main.TRUE |
| 277 | |
| 278 | except pexpect.EOF: |
| 279 | main.log.error(self.name + ": EOF exception found") |
| 280 | main.log.error(self.name + ": " + self.handle.before) |
| 281 | main.cleanup() |
| 282 | main.exit() |
| 283 | except: |
| 284 | main.log.info(self.name+" ::::::") |
| 285 | main.log.error( traceback.print_exc()) |
| 286 | main.log.info(self.name+" ::::::") |
| 287 | main.cleanup() |
| 288 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 289 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 290 | def nodes(self): |
| 291 | ''' |
| 292 | List the nodes currently visible |
| 293 | Issues command: 'nodes' |
| 294 | Returns: entire handle of list of nodes |
| 295 | ''' |
| 296 | try: |
| 297 | self.handle.sendline("") |
| 298 | self.handle.expect("onos>") |
| 299 | |
| 300 | self.handle.sendline("nodes") |
| 301 | self.handle.expect("onos>") |
| 302 | |
| 303 | self.handle.sendline("") |
| 304 | self.handle.expect("onos>") |
| 305 | |
| 306 | handle = self.handle.before |
| 307 | |
| 308 | return handle |
| 309 | |
| 310 | except pexpect.EOF: |
| 311 | main.log.error(self.name + ": EOF exception found") |
| 312 | main.log.error(self.name + ": " + self.handle.before) |
| 313 | main.cleanup() |
| 314 | main.exit() |
| 315 | except: |
| 316 | main.log.info(self.name+" ::::::") |
| 317 | main.log.error( traceback.print_exc()) |
| 318 | main.log.info(self.name+" ::::::") |
| 319 | main.cleanup() |
| 320 | main.exit() |
| 321 | |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 322 | def topology(self): |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 323 | ''' |
| 324 | Shows the current state of the topology |
| 325 | by issusing command: 'onos> onos:topology' |
| 326 | ''' |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 327 | try: |
| 328 | self.handle.sendline("") |
| 329 | self.handle.expect("onos>") |
andrewonlab | 38d6ae2 | 2014-10-15 14:23:45 -0400 | [diff] [blame] | 330 | #either onos:topology or 'topology' will work in CLI |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 331 | self.handle.sendline("onos:topology") |
| 332 | self.handle.expect("onos>") |
| 333 | |
| 334 | handle = self.handle.before |
| 335 | |
| 336 | main.log.info("onos:topology returned: " + |
| 337 | str(handle)) |
| 338 | |
| 339 | return handle |
| 340 | |
| 341 | except pexpect.EOF: |
| 342 | main.log.error(self.name + ": EOF exception found") |
| 343 | main.log.error(self.name + ": " + self.handle.before) |
| 344 | main.cleanup() |
| 345 | main.exit() |
| 346 | except: |
| 347 | main.log.info(self.name+" ::::::") |
| 348 | main.log.error( traceback.print_exc()) |
| 349 | main.log.info(self.name+" ::::::") |
| 350 | main.cleanup() |
| 351 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 352 | |
| 353 | def feature_install(self, feature_str): |
| 354 | ''' |
| 355 | Installs a specified feature |
| 356 | by issuing command: 'onos> feature:install <feature_str>' |
| 357 | ''' |
| 358 | try: |
| 359 | self.handle.sendline("") |
| 360 | self.handle.expect("onos>") |
| 361 | |
| 362 | self.handle.sendline("feature:install "+str(feature_str)) |
| 363 | self.handle.expect("onos>") |
| 364 | |
| 365 | return main.TRUE |
| 366 | |
| 367 | except pexpect.EOF: |
| 368 | main.log.error(self.name + ": EOF exception found") |
| 369 | main.log.error(self.name + ": " + self.handle.before) |
| 370 | main.cleanup() |
| 371 | main.exit() |
| 372 | except: |
| 373 | main.log.info(self.name+" ::::::") |
| 374 | main.log.error( traceback.print_exc()) |
| 375 | main.log.info(self.name+" ::::::") |
| 376 | main.cleanup() |
| 377 | main.exit() |
| 378 | |
| 379 | def feature_uninstall(self, feature_str): |
| 380 | ''' |
| 381 | Uninstalls a specified feature |
| 382 | by issuing command: 'onos> feature:uninstall <feature_str>' |
| 383 | ''' |
| 384 | try: |
| 385 | self.handle.sendline("") |
| 386 | self.handle.expect("onos>") |
| 387 | |
| 388 | self.handle.sendline("feature:uninstall "+str(feature_str)) |
| 389 | self.handle.expect("onos>") |
| 390 | |
| 391 | return main.TRUE |
| 392 | |
| 393 | except pexpect.EOF: |
| 394 | main.log.error(self.name + ": EOF exception found") |
| 395 | main.log.error(self.name + ": " + self.handle.before) |
| 396 | main.cleanup() |
| 397 | main.exit() |
| 398 | except: |
| 399 | main.log.info(self.name+" ::::::") |
| 400 | main.log.error( traceback.print_exc()) |
| 401 | main.log.info(self.name+" ::::::") |
| 402 | main.cleanup() |
| 403 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 404 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 405 | def devices(self, json_format=True, grep_str=""): |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 406 | ''' |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 407 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 408 | Optional argument: |
| 409 | * grep_str - pass in a string to grep |
| 410 | ''' |
| 411 | try: |
| 412 | self.handle.sendline("") |
| 413 | self.handle.expect("onos>") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 414 | |
| 415 | if json_format: |
| 416 | if not grep_str: |
| 417 | self.handle.sendline("devices -j") |
| 418 | self.handle.expect("devices -j") |
| 419 | self.handle.expect("onos>") |
| 420 | else: |
| 421 | self.handle.sendline("devices -j | grep '"+ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 422 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 423 | self.handle.expect("devices -j | grep '"+str(grep_str)+"'") |
| 424 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 425 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 426 | ''' |
| 427 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 428 | 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] | 429 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 430 | So we take off that escape sequence using |
| 431 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 432 | handle1 = ansi_escape.sub('', handle) |
| 433 | ''' |
| 434 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 435 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 436 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 437 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 438 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 439 | else: |
| 440 | if not grep_str: |
| 441 | self.handle.sendline("devices") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 442 | self.handle.expect("onos>") |
| 443 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 444 | self.handle.expect("onos>") |
| 445 | else: |
| 446 | self.handle.sendline("devices | grep '"+ |
| 447 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 448 | self.handle.expect("onos>") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 449 | self.handle.sendline("") |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 450 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 451 | handle = self.handle.before |
| 452 | print "handle =",handle |
| 453 | return handle |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 454 | except pexpect.EOF: |
| 455 | main.log.error(self.name + ": EOF exception found") |
| 456 | main.log.error(self.name + ": " + self.handle.before) |
| 457 | main.cleanup() |
| 458 | main.exit() |
| 459 | except: |
| 460 | main.log.info(self.name+" ::::::") |
| 461 | main.log.error( traceback.print_exc()) |
| 462 | main.log.info(self.name+" ::::::") |
| 463 | main.cleanup() |
| 464 | main.exit() |
| 465 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 466 | def links(self, json_format=True, grep_str=""): |
| 467 | ''' |
| 468 | Lists all core links |
| 469 | Optional argument: |
| 470 | * grep_str - pass in a string to grep |
| 471 | ''' |
| 472 | try: |
| 473 | self.handle.sendline("") |
| 474 | self.handle.expect("onos>") |
| 475 | |
| 476 | if json_format: |
| 477 | if not grep_str: |
| 478 | self.handle.sendline("links -j") |
| 479 | self.handle.expect("links -j") |
| 480 | self.handle.expect("onos>") |
| 481 | else: |
| 482 | self.handle.sendline("links -j | grep '"+ |
| 483 | str(grep_str)+"'") |
| 484 | self.handle.expect("links -j | grep '"+str(grep_str)+"'") |
| 485 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 486 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 487 | ''' |
| 488 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 489 | 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] | 490 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 491 | So we take off that escape sequence using |
| 492 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 493 | handle1 = ansi_escape.sub('', handle) |
| 494 | ''' |
| 495 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 496 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 497 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 498 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 499 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 500 | else: |
| 501 | if not grep_str: |
| 502 | self.handle.sendline("links") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 503 | self.handle.expect("onos>") |
| 504 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 505 | self.handle.expect("onos>") |
| 506 | else: |
| 507 | self.handle.sendline("links | grep '"+ |
| 508 | str(grep_str)+"'") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 509 | self.handle.expect("onos>") |
| 510 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 511 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 512 | handle = self.handle.before |
| 513 | print "handle =",handle |
| 514 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 515 | except pexpect.EOF: |
| 516 | main.log.error(self.name + ": EOF exception found") |
| 517 | main.log.error(self.name + ": " + self.handle.before) |
| 518 | main.cleanup() |
| 519 | main.exit() |
| 520 | except: |
| 521 | main.log.info(self.name+" ::::::") |
| 522 | main.log.error( traceback.print_exc()) |
| 523 | main.log.info(self.name+" ::::::") |
| 524 | main.cleanup() |
| 525 | main.exit() |
| 526 | |
| 527 | |
| 528 | def ports(self, json_format=True, grep_str=""): |
| 529 | ''' |
| 530 | Lists all ports |
| 531 | Optional argument: |
| 532 | * grep_str - pass in a string to grep |
| 533 | ''' |
| 534 | try: |
| 535 | self.handle.sendline("") |
| 536 | self.handle.expect("onos>") |
| 537 | |
| 538 | if json_format: |
| 539 | if not grep_str: |
| 540 | self.handle.sendline("ports -j") |
| 541 | self.handle.expect("ports -j") |
| 542 | self.handle.expect("onos>") |
| 543 | else: |
| 544 | self.handle.sendline("ports -j | grep '"+ |
| 545 | str(grep_str)+"'") |
| 546 | self.handle.expect("ports -j | grep '"+str(grep_str)+"'") |
| 547 | self.handle.expect("onos>") |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 548 | handle = self.handle.before |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 549 | ''' |
| 550 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
| 551 | 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] | 552 | In json.loads(somestring), this somestring variable is actually repr(somestring) and json.loads would fail with the escape sequence. |
Shreya Shah | 0c525cc | 2014-10-17 20:20:24 -0400 | [diff] [blame] | 553 | So we take off that escape sequence using the following commads: |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 554 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 555 | handle1 = ansi_escape.sub('', handle) |
| 556 | ''' |
| 557 | #print "repr(handle) =", repr(handle) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 558 | ansi_escape = re.compile(r'\r\r\n\x1b[^m]*m') |
| 559 | handle1 = ansi_escape.sub('', handle) |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 560 | #print "repr(handle1) = ", repr(handle1) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 561 | return handle1 |
| 562 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 563 | else: |
| 564 | if not grep_str: |
| 565 | self.handle.sendline("ports") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 566 | self.handle.expect("onos>") |
| 567 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 568 | self.handle.expect("onos>") |
| 569 | else: |
| 570 | self.handle.sendline("ports | grep '"+ |
| 571 | str(grep_str)+"'") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 572 | self.handle.expect("onos>") |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 573 | self.handle.sendline("") |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 574 | self.handle.expect("onos>") |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 575 | handle = self.handle.before |
| 576 | print "handle =",handle |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 577 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 578 | except pexpect.EOF: |
| 579 | main.log.error(self.name + ": EOF exception found") |
| 580 | main.log.error(self.name + ": " + self.handle.before) |
| 581 | main.cleanup() |
| 582 | main.exit() |
| 583 | except: |
| 584 | main.log.info(self.name+" ::::::") |
| 585 | main.log.error( traceback.print_exc()) |
| 586 | main.log.info(self.name+" ::::::") |
| 587 | main.cleanup() |
| 588 | main.exit() |
| 589 | |
| 590 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 591 | def device_role(self, device_id, node_id, role): |
| 592 | ''' |
| 593 | Set device role for specified device and node with role |
| 594 | Required: |
| 595 | * device_id : may be obtained by function get_all_devices_id |
| 596 | * node_id : may be obtained by function get_all_nodes_id |
| 597 | * role: specify one of the following roles: |
| 598 | - master |
| 599 | - standby |
| 600 | - none |
| 601 | ''' |
| 602 | try: |
| 603 | self.handle.sendline("") |
| 604 | self.handle.expect("onos>") |
| 605 | |
| 606 | self.handle.sendline("device-role "+ |
| 607 | str(device_id) + " " + |
| 608 | str(node_id) + " " + |
| 609 | str(role)) |
| 610 | i = self.handle.expect([ |
| 611 | "Error", |
| 612 | "onos>"]) |
| 613 | |
| 614 | self.handle.sendline("") |
| 615 | self.handle.expect("onos>") |
| 616 | |
| 617 | handle = self.handle.before |
| 618 | |
| 619 | if i == 0: |
| 620 | main.log.error("device-role command returned error") |
| 621 | return handle |
| 622 | else: |
| 623 | return main.TRUE |
| 624 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 625 | except pexpect.EOF: |
| 626 | main.log.error(self.name + ": EOF exception found") |
| 627 | main.log.error(self.name + ": " + self.handle.before) |
| 628 | main.cleanup() |
| 629 | main.exit() |
| 630 | except: |
| 631 | main.log.info(self.name+" ::::::") |
| 632 | main.log.error( traceback.print_exc()) |
| 633 | main.log.info(self.name+" ::::::") |
| 634 | main.cleanup() |
| 635 | main.exit() |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 636 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 637 | def paths(self, src_id, dst_id): |
| 638 | ''' |
| 639 | Returns string of paths, and the cost. |
| 640 | Issues command: onos:paths <src> <dst> |
| 641 | ''' |
| 642 | try: |
| 643 | self.handle.sendline("") |
| 644 | self.handle.expect("onos>") |
| 645 | |
| 646 | self.handle.sendline("onos:paths "+ |
| 647 | str(src_id) + " " + str(dst_id)) |
| 648 | i = self.handle.expect([ |
| 649 | "Error", |
| 650 | "onos>"]) |
| 651 | |
| 652 | self.handle.sendline("") |
| 653 | self.handle.expect("onos>") |
| 654 | |
| 655 | handle = self.handle.before |
| 656 | |
| 657 | if i == 0: |
| 658 | main.log.error("Error in getting paths") |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 659 | return (handle, "Error") |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 660 | else: |
| 661 | path = handle.split(";")[0] |
| 662 | cost = handle.split(";")[1] |
| 663 | return (path, cost) |
| 664 | |
| 665 | except pexpect.EOF: |
| 666 | main.log.error(self.name + ": EOF exception found") |
| 667 | main.log.error(self.name + ": " + self.handle.before) |
| 668 | main.cleanup() |
| 669 | main.exit() |
| 670 | except: |
| 671 | main.log.info(self.name+" ::::::") |
| 672 | main.log.error( traceback.print_exc()) |
| 673 | main.log.info(self.name+" ::::::") |
| 674 | main.cleanup() |
| 675 | main.exit() |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 676 | |
| 677 | #TODO: |
| 678 | #def hosts(self): |
| 679 | |
| 680 | def get_hosts_id(self, host_list): |
| 681 | ''' |
| 682 | Obtain list of hosts |
| 683 | Issues command: 'onos> hosts' |
| 684 | |
| 685 | Required: |
| 686 | * host_list: List of hosts obtained by Mininet |
| 687 | IMPORTANT: |
| 688 | This function assumes that you started your |
| 689 | topology with the option '--mac'. |
| 690 | Furthermore, it assumes that value of VLAN is '-1' |
| 691 | Description: |
| 692 | Converts mininet hosts (h1, h2, h3...) into |
| 693 | ONOS format (00:00:00:00:00:01/-1 , ...) |
| 694 | ''' |
| 695 | |
| 696 | try: |
Shreya Shah | d7310c5 | 2014-10-20 16:44:37 -0400 | [diff] [blame] | 697 | #self.handle.sendline("") |
| 698 | #self.handle.expect("onos>") |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 699 | |
| 700 | onos_host_list = [] |
| 701 | |
| 702 | for host in host_list: |
| 703 | host = host.replace("h", "") |
| 704 | host_hex = hex(int(host)).zfill(12) |
| 705 | host_hex = str(host_hex).replace('x','0') |
| 706 | i = iter(str(host_hex)) |
| 707 | host_hex = ":".join(a+b for a,b in zip(i,i)) |
| 708 | host_hex = host_hex + "/-1" |
| 709 | onos_host_list.append(host_hex) |
| 710 | |
| 711 | return onos_host_list |
| 712 | |
| 713 | except pexpect.EOF: |
| 714 | main.log.error(self.name + ": EOF exception found") |
| 715 | main.log.error(self.name + ": " + self.handle.before) |
| 716 | main.cleanup() |
| 717 | main.exit() |
| 718 | except: |
| 719 | main.log.info(self.name+" ::::::") |
| 720 | main.log.error( traceback.print_exc()) |
| 721 | main.log.info(self.name+" ::::::") |
| 722 | main.cleanup() |
| 723 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 724 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 725 | def add_host_intent(self, host_id_one, host_id_two): |
| 726 | ''' |
| 727 | Required: |
| 728 | * host_id_one: ONOS host id for host1 |
| 729 | * host_id_two: ONOS host id for host2 |
| 730 | Description: |
| 731 | Adds a host-to-host intent (bidrectional) by |
| 732 | specifying the two hosts. |
| 733 | ''' |
| 734 | try: |
| 735 | self.handle.sendline("") |
| 736 | self.handle.expect("onos>") |
| 737 | |
| 738 | self.handle.sendline("add-host-intent "+ |
| 739 | str(host_id_one) + " " + str(host_id_two)) |
| 740 | self.handle.expect("onos>") |
| 741 | |
| 742 | self.handle.sendline("") |
| 743 | self.handle.expect("onos>") |
| 744 | |
| 745 | handle = self.handle.before |
| 746 | |
| 747 | main.log.info("Intent installed between "+ |
| 748 | str(host_id_one) + " and " + str(host_id_two)) |
| 749 | |
| 750 | return handle |
| 751 | |
| 752 | except pexpect.EOF: |
| 753 | main.log.error(self.name + ": EOF exception found") |
| 754 | main.log.error(self.name + ": " + self.handle.before) |
| 755 | main.cleanup() |
| 756 | main.exit() |
| 757 | except: |
| 758 | main.log.info(self.name+" ::::::") |
| 759 | main.log.error( traceback.print_exc()) |
| 760 | main.log.info(self.name+" ::::::") |
| 761 | main.cleanup() |
| 762 | main.exit() |
| 763 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 764 | def add_point_intent(self, ingress_device, port_ingress, |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 765 | egress_device, port_egress, ethType="", ethSrc="", |
| 766 | ethDst=""): |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 767 | ''' |
| 768 | Required: |
| 769 | * ingress_device: device id of ingress device |
| 770 | * egress_device: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 771 | Optional: |
| 772 | * ethType: specify ethType |
| 773 | * ethSrc: specify ethSrc (i.e. src mac addr) |
| 774 | * ethDst: specify ethDst (i.e. dst mac addr) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 775 | Description: |
| 776 | Adds a point-to-point intent (uni-directional) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 777 | specifying device id's and optional fields |
| 778 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 779 | NOTE: This function may change depending on the |
| 780 | options developers provide for point-to-point |
| 781 | intent via cli |
| 782 | ''' |
| 783 | try: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 784 | cmd = "" |
| 785 | |
| 786 | #If there are no optional arguments |
| 787 | if not ethType and not ethSrc and not ethDst: |
| 788 | cmd = "add-point-intent "+\ |
| 789 | str(ingress_device) + "/" + str(port_ingress) + " " +\ |
| 790 | str(egress_device) + "/" + str(port_egress) |
| 791 | |
| 792 | else: |
| 793 | cmd = "add-point-intent "+\ |
| 794 | str(ingress_device) + "/" + str(port_ingress) + " " +\ |
| 795 | str(egress_device) + "/" + str(port_egress) |
andrewonlab | 0c0a677 | 2014-10-22 12:31:18 -0400 | [diff] [blame] | 796 | if ethType: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 797 | cmd += " --ethType " + str(ethType) |
| 798 | if ethSrc: |
| 799 | cmd += " --ethSrc " + str(ethSrc) |
| 800 | if ethDst: |
| 801 | cmd += " --ethDst " + str(ethDst) |
| 802 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 803 | self.handle.sendline("") |
| 804 | self.handle.expect("onos>") |
| 805 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 806 | self.handle.sendline(cmd) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 807 | i = self.handle.expect([ |
| 808 | "Error", |
| 809 | "onos>"]) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 810 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 811 | self.handle.sendline("") |
| 812 | self.handle.expect("onos>") |
| 813 | |
| 814 | handle = self.handle.before |
| 815 | |
| 816 | if i == 0: |
| 817 | main.log.error("Error in adding point-to-point intent") |
| 818 | return handle |
| 819 | else: |
| 820 | return main.TRUE |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 821 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 822 | except pexpect.EOF: |
| 823 | main.log.error(self.name + ": EOF exception found") |
| 824 | main.log.error(self.name + ": " + self.handle.before) |
| 825 | main.cleanup() |
| 826 | main.exit() |
| 827 | except: |
| 828 | main.log.info(self.name+" ::::::") |
| 829 | main.log.error( traceback.print_exc()) |
| 830 | main.log.info(self.name+" ::::::") |
| 831 | main.cleanup() |
| 832 | main.exit() |
| 833 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 834 | def remove_intent(self, intent_id): |
| 835 | ''' |
| 836 | Remove intent for specified intent id |
| 837 | ''' |
| 838 | try: |
| 839 | self.handle.sendline("") |
| 840 | self.handle.expect("onos>") |
| 841 | |
| 842 | self.handle.sendline("remove-intent "+str(intent_id)) |
| 843 | i = self.handle.expect([ |
| 844 | "Error", |
| 845 | "onos>"]) |
| 846 | |
| 847 | handle = self.handle.before |
| 848 | |
| 849 | if i == 0: |
| 850 | main.log.error("Error in removing intent") |
| 851 | return handle |
| 852 | else: |
| 853 | return handle |
| 854 | |
| 855 | except pexpect.EOF: |
| 856 | main.log.error(self.name + ": EOF exception found") |
| 857 | main.log.error(self.name + ": " + self.handle.before) |
| 858 | main.cleanup() |
| 859 | main.exit() |
| 860 | except: |
| 861 | main.log.info(self.name+" ::::::") |
| 862 | main.log.error( traceback.print_exc()) |
| 863 | main.log.info(self.name+" ::::::") |
| 864 | main.cleanup() |
| 865 | main.exit() |
| 866 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 867 | def intents(self, json_format = False): |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 868 | ''' |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 869 | Optional: |
| 870 | * json_format: enable output formatting in json |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 871 | Description: |
| 872 | Obtain intents currently installed |
| 873 | ''' |
| 874 | try: |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 875 | if json_format: |
| 876 | self.handle.sendline("intents -j") |
| 877 | self.handle.expect("intents -j") |
| 878 | self.handle.expect("onos>") |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 879 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 880 | handle = self.handle.before |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 881 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 882 | else: |
| 883 | self.handle.sendline("") |
| 884 | self.handle.expect("onos>") |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 885 | |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 886 | self.handle.sendline("intents") |
| 887 | self.handle.expect("onos>") |
| 888 | |
| 889 | self.handle.sendline("") |
| 890 | self.handle.expect("onos>") |
| 891 | |
| 892 | handle = self.handle.before |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 893 | |
| 894 | return handle |
| 895 | |
| 896 | except pexpect.EOF: |
| 897 | main.log.error(self.name + ": EOF exception found") |
| 898 | main.log.error(self.name + ": " + self.handle.before) |
| 899 | main.cleanup() |
| 900 | main.exit() |
| 901 | except: |
| 902 | main.log.info(self.name+" ::::::") |
| 903 | main.log.error( traceback.print_exc()) |
| 904 | main.log.info(self.name+" ::::::") |
| 905 | main.cleanup() |
| 906 | main.exit() |
| 907 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 908 | #Wrapper functions **************** |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 909 | #Wrapper functions use existing driver |
| 910 | #functions and extends their use case. |
| 911 | #For example, we may use the output of |
| 912 | #a normal driver function, and parse it |
| 913 | #using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 914 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 915 | def get_all_intents_id(self): |
| 916 | ''' |
| 917 | Description: |
| 918 | Obtain all intent id's in a list |
| 919 | ''' |
| 920 | try: |
| 921 | #Obtain output of intents function |
| 922 | intents_str = self.intents() |
| 923 | all_intent_list = [] |
| 924 | intent_id_list = [] |
| 925 | |
| 926 | #Parse the intents output for ID's |
| 927 | intents_list = [s.strip() for s in intents_str.splitlines()] |
| 928 | for intents in intents_list: |
| 929 | if "onos>" in intents: |
| 930 | continue |
| 931 | elif "intents" in intents: |
| 932 | continue |
| 933 | else: |
| 934 | line_list = intents.split(" ") |
| 935 | all_intent_list.append(line_list[0]) |
| 936 | |
| 937 | all_intent_list = all_intent_list[1:-2] |
| 938 | |
| 939 | for intents in all_intent_list: |
| 940 | if not intents: |
| 941 | continue |
| 942 | else: |
| 943 | intent_id_list.append(intents) |
| 944 | |
| 945 | return intent_id_list |
| 946 | |
| 947 | except pexpect.EOF: |
| 948 | main.log.error(self.name + ": EOF exception found") |
| 949 | main.log.error(self.name + ": " + self.handle.before) |
| 950 | main.cleanup() |
| 951 | main.exit() |
| 952 | except: |
| 953 | main.log.info(self.name+" ::::::") |
| 954 | main.log.error( traceback.print_exc()) |
| 955 | main.log.info(self.name+" ::::::") |
| 956 | main.cleanup() |
| 957 | main.exit() |
| 958 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 959 | def get_all_devices_id(self): |
| 960 | ''' |
| 961 | Use 'devices' function to obtain list of all devices |
| 962 | and parse the result to obtain a list of all device |
| 963 | id's. Returns this list. Returns empty list if no |
| 964 | devices exist |
| 965 | List is ordered sequentially |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 966 | |
| 967 | This function may be useful if you are not sure of the |
| 968 | device id, and wish to execute other commands using |
| 969 | the ids. By obtaining the list of device ids on the fly, |
| 970 | you can iterate through the list to get mastership, etc. |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 971 | ''' |
| 972 | try: |
| 973 | #Call devices and store result string |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 974 | devices_str = self.devices(json_format=False) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 975 | id_list = [] |
| 976 | |
| 977 | if not devices_str: |
| 978 | main.log.info("There are no devices to get id from") |
| 979 | return id_list |
| 980 | |
| 981 | #Split the string into list by comma |
| 982 | device_list = devices_str.split(",") |
| 983 | #Get temporary list of all arguments with string 'id=' |
| 984 | temp_list = [dev for dev in device_list if "id=" in dev] |
| 985 | #Split list further into arguments before and after string |
| 986 | # 'id='. Get the latter portion (the actual device id) and |
| 987 | # append to id_list |
| 988 | for arg in temp_list: |
| 989 | id_list.append(arg.split("id=")[1]) |
| 990 | |
| 991 | return id_list |
| 992 | |
| 993 | except pexpect.EOF: |
| 994 | main.log.error(self.name + ": EOF exception found") |
| 995 | main.log.error(self.name + ": " + self.handle.before) |
| 996 | main.cleanup() |
| 997 | main.exit() |
| 998 | except: |
| 999 | main.log.info(self.name+" ::::::") |
| 1000 | main.log.error( traceback.print_exc()) |
| 1001 | main.log.info(self.name+" ::::::") |
| 1002 | main.cleanup() |
| 1003 | main.exit() |
| 1004 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1005 | def get_all_nodes_id(self): |
| 1006 | ''' |
| 1007 | Uses 'nodes' function to obtain list of all nodes |
| 1008 | and parse the result of nodes to obtain just the |
| 1009 | node id's. |
| 1010 | Returns: |
| 1011 | list of node id's |
| 1012 | ''' |
| 1013 | try: |
| 1014 | nodes_str = self.nodes() |
| 1015 | id_list = [] |
| 1016 | |
| 1017 | if not nodes_str: |
| 1018 | main.log.info("There are no nodes to get id from") |
| 1019 | return id_list |
| 1020 | |
| 1021 | #Sample nodes_str output |
| 1022 | #id=local, address=127.0.0.1:9876, state=ACTIVE * |
| 1023 | |
| 1024 | #Split the string into list by comma |
| 1025 | nodes_list = nodes_str.split(",") |
| 1026 | temp_list = [node for node in nodes_list if "id=" in node] |
| 1027 | for arg in temp_list: |
| 1028 | id_list.append(arg.split("id=")[1]) |
| 1029 | |
| 1030 | return id_list |
| 1031 | |
| 1032 | except pexpect.EOF: |
| 1033 | main.log.error(self.name + ": EOF exception found") |
| 1034 | main.log.error(self.name + ": " + self.handle.before) |
| 1035 | main.cleanup() |
| 1036 | main.exit() |
| 1037 | except: |
| 1038 | main.log.info(self.name+" ::::::") |
| 1039 | main.log.error( traceback.print_exc()) |
| 1040 | main.log.info(self.name+" ::::::") |
| 1041 | main.cleanup() |
| 1042 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1043 | |
| 1044 | #*********************************** |