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