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