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