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