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 | |
| 159 | #IMPORTANT NOTE: |
| 160 | #For all cli commands, naming convention should match |
| 161 | #the cli command replacing ':' with '_'. |
| 162 | #Ex) onos:topology > onos_topology |
| 163 | # onos:links > onos_links |
| 164 | # feature:list > feature_list |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame^] | 165 | |
| 166 | def add_node(self, node_id, ONOS_ip, tcp_port=""): |
| 167 | ''' |
| 168 | Adds a new cluster node by ID and address information. |
| 169 | Required: |
| 170 | * node_id |
| 171 | * ONOS_ip |
| 172 | Optional: |
| 173 | * tcp_port |
| 174 | ''' |
| 175 | try: |
| 176 | self.handle.sendline("") |
| 177 | self.handle.expect("onos>") |
| 178 | |
| 179 | self.handle.sendline("add-node "+ |
| 180 | str(node_id)+" "+ |
| 181 | str(ONOS_ip)+" "+ |
| 182 | str(tcp_port)) |
| 183 | |
| 184 | i = self.handle.expect([ |
| 185 | "Error", |
| 186 | "onos>" ]) |
| 187 | |
| 188 | #Clear handle to get previous output |
| 189 | self.handle.sendline("") |
| 190 | self.handle.expect("onos>") |
| 191 | |
| 192 | handle = self.handle.before |
| 193 | |
| 194 | if i == 0: |
| 195 | main.log.error("Error in adding node") |
| 196 | main.log.error(handle) |
| 197 | return main.FALSE |
| 198 | else: |
| 199 | main.log.info("Node "+str(ONOS_ip)+" added") |
| 200 | return main.TRUE |
| 201 | |
| 202 | except pexpect.EOF: |
| 203 | main.log.error(self.name + ": EOF exception found") |
| 204 | main.log.error(self.name + ": " + self.handle.before) |
| 205 | main.cleanup() |
| 206 | main.exit() |
| 207 | except: |
| 208 | main.log.info(self.name+" ::::::") |
| 209 | main.log.error( traceback.print_exc()) |
| 210 | main.log.info(self.name+" ::::::") |
| 211 | main.cleanup() |
| 212 | main.exit() |
| 213 | |
| 214 | |
| 215 | def onos_topology(self): |
| 216 | ''' |
| 217 | Shows the current state of the topology |
| 218 | by issusing command: 'onos> onos:topology' |
| 219 | ''' |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 220 | try: |
| 221 | self.handle.sendline("") |
| 222 | self.handle.expect("onos>") |
| 223 | self.handle.sendline("onos:topology") |
| 224 | self.handle.expect("onos>") |
| 225 | |
| 226 | handle = self.handle.before |
| 227 | |
| 228 | main.log.info("onos:topology returned: " + |
| 229 | str(handle)) |
| 230 | |
| 231 | return handle |
| 232 | |
| 233 | except pexpect.EOF: |
| 234 | main.log.error(self.name + ": EOF exception found") |
| 235 | main.log.error(self.name + ": " + self.handle.before) |
| 236 | main.cleanup() |
| 237 | main.exit() |
| 238 | except: |
| 239 | main.log.info(self.name+" ::::::") |
| 240 | main.log.error( traceback.print_exc()) |
| 241 | main.log.info(self.name+" ::::::") |
| 242 | main.cleanup() |
| 243 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame^] | 244 | |
| 245 | def feature_install(self, feature_str): |
| 246 | ''' |
| 247 | Installs a specified feature |
| 248 | by issuing command: 'onos> feature:install <feature_str>' |
| 249 | ''' |
| 250 | try: |
| 251 | self.handle.sendline("") |
| 252 | self.handle.expect("onos>") |
| 253 | |
| 254 | self.handle.sendline("feature:install "+str(feature_str)) |
| 255 | self.handle.expect("onos>") |
| 256 | |
| 257 | return main.TRUE |
| 258 | |
| 259 | except pexpect.EOF: |
| 260 | main.log.error(self.name + ": EOF exception found") |
| 261 | main.log.error(self.name + ": " + self.handle.before) |
| 262 | main.cleanup() |
| 263 | main.exit() |
| 264 | except: |
| 265 | main.log.info(self.name+" ::::::") |
| 266 | main.log.error( traceback.print_exc()) |
| 267 | main.log.info(self.name+" ::::::") |
| 268 | main.cleanup() |
| 269 | main.exit() |
| 270 | |
| 271 | def feature_uninstall(self, feature_str): |
| 272 | ''' |
| 273 | Uninstalls a specified feature |
| 274 | by issuing command: 'onos> feature:uninstall <feature_str>' |
| 275 | ''' |
| 276 | try: |
| 277 | self.handle.sendline("") |
| 278 | self.handle.expect("onos>") |
| 279 | |
| 280 | self.handle.sendline("feature:uninstall "+str(feature_str)) |
| 281 | self.handle.expect("onos>") |
| 282 | |
| 283 | return main.TRUE |
| 284 | |
| 285 | except pexpect.EOF: |
| 286 | main.log.error(self.name + ": EOF exception found") |
| 287 | main.log.error(self.name + ": " + self.handle.before) |
| 288 | main.cleanup() |
| 289 | main.exit() |
| 290 | except: |
| 291 | main.log.info(self.name+" ::::::") |
| 292 | main.log.error( traceback.print_exc()) |
| 293 | main.log.info(self.name+" ::::::") |
| 294 | main.cleanup() |
| 295 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 296 | |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame^] | 297 | |