andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 4 | This driver enters the onos> prompt to issue commands. |
| 5 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 6 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 7 | functions and document properly. |
| 8 | |
| 9 | If you are a contributor to the driver, please |
| 10 | list your email here for future contact: |
| 11 | |
| 12 | jhall@onlab.us |
| 13 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 14 | shreya@onlab.us |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 15 | |
| 16 | OCT 13 2014 |
| 17 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 18 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 19 | import pexpect |
| 20 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 21 | import json |
| 22 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 23 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 24 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 25 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 26 | from core.graph import Graph |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 27 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 28 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 29 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 30 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 31 | def __init__( self ): |
| 32 | """ |
| 33 | Initialize client |
| 34 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 35 | self.name = None |
| 36 | self.home = None |
| 37 | self.handle = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 38 | self.graph = Graph() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 39 | super( CLI, self ).__init__() |
| 40 | |
| 41 | def connect( self, **connectargs ): |
| 42 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 43 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 45 | try: |
| 46 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 47 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 48 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 49 | for key in self.options: |
| 50 | if key == "home": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 51 | self.home = self.options[ 'home' ] |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 52 | break |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 53 | if self.home is None or self.home == "": |
Jon Hall | e94919c | 2015-03-23 11:42:57 -0700 | [diff] [blame] | 54 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 55 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 56 | for key in self.options: |
| 57 | if key == 'onosIp': |
| 58 | self.onosIp = self.options[ 'onosIp' ] |
| 59 | break |
| 60 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 61 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 62 | |
| 63 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 64 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 65 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 66 | else: |
| 67 | main.log.info( self.name + |
| 68 | ": Trying to connect to " + |
| 69 | self.ip_address ) |
| 70 | |
| 71 | except KeyError: |
| 72 | main.log.info( "Invalid host name," + |
| 73 | " connecting to local host instead" ) |
| 74 | self.ip_address = 'localhost' |
| 75 | except Exception as inst: |
| 76 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 77 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 78 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 79 | user_name=self.user_name, |
| 80 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 81 | port=self.port, |
| 82 | pwd=self.pwd, |
| 83 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 84 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 85 | self.handle.sendline( "cd " + self.home ) |
| 86 | self.handle.expect( "\$" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 87 | if self.handle: |
| 88 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 89 | else: |
| 90 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 91 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 92 | except TypeError: |
| 93 | main.log.exception( self.name + ": Object not as expected" ) |
| 94 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 95 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 96 | main.log.error( self.name + ": EOF exception found" ) |
| 97 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 98 | main.cleanup() |
| 99 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 100 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 101 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 102 | main.cleanup() |
| 103 | main.exit() |
| 104 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 105 | def disconnect( self ): |
| 106 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 107 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 108 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 109 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 110 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 111 | if self.handle: |
| 112 | i = self.logout() |
| 113 | if i == main.TRUE: |
| 114 | self.handle.sendline( "" ) |
| 115 | self.handle.expect( "\$" ) |
| 116 | self.handle.sendline( "exit" ) |
| 117 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 118 | except TypeError: |
| 119 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 120 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 121 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 122 | main.log.error( self.name + ": EOF exception found" ) |
| 123 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 124 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 125 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 126 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 127 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 128 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 129 | response = main.FALSE |
| 130 | return response |
| 131 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 132 | def logout( self ): |
| 133 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 134 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 135 | Returns main.TRUE if exited CLI and |
| 136 | main.FALSE on timeout (not guranteed you are disconnected) |
| 137 | None on TypeError |
| 138 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 139 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 140 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 141 | if self.handle: |
| 142 | self.handle.sendline( "" ) |
| 143 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ], |
| 144 | timeout=10 ) |
| 145 | if i == 0: # In ONOS CLI |
| 146 | self.handle.sendline( "logout" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 147 | j = self.handle.expect( [ "\$", |
| 148 | "Command not found:", |
| 149 | pexpect.TIMEOUT ] ) |
| 150 | if j == 0: # Successfully logged out |
| 151 | return main.TRUE |
| 152 | elif j == 1 or j == 2: |
| 153 | # ONOS didn't fully load, and logout command isn't working |
| 154 | # or the command timed out |
| 155 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 156 | try: |
| 157 | self.handle.expect( "\$" ) |
| 158 | except pexpect.TIMEOUT: |
| 159 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 160 | return main.TRUE |
| 161 | else: # some other output |
| 162 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 163 | repr( self.handle.before ) ) |
| 164 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 165 | elif i == 1: # not in CLI |
| 166 | return main.TRUE |
| 167 | elif i == 3: # Timeout |
| 168 | return main.FALSE |
| 169 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 170 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 171 | except TypeError: |
| 172 | main.log.exception( self.name + ": Object not as expected" ) |
| 173 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 174 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 175 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 176 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 177 | main.cleanup() |
| 178 | main.exit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 179 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 180 | main.log.error( self.name + |
| 181 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 182 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 183 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 184 | main.cleanup() |
| 185 | main.exit() |
| 186 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 187 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 188 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 189 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 190 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 191 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 192 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 193 | try: |
| 194 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | main.log.error( "Must define cellname" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 196 | main.cleanup() |
| 197 | main.exit() |
| 198 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 199 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 200 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 201 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 202 | # and that this driver will have to change accordingly |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 203 | self.handle.expect(str(cellname)) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 204 | handleBefore = self.handle.before |
| 205 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 206 | # Get the rest of the handle |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 207 | self.handle.sendline("") |
| 208 | self.handle.expect("\$") |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 209 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 210 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 211 | main.log.info( "Cell call returned: " + handleBefore + |
| 212 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 213 | |
| 214 | return main.TRUE |
| 215 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 216 | except TypeError: |
| 217 | main.log.exception( self.name + ": Object not as expected" ) |
| 218 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 219 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 220 | main.log.error( self.name + ": eof exception found" ) |
| 221 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 222 | main.cleanup() |
| 223 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 224 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 225 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 226 | main.cleanup() |
| 227 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 228 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 229 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 230 | commandlineTimeout=10, onosStartTimeout=60 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 231 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 232 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 233 | by user would be used to set the current karaf shell idle timeout. |
| 234 | 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] | 235 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 236 | Below is an example to start a session with 60 seconds idle timeout |
| 237 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 238 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 239 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 240 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 241 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 242 | Note: karafTimeout is left as str so that this could be read |
| 243 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 244 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 245 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 246 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 247 | self.handle.sendline( "" ) |
| 248 | x = self.handle.expect( [ |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 249 | "\$", "onos>" ], commandlineTimeout) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 250 | |
| 251 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 252 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 253 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 254 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 255 | # Wait for onos start ( -w ) and enter onos cli |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 256 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 257 | i = self.handle.expect( [ |
| 258 | "onos>", |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 259 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 260 | |
| 261 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 262 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 263 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 264 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 265 | "config:property-set -p org.apache.karaf.shell\ |
| 266 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | karafTimeout ) |
| 268 | self.handle.expect( "\$" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 269 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 270 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 271 | return main.TRUE |
| 272 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 273 | # If failed, send ctrl+c to process and try again |
| 274 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 275 | self.handle.send( "\x03" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 276 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 277 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 278 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 279 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 280 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 281 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 282 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 283 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 284 | "config:property-set -p org.apache.karaf.shell\ |
| 285 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 286 | karafTimeout ) |
| 287 | self.handle.expect( "\$" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 288 | self.handle.sendline( "onos -w " + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 289 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 290 | return main.TRUE |
| 291 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 292 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 293 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 294 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 295 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 296 | except TypeError: |
| 297 | main.log.exception( self.name + ": Object not as expected" ) |
| 298 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 299 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 300 | main.log.error( self.name + ": EOF exception found" ) |
| 301 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 302 | main.cleanup() |
| 303 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 304 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 305 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 306 | main.cleanup() |
| 307 | main.exit() |
| 308 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 309 | def startCellCli( self, karafTimeout="", |
| 310 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 311 | """ |
| 312 | Start CLI on onos ecll handle. |
| 313 | |
| 314 | karafTimeout is an optional argument. karafTimeout value passed |
| 315 | by user would be used to set the current karaf shell idle timeout. |
| 316 | Note that when ever this property is modified the shell will exit and |
| 317 | the subsequent login would reflect new idle timeout. |
| 318 | Below is an example to start a session with 60 seconds idle timeout |
| 319 | ( input value is in milliseconds ): |
| 320 | |
| 321 | tValue = "60000" |
| 322 | |
| 323 | Note: karafTimeout is left as str so that this could be read |
| 324 | and passed to startOnosCli from PARAMS file as str. |
| 325 | """ |
| 326 | |
| 327 | try: |
| 328 | self.handle.sendline( "" ) |
| 329 | x = self.handle.expect( [ |
| 330 | "\$", "onos>" ], commandlineTimeout) |
| 331 | |
| 332 | if x == 1: |
| 333 | main.log.info( "ONOS cli is already running" ) |
| 334 | return main.TRUE |
| 335 | |
| 336 | # Wait for onos start ( -w ) and enter onos cli |
| 337 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 338 | i = self.handle.expect( [ |
| 339 | "onos>", |
| 340 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 341 | |
| 342 | if i == 0: |
| 343 | main.log.info( self.name + " CLI Started successfully" ) |
| 344 | if karafTimeout: |
| 345 | self.handle.sendline( |
| 346 | "config:property-set -p org.apache.karaf.shell\ |
| 347 | sshIdleTimeout " + |
| 348 | karafTimeout ) |
| 349 | self.handle.expect( "\$" ) |
| 350 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 351 | self.handle.expect( "onos>" ) |
| 352 | return main.TRUE |
| 353 | else: |
| 354 | # If failed, send ctrl+c to process and try again |
| 355 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 356 | self.handle.send( "\x03" ) |
| 357 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 358 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 359 | timeout=30 ) |
| 360 | if i == 0: |
| 361 | main.log.info( self.name + " CLI Started " + |
| 362 | "successfully after retry attempt" ) |
| 363 | if karafTimeout: |
| 364 | self.handle.sendline( |
| 365 | "config:property-set -p org.apache.karaf.shell\ |
| 366 | sshIdleTimeout " + |
| 367 | karafTimeout ) |
| 368 | self.handle.expect( "\$" ) |
| 369 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 370 | self.handle.expect( "onos>" ) |
| 371 | return main.TRUE |
| 372 | else: |
| 373 | main.log.error( "Connection to CLI " + |
| 374 | self.name + " timeout" ) |
| 375 | return main.FALSE |
| 376 | |
| 377 | except TypeError: |
| 378 | main.log.exception( self.name + ": Object not as expected" ) |
| 379 | return None |
| 380 | except pexpect.EOF: |
| 381 | main.log.error( self.name + ": EOF exception found" ) |
| 382 | main.log.error( self.name + ": " + self.handle.before ) |
| 383 | main.cleanup() |
| 384 | main.exit() |
| 385 | except Exception: |
| 386 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 387 | main.cleanup() |
| 388 | main.exit() |
| 389 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 390 | def log( self, cmdStr, level="",noExit=False): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 391 | """ |
| 392 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 393 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 394 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 395 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 396 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 397 | Level defaults to INFO |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 398 | """ |
| 399 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 400 | lvlStr = "" |
| 401 | if level: |
| 402 | lvlStr = "--level=" + level |
| 403 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 404 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 405 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 406 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 407 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 408 | response = self.handle.before |
| 409 | if re.search( "Error", response ): |
| 410 | return main.FALSE |
| 411 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 412 | except pexpect.TIMEOUT: |
| 413 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 414 | if noExit: |
| 415 | main.cleanup() |
| 416 | return None |
| 417 | else: |
| 418 | main.cleanup() |
| 419 | main.exit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 420 | except pexpect.EOF: |
| 421 | main.log.error( self.name + ": EOF exception found" ) |
| 422 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 423 | if noExit: |
| 424 | main.cleanup() |
| 425 | return None |
| 426 | else: |
| 427 | main.cleanup() |
| 428 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 429 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 430 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 431 | if noExit: |
| 432 | main.cleanup() |
| 433 | return None |
| 434 | else: |
| 435 | main.cleanup() |
| 436 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 437 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 438 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 439 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 440 | Send a completely user specified string to |
| 441 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 442 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 443 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 444 | if noExit is True, TestON will not exit, but clean up |
| 445 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 446 | Warning: There are no sanity checking to commands |
| 447 | sent using this method. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 448 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 449 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 450 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 451 | # Try to reconnect if disconnected from cli |
| 452 | self.handle.sendline( "" ) |
| 453 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ] ) |
| 454 | if i == 1: |
| 455 | main.log.error( self.name + ": onos cli session closed. ") |
| 456 | if self.onosIp: |
| 457 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 458 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 459 | if reconnectResult: |
| 460 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 461 | else: |
| 462 | main.log.error( self.name + ": reconnection failed." ) |
| 463 | main.cleanup() |
| 464 | main.exit() |
| 465 | else: |
| 466 | main.cleanup() |
| 467 | main.exit() |
| 468 | if i == 2: |
| 469 | self.handle.sendline( "" ) |
| 470 | self.handle.expect( "onos>" ) |
| 471 | |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 472 | if debug: |
| 473 | # NOTE: This adds and average of .4 seconds per call |
| 474 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 475 | self.log( logStr,noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 476 | self.handle.sendline( cmdStr ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 477 | i = self.handle.expect( ["onos>", "\$"], timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 478 | response = self.handle.before |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 479 | # TODO: do something with i |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 480 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
| 481 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 482 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 483 | main.log.debug( self.name + ": Raw output" ) |
| 484 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 485 | |
| 486 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 487 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 488 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 489 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 490 | main.log.debug( self.name + ": ansiEscape output" ) |
| 491 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 492 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 493 | # Remove extra return chars that get added |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 494 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 495 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 496 | main.log.debug( self.name + ": Removed extra returns " + |
| 497 | "from output" ) |
| 498 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 499 | |
| 500 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 501 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 502 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 503 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 504 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 505 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 506 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 507 | output = response.split( cmdStr.strip(), 1 ) |
| 508 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 509 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 510 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 511 | main.log.debug( self.name + ": " + repr( r ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 512 | output = output[1].strip() |
| 513 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 514 | main.log.info( "Response from ONOS: {}".format( output ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 515 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 516 | except pexpect.TIMEOUT: |
| 517 | main.log.error( self.name + ":ONOS timeout" ) |
| 518 | if debug: |
| 519 | main.log.debug( self.handle.before ) |
| 520 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 521 | except IndexError: |
| 522 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 523 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 524 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 525 | except TypeError: |
| 526 | main.log.exception( self.name + ": Object not as expected" ) |
| 527 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 528 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 529 | main.log.error( self.name + ": EOF exception found" ) |
| 530 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 531 | if noExit: |
| 532 | main.cleanup() |
| 533 | return None |
| 534 | else: |
| 535 | main.cleanup() |
| 536 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 537 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 538 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 539 | if noExit: |
| 540 | main.cleanup() |
| 541 | return None |
| 542 | else: |
| 543 | main.cleanup() |
| 544 | main.exit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 545 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 546 | # IMPORTANT NOTE: |
| 547 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 548 | # the cli command changing 'a:b' with 'aB'. |
| 549 | # Ex ) onos:topology > onosTopology |
| 550 | # onos:links > onosLinks |
| 551 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 552 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 553 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 554 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 555 | Adds a new cluster node by ID and address information. |
| 556 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 557 | * nodeId |
| 558 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 559 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 560 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 561 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 562 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 563 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 564 | str( ONOSIp ) + " " + str( tcpPort ) |
| 565 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 566 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 567 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 568 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 569 | main.log.error( "Error in adding node" ) |
| 570 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 571 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 572 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 573 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 574 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 575 | except AssertionError: |
| 576 | main.log.exception( "" ) |
| 577 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 578 | except TypeError: |
| 579 | main.log.exception( self.name + ": Object not as expected" ) |
| 580 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 581 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 582 | main.log.error( self.name + ": EOF exception found" ) |
| 583 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 584 | main.cleanup() |
| 585 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 586 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 587 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 588 | main.cleanup() |
| 589 | main.exit() |
| 590 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 591 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 592 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 593 | Removes a cluster by ID |
| 594 | Issues command: 'remove-node [<node-id>]' |
| 595 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 596 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 597 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 598 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 599 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 600 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 601 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 602 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 603 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 604 | if re.search( "Error", handle ): |
| 605 | main.log.error( "Error in removing node" ) |
| 606 | main.log.error( handle ) |
| 607 | return main.FALSE |
| 608 | else: |
| 609 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 610 | except AssertionError: |
| 611 | main.log.exception( "" ) |
| 612 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 613 | except TypeError: |
| 614 | main.log.exception( self.name + ": Object not as expected" ) |
| 615 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 616 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 617 | main.log.error( self.name + ": EOF exception found" ) |
| 618 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 619 | main.cleanup() |
| 620 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 621 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 622 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 623 | main.cleanup() |
| 624 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 625 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 626 | def nodes( self, jsonFormat=True): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 627 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 628 | List the nodes currently visible |
| 629 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 630 | Optional argument: |
| 631 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 632 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 633 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 634 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 635 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 636 | cmdStr += " -j" |
| 637 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 638 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 639 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 640 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 641 | except AssertionError: |
| 642 | main.log.exception( "" ) |
| 643 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 644 | except TypeError: |
| 645 | main.log.exception( self.name + ": Object not as expected" ) |
| 646 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 647 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 648 | main.log.error( self.name + ": EOF exception found" ) |
| 649 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 650 | main.cleanup() |
| 651 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 652 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 653 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 654 | main.cleanup() |
| 655 | main.exit() |
| 656 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 657 | def topology( self ): |
| 658 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 659 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 660 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 661 | Return: |
| 662 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 663 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 664 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 665 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 666 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 667 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 668 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 669 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 670 | except AssertionError: |
| 671 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 672 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 673 | except TypeError: |
| 674 | main.log.exception( self.name + ": Object not as expected" ) |
| 675 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 676 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 677 | main.log.error( self.name + ": EOF exception found" ) |
| 678 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 679 | main.cleanup() |
| 680 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 681 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 682 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 683 | main.cleanup() |
| 684 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 685 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 686 | def deviceRemove( self, deviceId ): |
| 687 | """ |
| 688 | Removes particular device from storage |
| 689 | |
| 690 | TODO: refactor this function |
| 691 | """ |
| 692 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 693 | cmdStr = "device-remove " + str( deviceId ) |
| 694 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 695 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 696 | if re.search( "Error", handle ): |
| 697 | main.log.error( "Error in removing device" ) |
| 698 | main.log.error( handle ) |
| 699 | return main.FALSE |
| 700 | else: |
| 701 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 702 | except AssertionError: |
| 703 | main.log.exception( "" ) |
| 704 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 705 | except TypeError: |
| 706 | main.log.exception( self.name + ": Object not as expected" ) |
| 707 | return None |
| 708 | except pexpect.EOF: |
| 709 | main.log.error( self.name + ": EOF exception found" ) |
| 710 | main.log.error( self.name + ": " + self.handle.before ) |
| 711 | main.cleanup() |
| 712 | main.exit() |
| 713 | except Exception: |
| 714 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 715 | main.cleanup() |
| 716 | main.exit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 717 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 718 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 719 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 720 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 721 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 722 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 723 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 724 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 725 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 726 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 727 | cmdStr += " -j" |
| 728 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 729 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 730 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 731 | except AssertionError: |
| 732 | main.log.exception( "" ) |
| 733 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 734 | except TypeError: |
| 735 | main.log.exception( self.name + ": Object not as expected" ) |
| 736 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 737 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 738 | main.log.error( self.name + ": EOF exception found" ) |
| 739 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 740 | main.cleanup() |
| 741 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 742 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 743 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 744 | main.cleanup() |
| 745 | main.exit() |
| 746 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 747 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 748 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 749 | This balances the devices across all controllers |
| 750 | by issuing command: 'onos> onos:balance-masters' |
| 751 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 752 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 753 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 754 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 755 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 756 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 757 | if re.search( "Error", handle ): |
| 758 | main.log.error( "Error in balancing masters" ) |
| 759 | main.log.error( handle ) |
| 760 | return main.FALSE |
| 761 | else: |
| 762 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 763 | except AssertionError: |
| 764 | main.log.exception( "" ) |
| 765 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 766 | except TypeError: |
| 767 | main.log.exception( self.name + ": Object not as expected" ) |
| 768 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 769 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 770 | main.log.error( self.name + ": EOF exception found" ) |
| 771 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 772 | main.cleanup() |
| 773 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 774 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 775 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 776 | main.cleanup() |
| 777 | main.exit() |
| 778 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 779 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 780 | """ |
| 781 | Returns the output of the masters command. |
| 782 | Optional argument: |
| 783 | * jsonFormat - boolean indicating if you want output in json |
| 784 | """ |
| 785 | try: |
| 786 | cmdStr = "onos:masters" |
| 787 | if jsonFormat: |
| 788 | cmdStr += " -j" |
| 789 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 790 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 791 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 792 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 793 | except AssertionError: |
| 794 | main.log.exception( "" ) |
| 795 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 796 | except TypeError: |
| 797 | main.log.exception( self.name + ": Object not as expected" ) |
| 798 | return None |
| 799 | except pexpect.EOF: |
| 800 | main.log.error( self.name + ": EOF exception found" ) |
| 801 | main.log.error( self.name + ": " + self.handle.before ) |
| 802 | main.cleanup() |
| 803 | main.exit() |
| 804 | except Exception: |
| 805 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 806 | main.cleanup() |
| 807 | main.exit() |
| 808 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 809 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 810 | """ |
| 811 | Uses the master command to check that the devices' leadership |
| 812 | is evenly divided |
| 813 | |
| 814 | Dependencies: checkMasters() and summary() |
| 815 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 816 | Returns main.TRUE if the devices are balanced |
| 817 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 818 | Exits on Exception |
| 819 | Returns None on TypeError |
| 820 | """ |
| 821 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 822 | summaryOutput = self.summary() |
| 823 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 824 | except ( TypeError, ValueError ): |
| 825 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 826 | return None |
| 827 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 828 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 829 | mastersOutput = self.checkMasters() |
| 830 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 831 | first = masters[ 0 ][ "size" ] |
| 832 | for master in masters: |
| 833 | totalOwnedDevices += master[ "size" ] |
| 834 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 835 | main.log.error( "Mastership not balanced" ) |
| 836 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 837 | return main.FALSE |
| 838 | main.log.info( "Mastership balanced between " \ |
| 839 | + str( len(masters) ) + " masters" ) |
| 840 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 841 | except ( TypeError, ValueError ): |
| 842 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 843 | return None |
| 844 | except pexpect.EOF: |
| 845 | main.log.error( self.name + ": EOF exception found" ) |
| 846 | main.log.error( self.name + ": " + self.handle.before ) |
| 847 | main.cleanup() |
| 848 | main.exit() |
| 849 | except Exception: |
| 850 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 851 | main.cleanup() |
| 852 | main.exit() |
| 853 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 854 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 855 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 856 | Lists all core links |
| 857 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 858 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 859 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 860 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 861 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 862 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 863 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 864 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 865 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 866 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 867 | except AssertionError: |
| 868 | main.log.exception( "" ) |
| 869 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 870 | except TypeError: |
| 871 | main.log.exception( self.name + ": Object not as expected" ) |
| 872 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 873 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 874 | main.log.error( self.name + ": EOF exception found" ) |
| 875 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 876 | main.cleanup() |
| 877 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 878 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 879 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 880 | main.cleanup() |
| 881 | main.exit() |
| 882 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 883 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 884 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 885 | Lists all ports |
| 886 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 887 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 888 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 889 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 890 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 891 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 892 | cmdStr += " -j" |
| 893 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 894 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 895 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 896 | except AssertionError: |
| 897 | main.log.exception( "" ) |
| 898 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 899 | except TypeError: |
| 900 | main.log.exception( self.name + ": Object not as expected" ) |
| 901 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 902 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 903 | main.log.error( self.name + ": EOF exception found" ) |
| 904 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 905 | main.cleanup() |
| 906 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 907 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 908 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 909 | main.cleanup() |
| 910 | main.exit() |
| 911 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 912 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 913 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 914 | Lists all devices and the controllers with roles assigned to them |
| 915 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 916 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 917 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 918 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 919 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 920 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 921 | cmdStr += " -j" |
| 922 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 923 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 924 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 925 | except AssertionError: |
| 926 | main.log.exception( "" ) |
| 927 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 928 | except TypeError: |
| 929 | main.log.exception( self.name + ": Object not as expected" ) |
| 930 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 931 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 932 | main.log.error( self.name + ": EOF exception found" ) |
| 933 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 934 | main.cleanup() |
| 935 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 936 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 937 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 938 | main.cleanup() |
| 939 | main.exit() |
| 940 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 941 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 942 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 943 | Given the a string containing the json representation of the "roles" |
| 944 | cli command and a partial or whole device id, returns a json object |
| 945 | containing the roles output for the first device whose id contains |
| 946 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 947 | |
| 948 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 949 | A dict of the role assignments for the given device or |
| 950 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 951 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 952 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 953 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 954 | return None |
| 955 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 956 | rawRoles = self.roles() |
| 957 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 958 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 959 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 960 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 961 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 962 | return device |
| 963 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 964 | except ( TypeError, ValueError ): |
| 965 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 966 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 967 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 968 | main.log.error( self.name + ": EOF exception found" ) |
| 969 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 970 | main.cleanup() |
| 971 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 972 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 973 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 974 | main.cleanup() |
| 975 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 976 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 977 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 978 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 979 | Iterates through each device and checks if there is a master assigned |
| 980 | Returns: main.TRUE if each device has a master |
| 981 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 982 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 983 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 984 | rawRoles = self.roles() |
| 985 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 986 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 987 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 988 | # print device |
| 989 | if device[ 'master' ] == "none": |
| 990 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 991 | return main.FALSE |
| 992 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 993 | except ( TypeError, ValueError ): |
| 994 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 995 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 996 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 997 | main.log.error( self.name + ": EOF exception found" ) |
| 998 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 999 | main.cleanup() |
| 1000 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1001 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1002 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1003 | main.cleanup() |
| 1004 | main.exit() |
| 1005 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1006 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1007 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1008 | Returns string of paths, and the cost. |
| 1009 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1010 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1011 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1012 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1013 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1014 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1015 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1016 | main.log.error( "Error in getting paths" ) |
| 1017 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1018 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1019 | path = handle.split( ";" )[ 0 ] |
| 1020 | cost = handle.split( ";" )[ 1 ] |
| 1021 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1022 | except AssertionError: |
| 1023 | main.log.exception( "" ) |
| 1024 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1025 | except TypeError: |
| 1026 | main.log.exception( self.name + ": Object not as expected" ) |
| 1027 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1028 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1029 | main.log.error( self.name + ": EOF exception found" ) |
| 1030 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1031 | main.cleanup() |
| 1032 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1033 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1034 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1035 | main.cleanup() |
| 1036 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1037 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1038 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1039 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1040 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1041 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1042 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1043 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1044 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1045 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1046 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1047 | cmdStr += " -j" |
| 1048 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1049 | if handle: |
| 1050 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1051 | # TODO: Maybe make this less hardcoded |
| 1052 | # ConsistentMap Exceptions |
| 1053 | assert "org.onosproject.store.service" not in handle |
| 1054 | # Node not leader |
| 1055 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1056 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1057 | except AssertionError: |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1058 | main.log.exception( "Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1059 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1060 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1061 | except TypeError: |
| 1062 | main.log.exception( self.name + ": Object not as expected" ) |
| 1063 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1064 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1065 | main.log.error( self.name + ": EOF exception found" ) |
| 1066 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1067 | main.cleanup() |
| 1068 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1069 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1070 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1071 | main.cleanup() |
| 1072 | main.exit() |
| 1073 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1074 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1075 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1076 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1077 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1078 | 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] | 1079 | partial mac address |
| 1080 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1081 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1082 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1083 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1084 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1085 | return None |
| 1086 | else: |
| 1087 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1088 | rawHosts = self.hosts() |
| 1089 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1090 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1091 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1092 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1093 | if not host: |
| 1094 | pass |
| 1095 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1096 | return host |
| 1097 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1098 | except ( TypeError, ValueError ): |
| 1099 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1100 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1101 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1102 | main.log.error( self.name + ": EOF exception found" ) |
| 1103 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1104 | main.cleanup() |
| 1105 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1106 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1107 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1108 | main.cleanup() |
| 1109 | main.exit() |
| 1110 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1111 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1112 | """ |
| 1113 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1114 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1115 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1116 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1117 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1118 | IMPORTANT: |
| 1119 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1120 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1121 | Furthermore, it assumes that value of VLAN is '-1' |
| 1122 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1123 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1124 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1125 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1126 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1127 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1128 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1129 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1130 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1131 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1132 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1133 | i = iter( str( hostHex ) ) |
| 1134 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1135 | hostHex = hostHex + "/-1" |
| 1136 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1137 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1138 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1139 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1140 | except TypeError: |
| 1141 | main.log.exception( self.name + ": Object not as expected" ) |
| 1142 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1143 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1144 | main.log.error( self.name + ": EOF exception found" ) |
| 1145 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1146 | main.cleanup() |
| 1147 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1148 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1149 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1150 | main.cleanup() |
| 1151 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1152 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1153 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1154 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1155 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1156 | * hostIdOne: ONOS host id for host1 |
| 1157 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1158 | Optional: |
| 1159 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1160 | * setVlan: specify a VLAN id treatment |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1161 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1162 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1163 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1164 | Returns: |
| 1165 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1166 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1167 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1168 | cmdStr = "add-host-intent " |
| 1169 | if vlanId: |
| 1170 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1171 | if setVlan: |
| 1172 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1173 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1174 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1175 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1176 | if re.search( "Error", handle ): |
| 1177 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1178 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1179 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1180 | else: |
| 1181 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1182 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
| 1183 | match = re.search('id=0x([\da-f]+),', handle) |
| 1184 | if match: |
| 1185 | return match.group()[3:-1] |
| 1186 | else: |
| 1187 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1188 | main.log.debug( "Response from ONOS was: " + |
| 1189 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1190 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1191 | except AssertionError: |
| 1192 | main.log.exception( "" ) |
| 1193 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1194 | except TypeError: |
| 1195 | main.log.exception( self.name + ": Object not as expected" ) |
| 1196 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1197 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1198 | main.log.error( self.name + ": EOF exception found" ) |
| 1199 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1200 | main.cleanup() |
| 1201 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1202 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1203 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1204 | main.cleanup() |
| 1205 | main.exit() |
| 1206 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1207 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1208 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1209 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1210 | * ingressDevice: device id of ingress device |
| 1211 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1212 | Optional: |
| 1213 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1214 | Description: |
| 1215 | Adds an optical intent by specifying an ingress and egress device |
| 1216 | Returns: |
| 1217 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1218 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1219 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1220 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1221 | " " + str( egressDevice ) |
| 1222 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1223 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1224 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1225 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1226 | main.log.error( "Error in adding Optical intent" ) |
| 1227 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1228 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1229 | main.log.info( "Optical intent installed between " + |
| 1230 | str( ingressDevice ) + " and " + |
| 1231 | str( egressDevice ) ) |
| 1232 | match = re.search('id=0x([\da-f]+),', handle) |
| 1233 | if match: |
| 1234 | return match.group()[3:-1] |
| 1235 | else: |
| 1236 | main.log.error( "Error, intent ID not found" ) |
| 1237 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1238 | except AssertionError: |
| 1239 | main.log.exception( "" ) |
| 1240 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1241 | except TypeError: |
| 1242 | main.log.exception( self.name + ": Object not as expected" ) |
| 1243 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1244 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1245 | main.log.error( self.name + ": EOF exception found" ) |
| 1246 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1247 | main.cleanup() |
| 1248 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1249 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1250 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1251 | main.cleanup() |
| 1252 | main.exit() |
| 1253 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1254 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1255 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1256 | ingressDevice, |
| 1257 | egressDevice, |
| 1258 | portIngress="", |
| 1259 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1260 | ethType="", |
| 1261 | ethSrc="", |
| 1262 | ethDst="", |
| 1263 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1264 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1265 | ipProto="", |
| 1266 | ipSrc="", |
| 1267 | ipDst="", |
| 1268 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1269 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1270 | vlanId="", |
| 1271 | setVlan="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1272 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1273 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1274 | * ingressDevice: device id of ingress device |
| 1275 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1276 | Optional: |
| 1277 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1278 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1279 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1280 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1281 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1282 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1283 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1284 | * ipSrc: specify ip source address |
| 1285 | * ipDst: specify ip destination address |
| 1286 | * tcpSrc: specify tcp source port |
| 1287 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1288 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1289 | * setVlan: specify a VLAN id treatment |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1290 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1291 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1292 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1293 | Returns: |
| 1294 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1295 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1296 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1297 | options developers provide for point-to-point |
| 1298 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1299 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1300 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1301 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1302 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1303 | if ethType: |
| 1304 | cmd += " --ethType " + str( ethType ) |
| 1305 | if ethSrc: |
| 1306 | cmd += " --ethSrc " + str( ethSrc ) |
| 1307 | if ethDst: |
| 1308 | cmd += " --ethDst " + str( ethDst ) |
| 1309 | if bandwidth: |
| 1310 | cmd += " --bandwidth " + str( bandwidth ) |
| 1311 | if lambdaAlloc: |
| 1312 | cmd += " --lambda " |
| 1313 | if ipProto: |
| 1314 | cmd += " --ipProto " + str( ipProto ) |
| 1315 | if ipSrc: |
| 1316 | cmd += " --ipSrc " + str( ipSrc ) |
| 1317 | if ipDst: |
| 1318 | cmd += " --ipDst " + str( ipDst ) |
| 1319 | if tcpSrc: |
| 1320 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1321 | if tcpDst: |
| 1322 | cmd += " --tcpDst " + str( tcpDst ) |
| 1323 | if vlanId: |
| 1324 | cmd += " -v " + str( vlanId ) |
| 1325 | if setVlan: |
| 1326 | cmd += " --setVlan " + str( setVlan ) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1327 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1328 | # Check whether the user appended the port |
| 1329 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1330 | if "/" in ingressDevice: |
| 1331 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1332 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1333 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1334 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1335 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1336 | # Would it make sense to throw an exception and exit |
| 1337 | # the test? |
| 1338 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1339 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1340 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1341 | str( ingressDevice ) + "/" +\ |
| 1342 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1343 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1344 | if "/" in egressDevice: |
| 1345 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1346 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1347 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1348 | main.log.error( "You must specify the egress port" ) |
| 1349 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1350 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1351 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1352 | str( egressDevice ) + "/" +\ |
| 1353 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1354 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1355 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1356 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1357 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1358 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1359 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1360 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1361 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1362 | # TODO: print out all the options in this message? |
| 1363 | main.log.info( "Point-to-point intent installed between " + |
| 1364 | str( ingressDevice ) + " and " + |
| 1365 | str( egressDevice ) ) |
| 1366 | match = re.search('id=0x([\da-f]+),', handle) |
| 1367 | if match: |
| 1368 | return match.group()[3:-1] |
| 1369 | else: |
| 1370 | main.log.error( "Error, intent ID not found" ) |
| 1371 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1372 | except AssertionError: |
| 1373 | main.log.exception( "" ) |
| 1374 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1375 | except TypeError: |
| 1376 | main.log.exception( self.name + ": Object not as expected" ) |
| 1377 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1378 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1379 | main.log.error( self.name + ": EOF exception found" ) |
| 1380 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1381 | main.cleanup() |
| 1382 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1383 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1384 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1385 | main.cleanup() |
| 1386 | main.exit() |
| 1387 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1388 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1389 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1390 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1391 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1392 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1393 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1394 | ethType="", |
| 1395 | ethSrc="", |
| 1396 | ethDst="", |
| 1397 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1398 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1399 | ipProto="", |
| 1400 | ipSrc="", |
| 1401 | ipDst="", |
| 1402 | tcpSrc="", |
| 1403 | tcpDst="", |
| 1404 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1405 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1406 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1407 | setVlan="", |
| 1408 | partial=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1409 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1410 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1411 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1412 | is same. That is, all ingress devices include port numbers |
| 1413 | with a "/" or all ingress devices could specify device |
| 1414 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1415 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1416 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1417 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1418 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1419 | Optional: |
| 1420 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1421 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1422 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1423 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1424 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1425 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1426 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1427 | * ipSrc: specify ip source address |
| 1428 | * ipDst: specify ip destination address |
| 1429 | * tcpSrc: specify tcp source port |
| 1430 | * tcpDst: specify tcp destination port |
| 1431 | * setEthSrc: action to Rewrite Source MAC Address |
| 1432 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1433 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1434 | * setVlan: specify VLAN Id treatment |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1435 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1436 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1437 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1438 | Returns: |
| 1439 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1440 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1441 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1442 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1443 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1444 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1445 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1446 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1447 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1448 | if ethType: |
| 1449 | cmd += " --ethType " + str( ethType ) |
| 1450 | if ethSrc: |
| 1451 | cmd += " --ethSrc " + str( ethSrc ) |
| 1452 | if ethDst: |
| 1453 | cmd += " --ethDst " + str( ethDst ) |
| 1454 | if bandwidth: |
| 1455 | cmd += " --bandwidth " + str( bandwidth ) |
| 1456 | if lambdaAlloc: |
| 1457 | cmd += " --lambda " |
| 1458 | if ipProto: |
| 1459 | cmd += " --ipProto " + str( ipProto ) |
| 1460 | if ipSrc: |
| 1461 | cmd += " --ipSrc " + str( ipSrc ) |
| 1462 | if ipDst: |
| 1463 | cmd += " --ipDst " + str( ipDst ) |
| 1464 | if tcpSrc: |
| 1465 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1466 | if tcpDst: |
| 1467 | cmd += " --tcpDst " + str( tcpDst ) |
| 1468 | if setEthSrc: |
| 1469 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1470 | if setEthDst: |
| 1471 | cmd += " --setEthDst " + str( setEthDst ) |
| 1472 | if vlanId: |
| 1473 | cmd += " -v " + str( vlanId ) |
| 1474 | if setVlan: |
| 1475 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1476 | if partial: |
| 1477 | cmd += " --partial" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1478 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1479 | # Check whether the user appended the port |
| 1480 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1481 | |
| 1482 | if portIngressList is None: |
| 1483 | for ingressDevice in ingressDeviceList: |
| 1484 | if "/" in ingressDevice: |
| 1485 | cmd += " " + str( ingressDevice ) |
| 1486 | else: |
| 1487 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1488 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1489 | # TODO: perhaps more meaningful return |
| 1490 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1491 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1492 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1493 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1494 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1495 | cmd += " " + \ |
| 1496 | str( ingressDevice ) + "/" +\ |
| 1497 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1498 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1499 | main.log.error( "Device list and port list does not " + |
| 1500 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1501 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1502 | if "/" in egressDevice: |
| 1503 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1504 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1505 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1506 | main.log.error( "You must specify " + |
| 1507 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1508 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1509 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1510 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1511 | str( egressDevice ) + "/" +\ |
| 1512 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1513 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1514 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1515 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1516 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1517 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1518 | "intent" ) |
| 1519 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1520 | else: |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1521 | match = re.search('id=0x([\da-f]+),', handle) |
| 1522 | if match: |
| 1523 | return match.group()[3:-1] |
| 1524 | else: |
| 1525 | main.log.error( "Error, intent ID not found" ) |
| 1526 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1527 | except AssertionError: |
| 1528 | main.log.exception( "" ) |
| 1529 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1530 | except TypeError: |
| 1531 | main.log.exception( self.name + ": Object not as expected" ) |
| 1532 | return None |
| 1533 | except pexpect.EOF: |
| 1534 | main.log.error( self.name + ": EOF exception found" ) |
| 1535 | main.log.error( self.name + ": " + self.handle.before ) |
| 1536 | main.cleanup() |
| 1537 | main.exit() |
| 1538 | except Exception: |
| 1539 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1540 | main.cleanup() |
| 1541 | main.exit() |
| 1542 | |
| 1543 | def addSinglepointToMultipointIntent( |
| 1544 | self, |
| 1545 | ingressDevice, |
| 1546 | egressDeviceList, |
| 1547 | portIngress="", |
| 1548 | portEgressList=None, |
| 1549 | ethType="", |
| 1550 | ethSrc="", |
| 1551 | ethDst="", |
| 1552 | bandwidth="", |
| 1553 | lambdaAlloc=False, |
| 1554 | ipProto="", |
| 1555 | ipSrc="", |
| 1556 | ipDst="", |
| 1557 | tcpSrc="", |
| 1558 | tcpDst="", |
| 1559 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1560 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1561 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1562 | setVlan="", |
| 1563 | partial=False ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1564 | """ |
| 1565 | Note: |
| 1566 | This function assumes the format of all egress devices |
| 1567 | is same. That is, all egress devices include port numbers |
| 1568 | with a "/" or all egress devices could specify device |
| 1569 | ids and port numbers seperately. |
| 1570 | Required: |
| 1571 | * EgressDeviceList: List of device ids of egress device |
| 1572 | ( Atleast 2 eress devices required in the list ) |
| 1573 | * ingressDevice: device id of ingress device |
| 1574 | Optional: |
| 1575 | * ethType: specify ethType |
| 1576 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1577 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1578 | * bandwidth: specify bandwidth capacity of link |
| 1579 | * lambdaAlloc: if True, intent will allocate lambda |
| 1580 | for the specified intent |
| 1581 | * ipProto: specify ip protocol |
| 1582 | * ipSrc: specify ip source address |
| 1583 | * ipDst: specify ip destination address |
| 1584 | * tcpSrc: specify tcp source port |
| 1585 | * tcpDst: specify tcp destination port |
| 1586 | * setEthSrc: action to Rewrite Source MAC Address |
| 1587 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1588 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1589 | * setVlan: specify VLAN ID treatment |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1590 | Description: |
| 1591 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1592 | specifying device id's and optional fields |
| 1593 | Returns: |
| 1594 | A string of the intent id or None on error |
| 1595 | |
| 1596 | NOTE: This function may change depending on the |
| 1597 | options developers provide for singlepoint-to-multipoint |
| 1598 | intent via cli |
| 1599 | """ |
| 1600 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1601 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1602 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1603 | if ethType: |
| 1604 | cmd += " --ethType " + str( ethType ) |
| 1605 | if ethSrc: |
| 1606 | cmd += " --ethSrc " + str( ethSrc ) |
| 1607 | if ethDst: |
| 1608 | cmd += " --ethDst " + str( ethDst ) |
| 1609 | if bandwidth: |
| 1610 | cmd += " --bandwidth " + str( bandwidth ) |
| 1611 | if lambdaAlloc: |
| 1612 | cmd += " --lambda " |
| 1613 | if ipProto: |
| 1614 | cmd += " --ipProto " + str( ipProto ) |
| 1615 | if ipSrc: |
| 1616 | cmd += " --ipSrc " + str( ipSrc ) |
| 1617 | if ipDst: |
| 1618 | cmd += " --ipDst " + str( ipDst ) |
| 1619 | if tcpSrc: |
| 1620 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1621 | if tcpDst: |
| 1622 | cmd += " --tcpDst " + str( tcpDst ) |
| 1623 | if setEthSrc: |
| 1624 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1625 | if setEthDst: |
| 1626 | cmd += " --setEthDst " + str( setEthDst ) |
| 1627 | if vlanId: |
| 1628 | cmd += " -v " + str( vlanId ) |
| 1629 | if setVlan: |
| 1630 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1631 | if partial: |
| 1632 | cmd += " --partial" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1633 | |
| 1634 | # Check whether the user appended the port |
| 1635 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1636 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1637 | if "/" in ingressDevice: |
| 1638 | cmd += " " + str( ingressDevice ) |
| 1639 | else: |
| 1640 | if not portIngress: |
| 1641 | main.log.error( "You must specify " + |
| 1642 | "the Ingress port" ) |
| 1643 | return main.FALSE |
| 1644 | |
| 1645 | cmd += " " +\ |
| 1646 | str( ingressDevice ) + "/" +\ |
| 1647 | str( portIngress ) |
| 1648 | |
| 1649 | if portEgressList is None: |
| 1650 | for egressDevice in egressDeviceList: |
| 1651 | if "/" in egressDevice: |
| 1652 | cmd += " " + str( egressDevice ) |
| 1653 | else: |
| 1654 | main.log.error( "You must specify " + |
| 1655 | "the egress port" ) |
| 1656 | # TODO: perhaps more meaningful return |
| 1657 | return main.FALSE |
| 1658 | else: |
| 1659 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1660 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1661 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1662 | cmd += " " + \ |
| 1663 | str( egressDevice ) + "/" +\ |
| 1664 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1665 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1666 | main.log.error( "Device list and port list does not " + |
| 1667 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1668 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1669 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1670 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1671 | # If error, return error message |
| 1672 | if re.search( "Error", handle ): |
| 1673 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1674 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1675 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1676 | else: |
| 1677 | match = re.search('id=0x([\da-f]+),', handle) |
| 1678 | if match: |
| 1679 | return match.group()[3:-1] |
| 1680 | else: |
| 1681 | main.log.error( "Error, intent ID not found" ) |
| 1682 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1683 | except AssertionError: |
| 1684 | main.log.exception( "" ) |
| 1685 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1686 | except TypeError: |
| 1687 | main.log.exception( self.name + ": Object not as expected" ) |
| 1688 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1689 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1690 | main.log.error( self.name + ": EOF exception found" ) |
| 1691 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1692 | main.cleanup() |
| 1693 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1694 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1695 | main.log.exception( self.name + ": Uncaught exception!" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1696 | main.cleanup() |
| 1697 | main.exit() |
| 1698 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1699 | def addMplsIntent( |
| 1700 | self, |
| 1701 | ingressDevice, |
| 1702 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1703 | ingressPort="", |
| 1704 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1705 | ethType="", |
| 1706 | ethSrc="", |
| 1707 | ethDst="", |
| 1708 | bandwidth="", |
| 1709 | lambdaAlloc=False, |
| 1710 | ipProto="", |
| 1711 | ipSrc="", |
| 1712 | ipDst="", |
| 1713 | tcpSrc="", |
| 1714 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1715 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1716 | egressLabel="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1717 | priority=""): |
| 1718 | """ |
| 1719 | Required: |
| 1720 | * ingressDevice: device id of ingress device |
| 1721 | * egressDevice: device id of egress device |
| 1722 | Optional: |
| 1723 | * ethType: specify ethType |
| 1724 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1725 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1726 | * bandwidth: specify bandwidth capacity of link |
| 1727 | * lambdaAlloc: if True, intent will allocate lambda |
| 1728 | for the specified intent |
| 1729 | * ipProto: specify ip protocol |
| 1730 | * ipSrc: specify ip source address |
| 1731 | * ipDst: specify ip destination address |
| 1732 | * tcpSrc: specify tcp source port |
| 1733 | * tcpDst: specify tcp destination port |
| 1734 | * ingressLabel: Ingress MPLS label |
| 1735 | * egressLabel: Egress MPLS label |
| 1736 | Description: |
| 1737 | Adds MPLS intent by |
| 1738 | specifying device id's and optional fields |
| 1739 | Returns: |
| 1740 | A string of the intent id or None on error |
| 1741 | |
| 1742 | NOTE: This function may change depending on the |
| 1743 | options developers provide for MPLS |
| 1744 | intent via cli |
| 1745 | """ |
| 1746 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1747 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1748 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1749 | if ethType: |
| 1750 | cmd += " --ethType " + str( ethType ) |
| 1751 | if ethSrc: |
| 1752 | cmd += " --ethSrc " + str( ethSrc ) |
| 1753 | if ethDst: |
| 1754 | cmd += " --ethDst " + str( ethDst ) |
| 1755 | if bandwidth: |
| 1756 | cmd += " --bandwidth " + str( bandwidth ) |
| 1757 | if lambdaAlloc: |
| 1758 | cmd += " --lambda " |
| 1759 | if ipProto: |
| 1760 | cmd += " --ipProto " + str( ipProto ) |
| 1761 | if ipSrc: |
| 1762 | cmd += " --ipSrc " + str( ipSrc ) |
| 1763 | if ipDst: |
| 1764 | cmd += " --ipDst " + str( ipDst ) |
| 1765 | if tcpSrc: |
| 1766 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1767 | if tcpDst: |
| 1768 | cmd += " --tcpDst " + str( tcpDst ) |
| 1769 | if ingressLabel: |
| 1770 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1771 | if egressLabel: |
| 1772 | cmd += " --egressLabel " + str( egressLabel ) |
| 1773 | if priority: |
| 1774 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1775 | |
| 1776 | # Check whether the user appended the port |
| 1777 | # or provided it as an input |
| 1778 | if "/" in ingressDevice: |
| 1779 | cmd += " " + str( ingressDevice ) |
| 1780 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1781 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1782 | main.log.error( "You must specify the ingress port" ) |
| 1783 | return None |
| 1784 | |
| 1785 | cmd += " " + \ |
| 1786 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1787 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1788 | |
| 1789 | if "/" in egressDevice: |
| 1790 | cmd += " " + str( egressDevice ) |
| 1791 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1792 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1793 | main.log.error( "You must specify the egress port" ) |
| 1794 | return None |
| 1795 | |
| 1796 | cmd += " " +\ |
| 1797 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1798 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1799 | |
| 1800 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1801 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1802 | # If error, return error message |
| 1803 | if re.search( "Error", handle ): |
| 1804 | main.log.error( "Error in adding mpls intent" ) |
| 1805 | return None |
| 1806 | else: |
| 1807 | # TODO: print out all the options in this message? |
| 1808 | main.log.info( "MPLS intent installed between " + |
| 1809 | str( ingressDevice ) + " and " + |
| 1810 | str( egressDevice ) ) |
| 1811 | match = re.search('id=0x([\da-f]+),', handle) |
| 1812 | if match: |
| 1813 | return match.group()[3:-1] |
| 1814 | else: |
| 1815 | main.log.error( "Error, intent ID not found" ) |
| 1816 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1817 | except AssertionError: |
| 1818 | main.log.exception( "" ) |
| 1819 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1820 | except TypeError: |
| 1821 | main.log.exception( self.name + ": Object not as expected" ) |
| 1822 | return None |
| 1823 | except pexpect.EOF: |
| 1824 | main.log.error( self.name + ": EOF exception found" ) |
| 1825 | main.log.error( self.name + ": " + self.handle.before ) |
| 1826 | main.cleanup() |
| 1827 | main.exit() |
| 1828 | except Exception: |
| 1829 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1830 | main.cleanup() |
| 1831 | main.exit() |
| 1832 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1833 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1834 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1835 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1836 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1837 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1838 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1839 | -p or --purge: Purge the intent from the store after removal |
| 1840 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1841 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 1842 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1843 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1844 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1845 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1846 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1847 | if purge: |
| 1848 | cmdStr += " -p" |
| 1849 | if sync: |
| 1850 | cmdStr += " -s" |
| 1851 | |
| 1852 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1853 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1854 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1855 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1856 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1857 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1858 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1859 | # TODO: Should this be main.TRUE |
| 1860 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1861 | except AssertionError: |
| 1862 | main.log.exception( "" ) |
| 1863 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1864 | except TypeError: |
| 1865 | main.log.exception( self.name + ": Object not as expected" ) |
| 1866 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1867 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1868 | main.log.error( self.name + ": EOF exception found" ) |
| 1869 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1870 | main.cleanup() |
| 1871 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1872 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1873 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1874 | main.cleanup() |
| 1875 | main.exit() |
| 1876 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1877 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1878 | """ |
| 1879 | Description: |
| 1880 | Remove all the intents |
| 1881 | Optional args:- |
| 1882 | -s or --sync: Waits for the removal before returning |
| 1883 | -p or --purge: Purge the intent from the store after removal |
| 1884 | Returns: |
| 1885 | Returns main.TRUE if all intents are removed, otherwise returns |
| 1886 | main.FALSE; Returns None for exception |
| 1887 | """ |
| 1888 | try: |
| 1889 | cmdStr = "remove-intent" |
| 1890 | if purge: |
| 1891 | cmdStr += " -p" |
| 1892 | if sync: |
| 1893 | cmdStr += " -s" |
| 1894 | |
| 1895 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1896 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1897 | assert "Command not found:" not in handle, handle |
| 1898 | if re.search( "Error", handle ): |
| 1899 | main.log.error( "Error in removing intent" ) |
| 1900 | return main.FALSE |
| 1901 | else: |
| 1902 | return main.TRUE |
| 1903 | except AssertionError: |
| 1904 | main.log.exception( "" ) |
| 1905 | return None |
| 1906 | except TypeError: |
| 1907 | main.log.exception( self.name + ": Object not as expected" ) |
| 1908 | return None |
| 1909 | except pexpect.EOF: |
| 1910 | main.log.error( self.name + ": EOF exception found" ) |
| 1911 | main.log.error( self.name + ": " + self.handle.before ) |
| 1912 | main.cleanup() |
| 1913 | main.exit() |
| 1914 | except Exception: |
| 1915 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1916 | main.cleanup() |
| 1917 | main.exit() |
| 1918 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 1919 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1920 | """ |
| 1921 | Purges all WITHDRAWN Intents |
| 1922 | """ |
| 1923 | try: |
| 1924 | cmdStr = "purge-intents" |
| 1925 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1926 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1927 | if re.search( "Error", handle ): |
| 1928 | main.log.error( "Error in purging intents" ) |
| 1929 | return main.FALSE |
| 1930 | else: |
| 1931 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1932 | except AssertionError: |
| 1933 | main.log.exception( "" ) |
| 1934 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1935 | except TypeError: |
| 1936 | main.log.exception( self.name + ": Object not as expected" ) |
| 1937 | return None |
| 1938 | except pexpect.EOF: |
| 1939 | main.log.error( self.name + ": EOF exception found" ) |
| 1940 | main.log.error( self.name + ": " + self.handle.before ) |
| 1941 | main.cleanup() |
| 1942 | main.exit() |
| 1943 | except Exception: |
| 1944 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1945 | main.cleanup() |
| 1946 | main.exit() |
| 1947 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1948 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1949 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1950 | NOTE: This method should be used after installing application: |
| 1951 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1952 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1953 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1954 | Description: |
| 1955 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1956 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1957 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1958 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1959 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1960 | cmdStr += " -j" |
| 1961 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1962 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1963 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1964 | except AssertionError: |
| 1965 | main.log.exception( "" ) |
| 1966 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1967 | except TypeError: |
| 1968 | main.log.exception( self.name + ": Object not as expected" ) |
| 1969 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1970 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1971 | main.log.error( self.name + ": EOF exception found" ) |
| 1972 | main.log.error( self.name + ": " + self.handle.before ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1973 | main.cleanup() |
| 1974 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1975 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1976 | main.log.exception( self.name + ": Uncaught exception!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1977 | main.cleanup() |
| 1978 | main.exit() |
| 1979 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 1980 | def ipv4RouteNumber( self ): |
| 1981 | """ |
| 1982 | NOTE: This method should be used after installing application: |
| 1983 | onos-app-sdnip |
| 1984 | Description: |
| 1985 | Obtain the total IPv4 routes number in the system |
| 1986 | """ |
| 1987 | try: |
| 1988 | cmdStr = "routes -s -j" |
| 1989 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1990 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 1991 | jsonResult = json.loads( handle ) |
| 1992 | return jsonResult['totalRoutes4'] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1993 | except AssertionError: |
| 1994 | main.log.exception( "" ) |
| 1995 | return None |
| 1996 | except ( TypeError, ValueError ): |
| 1997 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 1998 | return None |
| 1999 | except pexpect.EOF: |
| 2000 | main.log.error( self.name + ": EOF exception found" ) |
| 2001 | main.log.error( self.name + ": " + self.handle.before ) |
| 2002 | main.cleanup() |
| 2003 | main.exit() |
| 2004 | except Exception: |
| 2005 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2006 | main.cleanup() |
| 2007 | main.exit() |
| 2008 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2009 | def intents( self, jsonFormat = True, summary = False, **intentargs): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2010 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2011 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2012 | Obtain intents from the ONOS cli. |
| 2013 | Optional: |
| 2014 | * jsonFormat: Enable output formatting in json, default to True |
| 2015 | * summary: Whether only output the intent summary, defaults to False |
| 2016 | * type: Only output a certain type of intent. This options is valid |
| 2017 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2018 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2019 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2020 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2021 | if summary: |
| 2022 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2023 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2024 | cmdStr += " -j" |
| 2025 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2026 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2027 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2028 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2029 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2030 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2031 | intentType = "" |
| 2032 | # IF we want the summary of a specific intent type |
| 2033 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2034 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2035 | if intentType in jsonResult.keys(): |
| 2036 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2037 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2038 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2039 | return handle |
| 2040 | else: |
| 2041 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2042 | except AssertionError: |
| 2043 | main.log.exception( "" ) |
| 2044 | return None |
| 2045 | except ( TypeError, ValueError ): |
| 2046 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2047 | return None |
| 2048 | except pexpect.EOF: |
| 2049 | main.log.error( self.name + ": EOF exception found" ) |
| 2050 | main.log.error( self.name + ": " + self.handle.before ) |
| 2051 | main.cleanup() |
| 2052 | main.exit() |
| 2053 | except Exception: |
| 2054 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2055 | main.cleanup() |
| 2056 | main.exit() |
| 2057 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2058 | def getIntentState(self, intentsId, intentsJson=None): |
| 2059 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2060 | Description: |
| 2061 | Gets intent state. Accepts a single intent ID (string type) or a |
| 2062 | list of intent IDs. |
| 2063 | Parameters: |
| 2064 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2065 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2066 | Returns: |
| 2067 | Returns the state (string type) of the ID if a single intent ID is |
| 2068 | accepted. |
| 2069 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2070 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2071 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2072 | """ |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2073 | try: |
| 2074 | state = "State is Undefined" |
| 2075 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2076 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2077 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2078 | rawJson = intentsJson |
| 2079 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2080 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2081 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2082 | if intentsId == intent[ 'id' ]: |
| 2083 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2084 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2085 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
| 2086 | " on the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2087 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2088 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2089 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2090 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2091 | stateDict = {} |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2092 | for intents in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2093 | if intentsId[ i ] == intents[ 'id' ]: |
| 2094 | stateDict[ 'state' ] = intents[ 'state' ] |
| 2095 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2096 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2097 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2098 | if len( intentsId ) != len( dictList ): |
| 2099 | main.log.info( "Cannot find some of the intent ID state" ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2100 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2101 | else: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2102 | main.log.info( "Invalid intents ID entry" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2103 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2104 | except ( TypeError, ValueError ): |
| 2105 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2106 | return None |
| 2107 | except pexpect.EOF: |
| 2108 | main.log.error( self.name + ": EOF exception found" ) |
| 2109 | main.log.error( self.name + ": " + self.handle.before ) |
| 2110 | main.cleanup() |
| 2111 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2112 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2113 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2114 | main.cleanup() |
| 2115 | main.exit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2116 | |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2117 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2118 | """ |
| 2119 | Description: |
| 2120 | Check intents state |
| 2121 | Required: |
| 2122 | intentsId - List of intents ID to be checked |
| 2123 | Optional: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2124 | expectedState - Check the expected state(s) of each intents |
| 2125 | state in the list. |
| 2126 | *NOTE: You can pass in a list of expected state, |
| 2127 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2128 | Return: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2129 | Returns main.TRUE only if all intent are the same as expected states |
| 2130 | , otherwise, returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2131 | """ |
| 2132 | try: |
| 2133 | # Generating a dictionary: intent id as a key and state as value |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2134 | returnValue = main.TRUE |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2135 | intentsDict = self.getIntentState( intentsId ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2136 | if len( intentsId ) != len( intentsDict ): |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2137 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2138 | "getting intents state" ) |
| 2139 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2140 | |
| 2141 | if isinstance( expectedState, types.StringType ): |
| 2142 | for intents in intentsDict: |
| 2143 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2144 | main.log.debug( self.name + " : Intent ID - " + |
| 2145 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2146 | " actual state = " + |
| 2147 | intents.get( 'state' ) |
| 2148 | + " does not equal expected state = " |
| 2149 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2150 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2151 | |
| 2152 | elif isinstance( expectedState, types.ListType ): |
| 2153 | for intents in intentsDict: |
| 2154 | if not any( state == intents.get( 'state' ) for state in |
| 2155 | expectedState ): |
| 2156 | main.log.debug( self.name + " : Intent ID - " + |
| 2157 | intents.get( 'id' ) + |
| 2158 | " actual state = " + |
| 2159 | intents.get( 'state' ) + |
| 2160 | " does not equal expected states = " |
| 2161 | + str( expectedState ) ) |
| 2162 | returnValue = main.FALSE |
| 2163 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2164 | if returnValue == main.TRUE: |
| 2165 | main.log.info( self.name + ": All " + |
| 2166 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2167 | " intents are in " + str( expectedState ) + |
| 2168 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2169 | return returnValue |
| 2170 | except TypeError: |
| 2171 | main.log.exception( self.name + ": Object not as expected" ) |
| 2172 | return None |
| 2173 | except pexpect.EOF: |
| 2174 | main.log.error( self.name + ": EOF exception found" ) |
| 2175 | main.log.error( self.name + ": " + self.handle.before ) |
| 2176 | main.cleanup() |
| 2177 | main.exit() |
| 2178 | except Exception: |
| 2179 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2180 | main.cleanup() |
| 2181 | main.exit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2182 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2183 | def compareIntent( self, intentDict ): |
| 2184 | """ |
| 2185 | Description: |
| 2186 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2187 | Return: |
| 2188 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2189 | Arguments: |
| 2190 | intentDict: a dictionary which maps intent ids to intent states |
| 2191 | """ |
| 2192 | try: |
| 2193 | intentsRaw = self.intents() |
| 2194 | intentsJson = json.loads( intentsRaw ) |
| 2195 | intentDictONOS = {} |
| 2196 | for intent in intentsJson: |
| 2197 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
| 2198 | if len( intentDict ) != len( intentDictONOS ): |
| 2199 | main.log.info( self.name + ": expected intent count does not match that in ONOS, " + |
| 2200 | str( len( intentDict ) ) + " expected and " + |
| 2201 | str( len( intentDictONOS ) ) + " actual" ) |
| 2202 | return main.FALSE |
| 2203 | returnValue = main.TRUE |
| 2204 | for intentID in intentDict.keys(): |
| 2205 | if not intentID in intentDictONOS.keys(): |
| 2206 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2207 | returnValue = main.FALSE |
| 2208 | elif intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2209 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2210 | " expected state is " + intentDict[ intentID ] + |
| 2211 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2212 | returnValue = main.FALSE |
| 2213 | if returnValue == main.TRUE: |
| 2214 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2215 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2216 | except KeyError: |
| 2217 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2218 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2219 | except ( TypeError, ValueError ): |
| 2220 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2221 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2222 | except pexpect.EOF: |
| 2223 | main.log.error( self.name + ": EOF exception found" ) |
| 2224 | main.log.error( self.name + ": " + self.handle.before ) |
| 2225 | main.cleanup() |
| 2226 | main.exit() |
| 2227 | except Exception: |
| 2228 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2229 | main.cleanup() |
| 2230 | main.exit() |
| 2231 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2232 | def checkIntentSummary( self, timeout=60 ): |
| 2233 | """ |
| 2234 | Description: |
| 2235 | Check the number of installed intents. |
| 2236 | Optional: |
| 2237 | timeout - the timeout for pexcept |
| 2238 | Return: |
| 2239 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2240 | , otherwise, returns main.FALSE. |
| 2241 | """ |
| 2242 | |
| 2243 | try: |
| 2244 | cmd = "intents -s -j" |
| 2245 | |
| 2246 | # Check response if something wrong |
| 2247 | response = self.sendline( cmd, timeout=timeout ) |
| 2248 | if response == None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2249 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2250 | response = json.loads( response ) |
| 2251 | |
| 2252 | # get total and installed number, see if they are match |
| 2253 | allState = response.get( 'all' ) |
| 2254 | if allState.get('total') == allState.get('installed'): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2255 | 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] | 2256 | return main.TRUE |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2257 | 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] | 2258 | return main.FALSE |
| 2259 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2260 | except ( TypeError, ValueError ): |
| 2261 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2262 | return None |
| 2263 | except pexpect.EOF: |
| 2264 | main.log.error( self.name + ": EOF exception found" ) |
| 2265 | main.log.error( self.name + ": " + self.handle.before ) |
| 2266 | main.cleanup() |
| 2267 | main.exit() |
| 2268 | except Exception: |
| 2269 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2270 | main.cleanup() |
| 2271 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2272 | except pexpect.TIMEOUT: |
| 2273 | main.log.error( self.name + ": ONOS timeout" ) |
| 2274 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2275 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2276 | def flows( self, state="", jsonFormat=True, timeout=60, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2277 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2278 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2279 | * jsonFormat: enable output formatting in json |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2280 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2281 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2282 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2283 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2284 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2285 | if jsonFormat: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2286 | cmdStr += " -j " |
| 2287 | cmdStr += state |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2288 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2289 | assert "Command not found:" not in handle, handle |
| 2290 | if re.search( "Error:", handle ): |
| 2291 | main.log.error( self.name + ": flows() response: " + |
| 2292 | str( handle ) ) |
| 2293 | return handle |
| 2294 | except AssertionError: |
| 2295 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2296 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2297 | except TypeError: |
| 2298 | main.log.exception( self.name + ": Object not as expected" ) |
| 2299 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2300 | except pexpect.TIMEOUT: |
| 2301 | main.log.error( self.name + ": ONOS timeout" ) |
| 2302 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2303 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2304 | main.log.error( self.name + ": EOF exception found" ) |
| 2305 | main.log.error( self.name + ": " + self.handle.before ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2306 | main.cleanup() |
| 2307 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2308 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2309 | main.log.exception( self.name + ": Uncaught exception!" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2310 | main.cleanup() |
| 2311 | main.exit() |
| 2312 | |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2313 | def checkFlowCount(self, min=0, timeout=60 ): |
| 2314 | count = int(self.getTotalFlowsNum( timeout=timeout )) |
| 2315 | return count if (count > min) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2316 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2317 | def checkFlowsState( self, isPENDING=True, timeout=60,noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2318 | """ |
| 2319 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2320 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2321 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2322 | if the count of those states is 0, which means all current flows |
| 2323 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2324 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2325 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2326 | Return: |
| 2327 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2328 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2329 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2330 | """ |
| 2331 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2332 | states = ["PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED"] |
| 2333 | checkedStates = [] |
| 2334 | statesCount = [0, 0, 0, 0] |
| 2335 | for s in states: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2336 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2337 | if rawFlows: |
| 2338 | # if we didn't get flows or flows function return None, we should return |
| 2339 | # main.Flase |
| 2340 | checkedStates.append( json.loads( rawFlows ) ) |
| 2341 | else: |
| 2342 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2343 | for i in range( len( states ) ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2344 | for c in checkedStates[i]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2345 | try: |
| 2346 | statesCount[i] += int( c.get( "flowCount" ) ) |
| 2347 | except TypeError: |
| 2348 | main.log.exception( "Json object not as expected" ) |
| 2349 | main.log.info( states[i] + " flows: " + str( statesCount[i] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2350 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2351 | # We want to count PENDING_ADD if isPENDING is true |
| 2352 | if isPENDING: |
| 2353 | if statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2354 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2355 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2356 | if statesCount[0] + statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2357 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2358 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2359 | except ( TypeError, ValueError ): |
| 2360 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2361 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2362 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2363 | except AssertionError: |
| 2364 | main.log.exception( "" ) |
| 2365 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2366 | except pexpect.EOF: |
| 2367 | main.log.error( self.name + ": EOF exception found" ) |
| 2368 | main.log.error( self.name + ": " + self.handle.before ) |
| 2369 | main.cleanup() |
| 2370 | main.exit() |
| 2371 | except Exception: |
| 2372 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2373 | main.cleanup() |
| 2374 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2375 | except pexpect.TIMEOUT: |
| 2376 | main.log.error( self.name + ": ONOS timeout" ) |
| 2377 | return None |
| 2378 | |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2379 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2380 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2381 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2382 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2383 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2384 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2385 | a specific point-to-point intent definition |
| 2386 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2387 | * ingress: specify source dpid |
| 2388 | * egress: specify destination dpid |
| 2389 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2390 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2391 | * offset: the keyOffset is where the next batch of intents |
| 2392 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2393 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2394 | * getResponse: If set to True, function will return ONOS response. |
| 2395 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2396 | Returns: If failed to push test intents, it will returen None, |
| 2397 | if successful, return true. |
| 2398 | Timeout expection will return None, |
| 2399 | TypeError will return false |
| 2400 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2401 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2402 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2403 | if background: |
| 2404 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2405 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2406 | back = "" |
| 2407 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2408 | ingress, |
| 2409 | egress, |
| 2410 | batchSize, |
| 2411 | offset, |
| 2412 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2413 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2414 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2415 | main.log.info( response ) |
| 2416 | if response == None: |
| 2417 | return None |
| 2418 | |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2419 | if getResponse: |
| 2420 | return response |
| 2421 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2422 | # TODO: We should handle if there is failure in installation |
| 2423 | return main.TRUE |
| 2424 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2425 | except AssertionError: |
| 2426 | main.log.exception( "" ) |
| 2427 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2428 | except pexpect.TIMEOUT: |
| 2429 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2430 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2431 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2432 | main.log.error( self.name + ": EOF exception found" ) |
| 2433 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2434 | main.cleanup() |
| 2435 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2436 | except TypeError: |
| 2437 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2438 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2439 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2440 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2441 | main.cleanup() |
| 2442 | main.exit() |
| 2443 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2444 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2445 | """ |
| 2446 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2447 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2448 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2449 | The number of ADDED flows |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2450 | """ |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2451 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2452 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2453 | # get total added flows number |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2454 | cmd = "flows -s|grep ADDED|wc -l" |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2455 | totalFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2456 | |
| 2457 | if totalFlows == None: |
| 2458 | # if timeout, we will get total number of all flows, and subtract other states |
| 2459 | states = ["PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED"] |
| 2460 | checkedStates = [] |
| 2461 | totalFlows = 0 |
| 2462 | statesCount = [0, 0, 0, 0] |
| 2463 | |
| 2464 | # get total flows from summary |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2465 | response = json.loads( self.sendline( "summary -j", timeout=timeout, noExit=noExit ) ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2466 | totalFlows = int( response.get("flows") ) |
| 2467 | |
| 2468 | for s in states: |
| 2469 | rawFlows = self.flows( state=s, timeout = timeout ) |
| 2470 | if rawFlows == None: |
| 2471 | # if timeout, return the total flows number from summary command |
| 2472 | return totalFlows |
| 2473 | checkedStates.append( json.loads( rawFlows ) ) |
| 2474 | |
| 2475 | # Calculate ADDED flows number, equal total subtracts others |
| 2476 | for i in range( len( states ) ): |
| 2477 | for c in checkedStates[i]: |
| 2478 | try: |
| 2479 | statesCount[i] += int( c.get( "flowCount" ) ) |
| 2480 | except TypeError: |
| 2481 | main.log.exception( "Json object not as expected" ) |
| 2482 | totalFlows = totalFlows - int( statesCount[i] ) |
| 2483 | main.log.info( states[i] + " flows: " + str( statesCount[i] ) ) |
| 2484 | |
| 2485 | return totalFlows |
| 2486 | |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2487 | return int(totalFlows) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2488 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2489 | except ( TypeError, ValueError ): |
| 2490 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2491 | return None |
| 2492 | except pexpect.EOF: |
| 2493 | main.log.error( self.name + ": EOF exception found" ) |
| 2494 | main.log.error( self.name + ": " + self.handle.before ) |
| 2495 | main.cleanup() |
| 2496 | main.exit() |
| 2497 | except Exception: |
| 2498 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2499 | main.cleanup() |
| 2500 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2501 | except pexpect.TIMEOUT: |
| 2502 | main.log.error( self.name + ": ONOS timeout" ) |
| 2503 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2504 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2505 | def getTotalIntentsNum( self, timeout=60 ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2506 | """ |
| 2507 | Description: |
| 2508 | Get the total number of intents, include every states. |
| 2509 | Return: |
| 2510 | The number of intents |
| 2511 | """ |
| 2512 | try: |
| 2513 | cmd = "summary -j" |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2514 | response = self.sendline( cmd, timeout=timeout ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2515 | if response == None: |
| 2516 | return -1 |
| 2517 | response = json.loads( response ) |
| 2518 | return int( response.get("intents") ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2519 | except ( TypeError, ValueError ): |
| 2520 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2521 | return None |
| 2522 | except pexpect.EOF: |
| 2523 | main.log.error( self.name + ": EOF exception found" ) |
| 2524 | main.log.error( self.name + ": " + self.handle.before ) |
| 2525 | main.cleanup() |
| 2526 | main.exit() |
| 2527 | except Exception: |
| 2528 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2529 | main.cleanup() |
| 2530 | main.exit() |
| 2531 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2532 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2533 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2534 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2535 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2536 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2537 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2538 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2539 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2540 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2541 | cmdStr += " -j" |
| 2542 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2543 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2544 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2545 | except AssertionError: |
| 2546 | main.log.exception( "" ) |
| 2547 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2548 | except TypeError: |
| 2549 | main.log.exception( self.name + ": Object not as expected" ) |
| 2550 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2551 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2552 | main.log.error( self.name + ": EOF exception found" ) |
| 2553 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2554 | main.cleanup() |
| 2555 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2556 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2557 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2558 | main.cleanup() |
| 2559 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2560 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2561 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2562 | """ |
| 2563 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2564 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2565 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2566 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2567 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2568 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2569 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2570 | cmdStr += " -j" |
| 2571 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2572 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2573 | if handle: |
| 2574 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2575 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2576 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2577 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2578 | else: |
| 2579 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2580 | except AssertionError: |
| 2581 | main.log.exception( "" ) |
| 2582 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2583 | except TypeError: |
| 2584 | main.log.exception( self.name + ": Object not as expected" ) |
| 2585 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2586 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2587 | main.log.error( self.name + ": EOF exception found" ) |
| 2588 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2589 | main.cleanup() |
| 2590 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2591 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2592 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2593 | main.cleanup() |
| 2594 | main.exit() |
| 2595 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2596 | # Wrapper functions **************** |
| 2597 | # Wrapper functions use existing driver |
| 2598 | # functions and extends their use case. |
| 2599 | # For example, we may use the output of |
| 2600 | # a normal driver function, and parse it |
| 2601 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2602 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2603 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2604 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2605 | Description: |
| 2606 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2607 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2608 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2609 | # Obtain output of intents function |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2610 | intentsStr = self.intents(jsonFormat=False) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2611 | intentIdList = [] |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2612 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2613 | # Parse the intents output for ID's |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2614 | intentsList = [ s.strip() for s in intentsStr.splitlines() ] |
| 2615 | for intents in intentsList: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2616 | match = re.search('id=0x([\da-f]+),', intents) |
| 2617 | if match: |
| 2618 | tmpId = match.group()[3:-1] |
| 2619 | intentIdList.append( tmpId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2620 | return intentIdList |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2621 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2622 | except TypeError: |
| 2623 | main.log.exception( self.name + ": Object not as expected" ) |
| 2624 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2625 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2626 | main.log.error( self.name + ": EOF exception found" ) |
| 2627 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2628 | main.cleanup() |
| 2629 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2630 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2631 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2632 | main.cleanup() |
| 2633 | main.exit() |
| 2634 | |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2635 | def FlowAddedCount( self, deviceId ): |
| 2636 | """ |
| 2637 | Determine the number of flow rules for the given device id that are |
| 2638 | in the added state |
| 2639 | """ |
| 2640 | try: |
| 2641 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2642 | "grep 'state=ADDED' | wc -l" |
| 2643 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2644 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2645 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2646 | except AssertionError: |
| 2647 | main.log.exception( "" ) |
| 2648 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2649 | except pexpect.EOF: |
| 2650 | main.log.error( self.name + ": EOF exception found" ) |
| 2651 | main.log.error( self.name + ": " + self.handle.before ) |
| 2652 | main.cleanup() |
| 2653 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2654 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2655 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2656 | main.cleanup() |
| 2657 | main.exit() |
| 2658 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2659 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2660 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2661 | Use 'devices' function to obtain list of all devices |
| 2662 | and parse the result to obtain a list of all device |
| 2663 | id's. Returns this list. Returns empty list if no |
| 2664 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2665 | List is ordered sequentially |
| 2666 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2667 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2668 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2669 | the ids. By obtaining the list of device ids on the fly, |
| 2670 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2671 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2672 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2673 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2674 | devicesStr = self.devices( jsonFormat=False ) |
| 2675 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2676 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2677 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2678 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2679 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2680 | |
| 2681 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2682 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2683 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2684 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2685 | # Split list further into arguments before and after string |
| 2686 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2687 | # append to idList |
| 2688 | for arg in tempList: |
| 2689 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 2690 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2691 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2692 | except TypeError: |
| 2693 | main.log.exception( self.name + ": Object not as expected" ) |
| 2694 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2695 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2696 | main.log.error( self.name + ": EOF exception found" ) |
| 2697 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2698 | main.cleanup() |
| 2699 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2700 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2701 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2702 | main.cleanup() |
| 2703 | main.exit() |
| 2704 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2705 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2706 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2707 | Uses 'nodes' function to obtain list of all nodes |
| 2708 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2709 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2710 | Returns: |
| 2711 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2712 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2713 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2714 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2715 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2716 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 2717 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2718 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2719 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2720 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2721 | nodesJson = json.loads( nodesStr ) |
| 2722 | idList = [ node.get('id') for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2723 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2724 | except ( TypeError, ValueError ): |
| 2725 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2726 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2727 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2728 | main.log.error( self.name + ": EOF exception found" ) |
| 2729 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2730 | main.cleanup() |
| 2731 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2732 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2733 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2734 | main.cleanup() |
| 2735 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2736 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2737 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2738 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2739 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 2740 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2741 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2742 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2743 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2744 | return None |
| 2745 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2746 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2747 | rawDevices = self.devices() |
| 2748 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2749 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2750 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2751 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 2752 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2753 | return device |
| 2754 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2755 | except ( TypeError, ValueError ): |
| 2756 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2757 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2758 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2759 | main.log.error( self.name + ": EOF exception found" ) |
| 2760 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2761 | main.cleanup() |
| 2762 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2763 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2764 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2765 | main.cleanup() |
| 2766 | main.exit() |
| 2767 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2768 | def getTopology( self, topologyOutput ): |
| 2769 | """ |
| 2770 | Definition: |
| 2771 | Loads a json topology output |
| 2772 | Return: |
| 2773 | topology = current ONOS topology |
| 2774 | """ |
| 2775 | import json |
| 2776 | try: |
| 2777 | # either onos:topology or 'topology' will work in CLI |
| 2778 | topology = json.loads(topologyOutput) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2779 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2780 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2781 | except ( TypeError, ValueError ): |
| 2782 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 2783 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2784 | except pexpect.EOF: |
| 2785 | main.log.error( self.name + ": EOF exception found" ) |
| 2786 | main.log.error( self.name + ": " + self.handle.before ) |
| 2787 | main.cleanup() |
| 2788 | main.exit() |
| 2789 | except Exception: |
| 2790 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2791 | main.cleanup() |
| 2792 | main.exit() |
| 2793 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2794 | def checkStatus(self, numoswitch, numolink, numoctrl = -1, logLevel="info"): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2795 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2796 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2797 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2798 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2799 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2800 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2801 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2802 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2803 | logLevel = level to log to. |
| 2804 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2805 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2806 | Returns: main.TRUE if the number of switches and links are correct, |
| 2807 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2808 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2809 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2810 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2811 | try: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2812 | topology = self.getTopology( self.topology() ) |
| 2813 | summary = json.loads( self.summary() ) |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 2814 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2815 | if topology == {} or topology == None or summary == {} or summary == None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2816 | return main.ERROR |
| 2817 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2818 | # Is the number of switches is what we expected |
| 2819 | devices = topology.get( 'devices', False ) |
| 2820 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2821 | nodes = summary.get( 'nodes', False ) |
| 2822 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2823 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2824 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2825 | # Is the number of links is what we expected |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2826 | linkCheck = ( int( links ) == int( numolink ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2827 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 2828 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2829 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2830 | output = output + "The number of links and switches match "\ |
| 2831 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2832 | result = main.TRUE |
| 2833 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2834 | output = output + \ |
| 2835 | "The number of links and switches does not match " + \ |
| 2836 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2837 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2838 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 2839 | output = output + " (%i expected) " % int( numoswitch ) |
| 2840 | output = output + "and %i links " % int( links ) |
| 2841 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 2842 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2843 | output = output + "and %i controllers " % int( nodes ) |
| 2844 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2845 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2846 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2847 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2848 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2849 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2850 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2851 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2852 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2853 | main.log.error( self.name + ": EOF exception found" ) |
| 2854 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2855 | main.cleanup() |
| 2856 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2857 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2858 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2859 | main.cleanup() |
| 2860 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2861 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2862 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2863 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2864 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2865 | deviceId must be the id of a device as seen in the onos devices command |
| 2866 | 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] | 2867 | role must be either master, standby, or none |
| 2868 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2869 | Returns: |
| 2870 | main.TRUE or main.FALSE based on argument verification and |
| 2871 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2872 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2873 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2874 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2875 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2876 | cmdStr = "device-role " +\ |
| 2877 | str( deviceId ) + " " +\ |
| 2878 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2879 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2880 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2881 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2882 | if re.search( "Error", handle ): |
| 2883 | # end color output to escape any colours |
| 2884 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2885 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2886 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2887 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2888 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2889 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2890 | main.log.error( "Invalid 'role' given to device_role(). " + |
| 2891 | "Value was '" + str(role) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2892 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2893 | except AssertionError: |
| 2894 | main.log.exception( "" ) |
| 2895 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2896 | except TypeError: |
| 2897 | main.log.exception( self.name + ": Object not as expected" ) |
| 2898 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2899 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2900 | main.log.error( self.name + ": EOF exception found" ) |
| 2901 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2902 | main.cleanup() |
| 2903 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2904 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2905 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2906 | main.cleanup() |
| 2907 | main.exit() |
| 2908 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2909 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2910 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2911 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2912 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2913 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2914 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2915 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2916 | cmdStr = "clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2917 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2918 | cmdStr += " -j" |
| 2919 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2920 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2921 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2922 | except AssertionError: |
| 2923 | main.log.exception( "" ) |
| 2924 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2925 | except TypeError: |
| 2926 | main.log.exception( self.name + ": Object not as expected" ) |
| 2927 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2928 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2929 | main.log.error( self.name + ": EOF exception found" ) |
| 2930 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2931 | main.cleanup() |
| 2932 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2933 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2934 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2935 | main.cleanup() |
| 2936 | main.exit() |
| 2937 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2938 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2939 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2940 | CLI command to get the current leader for the Election test application |
| 2941 | NOTE: Requires installation of the onos-app-election feature |
| 2942 | Returns: Node IP of the leader if one exists |
| 2943 | None if none exists |
| 2944 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2945 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2946 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2947 | cmdStr = "election-test-leader" |
| 2948 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2949 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2950 | # Leader |
| 2951 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2952 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2953 | nodeSearch = re.search( leaderPattern, response ) |
| 2954 | if nodeSearch: |
| 2955 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2956 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2957 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2958 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2959 | # no leader |
| 2960 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2961 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2962 | nullSearch = re.search( nullPattern, response ) |
| 2963 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2964 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2965 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2966 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2967 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 2968 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 2969 | ": " + "unexpected response" ) |
| 2970 | main.log.error( repr( response ) ) |
| 2971 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2972 | except AssertionError: |
| 2973 | main.log.exception( "" ) |
| 2974 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2975 | except TypeError: |
| 2976 | main.log.exception( self.name + ": Object not as expected" ) |
| 2977 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2978 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2979 | main.log.error( self.name + ": EOF exception found" ) |
| 2980 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2981 | main.cleanup() |
| 2982 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2983 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2984 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2985 | main.cleanup() |
| 2986 | main.exit() |
| 2987 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2988 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2989 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2990 | CLI command to run for leadership of the Election test application. |
| 2991 | NOTE: Requires installation of the onos-app-election feature |
| 2992 | Returns: Main.TRUE on success |
| 2993 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2994 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2995 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2996 | cmdStr = "election-test-run" |
| 2997 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2998 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2999 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3000 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3001 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3002 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3003 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3004 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3005 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3006 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3007 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3008 | main.log.error( "Error in electionTestRun on " + self.name + |
| 3009 | ": " + "unexpected response" ) |
| 3010 | main.log.error( repr( response ) ) |
| 3011 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3012 | except AssertionError: |
| 3013 | main.log.exception( "" ) |
| 3014 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3015 | except TypeError: |
| 3016 | main.log.exception( self.name + ": Object not as expected" ) |
| 3017 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3018 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3019 | main.log.error( self.name + ": EOF exception found" ) |
| 3020 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3021 | main.cleanup() |
| 3022 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3023 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3024 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3025 | main.cleanup() |
| 3026 | main.exit() |
| 3027 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3028 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3029 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3030 | * CLI command to withdraw the local node from leadership election for |
| 3031 | * the Election test application. |
| 3032 | #NOTE: Requires installation of the onos-app-election feature |
| 3033 | Returns: Main.TRUE on success |
| 3034 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3035 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3036 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3037 | cmdStr = "election-test-withdraw" |
| 3038 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3039 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3040 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3041 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3042 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3043 | if re.search( successPattern, response ): |
| 3044 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3045 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3046 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3047 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3048 | main.log.error( "Error in electionTestWithdraw on " + |
| 3049 | self.name + ": " + "unexpected response" ) |
| 3050 | main.log.error( repr( response ) ) |
| 3051 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3052 | except AssertionError: |
| 3053 | main.log.exception( "" ) |
| 3054 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3055 | except TypeError: |
| 3056 | main.log.exception( self.name + ": Object not as expected" ) |
| 3057 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3058 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3059 | main.log.error( self.name + ": EOF exception found" ) |
| 3060 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3061 | main.cleanup() |
| 3062 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3063 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3064 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3065 | main.cleanup() |
| 3066 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3067 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3068 | def getDevicePortsEnabledCount( self, dpid ): |
| 3069 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3070 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3071 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3072 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3073 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3074 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 3075 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3076 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3077 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3078 | if re.search( "No such device", output ): |
| 3079 | main.log.error( "Error in getting ports" ) |
| 3080 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3081 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3082 | except AssertionError: |
| 3083 | main.log.exception( "" ) |
| 3084 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3085 | except TypeError: |
| 3086 | main.log.exception( self.name + ": Object not as expected" ) |
| 3087 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3088 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3089 | main.log.error( self.name + ": EOF exception found" ) |
| 3090 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3091 | main.cleanup() |
| 3092 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3093 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3094 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3095 | main.cleanup() |
| 3096 | main.exit() |
| 3097 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3098 | def getDeviceLinksActiveCount( self, dpid ): |
| 3099 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3100 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3101 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3102 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3103 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3104 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3105 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3106 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3107 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3108 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3109 | main.log.error( "Error in getting ports " ) |
| 3110 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3111 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3112 | except AssertionError: |
| 3113 | main.log.exception( "" ) |
| 3114 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3115 | except TypeError: |
| 3116 | main.log.exception( self.name + ": Object not as expected" ) |
| 3117 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3118 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3119 | main.log.error( self.name + ": EOF exception found" ) |
| 3120 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3121 | main.cleanup() |
| 3122 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3123 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3124 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3125 | main.cleanup() |
| 3126 | main.exit() |
| 3127 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3128 | def getAllIntentIds( self ): |
| 3129 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3130 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3131 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3132 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3133 | cmdStr = "onos:intents | grep id=" |
| 3134 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3135 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3136 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3137 | if re.search( "Error", output ): |
| 3138 | main.log.error( "Error in getting ports" ) |
| 3139 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3140 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3141 | except AssertionError: |
| 3142 | main.log.exception( "" ) |
| 3143 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3144 | except TypeError: |
| 3145 | main.log.exception( self.name + ": Object not as expected" ) |
| 3146 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3147 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3148 | main.log.error( self.name + ": EOF exception found" ) |
| 3149 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3150 | main.cleanup() |
| 3151 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3152 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3153 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3154 | main.cleanup() |
| 3155 | main.exit() |
| 3156 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3157 | def intentSummary( self ): |
| 3158 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3159 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3160 | """ |
| 3161 | try: |
| 3162 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3163 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3164 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3165 | states.append( intent.get( 'state', None ) ) |
| 3166 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3167 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3168 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3169 | except ( TypeError, ValueError ): |
| 3170 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3171 | return None |
| 3172 | except pexpect.EOF: |
| 3173 | main.log.error( self.name + ": EOF exception found" ) |
| 3174 | main.log.error( self.name + ": " + self.handle.before ) |
| 3175 | main.cleanup() |
| 3176 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3177 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3178 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3179 | main.cleanup() |
| 3180 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3181 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3182 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3183 | """ |
| 3184 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3185 | Optional argument: |
| 3186 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3187 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3188 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3189 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3190 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3191 | cmdStr += " -j" |
| 3192 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3193 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3194 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3195 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3196 | except AssertionError: |
| 3197 | main.log.exception( "" ) |
| 3198 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3199 | except TypeError: |
| 3200 | main.log.exception( self.name + ": Object not as expected" ) |
| 3201 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3202 | except pexpect.EOF: |
| 3203 | main.log.error( self.name + ": EOF exception found" ) |
| 3204 | main.log.error( self.name + ": " + self.handle.before ) |
| 3205 | main.cleanup() |
| 3206 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3207 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3208 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3209 | main.cleanup() |
| 3210 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3211 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3212 | def leaderCandidates( self, jsonFormat=True ): |
| 3213 | """ |
| 3214 | Returns the output of the leaders -c command. |
| 3215 | Optional argument: |
| 3216 | * jsonFormat - boolean indicating if you want output in json |
| 3217 | """ |
| 3218 | try: |
| 3219 | cmdStr = "onos:leaders -c" |
| 3220 | if jsonFormat: |
| 3221 | cmdStr += " -j" |
| 3222 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3223 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3224 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3225 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3226 | except AssertionError: |
| 3227 | main.log.exception( "" ) |
| 3228 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3229 | except TypeError: |
| 3230 | main.log.exception( self.name + ": Object not as expected" ) |
| 3231 | return None |
| 3232 | except pexpect.EOF: |
| 3233 | main.log.error( self.name + ": EOF exception found" ) |
| 3234 | main.log.error( self.name + ": " + self.handle.before ) |
| 3235 | main.cleanup() |
| 3236 | main.exit() |
| 3237 | except Exception: |
| 3238 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3239 | main.cleanup() |
| 3240 | main.exit() |
| 3241 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3242 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3243 | """ |
| 3244 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
| 3245 | topic parameter and an empty list if the topic doesn't exist |
| 3246 | If no leader is elected leader in the returned list will be "none" |
| 3247 | Returns None if there is a type error processing the json object |
| 3248 | """ |
| 3249 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3250 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3251 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3252 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3253 | assert "Command not found:" not in rawOutput, rawOutput |
| 3254 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3255 | results = [] |
| 3256 | for dict in output: |
| 3257 | if dict["topic"] == topic: |
| 3258 | leader = dict["leader"] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3259 | candidates = re.split( ", ", dict["candidates"][1:-1] ) |
| 3260 | results.append( leader ) |
| 3261 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3262 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3263 | except AssertionError: |
| 3264 | main.log.exception( "" ) |
| 3265 | return None |
| 3266 | except ( TypeError, ValueError ): |
| 3267 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3268 | return None |
| 3269 | except pexpect.EOF: |
| 3270 | main.log.error( self.name + ": EOF exception found" ) |
| 3271 | main.log.error( self.name + ": " + self.handle.before ) |
| 3272 | main.cleanup() |
| 3273 | main.exit() |
| 3274 | except Exception: |
| 3275 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3276 | main.cleanup() |
| 3277 | main.exit() |
| 3278 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3279 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3280 | """ |
| 3281 | Returns the output of the intent Pending map. |
| 3282 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3283 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3284 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3285 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3286 | cmdStr += " -j" |
| 3287 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3288 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3289 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3290 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3291 | except AssertionError: |
| 3292 | main.log.exception( "" ) |
| 3293 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3294 | except TypeError: |
| 3295 | main.log.exception( self.name + ": Object not as expected" ) |
| 3296 | return None |
| 3297 | except pexpect.EOF: |
| 3298 | main.log.error( self.name + ": EOF exception found" ) |
| 3299 | main.log.error( self.name + ": " + self.handle.before ) |
| 3300 | main.cleanup() |
| 3301 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3302 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3303 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3304 | main.cleanup() |
| 3305 | main.exit() |
| 3306 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3307 | def partitions( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3308 | """ |
| 3309 | Returns the output of the raft partitions command for ONOS. |
| 3310 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3311 | # Sample JSON |
| 3312 | # { |
| 3313 | # "leader": "tcp://10.128.30.11:7238", |
| 3314 | # "members": [ |
| 3315 | # "tcp://10.128.30.11:7238", |
| 3316 | # "tcp://10.128.30.17:7238", |
| 3317 | # "tcp://10.128.30.13:7238", |
| 3318 | # ], |
| 3319 | # "name": "p1", |
| 3320 | # "term": 3 |
| 3321 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3322 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3323 | cmdStr = "onos:partitions" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3324 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3325 | cmdStr += " -j" |
| 3326 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3327 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3328 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3329 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3330 | except AssertionError: |
| 3331 | main.log.exception( "" ) |
| 3332 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3333 | except TypeError: |
| 3334 | main.log.exception( self.name + ": Object not as expected" ) |
| 3335 | return None |
| 3336 | except pexpect.EOF: |
| 3337 | main.log.error( self.name + ": EOF exception found" ) |
| 3338 | main.log.error( self.name + ": " + self.handle.before ) |
| 3339 | main.cleanup() |
| 3340 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3341 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3342 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3343 | main.cleanup() |
| 3344 | main.exit() |
| 3345 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3346 | def apps( self, jsonFormat=True ): |
| 3347 | """ |
| 3348 | Returns the output of the apps command for ONOS. This command lists |
| 3349 | information about installed ONOS applications |
| 3350 | """ |
| 3351 | # Sample JSON object |
| 3352 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
| 3353 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3354 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
| 3355 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
| 3356 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3357 | cmdStr = "onos:apps" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3358 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3359 | cmdStr += " -j" |
| 3360 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3361 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3362 | assert "Command not found:" not in output, output |
| 3363 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3364 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3365 | # FIXME: look at specific exceptions/Errors |
| 3366 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3367 | main.log.exception( "Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3368 | return None |
| 3369 | except TypeError: |
| 3370 | main.log.exception( self.name + ": Object not as expected" ) |
| 3371 | return None |
| 3372 | except pexpect.EOF: |
| 3373 | main.log.error( self.name + ": EOF exception found" ) |
| 3374 | main.log.error( self.name + ": " + self.handle.before ) |
| 3375 | main.cleanup() |
| 3376 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3377 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3378 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3379 | main.cleanup() |
| 3380 | main.exit() |
| 3381 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3382 | def appStatus( self, appName ): |
| 3383 | """ |
| 3384 | Uses the onos:apps cli command to return the status of an application. |
| 3385 | Returns: |
| 3386 | "ACTIVE" - If app is installed and activated |
| 3387 | "INSTALLED" - If app is installed and deactivated |
| 3388 | "UNINSTALLED" - If app is not installed |
| 3389 | None - on error |
| 3390 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3391 | try: |
| 3392 | if not isinstance( appName, types.StringType ): |
| 3393 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3394 | " a string" ) |
| 3395 | return None |
| 3396 | output = self.apps( jsonFormat=True ) |
| 3397 | appsJson = json.loads( output ) |
| 3398 | state = None |
| 3399 | for app in appsJson: |
| 3400 | if appName == app.get('name'): |
| 3401 | state = app.get('state') |
| 3402 | break |
| 3403 | if state == "ACTIVE" or state == "INSTALLED": |
| 3404 | return state |
| 3405 | elif state is None: |
| 3406 | return "UNINSTALLED" |
| 3407 | elif state: |
| 3408 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3409 | str( state ) ) |
| 3410 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3411 | except ( TypeError, ValueError ): |
| 3412 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 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 | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3420 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3421 | main.cleanup() |
| 3422 | main.exit() |
| 3423 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3424 | def app( self, appName, option ): |
| 3425 | """ |
| 3426 | Interacts with the app command for ONOS. This command manages |
| 3427 | application inventory. |
| 3428 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3429 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3430 | # Validate argument types |
| 3431 | valid = True |
| 3432 | if not isinstance( appName, types.StringType ): |
| 3433 | main.log.error( self.name + ".app(): appName must be a " + |
| 3434 | "string" ) |
| 3435 | valid = False |
| 3436 | if not isinstance( option, types.StringType ): |
| 3437 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3438 | valid = False |
| 3439 | if not valid: |
| 3440 | return main.FALSE |
| 3441 | # Validate Option |
| 3442 | option = option.lower() |
| 3443 | # NOTE: Install may become a valid option |
| 3444 | if option == "activate": |
| 3445 | pass |
| 3446 | elif option == "deactivate": |
| 3447 | pass |
| 3448 | elif option == "uninstall": |
| 3449 | pass |
| 3450 | else: |
| 3451 | # Invalid option |
| 3452 | main.log.error( "The ONOS app command argument only takes " + |
| 3453 | "the values: (activate|deactivate|uninstall)" + |
| 3454 | "; was given '" + option + "'") |
| 3455 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3456 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3457 | output = self.sendline( cmdStr ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3458 | if "Error executing command" in output: |
| 3459 | main.log.error( "Error in processing onos:app command: " + |
| 3460 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3461 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3462 | elif "No such application" in output: |
| 3463 | main.log.error( "The application '" + appName + |
| 3464 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3465 | return main.FALSE |
| 3466 | elif "Command not found:" in output: |
| 3467 | main.log.error( "Error in processing onos:app command: " + |
| 3468 | str( output ) ) |
| 3469 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3470 | elif "Unsupported command:" in output: |
| 3471 | main.log.error( "Incorrect command given to 'app': " + |
| 3472 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3473 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3474 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3475 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3476 | return main.TRUE |
| 3477 | except TypeError: |
| 3478 | main.log.exception( self.name + ": Object not as expected" ) |
| 3479 | return main.ERROR |
| 3480 | except pexpect.EOF: |
| 3481 | main.log.error( self.name + ": EOF exception found" ) |
| 3482 | main.log.error( self.name + ": " + self.handle.before ) |
| 3483 | main.cleanup() |
| 3484 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3485 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3486 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3487 | main.cleanup() |
| 3488 | main.exit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3489 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3490 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3491 | """ |
| 3492 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3493 | appName is the hierarchical app name, not the feature name |
| 3494 | If check is True, method will check the status of the app after the |
| 3495 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3496 | Returns main.TRUE if the command was successfully sent |
| 3497 | main.FALSE if the cli responded with an error or given |
| 3498 | incorrect input |
| 3499 | """ |
| 3500 | try: |
| 3501 | if not isinstance( appName, types.StringType ): |
| 3502 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3503 | " a string" ) |
| 3504 | return main.FALSE |
| 3505 | status = self.appStatus( appName ) |
| 3506 | if status == "INSTALLED": |
| 3507 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3508 | if check and response == main.TRUE: |
| 3509 | for i in range(10): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3510 | status = self.appStatus( appName ) |
| 3511 | if status == "ACTIVE": |
| 3512 | return main.TRUE |
| 3513 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3514 | main.log.debug( "The state of application " + |
| 3515 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3516 | time.sleep( 1 ) |
| 3517 | return main.FALSE |
| 3518 | else: # not 'check' or command didn't succeed |
| 3519 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3520 | elif status == "ACTIVE": |
| 3521 | return main.TRUE |
| 3522 | elif status == "UNINSTALLED": |
| 3523 | main.log.error( self.name + ": Tried to activate the " + |
| 3524 | "application '" + appName + "' which is not " + |
| 3525 | "installed." ) |
| 3526 | else: |
| 3527 | main.log.error( "Unexpected return value from appStatus: " + |
| 3528 | str( status ) ) |
| 3529 | return main.ERROR |
| 3530 | except TypeError: |
| 3531 | main.log.exception( self.name + ": Object not as expected" ) |
| 3532 | return main.ERROR |
| 3533 | except pexpect.EOF: |
| 3534 | main.log.error( self.name + ": EOF exception found" ) |
| 3535 | main.log.error( self.name + ": " + self.handle.before ) |
| 3536 | main.cleanup() |
| 3537 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3538 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3539 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3540 | main.cleanup() |
| 3541 | main.exit() |
| 3542 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3543 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3544 | """ |
| 3545 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3546 | appName is the hierarchical app name, not the feature name |
| 3547 | If check is True, method will check the status of the app after the |
| 3548 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3549 | Returns main.TRUE if the command was successfully sent |
| 3550 | main.FALSE if the cli responded with an error or given |
| 3551 | incorrect input |
| 3552 | """ |
| 3553 | try: |
| 3554 | if not isinstance( appName, types.StringType ): |
| 3555 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3556 | "be a string" ) |
| 3557 | return main.FALSE |
| 3558 | status = self.appStatus( appName ) |
| 3559 | if status == "INSTALLED": |
| 3560 | return main.TRUE |
| 3561 | elif status == "ACTIVE": |
| 3562 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3563 | if check and response == main.TRUE: |
| 3564 | for i in range(10): # try 10 times then give up |
| 3565 | status = self.appStatus( appName ) |
| 3566 | if status == "INSTALLED": |
| 3567 | return main.TRUE |
| 3568 | else: |
| 3569 | time.sleep( 1 ) |
| 3570 | return main.FALSE |
| 3571 | else: # not check or command didn't succeed |
| 3572 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3573 | elif status == "UNINSTALLED": |
| 3574 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3575 | "application '" + appName + "' which is not " + |
| 3576 | "installed." ) |
| 3577 | return main.TRUE |
| 3578 | else: |
| 3579 | main.log.error( "Unexpected return value from appStatus: " + |
| 3580 | str( status ) ) |
| 3581 | return main.ERROR |
| 3582 | except TypeError: |
| 3583 | main.log.exception( self.name + ": Object not as expected" ) |
| 3584 | return main.ERROR |
| 3585 | except pexpect.EOF: |
| 3586 | main.log.error( self.name + ": EOF exception found" ) |
| 3587 | main.log.error( self.name + ": " + self.handle.before ) |
| 3588 | main.cleanup() |
| 3589 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3590 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3591 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3592 | main.cleanup() |
| 3593 | main.exit() |
| 3594 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3595 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3596 | """ |
| 3597 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3598 | appName is the hierarchical app name, not the feature name |
| 3599 | If check is True, method will check the status of the app after the |
| 3600 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3601 | Returns main.TRUE if the command was successfully sent |
| 3602 | main.FALSE if the cli responded with an error or given |
| 3603 | incorrect input |
| 3604 | """ |
| 3605 | # TODO: check with Thomas about the state machine for apps |
| 3606 | try: |
| 3607 | if not isinstance( appName, types.StringType ): |
| 3608 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3609 | "be a string" ) |
| 3610 | return main.FALSE |
| 3611 | status = self.appStatus( appName ) |
| 3612 | if status == "INSTALLED": |
| 3613 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3614 | if check and response == main.TRUE: |
| 3615 | for i in range(10): # try 10 times then give up |
| 3616 | status = self.appStatus( appName ) |
| 3617 | if status == "UNINSTALLED": |
| 3618 | return main.TRUE |
| 3619 | else: |
| 3620 | time.sleep( 1 ) |
| 3621 | return main.FALSE |
| 3622 | else: # not check or command didn't succeed |
| 3623 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3624 | elif status == "ACTIVE": |
| 3625 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3626 | "application '" + appName + "' which is " + |
| 3627 | "currently active." ) |
| 3628 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3629 | if check and response == main.TRUE: |
| 3630 | for i in range(10): # try 10 times then give up |
| 3631 | status = self.appStatus( appName ) |
| 3632 | if status == "UNINSTALLED": |
| 3633 | return main.TRUE |
| 3634 | else: |
| 3635 | time.sleep( 1 ) |
| 3636 | return main.FALSE |
| 3637 | else: # not check or command didn't succeed |
| 3638 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3639 | elif status == "UNINSTALLED": |
| 3640 | return main.TRUE |
| 3641 | else: |
| 3642 | main.log.error( "Unexpected return value from appStatus: " + |
| 3643 | str( status ) ) |
| 3644 | return main.ERROR |
| 3645 | except TypeError: |
| 3646 | main.log.exception( self.name + ": Object not as expected" ) |
| 3647 | return main.ERROR |
| 3648 | except pexpect.EOF: |
| 3649 | main.log.error( self.name + ": EOF exception found" ) |
| 3650 | main.log.error( self.name + ": " + self.handle.before ) |
| 3651 | main.cleanup() |
| 3652 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3653 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3654 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3655 | main.cleanup() |
| 3656 | main.exit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3657 | |
| 3658 | def appIDs( self, jsonFormat=True ): |
| 3659 | """ |
| 3660 | Show the mappings between app id and app names given by the 'app-ids' |
| 3661 | cli command |
| 3662 | """ |
| 3663 | try: |
| 3664 | cmdStr = "app-ids" |
| 3665 | if jsonFormat: |
| 3666 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3667 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3668 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3669 | assert "Command not found:" not in output, output |
| 3670 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3671 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3672 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3673 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3674 | return None |
| 3675 | except TypeError: |
| 3676 | main.log.exception( self.name + ": Object not as expected" ) |
| 3677 | return None |
| 3678 | except pexpect.EOF: |
| 3679 | main.log.error( self.name + ": EOF exception found" ) |
| 3680 | main.log.error( self.name + ": " + self.handle.before ) |
| 3681 | main.cleanup() |
| 3682 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3683 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3684 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3685 | main.cleanup() |
| 3686 | main.exit() |
| 3687 | |
| 3688 | def appToIDCheck( self ): |
| 3689 | """ |
| 3690 | This method will check that each application's ID listed in 'apps' is |
| 3691 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3692 | there are no duplicate IDs issued. Note that an app ID should be |
| 3693 | a globaly unique numerical identifier for app/app-like features. Once |
| 3694 | an ID is registered, the ID is never freed up so that if an app is |
| 3695 | reinstalled it will have the same ID. |
| 3696 | |
| 3697 | Returns: main.TRUE if the check passes and |
| 3698 | main.FALSE if the check fails or |
| 3699 | main.ERROR if there is some error in processing the test |
| 3700 | """ |
| 3701 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3702 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3703 | rawJson = self.appIDs( jsonFormat=True ) |
| 3704 | if rawJson: |
| 3705 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3706 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3707 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3708 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3709 | rawJson = self.apps( jsonFormat=True ) |
| 3710 | if rawJson: |
| 3711 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3712 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3713 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3714 | bail = True |
| 3715 | if bail: |
| 3716 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3717 | result = main.TRUE |
| 3718 | for app in apps: |
| 3719 | appID = app.get( 'id' ) |
| 3720 | if appID is None: |
| 3721 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3722 | result = main.FALSE |
| 3723 | appName = app.get( 'name' ) |
| 3724 | if appName is None: |
| 3725 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3726 | result = main.FALSE |
| 3727 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3728 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3729 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3730 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3731 | if not current: # if ids doesn't have this id |
| 3732 | result = main.FALSE |
| 3733 | main.log.error( "'app-ids' does not have the ID for " + |
| 3734 | str( appName ) + " that apps does." ) |
| 3735 | elif len( current ) > 1: |
| 3736 | # there is more than one app with this ID |
| 3737 | result = main.FALSE |
| 3738 | # We will log this later in the method |
| 3739 | elif not current[0][ 'name' ] == appName: |
| 3740 | currentName = current[0][ 'name' ] |
| 3741 | result = main.FALSE |
| 3742 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3743 | " registered under id:" + str( appID ) + |
| 3744 | " but 'apps' has " + str( appName ) ) |
| 3745 | else: |
| 3746 | pass # id and name match! |
| 3747 | # now make sure that app-ids has no duplicates |
| 3748 | idsList = [] |
| 3749 | namesList = [] |
| 3750 | for item in ids: |
| 3751 | idsList.append( item[ 'id' ] ) |
| 3752 | namesList.append( item[ 'name' ] ) |
| 3753 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3754 | len( namesList ) != len( set( namesList ) ): |
| 3755 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3756 | + json.dumps( ids, |
| 3757 | sort_keys=True, |
| 3758 | indent=4, |
| 3759 | separators=( ',', ': ' ) ) ) |
| 3760 | result = main.FALSE |
| 3761 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3762 | except ( TypeError, ValueError ): |
| 3763 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3764 | return main.ERROR |
| 3765 | except pexpect.EOF: |
| 3766 | main.log.error( self.name + ": EOF exception found" ) |
| 3767 | main.log.error( self.name + ": " + self.handle.before ) |
| 3768 | main.cleanup() |
| 3769 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3770 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3771 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3772 | main.cleanup() |
| 3773 | main.exit() |
| 3774 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3775 | def getCfg( self, component=None, propName=None, short=False, |
| 3776 | jsonFormat=True ): |
| 3777 | """ |
| 3778 | Get configuration settings from onos cli |
| 3779 | Optional arguments: |
| 3780 | component - Optionally only list configurations for a specific |
| 3781 | component. If None, all components with configurations |
| 3782 | are displayed. Case Sensitive string. |
| 3783 | propName - If component is specified, propName option will show |
| 3784 | only this specific configuration from that component. |
| 3785 | Case Sensitive string. |
| 3786 | jsonFormat - Returns output as json. Note that this will override |
| 3787 | the short option |
| 3788 | short - Short, less verbose, version of configurations. |
| 3789 | This is overridden by the json option |
| 3790 | returns: |
| 3791 | Output from cli as a string or None on error |
| 3792 | """ |
| 3793 | try: |
| 3794 | baseStr = "cfg" |
| 3795 | cmdStr = " get" |
| 3796 | componentStr = "" |
| 3797 | if component: |
| 3798 | componentStr += " " + component |
| 3799 | if propName: |
| 3800 | componentStr += " " + propName |
| 3801 | if jsonFormat: |
| 3802 | baseStr += " -j" |
| 3803 | elif short: |
| 3804 | baseStr += " -s" |
| 3805 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3806 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3807 | assert "Command not found:" not in output, output |
| 3808 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3809 | return output |
| 3810 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3811 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3812 | return None |
| 3813 | except TypeError: |
| 3814 | main.log.exception( self.name + ": Object not as expected" ) |
| 3815 | return None |
| 3816 | except pexpect.EOF: |
| 3817 | main.log.error( self.name + ": EOF exception found" ) |
| 3818 | main.log.error( self.name + ": " + self.handle.before ) |
| 3819 | main.cleanup() |
| 3820 | main.exit() |
| 3821 | except Exception: |
| 3822 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3823 | main.cleanup() |
| 3824 | main.exit() |
| 3825 | |
| 3826 | def setCfg( self, component, propName, value=None, check=True ): |
| 3827 | """ |
| 3828 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3829 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3830 | component - The case sensitive name of the component whose |
| 3831 | property is to be set |
| 3832 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3833 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3834 | value - The value to set the property to. If None, will unset the |
| 3835 | property and revert it to it's default value(if applicable) |
| 3836 | check - Boolean, Check whether the option was successfully set this |
| 3837 | only applies when a value is given. |
| 3838 | returns: |
| 3839 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3840 | will return main.TRUE unless there is an error |
| 3841 | """ |
| 3842 | try: |
| 3843 | baseStr = "cfg" |
| 3844 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3845 | if value is not None: |
| 3846 | cmdStr += " " + str( value ) |
| 3847 | output = self.sendline( baseStr + cmdStr ) |
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 | if value and check: |
| 3852 | results = self.getCfg( component=str( component ), |
| 3853 | propName=str( propName ), |
| 3854 | jsonFormat=True ) |
| 3855 | # Check if current value is what we just set |
| 3856 | try: |
| 3857 | jsonOutput = json.loads( results ) |
| 3858 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3859 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3860 | main.log.exception( "Error parsing cfg output" ) |
| 3861 | main.log.error( "output:" + repr( results ) ) |
| 3862 | return main.FALSE |
| 3863 | if current == str( value ): |
| 3864 | return main.TRUE |
| 3865 | return main.FALSE |
| 3866 | return main.TRUE |
| 3867 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3868 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3869 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3870 | except ( TypeError, ValueError ): |
| 3871 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3872 | return main.FALSE |
| 3873 | except pexpect.EOF: |
| 3874 | main.log.error( self.name + ": EOF exception found" ) |
| 3875 | main.log.error( self.name + ": " + self.handle.before ) |
| 3876 | main.cleanup() |
| 3877 | main.exit() |
| 3878 | except Exception: |
| 3879 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3880 | main.cleanup() |
| 3881 | main.exit() |
| 3882 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3883 | def setTestAdd( self, setName, values ): |
| 3884 | """ |
| 3885 | CLI command to add elements to a distributed set. |
| 3886 | Arguments: |
| 3887 | setName - The name of the set to add to. |
| 3888 | values - The value(s) to add to the set, space seperated. |
| 3889 | Example usages: |
| 3890 | setTestAdd( "set1", "a b c" ) |
| 3891 | setTestAdd( "set2", "1" ) |
| 3892 | returns: |
| 3893 | main.TRUE on success OR |
| 3894 | main.FALSE if elements were already in the set OR |
| 3895 | main.ERROR on error |
| 3896 | """ |
| 3897 | try: |
| 3898 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
| 3899 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3900 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3901 | assert "Command not found:" not in output, output |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3902 | try: |
| 3903 | # TODO: Maybe make this less hardcoded |
| 3904 | # ConsistentMap Exceptions |
| 3905 | assert "org.onosproject.store.service" not in output |
| 3906 | # Node not leader |
| 3907 | assert "java.lang.IllegalStateException" not in output |
| 3908 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3909 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3910 | "command: " + str( output ) ) |
| 3911 | retryTime = 30 # Conservative time, given by Madan |
| 3912 | main.log.info( "Waiting " + str( retryTime ) + |
| 3913 | "seconds before retrying." ) |
| 3914 | time.sleep( retryTime ) # Due to change in mastership |
| 3915 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3916 | assert output is not None, "Error in sendline" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3917 | assert "Error executing command" not in output |
| 3918 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3919 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
| 3920 | main.log.info( self.name + ": " + output ) |
| 3921 | if re.search( positiveMatch, output): |
| 3922 | return main.TRUE |
| 3923 | elif re.search( negativeMatch, output): |
| 3924 | return main.FALSE |
| 3925 | else: |
| 3926 | main.log.error( self.name + ": setTestAdd did not" + |
| 3927 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3928 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3929 | return main.ERROR |
| 3930 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3931 | main.log.exception( "Error in processing '" + cmdStr + "' command. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3932 | return main.ERROR |
| 3933 | except TypeError: |
| 3934 | main.log.exception( self.name + ": Object not as expected" ) |
| 3935 | return main.ERROR |
| 3936 | except pexpect.EOF: |
| 3937 | main.log.error( self.name + ": EOF exception found" ) |
| 3938 | main.log.error( self.name + ": " + self.handle.before ) |
| 3939 | main.cleanup() |
| 3940 | main.exit() |
| 3941 | except Exception: |
| 3942 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3943 | main.cleanup() |
| 3944 | main.exit() |
| 3945 | |
| 3946 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 3947 | """ |
| 3948 | CLI command to remove elements from a distributed set. |
| 3949 | Required arguments: |
| 3950 | setName - The name of the set to remove from. |
| 3951 | values - The value(s) to remove from the set, space seperated. |
| 3952 | Optional arguments: |
| 3953 | clear - Clear all elements from the set |
| 3954 | retain - Retain only the given values. (intersection of the |
| 3955 | original set and the given set) |
| 3956 | returns: |
| 3957 | main.TRUE on success OR |
| 3958 | main.FALSE if the set was not changed OR |
| 3959 | main.ERROR on error |
| 3960 | """ |
| 3961 | try: |
| 3962 | cmdStr = "set-test-remove " |
| 3963 | if clear: |
| 3964 | cmdStr += "-c " + str( setName ) |
| 3965 | elif retain: |
| 3966 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 3967 | else: |
| 3968 | cmdStr += str( setName ) + " " + str( values ) |
| 3969 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3970 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3971 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3972 | # TODO: Maybe make this less hardcoded |
| 3973 | # ConsistentMap Exceptions |
| 3974 | assert "org.onosproject.store.service" not in output |
| 3975 | # Node not leader |
| 3976 | assert "java.lang.IllegalStateException" not in output |
| 3977 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3978 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3979 | "command: " + str( output ) ) |
| 3980 | retryTime = 30 # Conservative time, given by Madan |
| 3981 | main.log.info( "Waiting " + str( retryTime ) + |
| 3982 | "seconds before retrying." ) |
| 3983 | time.sleep( retryTime ) # Due to change in mastership |
| 3984 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3985 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3986 | assert "Command not found:" not in output, output |
| 3987 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3988 | main.log.info( self.name + ": " + output ) |
| 3989 | if clear: |
| 3990 | pattern = "Set " + str( setName ) + " cleared" |
| 3991 | if re.search( pattern, output ): |
| 3992 | return main.TRUE |
| 3993 | elif retain: |
| 3994 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 3995 | "only elements of set \[(.*)\]" |
| 3996 | negativePattern = str( setName ) + " was not changed by " +\ |
| 3997 | "retaining only elements of the set " +\ |
| 3998 | "\[(.*)\]" |
| 3999 | if re.search( positivePattern, output ): |
| 4000 | return main.TRUE |
| 4001 | elif re.search( negativePattern, output ): |
| 4002 | return main.FALSE |
| 4003 | else: |
| 4004 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4005 | str( setName ) |
| 4006 | if ( len( values.split() ) == 1 ): |
| 4007 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4008 | str( setName ) |
| 4009 | else: |
| 4010 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4011 | str( setName ) |
| 4012 | if re.search( positivePattern, output ): |
| 4013 | return main.TRUE |
| 4014 | elif re.search( negativePattern, output ): |
| 4015 | return main.FALSE |
| 4016 | main.log.error( self.name + ": setTestRemove did not" + |
| 4017 | " match expected output" ) |
| 4018 | main.log.debug( self.name + " expected: " + pattern ) |
| 4019 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4020 | return main.ERROR |
| 4021 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4022 | main.log.exception( "Error in processing '" + cmdStr + "' commandr. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4023 | return main.ERROR |
| 4024 | except TypeError: |
| 4025 | main.log.exception( self.name + ": Object not as expected" ) |
| 4026 | return main.ERROR |
| 4027 | except pexpect.EOF: |
| 4028 | main.log.error( self.name + ": EOF exception found" ) |
| 4029 | main.log.error( self.name + ": " + self.handle.before ) |
| 4030 | main.cleanup() |
| 4031 | main.exit() |
| 4032 | except Exception: |
| 4033 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4034 | main.cleanup() |
| 4035 | main.exit() |
| 4036 | |
| 4037 | def setTestGet( self, setName, values="" ): |
| 4038 | """ |
| 4039 | CLI command to get the elements in a distributed set. |
| 4040 | Required arguments: |
| 4041 | setName - The name of the set to remove from. |
| 4042 | Optional arguments: |
| 4043 | values - The value(s) to check if in the set, space seperated. |
| 4044 | returns: |
| 4045 | main.ERROR on error OR |
| 4046 | A list of elements in the set if no optional arguments are |
| 4047 | supplied OR |
| 4048 | A tuple containing the list then: |
| 4049 | main.FALSE if the given values are not in the set OR |
| 4050 | main.TRUE if the given values are in the set OR |
| 4051 | """ |
| 4052 | try: |
| 4053 | values = str( values ).strip() |
| 4054 | setName = str( setName ).strip() |
| 4055 | length = len( values.split() ) |
| 4056 | containsCheck = None |
| 4057 | # Patterns to match |
| 4058 | setPattern = "\[(.*)\]" |
| 4059 | pattern = "Items in set " + setName + ":\n" + setPattern |
| 4060 | containsTrue = "Set " + setName + " contains the value " + values |
| 4061 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4062 | values |
| 4063 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4064 | setPattern |
| 4065 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4066 | " subset " + setPattern |
| 4067 | |
| 4068 | cmdStr = "set-test-get " |
| 4069 | cmdStr += setName + " " + values |
| 4070 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4071 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4072 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4073 | # TODO: Maybe make this less hardcoded |
| 4074 | # ConsistentMap Exceptions |
| 4075 | assert "org.onosproject.store.service" not in output |
| 4076 | # Node not leader |
| 4077 | assert "java.lang.IllegalStateException" not in output |
| 4078 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4079 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4080 | "command: " + str( output ) ) |
| 4081 | retryTime = 30 # Conservative time, given by Madan |
| 4082 | main.log.info( "Waiting " + str( retryTime ) + |
| 4083 | "seconds before retrying." ) |
| 4084 | time.sleep( retryTime ) # Due to change in mastership |
| 4085 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4086 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4087 | assert "Command not found:" not in output, output |
| 4088 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4089 | main.log.info( self.name + ": " + output ) |
| 4090 | |
| 4091 | if length == 0: |
| 4092 | match = re.search( pattern, output ) |
| 4093 | else: # if given values |
| 4094 | if length == 1: # Contains output |
| 4095 | patternTrue = pattern + "\n" + containsTrue |
| 4096 | patternFalse = pattern + "\n" + containsFalse |
| 4097 | else: # ContainsAll output |
| 4098 | patternTrue = pattern + "\n" + containsAllTrue |
| 4099 | patternFalse = pattern + "\n" + containsAllFalse |
| 4100 | matchTrue = re.search( patternTrue, output ) |
| 4101 | matchFalse = re.search( patternFalse, output ) |
| 4102 | if matchTrue: |
| 4103 | containsCheck = main.TRUE |
| 4104 | match = matchTrue |
| 4105 | elif matchFalse: |
| 4106 | containsCheck = main.FALSE |
| 4107 | match = matchFalse |
| 4108 | else: |
| 4109 | main.log.error( self.name + " setTestGet did not match " +\ |
| 4110 | "expected output" ) |
| 4111 | main.log.debug( self.name + " expected: " + pattern ) |
| 4112 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4113 | match = None |
| 4114 | if match: |
| 4115 | setMatch = match.group( 1 ) |
| 4116 | if setMatch == '': |
| 4117 | setList = [] |
| 4118 | else: |
| 4119 | setList = setMatch.split( ", " ) |
| 4120 | if length > 0: |
| 4121 | return ( setList, containsCheck ) |
| 4122 | else: |
| 4123 | return setList |
| 4124 | else: # no match |
| 4125 | main.log.error( self.name + ": setTestGet did not" + |
| 4126 | " match expected output" ) |
| 4127 | main.log.debug( self.name + " expected: " + pattern ) |
| 4128 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4129 | return main.ERROR |
| 4130 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4131 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4132 | return main.ERROR |
| 4133 | except TypeError: |
| 4134 | main.log.exception( self.name + ": Object not as expected" ) |
| 4135 | return main.ERROR |
| 4136 | except pexpect.EOF: |
| 4137 | main.log.error( self.name + ": EOF exception found" ) |
| 4138 | main.log.error( self.name + ": " + self.handle.before ) |
| 4139 | main.cleanup() |
| 4140 | main.exit() |
| 4141 | except Exception: |
| 4142 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4143 | main.cleanup() |
| 4144 | main.exit() |
| 4145 | |
| 4146 | def setTestSize( self, setName ): |
| 4147 | """ |
| 4148 | CLI command to get the elements in a distributed set. |
| 4149 | Required arguments: |
| 4150 | setName - The name of the set to remove from. |
| 4151 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4152 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4153 | None on error |
| 4154 | """ |
| 4155 | try: |
| 4156 | # TODO: Should this check against the number of elements returned |
| 4157 | # and then return true/false based on that? |
| 4158 | setName = str( setName ).strip() |
| 4159 | # Patterns to match |
| 4160 | setPattern = "\[(.*)\]" |
| 4161 | pattern = "There are (\d+) items in set " + setName + ":\n" +\ |
| 4162 | setPattern |
| 4163 | cmdStr = "set-test-get -s " |
| 4164 | cmdStr += setName |
| 4165 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4166 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4167 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4168 | # TODO: Maybe make this less hardcoded |
| 4169 | # ConsistentMap Exceptions |
| 4170 | assert "org.onosproject.store.service" not in output |
| 4171 | # Node not leader |
| 4172 | assert "java.lang.IllegalStateException" not in output |
| 4173 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4174 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4175 | "command: " + str( output ) ) |
| 4176 | retryTime = 30 # Conservative time, given by Madan |
| 4177 | main.log.info( "Waiting " + str( retryTime ) + |
| 4178 | "seconds before retrying." ) |
| 4179 | time.sleep( retryTime ) # Due to change in mastership |
| 4180 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4181 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4182 | assert "Command not found:" not in output, output |
| 4183 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4184 | main.log.info( self.name + ": " + output ) |
| 4185 | match = re.search( pattern, output ) |
| 4186 | if match: |
| 4187 | setSize = int( match.group( 1 ) ) |
| 4188 | setMatch = match.group( 2 ) |
| 4189 | if len( setMatch.split() ) == setSize: |
| 4190 | main.log.info( "The size returned by " + self.name + |
| 4191 | " matches the number of elements in " + |
| 4192 | "the returned set" ) |
| 4193 | else: |
| 4194 | main.log.error( "The size returned by " + self.name + |
| 4195 | " does not match the number of " + |
| 4196 | "elements in the returned set." ) |
| 4197 | return setSize |
| 4198 | else: # no match |
| 4199 | main.log.error( self.name + ": setTestGet did not" + |
| 4200 | " match expected output" ) |
| 4201 | main.log.debug( self.name + " expected: " + pattern ) |
| 4202 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4203 | return None |
| 4204 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4205 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 4206 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4207 | except TypeError: |
| 4208 | main.log.exception( self.name + ": Object not as expected" ) |
| 4209 | return None |
| 4210 | except pexpect.EOF: |
| 4211 | main.log.error( self.name + ": EOF exception found" ) |
| 4212 | main.log.error( self.name + ": " + self.handle.before ) |
| 4213 | main.cleanup() |
| 4214 | main.exit() |
| 4215 | except Exception: |
| 4216 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4217 | main.cleanup() |
| 4218 | main.exit() |
| 4219 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4220 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4221 | """ |
| 4222 | Command to list the various counters in the system. |
| 4223 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4224 | if jsonFormat, a string of the json object returned by the cli |
| 4225 | command |
| 4226 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4227 | None on error |
| 4228 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4229 | try: |
| 4230 | counters = {} |
| 4231 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4232 | if jsonFormat: |
| 4233 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4234 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4235 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4236 | assert "Command not found:" not in output, output |
| 4237 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4238 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4239 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4240 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4241 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4242 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4243 | except TypeError: |
| 4244 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4245 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4246 | except pexpect.EOF: |
| 4247 | main.log.error( self.name + ": EOF exception found" ) |
| 4248 | main.log.error( self.name + ": " + self.handle.before ) |
| 4249 | main.cleanup() |
| 4250 | main.exit() |
| 4251 | except Exception: |
| 4252 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4253 | main.cleanup() |
| 4254 | main.exit() |
| 4255 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4256 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4257 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4258 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4259 | Required arguments: |
| 4260 | counter - The name of the counter to increment. |
| 4261 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4262 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4263 | returns: |
| 4264 | integer value of the counter or |
| 4265 | None on Error |
| 4266 | """ |
| 4267 | try: |
| 4268 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4269 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4270 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4271 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4272 | if delta != 1: |
| 4273 | cmdStr += " " + str( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4274 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4275 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4276 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4277 | # TODO: Maybe make this less hardcoded |
| 4278 | # ConsistentMap Exceptions |
| 4279 | assert "org.onosproject.store.service" not in output |
| 4280 | # Node not leader |
| 4281 | assert "java.lang.IllegalStateException" not in output |
| 4282 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4283 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4284 | "command: " + str( output ) ) |
| 4285 | retryTime = 30 # Conservative time, given by Madan |
| 4286 | main.log.info( "Waiting " + str( retryTime ) + |
| 4287 | "seconds before retrying." ) |
| 4288 | time.sleep( retryTime ) # Due to change in mastership |
| 4289 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4290 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4291 | assert "Command not found:" not in output, output |
| 4292 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4293 | main.log.info( self.name + ": " + output ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4294 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4295 | match = re.search( pattern, output ) |
| 4296 | if match: |
| 4297 | return int( match.group( 1 ) ) |
| 4298 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4299 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4300 | " match expected output." ) |
| 4301 | main.log.debug( self.name + " expected: " + pattern ) |
| 4302 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4303 | return None |
| 4304 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4305 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4306 | return None |
| 4307 | except TypeError: |
| 4308 | main.log.exception( self.name + ": Object not as expected" ) |
| 4309 | return None |
| 4310 | except pexpect.EOF: |
| 4311 | main.log.error( self.name + ": EOF exception found" ) |
| 4312 | main.log.error( self.name + ": " + self.handle.before ) |
| 4313 | main.cleanup() |
| 4314 | main.exit() |
| 4315 | except Exception: |
| 4316 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4317 | main.cleanup() |
| 4318 | main.exit() |
| 4319 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4320 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4321 | """ |
| 4322 | CLI command to get a distributed counter then add a delta to it. |
| 4323 | Required arguments: |
| 4324 | counter - The name of the counter to increment. |
| 4325 | Optional arguments: |
| 4326 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4327 | returns: |
| 4328 | integer value of the counter or |
| 4329 | None on Error |
| 4330 | """ |
| 4331 | try: |
| 4332 | counter = str( counter ) |
| 4333 | delta = int( delta ) |
| 4334 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4335 | cmdStr += counter |
| 4336 | if delta != 1: |
| 4337 | cmdStr += " " + str( delta ) |
| 4338 | output = self.sendline( cmdStr ) |
| 4339 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4340 | assert output is not None, "Error in sendline" |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4341 | # TODO: Maybe make this less hardcoded |
| 4342 | # ConsistentMap Exceptions |
| 4343 | assert "org.onosproject.store.service" not in output |
| 4344 | # Node not leader |
| 4345 | assert "java.lang.IllegalStateException" not in output |
| 4346 | except AssertionError: |
| 4347 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4348 | "command: " + str( output ) ) |
| 4349 | retryTime = 30 # Conservative time, given by Madan |
| 4350 | main.log.info( "Waiting " + str( retryTime ) + |
| 4351 | "seconds before retrying." ) |
| 4352 | time.sleep( retryTime ) # Due to change in mastership |
| 4353 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4354 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4355 | assert "Command not found:" not in output, output |
| 4356 | assert "Error executing command" not in output, output |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4357 | main.log.info( self.name + ": " + output ) |
| 4358 | pattern = counter + " was updated to (-?\d+)" |
| 4359 | match = re.search( pattern, output ) |
| 4360 | if match: |
| 4361 | return int( match.group( 1 ) ) |
| 4362 | else: |
| 4363 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4364 | " match expected output." ) |
| 4365 | main.log.debug( self.name + " expected: " + pattern ) |
| 4366 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4367 | return None |
| 4368 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4369 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4370 | return None |
| 4371 | except TypeError: |
| 4372 | main.log.exception( self.name + ": Object not as expected" ) |
| 4373 | return None |
| 4374 | except pexpect.EOF: |
| 4375 | main.log.error( self.name + ": EOF exception found" ) |
| 4376 | main.log.error( self.name + ": " + self.handle.before ) |
| 4377 | main.cleanup() |
| 4378 | main.exit() |
| 4379 | except Exception: |
| 4380 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4381 | main.cleanup() |
| 4382 | main.exit() |
| 4383 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4384 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4385 | """ |
| 4386 | Description: Execute summary command in onos |
| 4387 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4388 | no output |
| 4389 | |
| 4390 | """ |
| 4391 | try: |
| 4392 | cmdStr = "summary" |
| 4393 | if jsonFormat: |
| 4394 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4395 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4396 | assert handle 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 handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4398 | assert "Error:" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4399 | if not handle: |
| 4400 | main.log.error( self.name + ": There is no output in " + |
| 4401 | "summary command" ) |
| 4402 | return main.FALSE |
| 4403 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4404 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4405 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4406 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4407 | except TypeError: |
| 4408 | main.log.exception( self.name + ": Object not as expected" ) |
| 4409 | return None |
| 4410 | except pexpect.EOF: |
| 4411 | main.log.error( self.name + ": EOF exception found" ) |
| 4412 | main.log.error( self.name + ": " + self.handle.before ) |
| 4413 | main.cleanup() |
| 4414 | main.exit() |
| 4415 | except Exception: |
| 4416 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4417 | main.cleanup() |
| 4418 | main.exit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4419 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4420 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4421 | """ |
| 4422 | CLI command to get the value of a key in a consistent map using |
| 4423 | transactions. This a test function and can only get keys from the |
| 4424 | test map hard coded into the cli command |
| 4425 | Required arguments: |
| 4426 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4427 | returns: |
| 4428 | The string value of the key or |
| 4429 | None on Error |
| 4430 | """ |
| 4431 | try: |
| 4432 | keyName = str( keyName ) |
| 4433 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4434 | cmdStr += keyName |
| 4435 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4436 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4437 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4438 | try: |
| 4439 | # TODO: Maybe make this less hardcoded |
| 4440 | # ConsistentMap Exceptions |
| 4441 | assert "org.onosproject.store.service" not in output |
| 4442 | # Node not leader |
| 4443 | assert "java.lang.IllegalStateException" not in output |
| 4444 | except AssertionError: |
| 4445 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4446 | "command: " + str( output ) ) |
| 4447 | return None |
| 4448 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4449 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4450 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4451 | return None |
| 4452 | else: |
| 4453 | match = re.search( pattern, output ) |
| 4454 | if match: |
| 4455 | return match.groupdict()[ 'value' ] |
| 4456 | else: |
| 4457 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4458 | " match expected output." ) |
| 4459 | main.log.debug( self.name + " expected: " + pattern ) |
| 4460 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4461 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4462 | except AssertionError: |
| 4463 | main.log.exception( "" ) |
| 4464 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4465 | except TypeError: |
| 4466 | main.log.exception( self.name + ": Object not as expected" ) |
| 4467 | return None |
| 4468 | except pexpect.EOF: |
| 4469 | main.log.error( self.name + ": EOF exception found" ) |
| 4470 | main.log.error( self.name + ": " + self.handle.before ) |
| 4471 | main.cleanup() |
| 4472 | main.exit() |
| 4473 | except Exception: |
| 4474 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4475 | main.cleanup() |
| 4476 | main.exit() |
| 4477 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4478 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4479 | """ |
| 4480 | CLI command to put a value into 'numKeys' number of keys in a |
| 4481 | consistent map using transactions. This a test function and can only |
| 4482 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4483 | Required arguments: |
| 4484 | numKeys - Number of keys to add the value to |
| 4485 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4486 | returns: |
| 4487 | A dictionary whose keys are the name of the keys put into the map |
| 4488 | and the values of the keys are dictionaries whose key-values are |
| 4489 | 'value': value put into map and optionaly |
| 4490 | 'oldValue': Previous value in the key or |
| 4491 | None on Error |
| 4492 | |
| 4493 | Example output |
| 4494 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4495 | 'Key2': {'value': 'Testing'} } |
| 4496 | """ |
| 4497 | try: |
| 4498 | numKeys = str( numKeys ) |
| 4499 | value = str( value ) |
| 4500 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4501 | cmdStr += numKeys + " " + value |
| 4502 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4503 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4504 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4505 | try: |
| 4506 | # TODO: Maybe make this less hardcoded |
| 4507 | # ConsistentMap Exceptions |
| 4508 | assert "org.onosproject.store.service" not in output |
| 4509 | # Node not leader |
| 4510 | assert "java.lang.IllegalStateException" not in output |
| 4511 | except AssertionError: |
| 4512 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4513 | "command: " + str( output ) ) |
| 4514 | return None |
| 4515 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4516 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4517 | results = {} |
| 4518 | for line in output.splitlines(): |
| 4519 | new = re.search( newPattern, line ) |
| 4520 | updated = re.search( updatedPattern, line ) |
| 4521 | if new: |
| 4522 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4523 | elif updated: |
| 4524 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4525 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4526 | else: |
| 4527 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4528 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4529 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4530 | newPattern, |
| 4531 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4532 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4533 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4534 | except AssertionError: |
| 4535 | main.log.exception( "" ) |
| 4536 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4537 | except TypeError: |
| 4538 | main.log.exception( self.name + ": Object not as expected" ) |
| 4539 | return None |
| 4540 | except pexpect.EOF: |
| 4541 | main.log.error( self.name + ": EOF exception found" ) |
| 4542 | main.log.error( self.name + ": " + self.handle.before ) |
| 4543 | main.cleanup() |
| 4544 | main.exit() |
| 4545 | except Exception: |
| 4546 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4547 | main.cleanup() |
| 4548 | main.exit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4549 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4550 | def maps( self, jsonFormat=True ): |
| 4551 | """ |
| 4552 | Description: Returns result of onos:maps |
| 4553 | Optional: |
| 4554 | * jsonFormat: enable json formatting of output |
| 4555 | """ |
| 4556 | try: |
| 4557 | cmdStr = "maps" |
| 4558 | if jsonFormat: |
| 4559 | cmdStr += " -j" |
| 4560 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4561 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4562 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4563 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4564 | except AssertionError: |
| 4565 | main.log.exception( "" ) |
| 4566 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4567 | except TypeError: |
| 4568 | main.log.exception( self.name + ": Object not as expected" ) |
| 4569 | return None |
| 4570 | except pexpect.EOF: |
| 4571 | main.log.error( self.name + ": EOF exception found" ) |
| 4572 | main.log.error( self.name + ": " + self.handle.before ) |
| 4573 | main.cleanup() |
| 4574 | main.exit() |
| 4575 | except Exception: |
| 4576 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4577 | main.cleanup() |
| 4578 | main.exit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4579 | |
| 4580 | def getSwController( self, uri, jsonFormat=True ): |
| 4581 | """ |
| 4582 | Descrition: Gets the controller information from the device |
| 4583 | """ |
| 4584 | try: |
| 4585 | cmd = "device-controllers " |
| 4586 | if jsonFormat: |
| 4587 | cmd += "-j " |
| 4588 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4589 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4590 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4591 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4592 | except AssertionError: |
| 4593 | main.log.exception( "" ) |
| 4594 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4595 | except TypeError: |
| 4596 | main.log.exception( self.name + ": Object not as expected" ) |
| 4597 | return None |
| 4598 | except pexpect.EOF: |
| 4599 | main.log.error( self.name + ": EOF exception found" ) |
| 4600 | main.log.error( self.name + ": " + self.handle.before ) |
| 4601 | main.cleanup() |
| 4602 | main.exit() |
| 4603 | except Exception: |
| 4604 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4605 | main.cleanup() |
| 4606 | main.exit() |
| 4607 | |
| 4608 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4609 | """ |
| 4610 | Descrition: sets the controller(s) for the specified device |
| 4611 | |
| 4612 | Parameters: |
| 4613 | Required: uri - String: The uri of the device(switch). |
| 4614 | ip - String or List: The ip address of the controller. |
| 4615 | This parameter can be formed in a couple of different ways. |
| 4616 | VALID: |
| 4617 | 10.0.0.1 - just the ip address |
| 4618 | tcp:10.0.0.1 - the protocol and the ip address |
| 4619 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4620 | so that you can add controllers with different |
| 4621 | protocols and ports |
| 4622 | INVALID: |
| 4623 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4624 | |
| 4625 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4626 | port - The port number. |
| 4627 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4628 | |
| 4629 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4630 | """ |
| 4631 | try: |
| 4632 | cmd = "device-setcontrollers" |
| 4633 | |
| 4634 | if jsonFormat: |
| 4635 | cmd += " -j" |
| 4636 | cmd += " " + uri |
| 4637 | if isinstance( ip, str ): |
| 4638 | ip = [ip] |
| 4639 | for item in ip: |
| 4640 | if ":" in item: |
| 4641 | sitem = item.split( ":" ) |
| 4642 | if len(sitem) == 3: |
| 4643 | cmd += " " + item |
| 4644 | elif "." in sitem[1]: |
| 4645 | cmd += " {}:{}".format(item, port) |
| 4646 | else: |
| 4647 | main.log.error( "Malformed entry: " + item ) |
| 4648 | raise TypeError |
| 4649 | else: |
| 4650 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4651 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4652 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4653 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4654 | if "Error" in response: |
| 4655 | main.log.error( response ) |
| 4656 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4657 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4658 | except AssertionError: |
| 4659 | main.log.exception( "" ) |
| 4660 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4661 | except TypeError: |
| 4662 | main.log.exception( self.name + ": Object not as expected" ) |
| 4663 | return main.FALSE |
| 4664 | except pexpect.EOF: |
| 4665 | main.log.error( self.name + ": EOF exception found" ) |
| 4666 | main.log.error( self.name + ": " + self.handle.before ) |
| 4667 | main.cleanup() |
| 4668 | main.exit() |
| 4669 | except Exception: |
| 4670 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4671 | main.cleanup() |
| 4672 | main.exit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4673 | |
| 4674 | def removeDevice( self, device ): |
| 4675 | ''' |
| 4676 | Description: |
| 4677 | Remove a device from ONOS by passing the uri of the device(s). |
| 4678 | Parameters: |
| 4679 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
| 4680 | Returns: |
| 4681 | Returns main.FALSE if an exception is thrown or an error is present |
| 4682 | in the response. Otherwise, returns main.TRUE. |
| 4683 | NOTE: |
| 4684 | If a host cannot be removed, then this function will return main.FALSE |
| 4685 | ''' |
| 4686 | try: |
| 4687 | if type( device ) is str: |
| 4688 | device = list( device ) |
| 4689 | |
| 4690 | for d in device: |
| 4691 | time.sleep( 1 ) |
| 4692 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4693 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4694 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4695 | if "Error" in response: |
| 4696 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4697 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4698 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4699 | except AssertionError: |
| 4700 | main.log.exception( "" ) |
| 4701 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4702 | except TypeError: |
| 4703 | main.log.exception( self.name + ": Object not as expected" ) |
| 4704 | return main.FALSE |
| 4705 | except pexpect.EOF: |
| 4706 | main.log.error( self.name + ": EOF exception found" ) |
| 4707 | main.log.error( self.name + ": " + self.handle.before ) |
| 4708 | main.cleanup() |
| 4709 | main.exit() |
| 4710 | except Exception: |
| 4711 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4712 | main.cleanup() |
| 4713 | main.exit() |
| 4714 | |
| 4715 | def removeHost( self, host ): |
| 4716 | ''' |
| 4717 | Description: |
| 4718 | Remove a host from ONOS by passing the id of the host(s) |
| 4719 | Parameters: |
| 4720 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
| 4721 | Returns: |
| 4722 | Returns main.FALSE if an exception is thrown or an error is present |
| 4723 | in the response. Otherwise, returns main.TRUE. |
| 4724 | NOTE: |
| 4725 | If a host cannot be removed, then this function will return main.FALSE |
| 4726 | ''' |
| 4727 | try: |
| 4728 | if type( host ) is str: |
| 4729 | host = list( host ) |
| 4730 | |
| 4731 | for h in host: |
| 4732 | time.sleep( 1 ) |
| 4733 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4734 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4735 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4736 | if "Error" in response: |
| 4737 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 4738 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4739 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4740 | except AssertionError: |
| 4741 | main.log.exception( "" ) |
| 4742 | return None |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4743 | except TypeError: |
| 4744 | main.log.exception( self.name + ": Object not as expected" ) |
| 4745 | return main.FALSE |
| 4746 | except pexpect.EOF: |
| 4747 | main.log.error( self.name + ": EOF exception found" ) |
| 4748 | main.log.error( self.name + ": " + self.handle.before ) |
| 4749 | main.cleanup() |
| 4750 | main.exit() |
| 4751 | except Exception: |
| 4752 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4753 | main.cleanup() |
| 4754 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4755 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4756 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4757 | ''' |
| 4758 | Description: |
| 4759 | Bring link down or up in the null-provider. |
| 4760 | params: |
| 4761 | begin - (string) One end of a device or switch. |
| 4762 | end - (string) the other end of the device or switch |
| 4763 | returns: |
| 4764 | main.TRUE if no exceptions were thrown and no Errors are |
| 4765 | present in the resoponse. Otherwise, returns main.FALSE |
| 4766 | ''' |
| 4767 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4768 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4769 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4770 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4771 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4772 | if "Error" in response or "Failure" in response: |
| 4773 | main.log.error( response ) |
| 4774 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4775 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4776 | except AssertionError: |
| 4777 | main.log.exception( "" ) |
| 4778 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4779 | except TypeError: |
| 4780 | main.log.exception( self.name + ": Object not as expected" ) |
| 4781 | return main.FALSE |
| 4782 | except pexpect.EOF: |
| 4783 | main.log.error( self.name + ": EOF exception found" ) |
| 4784 | main.log.error( self.name + ": " + self.handle.before ) |
| 4785 | main.cleanup() |
| 4786 | main.exit() |
| 4787 | except Exception: |
| 4788 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4789 | main.cleanup() |
| 4790 | main.exit() |
| 4791 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4792 | def portstate(self, dpid='of:0000000000000102', port='2', state='enable'): |
| 4793 | ''' |
| 4794 | Description: |
| 4795 | Changes the state of port in an OF switch by means of the |
| 4796 | PORTSTATUS OF messages. |
| 4797 | params: |
| 4798 | dpid - (string) Datapath ID of the device |
| 4799 | port - (string) target port in the device |
| 4800 | state - (string) target state (enable or disabled) |
| 4801 | returns: |
| 4802 | main.TRUE if no exceptions were thrown and no Errors are |
| 4803 | present in the resoponse. Otherwise, returns main.FALSE |
| 4804 | ''' |
| 4805 | try: |
| 4806 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
| 4807 | response = self.sendline( cmd, showResponse=True ) |
| 4808 | assert response is not None, "Error in sendline" |
| 4809 | assert "Command not found:" not in response, response |
| 4810 | if "Error" in response or "Failure" in response: |
| 4811 | main.log.error( response ) |
| 4812 | return main.FALSE |
| 4813 | return main.TRUE |
| 4814 | except AssertionError: |
| 4815 | main.log.exception( "" ) |
| 4816 | return None |
| 4817 | except TypeError: |
| 4818 | main.log.exception( self.name + ": Object not as expected" ) |
| 4819 | return main.FALSE |
| 4820 | except pexpect.EOF: |
| 4821 | main.log.error( self.name + ": EOF exception found" ) |
| 4822 | main.log.error( self.name + ": " + self.handle.before ) |
| 4823 | main.cleanup() |
| 4824 | main.exit() |
| 4825 | except Exception: |
| 4826 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4827 | main.cleanup() |
| 4828 | main.exit() |
| 4829 | |
| 4830 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 4831 | """ |
| 4832 | Set the logging level to lvl for a specific app |
| 4833 | returns main.TRUE on success |
| 4834 | returns main.FALSE if Error occurred |
| 4835 | if noExit is True, TestON will not exit, but clean up |
| 4836 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 4837 | Level defaults to INFO |
| 4838 | """ |
| 4839 | try: |
| 4840 | self.handle.sendline( "log:set %s %s" %( level, app ) ) |
| 4841 | self.handle.expect( "onos>" ) |
| 4842 | |
| 4843 | response = self.handle.before |
| 4844 | if re.search( "Error", response ): |
| 4845 | return main.FALSE |
| 4846 | return main.TRUE |
| 4847 | except pexpect.TIMEOUT: |
| 4848 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 4849 | main.cleanup() |
| 4850 | main.exit() |
| 4851 | except pexpect.EOF: |
| 4852 | main.log.error( self.name + ": EOF exception found" ) |
| 4853 | main.log.error( self.name + ": " + self.handle.before ) |
| 4854 | main.cleanup() |
| 4855 | main.exit() |
| 4856 | except Exception: |
| 4857 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4858 | main.cleanup() |
| 4859 | main.exit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4860 | |
| 4861 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 4862 | """ |
| 4863 | Return a dictionary which describes the latest network topology data as a |
| 4864 | graph. |
| 4865 | An example of the dictionary: |
| 4866 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 4867 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 4868 | Each vertex should at least have an 'edges' attribute which describes the |
| 4869 | adjacency information. The value of 'edges' attribute is also represented by |
| 4870 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 4871 | list of attributes. |
| 4872 | An example of the edges dictionary: |
| 4873 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 4874 | vertex3: { 'port': ..., 'weight': ... } } |
| 4875 | If includeHost == True, all hosts (and host-switch links) will be included |
| 4876 | in topology data. |
| 4877 | """ |
| 4878 | graphDict = {} |
| 4879 | try: |
| 4880 | links = self.links() |
| 4881 | links = json.loads( links ) |
| 4882 | devices = self.devices() |
| 4883 | devices = json.loads( devices ) |
| 4884 | idToDevice = {} |
| 4885 | for device in devices: |
| 4886 | idToDevice[ device[ 'id' ] ] = device |
| 4887 | if includeHost: |
| 4888 | hosts = self.hosts() |
| 4889 | # FIXME: support 'includeHost' argument |
| 4890 | for link in links: |
| 4891 | nodeA = link[ 'src' ][ 'device' ] |
| 4892 | nodeB = link[ 'dst' ][ 'device' ] |
| 4893 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
| 4894 | if not nodeA in graphDict.keys(): |
| 4895 | graphDict[ nodeA ] = { 'edges':{}, |
| 4896 | 'dpid':idToDevice[ nodeA ][ 'id' ][3:], |
| 4897 | 'type':idToDevice[ nodeA ][ 'type' ], |
| 4898 | 'available':idToDevice[ nodeA ][ 'available' ], |
| 4899 | 'role':idToDevice[ nodeA ][ 'role' ], |
| 4900 | 'mfr':idToDevice[ nodeA ][ 'mfr' ], |
| 4901 | 'hw':idToDevice[ nodeA ][ 'hw' ], |
| 4902 | 'sw':idToDevice[ nodeA ][ 'sw' ], |
| 4903 | 'serial':idToDevice[ nodeA ][ 'serial' ], |
| 4904 | 'chassisId':idToDevice[ nodeA ][ 'chassisId' ], |
| 4905 | 'annotations':idToDevice[ nodeA ][ 'annotations' ]} |
| 4906 | else: |
| 4907 | # Assert nodeB is not connected to any current links of nodeA |
| 4908 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 4909 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port':link[ 'src' ][ 'port' ], |
| 4910 | 'type':link[ 'type' ], |
| 4911 | 'state':link[ 'state' ] } |
| 4912 | return graphDict |
| 4913 | except ( TypeError, ValueError ): |
| 4914 | main.log.exception( self.name + ": Object not as expected" ) |
| 4915 | return None |
| 4916 | except KeyError: |
| 4917 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4918 | return None |
| 4919 | except AssertionError: |
| 4920 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4921 | return None |
| 4922 | except pexpect.EOF: |
| 4923 | main.log.error( self.name + ": EOF exception found" ) |
| 4924 | main.log.error( self.name + ": " + self.handle.before ) |
| 4925 | return None |
| 4926 | except Exception: |
| 4927 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4928 | return None |