andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 4 | This driver enters the onos> prompt to issue commands. |
| 5 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 6 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 7 | functions and document properly. |
| 8 | |
| 9 | If you are a contributor to the driver, please |
| 10 | list your email here for future contact: |
| 11 | |
| 12 | jhall@onlab.us |
| 13 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 14 | shreya@onlab.us |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 15 | |
| 16 | OCT 13 2014 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 17 | Modified 2016 by ON.Lab |
| 18 | |
| 19 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 20 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 21 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 22 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 23 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 24 | import pexpect |
| 25 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 26 | import json |
| 27 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 28 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 29 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 30 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 31 | from core.graph import Graph |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 32 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 33 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 34 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 35 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 36 | def __init__( self ): |
| 37 | """ |
| 38 | Initialize client |
| 39 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 40 | self.name = None |
| 41 | self.home = None |
| 42 | self.handle = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 43 | self.graph = Graph() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | super( CLI, self ).__init__() |
| 45 | |
| 46 | def connect( self, **connectargs ): |
| 47 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 48 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 49 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 50 | try: |
| 51 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 53 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 54 | for key in self.options: |
| 55 | if key == "home": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 56 | self.home = self.options[ 'home' ] |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 57 | break |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 58 | if self.home is None or self.home == "": |
Jon Hall | e94919c | 2015-03-23 11:42:57 -0700 | [diff] [blame] | 59 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 60 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 61 | for key in self.options: |
| 62 | if key == 'onosIp': |
| 63 | self.onosIp = self.options[ 'onosIp' ] |
| 64 | break |
| 65 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 66 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 67 | |
| 68 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 69 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 70 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 71 | else: |
| 72 | main.log.info( self.name + |
| 73 | ": Trying to connect to " + |
| 74 | self.ip_address ) |
| 75 | |
| 76 | except KeyError: |
| 77 | main.log.info( "Invalid host name," + |
| 78 | " connecting to local host instead" ) |
| 79 | self.ip_address = 'localhost' |
| 80 | except Exception as inst: |
| 81 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 82 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 83 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 84 | user_name=self.user_name, |
| 85 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 86 | port=self.port, |
| 87 | pwd=self.pwd, |
| 88 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 89 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 90 | self.handle.sendline( "cd " + self.home ) |
| 91 | self.handle.expect( "\$" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 92 | if self.handle: |
| 93 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | else: |
| 95 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 96 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 97 | except TypeError: |
| 98 | main.log.exception( self.name + ": Object not as expected" ) |
| 99 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 100 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 101 | main.log.error( self.name + ": EOF exception found" ) |
| 102 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 103 | main.cleanup() |
| 104 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 105 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 106 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 107 | main.cleanup() |
| 108 | main.exit() |
| 109 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 110 | def disconnect( self ): |
| 111 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 112 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 113 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 114 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 115 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 116 | if self.handle: |
| 117 | i = self.logout() |
| 118 | if i == main.TRUE: |
| 119 | self.handle.sendline( "" ) |
| 120 | self.handle.expect( "\$" ) |
| 121 | self.handle.sendline( "exit" ) |
| 122 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 123 | except TypeError: |
| 124 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 125 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 126 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 127 | main.log.error( self.name + ": EOF exception found" ) |
| 128 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 129 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 130 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 131 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 132 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 133 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | response = main.FALSE |
| 135 | return response |
| 136 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 137 | def logout( self ): |
| 138 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 139 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 140 | Returns main.TRUE if exited CLI and |
| 141 | main.FALSE on timeout (not guranteed you are disconnected) |
| 142 | None on TypeError |
| 143 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 144 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 145 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 146 | if self.handle: |
| 147 | self.handle.sendline( "" ) |
| 148 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ], |
| 149 | timeout=10 ) |
| 150 | if i == 0: # In ONOS CLI |
| 151 | self.handle.sendline( "logout" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 152 | j = self.handle.expect( [ "\$", |
| 153 | "Command not found:", |
| 154 | pexpect.TIMEOUT ] ) |
| 155 | if j == 0: # Successfully logged out |
| 156 | return main.TRUE |
| 157 | elif j == 1 or j == 2: |
| 158 | # ONOS didn't fully load, and logout command isn't working |
| 159 | # or the command timed out |
| 160 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 161 | try: |
| 162 | self.handle.expect( "\$" ) |
| 163 | except pexpect.TIMEOUT: |
| 164 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 165 | return main.TRUE |
| 166 | else: # some other output |
| 167 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 168 | repr( self.handle.before ) ) |
| 169 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 170 | elif i == 1: # not in CLI |
| 171 | return main.TRUE |
| 172 | elif i == 3: # Timeout |
| 173 | return main.FALSE |
| 174 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 175 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 176 | except TypeError: |
| 177 | main.log.exception( self.name + ": Object not as expected" ) |
| 178 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 179 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 181 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 182 | main.cleanup() |
| 183 | main.exit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 184 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 185 | main.log.error( self.name + |
| 186 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 187 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 188 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 189 | main.cleanup() |
| 190 | main.exit() |
| 191 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 192 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 193 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 194 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 196 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 197 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 198 | try: |
| 199 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 200 | main.log.error( "Must define cellname" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 201 | main.cleanup() |
| 202 | main.exit() |
| 203 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 204 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 205 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 206 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 207 | # and that this driver will have to change accordingly |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 208 | self.handle.expect(str(cellname)) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 209 | handleBefore = self.handle.before |
| 210 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 211 | # Get the rest of the handle |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 212 | self.handle.sendline("") |
| 213 | self.handle.expect("\$") |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 214 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 215 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 216 | main.log.info( "Cell call returned: " + handleBefore + |
| 217 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 218 | |
| 219 | return main.TRUE |
| 220 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 221 | except TypeError: |
| 222 | main.log.exception( self.name + ": Object not as expected" ) |
| 223 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 224 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 225 | main.log.error( self.name + ": eof exception found" ) |
| 226 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 227 | main.cleanup() |
| 228 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 229 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 230 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 231 | main.cleanup() |
| 232 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 233 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 234 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 235 | commandlineTimeout=10, onosStartTimeout=60 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 236 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 237 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 238 | by user would be used to set the current karaf shell idle timeout. |
| 239 | Note that when ever this property is modified the shell will exit and |
Hari Krishna | d7b9c20 | 2015-01-05 10:38:14 -0800 | [diff] [blame] | 240 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 241 | Below is an example to start a session with 60 seconds idle timeout |
| 242 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 243 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 244 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 245 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 246 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 247 | Note: karafTimeout is left as str so that this could be read |
| 248 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 249 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 250 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 251 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 252 | self.handle.sendline( "" ) |
| 253 | x = self.handle.expect( [ |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 254 | "\$", "onos>" ], commandlineTimeout) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 255 | |
| 256 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 257 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 258 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 259 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 260 | # Wait for onos start ( -w ) and enter onos cli |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 261 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 262 | i = self.handle.expect( [ |
| 263 | "onos>", |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 264 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 265 | |
| 266 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 267 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 268 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 269 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 270 | "config:property-set -p org.apache.karaf.shell\ |
| 271 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 272 | karafTimeout ) |
| 273 | self.handle.expect( "\$" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 274 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 275 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 276 | return main.TRUE |
| 277 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 278 | # If failed, send ctrl+c to process and try again |
| 279 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 280 | self.handle.send( "\x03" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 281 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 282 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 283 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 284 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 285 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 286 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 287 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 288 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 289 | "config:property-set -p org.apache.karaf.shell\ |
| 290 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 291 | karafTimeout ) |
| 292 | self.handle.expect( "\$" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 293 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 294 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 295 | return main.TRUE |
| 296 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 297 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 298 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 299 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 300 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 301 | except TypeError: |
| 302 | main.log.exception( self.name + ": Object not as expected" ) |
| 303 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 304 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 305 | main.log.error( self.name + ": EOF exception found" ) |
| 306 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 307 | main.cleanup() |
| 308 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 309 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 310 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 311 | main.cleanup() |
| 312 | main.exit() |
| 313 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 314 | def startCellCli( self, karafTimeout="", |
| 315 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 316 | """ |
| 317 | Start CLI on onos ecll handle. |
| 318 | |
| 319 | karafTimeout is an optional argument. karafTimeout value passed |
| 320 | by user would be used to set the current karaf shell idle timeout. |
| 321 | Note that when ever this property is modified the shell will exit and |
| 322 | the subsequent login would reflect new idle timeout. |
| 323 | Below is an example to start a session with 60 seconds idle timeout |
| 324 | ( input value is in milliseconds ): |
| 325 | |
| 326 | tValue = "60000" |
| 327 | |
| 328 | Note: karafTimeout is left as str so that this could be read |
| 329 | and passed to startOnosCli from PARAMS file as str. |
| 330 | """ |
| 331 | |
| 332 | try: |
| 333 | self.handle.sendline( "" ) |
| 334 | x = self.handle.expect( [ |
| 335 | "\$", "onos>" ], commandlineTimeout) |
| 336 | |
| 337 | if x == 1: |
| 338 | main.log.info( "ONOS cli is already running" ) |
| 339 | return main.TRUE |
| 340 | |
| 341 | # Wait for onos start ( -w ) and enter onos cli |
| 342 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 343 | i = self.handle.expect( [ |
| 344 | "onos>", |
| 345 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 346 | |
| 347 | if i == 0: |
| 348 | main.log.info( self.name + " CLI Started successfully" ) |
| 349 | if karafTimeout: |
| 350 | self.handle.sendline( |
| 351 | "config:property-set -p org.apache.karaf.shell\ |
| 352 | sshIdleTimeout " + |
| 353 | karafTimeout ) |
| 354 | self.handle.expect( "\$" ) |
| 355 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 356 | self.handle.expect( "onos>" ) |
| 357 | return main.TRUE |
| 358 | else: |
| 359 | # If failed, send ctrl+c to process and try again |
| 360 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 361 | self.handle.send( "\x03" ) |
| 362 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 363 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 364 | timeout=30 ) |
| 365 | if i == 0: |
| 366 | main.log.info( self.name + " CLI Started " + |
| 367 | "successfully after retry attempt" ) |
| 368 | if karafTimeout: |
| 369 | self.handle.sendline( |
| 370 | "config:property-set -p org.apache.karaf.shell\ |
| 371 | sshIdleTimeout " + |
| 372 | karafTimeout ) |
| 373 | self.handle.expect( "\$" ) |
| 374 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 375 | self.handle.expect( "onos>" ) |
| 376 | return main.TRUE |
| 377 | else: |
| 378 | main.log.error( "Connection to CLI " + |
| 379 | self.name + " timeout" ) |
| 380 | return main.FALSE |
| 381 | |
| 382 | except TypeError: |
| 383 | main.log.exception( self.name + ": Object not as expected" ) |
| 384 | return None |
| 385 | except pexpect.EOF: |
| 386 | main.log.error( self.name + ": EOF exception found" ) |
| 387 | main.log.error( self.name + ": " + self.handle.before ) |
| 388 | main.cleanup() |
| 389 | main.exit() |
| 390 | except Exception: |
| 391 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 392 | main.cleanup() |
| 393 | main.exit() |
| 394 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 395 | def log( self, cmdStr, level="",noExit=False): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 396 | """ |
| 397 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 398 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 399 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 400 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 401 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 402 | Level defaults to INFO |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 403 | """ |
| 404 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 405 | lvlStr = "" |
| 406 | if level: |
| 407 | lvlStr = "--level=" + level |
| 408 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 409 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 410 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 411 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 412 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 413 | response = self.handle.before |
| 414 | if re.search( "Error", response ): |
| 415 | return main.FALSE |
| 416 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 417 | except pexpect.TIMEOUT: |
| 418 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 419 | if noExit: |
| 420 | main.cleanup() |
| 421 | return None |
| 422 | else: |
| 423 | main.cleanup() |
| 424 | main.exit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 425 | except pexpect.EOF: |
| 426 | main.log.error( self.name + ": EOF exception found" ) |
| 427 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 428 | if noExit: |
| 429 | main.cleanup() |
| 430 | return None |
| 431 | else: |
| 432 | main.cleanup() |
| 433 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 434 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 435 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 436 | if noExit: |
| 437 | main.cleanup() |
| 438 | return None |
| 439 | else: |
| 440 | main.cleanup() |
| 441 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 442 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 443 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 444 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 445 | Send a completely user specified string to |
| 446 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 447 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 448 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 449 | if noExit is True, TestON will not exit, and return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 450 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 451 | Warning: There are no sanity checking to commands |
| 452 | sent using this method. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 453 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 454 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 455 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 456 | # Try to reconnect if disconnected from cli |
| 457 | self.handle.sendline( "" ) |
| 458 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ] ) |
| 459 | if i == 1: |
| 460 | main.log.error( self.name + ": onos cli session closed. ") |
| 461 | if self.onosIp: |
| 462 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 463 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 464 | if reconnectResult: |
| 465 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 466 | else: |
| 467 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 468 | if noExit: |
| 469 | return None |
| 470 | else: |
| 471 | main.cleanup() |
| 472 | main.exit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 473 | else: |
| 474 | main.cleanup() |
| 475 | main.exit() |
| 476 | if i == 2: |
| 477 | self.handle.sendline( "" ) |
| 478 | self.handle.expect( "onos>" ) |
| 479 | |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 480 | if debug: |
| 481 | # NOTE: This adds and average of .4 seconds per call |
| 482 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 483 | self.log( logStr,noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 484 | self.handle.sendline( cmdStr ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 485 | i = self.handle.expect( ["onos>", "\$"], timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 486 | response = self.handle.before |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 487 | # TODO: do something with i |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 488 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
| 489 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 490 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 491 | main.log.debug( self.name + ": Raw output" ) |
| 492 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 493 | |
| 494 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 495 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 496 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 497 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 498 | main.log.debug( self.name + ": ansiEscape output" ) |
| 499 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 500 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 501 | # Remove extra return chars that get added |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 502 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 503 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 504 | main.log.debug( self.name + ": Removed extra returns " + |
| 505 | "from output" ) |
| 506 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 507 | |
| 508 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 509 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 510 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 511 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 512 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 513 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 514 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 515 | output = response.split( cmdStr.strip(), 1 ) |
| 516 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 517 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 518 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 519 | main.log.debug( self.name + ": " + repr( r ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 520 | output = output[1].strip() |
| 521 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 522 | main.log.info( "Response from ONOS: {}".format( output ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 523 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 524 | except pexpect.TIMEOUT: |
| 525 | main.log.error( self.name + ":ONOS timeout" ) |
| 526 | if debug: |
| 527 | main.log.debug( self.handle.before ) |
| 528 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 529 | except IndexError: |
| 530 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 531 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 532 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 533 | except TypeError: |
| 534 | main.log.exception( self.name + ": Object not as expected" ) |
| 535 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 536 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 537 | main.log.error( self.name + ": EOF exception found" ) |
| 538 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 539 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 540 | return None |
| 541 | else: |
| 542 | main.cleanup() |
| 543 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 544 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 545 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 546 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 547 | return None |
| 548 | else: |
| 549 | main.cleanup() |
| 550 | main.exit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 551 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 552 | # IMPORTANT NOTE: |
| 553 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 554 | # the cli command changing 'a:b' with 'aB'. |
| 555 | # Ex ) onos:topology > onosTopology |
| 556 | # onos:links > onosLinks |
| 557 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 558 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 559 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 560 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 561 | Adds a new cluster node by ID and address information. |
| 562 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 563 | * nodeId |
| 564 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 565 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 566 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 567 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 568 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 569 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 570 | str( ONOSIp ) + " " + str( tcpPort ) |
| 571 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 572 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 573 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 574 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 575 | main.log.error( "Error in adding node" ) |
| 576 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 577 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 578 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 579 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 580 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 581 | except AssertionError: |
| 582 | main.log.exception( "" ) |
| 583 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 584 | except TypeError: |
| 585 | main.log.exception( self.name + ": Object not as expected" ) |
| 586 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 587 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 588 | main.log.error( self.name + ": EOF exception found" ) |
| 589 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 590 | main.cleanup() |
| 591 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 592 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 593 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 594 | main.cleanup() |
| 595 | main.exit() |
| 596 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 597 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 598 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 599 | Removes a cluster by ID |
| 600 | Issues command: 'remove-node [<node-id>]' |
| 601 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 602 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 603 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 604 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 605 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 606 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 607 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 608 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 609 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 610 | if re.search( "Error", handle ): |
| 611 | main.log.error( "Error in removing node" ) |
| 612 | main.log.error( handle ) |
| 613 | return main.FALSE |
| 614 | else: |
| 615 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 616 | except AssertionError: |
| 617 | main.log.exception( "" ) |
| 618 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 619 | except TypeError: |
| 620 | main.log.exception( self.name + ": Object not as expected" ) |
| 621 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 622 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 623 | main.log.error( self.name + ": EOF exception found" ) |
| 624 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 625 | main.cleanup() |
| 626 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 627 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 628 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 629 | main.cleanup() |
| 630 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 631 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 632 | def nodes( self, jsonFormat=True): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 633 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 634 | List the nodes currently visible |
| 635 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 636 | Optional argument: |
| 637 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 638 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 639 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 640 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 641 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 642 | cmdStr += " -j" |
| 643 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 644 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 645 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 646 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 647 | except AssertionError: |
| 648 | main.log.exception( "" ) |
| 649 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 650 | except TypeError: |
| 651 | main.log.exception( self.name + ": Object not as expected" ) |
| 652 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 653 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 654 | main.log.error( self.name + ": EOF exception found" ) |
| 655 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 656 | main.cleanup() |
| 657 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 658 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 659 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 660 | main.cleanup() |
| 661 | main.exit() |
| 662 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 663 | def topology( self ): |
| 664 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 665 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 666 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 667 | Return: |
| 668 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 669 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 670 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 671 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 672 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 673 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 674 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 675 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 676 | except AssertionError: |
| 677 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 678 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 679 | except TypeError: |
| 680 | main.log.exception( self.name + ": Object not as expected" ) |
| 681 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 682 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 683 | main.log.error( self.name + ": EOF exception found" ) |
| 684 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 685 | main.cleanup() |
| 686 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 687 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 688 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 689 | main.cleanup() |
| 690 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 691 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 692 | def deviceRemove( self, deviceId ): |
| 693 | """ |
| 694 | Removes particular device from storage |
| 695 | |
| 696 | TODO: refactor this function |
| 697 | """ |
| 698 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 699 | cmdStr = "device-remove " + str( deviceId ) |
| 700 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 701 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 702 | if re.search( "Error", handle ): |
| 703 | main.log.error( "Error in removing device" ) |
| 704 | main.log.error( handle ) |
| 705 | return main.FALSE |
| 706 | else: |
| 707 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 708 | except AssertionError: |
| 709 | main.log.exception( "" ) |
| 710 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 711 | except TypeError: |
| 712 | main.log.exception( self.name + ": Object not as expected" ) |
| 713 | return None |
| 714 | except pexpect.EOF: |
| 715 | main.log.error( self.name + ": EOF exception found" ) |
| 716 | main.log.error( self.name + ": " + self.handle.before ) |
| 717 | main.cleanup() |
| 718 | main.exit() |
| 719 | except Exception: |
| 720 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 721 | main.cleanup() |
| 722 | main.exit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 723 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 724 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 725 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 726 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 727 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 728 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 729 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 730 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 731 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 732 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 733 | cmdStr += " -j" |
| 734 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 735 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 736 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 737 | except AssertionError: |
| 738 | main.log.exception( "" ) |
| 739 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 740 | except TypeError: |
| 741 | main.log.exception( self.name + ": Object not as expected" ) |
| 742 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 743 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 744 | main.log.error( self.name + ": EOF exception found" ) |
| 745 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 746 | main.cleanup() |
| 747 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 748 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 749 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 750 | main.cleanup() |
| 751 | main.exit() |
| 752 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 753 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 754 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 755 | This balances the devices across all controllers |
| 756 | by issuing command: 'onos> onos:balance-masters' |
| 757 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 758 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 759 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 760 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 761 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 762 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 763 | if re.search( "Error", handle ): |
| 764 | main.log.error( "Error in balancing masters" ) |
| 765 | main.log.error( handle ) |
| 766 | return main.FALSE |
| 767 | else: |
| 768 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 769 | except AssertionError: |
| 770 | main.log.exception( "" ) |
| 771 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 772 | except TypeError: |
| 773 | main.log.exception( self.name + ": Object not as expected" ) |
| 774 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 775 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 776 | main.log.error( self.name + ": EOF exception found" ) |
| 777 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 778 | main.cleanup() |
| 779 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 780 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 781 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 782 | main.cleanup() |
| 783 | main.exit() |
| 784 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 785 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 786 | """ |
| 787 | Returns the output of the masters command. |
| 788 | Optional argument: |
| 789 | * jsonFormat - boolean indicating if you want output in json |
| 790 | """ |
| 791 | try: |
| 792 | cmdStr = "onos:masters" |
| 793 | if jsonFormat: |
| 794 | cmdStr += " -j" |
| 795 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 796 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 797 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 798 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 799 | except AssertionError: |
| 800 | main.log.exception( "" ) |
| 801 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 802 | except TypeError: |
| 803 | main.log.exception( self.name + ": Object not as expected" ) |
| 804 | return None |
| 805 | except pexpect.EOF: |
| 806 | main.log.error( self.name + ": EOF exception found" ) |
| 807 | main.log.error( self.name + ": " + self.handle.before ) |
| 808 | main.cleanup() |
| 809 | main.exit() |
| 810 | except Exception: |
| 811 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 812 | main.cleanup() |
| 813 | main.exit() |
| 814 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 815 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 816 | """ |
| 817 | Uses the master command to check that the devices' leadership |
| 818 | is evenly divided |
| 819 | |
| 820 | Dependencies: checkMasters() and summary() |
| 821 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 822 | Returns main.TRUE if the devices are balanced |
| 823 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 824 | Exits on Exception |
| 825 | Returns None on TypeError |
| 826 | """ |
| 827 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 828 | summaryOutput = self.summary() |
| 829 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 830 | except ( TypeError, ValueError ): |
| 831 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 832 | return None |
| 833 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 834 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 835 | mastersOutput = self.checkMasters() |
| 836 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 837 | first = masters[ 0 ][ "size" ] |
| 838 | for master in masters: |
| 839 | totalOwnedDevices += master[ "size" ] |
| 840 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 841 | main.log.error( "Mastership not balanced" ) |
| 842 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 843 | return main.FALSE |
| 844 | main.log.info( "Mastership balanced between " \ |
| 845 | + str( len(masters) ) + " masters" ) |
| 846 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 847 | except ( TypeError, ValueError ): |
| 848 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 849 | return None |
| 850 | except pexpect.EOF: |
| 851 | main.log.error( self.name + ": EOF exception found" ) |
| 852 | main.log.error( self.name + ": " + self.handle.before ) |
| 853 | main.cleanup() |
| 854 | main.exit() |
| 855 | except Exception: |
| 856 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 857 | main.cleanup() |
| 858 | main.exit() |
| 859 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 860 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 861 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 862 | Lists all core links |
| 863 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 864 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 865 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 866 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 867 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 868 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 869 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 870 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 871 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 872 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 873 | except AssertionError: |
| 874 | main.log.exception( "" ) |
| 875 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 876 | except TypeError: |
| 877 | main.log.exception( self.name + ": Object not as expected" ) |
| 878 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 879 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 880 | main.log.error( self.name + ": EOF exception found" ) |
| 881 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 882 | main.cleanup() |
| 883 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 884 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 885 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 886 | main.cleanup() |
| 887 | main.exit() |
| 888 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 889 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 890 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 891 | Lists all ports |
| 892 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 893 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 894 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 895 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 896 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 897 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 898 | cmdStr += " -j" |
| 899 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 900 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 901 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 902 | except AssertionError: |
| 903 | main.log.exception( "" ) |
| 904 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 905 | except TypeError: |
| 906 | main.log.exception( self.name + ": Object not as expected" ) |
| 907 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 908 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 909 | main.log.error( self.name + ": EOF exception found" ) |
| 910 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 911 | main.cleanup() |
| 912 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 913 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 914 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 915 | main.cleanup() |
| 916 | main.exit() |
| 917 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 918 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 919 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 920 | Lists all devices and the controllers with roles assigned to them |
| 921 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 922 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 923 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 924 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 925 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 926 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 927 | cmdStr += " -j" |
| 928 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 929 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 930 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 931 | except AssertionError: |
| 932 | main.log.exception( "" ) |
| 933 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 934 | except TypeError: |
| 935 | main.log.exception( self.name + ": Object not as expected" ) |
| 936 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 937 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 938 | main.log.error( self.name + ": EOF exception found" ) |
| 939 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 940 | main.cleanup() |
| 941 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 942 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 943 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 944 | main.cleanup() |
| 945 | main.exit() |
| 946 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 947 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 948 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 949 | Given the a string containing the json representation of the "roles" |
| 950 | cli command and a partial or whole device id, returns a json object |
| 951 | containing the roles output for the first device whose id contains |
| 952 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 953 | |
| 954 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 955 | A dict of the role assignments for the given device or |
| 956 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 957 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 958 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 959 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 960 | return None |
| 961 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 962 | rawRoles = self.roles() |
| 963 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 964 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 965 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 966 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 967 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 968 | return device |
| 969 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 970 | except ( TypeError, ValueError ): |
| 971 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 972 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 973 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 974 | main.log.error( self.name + ": EOF exception found" ) |
| 975 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 976 | main.cleanup() |
| 977 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 978 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 979 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 980 | main.cleanup() |
| 981 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 982 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 983 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 984 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 985 | Iterates through each device and checks if there is a master assigned |
| 986 | Returns: main.TRUE if each device has a master |
| 987 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 988 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 989 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 990 | rawRoles = self.roles() |
| 991 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 992 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 993 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 994 | # print device |
| 995 | if device[ 'master' ] == "none": |
| 996 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 997 | return main.FALSE |
| 998 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 999 | except ( TypeError, ValueError ): |
| 1000 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1001 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1002 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1003 | main.log.error( self.name + ": EOF exception found" ) |
| 1004 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1005 | main.cleanup() |
| 1006 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1007 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1008 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1009 | main.cleanup() |
| 1010 | main.exit() |
| 1011 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1012 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1013 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1014 | Returns string of paths, and the cost. |
| 1015 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1016 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1017 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1018 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1019 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1020 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1021 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1022 | main.log.error( "Error in getting paths" ) |
| 1023 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1024 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1025 | path = handle.split( ";" )[ 0 ] |
| 1026 | cost = handle.split( ";" )[ 1 ] |
| 1027 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1028 | except AssertionError: |
| 1029 | main.log.exception( "" ) |
| 1030 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1031 | except TypeError: |
| 1032 | main.log.exception( self.name + ": Object not as expected" ) |
| 1033 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1034 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1035 | main.log.error( self.name + ": EOF exception found" ) |
| 1036 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1037 | main.cleanup() |
| 1038 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1039 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1040 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1041 | main.cleanup() |
| 1042 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1043 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1044 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1045 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1046 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1047 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1048 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1049 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1050 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1051 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1052 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1053 | cmdStr += " -j" |
| 1054 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1055 | if handle: |
| 1056 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1057 | # TODO: Maybe make this less hardcoded |
| 1058 | # ConsistentMap Exceptions |
| 1059 | assert "org.onosproject.store.service" not in handle |
| 1060 | # Node not leader |
| 1061 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1062 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1063 | except AssertionError: |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1064 | main.log.exception( "Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1065 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1066 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1067 | except TypeError: |
| 1068 | main.log.exception( self.name + ": Object not as expected" ) |
| 1069 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1070 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1071 | main.log.error( self.name + ": EOF exception found" ) |
| 1072 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1073 | main.cleanup() |
| 1074 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1075 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1076 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1077 | main.cleanup() |
| 1078 | main.exit() |
| 1079 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1080 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1081 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1082 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1083 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1084 | Note: mac must be a colon separated mac address, but could be a |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1085 | partial mac address |
| 1086 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1087 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1088 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1089 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1090 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1091 | return None |
| 1092 | else: |
| 1093 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1094 | rawHosts = self.hosts() |
| 1095 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1096 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1097 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1098 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1099 | if not host: |
| 1100 | pass |
| 1101 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1102 | return host |
| 1103 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1104 | except ( TypeError, ValueError ): |
| 1105 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1106 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1107 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1108 | main.log.error( self.name + ": EOF exception found" ) |
| 1109 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1110 | main.cleanup() |
| 1111 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1112 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1113 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1114 | main.cleanup() |
| 1115 | main.exit() |
| 1116 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1117 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1118 | """ |
| 1119 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1120 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1121 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1122 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1123 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1124 | IMPORTANT: |
| 1125 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1126 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1127 | Furthermore, it assumes that value of VLAN is '-1' |
| 1128 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1129 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1130 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1131 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1132 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1133 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1134 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1135 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1136 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1137 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1138 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1139 | i = iter( str( hostHex ) ) |
| 1140 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1141 | hostHex = hostHex + "/-1" |
| 1142 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1143 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1144 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1145 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1146 | except TypeError: |
| 1147 | main.log.exception( self.name + ": Object not as expected" ) |
| 1148 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1149 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1150 | main.log.error( self.name + ": EOF exception found" ) |
| 1151 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1152 | main.cleanup() |
| 1153 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1154 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1155 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1156 | main.cleanup() |
| 1157 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1158 | |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1159 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1160 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1161 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1162 | * hostIdOne: ONOS host id for host1 |
| 1163 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1164 | Optional: |
| 1165 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1166 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1167 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1168 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1169 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1170 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1171 | Returns: |
| 1172 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1173 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1174 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1175 | cmdStr = "add-host-intent " |
| 1176 | if vlanId: |
| 1177 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1178 | if setVlan: |
| 1179 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1180 | if encap: |
| 1181 | cmdStr += "--encapsulation " + str( encap ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1182 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1183 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1184 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1185 | if re.search( "Error", handle ): |
| 1186 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1187 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1188 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1189 | else: |
| 1190 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1191 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
| 1192 | match = re.search('id=0x([\da-f]+),', handle) |
| 1193 | if match: |
| 1194 | return match.group()[3:-1] |
| 1195 | else: |
| 1196 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1197 | main.log.debug( "Response from ONOS was: " + |
| 1198 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1199 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1200 | except AssertionError: |
| 1201 | main.log.exception( "" ) |
| 1202 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1203 | except TypeError: |
| 1204 | main.log.exception( self.name + ": Object not as expected" ) |
| 1205 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1206 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1207 | main.log.error( self.name + ": EOF exception found" ) |
| 1208 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1209 | main.cleanup() |
| 1210 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1211 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1212 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1213 | main.cleanup() |
| 1214 | main.exit() |
| 1215 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1216 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1217 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1218 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1219 | * ingressDevice: device id of ingress device |
| 1220 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1221 | Optional: |
| 1222 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1223 | Description: |
| 1224 | Adds an optical intent by specifying an ingress and egress device |
| 1225 | Returns: |
| 1226 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1227 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1228 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1229 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1230 | " " + str( egressDevice ) |
| 1231 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1232 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1233 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1234 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1235 | main.log.error( "Error in adding Optical intent" ) |
| 1236 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1237 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1238 | main.log.info( "Optical intent installed between " + |
| 1239 | str( ingressDevice ) + " and " + |
| 1240 | str( egressDevice ) ) |
| 1241 | match = re.search('id=0x([\da-f]+),', handle) |
| 1242 | if match: |
| 1243 | return match.group()[3:-1] |
| 1244 | else: |
| 1245 | main.log.error( "Error, intent ID not found" ) |
| 1246 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1247 | except AssertionError: |
| 1248 | main.log.exception( "" ) |
| 1249 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1250 | except TypeError: |
| 1251 | main.log.exception( self.name + ": Object not as expected" ) |
| 1252 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1253 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1254 | main.log.error( self.name + ": EOF exception found" ) |
| 1255 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1256 | main.cleanup() |
| 1257 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1258 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1259 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1260 | main.cleanup() |
| 1261 | main.exit() |
| 1262 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1263 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1264 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1265 | ingressDevice, |
| 1266 | egressDevice, |
| 1267 | portIngress="", |
| 1268 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1269 | ethType="", |
| 1270 | ethSrc="", |
| 1271 | ethDst="", |
| 1272 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1273 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1274 | ipProto="", |
| 1275 | ipSrc="", |
| 1276 | ipDst="", |
| 1277 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1278 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1279 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1280 | setVlan="", |
| 1281 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1282 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1283 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1284 | * ingressDevice: device id of ingress device |
| 1285 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1286 | Optional: |
| 1287 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1288 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1289 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1290 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1291 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1292 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1293 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1294 | * ipSrc: specify ip source address |
| 1295 | * ipDst: specify ip destination address |
| 1296 | * tcpSrc: specify tcp source port |
| 1297 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1298 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1299 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1300 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1301 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1302 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1303 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1304 | Returns: |
| 1305 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1306 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1307 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1308 | options developers provide for point-to-point |
| 1309 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1310 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1311 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1312 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1313 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1314 | if ethType: |
| 1315 | cmd += " --ethType " + str( ethType ) |
| 1316 | if ethSrc: |
| 1317 | cmd += " --ethSrc " + str( ethSrc ) |
| 1318 | if ethDst: |
| 1319 | cmd += " --ethDst " + str( ethDst ) |
| 1320 | if bandwidth: |
| 1321 | cmd += " --bandwidth " + str( bandwidth ) |
| 1322 | if lambdaAlloc: |
| 1323 | cmd += " --lambda " |
| 1324 | if ipProto: |
| 1325 | cmd += " --ipProto " + str( ipProto ) |
| 1326 | if ipSrc: |
| 1327 | cmd += " --ipSrc " + str( ipSrc ) |
| 1328 | if ipDst: |
| 1329 | cmd += " --ipDst " + str( ipDst ) |
| 1330 | if tcpSrc: |
| 1331 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1332 | if tcpDst: |
| 1333 | cmd += " --tcpDst " + str( tcpDst ) |
| 1334 | if vlanId: |
| 1335 | cmd += " -v " + str( vlanId ) |
| 1336 | if setVlan: |
| 1337 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1338 | if encap: |
| 1339 | cmd += " --encapsulation " + str( encap ) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1340 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1341 | # Check whether the user appended the port |
| 1342 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1343 | if "/" in ingressDevice: |
| 1344 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1345 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1346 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1347 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1348 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1349 | # Would it make sense to throw an exception and exit |
| 1350 | # the test? |
| 1351 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1352 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1353 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1354 | str( ingressDevice ) + "/" +\ |
| 1355 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1356 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1357 | if "/" in egressDevice: |
| 1358 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1359 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1360 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1361 | main.log.error( "You must specify the egress port" ) |
| 1362 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1363 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1364 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1365 | str( egressDevice ) + "/" +\ |
| 1366 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1367 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1368 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1369 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1370 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1371 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1372 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1373 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1374 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1375 | # TODO: print out all the options in this message? |
| 1376 | main.log.info( "Point-to-point intent installed between " + |
| 1377 | str( ingressDevice ) + " and " + |
| 1378 | str( egressDevice ) ) |
| 1379 | match = re.search('id=0x([\da-f]+),', handle) |
| 1380 | if match: |
| 1381 | return match.group()[3:-1] |
| 1382 | else: |
| 1383 | main.log.error( "Error, intent ID not found" ) |
| 1384 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1385 | except AssertionError: |
| 1386 | main.log.exception( "" ) |
| 1387 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1388 | except TypeError: |
| 1389 | main.log.exception( self.name + ": Object not as expected" ) |
| 1390 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1391 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1392 | main.log.error( self.name + ": EOF exception found" ) |
| 1393 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1394 | main.cleanup() |
| 1395 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1396 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1397 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1398 | main.cleanup() |
| 1399 | main.exit() |
| 1400 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1401 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1402 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1403 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1404 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1405 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1406 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1407 | ethType="", |
| 1408 | ethSrc="", |
| 1409 | ethDst="", |
| 1410 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1411 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1412 | ipProto="", |
| 1413 | ipSrc="", |
| 1414 | ipDst="", |
| 1415 | tcpSrc="", |
| 1416 | tcpDst="", |
| 1417 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1418 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1419 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1420 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1421 | partial=False, |
| 1422 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1423 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1424 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1425 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1426 | is same. That is, all ingress devices include port numbers |
| 1427 | with a "/" or all ingress devices could specify device |
| 1428 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1429 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1430 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1431 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1432 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1433 | Optional: |
| 1434 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1435 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1436 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1437 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1438 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1439 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1440 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1441 | * ipSrc: specify ip source address |
| 1442 | * ipDst: specify ip destination address |
| 1443 | * tcpSrc: specify tcp source port |
| 1444 | * tcpDst: specify tcp destination port |
| 1445 | * setEthSrc: action to Rewrite Source MAC Address |
| 1446 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1447 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1448 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1449 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1450 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1451 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1452 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1453 | Returns: |
| 1454 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1455 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1456 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1457 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1458 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1459 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1460 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1461 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1462 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1463 | if ethType: |
| 1464 | cmd += " --ethType " + str( ethType ) |
| 1465 | if ethSrc: |
| 1466 | cmd += " --ethSrc " + str( ethSrc ) |
| 1467 | if ethDst: |
| 1468 | cmd += " --ethDst " + str( ethDst ) |
| 1469 | if bandwidth: |
| 1470 | cmd += " --bandwidth " + str( bandwidth ) |
| 1471 | if lambdaAlloc: |
| 1472 | cmd += " --lambda " |
| 1473 | if ipProto: |
| 1474 | cmd += " --ipProto " + str( ipProto ) |
| 1475 | if ipSrc: |
| 1476 | cmd += " --ipSrc " + str( ipSrc ) |
| 1477 | if ipDst: |
| 1478 | cmd += " --ipDst " + str( ipDst ) |
| 1479 | if tcpSrc: |
| 1480 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1481 | if tcpDst: |
| 1482 | cmd += " --tcpDst " + str( tcpDst ) |
| 1483 | if setEthSrc: |
| 1484 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1485 | if setEthDst: |
| 1486 | cmd += " --setEthDst " + str( setEthDst ) |
| 1487 | if vlanId: |
| 1488 | cmd += " -v " + str( vlanId ) |
| 1489 | if setVlan: |
| 1490 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1491 | if partial: |
| 1492 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1493 | if encap: |
| 1494 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1495 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1496 | # Check whether the user appended the port |
| 1497 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1498 | |
| 1499 | if portIngressList is None: |
| 1500 | for ingressDevice in ingressDeviceList: |
| 1501 | if "/" in ingressDevice: |
| 1502 | cmd += " " + str( ingressDevice ) |
| 1503 | else: |
| 1504 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1505 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1506 | # TODO: perhaps more meaningful return |
| 1507 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1508 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1509 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1510 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1511 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1512 | cmd += " " + \ |
| 1513 | str( ingressDevice ) + "/" +\ |
| 1514 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1515 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1516 | main.log.error( "Device list and port list does not " + |
| 1517 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1518 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1519 | if "/" in egressDevice: |
| 1520 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1521 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1522 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1523 | main.log.error( "You must specify " + |
| 1524 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1525 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1526 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1527 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1528 | str( egressDevice ) + "/" +\ |
| 1529 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1530 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1531 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1532 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1533 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1534 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1535 | "intent" ) |
| 1536 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1537 | else: |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1538 | match = re.search('id=0x([\da-f]+),', handle) |
| 1539 | if match: |
| 1540 | return match.group()[3:-1] |
| 1541 | else: |
| 1542 | main.log.error( "Error, intent ID not found" ) |
| 1543 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1544 | except AssertionError: |
| 1545 | main.log.exception( "" ) |
| 1546 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1547 | except TypeError: |
| 1548 | main.log.exception( self.name + ": Object not as expected" ) |
| 1549 | return None |
| 1550 | except pexpect.EOF: |
| 1551 | main.log.error( self.name + ": EOF exception found" ) |
| 1552 | main.log.error( self.name + ": " + self.handle.before ) |
| 1553 | main.cleanup() |
| 1554 | main.exit() |
| 1555 | except Exception: |
| 1556 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1557 | main.cleanup() |
| 1558 | main.exit() |
| 1559 | |
| 1560 | def addSinglepointToMultipointIntent( |
| 1561 | self, |
| 1562 | ingressDevice, |
| 1563 | egressDeviceList, |
| 1564 | portIngress="", |
| 1565 | portEgressList=None, |
| 1566 | ethType="", |
| 1567 | ethSrc="", |
| 1568 | ethDst="", |
| 1569 | bandwidth="", |
| 1570 | lambdaAlloc=False, |
| 1571 | ipProto="", |
| 1572 | ipSrc="", |
| 1573 | ipDst="", |
| 1574 | tcpSrc="", |
| 1575 | tcpDst="", |
| 1576 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1577 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1578 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1579 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1580 | partial=False, |
| 1581 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1582 | """ |
| 1583 | Note: |
| 1584 | This function assumes the format of all egress devices |
| 1585 | is same. That is, all egress devices include port numbers |
| 1586 | with a "/" or all egress devices could specify device |
| 1587 | ids and port numbers seperately. |
| 1588 | Required: |
| 1589 | * EgressDeviceList: List of device ids of egress device |
| 1590 | ( Atleast 2 eress devices required in the list ) |
| 1591 | * ingressDevice: device id of ingress device |
| 1592 | Optional: |
| 1593 | * ethType: specify ethType |
| 1594 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1595 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1596 | * bandwidth: specify bandwidth capacity of link |
| 1597 | * lambdaAlloc: if True, intent will allocate lambda |
| 1598 | for the specified intent |
| 1599 | * ipProto: specify ip protocol |
| 1600 | * ipSrc: specify ip source address |
| 1601 | * ipDst: specify ip destination address |
| 1602 | * tcpSrc: specify tcp source port |
| 1603 | * tcpDst: specify tcp destination port |
| 1604 | * setEthSrc: action to Rewrite Source MAC Address |
| 1605 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1606 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1607 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1608 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1609 | Description: |
| 1610 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1611 | specifying device id's and optional fields |
| 1612 | Returns: |
| 1613 | A string of the intent id or None on error |
| 1614 | |
| 1615 | NOTE: This function may change depending on the |
| 1616 | options developers provide for singlepoint-to-multipoint |
| 1617 | intent via cli |
| 1618 | """ |
| 1619 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1620 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1621 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1622 | if ethType: |
| 1623 | cmd += " --ethType " + str( ethType ) |
| 1624 | if ethSrc: |
| 1625 | cmd += " --ethSrc " + str( ethSrc ) |
| 1626 | if ethDst: |
| 1627 | cmd += " --ethDst " + str( ethDst ) |
| 1628 | if bandwidth: |
| 1629 | cmd += " --bandwidth " + str( bandwidth ) |
| 1630 | if lambdaAlloc: |
| 1631 | cmd += " --lambda " |
| 1632 | if ipProto: |
| 1633 | cmd += " --ipProto " + str( ipProto ) |
| 1634 | if ipSrc: |
| 1635 | cmd += " --ipSrc " + str( ipSrc ) |
| 1636 | if ipDst: |
| 1637 | cmd += " --ipDst " + str( ipDst ) |
| 1638 | if tcpSrc: |
| 1639 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1640 | if tcpDst: |
| 1641 | cmd += " --tcpDst " + str( tcpDst ) |
| 1642 | if setEthSrc: |
| 1643 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1644 | if setEthDst: |
| 1645 | cmd += " --setEthDst " + str( setEthDst ) |
| 1646 | if vlanId: |
| 1647 | cmd += " -v " + str( vlanId ) |
| 1648 | if setVlan: |
| 1649 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1650 | if partial: |
| 1651 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1652 | if encap: |
| 1653 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1654 | |
| 1655 | # Check whether the user appended the port |
| 1656 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1657 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1658 | if "/" in ingressDevice: |
| 1659 | cmd += " " + str( ingressDevice ) |
| 1660 | else: |
| 1661 | if not portIngress: |
| 1662 | main.log.error( "You must specify " + |
| 1663 | "the Ingress port" ) |
| 1664 | return main.FALSE |
| 1665 | |
| 1666 | cmd += " " +\ |
| 1667 | str( ingressDevice ) + "/" +\ |
| 1668 | str( portIngress ) |
| 1669 | |
| 1670 | if portEgressList is None: |
| 1671 | for egressDevice in egressDeviceList: |
| 1672 | if "/" in egressDevice: |
| 1673 | cmd += " " + str( egressDevice ) |
| 1674 | else: |
| 1675 | main.log.error( "You must specify " + |
| 1676 | "the egress port" ) |
| 1677 | # TODO: perhaps more meaningful return |
| 1678 | return main.FALSE |
| 1679 | else: |
| 1680 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1681 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1682 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1683 | cmd += " " + \ |
| 1684 | str( egressDevice ) + "/" +\ |
| 1685 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1686 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1687 | main.log.error( "Device list and port list does not " + |
| 1688 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1689 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1690 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1691 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1692 | # If error, return error message |
| 1693 | if re.search( "Error", handle ): |
| 1694 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1695 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1696 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1697 | else: |
| 1698 | match = re.search('id=0x([\da-f]+),', handle) |
| 1699 | if match: |
| 1700 | return match.group()[3:-1] |
| 1701 | else: |
| 1702 | main.log.error( "Error, intent ID not found" ) |
| 1703 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1704 | except AssertionError: |
| 1705 | main.log.exception( "" ) |
| 1706 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1707 | except TypeError: |
| 1708 | main.log.exception( self.name + ": Object not as expected" ) |
| 1709 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1710 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1711 | main.log.error( self.name + ": EOF exception found" ) |
| 1712 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1713 | main.cleanup() |
| 1714 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1715 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1716 | main.log.exception( self.name + ": Uncaught exception!" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1717 | main.cleanup() |
| 1718 | main.exit() |
| 1719 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1720 | def addMplsIntent( |
| 1721 | self, |
| 1722 | ingressDevice, |
| 1723 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1724 | ingressPort="", |
| 1725 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1726 | ethType="", |
| 1727 | ethSrc="", |
| 1728 | ethDst="", |
| 1729 | bandwidth="", |
| 1730 | lambdaAlloc=False, |
| 1731 | ipProto="", |
| 1732 | ipSrc="", |
| 1733 | ipDst="", |
| 1734 | tcpSrc="", |
| 1735 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1736 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1737 | egressLabel="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1738 | priority=""): |
| 1739 | """ |
| 1740 | Required: |
| 1741 | * ingressDevice: device id of ingress device |
| 1742 | * egressDevice: device id of egress device |
| 1743 | Optional: |
| 1744 | * ethType: specify ethType |
| 1745 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1746 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1747 | * bandwidth: specify bandwidth capacity of link |
| 1748 | * lambdaAlloc: if True, intent will allocate lambda |
| 1749 | for the specified intent |
| 1750 | * ipProto: specify ip protocol |
| 1751 | * ipSrc: specify ip source address |
| 1752 | * ipDst: specify ip destination address |
| 1753 | * tcpSrc: specify tcp source port |
| 1754 | * tcpDst: specify tcp destination port |
| 1755 | * ingressLabel: Ingress MPLS label |
| 1756 | * egressLabel: Egress MPLS label |
| 1757 | Description: |
| 1758 | Adds MPLS intent by |
| 1759 | specifying device id's and optional fields |
| 1760 | Returns: |
| 1761 | A string of the intent id or None on error |
| 1762 | |
| 1763 | NOTE: This function may change depending on the |
| 1764 | options developers provide for MPLS |
| 1765 | intent via cli |
| 1766 | """ |
| 1767 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1768 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1769 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1770 | if ethType: |
| 1771 | cmd += " --ethType " + str( ethType ) |
| 1772 | if ethSrc: |
| 1773 | cmd += " --ethSrc " + str( ethSrc ) |
| 1774 | if ethDst: |
| 1775 | cmd += " --ethDst " + str( ethDst ) |
| 1776 | if bandwidth: |
| 1777 | cmd += " --bandwidth " + str( bandwidth ) |
| 1778 | if lambdaAlloc: |
| 1779 | cmd += " --lambda " |
| 1780 | if ipProto: |
| 1781 | cmd += " --ipProto " + str( ipProto ) |
| 1782 | if ipSrc: |
| 1783 | cmd += " --ipSrc " + str( ipSrc ) |
| 1784 | if ipDst: |
| 1785 | cmd += " --ipDst " + str( ipDst ) |
| 1786 | if tcpSrc: |
| 1787 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1788 | if tcpDst: |
| 1789 | cmd += " --tcpDst " + str( tcpDst ) |
| 1790 | if ingressLabel: |
| 1791 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1792 | if egressLabel: |
| 1793 | cmd += " --egressLabel " + str( egressLabel ) |
| 1794 | if priority: |
| 1795 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1796 | |
| 1797 | # Check whether the user appended the port |
| 1798 | # or provided it as an input |
| 1799 | if "/" in ingressDevice: |
| 1800 | cmd += " " + str( ingressDevice ) |
| 1801 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1802 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1803 | main.log.error( "You must specify the ingress port" ) |
| 1804 | return None |
| 1805 | |
| 1806 | cmd += " " + \ |
| 1807 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1808 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1809 | |
| 1810 | if "/" in egressDevice: |
| 1811 | cmd += " " + str( egressDevice ) |
| 1812 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1813 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1814 | main.log.error( "You must specify the egress port" ) |
| 1815 | return None |
| 1816 | |
| 1817 | cmd += " " +\ |
| 1818 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1819 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1820 | |
| 1821 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1822 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1823 | # If error, return error message |
| 1824 | if re.search( "Error", handle ): |
| 1825 | main.log.error( "Error in adding mpls intent" ) |
| 1826 | return None |
| 1827 | else: |
| 1828 | # TODO: print out all the options in this message? |
| 1829 | main.log.info( "MPLS intent installed between " + |
| 1830 | str( ingressDevice ) + " and " + |
| 1831 | str( egressDevice ) ) |
| 1832 | match = re.search('id=0x([\da-f]+),', handle) |
| 1833 | if match: |
| 1834 | return match.group()[3:-1] |
| 1835 | else: |
| 1836 | main.log.error( "Error, intent ID not found" ) |
| 1837 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1838 | except AssertionError: |
| 1839 | main.log.exception( "" ) |
| 1840 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1841 | except TypeError: |
| 1842 | main.log.exception( self.name + ": Object not as expected" ) |
| 1843 | return None |
| 1844 | except pexpect.EOF: |
| 1845 | main.log.error( self.name + ": EOF exception found" ) |
| 1846 | main.log.error( self.name + ": " + self.handle.before ) |
| 1847 | main.cleanup() |
| 1848 | main.exit() |
| 1849 | except Exception: |
| 1850 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1851 | main.cleanup() |
| 1852 | main.exit() |
| 1853 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1854 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1855 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1856 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1857 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1858 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1859 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1860 | -p or --purge: Purge the intent from the store after removal |
| 1861 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1862 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 1863 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1864 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1865 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1866 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1867 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1868 | if purge: |
| 1869 | cmdStr += " -p" |
| 1870 | if sync: |
| 1871 | cmdStr += " -s" |
| 1872 | |
| 1873 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1874 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1875 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1876 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1877 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1878 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1879 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1880 | # TODO: Should this be main.TRUE |
| 1881 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1882 | except AssertionError: |
| 1883 | main.log.exception( "" ) |
| 1884 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1885 | except TypeError: |
| 1886 | main.log.exception( self.name + ": Object not as expected" ) |
| 1887 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1888 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1889 | main.log.error( self.name + ": EOF exception found" ) |
| 1890 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1891 | main.cleanup() |
| 1892 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1893 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1894 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1895 | main.cleanup() |
| 1896 | main.exit() |
| 1897 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1898 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1899 | """ |
| 1900 | Description: |
| 1901 | Remove all the intents |
| 1902 | Optional args:- |
| 1903 | -s or --sync: Waits for the removal before returning |
| 1904 | -p or --purge: Purge the intent from the store after removal |
| 1905 | Returns: |
| 1906 | Returns main.TRUE if all intents are removed, otherwise returns |
| 1907 | main.FALSE; Returns None for exception |
| 1908 | """ |
| 1909 | try: |
| 1910 | cmdStr = "remove-intent" |
| 1911 | if purge: |
| 1912 | cmdStr += " -p" |
| 1913 | if sync: |
| 1914 | cmdStr += " -s" |
| 1915 | |
| 1916 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1917 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1918 | assert "Command not found:" not in handle, handle |
| 1919 | if re.search( "Error", handle ): |
| 1920 | main.log.error( "Error in removing intent" ) |
| 1921 | return main.FALSE |
| 1922 | else: |
| 1923 | return main.TRUE |
| 1924 | except AssertionError: |
| 1925 | main.log.exception( "" ) |
| 1926 | return None |
| 1927 | except TypeError: |
| 1928 | main.log.exception( self.name + ": Object not as expected" ) |
| 1929 | return None |
| 1930 | except pexpect.EOF: |
| 1931 | main.log.error( self.name + ": EOF exception found" ) |
| 1932 | main.log.error( self.name + ": " + self.handle.before ) |
| 1933 | main.cleanup() |
| 1934 | main.exit() |
| 1935 | except Exception: |
| 1936 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1937 | main.cleanup() |
| 1938 | main.exit() |
| 1939 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 1940 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1941 | """ |
| 1942 | Purges all WITHDRAWN Intents |
| 1943 | """ |
| 1944 | try: |
| 1945 | cmdStr = "purge-intents" |
| 1946 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1947 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1948 | if re.search( "Error", handle ): |
| 1949 | main.log.error( "Error in purging intents" ) |
| 1950 | return main.FALSE |
| 1951 | else: |
| 1952 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1953 | except AssertionError: |
| 1954 | main.log.exception( "" ) |
| 1955 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1956 | except TypeError: |
| 1957 | main.log.exception( self.name + ": Object not as expected" ) |
| 1958 | return None |
| 1959 | except pexpect.EOF: |
| 1960 | main.log.error( self.name + ": EOF exception found" ) |
| 1961 | main.log.error( self.name + ": " + self.handle.before ) |
| 1962 | main.cleanup() |
| 1963 | main.exit() |
| 1964 | except Exception: |
| 1965 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1966 | main.cleanup() |
| 1967 | main.exit() |
| 1968 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1969 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1970 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1971 | NOTE: This method should be used after installing application: |
| 1972 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1973 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1974 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1975 | Description: |
| 1976 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1977 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1978 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1979 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1980 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1981 | cmdStr += " -j" |
| 1982 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1983 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1984 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1985 | except AssertionError: |
| 1986 | main.log.exception( "" ) |
| 1987 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1988 | except TypeError: |
| 1989 | main.log.exception( self.name + ": Object not as expected" ) |
| 1990 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1991 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1992 | main.log.error( self.name + ": EOF exception found" ) |
| 1993 | main.log.error( self.name + ": " + self.handle.before ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1994 | main.cleanup() |
| 1995 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1996 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1997 | main.log.exception( self.name + ": Uncaught exception!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1998 | main.cleanup() |
| 1999 | main.exit() |
| 2000 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2001 | def ipv4RouteNumber( self ): |
| 2002 | """ |
| 2003 | NOTE: This method should be used after installing application: |
| 2004 | onos-app-sdnip |
| 2005 | Description: |
| 2006 | Obtain the total IPv4 routes number in the system |
| 2007 | """ |
| 2008 | try: |
| 2009 | cmdStr = "routes -s -j" |
| 2010 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2011 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2012 | jsonResult = json.loads( handle ) |
| 2013 | return jsonResult['totalRoutes4'] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2014 | except AssertionError: |
| 2015 | main.log.exception( "" ) |
| 2016 | return None |
| 2017 | except ( TypeError, ValueError ): |
| 2018 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2019 | return None |
| 2020 | except pexpect.EOF: |
| 2021 | main.log.error( self.name + ": EOF exception found" ) |
| 2022 | main.log.error( self.name + ": " + self.handle.before ) |
| 2023 | main.cleanup() |
| 2024 | main.exit() |
| 2025 | except Exception: |
| 2026 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2027 | main.cleanup() |
| 2028 | main.exit() |
| 2029 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2030 | def intents( self, jsonFormat = True, summary = False, **intentargs): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2031 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2032 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2033 | Obtain intents from the ONOS cli. |
| 2034 | Optional: |
| 2035 | * jsonFormat: Enable output formatting in json, default to True |
| 2036 | * summary: Whether only output the intent summary, defaults to False |
| 2037 | * type: Only output a certain type of intent. This options is valid |
| 2038 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2039 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2040 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2041 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2042 | if summary: |
| 2043 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2044 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2045 | cmdStr += " -j" |
| 2046 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2047 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2048 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2049 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2050 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2051 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2052 | intentType = "" |
| 2053 | # IF we want the summary of a specific intent type |
| 2054 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2055 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2056 | if intentType in jsonResult.keys(): |
| 2057 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2058 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2059 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2060 | return handle |
| 2061 | else: |
| 2062 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2063 | except AssertionError: |
| 2064 | main.log.exception( "" ) |
| 2065 | return None |
| 2066 | except ( TypeError, ValueError ): |
| 2067 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2068 | return None |
| 2069 | except pexpect.EOF: |
| 2070 | main.log.error( self.name + ": EOF exception found" ) |
| 2071 | main.log.error( self.name + ": " + self.handle.before ) |
| 2072 | main.cleanup() |
| 2073 | main.exit() |
| 2074 | except Exception: |
| 2075 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2076 | main.cleanup() |
| 2077 | main.exit() |
| 2078 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2079 | def getIntentState(self, intentsId, intentsJson=None): |
| 2080 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2081 | Description: |
| 2082 | Gets intent state. Accepts a single intent ID (string type) or a |
| 2083 | list of intent IDs. |
| 2084 | Parameters: |
| 2085 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2086 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2087 | Returns: |
| 2088 | Returns the state (string type) of the ID if a single intent ID is |
| 2089 | accepted. |
| 2090 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2091 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2092 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2093 | """ |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2094 | try: |
| 2095 | state = "State is Undefined" |
| 2096 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2097 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2098 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2099 | rawJson = intentsJson |
| 2100 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2101 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2102 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2103 | if intentsId == intent[ 'id' ]: |
| 2104 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2105 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2106 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
| 2107 | " on the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2108 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2109 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2110 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2111 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2112 | stateDict = {} |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2113 | for intents in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2114 | if intentsId[ i ] == intents[ 'id' ]: |
| 2115 | stateDict[ 'state' ] = intents[ 'state' ] |
| 2116 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2117 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2118 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2119 | if len( intentsId ) != len( dictList ): |
| 2120 | main.log.info( "Cannot find some of the intent ID state" ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2121 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2122 | else: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2123 | main.log.info( "Invalid intents ID entry" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2124 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2125 | except ( TypeError, ValueError ): |
| 2126 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2127 | return None |
| 2128 | except pexpect.EOF: |
| 2129 | main.log.error( self.name + ": EOF exception found" ) |
| 2130 | main.log.error( self.name + ": " + self.handle.before ) |
| 2131 | main.cleanup() |
| 2132 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2133 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2134 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2135 | main.cleanup() |
| 2136 | main.exit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2137 | |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2138 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2139 | """ |
| 2140 | Description: |
| 2141 | Check intents state |
| 2142 | Required: |
| 2143 | intentsId - List of intents ID to be checked |
| 2144 | Optional: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2145 | expectedState - Check the expected state(s) of each intents |
| 2146 | state in the list. |
| 2147 | *NOTE: You can pass in a list of expected state, |
| 2148 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2149 | Return: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2150 | Returns main.TRUE only if all intent are the same as expected states |
| 2151 | , otherwise, returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2152 | """ |
| 2153 | try: |
| 2154 | # Generating a dictionary: intent id as a key and state as value |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2155 | returnValue = main.TRUE |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2156 | intentsDict = self.getIntentState( intentsId ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2157 | if len( intentsId ) != len( intentsDict ): |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2158 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2159 | "getting intents state" ) |
| 2160 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2161 | |
| 2162 | if isinstance( expectedState, types.StringType ): |
| 2163 | for intents in intentsDict: |
| 2164 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2165 | main.log.debug( self.name + " : Intent ID - " + |
| 2166 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2167 | " actual state = " + |
| 2168 | intents.get( 'state' ) |
| 2169 | + " does not equal expected state = " |
| 2170 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2171 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2172 | |
| 2173 | elif isinstance( expectedState, types.ListType ): |
| 2174 | for intents in intentsDict: |
| 2175 | if not any( state == intents.get( 'state' ) for state in |
| 2176 | expectedState ): |
| 2177 | main.log.debug( self.name + " : Intent ID - " + |
| 2178 | intents.get( 'id' ) + |
| 2179 | " actual state = " + |
| 2180 | intents.get( 'state' ) + |
| 2181 | " does not equal expected states = " |
| 2182 | + str( expectedState ) ) |
| 2183 | returnValue = main.FALSE |
| 2184 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2185 | if returnValue == main.TRUE: |
| 2186 | main.log.info( self.name + ": All " + |
| 2187 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2188 | " intents are in " + str( expectedState ) + |
| 2189 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2190 | return returnValue |
| 2191 | except TypeError: |
| 2192 | main.log.exception( self.name + ": Object not as expected" ) |
| 2193 | return None |
| 2194 | except pexpect.EOF: |
| 2195 | main.log.error( self.name + ": EOF exception found" ) |
| 2196 | main.log.error( self.name + ": " + self.handle.before ) |
| 2197 | main.cleanup() |
| 2198 | main.exit() |
| 2199 | except Exception: |
| 2200 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2201 | main.cleanup() |
| 2202 | main.exit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2203 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2204 | def compareIntent( self, intentDict ): |
| 2205 | """ |
| 2206 | Description: |
| 2207 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2208 | Return: |
| 2209 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2210 | Arguments: |
| 2211 | intentDict: a dictionary which maps intent ids to intent states |
| 2212 | """ |
| 2213 | try: |
| 2214 | intentsRaw = self.intents() |
| 2215 | intentsJson = json.loads( intentsRaw ) |
| 2216 | intentDictONOS = {} |
| 2217 | for intent in intentsJson: |
| 2218 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2219 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2220 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2221 | main.log.warn( self.name + ": expected intent count does not match that in ONOS, " + |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2222 | str( len( intentDict ) ) + " expected and " + |
| 2223 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2224 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2225 | for intentID in intentDict.keys(): |
| 2226 | if not intentID in intentDictONOS.keys(): |
| 2227 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2228 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2229 | else: |
| 2230 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2231 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2232 | " expected state is " + intentDict[ intentID ] + |
| 2233 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2234 | returnValue = main.FALSE |
| 2235 | intentDictONOS.pop( intentID ) |
| 2236 | if len( intentDictONOS ) > 0: |
| 2237 | returnValue = main.FALSE |
| 2238 | for intentID in intentDictONOS.keys(): |
| 2239 | main.log.debug( self.name + ": find extra intent in ONOS: intent ID " + intentID ) |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2240 | if returnValue == main.TRUE: |
| 2241 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2242 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2243 | except KeyError: |
| 2244 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2245 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2246 | except ( TypeError, ValueError ): |
| 2247 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2248 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2249 | except pexpect.EOF: |
| 2250 | main.log.error( self.name + ": EOF exception found" ) |
| 2251 | main.log.error( self.name + ": " + self.handle.before ) |
| 2252 | main.cleanup() |
| 2253 | main.exit() |
| 2254 | except Exception: |
| 2255 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2256 | main.cleanup() |
| 2257 | main.exit() |
| 2258 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2259 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2260 | """ |
| 2261 | Description: |
| 2262 | Check the number of installed intents. |
| 2263 | Optional: |
| 2264 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2265 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2266 | Return: |
| 2267 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2268 | , otherwise, returns main.FALSE. |
| 2269 | """ |
| 2270 | |
| 2271 | try: |
| 2272 | cmd = "intents -s -j" |
| 2273 | |
| 2274 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2275 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2276 | if response == None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2277 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2278 | response = json.loads( response ) |
| 2279 | |
| 2280 | # get total and installed number, see if they are match |
| 2281 | allState = response.get( 'all' ) |
| 2282 | if allState.get('total') == allState.get('installed'): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2283 | 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] | 2284 | return main.TRUE |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2285 | 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] | 2286 | return main.FALSE |
| 2287 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2288 | except ( TypeError, ValueError ): |
| 2289 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2290 | return None |
| 2291 | except pexpect.EOF: |
| 2292 | main.log.error( self.name + ": EOF exception found" ) |
| 2293 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2294 | if noExit: |
| 2295 | return main.FALSE |
| 2296 | else: |
| 2297 | main.cleanup() |
| 2298 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2299 | except Exception: |
| 2300 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2301 | if noExit: |
| 2302 | return main.FALSE |
| 2303 | else: |
| 2304 | main.cleanup() |
| 2305 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2306 | except pexpect.TIMEOUT: |
| 2307 | main.log.error( self.name + ": ONOS timeout" ) |
| 2308 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2309 | |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2310 | def flows( self, state="", jsonFormat=True, timeout=60, noExit=False, noCore=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2311 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2312 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2313 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2314 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2315 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2316 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2317 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2318 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2319 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2320 | if jsonFormat: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2321 | cmdStr += " -j " |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2322 | if noCore: |
| 2323 | cmdStr += " -n " |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2324 | cmdStr += state |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2325 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2326 | assert "Command not found:" not in handle, handle |
| 2327 | if re.search( "Error:", handle ): |
| 2328 | main.log.error( self.name + ": flows() response: " + |
| 2329 | str( handle ) ) |
| 2330 | return handle |
| 2331 | except AssertionError: |
| 2332 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2333 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2334 | except TypeError: |
| 2335 | main.log.exception( self.name + ": Object not as expected" ) |
| 2336 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2337 | except pexpect.TIMEOUT: |
| 2338 | main.log.error( self.name + ": ONOS timeout" ) |
| 2339 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2340 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2341 | main.log.error( self.name + ": EOF exception found" ) |
| 2342 | main.log.error( self.name + ": " + self.handle.before ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2343 | main.cleanup() |
| 2344 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2345 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2346 | main.log.exception( self.name + ": Uncaught exception!" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2347 | main.cleanup() |
| 2348 | main.exit() |
| 2349 | |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2350 | def checkFlowCount(self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2351 | count = self.getTotalFlowsNum( timeout=timeout ) |
| 2352 | count = int (count) if count else 0 |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2353 | return count if (count > min) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2354 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2355 | def checkFlowsState( self, isPENDING=True, timeout=60,noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2356 | """ |
| 2357 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2358 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2359 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2360 | if the count of those states is 0, which means all current flows |
| 2361 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2362 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2363 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2364 | Return: |
| 2365 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2366 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2367 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2368 | """ |
| 2369 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2370 | states = ["PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED"] |
| 2371 | checkedStates = [] |
| 2372 | statesCount = [0, 0, 0, 0] |
| 2373 | for s in states: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2374 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2375 | if rawFlows: |
| 2376 | # if we didn't get flows or flows function return None, we should return |
| 2377 | # main.Flase |
| 2378 | checkedStates.append( json.loads( rawFlows ) ) |
| 2379 | else: |
| 2380 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2381 | for i in range( len( states ) ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2382 | for c in checkedStates[i]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2383 | try: |
| 2384 | statesCount[i] += int( c.get( "flowCount" ) ) |
| 2385 | except TypeError: |
| 2386 | main.log.exception( "Json object not as expected" ) |
| 2387 | main.log.info( states[i] + " flows: " + str( statesCount[i] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2388 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2389 | # We want to count PENDING_ADD if isPENDING is true |
| 2390 | if isPENDING: |
| 2391 | if statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2392 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2393 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2394 | if statesCount[0] + statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2395 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2396 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2397 | except ( TypeError, ValueError ): |
| 2398 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2399 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2400 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2401 | except AssertionError: |
| 2402 | main.log.exception( "" ) |
| 2403 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2404 | except pexpect.EOF: |
| 2405 | main.log.error( self.name + ": EOF exception found" ) |
| 2406 | main.log.error( self.name + ": " + self.handle.before ) |
| 2407 | main.cleanup() |
| 2408 | main.exit() |
| 2409 | except Exception: |
| 2410 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2411 | main.cleanup() |
| 2412 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2413 | except pexpect.TIMEOUT: |
| 2414 | main.log.error( self.name + ": ONOS timeout" ) |
| 2415 | return None |
| 2416 | |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2417 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2418 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2419 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2420 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2421 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2422 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2423 | a specific point-to-point intent definition |
| 2424 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2425 | * ingress: specify source dpid |
| 2426 | * egress: specify destination dpid |
| 2427 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2428 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2429 | * offset: the keyOffset is where the next batch of intents |
| 2430 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2431 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2432 | * getResponse: If set to True, function will return ONOS response. |
| 2433 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2434 | Returns: If failed to push test intents, it will returen None, |
| 2435 | if successful, return true. |
| 2436 | Timeout expection will return None, |
| 2437 | TypeError will return false |
| 2438 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2439 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2440 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2441 | if background: |
| 2442 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2443 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2444 | back = "" |
| 2445 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2446 | ingress, |
| 2447 | egress, |
| 2448 | batchSize, |
| 2449 | offset, |
| 2450 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2451 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2452 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2453 | main.log.info( response ) |
| 2454 | if response == None: |
| 2455 | return None |
| 2456 | |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2457 | if getResponse: |
| 2458 | return response |
| 2459 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2460 | # TODO: We should handle if there is failure in installation |
| 2461 | return main.TRUE |
| 2462 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2463 | except AssertionError: |
| 2464 | main.log.exception( "" ) |
| 2465 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2466 | except pexpect.TIMEOUT: |
| 2467 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2468 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2469 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2470 | main.log.error( self.name + ": EOF exception found" ) |
| 2471 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2472 | main.cleanup() |
| 2473 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2474 | except TypeError: |
| 2475 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2476 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2477 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2478 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2479 | main.cleanup() |
| 2480 | main.exit() |
| 2481 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2482 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2483 | """ |
| 2484 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2485 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2486 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2487 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2488 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2489 | """ |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2490 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2491 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2492 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2493 | cmd = "flows -c added" |
| 2494 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2495 | if rawFlows: |
| 2496 | rawFlows = rawFlows.split("\n") |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2497 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2498 | for l in rawFlows: |
| 2499 | totalFlows += int(l.split("Count=")[1]) |
| 2500 | else: |
| 2501 | main.log.error("Response not as expected!") |
| 2502 | return None |
| 2503 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2504 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2505 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2506 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2507 | return None |
| 2508 | except pexpect.EOF: |
| 2509 | main.log.error( self.name + ": EOF exception found" ) |
| 2510 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2511 | if not noExit: |
| 2512 | main.cleanup() |
| 2513 | main.exit() |
| 2514 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2515 | except Exception: |
| 2516 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2517 | if not noExit: |
| 2518 | main.cleanup() |
| 2519 | main.exit() |
| 2520 | return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2521 | except pexpect.TIMEOUT: |
| 2522 | main.log.error( self.name + ": ONOS timeout" ) |
| 2523 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2524 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2525 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2526 | """ |
| 2527 | Description: |
| 2528 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2529 | Optional: |
| 2530 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2531 | Return: |
| 2532 | The number of intents |
| 2533 | """ |
| 2534 | try: |
| 2535 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2536 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2537 | if response == None: |
| 2538 | return -1 |
| 2539 | response = json.loads( response ) |
| 2540 | return int( response.get("intents") ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2541 | except ( TypeError, ValueError ): |
| 2542 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2543 | return None |
| 2544 | except pexpect.EOF: |
| 2545 | main.log.error( self.name + ": EOF exception found" ) |
| 2546 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2547 | if noExit: |
| 2548 | return -1 |
| 2549 | else: |
| 2550 | main.cleanup() |
| 2551 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2552 | except Exception: |
| 2553 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2554 | if noExit: |
| 2555 | return -1 |
| 2556 | else: |
| 2557 | main.cleanup() |
| 2558 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2559 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2560 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2561 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2562 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2563 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2564 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2565 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2566 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2567 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2568 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2569 | cmdStr += " -j" |
| 2570 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2571 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2572 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2573 | except AssertionError: |
| 2574 | main.log.exception( "" ) |
| 2575 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2576 | except TypeError: |
| 2577 | main.log.exception( self.name + ": Object not as expected" ) |
| 2578 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2579 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2580 | main.log.error( self.name + ": EOF exception found" ) |
| 2581 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2582 | main.cleanup() |
| 2583 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2584 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2585 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2586 | main.cleanup() |
| 2587 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2588 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2589 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2590 | """ |
| 2591 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2592 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2593 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2594 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2595 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2596 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2597 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2598 | cmdStr += " -j" |
| 2599 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2600 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2601 | if handle: |
| 2602 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2603 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2604 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2605 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2606 | else: |
| 2607 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2608 | except AssertionError: |
| 2609 | main.log.exception( "" ) |
| 2610 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2611 | except TypeError: |
| 2612 | main.log.exception( self.name + ": Object not as expected" ) |
| 2613 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2614 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2615 | main.log.error( self.name + ": EOF exception found" ) |
| 2616 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2617 | main.cleanup() |
| 2618 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2619 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2620 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2621 | main.cleanup() |
| 2622 | main.exit() |
| 2623 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2624 | # Wrapper functions **************** |
| 2625 | # Wrapper functions use existing driver |
| 2626 | # functions and extends their use case. |
| 2627 | # For example, we may use the output of |
| 2628 | # a normal driver function, and parse it |
| 2629 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2630 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2631 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2632 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2633 | Description: |
| 2634 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2635 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2636 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2637 | # Obtain output of intents function |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2638 | intentsStr = self.intents(jsonFormat=False) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2639 | intentIdList = [] |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2640 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2641 | # Parse the intents output for ID's |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2642 | intentsList = [ s.strip() for s in intentsStr.splitlines() ] |
| 2643 | for intents in intentsList: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2644 | match = re.search('id=0x([\da-f]+),', intents) |
| 2645 | if match: |
| 2646 | tmpId = match.group()[3:-1] |
| 2647 | intentIdList.append( tmpId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2648 | return intentIdList |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2649 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2650 | except TypeError: |
| 2651 | main.log.exception( self.name + ": Object not as expected" ) |
| 2652 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2653 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2654 | main.log.error( self.name + ": EOF exception found" ) |
| 2655 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2656 | main.cleanup() |
| 2657 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2658 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2659 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2660 | main.cleanup() |
| 2661 | main.exit() |
| 2662 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2663 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2664 | """ |
| 2665 | Determine the number of flow rules for the given device id that are |
| 2666 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2667 | Params: |
| 2668 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2669 | """ |
| 2670 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2671 | if core: |
| 2672 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2673 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2674 | else: |
| 2675 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2676 | "grep 'state=ADDED' | wc -l" |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2677 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2678 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2679 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2680 | except AssertionError: |
| 2681 | main.log.exception( "" ) |
| 2682 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2683 | except pexpect.EOF: |
| 2684 | main.log.error( self.name + ": EOF exception found" ) |
| 2685 | main.log.error( self.name + ": " + self.handle.before ) |
| 2686 | main.cleanup() |
| 2687 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2688 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2689 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2690 | main.cleanup() |
| 2691 | main.exit() |
| 2692 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2693 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2694 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2695 | Use 'devices' function to obtain list of all devices |
| 2696 | and parse the result to obtain a list of all device |
| 2697 | id's. Returns this list. Returns empty list if no |
| 2698 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2699 | List is ordered sequentially |
| 2700 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2701 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2702 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2703 | the ids. By obtaining the list of device ids on the fly, |
| 2704 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2705 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2706 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2707 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2708 | devicesStr = self.devices( jsonFormat=False ) |
| 2709 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2710 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2711 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2712 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2713 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2714 | |
| 2715 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2716 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2717 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2718 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2719 | # Split list further into arguments before and after string |
| 2720 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2721 | # append to idList |
| 2722 | for arg in tempList: |
| 2723 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 2724 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2725 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2726 | except TypeError: |
| 2727 | main.log.exception( self.name + ": Object not as expected" ) |
| 2728 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2729 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2730 | main.log.error( self.name + ": EOF exception found" ) |
| 2731 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2732 | main.cleanup() |
| 2733 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2734 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2735 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2736 | main.cleanup() |
| 2737 | main.exit() |
| 2738 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2739 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2740 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2741 | Uses 'nodes' function to obtain list of all nodes |
| 2742 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2743 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2744 | Returns: |
| 2745 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2746 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2747 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2748 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2749 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2750 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 2751 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2752 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2753 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2754 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2755 | nodesJson = json.loads( nodesStr ) |
| 2756 | idList = [ node.get('id') for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2757 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2758 | except ( TypeError, ValueError ): |
| 2759 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2760 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2761 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2762 | main.log.error( self.name + ": EOF exception found" ) |
| 2763 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2764 | main.cleanup() |
| 2765 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2766 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2767 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2768 | main.cleanup() |
| 2769 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2770 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2771 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2772 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2773 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 2774 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2775 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2776 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2777 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2778 | return None |
| 2779 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2780 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2781 | rawDevices = self.devices() |
| 2782 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2783 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2784 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2785 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 2786 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2787 | return device |
| 2788 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2789 | except ( TypeError, ValueError ): |
| 2790 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2791 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2792 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2793 | main.log.error( self.name + ": EOF exception found" ) |
| 2794 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2795 | main.cleanup() |
| 2796 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2797 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2798 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2799 | main.cleanup() |
| 2800 | main.exit() |
| 2801 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2802 | def getTopology( self, topologyOutput ): |
| 2803 | """ |
| 2804 | Definition: |
| 2805 | Loads a json topology output |
| 2806 | Return: |
| 2807 | topology = current ONOS topology |
| 2808 | """ |
| 2809 | import json |
| 2810 | try: |
| 2811 | # either onos:topology or 'topology' will work in CLI |
| 2812 | topology = json.loads(topologyOutput) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2813 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2814 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2815 | except ( TypeError, ValueError ): |
| 2816 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 2817 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2818 | except pexpect.EOF: |
| 2819 | main.log.error( self.name + ": EOF exception found" ) |
| 2820 | main.log.error( self.name + ": " + self.handle.before ) |
| 2821 | main.cleanup() |
| 2822 | main.exit() |
| 2823 | except Exception: |
| 2824 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2825 | main.cleanup() |
| 2826 | main.exit() |
| 2827 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2828 | def checkStatus(self, numoswitch, numolink, numoctrl = -1, logLevel="info"): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2829 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2830 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2831 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2832 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2833 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2834 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2835 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2836 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2837 | logLevel = level to log to. |
| 2838 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2839 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2840 | Returns: main.TRUE if the number of switches and links are correct, |
| 2841 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2842 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2843 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2844 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2845 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 2846 | summary = self.summary() |
| 2847 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 2848 | except ( TypeError, ValueError ): |
| 2849 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 2850 | return main.ERROR |
| 2851 | try: |
| 2852 | topology = self.getTopology( self.topology() ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2853 | if topology == {} or topology == None or summary == {} or summary == None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2854 | return main.ERROR |
| 2855 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2856 | # Is the number of switches is what we expected |
| 2857 | devices = topology.get( 'devices', False ) |
| 2858 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2859 | nodes = summary.get( 'nodes', False ) |
| 2860 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2861 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2862 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2863 | # Is the number of links is what we expected |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2864 | linkCheck = ( int( links ) == int( numolink ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2865 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 2866 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2867 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2868 | output = output + "The number of links and switches match "\ |
| 2869 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2870 | result = main.TRUE |
| 2871 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2872 | output = output + \ |
| 2873 | "The number of links and switches does not match " + \ |
| 2874 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2875 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2876 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 2877 | output = output + " (%i expected) " % int( numoswitch ) |
| 2878 | output = output + "and %i links " % int( links ) |
| 2879 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 2880 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2881 | output = output + "and %i controllers " % int( nodes ) |
| 2882 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2883 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2884 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2885 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2886 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2887 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2888 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2889 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2890 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2891 | main.log.error( self.name + ": EOF exception found" ) |
| 2892 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2893 | main.cleanup() |
| 2894 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2895 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2896 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2897 | main.cleanup() |
| 2898 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2899 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2900 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2901 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2902 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2903 | deviceId must be the id of a device as seen in the onos devices command |
| 2904 | 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] | 2905 | role must be either master, standby, or none |
| 2906 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2907 | Returns: |
| 2908 | main.TRUE or main.FALSE based on argument verification and |
| 2909 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2910 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2911 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2912 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2913 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2914 | cmdStr = "device-role " +\ |
| 2915 | str( deviceId ) + " " +\ |
| 2916 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2917 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2918 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2919 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2920 | if re.search( "Error", handle ): |
| 2921 | # end color output to escape any colours |
| 2922 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2923 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2924 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2925 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2926 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2927 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2928 | main.log.error( "Invalid 'role' given to device_role(). " + |
| 2929 | "Value was '" + str(role) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2930 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2931 | except AssertionError: |
| 2932 | main.log.exception( "" ) |
| 2933 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2934 | except TypeError: |
| 2935 | main.log.exception( self.name + ": Object not as expected" ) |
| 2936 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2937 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2938 | main.log.error( self.name + ": EOF exception found" ) |
| 2939 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2940 | main.cleanup() |
| 2941 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2942 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2943 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2944 | main.cleanup() |
| 2945 | main.exit() |
| 2946 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2947 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2948 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2949 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2950 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2951 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2952 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2953 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2954 | cmdStr = "clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2955 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2956 | cmdStr += " -j" |
| 2957 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2958 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2959 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2960 | except AssertionError: |
| 2961 | main.log.exception( "" ) |
| 2962 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2963 | except TypeError: |
| 2964 | main.log.exception( self.name + ": Object not as expected" ) |
| 2965 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2966 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2967 | main.log.error( self.name + ": EOF exception found" ) |
| 2968 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2969 | main.cleanup() |
| 2970 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2971 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2972 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2973 | main.cleanup() |
| 2974 | main.exit() |
| 2975 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2976 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2977 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2978 | CLI command to get the current leader for the Election test application |
| 2979 | NOTE: Requires installation of the onos-app-election feature |
| 2980 | Returns: Node IP of the leader if one exists |
| 2981 | None if none exists |
| 2982 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2983 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2984 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2985 | cmdStr = "election-test-leader" |
| 2986 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2987 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2988 | # Leader |
| 2989 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2990 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2991 | nodeSearch = re.search( leaderPattern, response ) |
| 2992 | if nodeSearch: |
| 2993 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2994 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2995 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2996 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2997 | # no leader |
| 2998 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2999 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3000 | nullSearch = re.search( nullPattern, response ) |
| 3001 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3002 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3003 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3004 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3005 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3006 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 3007 | ": " + "unexpected response" ) |
| 3008 | main.log.error( repr( response ) ) |
| 3009 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3010 | except AssertionError: |
| 3011 | main.log.exception( "" ) |
| 3012 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3013 | except TypeError: |
| 3014 | main.log.exception( self.name + ": Object not as expected" ) |
| 3015 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3016 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3017 | main.log.error( self.name + ": EOF exception found" ) |
| 3018 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3019 | main.cleanup() |
| 3020 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3021 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3022 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3023 | main.cleanup() |
| 3024 | main.exit() |
| 3025 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3026 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3027 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3028 | CLI command to run for leadership of the Election test application. |
| 3029 | NOTE: Requires installation of the onos-app-election feature |
| 3030 | Returns: Main.TRUE on success |
| 3031 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3032 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3033 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3034 | cmdStr = "election-test-run" |
| 3035 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3036 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3037 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3038 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3039 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3040 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3041 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3042 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3043 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3044 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3045 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3046 | main.log.error( "Error in electionTestRun on " + self.name + |
| 3047 | ": " + "unexpected response" ) |
| 3048 | main.log.error( repr( response ) ) |
| 3049 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3050 | except AssertionError: |
| 3051 | main.log.exception( "" ) |
| 3052 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3053 | except TypeError: |
| 3054 | main.log.exception( self.name + ": Object not as expected" ) |
| 3055 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3056 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3057 | main.log.error( self.name + ": EOF exception found" ) |
| 3058 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3059 | main.cleanup() |
| 3060 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3061 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3062 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3063 | main.cleanup() |
| 3064 | main.exit() |
| 3065 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3066 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3067 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3068 | * CLI command to withdraw the local node from leadership election for |
| 3069 | * the Election test application. |
| 3070 | #NOTE: Requires installation of the onos-app-election feature |
| 3071 | Returns: Main.TRUE on success |
| 3072 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3073 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3074 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3075 | cmdStr = "election-test-withdraw" |
| 3076 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3077 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3078 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3079 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3080 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3081 | if re.search( successPattern, response ): |
| 3082 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3083 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3084 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3085 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3086 | main.log.error( "Error in electionTestWithdraw on " + |
| 3087 | self.name + ": " + "unexpected response" ) |
| 3088 | main.log.error( repr( response ) ) |
| 3089 | return main.FALSE |
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 main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -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 ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -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!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3103 | main.cleanup() |
| 3104 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3105 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3106 | def getDevicePortsEnabledCount( 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: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3111 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3112 | cmdStr = "onos:ports -e " + dpid + " | 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 ): |
| 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 getDeviceLinksActiveCount( self, dpid ): |
| 3137 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3138 | Get the count of all enabled ports on a particular device/switch |
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 | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3141 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3142 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3143 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3144 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3145 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3146 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3147 | main.log.error( "Error in getting ports " ) |
| 3148 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3149 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3150 | except AssertionError: |
| 3151 | main.log.exception( "" ) |
| 3152 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3153 | except TypeError: |
| 3154 | main.log.exception( self.name + ": Object not as expected" ) |
| 3155 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3156 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3157 | main.log.error( self.name + ": EOF exception found" ) |
| 3158 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3159 | main.cleanup() |
| 3160 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3161 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3162 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3163 | main.cleanup() |
| 3164 | main.exit() |
| 3165 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3166 | def getAllIntentIds( self ): |
| 3167 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3168 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3169 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3170 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3171 | cmdStr = "onos:intents | grep id=" |
| 3172 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3173 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3174 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3175 | if re.search( "Error", output ): |
| 3176 | main.log.error( "Error in getting ports" ) |
| 3177 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3178 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3179 | except AssertionError: |
| 3180 | main.log.exception( "" ) |
| 3181 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3182 | except TypeError: |
| 3183 | main.log.exception( self.name + ": Object not as expected" ) |
| 3184 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3185 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3186 | main.log.error( self.name + ": EOF exception found" ) |
| 3187 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3188 | main.cleanup() |
| 3189 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3190 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3191 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3192 | main.cleanup() |
| 3193 | main.exit() |
| 3194 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3195 | def intentSummary( self ): |
| 3196 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3197 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3198 | """ |
| 3199 | try: |
| 3200 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3201 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3202 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3203 | states.append( intent.get( 'state', None ) ) |
| 3204 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3205 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3206 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3207 | except ( TypeError, ValueError ): |
| 3208 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3209 | return None |
| 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 | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3215 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3216 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3217 | main.cleanup() |
| 3218 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3219 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3220 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3221 | """ |
| 3222 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3223 | Optional argument: |
| 3224 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3225 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3226 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3227 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3228 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 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 |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -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 |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3237 | except TypeError: |
| 3238 | main.log.exception( self.name + ": Object not as expected" ) |
| 3239 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 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() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3245 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3246 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3247 | main.cleanup() |
| 3248 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3249 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3250 | def leaderCandidates( self, jsonFormat=True ): |
| 3251 | """ |
| 3252 | Returns the output of the leaders -c command. |
| 3253 | Optional argument: |
| 3254 | * jsonFormat - boolean indicating if you want output in json |
| 3255 | """ |
| 3256 | try: |
| 3257 | cmdStr = "onos:leaders -c" |
| 3258 | if jsonFormat: |
| 3259 | cmdStr += " -j" |
| 3260 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3261 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3262 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3263 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3264 | except AssertionError: |
| 3265 | main.log.exception( "" ) |
| 3266 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3267 | except TypeError: |
| 3268 | main.log.exception( self.name + ": Object not as expected" ) |
| 3269 | return None |
| 3270 | except pexpect.EOF: |
| 3271 | main.log.error( self.name + ": EOF exception found" ) |
| 3272 | main.log.error( self.name + ": " + self.handle.before ) |
| 3273 | main.cleanup() |
| 3274 | main.exit() |
| 3275 | except Exception: |
| 3276 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3277 | main.cleanup() |
| 3278 | main.exit() |
| 3279 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3280 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3281 | """ |
| 3282 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
| 3283 | topic parameter and an empty list if the topic doesn't exist |
| 3284 | If no leader is elected leader in the returned list will be "none" |
| 3285 | Returns None if there is a type error processing the json object |
| 3286 | """ |
| 3287 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3288 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3289 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3290 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3291 | assert "Command not found:" not in rawOutput, rawOutput |
| 3292 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3293 | results = [] |
| 3294 | for dict in output: |
| 3295 | if dict["topic"] == topic: |
| 3296 | leader = dict["leader"] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3297 | candidates = re.split( ", ", dict["candidates"][1:-1] ) |
| 3298 | results.append( leader ) |
| 3299 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3300 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3301 | except AssertionError: |
| 3302 | main.log.exception( "" ) |
| 3303 | return None |
| 3304 | except ( TypeError, ValueError ): |
| 3305 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3306 | return None |
| 3307 | except pexpect.EOF: |
| 3308 | main.log.error( self.name + ": EOF exception found" ) |
| 3309 | main.log.error( self.name + ": " + self.handle.before ) |
| 3310 | main.cleanup() |
| 3311 | main.exit() |
| 3312 | except Exception: |
| 3313 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3314 | main.cleanup() |
| 3315 | main.exit() |
| 3316 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3317 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3318 | """ |
| 3319 | Returns the output of the intent Pending map. |
| 3320 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3321 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3322 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3323 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3324 | cmdStr += " -j" |
| 3325 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3326 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3327 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3328 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3329 | except AssertionError: |
| 3330 | main.log.exception( "" ) |
| 3331 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3332 | except TypeError: |
| 3333 | main.log.exception( self.name + ": Object not as expected" ) |
| 3334 | return None |
| 3335 | except pexpect.EOF: |
| 3336 | main.log.error( self.name + ": EOF exception found" ) |
| 3337 | main.log.error( self.name + ": " + self.handle.before ) |
| 3338 | main.cleanup() |
| 3339 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3340 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3341 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3342 | main.cleanup() |
| 3343 | main.exit() |
| 3344 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3345 | def partitions( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3346 | """ |
| 3347 | Returns the output of the raft partitions command for ONOS. |
| 3348 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3349 | # Sample JSON |
| 3350 | # { |
| 3351 | # "leader": "tcp://10.128.30.11:7238", |
| 3352 | # "members": [ |
| 3353 | # "tcp://10.128.30.11:7238", |
| 3354 | # "tcp://10.128.30.17:7238", |
| 3355 | # "tcp://10.128.30.13:7238", |
| 3356 | # ], |
| 3357 | # "name": "p1", |
| 3358 | # "term": 3 |
| 3359 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3360 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3361 | cmdStr = "onos:partitions" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3362 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3363 | cmdStr += " -j" |
| 3364 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3365 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3366 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3367 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3368 | except AssertionError: |
| 3369 | main.log.exception( "" ) |
| 3370 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3371 | except TypeError: |
| 3372 | main.log.exception( self.name + ": Object not as expected" ) |
| 3373 | return None |
| 3374 | except pexpect.EOF: |
| 3375 | main.log.error( self.name + ": EOF exception found" ) |
| 3376 | main.log.error( self.name + ": " + self.handle.before ) |
| 3377 | main.cleanup() |
| 3378 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3379 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3380 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3381 | main.cleanup() |
| 3382 | main.exit() |
| 3383 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3384 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3385 | """ |
| 3386 | Returns the output of the apps command for ONOS. This command lists |
| 3387 | information about installed ONOS applications |
| 3388 | """ |
| 3389 | # Sample JSON object |
| 3390 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
| 3391 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3392 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
| 3393 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
| 3394 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3395 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3396 | if summary: |
| 3397 | cmdStr += " -s" |
| 3398 | if active: |
| 3399 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3400 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3401 | cmdStr += " -j" |
| 3402 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3403 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3404 | assert "Command not found:" not in output, output |
| 3405 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3406 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3407 | # FIXME: look at specific exceptions/Errors |
| 3408 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3409 | main.log.exception( "Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3410 | return None |
| 3411 | except TypeError: |
| 3412 | main.log.exception( self.name + ": Object not as expected" ) |
| 3413 | return None |
| 3414 | except pexpect.EOF: |
| 3415 | main.log.error( self.name + ": EOF exception found" ) |
| 3416 | main.log.error( self.name + ": " + self.handle.before ) |
| 3417 | main.cleanup() |
| 3418 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3419 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3420 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3421 | main.cleanup() |
| 3422 | main.exit() |
| 3423 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3424 | def appStatus( self, appName ): |
| 3425 | """ |
| 3426 | Uses the onos:apps cli command to return the status of an application. |
| 3427 | Returns: |
| 3428 | "ACTIVE" - If app is installed and activated |
| 3429 | "INSTALLED" - If app is installed and deactivated |
| 3430 | "UNINSTALLED" - If app is not installed |
| 3431 | None - on error |
| 3432 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3433 | try: |
| 3434 | if not isinstance( appName, types.StringType ): |
| 3435 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3436 | " a string" ) |
| 3437 | return None |
| 3438 | output = self.apps( jsonFormat=True ) |
| 3439 | appsJson = json.loads( output ) |
| 3440 | state = None |
| 3441 | for app in appsJson: |
| 3442 | if appName == app.get('name'): |
| 3443 | state = app.get('state') |
| 3444 | break |
| 3445 | if state == "ACTIVE" or state == "INSTALLED": |
| 3446 | return state |
| 3447 | elif state is None: |
| 3448 | return "UNINSTALLED" |
| 3449 | elif state: |
| 3450 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3451 | str( state ) ) |
| 3452 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3453 | except ( TypeError, ValueError ): |
| 3454 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3455 | return None |
| 3456 | except pexpect.EOF: |
| 3457 | main.log.error( self.name + ": EOF exception found" ) |
| 3458 | main.log.error( self.name + ": " + self.handle.before ) |
| 3459 | main.cleanup() |
| 3460 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3461 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3462 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3463 | main.cleanup() |
| 3464 | main.exit() |
| 3465 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3466 | def app( self, appName, option ): |
| 3467 | """ |
| 3468 | Interacts with the app command for ONOS. This command manages |
| 3469 | application inventory. |
| 3470 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3471 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3472 | # Validate argument types |
| 3473 | valid = True |
| 3474 | if not isinstance( appName, types.StringType ): |
| 3475 | main.log.error( self.name + ".app(): appName must be a " + |
| 3476 | "string" ) |
| 3477 | valid = False |
| 3478 | if not isinstance( option, types.StringType ): |
| 3479 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3480 | valid = False |
| 3481 | if not valid: |
| 3482 | return main.FALSE |
| 3483 | # Validate Option |
| 3484 | option = option.lower() |
| 3485 | # NOTE: Install may become a valid option |
| 3486 | if option == "activate": |
| 3487 | pass |
| 3488 | elif option == "deactivate": |
| 3489 | pass |
| 3490 | elif option == "uninstall": |
| 3491 | pass |
| 3492 | else: |
| 3493 | # Invalid option |
| 3494 | main.log.error( "The ONOS app command argument only takes " + |
| 3495 | "the values: (activate|deactivate|uninstall)" + |
| 3496 | "; was given '" + option + "'") |
| 3497 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3498 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3499 | output = self.sendline( cmdStr ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3500 | if "Error executing command" in output: |
| 3501 | main.log.error( "Error in processing onos:app command: " + |
| 3502 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3503 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3504 | elif "No such application" in output: |
| 3505 | main.log.error( "The application '" + appName + |
| 3506 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3507 | return main.FALSE |
| 3508 | elif "Command not found:" in output: |
| 3509 | main.log.error( "Error in processing onos:app command: " + |
| 3510 | str( output ) ) |
| 3511 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3512 | elif "Unsupported command:" in output: |
| 3513 | main.log.error( "Incorrect command given to 'app': " + |
| 3514 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3515 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3516 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3517 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3518 | return main.TRUE |
| 3519 | except TypeError: |
| 3520 | main.log.exception( self.name + ": Object not as expected" ) |
| 3521 | return main.ERROR |
| 3522 | except pexpect.EOF: |
| 3523 | main.log.error( self.name + ": EOF exception found" ) |
| 3524 | main.log.error( self.name + ": " + self.handle.before ) |
| 3525 | main.cleanup() |
| 3526 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3527 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3528 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3529 | main.cleanup() |
| 3530 | main.exit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3531 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3532 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3533 | """ |
| 3534 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3535 | appName is the hierarchical app name, not the feature name |
| 3536 | If check is True, method will check the status of the app after the |
| 3537 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3538 | Returns main.TRUE if the command was successfully sent |
| 3539 | main.FALSE if the cli responded with an error or given |
| 3540 | incorrect input |
| 3541 | """ |
| 3542 | try: |
| 3543 | if not isinstance( appName, types.StringType ): |
| 3544 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3545 | " a string" ) |
| 3546 | return main.FALSE |
| 3547 | status = self.appStatus( appName ) |
| 3548 | if status == "INSTALLED": |
| 3549 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3550 | if check and response == main.TRUE: |
| 3551 | for i in range(10): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3552 | status = self.appStatus( appName ) |
| 3553 | if status == "ACTIVE": |
| 3554 | return main.TRUE |
| 3555 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3556 | main.log.debug( "The state of application " + |
| 3557 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3558 | time.sleep( 1 ) |
| 3559 | return main.FALSE |
| 3560 | else: # not 'check' or command didn't succeed |
| 3561 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3562 | elif status == "ACTIVE": |
| 3563 | return main.TRUE |
| 3564 | elif status == "UNINSTALLED": |
| 3565 | main.log.error( self.name + ": Tried to activate the " + |
| 3566 | "application '" + appName + "' which is not " + |
| 3567 | "installed." ) |
| 3568 | else: |
| 3569 | main.log.error( "Unexpected return value from appStatus: " + |
| 3570 | str( status ) ) |
| 3571 | return main.ERROR |
| 3572 | except TypeError: |
| 3573 | main.log.exception( self.name + ": Object not as expected" ) |
| 3574 | return main.ERROR |
| 3575 | except pexpect.EOF: |
| 3576 | main.log.error( self.name + ": EOF exception found" ) |
| 3577 | main.log.error( self.name + ": " + self.handle.before ) |
| 3578 | main.cleanup() |
| 3579 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3580 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3581 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3582 | main.cleanup() |
| 3583 | main.exit() |
| 3584 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3585 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3586 | """ |
| 3587 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3588 | appName is the hierarchical app name, not the feature name |
| 3589 | If check is True, method will check the status of the app after the |
| 3590 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3591 | Returns main.TRUE if the command was successfully sent |
| 3592 | main.FALSE if the cli responded with an error or given |
| 3593 | incorrect input |
| 3594 | """ |
| 3595 | try: |
| 3596 | if not isinstance( appName, types.StringType ): |
| 3597 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3598 | "be a string" ) |
| 3599 | return main.FALSE |
| 3600 | status = self.appStatus( appName ) |
| 3601 | if status == "INSTALLED": |
| 3602 | return main.TRUE |
| 3603 | elif status == "ACTIVE": |
| 3604 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3605 | if check and response == main.TRUE: |
| 3606 | for i in range(10): # try 10 times then give up |
| 3607 | status = self.appStatus( appName ) |
| 3608 | if status == "INSTALLED": |
| 3609 | return main.TRUE |
| 3610 | else: |
| 3611 | time.sleep( 1 ) |
| 3612 | return main.FALSE |
| 3613 | else: # not check or command didn't succeed |
| 3614 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3615 | elif status == "UNINSTALLED": |
| 3616 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3617 | "application '" + appName + "' which is not " + |
| 3618 | "installed." ) |
| 3619 | return main.TRUE |
| 3620 | else: |
| 3621 | main.log.error( "Unexpected return value from appStatus: " + |
| 3622 | str( status ) ) |
| 3623 | return main.ERROR |
| 3624 | except TypeError: |
| 3625 | main.log.exception( self.name + ": Object not as expected" ) |
| 3626 | return main.ERROR |
| 3627 | except pexpect.EOF: |
| 3628 | main.log.error( self.name + ": EOF exception found" ) |
| 3629 | main.log.error( self.name + ": " + self.handle.before ) |
| 3630 | main.cleanup() |
| 3631 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3632 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3633 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3634 | main.cleanup() |
| 3635 | main.exit() |
| 3636 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3637 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3638 | """ |
| 3639 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3640 | appName is the hierarchical app name, not the feature name |
| 3641 | If check is True, method will check the status of the app after the |
| 3642 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3643 | Returns main.TRUE if the command was successfully sent |
| 3644 | main.FALSE if the cli responded with an error or given |
| 3645 | incorrect input |
| 3646 | """ |
| 3647 | # TODO: check with Thomas about the state machine for apps |
| 3648 | try: |
| 3649 | if not isinstance( appName, types.StringType ): |
| 3650 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3651 | "be a string" ) |
| 3652 | return main.FALSE |
| 3653 | status = self.appStatus( appName ) |
| 3654 | if status == "INSTALLED": |
| 3655 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3656 | if check and response == main.TRUE: |
| 3657 | for i in range(10): # try 10 times then give up |
| 3658 | status = self.appStatus( appName ) |
| 3659 | if status == "UNINSTALLED": |
| 3660 | return main.TRUE |
| 3661 | else: |
| 3662 | time.sleep( 1 ) |
| 3663 | return main.FALSE |
| 3664 | else: # not check or command didn't succeed |
| 3665 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3666 | elif status == "ACTIVE": |
| 3667 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3668 | "application '" + appName + "' which is " + |
| 3669 | "currently active." ) |
| 3670 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3671 | if check and response == main.TRUE: |
| 3672 | for i in range(10): # try 10 times then give up |
| 3673 | status = self.appStatus( appName ) |
| 3674 | if status == "UNINSTALLED": |
| 3675 | return main.TRUE |
| 3676 | else: |
| 3677 | time.sleep( 1 ) |
| 3678 | return main.FALSE |
| 3679 | else: # not check or command didn't succeed |
| 3680 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3681 | elif status == "UNINSTALLED": |
| 3682 | return main.TRUE |
| 3683 | else: |
| 3684 | main.log.error( "Unexpected return value from appStatus: " + |
| 3685 | str( status ) ) |
| 3686 | return main.ERROR |
| 3687 | except TypeError: |
| 3688 | main.log.exception( self.name + ": Object not as expected" ) |
| 3689 | return main.ERROR |
| 3690 | except pexpect.EOF: |
| 3691 | main.log.error( self.name + ": EOF exception found" ) |
| 3692 | main.log.error( self.name + ": " + self.handle.before ) |
| 3693 | main.cleanup() |
| 3694 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3695 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3696 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3697 | main.cleanup() |
| 3698 | main.exit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3699 | |
| 3700 | def appIDs( self, jsonFormat=True ): |
| 3701 | """ |
| 3702 | Show the mappings between app id and app names given by the 'app-ids' |
| 3703 | cli command |
| 3704 | """ |
| 3705 | try: |
| 3706 | cmdStr = "app-ids" |
| 3707 | if jsonFormat: |
| 3708 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3709 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3710 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3711 | assert "Command not found:" not in output, output |
| 3712 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3713 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3714 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3715 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3716 | return None |
| 3717 | except TypeError: |
| 3718 | main.log.exception( self.name + ": Object not as expected" ) |
| 3719 | return None |
| 3720 | except pexpect.EOF: |
| 3721 | main.log.error( self.name + ": EOF exception found" ) |
| 3722 | main.log.error( self.name + ": " + self.handle.before ) |
| 3723 | main.cleanup() |
| 3724 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3725 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3726 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3727 | main.cleanup() |
| 3728 | main.exit() |
| 3729 | |
| 3730 | def appToIDCheck( self ): |
| 3731 | """ |
| 3732 | This method will check that each application's ID listed in 'apps' is |
| 3733 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3734 | there are no duplicate IDs issued. Note that an app ID should be |
| 3735 | a globaly unique numerical identifier for app/app-like features. Once |
| 3736 | an ID is registered, the ID is never freed up so that if an app is |
| 3737 | reinstalled it will have the same ID. |
| 3738 | |
| 3739 | Returns: main.TRUE if the check passes and |
| 3740 | main.FALSE if the check fails or |
| 3741 | main.ERROR if there is some error in processing the test |
| 3742 | """ |
| 3743 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3744 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3745 | rawJson = self.appIDs( jsonFormat=True ) |
| 3746 | if rawJson: |
| 3747 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3748 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3749 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3750 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3751 | rawJson = self.apps( jsonFormat=True ) |
| 3752 | if rawJson: |
| 3753 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3754 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3755 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3756 | bail = True |
| 3757 | if bail: |
| 3758 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3759 | result = main.TRUE |
| 3760 | for app in apps: |
| 3761 | appID = app.get( 'id' ) |
| 3762 | if appID is None: |
| 3763 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3764 | result = main.FALSE |
| 3765 | appName = app.get( 'name' ) |
| 3766 | if appName is None: |
| 3767 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3768 | result = main.FALSE |
| 3769 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3770 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3771 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3772 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3773 | if not current: # if ids doesn't have this id |
| 3774 | result = main.FALSE |
| 3775 | main.log.error( "'app-ids' does not have the ID for " + |
| 3776 | str( appName ) + " that apps does." ) |
| 3777 | elif len( current ) > 1: |
| 3778 | # there is more than one app with this ID |
| 3779 | result = main.FALSE |
| 3780 | # We will log this later in the method |
| 3781 | elif not current[0][ 'name' ] == appName: |
| 3782 | currentName = current[0][ 'name' ] |
| 3783 | result = main.FALSE |
| 3784 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3785 | " registered under id:" + str( appID ) + |
| 3786 | " but 'apps' has " + str( appName ) ) |
| 3787 | else: |
| 3788 | pass # id and name match! |
| 3789 | # now make sure that app-ids has no duplicates |
| 3790 | idsList = [] |
| 3791 | namesList = [] |
| 3792 | for item in ids: |
| 3793 | idsList.append( item[ 'id' ] ) |
| 3794 | namesList.append( item[ 'name' ] ) |
| 3795 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3796 | len( namesList ) != len( set( namesList ) ): |
| 3797 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3798 | + json.dumps( ids, |
| 3799 | sort_keys=True, |
| 3800 | indent=4, |
| 3801 | separators=( ',', ': ' ) ) ) |
| 3802 | result = main.FALSE |
| 3803 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3804 | except ( TypeError, ValueError ): |
| 3805 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3806 | return main.ERROR |
| 3807 | except pexpect.EOF: |
| 3808 | main.log.error( self.name + ": EOF exception found" ) |
| 3809 | main.log.error( self.name + ": " + self.handle.before ) |
| 3810 | main.cleanup() |
| 3811 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3812 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3813 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3814 | main.cleanup() |
| 3815 | main.exit() |
| 3816 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3817 | def getCfg( self, component=None, propName=None, short=False, |
| 3818 | jsonFormat=True ): |
| 3819 | """ |
| 3820 | Get configuration settings from onos cli |
| 3821 | Optional arguments: |
| 3822 | component - Optionally only list configurations for a specific |
| 3823 | component. If None, all components with configurations |
| 3824 | are displayed. Case Sensitive string. |
| 3825 | propName - If component is specified, propName option will show |
| 3826 | only this specific configuration from that component. |
| 3827 | Case Sensitive string. |
| 3828 | jsonFormat - Returns output as json. Note that this will override |
| 3829 | the short option |
| 3830 | short - Short, less verbose, version of configurations. |
| 3831 | This is overridden by the json option |
| 3832 | returns: |
| 3833 | Output from cli as a string or None on error |
| 3834 | """ |
| 3835 | try: |
| 3836 | baseStr = "cfg" |
| 3837 | cmdStr = " get" |
| 3838 | componentStr = "" |
| 3839 | if component: |
| 3840 | componentStr += " " + component |
| 3841 | if propName: |
| 3842 | componentStr += " " + propName |
| 3843 | if jsonFormat: |
| 3844 | baseStr += " -j" |
| 3845 | elif short: |
| 3846 | baseStr += " -s" |
| 3847 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3848 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3849 | assert "Command not found:" not in output, output |
| 3850 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3851 | return output |
| 3852 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3853 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3854 | return None |
| 3855 | except TypeError: |
| 3856 | main.log.exception( self.name + ": Object not as expected" ) |
| 3857 | return None |
| 3858 | except pexpect.EOF: |
| 3859 | main.log.error( self.name + ": EOF exception found" ) |
| 3860 | main.log.error( self.name + ": " + self.handle.before ) |
| 3861 | main.cleanup() |
| 3862 | main.exit() |
| 3863 | except Exception: |
| 3864 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3865 | main.cleanup() |
| 3866 | main.exit() |
| 3867 | |
| 3868 | def setCfg( self, component, propName, value=None, check=True ): |
| 3869 | """ |
| 3870 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3871 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3872 | component - The case sensitive name of the component whose |
| 3873 | property is to be set |
| 3874 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3875 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3876 | value - The value to set the property to. If None, will unset the |
| 3877 | property and revert it to it's default value(if applicable) |
| 3878 | check - Boolean, Check whether the option was successfully set this |
| 3879 | only applies when a value is given. |
| 3880 | returns: |
| 3881 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3882 | will return main.TRUE unless there is an error |
| 3883 | """ |
| 3884 | try: |
| 3885 | baseStr = "cfg" |
| 3886 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3887 | if value is not None: |
| 3888 | cmdStr += " " + str( value ) |
| 3889 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3890 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3891 | assert "Command not found:" not in output, output |
| 3892 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3893 | if value and check: |
| 3894 | results = self.getCfg( component=str( component ), |
| 3895 | propName=str( propName ), |
| 3896 | jsonFormat=True ) |
| 3897 | # Check if current value is what we just set |
| 3898 | try: |
| 3899 | jsonOutput = json.loads( results ) |
| 3900 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3901 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3902 | main.log.exception( "Error parsing cfg output" ) |
| 3903 | main.log.error( "output:" + repr( results ) ) |
| 3904 | return main.FALSE |
| 3905 | if current == str( value ): |
| 3906 | return main.TRUE |
| 3907 | return main.FALSE |
| 3908 | return main.TRUE |
| 3909 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3910 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3911 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3912 | except ( TypeError, ValueError ): |
| 3913 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3914 | return main.FALSE |
| 3915 | except pexpect.EOF: |
| 3916 | main.log.error( self.name + ": EOF exception found" ) |
| 3917 | main.log.error( self.name + ": " + self.handle.before ) |
| 3918 | main.cleanup() |
| 3919 | main.exit() |
| 3920 | except Exception: |
| 3921 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3922 | main.cleanup() |
| 3923 | main.exit() |
| 3924 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3925 | def setTestAdd( self, setName, values ): |
| 3926 | """ |
| 3927 | CLI command to add elements to a distributed set. |
| 3928 | Arguments: |
| 3929 | setName - The name of the set to add to. |
| 3930 | values - The value(s) to add to the set, space seperated. |
| 3931 | Example usages: |
| 3932 | setTestAdd( "set1", "a b c" ) |
| 3933 | setTestAdd( "set2", "1" ) |
| 3934 | returns: |
| 3935 | main.TRUE on success OR |
| 3936 | main.FALSE if elements were already in the set OR |
| 3937 | main.ERROR on error |
| 3938 | """ |
| 3939 | try: |
| 3940 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
| 3941 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3942 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3943 | assert "Command not found:" not in output, output |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3944 | try: |
| 3945 | # TODO: Maybe make this less hardcoded |
| 3946 | # ConsistentMap Exceptions |
| 3947 | assert "org.onosproject.store.service" not in output |
| 3948 | # Node not leader |
| 3949 | assert "java.lang.IllegalStateException" not in output |
| 3950 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3951 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3952 | "command: " + str( output ) ) |
| 3953 | retryTime = 30 # Conservative time, given by Madan |
| 3954 | main.log.info( "Waiting " + str( retryTime ) + |
| 3955 | "seconds before retrying." ) |
| 3956 | time.sleep( retryTime ) # Due to change in mastership |
| 3957 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3958 | assert output is not None, "Error in sendline" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3959 | assert "Error executing command" not in output |
| 3960 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3961 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
| 3962 | main.log.info( self.name + ": " + output ) |
| 3963 | if re.search( positiveMatch, output): |
| 3964 | return main.TRUE |
| 3965 | elif re.search( negativeMatch, output): |
| 3966 | return main.FALSE |
| 3967 | else: |
| 3968 | main.log.error( self.name + ": setTestAdd did not" + |
| 3969 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3970 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3971 | return main.ERROR |
| 3972 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3973 | main.log.exception( "Error in processing '" + cmdStr + "' command. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3974 | return main.ERROR |
| 3975 | except TypeError: |
| 3976 | main.log.exception( self.name + ": Object not as expected" ) |
| 3977 | return main.ERROR |
| 3978 | except pexpect.EOF: |
| 3979 | main.log.error( self.name + ": EOF exception found" ) |
| 3980 | main.log.error( self.name + ": " + self.handle.before ) |
| 3981 | main.cleanup() |
| 3982 | main.exit() |
| 3983 | except Exception: |
| 3984 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3985 | main.cleanup() |
| 3986 | main.exit() |
| 3987 | |
| 3988 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 3989 | """ |
| 3990 | CLI command to remove elements from a distributed set. |
| 3991 | Required arguments: |
| 3992 | setName - The name of the set to remove from. |
| 3993 | values - The value(s) to remove from the set, space seperated. |
| 3994 | Optional arguments: |
| 3995 | clear - Clear all elements from the set |
| 3996 | retain - Retain only the given values. (intersection of the |
| 3997 | original set and the given set) |
| 3998 | returns: |
| 3999 | main.TRUE on success OR |
| 4000 | main.FALSE if the set was not changed OR |
| 4001 | main.ERROR on error |
| 4002 | """ |
| 4003 | try: |
| 4004 | cmdStr = "set-test-remove " |
| 4005 | if clear: |
| 4006 | cmdStr += "-c " + str( setName ) |
| 4007 | elif retain: |
| 4008 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4009 | else: |
| 4010 | cmdStr += str( setName ) + " " + str( values ) |
| 4011 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4012 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4013 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4014 | # TODO: Maybe make this less hardcoded |
| 4015 | # ConsistentMap Exceptions |
| 4016 | assert "org.onosproject.store.service" not in output |
| 4017 | # Node not leader |
| 4018 | assert "java.lang.IllegalStateException" not in output |
| 4019 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4020 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4021 | "command: " + str( output ) ) |
| 4022 | retryTime = 30 # Conservative time, given by Madan |
| 4023 | main.log.info( "Waiting " + str( retryTime ) + |
| 4024 | "seconds before retrying." ) |
| 4025 | time.sleep( retryTime ) # Due to change in mastership |
| 4026 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4027 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4028 | assert "Command not found:" not in output, output |
| 4029 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4030 | main.log.info( self.name + ": " + output ) |
| 4031 | if clear: |
| 4032 | pattern = "Set " + str( setName ) + " cleared" |
| 4033 | if re.search( pattern, output ): |
| 4034 | return main.TRUE |
| 4035 | elif retain: |
| 4036 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4037 | "only elements of set \[(.*)\]" |
| 4038 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4039 | "retaining only elements of the set " +\ |
| 4040 | "\[(.*)\]" |
| 4041 | if re.search( positivePattern, output ): |
| 4042 | return main.TRUE |
| 4043 | elif re.search( negativePattern, output ): |
| 4044 | return main.FALSE |
| 4045 | else: |
| 4046 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4047 | str( setName ) |
| 4048 | if ( len( values.split() ) == 1 ): |
| 4049 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4050 | str( setName ) |
| 4051 | else: |
| 4052 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4053 | str( setName ) |
| 4054 | if re.search( positivePattern, output ): |
| 4055 | return main.TRUE |
| 4056 | elif re.search( negativePattern, output ): |
| 4057 | return main.FALSE |
| 4058 | main.log.error( self.name + ": setTestRemove did not" + |
| 4059 | " match expected output" ) |
| 4060 | main.log.debug( self.name + " expected: " + pattern ) |
| 4061 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4062 | return main.ERROR |
| 4063 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4064 | main.log.exception( "Error in processing '" + cmdStr + "' commandr. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4065 | return main.ERROR |
| 4066 | except TypeError: |
| 4067 | main.log.exception( self.name + ": Object not as expected" ) |
| 4068 | return main.ERROR |
| 4069 | except pexpect.EOF: |
| 4070 | main.log.error( self.name + ": EOF exception found" ) |
| 4071 | main.log.error( self.name + ": " + self.handle.before ) |
| 4072 | main.cleanup() |
| 4073 | main.exit() |
| 4074 | except Exception: |
| 4075 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4076 | main.cleanup() |
| 4077 | main.exit() |
| 4078 | |
| 4079 | def setTestGet( self, setName, values="" ): |
| 4080 | """ |
| 4081 | CLI command to get the elements in a distributed set. |
| 4082 | Required arguments: |
| 4083 | setName - The name of the set to remove from. |
| 4084 | Optional arguments: |
| 4085 | values - The value(s) to check if in the set, space seperated. |
| 4086 | returns: |
| 4087 | main.ERROR on error OR |
| 4088 | A list of elements in the set if no optional arguments are |
| 4089 | supplied OR |
| 4090 | A tuple containing the list then: |
| 4091 | main.FALSE if the given values are not in the set OR |
| 4092 | main.TRUE if the given values are in the set OR |
| 4093 | """ |
| 4094 | try: |
| 4095 | values = str( values ).strip() |
| 4096 | setName = str( setName ).strip() |
| 4097 | length = len( values.split() ) |
| 4098 | containsCheck = None |
| 4099 | # Patterns to match |
| 4100 | setPattern = "\[(.*)\]" |
| 4101 | pattern = "Items in set " + setName + ":\n" + setPattern |
| 4102 | containsTrue = "Set " + setName + " contains the value " + values |
| 4103 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4104 | values |
| 4105 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4106 | setPattern |
| 4107 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4108 | " subset " + setPattern |
| 4109 | |
| 4110 | cmdStr = "set-test-get " |
| 4111 | cmdStr += setName + " " + values |
| 4112 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4113 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4114 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4115 | # TODO: Maybe make this less hardcoded |
| 4116 | # ConsistentMap Exceptions |
| 4117 | assert "org.onosproject.store.service" not in output |
| 4118 | # Node not leader |
| 4119 | assert "java.lang.IllegalStateException" not in output |
| 4120 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4121 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4122 | "command: " + str( output ) ) |
| 4123 | retryTime = 30 # Conservative time, given by Madan |
| 4124 | main.log.info( "Waiting " + str( retryTime ) + |
| 4125 | "seconds before retrying." ) |
| 4126 | time.sleep( retryTime ) # Due to change in mastership |
| 4127 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4128 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4129 | assert "Command not found:" not in output, output |
| 4130 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4131 | main.log.info( self.name + ": " + output ) |
| 4132 | |
| 4133 | if length == 0: |
| 4134 | match = re.search( pattern, output ) |
| 4135 | else: # if given values |
| 4136 | if length == 1: # Contains output |
| 4137 | patternTrue = pattern + "\n" + containsTrue |
| 4138 | patternFalse = pattern + "\n" + containsFalse |
| 4139 | else: # ContainsAll output |
| 4140 | patternTrue = pattern + "\n" + containsAllTrue |
| 4141 | patternFalse = pattern + "\n" + containsAllFalse |
| 4142 | matchTrue = re.search( patternTrue, output ) |
| 4143 | matchFalse = re.search( patternFalse, output ) |
| 4144 | if matchTrue: |
| 4145 | containsCheck = main.TRUE |
| 4146 | match = matchTrue |
| 4147 | elif matchFalse: |
| 4148 | containsCheck = main.FALSE |
| 4149 | match = matchFalse |
| 4150 | else: |
| 4151 | main.log.error( self.name + " setTestGet did not match " +\ |
| 4152 | "expected output" ) |
| 4153 | main.log.debug( self.name + " expected: " + pattern ) |
| 4154 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4155 | match = None |
| 4156 | if match: |
| 4157 | setMatch = match.group( 1 ) |
| 4158 | if setMatch == '': |
| 4159 | setList = [] |
| 4160 | else: |
| 4161 | setList = setMatch.split( ", " ) |
| 4162 | if length > 0: |
| 4163 | return ( setList, containsCheck ) |
| 4164 | else: |
| 4165 | return setList |
| 4166 | else: # no match |
| 4167 | main.log.error( self.name + ": setTestGet did not" + |
| 4168 | " match expected output" ) |
| 4169 | main.log.debug( self.name + " expected: " + pattern ) |
| 4170 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4171 | return main.ERROR |
| 4172 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4173 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4174 | return main.ERROR |
| 4175 | except TypeError: |
| 4176 | main.log.exception( self.name + ": Object not as expected" ) |
| 4177 | return main.ERROR |
| 4178 | except pexpect.EOF: |
| 4179 | main.log.error( self.name + ": EOF exception found" ) |
| 4180 | main.log.error( self.name + ": " + self.handle.before ) |
| 4181 | main.cleanup() |
| 4182 | main.exit() |
| 4183 | except Exception: |
| 4184 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4185 | main.cleanup() |
| 4186 | main.exit() |
| 4187 | |
| 4188 | def setTestSize( self, setName ): |
| 4189 | """ |
| 4190 | CLI command to get the elements in a distributed set. |
| 4191 | Required arguments: |
| 4192 | setName - The name of the set to remove from. |
| 4193 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4194 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4195 | None on error |
| 4196 | """ |
| 4197 | try: |
| 4198 | # TODO: Should this check against the number of elements returned |
| 4199 | # and then return true/false based on that? |
| 4200 | setName = str( setName ).strip() |
| 4201 | # Patterns to match |
| 4202 | setPattern = "\[(.*)\]" |
| 4203 | pattern = "There are (\d+) items in set " + setName + ":\n" +\ |
| 4204 | setPattern |
| 4205 | cmdStr = "set-test-get -s " |
| 4206 | cmdStr += setName |
| 4207 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4208 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4209 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4210 | # TODO: Maybe make this less hardcoded |
| 4211 | # ConsistentMap Exceptions |
| 4212 | assert "org.onosproject.store.service" not in output |
| 4213 | # Node not leader |
| 4214 | assert "java.lang.IllegalStateException" not in output |
| 4215 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4216 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4217 | "command: " + str( output ) ) |
| 4218 | retryTime = 30 # Conservative time, given by Madan |
| 4219 | main.log.info( "Waiting " + str( retryTime ) + |
| 4220 | "seconds before retrying." ) |
| 4221 | time.sleep( retryTime ) # Due to change in mastership |
| 4222 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4223 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4224 | assert "Command not found:" not in output, output |
| 4225 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4226 | main.log.info( self.name + ": " + output ) |
| 4227 | match = re.search( pattern, output ) |
| 4228 | if match: |
| 4229 | setSize = int( match.group( 1 ) ) |
| 4230 | setMatch = match.group( 2 ) |
| 4231 | if len( setMatch.split() ) == setSize: |
| 4232 | main.log.info( "The size returned by " + self.name + |
| 4233 | " matches the number of elements in " + |
| 4234 | "the returned set" ) |
| 4235 | else: |
| 4236 | main.log.error( "The size returned by " + self.name + |
| 4237 | " does not match the number of " + |
| 4238 | "elements in the returned set." ) |
| 4239 | return setSize |
| 4240 | else: # no match |
| 4241 | main.log.error( self.name + ": setTestGet did not" + |
| 4242 | " match expected output" ) |
| 4243 | main.log.debug( self.name + " expected: " + pattern ) |
| 4244 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4245 | return None |
| 4246 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4247 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 4248 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4249 | except TypeError: |
| 4250 | main.log.exception( self.name + ": Object not as expected" ) |
| 4251 | return None |
| 4252 | except pexpect.EOF: |
| 4253 | main.log.error( self.name + ": EOF exception found" ) |
| 4254 | main.log.error( self.name + ": " + self.handle.before ) |
| 4255 | main.cleanup() |
| 4256 | main.exit() |
| 4257 | except Exception: |
| 4258 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4259 | main.cleanup() |
| 4260 | main.exit() |
| 4261 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4262 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4263 | """ |
| 4264 | Command to list the various counters in the system. |
| 4265 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4266 | if jsonFormat, a string of the json object returned by the cli |
| 4267 | command |
| 4268 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4269 | None on error |
| 4270 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4271 | try: |
| 4272 | counters = {} |
| 4273 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4274 | if jsonFormat: |
| 4275 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4276 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4277 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4278 | assert "Command not found:" not in output, output |
| 4279 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4280 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4281 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4282 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4283 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4284 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4285 | except TypeError: |
| 4286 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4287 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4288 | except pexpect.EOF: |
| 4289 | main.log.error( self.name + ": EOF exception found" ) |
| 4290 | main.log.error( self.name + ": " + self.handle.before ) |
| 4291 | main.cleanup() |
| 4292 | main.exit() |
| 4293 | except Exception: |
| 4294 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4295 | main.cleanup() |
| 4296 | main.exit() |
| 4297 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4298 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4299 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4300 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4301 | Required arguments: |
| 4302 | counter - The name of the counter to increment. |
| 4303 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4304 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4305 | returns: |
| 4306 | integer value of the counter or |
| 4307 | None on Error |
| 4308 | """ |
| 4309 | try: |
| 4310 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4311 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4312 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4313 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4314 | if delta != 1: |
| 4315 | cmdStr += " " + str( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4316 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4317 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4318 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4319 | # TODO: Maybe make this less hardcoded |
| 4320 | # ConsistentMap Exceptions |
| 4321 | assert "org.onosproject.store.service" not in output |
| 4322 | # Node not leader |
| 4323 | assert "java.lang.IllegalStateException" not in output |
| 4324 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4325 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4326 | "command: " + str( output ) ) |
| 4327 | retryTime = 30 # Conservative time, given by Madan |
| 4328 | main.log.info( "Waiting " + str( retryTime ) + |
| 4329 | "seconds before retrying." ) |
| 4330 | time.sleep( retryTime ) # Due to change in mastership |
| 4331 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4332 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4333 | assert "Command not found:" not in output, output |
| 4334 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4335 | main.log.info( self.name + ": " + output ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4336 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4337 | match = re.search( pattern, output ) |
| 4338 | if match: |
| 4339 | return int( match.group( 1 ) ) |
| 4340 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4341 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4342 | " match expected output." ) |
| 4343 | main.log.debug( self.name + " expected: " + pattern ) |
| 4344 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4345 | return None |
| 4346 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4347 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4348 | return None |
| 4349 | except TypeError: |
| 4350 | main.log.exception( self.name + ": Object not as expected" ) |
| 4351 | return None |
| 4352 | except pexpect.EOF: |
| 4353 | main.log.error( self.name + ": EOF exception found" ) |
| 4354 | main.log.error( self.name + ": " + self.handle.before ) |
| 4355 | main.cleanup() |
| 4356 | main.exit() |
| 4357 | except Exception: |
| 4358 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4359 | main.cleanup() |
| 4360 | main.exit() |
| 4361 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4362 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4363 | """ |
| 4364 | CLI command to get a distributed counter then add a delta to it. |
| 4365 | Required arguments: |
| 4366 | counter - The name of the counter to increment. |
| 4367 | Optional arguments: |
| 4368 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4369 | returns: |
| 4370 | integer value of the counter or |
| 4371 | None on Error |
| 4372 | """ |
| 4373 | try: |
| 4374 | counter = str( counter ) |
| 4375 | delta = int( delta ) |
| 4376 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4377 | cmdStr += counter |
| 4378 | if delta != 1: |
| 4379 | cmdStr += " " + str( delta ) |
| 4380 | output = self.sendline( cmdStr ) |
| 4381 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4382 | assert output is not None, "Error in sendline" |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4383 | # TODO: Maybe make this less hardcoded |
| 4384 | # ConsistentMap Exceptions |
| 4385 | assert "org.onosproject.store.service" not in output |
| 4386 | # Node not leader |
| 4387 | assert "java.lang.IllegalStateException" not in output |
| 4388 | except AssertionError: |
| 4389 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4390 | "command: " + str( output ) ) |
| 4391 | retryTime = 30 # Conservative time, given by Madan |
| 4392 | main.log.info( "Waiting " + str( retryTime ) + |
| 4393 | "seconds before retrying." ) |
| 4394 | time.sleep( retryTime ) # Due to change in mastership |
| 4395 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4396 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4397 | assert "Command not found:" not in output, output |
| 4398 | assert "Error executing command" not in output, output |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4399 | main.log.info( self.name + ": " + output ) |
| 4400 | pattern = counter + " was updated to (-?\d+)" |
| 4401 | match = re.search( pattern, output ) |
| 4402 | if match: |
| 4403 | return int( match.group( 1 ) ) |
| 4404 | else: |
| 4405 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4406 | " match expected output." ) |
| 4407 | main.log.debug( self.name + " expected: " + pattern ) |
| 4408 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4409 | return None |
| 4410 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4411 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4412 | return None |
| 4413 | except TypeError: |
| 4414 | main.log.exception( self.name + ": Object not as expected" ) |
| 4415 | return None |
| 4416 | except pexpect.EOF: |
| 4417 | main.log.error( self.name + ": EOF exception found" ) |
| 4418 | main.log.error( self.name + ": " + self.handle.before ) |
| 4419 | main.cleanup() |
| 4420 | main.exit() |
| 4421 | except Exception: |
| 4422 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4423 | main.cleanup() |
| 4424 | main.exit() |
| 4425 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4426 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4427 | """ |
| 4428 | Description: Execute summary command in onos |
| 4429 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4430 | no output |
| 4431 | |
| 4432 | """ |
| 4433 | try: |
| 4434 | cmdStr = "summary" |
| 4435 | if jsonFormat: |
| 4436 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4437 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4438 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4439 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4440 | assert "Error:" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4441 | if not handle: |
| 4442 | main.log.error( self.name + ": There is no output in " + |
| 4443 | "summary command" ) |
| 4444 | return main.FALSE |
| 4445 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4446 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4447 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4448 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4449 | except TypeError: |
| 4450 | main.log.exception( self.name + ": Object not as expected" ) |
| 4451 | return None |
| 4452 | except pexpect.EOF: |
| 4453 | main.log.error( self.name + ": EOF exception found" ) |
| 4454 | main.log.error( self.name + ": " + self.handle.before ) |
| 4455 | main.cleanup() |
| 4456 | main.exit() |
| 4457 | except Exception: |
| 4458 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4459 | main.cleanup() |
| 4460 | main.exit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4461 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4462 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4463 | """ |
| 4464 | CLI command to get the value of a key in a consistent map using |
| 4465 | transactions. This a test function and can only get keys from the |
| 4466 | test map hard coded into the cli command |
| 4467 | Required arguments: |
| 4468 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4469 | returns: |
| 4470 | The string value of the key or |
| 4471 | None on Error |
| 4472 | """ |
| 4473 | try: |
| 4474 | keyName = str( keyName ) |
| 4475 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4476 | cmdStr += keyName |
| 4477 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4478 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4479 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4480 | try: |
| 4481 | # TODO: Maybe make this less hardcoded |
| 4482 | # ConsistentMap Exceptions |
| 4483 | assert "org.onosproject.store.service" not in output |
| 4484 | # Node not leader |
| 4485 | assert "java.lang.IllegalStateException" not in output |
| 4486 | except AssertionError: |
| 4487 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4488 | "command: " + str( output ) ) |
| 4489 | return None |
| 4490 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4491 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4492 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4493 | return None |
| 4494 | else: |
| 4495 | match = re.search( pattern, output ) |
| 4496 | if match: |
| 4497 | return match.groupdict()[ 'value' ] |
| 4498 | else: |
| 4499 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4500 | " match expected output." ) |
| 4501 | main.log.debug( self.name + " expected: " + pattern ) |
| 4502 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4503 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4504 | except AssertionError: |
| 4505 | main.log.exception( "" ) |
| 4506 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4507 | except TypeError: |
| 4508 | main.log.exception( self.name + ": Object not as expected" ) |
| 4509 | return None |
| 4510 | except pexpect.EOF: |
| 4511 | main.log.error( self.name + ": EOF exception found" ) |
| 4512 | main.log.error( self.name + ": " + self.handle.before ) |
| 4513 | main.cleanup() |
| 4514 | main.exit() |
| 4515 | except Exception: |
| 4516 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4517 | main.cleanup() |
| 4518 | main.exit() |
| 4519 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4520 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4521 | """ |
| 4522 | CLI command to put a value into 'numKeys' number of keys in a |
| 4523 | consistent map using transactions. This a test function and can only |
| 4524 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4525 | Required arguments: |
| 4526 | numKeys - Number of keys to add the value to |
| 4527 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4528 | returns: |
| 4529 | A dictionary whose keys are the name of the keys put into the map |
| 4530 | and the values of the keys are dictionaries whose key-values are |
| 4531 | 'value': value put into map and optionaly |
| 4532 | 'oldValue': Previous value in the key or |
| 4533 | None on Error |
| 4534 | |
| 4535 | Example output |
| 4536 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4537 | 'Key2': {'value': 'Testing'} } |
| 4538 | """ |
| 4539 | try: |
| 4540 | numKeys = str( numKeys ) |
| 4541 | value = str( value ) |
| 4542 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4543 | cmdStr += numKeys + " " + value |
| 4544 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4545 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4546 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4547 | try: |
| 4548 | # TODO: Maybe make this less hardcoded |
| 4549 | # ConsistentMap Exceptions |
| 4550 | assert "org.onosproject.store.service" not in output |
| 4551 | # Node not leader |
| 4552 | assert "java.lang.IllegalStateException" not in output |
| 4553 | except AssertionError: |
| 4554 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4555 | "command: " + str( output ) ) |
| 4556 | return None |
| 4557 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4558 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4559 | results = {} |
| 4560 | for line in output.splitlines(): |
| 4561 | new = re.search( newPattern, line ) |
| 4562 | updated = re.search( updatedPattern, line ) |
| 4563 | if new: |
| 4564 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4565 | elif updated: |
| 4566 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4567 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4568 | else: |
| 4569 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4570 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4571 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4572 | newPattern, |
| 4573 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4574 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4575 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4576 | except AssertionError: |
| 4577 | main.log.exception( "" ) |
| 4578 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4579 | except TypeError: |
| 4580 | main.log.exception( self.name + ": Object not as expected" ) |
| 4581 | return None |
| 4582 | except pexpect.EOF: |
| 4583 | main.log.error( self.name + ": EOF exception found" ) |
| 4584 | main.log.error( self.name + ": " + self.handle.before ) |
| 4585 | main.cleanup() |
| 4586 | main.exit() |
| 4587 | except Exception: |
| 4588 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4589 | main.cleanup() |
| 4590 | main.exit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4591 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4592 | def maps( self, jsonFormat=True ): |
| 4593 | """ |
| 4594 | Description: Returns result of onos:maps |
| 4595 | Optional: |
| 4596 | * jsonFormat: enable json formatting of output |
| 4597 | """ |
| 4598 | try: |
| 4599 | cmdStr = "maps" |
| 4600 | if jsonFormat: |
| 4601 | cmdStr += " -j" |
| 4602 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4603 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4604 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4605 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4606 | except AssertionError: |
| 4607 | main.log.exception( "" ) |
| 4608 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4609 | except TypeError: |
| 4610 | main.log.exception( self.name + ": Object not as expected" ) |
| 4611 | return None |
| 4612 | except pexpect.EOF: |
| 4613 | main.log.error( self.name + ": EOF exception found" ) |
| 4614 | main.log.error( self.name + ": " + self.handle.before ) |
| 4615 | main.cleanup() |
| 4616 | main.exit() |
| 4617 | except Exception: |
| 4618 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4619 | main.cleanup() |
| 4620 | main.exit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4621 | |
| 4622 | def getSwController( self, uri, jsonFormat=True ): |
| 4623 | """ |
| 4624 | Descrition: Gets the controller information from the device |
| 4625 | """ |
| 4626 | try: |
| 4627 | cmd = "device-controllers " |
| 4628 | if jsonFormat: |
| 4629 | cmd += "-j " |
| 4630 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4631 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4632 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4633 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4634 | except AssertionError: |
| 4635 | main.log.exception( "" ) |
| 4636 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4637 | except TypeError: |
| 4638 | main.log.exception( self.name + ": Object not as expected" ) |
| 4639 | return None |
| 4640 | except pexpect.EOF: |
| 4641 | main.log.error( self.name + ": EOF exception found" ) |
| 4642 | main.log.error( self.name + ": " + self.handle.before ) |
| 4643 | main.cleanup() |
| 4644 | main.exit() |
| 4645 | except Exception: |
| 4646 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4647 | main.cleanup() |
| 4648 | main.exit() |
| 4649 | |
| 4650 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4651 | """ |
| 4652 | Descrition: sets the controller(s) for the specified device |
| 4653 | |
| 4654 | Parameters: |
| 4655 | Required: uri - String: The uri of the device(switch). |
| 4656 | ip - String or List: The ip address of the controller. |
| 4657 | This parameter can be formed in a couple of different ways. |
| 4658 | VALID: |
| 4659 | 10.0.0.1 - just the ip address |
| 4660 | tcp:10.0.0.1 - the protocol and the ip address |
| 4661 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4662 | so that you can add controllers with different |
| 4663 | protocols and ports |
| 4664 | INVALID: |
| 4665 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4666 | |
| 4667 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4668 | port - The port number. |
| 4669 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4670 | |
| 4671 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4672 | """ |
| 4673 | try: |
| 4674 | cmd = "device-setcontrollers" |
| 4675 | |
| 4676 | if jsonFormat: |
| 4677 | cmd += " -j" |
| 4678 | cmd += " " + uri |
| 4679 | if isinstance( ip, str ): |
| 4680 | ip = [ip] |
| 4681 | for item in ip: |
| 4682 | if ":" in item: |
| 4683 | sitem = item.split( ":" ) |
| 4684 | if len(sitem) == 3: |
| 4685 | cmd += " " + item |
| 4686 | elif "." in sitem[1]: |
| 4687 | cmd += " {}:{}".format(item, port) |
| 4688 | else: |
| 4689 | main.log.error( "Malformed entry: " + item ) |
| 4690 | raise TypeError |
| 4691 | else: |
| 4692 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4693 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4694 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4695 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4696 | if "Error" in response: |
| 4697 | main.log.error( response ) |
| 4698 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4699 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4700 | except AssertionError: |
| 4701 | main.log.exception( "" ) |
| 4702 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4703 | except TypeError: |
| 4704 | main.log.exception( self.name + ": Object not as expected" ) |
| 4705 | return main.FALSE |
| 4706 | except pexpect.EOF: |
| 4707 | main.log.error( self.name + ": EOF exception found" ) |
| 4708 | main.log.error( self.name + ": " + self.handle.before ) |
| 4709 | main.cleanup() |
| 4710 | main.exit() |
| 4711 | except Exception: |
| 4712 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4713 | main.cleanup() |
| 4714 | main.exit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4715 | |
| 4716 | def removeDevice( self, device ): |
| 4717 | ''' |
| 4718 | Description: |
| 4719 | Remove a device from ONOS by passing the uri of the device(s). |
| 4720 | Parameters: |
| 4721 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
| 4722 | Returns: |
| 4723 | Returns main.FALSE if an exception is thrown or an error is present |
| 4724 | in the response. Otherwise, returns main.TRUE. |
| 4725 | NOTE: |
| 4726 | If a host cannot be removed, then this function will return main.FALSE |
| 4727 | ''' |
| 4728 | try: |
| 4729 | if type( device ) is str: |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 4730 | deviceStr = device |
| 4731 | device = [] |
| 4732 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4733 | |
| 4734 | for d in device: |
| 4735 | time.sleep( 1 ) |
| 4736 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4737 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4738 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4739 | if "Error" in response: |
| 4740 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4741 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4742 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4743 | except AssertionError: |
| 4744 | main.log.exception( "" ) |
| 4745 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4746 | except TypeError: |
| 4747 | main.log.exception( self.name + ": Object not as expected" ) |
| 4748 | return main.FALSE |
| 4749 | except pexpect.EOF: |
| 4750 | main.log.error( self.name + ": EOF exception found" ) |
| 4751 | main.log.error( self.name + ": " + self.handle.before ) |
| 4752 | main.cleanup() |
| 4753 | main.exit() |
| 4754 | except Exception: |
| 4755 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4756 | main.cleanup() |
| 4757 | main.exit() |
| 4758 | |
| 4759 | def removeHost( self, host ): |
| 4760 | ''' |
| 4761 | Description: |
| 4762 | Remove a host from ONOS by passing the id of the host(s) |
| 4763 | Parameters: |
| 4764 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
| 4765 | Returns: |
| 4766 | Returns main.FALSE if an exception is thrown or an error is present |
| 4767 | in the response. Otherwise, returns main.TRUE. |
| 4768 | NOTE: |
| 4769 | If a host cannot be removed, then this function will return main.FALSE |
| 4770 | ''' |
| 4771 | try: |
| 4772 | if type( host ) is str: |
| 4773 | host = list( host ) |
| 4774 | |
| 4775 | for h in host: |
| 4776 | time.sleep( 1 ) |
| 4777 | response = self.sendline( "host-remove {}".format( h ) ) |
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 | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4780 | if "Error" in response: |
| 4781 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 4782 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -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 | 20fc652 | 2015-12-23 23:26:57 -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() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4799 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4800 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4801 | ''' |
| 4802 | Description: |
| 4803 | Bring link down or up in the null-provider. |
| 4804 | params: |
| 4805 | begin - (string) One end of a device or switch. |
| 4806 | end - (string) the other end of the device or switch |
| 4807 | returns: |
| 4808 | main.TRUE if no exceptions were thrown and no Errors are |
| 4809 | present in the resoponse. Otherwise, returns main.FALSE |
| 4810 | ''' |
| 4811 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4812 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4813 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4814 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4815 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4816 | if "Error" in response or "Failure" in response: |
| 4817 | main.log.error( response ) |
| 4818 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4819 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4820 | except AssertionError: |
| 4821 | main.log.exception( "" ) |
| 4822 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4823 | except TypeError: |
| 4824 | main.log.exception( self.name + ": Object not as expected" ) |
| 4825 | return main.FALSE |
| 4826 | except pexpect.EOF: |
| 4827 | main.log.error( self.name + ": EOF exception found" ) |
| 4828 | main.log.error( self.name + ": " + self.handle.before ) |
| 4829 | main.cleanup() |
| 4830 | main.exit() |
| 4831 | except Exception: |
| 4832 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4833 | main.cleanup() |
| 4834 | main.exit() |
| 4835 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4836 | def portstate(self, dpid='of:0000000000000102', port='2', state='enable'): |
| 4837 | ''' |
| 4838 | Description: |
| 4839 | Changes the state of port in an OF switch by means of the |
| 4840 | PORTSTATUS OF messages. |
| 4841 | params: |
| 4842 | dpid - (string) Datapath ID of the device |
| 4843 | port - (string) target port in the device |
| 4844 | state - (string) target state (enable or disabled) |
| 4845 | returns: |
| 4846 | main.TRUE if no exceptions were thrown and no Errors are |
| 4847 | present in the resoponse. Otherwise, returns main.FALSE |
| 4848 | ''' |
| 4849 | try: |
| 4850 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
| 4851 | response = self.sendline( cmd, showResponse=True ) |
| 4852 | assert response is not None, "Error in sendline" |
| 4853 | assert "Command not found:" not in response, response |
| 4854 | if "Error" in response or "Failure" in response: |
| 4855 | main.log.error( response ) |
| 4856 | return main.FALSE |
| 4857 | return main.TRUE |
| 4858 | except AssertionError: |
| 4859 | main.log.exception( "" ) |
| 4860 | return None |
| 4861 | except TypeError: |
| 4862 | main.log.exception( self.name + ": Object not as expected" ) |
| 4863 | return main.FALSE |
| 4864 | except pexpect.EOF: |
| 4865 | main.log.error( self.name + ": EOF exception found" ) |
| 4866 | main.log.error( self.name + ": " + self.handle.before ) |
| 4867 | main.cleanup() |
| 4868 | main.exit() |
| 4869 | except Exception: |
| 4870 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4871 | main.cleanup() |
| 4872 | main.exit() |
| 4873 | |
| 4874 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 4875 | """ |
| 4876 | Set the logging level to lvl for a specific app |
| 4877 | returns main.TRUE on success |
| 4878 | returns main.FALSE if Error occurred |
| 4879 | if noExit is True, TestON will not exit, but clean up |
| 4880 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 4881 | Level defaults to INFO |
| 4882 | """ |
| 4883 | try: |
| 4884 | self.handle.sendline( "log:set %s %s" %( level, app ) ) |
| 4885 | self.handle.expect( "onos>" ) |
| 4886 | |
| 4887 | response = self.handle.before |
| 4888 | if re.search( "Error", response ): |
| 4889 | return main.FALSE |
| 4890 | return main.TRUE |
| 4891 | except pexpect.TIMEOUT: |
| 4892 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 4893 | main.cleanup() |
| 4894 | main.exit() |
| 4895 | except pexpect.EOF: |
| 4896 | main.log.error( self.name + ": EOF exception found" ) |
| 4897 | main.log.error( self.name + ": " + self.handle.before ) |
| 4898 | main.cleanup() |
| 4899 | main.exit() |
| 4900 | except Exception: |
| 4901 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4902 | main.cleanup() |
| 4903 | main.exit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4904 | |
| 4905 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 4906 | """ |
| 4907 | Return a dictionary which describes the latest network topology data as a |
| 4908 | graph. |
| 4909 | An example of the dictionary: |
| 4910 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 4911 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 4912 | Each vertex should at least have an 'edges' attribute which describes the |
| 4913 | adjacency information. The value of 'edges' attribute is also represented by |
| 4914 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 4915 | list of attributes. |
| 4916 | An example of the edges dictionary: |
| 4917 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 4918 | vertex3: { 'port': ..., 'weight': ... } } |
| 4919 | If includeHost == True, all hosts (and host-switch links) will be included |
| 4920 | in topology data. |
| 4921 | """ |
| 4922 | graphDict = {} |
| 4923 | try: |
| 4924 | links = self.links() |
| 4925 | links = json.loads( links ) |
| 4926 | devices = self.devices() |
| 4927 | devices = json.loads( devices ) |
| 4928 | idToDevice = {} |
| 4929 | for device in devices: |
| 4930 | idToDevice[ device[ 'id' ] ] = device |
| 4931 | if includeHost: |
| 4932 | hosts = self.hosts() |
| 4933 | # FIXME: support 'includeHost' argument |
| 4934 | for link in links: |
| 4935 | nodeA = link[ 'src' ][ 'device' ] |
| 4936 | nodeB = link[ 'dst' ][ 'device' ] |
| 4937 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
| 4938 | if not nodeA in graphDict.keys(): |
| 4939 | graphDict[ nodeA ] = { 'edges':{}, |
| 4940 | 'dpid':idToDevice[ nodeA ][ 'id' ][3:], |
| 4941 | 'type':idToDevice[ nodeA ][ 'type' ], |
| 4942 | 'available':idToDevice[ nodeA ][ 'available' ], |
| 4943 | 'role':idToDevice[ nodeA ][ 'role' ], |
| 4944 | 'mfr':idToDevice[ nodeA ][ 'mfr' ], |
| 4945 | 'hw':idToDevice[ nodeA ][ 'hw' ], |
| 4946 | 'sw':idToDevice[ nodeA ][ 'sw' ], |
| 4947 | 'serial':idToDevice[ nodeA ][ 'serial' ], |
| 4948 | 'chassisId':idToDevice[ nodeA ][ 'chassisId' ], |
| 4949 | 'annotations':idToDevice[ nodeA ][ 'annotations' ]} |
| 4950 | else: |
| 4951 | # Assert nodeB is not connected to any current links of nodeA |
| 4952 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 4953 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port':link[ 'src' ][ 'port' ], |
| 4954 | 'type':link[ 'type' ], |
| 4955 | 'state':link[ 'state' ] } |
| 4956 | return graphDict |
| 4957 | except ( TypeError, ValueError ): |
| 4958 | main.log.exception( self.name + ": Object not as expected" ) |
| 4959 | return None |
| 4960 | except KeyError: |
| 4961 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4962 | return None |
| 4963 | except AssertionError: |
| 4964 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4965 | return None |
| 4966 | except pexpect.EOF: |
| 4967 | main.log.error( self.name + ": EOF exception found" ) |
| 4968 | main.log.error( self.name + ": " + self.handle.before ) |
| 4969 | return None |
| 4970 | except Exception: |
| 4971 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4972 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 4973 | |
| 4974 | def getIntentPerfSummary( self ): |
| 4975 | ''' |
| 4976 | Send command to check intent-perf summary |
| 4977 | Returns: dictionary for intent-perf summary |
| 4978 | if something wrong, function will return None |
| 4979 | ''' |
| 4980 | cmd = "intent-perf -s" |
| 4981 | respDic = {} |
| 4982 | resp = self.sendline( cmd ) |
| 4983 | try: |
| 4984 | # Generate the dictionary to return |
| 4985 | for l in resp.split( "\n" ): |
| 4986 | # Delete any white space in line |
| 4987 | temp = re.sub( r'\s+', '', l ) |
| 4988 | temp = temp.split( ":" ) |
| 4989 | respDic[ temp[0] ] = temp[ 1 ] |
| 4990 | |
| 4991 | except (TypeError, ValueError): |
| 4992 | main.log.exception( self.name + ": Object not as expected" ) |
| 4993 | return None |
| 4994 | except KeyError: |
| 4995 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4996 | return None |
| 4997 | except AssertionError: |
| 4998 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4999 | return None |
| 5000 | except pexpect.EOF: |
| 5001 | main.log.error( self.name + ": EOF exception found" ) |
| 5002 | main.log.error( self.name + ": " + self.handle.before ) |
| 5003 | return None |
| 5004 | except Exception: |
| 5005 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5006 | return None |
| 5007 | return respDic |
| 5008 | |
| 5009 | |