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