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 |
| 14 | |
| 15 | OCT 13 2014 |
| 16 | |
| 17 | ''' |
| 18 | |
| 19 | import sys |
| 20 | import time |
| 21 | import pexpect |
| 22 | import re |
| 23 | import traceback |
| 24 | import os.path |
| 25 | import pydoc |
| 26 | sys.path.append("../") |
| 27 | from drivers.common.clidriver import CLI |
| 28 | |
| 29 | class OnosCliDriver(CLI): |
| 30 | |
| 31 | def __init__(self): |
| 32 | ''' |
| 33 | Initialize client |
| 34 | ''' |
| 35 | super(CLI, self).__init__() |
| 36 | |
| 37 | def connect(self,**connectargs): |
| 38 | ''' |
| 39 | Creates ssh handle for ONOS cli. |
| 40 | ''' |
| 41 | try: |
| 42 | for key in connectargs: |
| 43 | vars(self)[key] = connectargs[key] |
| 44 | self.home = "~/ONOS" |
| 45 | for key in self.options: |
| 46 | if key == "home": |
| 47 | self.home = self.options['home'] |
| 48 | break |
| 49 | |
| 50 | |
| 51 | self.name = self.options['name'] |
| 52 | self.handle = super(OnosCliDriver,self).connect( |
| 53 | user_name = self.user_name, |
| 54 | ip_address = self.ip_address, |
| 55 | port = self.port, |
| 56 | pwd = self.pwd, |
| 57 | home = self.home) |
| 58 | |
| 59 | self.handle.sendline("cd "+ self.home) |
| 60 | self.handle.expect("\$") |
| 61 | if self.handle: |
| 62 | return self.handle |
| 63 | else : |
| 64 | main.log.info("NO ONOS HANDLE") |
| 65 | return main.FALSE |
| 66 | except pexpect.EOF: |
| 67 | main.log.error(self.name + ": EOF exception found") |
| 68 | main.log.error(self.name + ": " + self.handle.before) |
| 69 | main.cleanup() |
| 70 | main.exit() |
| 71 | except: |
| 72 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 73 | main.log.error( traceback.print_exc() ) |
| 74 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 75 | main.cleanup() |
| 76 | main.exit() |
| 77 | |
| 78 | def disconnect(self): |
| 79 | ''' |
| 80 | Called when Test is complete to disconnect the ONOS handle. |
| 81 | ''' |
| 82 | response = '' |
| 83 | try: |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 84 | self.handle.sendline("system:shutdown") |
| 85 | self.handle.expect("Confirm") |
| 86 | self.handle.sendline("Yes") |
| 87 | self.handle.expect("\$") |
| 88 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 89 | except pexpect.EOF: |
| 90 | main.log.error(self.name + ": EOF exception found") |
| 91 | main.log.error(self.name + ": " + self.handle.before) |
| 92 | except: |
| 93 | main.log.error(self.name + ": Connection failed to the host") |
| 94 | response = main.FALSE |
| 95 | return response |
| 96 | |
| 97 | def set_cell(self, cellname): |
| 98 | ''' |
| 99 | Calls 'cell <name>' to set the environment variables on ONOSbench |
| 100 | |
| 101 | Before issuing any cli commands, set the environment variable first. |
| 102 | ''' |
| 103 | try: |
| 104 | if not cellname: |
| 105 | main.log.error("Must define cellname") |
| 106 | main.cleanup() |
| 107 | main.exit() |
| 108 | else: |
| 109 | self.handle.sendline("cell "+str(cellname)) |
| 110 | #Expect the cellname in the ONOS_CELL variable. |
| 111 | #Note that this variable name is subject to change |
| 112 | # and that this driver will have to change accordingly |
| 113 | self.handle.expect("ONOS_CELL="+str(cellname)) |
| 114 | handle_before = self.handle.before |
| 115 | handle_after = self.handle.after |
| 116 | #Get the rest of the handle |
| 117 | self.handle.sendline("") |
| 118 | self.handle.expect("\$") |
| 119 | handle_more = self.handle.before |
| 120 | |
| 121 | main.log.info("Cell call returned: "+handle_before+ |
| 122 | handle_after + handle_more) |
| 123 | |
| 124 | return main.TRUE |
| 125 | |
| 126 | except pexpect.EOF: |
| 127 | main.log.error(self.name + ": EOF exception found") |
| 128 | main.log.error(self.name + ": " + self.handle.before) |
| 129 | main.cleanup() |
| 130 | main.exit() |
| 131 | except: |
| 132 | main.log.info(self.name+" ::::::") |
| 133 | main.log.error( traceback.print_exc()) |
| 134 | main.log.info(self.name+" ::::::") |
| 135 | main.cleanup() |
| 136 | main.exit() |
| 137 | |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 138 | def start_onos_cli(self, ONOS_ip): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 139 | try: |
| 140 | self.handle.sendline("") |
| 141 | self.handle.expect("\$") |
| 142 | |
| 143 | #Wait for onos start (-w) and enter onos cli |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 144 | self.handle.sendline("onos -w "+str(ONOS_ip)) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 145 | self.handle.expect("onos>") |
| 146 | |
| 147 | except pexpect.EOF: |
| 148 | main.log.error(self.name + ": EOF exception found") |
| 149 | main.log.error(self.name + ": " + self.handle.before) |
| 150 | main.cleanup() |
| 151 | main.exit() |
| 152 | except: |
| 153 | main.log.info(self.name+" ::::::") |
| 154 | main.log.error( traceback.print_exc()) |
| 155 | main.log.info(self.name+" ::::::") |
| 156 | main.cleanup() |
| 157 | main.exit() |
| 158 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 159 | def sendline(self, cmd_str): |
| 160 | ''' |
| 161 | Send a completely user specified string to |
| 162 | the onos> prompt. Use this function if you have |
| 163 | a very specific command to send. |
| 164 | |
| 165 | Warning: There are no sanity checking to commands |
| 166 | sent using this method. |
| 167 | ''' |
| 168 | try: |
| 169 | self.handle.sendline("") |
| 170 | self.handle.expect("onos>") |
| 171 | |
| 172 | self.handle.sendline(cmd_str) |
| 173 | self.handle.expect("onos>") |
| 174 | |
| 175 | handle = self.handle.before |
| 176 | |
| 177 | self.handle.sendline("") |
| 178 | self.handle.expect("onos>") |
| 179 | |
| 180 | handle += self.handle.before |
| 181 | handle += self.handle.after |
| 182 | |
| 183 | main.log.info("Command sent.") |
| 184 | |
| 185 | return handle |
| 186 | except pexpect.EOF: |
| 187 | main.log.error(self.name + ": EOF exception found") |
| 188 | main.log.error(self.name + ": " + self.handle.before) |
| 189 | main.cleanup() |
| 190 | main.exit() |
| 191 | except: |
| 192 | main.log.info(self.name+" ::::::") |
| 193 | main.log.error( traceback.print_exc()) |
| 194 | main.log.info(self.name+" ::::::") |
| 195 | main.cleanup() |
| 196 | main.exit() |
| 197 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 198 | #IMPORTANT NOTE: |
| 199 | #For all cli commands, naming convention should match |
| 200 | #the cli command replacing ':' with '_'. |
| 201 | #Ex) onos:topology > onos_topology |
| 202 | # onos:links > onos_links |
| 203 | # feature:list > feature_list |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 204 | |
| 205 | def add_node(self, node_id, ONOS_ip, tcp_port=""): |
| 206 | ''' |
| 207 | Adds a new cluster node by ID and address information. |
| 208 | Required: |
| 209 | * node_id |
| 210 | * ONOS_ip |
| 211 | Optional: |
| 212 | * tcp_port |
| 213 | ''' |
| 214 | try: |
| 215 | self.handle.sendline("") |
| 216 | self.handle.expect("onos>") |
| 217 | |
| 218 | self.handle.sendline("add-node "+ |
| 219 | str(node_id)+" "+ |
| 220 | str(ONOS_ip)+" "+ |
| 221 | str(tcp_port)) |
| 222 | |
| 223 | i = self.handle.expect([ |
| 224 | "Error", |
| 225 | "onos>" ]) |
| 226 | |
| 227 | #Clear handle to get previous output |
| 228 | self.handle.sendline("") |
| 229 | self.handle.expect("onos>") |
| 230 | |
| 231 | handle = self.handle.before |
| 232 | |
| 233 | if i == 0: |
| 234 | main.log.error("Error in adding node") |
| 235 | main.log.error(handle) |
| 236 | return main.FALSE |
| 237 | else: |
| 238 | main.log.info("Node "+str(ONOS_ip)+" added") |
| 239 | return main.TRUE |
| 240 | |
| 241 | except pexpect.EOF: |
| 242 | main.log.error(self.name + ": EOF exception found") |
| 243 | main.log.error(self.name + ": " + self.handle.before) |
| 244 | main.cleanup() |
| 245 | main.exit() |
| 246 | except: |
| 247 | main.log.info(self.name+" ::::::") |
| 248 | main.log.error( traceback.print_exc()) |
| 249 | main.log.info(self.name+" ::::::") |
| 250 | main.cleanup() |
| 251 | main.exit() |
| 252 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 253 | def remove_node(self, node_id): |
| 254 | ''' |
| 255 | Removes a cluster by ID |
| 256 | Issues command: 'remove-node [<node-id>]' |
| 257 | Required: |
| 258 | * node_id |
| 259 | ''' |
| 260 | try: |
| 261 | self.handle.sendline("") |
| 262 | self.handle.expect("onos>") |
| 263 | |
| 264 | self.handle.sendline("remove-node "+str(node_id)) |
| 265 | self.handle.expect("onos>") |
| 266 | |
| 267 | return main.TRUE |
| 268 | |
| 269 | except pexpect.EOF: |
| 270 | main.log.error(self.name + ": EOF exception found") |
| 271 | main.log.error(self.name + ": " + self.handle.before) |
| 272 | main.cleanup() |
| 273 | main.exit() |
| 274 | except: |
| 275 | main.log.info(self.name+" ::::::") |
| 276 | main.log.error( traceback.print_exc()) |
| 277 | main.log.info(self.name+" ::::::") |
| 278 | main.cleanup() |
| 279 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 280 | |
| 281 | def onos_topology(self): |
| 282 | ''' |
| 283 | Shows the current state of the topology |
| 284 | by issusing command: 'onos> onos:topology' |
| 285 | ''' |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 286 | try: |
| 287 | self.handle.sendline("") |
| 288 | self.handle.expect("onos>") |
| 289 | self.handle.sendline("onos:topology") |
| 290 | self.handle.expect("onos>") |
| 291 | |
| 292 | handle = self.handle.before |
| 293 | |
| 294 | main.log.info("onos:topology returned: " + |
| 295 | str(handle)) |
| 296 | |
| 297 | return handle |
| 298 | |
| 299 | except pexpect.EOF: |
| 300 | main.log.error(self.name + ": EOF exception found") |
| 301 | main.log.error(self.name + ": " + self.handle.before) |
| 302 | main.cleanup() |
| 303 | main.exit() |
| 304 | except: |
| 305 | main.log.info(self.name+" ::::::") |
| 306 | main.log.error( traceback.print_exc()) |
| 307 | main.log.info(self.name+" ::::::") |
| 308 | main.cleanup() |
| 309 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 310 | |
| 311 | def feature_install(self, feature_str): |
| 312 | ''' |
| 313 | Installs a specified feature |
| 314 | by issuing command: 'onos> feature:install <feature_str>' |
| 315 | ''' |
| 316 | try: |
| 317 | self.handle.sendline("") |
| 318 | self.handle.expect("onos>") |
| 319 | |
| 320 | self.handle.sendline("feature:install "+str(feature_str)) |
| 321 | self.handle.expect("onos>") |
| 322 | |
| 323 | return main.TRUE |
| 324 | |
| 325 | except pexpect.EOF: |
| 326 | main.log.error(self.name + ": EOF exception found") |
| 327 | main.log.error(self.name + ": " + self.handle.before) |
| 328 | main.cleanup() |
| 329 | main.exit() |
| 330 | except: |
| 331 | main.log.info(self.name+" ::::::") |
| 332 | main.log.error( traceback.print_exc()) |
| 333 | main.log.info(self.name+" ::::::") |
| 334 | main.cleanup() |
| 335 | main.exit() |
| 336 | |
| 337 | def feature_uninstall(self, feature_str): |
| 338 | ''' |
| 339 | Uninstalls a specified feature |
| 340 | by issuing command: 'onos> feature:uninstall <feature_str>' |
| 341 | ''' |
| 342 | try: |
| 343 | self.handle.sendline("") |
| 344 | self.handle.expect("onos>") |
| 345 | |
| 346 | self.handle.sendline("feature:uninstall "+str(feature_str)) |
| 347 | self.handle.expect("onos>") |
| 348 | |
| 349 | return main.TRUE |
| 350 | |
| 351 | except pexpect.EOF: |
| 352 | main.log.error(self.name + ": EOF exception found") |
| 353 | main.log.error(self.name + ": " + self.handle.before) |
| 354 | main.cleanup() |
| 355 | main.exit() |
| 356 | except: |
| 357 | main.log.info(self.name+" ::::::") |
| 358 | main.log.error( traceback.print_exc()) |
| 359 | main.log.info(self.name+" ::::::") |
| 360 | main.cleanup() |
| 361 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 362 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 363 | def devices(self, grep_str=""): |
| 364 | ''' |
| 365 | Lists all infrastructure devices |
| 366 | Optional argument: |
| 367 | * grep_str - pass in a string to grep |
| 368 | ''' |
| 369 | try: |
| 370 | self.handle.sendline("") |
| 371 | self.handle.expect("onos>") |
| 372 | |
| 373 | if not grep_str: |
| 374 | self.handle.sendline("devices") |
| 375 | self.handle.expect("onos>") |
| 376 | else: |
| 377 | self.handle.sendline("devices | grep '"+ |
| 378 | str(grep_str)+"'") |
| 379 | self.handle.expect("onos>") |
| 380 | |
| 381 | handle = self.handle.before |
| 382 | handle += self.handle.after |
| 383 | |
| 384 | self.handle.sendline("") |
| 385 | self.handle.expect("onos>") |
| 386 | |
| 387 | handle += self.handle.before |
| 388 | handle += self.handle.after |
| 389 | |
| 390 | return handle |
| 391 | except pexpect.EOF: |
| 392 | main.log.error(self.name + ": EOF exception found") |
| 393 | main.log.error(self.name + ": " + self.handle.before) |
| 394 | main.cleanup() |
| 395 | main.exit() |
| 396 | except: |
| 397 | main.log.info(self.name+" ::::::") |
| 398 | main.log.error( traceback.print_exc()) |
| 399 | main.log.info(self.name+" ::::::") |
| 400 | main.cleanup() |
| 401 | main.exit() |
| 402 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame^] | 403 | #Wrapper function for devices******* |
| 404 | #Wrapper functions use existing driver |
| 405 | #functions and extends their use case. |
| 406 | #For example, we may use the output of |
| 407 | #a normal driver function, and parse it |
| 408 | #using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 409 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame^] | 410 | def get_all_devices_id(self): |
| 411 | ''' |
| 412 | Use 'devices' function to obtain list of all devices |
| 413 | and parse the result to obtain a list of all device |
| 414 | id's. Returns this list. Returns empty list if no |
| 415 | devices exist |
| 416 | List is ordered sequentially |
| 417 | ''' |
| 418 | try: |
| 419 | #Call devices and store result string |
| 420 | devices_str = self.devices() |
| 421 | id_list = [] |
| 422 | |
| 423 | if not devices_str: |
| 424 | main.log.info("There are no devices to get id from") |
| 425 | return id_list |
| 426 | |
| 427 | #Split the string into list by comma |
| 428 | device_list = devices_str.split(",") |
| 429 | #Get temporary list of all arguments with string 'id=' |
| 430 | temp_list = [dev for dev in device_list if "id=" in dev] |
| 431 | #Split list further into arguments before and after string |
| 432 | # 'id='. Get the latter portion (the actual device id) and |
| 433 | # append to id_list |
| 434 | for arg in temp_list: |
| 435 | id_list.append(arg.split("id=")[1]) |
| 436 | |
| 437 | return id_list |
| 438 | |
| 439 | except pexpect.EOF: |
| 440 | main.log.error(self.name + ": EOF exception found") |
| 441 | main.log.error(self.name + ": " + self.handle.before) |
| 442 | main.cleanup() |
| 443 | main.exit() |
| 444 | except: |
| 445 | main.log.info(self.name+" ::::::") |
| 446 | main.log.error( traceback.print_exc()) |
| 447 | main.log.info(self.name+" ::::::") |
| 448 | main.cleanup() |
| 449 | main.exit() |
| 450 | |
| 451 | |
| 452 | #*********************************** |