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: |
| 84 | self.handle.sendline("exit") |
| 85 | self.handle.expect("closed") |
| 86 | except pexpect.EOF: |
| 87 | main.log.error(self.name + ": EOF exception found") |
| 88 | main.log.error(self.name + ": " + self.handle.before) |
| 89 | except: |
| 90 | main.log.error(self.name + ": Connection failed to the host") |
| 91 | response = main.FALSE |
| 92 | return response |
| 93 | |
| 94 | def set_cell(self, cellname): |
| 95 | ''' |
| 96 | Calls 'cell <name>' to set the environment variables on ONOSbench |
| 97 | |
| 98 | Before issuing any cli commands, set the environment variable first. |
| 99 | ''' |
| 100 | try: |
| 101 | if not cellname: |
| 102 | main.log.error("Must define cellname") |
| 103 | main.cleanup() |
| 104 | main.exit() |
| 105 | else: |
| 106 | self.handle.sendline("cell "+str(cellname)) |
| 107 | #Expect the cellname in the ONOS_CELL variable. |
| 108 | #Note that this variable name is subject to change |
| 109 | # and that this driver will have to change accordingly |
| 110 | self.handle.expect("ONOS_CELL="+str(cellname)) |
| 111 | handle_before = self.handle.before |
| 112 | handle_after = self.handle.after |
| 113 | #Get the rest of the handle |
| 114 | self.handle.sendline("") |
| 115 | self.handle.expect("\$") |
| 116 | handle_more = self.handle.before |
| 117 | |
| 118 | main.log.info("Cell call returned: "+handle_before+ |
| 119 | handle_after + handle_more) |
| 120 | |
| 121 | return main.TRUE |
| 122 | |
| 123 | except pexpect.EOF: |
| 124 | main.log.error(self.name + ": EOF exception found") |
| 125 | main.log.error(self.name + ": " + self.handle.before) |
| 126 | main.cleanup() |
| 127 | main.exit() |
| 128 | except: |
| 129 | main.log.info(self.name+" ::::::") |
| 130 | main.log.error( traceback.print_exc()) |
| 131 | main.log.info(self.name+" ::::::") |
| 132 | main.cleanup() |
| 133 | main.exit() |
| 134 | |
| 135 | def start_onos_cli(self): |
| 136 | try: |
| 137 | self.handle.sendline("") |
| 138 | self.handle.expect("\$") |
| 139 | |
| 140 | #Wait for onos start (-w) and enter onos cli |
| 141 | self.handle.sendline("onos -w") |
| 142 | self.handle.expect("onos>") |
| 143 | |
| 144 | except pexpect.EOF: |
| 145 | main.log.error(self.name + ": EOF exception found") |
| 146 | main.log.error(self.name + ": " + self.handle.before) |
| 147 | main.cleanup() |
| 148 | main.exit() |
| 149 | except: |
| 150 | main.log.info(self.name+" ::::::") |
| 151 | main.log.error( traceback.print_exc()) |
| 152 | main.log.info(self.name+" ::::::") |
| 153 | main.cleanup() |
| 154 | main.exit() |
| 155 | |
| 156 | #IMPORTANT NOTE: |
| 157 | #For all cli commands, naming convention should match |
| 158 | #the cli command replacing ':' with '_'. |
| 159 | #Ex) onos:topology > onos_topology |
| 160 | # onos:links > onos_links |
| 161 | # feature:list > feature_list |
| 162 | |
| 163 | def onos_topology(self): |
| 164 | try: |
| 165 | self.handle.sendline("") |
| 166 | self.handle.expect("onos>") |
| 167 | self.handle.sendline("onos:topology") |
| 168 | self.handle.expect("onos>") |
| 169 | |
| 170 | handle = self.handle.before |
| 171 | |
| 172 | main.log.info("onos:topology returned: " + |
| 173 | str(handle)) |
| 174 | |
| 175 | return handle |
| 176 | |
| 177 | except pexpect.EOF: |
| 178 | main.log.error(self.name + ": EOF exception found") |
| 179 | main.log.error(self.name + ": " + self.handle.before) |
| 180 | main.cleanup() |
| 181 | main.exit() |
| 182 | except: |
| 183 | main.log.info(self.name+" ::::::") |
| 184 | main.log.error( traceback.print_exc()) |
| 185 | main.log.info(self.name+" ::::::") |
| 186 | main.cleanup() |
| 187 | main.exit() |
| 188 | |