andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 4 | This driver enters the onos> prompt to issue commands. |
| 5 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 6 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 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 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 18 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 19 | import sys |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 20 | import pexpect |
| 21 | import re |
| 22 | import traceback |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 23 | # import os.path |
| 24 | sys.path.append( "../" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 25 | from drivers.common.clidriver import CLI |
| 26 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 27 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 28 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 29 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 30 | def __init__( self ): |
| 31 | """ |
| 32 | Initialize client |
| 33 | """ |
| 34 | super( CLI, self ).__init__() |
| 35 | |
| 36 | def connect( self, **connectargs ): |
| 37 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 38 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 39 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 40 | try: |
| 41 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 42 | vars( self )[ key ] = connectargs[ key ] |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 43 | self.home = "~/ONOS" |
| 44 | for key in self.options: |
| 45 | if key == "home": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 46 | self.home = self.options[ 'home' ] |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 47 | break |
| 48 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 49 | self.name = self.options[ 'name' ] |
| 50 | self.handle = super( OnosCliDriver, self ).connect( |
| 51 | user_name=self.user_name, |
| 52 | ip_address=self.ip_address, |
| 53 | port=self.port, |
| 54 | pwd=self.pwd, |
| 55 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 56 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 57 | self.handle.sendline( "cd " + self.home ) |
| 58 | self.handle.expect( "\$" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 59 | if self.handle: |
| 60 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 61 | else: |
| 62 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 63 | return main.FALSE |
| 64 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 65 | main.log.error( self.name + ": EOF exception found" ) |
| 66 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 67 | main.cleanup() |
| 68 | main.exit() |
| 69 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 70 | main.log.info( self.name + ":::::::::::::::::::::::" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 71 | main.log.error( traceback.print_exc() ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 72 | main.log.info( ":::::::::::::::::::::::" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 73 | main.cleanup() |
| 74 | main.exit() |
| 75 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | def disconnect( self ): |
| 77 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 78 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 79 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 80 | response = '' |
| 81 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 82 | self.handle.sendline( "" ) |
| 83 | i = self.handle.expect( [ "onos>", "\$" ] ) |
Jon Hall | 7e5b917 | 2014-10-22 12:32:47 -0400 | [diff] [blame] | 84 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 85 | self.handle.sendline( "system:shutdown" ) |
| 86 | self.handle.expect( "Confirm" ) |
| 87 | self.handle.sendline( "yes" ) |
| 88 | self.handle.expect( "\$" ) |
| 89 | self.handle.sendline( "" ) |
| 90 | self.handle.expect( "\$" ) |
| 91 | self.handle.sendline( "exit" ) |
| 92 | self.handle.expect( "closed" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 93 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 94 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 95 | main.log.error( self.name + ": EOF exception found" ) |
| 96 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 97 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 98 | main.log.error( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 99 | response = main.FALSE |
| 100 | return response |
| 101 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 102 | def logout( self ): |
| 103 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 104 | Sends 'logout' command to ONOS cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 105 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 106 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 107 | self.handle.sendline( "" ) |
| 108 | i = self.handle.expect( [ |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 109 | "onos>", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 110 | "\$" ], timeout=10 ) |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 111 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 112 | self.handle.sendline( "logout" ) |
| 113 | self.handle.expect( "\$" ) |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 114 | elif i == 1: |
| 115 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 116 | |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 117 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 118 | main.log.error( self.name + ": eof exception found" ) |
| 119 | main.log.error( self.name + ": " + |
| 120 | self.handle.before ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 121 | main.cleanup() |
| 122 | main.exit() |
| 123 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 124 | main.log.info( self.name + " ::::::" ) |
| 125 | main.log.error( traceback.print_exc() ) |
| 126 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 127 | main.cleanup() |
| 128 | main.exit() |
| 129 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 130 | def set_cell( self, cellname ): |
| 131 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 132 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 133 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 135 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 136 | try: |
| 137 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 138 | main.log.error( "Must define cellname" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 139 | main.cleanup() |
| 140 | main.exit() |
| 141 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 142 | self.handle.sendline( "cell " + str( cellname ) ) |
| 143 | # Expect the cellname in the ONOS_CELL variable. |
| 144 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 145 | # and that this driver will have to change accordingly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 146 | self.handle.expect( "ONOS_CELL=" + str( cellname ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 147 | handle_before = self.handle.before |
| 148 | handle_after = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 149 | # Get the rest of the handle |
| 150 | self.handle.sendline( "" ) |
| 151 | self.handle.expect( "\$" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 152 | handle_more = self.handle.before |
| 153 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 154 | main.log.info( "Cell call returned: " + handle_before + |
| 155 | handle_after + handle_more ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 156 | |
| 157 | return main.TRUE |
| 158 | |
| 159 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 160 | main.log.error( self.name + ": eof exception found" ) |
| 161 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 162 | main.cleanup() |
| 163 | main.exit() |
| 164 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 165 | main.log.info( self.name + " ::::::" ) |
| 166 | main.log.error( traceback.print_exc() ) |
| 167 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 168 | main.cleanup() |
| 169 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 170 | |
| 171 | def start_onos_cli( self, ONOS_ip, karafTimeout="" ): |
| 172 | """ |
| 173 | karafTimeout is an optional arugument. karafTimeout value passed by user would be used to set the |
Hari Krishna | d7b9c20 | 2015-01-05 10:38:14 -0800 | [diff] [blame] | 174 | current karaf shell idle timeout. Note that when ever this property is modified the shell will exit and |
| 175 | the subsequent login would reflect new idle timeout. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 176 | Below is an example to start a session with 60 seconds idle timeout ( input value is in milliseconds ): |
| 177 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 178 | tValue = "60000" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 179 | main.ONOScli1.start_onos_cli( ONOS_ip, karafTimeout=tValue ) |
| 180 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 181 | Note: karafTimeout is left as str so that this could be read and passed to start_onos_cli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 182 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 183 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 184 | self.handle.sendline( "" ) |
| 185 | x = self.handle.expect( [ |
| 186 | "\$", "onos>" ], timeout=10 ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 187 | |
| 188 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 189 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 190 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 191 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 192 | # Wait for onos start ( -w ) and enter onos cli |
| 193 | self.handle.sendline( "onos -w " + str( ONOS_ip ) ) |
| 194 | i = self.handle.expect( [ |
| 195 | "onos>", |
| 196 | pexpect.TIMEOUT ], timeout=60 ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 197 | |
| 198 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 199 | main.log.info( str( ONOS_ip ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 200 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 201 | self.handle.sendline( |
| 202 | "config:property-set -p org.apache.karaf.shell sshIdleTimeout " + |
| 203 | karafTimeout ) |
| 204 | self.handle.expect( "\$" ) |
| 205 | self.handle.sendline( "onos -w " + str( ONOS_ip ) ) |
| 206 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 207 | return main.TRUE |
| 208 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 209 | # If failed, send ctrl+c to process and try again |
| 210 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 211 | self.handle.send( "\x03" ) |
| 212 | self.handle.sendline( "onos -w " + str( ONOS_ip ) ) |
| 213 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 214 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 215 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 216 | main.log.info( str( ONOS_ip ) + " CLI Started " + |
| 217 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 218 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 219 | self.handle.sendline( |
| 220 | "config:property-set -p org.apache.karaf.shell sshIdleTimeout " + |
| 221 | karafTimeout ) |
| 222 | self.handle.expect( "\$" ) |
| 223 | self.handle.sendline( "onos -w " + str( ONOS_ip ) ) |
| 224 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 225 | return main.TRUE |
| 226 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 227 | main.log.error( "Connection to CLI " + |
| 228 | str( ONOS_ip ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 229 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 230 | |
| 231 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 232 | main.log.error( self.name + ": EOF exception found" ) |
| 233 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 234 | main.cleanup() |
| 235 | main.exit() |
| 236 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 237 | main.log.info( self.name + " ::::::" ) |
| 238 | main.log.error( traceback.print_exc() ) |
| 239 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 240 | main.cleanup() |
| 241 | main.exit() |
| 242 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 243 | def sendline( self, cmd_str ): |
| 244 | """ |
| 245 | Send a completely user specified string to |
| 246 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 247 | a very specific command to send. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 248 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 249 | Warning: There are no sanity checking to commands |
| 250 | sent using this method. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 251 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 252 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 253 | self.handle.sendline( "" ) |
| 254 | self.handle.expect( "onos>" ) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 255 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 256 | self.handle.sendline( cmd_str ) |
| 257 | self.handle.expect( "onos>" ) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 258 | |
| 259 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 260 | |
| 261 | self.handle.sendline( "" ) |
| 262 | self.handle.expect( "onos>" ) |
| 263 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 264 | handle += self.handle.before |
| 265 | handle += self.handle.after |
| 266 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | main.log.info( "Command sent." ) |
| 268 | ansi_escape = re.compile( r'\x1b[^m]*m' ) |
| 269 | handle = ansi_escape.sub( '', handle ) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 270 | |
| 271 | return handle |
| 272 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 273 | main.log.error( self.name + ": EOF exception found" ) |
| 274 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 275 | main.cleanup() |
| 276 | main.exit() |
| 277 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 278 | main.log.info( self.name + " ::::::" ) |
| 279 | main.log.error( traceback.print_exc() ) |
| 280 | main.log.info( self.name + " ::::::" ) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 281 | main.cleanup() |
| 282 | main.exit() |
| 283 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 284 | # IMPORTANT NOTE: |
| 285 | # For all cli commands, naming convention should match |
| 286 | # the cli command replacing ':' with '_'. |
| 287 | # Ex ) onos:topology > onos_topology |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 288 | # onos:links > onos_links |
| 289 | # feature:list > feature_list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 290 | |
| 291 | def add_node( self, node_id, ONOS_ip, tcp_port="" ): |
| 292 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 293 | Adds a new cluster node by ID and address information. |
| 294 | Required: |
| 295 | * node_id |
| 296 | * ONOS_ip |
| 297 | Optional: |
| 298 | * tcp_port |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 299 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 300 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 301 | self.handle.sendline( "" ) |
| 302 | self.handle.expect( "onos>" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 303 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 304 | self.handle.sendline( "add-node " + |
| 305 | str( node_id ) + " " + |
| 306 | str( ONOS_ip ) + " " + |
| 307 | str( tcp_port ) ) |
| 308 | |
| 309 | i = self.handle.expect( [ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 310 | "Error", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 311 | "onos>" ] ) |
| 312 | |
| 313 | # Clear handle to get previous output |
| 314 | self.handle.sendline( "" ) |
| 315 | self.handle.expect( "onos>" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 316 | |
| 317 | handle = self.handle.before |
| 318 | |
| 319 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 320 | main.log.error( "Error in adding node" ) |
| 321 | main.log.error( handle ) |
| 322 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 323 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 324 | main.log.info( "Node " + str( ONOS_ip ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 325 | return main.TRUE |
| 326 | |
| 327 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 328 | main.log.error( self.name + ": EOF exception found" ) |
| 329 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 330 | main.cleanup() |
| 331 | main.exit() |
| 332 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 333 | main.log.info( self.name + " ::::::" ) |
| 334 | main.log.error( traceback.print_exc() ) |
| 335 | main.log.info( self.name + " ::::::" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 336 | main.cleanup() |
| 337 | main.exit() |
| 338 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 339 | def remove_node( self, node_id ): |
| 340 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 341 | Removes a cluster by ID |
| 342 | Issues command: 'remove-node [<node-id>]' |
| 343 | Required: |
| 344 | * node_id |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 345 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 346 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 347 | self.handle.sendline( "" ) |
| 348 | self.handle.expect( "onos>" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 349 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 350 | self.handle.sendline( "remove-node " + str( node_id ) ) |
| 351 | self.handle.expect( "onos>" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 352 | |
| 353 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 354 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 355 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 356 | main.log.error( self.name + ": EOF exception found" ) |
| 357 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 358 | main.cleanup() |
| 359 | main.exit() |
| 360 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 361 | main.log.info( self.name + " ::::::" ) |
| 362 | main.log.error( traceback.print_exc() ) |
| 363 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 364 | main.cleanup() |
| 365 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 366 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 367 | def nodes( self ): |
| 368 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 369 | List the nodes currently visible |
| 370 | Issues command: 'nodes' |
| 371 | Returns: entire handle of list of nodes |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 372 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 373 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 374 | self.handle.sendline( "" ) |
| 375 | self.handle.expect( "onos>" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 376 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 377 | self.handle.sendline( "nodes" ) |
| 378 | self.handle.expect( "onos>" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 379 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 380 | self.handle.sendline( "" ) |
| 381 | self.handle.expect( "onos>" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 382 | |
| 383 | handle = self.handle.before |
| 384 | |
| 385 | return handle |
| 386 | |
| 387 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 388 | main.log.error( self.name + ": EOF exception found" ) |
| 389 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 390 | main.cleanup() |
| 391 | main.exit() |
| 392 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 393 | main.log.info( self.name + " ::::::" ) |
| 394 | main.log.error( traceback.print_exc() ) |
| 395 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 396 | main.cleanup() |
| 397 | main.exit() |
| 398 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 399 | def topology( self ): |
| 400 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 401 | Shows the current state of the topology |
| 402 | by issusing command: 'onos> onos:topology' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 403 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 404 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 405 | self.handle.sendline( "" ) |
| 406 | self.handle.expect( "onos>" ) |
| 407 | # either onos:topology or 'topology' will work in CLI |
| 408 | self.handle.sendline( "onos:topology" ) |
| 409 | self.handle.expect( "onos>" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 410 | |
| 411 | handle = self.handle.before |
| 412 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 413 | main.log.info( "onos:topology returned: " + |
| 414 | str( handle ) ) |
| 415 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 416 | return handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 417 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 418 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 419 | main.log.error( self.name + ": EOF exception found" ) |
| 420 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 421 | main.cleanup() |
| 422 | main.exit() |
| 423 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 424 | main.log.info( self.name + " ::::::" ) |
| 425 | main.log.error( traceback.print_exc() ) |
| 426 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 427 | main.cleanup() |
| 428 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 429 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 430 | def feature_install( self, feature_str ): |
| 431 | """ |
| 432 | Installs a specified feature |
| 433 | by issuing command: 'onos> feature:install <feature_str>' |
| 434 | """ |
| 435 | try: |
| 436 | self.handle.sendline( "" ) |
| 437 | self.handle.expect( "onos>" ) |
| 438 | |
| 439 | self.handle.sendline( "feature:install " + str( feature_str ) ) |
| 440 | self.handle.expect( "onos>" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 441 | |
| 442 | return main.TRUE |
| 443 | |
| 444 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 445 | main.log.error( self.name + ": EOF exception found" ) |
| 446 | main.log.error( self.name + ": " + self.handle.before ) |
| 447 | main.log.report( "Failed to install feature" ) |
| 448 | main.log.report( "Exiting test" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 449 | main.cleanup() |
| 450 | main.exit() |
| 451 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 452 | main.log.info( self.name + " ::::::" ) |
| 453 | main.log.error( traceback.print_exc() ) |
| 454 | main.log.report( "Failed to install feature" ) |
| 455 | main.log.report( "Exiting test" ) |
| 456 | main.log.info( self.name + " ::::::" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 457 | main.cleanup() |
| 458 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 459 | |
| 460 | def feature_uninstall( self, feature_str ): |
| 461 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 462 | Uninstalls a specified feature |
| 463 | by issuing command: 'onos> feature:uninstall <feature_str>' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 464 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 465 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 466 | self.handle.sendline( "" ) |
| 467 | self.handle.expect( "onos>" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 468 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 469 | self.handle.sendline( "feature:uninstall " + str( feature_str ) ) |
| 470 | self.handle.expect( "onos>" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 471 | |
| 472 | return main.TRUE |
| 473 | |
| 474 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 475 | main.log.error( self.name + ": EOF exception found" ) |
| 476 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 477 | main.cleanup() |
| 478 | main.exit() |
| 479 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 480 | main.log.info( self.name + " ::::::" ) |
| 481 | main.log.error( traceback.print_exc() ) |
| 482 | main.log.info( self.name + " ::::::" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 483 | main.cleanup() |
| 484 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 485 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 486 | def devices( self, json_format=True ): |
| 487 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 488 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 489 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 490 | * json_format - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 491 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 492 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 493 | self.handle.sendline( "" ) |
| 494 | self.handle.expect( "onos>" ) |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 495 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 496 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 497 | self.handle.sendline( "devices -j" ) |
| 498 | self.handle.expect( "devices -j" ) |
| 499 | self.handle.expect( "onos>" ) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 500 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 501 | """ |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 502 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 503 | To make that escape sequence visible, use repr() function. The repr( handle ) output when printed shows the ANSI escape sequences. |
| 504 | In json.loads( somestring ), this somestring variable is actually repr( somestring ) and json.loads would fail with the escape sequence. |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 505 | So we take off that escape sequence using |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 506 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 507 | handle1 = ansi_escape.sub( '', handle ) |
| 508 | """ |
| 509 | # print "repr(handle) =", repr( handle ) |
| 510 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 511 | handle1 = ansi_escape.sub( '', handle ) |
| 512 | # print "repr(handle1) = ", repr( handle1 ) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 513 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 514 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 515 | self.handle.sendline( "devices" ) |
| 516 | self.handle.expect( "onos>" ) |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 517 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 518 | # print "handle =",handle |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 519 | return handle |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 520 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 521 | main.log.error( self.name + ": EOF exception found" ) |
| 522 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 523 | main.cleanup() |
| 524 | main.exit() |
| 525 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 526 | main.log.info( self.name + " ::::::" ) |
| 527 | main.log.error( traceback.print_exc() ) |
| 528 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 529 | main.cleanup() |
| 530 | main.exit() |
| 531 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 532 | def balance_masters( self ): |
| 533 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 534 | This balances the devices across all controllers |
| 535 | by issuing command: 'onos> onos:balance-masters' |
| 536 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 537 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 538 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 539 | self.handle.sendline( "" ) |
| 540 | self.handle.expect( "onos>" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 541 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 542 | self.handle.sendline( "onos:balance-masters" ) |
| 543 | self.handle.expect( "onos>" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 544 | return main.TRUE |
| 545 | |
| 546 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 547 | main.log.error( self.name + ": EOF exception found" ) |
| 548 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 549 | main.cleanup() |
| 550 | main.exit() |
| 551 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 552 | main.log.info( self.name + " ::::::" ) |
| 553 | main.log.error( traceback.print_exc() ) |
| 554 | main.log.info( self.name + " ::::::" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 555 | main.cleanup() |
| 556 | main.exit() |
| 557 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 558 | def links( self, json_format=True ): |
| 559 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 560 | Lists all core links |
| 561 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 562 | * json_format - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 563 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 564 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 565 | self.handle.sendline( "" ) |
| 566 | self.handle.expect( "onos>" ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 567 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 568 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 569 | self.handle.sendline( "links -j" ) |
| 570 | self.handle.expect( "links -j" ) |
| 571 | self.handle.expect( "onos>" ) |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 572 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 573 | """ |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 574 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 575 | To make that escape sequence visible, use repr() function. The repr( handle ) output when printed shows the ANSI escape sequences. |
| 576 | In json.loads( somestring ), this somestring variable is actually repr( somestring ) and json.loads would fail with the escape sequence. |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 577 | So we take off that escape sequence using |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 578 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 579 | handle1 = ansi_escape.sub( '', handle ) |
| 580 | """ |
| 581 | # print "repr(handle) =", repr( handle ) |
| 582 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 583 | handle1 = ansi_escape.sub( '', handle ) |
| 584 | # print "repr(handle1) = ", repr( handle1 ) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 585 | return handle1 |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 586 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 587 | self.handle.sendline( "links" ) |
| 588 | self.handle.expect( "onos>" ) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 589 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 590 | # print "handle =",handle |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 591 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 592 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 593 | main.log.error( self.name + ": EOF exception found" ) |
| 594 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 595 | main.cleanup() |
| 596 | main.exit() |
| 597 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 598 | main.log.info( self.name + " ::::::" ) |
| 599 | main.log.error( traceback.print_exc() ) |
| 600 | main.log.info( self.name + " ::::::" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 601 | main.cleanup() |
| 602 | main.exit() |
| 603 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 604 | def ports( self, json_format=True ): |
| 605 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 606 | Lists all ports |
| 607 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 608 | * json_format - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 609 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 610 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 611 | self.handle.sendline( "" ) |
| 612 | self.handle.expect( "onos>" ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 613 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 614 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 615 | self.handle.sendline( "ports -j" ) |
| 616 | self.handle.expect( "ports -j" ) |
| 617 | self.handle.expect( "onos>" ) |
Jon Hall | cd70729 | 2014-10-17 19:06:17 -0400 | [diff] [blame] | 618 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 619 | """ |
Jon Hall | d815ce4 | 2014-10-17 19:52:30 -0400 | [diff] [blame] | 620 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 621 | To make that escape sequence visible, use repr() function. The repr( handle ) output when printed shows the ANSI escape sequences. |
| 622 | In json.loads( somestring ), this somestring variable is actually repr( somestring ) and json.loads would fail with the escape sequence. |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 623 | So we take off that escape sequence using the following commads: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 624 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 625 | handle1 = ansi_escape.sub( '', handle ) |
| 626 | """ |
| 627 | # print "repr(handle) =", repr( handle ) |
| 628 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 629 | handle1 = ansi_escape.sub( '', handle ) |
| 630 | # print "repr(handle1) = ", repr( handle1 ) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 631 | return handle1 |
| 632 | |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 633 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 634 | self.handle.sendline( "ports" ) |
| 635 | self.handle.expect( "onos>" ) |
| 636 | self.handle.sendline( "" ) |
| 637 | self.handle.expect( "onos>" ) |
Jon Hall | a001c39 | 2014-10-17 18:50:59 -0400 | [diff] [blame] | 638 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 639 | # print "handle =",handle |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 640 | return handle |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 641 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 642 | main.log.error( self.name + ": EOF exception found" ) |
| 643 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 644 | main.cleanup() |
| 645 | main.exit() |
| 646 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 647 | main.log.info( self.name + " ::::::" ) |
| 648 | main.log.error( traceback.print_exc() ) |
| 649 | main.log.info( self.name + " ::::::" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 650 | main.cleanup() |
| 651 | main.exit() |
| 652 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 653 | def roles( self, json_format=True ): |
| 654 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 655 | Lists all devices and the controllers with roles assigned to them |
| 656 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 657 | * json_format - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 658 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 659 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 660 | self.handle.sendline( "" ) |
| 661 | self.handle.expect( "onos>" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 662 | |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 663 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 664 | self.handle.sendline( "roles -j" ) |
| 665 | self.handle.expect( "roles -j" ) |
| 666 | self.handle.expect( "onos>" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 667 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 668 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 669 | handle variable here contains some ANSI escape color code sequences at the |
| 670 | end which are invisible in the print command output. To make that escape |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 671 | sequence visible, use repr() function. The repr( handle ) output when printed |
| 672 | shows the ANSI escape sequences. In json.loads( somestring ), this somestring |
| 673 | variable is actually repr( somestring ) and json.loads would fail with the escape |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 674 | sequence. |
| 675 | |
| 676 | So we take off that escape sequence using the following commads: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 677 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 678 | handle1 = ansi_escape.sub( '', handle ) |
| 679 | """ |
| 680 | # print "repr(handle) =", repr( handle ) |
| 681 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 682 | handle1 = ansi_escape.sub( '', handle ) |
| 683 | # print "repr(handle1) = ", repr( handle1 ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 684 | return handle1 |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 685 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 686 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 687 | self.handle.sendline( "roles" ) |
| 688 | self.handle.expect( "onos>" ) |
| 689 | self.handle.sendline( "" ) |
| 690 | self.handle.expect( "onos>" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 691 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 692 | # print "handle =",handle |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 693 | return handle |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 694 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 695 | main.log.error( self.name + ": EOF exception found" ) |
| 696 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 697 | main.cleanup() |
| 698 | main.exit() |
| 699 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 700 | main.log.info( self.name + " ::::::" ) |
| 701 | main.log.error( traceback.print_exc() ) |
| 702 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 703 | main.cleanup() |
| 704 | main.exit() |
| 705 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 706 | def get_role( self, device_id ): |
| 707 | """ |
| 708 | Given the a string containing the json representation of the "roles" cli command and a |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 709 | partial or whole device id, returns a json object containing the |
| 710 | roles output for the first device whose id contains "device_id" |
| 711 | |
| 712 | Returns: |
| 713 | Dict of the role assignments for the given device or |
| 714 | None if not match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 715 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 716 | try: |
| 717 | import json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 718 | if device_id is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 719 | return None |
| 720 | else: |
| 721 | raw_roles = self.roles() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 722 | roles_json = json.loads( raw_roles ) |
| 723 | # search json for the device with id then return the device |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 724 | for device in roles_json: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 725 | # print device |
| 726 | if str( device_id ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 727 | return device |
| 728 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 729 | |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 730 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 731 | main.log.error( self.name + ": EOF exception found" ) |
| 732 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 733 | main.cleanup() |
| 734 | main.exit() |
| 735 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 736 | main.log.info( self.name + " ::::::" ) |
| 737 | main.log.error( traceback.print_exc() ) |
| 738 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 739 | main.cleanup() |
| 740 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 741 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 742 | def roles_not_null( self ): |
| 743 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 744 | Iterates through each device and checks if there is a master assigned |
| 745 | Returns: main.TRUE if each device has a master |
| 746 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 747 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 748 | try: |
| 749 | import json |
| 750 | raw_roles = self.roles() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 751 | roles_json = json.loads( raw_roles ) |
| 752 | # search json for the device with id then return the device |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 753 | for device in roles_json: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 754 | # print device |
| 755 | if device[ 'master' ] == "none": |
| 756 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 757 | return main.FALSE |
| 758 | return main.TRUE |
| 759 | |
| 760 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 761 | main.log.error( self.name + ": EOF exception found" ) |
| 762 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 763 | main.cleanup() |
| 764 | main.exit() |
| 765 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 766 | main.log.info( self.name + " ::::::" ) |
| 767 | main.log.error( traceback.print_exc() ) |
| 768 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 769 | main.cleanup() |
| 770 | main.exit() |
| 771 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 772 | def paths( self, src_id, dst_id ): |
| 773 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 774 | Returns string of paths, and the cost. |
| 775 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 776 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 777 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 778 | self.handle.sendline( "" ) |
| 779 | self.handle.expect( "onos>" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 780 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 781 | self.handle.sendline( "onos:paths " + |
| 782 | str( src_id ) + " " + str( dst_id ) ) |
| 783 | i = self.handle.expect( [ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 784 | "Error", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 785 | "onos>" ] ) |
| 786 | |
| 787 | self.handle.sendline( "" ) |
| 788 | self.handle.expect( "onos>" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 789 | |
| 790 | handle = self.handle.before |
| 791 | |
| 792 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 793 | main.log.error( "Error in getting paths" ) |
| 794 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 795 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 796 | path = handle.split( ";" )[ 0 ] |
| 797 | cost = handle.split( ";" )[ 1 ] |
| 798 | return ( path, cost ) |
| 799 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 800 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 801 | main.log.error( self.name + ": EOF exception found" ) |
| 802 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 803 | main.cleanup() |
| 804 | main.exit() |
| 805 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 806 | main.log.info( self.name + " ::::::" ) |
| 807 | main.log.error( traceback.print_exc() ) |
| 808 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 809 | main.cleanup() |
| 810 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 811 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 812 | def hosts( self, json_format=True ): |
| 813 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 814 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 815 | Optional argument: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 816 | * json_format - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 817 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 818 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 819 | self.handle.sendline( "" ) |
| 820 | self.handle.expect( "onos>" ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 821 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 822 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 823 | self.handle.sendline( "hosts -j" ) |
| 824 | self.handle.expect( "hosts -j" ) |
| 825 | self.handle.expect( "onos>" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 826 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 827 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 828 | handle variable here contains some ANSI escape color code sequences at the end which are invisible in the print command output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 829 | To make that escape sequence visible, use repr() function. The repr( handle ) output when printed shows the ANSI escape sequences. |
| 830 | In json.loads( somestring ), this somestring variable is actually repr( somestring ) and json.loads would fail with the escape sequence. |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 831 | So we take off that escape sequence using |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 832 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 833 | handle1 = ansi_escape.sub( '', handle ) |
| 834 | """ |
| 835 | # print "repr(handle) =", repr( handle ) |
| 836 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 837 | handle1 = ansi_escape.sub( '', handle ) |
| 838 | # print "repr(handle1) = ", repr( handle1 ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 839 | return handle1 |
| 840 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 841 | self.handle.sendline( "hosts" ) |
| 842 | self.handle.expect( "onos>" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 843 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 844 | # print "handle =",handle |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 845 | return handle |
| 846 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 847 | main.log.error( self.name + ": EOF exception found" ) |
| 848 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 849 | main.cleanup() |
| 850 | main.exit() |
| 851 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 852 | main.log.info( self.name + " ::::::" ) |
| 853 | main.log.error( traceback.print_exc() ) |
| 854 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 855 | main.cleanup() |
| 856 | main.exit() |
| 857 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 858 | def get_host( self, mac ): |
| 859 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 860 | Return the first host from the hosts api whose 'id' contains 'mac' |
| 861 | Note: mac must be a colon seperated mac address, but could be a partial mac address |
| 862 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 863 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 864 | import json |
| 865 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 866 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 867 | return None |
| 868 | else: |
| 869 | mac = mac |
| 870 | raw_hosts = self.hosts() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 871 | hosts_json = json.loads( raw_hosts ) |
| 872 | # search json for the host with mac then return the device |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 873 | for host in hosts_json: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 874 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
| 875 | if mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 876 | return host |
| 877 | return None |
| 878 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 879 | main.log.error( self.name + ": EOF exception found" ) |
| 880 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 881 | main.cleanup() |
| 882 | main.exit() |
| 883 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 884 | main.log.info( self.name + " ::::::" ) |
| 885 | main.log.error( traceback.print_exc() ) |
| 886 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 887 | main.cleanup() |
| 888 | main.exit() |
| 889 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 890 | def get_hosts_id( self, host_list ): |
| 891 | """ |
| 892 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 893 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 894 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 895 | Required: |
| 896 | * host_list: List of hosts obtained by Mininet |
| 897 | IMPORTANT: |
| 898 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 899 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 900 | Furthermore, it assumes that value of VLAN is '-1' |
| 901 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 902 | Converts mininet hosts ( h1, h2, h3... ) into |
| 903 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 904 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 905 | try: |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 906 | onos_host_list = [] |
| 907 | |
| 908 | for host in host_list: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 909 | host = host.replace( "h", "" ) |
| 910 | host_hex = hex( int( host ) ).zfill( 12 ) |
| 911 | host_hex = str( host_hex ).replace( 'x', '0' ) |
| 912 | i = iter( str( host_hex ) ) |
| 913 | host_hex = ":".join( a + b for a, b in zip( i, i ) ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 914 | host_hex = host_hex + "/-1" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 915 | onos_host_list.append( host_hex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 916 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 917 | return onos_host_list |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 918 | |
| 919 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 920 | main.log.error( self.name + ": EOF exception found" ) |
| 921 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 922 | main.cleanup() |
| 923 | main.exit() |
| 924 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 925 | main.log.info( self.name + " ::::::" ) |
| 926 | main.log.error( traceback.print_exc() ) |
| 927 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 928 | main.cleanup() |
| 929 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 930 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 931 | def add_host_intent( self, host_id_one, host_id_two ): |
| 932 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 933 | Required: |
| 934 | * host_id_one: ONOS host id for host1 |
| 935 | * host_id_two: ONOS host id for host2 |
| 936 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 937 | Adds a host-to-host intent ( bidrectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 938 | specifying the two hosts. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 939 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 940 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 941 | self.handle.sendline( "" ) |
| 942 | self.handle.expect( "onos>" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 943 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 944 | self.handle.sendline( "add-host-intent " + |
| 945 | str( host_id_one ) + " " + str( host_id_two ) ) |
| 946 | self.handle.expect( "onos>" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 947 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 948 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 949 | # print "handle =", handle |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 950 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 951 | main.log.info( "Host intent installed between " + |
| 952 | str( host_id_one ) + " and " + str( host_id_two ) ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 953 | |
| 954 | return handle |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 955 | |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 956 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 957 | main.log.error( self.name + ": EOF exception found" ) |
| 958 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 959 | main.cleanup() |
| 960 | main.exit() |
| 961 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 962 | main.log.info( self.name + " ::::::" ) |
| 963 | main.log.error( traceback.print_exc() ) |
| 964 | main.log.info( self.name + " ::::::" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 965 | main.cleanup() |
| 966 | main.exit() |
| 967 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 968 | def add_optical_intent( self, ingress_device, egress_device ): |
| 969 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 970 | Required: |
| 971 | * ingress_device: device id of ingress device |
| 972 | * egress_device: device id of egress device |
| 973 | Optional: |
| 974 | TODO: Still needs to be implemented via dev side |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 975 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 976 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 977 | self.handle.sendline( "add-optical-intent " + |
| 978 | str( ingress_device ) + " " + str( egress_device ) ) |
| 979 | self.handle.expect( "add-optical-intent" ) |
| 980 | i = self.handle.expect( [ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 981 | "Error", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 982 | "onos>" ] ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 983 | |
| 984 | handle = self.handle.before |
| 985 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 986 | # If error, return error message |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 987 | if i == 0: |
| 988 | return handle |
| 989 | else: |
| 990 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 991 | |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 992 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 993 | main.log.error( self.name + ": EOF exception found" ) |
| 994 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 995 | main.cleanup() |
| 996 | main.exit() |
| 997 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 998 | main.log.info( self.name + " ::::::" ) |
| 999 | main.log.error( traceback.print_exc() ) |
| 1000 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1001 | main.cleanup() |
| 1002 | main.exit() |
| 1003 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1004 | def add_point_intent( self, ingress_device, egress_device, |
| 1005 | port_ingress="", port_egress="", ethType="", ethSrc="", |
| 1006 | ethDst="", bandwidth="", lambda_alloc=False, |
| 1007 | ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="" ): |
| 1008 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1009 | Required: |
| 1010 | * ingress_device: device id of ingress device |
| 1011 | * egress_device: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1012 | Optional: |
| 1013 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1014 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1015 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1016 | * bandwidth: specify bandwidth capacity of link |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1017 | * lambda_alloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1018 | for the specified intent |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1019 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1020 | * ipSrc: specify ip source address |
| 1021 | * ipDst: specify ip destination address |
| 1022 | * tcpSrc: specify tcp source port |
| 1023 | * tcpDst: specify tcp destination port |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1024 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1025 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1026 | specifying device id's and optional fields |
| 1027 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1028 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1029 | options developers provide for point-to-point |
| 1030 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1031 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1032 | try: |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1033 | cmd = "" |
| 1034 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1035 | # If there are no optional arguments |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1036 | if not ethType and not ethSrc and not ethDst\ |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1037 | and not bandwidth and not lambda_alloc \ |
| 1038 | and not ipProto and not ipSrc and not ipDst \ |
| 1039 | and not tcpSrc and not tcpDst: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1040 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1041 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1042 | else: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1043 | cmd = "add-point-intent" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1044 | |
andrewonlab | 0c0a677 | 2014-10-22 12:31:18 -0400 | [diff] [blame] | 1045 | if ethType: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1046 | cmd += " --ethType " + str( ethType ) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1047 | if ethSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1048 | cmd += " --ethSrc " + str( ethSrc ) |
| 1049 | if ethDst: |
| 1050 | cmd += " --ethDst " + str( ethDst ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1051 | if bandwidth: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1052 | cmd += " --bandwidth " + str( bandwidth ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1053 | if lambda_alloc: |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1054 | cmd += " --lambda " |
| 1055 | if ipProto: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1056 | cmd += " --ipProto " + str( ipProto ) |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1057 | if ipSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1058 | cmd += " --ipSrc " + str( ipSrc ) |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1059 | if ipDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1060 | cmd += " --ipDst " + str( ipDst ) |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1061 | if tcpSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1062 | cmd += " --tcpSrc " + str( tcpSrc ) |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1063 | if tcpDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1064 | cmd += " --tcpDst " + str( tcpDst ) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1065 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1066 | # Check whether the user appended the port |
| 1067 | # or provided it as an input |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1068 | if "/" in ingress_device: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1069 | cmd += " " + str( ingress_device ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1070 | else: |
| 1071 | if not port_ingress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1072 | main.log.error( "You must specify " + |
| 1073 | "the ingress port" ) |
| 1074 | # TODO: perhaps more meaningful return |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1075 | return main.FALSE |
| 1076 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1077 | cmd += " " + \ |
| 1078 | str( ingress_device ) + "/" +\ |
| 1079 | str( port_ingress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1080 | |
| 1081 | if "/" in egress_device: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1082 | cmd += " " + str( egress_device ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1083 | else: |
| 1084 | if not port_egress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1085 | main.log.error( "You must specify " + |
| 1086 | "the egress port" ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1087 | return main.FALSE |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1088 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1089 | cmd += " " +\ |
| 1090 | str( egress_device ) + "/" +\ |
| 1091 | str( port_egress ) |
| 1092 | |
| 1093 | self.handle.sendline( cmd ) |
| 1094 | |
| 1095 | main.log.info( cmd + " sent" ) |
| 1096 | i = self.handle.expect( [ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1097 | "Error", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1098 | "onos>" ] ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1099 | |
| 1100 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1101 | main.log.error( "Error in adding point-to-point intent" ) |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 1102 | return main.FALSE |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1103 | else: |
| 1104 | return main.TRUE |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1105 | |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1106 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1107 | main.log.error( self.name + ": EOF exception found" ) |
| 1108 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1109 | main.cleanup() |
| 1110 | main.exit() |
| 1111 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1112 | main.log.info( self.name + " ::::::" ) |
| 1113 | main.log.error( traceback.print_exc() ) |
| 1114 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1115 | main.cleanup() |
| 1116 | main.exit() |
| 1117 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1118 | def add_multipoint_to_singlepoint_intent( |
| 1119 | self, ingress_device1, ingress_device2, egress_device, |
| 1120 | port_ingress="", port_egress="", ethType="", ethSrc="", |
| 1121 | ethDst="", bandwidth="", lambda_alloc=False, |
| 1122 | ipProto="", ipSrc="", ipDst="", tcpSrc="", tcpDst="", setEthSrc="", setEthDst="" ): |
| 1123 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1124 | Note: |
| 1125 | This function assumes that there would be 2 ingress devices and one egress device |
| 1126 | For more number of ingress devices, this function needs to be modified |
| 1127 | Required: |
| 1128 | * ingress_device1: device id of ingress device1 |
| 1129 | * ingress_device2: device id of ingress device2 |
| 1130 | * egress_device: device id of egress device |
| 1131 | Optional: |
| 1132 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1133 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1134 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1135 | * bandwidth: specify bandwidth capacity of link |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1136 | * lambda_alloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1137 | for the specified intent |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1138 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1139 | * ipSrc: specify ip source address |
| 1140 | * ipDst: specify ip destination address |
| 1141 | * tcpSrc: specify tcp source port |
| 1142 | * tcpDst: specify tcp destination port |
| 1143 | * setEthSrc: action to Rewrite Source MAC Address |
| 1144 | * setEthDst: action to Rewrite Destination MAC Address |
| 1145 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1146 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1147 | specifying device id's and optional fields |
| 1148 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1149 | NOTE: This function may change depending on the |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1150 | options developers provide for multipointpoint-to-singlepoint |
| 1151 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1152 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1153 | try: |
| 1154 | cmd = "" |
| 1155 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1156 | # If there are no optional arguments |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1157 | if not ethType and not ethSrc and not ethDst\ |
| 1158 | and not bandwidth and not lambda_alloc \ |
| 1159 | and not ipProto and not ipSrc and not ipDst \ |
| 1160 | and not tcpSrc and not tcpDst and not setEthSrc and not setEthDst: |
| 1161 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1162 | |
| 1163 | else: |
| 1164 | cmd = "add-multi-to-single-intent" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1165 | |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1166 | if ethType: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1167 | cmd += " --ethType " + str( ethType ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1168 | if ethSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1169 | cmd += " --ethSrc " + str( ethSrc ) |
| 1170 | if ethDst: |
| 1171 | cmd += " --ethDst " + str( ethDst ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1172 | if bandwidth: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1173 | cmd += " --bandwidth " + str( bandwidth ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1174 | if lambda_alloc: |
| 1175 | cmd += " --lambda " |
| 1176 | if ipProto: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1177 | cmd += " --ipProto " + str( ipProto ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1178 | if ipSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1179 | cmd += " --ipSrc " + str( ipSrc ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1180 | if ipDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1181 | cmd += " --ipDst " + str( ipDst ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1182 | if tcpSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1183 | cmd += " --tcpSrc " + str( tcpSrc ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1184 | if tcpDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1185 | cmd += " --tcpDst " + str( tcpDst ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1186 | if setEthSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1187 | cmd += " --setEthSrc " + str( setEthSrc ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1188 | if setEthDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1189 | cmd += " --setEthDst " + str( setEthDst ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1190 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1191 | # Check whether the user appended the port |
| 1192 | # or provided it as an input |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1193 | if "/" in ingress_device1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1194 | cmd += " " + str( ingress_device1 ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1195 | else: |
| 1196 | if not port_ingress1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1197 | main.log.error( "You must specify " + |
| 1198 | "the ingress port1" ) |
| 1199 | # TODO: perhaps more meaningful return |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1200 | return main.FALSE |
| 1201 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1202 | cmd += " " + \ |
| 1203 | str( ingress_device1 ) + "/" +\ |
| 1204 | str( port_ingress1 ) + " " |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1205 | |
| 1206 | if "/" in ingress_device2: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1207 | cmd += " " + str( ingress_device2 ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1208 | else: |
| 1209 | if not port_ingress2: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1210 | main.log.error( "You must specify " + |
| 1211 | "the ingress port2" ) |
| 1212 | # TODO: perhaps more meaningful return |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1213 | return main.FALSE |
| 1214 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1215 | cmd += " " + \ |
| 1216 | str( ingress_device2 ) + "/" +\ |
| 1217 | str( port_ingress2 ) + " " |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1218 | |
| 1219 | if "/" in egress_device: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1220 | cmd += " " + str( egress_device ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1221 | else: |
| 1222 | if not port_egress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1223 | main.log.error( "You must specify " + |
| 1224 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1225 | return main.FALSE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1226 | |
| 1227 | cmd += " " +\ |
| 1228 | str( egress_device ) + "/" +\ |
| 1229 | str( port_egress ) |
| 1230 | print "cmd= ", cmd |
| 1231 | self.handle.sendline( cmd ) |
| 1232 | |
| 1233 | main.log.info( cmd + " sent" ) |
| 1234 | i = self.handle.expect( [ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1235 | "Error", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1236 | "onos>" ] ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1237 | |
| 1238 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1239 | main.log.error( "Error in adding point-to-point intent" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1240 | return self.handle |
| 1241 | else: |
| 1242 | return main.TRUE |
| 1243 | |
| 1244 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1245 | main.log.error( self.name + ": EOF exception found" ) |
| 1246 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1247 | main.cleanup() |
| 1248 | main.exit() |
| 1249 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1250 | main.log.info( self.name + " ::::::" ) |
| 1251 | main.log.error( traceback.print_exc() ) |
| 1252 | main.log.info( self.name + " ::::::" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1253 | main.cleanup() |
| 1254 | main.exit() |
| 1255 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1256 | def remove_intent( self, intent_id ): |
| 1257 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1258 | Remove intent for specified intent id |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1259 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1260 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1261 | self.handle.sendline( "" ) |
| 1262 | self.handle.expect( "onos>" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1263 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1264 | self.handle.sendline( "remove-intent " + str( intent_id ) ) |
| 1265 | i = self.handle.expect( [ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1266 | "Error", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1267 | "onos>" ] ) |
| 1268 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1269 | handle = self.handle.before |
| 1270 | |
| 1271 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1272 | main.log.error( "Error in removing intent" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1273 | return handle |
| 1274 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1275 | return handle |
| 1276 | |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1277 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1278 | main.log.error( self.name + ": EOF exception found" ) |
| 1279 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1280 | main.cleanup() |
| 1281 | main.exit() |
| 1282 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1283 | main.log.info( self.name + " ::::::" ) |
| 1284 | main.log.error( traceback.print_exc() ) |
| 1285 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1286 | main.cleanup() |
| 1287 | main.exit() |
| 1288 | |
pingping-lin | dabe797 | 2014-11-17 19:29:44 -0800 | [diff] [blame] | 1289 | # This method should be used after installing application: onos-app-sdnip |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1290 | def routes( self, json_format=False ): |
| 1291 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1292 | Optional: |
| 1293 | * json_format: enable output formatting in json |
| 1294 | Description: |
| 1295 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1296 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1297 | try: |
| 1298 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1299 | self.handle.sendline( "routes -j" ) |
| 1300 | self.handle.expect( "routes -j" ) |
| 1301 | self.handle.expect( "onos>" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1302 | handle_tmp = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1303 | |
| 1304 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 1305 | handle = ansi_escape.sub( '', handle_tmp ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1306 | |
| 1307 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1308 | self.handle.sendline( "" ) |
| 1309 | self.handle.expect( "onos>" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1310 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1311 | self.handle.sendline( "routes" ) |
| 1312 | self.handle.expect( "onos>" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1313 | handle = self.handle.before |
| 1314 | |
| 1315 | return handle |
| 1316 | |
| 1317 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1318 | main.log.error( self.name + ": EOF exception found" ) |
| 1319 | main.log.error( self.name + ": " + self.handle.before ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1320 | main.cleanup() |
| 1321 | main.exit() |
| 1322 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1323 | main.log.info( self.name + " ::::::" ) |
| 1324 | main.log.error( traceback.print_exc() ) |
| 1325 | main.log.info( self.name + " ::::::" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1326 | main.cleanup() |
| 1327 | main.exit() |
| 1328 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1329 | def intents( self, json_format=True ): |
| 1330 | """ |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1331 | Optional: |
| 1332 | * json_format: enable output formatting in json |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1333 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1334 | Obtain intents currently installed |
| 1335 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1336 | try: |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1337 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1338 | self.handle.sendline( "intents -j" ) |
| 1339 | self.handle.expect( "intents -j" ) |
| 1340 | self.handle.expect( "onos>" ) |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1341 | handle = self.handle.before |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1342 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1343 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 1344 | handle = ansi_escape.sub( '', handle ) |
| 1345 | else: |
| 1346 | self.handle.sendline( "" ) |
| 1347 | self.handle.expect( "onos>" ) |
| 1348 | |
| 1349 | self.handle.sendline( "intents" ) |
| 1350 | self.handle.expect( "onos>" ) |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1351 | handle = self.handle.before |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1352 | |
| 1353 | return handle |
| 1354 | |
| 1355 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1356 | main.log.error( self.name + ": EOF exception found" ) |
| 1357 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1358 | main.cleanup() |
| 1359 | main.exit() |
| 1360 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1361 | main.log.info( self.name + " ::::::" ) |
| 1362 | main.log.error( traceback.print_exc() ) |
| 1363 | main.log.info( self.name + " ::::::" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1364 | main.cleanup() |
| 1365 | main.exit() |
| 1366 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1367 | def flows( self, json_format=True ): |
| 1368 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1369 | Optional: |
| 1370 | * json_format: enable output formatting in json |
| 1371 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1372 | Obtain flows currently installed |
| 1373 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1374 | try: |
| 1375 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1376 | self.handle.sendline( "flows -j" ) |
| 1377 | self.handle.expect( "flows -j" ) |
| 1378 | self.handle.expect( "onos>" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1379 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1380 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 1381 | handle = ansi_escape.sub( '', handle ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1382 | |
| 1383 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1384 | self.handle.sendline( "" ) |
| 1385 | self.handle.expect( "onos>" ) |
| 1386 | self.handle.sendline( "flows" ) |
| 1387 | self.handle.expect( "onos>" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1388 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1389 | if re.search( "Error\sexecuting\scommand:", handle ): |
| 1390 | main.log.error( |
| 1391 | self.name + ".flows() response: " + str( handle ) ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1392 | |
| 1393 | return handle |
| 1394 | |
| 1395 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1396 | main.log.error( self.name + ": EOF exception found" ) |
| 1397 | main.log.error( self.name + ": " + self.handle.before ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1398 | main.cleanup() |
| 1399 | main.exit() |
| 1400 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1401 | main.log.info( self.name + " ::::::" ) |
| 1402 | main.log.error( traceback.print_exc() ) |
| 1403 | main.log.info( self.name + " ::::::" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1404 | main.cleanup() |
| 1405 | main.exit() |
| 1406 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1407 | def push_test_intents( self, dpid_src, dpid_dst, num_intents, |
| 1408 | num_mult="", app_id="", report=True ): |
| 1409 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1410 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1411 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1412 | a specific point-to-point intent definition |
| 1413 | Required: |
| 1414 | * dpid_src: specify source dpid |
| 1415 | * dpid_dst: specify destination dpid |
| 1416 | * num_intents: specify number of intents to push |
| 1417 | Optional: |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1418 | * num_mult: number multiplier for multiplying |
| 1419 | the number of intents specified |
| 1420 | * app_id: specify the application id init to further |
| 1421 | modularize the intents |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1422 | * report: default True, returns latency information |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1423 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1424 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1425 | cmd = "push-test-intents " +\ |
| 1426 | str( dpid_src ) + " " + str( dpid_dst ) + " " +\ |
| 1427 | str( num_intents ) |
| 1428 | |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1429 | if num_mult: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1430 | cmd += " " + str( num_mult ) |
| 1431 | # If app id is specified, then num_mult |
| 1432 | # must exist because of the way this command |
| 1433 | # takes in arguments |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1434 | if app_id: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1435 | cmd += " " + str( app_id ) |
| 1436 | |
| 1437 | self.handle.sendline( cmd ) |
| 1438 | self.handle.expect( cmd ) |
| 1439 | self.handle.expect( "onos>" ) |
| 1440 | |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1441 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1442 | |
| 1443 | # Some color thing that we want to escape |
| 1444 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 1445 | handle = ansi_escape.sub( '', handle ) |
| 1446 | |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1447 | if report: |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1448 | lat_result = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1449 | main.log.info( handle ) |
| 1450 | # Split result by newline |
| 1451 | newline = handle.split( "\r\r\n" ) |
| 1452 | # Ignore the first object of list, which is empty |
| 1453 | newline = newline[ 1: ] |
| 1454 | # Some sloppy parsing method to get the latency |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1455 | for result in newline: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1456 | result = result.split( ": " ) |
| 1457 | # Append the first result of second parse |
| 1458 | lat_result.append( result[ 1 ].split( " " )[ 0 ] ) |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1459 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1460 | main.log.info( lat_result ) |
| 1461 | return lat_result |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1462 | else: |
| 1463 | return main.TRUE |
| 1464 | |
| 1465 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1466 | main.log.error( self.name + ": EOF exception found" ) |
| 1467 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1468 | main.cleanup() |
| 1469 | main.exit() |
| 1470 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1471 | main.log.info( self.name + " ::::::" ) |
| 1472 | main.log.error( traceback.print_exc() ) |
| 1473 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 1474 | main.cleanup() |
| 1475 | main.exit() |
| 1476 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1477 | def intents_events_metrics( self, json_format=True ): |
| 1478 | """ |
| 1479 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1480 | Optional: |
| 1481 | * json_format: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1482 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1483 | try: |
| 1484 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1485 | self.handle.sendline( "intents-events-metrics -j" ) |
| 1486 | self.handle.expect( "intents-events-metrics -j" ) |
| 1487 | self.handle.expect( "onos>" ) |
| 1488 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1489 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1490 | |
| 1491 | # Some color thing that we want to escape |
| 1492 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 1493 | handle = ansi_escape.sub( '', handle ) |
| 1494 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1495 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1496 | self.handle.sendline( "intents-events-metrics" ) |
| 1497 | self.handle.expect( "intents-events-metrics" ) |
| 1498 | self.handle.expect( "onos>" ) |
| 1499 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1500 | handle = self.handle.before |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1501 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1502 | return handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1503 | |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1504 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1505 | main.log.error( self.name + ": EOF exception found" ) |
| 1506 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1507 | main.cleanup() |
| 1508 | main.exit() |
| 1509 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1510 | main.log.info( self.name + " ::::::" ) |
| 1511 | main.log.error( traceback.print_exc() ) |
| 1512 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1513 | main.cleanup() |
| 1514 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1515 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1516 | def topology_events_metrics( self, json_format=True ): |
| 1517 | """ |
| 1518 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1519 | Optional: |
| 1520 | * json_format: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1521 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1522 | try: |
| 1523 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1524 | self.handle.sendline( "topology-events-metrics -j" ) |
| 1525 | self.handle.expect( "topology-events-metrics -j" ) |
| 1526 | self.handle.expect( "onos>" ) |
| 1527 | |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1528 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1529 | |
| 1530 | # Some color thing that we want to escape |
| 1531 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 1532 | handle = ansi_escape.sub( '', handle ) |
| 1533 | |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1534 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1535 | self.handle.sendline( "topology-events-metrics" ) |
| 1536 | self.handle.expect( "topology-events-metrics" ) |
| 1537 | self.handle.expect( "onos>" ) |
| 1538 | |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1539 | handle = self.handle.before |
| 1540 | |
| 1541 | return handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1542 | |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1543 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1544 | main.log.error( self.name + ": EOF exception found" ) |
| 1545 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1546 | main.cleanup() |
| 1547 | main.exit() |
| 1548 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1549 | main.log.info( self.name + " ::::::" ) |
| 1550 | main.log.error( traceback.print_exc() ) |
| 1551 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 1552 | main.cleanup() |
| 1553 | main.exit() |
| 1554 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1555 | # Wrapper functions **************** |
| 1556 | # Wrapper functions use existing driver |
| 1557 | # functions and extends their use case. |
| 1558 | # For example, we may use the output of |
| 1559 | # a normal driver function, and parse it |
| 1560 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 1561 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1562 | def get_all_intents_id( self ): |
| 1563 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1564 | Description: |
| 1565 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1566 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1567 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1568 | # Obtain output of intents function |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1569 | intents_str = self.intents() |
| 1570 | all_intent_list = [] |
| 1571 | intent_id_list = [] |
| 1572 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1573 | # Parse the intents output for ID's |
| 1574 | intents_list = [ s.strip() for s in intents_str.splitlines() ] |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1575 | for intents in intents_list: |
| 1576 | if "onos>" in intents: |
| 1577 | continue |
| 1578 | elif "intents" in intents: |
| 1579 | continue |
| 1580 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1581 | line_list = intents.split( " " ) |
| 1582 | all_intent_list.append( line_list[ 0 ] ) |
| 1583 | |
| 1584 | all_intent_list = all_intent_list[ 1:-2 ] |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1585 | |
| 1586 | for intents in all_intent_list: |
| 1587 | if not intents: |
| 1588 | continue |
| 1589 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1590 | intent_id_list.append( intents ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1591 | |
| 1592 | return intent_id_list |
| 1593 | |
| 1594 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1595 | main.log.error( self.name + ": EOF exception found" ) |
| 1596 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1597 | main.cleanup() |
| 1598 | main.exit() |
| 1599 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1600 | main.log.info( self.name + " ::::::" ) |
| 1601 | main.log.error( traceback.print_exc() ) |
| 1602 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1603 | main.cleanup() |
| 1604 | main.exit() |
| 1605 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1606 | def get_all_devices_id( self ): |
| 1607 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1608 | Use 'devices' function to obtain list of all devices |
| 1609 | and parse the result to obtain a list of all device |
| 1610 | id's. Returns this list. Returns empty list if no |
| 1611 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1612 | List is ordered sequentially |
| 1613 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1614 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1615 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1616 | the ids. By obtaining the list of device ids on the fly, |
| 1617 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1618 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1619 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1620 | # Call devices and store result string |
| 1621 | devices_str = self.devices( json_format=False ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1622 | id_list = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1623 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1624 | if not devices_str: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1625 | main.log.info( "There are no devices to get id from" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1626 | return id_list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1627 | |
| 1628 | # Split the string into list by comma |
| 1629 | device_list = devices_str.split( "," ) |
| 1630 | # Get temporary list of all arguments with string 'id=' |
| 1631 | temp_list = [ dev for dev in device_list if "id=" in dev ] |
| 1632 | # Split list further into arguments before and after string |
| 1633 | # 'id='. Get the latter portion ( the actual device id ) and |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1634 | # append to id_list |
| 1635 | for arg in temp_list: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1636 | id_list.append( arg.split( "id=" )[ 1 ] ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1637 | return id_list |
| 1638 | |
| 1639 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1640 | main.log.error( self.name + ": EOF exception found" ) |
| 1641 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1642 | main.cleanup() |
| 1643 | main.exit() |
| 1644 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1645 | main.log.info( self.name + " ::::::" ) |
| 1646 | main.log.error( traceback.print_exc() ) |
| 1647 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1648 | main.cleanup() |
| 1649 | main.exit() |
| 1650 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1651 | def get_all_nodes_id( self ): |
| 1652 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1653 | Uses 'nodes' function to obtain list of all nodes |
| 1654 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1655 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1656 | Returns: |
| 1657 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1658 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1659 | try: |
| 1660 | nodes_str = self.nodes() |
| 1661 | id_list = [] |
| 1662 | |
| 1663 | if not nodes_str: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1664 | main.log.info( "There are no nodes to get id from" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1665 | return id_list |
| 1666 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1667 | # Sample nodes_str output |
| 1668 | # id=local, address=127.0.0.1:9876, state=ACTIVE * |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1669 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1670 | # Split the string into list by comma |
| 1671 | nodes_list = nodes_str.split( "," ) |
| 1672 | temp_list = [ node for node in nodes_list if "id=" in node ] |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1673 | for arg in temp_list: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1674 | id_list.append( arg.split( "id=" )[ 1 ] ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1675 | |
| 1676 | return id_list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1677 | |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1678 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1679 | main.log.error( self.name + ": EOF exception found" ) |
| 1680 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1681 | main.cleanup() |
| 1682 | main.exit() |
| 1683 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1684 | main.log.info( self.name + " ::::::" ) |
| 1685 | main.log.error( traceback.print_exc() ) |
| 1686 | main.log.info( self.name + " ::::::" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1687 | main.cleanup() |
| 1688 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 1689 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1690 | def get_device( self, dpid=None ): |
| 1691 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1692 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 1693 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1694 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1695 | import json |
| 1696 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1697 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1698 | return None |
| 1699 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1700 | dpid = dpid.replace( ':', '' ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1701 | raw_devices = self.devices() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1702 | devices_json = json.loads( raw_devices ) |
| 1703 | # search json for the device with dpid then return the device |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1704 | for device in devices_json: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1705 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 1706 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1707 | return device |
| 1708 | return None |
| 1709 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1710 | main.log.error( self.name + ": EOF exception found" ) |
| 1711 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1712 | main.cleanup() |
| 1713 | main.exit() |
| 1714 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1715 | main.log.info( self.name + " ::::::" ) |
| 1716 | main.log.error( traceback.print_exc() ) |
| 1717 | main.log.info( self.name + " ::::::" ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 1718 | main.cleanup() |
| 1719 | main.exit() |
| 1720 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1721 | def check_status( self, ip, numoswitch, numolink, log_level="info" ): |
| 1722 | """ |
| 1723 | Checks the number of swithes & links that ONOS sees against the |
| 1724 | supplied values. By default this will report to main.log, but the |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1725 | log level can be specifid. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1726 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1727 | Params: ip = ip used for the onos cli |
| 1728 | numoswitch = expected number of switches |
| 1729 | numlink = expected number of links |
| 1730 | log_level = level to log to. Currently accepts 'info', 'warn' and 'report' |
| 1731 | |
| 1732 | |
| 1733 | log_level can |
| 1734 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1735 | Returns: main.TRUE if the number of switchs and links are correct, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1736 | main.FALSE if the numer of switches and links is incorrect, |
| 1737 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1738 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1739 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1740 | topology = self.get_topology( ip ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1741 | if topology == {}: |
| 1742 | return main.ERROR |
| 1743 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1744 | # Is the number of switches is what we expected |
| 1745 | devices = topology.get( 'devices', False ) |
| 1746 | links = topology.get( 'links', False ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1747 | if devices == False or links == False: |
| 1748 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1749 | switch_check = ( int( devices ) == int( numoswitch ) ) |
| 1750 | # Is the number of links is what we expected |
| 1751 | link_check = ( int( links ) == int( numolink ) ) |
| 1752 | if ( switch_check and link_check ): |
| 1753 | # We expected the correct numbers |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1754 | output = output + "The number of links and switches match "\ |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1755 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1756 | result = main.TRUE |
| 1757 | else: |
| 1758 | output = output + \ |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1759 | "The number of links and switches does not match what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1760 | result = main.FALSE |
| 1761 | output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\ |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1762 | % ( int( devices ), int( numoswitch ), int( links ), int( numolink ) ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1763 | if log_level == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1764 | main.log.report( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1765 | elif log_level == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1766 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1767 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1768 | main.log.info( output ) |
| 1769 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1770 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1771 | main.log.error( self.name + ": EOF exception found" ) |
| 1772 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1773 | main.cleanup() |
| 1774 | main.exit() |
| 1775 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1776 | main.log.info( self.name + " ::::::" ) |
| 1777 | main.log.error( traceback.print_exc() ) |
| 1778 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1779 | main.cleanup() |
| 1780 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1781 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1782 | def device_role( self, device_id, onos_node, role="master" ): |
| 1783 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1784 | Calls the device-role cli command. |
| 1785 | device_id must be the id of a device as seen in the onos devices command |
| 1786 | onos_node is the ip of one of the onos nodes in the cluster |
| 1787 | role must be either master, standby, or none |
| 1788 | |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1789 | Returns main.TRUE or main.FALSE based on argument verification. |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1790 | When device-role supports errors this should be extended to |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1791 | support that output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1792 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1793 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1794 | # print "beginning device_role... \n\tdevice_id:" + device_id |
| 1795 | # print "\tonos_node: " + onos_node |
| 1796 | # print "\trole: "+ role |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1797 | if role.lower() == "master" or \ |
| 1798 | role.lower() == "standby" or \ |
| 1799 | role.lower() == "none": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1800 | self.handle.sendline( "" ) |
| 1801 | self.handle.expect( "onos>" ) |
| 1802 | self.handle.sendline( "device-role " + |
| 1803 | str( device_id ) + " " + |
| 1804 | str( onos_node ) + " " + |
| 1805 | str( role ) ) |
| 1806 | i = self.handle.expect( [ "Error", "onos>" ] ) |
| 1807 | if i == 0: |
| 1808 | output = str( self.handle.before ) |
| 1809 | self.handle.expect( "onos>" ) |
| 1810 | output = output + str( self.handle.before ) |
| 1811 | main.log.error( self.name + ": " + |
| 1812 | output + '\033[0m' ) # end color output to escape any colours from the cli |
| 1813 | return main.ERROR |
| 1814 | self.handle.sendline( "" ) |
| 1815 | self.handle.expect( "onos>" ) |
| 1816 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1817 | else: |
| 1818 | return main.FALSE |
| 1819 | |
| 1820 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1821 | main.log.error( self.name + ": EOF exception found" ) |
| 1822 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1823 | main.cleanup() |
| 1824 | main.exit() |
| 1825 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1826 | main.log.info( self.name + " ::::::" ) |
| 1827 | main.log.error( traceback.print_exc() ) |
| 1828 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 1829 | main.cleanup() |
| 1830 | main.exit() |
| 1831 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1832 | def clusters( self, json_format=True ): |
| 1833 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1834 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1835 | Optional argument: |
| 1836 | * json_format - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1837 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1838 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1839 | self.handle.sendline( "" ) |
| 1840 | self.handle.expect( "onos>" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1841 | |
| 1842 | if json_format: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1843 | self.handle.sendline( "clusters -j" ) |
| 1844 | self.handle.expect( "clusters -j" ) |
| 1845 | self.handle.expect( "onos>" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1846 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1847 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1848 | handle variable here contains some ANSI escape color code |
| 1849 | sequences at the end which are invisible in the print command |
| 1850 | output. To make that escape sequence visible, use repr() function. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1851 | The repr( handle ) output when printed shows the ANSI escape sequences. |
| 1852 | In json.loads( somestring ), this somestring variable is actually |
| 1853 | repr( somestring ) and json.loads would fail with the escape sequence. |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1854 | So we take off that escape sequence using |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1855 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 1856 | handle1 = ansi_escape.sub( '', handle ) |
| 1857 | """ |
| 1858 | # print "repr(handle) =", repr( handle ) |
| 1859 | ansi_escape = re.compile( r'\r\r\n\x1b[^m]*m' ) |
| 1860 | handle1 = ansi_escape.sub( '', handle ) |
| 1861 | # print "repr(handle1) = ", repr( handle1 ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1862 | return handle1 |
| 1863 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1864 | self.handle.sendline( "clusters" ) |
| 1865 | self.handle.expect( "onos>" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1866 | handle = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1867 | # print "handle =",handle |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1868 | return handle |
| 1869 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1870 | main.log.error( self.name + ": EOF exception found" ) |
| 1871 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1872 | main.cleanup() |
| 1873 | main.exit() |
| 1874 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1875 | main.log.info( self.name + " ::::::" ) |
| 1876 | main.log.error( traceback.print_exc() ) |
| 1877 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 1878 | main.cleanup() |
| 1879 | main.exit() |
| 1880 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1881 | def election_test_leader( self ): |
| 1882 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1883 | * CLI command to get the current leader for the Election test application. |
| 1884 | #NOTE: Requires installation of the onos-app-election feature |
| 1885 | Returns: Node IP of the leader if one exists |
| 1886 | None if none exists |
| 1887 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1888 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1889 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1890 | self.handle.sendline( "election-test-leader" ) |
| 1891 | self.handle.expect( "election-test-leader" ) |
| 1892 | self.handle.expect( "onos>" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1893 | response = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1894 | # Leader |
| 1895 | node_search = re.search( |
| 1896 | "The\scurrent\sleader\sfor\sthe\sElection\sapp\sis\s(?P<node>.+)\.", |
| 1897 | response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1898 | if node_search: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1899 | node = node_search.group( 'node' ) |
| 1900 | main.log.info( "Election-test-leader on " + str( |
| 1901 | self.name ) + " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1902 | return node |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1903 | # no leader |
| 1904 | null_search = re.search( |
| 1905 | "There\sis\scurrently\sno\sleader\selected\sfor\sthe\sElection\sapp", |
| 1906 | response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1907 | if null_search: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1908 | main.log.info( |
| 1909 | "Election-test-leader found no leader on " + |
| 1910 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1911 | return None |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1912 | # error |
| 1913 | if re.search( "Command\snot\sfound", response ): |
| 1914 | main.log.error( "Election app is not loaded on " + self.name ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1915 | return main.FALSE |
| 1916 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1917 | main.log.error( |
| 1918 | "Error in election_test_leader: unexpected response" ) |
| 1919 | main.log.error( repr( response ) ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1920 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1921 | |
| 1922 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1923 | main.log.error( self.name + ": EOF exception found" ) |
| 1924 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1925 | main.cleanup() |
| 1926 | main.exit() |
| 1927 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1928 | main.log.info( self.name + " ::::::" ) |
| 1929 | main.log.error( traceback.print_exc() ) |
| 1930 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1931 | main.cleanup() |
| 1932 | main.exit() |
| 1933 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1934 | def election_test_run( self ): |
| 1935 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1936 | * CLI command to run for leadership of the Election test application. |
| 1937 | #NOTE: Requires installation of the onos-app-election feature |
| 1938 | Returns: Main.TRUE on success |
| 1939 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1940 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1941 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1942 | self.handle.sendline( "election-test-run" ) |
| 1943 | self.handle.expect( "election-test-run" ) |
| 1944 | self.handle.expect( "onos>" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1945 | response = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1946 | # success |
| 1947 | search = re.search( |
| 1948 | "Entering\sleadership\selections\sfor\sthe\sElection\sapp.", |
| 1949 | response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1950 | if search: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1951 | main.log.info( |
| 1952 | self.name + |
| 1953 | " entering leadership elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1954 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1955 | # error |
| 1956 | if re.search( "Command\snot\sfound", response ): |
| 1957 | main.log.error( "Election app is not loaded on " + self.name ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1958 | return main.FALSE |
| 1959 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1960 | main.log.error( |
| 1961 | "Error in election_test_run: unexpected response" ) |
| 1962 | main.log.error( repr( response ) ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 1963 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1964 | |
| 1965 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1966 | main.log.error( self.name + ": EOF exception found" ) |
| 1967 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1968 | main.cleanup() |
| 1969 | main.exit() |
| 1970 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1971 | main.log.info( self.name + " ::::::" ) |
| 1972 | main.log.error( traceback.print_exc() ) |
| 1973 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1974 | main.cleanup() |
| 1975 | main.exit() |
| 1976 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1977 | def election_test_withdraw( self ): |
| 1978 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1979 | * CLI command to withdraw the local node from leadership election for |
| 1980 | * the Election test application. |
| 1981 | #NOTE: Requires installation of the onos-app-election feature |
| 1982 | Returns: Main.TRUE on success |
| 1983 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1984 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1985 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1986 | self.handle.sendline( "election-test-withdraw" ) |
| 1987 | self.handle.expect( "election-test-withdraw" ) |
| 1988 | self.handle.expect( "onos>" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1989 | response = self.handle.before |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1990 | # success |
| 1991 | search = re.search( |
| 1992 | "Withdrawing\sfrom\sleadership\selections\sfor\sthe\sElection\sapp.", |
| 1993 | response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1994 | if search: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1995 | main.log.info( |
| 1996 | self.name + |
| 1997 | " withdrawing from leadership elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1998 | return main.TRUE |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1999 | # error |
| 2000 | if re.search( "Command\snot\sfound", response ): |
| 2001 | main.log.error( "Election app is not loaded on " + self.name ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 2002 | return main.FALSE |
| 2003 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2004 | main.log.error( |
| 2005 | "Error in election_test_withdraw: unexpected response" ) |
| 2006 | main.log.error( repr( response ) ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 2007 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2008 | |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2009 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2010 | main.log.error( self.name + ": EOF exception found" ) |
| 2011 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2012 | main.cleanup() |
| 2013 | main.exit() |
| 2014 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2015 | main.log.info( self.name + " ::::::" ) |
| 2016 | main.log.error( traceback.print_exc() ) |
| 2017 | main.log.info( self.name + " ::::::" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2018 | main.cleanup() |
| 2019 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2020 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2021 | #*********************************** |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2022 | def getDevicePortsEnabledCount( self, dpid ): |
| 2023 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2024 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2025 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2026 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2027 | dpid = str( dpid ) |
| 2028 | self.handle.sendline( "" ) |
| 2029 | self.handle.expect( "onos>" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2030 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2031 | self.handle.sendline( "onos:ports -e " + dpid + " | wc -l" ) |
| 2032 | i = self.handle.expect( [ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2033 | "No such device", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2034 | "onos>" ] ) |
| 2035 | |
| 2036 | # self.handle.sendline( "" ) |
| 2037 | # self.handle.expect( "onos>" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2038 | |
| 2039 | output = self.handle.before |
| 2040 | |
| 2041 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2042 | main.log.error( "Error in getting ports" ) |
| 2043 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2044 | else: |
| 2045 | result = output |
| 2046 | return result |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2047 | |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2048 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2049 | main.log.error( self.name + ": EOF exception found" ) |
| 2050 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2051 | main.cleanup() |
| 2052 | main.exit() |
| 2053 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2054 | main.log.info( self.name + " ::::::" ) |
| 2055 | main.log.error( traceback.print_exc() ) |
| 2056 | main.log.info( self.name + " ::::::" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2057 | main.cleanup() |
| 2058 | main.exit() |
| 2059 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2060 | def getDeviceLinksActiveCount( self, dpid ): |
| 2061 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2062 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2063 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2064 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2065 | dpid = str( dpid ) |
| 2066 | self.handle.sendline( "" ) |
| 2067 | self.handle.expect( "onos>" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2068 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2069 | self.handle.sendline( |
| 2070 | "onos:links " + |
| 2071 | dpid + |
| 2072 | " | grep ACTIVE | wc -l" ) |
| 2073 | i = self.handle.expect( [ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2074 | "No such device", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2075 | "onos>" ] ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2076 | |
| 2077 | output = self.handle.before |
| 2078 | |
| 2079 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2080 | main.log.error( "Error in getting ports" ) |
| 2081 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2082 | else: |
| 2083 | result = output |
| 2084 | return result |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2085 | |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2086 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2087 | main.log.error( self.name + ": EOF exception found" ) |
| 2088 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2089 | main.cleanup() |
| 2090 | main.exit() |
| 2091 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2092 | main.log.info( self.name + " ::::::" ) |
| 2093 | main.log.error( traceback.print_exc() ) |
| 2094 | main.log.info( self.name + " ::::::" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2095 | main.cleanup() |
| 2096 | main.exit() |
| 2097 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2098 | def getAllIntentIds( self ): |
| 2099 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2100 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2101 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2102 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2103 | self.handle.sendline( "" ) |
| 2104 | self.handle.expect( "onos>" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2105 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2106 | self.handle.sendline( "onos:intents | grep id=" ) |
| 2107 | i = self.handle.expect( [ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2108 | "Error", |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2109 | "onos>" ] ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2110 | |
| 2111 | output = self.handle.before |
| 2112 | |
| 2113 | if i == 0: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2114 | main.log.error( "Error in getting ports" ) |
| 2115 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2116 | else: |
| 2117 | result = output |
| 2118 | return result |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2119 | |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2120 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2121 | main.log.error( self.name + ": EOF exception found" ) |
| 2122 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2123 | main.cleanup() |
| 2124 | main.exit() |
| 2125 | except: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2126 | main.log.info( self.name + " ::::::" ) |
| 2127 | main.log.error( traceback.print_exc() ) |
| 2128 | main.log.info( self.name + " ::::::" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2129 | main.cleanup() |
| 2130 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2131 | |