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