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