Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 2 | |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 3 | ''' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 4 | This driver interacts with ONOS bench, the OSGi platform |
| 5 | that configures the ONOS nodes. (aka ONOS-next) |
| 6 | |
| 7 | Please follow the coding style demonstrated by existing |
| 8 | functions and document properly. |
| 9 | |
| 10 | If you are a contributor to the driver, please |
| 11 | list your email here for future contact: |
| 12 | |
| 13 | jhall@onlab.us |
| 14 | andrew@onlab.us |
| 15 | |
| 16 | OCT 9 2014 |
| 17 | |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 18 | ''' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 19 | |
andrewonlab | 7735d85 | 2014-10-09 13:02:47 -0400 | [diff] [blame] | 20 | import sys |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 21 | import time |
| 22 | import pexpect |
| 23 | import re |
| 24 | import traceback |
andrewonlab | 7735d85 | 2014-10-09 13:02:47 -0400 | [diff] [blame] | 25 | import os.path |
andrewonlab | 6e20c34 | 2014-10-10 18:08:48 -0400 | [diff] [blame] | 26 | import pydoc |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 27 | sys.path.append("../") |
| 28 | from drivers.common.clidriver import CLI |
| 29 | |
| 30 | class OnosDriver(CLI): |
| 31 | |
| 32 | def __init__(self): |
andrewonlab | 6e20c34 | 2014-10-10 18:08:48 -0400 | [diff] [blame] | 33 | ''' |
| 34 | Initialize client |
| 35 | ''' |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 36 | super(CLI, self).__init__() |
| 37 | |
| 38 | def connect(self,**connectargs): |
| 39 | ''' |
| 40 | Creates ssh handle for ONOS "bench". |
| 41 | ''' |
| 42 | try: |
| 43 | for key in connectargs: |
| 44 | vars(self)[key] = connectargs[key] |
| 45 | self.home = "~/ONOS" |
| 46 | for key in self.options: |
| 47 | if key == "home": |
| 48 | self.home = self.options['home'] |
| 49 | break |
| 50 | |
| 51 | |
| 52 | self.name = self.options['name'] |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 53 | self.handle = super(OnosDriver,self).connect( |
| 54 | user_name = self.user_name, |
| 55 | ip_address = self.ip_address, |
| 56 | port = self.port, |
| 57 | pwd = self.pwd, |
| 58 | home = self.home) |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 59 | |
| 60 | self.handle.sendline("cd "+ self.home) |
| 61 | self.handle.expect("\$") |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 62 | if self.handle: |
| 63 | return self.handle |
| 64 | else : |
| 65 | main.log.info("NO ONOS HANDLE") |
| 66 | return main.FALSE |
| 67 | except pexpect.EOF: |
| 68 | main.log.error(self.name + ": EOF exception found") |
| 69 | main.log.error(self.name + ": " + self.handle.before) |
| 70 | main.cleanup() |
| 71 | main.exit() |
| 72 | except: |
| 73 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 74 | main.log.error( traceback.print_exc() ) |
| 75 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 76 | main.cleanup() |
| 77 | main.exit() |
| 78 | |
| 79 | def disconnect(self): |
| 80 | ''' |
| 81 | Called when Test is complete to disconnect the ONOS handle. |
| 82 | ''' |
| 83 | response = '' |
| 84 | try: |
Jon Hall | f8d1098 | 2014-10-22 12:23:38 -0400 | [diff] [blame] | 85 | self.handle.sendline("\n") |
| 86 | self.handle.expect("\$") |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 87 | self.handle.sendline("exit") |
| 88 | self.handle.expect("closed") |
| 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 |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 96 | |
| 97 | def onos_package(self): |
| 98 | ''' |
| 99 | Produce a self-contained tar.gz file that can be deployed |
| 100 | and executed on any platform with Java 7 JRE. |
| 101 | ''' |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 102 | |
| 103 | try: |
| 104 | self.handle.sendline("onos-package") |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 105 | self.handle.expect("onos-package") |
andrewonlab | 0748d2a | 2014-10-09 13:24:17 -0400 | [diff] [blame] | 106 | self.handle.expect("tar.gz",timeout=10) |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 107 | handle = str(self.handle.before) |
| 108 | main.log.info("onos-package command returned: "+ |
| 109 | handle) |
andrewonlab | 0748d2a | 2014-10-09 13:24:17 -0400 | [diff] [blame] | 110 | #As long as the sendline does not time out, |
| 111 | #return true. However, be careful to interpret |
| 112 | #the results of the onos-package command return |
| 113 | return main.TRUE |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 114 | |
andrewonlab | 7735d85 | 2014-10-09 13:02:47 -0400 | [diff] [blame] | 115 | except pexpect.EOF: |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 116 | main.log.error(self.name + ": EOF exception found") |
| 117 | main.log.error(self.name + ": " + self.handle.before) |
| 118 | except: |
| 119 | main.log.error("Failed to package ONOS") |
| 120 | main.cleanup() |
| 121 | main.exit() |
| 122 | |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 123 | def clean_install(self): |
| 124 | ''' |
| 125 | Runs mvn clean install in the root of the ONOS directory. |
| 126 | This will clean all ONOS artifacts then compile each module |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 127 | |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 128 | Returns: main.TRUE on success |
| 129 | On Failure, exits the test |
| 130 | ''' |
| 131 | try: |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 132 | main.log.info("Running 'mvn clean install' on " + str(self.name) + |
| 133 | ". This may take some time.") |
| 134 | self.handle.sendline("cd "+ self.home) |
| 135 | self.handle.expect("\$") |
| 136 | |
| 137 | self.handle.sendline("\n") |
| 138 | self.handle.expect("\$") |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 139 | self.handle.sendline("mvn clean install") |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 140 | self.handle.expect("mvn clean install") |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 141 | while 1: |
| 142 | i=self.handle.expect([ |
| 143 | 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s\ |
| 144 | Runtime\sEnvironment\sto\scontinue', |
| 145 | 'BUILD\sFAILURE', |
| 146 | 'BUILD\sSUCCESS', |
| 147 | 'ONOS\$', |
| 148 | pexpect.TIMEOUT],timeout=600) |
| 149 | if i == 0: |
| 150 | main.log.error(self.name + ":There is insufficient memory \ |
| 151 | for the Java Runtime Environment to continue.") |
| 152 | #return main.FALSE |
| 153 | main.cleanup() |
| 154 | main.exit() |
| 155 | if i == 1: |
| 156 | main.log.error(self.name + ": Build failure!") |
| 157 | #return main.FALSE |
| 158 | main.cleanup() |
| 159 | main.exit() |
| 160 | elif i == 2: |
| 161 | main.log.info(self.name + ": Build success!") |
| 162 | elif i == 3: |
| 163 | main.log.info(self.name + ": Build complete") |
Jon Hall | f8ef52c | 2014-10-09 19:37:33 -0400 | [diff] [blame] | 164 | #Print the build time |
| 165 | for line in self.handle.before.splitlines(): |
| 166 | if "Total time:" in line: |
| 167 | main.log.info(line) |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 168 | self.handle.sendline("\n") |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 169 | self.handle.expect("\$", timeout=60) |
| 170 | return main.TRUE |
| 171 | elif i == 4: |
| 172 | main.log.error(self.name + ": mvn clean install TIMEOUT!") |
| 173 | #return main.FALSE |
| 174 | main.cleanup() |
| 175 | main.exit() |
| 176 | else: |
| 177 | main.log.error(self.name + ": unexpected response from \ |
| 178 | mvn clean install") |
| 179 | #return main.FALSE |
| 180 | main.cleanup() |
| 181 | main.exit() |
| 182 | except pexpect.EOF: |
| 183 | main.log.error(self.name + ": EOF exception found") |
| 184 | main.log.error(self.name + ": " + self.handle.before) |
| 185 | main.cleanup() |
| 186 | main.exit() |
| 187 | except: |
| 188 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 189 | main.log.error( traceback.print_exc() ) |
| 190 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 191 | main.cleanup() |
| 192 | main.exit() |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 193 | |
| 194 | def git_pull(self, comp1=""): |
| 195 | ''' |
| 196 | Assumes that "git pull" works without login |
| 197 | |
| 198 | This function will perform a git pull on the ONOS instance. |
| 199 | If used as git_pull("NODE") it will do git pull + NODE. This is |
| 200 | for the purpose of pulling from other nodes if necessary. |
| 201 | |
| 202 | Otherwise, this function will perform a git pull in the |
| 203 | ONOS repository. If it has any problems, it will return main.ERROR |
Shreya Shah | ee15f6c | 2014-10-28 18:12:30 -0400 | [diff] [blame] | 204 | If it successfully does a git_pull, it will return a 1 (main.TRUE) |
| 205 | If it has no updates, it will return 3. |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 206 | |
| 207 | ''' |
| 208 | try: |
| 209 | # main.log.info(self.name + ": Stopping ONOS") |
| 210 | #self.stop() |
| 211 | self.handle.sendline("cd " + self.home) |
| 212 | self.handle.expect("ONOS\$") |
| 213 | if comp1=="": |
| 214 | self.handle.sendline("git pull") |
| 215 | else: |
| 216 | self.handle.sendline("git pull " + comp1) |
| 217 | |
| 218 | uptodate = 0 |
| 219 | i=self.handle.expect(['fatal', |
| 220 | 'Username\sfor\s(.*):\s', |
| 221 | '\sfile(s*) changed,\s', |
| 222 | 'Already up-to-date', |
| 223 | 'Aborting', |
| 224 | 'You\sare\snot\scurrently\son\sa\sbranch', |
| 225 | 'You\sasked\sme\sto\spull\swithout\stelling\sme\swhich\sbranch\syou', |
| 226 | 'Pull\sis\snot\spossible\sbecause\syou\shave\sunmerged\sfiles', |
| 227 | pexpect.TIMEOUT], |
| 228 | timeout=300) |
| 229 | #debug |
| 230 | #main.log.report(self.name +": \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after)) |
| 231 | if i==0: |
| 232 | main.log.error(self.name + ": Git pull had some issue...") |
| 233 | return main.ERROR |
| 234 | elif i==1: |
| 235 | main.log.error(self.name + ": Git Pull Asking for username. ") |
| 236 | return main.ERROR |
| 237 | elif i==2: |
| 238 | main.log.info(self.name + ": Git Pull - pulling repository now") |
| 239 | self.handle.expect("ONOS\$", 120) |
Shreya Shah | ee15f6c | 2014-10-28 18:12:30 -0400 | [diff] [blame] | 240 | return main.TRUE # So that only when git pull is done, we do mvn clean compile |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 241 | elif i==3: |
| 242 | main.log.info(self.name + ": Git Pull - Already up to date") |
Shreya Shah | ee15f6c | 2014-10-28 18:12:30 -0400 | [diff] [blame] | 243 | return i |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 244 | elif i==4: |
| 245 | main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?") |
| 246 | return main.ERROR |
| 247 | elif i==5: |
| 248 | main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!") |
| 249 | return main.ERROR |
| 250 | elif i==6: |
| 251 | main.log.info(self.name + ": Git Pull - You have not configured an upstream branch to pull from. Git pull failed!") |
| 252 | return main.ERROR |
| 253 | elif i==7: |
| 254 | main.log.info(self.name + ": Git Pull - Pull is not possible because you have unmerged files.") |
| 255 | return main.ERROR |
| 256 | elif i==8: |
| 257 | main.log.error(self.name + ": Git Pull - TIMEOUT") |
| 258 | main.log.error(self.name + " Response was: " + str(self.handle.before)) |
| 259 | return main.ERROR |
| 260 | else: |
| 261 | main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors") |
| 262 | return main.ERROR |
| 263 | except pexpect.EOF: |
| 264 | main.log.error(self.name + ": EOF exception found") |
| 265 | main.log.error(self.name + ": " + self.handle.before) |
| 266 | main.cleanup() |
| 267 | main.exit() |
| 268 | except: |
| 269 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 270 | main.log.error( traceback.print_exc() ) |
| 271 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 272 | main.cleanup() |
| 273 | main.exit() |
| 274 | |
| 275 | def git_checkout(self, branch="master"): |
| 276 | ''' |
| 277 | Assumes that "git pull" works without login |
| 278 | |
| 279 | This function will perform a git git checkout on the ONOS instance. |
| 280 | If used as git_checkout("branch") it will do git checkout of the "branch". |
| 281 | |
| 282 | Otherwise, this function will perform a git checkout of the master |
| 283 | branch of the ONOS repository. If it has any problems, it will return |
| 284 | main.ERROR. |
| 285 | If the branch was already the specified branch, or the git checkout was |
| 286 | successful then the function will return main.TRUE. |
| 287 | |
| 288 | ''' |
| 289 | try: |
| 290 | # main.log.info(self.name + ": Stopping ONOS") |
| 291 | #self.stop() |
| 292 | self.handle.sendline("cd " + self.home) |
| 293 | self.handle.expect("ONOS\$") |
| 294 | if branch != 'master': |
| 295 | #self.handle.sendline('git stash') |
| 296 | #self.handle.expect('ONOS\$') |
| 297 | #print "After issuing git stash cmnd: ", self.handle.before |
| 298 | cmd = "git checkout "+branch |
| 299 | print "checkout cmd = ", cmd |
| 300 | self.handle.sendline(cmd) |
| 301 | uptodate = 0 |
| 302 | i=self.handle.expect(['fatal', |
| 303 | 'Username\sfor\s(.*):\s', |
| 304 | 'Already\son\s\'', |
| 305 | 'Switched\sto\sbranch\s\'', |
| 306 | pexpect.TIMEOUT],timeout=60) |
| 307 | else: |
| 308 | #self.handle.sendline('git stash apply') |
| 309 | #self.handle.expect('ONOS\$') |
| 310 | #print "After issuing git stash apply cmnd: ", self.handle.before |
| 311 | cmd = "git checkout "+branch |
| 312 | print "checkout cmd = ", cmd |
| 313 | self.handle.sendline(cmd) |
| 314 | uptodate = 0 |
| 315 | switchedToMaster = 0 |
| 316 | i=self.handle.expect(['fatal', |
| 317 | 'Username\sfor\s(.*):\s', |
| 318 | 'Already\son\s\'master\'', |
| 319 | 'Switched\sto\sbranch\s\'master\'', |
| 320 | pexpect.TIMEOUT],timeout=60) |
| 321 | |
| 322 | |
| 323 | if i==0: |
| 324 | main.log.error(self.name + ": Git checkout had some issue...") |
| 325 | return main.ERROR |
| 326 | elif i==1: |
| 327 | main.log.error(self.name + ": Git checkout Asking for username!!! Bad!") |
| 328 | return main.ERROR |
| 329 | elif i==2: |
| 330 | main.log.info(self.name + ": Git Checkout %s : Already on this branch" %branch) |
| 331 | self.handle.expect("ONOS\$") |
| 332 | print "after checkout cmd = ", self.handle.before |
| 333 | switchedToMaster = 1 |
| 334 | return main.TRUE |
| 335 | elif i==3: |
| 336 | main.log.info(self.name + ": Git checkout %s - Switched to this branch" %branch) |
| 337 | self.handle.expect("ONOS\$") |
| 338 | print "after checkout cmd = ", self.handle.before |
| 339 | switchedToMaster = 1 |
| 340 | return main.TRUE |
| 341 | elif i==4: |
| 342 | main.log.error(self.name + ": Git Checkout- TIMEOUT") |
| 343 | main.log.error(self.name + " Response was: " + str(self.handle.before)) |
| 344 | return main.ERROR |
| 345 | else: |
| 346 | main.log.error(self.name + ": Git Checkout - Unexpected response, check for pull errors") |
| 347 | return main.ERROR |
| 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(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 358 | main.cleanup() |
| 359 | main.exit() |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 360 | |
Jon Hall | 45ec092 | 2014-10-10 19:33:49 -0400 | [diff] [blame] | 361 | def get_version(self): |
| 362 | ''' |
| 363 | Writes the COMMIT number to the report to be parsed by Jenkins data collecter. |
| 364 | ''' |
| 365 | try: |
| 366 | self.handle.sendline("export TERM=xterm-256color") |
| 367 | self.handle.expect("xterm-256color") |
| 368 | self.handle.expect("\$") |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 369 | self.handle.sendline("\n") |
| 370 | self.handle.expect("\$") |
| 371 | self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller --decorate=short | grep -A 6 \"commit\" --color=never") |
| 372 | self.handle.expect("--color=never") |
Jon Hall | 45ec092 | 2014-10-10 19:33:49 -0400 | [diff] [blame] | 373 | self.handle.expect("\$") |
| 374 | response=(self.name +": \n"+ str(self.handle.before + self.handle.after)) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 375 | self.handle.sendline("cd " + self.home) |
| 376 | self.handle.expect("\$") |
Jon Hall | 45ec092 | 2014-10-10 19:33:49 -0400 | [diff] [blame] | 377 | lines=response.splitlines() |
| 378 | for line in lines: |
| 379 | print line |
| 380 | return lines[2] |
| 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(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 390 | main.cleanup() |
| 391 | main.exit() |
| 392 | |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 393 | def create_cell_file(self, bench_ip, file_name, mn_ip_addrs, |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 394 | extra_feature_string, *onos_ip_addrs): |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 395 | ''' |
| 396 | Creates a cell file based on arguments |
| 397 | Required: |
| 398 | * Bench IP address (bench_ip) |
| 399 | - Needed to copy the cell file over |
| 400 | * File name of the cell file (file_name) |
| 401 | * Mininet IP address (mn_ip_addrs) |
| 402 | - Note that only 1 ip address is |
| 403 | supported currently |
| 404 | * ONOS IP addresses (onos_ip_addrs) |
| 405 | - Must be passed in as last arguments |
| 406 | |
| 407 | NOTE: Assumes cells are located at: |
| 408 | ~/<self.home>/tools/test/cells/ |
| 409 | ''' |
| 410 | |
| 411 | #Variable initialization |
| 412 | cell_directory = self.home + "/tools/test/cells/" |
| 413 | #We want to create the cell file in the dependencies directory |
| 414 | #of TestON first, then copy over to ONOS bench |
| 415 | temp_directory = "/tmp/" |
| 416 | #Create the cell file in the directory for writing (w+) |
| 417 | cell_file = open(temp_directory+file_name , 'w+') |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 418 | |
| 419 | comment = "" |
| 420 | comment_string = "#"+ comment |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 421 | #Feature string is hardcoded environment variables |
| 422 | #That you may wish to use by default on startup. |
| 423 | #Note that you may not want certain features listed |
| 424 | #on here. |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 425 | core_feature_string = "export ONOS_FEATURES=webconsole,onos-api,"+\ |
| 426 | "onos-cli,onos-openflow,onos-app-mobility,onos-app-tvue,"+\ |
| 427 | "onos-app-proxyarp,"+extra_feature_string |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 428 | mn_string = "export OCN=" |
| 429 | onos_string = "export OC" |
| 430 | temp_count = 1 |
| 431 | |
| 432 | #Create ONOS_NIC ip address prefix |
| 433 | temp_onos_ip = onos_ip_addrs[0] |
| 434 | temp_list = [] |
| 435 | temp_list = temp_onos_ip.split(".") |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 436 | #Omit last element of list to format for NIC |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 437 | temp_list = temp_list[:-1] |
| 438 | #Structure the nic string ip |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 439 | nic_addr = ".".join(temp_list) + ".*" |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 440 | onos_nic_string = "export ONOS_NIC="+nic_addr |
| 441 | |
| 442 | try: |
| 443 | #Start writing to file |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 444 | cell_file.write(onos_nic_string + "\n") |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 445 | |
| 446 | for arg in onos_ip_addrs: |
| 447 | #For each argument in onos_ip_addrs, write to file |
| 448 | #Output should look like the following: |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 449 | # export OC1="10.128.20.11" |
| 450 | # export OC2="10.128.20.12" |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 451 | cell_file.write(onos_string + str(temp_count) + |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 452 | "=" + "\"" + arg + "\"" + "\n" ) |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 453 | temp_count = temp_count + 1 |
| 454 | |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 455 | cell_file.write(mn_string +"\""+ mn_ip_addrs +"\""+ "\n") |
| 456 | cell_file.write(core_feature_string + "\n") |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 457 | cell_file.close() |
| 458 | |
| 459 | #We use os.system to send the command to TestON cluster |
| 460 | #to account for the case in which TestON is not located |
| 461 | #on the same cluster as the ONOS bench |
| 462 | #Note that even if TestON is located on the same cluster |
| 463 | #as ONOS bench, you must setup passwordless ssh |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 464 | #between TestON and ONOS bench in order to automate the test. |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 465 | os.system("scp "+temp_directory+file_name+ |
| 466 | " admin@"+bench_ip+":"+cell_directory) |
| 467 | |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 468 | return main.TRUE |
| 469 | |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 470 | except pexpect.EOF: |
| 471 | main.log.error(self.name + ": EOF exception found") |
| 472 | main.log.error(self.name + ": " + self.handle.before) |
| 473 | main.cleanup() |
| 474 | main.exit() |
| 475 | except: |
| 476 | main.log.info(self.name + ":::::::::") |
| 477 | main.log.error( traceback.print_exc() ) |
| 478 | main.log.info(":::::::") |
| 479 | main.cleanup() |
| 480 | main.exit() |
| 481 | |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 482 | def set_cell(self, cellname): |
| 483 | ''' |
| 484 | Calls 'cell <name>' to set the environment variables on ONOSbench |
| 485 | ''' |
| 486 | try: |
| 487 | if not cellname: |
| 488 | main.log.error("Must define cellname") |
| 489 | main.cleanup() |
| 490 | main.exit() |
| 491 | else: |
| 492 | self.handle.sendline("cell "+str(cellname)) |
| 493 | #Expect the cellname in the ONOS_CELL variable. |
| 494 | #Note that this variable name is subject to change |
| 495 | # and that this driver will have to change accordingly |
| 496 | self.handle.expect("ONOS_CELL="+str(cellname)) |
| 497 | handle_before = self.handle.before |
| 498 | handle_after = self.handle.after |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 499 | #Get the rest of the handle |
| 500 | self.handle.sendline("") |
| 501 | self.handle.expect("\$") |
| 502 | handle_more = self.handle.before |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 503 | |
| 504 | main.log.info("Cell call returned: "+handle_before+ |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 505 | handle_after + handle_more) |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 506 | |
| 507 | return main.TRUE |
| 508 | |
| 509 | except pexpect.EOF: |
| 510 | main.log.error(self.name + ": EOF exception found") |
| 511 | main.log.error(self.name + ": " + self.handle.before) |
| 512 | main.cleanup() |
| 513 | main.exit() |
| 514 | except: |
| 515 | main.log.info(self.name+" ::::::") |
| 516 | main.log.error( traceback.print_exc()) |
| 517 | main.log.info(self.name+" ::::::") |
| 518 | main.cleanup() |
| 519 | main.exit() |
| 520 | |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 521 | def verify_cell(self): |
| 522 | ''' |
| 523 | Calls 'onos-verify-cell' to check for cell installation |
| 524 | ''' |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 525 | #TODO: Add meaningful expect value |
| 526 | |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 527 | try: |
| 528 | #Clean handle by sending empty and expecting $ |
| 529 | self.handle.sendline("") |
| 530 | self.handle.expect("\$") |
| 531 | self.handle.sendline("onos-verify-cell") |
| 532 | self.handle.expect("\$") |
| 533 | handle_before = self.handle.before |
| 534 | handle_after = self.handle.after |
| 535 | #Get the rest of the handle |
| 536 | self.handle.sendline("") |
| 537 | self.handle.expect("\$") |
| 538 | handle_more = self.handle.before |
| 539 | |
| 540 | main.log.info("Verify cell returned: "+handle_before+ |
| 541 | handle_after + handle_more) |
| 542 | |
| 543 | return main.TRUE |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 544 | except pexpect.EOF: |
| 545 | main.log.error(self.name + ": EOF exception found") |
| 546 | main.log.error(self.name + ": " + self.handle.before) |
| 547 | main.cleanup() |
| 548 | main.exit() |
| 549 | except: |
| 550 | main.log.info(self.name+" ::::::") |
| 551 | main.log.error( traceback.print_exc()) |
| 552 | main.log.info(self.name+" ::::::") |
| 553 | main.cleanup() |
| 554 | main.exit() |
| 555 | |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 556 | def onos_cli(self, ONOS_ip, cmdstr): |
| 557 | ''' |
| 558 | Uses 'onos' command to send various ONOS CLI arguments. |
| 559 | Required: |
| 560 | * ONOS_ip: specify the ip of the cell machine |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 561 | * cmdstr: specify the command string to send |
andrewonlab | 6e20c34 | 2014-10-10 18:08:48 -0400 | [diff] [blame] | 562 | |
| 563 | This function is intended to expose the entire karaf |
| 564 | CLI commands for ONOS. Try to use this function first |
| 565 | before attempting to write a ONOS CLI specific driver |
| 566 | function. |
| 567 | You can see a list of available 'cmdstr' arguments |
| 568 | by starting onos, and typing in 'onos' to enter the |
| 569 | onos> CLI. Then, type 'help' to see the list of |
| 570 | available commands. |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 571 | ''' |
| 572 | try: |
| 573 | if not ONOS_ip: |
| 574 | main.log.error("You must specify the IP address") |
| 575 | return main.FALSE |
| 576 | if not cmdstr: |
| 577 | main.log.error("You must specify the command string") |
| 578 | return main.FALSE |
| 579 | |
| 580 | cmdstr = str(cmdstr) |
| 581 | self.handle.sendline("") |
| 582 | self.handle.expect("\$") |
| 583 | |
| 584 | self.handle.sendline("onos -w " + ONOS_ip + " " + cmdstr) |
| 585 | self.handle.expect("\$") |
| 586 | |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 587 | handle_before = self.handle.before |
| 588 | print "handle_before = ", self.handle.before |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 589 | handle_after = str(self.handle.after) |
| 590 | |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 591 | #self.handle.sendline("") |
| 592 | #self.handle.expect("\$") |
| 593 | #handle_more = str(self.handle.before) |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 594 | |
| 595 | main.log.info("Command sent successfully") |
| 596 | |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 597 | #Obtain return handle that consists of result from |
| 598 | #the onos command. The string may need to be |
| 599 | #configured further. |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 600 | #return_string = handle_before + handle_after |
| 601 | return_string = handle_before |
| 602 | print "return_string = ", return_string |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 603 | return return_string |
| 604 | |
| 605 | except pexpect.EOF: |
| 606 | main.log.error(self.name + ": EOF exception found") |
| 607 | main.log.error(self.name + ": " + self.handle.before) |
| 608 | main.cleanup() |
| 609 | main.exit() |
| 610 | except: |
| 611 | main.log.info(self.name+" ::::::") |
| 612 | main.log.error( traceback.print_exc()) |
| 613 | main.log.info(self.name+" ::::::") |
| 614 | main.cleanup() |
| 615 | main.exit() |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 616 | |
| 617 | def onos_install(self, options="-f", node = ""): |
| 618 | ''' |
| 619 | Installs ONOS bits on the designated cell machine. |
| 620 | If -f option is provided, it also forces an uninstall. |
| 621 | Presently, install also includes onos-push-bits and |
| 622 | onos-config within. |
| 623 | The node option allows you to selectively only push the jar |
| 624 | files to certain onos nodes |
| 625 | |
| 626 | Returns: main.TRUE on success and main.FALSE on failure |
| 627 | ''' |
| 628 | try: |
| 629 | self.handle.sendline("onos-install " + options + " " + node) |
| 630 | self.handle.expect("onos-install ") |
| 631 | #NOTE: this timeout may need to change depending on the network and size of ONOS |
| 632 | i=self.handle.expect(["Network\sis\sunreachable", |
| 633 | "onos\sstart/running,\sprocess", |
| 634 | pexpect.TIMEOUT],timeout=60) |
| 635 | |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 636 | if i == 0: |
| 637 | main.log.warn("Network is unreachable") |
| 638 | return main.FALSE |
| 639 | elif i == 1: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 640 | main.log.info("ONOS was installed on " + node + " and started") |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 641 | return main.TRUE |
| 642 | elif i == 2: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 643 | main.log.info("Installation of ONOS on " + node + " timed out") |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 644 | return main.FALSE |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 645 | |
| 646 | except pexpect.EOF: |
| 647 | main.log.error(self.name + ": EOF exception found") |
| 648 | main.log.error(self.name + ": " + self.handle.before) |
| 649 | main.cleanup() |
| 650 | main.exit() |
| 651 | except: |
| 652 | main.log.info(self.name+" ::::::") |
| 653 | main.log.error( traceback.print_exc()) |
| 654 | main.log.info(self.name+" ::::::") |
| 655 | main.cleanup() |
| 656 | main.exit() |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 657 | |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 658 | def onos_start(self, node_ip): |
| 659 | ''' |
| 660 | Calls onos command: 'onos-service [<node-ip>] start' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 661 | This command is a remote management of the ONOS upstart daemon |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 662 | ''' |
| 663 | |
| 664 | try: |
| 665 | self.handle.sendline("") |
| 666 | self.handle.expect("\$") |
| 667 | self.handle.sendline("onos-service "+str(node_ip)+ |
| 668 | " start") |
| 669 | i = self.handle.expect([ |
| 670 | "Job\sis\salready\srunning", |
| 671 | "start/running", |
| 672 | "Unknown\sinstance", |
Shreya Shah | d01153d | 2014-10-23 15:08:56 -0400 | [diff] [blame] | 673 | pexpect.TIMEOUT],timeout=120) |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 674 | |
| 675 | if i == 0: |
| 676 | main.log.info("Service is already running") |
| 677 | return main.TRUE |
| 678 | elif i == 1: |
| 679 | main.log.info("ONOS service started") |
| 680 | return main.TRUE |
| 681 | else: |
| 682 | main.log.error("ONOS service failed to start") |
| 683 | main.cleanup() |
| 684 | main.exit() |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 685 | except pexpect.EOF: |
| 686 | main.log.error(self.name + ": EOF exception found") |
| 687 | main.log.error(self.name + ": " + self.handle.before) |
| 688 | main.cleanup() |
| 689 | main.exit() |
| 690 | except: |
| 691 | main.log.info(self.name+" ::::::") |
| 692 | main.log.error( traceback.print_exc()) |
| 693 | main.log.info(self.name+" ::::::") |
| 694 | main.cleanup() |
| 695 | main.exit() |
| 696 | |
andrewonlab | 2b30bd3 | 2014-10-09 16:48:55 -0400 | [diff] [blame] | 697 | def onos_stop(self, node_ip): |
| 698 | ''' |
| 699 | Calls onos command: 'onos-service [<node-ip>] stop' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 700 | This command is a remote management of the ONOS upstart daemon |
andrewonlab | 2b30bd3 | 2014-10-09 16:48:55 -0400 | [diff] [blame] | 701 | ''' |
| 702 | try: |
| 703 | self.handle.sendline("") |
| 704 | self.handle.expect("\$") |
| 705 | self.handle.sendline("onos-service "+str(node_ip)+ |
| 706 | " stop") |
| 707 | i = self.handle.expect([ |
| 708 | "stop/waiting", |
| 709 | "Unknown\sinstance", |
| 710 | pexpect.TIMEOUT],timeout=60) |
| 711 | |
| 712 | if i == 0: |
| 713 | main.log.info("ONOS service stopped") |
| 714 | return main.TRUE |
| 715 | elif i == 1: |
| 716 | main.log.info("Unknown ONOS instance specified: "+ |
| 717 | str(node_ip)) |
| 718 | return main.FALSE |
| 719 | else: |
| 720 | main.log.error("ONOS service failed to stop") |
| 721 | return main.FALSE |
| 722 | |
| 723 | except pexpect.EOF: |
| 724 | main.log.error(self.name + ": EOF exception found") |
| 725 | main.log.error(self.name + ": " + self.handle.before) |
| 726 | main.cleanup() |
| 727 | main.exit() |
| 728 | except: |
| 729 | main.log.info(self.name+" ::::::") |
| 730 | main.log.error( traceback.print_exc()) |
| 731 | main.log.info(self.name+" ::::::") |
| 732 | main.cleanup() |
| 733 | main.exit() |
| 734 | |
andrewonlab | c8d4797 | 2014-10-09 16:52:36 -0400 | [diff] [blame] | 735 | def onos_uninstall(self): |
| 736 | ''' |
| 737 | Calls the command: 'onos-uninstall' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 738 | Uninstalls ONOS from the designated cell machine, stopping |
| 739 | if needed |
andrewonlab | c8d4797 | 2014-10-09 16:52:36 -0400 | [diff] [blame] | 740 | ''' |
| 741 | try: |
| 742 | self.handle.sendline("") |
| 743 | self.handle.expect("\$") |
| 744 | self.handle.sendline("onos-uninstall") |
| 745 | self.handle.expect("\$") |
| 746 | |
andrewonlab | 8472745 | 2014-10-09 18:15:36 -0400 | [diff] [blame] | 747 | main.log.info("ONOS cell machine was uninstalled") |
andrewonlab | c8d4797 | 2014-10-09 16:52:36 -0400 | [diff] [blame] | 748 | #onos-uninstall command does not return any text |
| 749 | return main.TRUE |
| 750 | |
| 751 | except pexpect.EOF: |
| 752 | main.log.error(self.name + ": EOF exception found") |
| 753 | main.log.error(self.name + ": " + self.handle.before) |
| 754 | main.cleanup() |
| 755 | main.exit() |
| 756 | except: |
| 757 | main.log.info(self.name+" ::::::") |
| 758 | main.log.error( traceback.print_exc()) |
| 759 | main.log.info(self.name+" ::::::") |
| 760 | main.cleanup() |
| 761 | main.exit() |
andrewonlab | 2b30bd3 | 2014-10-09 16:48:55 -0400 | [diff] [blame] | 762 | |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 763 | def onos_kill(self, node_ip): |
| 764 | ''' |
| 765 | Calls the command: 'onos-kill [<node-ip>]' |
| 766 | "Remotely, and unceremoniously kills the ONOS instance running on |
| 767 | the specified cell machine" - Tom V |
| 768 | ''' |
| 769 | |
| 770 | try: |
| 771 | self.handle.sendline("") |
| 772 | self.handle.expect("\$") |
| 773 | self.handle.sendline("onos-kill " + str(node_ip)) |
| 774 | i = self.handle.expect([ |
| 775 | "\$", |
| 776 | "No\sroute\sto\shost", |
| 777 | "password:", |
| 778 | pexpect.TIMEOUT], timeout=20) |
| 779 | |
| 780 | if i == 0: |
| 781 | main.log.info("ONOS instance "+str(node_ip)+" was killed") |
| 782 | return main.TRUE |
| 783 | elif i == 1: |
| 784 | main.log.info("No route to host") |
| 785 | return main.FALSE |
| 786 | elif i == 2: |
| 787 | main.log.info("Passwordless login for host: "+str(node_ip)+ |
| 788 | " not configured") |
| 789 | return main.FALSE |
| 790 | else: |
| 791 | main.log.info("ONOS instasnce was not killed") |
| 792 | return main.FALSE |
| 793 | |
| 794 | except pexpect.EOF: |
| 795 | main.log.error(self.name + ": EOF exception found") |
| 796 | main.log.error(self.name + ": " + self.handle.before) |
| 797 | main.cleanup() |
| 798 | main.exit() |
| 799 | except: |
| 800 | main.log.info(self.name+" ::::::") |
| 801 | main.log.error( traceback.print_exc()) |
| 802 | main.log.info(self.name+" ::::::") |
| 803 | main.cleanup() |
| 804 | main.exit() |
| 805 | |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 806 | def onos_start_network(self, mntopo): |
| 807 | ''' |
| 808 | Calls the command 'onos-start-network [<mininet-topo>] |
| 809 | "remotely starts the specified topology on the cell's |
| 810 | mininet machine against all controllers configured in the |
| 811 | cell." |
| 812 | * Specify mininet topology file name for mntopo |
| 813 | * Topo files should be placed at: |
| 814 | ~/<your-onos-directory>/tools/test/topos |
| 815 | |
| 816 | NOTE: This function will take you to the mininet prompt |
| 817 | ''' |
| 818 | try: |
| 819 | if not mntopo: |
| 820 | main.log.error("You must specify a topo file to execute") |
| 821 | return main.FALSE |
| 822 | |
| 823 | mntopo = str(mntopo) |
| 824 | self.handle.sendline("") |
| 825 | self.handle.expect("\$") |
| 826 | |
| 827 | self.handle.sendline("onos-start-network " + mntopo) |
| 828 | self.handle.expect("mininet>") |
| 829 | main.log.info("Network started, entered mininet prompt") |
| 830 | |
| 831 | #TODO: Think about whether return is necessary or not |
| 832 | |
| 833 | except pexpect.EOF: |
| 834 | main.log.error(self.name + ": EOF exception found") |
| 835 | main.log.error(self.name + ": " + self.handle.before) |
| 836 | main.cleanup() |
| 837 | main.exit() |
| 838 | except: |
| 839 | main.log.info(self.name+" ::::::") |
| 840 | main.log.error( traceback.print_exc()) |
| 841 | main.log.info(self.name+" ::::::") |
| 842 | main.cleanup() |
| 843 | main.exit() |
| 844 | |
| 845 | |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 846 | def isup(self, node = ""): |
| 847 | ''' |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 848 | Run's onos-wait-for-start which only returns once ONOS is at run |
| 849 | level 100(ready for use) |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 850 | |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 851 | Returns: main.TRUE if ONOS is running and main.FALSE on timeout |
| 852 | ''' |
| 853 | try: |
| 854 | self.handle.sendline("onos-wait-for-start " + node ) |
| 855 | self.handle.expect("onos-wait-for-start") |
| 856 | #NOTE: this timeout is arbitrary" |
| 857 | i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout = 120) |
| 858 | if i == 0: |
| 859 | main.log.info(self.name + ": " + node + " is up") |
| 860 | return main.TRUE |
| 861 | elif i == 1: |
| 862 | #NOTE: since this function won't return until ONOS is ready, |
| 863 | # we will kill it on timeout |
| 864 | self.handle.sendline("\003") #Control-C |
| 865 | self.handle.expect("\$") |
| 866 | return main.FALSE |
| 867 | except pexpect.EOF: |
| 868 | main.log.error(self.name + ": EOF exception found") |
| 869 | main.log.error(self.name + ": " + self.handle.before) |
| 870 | main.cleanup() |
| 871 | main.exit() |
| 872 | except: |
| 873 | main.log.info(self.name+" ::::::") |
| 874 | main.log.error( traceback.print_exc()) |
| 875 | main.log.info(self.name+" ::::::") |
| 876 | main.cleanup() |
| 877 | main.exit() |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 878 | |
| 879 | |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 880 | def get_topology(self,topology_output): |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 881 | ''' |
| 882 | parses the onos:topology output |
| 883 | Returns: a topology dict populated by the key values found in |
| 884 | the cli command. |
| 885 | ''' |
| 886 | |
| 887 | try: |
| 888 | #call the cli to get the topology summary |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 889 | #cmdstr = "onos:topology" |
| 890 | #cli_result = self.onos_cli(ip, cmdstr) |
| 891 | #print "cli_result = ", cli_result |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 892 | |
| 893 | #Parse the output |
| 894 | topology = {} |
| 895 | #for line in cli_result.split("\n"): |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 896 | for line in topology_output.splitlines(): |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 897 | if not line.startswith("time="): |
| 898 | continue |
| 899 | #else |
| 900 | print line |
| 901 | for var in line.split(","): |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 902 | #print "'"+var+"'" |
| 903 | #print "'"+var.strip()+"'" |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 904 | key, value = var.strip().split("=") |
| 905 | topology[key] = value |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 906 | print "topology = ", topology |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 907 | devices = topology.get('devices', False) |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 908 | print "devices = ", devices |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 909 | links = topology.get('links', False) |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 910 | print "links = ", links |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 911 | clusters = topology.get('clusters', False) |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 912 | print "clusters = ", clusters |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 913 | paths = topology.get('paths', False) |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 914 | print "paths = ", paths |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 915 | |
| 916 | return topology |
| 917 | except pexpect.EOF: |
| 918 | main.log.error(self.name + ": EOF exception found") |
| 919 | main.log.error(self.name + ": " + self.handle.before) |
| 920 | main.cleanup() |
| 921 | main.exit() |
| 922 | except: |
| 923 | main.log.info(self.name+" ::::::") |
| 924 | main.log.error( traceback.print_exc()) |
| 925 | main.log.info(self.name+" ::::::") |
| 926 | main.cleanup() |
| 927 | main.exit() |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 928 | |
| 929 | |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 930 | |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 931 | def check_status(self, topology_result, numoswitch, numolink, log_level="info"): |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 932 | ''' |
| 933 | Checks the number of swithes & links that ONOS sees against the |
| 934 | supplied values. By default this will report to main.log, but the |
| 935 | log level can be specifid. |
| 936 | |
| 937 | Params: ip = ip used for the onos cli |
| 938 | numoswitch = expected number of switches |
| 939 | numlink = expected number of links |
| 940 | log_level = level to log to. Currently accepts 'info', 'warn' and 'report' |
| 941 | |
| 942 | |
| 943 | log_level can |
| 944 | |
| 945 | Returns: main.TRUE if the number of switchs and links are correct, |
| 946 | main.FALSE if the numer of switches and links is incorrect, |
| 947 | and main.ERROR otherwise |
| 948 | ''' |
| 949 | |
| 950 | try: |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 951 | topology = self.get_topology(topology_result) |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 952 | if topology == {}: |
| 953 | return main.ERROR |
| 954 | output = "" |
| 955 | #Is the number of switches is what we expected |
| 956 | devices = topology.get('devices',False) |
| 957 | links = topology.get('links',False) |
| 958 | if devices == False or links == False: |
| 959 | return main.ERROR |
| 960 | switch_check = ( int(devices) == int(numoswitch) ) |
| 961 | #Is the number of links is what we expected |
| 962 | link_check = ( int(links) == int(numolink) ) |
| 963 | if (switch_check and link_check): |
| 964 | #We expected the correct numbers |
| 965 | output = output + "The number of links and switches match "\ |
| 966 | + "what was expected" |
| 967 | result = main.TRUE |
| 968 | else: |
| 969 | output = output + \ |
| 970 | "The number of links and switches does not match what was expected" |
| 971 | result = main.FALSE |
| 972 | output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\ |
| 973 | % ( int(devices), int(numoswitch), int(links), int(numolink) ) |
| 974 | if log_level == "report": |
| 975 | main.log.report(output) |
| 976 | elif log_level == "warn": |
| 977 | main.log.warn(output) |
| 978 | else: |
| 979 | main.log.info(output) |
| 980 | return result |
| 981 | except pexpect.EOF: |
| 982 | main.log.error(self.name + ": EOF exception found") |
| 983 | main.log.error(self.name + ": " + self.handle.before) |
| 984 | main.cleanup() |
| 985 | main.exit() |
| 986 | except: |
| 987 | main.log.info(self.name+" ::::::") |
| 988 | main.log.error( traceback.print_exc()) |
| 989 | main.log.info(self.name+" ::::::") |
| 990 | main.cleanup() |
| 991 | main.exit() |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 992 | |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 993 | |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 994 | def tshark_grep(self, grep, directory, interface='eth0'): |
| 995 | ''' |
| 996 | Required: |
| 997 | * grep string |
| 998 | * directory to store results |
| 999 | Optional: |
| 1000 | * interface - default: eth0 |
| 1001 | Description: |
| 1002 | Uses tshark command to grep specific group of packets |
| 1003 | and stores the results to specified directory. |
| 1004 | The timestamp is hardcoded to be in epoch |
| 1005 | ''' |
| 1006 | self.handle.sendline("") |
| 1007 | self.handle.expect("\$") |
andrewonlab | efd7f3d | 2014-10-21 16:02:31 -0400 | [diff] [blame] | 1008 | self.handle.sendline("") |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1009 | self.handle.sendline("tshark -i "+str(interface)+ |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 1010 | " -t e | grep --line-buffered \""+str(grep)+"\" >"+directory+" &") |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1011 | self.handle.sendline("\r") |
| 1012 | self.handle.expect("Capturing on") |
| 1013 | self.handle.sendline("\r") |
| 1014 | self.handle.expect("\$") |
| 1015 | |
| 1016 | def tshark_stop(self): |
| 1017 | ''' |
| 1018 | Removes wireshark files from /tmp and kills all tshark processes |
| 1019 | ''' |
andrewonlab | 5ba0d9e | 2014-10-24 13:32:23 -0400 | [diff] [blame] | 1020 | #Remove all pcap from previous captures |
| 1021 | self.execute(cmd="sudo rm /tmp/wireshark*") |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1022 | self.handle.sendline("") |
| 1023 | self.handle.sendline("sudo kill -9 `ps -ef | grep \"tshark -i\" |"+ |
| 1024 | " grep -v grep | awk '{print $2}'`") |
| 1025 | self.handle.sendline("") |
| 1026 | main.log.info("Tshark stopped") |
| 1027 | |
andrewonlab | 0c38a4a | 2014-10-28 18:35:35 -0400 | [diff] [blame] | 1028 | def ptpd(self, args): |
| 1029 | ''' |
| 1030 | Initiate ptp with user-specified args. |
| 1031 | Required: |
| 1032 | * args: specify string of args after command |
| 1033 | 'sudo ptpd' |
| 1034 | ''' |
| 1035 | try: |
| 1036 | self.handle.sendline("sudo ptpd "+str(args)) |
andrewonlab | 978dff6 | 2014-10-29 16:53:35 -0400 | [diff] [blame] | 1037 | i = self.handle.expect([ |
andrewonlab | 0c38a4a | 2014-10-28 18:35:35 -0400 | [diff] [blame] | 1038 | "Multiple", |
| 1039 | "Error", |
| 1040 | "\$"]) |
| 1041 | self.handle.expect("\$") |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1042 | |
andrewonlab | 0c38a4a | 2014-10-28 18:35:35 -0400 | [diff] [blame] | 1043 | if i == 0: |
| 1044 | handle = self.handle.before |
| 1045 | main.log.info("ptpd returned an error: "+ |
| 1046 | str(handle)) |
| 1047 | return handle |
| 1048 | elif i == 1: |
| 1049 | handle = self.handle.before |
| 1050 | main.log.error("ptpd returned an error: "+ |
| 1051 | str(handle)) |
| 1052 | return handle |
| 1053 | else: |
| 1054 | return main.TRUE |
| 1055 | |
| 1056 | except pexpect.EOF: |
| 1057 | main.log.error(self.name + ": EOF exception found") |
| 1058 | main.log.error(self.name + ": " + self.handle.before) |
| 1059 | main.cleanup() |
| 1060 | main.exit() |
| 1061 | except: |
| 1062 | main.log.info(self.name+" ::::::") |
| 1063 | main.log.error( traceback.print_exc()) |
| 1064 | main.log.info(self.name+" ::::::") |
| 1065 | main.cleanup() |
| 1066 | main.exit() |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1067 | |
| 1068 | |