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