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