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