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