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