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 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 17 | Modified 2016 by ON.Lab |
| 18 | |
| 19 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 20 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 21 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 22 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 23 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 24 | import pexpect |
| 25 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 26 | import json |
| 27 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 28 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 29 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 30 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 31 | from core.graph import Graph |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 32 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 33 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 34 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 35 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 36 | def __init__( self ): |
| 37 | """ |
| 38 | Initialize client |
| 39 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 40 | self.name = None |
| 41 | self.home = None |
| 42 | self.handle = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 43 | self.graph = Graph() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | super( CLI, self ).__init__() |
| 45 | |
| 46 | def connect( self, **connectargs ): |
| 47 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 48 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 49 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 50 | try: |
| 51 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 53 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 54 | for key in self.options: |
| 55 | if key == "home": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 56 | self.home = self.options[ 'home' ] |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 57 | break |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 58 | if self.home is None or self.home == "": |
Jon Hall | e94919c | 2015-03-23 11:42:57 -0700 | [diff] [blame] | 59 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 60 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 61 | for key in self.options: |
| 62 | if key == 'onosIp': |
| 63 | self.onosIp = self.options[ 'onosIp' ] |
| 64 | break |
| 65 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 66 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 67 | |
| 68 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 69 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 70 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 71 | else: |
| 72 | main.log.info( self.name + |
| 73 | ": Trying to connect to " + |
| 74 | self.ip_address ) |
| 75 | |
| 76 | except KeyError: |
| 77 | main.log.info( "Invalid host name," + |
| 78 | " connecting to local host instead" ) |
| 79 | self.ip_address = 'localhost' |
| 80 | except Exception as inst: |
| 81 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 82 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 83 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 84 | user_name=self.user_name, |
| 85 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 86 | port=self.port, |
| 87 | pwd=self.pwd, |
| 88 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 89 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 90 | self.handle.sendline( "cd " + self.home ) |
| 91 | self.handle.expect( "\$" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 92 | if self.handle: |
| 93 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | else: |
| 95 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 96 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 97 | except TypeError: |
| 98 | main.log.exception( self.name + ": Object not as expected" ) |
| 99 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 100 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 101 | main.log.error( self.name + ": EOF exception found" ) |
| 102 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 103 | main.cleanup() |
| 104 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 105 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 106 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 107 | main.cleanup() |
| 108 | main.exit() |
| 109 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 110 | def disconnect( self ): |
| 111 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 112 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 113 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 114 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 115 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 116 | if self.handle: |
| 117 | i = self.logout() |
| 118 | if i == main.TRUE: |
| 119 | self.handle.sendline( "" ) |
| 120 | self.handle.expect( "\$" ) |
| 121 | self.handle.sendline( "exit" ) |
| 122 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 123 | except TypeError: |
| 124 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 125 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 126 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 127 | main.log.error( self.name + ": EOF exception found" ) |
| 128 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 129 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 130 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 131 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 132 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 133 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | response = main.FALSE |
| 135 | return response |
| 136 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 137 | def logout( self ): |
| 138 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 139 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 140 | Returns main.TRUE if exited CLI and |
| 141 | main.FALSE on timeout (not guranteed you are disconnected) |
| 142 | None on TypeError |
| 143 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 144 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 145 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 146 | if self.handle: |
| 147 | self.handle.sendline( "" ) |
| 148 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ], |
| 149 | timeout=10 ) |
| 150 | if i == 0: # In ONOS CLI |
| 151 | self.handle.sendline( "logout" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 152 | j = self.handle.expect( [ "\$", |
| 153 | "Command not found:", |
| 154 | pexpect.TIMEOUT ] ) |
| 155 | if j == 0: # Successfully logged out |
| 156 | return main.TRUE |
| 157 | elif j == 1 or j == 2: |
| 158 | # ONOS didn't fully load, and logout command isn't working |
| 159 | # or the command timed out |
| 160 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 161 | try: |
| 162 | self.handle.expect( "\$" ) |
| 163 | except pexpect.TIMEOUT: |
| 164 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 165 | return main.TRUE |
| 166 | else: # some other output |
| 167 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 168 | repr( self.handle.before ) ) |
| 169 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 170 | elif i == 1: # not in CLI |
| 171 | return main.TRUE |
| 172 | elif i == 3: # Timeout |
| 173 | return main.FALSE |
| 174 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 175 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 176 | except TypeError: |
| 177 | main.log.exception( self.name + ": Object not as expected" ) |
| 178 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 179 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 181 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 182 | main.cleanup() |
| 183 | main.exit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 184 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 185 | main.log.error( self.name + |
| 186 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 187 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 188 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 189 | main.cleanup() |
| 190 | main.exit() |
| 191 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 192 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 193 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 194 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 196 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 197 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 198 | try: |
| 199 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 200 | main.log.error( "Must define cellname" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 201 | main.cleanup() |
| 202 | main.exit() |
| 203 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 204 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 205 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 206 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 207 | # and that this driver will have to change accordingly |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 208 | self.handle.expect(str(cellname)) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 209 | handleBefore = self.handle.before |
| 210 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 211 | # Get the rest of the handle |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 212 | self.handle.sendline("") |
| 213 | self.handle.expect("\$") |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 214 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 215 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 216 | main.log.info( "Cell call returned: " + handleBefore + |
| 217 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 218 | |
| 219 | return main.TRUE |
| 220 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 221 | except TypeError: |
| 222 | main.log.exception( self.name + ": Object not as expected" ) |
| 223 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 224 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 225 | main.log.error( self.name + ": eof exception found" ) |
| 226 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 227 | main.cleanup() |
| 228 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 229 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 230 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 231 | main.cleanup() |
| 232 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 233 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 234 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 235 | commandlineTimeout=10, onosStartTimeout=60 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 236 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 237 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 238 | by user would be used to set the current karaf shell idle timeout. |
| 239 | 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] | 240 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 241 | Below is an example to start a session with 60 seconds idle timeout |
| 242 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 243 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 244 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 245 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 246 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 247 | Note: karafTimeout is left as str so that this could be read |
| 248 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 249 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 250 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 251 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 252 | self.handle.sendline( "" ) |
| 253 | x = self.handle.expect( [ |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 254 | "\$", "onos>" ], commandlineTimeout) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 255 | |
| 256 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 257 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 258 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 259 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 260 | # Wait for onos start ( -w ) and enter onos cli |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 261 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 262 | i = self.handle.expect( [ |
| 263 | "onos>", |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 264 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 265 | |
| 266 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 267 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 268 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 269 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 270 | "config:property-set -p org.apache.karaf.shell\ |
| 271 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 272 | karafTimeout ) |
| 273 | self.handle.expect( "\$" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 274 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 275 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 276 | return main.TRUE |
| 277 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 278 | # If failed, send ctrl+c to process and try again |
| 279 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 280 | self.handle.send( "\x03" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 281 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 282 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 283 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 284 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 285 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 286 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 287 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 288 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 289 | "config:property-set -p org.apache.karaf.shell\ |
| 290 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 291 | karafTimeout ) |
| 292 | self.handle.expect( "\$" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 293 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 294 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 295 | return main.TRUE |
| 296 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 297 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 298 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 299 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 300 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 301 | except TypeError: |
| 302 | main.log.exception( self.name + ": Object not as expected" ) |
| 303 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 304 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 305 | main.log.error( self.name + ": EOF exception found" ) |
| 306 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 307 | main.cleanup() |
| 308 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 309 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 310 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 311 | main.cleanup() |
| 312 | main.exit() |
| 313 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 314 | def startCellCli( self, karafTimeout="", |
| 315 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 316 | """ |
| 317 | Start CLI on onos ecll handle. |
| 318 | |
| 319 | karafTimeout is an optional argument. karafTimeout value passed |
| 320 | by user would be used to set the current karaf shell idle timeout. |
| 321 | Note that when ever this property is modified the shell will exit and |
| 322 | the subsequent login would reflect new idle timeout. |
| 323 | Below is an example to start a session with 60 seconds idle timeout |
| 324 | ( input value is in milliseconds ): |
| 325 | |
| 326 | tValue = "60000" |
| 327 | |
| 328 | Note: karafTimeout is left as str so that this could be read |
| 329 | and passed to startOnosCli from PARAMS file as str. |
| 330 | """ |
| 331 | |
| 332 | try: |
| 333 | self.handle.sendline( "" ) |
| 334 | x = self.handle.expect( [ |
| 335 | "\$", "onos>" ], commandlineTimeout) |
| 336 | |
| 337 | if x == 1: |
| 338 | main.log.info( "ONOS cli is already running" ) |
| 339 | return main.TRUE |
| 340 | |
| 341 | # Wait for onos start ( -w ) and enter onos cli |
| 342 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 343 | i = self.handle.expect( [ |
| 344 | "onos>", |
| 345 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 346 | |
| 347 | if i == 0: |
| 348 | main.log.info( self.name + " CLI Started successfully" ) |
| 349 | if karafTimeout: |
| 350 | self.handle.sendline( |
| 351 | "config:property-set -p org.apache.karaf.shell\ |
| 352 | sshIdleTimeout " + |
| 353 | karafTimeout ) |
| 354 | self.handle.expect( "\$" ) |
| 355 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 356 | self.handle.expect( "onos>" ) |
| 357 | return main.TRUE |
| 358 | else: |
| 359 | # If failed, send ctrl+c to process and try again |
| 360 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 361 | self.handle.send( "\x03" ) |
| 362 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 363 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 364 | timeout=30 ) |
| 365 | if i == 0: |
| 366 | main.log.info( self.name + " CLI Started " + |
| 367 | "successfully after retry attempt" ) |
| 368 | if karafTimeout: |
| 369 | self.handle.sendline( |
| 370 | "config:property-set -p org.apache.karaf.shell\ |
| 371 | sshIdleTimeout " + |
| 372 | karafTimeout ) |
| 373 | self.handle.expect( "\$" ) |
| 374 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 375 | self.handle.expect( "onos>" ) |
| 376 | return main.TRUE |
| 377 | else: |
| 378 | main.log.error( "Connection to CLI " + |
| 379 | self.name + " timeout" ) |
| 380 | return main.FALSE |
| 381 | |
| 382 | except TypeError: |
| 383 | main.log.exception( self.name + ": Object not as expected" ) |
| 384 | return None |
| 385 | except pexpect.EOF: |
| 386 | main.log.error( self.name + ": EOF exception found" ) |
| 387 | main.log.error( self.name + ": " + self.handle.before ) |
| 388 | main.cleanup() |
| 389 | main.exit() |
| 390 | except Exception: |
| 391 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 392 | main.cleanup() |
| 393 | main.exit() |
| 394 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 395 | def log( self, cmdStr, level="",noExit=False): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 396 | """ |
| 397 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 398 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 399 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 400 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 401 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 402 | Level defaults to INFO |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 403 | """ |
| 404 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 405 | lvlStr = "" |
| 406 | if level: |
| 407 | lvlStr = "--level=" + level |
| 408 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 409 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 410 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 411 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 412 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 413 | response = self.handle.before |
| 414 | if re.search( "Error", response ): |
| 415 | return main.FALSE |
| 416 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 417 | except pexpect.TIMEOUT: |
| 418 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 419 | if noExit: |
| 420 | main.cleanup() |
| 421 | return None |
| 422 | else: |
| 423 | main.cleanup() |
| 424 | main.exit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 425 | except pexpect.EOF: |
| 426 | main.log.error( self.name + ": EOF exception found" ) |
| 427 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 428 | if noExit: |
| 429 | main.cleanup() |
| 430 | return None |
| 431 | else: |
| 432 | main.cleanup() |
| 433 | main.exit() |
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!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 436 | if noExit: |
| 437 | main.cleanup() |
| 438 | return None |
| 439 | else: |
| 440 | main.cleanup() |
| 441 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 442 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 443 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 444 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 445 | Send a completely user specified string to |
| 446 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 447 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 448 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 449 | if noExit is True, TestON will not exit, and return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 450 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 451 | Warning: There are no sanity checking to commands |
| 452 | sent using this method. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 453 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 454 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 455 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 456 | # Try to reconnect if disconnected from cli |
| 457 | self.handle.sendline( "" ) |
| 458 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ] ) |
| 459 | if i == 1: |
| 460 | main.log.error( self.name + ": onos cli session closed. ") |
| 461 | if self.onosIp: |
| 462 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 463 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 464 | if reconnectResult: |
| 465 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 466 | else: |
| 467 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 468 | if noExit: |
| 469 | return None |
| 470 | else: |
| 471 | main.cleanup() |
| 472 | main.exit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 473 | else: |
| 474 | main.cleanup() |
| 475 | main.exit() |
| 476 | if i == 2: |
| 477 | self.handle.sendline( "" ) |
| 478 | self.handle.expect( "onos>" ) |
| 479 | |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 480 | if debug: |
| 481 | # NOTE: This adds and average of .4 seconds per call |
| 482 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 483 | self.log( logStr,noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 484 | self.handle.sendline( cmdStr ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 485 | i = self.handle.expect( ["onos>", "\$"], timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 486 | response = self.handle.before |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 487 | # TODO: do something with i |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 488 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
| 489 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 490 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 491 | main.log.debug( self.name + ": Raw output" ) |
| 492 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 493 | |
| 494 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 495 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 496 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 497 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 498 | main.log.debug( self.name + ": ansiEscape output" ) |
| 499 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 500 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 501 | # Remove extra return chars that get added |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 502 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 503 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 504 | main.log.debug( self.name + ": Removed extra returns " + |
| 505 | "from output" ) |
| 506 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 507 | |
| 508 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 509 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 510 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 511 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 512 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 513 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 514 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 515 | output = response.split( cmdStr.strip(), 1 ) |
| 516 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 517 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 518 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 519 | main.log.debug( self.name + ": " + repr( r ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 520 | output = output[1].strip() |
| 521 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 522 | main.log.info( "Response from ONOS: {}".format( output ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 523 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 524 | except pexpect.TIMEOUT: |
| 525 | main.log.error( self.name + ":ONOS timeout" ) |
| 526 | if debug: |
| 527 | main.log.debug( self.handle.before ) |
| 528 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 529 | except IndexError: |
| 530 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 531 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 532 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 533 | except TypeError: |
| 534 | main.log.exception( self.name + ": Object not as expected" ) |
| 535 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 536 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 537 | main.log.error( self.name + ": EOF exception found" ) |
| 538 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 539 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 540 | return None |
| 541 | else: |
| 542 | main.cleanup() |
| 543 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 544 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 545 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 546 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 547 | return None |
| 548 | else: |
| 549 | main.cleanup() |
| 550 | main.exit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 551 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 552 | # IMPORTANT NOTE: |
| 553 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 554 | # the cli command changing 'a:b' with 'aB'. |
| 555 | # Ex ) onos:topology > onosTopology |
| 556 | # onos:links > onosLinks |
| 557 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 558 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 559 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 560 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 561 | Adds a new cluster node by ID and address information. |
| 562 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 563 | * nodeId |
| 564 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 565 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 566 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 567 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 568 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 569 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 570 | str( ONOSIp ) + " " + str( tcpPort ) |
| 571 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 572 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 573 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 574 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 575 | main.log.error( "Error in adding node" ) |
| 576 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 577 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 578 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 579 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 580 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 581 | except AssertionError: |
| 582 | main.log.exception( "" ) |
| 583 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 584 | except TypeError: |
| 585 | main.log.exception( self.name + ": Object not as expected" ) |
| 586 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 587 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 588 | main.log.error( self.name + ": EOF exception found" ) |
| 589 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 590 | main.cleanup() |
| 591 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 592 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 593 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 594 | main.cleanup() |
| 595 | main.exit() |
| 596 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 597 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 598 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 599 | Removes a cluster by ID |
| 600 | Issues command: 'remove-node [<node-id>]' |
| 601 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 602 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 603 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 604 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 605 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 606 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 607 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 608 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 609 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 610 | if re.search( "Error", handle ): |
| 611 | main.log.error( "Error in removing node" ) |
| 612 | main.log.error( handle ) |
| 613 | return main.FALSE |
| 614 | else: |
| 615 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 616 | except AssertionError: |
| 617 | main.log.exception( "" ) |
| 618 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 619 | except TypeError: |
| 620 | main.log.exception( self.name + ": Object not as expected" ) |
| 621 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 622 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 623 | main.log.error( self.name + ": EOF exception found" ) |
| 624 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 625 | main.cleanup() |
| 626 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 627 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 628 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 629 | main.cleanup() |
| 630 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 631 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 632 | def nodes( self, jsonFormat=True): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 633 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 634 | List the nodes currently visible |
| 635 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 636 | Optional argument: |
| 637 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 638 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 639 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 640 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 641 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 642 | cmdStr += " -j" |
| 643 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 644 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 645 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 646 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 647 | except AssertionError: |
| 648 | main.log.exception( "" ) |
| 649 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 650 | except TypeError: |
| 651 | main.log.exception( self.name + ": Object not as expected" ) |
| 652 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 653 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 654 | main.log.error( self.name + ": EOF exception found" ) |
| 655 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 656 | main.cleanup() |
| 657 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 658 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 659 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 660 | main.cleanup() |
| 661 | main.exit() |
| 662 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 663 | def topology( self ): |
| 664 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 665 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 666 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 667 | Return: |
| 668 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 669 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 670 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 671 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 672 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 673 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 674 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 675 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 676 | except AssertionError: |
| 677 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 678 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 679 | except TypeError: |
| 680 | main.log.exception( self.name + ": Object not as expected" ) |
| 681 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 682 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 683 | main.log.error( self.name + ": EOF exception found" ) |
| 684 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 685 | main.cleanup() |
| 686 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 687 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 688 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 689 | main.cleanup() |
| 690 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 691 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 692 | def deviceRemove( self, deviceId ): |
| 693 | """ |
| 694 | Removes particular device from storage |
| 695 | |
| 696 | TODO: refactor this function |
| 697 | """ |
| 698 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 699 | cmdStr = "device-remove " + str( deviceId ) |
| 700 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 701 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 702 | if re.search( "Error", handle ): |
| 703 | main.log.error( "Error in removing device" ) |
| 704 | main.log.error( handle ) |
| 705 | return main.FALSE |
| 706 | else: |
| 707 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 708 | except AssertionError: |
| 709 | main.log.exception( "" ) |
| 710 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 711 | except TypeError: |
| 712 | main.log.exception( self.name + ": Object not as expected" ) |
| 713 | return None |
| 714 | except pexpect.EOF: |
| 715 | main.log.error( self.name + ": EOF exception found" ) |
| 716 | main.log.error( self.name + ": " + self.handle.before ) |
| 717 | main.cleanup() |
| 718 | main.exit() |
| 719 | except Exception: |
| 720 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 721 | main.cleanup() |
| 722 | main.exit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 723 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 724 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 725 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 726 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 727 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 728 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 729 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 730 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 731 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 732 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 733 | cmdStr += " -j" |
| 734 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 735 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 736 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 737 | except AssertionError: |
| 738 | main.log.exception( "" ) |
| 739 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 740 | except TypeError: |
| 741 | main.log.exception( self.name + ": Object not as expected" ) |
| 742 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 743 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 744 | main.log.error( self.name + ": EOF exception found" ) |
| 745 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 746 | main.cleanup() |
| 747 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 748 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 749 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 750 | main.cleanup() |
| 751 | main.exit() |
| 752 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 753 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 754 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 755 | This balances the devices across all controllers |
| 756 | by issuing command: 'onos> onos:balance-masters' |
| 757 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 758 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 759 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 760 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 761 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 762 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 763 | if re.search( "Error", handle ): |
| 764 | main.log.error( "Error in balancing masters" ) |
| 765 | main.log.error( handle ) |
| 766 | return main.FALSE |
| 767 | else: |
| 768 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 769 | except AssertionError: |
| 770 | main.log.exception( "" ) |
| 771 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 772 | except TypeError: |
| 773 | main.log.exception( self.name + ": Object not as expected" ) |
| 774 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 775 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 776 | main.log.error( self.name + ": EOF exception found" ) |
| 777 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 778 | main.cleanup() |
| 779 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 780 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 781 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 782 | main.cleanup() |
| 783 | main.exit() |
| 784 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 785 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 786 | """ |
| 787 | Returns the output of the masters command. |
| 788 | Optional argument: |
| 789 | * jsonFormat - boolean indicating if you want output in json |
| 790 | """ |
| 791 | try: |
| 792 | cmdStr = "onos:masters" |
| 793 | if jsonFormat: |
| 794 | cmdStr += " -j" |
| 795 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 796 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 797 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 798 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 799 | except AssertionError: |
| 800 | main.log.exception( "" ) |
| 801 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 802 | except TypeError: |
| 803 | main.log.exception( self.name + ": Object not as expected" ) |
| 804 | return None |
| 805 | except pexpect.EOF: |
| 806 | main.log.error( self.name + ": EOF exception found" ) |
| 807 | main.log.error( self.name + ": " + self.handle.before ) |
| 808 | main.cleanup() |
| 809 | main.exit() |
| 810 | except Exception: |
| 811 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 812 | main.cleanup() |
| 813 | main.exit() |
| 814 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 815 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 816 | """ |
| 817 | Uses the master command to check that the devices' leadership |
| 818 | is evenly divided |
| 819 | |
| 820 | Dependencies: checkMasters() and summary() |
| 821 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 822 | Returns main.TRUE if the devices are balanced |
| 823 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 824 | Exits on Exception |
| 825 | Returns None on TypeError |
| 826 | """ |
| 827 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 828 | summaryOutput = self.summary() |
| 829 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 830 | except ( TypeError, ValueError ): |
| 831 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 832 | return None |
| 833 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 834 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 835 | mastersOutput = self.checkMasters() |
| 836 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 837 | first = masters[ 0 ][ "size" ] |
| 838 | for master in masters: |
| 839 | totalOwnedDevices += master[ "size" ] |
| 840 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 841 | main.log.error( "Mastership not balanced" ) |
| 842 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 843 | return main.FALSE |
| 844 | main.log.info( "Mastership balanced between " \ |
| 845 | + str( len(masters) ) + " masters" ) |
| 846 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 847 | except ( TypeError, ValueError ): |
| 848 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 849 | return None |
| 850 | except pexpect.EOF: |
| 851 | main.log.error( self.name + ": EOF exception found" ) |
| 852 | main.log.error( self.name + ": " + self.handle.before ) |
| 853 | main.cleanup() |
| 854 | main.exit() |
| 855 | except Exception: |
| 856 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 857 | main.cleanup() |
| 858 | main.exit() |
| 859 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 860 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 861 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 862 | Lists all core links |
| 863 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 864 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 865 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 866 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 867 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 868 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 869 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 870 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 871 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 872 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 873 | except AssertionError: |
| 874 | main.log.exception( "" ) |
| 875 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 876 | except TypeError: |
| 877 | main.log.exception( self.name + ": Object not as expected" ) |
| 878 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 879 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 880 | main.log.error( self.name + ": EOF exception found" ) |
| 881 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 882 | main.cleanup() |
| 883 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 884 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 885 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 886 | main.cleanup() |
| 887 | main.exit() |
| 888 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 889 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 890 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 891 | Lists all ports |
| 892 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 893 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 894 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 895 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 896 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 897 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 898 | cmdStr += " -j" |
| 899 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 900 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 901 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 902 | except AssertionError: |
| 903 | main.log.exception( "" ) |
| 904 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 905 | except TypeError: |
| 906 | main.log.exception( self.name + ": Object not as expected" ) |
| 907 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 908 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 909 | main.log.error( self.name + ": EOF exception found" ) |
| 910 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 911 | main.cleanup() |
| 912 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 913 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 914 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 915 | main.cleanup() |
| 916 | main.exit() |
| 917 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 918 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 919 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 920 | Lists all devices and the controllers with roles assigned to them |
| 921 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 922 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 923 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 924 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 925 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 926 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 927 | cmdStr += " -j" |
| 928 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 929 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 930 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 931 | except AssertionError: |
| 932 | main.log.exception( "" ) |
| 933 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 934 | except TypeError: |
| 935 | main.log.exception( self.name + ": Object not as expected" ) |
| 936 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 937 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 938 | main.log.error( self.name + ": EOF exception found" ) |
| 939 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 940 | main.cleanup() |
| 941 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 942 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 943 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 944 | main.cleanup() |
| 945 | main.exit() |
| 946 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 947 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 948 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 949 | Given the a string containing the json representation of the "roles" |
| 950 | cli command and a partial or whole device id, returns a json object |
| 951 | containing the roles output for the first device whose id contains |
| 952 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 953 | |
| 954 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 955 | A dict of the role assignments for the given device or |
| 956 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 957 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 958 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 959 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 960 | return None |
| 961 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 962 | rawRoles = self.roles() |
| 963 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 964 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 965 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 966 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 967 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 968 | return device |
| 969 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 970 | except ( TypeError, ValueError ): |
| 971 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 972 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 973 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 974 | main.log.error( self.name + ": EOF exception found" ) |
| 975 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 976 | main.cleanup() |
| 977 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 978 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 979 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 980 | main.cleanup() |
| 981 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 982 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 983 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 984 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 985 | Iterates through each device and checks if there is a master assigned |
| 986 | Returns: main.TRUE if each device has a master |
| 987 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 988 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 989 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 990 | rawRoles = self.roles() |
| 991 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 992 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 993 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 994 | # print device |
| 995 | if device[ 'master' ] == "none": |
| 996 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 997 | return main.FALSE |
| 998 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 999 | except ( TypeError, ValueError ): |
| 1000 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1001 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1002 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1003 | main.log.error( self.name + ": EOF exception found" ) |
| 1004 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1005 | main.cleanup() |
| 1006 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1007 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1008 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1009 | main.cleanup() |
| 1010 | main.exit() |
| 1011 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1012 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1013 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1014 | Returns string of paths, and the cost. |
| 1015 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1016 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1017 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1018 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1019 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1020 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1021 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1022 | main.log.error( "Error in getting paths" ) |
| 1023 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1024 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1025 | path = handle.split( ";" )[ 0 ] |
| 1026 | cost = handle.split( ";" )[ 1 ] |
| 1027 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1028 | except AssertionError: |
| 1029 | main.log.exception( "" ) |
| 1030 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1031 | except TypeError: |
| 1032 | main.log.exception( self.name + ": Object not as expected" ) |
| 1033 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1034 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1035 | main.log.error( self.name + ": EOF exception found" ) |
| 1036 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1037 | main.cleanup() |
| 1038 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1039 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1040 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1041 | main.cleanup() |
| 1042 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1043 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1044 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1045 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1046 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1047 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1048 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1049 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1050 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1051 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1052 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1053 | cmdStr += " -j" |
| 1054 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1055 | if handle: |
| 1056 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1057 | # TODO: Maybe make this less hardcoded |
| 1058 | # ConsistentMap Exceptions |
| 1059 | assert "org.onosproject.store.service" not in handle |
| 1060 | # Node not leader |
| 1061 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1062 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1063 | except AssertionError: |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1064 | main.log.exception( "Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1065 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1066 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1067 | except TypeError: |
| 1068 | main.log.exception( self.name + ": Object not as expected" ) |
| 1069 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1070 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1071 | main.log.error( self.name + ": EOF exception found" ) |
| 1072 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1073 | main.cleanup() |
| 1074 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1075 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1076 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1077 | main.cleanup() |
| 1078 | main.exit() |
| 1079 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1080 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1081 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1082 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1083 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1084 | 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] | 1085 | partial mac address |
| 1086 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1087 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1088 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1089 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1090 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1091 | return None |
| 1092 | else: |
| 1093 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1094 | rawHosts = self.hosts() |
| 1095 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1096 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1097 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1098 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1099 | if not host: |
| 1100 | pass |
| 1101 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1102 | return host |
| 1103 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1104 | except ( TypeError, ValueError ): |
| 1105 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1106 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1107 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1108 | main.log.error( self.name + ": EOF exception found" ) |
| 1109 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1110 | main.cleanup() |
| 1111 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1112 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1113 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1114 | main.cleanup() |
| 1115 | main.exit() |
| 1116 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1117 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1118 | """ |
| 1119 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1120 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1121 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1122 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1123 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1124 | IMPORTANT: |
| 1125 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1126 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1127 | Furthermore, it assumes that value of VLAN is '-1' |
| 1128 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1129 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1130 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1131 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1132 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1133 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1134 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1135 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1136 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1137 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1138 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1139 | i = iter( str( hostHex ) ) |
| 1140 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1141 | hostHex = hostHex + "/-1" |
| 1142 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1143 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1144 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1145 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1146 | except TypeError: |
| 1147 | main.log.exception( self.name + ": Object not as expected" ) |
| 1148 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1149 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1150 | main.log.error( self.name + ": EOF exception found" ) |
| 1151 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1152 | main.cleanup() |
| 1153 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1154 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1155 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1156 | main.cleanup() |
| 1157 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1158 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1159 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1160 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1161 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1162 | * hostIdOne: ONOS host id for host1 |
| 1163 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1164 | Optional: |
| 1165 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1166 | * setVlan: specify a VLAN id treatment |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1167 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1168 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1169 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1170 | Returns: |
| 1171 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1172 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1173 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1174 | cmdStr = "add-host-intent " |
| 1175 | if vlanId: |
| 1176 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1177 | if setVlan: |
| 1178 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1179 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1180 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1181 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1182 | if re.search( "Error", handle ): |
| 1183 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1184 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1185 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1186 | else: |
| 1187 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1188 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
| 1189 | match = re.search('id=0x([\da-f]+),', handle) |
| 1190 | if match: |
| 1191 | return match.group()[3:-1] |
| 1192 | else: |
| 1193 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1194 | main.log.debug( "Response from ONOS was: " + |
| 1195 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1196 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1197 | except AssertionError: |
| 1198 | main.log.exception( "" ) |
| 1199 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1200 | except TypeError: |
| 1201 | main.log.exception( self.name + ": Object not as expected" ) |
| 1202 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1203 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1204 | main.log.error( self.name + ": EOF exception found" ) |
| 1205 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1206 | main.cleanup() |
| 1207 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1208 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1209 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1210 | main.cleanup() |
| 1211 | main.exit() |
| 1212 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1213 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1214 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1215 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1216 | * ingressDevice: device id of ingress device |
| 1217 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1218 | Optional: |
| 1219 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1220 | Description: |
| 1221 | Adds an optical intent by specifying an ingress and egress device |
| 1222 | Returns: |
| 1223 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1224 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1225 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1226 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1227 | " " + str( egressDevice ) |
| 1228 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1229 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1230 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1231 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1232 | main.log.error( "Error in adding Optical intent" ) |
| 1233 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1234 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1235 | main.log.info( "Optical intent installed between " + |
| 1236 | str( ingressDevice ) + " and " + |
| 1237 | str( egressDevice ) ) |
| 1238 | match = re.search('id=0x([\da-f]+),', handle) |
| 1239 | if match: |
| 1240 | return match.group()[3:-1] |
| 1241 | else: |
| 1242 | main.log.error( "Error, intent ID not found" ) |
| 1243 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1244 | except AssertionError: |
| 1245 | main.log.exception( "" ) |
| 1246 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1247 | except TypeError: |
| 1248 | main.log.exception( self.name + ": Object not as expected" ) |
| 1249 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1250 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1251 | main.log.error( self.name + ": EOF exception found" ) |
| 1252 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1253 | main.cleanup() |
| 1254 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1255 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1256 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1257 | main.cleanup() |
| 1258 | main.exit() |
| 1259 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1260 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1261 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1262 | ingressDevice, |
| 1263 | egressDevice, |
| 1264 | portIngress="", |
| 1265 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1266 | ethType="", |
| 1267 | ethSrc="", |
| 1268 | ethDst="", |
| 1269 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1270 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1271 | ipProto="", |
| 1272 | ipSrc="", |
| 1273 | ipDst="", |
| 1274 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1275 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1276 | vlanId="", |
| 1277 | setVlan="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1278 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1279 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1280 | * ingressDevice: device id of ingress device |
| 1281 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1282 | Optional: |
| 1283 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1284 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1285 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1286 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1287 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1288 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1289 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1290 | * ipSrc: specify ip source address |
| 1291 | * ipDst: specify ip destination address |
| 1292 | * tcpSrc: specify tcp source port |
| 1293 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1294 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1295 | * setVlan: specify a VLAN id treatment |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1296 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1297 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1298 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1299 | Returns: |
| 1300 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1301 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1302 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1303 | options developers provide for point-to-point |
| 1304 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1305 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1306 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1307 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1308 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1309 | if ethType: |
| 1310 | cmd += " --ethType " + str( ethType ) |
| 1311 | if ethSrc: |
| 1312 | cmd += " --ethSrc " + str( ethSrc ) |
| 1313 | if ethDst: |
| 1314 | cmd += " --ethDst " + str( ethDst ) |
| 1315 | if bandwidth: |
| 1316 | cmd += " --bandwidth " + str( bandwidth ) |
| 1317 | if lambdaAlloc: |
| 1318 | cmd += " --lambda " |
| 1319 | if ipProto: |
| 1320 | cmd += " --ipProto " + str( ipProto ) |
| 1321 | if ipSrc: |
| 1322 | cmd += " --ipSrc " + str( ipSrc ) |
| 1323 | if ipDst: |
| 1324 | cmd += " --ipDst " + str( ipDst ) |
| 1325 | if tcpSrc: |
| 1326 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1327 | if tcpDst: |
| 1328 | cmd += " --tcpDst " + str( tcpDst ) |
| 1329 | if vlanId: |
| 1330 | cmd += " -v " + str( vlanId ) |
| 1331 | if setVlan: |
| 1332 | cmd += " --setVlan " + str( setVlan ) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1333 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1334 | # Check whether the user appended the port |
| 1335 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1336 | if "/" in ingressDevice: |
| 1337 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1338 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1339 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1340 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1341 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1342 | # Would it make sense to throw an exception and exit |
| 1343 | # the test? |
| 1344 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1345 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1346 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1347 | str( ingressDevice ) + "/" +\ |
| 1348 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1349 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1350 | if "/" in egressDevice: |
| 1351 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1352 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1353 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1354 | main.log.error( "You must specify the egress port" ) |
| 1355 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1356 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1357 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1358 | str( egressDevice ) + "/" +\ |
| 1359 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1360 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1361 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1362 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1363 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1364 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1365 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1366 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1367 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1368 | # TODO: print out all the options in this message? |
| 1369 | main.log.info( "Point-to-point intent installed between " + |
| 1370 | str( ingressDevice ) + " and " + |
| 1371 | str( egressDevice ) ) |
| 1372 | match = re.search('id=0x([\da-f]+),', handle) |
| 1373 | if match: |
| 1374 | return match.group()[3:-1] |
| 1375 | else: |
| 1376 | main.log.error( "Error, intent ID not found" ) |
| 1377 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1378 | except AssertionError: |
| 1379 | main.log.exception( "" ) |
| 1380 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1381 | except TypeError: |
| 1382 | main.log.exception( self.name + ": Object not as expected" ) |
| 1383 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1384 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1385 | main.log.error( self.name + ": EOF exception found" ) |
| 1386 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1387 | main.cleanup() |
| 1388 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1389 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1390 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1391 | main.cleanup() |
| 1392 | main.exit() |
| 1393 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1394 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1395 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1396 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1397 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1398 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1399 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1400 | ethType="", |
| 1401 | ethSrc="", |
| 1402 | ethDst="", |
| 1403 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1404 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1405 | ipProto="", |
| 1406 | ipSrc="", |
| 1407 | ipDst="", |
| 1408 | tcpSrc="", |
| 1409 | tcpDst="", |
| 1410 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1411 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1412 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1413 | setVlan="", |
| 1414 | partial=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1415 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1416 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1417 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1418 | is same. That is, all ingress devices include port numbers |
| 1419 | with a "/" or all ingress devices could specify device |
| 1420 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1421 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1422 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1423 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1424 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1425 | Optional: |
| 1426 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1427 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1428 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1429 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1430 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1431 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1432 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1433 | * ipSrc: specify ip source address |
| 1434 | * ipDst: specify ip destination address |
| 1435 | * tcpSrc: specify tcp source port |
| 1436 | * tcpDst: specify tcp destination port |
| 1437 | * setEthSrc: action to Rewrite Source MAC Address |
| 1438 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1439 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1440 | * setVlan: specify VLAN Id treatment |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1441 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1442 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1443 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1444 | Returns: |
| 1445 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1446 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1447 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1448 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1449 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1450 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1451 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1452 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1453 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1454 | if ethType: |
| 1455 | cmd += " --ethType " + str( ethType ) |
| 1456 | if ethSrc: |
| 1457 | cmd += " --ethSrc " + str( ethSrc ) |
| 1458 | if ethDst: |
| 1459 | cmd += " --ethDst " + str( ethDst ) |
| 1460 | if bandwidth: |
| 1461 | cmd += " --bandwidth " + str( bandwidth ) |
| 1462 | if lambdaAlloc: |
| 1463 | cmd += " --lambda " |
| 1464 | if ipProto: |
| 1465 | cmd += " --ipProto " + str( ipProto ) |
| 1466 | if ipSrc: |
| 1467 | cmd += " --ipSrc " + str( ipSrc ) |
| 1468 | if ipDst: |
| 1469 | cmd += " --ipDst " + str( ipDst ) |
| 1470 | if tcpSrc: |
| 1471 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1472 | if tcpDst: |
| 1473 | cmd += " --tcpDst " + str( tcpDst ) |
| 1474 | if setEthSrc: |
| 1475 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1476 | if setEthDst: |
| 1477 | cmd += " --setEthDst " + str( setEthDst ) |
| 1478 | if vlanId: |
| 1479 | cmd += " -v " + str( vlanId ) |
| 1480 | if setVlan: |
| 1481 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1482 | if partial: |
| 1483 | cmd += " --partial" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1484 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1485 | # Check whether the user appended the port |
| 1486 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1487 | |
| 1488 | if portIngressList is None: |
| 1489 | for ingressDevice in ingressDeviceList: |
| 1490 | if "/" in ingressDevice: |
| 1491 | cmd += " " + str( ingressDevice ) |
| 1492 | else: |
| 1493 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1494 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1495 | # TODO: perhaps more meaningful return |
| 1496 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1497 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1498 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1499 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1500 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1501 | cmd += " " + \ |
| 1502 | str( ingressDevice ) + "/" +\ |
| 1503 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1504 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1505 | main.log.error( "Device list and port list does not " + |
| 1506 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1507 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1508 | if "/" in egressDevice: |
| 1509 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1510 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1511 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1512 | main.log.error( "You must specify " + |
| 1513 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1514 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1515 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1516 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1517 | str( egressDevice ) + "/" +\ |
| 1518 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1519 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1520 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1521 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1522 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1523 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1524 | "intent" ) |
| 1525 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1526 | else: |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1527 | match = re.search('id=0x([\da-f]+),', handle) |
| 1528 | if match: |
| 1529 | return match.group()[3:-1] |
| 1530 | else: |
| 1531 | main.log.error( "Error, intent ID not found" ) |
| 1532 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1533 | except AssertionError: |
| 1534 | main.log.exception( "" ) |
| 1535 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1536 | except TypeError: |
| 1537 | main.log.exception( self.name + ": Object not as expected" ) |
| 1538 | return None |
| 1539 | except pexpect.EOF: |
| 1540 | main.log.error( self.name + ": EOF exception found" ) |
| 1541 | main.log.error( self.name + ": " + self.handle.before ) |
| 1542 | main.cleanup() |
| 1543 | main.exit() |
| 1544 | except Exception: |
| 1545 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1546 | main.cleanup() |
| 1547 | main.exit() |
| 1548 | |
| 1549 | def addSinglepointToMultipointIntent( |
| 1550 | self, |
| 1551 | ingressDevice, |
| 1552 | egressDeviceList, |
| 1553 | portIngress="", |
| 1554 | portEgressList=None, |
| 1555 | ethType="", |
| 1556 | ethSrc="", |
| 1557 | ethDst="", |
| 1558 | bandwidth="", |
| 1559 | lambdaAlloc=False, |
| 1560 | ipProto="", |
| 1561 | ipSrc="", |
| 1562 | ipDst="", |
| 1563 | tcpSrc="", |
| 1564 | tcpDst="", |
| 1565 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1566 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1567 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1568 | setVlan="", |
| 1569 | partial=False ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1570 | """ |
| 1571 | Note: |
| 1572 | This function assumes the format of all egress devices |
| 1573 | is same. That is, all egress devices include port numbers |
| 1574 | with a "/" or all egress devices could specify device |
| 1575 | ids and port numbers seperately. |
| 1576 | Required: |
| 1577 | * EgressDeviceList: List of device ids of egress device |
| 1578 | ( Atleast 2 eress devices required in the list ) |
| 1579 | * ingressDevice: device id of ingress device |
| 1580 | Optional: |
| 1581 | * ethType: specify ethType |
| 1582 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1583 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1584 | * bandwidth: specify bandwidth capacity of link |
| 1585 | * lambdaAlloc: if True, intent will allocate lambda |
| 1586 | for the specified intent |
| 1587 | * ipProto: specify ip protocol |
| 1588 | * ipSrc: specify ip source address |
| 1589 | * ipDst: specify ip destination address |
| 1590 | * tcpSrc: specify tcp source port |
| 1591 | * tcpDst: specify tcp destination port |
| 1592 | * setEthSrc: action to Rewrite Source MAC Address |
| 1593 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1594 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1595 | * setVlan: specify VLAN ID treatment |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1596 | Description: |
| 1597 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1598 | specifying device id's and optional fields |
| 1599 | Returns: |
| 1600 | A string of the intent id or None on error |
| 1601 | |
| 1602 | NOTE: This function may change depending on the |
| 1603 | options developers provide for singlepoint-to-multipoint |
| 1604 | intent via cli |
| 1605 | """ |
| 1606 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1607 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1608 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1609 | if ethType: |
| 1610 | cmd += " --ethType " + str( ethType ) |
| 1611 | if ethSrc: |
| 1612 | cmd += " --ethSrc " + str( ethSrc ) |
| 1613 | if ethDst: |
| 1614 | cmd += " --ethDst " + str( ethDst ) |
| 1615 | if bandwidth: |
| 1616 | cmd += " --bandwidth " + str( bandwidth ) |
| 1617 | if lambdaAlloc: |
| 1618 | cmd += " --lambda " |
| 1619 | if ipProto: |
| 1620 | cmd += " --ipProto " + str( ipProto ) |
| 1621 | if ipSrc: |
| 1622 | cmd += " --ipSrc " + str( ipSrc ) |
| 1623 | if ipDst: |
| 1624 | cmd += " --ipDst " + str( ipDst ) |
| 1625 | if tcpSrc: |
| 1626 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1627 | if tcpDst: |
| 1628 | cmd += " --tcpDst " + str( tcpDst ) |
| 1629 | if setEthSrc: |
| 1630 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1631 | if setEthDst: |
| 1632 | cmd += " --setEthDst " + str( setEthDst ) |
| 1633 | if vlanId: |
| 1634 | cmd += " -v " + str( vlanId ) |
| 1635 | if setVlan: |
| 1636 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1637 | if partial: |
| 1638 | cmd += " --partial" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1639 | |
| 1640 | # Check whether the user appended the port |
| 1641 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1642 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1643 | if "/" in ingressDevice: |
| 1644 | cmd += " " + str( ingressDevice ) |
| 1645 | else: |
| 1646 | if not portIngress: |
| 1647 | main.log.error( "You must specify " + |
| 1648 | "the Ingress port" ) |
| 1649 | return main.FALSE |
| 1650 | |
| 1651 | cmd += " " +\ |
| 1652 | str( ingressDevice ) + "/" +\ |
| 1653 | str( portIngress ) |
| 1654 | |
| 1655 | if portEgressList is None: |
| 1656 | for egressDevice in egressDeviceList: |
| 1657 | if "/" in egressDevice: |
| 1658 | cmd += " " + str( egressDevice ) |
| 1659 | else: |
| 1660 | main.log.error( "You must specify " + |
| 1661 | "the egress port" ) |
| 1662 | # TODO: perhaps more meaningful return |
| 1663 | return main.FALSE |
| 1664 | else: |
| 1665 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1666 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1667 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1668 | cmd += " " + \ |
| 1669 | str( egressDevice ) + "/" +\ |
| 1670 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1671 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1672 | main.log.error( "Device list and port list does not " + |
| 1673 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1674 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1675 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1676 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1677 | # If error, return error message |
| 1678 | if re.search( "Error", handle ): |
| 1679 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1680 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1681 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1682 | else: |
| 1683 | match = re.search('id=0x([\da-f]+),', handle) |
| 1684 | if match: |
| 1685 | return match.group()[3:-1] |
| 1686 | else: |
| 1687 | main.log.error( "Error, intent ID not found" ) |
| 1688 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1689 | except AssertionError: |
| 1690 | main.log.exception( "" ) |
| 1691 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1692 | except TypeError: |
| 1693 | main.log.exception( self.name + ": Object not as expected" ) |
| 1694 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1695 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1696 | main.log.error( self.name + ": EOF exception found" ) |
| 1697 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1698 | main.cleanup() |
| 1699 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1700 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1701 | main.log.exception( self.name + ": Uncaught exception!" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1702 | main.cleanup() |
| 1703 | main.exit() |
| 1704 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1705 | def addMplsIntent( |
| 1706 | self, |
| 1707 | ingressDevice, |
| 1708 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1709 | ingressPort="", |
| 1710 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1711 | ethType="", |
| 1712 | ethSrc="", |
| 1713 | ethDst="", |
| 1714 | bandwidth="", |
| 1715 | lambdaAlloc=False, |
| 1716 | ipProto="", |
| 1717 | ipSrc="", |
| 1718 | ipDst="", |
| 1719 | tcpSrc="", |
| 1720 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1721 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1722 | egressLabel="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1723 | priority=""): |
| 1724 | """ |
| 1725 | Required: |
| 1726 | * ingressDevice: device id of ingress device |
| 1727 | * egressDevice: device id of egress device |
| 1728 | Optional: |
| 1729 | * ethType: specify ethType |
| 1730 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1731 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1732 | * bandwidth: specify bandwidth capacity of link |
| 1733 | * lambdaAlloc: if True, intent will allocate lambda |
| 1734 | for the specified intent |
| 1735 | * ipProto: specify ip protocol |
| 1736 | * ipSrc: specify ip source address |
| 1737 | * ipDst: specify ip destination address |
| 1738 | * tcpSrc: specify tcp source port |
| 1739 | * tcpDst: specify tcp destination port |
| 1740 | * ingressLabel: Ingress MPLS label |
| 1741 | * egressLabel: Egress MPLS label |
| 1742 | Description: |
| 1743 | Adds MPLS intent by |
| 1744 | specifying device id's and optional fields |
| 1745 | Returns: |
| 1746 | A string of the intent id or None on error |
| 1747 | |
| 1748 | NOTE: This function may change depending on the |
| 1749 | options developers provide for MPLS |
| 1750 | intent via cli |
| 1751 | """ |
| 1752 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1753 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1754 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1755 | if ethType: |
| 1756 | cmd += " --ethType " + str( ethType ) |
| 1757 | if ethSrc: |
| 1758 | cmd += " --ethSrc " + str( ethSrc ) |
| 1759 | if ethDst: |
| 1760 | cmd += " --ethDst " + str( ethDst ) |
| 1761 | if bandwidth: |
| 1762 | cmd += " --bandwidth " + str( bandwidth ) |
| 1763 | if lambdaAlloc: |
| 1764 | cmd += " --lambda " |
| 1765 | if ipProto: |
| 1766 | cmd += " --ipProto " + str( ipProto ) |
| 1767 | if ipSrc: |
| 1768 | cmd += " --ipSrc " + str( ipSrc ) |
| 1769 | if ipDst: |
| 1770 | cmd += " --ipDst " + str( ipDst ) |
| 1771 | if tcpSrc: |
| 1772 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1773 | if tcpDst: |
| 1774 | cmd += " --tcpDst " + str( tcpDst ) |
| 1775 | if ingressLabel: |
| 1776 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1777 | if egressLabel: |
| 1778 | cmd += " --egressLabel " + str( egressLabel ) |
| 1779 | if priority: |
| 1780 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1781 | |
| 1782 | # Check whether the user appended the port |
| 1783 | # or provided it as an input |
| 1784 | if "/" in ingressDevice: |
| 1785 | cmd += " " + str( ingressDevice ) |
| 1786 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1787 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1788 | main.log.error( "You must specify the ingress port" ) |
| 1789 | return None |
| 1790 | |
| 1791 | cmd += " " + \ |
| 1792 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1793 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1794 | |
| 1795 | if "/" in egressDevice: |
| 1796 | cmd += " " + str( egressDevice ) |
| 1797 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1798 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1799 | main.log.error( "You must specify the egress port" ) |
| 1800 | return None |
| 1801 | |
| 1802 | cmd += " " +\ |
| 1803 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1804 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1805 | |
| 1806 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1807 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1808 | # If error, return error message |
| 1809 | if re.search( "Error", handle ): |
| 1810 | main.log.error( "Error in adding mpls intent" ) |
| 1811 | return None |
| 1812 | else: |
| 1813 | # TODO: print out all the options in this message? |
| 1814 | main.log.info( "MPLS intent installed between " + |
| 1815 | str( ingressDevice ) + " and " + |
| 1816 | str( egressDevice ) ) |
| 1817 | match = re.search('id=0x([\da-f]+),', handle) |
| 1818 | if match: |
| 1819 | return match.group()[3:-1] |
| 1820 | else: |
| 1821 | main.log.error( "Error, intent ID not found" ) |
| 1822 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1823 | except AssertionError: |
| 1824 | main.log.exception( "" ) |
| 1825 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1826 | except TypeError: |
| 1827 | main.log.exception( self.name + ": Object not as expected" ) |
| 1828 | return None |
| 1829 | except pexpect.EOF: |
| 1830 | main.log.error( self.name + ": EOF exception found" ) |
| 1831 | main.log.error( self.name + ": " + self.handle.before ) |
| 1832 | main.cleanup() |
| 1833 | main.exit() |
| 1834 | except Exception: |
| 1835 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1836 | main.cleanup() |
| 1837 | main.exit() |
| 1838 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1839 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1840 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1841 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1842 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1843 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1844 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1845 | -p or --purge: Purge the intent from the store after removal |
| 1846 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1847 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 1848 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1849 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1850 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1851 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1852 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1853 | if purge: |
| 1854 | cmdStr += " -p" |
| 1855 | if sync: |
| 1856 | cmdStr += " -s" |
| 1857 | |
| 1858 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1859 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1860 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1861 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1862 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1863 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1864 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1865 | # TODO: Should this be main.TRUE |
| 1866 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1867 | except AssertionError: |
| 1868 | main.log.exception( "" ) |
| 1869 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1870 | except TypeError: |
| 1871 | main.log.exception( self.name + ": Object not as expected" ) |
| 1872 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1873 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1874 | main.log.error( self.name + ": EOF exception found" ) |
| 1875 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1876 | main.cleanup() |
| 1877 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1878 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1879 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1880 | main.cleanup() |
| 1881 | main.exit() |
| 1882 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1883 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1884 | """ |
| 1885 | Description: |
| 1886 | Remove all the intents |
| 1887 | Optional args:- |
| 1888 | -s or --sync: Waits for the removal before returning |
| 1889 | -p or --purge: Purge the intent from the store after removal |
| 1890 | Returns: |
| 1891 | Returns main.TRUE if all intents are removed, otherwise returns |
| 1892 | main.FALSE; Returns None for exception |
| 1893 | """ |
| 1894 | try: |
| 1895 | cmdStr = "remove-intent" |
| 1896 | if purge: |
| 1897 | cmdStr += " -p" |
| 1898 | if sync: |
| 1899 | cmdStr += " -s" |
| 1900 | |
| 1901 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1902 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1903 | assert "Command not found:" not in handle, handle |
| 1904 | if re.search( "Error", handle ): |
| 1905 | main.log.error( "Error in removing intent" ) |
| 1906 | return main.FALSE |
| 1907 | else: |
| 1908 | return main.TRUE |
| 1909 | except AssertionError: |
| 1910 | main.log.exception( "" ) |
| 1911 | return None |
| 1912 | except TypeError: |
| 1913 | main.log.exception( self.name + ": Object not as expected" ) |
| 1914 | return None |
| 1915 | except pexpect.EOF: |
| 1916 | main.log.error( self.name + ": EOF exception found" ) |
| 1917 | main.log.error( self.name + ": " + self.handle.before ) |
| 1918 | main.cleanup() |
| 1919 | main.exit() |
| 1920 | except Exception: |
| 1921 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1922 | main.cleanup() |
| 1923 | main.exit() |
| 1924 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 1925 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1926 | """ |
| 1927 | Purges all WITHDRAWN Intents |
| 1928 | """ |
| 1929 | try: |
| 1930 | cmdStr = "purge-intents" |
| 1931 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1932 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1933 | if re.search( "Error", handle ): |
| 1934 | main.log.error( "Error in purging intents" ) |
| 1935 | return main.FALSE |
| 1936 | else: |
| 1937 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1938 | except AssertionError: |
| 1939 | main.log.exception( "" ) |
| 1940 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1941 | except TypeError: |
| 1942 | main.log.exception( self.name + ": Object not as expected" ) |
| 1943 | return None |
| 1944 | except pexpect.EOF: |
| 1945 | main.log.error( self.name + ": EOF exception found" ) |
| 1946 | main.log.error( self.name + ": " + self.handle.before ) |
| 1947 | main.cleanup() |
| 1948 | main.exit() |
| 1949 | except Exception: |
| 1950 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1951 | main.cleanup() |
| 1952 | main.exit() |
| 1953 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1954 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1955 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1956 | NOTE: This method should be used after installing application: |
| 1957 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1958 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1959 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1960 | Description: |
| 1961 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1962 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1963 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1964 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1965 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1966 | cmdStr += " -j" |
| 1967 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1968 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1969 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1970 | except AssertionError: |
| 1971 | main.log.exception( "" ) |
| 1972 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1973 | except TypeError: |
| 1974 | main.log.exception( self.name + ": Object not as expected" ) |
| 1975 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1976 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1977 | main.log.error( self.name + ": EOF exception found" ) |
| 1978 | main.log.error( self.name + ": " + self.handle.before ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1979 | main.cleanup() |
| 1980 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1981 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1982 | main.log.exception( self.name + ": Uncaught exception!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1983 | main.cleanup() |
| 1984 | main.exit() |
| 1985 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 1986 | def ipv4RouteNumber( self ): |
| 1987 | """ |
| 1988 | NOTE: This method should be used after installing application: |
| 1989 | onos-app-sdnip |
| 1990 | Description: |
| 1991 | Obtain the total IPv4 routes number in the system |
| 1992 | """ |
| 1993 | try: |
| 1994 | cmdStr = "routes -s -j" |
| 1995 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1996 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 1997 | jsonResult = json.loads( handle ) |
| 1998 | return jsonResult['totalRoutes4'] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1999 | except AssertionError: |
| 2000 | main.log.exception( "" ) |
| 2001 | return None |
| 2002 | except ( TypeError, ValueError ): |
| 2003 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2004 | return None |
| 2005 | except pexpect.EOF: |
| 2006 | main.log.error( self.name + ": EOF exception found" ) |
| 2007 | main.log.error( self.name + ": " + self.handle.before ) |
| 2008 | main.cleanup() |
| 2009 | main.exit() |
| 2010 | except Exception: |
| 2011 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2012 | main.cleanup() |
| 2013 | main.exit() |
| 2014 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2015 | def intents( self, jsonFormat = True, summary = False, **intentargs): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2016 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2017 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2018 | Obtain intents from the ONOS cli. |
| 2019 | Optional: |
| 2020 | * jsonFormat: Enable output formatting in json, default to True |
| 2021 | * summary: Whether only output the intent summary, defaults to False |
| 2022 | * type: Only output a certain type of intent. This options is valid |
| 2023 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2024 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2025 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2026 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2027 | if summary: |
| 2028 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2029 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2030 | cmdStr += " -j" |
| 2031 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2032 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2033 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2034 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2035 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2036 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2037 | intentType = "" |
| 2038 | # IF we want the summary of a specific intent type |
| 2039 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2040 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2041 | if intentType in jsonResult.keys(): |
| 2042 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2043 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2044 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2045 | return handle |
| 2046 | else: |
| 2047 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2048 | except AssertionError: |
| 2049 | main.log.exception( "" ) |
| 2050 | return None |
| 2051 | except ( TypeError, ValueError ): |
| 2052 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2053 | return None |
| 2054 | except pexpect.EOF: |
| 2055 | main.log.error( self.name + ": EOF exception found" ) |
| 2056 | main.log.error( self.name + ": " + self.handle.before ) |
| 2057 | main.cleanup() |
| 2058 | main.exit() |
| 2059 | except Exception: |
| 2060 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2061 | main.cleanup() |
| 2062 | main.exit() |
| 2063 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2064 | def getIntentState(self, intentsId, intentsJson=None): |
| 2065 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2066 | Description: |
| 2067 | Gets intent state. Accepts a single intent ID (string type) or a |
| 2068 | list of intent IDs. |
| 2069 | Parameters: |
| 2070 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2071 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2072 | Returns: |
| 2073 | Returns the state (string type) of the ID if a single intent ID is |
| 2074 | accepted. |
| 2075 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2076 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2077 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2078 | """ |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2079 | try: |
| 2080 | state = "State is Undefined" |
| 2081 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2082 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2083 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2084 | rawJson = intentsJson |
| 2085 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2086 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2087 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2088 | if intentsId == intent[ 'id' ]: |
| 2089 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2090 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2091 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
| 2092 | " on the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2093 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2094 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2095 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2096 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2097 | stateDict = {} |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2098 | for intents in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2099 | if intentsId[ i ] == intents[ 'id' ]: |
| 2100 | stateDict[ 'state' ] = intents[ 'state' ] |
| 2101 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2102 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2103 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2104 | if len( intentsId ) != len( dictList ): |
| 2105 | main.log.info( "Cannot find some of the intent ID state" ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2106 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2107 | else: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2108 | main.log.info( "Invalid intents ID entry" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2109 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2110 | except ( TypeError, ValueError ): |
| 2111 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2112 | return None |
| 2113 | except pexpect.EOF: |
| 2114 | main.log.error( self.name + ": EOF exception found" ) |
| 2115 | main.log.error( self.name + ": " + self.handle.before ) |
| 2116 | main.cleanup() |
| 2117 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2118 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2119 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2120 | main.cleanup() |
| 2121 | main.exit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2122 | |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2123 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2124 | """ |
| 2125 | Description: |
| 2126 | Check intents state |
| 2127 | Required: |
| 2128 | intentsId - List of intents ID to be checked |
| 2129 | Optional: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2130 | expectedState - Check the expected state(s) of each intents |
| 2131 | state in the list. |
| 2132 | *NOTE: You can pass in a list of expected state, |
| 2133 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2134 | Return: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2135 | Returns main.TRUE only if all intent are the same as expected states |
| 2136 | , otherwise, returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2137 | """ |
| 2138 | try: |
| 2139 | # Generating a dictionary: intent id as a key and state as value |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2140 | returnValue = main.TRUE |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2141 | intentsDict = self.getIntentState( intentsId ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2142 | if len( intentsId ) != len( intentsDict ): |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2143 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2144 | "getting intents state" ) |
| 2145 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2146 | |
| 2147 | if isinstance( expectedState, types.StringType ): |
| 2148 | for intents in intentsDict: |
| 2149 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2150 | main.log.debug( self.name + " : Intent ID - " + |
| 2151 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2152 | " actual state = " + |
| 2153 | intents.get( 'state' ) |
| 2154 | + " does not equal expected state = " |
| 2155 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2156 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2157 | |
| 2158 | elif isinstance( expectedState, types.ListType ): |
| 2159 | for intents in intentsDict: |
| 2160 | if not any( state == intents.get( 'state' ) for state in |
| 2161 | expectedState ): |
| 2162 | main.log.debug( self.name + " : Intent ID - " + |
| 2163 | intents.get( 'id' ) + |
| 2164 | " actual state = " + |
| 2165 | intents.get( 'state' ) + |
| 2166 | " does not equal expected states = " |
| 2167 | + str( expectedState ) ) |
| 2168 | returnValue = main.FALSE |
| 2169 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2170 | if returnValue == main.TRUE: |
| 2171 | main.log.info( self.name + ": All " + |
| 2172 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2173 | " intents are in " + str( expectedState ) + |
| 2174 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2175 | return returnValue |
| 2176 | except TypeError: |
| 2177 | main.log.exception( self.name + ": Object not as expected" ) |
| 2178 | return None |
| 2179 | except pexpect.EOF: |
| 2180 | main.log.error( self.name + ": EOF exception found" ) |
| 2181 | main.log.error( self.name + ": " + self.handle.before ) |
| 2182 | main.cleanup() |
| 2183 | main.exit() |
| 2184 | except Exception: |
| 2185 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2186 | main.cleanup() |
| 2187 | main.exit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2188 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2189 | def compareIntent( self, intentDict ): |
| 2190 | """ |
| 2191 | Description: |
| 2192 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2193 | Return: |
| 2194 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2195 | Arguments: |
| 2196 | intentDict: a dictionary which maps intent ids to intent states |
| 2197 | """ |
| 2198 | try: |
| 2199 | intentsRaw = self.intents() |
| 2200 | intentsJson = json.loads( intentsRaw ) |
| 2201 | intentDictONOS = {} |
| 2202 | for intent in intentsJson: |
| 2203 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
| 2204 | if len( intentDict ) != len( intentDictONOS ): |
| 2205 | main.log.info( self.name + ": expected intent count does not match that in ONOS, " + |
| 2206 | str( len( intentDict ) ) + " expected and " + |
| 2207 | str( len( intentDictONOS ) ) + " actual" ) |
| 2208 | return main.FALSE |
| 2209 | returnValue = main.TRUE |
| 2210 | for intentID in intentDict.keys(): |
| 2211 | if not intentID in intentDictONOS.keys(): |
| 2212 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2213 | returnValue = main.FALSE |
| 2214 | elif intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2215 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2216 | " expected state is " + intentDict[ intentID ] + |
| 2217 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2218 | returnValue = main.FALSE |
| 2219 | if returnValue == main.TRUE: |
| 2220 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2221 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2222 | except KeyError: |
| 2223 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2224 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2225 | except ( TypeError, ValueError ): |
| 2226 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2227 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2228 | except pexpect.EOF: |
| 2229 | main.log.error( self.name + ": EOF exception found" ) |
| 2230 | main.log.error( self.name + ": " + self.handle.before ) |
| 2231 | main.cleanup() |
| 2232 | main.exit() |
| 2233 | except Exception: |
| 2234 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2235 | main.cleanup() |
| 2236 | main.exit() |
| 2237 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2238 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2239 | """ |
| 2240 | Description: |
| 2241 | Check the number of installed intents. |
| 2242 | Optional: |
| 2243 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2244 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2245 | Return: |
| 2246 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2247 | , otherwise, returns main.FALSE. |
| 2248 | """ |
| 2249 | |
| 2250 | try: |
| 2251 | cmd = "intents -s -j" |
| 2252 | |
| 2253 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2254 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2255 | if response == None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2256 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2257 | response = json.loads( response ) |
| 2258 | |
| 2259 | # get total and installed number, see if they are match |
| 2260 | allState = response.get( 'all' ) |
| 2261 | if allState.get('total') == allState.get('installed'): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2262 | 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] | 2263 | return main.TRUE |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2264 | 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] | 2265 | return main.FALSE |
| 2266 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2267 | except ( TypeError, ValueError ): |
| 2268 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2269 | return None |
| 2270 | except pexpect.EOF: |
| 2271 | main.log.error( self.name + ": EOF exception found" ) |
| 2272 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2273 | if noExit: |
| 2274 | return main.FALSE |
| 2275 | else: |
| 2276 | main.cleanup() |
| 2277 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2278 | except Exception: |
| 2279 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2280 | if noExit: |
| 2281 | return main.FALSE |
| 2282 | else: |
| 2283 | main.cleanup() |
| 2284 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2285 | except pexpect.TIMEOUT: |
| 2286 | main.log.error( self.name + ": ONOS timeout" ) |
| 2287 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2288 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2289 | def flows( self, state="", jsonFormat=True, timeout=60, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2290 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2291 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2292 | * jsonFormat: enable output formatting in json |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2293 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2294 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2295 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2296 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2297 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2298 | if jsonFormat: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2299 | cmdStr += " -j " |
| 2300 | cmdStr += state |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2301 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2302 | assert "Command not found:" not in handle, handle |
| 2303 | if re.search( "Error:", handle ): |
| 2304 | main.log.error( self.name + ": flows() response: " + |
| 2305 | str( handle ) ) |
| 2306 | return handle |
| 2307 | except AssertionError: |
| 2308 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2309 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2310 | except TypeError: |
| 2311 | main.log.exception( self.name + ": Object not as expected" ) |
| 2312 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2313 | except pexpect.TIMEOUT: |
| 2314 | main.log.error( self.name + ": ONOS timeout" ) |
| 2315 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2316 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2317 | main.log.error( self.name + ": EOF exception found" ) |
| 2318 | main.log.error( self.name + ": " + self.handle.before ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2319 | main.cleanup() |
| 2320 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2321 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2322 | main.log.exception( self.name + ": Uncaught exception!" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2323 | main.cleanup() |
| 2324 | main.exit() |
| 2325 | |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2326 | def checkFlowCount(self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2327 | count = self.getTotalFlowsNum( timeout=timeout ) |
| 2328 | count = int (count) if count else 0 |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2329 | return count if (count > min) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2330 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2331 | def checkFlowsState( self, isPENDING=True, timeout=60,noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2332 | """ |
| 2333 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2334 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2335 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2336 | if the count of those states is 0, which means all current flows |
| 2337 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2338 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2339 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2340 | Return: |
| 2341 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2342 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2343 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2344 | """ |
| 2345 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2346 | states = ["PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED"] |
| 2347 | checkedStates = [] |
| 2348 | statesCount = [0, 0, 0, 0] |
| 2349 | for s in states: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2350 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2351 | if rawFlows: |
| 2352 | # if we didn't get flows or flows function return None, we should return |
| 2353 | # main.Flase |
| 2354 | checkedStates.append( json.loads( rawFlows ) ) |
| 2355 | else: |
| 2356 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2357 | for i in range( len( states ) ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2358 | for c in checkedStates[i]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2359 | try: |
| 2360 | statesCount[i] += int( c.get( "flowCount" ) ) |
| 2361 | except TypeError: |
| 2362 | main.log.exception( "Json object not as expected" ) |
| 2363 | main.log.info( states[i] + " flows: " + str( statesCount[i] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2364 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2365 | # We want to count PENDING_ADD if isPENDING is true |
| 2366 | if isPENDING: |
| 2367 | if statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2368 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2369 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2370 | if statesCount[0] + statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2371 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2372 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2373 | except ( TypeError, ValueError ): |
| 2374 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2375 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2376 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2377 | except AssertionError: |
| 2378 | main.log.exception( "" ) |
| 2379 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2380 | except pexpect.EOF: |
| 2381 | main.log.error( self.name + ": EOF exception found" ) |
| 2382 | main.log.error( self.name + ": " + self.handle.before ) |
| 2383 | main.cleanup() |
| 2384 | main.exit() |
| 2385 | except Exception: |
| 2386 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2387 | main.cleanup() |
| 2388 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2389 | except pexpect.TIMEOUT: |
| 2390 | main.log.error( self.name + ": ONOS timeout" ) |
| 2391 | return None |
| 2392 | |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2393 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2394 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2395 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2396 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2397 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2398 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2399 | a specific point-to-point intent definition |
| 2400 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2401 | * ingress: specify source dpid |
| 2402 | * egress: specify destination dpid |
| 2403 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2404 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2405 | * offset: the keyOffset is where the next batch of intents |
| 2406 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2407 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2408 | * getResponse: If set to True, function will return ONOS response. |
| 2409 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2410 | Returns: If failed to push test intents, it will returen None, |
| 2411 | if successful, return true. |
| 2412 | Timeout expection will return None, |
| 2413 | TypeError will return false |
| 2414 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2415 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2416 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2417 | if background: |
| 2418 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2419 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2420 | back = "" |
| 2421 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2422 | ingress, |
| 2423 | egress, |
| 2424 | batchSize, |
| 2425 | offset, |
| 2426 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2427 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2428 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2429 | main.log.info( response ) |
| 2430 | if response == None: |
| 2431 | return None |
| 2432 | |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2433 | if getResponse: |
| 2434 | return response |
| 2435 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2436 | # TODO: We should handle if there is failure in installation |
| 2437 | return main.TRUE |
| 2438 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2439 | except AssertionError: |
| 2440 | main.log.exception( "" ) |
| 2441 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2442 | except pexpect.TIMEOUT: |
| 2443 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2444 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2445 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2446 | main.log.error( self.name + ": EOF exception found" ) |
| 2447 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2448 | main.cleanup() |
| 2449 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2450 | except TypeError: |
| 2451 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2452 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2453 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2454 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2455 | main.cleanup() |
| 2456 | main.exit() |
| 2457 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2458 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2459 | """ |
| 2460 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2461 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2462 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2463 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2464 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2465 | """ |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2466 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2467 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2468 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2469 | cmd = "flows -c added" |
| 2470 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2471 | if rawFlows: |
| 2472 | rawFlows = rawFlows.split("\n") |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2473 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2474 | for l in rawFlows: |
| 2475 | totalFlows += int(l.split("Count=")[1]) |
| 2476 | else: |
| 2477 | main.log.error("Response not as expected!") |
| 2478 | return None |
| 2479 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2480 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2481 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2482 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2483 | return None |
| 2484 | except pexpect.EOF: |
| 2485 | main.log.error( self.name + ": EOF exception found" ) |
| 2486 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2487 | if not noExit: |
| 2488 | main.cleanup() |
| 2489 | main.exit() |
| 2490 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2491 | except Exception: |
| 2492 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2493 | if not noExit: |
| 2494 | main.cleanup() |
| 2495 | main.exit() |
| 2496 | return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2497 | except pexpect.TIMEOUT: |
| 2498 | main.log.error( self.name + ": ONOS timeout" ) |
| 2499 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2500 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2501 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2502 | """ |
| 2503 | Description: |
| 2504 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2505 | Optional: |
| 2506 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2507 | Return: |
| 2508 | The number of intents |
| 2509 | """ |
| 2510 | try: |
| 2511 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2512 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2513 | if response == None: |
| 2514 | return -1 |
| 2515 | response = json.loads( response ) |
| 2516 | return int( response.get("intents") ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2517 | except ( TypeError, ValueError ): |
| 2518 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2519 | return None |
| 2520 | except pexpect.EOF: |
| 2521 | main.log.error( self.name + ": EOF exception found" ) |
| 2522 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2523 | if noExit: |
| 2524 | return -1 |
| 2525 | else: |
| 2526 | main.cleanup() |
| 2527 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2528 | except Exception: |
| 2529 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2530 | if noExit: |
| 2531 | return -1 |
| 2532 | else: |
| 2533 | main.cleanup() |
| 2534 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2535 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2536 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2537 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2538 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2539 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2540 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2541 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2542 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2543 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2544 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2545 | cmdStr += " -j" |
| 2546 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2547 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2548 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2549 | except AssertionError: |
| 2550 | main.log.exception( "" ) |
| 2551 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2552 | except TypeError: |
| 2553 | main.log.exception( self.name + ": Object not as expected" ) |
| 2554 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2555 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2556 | main.log.error( self.name + ": EOF exception found" ) |
| 2557 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2558 | main.cleanup() |
| 2559 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2560 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2561 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2562 | main.cleanup() |
| 2563 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2564 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2565 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2566 | """ |
| 2567 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2568 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2569 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2570 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2571 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2572 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2573 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2574 | cmdStr += " -j" |
| 2575 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2576 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2577 | if handle: |
| 2578 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2579 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2580 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2581 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2582 | else: |
| 2583 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2584 | except AssertionError: |
| 2585 | main.log.exception( "" ) |
| 2586 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2587 | except TypeError: |
| 2588 | main.log.exception( self.name + ": Object not as expected" ) |
| 2589 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2590 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2591 | main.log.error( self.name + ": EOF exception found" ) |
| 2592 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2593 | main.cleanup() |
| 2594 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2595 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2596 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2597 | main.cleanup() |
| 2598 | main.exit() |
| 2599 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2600 | # Wrapper functions **************** |
| 2601 | # Wrapper functions use existing driver |
| 2602 | # functions and extends their use case. |
| 2603 | # For example, we may use the output of |
| 2604 | # a normal driver function, and parse it |
| 2605 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2606 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2607 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2608 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2609 | Description: |
| 2610 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2611 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2612 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2613 | # Obtain output of intents function |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2614 | intentsStr = self.intents(jsonFormat=False) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2615 | intentIdList = [] |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2616 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2617 | # Parse the intents output for ID's |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2618 | intentsList = [ s.strip() for s in intentsStr.splitlines() ] |
| 2619 | for intents in intentsList: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2620 | match = re.search('id=0x([\da-f]+),', intents) |
| 2621 | if match: |
| 2622 | tmpId = match.group()[3:-1] |
| 2623 | intentIdList.append( tmpId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2624 | return intentIdList |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2625 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2626 | except TypeError: |
| 2627 | main.log.exception( self.name + ": Object not as expected" ) |
| 2628 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2629 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2630 | main.log.error( self.name + ": EOF exception found" ) |
| 2631 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2632 | main.cleanup() |
| 2633 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2634 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2635 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2636 | main.cleanup() |
| 2637 | main.exit() |
| 2638 | |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2639 | def FlowAddedCount( self, deviceId ): |
| 2640 | """ |
| 2641 | Determine the number of flow rules for the given device id that are |
| 2642 | in the added state |
| 2643 | """ |
| 2644 | try: |
| 2645 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2646 | "grep 'state=ADDED' | wc -l" |
| 2647 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2648 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2649 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2650 | except AssertionError: |
| 2651 | main.log.exception( "" ) |
| 2652 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2653 | except pexpect.EOF: |
| 2654 | main.log.error( self.name + ": EOF exception found" ) |
| 2655 | main.log.error( self.name + ": " + self.handle.before ) |
| 2656 | main.cleanup() |
| 2657 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2658 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2659 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2660 | main.cleanup() |
| 2661 | main.exit() |
| 2662 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2663 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2664 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2665 | Use 'devices' function to obtain list of all devices |
| 2666 | and parse the result to obtain a list of all device |
| 2667 | id's. Returns this list. Returns empty list if no |
| 2668 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2669 | List is ordered sequentially |
| 2670 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2671 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2672 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2673 | the ids. By obtaining the list of device ids on the fly, |
| 2674 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2675 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2676 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2677 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2678 | devicesStr = self.devices( jsonFormat=False ) |
| 2679 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2680 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2681 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2682 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2683 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2684 | |
| 2685 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2686 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2687 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2688 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2689 | # Split list further into arguments before and after string |
| 2690 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2691 | # append to idList |
| 2692 | for arg in tempList: |
| 2693 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 2694 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2695 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2696 | except TypeError: |
| 2697 | main.log.exception( self.name + ": Object not as expected" ) |
| 2698 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2699 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2700 | main.log.error( self.name + ": EOF exception found" ) |
| 2701 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2702 | main.cleanup() |
| 2703 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2704 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2705 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2706 | main.cleanup() |
| 2707 | main.exit() |
| 2708 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2709 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2710 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2711 | Uses 'nodes' function to obtain list of all nodes |
| 2712 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2713 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2714 | Returns: |
| 2715 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2716 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2717 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2718 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2719 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2720 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 2721 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2722 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2723 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2724 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2725 | nodesJson = json.loads( nodesStr ) |
| 2726 | idList = [ node.get('id') for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2727 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2728 | except ( TypeError, ValueError ): |
| 2729 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2730 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2731 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2732 | main.log.error( self.name + ": EOF exception found" ) |
| 2733 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2734 | main.cleanup() |
| 2735 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2736 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2737 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2738 | main.cleanup() |
| 2739 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2740 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2741 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2742 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2743 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 2744 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2745 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2746 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2747 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2748 | return None |
| 2749 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2750 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2751 | rawDevices = self.devices() |
| 2752 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2753 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2754 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2755 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 2756 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2757 | return device |
| 2758 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2759 | except ( TypeError, ValueError ): |
| 2760 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2761 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2762 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2763 | main.log.error( self.name + ": EOF exception found" ) |
| 2764 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2765 | main.cleanup() |
| 2766 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2767 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2768 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2769 | main.cleanup() |
| 2770 | main.exit() |
| 2771 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2772 | def getTopology( self, topologyOutput ): |
| 2773 | """ |
| 2774 | Definition: |
| 2775 | Loads a json topology output |
| 2776 | Return: |
| 2777 | topology = current ONOS topology |
| 2778 | """ |
| 2779 | import json |
| 2780 | try: |
| 2781 | # either onos:topology or 'topology' will work in CLI |
| 2782 | topology = json.loads(topologyOutput) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2783 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2784 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2785 | except ( TypeError, ValueError ): |
| 2786 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 2787 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2788 | except pexpect.EOF: |
| 2789 | main.log.error( self.name + ": EOF exception found" ) |
| 2790 | main.log.error( self.name + ": " + self.handle.before ) |
| 2791 | main.cleanup() |
| 2792 | main.exit() |
| 2793 | except Exception: |
| 2794 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2795 | main.cleanup() |
| 2796 | main.exit() |
| 2797 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2798 | def checkStatus(self, numoswitch, numolink, numoctrl = -1, logLevel="info"): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2799 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2800 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2801 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2802 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2803 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2804 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2805 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2806 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2807 | logLevel = level to log to. |
| 2808 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2809 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2810 | Returns: main.TRUE if the number of switches and links are correct, |
| 2811 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2812 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2813 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2814 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2815 | try: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2816 | summary = json.loads( self.summary() ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 2817 | except ( TypeError, ValueError ): |
| 2818 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 2819 | return main.ERROR |
| 2820 | try: |
| 2821 | topology = self.getTopology( self.topology() ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2822 | if topology == {} or topology == None or summary == {} or summary == None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2823 | return main.ERROR |
| 2824 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2825 | # Is the number of switches is what we expected |
| 2826 | devices = topology.get( 'devices', False ) |
| 2827 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2828 | nodes = summary.get( 'nodes', False ) |
| 2829 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2830 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2831 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2832 | # Is the number of links is what we expected |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2833 | linkCheck = ( int( links ) == int( numolink ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2834 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 2835 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2836 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2837 | output = output + "The number of links and switches match "\ |
| 2838 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2839 | result = main.TRUE |
| 2840 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2841 | output = output + \ |
| 2842 | "The number of links and switches does not match " + \ |
| 2843 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2844 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2845 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 2846 | output = output + " (%i expected) " % int( numoswitch ) |
| 2847 | output = output + "and %i links " % int( links ) |
| 2848 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 2849 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2850 | output = output + "and %i controllers " % int( nodes ) |
| 2851 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2852 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2853 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2854 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2855 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2856 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2857 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2858 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2859 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2860 | main.log.error( self.name + ": EOF exception found" ) |
| 2861 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2862 | main.cleanup() |
| 2863 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2864 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2865 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2866 | main.cleanup() |
| 2867 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2868 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2869 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2870 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2871 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2872 | deviceId must be the id of a device as seen in the onos devices command |
| 2873 | 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] | 2874 | role must be either master, standby, or none |
| 2875 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2876 | Returns: |
| 2877 | main.TRUE or main.FALSE based on argument verification and |
| 2878 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2879 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2880 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2881 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2882 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2883 | cmdStr = "device-role " +\ |
| 2884 | str( deviceId ) + " " +\ |
| 2885 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2886 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2887 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2888 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2889 | if re.search( "Error", handle ): |
| 2890 | # end color output to escape any colours |
| 2891 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2892 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2893 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2894 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2895 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2896 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2897 | main.log.error( "Invalid 'role' given to device_role(). " + |
| 2898 | "Value was '" + str(role) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2899 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2900 | except AssertionError: |
| 2901 | main.log.exception( "" ) |
| 2902 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2903 | except TypeError: |
| 2904 | main.log.exception( self.name + ": Object not as expected" ) |
| 2905 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2906 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2907 | main.log.error( self.name + ": EOF exception found" ) |
| 2908 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2909 | main.cleanup() |
| 2910 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2911 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2912 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2913 | main.cleanup() |
| 2914 | main.exit() |
| 2915 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2916 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2917 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2918 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2919 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2920 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2921 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2922 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2923 | cmdStr = "clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2924 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2925 | cmdStr += " -j" |
| 2926 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2927 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2928 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2929 | except AssertionError: |
| 2930 | main.log.exception( "" ) |
| 2931 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2932 | except TypeError: |
| 2933 | main.log.exception( self.name + ": Object not as expected" ) |
| 2934 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2935 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2936 | main.log.error( self.name + ": EOF exception found" ) |
| 2937 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2938 | main.cleanup() |
| 2939 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2940 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2941 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2942 | main.cleanup() |
| 2943 | main.exit() |
| 2944 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2945 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2946 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2947 | CLI command to get the current leader for the Election test application |
| 2948 | NOTE: Requires installation of the onos-app-election feature |
| 2949 | Returns: Node IP of the leader if one exists |
| 2950 | None if none exists |
| 2951 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2952 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2953 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2954 | cmdStr = "election-test-leader" |
| 2955 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2956 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2957 | # Leader |
| 2958 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2959 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2960 | nodeSearch = re.search( leaderPattern, response ) |
| 2961 | if nodeSearch: |
| 2962 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2963 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2964 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2965 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2966 | # no leader |
| 2967 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2968 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2969 | nullSearch = re.search( nullPattern, response ) |
| 2970 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2971 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2972 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2973 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2974 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 2975 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 2976 | ": " + "unexpected response" ) |
| 2977 | main.log.error( repr( response ) ) |
| 2978 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2979 | except AssertionError: |
| 2980 | main.log.exception( "" ) |
| 2981 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2982 | except TypeError: |
| 2983 | main.log.exception( self.name + ": Object not as expected" ) |
| 2984 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2985 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2986 | main.log.error( self.name + ": EOF exception found" ) |
| 2987 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2988 | main.cleanup() |
| 2989 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2990 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2991 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2992 | main.cleanup() |
| 2993 | main.exit() |
| 2994 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2995 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2996 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2997 | CLI command to run for leadership of the Election test application. |
| 2998 | NOTE: Requires installation of the onos-app-election feature |
| 2999 | Returns: Main.TRUE on success |
| 3000 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3001 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3002 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3003 | cmdStr = "election-test-run" |
| 3004 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3005 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3006 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3007 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3008 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3009 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3010 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3011 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3012 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3013 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3014 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3015 | main.log.error( "Error in electionTestRun on " + self.name + |
| 3016 | ": " + "unexpected response" ) |
| 3017 | main.log.error( repr( response ) ) |
| 3018 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3019 | except AssertionError: |
| 3020 | main.log.exception( "" ) |
| 3021 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3022 | except TypeError: |
| 3023 | main.log.exception( self.name + ": Object not as expected" ) |
| 3024 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3025 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3026 | main.log.error( self.name + ": EOF exception found" ) |
| 3027 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3028 | main.cleanup() |
| 3029 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3030 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3031 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3032 | main.cleanup() |
| 3033 | main.exit() |
| 3034 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3035 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3036 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3037 | * CLI command to withdraw the local node from leadership election for |
| 3038 | * the Election test application. |
| 3039 | #NOTE: Requires installation of the onos-app-election feature |
| 3040 | Returns: Main.TRUE on success |
| 3041 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3042 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3043 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3044 | cmdStr = "election-test-withdraw" |
| 3045 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3046 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3047 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3048 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3049 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3050 | if re.search( successPattern, response ): |
| 3051 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3052 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3053 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3054 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3055 | main.log.error( "Error in electionTestWithdraw on " + |
| 3056 | self.name + ": " + "unexpected response" ) |
| 3057 | main.log.error( repr( response ) ) |
| 3058 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3059 | except AssertionError: |
| 3060 | main.log.exception( "" ) |
| 3061 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3062 | except TypeError: |
| 3063 | main.log.exception( self.name + ": Object not as expected" ) |
| 3064 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3065 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3066 | main.log.error( self.name + ": EOF exception found" ) |
| 3067 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3068 | main.cleanup() |
| 3069 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3070 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3071 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3072 | main.cleanup() |
| 3073 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3074 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3075 | def getDevicePortsEnabledCount( self, dpid ): |
| 3076 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3077 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3078 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3079 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3080 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3081 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 3082 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3083 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3084 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3085 | if re.search( "No such device", output ): |
| 3086 | main.log.error( "Error in getting ports" ) |
| 3087 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3088 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3089 | except AssertionError: |
| 3090 | main.log.exception( "" ) |
| 3091 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3092 | except TypeError: |
| 3093 | main.log.exception( self.name + ": Object not as expected" ) |
| 3094 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3095 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3096 | main.log.error( self.name + ": EOF exception found" ) |
| 3097 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3098 | main.cleanup() |
| 3099 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3100 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3101 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3102 | main.cleanup() |
| 3103 | main.exit() |
| 3104 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3105 | def getDeviceLinksActiveCount( self, dpid ): |
| 3106 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3107 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3108 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3109 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3110 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3111 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3112 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3113 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3114 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3115 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3116 | main.log.error( "Error in getting ports " ) |
| 3117 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3118 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3119 | except AssertionError: |
| 3120 | main.log.exception( "" ) |
| 3121 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3122 | except TypeError: |
| 3123 | main.log.exception( self.name + ": Object not as expected" ) |
| 3124 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3125 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3126 | main.log.error( self.name + ": EOF exception found" ) |
| 3127 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3128 | main.cleanup() |
| 3129 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3130 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3131 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3132 | main.cleanup() |
| 3133 | main.exit() |
| 3134 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3135 | def getAllIntentIds( self ): |
| 3136 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3137 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3138 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3139 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3140 | cmdStr = "onos:intents | grep id=" |
| 3141 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3142 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3143 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3144 | if re.search( "Error", output ): |
| 3145 | main.log.error( "Error in getting ports" ) |
| 3146 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3147 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3148 | except AssertionError: |
| 3149 | main.log.exception( "" ) |
| 3150 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3151 | except TypeError: |
| 3152 | main.log.exception( self.name + ": Object not as expected" ) |
| 3153 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3154 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3155 | main.log.error( self.name + ": EOF exception found" ) |
| 3156 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3157 | main.cleanup() |
| 3158 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3159 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3160 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3161 | main.cleanup() |
| 3162 | main.exit() |
| 3163 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3164 | def intentSummary( self ): |
| 3165 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3166 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3167 | """ |
| 3168 | try: |
| 3169 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3170 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3171 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3172 | states.append( intent.get( 'state', None ) ) |
| 3173 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3174 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3175 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3176 | except ( TypeError, ValueError ): |
| 3177 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3178 | return None |
| 3179 | except pexpect.EOF: |
| 3180 | main.log.error( self.name + ": EOF exception found" ) |
| 3181 | main.log.error( self.name + ": " + self.handle.before ) |
| 3182 | main.cleanup() |
| 3183 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3184 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3185 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3186 | main.cleanup() |
| 3187 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3188 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3189 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3190 | """ |
| 3191 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3192 | Optional argument: |
| 3193 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3194 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3195 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3196 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3197 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3198 | cmdStr += " -j" |
| 3199 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3200 | assert output is not None, "Error in sendline" |
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 |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 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!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3216 | main.cleanup() |
| 3217 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3218 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3219 | def leaderCandidates( self, jsonFormat=True ): |
| 3220 | """ |
| 3221 | Returns the output of the leaders -c command. |
| 3222 | Optional argument: |
| 3223 | * jsonFormat - boolean indicating if you want output in json |
| 3224 | """ |
| 3225 | try: |
| 3226 | cmdStr = "onos:leaders -c" |
| 3227 | if jsonFormat: |
| 3228 | cmdStr += " -j" |
| 3229 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3230 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3231 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3232 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3233 | except AssertionError: |
| 3234 | main.log.exception( "" ) |
| 3235 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3236 | except TypeError: |
| 3237 | main.log.exception( self.name + ": Object not as expected" ) |
| 3238 | return None |
| 3239 | except pexpect.EOF: |
| 3240 | main.log.error( self.name + ": EOF exception found" ) |
| 3241 | main.log.error( self.name + ": " + self.handle.before ) |
| 3242 | main.cleanup() |
| 3243 | main.exit() |
| 3244 | except Exception: |
| 3245 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3246 | main.cleanup() |
| 3247 | main.exit() |
| 3248 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3249 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3250 | """ |
| 3251 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
| 3252 | topic parameter and an empty list if the topic doesn't exist |
| 3253 | If no leader is elected leader in the returned list will be "none" |
| 3254 | Returns None if there is a type error processing the json object |
| 3255 | """ |
| 3256 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3257 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3258 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3259 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3260 | assert "Command not found:" not in rawOutput, rawOutput |
| 3261 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3262 | results = [] |
| 3263 | for dict in output: |
| 3264 | if dict["topic"] == topic: |
| 3265 | leader = dict["leader"] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3266 | candidates = re.split( ", ", dict["candidates"][1:-1] ) |
| 3267 | results.append( leader ) |
| 3268 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3269 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3270 | except AssertionError: |
| 3271 | main.log.exception( "" ) |
| 3272 | return None |
| 3273 | except ( TypeError, ValueError ): |
| 3274 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3275 | return None |
| 3276 | except pexpect.EOF: |
| 3277 | main.log.error( self.name + ": EOF exception found" ) |
| 3278 | main.log.error( self.name + ": " + self.handle.before ) |
| 3279 | main.cleanup() |
| 3280 | main.exit() |
| 3281 | except Exception: |
| 3282 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3283 | main.cleanup() |
| 3284 | main.exit() |
| 3285 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3286 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3287 | """ |
| 3288 | Returns the output of the intent Pending map. |
| 3289 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3290 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3291 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3292 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3293 | cmdStr += " -j" |
| 3294 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3295 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3296 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3297 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3298 | except AssertionError: |
| 3299 | main.log.exception( "" ) |
| 3300 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3301 | except TypeError: |
| 3302 | main.log.exception( self.name + ": Object not as expected" ) |
| 3303 | return None |
| 3304 | except pexpect.EOF: |
| 3305 | main.log.error( self.name + ": EOF exception found" ) |
| 3306 | main.log.error( self.name + ": " + self.handle.before ) |
| 3307 | main.cleanup() |
| 3308 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3309 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3310 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3311 | main.cleanup() |
| 3312 | main.exit() |
| 3313 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3314 | def partitions( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3315 | """ |
| 3316 | Returns the output of the raft partitions command for ONOS. |
| 3317 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3318 | # Sample JSON |
| 3319 | # { |
| 3320 | # "leader": "tcp://10.128.30.11:7238", |
| 3321 | # "members": [ |
| 3322 | # "tcp://10.128.30.11:7238", |
| 3323 | # "tcp://10.128.30.17:7238", |
| 3324 | # "tcp://10.128.30.13:7238", |
| 3325 | # ], |
| 3326 | # "name": "p1", |
| 3327 | # "term": 3 |
| 3328 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3329 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3330 | cmdStr = "onos:partitions" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3331 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3332 | cmdStr += " -j" |
| 3333 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3334 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3335 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3336 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3337 | except AssertionError: |
| 3338 | main.log.exception( "" ) |
| 3339 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3340 | except TypeError: |
| 3341 | main.log.exception( self.name + ": Object not as expected" ) |
| 3342 | return None |
| 3343 | except pexpect.EOF: |
| 3344 | main.log.error( self.name + ": EOF exception found" ) |
| 3345 | main.log.error( self.name + ": " + self.handle.before ) |
| 3346 | main.cleanup() |
| 3347 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3348 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3349 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3350 | main.cleanup() |
| 3351 | main.exit() |
| 3352 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3353 | def apps( self, jsonFormat=True ): |
| 3354 | """ |
| 3355 | Returns the output of the apps command for ONOS. This command lists |
| 3356 | information about installed ONOS applications |
| 3357 | """ |
| 3358 | # Sample JSON object |
| 3359 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
| 3360 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3361 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
| 3362 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
| 3363 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3364 | cmdStr = "onos:apps" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3365 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3366 | cmdStr += " -j" |
| 3367 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3368 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3369 | assert "Command not found:" not in output, output |
| 3370 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3371 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3372 | # FIXME: look at specific exceptions/Errors |
| 3373 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3374 | main.log.exception( "Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3375 | return None |
| 3376 | except TypeError: |
| 3377 | main.log.exception( self.name + ": Object not as expected" ) |
| 3378 | return None |
| 3379 | except pexpect.EOF: |
| 3380 | main.log.error( self.name + ": EOF exception found" ) |
| 3381 | main.log.error( self.name + ": " + self.handle.before ) |
| 3382 | main.cleanup() |
| 3383 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3384 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3385 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3386 | main.cleanup() |
| 3387 | main.exit() |
| 3388 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3389 | def appStatus( self, appName ): |
| 3390 | """ |
| 3391 | Uses the onos:apps cli command to return the status of an application. |
| 3392 | Returns: |
| 3393 | "ACTIVE" - If app is installed and activated |
| 3394 | "INSTALLED" - If app is installed and deactivated |
| 3395 | "UNINSTALLED" - If app is not installed |
| 3396 | None - on error |
| 3397 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3398 | try: |
| 3399 | if not isinstance( appName, types.StringType ): |
| 3400 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3401 | " a string" ) |
| 3402 | return None |
| 3403 | output = self.apps( jsonFormat=True ) |
| 3404 | appsJson = json.loads( output ) |
| 3405 | state = None |
| 3406 | for app in appsJson: |
| 3407 | if appName == app.get('name'): |
| 3408 | state = app.get('state') |
| 3409 | break |
| 3410 | if state == "ACTIVE" or state == "INSTALLED": |
| 3411 | return state |
| 3412 | elif state is None: |
| 3413 | return "UNINSTALLED" |
| 3414 | elif state: |
| 3415 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3416 | str( state ) ) |
| 3417 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3418 | except ( TypeError, ValueError ): |
| 3419 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3420 | return None |
| 3421 | except pexpect.EOF: |
| 3422 | main.log.error( self.name + ": EOF exception found" ) |
| 3423 | main.log.error( self.name + ": " + self.handle.before ) |
| 3424 | main.cleanup() |
| 3425 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3426 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3427 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3428 | main.cleanup() |
| 3429 | main.exit() |
| 3430 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3431 | def app( self, appName, option ): |
| 3432 | """ |
| 3433 | Interacts with the app command for ONOS. This command manages |
| 3434 | application inventory. |
| 3435 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3436 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3437 | # Validate argument types |
| 3438 | valid = True |
| 3439 | if not isinstance( appName, types.StringType ): |
| 3440 | main.log.error( self.name + ".app(): appName must be a " + |
| 3441 | "string" ) |
| 3442 | valid = False |
| 3443 | if not isinstance( option, types.StringType ): |
| 3444 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3445 | valid = False |
| 3446 | if not valid: |
| 3447 | return main.FALSE |
| 3448 | # Validate Option |
| 3449 | option = option.lower() |
| 3450 | # NOTE: Install may become a valid option |
| 3451 | if option == "activate": |
| 3452 | pass |
| 3453 | elif option == "deactivate": |
| 3454 | pass |
| 3455 | elif option == "uninstall": |
| 3456 | pass |
| 3457 | else: |
| 3458 | # Invalid option |
| 3459 | main.log.error( "The ONOS app command argument only takes " + |
| 3460 | "the values: (activate|deactivate|uninstall)" + |
| 3461 | "; was given '" + option + "'") |
| 3462 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3463 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3464 | output = self.sendline( cmdStr ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3465 | if "Error executing command" in output: |
| 3466 | main.log.error( "Error in processing onos:app command: " + |
| 3467 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3468 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3469 | elif "No such application" in output: |
| 3470 | main.log.error( "The application '" + appName + |
| 3471 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3472 | return main.FALSE |
| 3473 | elif "Command not found:" in output: |
| 3474 | main.log.error( "Error in processing onos:app command: " + |
| 3475 | str( output ) ) |
| 3476 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3477 | elif "Unsupported command:" in output: |
| 3478 | main.log.error( "Incorrect command given to 'app': " + |
| 3479 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3480 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3481 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3482 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3483 | return main.TRUE |
| 3484 | except TypeError: |
| 3485 | main.log.exception( self.name + ": Object not as expected" ) |
| 3486 | return main.ERROR |
| 3487 | except pexpect.EOF: |
| 3488 | main.log.error( self.name + ": EOF exception found" ) |
| 3489 | main.log.error( self.name + ": " + self.handle.before ) |
| 3490 | main.cleanup() |
| 3491 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3492 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3493 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3494 | main.cleanup() |
| 3495 | main.exit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3496 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3497 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3498 | """ |
| 3499 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3500 | appName is the hierarchical app name, not the feature name |
| 3501 | If check is True, method will check the status of the app after the |
| 3502 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3503 | Returns main.TRUE if the command was successfully sent |
| 3504 | main.FALSE if the cli responded with an error or given |
| 3505 | incorrect input |
| 3506 | """ |
| 3507 | try: |
| 3508 | if not isinstance( appName, types.StringType ): |
| 3509 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3510 | " a string" ) |
| 3511 | return main.FALSE |
| 3512 | status = self.appStatus( appName ) |
| 3513 | if status == "INSTALLED": |
| 3514 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3515 | if check and response == main.TRUE: |
| 3516 | for i in range(10): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3517 | status = self.appStatus( appName ) |
| 3518 | if status == "ACTIVE": |
| 3519 | return main.TRUE |
| 3520 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3521 | main.log.debug( "The state of application " + |
| 3522 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3523 | time.sleep( 1 ) |
| 3524 | return main.FALSE |
| 3525 | else: # not 'check' or command didn't succeed |
| 3526 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3527 | elif status == "ACTIVE": |
| 3528 | return main.TRUE |
| 3529 | elif status == "UNINSTALLED": |
| 3530 | main.log.error( self.name + ": Tried to activate the " + |
| 3531 | "application '" + appName + "' which is not " + |
| 3532 | "installed." ) |
| 3533 | else: |
| 3534 | main.log.error( "Unexpected return value from appStatus: " + |
| 3535 | str( status ) ) |
| 3536 | return main.ERROR |
| 3537 | except TypeError: |
| 3538 | main.log.exception( self.name + ": Object not as expected" ) |
| 3539 | return main.ERROR |
| 3540 | except pexpect.EOF: |
| 3541 | main.log.error( self.name + ": EOF exception found" ) |
| 3542 | main.log.error( self.name + ": " + self.handle.before ) |
| 3543 | main.cleanup() |
| 3544 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3545 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3546 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3547 | main.cleanup() |
| 3548 | main.exit() |
| 3549 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3550 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3551 | """ |
| 3552 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3553 | appName is the hierarchical app name, not the feature name |
| 3554 | If check is True, method will check the status of the app after the |
| 3555 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3556 | Returns main.TRUE if the command was successfully sent |
| 3557 | main.FALSE if the cli responded with an error or given |
| 3558 | incorrect input |
| 3559 | """ |
| 3560 | try: |
| 3561 | if not isinstance( appName, types.StringType ): |
| 3562 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3563 | "be a string" ) |
| 3564 | return main.FALSE |
| 3565 | status = self.appStatus( appName ) |
| 3566 | if status == "INSTALLED": |
| 3567 | return main.TRUE |
| 3568 | elif status == "ACTIVE": |
| 3569 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3570 | if check and response == main.TRUE: |
| 3571 | for i in range(10): # try 10 times then give up |
| 3572 | status = self.appStatus( appName ) |
| 3573 | if status == "INSTALLED": |
| 3574 | return main.TRUE |
| 3575 | else: |
| 3576 | time.sleep( 1 ) |
| 3577 | return main.FALSE |
| 3578 | else: # not check or command didn't succeed |
| 3579 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3580 | elif status == "UNINSTALLED": |
| 3581 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3582 | "application '" + appName + "' which is not " + |
| 3583 | "installed." ) |
| 3584 | return main.TRUE |
| 3585 | else: |
| 3586 | main.log.error( "Unexpected return value from appStatus: " + |
| 3587 | str( status ) ) |
| 3588 | return main.ERROR |
| 3589 | except TypeError: |
| 3590 | main.log.exception( self.name + ": Object not as expected" ) |
| 3591 | return main.ERROR |
| 3592 | except pexpect.EOF: |
| 3593 | main.log.error( self.name + ": EOF exception found" ) |
| 3594 | main.log.error( self.name + ": " + self.handle.before ) |
| 3595 | main.cleanup() |
| 3596 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3597 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3598 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3599 | main.cleanup() |
| 3600 | main.exit() |
| 3601 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3602 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3603 | """ |
| 3604 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3605 | appName is the hierarchical app name, not the feature name |
| 3606 | If check is True, method will check the status of the app after the |
| 3607 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3608 | Returns main.TRUE if the command was successfully sent |
| 3609 | main.FALSE if the cli responded with an error or given |
| 3610 | incorrect input |
| 3611 | """ |
| 3612 | # TODO: check with Thomas about the state machine for apps |
| 3613 | try: |
| 3614 | if not isinstance( appName, types.StringType ): |
| 3615 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3616 | "be a string" ) |
| 3617 | return main.FALSE |
| 3618 | status = self.appStatus( appName ) |
| 3619 | if status == "INSTALLED": |
| 3620 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3621 | if check and response == main.TRUE: |
| 3622 | for i in range(10): # try 10 times then give up |
| 3623 | status = self.appStatus( appName ) |
| 3624 | if status == "UNINSTALLED": |
| 3625 | return main.TRUE |
| 3626 | else: |
| 3627 | time.sleep( 1 ) |
| 3628 | return main.FALSE |
| 3629 | else: # not check or command didn't succeed |
| 3630 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3631 | elif status == "ACTIVE": |
| 3632 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3633 | "application '" + appName + "' which is " + |
| 3634 | "currently active." ) |
| 3635 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3636 | if check and response == main.TRUE: |
| 3637 | for i in range(10): # try 10 times then give up |
| 3638 | status = self.appStatus( appName ) |
| 3639 | if status == "UNINSTALLED": |
| 3640 | return main.TRUE |
| 3641 | else: |
| 3642 | time.sleep( 1 ) |
| 3643 | return main.FALSE |
| 3644 | else: # not check or command didn't succeed |
| 3645 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3646 | elif status == "UNINSTALLED": |
| 3647 | return main.TRUE |
| 3648 | else: |
| 3649 | main.log.error( "Unexpected return value from appStatus: " + |
| 3650 | str( status ) ) |
| 3651 | return main.ERROR |
| 3652 | except TypeError: |
| 3653 | main.log.exception( self.name + ": Object not as expected" ) |
| 3654 | return main.ERROR |
| 3655 | except pexpect.EOF: |
| 3656 | main.log.error( self.name + ": EOF exception found" ) |
| 3657 | main.log.error( self.name + ": " + self.handle.before ) |
| 3658 | main.cleanup() |
| 3659 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3660 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3661 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3662 | main.cleanup() |
| 3663 | main.exit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3664 | |
| 3665 | def appIDs( self, jsonFormat=True ): |
| 3666 | """ |
| 3667 | Show the mappings between app id and app names given by the 'app-ids' |
| 3668 | cli command |
| 3669 | """ |
| 3670 | try: |
| 3671 | cmdStr = "app-ids" |
| 3672 | if jsonFormat: |
| 3673 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3674 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3675 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3676 | assert "Command not found:" not in output, output |
| 3677 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3678 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3679 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3680 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3681 | return None |
| 3682 | except TypeError: |
| 3683 | main.log.exception( self.name + ": Object not as expected" ) |
| 3684 | return None |
| 3685 | except pexpect.EOF: |
| 3686 | main.log.error( self.name + ": EOF exception found" ) |
| 3687 | main.log.error( self.name + ": " + self.handle.before ) |
| 3688 | main.cleanup() |
| 3689 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3690 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3691 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3692 | main.cleanup() |
| 3693 | main.exit() |
| 3694 | |
| 3695 | def appToIDCheck( self ): |
| 3696 | """ |
| 3697 | This method will check that each application's ID listed in 'apps' is |
| 3698 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3699 | there are no duplicate IDs issued. Note that an app ID should be |
| 3700 | a globaly unique numerical identifier for app/app-like features. Once |
| 3701 | an ID is registered, the ID is never freed up so that if an app is |
| 3702 | reinstalled it will have the same ID. |
| 3703 | |
| 3704 | Returns: main.TRUE if the check passes and |
| 3705 | main.FALSE if the check fails or |
| 3706 | main.ERROR if there is some error in processing the test |
| 3707 | """ |
| 3708 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3709 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3710 | rawJson = self.appIDs( jsonFormat=True ) |
| 3711 | if rawJson: |
| 3712 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3713 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3714 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3715 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3716 | rawJson = self.apps( jsonFormat=True ) |
| 3717 | if rawJson: |
| 3718 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3719 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3720 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3721 | bail = True |
| 3722 | if bail: |
| 3723 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3724 | result = main.TRUE |
| 3725 | for app in apps: |
| 3726 | appID = app.get( 'id' ) |
| 3727 | if appID is None: |
| 3728 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3729 | result = main.FALSE |
| 3730 | appName = app.get( 'name' ) |
| 3731 | if appName is None: |
| 3732 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3733 | result = main.FALSE |
| 3734 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3735 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3736 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3737 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3738 | if not current: # if ids doesn't have this id |
| 3739 | result = main.FALSE |
| 3740 | main.log.error( "'app-ids' does not have the ID for " + |
| 3741 | str( appName ) + " that apps does." ) |
| 3742 | elif len( current ) > 1: |
| 3743 | # there is more than one app with this ID |
| 3744 | result = main.FALSE |
| 3745 | # We will log this later in the method |
| 3746 | elif not current[0][ 'name' ] == appName: |
| 3747 | currentName = current[0][ 'name' ] |
| 3748 | result = main.FALSE |
| 3749 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3750 | " registered under id:" + str( appID ) + |
| 3751 | " but 'apps' has " + str( appName ) ) |
| 3752 | else: |
| 3753 | pass # id and name match! |
| 3754 | # now make sure that app-ids has no duplicates |
| 3755 | idsList = [] |
| 3756 | namesList = [] |
| 3757 | for item in ids: |
| 3758 | idsList.append( item[ 'id' ] ) |
| 3759 | namesList.append( item[ 'name' ] ) |
| 3760 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3761 | len( namesList ) != len( set( namesList ) ): |
| 3762 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3763 | + json.dumps( ids, |
| 3764 | sort_keys=True, |
| 3765 | indent=4, |
| 3766 | separators=( ',', ': ' ) ) ) |
| 3767 | result = main.FALSE |
| 3768 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3769 | except ( TypeError, ValueError ): |
| 3770 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3771 | return main.ERROR |
| 3772 | except pexpect.EOF: |
| 3773 | main.log.error( self.name + ": EOF exception found" ) |
| 3774 | main.log.error( self.name + ": " + self.handle.before ) |
| 3775 | main.cleanup() |
| 3776 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3777 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3778 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3779 | main.cleanup() |
| 3780 | main.exit() |
| 3781 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3782 | def getCfg( self, component=None, propName=None, short=False, |
| 3783 | jsonFormat=True ): |
| 3784 | """ |
| 3785 | Get configuration settings from onos cli |
| 3786 | Optional arguments: |
| 3787 | component - Optionally only list configurations for a specific |
| 3788 | component. If None, all components with configurations |
| 3789 | are displayed. Case Sensitive string. |
| 3790 | propName - If component is specified, propName option will show |
| 3791 | only this specific configuration from that component. |
| 3792 | Case Sensitive string. |
| 3793 | jsonFormat - Returns output as json. Note that this will override |
| 3794 | the short option |
| 3795 | short - Short, less verbose, version of configurations. |
| 3796 | This is overridden by the json option |
| 3797 | returns: |
| 3798 | Output from cli as a string or None on error |
| 3799 | """ |
| 3800 | try: |
| 3801 | baseStr = "cfg" |
| 3802 | cmdStr = " get" |
| 3803 | componentStr = "" |
| 3804 | if component: |
| 3805 | componentStr += " " + component |
| 3806 | if propName: |
| 3807 | componentStr += " " + propName |
| 3808 | if jsonFormat: |
| 3809 | baseStr += " -j" |
| 3810 | elif short: |
| 3811 | baseStr += " -s" |
| 3812 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3813 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3814 | assert "Command not found:" not in output, output |
| 3815 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3816 | return output |
| 3817 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3818 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3819 | return None |
| 3820 | except TypeError: |
| 3821 | main.log.exception( self.name + ": Object not as expected" ) |
| 3822 | return None |
| 3823 | except pexpect.EOF: |
| 3824 | main.log.error( self.name + ": EOF exception found" ) |
| 3825 | main.log.error( self.name + ": " + self.handle.before ) |
| 3826 | main.cleanup() |
| 3827 | main.exit() |
| 3828 | except Exception: |
| 3829 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3830 | main.cleanup() |
| 3831 | main.exit() |
| 3832 | |
| 3833 | def setCfg( self, component, propName, value=None, check=True ): |
| 3834 | """ |
| 3835 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3836 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3837 | component - The case sensitive name of the component whose |
| 3838 | property is to be set |
| 3839 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3840 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3841 | value - The value to set the property to. If None, will unset the |
| 3842 | property and revert it to it's default value(if applicable) |
| 3843 | check - Boolean, Check whether the option was successfully set this |
| 3844 | only applies when a value is given. |
| 3845 | returns: |
| 3846 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3847 | will return main.TRUE unless there is an error |
| 3848 | """ |
| 3849 | try: |
| 3850 | baseStr = "cfg" |
| 3851 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3852 | if value is not None: |
| 3853 | cmdStr += " " + str( value ) |
| 3854 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3855 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3856 | assert "Command not found:" not in output, output |
| 3857 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3858 | if value and check: |
| 3859 | results = self.getCfg( component=str( component ), |
| 3860 | propName=str( propName ), |
| 3861 | jsonFormat=True ) |
| 3862 | # Check if current value is what we just set |
| 3863 | try: |
| 3864 | jsonOutput = json.loads( results ) |
| 3865 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3866 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3867 | main.log.exception( "Error parsing cfg output" ) |
| 3868 | main.log.error( "output:" + repr( results ) ) |
| 3869 | return main.FALSE |
| 3870 | if current == str( value ): |
| 3871 | return main.TRUE |
| 3872 | return main.FALSE |
| 3873 | return main.TRUE |
| 3874 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3875 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3876 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3877 | except ( TypeError, ValueError ): |
| 3878 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3879 | return main.FALSE |
| 3880 | except pexpect.EOF: |
| 3881 | main.log.error( self.name + ": EOF exception found" ) |
| 3882 | main.log.error( self.name + ": " + self.handle.before ) |
| 3883 | main.cleanup() |
| 3884 | main.exit() |
| 3885 | except Exception: |
| 3886 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3887 | main.cleanup() |
| 3888 | main.exit() |
| 3889 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3890 | def setTestAdd( self, setName, values ): |
| 3891 | """ |
| 3892 | CLI command to add elements to a distributed set. |
| 3893 | Arguments: |
| 3894 | setName - The name of the set to add to. |
| 3895 | values - The value(s) to add to the set, space seperated. |
| 3896 | Example usages: |
| 3897 | setTestAdd( "set1", "a b c" ) |
| 3898 | setTestAdd( "set2", "1" ) |
| 3899 | returns: |
| 3900 | main.TRUE on success OR |
| 3901 | main.FALSE if elements were already in the set OR |
| 3902 | main.ERROR on error |
| 3903 | """ |
| 3904 | try: |
| 3905 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
| 3906 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3907 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3908 | assert "Command not found:" not in output, output |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3909 | try: |
| 3910 | # TODO: Maybe make this less hardcoded |
| 3911 | # ConsistentMap Exceptions |
| 3912 | assert "org.onosproject.store.service" not in output |
| 3913 | # Node not leader |
| 3914 | assert "java.lang.IllegalStateException" not in output |
| 3915 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3916 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3917 | "command: " + str( output ) ) |
| 3918 | retryTime = 30 # Conservative time, given by Madan |
| 3919 | main.log.info( "Waiting " + str( retryTime ) + |
| 3920 | "seconds before retrying." ) |
| 3921 | time.sleep( retryTime ) # Due to change in mastership |
| 3922 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3923 | assert output is not None, "Error in sendline" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3924 | assert "Error executing command" not in output |
| 3925 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3926 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
| 3927 | main.log.info( self.name + ": " + output ) |
| 3928 | if re.search( positiveMatch, output): |
| 3929 | return main.TRUE |
| 3930 | elif re.search( negativeMatch, output): |
| 3931 | return main.FALSE |
| 3932 | else: |
| 3933 | main.log.error( self.name + ": setTestAdd did not" + |
| 3934 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3935 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3936 | return main.ERROR |
| 3937 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3938 | main.log.exception( "Error in processing '" + cmdStr + "' command. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3939 | return main.ERROR |
| 3940 | except TypeError: |
| 3941 | main.log.exception( self.name + ": Object not as expected" ) |
| 3942 | return main.ERROR |
| 3943 | except pexpect.EOF: |
| 3944 | main.log.error( self.name + ": EOF exception found" ) |
| 3945 | main.log.error( self.name + ": " + self.handle.before ) |
| 3946 | main.cleanup() |
| 3947 | main.exit() |
| 3948 | except Exception: |
| 3949 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3950 | main.cleanup() |
| 3951 | main.exit() |
| 3952 | |
| 3953 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 3954 | """ |
| 3955 | CLI command to remove elements from a distributed set. |
| 3956 | Required arguments: |
| 3957 | setName - The name of the set to remove from. |
| 3958 | values - The value(s) to remove from the set, space seperated. |
| 3959 | Optional arguments: |
| 3960 | clear - Clear all elements from the set |
| 3961 | retain - Retain only the given values. (intersection of the |
| 3962 | original set and the given set) |
| 3963 | returns: |
| 3964 | main.TRUE on success OR |
| 3965 | main.FALSE if the set was not changed OR |
| 3966 | main.ERROR on error |
| 3967 | """ |
| 3968 | try: |
| 3969 | cmdStr = "set-test-remove " |
| 3970 | if clear: |
| 3971 | cmdStr += "-c " + str( setName ) |
| 3972 | elif retain: |
| 3973 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 3974 | else: |
| 3975 | cmdStr += str( setName ) + " " + str( values ) |
| 3976 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3977 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3978 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3979 | # TODO: Maybe make this less hardcoded |
| 3980 | # ConsistentMap Exceptions |
| 3981 | assert "org.onosproject.store.service" not in output |
| 3982 | # Node not leader |
| 3983 | assert "java.lang.IllegalStateException" not in output |
| 3984 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3985 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3986 | "command: " + str( output ) ) |
| 3987 | retryTime = 30 # Conservative time, given by Madan |
| 3988 | main.log.info( "Waiting " + str( retryTime ) + |
| 3989 | "seconds before retrying." ) |
| 3990 | time.sleep( retryTime ) # Due to change in mastership |
| 3991 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3992 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3993 | assert "Command not found:" not in output, output |
| 3994 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3995 | main.log.info( self.name + ": " + output ) |
| 3996 | if clear: |
| 3997 | pattern = "Set " + str( setName ) + " cleared" |
| 3998 | if re.search( pattern, output ): |
| 3999 | return main.TRUE |
| 4000 | elif retain: |
| 4001 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4002 | "only elements of set \[(.*)\]" |
| 4003 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4004 | "retaining only elements of the set " +\ |
| 4005 | "\[(.*)\]" |
| 4006 | if re.search( positivePattern, output ): |
| 4007 | return main.TRUE |
| 4008 | elif re.search( negativePattern, output ): |
| 4009 | return main.FALSE |
| 4010 | else: |
| 4011 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4012 | str( setName ) |
| 4013 | if ( len( values.split() ) == 1 ): |
| 4014 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4015 | str( setName ) |
| 4016 | else: |
| 4017 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4018 | str( setName ) |
| 4019 | if re.search( positivePattern, output ): |
| 4020 | return main.TRUE |
| 4021 | elif re.search( negativePattern, output ): |
| 4022 | return main.FALSE |
| 4023 | main.log.error( self.name + ": setTestRemove did not" + |
| 4024 | " match expected output" ) |
| 4025 | main.log.debug( self.name + " expected: " + pattern ) |
| 4026 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4027 | return main.ERROR |
| 4028 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4029 | main.log.exception( "Error in processing '" + cmdStr + "' commandr. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4030 | return main.ERROR |
| 4031 | except TypeError: |
| 4032 | main.log.exception( self.name + ": Object not as expected" ) |
| 4033 | return main.ERROR |
| 4034 | except pexpect.EOF: |
| 4035 | main.log.error( self.name + ": EOF exception found" ) |
| 4036 | main.log.error( self.name + ": " + self.handle.before ) |
| 4037 | main.cleanup() |
| 4038 | main.exit() |
| 4039 | except Exception: |
| 4040 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4041 | main.cleanup() |
| 4042 | main.exit() |
| 4043 | |
| 4044 | def setTestGet( self, setName, values="" ): |
| 4045 | """ |
| 4046 | CLI command to get the elements in a distributed set. |
| 4047 | Required arguments: |
| 4048 | setName - The name of the set to remove from. |
| 4049 | Optional arguments: |
| 4050 | values - The value(s) to check if in the set, space seperated. |
| 4051 | returns: |
| 4052 | main.ERROR on error OR |
| 4053 | A list of elements in the set if no optional arguments are |
| 4054 | supplied OR |
| 4055 | A tuple containing the list then: |
| 4056 | main.FALSE if the given values are not in the set OR |
| 4057 | main.TRUE if the given values are in the set OR |
| 4058 | """ |
| 4059 | try: |
| 4060 | values = str( values ).strip() |
| 4061 | setName = str( setName ).strip() |
| 4062 | length = len( values.split() ) |
| 4063 | containsCheck = None |
| 4064 | # Patterns to match |
| 4065 | setPattern = "\[(.*)\]" |
| 4066 | pattern = "Items in set " + setName + ":\n" + setPattern |
| 4067 | containsTrue = "Set " + setName + " contains the value " + values |
| 4068 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4069 | values |
| 4070 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4071 | setPattern |
| 4072 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4073 | " subset " + setPattern |
| 4074 | |
| 4075 | cmdStr = "set-test-get " |
| 4076 | cmdStr += setName + " " + values |
| 4077 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4078 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4079 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4080 | # TODO: Maybe make this less hardcoded |
| 4081 | # ConsistentMap Exceptions |
| 4082 | assert "org.onosproject.store.service" not in output |
| 4083 | # Node not leader |
| 4084 | assert "java.lang.IllegalStateException" not in output |
| 4085 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4086 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4087 | "command: " + str( output ) ) |
| 4088 | retryTime = 30 # Conservative time, given by Madan |
| 4089 | main.log.info( "Waiting " + str( retryTime ) + |
| 4090 | "seconds before retrying." ) |
| 4091 | time.sleep( retryTime ) # Due to change in mastership |
| 4092 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4093 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4094 | assert "Command not found:" not in output, output |
| 4095 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4096 | main.log.info( self.name + ": " + output ) |
| 4097 | |
| 4098 | if length == 0: |
| 4099 | match = re.search( pattern, output ) |
| 4100 | else: # if given values |
| 4101 | if length == 1: # Contains output |
| 4102 | patternTrue = pattern + "\n" + containsTrue |
| 4103 | patternFalse = pattern + "\n" + containsFalse |
| 4104 | else: # ContainsAll output |
| 4105 | patternTrue = pattern + "\n" + containsAllTrue |
| 4106 | patternFalse = pattern + "\n" + containsAllFalse |
| 4107 | matchTrue = re.search( patternTrue, output ) |
| 4108 | matchFalse = re.search( patternFalse, output ) |
| 4109 | if matchTrue: |
| 4110 | containsCheck = main.TRUE |
| 4111 | match = matchTrue |
| 4112 | elif matchFalse: |
| 4113 | containsCheck = main.FALSE |
| 4114 | match = matchFalse |
| 4115 | else: |
| 4116 | main.log.error( self.name + " setTestGet did not match " +\ |
| 4117 | "expected output" ) |
| 4118 | main.log.debug( self.name + " expected: " + pattern ) |
| 4119 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4120 | match = None |
| 4121 | if match: |
| 4122 | setMatch = match.group( 1 ) |
| 4123 | if setMatch == '': |
| 4124 | setList = [] |
| 4125 | else: |
| 4126 | setList = setMatch.split( ", " ) |
| 4127 | if length > 0: |
| 4128 | return ( setList, containsCheck ) |
| 4129 | else: |
| 4130 | return setList |
| 4131 | else: # no match |
| 4132 | main.log.error( self.name + ": setTestGet did not" + |
| 4133 | " match expected output" ) |
| 4134 | main.log.debug( self.name + " expected: " + pattern ) |
| 4135 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4136 | return main.ERROR |
| 4137 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4138 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4139 | return main.ERROR |
| 4140 | except TypeError: |
| 4141 | main.log.exception( self.name + ": Object not as expected" ) |
| 4142 | return main.ERROR |
| 4143 | except pexpect.EOF: |
| 4144 | main.log.error( self.name + ": EOF exception found" ) |
| 4145 | main.log.error( self.name + ": " + self.handle.before ) |
| 4146 | main.cleanup() |
| 4147 | main.exit() |
| 4148 | except Exception: |
| 4149 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4150 | main.cleanup() |
| 4151 | main.exit() |
| 4152 | |
| 4153 | def setTestSize( self, setName ): |
| 4154 | """ |
| 4155 | CLI command to get the elements in a distributed set. |
| 4156 | Required arguments: |
| 4157 | setName - The name of the set to remove from. |
| 4158 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4159 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4160 | None on error |
| 4161 | """ |
| 4162 | try: |
| 4163 | # TODO: Should this check against the number of elements returned |
| 4164 | # and then return true/false based on that? |
| 4165 | setName = str( setName ).strip() |
| 4166 | # Patterns to match |
| 4167 | setPattern = "\[(.*)\]" |
| 4168 | pattern = "There are (\d+) items in set " + setName + ":\n" +\ |
| 4169 | setPattern |
| 4170 | cmdStr = "set-test-get -s " |
| 4171 | cmdStr += setName |
| 4172 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4173 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4174 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4175 | # TODO: Maybe make this less hardcoded |
| 4176 | # ConsistentMap Exceptions |
| 4177 | assert "org.onosproject.store.service" not in output |
| 4178 | # Node not leader |
| 4179 | assert "java.lang.IllegalStateException" not in output |
| 4180 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4181 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4182 | "command: " + str( output ) ) |
| 4183 | retryTime = 30 # Conservative time, given by Madan |
| 4184 | main.log.info( "Waiting " + str( retryTime ) + |
| 4185 | "seconds before retrying." ) |
| 4186 | time.sleep( retryTime ) # Due to change in mastership |
| 4187 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4188 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4189 | assert "Command not found:" not in output, output |
| 4190 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4191 | main.log.info( self.name + ": " + output ) |
| 4192 | match = re.search( pattern, output ) |
| 4193 | if match: |
| 4194 | setSize = int( match.group( 1 ) ) |
| 4195 | setMatch = match.group( 2 ) |
| 4196 | if len( setMatch.split() ) == setSize: |
| 4197 | main.log.info( "The size returned by " + self.name + |
| 4198 | " matches the number of elements in " + |
| 4199 | "the returned set" ) |
| 4200 | else: |
| 4201 | main.log.error( "The size returned by " + self.name + |
| 4202 | " does not match the number of " + |
| 4203 | "elements in the returned set." ) |
| 4204 | return setSize |
| 4205 | else: # no match |
| 4206 | main.log.error( self.name + ": setTestGet did not" + |
| 4207 | " match expected output" ) |
| 4208 | main.log.debug( self.name + " expected: " + pattern ) |
| 4209 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4210 | return None |
| 4211 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4212 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 4213 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4214 | except TypeError: |
| 4215 | main.log.exception( self.name + ": Object not as expected" ) |
| 4216 | return None |
| 4217 | except pexpect.EOF: |
| 4218 | main.log.error( self.name + ": EOF exception found" ) |
| 4219 | main.log.error( self.name + ": " + self.handle.before ) |
| 4220 | main.cleanup() |
| 4221 | main.exit() |
| 4222 | except Exception: |
| 4223 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4224 | main.cleanup() |
| 4225 | main.exit() |
| 4226 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4227 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4228 | """ |
| 4229 | Command to list the various counters in the system. |
| 4230 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4231 | if jsonFormat, a string of the json object returned by the cli |
| 4232 | command |
| 4233 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4234 | None on error |
| 4235 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4236 | try: |
| 4237 | counters = {} |
| 4238 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4239 | if jsonFormat: |
| 4240 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4241 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4242 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4243 | assert "Command not found:" not in output, output |
| 4244 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4245 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4246 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4247 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4248 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4249 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4250 | except TypeError: |
| 4251 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4252 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4253 | except pexpect.EOF: |
| 4254 | main.log.error( self.name + ": EOF exception found" ) |
| 4255 | main.log.error( self.name + ": " + self.handle.before ) |
| 4256 | main.cleanup() |
| 4257 | main.exit() |
| 4258 | except Exception: |
| 4259 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4260 | main.cleanup() |
| 4261 | main.exit() |
| 4262 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4263 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4264 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4265 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4266 | Required arguments: |
| 4267 | counter - The name of the counter to increment. |
| 4268 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4269 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4270 | returns: |
| 4271 | integer value of the counter or |
| 4272 | None on Error |
| 4273 | """ |
| 4274 | try: |
| 4275 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4276 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4277 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4278 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4279 | if delta != 1: |
| 4280 | cmdStr += " " + str( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4281 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4282 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4283 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4284 | # TODO: Maybe make this less hardcoded |
| 4285 | # ConsistentMap Exceptions |
| 4286 | assert "org.onosproject.store.service" not in output |
| 4287 | # Node not leader |
| 4288 | assert "java.lang.IllegalStateException" not in output |
| 4289 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4290 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4291 | "command: " + str( output ) ) |
| 4292 | retryTime = 30 # Conservative time, given by Madan |
| 4293 | main.log.info( "Waiting " + str( retryTime ) + |
| 4294 | "seconds before retrying." ) |
| 4295 | time.sleep( retryTime ) # Due to change in mastership |
| 4296 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4297 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4298 | assert "Command not found:" not in output, output |
| 4299 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4300 | main.log.info( self.name + ": " + output ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4301 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4302 | match = re.search( pattern, output ) |
| 4303 | if match: |
| 4304 | return int( match.group( 1 ) ) |
| 4305 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4306 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4307 | " match expected output." ) |
| 4308 | main.log.debug( self.name + " expected: " + pattern ) |
| 4309 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4310 | return None |
| 4311 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4312 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4313 | return None |
| 4314 | except TypeError: |
| 4315 | main.log.exception( self.name + ": Object not as expected" ) |
| 4316 | return None |
| 4317 | except pexpect.EOF: |
| 4318 | main.log.error( self.name + ": EOF exception found" ) |
| 4319 | main.log.error( self.name + ": " + self.handle.before ) |
| 4320 | main.cleanup() |
| 4321 | main.exit() |
| 4322 | except Exception: |
| 4323 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4324 | main.cleanup() |
| 4325 | main.exit() |
| 4326 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4327 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4328 | """ |
| 4329 | CLI command to get a distributed counter then add a delta to it. |
| 4330 | Required arguments: |
| 4331 | counter - The name of the counter to increment. |
| 4332 | Optional arguments: |
| 4333 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4334 | returns: |
| 4335 | integer value of the counter or |
| 4336 | None on Error |
| 4337 | """ |
| 4338 | try: |
| 4339 | counter = str( counter ) |
| 4340 | delta = int( delta ) |
| 4341 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4342 | cmdStr += counter |
| 4343 | if delta != 1: |
| 4344 | cmdStr += " " + str( delta ) |
| 4345 | output = self.sendline( cmdStr ) |
| 4346 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4347 | assert output is not None, "Error in sendline" |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4348 | # TODO: Maybe make this less hardcoded |
| 4349 | # ConsistentMap Exceptions |
| 4350 | assert "org.onosproject.store.service" not in output |
| 4351 | # Node not leader |
| 4352 | assert "java.lang.IllegalStateException" not in output |
| 4353 | except AssertionError: |
| 4354 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4355 | "command: " + str( output ) ) |
| 4356 | retryTime = 30 # Conservative time, given by Madan |
| 4357 | main.log.info( "Waiting " + str( retryTime ) + |
| 4358 | "seconds before retrying." ) |
| 4359 | time.sleep( retryTime ) # Due to change in mastership |
| 4360 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4361 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4362 | assert "Command not found:" not in output, output |
| 4363 | assert "Error executing command" not in output, output |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4364 | main.log.info( self.name + ": " + output ) |
| 4365 | pattern = counter + " was updated to (-?\d+)" |
| 4366 | match = re.search( pattern, output ) |
| 4367 | if match: |
| 4368 | return int( match.group( 1 ) ) |
| 4369 | else: |
| 4370 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4371 | " match expected output." ) |
| 4372 | main.log.debug( self.name + " expected: " + pattern ) |
| 4373 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4374 | return None |
| 4375 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4376 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4377 | return None |
| 4378 | except TypeError: |
| 4379 | main.log.exception( self.name + ": Object not as expected" ) |
| 4380 | return None |
| 4381 | except pexpect.EOF: |
| 4382 | main.log.error( self.name + ": EOF exception found" ) |
| 4383 | main.log.error( self.name + ": " + self.handle.before ) |
| 4384 | main.cleanup() |
| 4385 | main.exit() |
| 4386 | except Exception: |
| 4387 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4388 | main.cleanup() |
| 4389 | main.exit() |
| 4390 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4391 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4392 | """ |
| 4393 | Description: Execute summary command in onos |
| 4394 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4395 | no output |
| 4396 | |
| 4397 | """ |
| 4398 | try: |
| 4399 | cmdStr = "summary" |
| 4400 | if jsonFormat: |
| 4401 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4402 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4403 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4404 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4405 | assert "Error:" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4406 | if not handle: |
| 4407 | main.log.error( self.name + ": There is no output in " + |
| 4408 | "summary command" ) |
| 4409 | return main.FALSE |
| 4410 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4411 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4412 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4413 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4414 | except TypeError: |
| 4415 | main.log.exception( self.name + ": Object not as expected" ) |
| 4416 | return None |
| 4417 | except pexpect.EOF: |
| 4418 | main.log.error( self.name + ": EOF exception found" ) |
| 4419 | main.log.error( self.name + ": " + self.handle.before ) |
| 4420 | main.cleanup() |
| 4421 | main.exit() |
| 4422 | except Exception: |
| 4423 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4424 | main.cleanup() |
| 4425 | main.exit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4426 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4427 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4428 | """ |
| 4429 | CLI command to get the value of a key in a consistent map using |
| 4430 | transactions. This a test function and can only get keys from the |
| 4431 | test map hard coded into the cli command |
| 4432 | Required arguments: |
| 4433 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4434 | returns: |
| 4435 | The string value of the key or |
| 4436 | None on Error |
| 4437 | """ |
| 4438 | try: |
| 4439 | keyName = str( keyName ) |
| 4440 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4441 | cmdStr += keyName |
| 4442 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4443 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4444 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4445 | try: |
| 4446 | # TODO: Maybe make this less hardcoded |
| 4447 | # ConsistentMap Exceptions |
| 4448 | assert "org.onosproject.store.service" not in output |
| 4449 | # Node not leader |
| 4450 | assert "java.lang.IllegalStateException" not in output |
| 4451 | except AssertionError: |
| 4452 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4453 | "command: " + str( output ) ) |
| 4454 | return None |
| 4455 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4456 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4457 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4458 | return None |
| 4459 | else: |
| 4460 | match = re.search( pattern, output ) |
| 4461 | if match: |
| 4462 | return match.groupdict()[ 'value' ] |
| 4463 | else: |
| 4464 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4465 | " match expected output." ) |
| 4466 | main.log.debug( self.name + " expected: " + pattern ) |
| 4467 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4468 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4469 | except AssertionError: |
| 4470 | main.log.exception( "" ) |
| 4471 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4472 | except TypeError: |
| 4473 | main.log.exception( self.name + ": Object not as expected" ) |
| 4474 | return None |
| 4475 | except pexpect.EOF: |
| 4476 | main.log.error( self.name + ": EOF exception found" ) |
| 4477 | main.log.error( self.name + ": " + self.handle.before ) |
| 4478 | main.cleanup() |
| 4479 | main.exit() |
| 4480 | except Exception: |
| 4481 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4482 | main.cleanup() |
| 4483 | main.exit() |
| 4484 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4485 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4486 | """ |
| 4487 | CLI command to put a value into 'numKeys' number of keys in a |
| 4488 | consistent map using transactions. This a test function and can only |
| 4489 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4490 | Required arguments: |
| 4491 | numKeys - Number of keys to add the value to |
| 4492 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4493 | returns: |
| 4494 | A dictionary whose keys are the name of the keys put into the map |
| 4495 | and the values of the keys are dictionaries whose key-values are |
| 4496 | 'value': value put into map and optionaly |
| 4497 | 'oldValue': Previous value in the key or |
| 4498 | None on Error |
| 4499 | |
| 4500 | Example output |
| 4501 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4502 | 'Key2': {'value': 'Testing'} } |
| 4503 | """ |
| 4504 | try: |
| 4505 | numKeys = str( numKeys ) |
| 4506 | value = str( value ) |
| 4507 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4508 | cmdStr += numKeys + " " + value |
| 4509 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4510 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4511 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4512 | try: |
| 4513 | # TODO: Maybe make this less hardcoded |
| 4514 | # ConsistentMap Exceptions |
| 4515 | assert "org.onosproject.store.service" not in output |
| 4516 | # Node not leader |
| 4517 | assert "java.lang.IllegalStateException" not in output |
| 4518 | except AssertionError: |
| 4519 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4520 | "command: " + str( output ) ) |
| 4521 | return None |
| 4522 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4523 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4524 | results = {} |
| 4525 | for line in output.splitlines(): |
| 4526 | new = re.search( newPattern, line ) |
| 4527 | updated = re.search( updatedPattern, line ) |
| 4528 | if new: |
| 4529 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4530 | elif updated: |
| 4531 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4532 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4533 | else: |
| 4534 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4535 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4536 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4537 | newPattern, |
| 4538 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4539 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4540 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4541 | except AssertionError: |
| 4542 | main.log.exception( "" ) |
| 4543 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4544 | except TypeError: |
| 4545 | main.log.exception( self.name + ": Object not as expected" ) |
| 4546 | return None |
| 4547 | except pexpect.EOF: |
| 4548 | main.log.error( self.name + ": EOF exception found" ) |
| 4549 | main.log.error( self.name + ": " + self.handle.before ) |
| 4550 | main.cleanup() |
| 4551 | main.exit() |
| 4552 | except Exception: |
| 4553 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4554 | main.cleanup() |
| 4555 | main.exit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4556 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4557 | def maps( self, jsonFormat=True ): |
| 4558 | """ |
| 4559 | Description: Returns result of onos:maps |
| 4560 | Optional: |
| 4561 | * jsonFormat: enable json formatting of output |
| 4562 | """ |
| 4563 | try: |
| 4564 | cmdStr = "maps" |
| 4565 | if jsonFormat: |
| 4566 | cmdStr += " -j" |
| 4567 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4568 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4569 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4570 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4571 | except AssertionError: |
| 4572 | main.log.exception( "" ) |
| 4573 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4574 | except TypeError: |
| 4575 | main.log.exception( self.name + ": Object not as expected" ) |
| 4576 | return None |
| 4577 | except pexpect.EOF: |
| 4578 | main.log.error( self.name + ": EOF exception found" ) |
| 4579 | main.log.error( self.name + ": " + self.handle.before ) |
| 4580 | main.cleanup() |
| 4581 | main.exit() |
| 4582 | except Exception: |
| 4583 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4584 | main.cleanup() |
| 4585 | main.exit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4586 | |
| 4587 | def getSwController( self, uri, jsonFormat=True ): |
| 4588 | """ |
| 4589 | Descrition: Gets the controller information from the device |
| 4590 | """ |
| 4591 | try: |
| 4592 | cmd = "device-controllers " |
| 4593 | if jsonFormat: |
| 4594 | cmd += "-j " |
| 4595 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4596 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4597 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4598 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4599 | except AssertionError: |
| 4600 | main.log.exception( "" ) |
| 4601 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4602 | except TypeError: |
| 4603 | main.log.exception( self.name + ": Object not as expected" ) |
| 4604 | return None |
| 4605 | except pexpect.EOF: |
| 4606 | main.log.error( self.name + ": EOF exception found" ) |
| 4607 | main.log.error( self.name + ": " + self.handle.before ) |
| 4608 | main.cleanup() |
| 4609 | main.exit() |
| 4610 | except Exception: |
| 4611 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4612 | main.cleanup() |
| 4613 | main.exit() |
| 4614 | |
| 4615 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4616 | """ |
| 4617 | Descrition: sets the controller(s) for the specified device |
| 4618 | |
| 4619 | Parameters: |
| 4620 | Required: uri - String: The uri of the device(switch). |
| 4621 | ip - String or List: The ip address of the controller. |
| 4622 | This parameter can be formed in a couple of different ways. |
| 4623 | VALID: |
| 4624 | 10.0.0.1 - just the ip address |
| 4625 | tcp:10.0.0.1 - the protocol and the ip address |
| 4626 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4627 | so that you can add controllers with different |
| 4628 | protocols and ports |
| 4629 | INVALID: |
| 4630 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4631 | |
| 4632 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4633 | port - The port number. |
| 4634 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4635 | |
| 4636 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4637 | """ |
| 4638 | try: |
| 4639 | cmd = "device-setcontrollers" |
| 4640 | |
| 4641 | if jsonFormat: |
| 4642 | cmd += " -j" |
| 4643 | cmd += " " + uri |
| 4644 | if isinstance( ip, str ): |
| 4645 | ip = [ip] |
| 4646 | for item in ip: |
| 4647 | if ":" in item: |
| 4648 | sitem = item.split( ":" ) |
| 4649 | if len(sitem) == 3: |
| 4650 | cmd += " " + item |
| 4651 | elif "." in sitem[1]: |
| 4652 | cmd += " {}:{}".format(item, port) |
| 4653 | else: |
| 4654 | main.log.error( "Malformed entry: " + item ) |
| 4655 | raise TypeError |
| 4656 | else: |
| 4657 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4658 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4659 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4660 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4661 | if "Error" in response: |
| 4662 | main.log.error( response ) |
| 4663 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4664 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4665 | except AssertionError: |
| 4666 | main.log.exception( "" ) |
| 4667 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4668 | except TypeError: |
| 4669 | main.log.exception( self.name + ": Object not as expected" ) |
| 4670 | return main.FALSE |
| 4671 | except pexpect.EOF: |
| 4672 | main.log.error( self.name + ": EOF exception found" ) |
| 4673 | main.log.error( self.name + ": " + self.handle.before ) |
| 4674 | main.cleanup() |
| 4675 | main.exit() |
| 4676 | except Exception: |
| 4677 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4678 | main.cleanup() |
| 4679 | main.exit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4680 | |
| 4681 | def removeDevice( self, device ): |
| 4682 | ''' |
| 4683 | Description: |
| 4684 | Remove a device from ONOS by passing the uri of the device(s). |
| 4685 | Parameters: |
| 4686 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
| 4687 | Returns: |
| 4688 | Returns main.FALSE if an exception is thrown or an error is present |
| 4689 | in the response. Otherwise, returns main.TRUE. |
| 4690 | NOTE: |
| 4691 | If a host cannot be removed, then this function will return main.FALSE |
| 4692 | ''' |
| 4693 | try: |
| 4694 | if type( device ) is str: |
| 4695 | device = list( device ) |
| 4696 | |
| 4697 | for d in device: |
| 4698 | time.sleep( 1 ) |
| 4699 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4700 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4701 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4702 | if "Error" in response: |
| 4703 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4704 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4705 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4706 | except AssertionError: |
| 4707 | main.log.exception( "" ) |
| 4708 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4709 | except TypeError: |
| 4710 | main.log.exception( self.name + ": Object not as expected" ) |
| 4711 | return main.FALSE |
| 4712 | except pexpect.EOF: |
| 4713 | main.log.error( self.name + ": EOF exception found" ) |
| 4714 | main.log.error( self.name + ": " + self.handle.before ) |
| 4715 | main.cleanup() |
| 4716 | main.exit() |
| 4717 | except Exception: |
| 4718 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4719 | main.cleanup() |
| 4720 | main.exit() |
| 4721 | |
| 4722 | def removeHost( self, host ): |
| 4723 | ''' |
| 4724 | Description: |
| 4725 | Remove a host from ONOS by passing the id of the host(s) |
| 4726 | Parameters: |
| 4727 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
| 4728 | Returns: |
| 4729 | Returns main.FALSE if an exception is thrown or an error is present |
| 4730 | in the response. Otherwise, returns main.TRUE. |
| 4731 | NOTE: |
| 4732 | If a host cannot be removed, then this function will return main.FALSE |
| 4733 | ''' |
| 4734 | try: |
| 4735 | if type( host ) is str: |
| 4736 | host = list( host ) |
| 4737 | |
| 4738 | for h in host: |
| 4739 | time.sleep( 1 ) |
| 4740 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4741 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4742 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4743 | if "Error" in response: |
| 4744 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 4745 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4746 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4747 | except AssertionError: |
| 4748 | main.log.exception( "" ) |
| 4749 | return None |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4750 | except TypeError: |
| 4751 | main.log.exception( self.name + ": Object not as expected" ) |
| 4752 | return main.FALSE |
| 4753 | except pexpect.EOF: |
| 4754 | main.log.error( self.name + ": EOF exception found" ) |
| 4755 | main.log.error( self.name + ": " + self.handle.before ) |
| 4756 | main.cleanup() |
| 4757 | main.exit() |
| 4758 | except Exception: |
| 4759 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4760 | main.cleanup() |
| 4761 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4762 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4763 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4764 | ''' |
| 4765 | Description: |
| 4766 | Bring link down or up in the null-provider. |
| 4767 | params: |
| 4768 | begin - (string) One end of a device or switch. |
| 4769 | end - (string) the other end of the device or switch |
| 4770 | returns: |
| 4771 | main.TRUE if no exceptions were thrown and no Errors are |
| 4772 | present in the resoponse. Otherwise, returns main.FALSE |
| 4773 | ''' |
| 4774 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4775 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4776 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4777 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4778 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4779 | if "Error" in response or "Failure" in response: |
| 4780 | main.log.error( response ) |
| 4781 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4782 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4783 | except AssertionError: |
| 4784 | main.log.exception( "" ) |
| 4785 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4786 | except TypeError: |
| 4787 | main.log.exception( self.name + ": Object not as expected" ) |
| 4788 | return main.FALSE |
| 4789 | except pexpect.EOF: |
| 4790 | main.log.error( self.name + ": EOF exception found" ) |
| 4791 | main.log.error( self.name + ": " + self.handle.before ) |
| 4792 | main.cleanup() |
| 4793 | main.exit() |
| 4794 | except Exception: |
| 4795 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4796 | main.cleanup() |
| 4797 | main.exit() |
| 4798 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4799 | def portstate(self, dpid='of:0000000000000102', port='2', state='enable'): |
| 4800 | ''' |
| 4801 | Description: |
| 4802 | Changes the state of port in an OF switch by means of the |
| 4803 | PORTSTATUS OF messages. |
| 4804 | params: |
| 4805 | dpid - (string) Datapath ID of the device |
| 4806 | port - (string) target port in the device |
| 4807 | state - (string) target state (enable or disabled) |
| 4808 | returns: |
| 4809 | main.TRUE if no exceptions were thrown and no Errors are |
| 4810 | present in the resoponse. Otherwise, returns main.FALSE |
| 4811 | ''' |
| 4812 | try: |
| 4813 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
| 4814 | response = self.sendline( cmd, showResponse=True ) |
| 4815 | assert response is not None, "Error in sendline" |
| 4816 | assert "Command not found:" not in response, response |
| 4817 | if "Error" in response or "Failure" in response: |
| 4818 | main.log.error( response ) |
| 4819 | return main.FALSE |
| 4820 | return main.TRUE |
| 4821 | except AssertionError: |
| 4822 | main.log.exception( "" ) |
| 4823 | return None |
| 4824 | except TypeError: |
| 4825 | main.log.exception( self.name + ": Object not as expected" ) |
| 4826 | return main.FALSE |
| 4827 | except pexpect.EOF: |
| 4828 | main.log.error( self.name + ": EOF exception found" ) |
| 4829 | main.log.error( self.name + ": " + self.handle.before ) |
| 4830 | main.cleanup() |
| 4831 | main.exit() |
| 4832 | except Exception: |
| 4833 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4834 | main.cleanup() |
| 4835 | main.exit() |
| 4836 | |
| 4837 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 4838 | """ |
| 4839 | Set the logging level to lvl for a specific app |
| 4840 | returns main.TRUE on success |
| 4841 | returns main.FALSE if Error occurred |
| 4842 | if noExit is True, TestON will not exit, but clean up |
| 4843 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 4844 | Level defaults to INFO |
| 4845 | """ |
| 4846 | try: |
| 4847 | self.handle.sendline( "log:set %s %s" %( level, app ) ) |
| 4848 | self.handle.expect( "onos>" ) |
| 4849 | |
| 4850 | response = self.handle.before |
| 4851 | if re.search( "Error", response ): |
| 4852 | return main.FALSE |
| 4853 | return main.TRUE |
| 4854 | except pexpect.TIMEOUT: |
| 4855 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 4856 | main.cleanup() |
| 4857 | main.exit() |
| 4858 | except pexpect.EOF: |
| 4859 | main.log.error( self.name + ": EOF exception found" ) |
| 4860 | main.log.error( self.name + ": " + self.handle.before ) |
| 4861 | main.cleanup() |
| 4862 | main.exit() |
| 4863 | except Exception: |
| 4864 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4865 | main.cleanup() |
| 4866 | main.exit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4867 | |
| 4868 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 4869 | """ |
| 4870 | Return a dictionary which describes the latest network topology data as a |
| 4871 | graph. |
| 4872 | An example of the dictionary: |
| 4873 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 4874 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 4875 | Each vertex should at least have an 'edges' attribute which describes the |
| 4876 | adjacency information. The value of 'edges' attribute is also represented by |
| 4877 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 4878 | list of attributes. |
| 4879 | An example of the edges dictionary: |
| 4880 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 4881 | vertex3: { 'port': ..., 'weight': ... } } |
| 4882 | If includeHost == True, all hosts (and host-switch links) will be included |
| 4883 | in topology data. |
| 4884 | """ |
| 4885 | graphDict = {} |
| 4886 | try: |
| 4887 | links = self.links() |
| 4888 | links = json.loads( links ) |
| 4889 | devices = self.devices() |
| 4890 | devices = json.loads( devices ) |
| 4891 | idToDevice = {} |
| 4892 | for device in devices: |
| 4893 | idToDevice[ device[ 'id' ] ] = device |
| 4894 | if includeHost: |
| 4895 | hosts = self.hosts() |
| 4896 | # FIXME: support 'includeHost' argument |
| 4897 | for link in links: |
| 4898 | nodeA = link[ 'src' ][ 'device' ] |
| 4899 | nodeB = link[ 'dst' ][ 'device' ] |
| 4900 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
| 4901 | if not nodeA in graphDict.keys(): |
| 4902 | graphDict[ nodeA ] = { 'edges':{}, |
| 4903 | 'dpid':idToDevice[ nodeA ][ 'id' ][3:], |
| 4904 | 'type':idToDevice[ nodeA ][ 'type' ], |
| 4905 | 'available':idToDevice[ nodeA ][ 'available' ], |
| 4906 | 'role':idToDevice[ nodeA ][ 'role' ], |
| 4907 | 'mfr':idToDevice[ nodeA ][ 'mfr' ], |
| 4908 | 'hw':idToDevice[ nodeA ][ 'hw' ], |
| 4909 | 'sw':idToDevice[ nodeA ][ 'sw' ], |
| 4910 | 'serial':idToDevice[ nodeA ][ 'serial' ], |
| 4911 | 'chassisId':idToDevice[ nodeA ][ 'chassisId' ], |
| 4912 | 'annotations':idToDevice[ nodeA ][ 'annotations' ]} |
| 4913 | else: |
| 4914 | # Assert nodeB is not connected to any current links of nodeA |
| 4915 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 4916 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port':link[ 'src' ][ 'port' ], |
| 4917 | 'type':link[ 'type' ], |
| 4918 | 'state':link[ 'state' ] } |
| 4919 | return graphDict |
| 4920 | except ( TypeError, ValueError ): |
| 4921 | main.log.exception( self.name + ": Object not as expected" ) |
| 4922 | return None |
| 4923 | except KeyError: |
| 4924 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4925 | return None |
| 4926 | except AssertionError: |
| 4927 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4928 | return None |
| 4929 | except pexpect.EOF: |
| 4930 | main.log.error( self.name + ": EOF exception found" ) |
| 4931 | main.log.error( self.name + ": " + self.handle.before ) |
| 4932 | return None |
| 4933 | except Exception: |
| 4934 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4935 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 4936 | |
| 4937 | def getIntentPerfSummary( self ): |
| 4938 | ''' |
| 4939 | Send command to check intent-perf summary |
| 4940 | Returns: dictionary for intent-perf summary |
| 4941 | if something wrong, function will return None |
| 4942 | ''' |
| 4943 | cmd = "intent-perf -s" |
| 4944 | respDic = {} |
| 4945 | resp = self.sendline( cmd ) |
| 4946 | try: |
| 4947 | # Generate the dictionary to return |
| 4948 | for l in resp.split( "\n" ): |
| 4949 | # Delete any white space in line |
| 4950 | temp = re.sub( r'\s+', '', l ) |
| 4951 | temp = temp.split( ":" ) |
| 4952 | respDic[ temp[0] ] = temp[ 1 ] |
| 4953 | |
| 4954 | except (TypeError, ValueError): |
| 4955 | main.log.exception( self.name + ": Object not as expected" ) |
| 4956 | return None |
| 4957 | except KeyError: |
| 4958 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4959 | return None |
| 4960 | except AssertionError: |
| 4961 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4962 | return None |
| 4963 | except pexpect.EOF: |
| 4964 | main.log.error( self.name + ": EOF exception found" ) |
| 4965 | main.log.error( self.name + ": " + self.handle.before ) |
| 4966 | return None |
| 4967 | except Exception: |
| 4968 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4969 | return None |
| 4970 | return respDic |
| 4971 | |
| 4972 | |