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 pexpect |
| 20 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 21 | import json |
| 22 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 23 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 24 | import os |
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 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 34 | self.name = None |
| 35 | self.home = None |
| 36 | self.handle = None |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 37 | super( CLI, self ).__init__() |
| 38 | |
| 39 | def connect( self, **connectargs ): |
| 40 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 41 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 42 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 43 | try: |
| 44 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 45 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 46 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 47 | for key in self.options: |
| 48 | if key == "home": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 49 | self.home = self.options[ 'home' ] |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 50 | break |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 51 | if self.home is None or self.home == "": |
Jon Hall | e94919c | 2015-03-23 11:42:57 -0700 | [diff] [blame] | 52 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 53 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 54 | for key in self.options: |
| 55 | if key == 'onosIp': |
| 56 | self.onosIp = self.options[ 'onosIp' ] |
| 57 | break |
| 58 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 59 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 60 | |
| 61 | try: |
| 62 | if os.getenv( str( self.ip_address ) ) != None: |
| 63 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 64 | else: |
| 65 | main.log.info( self.name + |
| 66 | ": Trying to connect to " + |
| 67 | self.ip_address ) |
| 68 | |
| 69 | except KeyError: |
| 70 | main.log.info( "Invalid host name," + |
| 71 | " connecting to local host instead" ) |
| 72 | self.ip_address = 'localhost' |
| 73 | except Exception as inst: |
| 74 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 75 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 77 | user_name=self.user_name, |
| 78 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 79 | port=self.port, |
| 80 | pwd=self.pwd, |
| 81 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 82 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 83 | self.handle.sendline( "cd " + self.home ) |
| 84 | self.handle.expect( "\$" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 85 | if self.handle: |
| 86 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 87 | else: |
| 88 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 89 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 90 | except TypeError: |
| 91 | main.log.exception( self.name + ": Object not as expected" ) |
| 92 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 93 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | main.log.error( self.name + ": EOF exception found" ) |
| 95 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 96 | main.cleanup() |
| 97 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 98 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 99 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 100 | main.cleanup() |
| 101 | main.exit() |
| 102 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 103 | def disconnect( self ): |
| 104 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 105 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 106 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 107 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 108 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 109 | if self.handle: |
| 110 | i = self.logout() |
| 111 | if i == main.TRUE: |
| 112 | self.handle.sendline( "" ) |
| 113 | self.handle.expect( "\$" ) |
| 114 | self.handle.sendline( "exit" ) |
| 115 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 116 | except TypeError: |
| 117 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 118 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 119 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 120 | main.log.error( self.name + ": EOF exception found" ) |
| 121 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 122 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 123 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 124 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 125 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 126 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 127 | response = main.FALSE |
| 128 | return response |
| 129 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 130 | def logout( self ): |
| 131 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 132 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 133 | Returns main.TRUE if exited CLI and |
| 134 | main.FALSE on timeout (not guranteed you are disconnected) |
| 135 | None on TypeError |
| 136 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 137 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 138 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 139 | if self.handle: |
| 140 | self.handle.sendline( "" ) |
| 141 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ], |
| 142 | timeout=10 ) |
| 143 | if i == 0: # In ONOS CLI |
| 144 | self.handle.sendline( "logout" ) |
| 145 | self.handle.expect( "\$" ) |
| 146 | return main.TRUE |
| 147 | elif i == 1: # not in CLI |
| 148 | return main.TRUE |
| 149 | elif i == 3: # Timeout |
| 150 | return main.FALSE |
| 151 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 152 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 153 | except TypeError: |
| 154 | main.log.exception( self.name + ": Object not as expected" ) |
| 155 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 156 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 157 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 158 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 159 | main.cleanup() |
| 160 | main.exit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 161 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 162 | main.log.error( self.name + |
| 163 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 164 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 165 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 166 | main.cleanup() |
| 167 | main.exit() |
| 168 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 169 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 170 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 171 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 172 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 173 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 174 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 175 | try: |
| 176 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 177 | main.log.error( "Must define cellname" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 178 | main.cleanup() |
| 179 | main.exit() |
| 180 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 181 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 182 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 183 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 184 | # and that this driver will have to change accordingly |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 185 | self.handle.expect(str(cellname)) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 186 | handleBefore = self.handle.before |
| 187 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 188 | # Get the rest of the handle |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 189 | self.handle.sendline("") |
| 190 | self.handle.expect("\$") |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 191 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 192 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 193 | main.log.info( "Cell call returned: " + handleBefore + |
| 194 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 195 | |
| 196 | return main.TRUE |
| 197 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 198 | except TypeError: |
| 199 | main.log.exception( self.name + ": Object not as expected" ) |
| 200 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 201 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 202 | main.log.error( self.name + ": eof exception found" ) |
| 203 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 204 | main.cleanup() |
| 205 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 206 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 207 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 208 | main.cleanup() |
| 209 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 210 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 211 | def startOnosCli( self, ONOSIp, karafTimeout="", |
| 212 | commandlineTimeout=10, onosStartTimeout=60 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 213 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 214 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 215 | by user would be used to set the current karaf shell idle timeout. |
| 216 | Note that when ever this property is modified the shell will exit and |
Hari Krishna | d7b9c20 | 2015-01-05 10:38:14 -0800 | [diff] [blame] | 217 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 218 | Below is an example to start a session with 60 seconds idle timeout |
| 219 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 220 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 221 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 222 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 223 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 224 | Note: karafTimeout is left as str so that this could be read |
| 225 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 226 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 227 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 228 | self.handle.sendline( "" ) |
| 229 | x = self.handle.expect( [ |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 230 | "\$", "onos>" ], commandlineTimeout) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 231 | |
| 232 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 233 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 234 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 235 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 236 | # Wait for onos start ( -w ) and enter onos cli |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 237 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 238 | i = self.handle.expect( [ |
| 239 | "onos>", |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 240 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 241 | |
| 242 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 243 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 244 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 245 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 246 | "config:property-set -p org.apache.karaf.shell\ |
| 247 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 248 | karafTimeout ) |
| 249 | self.handle.expect( "\$" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 250 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 251 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 252 | return main.TRUE |
| 253 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 254 | # If failed, send ctrl+c to process and try again |
| 255 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 256 | self.handle.send( "\x03" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 257 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 258 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 259 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 260 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 261 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 262 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 263 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 264 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 265 | "config:property-set -p org.apache.karaf.shell\ |
| 266 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | karafTimeout ) |
| 268 | self.handle.expect( "\$" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 269 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 270 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 271 | return main.TRUE |
| 272 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 273 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 274 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 275 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 276 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 277 | except TypeError: |
| 278 | main.log.exception( self.name + ": Object not as expected" ) |
| 279 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 280 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 281 | main.log.error( self.name + ": EOF exception found" ) |
| 282 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 283 | main.cleanup() |
| 284 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 285 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 286 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 287 | main.cleanup() |
| 288 | main.exit() |
| 289 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 290 | def log( self, cmdStr, level="" ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 291 | """ |
| 292 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 293 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 294 | returns main.FALSE if Error occurred |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 295 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 296 | Level defaults to INFO |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 297 | """ |
| 298 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 299 | lvlStr = "" |
| 300 | if level: |
| 301 | lvlStr = "--level=" + level |
| 302 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 303 | self.handle.sendline( "" ) |
Jon Hall | c9eabec | 2015-06-10 14:33:14 -0700 | [diff] [blame] | 304 | i = self.handle.expect( [ "onos>","\$", pexpect.TIMEOUT ] ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 305 | if i == 1: |
Jon Hall | c9eabec | 2015-06-10 14:33:14 -0700 | [diff] [blame] | 306 | main.log.error( self.name + ": onos cli session closed." ) |
| 307 | main.cleanup() |
| 308 | main.exit() |
| 309 | if i == 2: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 310 | self.handle.sendline( "" ) |
| 311 | self.handle.expect( "onos>" ) |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 312 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 313 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 314 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 315 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 316 | response = self.handle.before |
| 317 | if re.search( "Error", response ): |
| 318 | return main.FALSE |
| 319 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 320 | except pexpect.TIMEOUT: |
| 321 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 322 | main.cleanup() |
| 323 | main.exit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 324 | except pexpect.EOF: |
| 325 | main.log.error( self.name + ": EOF exception found" ) |
| 326 | main.log.error( self.name + ": " + self.handle.before ) |
| 327 | main.cleanup() |
| 328 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 329 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 330 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 331 | main.cleanup() |
| 332 | main.exit() |
| 333 | |
GlennRC | 208d4af | 2015-11-24 12:30:00 -0800 | [diff] [blame] | 334 | def sendline( self, cmdStr, showResponse=False, debug=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 335 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 336 | Send a completely user specified string to |
| 337 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 338 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 339 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 340 | Warning: There are no sanity checking to commands |
| 341 | sent using this method. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 342 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 343 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 344 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
| 345 | self.log( logStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 346 | self.handle.sendline( cmdStr ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 347 | i = self.handle.expect( ["onos>", "\$", pexpect.TIMEOUT] ) |
| 348 | response = self.handle.before |
| 349 | if i == 2: |
| 350 | self.handle.sendline() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 351 | self.handle.expect( ["\$", pexpect.TIMEOUT] ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 352 | response += self.handle.before |
| 353 | print response |
| 354 | try: |
| 355 | print self.handle.after |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 356 | except TypeError: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 357 | pass |
| 358 | # TODO: do something with i |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 359 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 360 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 361 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 362 | main.log.debug( self.name + ": Raw output" ) |
| 363 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 364 | |
| 365 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 366 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 367 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 368 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 369 | main.log.debug( self.name + ": ansiEscape output" ) |
| 370 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 371 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 372 | # Remove extra return chars that get added |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 373 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 374 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 375 | main.log.debug( self.name + ": Removed extra returns " + |
| 376 | "from output" ) |
| 377 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 378 | |
| 379 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 380 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 381 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 382 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 383 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 384 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 385 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 386 | output = response.split( cmdStr.strip(), 1 ) |
| 387 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 388 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 389 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 390 | main.log.debug( self.name + ": " + repr( r ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 391 | output = output[1].strip() |
| 392 | if showResponse: |
| 393 | main.log.info( "Resonse from ONOS: {}".format( output ) ) |
| 394 | return output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 395 | except IndexError: |
| 396 | main.log.exception( self.name + ": Object not as expected" ) |
| 397 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 398 | except TypeError: |
| 399 | main.log.exception( self.name + ": Object not as expected" ) |
| 400 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 401 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 402 | main.log.error( self.name + ": EOF exception found" ) |
| 403 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 404 | main.cleanup() |
| 405 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 406 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 407 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 408 | main.cleanup() |
| 409 | main.exit() |
| 410 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 411 | # IMPORTANT NOTE: |
| 412 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 413 | # the cli command changing 'a:b' with 'aB'. |
| 414 | # Ex ) onos:topology > onosTopology |
| 415 | # onos:links > onosLinks |
| 416 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 417 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 418 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 419 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 420 | Adds a new cluster node by ID and address information. |
| 421 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 422 | * nodeId |
| 423 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 424 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 425 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 426 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 427 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 428 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 429 | str( ONOSIp ) + " " + str( tcpPort ) |
| 430 | handle = self.sendline( cmdStr ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 431 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 432 | main.log.error( "Error in adding node" ) |
| 433 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 434 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 435 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 436 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 437 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 438 | except TypeError: |
| 439 | main.log.exception( self.name + ": Object not as expected" ) |
| 440 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 441 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 442 | main.log.error( self.name + ": EOF exception found" ) |
| 443 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 444 | main.cleanup() |
| 445 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 446 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 447 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 448 | main.cleanup() |
| 449 | main.exit() |
| 450 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 451 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 452 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 453 | Removes a cluster by ID |
| 454 | Issues command: 'remove-node [<node-id>]' |
| 455 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 456 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 457 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 458 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 459 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 460 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 461 | handle = self.sendline( cmdStr ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 462 | if re.search( "Error", handle ): |
| 463 | main.log.error( "Error in removing node" ) |
| 464 | main.log.error( handle ) |
| 465 | return main.FALSE |
| 466 | else: |
| 467 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 468 | except TypeError: |
| 469 | main.log.exception( self.name + ": Object not as expected" ) |
| 470 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 471 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 472 | main.log.error( self.name + ": EOF exception found" ) |
| 473 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 474 | main.cleanup() |
| 475 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 476 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 477 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 478 | main.cleanup() |
| 479 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 480 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 481 | def nodes( self, jsonFormat=True): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 482 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 483 | List the nodes currently visible |
| 484 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 485 | Optional argument: |
| 486 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 487 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 488 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 489 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 490 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 491 | cmdStr += " -j" |
| 492 | output = self.sendline( cmdStr ) |
| 493 | return output |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 494 | except TypeError: |
| 495 | main.log.exception( self.name + ": Object not as expected" ) |
| 496 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 497 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 498 | main.log.error( self.name + ": EOF exception found" ) |
| 499 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 500 | main.cleanup() |
| 501 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 502 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 503 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 504 | main.cleanup() |
| 505 | main.exit() |
| 506 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 507 | def topology( self ): |
| 508 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 509 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 510 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 511 | Return: |
| 512 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 513 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 514 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 515 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 516 | handle = self.sendline( cmdStr ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 517 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 518 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 519 | except TypeError: |
| 520 | main.log.exception( self.name + ": Object not as expected" ) |
| 521 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 522 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 523 | main.log.error( self.name + ": EOF exception found" ) |
| 524 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 525 | main.cleanup() |
| 526 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 527 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 528 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 529 | main.cleanup() |
| 530 | main.exit() |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 531 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 532 | def featureInstall( self, featureStr ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 533 | """ |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 534 | Installs a specified feature by issuing command: |
| 535 | 'feature:install <feature_str>' |
| 536 | NOTE: This is now deprecated, you should use the activateApp method |
| 537 | instead |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 538 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 539 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 540 | cmdStr = "feature:install " + str( featureStr ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 541 | handle = self.sendline( cmdStr ) |
| 542 | if re.search( "Error", handle ): |
| 543 | main.log.error( "Error in installing feature" ) |
| 544 | main.log.error( handle ) |
| 545 | return main.FALSE |
| 546 | else: |
| 547 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 548 | except TypeError: |
| 549 | main.log.exception( self.name + ": Object not as expected" ) |
| 550 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 551 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 552 | main.log.error( self.name + ": EOF exception found" ) |
| 553 | main.log.error( self.name + ": " + self.handle.before ) |
| 554 | main.log.report( "Failed to install feature" ) |
| 555 | main.log.report( "Exiting test" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 556 | main.cleanup() |
| 557 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 558 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 559 | main.log.exception( self.name + ": Uncaught exception!" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 560 | main.log.report( "Failed to install feature" ) |
| 561 | main.log.report( "Exiting test" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 562 | main.cleanup() |
| 563 | main.exit() |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 564 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 565 | def featureUninstall( self, featureStr ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 566 | """ |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 567 | Uninstalls a specified feature by issuing command: |
| 568 | 'feature:uninstall <feature_str>' |
| 569 | NOTE: This is now deprecated, you should use the deactivateApp method |
| 570 | instead |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 571 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 572 | try: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 573 | cmdStr = 'feature:list -i | grep "' + featureStr + '"' |
| 574 | handle = self.sendline( cmdStr ) |
| 575 | if handle != '': |
| 576 | cmdStr = "feature:uninstall " + str( featureStr ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 577 | output = self.sendline( cmdStr ) |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 578 | # TODO: Check for possible error responses from karaf |
| 579 | else: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 580 | main.log.info( "Feature needs to be installed before " + |
| 581 | "uninstalling it" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 582 | return main.TRUE |
| 583 | if re.search( "Error", output ): |
| 584 | main.log.error( "Error in uninstalling feature" ) |
| 585 | main.log.error( output ) |
| 586 | return main.FALSE |
| 587 | else: |
| 588 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 589 | except TypeError: |
| 590 | main.log.exception( self.name + ": Object not as expected" ) |
| 591 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -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 ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 595 | main.cleanup() |
| 596 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 597 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 598 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 599 | main.cleanup() |
| 600 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 601 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 602 | def deviceRemove( self, deviceId ): |
| 603 | """ |
| 604 | Removes particular device from storage |
| 605 | |
| 606 | TODO: refactor this function |
| 607 | """ |
| 608 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 609 | cmdStr = "device-remove " + str( deviceId ) |
| 610 | handle = self.sendline( cmdStr ) |
| 611 | if re.search( "Error", handle ): |
| 612 | main.log.error( "Error in removing device" ) |
| 613 | main.log.error( handle ) |
| 614 | return main.FALSE |
| 615 | else: |
| 616 | return main.TRUE |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 617 | except TypeError: |
| 618 | main.log.exception( self.name + ": Object not as expected" ) |
| 619 | return None |
| 620 | except pexpect.EOF: |
| 621 | main.log.error( self.name + ": EOF exception found" ) |
| 622 | main.log.error( self.name + ": " + self.handle.before ) |
| 623 | main.cleanup() |
| 624 | main.exit() |
| 625 | except Exception: |
| 626 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 627 | main.cleanup() |
| 628 | main.exit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 629 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 630 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 631 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 632 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 633 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 634 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 635 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 636 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 637 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 638 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 639 | cmdStr += " -j" |
| 640 | handle = self.sendline( cmdStr ) |
| 641 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 642 | except TypeError: |
| 643 | main.log.exception( self.name + ": Object not as expected" ) |
| 644 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 645 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 646 | main.log.error( self.name + ": EOF exception found" ) |
| 647 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 648 | main.cleanup() |
| 649 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 650 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 651 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 652 | main.cleanup() |
| 653 | main.exit() |
| 654 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 655 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 656 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 657 | This balances the devices across all controllers |
| 658 | by issuing command: 'onos> onos:balance-masters' |
| 659 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 660 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 661 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 662 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 663 | handle = self.sendline( cmdStr ) |
| 664 | if re.search( "Error", handle ): |
| 665 | main.log.error( "Error in balancing masters" ) |
| 666 | main.log.error( handle ) |
| 667 | return main.FALSE |
| 668 | else: |
| 669 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 670 | except TypeError: |
| 671 | main.log.exception( self.name + ": Object not as expected" ) |
| 672 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 673 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 674 | main.log.error( self.name + ": EOF exception found" ) |
| 675 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 676 | main.cleanup() |
| 677 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 678 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 679 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 680 | main.cleanup() |
| 681 | main.exit() |
| 682 | |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 683 | def checkMasters( self,jsonFormat=True ): |
| 684 | """ |
| 685 | Returns the output of the masters command. |
| 686 | Optional argument: |
| 687 | * jsonFormat - boolean indicating if you want output in json |
| 688 | """ |
| 689 | try: |
| 690 | cmdStr = "onos:masters" |
| 691 | if jsonFormat: |
| 692 | cmdStr += " -j" |
| 693 | output = self.sendline( cmdStr ) |
| 694 | return output |
| 695 | except TypeError: |
| 696 | main.log.exception( self.name + ": Object not as expected" ) |
| 697 | return None |
| 698 | except pexpect.EOF: |
| 699 | main.log.error( self.name + ": EOF exception found" ) |
| 700 | main.log.error( self.name + ": " + self.handle.before ) |
| 701 | main.cleanup() |
| 702 | main.exit() |
| 703 | except Exception: |
| 704 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 705 | main.cleanup() |
| 706 | main.exit() |
| 707 | |
| 708 | def checkBalanceMasters( self,jsonFormat=True ): |
| 709 | """ |
| 710 | Uses the master command to check that the devices' leadership |
| 711 | is evenly divided |
| 712 | |
| 713 | Dependencies: checkMasters() and summary() |
| 714 | |
| 715 | Returns main.True if the devices are balanced |
| 716 | Returns main.False if the devices are unbalanced |
| 717 | Exits on Exception |
| 718 | Returns None on TypeError |
| 719 | """ |
| 720 | try: |
| 721 | totalDevices = json.loads( self.summary() )[ "devices" ] |
| 722 | totalOwnedDevices = 0 |
| 723 | masters = json.loads( self.checkMasters() ) |
| 724 | first = masters[ 0 ][ "size" ] |
| 725 | for master in masters: |
| 726 | totalOwnedDevices += master[ "size" ] |
| 727 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 728 | main.log.error( "Mastership not balanced" ) |
| 729 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 730 | return main.FALSE |
| 731 | main.log.info( "Mastership balanced between " \ |
| 732 | + str( len(masters) ) + " masters" ) |
| 733 | return main.TRUE |
| 734 | except TypeError: |
| 735 | main.log.exception( self.name + ": Object not as expected" ) |
| 736 | return None |
| 737 | except pexpect.EOF: |
| 738 | main.log.error( self.name + ": EOF exception found" ) |
| 739 | main.log.error( self.name + ": " + self.handle.before ) |
| 740 | main.cleanup() |
| 741 | main.exit() |
| 742 | except Exception: |
| 743 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 744 | main.cleanup() |
| 745 | main.exit() |
| 746 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 747 | def links( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 748 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 749 | Lists all core links |
| 750 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 751 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 752 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 753 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 754 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 755 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 756 | cmdStr += " -j" |
| 757 | handle = self.sendline( cmdStr ) |
| 758 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 759 | except TypeError: |
| 760 | main.log.exception( self.name + ": Object not as expected" ) |
| 761 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 762 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 763 | main.log.error( self.name + ": EOF exception found" ) |
| 764 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 765 | main.cleanup() |
| 766 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 767 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 768 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 769 | main.cleanup() |
| 770 | main.exit() |
| 771 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 772 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 773 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 774 | Lists all ports |
| 775 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 776 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 777 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 778 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 779 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 780 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 781 | cmdStr += " -j" |
| 782 | handle = self.sendline( cmdStr ) |
| 783 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 784 | except TypeError: |
| 785 | main.log.exception( self.name + ": Object not as expected" ) |
| 786 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 787 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 788 | main.log.error( self.name + ": EOF exception found" ) |
| 789 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 790 | main.cleanup() |
| 791 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 792 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 793 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 794 | main.cleanup() |
| 795 | main.exit() |
| 796 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 797 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 798 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 799 | Lists all devices and the controllers with roles assigned to them |
| 800 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 801 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 802 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 803 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 804 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 805 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 806 | cmdStr += " -j" |
| 807 | handle = self.sendline( cmdStr ) |
| 808 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 809 | except TypeError: |
| 810 | main.log.exception( self.name + ": Object not as expected" ) |
| 811 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 812 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 813 | main.log.error( self.name + ": EOF exception found" ) |
| 814 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 815 | main.cleanup() |
| 816 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 817 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 818 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 819 | main.cleanup() |
| 820 | main.exit() |
| 821 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 822 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 823 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 824 | Given the a string containing the json representation of the "roles" |
| 825 | cli command and a partial or whole device id, returns a json object |
| 826 | containing the roles output for the first device whose id contains |
| 827 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 828 | |
| 829 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 830 | A dict of the role assignments for the given device or |
| 831 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 832 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 833 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 834 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 835 | return None |
| 836 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 837 | rawRoles = self.roles() |
| 838 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 839 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 840 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 841 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 842 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 843 | return device |
| 844 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 845 | except TypeError: |
| 846 | main.log.exception( self.name + ": Object not as expected" ) |
| 847 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 848 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 849 | main.log.error( self.name + ": EOF exception found" ) |
| 850 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 851 | main.cleanup() |
| 852 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 853 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 854 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 855 | main.cleanup() |
| 856 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 857 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 858 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 859 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 860 | Iterates through each device and checks if there is a master assigned |
| 861 | Returns: main.TRUE if each device has a master |
| 862 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 863 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 864 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 865 | rawRoles = self.roles() |
| 866 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 867 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 868 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 869 | # print device |
| 870 | if device[ 'master' ] == "none": |
| 871 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 872 | return main.FALSE |
| 873 | return main.TRUE |
| 874 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 875 | except TypeError: |
| 876 | main.log.exception( self.name + ": Object not as expected" ) |
| 877 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 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 | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 881 | main.cleanup() |
| 882 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 883 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 884 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 885 | main.cleanup() |
| 886 | main.exit() |
| 887 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 888 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 889 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 890 | Returns string of paths, and the cost. |
| 891 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 892 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 893 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 894 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 895 | handle = self.sendline( cmdStr ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 896 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 897 | main.log.error( "Error in getting paths" ) |
| 898 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 899 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 900 | path = handle.split( ";" )[ 0 ] |
| 901 | cost = handle.split( ";" )[ 1 ] |
| 902 | return ( path, cost ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 903 | except TypeError: |
| 904 | main.log.exception( self.name + ": Object not as expected" ) |
| 905 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 906 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 907 | main.log.error( self.name + ": EOF exception found" ) |
| 908 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 909 | main.cleanup() |
| 910 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 911 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 912 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 913 | main.cleanup() |
| 914 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 915 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 916 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 917 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 918 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 919 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 920 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 921 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 922 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 923 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 924 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 925 | cmdStr += " -j" |
| 926 | handle = self.sendline( cmdStr ) |
| 927 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 928 | except TypeError: |
| 929 | main.log.exception( self.name + ": Object not as expected" ) |
| 930 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 931 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 932 | main.log.error( self.name + ": EOF exception found" ) |
| 933 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 934 | main.cleanup() |
| 935 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 936 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 937 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 938 | main.cleanup() |
| 939 | main.exit() |
| 940 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 941 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 942 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 943 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 944 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 945 | Note: mac must be a colon separated mac address, but could be a |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 946 | partial mac address |
| 947 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 948 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 949 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 950 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 951 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 952 | return None |
| 953 | else: |
| 954 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 955 | rawHosts = self.hosts() |
| 956 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 957 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 958 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 959 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 960 | if not host: |
| 961 | pass |
| 962 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 963 | return host |
| 964 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 965 | except TypeError: |
| 966 | main.log.exception( self.name + ": Object not as expected" ) |
| 967 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 968 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 969 | main.log.error( self.name + ": EOF exception found" ) |
| 970 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 971 | main.cleanup() |
| 972 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 973 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 974 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 975 | main.cleanup() |
| 976 | main.exit() |
| 977 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 978 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 979 | """ |
| 980 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 981 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 982 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 983 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 984 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 985 | IMPORTANT: |
| 986 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 987 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 988 | Furthermore, it assumes that value of VLAN is '-1' |
| 989 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 990 | Converts mininet hosts ( h1, h2, h3... ) into |
| 991 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 992 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 993 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 994 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 995 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 996 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 997 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 998 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 999 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1000 | i = iter( str( hostHex ) ) |
| 1001 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1002 | hostHex = hostHex + "/-1" |
| 1003 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1004 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1005 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1006 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1007 | except TypeError: |
| 1008 | main.log.exception( self.name + ": Object not as expected" ) |
| 1009 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1010 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1011 | main.log.error( self.name + ": EOF exception found" ) |
| 1012 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1013 | main.cleanup() |
| 1014 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1015 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1016 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1017 | main.cleanup() |
| 1018 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1019 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1020 | def addHostIntent( self, hostIdOne, hostIdTwo ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1021 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1022 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1023 | * hostIdOne: ONOS host id for host1 |
| 1024 | * hostIdTwo: ONOS host id for host2 |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1025 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1026 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1027 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1028 | Returns: |
| 1029 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1030 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1031 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1032 | cmdStr = "add-host-intent " + str( hostIdOne ) +\ |
| 1033 | " " + str( hostIdTwo ) |
| 1034 | handle = self.sendline( cmdStr ) |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1035 | if re.search( "Error", handle ): |
| 1036 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1037 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1038 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1039 | else: |
| 1040 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1041 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
| 1042 | match = re.search('id=0x([\da-f]+),', handle) |
| 1043 | if match: |
| 1044 | return match.group()[3:-1] |
| 1045 | else: |
| 1046 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1047 | main.log.debug( "Response from ONOS was: " + |
| 1048 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1049 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1050 | except TypeError: |
| 1051 | main.log.exception( self.name + ": Object not as expected" ) |
| 1052 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1053 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1054 | main.log.error( self.name + ": EOF exception found" ) |
| 1055 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1056 | main.cleanup() |
| 1057 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1058 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1059 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1060 | main.cleanup() |
| 1061 | main.exit() |
| 1062 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1063 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1064 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1065 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1066 | * ingressDevice: device id of ingress device |
| 1067 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1068 | Optional: |
| 1069 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1070 | Description: |
| 1071 | Adds an optical intent by specifying an ingress and egress device |
| 1072 | Returns: |
| 1073 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1074 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1075 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1076 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1077 | " " + str( egressDevice ) |
| 1078 | handle = self.sendline( cmdStr ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1079 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1080 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1081 | main.log.error( "Error in adding Optical intent" ) |
| 1082 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1083 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1084 | main.log.info( "Optical intent installed between " + |
| 1085 | str( ingressDevice ) + " and " + |
| 1086 | str( egressDevice ) ) |
| 1087 | match = re.search('id=0x([\da-f]+),', handle) |
| 1088 | if match: |
| 1089 | return match.group()[3:-1] |
| 1090 | else: |
| 1091 | main.log.error( "Error, intent ID not found" ) |
| 1092 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1093 | except TypeError: |
| 1094 | main.log.exception( self.name + ": Object not as expected" ) |
| 1095 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1096 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1097 | main.log.error( self.name + ": EOF exception found" ) |
| 1098 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1099 | main.cleanup() |
| 1100 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1101 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1102 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1103 | main.cleanup() |
| 1104 | main.exit() |
| 1105 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1106 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1107 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1108 | ingressDevice, |
| 1109 | egressDevice, |
| 1110 | portIngress="", |
| 1111 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1112 | ethType="", |
| 1113 | ethSrc="", |
| 1114 | ethDst="", |
| 1115 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1116 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1117 | ipProto="", |
| 1118 | ipSrc="", |
| 1119 | ipDst="", |
| 1120 | tcpSrc="", |
| 1121 | tcpDst="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1122 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1123 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1124 | * ingressDevice: device id of ingress device |
| 1125 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1126 | Optional: |
| 1127 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1128 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1129 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1130 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1131 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1132 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1133 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1134 | * ipSrc: specify ip source address |
| 1135 | * ipDst: specify ip destination address |
| 1136 | * tcpSrc: specify tcp source port |
| 1137 | * tcpDst: specify tcp destination port |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1138 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1139 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1140 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1141 | Returns: |
| 1142 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1143 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1144 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1145 | options developers provide for point-to-point |
| 1146 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1147 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1148 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1149 | # If there are no optional arguments |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1150 | if not ethType and not ethSrc and not ethDst\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1151 | and not bandwidth and not lambdaAlloc \ |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1152 | and not ipProto and not ipSrc and not ipDst \ |
| 1153 | and not tcpSrc and not tcpDst: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1154 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1155 | |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1156 | else: |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1157 | cmd = "add-point-intent" |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1158 | |
andrewonlab | 0c0a677 | 2014-10-22 12:31:18 -0400 | [diff] [blame] | 1159 | if ethType: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1160 | cmd += " --ethType " + str( ethType ) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1161 | if ethSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1162 | cmd += " --ethSrc " + str( ethSrc ) |
| 1163 | if ethDst: |
| 1164 | cmd += " --ethDst " + str( ethDst ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1165 | if bandwidth: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1166 | cmd += " --bandwidth " + str( bandwidth ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1167 | if lambdaAlloc: |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1168 | cmd += " --lambda " |
| 1169 | if ipProto: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1170 | cmd += " --ipProto " + str( ipProto ) |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1171 | if ipSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1172 | cmd += " --ipSrc " + str( ipSrc ) |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1173 | if ipDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1174 | cmd += " --ipDst " + str( ipDst ) |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1175 | if tcpSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1176 | cmd += " --tcpSrc " + str( tcpSrc ) |
andrewonlab | fa4ff50 | 2014-11-11 16:41:30 -0500 | [diff] [blame] | 1177 | if tcpDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1178 | cmd += " --tcpDst " + str( tcpDst ) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1179 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1180 | # Check whether the user appended the port |
| 1181 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1182 | if "/" in ingressDevice: |
| 1183 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1184 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1185 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1186 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1187 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1188 | # Would it make sense to throw an exception and exit |
| 1189 | # the test? |
| 1190 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1191 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1192 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1193 | str( ingressDevice ) + "/" +\ |
| 1194 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1195 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1196 | if "/" in egressDevice: |
| 1197 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1198 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1199 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1200 | main.log.error( "You must specify the egress port" ) |
| 1201 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1202 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1203 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1204 | str( egressDevice ) + "/" +\ |
| 1205 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1206 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1207 | handle = self.sendline( cmd ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1208 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1209 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1210 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1211 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1212 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1213 | # TODO: print out all the options in this message? |
| 1214 | main.log.info( "Point-to-point intent installed between " + |
| 1215 | str( ingressDevice ) + " and " + |
| 1216 | str( egressDevice ) ) |
| 1217 | match = re.search('id=0x([\da-f]+),', handle) |
| 1218 | if match: |
| 1219 | return match.group()[3:-1] |
| 1220 | else: |
| 1221 | main.log.error( "Error, intent ID not found" ) |
| 1222 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1223 | except TypeError: |
| 1224 | main.log.exception( self.name + ": Object not as expected" ) |
| 1225 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1226 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1227 | main.log.error( self.name + ": EOF exception found" ) |
| 1228 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1229 | main.cleanup() |
| 1230 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1231 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1232 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1233 | main.cleanup() |
| 1234 | main.exit() |
| 1235 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1236 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1237 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1238 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1239 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1240 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1241 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1242 | ethType="", |
| 1243 | ethSrc="", |
| 1244 | ethDst="", |
| 1245 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1246 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1247 | ipProto="", |
| 1248 | ipSrc="", |
| 1249 | ipDst="", |
| 1250 | tcpSrc="", |
| 1251 | tcpDst="", |
| 1252 | setEthSrc="", |
| 1253 | setEthDst="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1254 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1255 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1256 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1257 | is same. That is, all ingress devices include port numbers |
| 1258 | with a "/" or all ingress devices could specify device |
| 1259 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1260 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1261 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1262 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1263 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1264 | Optional: |
| 1265 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1266 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1267 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1268 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1269 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1270 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1271 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1272 | * ipSrc: specify ip source address |
| 1273 | * ipDst: specify ip destination address |
| 1274 | * tcpSrc: specify tcp source port |
| 1275 | * tcpDst: specify tcp destination port |
| 1276 | * setEthSrc: action to Rewrite Source MAC Address |
| 1277 | * setEthDst: action to Rewrite Destination MAC Address |
| 1278 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1279 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1280 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1281 | Returns: |
| 1282 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1283 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1284 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1285 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1286 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1287 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1288 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1289 | # If there are no optional arguments |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1290 | if not ethType and not ethSrc and not ethDst\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1291 | and not bandwidth and not lambdaAlloc\ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1292 | and not ipProto and not ipSrc and not ipDst\ |
| 1293 | and not tcpSrc and not tcpDst and not setEthSrc\ |
| 1294 | and not setEthDst: |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1295 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1296 | |
| 1297 | else: |
| 1298 | cmd = "add-multi-to-single-intent" |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1299 | |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1300 | if ethType: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1301 | cmd += " --ethType " + str( ethType ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1302 | if ethSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1303 | cmd += " --ethSrc " + str( ethSrc ) |
| 1304 | if ethDst: |
| 1305 | cmd += " --ethDst " + str( ethDst ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1306 | if bandwidth: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1307 | cmd += " --bandwidth " + str( bandwidth ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1308 | if lambdaAlloc: |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1309 | cmd += " --lambda " |
| 1310 | if ipProto: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1311 | cmd += " --ipProto " + str( ipProto ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1312 | if ipSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1313 | cmd += " --ipSrc " + str( ipSrc ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1314 | if ipDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1315 | cmd += " --ipDst " + str( ipDst ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1316 | if tcpSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1317 | cmd += " --tcpSrc " + str( tcpSrc ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1318 | if tcpDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1319 | cmd += " --tcpDst " + str( tcpDst ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1320 | if setEthSrc: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1321 | cmd += " --setEthSrc " + str( setEthSrc ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1322 | if setEthDst: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1323 | cmd += " --setEthDst " + str( setEthDst ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1324 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1325 | # Check whether the user appended the port |
| 1326 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1327 | |
| 1328 | if portIngressList is None: |
| 1329 | for ingressDevice in ingressDeviceList: |
| 1330 | if "/" in ingressDevice: |
| 1331 | cmd += " " + str( ingressDevice ) |
| 1332 | else: |
| 1333 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1334 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1335 | # TODO: perhaps more meaningful return |
| 1336 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1337 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1338 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1339 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1340 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1341 | cmd += " " + \ |
| 1342 | str( ingressDevice ) + "/" +\ |
| 1343 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1344 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1345 | main.log.error( "Device list and port list does not " + |
| 1346 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1347 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1348 | if "/" in egressDevice: |
| 1349 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1350 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1351 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1352 | main.log.error( "You must specify " + |
| 1353 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1354 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1355 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1356 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1357 | str( egressDevice ) + "/" +\ |
| 1358 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1359 | handle = self.sendline( cmd ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1360 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1361 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1362 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1363 | "intent" ) |
| 1364 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1365 | else: |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1366 | match = re.search('id=0x([\da-f]+),', handle) |
| 1367 | if match: |
| 1368 | return match.group()[3:-1] |
| 1369 | else: |
| 1370 | main.log.error( "Error, intent ID not found" ) |
| 1371 | return None |
| 1372 | except TypeError: |
| 1373 | main.log.exception( self.name + ": Object not as expected" ) |
| 1374 | return None |
| 1375 | except pexpect.EOF: |
| 1376 | main.log.error( self.name + ": EOF exception found" ) |
| 1377 | main.log.error( self.name + ": " + self.handle.before ) |
| 1378 | main.cleanup() |
| 1379 | main.exit() |
| 1380 | except Exception: |
| 1381 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1382 | main.cleanup() |
| 1383 | main.exit() |
| 1384 | |
| 1385 | def addSinglepointToMultipointIntent( |
| 1386 | self, |
| 1387 | ingressDevice, |
| 1388 | egressDeviceList, |
| 1389 | portIngress="", |
| 1390 | portEgressList=None, |
| 1391 | ethType="", |
| 1392 | ethSrc="", |
| 1393 | ethDst="", |
| 1394 | bandwidth="", |
| 1395 | lambdaAlloc=False, |
| 1396 | ipProto="", |
| 1397 | ipSrc="", |
| 1398 | ipDst="", |
| 1399 | tcpSrc="", |
| 1400 | tcpDst="", |
| 1401 | setEthSrc="", |
| 1402 | setEthDst="" ): |
| 1403 | """ |
| 1404 | Note: |
| 1405 | This function assumes the format of all egress devices |
| 1406 | is same. That is, all egress devices include port numbers |
| 1407 | with a "/" or all egress devices could specify device |
| 1408 | ids and port numbers seperately. |
| 1409 | Required: |
| 1410 | * EgressDeviceList: List of device ids of egress device |
| 1411 | ( Atleast 2 eress devices required in the list ) |
| 1412 | * ingressDevice: device id of ingress device |
| 1413 | Optional: |
| 1414 | * ethType: specify ethType |
| 1415 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1416 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1417 | * bandwidth: specify bandwidth capacity of link |
| 1418 | * lambdaAlloc: if True, intent will allocate lambda |
| 1419 | for the specified intent |
| 1420 | * ipProto: specify ip protocol |
| 1421 | * ipSrc: specify ip source address |
| 1422 | * ipDst: specify ip destination address |
| 1423 | * tcpSrc: specify tcp source port |
| 1424 | * tcpDst: specify tcp destination port |
| 1425 | * setEthSrc: action to Rewrite Source MAC Address |
| 1426 | * setEthDst: action to Rewrite Destination MAC Address |
| 1427 | Description: |
| 1428 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1429 | specifying device id's and optional fields |
| 1430 | Returns: |
| 1431 | A string of the intent id or None on error |
| 1432 | |
| 1433 | NOTE: This function may change depending on the |
| 1434 | options developers provide for singlepoint-to-multipoint |
| 1435 | intent via cli |
| 1436 | """ |
| 1437 | try: |
| 1438 | # If there are no optional arguments |
| 1439 | if not ethType and not ethSrc and not ethDst\ |
| 1440 | and not bandwidth and not lambdaAlloc\ |
| 1441 | and not ipProto and not ipSrc and not ipDst\ |
| 1442 | and not tcpSrc and not tcpDst and not setEthSrc\ |
| 1443 | and not setEthDst: |
| 1444 | cmd = "add-single-to-multi-intent" |
| 1445 | |
| 1446 | else: |
| 1447 | cmd = "add-single-to-multi-intent" |
| 1448 | |
| 1449 | if ethType: |
| 1450 | cmd += " --ethType " + str( ethType ) |
| 1451 | if ethSrc: |
| 1452 | cmd += " --ethSrc " + str( ethSrc ) |
| 1453 | if ethDst: |
| 1454 | cmd += " --ethDst " + str( ethDst ) |
| 1455 | if bandwidth: |
| 1456 | cmd += " --bandwidth " + str( bandwidth ) |
| 1457 | if lambdaAlloc: |
| 1458 | cmd += " --lambda " |
| 1459 | if ipProto: |
| 1460 | cmd += " --ipProto " + str( ipProto ) |
| 1461 | if ipSrc: |
| 1462 | cmd += " --ipSrc " + str( ipSrc ) |
| 1463 | if ipDst: |
| 1464 | cmd += " --ipDst " + str( ipDst ) |
| 1465 | if tcpSrc: |
| 1466 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1467 | if tcpDst: |
| 1468 | cmd += " --tcpDst " + str( tcpDst ) |
| 1469 | if setEthSrc: |
| 1470 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1471 | if setEthDst: |
| 1472 | cmd += " --setEthDst " + str( setEthDst ) |
| 1473 | |
| 1474 | # Check whether the user appended the port |
| 1475 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1476 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1477 | if "/" in ingressDevice: |
| 1478 | cmd += " " + str( ingressDevice ) |
| 1479 | else: |
| 1480 | if not portIngress: |
| 1481 | main.log.error( "You must specify " + |
| 1482 | "the Ingress port" ) |
| 1483 | return main.FALSE |
| 1484 | |
| 1485 | cmd += " " +\ |
| 1486 | str( ingressDevice ) + "/" +\ |
| 1487 | str( portIngress ) |
| 1488 | |
| 1489 | if portEgressList is None: |
| 1490 | for egressDevice in egressDeviceList: |
| 1491 | if "/" in egressDevice: |
| 1492 | cmd += " " + str( egressDevice ) |
| 1493 | else: |
| 1494 | main.log.error( "You must specify " + |
| 1495 | "the egress port" ) |
| 1496 | # TODO: perhaps more meaningful return |
| 1497 | return main.FALSE |
| 1498 | else: |
| 1499 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1500 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1501 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1502 | cmd += " " + \ |
| 1503 | str( egressDevice ) + "/" +\ |
| 1504 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1505 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1506 | main.log.error( "Device list and port list does not " + |
| 1507 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1508 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1509 | handle = self.sendline( cmd ) |
| 1510 | # If error, return error message |
| 1511 | if re.search( "Error", handle ): |
| 1512 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1513 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1514 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1515 | else: |
| 1516 | match = re.search('id=0x([\da-f]+),', handle) |
| 1517 | if match: |
| 1518 | return match.group()[3:-1] |
| 1519 | else: |
| 1520 | main.log.error( "Error, intent ID not found" ) |
| 1521 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1522 | except TypeError: |
| 1523 | main.log.exception( self.name + ": Object not as expected" ) |
| 1524 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1525 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1526 | main.log.error( self.name + ": EOF exception found" ) |
| 1527 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1528 | main.cleanup() |
| 1529 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1530 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1531 | main.log.exception( self.name + ": Uncaught exception!" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1532 | main.cleanup() |
| 1533 | main.exit() |
| 1534 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1535 | def addMplsIntent( |
| 1536 | self, |
| 1537 | ingressDevice, |
| 1538 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1539 | ingressPort="", |
| 1540 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1541 | ethType="", |
| 1542 | ethSrc="", |
| 1543 | ethDst="", |
| 1544 | bandwidth="", |
| 1545 | lambdaAlloc=False, |
| 1546 | ipProto="", |
| 1547 | ipSrc="", |
| 1548 | ipDst="", |
| 1549 | tcpSrc="", |
| 1550 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1551 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1552 | egressLabel="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1553 | priority=""): |
| 1554 | """ |
| 1555 | Required: |
| 1556 | * ingressDevice: device id of ingress device |
| 1557 | * egressDevice: device id of egress device |
| 1558 | Optional: |
| 1559 | * ethType: specify ethType |
| 1560 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1561 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1562 | * bandwidth: specify bandwidth capacity of link |
| 1563 | * lambdaAlloc: if True, intent will allocate lambda |
| 1564 | for the specified intent |
| 1565 | * ipProto: specify ip protocol |
| 1566 | * ipSrc: specify ip source address |
| 1567 | * ipDst: specify ip destination address |
| 1568 | * tcpSrc: specify tcp source port |
| 1569 | * tcpDst: specify tcp destination port |
| 1570 | * ingressLabel: Ingress MPLS label |
| 1571 | * egressLabel: Egress MPLS label |
| 1572 | Description: |
| 1573 | Adds MPLS intent by |
| 1574 | specifying device id's and optional fields |
| 1575 | Returns: |
| 1576 | A string of the intent id or None on error |
| 1577 | |
| 1578 | NOTE: This function may change depending on the |
| 1579 | options developers provide for MPLS |
| 1580 | intent via cli |
| 1581 | """ |
| 1582 | try: |
| 1583 | # If there are no optional arguments |
| 1584 | if not ethType and not ethSrc and not ethDst\ |
| 1585 | and not bandwidth and not lambdaAlloc \ |
| 1586 | and not ipProto and not ipSrc and not ipDst \ |
| 1587 | and not tcpSrc and not tcpDst and not ingressLabel \ |
| 1588 | and not egressLabel: |
| 1589 | cmd = "add-mpls-intent" |
| 1590 | |
| 1591 | else: |
| 1592 | cmd = "add-mpls-intent" |
| 1593 | |
| 1594 | if ethType: |
| 1595 | cmd += " --ethType " + str( ethType ) |
| 1596 | if ethSrc: |
| 1597 | cmd += " --ethSrc " + str( ethSrc ) |
| 1598 | if ethDst: |
| 1599 | cmd += " --ethDst " + str( ethDst ) |
| 1600 | if bandwidth: |
| 1601 | cmd += " --bandwidth " + str( bandwidth ) |
| 1602 | if lambdaAlloc: |
| 1603 | cmd += " --lambda " |
| 1604 | if ipProto: |
| 1605 | cmd += " --ipProto " + str( ipProto ) |
| 1606 | if ipSrc: |
| 1607 | cmd += " --ipSrc " + str( ipSrc ) |
| 1608 | if ipDst: |
| 1609 | cmd += " --ipDst " + str( ipDst ) |
| 1610 | if tcpSrc: |
| 1611 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1612 | if tcpDst: |
| 1613 | cmd += " --tcpDst " + str( tcpDst ) |
| 1614 | if ingressLabel: |
| 1615 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1616 | if egressLabel: |
| 1617 | cmd += " --egressLabel " + str( egressLabel ) |
| 1618 | if priority: |
| 1619 | cmd += " --priority " + str( priority ) |
| 1620 | |
| 1621 | # Check whether the user appended the port |
| 1622 | # or provided it as an input |
| 1623 | if "/" in ingressDevice: |
| 1624 | cmd += " " + str( ingressDevice ) |
| 1625 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1626 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1627 | main.log.error( "You must specify the ingress port" ) |
| 1628 | return None |
| 1629 | |
| 1630 | cmd += " " + \ |
| 1631 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1632 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1633 | |
| 1634 | if "/" in egressDevice: |
| 1635 | cmd += " " + str( egressDevice ) |
| 1636 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1637 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1638 | main.log.error( "You must specify the egress port" ) |
| 1639 | return None |
| 1640 | |
| 1641 | cmd += " " +\ |
| 1642 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1643 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1644 | |
| 1645 | handle = self.sendline( cmd ) |
| 1646 | # If error, return error message |
| 1647 | if re.search( "Error", handle ): |
| 1648 | main.log.error( "Error in adding mpls intent" ) |
| 1649 | return None |
| 1650 | else: |
| 1651 | # TODO: print out all the options in this message? |
| 1652 | main.log.info( "MPLS intent installed between " + |
| 1653 | str( ingressDevice ) + " and " + |
| 1654 | str( egressDevice ) ) |
| 1655 | match = re.search('id=0x([\da-f]+),', handle) |
| 1656 | if match: |
| 1657 | return match.group()[3:-1] |
| 1658 | else: |
| 1659 | main.log.error( "Error, intent ID not found" ) |
| 1660 | return None |
| 1661 | except TypeError: |
| 1662 | main.log.exception( self.name + ": Object not as expected" ) |
| 1663 | return None |
| 1664 | except pexpect.EOF: |
| 1665 | main.log.error( self.name + ": EOF exception found" ) |
| 1666 | main.log.error( self.name + ": " + self.handle.before ) |
| 1667 | main.cleanup() |
| 1668 | main.exit() |
| 1669 | except Exception: |
| 1670 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1671 | main.cleanup() |
| 1672 | main.exit() |
| 1673 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1674 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1675 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1676 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1677 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1678 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1679 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1680 | -p or --purge: Purge the intent from the store after removal |
| 1681 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1682 | Returns: |
| 1683 | main.False on error and |
| 1684 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1685 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1686 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1687 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1688 | if purge: |
| 1689 | cmdStr += " -p" |
| 1690 | if sync: |
| 1691 | cmdStr += " -s" |
| 1692 | |
| 1693 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1694 | handle = self.sendline( cmdStr ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1695 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1696 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1697 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1698 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1699 | # TODO: Should this be main.TRUE |
| 1700 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1701 | except TypeError: |
| 1702 | main.log.exception( self.name + ": Object not as expected" ) |
| 1703 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1704 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1705 | main.log.error( self.name + ": EOF exception found" ) |
| 1706 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1707 | main.cleanup() |
| 1708 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1709 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1710 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1711 | main.cleanup() |
| 1712 | main.exit() |
| 1713 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 1714 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1715 | """ |
| 1716 | Purges all WITHDRAWN Intents |
| 1717 | """ |
| 1718 | try: |
| 1719 | cmdStr = "purge-intents" |
| 1720 | handle = self.sendline( cmdStr ) |
| 1721 | if re.search( "Error", handle ): |
| 1722 | main.log.error( "Error in purging intents" ) |
| 1723 | return main.FALSE |
| 1724 | else: |
| 1725 | return main.TRUE |
| 1726 | except TypeError: |
| 1727 | main.log.exception( self.name + ": Object not as expected" ) |
| 1728 | return None |
| 1729 | except pexpect.EOF: |
| 1730 | main.log.error( self.name + ": EOF exception found" ) |
| 1731 | main.log.error( self.name + ": " + self.handle.before ) |
| 1732 | main.cleanup() |
| 1733 | main.exit() |
| 1734 | except Exception: |
| 1735 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1736 | main.cleanup() |
| 1737 | main.exit() |
| 1738 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1739 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1740 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1741 | NOTE: This method should be used after installing application: |
| 1742 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1743 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1744 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1745 | Description: |
| 1746 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1747 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1748 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1749 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1750 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1751 | cmdStr += " -j" |
| 1752 | handle = self.sendline( cmdStr ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1753 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1754 | except TypeError: |
| 1755 | main.log.exception( self.name + ": Object not as expected" ) |
| 1756 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1757 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1758 | main.log.error( self.name + ": EOF exception found" ) |
| 1759 | main.log.error( self.name + ": " + self.handle.before ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1760 | main.cleanup() |
| 1761 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1762 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1763 | main.log.exception( self.name + ": Uncaught exception!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1764 | main.cleanup() |
| 1765 | main.exit() |
| 1766 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 1767 | def ipv4RouteNumber( self ): |
| 1768 | """ |
| 1769 | NOTE: This method should be used after installing application: |
| 1770 | onos-app-sdnip |
| 1771 | Description: |
| 1772 | Obtain the total IPv4 routes number in the system |
| 1773 | """ |
| 1774 | try: |
| 1775 | cmdStr = "routes -s -j" |
| 1776 | handle = self.sendline( cmdStr ) |
| 1777 | jsonResult = json.loads( handle ) |
| 1778 | return jsonResult['totalRoutes4'] |
| 1779 | |
| 1780 | except TypeError: |
| 1781 | main.log.exception( self.name + ": Object not as expected" ) |
| 1782 | return None |
| 1783 | except pexpect.EOF: |
| 1784 | main.log.error( self.name + ": EOF exception found" ) |
| 1785 | main.log.error( self.name + ": " + self.handle.before ) |
| 1786 | main.cleanup() |
| 1787 | main.exit() |
| 1788 | except Exception: |
| 1789 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1790 | main.cleanup() |
| 1791 | main.exit() |
| 1792 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1793 | def intents( self, jsonFormat = True, summary = False, **intentargs): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1794 | """ |
andrewonlab | 377693f | 2014-10-21 16:00:30 -0400 | [diff] [blame] | 1795 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1796 | * jsonFormat: enable output formatting in json |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1797 | * summary: whether only output the intent summary |
| 1798 | * type: only output a certain type of intent |
| 1799 | This options is valid only when jsonFormat is true and summary is |
| 1800 | true |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1801 | Description: |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1802 | Obtain intents |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1803 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1804 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1805 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1806 | if summary: |
| 1807 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1808 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1809 | cmdStr += " -j" |
| 1810 | handle = self.sendline( cmdStr ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1811 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 1812 | if "TYPE" in args.keys(): |
| 1813 | type = args[ "TYPE" ] |
| 1814 | else: |
| 1815 | type = "" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1816 | if jsonFormat and summary and ( type != "" ): |
| 1817 | jsonResult = json.loads( handle ) |
| 1818 | if type in jsonResult.keys(): |
| 1819 | return jsonResult[ type ] |
| 1820 | else: |
| 1821 | main.log.error( "unknown TYPE, return all types of intents" ) |
| 1822 | return handle |
| 1823 | else: |
| 1824 | return handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 1825 | |
| 1826 | except TypeError: |
| 1827 | main.log.exception( self.name + ": Object not as expected" ) |
| 1828 | return None |
| 1829 | except pexpect.EOF: |
| 1830 | main.log.error( self.name + ": EOF exception found" ) |
| 1831 | main.log.error( self.name + ": " + self.handle.before ) |
| 1832 | main.cleanup() |
| 1833 | main.exit() |
| 1834 | except Exception: |
| 1835 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1836 | main.cleanup() |
| 1837 | main.exit() |
| 1838 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1839 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1840 | def getIntentState(self, intentsId, intentsJson=None): |
| 1841 | """ |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1842 | Check intent state. |
| 1843 | Accepts a single intent ID (string type) or a list of intent IDs. |
| 1844 | Returns the state(string type) of the id if a single intent ID is |
| 1845 | accepted. |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1846 | Returns a dictionary with intent IDs as the key and its |
| 1847 | corresponding states as the values |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1848 | Parameters: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1849 | intentId: intent ID (string type) |
| 1850 | intentsJson: parsed json object from the onos:intents api |
| 1851 | Returns: |
| 1852 | state = An intent's state- INSTALL,WITHDRAWN etc. |
| 1853 | stateDict = Dictionary of intent's state. intent ID as the keys and |
| 1854 | state as the values. |
| 1855 | """ |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1856 | try: |
| 1857 | state = "State is Undefined" |
| 1858 | if not intentsJson: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1859 | intentsJsonTemp = json.loads( self.intents() ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1860 | else: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1861 | intentsJsonTemp = json.loads( intentsJson ) |
| 1862 | if isinstance( intentsId, types.StringType ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1863 | for intent in intentsJsonTemp: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1864 | if intentsId == intent[ 'id' ]: |
| 1865 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1866 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1867 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
| 1868 | " on the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1869 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1870 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 1871 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1872 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 1873 | stateDict = {} |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1874 | for intents in intentsJsonTemp: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1875 | if intentsId[ i ] == intents[ 'id' ]: |
| 1876 | stateDict[ 'state' ] = intents[ 'state' ] |
| 1877 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1878 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1879 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1880 | if len( intentsId ) != len( dictList ): |
| 1881 | main.log.info( "Cannot find some of the intent ID state" ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 1882 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1883 | else: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1884 | main.log.info( "Invalid intents ID entry" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1885 | return None |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1886 | except TypeError: |
| 1887 | main.log.exception( self.name + ": Object not as expected" ) |
| 1888 | return None |
| 1889 | except pexpect.EOF: |
| 1890 | main.log.error( self.name + ": EOF exception found" ) |
| 1891 | main.log.error( self.name + ": " + self.handle.before ) |
| 1892 | main.cleanup() |
| 1893 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1894 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 1895 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1896 | main.cleanup() |
| 1897 | main.exit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 1898 | |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 1899 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1900 | """ |
| 1901 | Description: |
| 1902 | Check intents state |
| 1903 | Required: |
| 1904 | intentsId - List of intents ID to be checked |
| 1905 | Optional: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 1906 | expectedState - Check the expected state(s) of each intents |
| 1907 | state in the list. |
| 1908 | *NOTE: You can pass in a list of expected state, |
| 1909 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1910 | Return: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 1911 | Returns main.TRUE only if all intent are the same as expected states |
| 1912 | , otherwise, returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1913 | """ |
| 1914 | try: |
| 1915 | # Generating a dictionary: intent id as a key and state as value |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 1916 | returnValue = main.TRUE |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1917 | intentsDict = self.getIntentState( intentsId ) |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 1918 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 1919 | #print "len of intentsDict ", str( len( intentsDict ) ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1920 | if len( intentsId ) != len( intentsDict ): |
| 1921 | main.log.info( self.name + "There is something wrong " + |
| 1922 | "getting intents state" ) |
| 1923 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 1924 | |
| 1925 | if isinstance( expectedState, types.StringType ): |
| 1926 | for intents in intentsDict: |
| 1927 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 1928 | main.log.debug( self.name + " : Intent ID - " + |
| 1929 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 1930 | " actual state = " + |
| 1931 | intents.get( 'state' ) |
| 1932 | + " does not equal expected state = " |
| 1933 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 1934 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 1935 | |
| 1936 | elif isinstance( expectedState, types.ListType ): |
| 1937 | for intents in intentsDict: |
| 1938 | if not any( state == intents.get( 'state' ) for state in |
| 1939 | expectedState ): |
| 1940 | main.log.debug( self.name + " : Intent ID - " + |
| 1941 | intents.get( 'id' ) + |
| 1942 | " actual state = " + |
| 1943 | intents.get( 'state' ) + |
| 1944 | " does not equal expected states = " |
| 1945 | + str( expectedState ) ) |
| 1946 | returnValue = main.FALSE |
| 1947 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1948 | if returnValue == main.TRUE: |
| 1949 | main.log.info( self.name + ": All " + |
| 1950 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 1951 | " intents are in " + str( expectedState ) + |
| 1952 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 1953 | return returnValue |
| 1954 | except TypeError: |
| 1955 | main.log.exception( self.name + ": Object not as expected" ) |
| 1956 | return None |
| 1957 | except pexpect.EOF: |
| 1958 | main.log.error( self.name + ": EOF exception found" ) |
| 1959 | main.log.error( self.name + ": " + self.handle.before ) |
| 1960 | main.cleanup() |
| 1961 | main.exit() |
| 1962 | except Exception: |
| 1963 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1964 | main.cleanup() |
| 1965 | main.exit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1966 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1967 | def flows( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1968 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1969 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1970 | * jsonFormat: enable output formatting in json |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1971 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1972 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1973 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1974 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1975 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1976 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1977 | cmdStr += " -j" |
| 1978 | handle = self.sendline( cmdStr ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1979 | if re.search( "Error:", handle ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 1980 | main.log.error( self.name + ": flows() response: " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1981 | str( handle ) ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1982 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1983 | except TypeError: |
| 1984 | main.log.exception( self.name + ": Object not as expected" ) |
| 1985 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1986 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1987 | main.log.error( self.name + ": EOF exception found" ) |
| 1988 | main.log.error( self.name + ": " + self.handle.before ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1989 | main.cleanup() |
| 1990 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1991 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1992 | main.log.exception( self.name + ": Uncaught exception!" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 1993 | main.cleanup() |
| 1994 | main.exit() |
| 1995 | |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 1996 | def checkFlowsState( self, isPENDING_ADD = True ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 1997 | """ |
| 1998 | Description: |
| 1999 | Check the if all the current flows are in ADDED state or |
| 2000 | PENDING_ADD state |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2001 | Optional: |
| 2002 | * isPENDING_ADD: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2003 | Return: |
| 2004 | returnValue - Returns main.TRUE only if all flows are in |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2005 | ADDED state or PENDING_ADD if the PENDING_ADD |
| 2006 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2007 | """ |
| 2008 | try: |
| 2009 | tempFlows = json.loads( self.flows() ) |
kelvin-onlab | f0594d7 | 2015-05-19 17:25:12 -0700 | [diff] [blame] | 2010 | #print tempFlows[0] |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2011 | returnValue = main.TRUE |
kelvin-onlab | f0594d7 | 2015-05-19 17:25:12 -0700 | [diff] [blame] | 2012 | |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2013 | if isPENDING_ADD: |
| 2014 | for device in tempFlows: |
| 2015 | for flow in device.get( 'flows' ): |
| 2016 | if flow.get( 'state' ) != 'ADDED' and \ |
| 2017 | flow.get( 'state' ) != 'PENDING_ADD': |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2018 | |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2019 | main.log.info( self.name + ": flow Id: " + |
| 2020 | str( flow.get( 'groupId' ) ) + |
| 2021 | " | state:" + |
| 2022 | str( flow.get( 'state' ) ) ) |
| 2023 | returnValue = main.FALSE |
| 2024 | else: |
| 2025 | for device in tempFlows: |
| 2026 | for flow in device.get( 'flows' ): |
| 2027 | if flow.get( 'state' ) != 'ADDED': |
| 2028 | |
| 2029 | main.log.info( self.name + ": flow Id: " + |
| 2030 | str( flow.get( 'groupId' ) ) + |
| 2031 | " | state:" + |
| 2032 | str( flow.get( 'state' ) ) ) |
| 2033 | returnValue = main.FALSE |
kelvin-onlab | f0594d7 | 2015-05-19 17:25:12 -0700 | [diff] [blame] | 2034 | |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2035 | return returnValue |
| 2036 | except TypeError: |
| 2037 | main.log.exception( self.name + ": Object not as expected" ) |
| 2038 | return None |
| 2039 | except pexpect.EOF: |
| 2040 | main.log.error( self.name + ": EOF exception found" ) |
| 2041 | main.log.error( self.name + ": " + self.handle.before ) |
| 2042 | main.cleanup() |
| 2043 | main.exit() |
| 2044 | except Exception: |
| 2045 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2046 | main.cleanup() |
| 2047 | main.exit() |
| 2048 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2049 | def pushTestIntents( self, dpidSrc, dpidDst, numIntents, |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2050 | numMult="", appId="", report=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2051 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2052 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2053 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2054 | a specific point-to-point intent definition |
| 2055 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2056 | * dpidSrc: specify source dpid |
| 2057 | * dpidDst: specify destination dpid |
| 2058 | * numIntents: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2059 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2060 | * numMult: number multiplier for multiplying |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 2061 | the number of intents specified |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2062 | * appId: specify the application id init to further |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 2063 | modularize the intents |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2064 | * report: default True, returns latency information |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2065 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2066 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2067 | cmd = "push-test-intents " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2068 | str( dpidSrc ) + " " + str( dpidDst ) + " " +\ |
| 2069 | str( numIntents ) |
| 2070 | if numMult: |
| 2071 | cmd += " " + str( numMult ) |
| 2072 | # If app id is specified, then numMult |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2073 | # must exist because of the way this command |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2074 | if appId: |
| 2075 | cmd += " " + str( appId ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2076 | handle = self.sendline( cmd ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2077 | if report: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2078 | latResult = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2079 | main.log.info( handle ) |
| 2080 | # Split result by newline |
| 2081 | newline = handle.split( "\r\r\n" ) |
| 2082 | # Ignore the first object of list, which is empty |
| 2083 | newline = newline[ 1: ] |
| 2084 | # Some sloppy parsing method to get the latency |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 2085 | for result in newline: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2086 | result = result.split( ": " ) |
| 2087 | # Append the first result of second parse |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2088 | latResult.append( result[ 1 ].split( " " )[ 0 ] ) |
| 2089 | main.log.info( latResult ) |
| 2090 | return latResult |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2091 | else: |
| 2092 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2093 | except TypeError: |
| 2094 | main.log.exception( self.name + ": Object not as expected" ) |
| 2095 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2096 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2097 | main.log.error( self.name + ": EOF exception found" ) |
| 2098 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2099 | main.cleanup() |
| 2100 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2101 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2102 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2103 | main.cleanup() |
| 2104 | main.exit() |
| 2105 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2106 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2107 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2108 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2109 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2110 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2111 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2112 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2113 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2114 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2115 | cmdStr += " -j" |
| 2116 | handle = self.sendline( cmdStr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2117 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2118 | except TypeError: |
| 2119 | main.log.exception( self.name + ": Object not as expected" ) |
| 2120 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2121 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2122 | main.log.error( self.name + ": EOF exception found" ) |
| 2123 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2124 | main.cleanup() |
| 2125 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2126 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2127 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2128 | main.cleanup() |
| 2129 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2130 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2131 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2132 | """ |
| 2133 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2134 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2135 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2136 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2137 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2138 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2139 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2140 | cmdStr += " -j" |
| 2141 | handle = self.sendline( cmdStr ) |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2142 | if handle: |
| 2143 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2144 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2145 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2146 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2147 | else: |
| 2148 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2149 | except TypeError: |
| 2150 | main.log.exception( self.name + ": Object not as expected" ) |
| 2151 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2152 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2153 | main.log.error( self.name + ": EOF exception found" ) |
| 2154 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2155 | main.cleanup() |
| 2156 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2157 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2158 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2159 | main.cleanup() |
| 2160 | main.exit() |
| 2161 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2162 | # Wrapper functions **************** |
| 2163 | # Wrapper functions use existing driver |
| 2164 | # functions and extends their use case. |
| 2165 | # For example, we may use the output of |
| 2166 | # a normal driver function, and parse it |
| 2167 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2168 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2169 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2170 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2171 | Description: |
| 2172 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2173 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2174 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2175 | # Obtain output of intents function |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2176 | intentsStr = self.intents(jsonFormat=False) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2177 | intentIdList = [] |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2178 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2179 | # Parse the intents output for ID's |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2180 | intentsList = [ s.strip() for s in intentsStr.splitlines() ] |
| 2181 | for intents in intentsList: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2182 | match = re.search('id=0x([\da-f]+),', intents) |
| 2183 | if match: |
| 2184 | tmpId = match.group()[3:-1] |
| 2185 | intentIdList.append( tmpId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2186 | return intentIdList |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2187 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2188 | except TypeError: |
| 2189 | main.log.exception( self.name + ": Object not as expected" ) |
| 2190 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2191 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2192 | main.log.error( self.name + ": EOF exception found" ) |
| 2193 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2194 | main.cleanup() |
| 2195 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2196 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2197 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2198 | main.cleanup() |
| 2199 | main.exit() |
| 2200 | |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2201 | def FlowAddedCount( self, deviceId ): |
| 2202 | """ |
| 2203 | Determine the number of flow rules for the given device id that are |
| 2204 | in the added state |
| 2205 | """ |
| 2206 | try: |
| 2207 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2208 | "grep 'state=ADDED' | wc -l" |
| 2209 | handle = self.sendline( cmdStr ) |
| 2210 | return handle |
| 2211 | except pexpect.EOF: |
| 2212 | main.log.error( self.name + ": EOF exception found" ) |
| 2213 | main.log.error( self.name + ": " + self.handle.before ) |
| 2214 | main.cleanup() |
| 2215 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2216 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2217 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2218 | main.cleanup() |
| 2219 | main.exit() |
| 2220 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2221 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2222 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2223 | Use 'devices' function to obtain list of all devices |
| 2224 | and parse the result to obtain a list of all device |
| 2225 | id's. Returns this list. Returns empty list if no |
| 2226 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2227 | List is ordered sequentially |
| 2228 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2229 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2230 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2231 | the ids. By obtaining the list of device ids on the fly, |
| 2232 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2233 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2234 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2235 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2236 | devicesStr = self.devices( jsonFormat=False ) |
| 2237 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2238 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2239 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2240 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2241 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2242 | |
| 2243 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2244 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2245 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2246 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2247 | # Split list further into arguments before and after string |
| 2248 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2249 | # append to idList |
| 2250 | for arg in tempList: |
| 2251 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 2252 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2253 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2254 | except TypeError: |
| 2255 | main.log.exception( self.name + ": Object not as expected" ) |
| 2256 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2257 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2258 | main.log.error( self.name + ": EOF exception found" ) |
| 2259 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2260 | main.cleanup() |
| 2261 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2262 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2263 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2264 | main.cleanup() |
| 2265 | main.exit() |
| 2266 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2267 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2268 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2269 | Uses 'nodes' function to obtain list of all nodes |
| 2270 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2271 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2272 | Returns: |
| 2273 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2274 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2275 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2276 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2277 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2278 | # Sample nodesStr output |
| 2279 | # id=local, address=127.0.0.1:9876, state=ACTIVE * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2280 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2281 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2282 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2283 | nodesJson = json.loads( nodesStr ) |
| 2284 | idList = [ node.get('id') for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2285 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2286 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2287 | except TypeError: |
| 2288 | main.log.exception( self.name + ": Object not as expected" ) |
| 2289 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2290 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2291 | main.log.error( self.name + ": EOF exception found" ) |
| 2292 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2293 | main.cleanup() |
| 2294 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2295 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2296 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2297 | main.cleanup() |
| 2298 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2299 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2300 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2301 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2302 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 2303 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2304 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2305 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2306 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2307 | return None |
| 2308 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2309 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2310 | rawDevices = self.devices() |
| 2311 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2312 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2313 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2314 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 2315 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2316 | return device |
| 2317 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2318 | except TypeError: |
| 2319 | main.log.exception( self.name + ": Object not as expected" ) |
| 2320 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2321 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2322 | main.log.error( self.name + ": EOF exception found" ) |
| 2323 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2324 | main.cleanup() |
| 2325 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2326 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2327 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2328 | main.cleanup() |
| 2329 | main.exit() |
| 2330 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2331 | def checkStatus( self, ip, numoswitch, numolink, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2332 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2333 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2334 | supplied values. By default this will report to main.log, but the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2335 | log level can be specified. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2336 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2337 | Params: ip = ip used for the onos cli |
| 2338 | numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2339 | numolink = expected number of links |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2340 | logLevel = level to log to. Currently accepts |
| 2341 | 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2342 | |
| 2343 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2344 | logLevel can |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2345 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2346 | Returns: main.TRUE if the number of switches and links are correct, |
| 2347 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2348 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2349 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2350 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2351 | topology = self.getTopology( ip ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2352 | if topology == {}: |
| 2353 | return main.ERROR |
| 2354 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2355 | # Is the number of switches is what we expected |
| 2356 | devices = topology.get( 'devices', False ) |
| 2357 | links = topology.get( 'links', False ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2358 | if devices is False or links is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2359 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2360 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2361 | # Is the number of links is what we expected |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2362 | linkCheck = ( int( links ) == int( numolink ) ) |
| 2363 | if ( switchCheck and linkCheck ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2364 | # We expected the correct numbers |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2365 | output += "The number of links and switches match " +\ |
| 2366 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2367 | result = main.TRUE |
| 2368 | else: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2369 | output += "The number of links and switches does not match " +\ |
| 2370 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2371 | result = main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2372 | output = output + "\n ONOS sees %i devices (%i expected) \ |
| 2373 | and %i links (%i expected)" % ( |
| 2374 | int( devices ), int( numoswitch ), int( links ), |
| 2375 | int( numolink ) ) |
| 2376 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2377 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2378 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2379 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2380 | else: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2381 | main.log.info( self.name + ": " + output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2382 | return result |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2383 | except TypeError: |
| 2384 | main.log.exception( self.name + ": Object not as expected" ) |
| 2385 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2386 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2387 | main.log.error( self.name + ": EOF exception found" ) |
| 2388 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2389 | main.cleanup() |
| 2390 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2391 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2392 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2393 | main.cleanup() |
| 2394 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2395 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2396 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2397 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2398 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2399 | deviceId must be the id of a device as seen in the onos devices command |
| 2400 | onosNode is the ip of one of the onos nodes in the cluster |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2401 | role must be either master, standby, or none |
| 2402 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2403 | Returns: |
| 2404 | main.TRUE or main.FALSE based on argument verification and |
| 2405 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2406 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2407 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2408 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2409 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2410 | cmdStr = "device-role " +\ |
| 2411 | str( deviceId ) + " " +\ |
| 2412 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2413 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2414 | handle = self.sendline( cmdStr ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2415 | if re.search( "Error", handle ): |
| 2416 | # end color output to escape any colours |
| 2417 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2418 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2419 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2420 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2421 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2422 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2423 | main.log.error( "Invalid 'role' given to device_role(). " + |
| 2424 | "Value was '" + str(role) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2425 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2426 | except TypeError: |
| 2427 | main.log.exception( self.name + ": Object not as expected" ) |
| 2428 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2429 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2430 | main.log.error( self.name + ": EOF exception found" ) |
| 2431 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2432 | main.cleanup() |
| 2433 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2434 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2435 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2436 | main.cleanup() |
| 2437 | main.exit() |
| 2438 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2439 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2440 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2441 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2442 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2443 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2444 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2445 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2446 | cmdStr = "clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2447 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2448 | cmdStr += " -j" |
| 2449 | handle = self.sendline( cmdStr ) |
| 2450 | return handle |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2451 | except TypeError: |
| 2452 | main.log.exception( self.name + ": Object not as expected" ) |
| 2453 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2454 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2455 | main.log.error( self.name + ": EOF exception found" ) |
| 2456 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2457 | main.cleanup() |
| 2458 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2459 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2460 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2461 | main.cleanup() |
| 2462 | main.exit() |
| 2463 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2464 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2465 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2466 | CLI command to get the current leader for the Election test application |
| 2467 | NOTE: Requires installation of the onos-app-election feature |
| 2468 | Returns: Node IP of the leader if one exists |
| 2469 | None if none exists |
| 2470 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2471 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2472 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2473 | cmdStr = "election-test-leader" |
| 2474 | response = self.sendline( cmdStr ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2475 | # Leader |
| 2476 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2477 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2478 | nodeSearch = re.search( leaderPattern, response ) |
| 2479 | if nodeSearch: |
| 2480 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2481 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2482 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2483 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2484 | # no leader |
| 2485 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2486 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2487 | nullSearch = re.search( nullPattern, response ) |
| 2488 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2489 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2490 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2491 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2492 | # error |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2493 | errorPattern = "Command\snot\sfound" |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2494 | if re.search( errorPattern, response ): |
| 2495 | main.log.error( "Election app is not loaded on " + self.name ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2496 | # TODO: Should this be main.ERROR? |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 2497 | return main.FALSE |
| 2498 | else: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2499 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 2500 | ": " + "unexpected response" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2501 | main.log.error( repr( response ) ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 2502 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2503 | except TypeError: |
| 2504 | main.log.exception( self.name + ": Object not as expected" ) |
| 2505 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2506 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2507 | main.log.error( self.name + ": EOF exception found" ) |
| 2508 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2509 | main.cleanup() |
| 2510 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2511 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2512 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2513 | main.cleanup() |
| 2514 | main.exit() |
| 2515 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2516 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2517 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2518 | CLI command to run for leadership of the Election test application. |
| 2519 | NOTE: Requires installation of the onos-app-election feature |
| 2520 | Returns: Main.TRUE on success |
| 2521 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2522 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2523 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2524 | cmdStr = "election-test-run" |
| 2525 | response = self.sendline( cmdStr ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2526 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2527 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2528 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2529 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2530 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2531 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2532 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2533 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2534 | # error |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2535 | errorPattern = "Command\snot\sfound" |
| 2536 | if re.search( errorPattern, response ): |
| 2537 | main.log.error( "Election app is not loaded on " + self.name ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 2538 | return main.FALSE |
| 2539 | else: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2540 | main.log.error( "Error in electionTestRun on " + self.name + |
| 2541 | ": " + "unexpected response" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2542 | main.log.error( repr( response ) ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 2543 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2544 | except TypeError: |
| 2545 | main.log.exception( self.name + ": Object not as expected" ) |
| 2546 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2547 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2548 | main.log.error( self.name + ": EOF exception found" ) |
| 2549 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2550 | main.cleanup() |
| 2551 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2552 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2553 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2554 | main.cleanup() |
| 2555 | main.exit() |
| 2556 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2557 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2558 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2559 | * CLI command to withdraw the local node from leadership election for |
| 2560 | * the Election test application. |
| 2561 | #NOTE: Requires installation of the onos-app-election feature |
| 2562 | Returns: Main.TRUE on success |
| 2563 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2564 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2565 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2566 | cmdStr = "election-test-withdraw" |
| 2567 | response = self.sendline( cmdStr ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2568 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2569 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2570 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2571 | if re.search( successPattern, response ): |
| 2572 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2573 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2574 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2575 | # error |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2576 | errorPattern = "Command\snot\sfound" |
| 2577 | if re.search( errorPattern, response ): |
| 2578 | main.log.error( "Election app is not loaded on " + self.name ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 2579 | return main.FALSE |
| 2580 | else: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2581 | main.log.error( "Error in electionTestWithdraw on " + |
| 2582 | self.name + ": " + "unexpected response" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2583 | main.log.error( repr( response ) ) |
Jon Hall | 669173b | 2014-12-17 11:36:30 -0800 | [diff] [blame] | 2584 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2585 | except TypeError: |
| 2586 | main.log.exception( self.name + ": Object not as expected" ) |
| 2587 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2588 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2589 | main.log.error( self.name + ": EOF exception found" ) |
| 2590 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2591 | main.cleanup() |
| 2592 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2593 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2594 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2595 | main.cleanup() |
| 2596 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2597 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2598 | def getDevicePortsEnabledCount( self, dpid ): |
| 2599 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2600 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2601 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2602 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2603 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2604 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 2605 | output = self.sendline( cmdStr ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2606 | if re.search( "No such device", output ): |
| 2607 | main.log.error( "Error in getting ports" ) |
| 2608 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2609 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2610 | return output |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2611 | except TypeError: |
| 2612 | main.log.exception( self.name + ": Object not as expected" ) |
| 2613 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2614 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2615 | main.log.error( self.name + ": EOF exception found" ) |
| 2616 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2617 | main.cleanup() |
| 2618 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2619 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2620 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2621 | main.cleanup() |
| 2622 | main.exit() |
| 2623 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2624 | def getDeviceLinksActiveCount( self, dpid ): |
| 2625 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2626 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2627 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2628 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2629 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2630 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 2631 | output = self.sendline( cmdStr ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2632 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2633 | main.log.error( "Error in getting ports " ) |
| 2634 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2635 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2636 | return output |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2637 | except TypeError: |
| 2638 | main.log.exception( self.name + ": Object not as expected" ) |
| 2639 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2640 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2641 | main.log.error( self.name + ": EOF exception found" ) |
| 2642 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2643 | main.cleanup() |
| 2644 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2645 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2646 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2647 | main.cleanup() |
| 2648 | main.exit() |
| 2649 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2650 | def getAllIntentIds( self ): |
| 2651 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2652 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2653 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2654 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2655 | cmdStr = "onos:intents | grep id=" |
| 2656 | output = self.sendline( cmdStr ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2657 | if re.search( "Error", output ): |
| 2658 | main.log.error( "Error in getting ports" ) |
| 2659 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2660 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2661 | return output |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2662 | except TypeError: |
| 2663 | main.log.exception( self.name + ": Object not as expected" ) |
| 2664 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2665 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2666 | main.log.error( self.name + ": EOF exception found" ) |
| 2667 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2668 | main.cleanup() |
| 2669 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2670 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2671 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2672 | main.cleanup() |
| 2673 | main.exit() |
| 2674 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 2675 | def intentSummary( self ): |
| 2676 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2677 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 2678 | """ |
| 2679 | try: |
| 2680 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 2681 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2682 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 2683 | states.append( intent.get( 'state', None ) ) |
| 2684 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2685 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 2686 | return dict( out ) |
| 2687 | except TypeError: |
| 2688 | main.log.exception( self.name + ": Object not as expected" ) |
| 2689 | return None |
| 2690 | except pexpect.EOF: |
| 2691 | main.log.error( self.name + ": EOF exception found" ) |
| 2692 | main.log.error( self.name + ": " + self.handle.before ) |
| 2693 | main.cleanup() |
| 2694 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2695 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 2696 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2697 | main.cleanup() |
| 2698 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2699 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2700 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2701 | """ |
| 2702 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2703 | Optional argument: |
| 2704 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2705 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2706 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2707 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2708 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2709 | cmdStr += " -j" |
| 2710 | output = self.sendline( cmdStr ) |
| 2711 | return output |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2712 | except TypeError: |
| 2713 | main.log.exception( self.name + ": Object not as expected" ) |
| 2714 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2715 | except pexpect.EOF: |
| 2716 | main.log.error( self.name + ": EOF exception found" ) |
| 2717 | main.log.error( self.name + ": " + self.handle.before ) |
| 2718 | main.cleanup() |
| 2719 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 2720 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2721 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 2722 | main.cleanup() |
| 2723 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2724 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 2725 | def leaderCandidates( self, jsonFormat=True ): |
| 2726 | """ |
| 2727 | Returns the output of the leaders -c command. |
| 2728 | Optional argument: |
| 2729 | * jsonFormat - boolean indicating if you want output in json |
| 2730 | """ |
| 2731 | try: |
| 2732 | cmdStr = "onos:leaders -c" |
| 2733 | if jsonFormat: |
| 2734 | cmdStr += " -j" |
| 2735 | output = self.sendline( cmdStr ) |
| 2736 | return output |
| 2737 | except TypeError: |
| 2738 | main.log.exception( self.name + ": Object not as expected" ) |
| 2739 | return None |
| 2740 | except pexpect.EOF: |
| 2741 | main.log.error( self.name + ": EOF exception found" ) |
| 2742 | main.log.error( self.name + ": " + self.handle.before ) |
| 2743 | main.cleanup() |
| 2744 | main.exit() |
| 2745 | except Exception: |
| 2746 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2747 | main.cleanup() |
| 2748 | main.exit() |
| 2749 | |
| 2750 | def specificLeaderCandidate(self,topic): |
| 2751 | """ |
| 2752 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
| 2753 | topic parameter and an empty list if the topic doesn't exist |
| 2754 | If no leader is elected leader in the returned list will be "none" |
| 2755 | Returns None if there is a type error processing the json object |
| 2756 | """ |
| 2757 | try: |
| 2758 | cmdStr = "onos:leaders -c -j" |
| 2759 | output = self.sendline( cmdStr ) |
| 2760 | output = json.loads(output) |
| 2761 | results = [] |
| 2762 | for dict in output: |
| 2763 | if dict["topic"] == topic: |
| 2764 | leader = dict["leader"] |
| 2765 | candidates = re.split(", ",dict["candidates"][1:-1]) |
| 2766 | results.append(leader) |
| 2767 | results.extend(candidates) |
| 2768 | return results |
| 2769 | except TypeError: |
| 2770 | main.log.exception( self.name + ": Object not as expected" ) |
| 2771 | return None |
| 2772 | except pexpect.EOF: |
| 2773 | main.log.error( self.name + ": EOF exception found" ) |
| 2774 | main.log.error( self.name + ": " + self.handle.before ) |
| 2775 | main.cleanup() |
| 2776 | main.exit() |
| 2777 | except Exception: |
| 2778 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2779 | main.cleanup() |
| 2780 | main.exit() |
| 2781 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2782 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2783 | """ |
| 2784 | Returns the output of the intent Pending map. |
| 2785 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2786 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2787 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2788 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2789 | cmdStr += " -j" |
| 2790 | output = self.sendline( cmdStr ) |
| 2791 | return output |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2792 | except TypeError: |
| 2793 | main.log.exception( self.name + ": Object not as expected" ) |
| 2794 | return None |
| 2795 | except pexpect.EOF: |
| 2796 | main.log.error( self.name + ": EOF exception found" ) |
| 2797 | main.log.error( self.name + ": " + self.handle.before ) |
| 2798 | main.cleanup() |
| 2799 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 2800 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2801 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2802 | main.cleanup() |
| 2803 | main.exit() |
| 2804 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2805 | def partitions( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2806 | """ |
| 2807 | Returns the output of the raft partitions command for ONOS. |
| 2808 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2809 | # Sample JSON |
| 2810 | # { |
| 2811 | # "leader": "tcp://10.128.30.11:7238", |
| 2812 | # "members": [ |
| 2813 | # "tcp://10.128.30.11:7238", |
| 2814 | # "tcp://10.128.30.17:7238", |
| 2815 | # "tcp://10.128.30.13:7238", |
| 2816 | # ], |
| 2817 | # "name": "p1", |
| 2818 | # "term": 3 |
| 2819 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2820 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2821 | cmdStr = "onos:partitions" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2822 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2823 | cmdStr += " -j" |
| 2824 | output = self.sendline( cmdStr ) |
| 2825 | return output |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2826 | except TypeError: |
| 2827 | main.log.exception( self.name + ": Object not as expected" ) |
| 2828 | return None |
| 2829 | except pexpect.EOF: |
| 2830 | main.log.error( self.name + ": EOF exception found" ) |
| 2831 | main.log.error( self.name + ": " + self.handle.before ) |
| 2832 | main.cleanup() |
| 2833 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 2834 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 2835 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2836 | main.cleanup() |
| 2837 | main.exit() |
| 2838 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2839 | def apps( self, jsonFormat=True ): |
| 2840 | """ |
| 2841 | Returns the output of the apps command for ONOS. This command lists |
| 2842 | information about installed ONOS applications |
| 2843 | """ |
| 2844 | # Sample JSON object |
| 2845 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
| 2846 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 2847 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
| 2848 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
| 2849 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2850 | cmdStr = "onos:apps" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2851 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2852 | cmdStr += " -j" |
| 2853 | output = self.sendline( cmdStr ) |
| 2854 | assert "Error executing command" not in output |
| 2855 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2856 | # FIXME: look at specific exceptions/Errors |
| 2857 | except AssertionError: |
| 2858 | main.log.error( "Error in processing onos:app command: " + |
| 2859 | str( output ) ) |
| 2860 | return None |
| 2861 | except TypeError: |
| 2862 | main.log.exception( self.name + ": Object not as expected" ) |
| 2863 | return None |
| 2864 | except pexpect.EOF: |
| 2865 | main.log.error( self.name + ": EOF exception found" ) |
| 2866 | main.log.error( self.name + ": " + self.handle.before ) |
| 2867 | main.cleanup() |
| 2868 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 2869 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2870 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2871 | main.cleanup() |
| 2872 | main.exit() |
| 2873 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 2874 | def appStatus( self, appName ): |
| 2875 | """ |
| 2876 | Uses the onos:apps cli command to return the status of an application. |
| 2877 | Returns: |
| 2878 | "ACTIVE" - If app is installed and activated |
| 2879 | "INSTALLED" - If app is installed and deactivated |
| 2880 | "UNINSTALLED" - If app is not installed |
| 2881 | None - on error |
| 2882 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 2883 | try: |
| 2884 | if not isinstance( appName, types.StringType ): |
| 2885 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 2886 | " a string" ) |
| 2887 | return None |
| 2888 | output = self.apps( jsonFormat=True ) |
| 2889 | appsJson = json.loads( output ) |
| 2890 | state = None |
| 2891 | for app in appsJson: |
| 2892 | if appName == app.get('name'): |
| 2893 | state = app.get('state') |
| 2894 | break |
| 2895 | if state == "ACTIVE" or state == "INSTALLED": |
| 2896 | return state |
| 2897 | elif state is None: |
| 2898 | return "UNINSTALLED" |
| 2899 | elif state: |
| 2900 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 2901 | str( state ) ) |
| 2902 | return state |
| 2903 | except TypeError: |
| 2904 | main.log.exception( self.name + ": Object not as expected" ) |
| 2905 | return None |
| 2906 | except pexpect.EOF: |
| 2907 | main.log.error( self.name + ": EOF exception found" ) |
| 2908 | main.log.error( self.name + ": " + self.handle.before ) |
| 2909 | main.cleanup() |
| 2910 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 2911 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 2912 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2913 | main.cleanup() |
| 2914 | main.exit() |
| 2915 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2916 | def app( self, appName, option ): |
| 2917 | """ |
| 2918 | Interacts with the app command for ONOS. This command manages |
| 2919 | application inventory. |
| 2920 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2921 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 2922 | # Validate argument types |
| 2923 | valid = True |
| 2924 | if not isinstance( appName, types.StringType ): |
| 2925 | main.log.error( self.name + ".app(): appName must be a " + |
| 2926 | "string" ) |
| 2927 | valid = False |
| 2928 | if not isinstance( option, types.StringType ): |
| 2929 | main.log.error( self.name + ".app(): option must be a string" ) |
| 2930 | valid = False |
| 2931 | if not valid: |
| 2932 | return main.FALSE |
| 2933 | # Validate Option |
| 2934 | option = option.lower() |
| 2935 | # NOTE: Install may become a valid option |
| 2936 | if option == "activate": |
| 2937 | pass |
| 2938 | elif option == "deactivate": |
| 2939 | pass |
| 2940 | elif option == "uninstall": |
| 2941 | pass |
| 2942 | else: |
| 2943 | # Invalid option |
| 2944 | main.log.error( "The ONOS app command argument only takes " + |
| 2945 | "the values: (activate|deactivate|uninstall)" + |
| 2946 | "; was given '" + option + "'") |
| 2947 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 2948 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2949 | output = self.sendline( cmdStr ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2950 | if "Error executing command" in output: |
| 2951 | main.log.error( "Error in processing onos:app command: " + |
| 2952 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 2953 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2954 | elif "No such application" in output: |
| 2955 | main.log.error( "The application '" + appName + |
| 2956 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 2957 | return main.FALSE |
| 2958 | elif "Command not found:" in output: |
| 2959 | main.log.error( "Error in processing onos:app command: " + |
| 2960 | str( output ) ) |
| 2961 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 2962 | elif "Unsupported command:" in output: |
| 2963 | main.log.error( "Incorrect command given to 'app': " + |
| 2964 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2965 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 2966 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 2967 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2968 | return main.TRUE |
| 2969 | except TypeError: |
| 2970 | main.log.exception( self.name + ": Object not as expected" ) |
| 2971 | return main.ERROR |
| 2972 | except pexpect.EOF: |
| 2973 | main.log.error( self.name + ": EOF exception found" ) |
| 2974 | main.log.error( self.name + ": " + self.handle.before ) |
| 2975 | main.cleanup() |
| 2976 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 2977 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2978 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2979 | main.cleanup() |
| 2980 | main.exit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 2981 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 2982 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 2983 | """ |
| 2984 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 2985 | appName is the hierarchical app name, not the feature name |
| 2986 | If check is True, method will check the status of the app after the |
| 2987 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 2988 | Returns main.TRUE if the command was successfully sent |
| 2989 | main.FALSE if the cli responded with an error or given |
| 2990 | incorrect input |
| 2991 | """ |
| 2992 | try: |
| 2993 | if not isinstance( appName, types.StringType ): |
| 2994 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 2995 | " a string" ) |
| 2996 | return main.FALSE |
| 2997 | status = self.appStatus( appName ) |
| 2998 | if status == "INSTALLED": |
| 2999 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3000 | if check and response == main.TRUE: |
| 3001 | for i in range(10): # try 10 times then give up |
| 3002 | # TODO: Check with Thomas about this delay |
| 3003 | status = self.appStatus( appName ) |
| 3004 | if status == "ACTIVE": |
| 3005 | return main.TRUE |
| 3006 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3007 | main.log.debug( "The state of application " + |
| 3008 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3009 | time.sleep( 1 ) |
| 3010 | return main.FALSE |
| 3011 | else: # not 'check' or command didn't succeed |
| 3012 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3013 | elif status == "ACTIVE": |
| 3014 | return main.TRUE |
| 3015 | elif status == "UNINSTALLED": |
| 3016 | main.log.error( self.name + ": Tried to activate the " + |
| 3017 | "application '" + appName + "' which is not " + |
| 3018 | "installed." ) |
| 3019 | else: |
| 3020 | main.log.error( "Unexpected return value from appStatus: " + |
| 3021 | str( status ) ) |
| 3022 | return main.ERROR |
| 3023 | except TypeError: |
| 3024 | main.log.exception( self.name + ": Object not as expected" ) |
| 3025 | return main.ERROR |
| 3026 | except pexpect.EOF: |
| 3027 | main.log.error( self.name + ": EOF exception found" ) |
| 3028 | main.log.error( self.name + ": " + self.handle.before ) |
| 3029 | main.cleanup() |
| 3030 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3031 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3032 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3033 | main.cleanup() |
| 3034 | main.exit() |
| 3035 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3036 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3037 | """ |
| 3038 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3039 | appName is the hierarchical app name, not the feature name |
| 3040 | If check is True, method will check the status of the app after the |
| 3041 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3042 | Returns main.TRUE if the command was successfully sent |
| 3043 | main.FALSE if the cli responded with an error or given |
| 3044 | incorrect input |
| 3045 | """ |
| 3046 | try: |
| 3047 | if not isinstance( appName, types.StringType ): |
| 3048 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3049 | "be a string" ) |
| 3050 | return main.FALSE |
| 3051 | status = self.appStatus( appName ) |
| 3052 | if status == "INSTALLED": |
| 3053 | return main.TRUE |
| 3054 | elif status == "ACTIVE": |
| 3055 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3056 | if check and response == main.TRUE: |
| 3057 | for i in range(10): # try 10 times then give up |
| 3058 | status = self.appStatus( appName ) |
| 3059 | if status == "INSTALLED": |
| 3060 | return main.TRUE |
| 3061 | else: |
| 3062 | time.sleep( 1 ) |
| 3063 | return main.FALSE |
| 3064 | else: # not check or command didn't succeed |
| 3065 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3066 | elif status == "UNINSTALLED": |
| 3067 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3068 | "application '" + appName + "' which is not " + |
| 3069 | "installed." ) |
| 3070 | return main.TRUE |
| 3071 | else: |
| 3072 | main.log.error( "Unexpected return value from appStatus: " + |
| 3073 | str( status ) ) |
| 3074 | return main.ERROR |
| 3075 | except TypeError: |
| 3076 | main.log.exception( self.name + ": Object not as expected" ) |
| 3077 | return main.ERROR |
| 3078 | except pexpect.EOF: |
| 3079 | main.log.error( self.name + ": EOF exception found" ) |
| 3080 | main.log.error( self.name + ": " + self.handle.before ) |
| 3081 | main.cleanup() |
| 3082 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3083 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3084 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3085 | main.cleanup() |
| 3086 | main.exit() |
| 3087 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3088 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3089 | """ |
| 3090 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3091 | appName is the hierarchical app name, not the feature name |
| 3092 | If check is True, method will check the status of the app after the |
| 3093 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3094 | Returns main.TRUE if the command was successfully sent |
| 3095 | main.FALSE if the cli responded with an error or given |
| 3096 | incorrect input |
| 3097 | """ |
| 3098 | # TODO: check with Thomas about the state machine for apps |
| 3099 | try: |
| 3100 | if not isinstance( appName, types.StringType ): |
| 3101 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3102 | "be a string" ) |
| 3103 | return main.FALSE |
| 3104 | status = self.appStatus( appName ) |
| 3105 | if status == "INSTALLED": |
| 3106 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3107 | if check and response == main.TRUE: |
| 3108 | for i in range(10): # try 10 times then give up |
| 3109 | status = self.appStatus( appName ) |
| 3110 | if status == "UNINSTALLED": |
| 3111 | return main.TRUE |
| 3112 | else: |
| 3113 | time.sleep( 1 ) |
| 3114 | return main.FALSE |
| 3115 | else: # not check or command didn't succeed |
| 3116 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3117 | elif status == "ACTIVE": |
| 3118 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3119 | "application '" + appName + "' which is " + |
| 3120 | "currently active." ) |
| 3121 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3122 | if check and response == main.TRUE: |
| 3123 | for i in range(10): # try 10 times then give up |
| 3124 | status = self.appStatus( appName ) |
| 3125 | if status == "UNINSTALLED": |
| 3126 | return main.TRUE |
| 3127 | else: |
| 3128 | time.sleep( 1 ) |
| 3129 | return main.FALSE |
| 3130 | else: # not check or command didn't succeed |
| 3131 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3132 | elif status == "UNINSTALLED": |
| 3133 | return main.TRUE |
| 3134 | else: |
| 3135 | main.log.error( "Unexpected return value from appStatus: " + |
| 3136 | str( status ) ) |
| 3137 | return main.ERROR |
| 3138 | except TypeError: |
| 3139 | main.log.exception( self.name + ": Object not as expected" ) |
| 3140 | return main.ERROR |
| 3141 | except pexpect.EOF: |
| 3142 | main.log.error( self.name + ": EOF exception found" ) |
| 3143 | main.log.error( self.name + ": " + self.handle.before ) |
| 3144 | main.cleanup() |
| 3145 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3146 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3147 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3148 | main.cleanup() |
| 3149 | main.exit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3150 | |
| 3151 | def appIDs( self, jsonFormat=True ): |
| 3152 | """ |
| 3153 | Show the mappings between app id and app names given by the 'app-ids' |
| 3154 | cli command |
| 3155 | """ |
| 3156 | try: |
| 3157 | cmdStr = "app-ids" |
| 3158 | if jsonFormat: |
| 3159 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3160 | output = self.sendline( cmdStr ) |
| 3161 | assert "Error executing command" not in output |
| 3162 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3163 | except AssertionError: |
| 3164 | main.log.error( "Error in processing onos:app-ids command: " + |
| 3165 | str( output ) ) |
| 3166 | return None |
| 3167 | except TypeError: |
| 3168 | main.log.exception( self.name + ": Object not as expected" ) |
| 3169 | return None |
| 3170 | except pexpect.EOF: |
| 3171 | main.log.error( self.name + ": EOF exception found" ) |
| 3172 | main.log.error( self.name + ": " + self.handle.before ) |
| 3173 | main.cleanup() |
| 3174 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3175 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3176 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3177 | main.cleanup() |
| 3178 | main.exit() |
| 3179 | |
| 3180 | def appToIDCheck( self ): |
| 3181 | """ |
| 3182 | This method will check that each application's ID listed in 'apps' is |
| 3183 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3184 | there are no duplicate IDs issued. Note that an app ID should be |
| 3185 | a globaly unique numerical identifier for app/app-like features. Once |
| 3186 | an ID is registered, the ID is never freed up so that if an app is |
| 3187 | reinstalled it will have the same ID. |
| 3188 | |
| 3189 | Returns: main.TRUE if the check passes and |
| 3190 | main.FALSE if the check fails or |
| 3191 | main.ERROR if there is some error in processing the test |
| 3192 | """ |
| 3193 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3194 | bail = False |
| 3195 | ids = self.appIDs( jsonFormat=True ) |
| 3196 | if ids: |
| 3197 | ids = json.loads( ids ) |
| 3198 | else: |
| 3199 | main.log.error( "app-ids returned nothing:" + repr( ids ) ) |
| 3200 | bail = True |
| 3201 | apps = self.apps( jsonFormat=True ) |
| 3202 | if apps: |
| 3203 | apps = json.loads( apps ) |
| 3204 | else: |
| 3205 | main.log.error( "apps returned nothing:" + repr( apps ) ) |
| 3206 | bail = True |
| 3207 | if bail: |
| 3208 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3209 | result = main.TRUE |
| 3210 | for app in apps: |
| 3211 | appID = app.get( 'id' ) |
| 3212 | if appID is None: |
| 3213 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3214 | result = main.FALSE |
| 3215 | appName = app.get( 'name' ) |
| 3216 | if appName is None: |
| 3217 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3218 | result = main.FALSE |
| 3219 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3220 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3221 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3222 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3223 | if not current: # if ids doesn't have this id |
| 3224 | result = main.FALSE |
| 3225 | main.log.error( "'app-ids' does not have the ID for " + |
| 3226 | str( appName ) + " that apps does." ) |
| 3227 | elif len( current ) > 1: |
| 3228 | # there is more than one app with this ID |
| 3229 | result = main.FALSE |
| 3230 | # We will log this later in the method |
| 3231 | elif not current[0][ 'name' ] == appName: |
| 3232 | currentName = current[0][ 'name' ] |
| 3233 | result = main.FALSE |
| 3234 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3235 | " registered under id:" + str( appID ) + |
| 3236 | " but 'apps' has " + str( appName ) ) |
| 3237 | else: |
| 3238 | pass # id and name match! |
| 3239 | # now make sure that app-ids has no duplicates |
| 3240 | idsList = [] |
| 3241 | namesList = [] |
| 3242 | for item in ids: |
| 3243 | idsList.append( item[ 'id' ] ) |
| 3244 | namesList.append( item[ 'name' ] ) |
| 3245 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3246 | len( namesList ) != len( set( namesList ) ): |
| 3247 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3248 | + json.dumps( ids, |
| 3249 | sort_keys=True, |
| 3250 | indent=4, |
| 3251 | separators=( ',', ': ' ) ) ) |
| 3252 | result = main.FALSE |
| 3253 | return result |
| 3254 | except ( ValueError, TypeError ): |
| 3255 | main.log.exception( self.name + ": Object not as expected" ) |
| 3256 | return main.ERROR |
| 3257 | except pexpect.EOF: |
| 3258 | main.log.error( self.name + ": EOF exception found" ) |
| 3259 | main.log.error( self.name + ": " + self.handle.before ) |
| 3260 | main.cleanup() |
| 3261 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3262 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3263 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3264 | main.cleanup() |
| 3265 | main.exit() |
| 3266 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3267 | def getCfg( self, component=None, propName=None, short=False, |
| 3268 | jsonFormat=True ): |
| 3269 | """ |
| 3270 | Get configuration settings from onos cli |
| 3271 | Optional arguments: |
| 3272 | component - Optionally only list configurations for a specific |
| 3273 | component. If None, all components with configurations |
| 3274 | are displayed. Case Sensitive string. |
| 3275 | propName - If component is specified, propName option will show |
| 3276 | only this specific configuration from that component. |
| 3277 | Case Sensitive string. |
| 3278 | jsonFormat - Returns output as json. Note that this will override |
| 3279 | the short option |
| 3280 | short - Short, less verbose, version of configurations. |
| 3281 | This is overridden by the json option |
| 3282 | returns: |
| 3283 | Output from cli as a string or None on error |
| 3284 | """ |
| 3285 | try: |
| 3286 | baseStr = "cfg" |
| 3287 | cmdStr = " get" |
| 3288 | componentStr = "" |
| 3289 | if component: |
| 3290 | componentStr += " " + component |
| 3291 | if propName: |
| 3292 | componentStr += " " + propName |
| 3293 | if jsonFormat: |
| 3294 | baseStr += " -j" |
| 3295 | elif short: |
| 3296 | baseStr += " -s" |
| 3297 | output = self.sendline( baseStr + cmdStr + componentStr ) |
| 3298 | assert "Error executing command" not in output |
| 3299 | return output |
| 3300 | except AssertionError: |
| 3301 | main.log.error( "Error in processing 'cfg get' command: " + |
| 3302 | str( output ) ) |
| 3303 | return None |
| 3304 | except TypeError: |
| 3305 | main.log.exception( self.name + ": Object not as expected" ) |
| 3306 | return None |
| 3307 | except pexpect.EOF: |
| 3308 | main.log.error( self.name + ": EOF exception found" ) |
| 3309 | main.log.error( self.name + ": " + self.handle.before ) |
| 3310 | main.cleanup() |
| 3311 | main.exit() |
| 3312 | except Exception: |
| 3313 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3314 | main.cleanup() |
| 3315 | main.exit() |
| 3316 | |
| 3317 | def setCfg( self, component, propName, value=None, check=True ): |
| 3318 | """ |
| 3319 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3320 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3321 | component - The case sensitive name of the component whose |
| 3322 | property is to be set |
| 3323 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3324 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3325 | value - The value to set the property to. If None, will unset the |
| 3326 | property and revert it to it's default value(if applicable) |
| 3327 | check - Boolean, Check whether the option was successfully set this |
| 3328 | only applies when a value is given. |
| 3329 | returns: |
| 3330 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3331 | will return main.TRUE unless there is an error |
| 3332 | """ |
| 3333 | try: |
| 3334 | baseStr = "cfg" |
| 3335 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3336 | if value is not None: |
| 3337 | cmdStr += " " + str( value ) |
| 3338 | output = self.sendline( baseStr + cmdStr ) |
| 3339 | assert "Error executing command" not in output |
| 3340 | if value and check: |
| 3341 | results = self.getCfg( component=str( component ), |
| 3342 | propName=str( propName ), |
| 3343 | jsonFormat=True ) |
| 3344 | # Check if current value is what we just set |
| 3345 | try: |
| 3346 | jsonOutput = json.loads( results ) |
| 3347 | current = jsonOutput[ 'value' ] |
| 3348 | except ( ValueError, TypeError ): |
| 3349 | main.log.exception( "Error parsing cfg output" ) |
| 3350 | main.log.error( "output:" + repr( results ) ) |
| 3351 | return main.FALSE |
| 3352 | if current == str( value ): |
| 3353 | return main.TRUE |
| 3354 | return main.FALSE |
| 3355 | return main.TRUE |
| 3356 | except AssertionError: |
| 3357 | main.log.error( "Error in processing 'cfg set' command: " + |
| 3358 | str( output ) ) |
| 3359 | return main.FALSE |
| 3360 | except TypeError: |
| 3361 | main.log.exception( self.name + ": Object not as expected" ) |
| 3362 | return main.FALSE |
| 3363 | except pexpect.EOF: |
| 3364 | main.log.error( self.name + ": EOF exception found" ) |
| 3365 | main.log.error( self.name + ": " + self.handle.before ) |
| 3366 | main.cleanup() |
| 3367 | main.exit() |
| 3368 | except Exception: |
| 3369 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3370 | main.cleanup() |
| 3371 | main.exit() |
| 3372 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3373 | def setTestAdd( self, setName, values ): |
| 3374 | """ |
| 3375 | CLI command to add elements to a distributed set. |
| 3376 | Arguments: |
| 3377 | setName - The name of the set to add to. |
| 3378 | values - The value(s) to add to the set, space seperated. |
| 3379 | Example usages: |
| 3380 | setTestAdd( "set1", "a b c" ) |
| 3381 | setTestAdd( "set2", "1" ) |
| 3382 | returns: |
| 3383 | main.TRUE on success OR |
| 3384 | main.FALSE if elements were already in the set OR |
| 3385 | main.ERROR on error |
| 3386 | """ |
| 3387 | try: |
| 3388 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
| 3389 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3390 | try: |
| 3391 | # TODO: Maybe make this less hardcoded |
| 3392 | # ConsistentMap Exceptions |
| 3393 | assert "org.onosproject.store.service" not in output |
| 3394 | # Node not leader |
| 3395 | assert "java.lang.IllegalStateException" not in output |
| 3396 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3397 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3398 | "command: " + str( output ) ) |
| 3399 | retryTime = 30 # Conservative time, given by Madan |
| 3400 | main.log.info( "Waiting " + str( retryTime ) + |
| 3401 | "seconds before retrying." ) |
| 3402 | time.sleep( retryTime ) # Due to change in mastership |
| 3403 | output = self.sendline( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3404 | assert "Error executing command" not in output |
| 3405 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3406 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
| 3407 | main.log.info( self.name + ": " + output ) |
| 3408 | if re.search( positiveMatch, output): |
| 3409 | return main.TRUE |
| 3410 | elif re.search( negativeMatch, output): |
| 3411 | return main.FALSE |
| 3412 | else: |
| 3413 | main.log.error( self.name + ": setTestAdd did not" + |
| 3414 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3415 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3416 | return main.ERROR |
| 3417 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3418 | main.log.error( "Error in processing '" + cmdStr + "' command: " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3419 | str( output ) ) |
| 3420 | return main.ERROR |
| 3421 | except TypeError: |
| 3422 | main.log.exception( self.name + ": Object not as expected" ) |
| 3423 | return main.ERROR |
| 3424 | except pexpect.EOF: |
| 3425 | main.log.error( self.name + ": EOF exception found" ) |
| 3426 | main.log.error( self.name + ": " + self.handle.before ) |
| 3427 | main.cleanup() |
| 3428 | main.exit() |
| 3429 | except Exception: |
| 3430 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3431 | main.cleanup() |
| 3432 | main.exit() |
| 3433 | |
| 3434 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 3435 | """ |
| 3436 | CLI command to remove elements from a distributed set. |
| 3437 | Required arguments: |
| 3438 | setName - The name of the set to remove from. |
| 3439 | values - The value(s) to remove from the set, space seperated. |
| 3440 | Optional arguments: |
| 3441 | clear - Clear all elements from the set |
| 3442 | retain - Retain only the given values. (intersection of the |
| 3443 | original set and the given set) |
| 3444 | returns: |
| 3445 | main.TRUE on success OR |
| 3446 | main.FALSE if the set was not changed OR |
| 3447 | main.ERROR on error |
| 3448 | """ |
| 3449 | try: |
| 3450 | cmdStr = "set-test-remove " |
| 3451 | if clear: |
| 3452 | cmdStr += "-c " + str( setName ) |
| 3453 | elif retain: |
| 3454 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 3455 | else: |
| 3456 | cmdStr += str( setName ) + " " + str( values ) |
| 3457 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3458 | try: |
| 3459 | # TODO: Maybe make this less hardcoded |
| 3460 | # ConsistentMap Exceptions |
| 3461 | assert "org.onosproject.store.service" not in output |
| 3462 | # Node not leader |
| 3463 | assert "java.lang.IllegalStateException" not in output |
| 3464 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3465 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3466 | "command: " + str( output ) ) |
| 3467 | retryTime = 30 # Conservative time, given by Madan |
| 3468 | main.log.info( "Waiting " + str( retryTime ) + |
| 3469 | "seconds before retrying." ) |
| 3470 | time.sleep( retryTime ) # Due to change in mastership |
| 3471 | output = self.sendline( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3472 | assert "Error executing command" not in output |
| 3473 | main.log.info( self.name + ": " + output ) |
| 3474 | if clear: |
| 3475 | pattern = "Set " + str( setName ) + " cleared" |
| 3476 | if re.search( pattern, output ): |
| 3477 | return main.TRUE |
| 3478 | elif retain: |
| 3479 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 3480 | "only elements of set \[(.*)\]" |
| 3481 | negativePattern = str( setName ) + " was not changed by " +\ |
| 3482 | "retaining only elements of the set " +\ |
| 3483 | "\[(.*)\]" |
| 3484 | if re.search( positivePattern, output ): |
| 3485 | return main.TRUE |
| 3486 | elif re.search( negativePattern, output ): |
| 3487 | return main.FALSE |
| 3488 | else: |
| 3489 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 3490 | str( setName ) |
| 3491 | if ( len( values.split() ) == 1 ): |
| 3492 | negativePattern = "\[(.*)\] was not in set " +\ |
| 3493 | str( setName ) |
| 3494 | else: |
| 3495 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 3496 | str( setName ) |
| 3497 | if re.search( positivePattern, output ): |
| 3498 | return main.TRUE |
| 3499 | elif re.search( negativePattern, output ): |
| 3500 | return main.FALSE |
| 3501 | main.log.error( self.name + ": setTestRemove did not" + |
| 3502 | " match expected output" ) |
| 3503 | main.log.debug( self.name + " expected: " + pattern ) |
| 3504 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3505 | return main.ERROR |
| 3506 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3507 | main.log.error( "Error in processing '" + cmdStr + "' command: " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3508 | str( output ) ) |
| 3509 | return main.ERROR |
| 3510 | except TypeError: |
| 3511 | main.log.exception( self.name + ": Object not as expected" ) |
| 3512 | return main.ERROR |
| 3513 | except pexpect.EOF: |
| 3514 | main.log.error( self.name + ": EOF exception found" ) |
| 3515 | main.log.error( self.name + ": " + self.handle.before ) |
| 3516 | main.cleanup() |
| 3517 | main.exit() |
| 3518 | except Exception: |
| 3519 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3520 | main.cleanup() |
| 3521 | main.exit() |
| 3522 | |
| 3523 | def setTestGet( self, setName, values="" ): |
| 3524 | """ |
| 3525 | CLI command to get the elements in a distributed set. |
| 3526 | Required arguments: |
| 3527 | setName - The name of the set to remove from. |
| 3528 | Optional arguments: |
| 3529 | values - The value(s) to check if in the set, space seperated. |
| 3530 | returns: |
| 3531 | main.ERROR on error OR |
| 3532 | A list of elements in the set if no optional arguments are |
| 3533 | supplied OR |
| 3534 | A tuple containing the list then: |
| 3535 | main.FALSE if the given values are not in the set OR |
| 3536 | main.TRUE if the given values are in the set OR |
| 3537 | """ |
| 3538 | try: |
| 3539 | values = str( values ).strip() |
| 3540 | setName = str( setName ).strip() |
| 3541 | length = len( values.split() ) |
| 3542 | containsCheck = None |
| 3543 | # Patterns to match |
| 3544 | setPattern = "\[(.*)\]" |
| 3545 | pattern = "Items in set " + setName + ":\n" + setPattern |
| 3546 | containsTrue = "Set " + setName + " contains the value " + values |
| 3547 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 3548 | values |
| 3549 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 3550 | setPattern |
| 3551 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 3552 | " subset " + setPattern |
| 3553 | |
| 3554 | cmdStr = "set-test-get " |
| 3555 | cmdStr += setName + " " + values |
| 3556 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3557 | try: |
| 3558 | # TODO: Maybe make this less hardcoded |
| 3559 | # ConsistentMap Exceptions |
| 3560 | assert "org.onosproject.store.service" not in output |
| 3561 | # Node not leader |
| 3562 | assert "java.lang.IllegalStateException" not in output |
| 3563 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3564 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3565 | "command: " + str( output ) ) |
| 3566 | retryTime = 30 # Conservative time, given by Madan |
| 3567 | main.log.info( "Waiting " + str( retryTime ) + |
| 3568 | "seconds before retrying." ) |
| 3569 | time.sleep( retryTime ) # Due to change in mastership |
| 3570 | output = self.sendline( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3571 | assert "Error executing command" not in output |
| 3572 | main.log.info( self.name + ": " + output ) |
| 3573 | |
| 3574 | if length == 0: |
| 3575 | match = re.search( pattern, output ) |
| 3576 | else: # if given values |
| 3577 | if length == 1: # Contains output |
| 3578 | patternTrue = pattern + "\n" + containsTrue |
| 3579 | patternFalse = pattern + "\n" + containsFalse |
| 3580 | else: # ContainsAll output |
| 3581 | patternTrue = pattern + "\n" + containsAllTrue |
| 3582 | patternFalse = pattern + "\n" + containsAllFalse |
| 3583 | matchTrue = re.search( patternTrue, output ) |
| 3584 | matchFalse = re.search( patternFalse, output ) |
| 3585 | if matchTrue: |
| 3586 | containsCheck = main.TRUE |
| 3587 | match = matchTrue |
| 3588 | elif matchFalse: |
| 3589 | containsCheck = main.FALSE |
| 3590 | match = matchFalse |
| 3591 | else: |
| 3592 | main.log.error( self.name + " setTestGet did not match " +\ |
| 3593 | "expected output" ) |
| 3594 | main.log.debug( self.name + " expected: " + pattern ) |
| 3595 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3596 | match = None |
| 3597 | if match: |
| 3598 | setMatch = match.group( 1 ) |
| 3599 | if setMatch == '': |
| 3600 | setList = [] |
| 3601 | else: |
| 3602 | setList = setMatch.split( ", " ) |
| 3603 | if length > 0: |
| 3604 | return ( setList, containsCheck ) |
| 3605 | else: |
| 3606 | return setList |
| 3607 | else: # no match |
| 3608 | main.log.error( self.name + ": setTestGet did not" + |
| 3609 | " match expected output" ) |
| 3610 | main.log.debug( self.name + " expected: " + pattern ) |
| 3611 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3612 | return main.ERROR |
| 3613 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3614 | main.log.error( "Error in processing '" + cmdStr + "' command: " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3615 | str( output ) ) |
| 3616 | return main.ERROR |
| 3617 | except TypeError: |
| 3618 | main.log.exception( self.name + ": Object not as expected" ) |
| 3619 | return main.ERROR |
| 3620 | except pexpect.EOF: |
| 3621 | main.log.error( self.name + ": EOF exception found" ) |
| 3622 | main.log.error( self.name + ": " + self.handle.before ) |
| 3623 | main.cleanup() |
| 3624 | main.exit() |
| 3625 | except Exception: |
| 3626 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3627 | main.cleanup() |
| 3628 | main.exit() |
| 3629 | |
| 3630 | def setTestSize( self, setName ): |
| 3631 | """ |
| 3632 | CLI command to get the elements in a distributed set. |
| 3633 | Required arguments: |
| 3634 | setName - The name of the set to remove from. |
| 3635 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3636 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3637 | None on error |
| 3638 | """ |
| 3639 | try: |
| 3640 | # TODO: Should this check against the number of elements returned |
| 3641 | # and then return true/false based on that? |
| 3642 | setName = str( setName ).strip() |
| 3643 | # Patterns to match |
| 3644 | setPattern = "\[(.*)\]" |
| 3645 | pattern = "There are (\d+) items in set " + setName + ":\n" +\ |
| 3646 | setPattern |
| 3647 | cmdStr = "set-test-get -s " |
| 3648 | cmdStr += setName |
| 3649 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3650 | try: |
| 3651 | # TODO: Maybe make this less hardcoded |
| 3652 | # ConsistentMap Exceptions |
| 3653 | assert "org.onosproject.store.service" not in output |
| 3654 | # Node not leader |
| 3655 | assert "java.lang.IllegalStateException" not in output |
| 3656 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3657 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3658 | "command: " + str( output ) ) |
| 3659 | retryTime = 30 # Conservative time, given by Madan |
| 3660 | main.log.info( "Waiting " + str( retryTime ) + |
| 3661 | "seconds before retrying." ) |
| 3662 | time.sleep( retryTime ) # Due to change in mastership |
| 3663 | output = self.sendline( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3664 | assert "Error executing command" not in output |
| 3665 | main.log.info( self.name + ": " + output ) |
| 3666 | match = re.search( pattern, output ) |
| 3667 | if match: |
| 3668 | setSize = int( match.group( 1 ) ) |
| 3669 | setMatch = match.group( 2 ) |
| 3670 | if len( setMatch.split() ) == setSize: |
| 3671 | main.log.info( "The size returned by " + self.name + |
| 3672 | " matches the number of elements in " + |
| 3673 | "the returned set" ) |
| 3674 | else: |
| 3675 | main.log.error( "The size returned by " + self.name + |
| 3676 | " does not match the number of " + |
| 3677 | "elements in the returned set." ) |
| 3678 | return setSize |
| 3679 | else: # no match |
| 3680 | main.log.error( self.name + ": setTestGet did not" + |
| 3681 | " match expected output" ) |
| 3682 | main.log.debug( self.name + " expected: " + pattern ) |
| 3683 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3684 | return None |
| 3685 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3686 | main.log.error( "Error in processing '" + cmdStr + "' command: " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3687 | str( output ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3688 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3689 | except TypeError: |
| 3690 | main.log.exception( self.name + ": Object not as expected" ) |
| 3691 | return None |
| 3692 | except pexpect.EOF: |
| 3693 | main.log.error( self.name + ": EOF exception found" ) |
| 3694 | main.log.error( self.name + ": " + self.handle.before ) |
| 3695 | main.cleanup() |
| 3696 | main.exit() |
| 3697 | except Exception: |
| 3698 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3699 | main.cleanup() |
| 3700 | main.exit() |
| 3701 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 3702 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3703 | """ |
| 3704 | Command to list the various counters in the system. |
| 3705 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 3706 | if jsonFormat, a string of the json object returned by the cli |
| 3707 | command |
| 3708 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3709 | None on error |
| 3710 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3711 | try: |
| 3712 | counters = {} |
| 3713 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 3714 | if jsonFormat: |
| 3715 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3716 | output = self.sendline( cmdStr ) |
| 3717 | assert "Error executing command" not in output |
| 3718 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 3719 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3720 | except AssertionError: |
| 3721 | main.log.error( "Error in processing 'counters' command: " + |
| 3722 | str( output ) ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 3723 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3724 | except TypeError: |
| 3725 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 3726 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3727 | except pexpect.EOF: |
| 3728 | main.log.error( self.name + ": EOF exception found" ) |
| 3729 | main.log.error( self.name + ": " + self.handle.before ) |
| 3730 | main.cleanup() |
| 3731 | main.exit() |
| 3732 | except Exception: |
| 3733 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3734 | main.cleanup() |
| 3735 | main.exit() |
| 3736 | |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3737 | def counterTestAddAndGet( self, counter, delta=1, inMemory=False ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3738 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3739 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3740 | Required arguments: |
| 3741 | counter - The name of the counter to increment. |
| 3742 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3743 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3744 | inMemory - use in memory map for the counter |
| 3745 | returns: |
| 3746 | integer value of the counter or |
| 3747 | None on Error |
| 3748 | """ |
| 3749 | try: |
| 3750 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3751 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3752 | cmdStr = "counter-test-increment " |
| 3753 | if inMemory: |
| 3754 | cmdStr += "-i " |
| 3755 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3756 | if delta != 1: |
| 3757 | cmdStr += " " + str( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3758 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3759 | try: |
| 3760 | # TODO: Maybe make this less hardcoded |
| 3761 | # ConsistentMap Exceptions |
| 3762 | assert "org.onosproject.store.service" not in output |
| 3763 | # Node not leader |
| 3764 | assert "java.lang.IllegalStateException" not in output |
| 3765 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3766 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3767 | "command: " + str( output ) ) |
| 3768 | retryTime = 30 # Conservative time, given by Madan |
| 3769 | main.log.info( "Waiting " + str( retryTime ) + |
| 3770 | "seconds before retrying." ) |
| 3771 | time.sleep( retryTime ) # Due to change in mastership |
| 3772 | output = self.sendline( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3773 | assert "Error executing command" not in output |
| 3774 | main.log.info( self.name + ": " + output ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3775 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3776 | match = re.search( pattern, output ) |
| 3777 | if match: |
| 3778 | return int( match.group( 1 ) ) |
| 3779 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3780 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3781 | " match expected output." ) |
| 3782 | main.log.debug( self.name + " expected: " + pattern ) |
| 3783 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3784 | return None |
| 3785 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3786 | main.log.error( "Error in processing '" + cmdStr + "'" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3787 | " command: " + str( output ) ) |
| 3788 | return None |
| 3789 | except TypeError: |
| 3790 | main.log.exception( self.name + ": Object not as expected" ) |
| 3791 | return None |
| 3792 | except pexpect.EOF: |
| 3793 | main.log.error( self.name + ": EOF exception found" ) |
| 3794 | main.log.error( self.name + ": " + self.handle.before ) |
| 3795 | main.cleanup() |
| 3796 | main.exit() |
| 3797 | except Exception: |
| 3798 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3799 | main.cleanup() |
| 3800 | main.exit() |
| 3801 | |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3802 | def counterTestGetAndAdd( self, counter, delta=1, inMemory=False ): |
| 3803 | """ |
| 3804 | CLI command to get a distributed counter then add a delta to it. |
| 3805 | Required arguments: |
| 3806 | counter - The name of the counter to increment. |
| 3807 | Optional arguments: |
| 3808 | delta - The long to add to the counter |
| 3809 | inMemory - use in memory map for the counter |
| 3810 | returns: |
| 3811 | integer value of the counter or |
| 3812 | None on Error |
| 3813 | """ |
| 3814 | try: |
| 3815 | counter = str( counter ) |
| 3816 | delta = int( delta ) |
| 3817 | cmdStr = "counter-test-increment -g " |
| 3818 | if inMemory: |
| 3819 | cmdStr += "-i " |
| 3820 | cmdStr += counter |
| 3821 | if delta != 1: |
| 3822 | cmdStr += " " + str( delta ) |
| 3823 | output = self.sendline( cmdStr ) |
| 3824 | try: |
| 3825 | # TODO: Maybe make this less hardcoded |
| 3826 | # ConsistentMap Exceptions |
| 3827 | assert "org.onosproject.store.service" not in output |
| 3828 | # Node not leader |
| 3829 | assert "java.lang.IllegalStateException" not in output |
| 3830 | except AssertionError: |
| 3831 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 3832 | "command: " + str( output ) ) |
| 3833 | retryTime = 30 # Conservative time, given by Madan |
| 3834 | main.log.info( "Waiting " + str( retryTime ) + |
| 3835 | "seconds before retrying." ) |
| 3836 | time.sleep( retryTime ) # Due to change in mastership |
| 3837 | output = self.sendline( cmdStr ) |
| 3838 | assert "Error executing command" not in output |
| 3839 | main.log.info( self.name + ": " + output ) |
| 3840 | pattern = counter + " was updated to (-?\d+)" |
| 3841 | match = re.search( pattern, output ) |
| 3842 | if match: |
| 3843 | return int( match.group( 1 ) ) |
| 3844 | else: |
| 3845 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 3846 | " match expected output." ) |
| 3847 | main.log.debug( self.name + " expected: " + pattern ) |
| 3848 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3849 | return None |
| 3850 | except AssertionError: |
| 3851 | main.log.error( "Error in processing '" + cmdStr + "'" + |
| 3852 | " command: " + str( output ) ) |
| 3853 | return None |
| 3854 | except TypeError: |
| 3855 | main.log.exception( self.name + ": Object not as expected" ) |
| 3856 | return None |
| 3857 | except pexpect.EOF: |
| 3858 | main.log.error( self.name + ": EOF exception found" ) |
| 3859 | main.log.error( self.name + ": " + self.handle.before ) |
| 3860 | main.cleanup() |
| 3861 | main.exit() |
| 3862 | except Exception: |
| 3863 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3864 | main.cleanup() |
| 3865 | main.exit() |
| 3866 | |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 3867 | def summary( self, jsonFormat=True ): |
| 3868 | """ |
| 3869 | Description: Execute summary command in onos |
| 3870 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 3871 | no output |
| 3872 | |
| 3873 | """ |
| 3874 | try: |
| 3875 | cmdStr = "summary" |
| 3876 | if jsonFormat: |
| 3877 | cmdStr += " -j" |
| 3878 | handle = self.sendline( cmdStr ) |
| 3879 | |
| 3880 | if re.search( "Error:", handle ): |
| 3881 | main.log.error( self.name + ": summary() response: " + |
| 3882 | str( handle ) ) |
| 3883 | if not handle: |
| 3884 | main.log.error( self.name + ": There is no output in " + |
| 3885 | "summary command" ) |
| 3886 | return main.FALSE |
| 3887 | return handle |
| 3888 | except TypeError: |
| 3889 | main.log.exception( self.name + ": Object not as expected" ) |
| 3890 | return None |
| 3891 | except pexpect.EOF: |
| 3892 | main.log.error( self.name + ": EOF exception found" ) |
| 3893 | main.log.error( self.name + ": " + self.handle.before ) |
| 3894 | main.cleanup() |
| 3895 | main.exit() |
| 3896 | except Exception: |
| 3897 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3898 | main.cleanup() |
| 3899 | main.exit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 3900 | |
| 3901 | def transactionalMapGet( self, keyName, inMemory=False ): |
| 3902 | """ |
| 3903 | CLI command to get the value of a key in a consistent map using |
| 3904 | transactions. This a test function and can only get keys from the |
| 3905 | test map hard coded into the cli command |
| 3906 | Required arguments: |
| 3907 | keyName - The name of the key to get |
| 3908 | Optional arguments: |
| 3909 | inMemory - use in memory map for the counter |
| 3910 | returns: |
| 3911 | The string value of the key or |
| 3912 | None on Error |
| 3913 | """ |
| 3914 | try: |
| 3915 | keyName = str( keyName ) |
| 3916 | cmdStr = "transactional-map-test-get " |
| 3917 | if inMemory: |
| 3918 | cmdStr += "-i " |
| 3919 | cmdStr += keyName |
| 3920 | output = self.sendline( cmdStr ) |
| 3921 | try: |
| 3922 | # TODO: Maybe make this less hardcoded |
| 3923 | # ConsistentMap Exceptions |
| 3924 | assert "org.onosproject.store.service" not in output |
| 3925 | # Node not leader |
| 3926 | assert "java.lang.IllegalStateException" not in output |
| 3927 | except AssertionError: |
| 3928 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 3929 | "command: " + str( output ) ) |
| 3930 | return None |
| 3931 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 3932 | if "Key " + keyName + " not found." in output: |
| 3933 | return None |
| 3934 | else: |
| 3935 | match = re.search( pattern, output ) |
| 3936 | if match: |
| 3937 | return match.groupdict()[ 'value' ] |
| 3938 | else: |
| 3939 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 3940 | " match expected output." ) |
| 3941 | main.log.debug( self.name + " expected: " + pattern ) |
| 3942 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3943 | return None |
| 3944 | except TypeError: |
| 3945 | main.log.exception( self.name + ": Object not as expected" ) |
| 3946 | return None |
| 3947 | except pexpect.EOF: |
| 3948 | main.log.error( self.name + ": EOF exception found" ) |
| 3949 | main.log.error( self.name + ": " + self.handle.before ) |
| 3950 | main.cleanup() |
| 3951 | main.exit() |
| 3952 | except Exception: |
| 3953 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3954 | main.cleanup() |
| 3955 | main.exit() |
| 3956 | |
| 3957 | def transactionalMapPut( self, numKeys, value, inMemory=False ): |
| 3958 | """ |
| 3959 | CLI command to put a value into 'numKeys' number of keys in a |
| 3960 | consistent map using transactions. This a test function and can only |
| 3961 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 3962 | Required arguments: |
| 3963 | numKeys - Number of keys to add the value to |
| 3964 | value - The string value to put into the keys |
| 3965 | Optional arguments: |
| 3966 | inMemory - use in memory map for the counter |
| 3967 | returns: |
| 3968 | A dictionary whose keys are the name of the keys put into the map |
| 3969 | and the values of the keys are dictionaries whose key-values are |
| 3970 | 'value': value put into map and optionaly |
| 3971 | 'oldValue': Previous value in the key or |
| 3972 | None on Error |
| 3973 | |
| 3974 | Example output |
| 3975 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 3976 | 'Key2': {'value': 'Testing'} } |
| 3977 | """ |
| 3978 | try: |
| 3979 | numKeys = str( numKeys ) |
| 3980 | value = str( value ) |
| 3981 | cmdStr = "transactional-map-test-put " |
| 3982 | if inMemory: |
| 3983 | cmdStr += "-i " |
| 3984 | cmdStr += numKeys + " " + value |
| 3985 | output = self.sendline( cmdStr ) |
| 3986 | try: |
| 3987 | # TODO: Maybe make this less hardcoded |
| 3988 | # ConsistentMap Exceptions |
| 3989 | assert "org.onosproject.store.service" not in output |
| 3990 | # Node not leader |
| 3991 | assert "java.lang.IllegalStateException" not in output |
| 3992 | except AssertionError: |
| 3993 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 3994 | "command: " + str( output ) ) |
| 3995 | return None |
| 3996 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 3997 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 3998 | results = {} |
| 3999 | for line in output.splitlines(): |
| 4000 | new = re.search( newPattern, line ) |
| 4001 | updated = re.search( updatedPattern, line ) |
| 4002 | if new: |
| 4003 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4004 | elif updated: |
| 4005 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
| 4006 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
| 4007 | else: |
| 4008 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4009 | " match expected output." ) |
| 4010 | main.log.debug( self.name + " expected: " + pattern ) |
| 4011 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4012 | return results |
| 4013 | except TypeError: |
| 4014 | main.log.exception( self.name + ": Object not as expected" ) |
| 4015 | return None |
| 4016 | except pexpect.EOF: |
| 4017 | main.log.error( self.name + ": EOF exception found" ) |
| 4018 | main.log.error( self.name + ": " + self.handle.before ) |
| 4019 | main.cleanup() |
| 4020 | main.exit() |
| 4021 | except Exception: |
| 4022 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4023 | main.cleanup() |
| 4024 | main.exit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4025 | def maps( self, jsonFormat=True ): |
| 4026 | """ |
| 4027 | Description: Returns result of onos:maps |
| 4028 | Optional: |
| 4029 | * jsonFormat: enable json formatting of output |
| 4030 | """ |
| 4031 | try: |
| 4032 | cmdStr = "maps" |
| 4033 | if jsonFormat: |
| 4034 | cmdStr += " -j" |
| 4035 | handle = self.sendline( cmdStr ) |
| 4036 | return handle |
| 4037 | except TypeError: |
| 4038 | main.log.exception( self.name + ": Object not as expected" ) |
| 4039 | return None |
| 4040 | except pexpect.EOF: |
| 4041 | main.log.error( self.name + ": EOF exception found" ) |
| 4042 | main.log.error( self.name + ": " + self.handle.before ) |
| 4043 | main.cleanup() |
| 4044 | main.exit() |
| 4045 | except Exception: |
| 4046 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4047 | main.cleanup() |
| 4048 | main.exit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4049 | |
| 4050 | def getSwController( self, uri, jsonFormat=True ): |
| 4051 | """ |
| 4052 | Descrition: Gets the controller information from the device |
| 4053 | """ |
| 4054 | try: |
| 4055 | cmd = "device-controllers " |
| 4056 | if jsonFormat: |
| 4057 | cmd += "-j " |
| 4058 | response = self.sendline( cmd + uri ) |
| 4059 | return response |
| 4060 | except TypeError: |
| 4061 | main.log.exception( self.name + ": Object not as expected" ) |
| 4062 | return None |
| 4063 | except pexpect.EOF: |
| 4064 | main.log.error( self.name + ": EOF exception found" ) |
| 4065 | main.log.error( self.name + ": " + self.handle.before ) |
| 4066 | main.cleanup() |
| 4067 | main.exit() |
| 4068 | except Exception: |
| 4069 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4070 | main.cleanup() |
| 4071 | main.exit() |
| 4072 | |
| 4073 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4074 | """ |
| 4075 | Descrition: sets the controller(s) for the specified device |
| 4076 | |
| 4077 | Parameters: |
| 4078 | Required: uri - String: The uri of the device(switch). |
| 4079 | ip - String or List: The ip address of the controller. |
| 4080 | This parameter can be formed in a couple of different ways. |
| 4081 | VALID: |
| 4082 | 10.0.0.1 - just the ip address |
| 4083 | tcp:10.0.0.1 - the protocol and the ip address |
| 4084 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4085 | so that you can add controllers with different |
| 4086 | protocols and ports |
| 4087 | INVALID: |
| 4088 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4089 | |
| 4090 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4091 | port - The port number. |
| 4092 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4093 | |
| 4094 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4095 | """ |
| 4096 | try: |
| 4097 | cmd = "device-setcontrollers" |
| 4098 | |
| 4099 | if jsonFormat: |
| 4100 | cmd += " -j" |
| 4101 | cmd += " " + uri |
| 4102 | if isinstance( ip, str ): |
| 4103 | ip = [ip] |
| 4104 | for item in ip: |
| 4105 | if ":" in item: |
| 4106 | sitem = item.split( ":" ) |
| 4107 | if len(sitem) == 3: |
| 4108 | cmd += " " + item |
| 4109 | elif "." in sitem[1]: |
| 4110 | cmd += " {}:{}".format(item, port) |
| 4111 | else: |
| 4112 | main.log.error( "Malformed entry: " + item ) |
| 4113 | raise TypeError |
| 4114 | else: |
| 4115 | cmd += " {}:{}:{}".format( proto, item, port ) |
| 4116 | |
| 4117 | response = self.sendline( cmd ) |
| 4118 | |
| 4119 | if "Error" in response: |
| 4120 | main.log.error( response ) |
| 4121 | return main.FALSE |
| 4122 | |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4123 | return main.TRUE |
| 4124 | |
| 4125 | except TypeError: |
| 4126 | main.log.exception( self.name + ": Object not as expected" ) |
| 4127 | return main.FALSE |
| 4128 | except pexpect.EOF: |
| 4129 | main.log.error( self.name + ": EOF exception found" ) |
| 4130 | main.log.error( self.name + ": " + self.handle.before ) |
| 4131 | main.cleanup() |
| 4132 | main.exit() |
| 4133 | except Exception: |
| 4134 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4135 | main.cleanup() |
| 4136 | main.exit() |