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 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 17 | Modified 2016 by ON.Lab |
| 18 | |
| 19 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 20 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 21 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 22 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 23 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 24 | import pexpect |
| 25 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 26 | import json |
| 27 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 28 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 29 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 30 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 31 | from core.graph import Graph |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 32 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 33 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 34 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 35 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 36 | def __init__( self ): |
| 37 | """ |
| 38 | Initialize client |
| 39 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 40 | self.name = None |
| 41 | self.home = None |
| 42 | self.handle = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 43 | self.graph = Graph() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | super( CLI, self ).__init__() |
| 45 | |
| 46 | def connect( self, **connectargs ): |
| 47 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 48 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 49 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 50 | try: |
| 51 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 53 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 54 | for key in self.options: |
| 55 | if key == "home": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 56 | self.home = self.options[ 'home' ] |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 57 | break |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 58 | if self.home is None or self.home == "": |
Jon Hall | e94919c | 2015-03-23 11:42:57 -0700 | [diff] [blame] | 59 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 60 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 61 | for key in self.options: |
| 62 | if key == 'onosIp': |
| 63 | self.onosIp = self.options[ 'onosIp' ] |
| 64 | break |
| 65 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 66 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 67 | |
| 68 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 69 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 70 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 71 | else: |
| 72 | main.log.info( self.name + |
| 73 | ": Trying to connect to " + |
| 74 | self.ip_address ) |
| 75 | |
| 76 | except KeyError: |
| 77 | main.log.info( "Invalid host name," + |
| 78 | " connecting to local host instead" ) |
| 79 | self.ip_address = 'localhost' |
| 80 | except Exception as inst: |
| 81 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 82 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 83 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 84 | user_name=self.user_name, |
| 85 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 86 | port=self.port, |
| 87 | pwd=self.pwd, |
| 88 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 89 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 90 | self.handle.sendline( "cd " + self.home ) |
| 91 | self.handle.expect( "\$" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 92 | if self.handle: |
| 93 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | else: |
| 95 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 96 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 97 | except TypeError: |
| 98 | main.log.exception( self.name + ": Object not as expected" ) |
| 99 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 100 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 101 | main.log.error( self.name + ": EOF exception found" ) |
| 102 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 103 | main.cleanup() |
| 104 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 105 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 106 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 107 | main.cleanup() |
| 108 | main.exit() |
| 109 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 110 | def disconnect( self ): |
| 111 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 112 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 113 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 114 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 115 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 116 | if self.handle: |
| 117 | i = self.logout() |
| 118 | if i == main.TRUE: |
| 119 | self.handle.sendline( "" ) |
| 120 | self.handle.expect( "\$" ) |
| 121 | self.handle.sendline( "exit" ) |
| 122 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 123 | except TypeError: |
| 124 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 125 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 126 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 127 | main.log.error( self.name + ": EOF exception found" ) |
| 128 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 129 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 130 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 131 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 132 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 133 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | response = main.FALSE |
| 135 | return response |
| 136 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 137 | def logout( self ): |
| 138 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 139 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 140 | Returns main.TRUE if exited CLI and |
| 141 | main.FALSE on timeout (not guranteed you are disconnected) |
| 142 | None on TypeError |
| 143 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 144 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 145 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 146 | if self.handle: |
| 147 | self.handle.sendline( "" ) |
| 148 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ], |
| 149 | timeout=10 ) |
| 150 | if i == 0: # In ONOS CLI |
| 151 | self.handle.sendline( "logout" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 152 | j = self.handle.expect( [ "\$", |
| 153 | "Command not found:", |
| 154 | pexpect.TIMEOUT ] ) |
| 155 | if j == 0: # Successfully logged out |
| 156 | return main.TRUE |
| 157 | elif j == 1 or j == 2: |
| 158 | # ONOS didn't fully load, and logout command isn't working |
| 159 | # or the command timed out |
| 160 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 161 | try: |
| 162 | self.handle.expect( "\$" ) |
| 163 | except pexpect.TIMEOUT: |
| 164 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 165 | return main.TRUE |
| 166 | else: # some other output |
| 167 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 168 | repr( self.handle.before ) ) |
| 169 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 170 | elif i == 1: # not in CLI |
| 171 | return main.TRUE |
| 172 | elif i == 3: # Timeout |
| 173 | return main.FALSE |
| 174 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 175 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 176 | except TypeError: |
| 177 | main.log.exception( self.name + ": Object not as expected" ) |
| 178 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 179 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 181 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 182 | main.cleanup() |
| 183 | main.exit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 184 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 185 | main.log.error( self.name + |
| 186 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 187 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 188 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 189 | main.cleanup() |
| 190 | main.exit() |
| 191 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 192 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 193 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 194 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 196 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 197 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 198 | try: |
| 199 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 200 | main.log.error( "Must define cellname" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 201 | main.cleanup() |
| 202 | main.exit() |
| 203 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 204 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 205 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 206 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 207 | # and that this driver will have to change accordingly |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 208 | self.handle.expect(str(cellname)) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 209 | handleBefore = self.handle.before |
| 210 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 211 | # Get the rest of the handle |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 212 | self.handle.sendline("") |
| 213 | self.handle.expect("\$") |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 214 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 215 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 216 | main.log.info( "Cell call returned: " + handleBefore + |
| 217 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 218 | |
| 219 | return main.TRUE |
| 220 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 221 | except TypeError: |
| 222 | main.log.exception( self.name + ": Object not as expected" ) |
| 223 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 224 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 225 | main.log.error( self.name + ": eof exception found" ) |
| 226 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 227 | main.cleanup() |
| 228 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 229 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 230 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 231 | main.cleanup() |
| 232 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 233 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 234 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 235 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 236 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 237 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 238 | by user would be used to set the current karaf shell idle timeout. |
| 239 | 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] | 240 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 241 | Below is an example to start a session with 60 seconds idle timeout |
| 242 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 243 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 244 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 245 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 246 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 247 | Note: karafTimeout is left as str so that this could be read |
| 248 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 249 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 250 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 251 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 252 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 253 | self.handle.sendline( "" ) |
| 254 | x = self.handle.expect( [ |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 255 | "\$", "onos>" ], commandlineTimeout) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 256 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 257 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 258 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 259 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 260 | # Not in CLI so login |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 261 | if waitForStart: |
| 262 | # Wait for onos start ( -w ) and enter onos cli |
| 263 | startCliCommand = "onos -w " |
| 264 | else: |
| 265 | startCliCommand = "onos " |
| 266 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | i = self.handle.expect( [ |
| 268 | "onos>", |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 269 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 270 | |
| 271 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 272 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 273 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 274 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 275 | "config:property-set -p org.apache.karaf.shell\ |
| 276 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 277 | karafTimeout ) |
| 278 | self.handle.expect( "\$" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 279 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 280 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 281 | return main.TRUE |
| 282 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 283 | # If failed, send ctrl+c to process and try again |
| 284 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 285 | self.handle.send( "\x03" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 286 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 287 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 288 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 289 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 290 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 291 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 292 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 293 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 294 | "config:property-set -p org.apache.karaf.shell\ |
| 295 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 296 | karafTimeout ) |
| 297 | self.handle.expect( "\$" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 298 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 299 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 300 | return main.TRUE |
| 301 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 302 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 303 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 304 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 305 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 306 | except TypeError: |
| 307 | main.log.exception( self.name + ": Object not as expected" ) |
| 308 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 309 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 310 | main.log.error( self.name + ": EOF exception found" ) |
| 311 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 312 | main.cleanup() |
| 313 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 314 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 315 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 316 | main.cleanup() |
| 317 | main.exit() |
| 318 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 319 | def startCellCli( self, karafTimeout="", |
| 320 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 321 | """ |
| 322 | Start CLI on onos ecll handle. |
| 323 | |
| 324 | karafTimeout is an optional argument. karafTimeout value passed |
| 325 | by user would be used to set the current karaf shell idle timeout. |
| 326 | Note that when ever this property is modified the shell will exit and |
| 327 | the subsequent login would reflect new idle timeout. |
| 328 | Below is an example to start a session with 60 seconds idle timeout |
| 329 | ( input value is in milliseconds ): |
| 330 | |
| 331 | tValue = "60000" |
| 332 | |
| 333 | Note: karafTimeout is left as str so that this could be read |
| 334 | and passed to startOnosCli from PARAMS file as str. |
| 335 | """ |
| 336 | |
| 337 | try: |
| 338 | self.handle.sendline( "" ) |
| 339 | x = self.handle.expect( [ |
| 340 | "\$", "onos>" ], commandlineTimeout) |
| 341 | |
| 342 | if x == 1: |
| 343 | main.log.info( "ONOS cli is already running" ) |
| 344 | return main.TRUE |
| 345 | |
| 346 | # Wait for onos start ( -w ) and enter onos cli |
| 347 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 348 | i = self.handle.expect( [ |
| 349 | "onos>", |
| 350 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 351 | |
| 352 | if i == 0: |
| 353 | main.log.info( self.name + " CLI Started successfully" ) |
| 354 | if karafTimeout: |
| 355 | self.handle.sendline( |
| 356 | "config:property-set -p org.apache.karaf.shell\ |
| 357 | sshIdleTimeout " + |
| 358 | karafTimeout ) |
| 359 | self.handle.expect( "\$" ) |
| 360 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 361 | self.handle.expect( "onos>" ) |
| 362 | return main.TRUE |
| 363 | else: |
| 364 | # If failed, send ctrl+c to process and try again |
| 365 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 366 | self.handle.send( "\x03" ) |
| 367 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 368 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 369 | timeout=30 ) |
| 370 | if i == 0: |
| 371 | main.log.info( self.name + " CLI Started " + |
| 372 | "successfully after retry attempt" ) |
| 373 | if karafTimeout: |
| 374 | self.handle.sendline( |
| 375 | "config:property-set -p org.apache.karaf.shell\ |
| 376 | sshIdleTimeout " + |
| 377 | karafTimeout ) |
| 378 | self.handle.expect( "\$" ) |
| 379 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 380 | self.handle.expect( "onos>" ) |
| 381 | return main.TRUE |
| 382 | else: |
| 383 | main.log.error( "Connection to CLI " + |
| 384 | self.name + " timeout" ) |
| 385 | return main.FALSE |
| 386 | |
| 387 | except TypeError: |
| 388 | main.log.exception( self.name + ": Object not as expected" ) |
| 389 | return None |
| 390 | except pexpect.EOF: |
| 391 | main.log.error( self.name + ": EOF exception found" ) |
| 392 | main.log.error( self.name + ": " + self.handle.before ) |
| 393 | main.cleanup() |
| 394 | main.exit() |
| 395 | except Exception: |
| 396 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 397 | main.cleanup() |
| 398 | main.exit() |
| 399 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 400 | def log( self, cmdStr, level="",noExit=False): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 401 | """ |
| 402 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 403 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 404 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 405 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 406 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 407 | Level defaults to INFO |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 408 | """ |
| 409 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 410 | lvlStr = "" |
| 411 | if level: |
| 412 | lvlStr = "--level=" + level |
| 413 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 414 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 415 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 416 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 417 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 418 | response = self.handle.before |
| 419 | if re.search( "Error", response ): |
| 420 | return main.FALSE |
| 421 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 422 | except pexpect.TIMEOUT: |
| 423 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 424 | if noExit: |
| 425 | main.cleanup() |
| 426 | return None |
| 427 | else: |
| 428 | main.cleanup() |
| 429 | main.exit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 430 | except pexpect.EOF: |
| 431 | main.log.error( self.name + ": EOF exception found" ) |
| 432 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 433 | if noExit: |
| 434 | main.cleanup() |
| 435 | return None |
| 436 | else: |
| 437 | main.cleanup() |
| 438 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 439 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 440 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 441 | if noExit: |
| 442 | main.cleanup() |
| 443 | return None |
| 444 | else: |
| 445 | main.cleanup() |
| 446 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 447 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 448 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 449 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 450 | Send a completely user specified string to |
| 451 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 452 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 453 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 454 | if noExit is True, TestON will not exit, and return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 455 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 456 | Warning: There are no sanity checking to commands |
| 457 | sent using this method. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 458 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 459 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 460 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 461 | # Try to reconnect if disconnected from cli |
| 462 | self.handle.sendline( "" ) |
| 463 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ] ) |
| 464 | if i == 1: |
| 465 | main.log.error( self.name + ": onos cli session closed. ") |
| 466 | if self.onosIp: |
| 467 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 468 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 469 | if reconnectResult: |
| 470 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 471 | else: |
| 472 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 473 | if noExit: |
| 474 | return None |
| 475 | else: |
| 476 | main.cleanup() |
| 477 | main.exit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 478 | else: |
| 479 | main.cleanup() |
| 480 | main.exit() |
| 481 | if i == 2: |
| 482 | self.handle.sendline( "" ) |
| 483 | self.handle.expect( "onos>" ) |
| 484 | |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 485 | if debug: |
| 486 | # NOTE: This adds and average of .4 seconds per call |
| 487 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 488 | self.log( logStr,noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 489 | self.handle.sendline( cmdStr ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 490 | i = self.handle.expect( ["onos>", "\$"], timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 491 | response = self.handle.before |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 492 | # TODO: do something with i |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 493 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
| 494 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 495 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 496 | main.log.debug( self.name + ": Raw output" ) |
| 497 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 498 | |
| 499 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 500 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 501 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 502 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 503 | main.log.debug( self.name + ": ansiEscape output" ) |
| 504 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 505 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 506 | # Remove extra return chars that get added |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 507 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 508 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 509 | main.log.debug( self.name + ": Removed extra returns " + |
| 510 | "from output" ) |
| 511 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 512 | |
| 513 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 514 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 515 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 516 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 517 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 518 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 519 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 520 | output = response.split( cmdStr.strip(), 1 ) |
| 521 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 522 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 523 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 524 | main.log.debug( self.name + ": " + repr( r ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 525 | output = output[1].strip() |
| 526 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 527 | main.log.info( "Response from ONOS: {}".format( output ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 528 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 529 | except pexpect.TIMEOUT: |
| 530 | main.log.error( self.name + ":ONOS timeout" ) |
| 531 | if debug: |
| 532 | main.log.debug( self.handle.before ) |
| 533 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 534 | except IndexError: |
| 535 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 536 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 537 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 538 | except TypeError: |
| 539 | main.log.exception( self.name + ": Object not as expected" ) |
| 540 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 541 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 542 | main.log.error( self.name + ": EOF exception found" ) |
| 543 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 544 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 545 | return None |
| 546 | else: |
| 547 | main.cleanup() |
| 548 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 549 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 550 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 551 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 552 | return None |
| 553 | else: |
| 554 | main.cleanup() |
| 555 | main.exit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 556 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 557 | # IMPORTANT NOTE: |
| 558 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 559 | # the cli command changing 'a:b' with 'aB'. |
| 560 | # Ex ) onos:topology > onosTopology |
| 561 | # onos:links > onosLinks |
| 562 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 563 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 564 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 565 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 566 | Adds a new cluster node by ID and address information. |
| 567 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 568 | * nodeId |
| 569 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 570 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 571 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 572 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 573 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 574 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 575 | str( ONOSIp ) + " " + str( tcpPort ) |
| 576 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 577 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 578 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 579 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 580 | main.log.error( "Error in adding node" ) |
| 581 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 582 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 583 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 584 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 585 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 586 | except AssertionError: |
| 587 | main.log.exception( "" ) |
| 588 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 589 | except TypeError: |
| 590 | main.log.exception( self.name + ": Object not as expected" ) |
| 591 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 592 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 593 | main.log.error( self.name + ": EOF exception found" ) |
| 594 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 595 | main.cleanup() |
| 596 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 597 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 598 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 599 | main.cleanup() |
| 600 | main.exit() |
| 601 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 602 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 603 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 604 | Removes a cluster by ID |
| 605 | Issues command: 'remove-node [<node-id>]' |
| 606 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 607 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 608 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 609 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 610 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 611 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 612 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 613 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 614 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 615 | if re.search( "Error", handle ): |
| 616 | main.log.error( "Error in removing node" ) |
| 617 | main.log.error( handle ) |
| 618 | return main.FALSE |
| 619 | else: |
| 620 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 621 | except AssertionError: |
| 622 | main.log.exception( "" ) |
| 623 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 624 | except TypeError: |
| 625 | main.log.exception( self.name + ": Object not as expected" ) |
| 626 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 627 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 628 | main.log.error( self.name + ": EOF exception found" ) |
| 629 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 630 | main.cleanup() |
| 631 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 632 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 633 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 634 | main.cleanup() |
| 635 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 636 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 637 | def nodes( self, jsonFormat=True): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 638 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 639 | List the nodes currently visible |
| 640 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 641 | Optional argument: |
| 642 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 643 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 644 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 645 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 646 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 647 | cmdStr += " -j" |
| 648 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 649 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 650 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 651 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 652 | except AssertionError: |
| 653 | main.log.exception( "" ) |
| 654 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 655 | except TypeError: |
| 656 | main.log.exception( self.name + ": Object not as expected" ) |
| 657 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 658 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 659 | main.log.error( self.name + ": EOF exception found" ) |
| 660 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 661 | main.cleanup() |
| 662 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 663 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 664 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 665 | main.cleanup() |
| 666 | main.exit() |
| 667 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 668 | def topology( self ): |
| 669 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 670 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 671 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 672 | Return: |
| 673 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 674 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 675 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 676 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 677 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 678 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 679 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 680 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 681 | except AssertionError: |
| 682 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 683 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 684 | except TypeError: |
| 685 | main.log.exception( self.name + ": Object not as expected" ) |
| 686 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 687 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 688 | main.log.error( self.name + ": EOF exception found" ) |
| 689 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 690 | main.cleanup() |
| 691 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 692 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 693 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 694 | main.cleanup() |
| 695 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 696 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 697 | def deviceRemove( self, deviceId ): |
| 698 | """ |
| 699 | Removes particular device from storage |
| 700 | |
| 701 | TODO: refactor this function |
| 702 | """ |
| 703 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 704 | cmdStr = "device-remove " + str( deviceId ) |
| 705 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 706 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 707 | if re.search( "Error", handle ): |
| 708 | main.log.error( "Error in removing device" ) |
| 709 | main.log.error( handle ) |
| 710 | return main.FALSE |
| 711 | else: |
| 712 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 713 | except AssertionError: |
| 714 | main.log.exception( "" ) |
| 715 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 716 | except TypeError: |
| 717 | main.log.exception( self.name + ": Object not as expected" ) |
| 718 | return None |
| 719 | except pexpect.EOF: |
| 720 | main.log.error( self.name + ": EOF exception found" ) |
| 721 | main.log.error( self.name + ": " + self.handle.before ) |
| 722 | main.cleanup() |
| 723 | main.exit() |
| 724 | except Exception: |
| 725 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 726 | main.cleanup() |
| 727 | main.exit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 728 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 729 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 730 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 731 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 732 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 733 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 734 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 735 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 736 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 737 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 738 | cmdStr += " -j" |
| 739 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 740 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 741 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 742 | except AssertionError: |
| 743 | main.log.exception( "" ) |
| 744 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 745 | except TypeError: |
| 746 | main.log.exception( self.name + ": Object not as expected" ) |
| 747 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 748 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 749 | main.log.error( self.name + ": EOF exception found" ) |
| 750 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 751 | main.cleanup() |
| 752 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 753 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 754 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 755 | main.cleanup() |
| 756 | main.exit() |
| 757 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 758 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 759 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 760 | This balances the devices across all controllers |
| 761 | by issuing command: 'onos> onos:balance-masters' |
| 762 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 763 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 764 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 765 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 766 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 767 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 768 | if re.search( "Error", handle ): |
| 769 | main.log.error( "Error in balancing masters" ) |
| 770 | main.log.error( handle ) |
| 771 | return main.FALSE |
| 772 | else: |
| 773 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 774 | except AssertionError: |
| 775 | main.log.exception( "" ) |
| 776 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 777 | except TypeError: |
| 778 | main.log.exception( self.name + ": Object not as expected" ) |
| 779 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 780 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 781 | main.log.error( self.name + ": EOF exception found" ) |
| 782 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 783 | main.cleanup() |
| 784 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 785 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 786 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 787 | main.cleanup() |
| 788 | main.exit() |
| 789 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 790 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 791 | """ |
| 792 | Returns the output of the masters command. |
| 793 | Optional argument: |
| 794 | * jsonFormat - boolean indicating if you want output in json |
| 795 | """ |
| 796 | try: |
| 797 | cmdStr = "onos:masters" |
| 798 | if jsonFormat: |
| 799 | cmdStr += " -j" |
| 800 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 801 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 802 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 803 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 804 | except AssertionError: |
| 805 | main.log.exception( "" ) |
| 806 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 807 | except TypeError: |
| 808 | main.log.exception( self.name + ": Object not as expected" ) |
| 809 | return None |
| 810 | except pexpect.EOF: |
| 811 | main.log.error( self.name + ": EOF exception found" ) |
| 812 | main.log.error( self.name + ": " + self.handle.before ) |
| 813 | main.cleanup() |
| 814 | main.exit() |
| 815 | except Exception: |
| 816 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 817 | main.cleanup() |
| 818 | main.exit() |
| 819 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 820 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 821 | """ |
| 822 | Uses the master command to check that the devices' leadership |
| 823 | is evenly divided |
| 824 | |
| 825 | Dependencies: checkMasters() and summary() |
| 826 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 827 | Returns main.TRUE if the devices are balanced |
| 828 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 829 | Exits on Exception |
| 830 | Returns None on TypeError |
| 831 | """ |
| 832 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 833 | summaryOutput = self.summary() |
| 834 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 835 | except ( TypeError, ValueError ): |
| 836 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 837 | return None |
| 838 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 839 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 840 | mastersOutput = self.checkMasters() |
| 841 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 842 | first = masters[ 0 ][ "size" ] |
| 843 | for master in masters: |
| 844 | totalOwnedDevices += master[ "size" ] |
| 845 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 846 | main.log.error( "Mastership not balanced" ) |
| 847 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 848 | return main.FALSE |
| 849 | main.log.info( "Mastership balanced between " \ |
| 850 | + str( len(masters) ) + " masters" ) |
| 851 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 852 | except ( TypeError, ValueError ): |
| 853 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 854 | return None |
| 855 | except pexpect.EOF: |
| 856 | main.log.error( self.name + ": EOF exception found" ) |
| 857 | main.log.error( self.name + ": " + self.handle.before ) |
| 858 | main.cleanup() |
| 859 | main.exit() |
| 860 | except Exception: |
| 861 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 862 | main.cleanup() |
| 863 | main.exit() |
| 864 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 865 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 866 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 867 | Lists all core links |
| 868 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 869 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 870 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 871 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 872 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 873 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 874 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 875 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 876 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 877 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 878 | except AssertionError: |
| 879 | main.log.exception( "" ) |
| 880 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 881 | except TypeError: |
| 882 | main.log.exception( self.name + ": Object not as expected" ) |
| 883 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 884 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 885 | main.log.error( self.name + ": EOF exception found" ) |
| 886 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 887 | main.cleanup() |
| 888 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 889 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 890 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 891 | main.cleanup() |
| 892 | main.exit() |
| 893 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 894 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 895 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 896 | Lists all ports |
| 897 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 898 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 899 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 900 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 901 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 902 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 903 | cmdStr += " -j" |
| 904 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 905 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 906 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 907 | except AssertionError: |
| 908 | main.log.exception( "" ) |
| 909 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 910 | except TypeError: |
| 911 | main.log.exception( self.name + ": Object not as expected" ) |
| 912 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 913 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 914 | main.log.error( self.name + ": EOF exception found" ) |
| 915 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 916 | main.cleanup() |
| 917 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 918 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 919 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 920 | main.cleanup() |
| 921 | main.exit() |
| 922 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 923 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 924 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 925 | Lists all devices and the controllers with roles assigned to them |
| 926 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 927 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 928 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 929 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 930 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 931 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 932 | cmdStr += " -j" |
| 933 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 934 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 935 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 936 | except AssertionError: |
| 937 | main.log.exception( "" ) |
| 938 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 939 | except TypeError: |
| 940 | main.log.exception( self.name + ": Object not as expected" ) |
| 941 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 942 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 943 | main.log.error( self.name + ": EOF exception found" ) |
| 944 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 945 | main.cleanup() |
| 946 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 947 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 948 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 949 | main.cleanup() |
| 950 | main.exit() |
| 951 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 952 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 953 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 954 | Given the a string containing the json representation of the "roles" |
| 955 | cli command and a partial or whole device id, returns a json object |
| 956 | containing the roles output for the first device whose id contains |
| 957 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 958 | |
| 959 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 960 | A dict of the role assignments for the given device or |
| 961 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 962 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 963 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 964 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 965 | return None |
| 966 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 967 | rawRoles = self.roles() |
| 968 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 969 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 970 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 971 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 972 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 973 | return device |
| 974 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 975 | except ( TypeError, ValueError ): |
| 976 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 977 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 978 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 979 | main.log.error( self.name + ": EOF exception found" ) |
| 980 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 981 | main.cleanup() |
| 982 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 983 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 984 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 985 | main.cleanup() |
| 986 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 987 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 988 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 989 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 990 | Iterates through each device and checks if there is a master assigned |
| 991 | Returns: main.TRUE if each device has a master |
| 992 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 993 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 994 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 995 | rawRoles = self.roles() |
| 996 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 997 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 998 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 999 | # print device |
| 1000 | if device[ 'master' ] == "none": |
| 1001 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1002 | return main.FALSE |
| 1003 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1004 | except ( TypeError, ValueError ): |
| 1005 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1006 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1007 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1008 | main.log.error( self.name + ": EOF exception found" ) |
| 1009 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1010 | main.cleanup() |
| 1011 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1012 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1013 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1014 | main.cleanup() |
| 1015 | main.exit() |
| 1016 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1017 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1018 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1019 | Returns string of paths, and the cost. |
| 1020 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1021 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1022 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1023 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1024 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1025 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1026 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1027 | main.log.error( "Error in getting paths" ) |
| 1028 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1029 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1030 | path = handle.split( ";" )[ 0 ] |
| 1031 | cost = handle.split( ";" )[ 1 ] |
| 1032 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1033 | except AssertionError: |
| 1034 | main.log.exception( "" ) |
| 1035 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1036 | except TypeError: |
| 1037 | main.log.exception( self.name + ": Object not as expected" ) |
| 1038 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1039 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1040 | main.log.error( self.name + ": EOF exception found" ) |
| 1041 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1042 | main.cleanup() |
| 1043 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1044 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1045 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1046 | main.cleanup() |
| 1047 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1048 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1049 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1050 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1051 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1052 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1053 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1054 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1055 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1056 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1057 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1058 | cmdStr += " -j" |
| 1059 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1060 | if handle: |
| 1061 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1062 | # TODO: Maybe make this less hardcoded |
| 1063 | # ConsistentMap Exceptions |
| 1064 | assert "org.onosproject.store.service" not in handle |
| 1065 | # Node not leader |
| 1066 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1067 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1068 | except AssertionError: |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1069 | main.log.exception( "Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1070 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1071 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1072 | except TypeError: |
| 1073 | main.log.exception( self.name + ": Object not as expected" ) |
| 1074 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1075 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1076 | main.log.error( self.name + ": EOF exception found" ) |
| 1077 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1078 | main.cleanup() |
| 1079 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1080 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1081 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1082 | main.cleanup() |
| 1083 | main.exit() |
| 1084 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1085 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1086 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1087 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1088 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1089 | 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] | 1090 | partial mac address |
| 1091 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1092 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1093 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1094 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1095 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1096 | return None |
| 1097 | else: |
| 1098 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1099 | rawHosts = self.hosts() |
| 1100 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1101 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1102 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1103 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1104 | if not host: |
| 1105 | pass |
| 1106 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1107 | return host |
| 1108 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1109 | except ( TypeError, ValueError ): |
| 1110 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1111 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1112 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1113 | main.log.error( self.name + ": EOF exception found" ) |
| 1114 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1115 | main.cleanup() |
| 1116 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1117 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1118 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1119 | main.cleanup() |
| 1120 | main.exit() |
| 1121 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1122 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1123 | """ |
| 1124 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1125 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1126 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1127 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1128 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1129 | IMPORTANT: |
| 1130 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1131 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1132 | Furthermore, it assumes that value of VLAN is '-1' |
| 1133 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1134 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1135 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1136 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1137 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1138 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1139 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1140 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1141 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1142 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1143 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1144 | i = iter( str( hostHex ) ) |
| 1145 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1146 | hostHex = hostHex + "/-1" |
| 1147 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1148 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1149 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1150 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1151 | except TypeError: |
| 1152 | main.log.exception( self.name + ": Object not as expected" ) |
| 1153 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1154 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1155 | main.log.error( self.name + ": EOF exception found" ) |
| 1156 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1157 | main.cleanup() |
| 1158 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1159 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1160 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1161 | main.cleanup() |
| 1162 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1163 | |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1164 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1165 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1166 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1167 | * hostIdOne: ONOS host id for host1 |
| 1168 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1169 | Optional: |
| 1170 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1171 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1172 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1173 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1174 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1175 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1176 | Returns: |
| 1177 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1178 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1179 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1180 | cmdStr = "add-host-intent " |
| 1181 | if vlanId: |
| 1182 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1183 | if setVlan: |
| 1184 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1185 | if encap: |
| 1186 | cmdStr += "--encapsulation " + str( encap ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1187 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1188 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1189 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1190 | if re.search( "Error", handle ): |
| 1191 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1192 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1193 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1194 | else: |
| 1195 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1196 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
| 1197 | match = re.search('id=0x([\da-f]+),', handle) |
| 1198 | if match: |
| 1199 | return match.group()[3:-1] |
| 1200 | else: |
| 1201 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1202 | main.log.debug( "Response from ONOS was: " + |
| 1203 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1204 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1205 | except AssertionError: |
| 1206 | main.log.exception( "" ) |
| 1207 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1208 | except TypeError: |
| 1209 | main.log.exception( self.name + ": Object not as expected" ) |
| 1210 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1211 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1212 | main.log.error( self.name + ": EOF exception found" ) |
| 1213 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1214 | main.cleanup() |
| 1215 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1216 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1217 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1218 | main.cleanup() |
| 1219 | main.exit() |
| 1220 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1221 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1222 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1223 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1224 | * ingressDevice: device id of ingress device |
| 1225 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1226 | Optional: |
| 1227 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1228 | Description: |
| 1229 | Adds an optical intent by specifying an ingress and egress device |
| 1230 | Returns: |
| 1231 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1232 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1233 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1234 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1235 | " " + str( egressDevice ) |
| 1236 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1237 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1238 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1239 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1240 | main.log.error( "Error in adding Optical intent" ) |
| 1241 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1242 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1243 | main.log.info( "Optical intent installed between " + |
| 1244 | str( ingressDevice ) + " and " + |
| 1245 | str( egressDevice ) ) |
| 1246 | match = re.search('id=0x([\da-f]+),', handle) |
| 1247 | if match: |
| 1248 | return match.group()[3:-1] |
| 1249 | else: |
| 1250 | main.log.error( "Error, intent ID not found" ) |
| 1251 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1252 | except AssertionError: |
| 1253 | main.log.exception( "" ) |
| 1254 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1255 | except TypeError: |
| 1256 | main.log.exception( self.name + ": Object not as expected" ) |
| 1257 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1258 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1259 | main.log.error( self.name + ": EOF exception found" ) |
| 1260 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1261 | main.cleanup() |
| 1262 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1263 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1264 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1265 | main.cleanup() |
| 1266 | main.exit() |
| 1267 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1268 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1269 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1270 | ingressDevice, |
| 1271 | egressDevice, |
| 1272 | portIngress="", |
| 1273 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1274 | ethType="", |
| 1275 | ethSrc="", |
| 1276 | ethDst="", |
| 1277 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1278 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1279 | ipProto="", |
| 1280 | ipSrc="", |
| 1281 | ipDst="", |
| 1282 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1283 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1284 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1285 | setVlan="", |
| 1286 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1287 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1288 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1289 | * ingressDevice: device id of ingress device |
| 1290 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1291 | Optional: |
| 1292 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1293 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1294 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1295 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1296 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1297 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1298 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1299 | * ipSrc: specify ip source address |
| 1300 | * ipDst: specify ip destination address |
| 1301 | * tcpSrc: specify tcp source port |
| 1302 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1303 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1304 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1305 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1306 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1307 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1308 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1309 | Returns: |
| 1310 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1311 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1312 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1313 | options developers provide for point-to-point |
| 1314 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1315 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1316 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1317 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1318 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1319 | if ethType: |
| 1320 | cmd += " --ethType " + str( ethType ) |
| 1321 | if ethSrc: |
| 1322 | cmd += " --ethSrc " + str( ethSrc ) |
| 1323 | if ethDst: |
| 1324 | cmd += " --ethDst " + str( ethDst ) |
| 1325 | if bandwidth: |
| 1326 | cmd += " --bandwidth " + str( bandwidth ) |
| 1327 | if lambdaAlloc: |
| 1328 | cmd += " --lambda " |
| 1329 | if ipProto: |
| 1330 | cmd += " --ipProto " + str( ipProto ) |
| 1331 | if ipSrc: |
| 1332 | cmd += " --ipSrc " + str( ipSrc ) |
| 1333 | if ipDst: |
| 1334 | cmd += " --ipDst " + str( ipDst ) |
| 1335 | if tcpSrc: |
| 1336 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1337 | if tcpDst: |
| 1338 | cmd += " --tcpDst " + str( tcpDst ) |
| 1339 | if vlanId: |
| 1340 | cmd += " -v " + str( vlanId ) |
| 1341 | if setVlan: |
| 1342 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1343 | if encap: |
| 1344 | cmd += " --encapsulation " + str( encap ) |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1345 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1346 | # Check whether the user appended the port |
| 1347 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1348 | if "/" in ingressDevice: |
| 1349 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1350 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1351 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1352 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1353 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1354 | # Would it make sense to throw an exception and exit |
| 1355 | # the test? |
| 1356 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1357 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1358 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1359 | str( ingressDevice ) + "/" +\ |
| 1360 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1361 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1362 | if "/" in egressDevice: |
| 1363 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1364 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1365 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1366 | main.log.error( "You must specify the egress port" ) |
| 1367 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1368 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1369 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1370 | str( egressDevice ) + "/" +\ |
| 1371 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1372 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1373 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1374 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1375 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1376 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1377 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1378 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1379 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1380 | # TODO: print out all the options in this message? |
| 1381 | main.log.info( "Point-to-point intent installed between " + |
| 1382 | str( ingressDevice ) + " and " + |
| 1383 | str( egressDevice ) ) |
| 1384 | match = re.search('id=0x([\da-f]+),', handle) |
| 1385 | if match: |
| 1386 | return match.group()[3:-1] |
| 1387 | else: |
| 1388 | main.log.error( "Error, intent ID not found" ) |
| 1389 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1390 | except AssertionError: |
| 1391 | main.log.exception( "" ) |
| 1392 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1393 | except TypeError: |
| 1394 | main.log.exception( self.name + ": Object not as expected" ) |
| 1395 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1396 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1397 | main.log.error( self.name + ": EOF exception found" ) |
| 1398 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1399 | main.cleanup() |
| 1400 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1401 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1402 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1403 | main.cleanup() |
| 1404 | main.exit() |
| 1405 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1406 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1407 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1408 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1409 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1410 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1411 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1412 | ethType="", |
| 1413 | ethSrc="", |
| 1414 | ethDst="", |
| 1415 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1416 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1417 | ipProto="", |
| 1418 | ipSrc="", |
| 1419 | ipDst="", |
| 1420 | tcpSrc="", |
| 1421 | tcpDst="", |
| 1422 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1423 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1424 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1425 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1426 | partial=False, |
| 1427 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1428 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1429 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1430 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1431 | is same. That is, all ingress devices include port numbers |
| 1432 | with a "/" or all ingress devices could specify device |
| 1433 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1434 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1435 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1436 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1437 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1438 | Optional: |
| 1439 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1440 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1441 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1442 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1443 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1444 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1445 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1446 | * ipSrc: specify ip source address |
| 1447 | * ipDst: specify ip destination address |
| 1448 | * tcpSrc: specify tcp source port |
| 1449 | * tcpDst: specify tcp destination port |
| 1450 | * setEthSrc: action to Rewrite Source MAC Address |
| 1451 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1452 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1453 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1454 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1455 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1456 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1457 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1458 | Returns: |
| 1459 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1460 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1461 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1462 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1463 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1464 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1465 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1466 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1467 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1468 | if ethType: |
| 1469 | cmd += " --ethType " + str( ethType ) |
| 1470 | if ethSrc: |
| 1471 | cmd += " --ethSrc " + str( ethSrc ) |
| 1472 | if ethDst: |
| 1473 | cmd += " --ethDst " + str( ethDst ) |
| 1474 | if bandwidth: |
| 1475 | cmd += " --bandwidth " + str( bandwidth ) |
| 1476 | if lambdaAlloc: |
| 1477 | cmd += " --lambda " |
| 1478 | if ipProto: |
| 1479 | cmd += " --ipProto " + str( ipProto ) |
| 1480 | if ipSrc: |
| 1481 | cmd += " --ipSrc " + str( ipSrc ) |
| 1482 | if ipDst: |
| 1483 | cmd += " --ipDst " + str( ipDst ) |
| 1484 | if tcpSrc: |
| 1485 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1486 | if tcpDst: |
| 1487 | cmd += " --tcpDst " + str( tcpDst ) |
| 1488 | if setEthSrc: |
| 1489 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1490 | if setEthDst: |
| 1491 | cmd += " --setEthDst " + str( setEthDst ) |
| 1492 | if vlanId: |
| 1493 | cmd += " -v " + str( vlanId ) |
| 1494 | if setVlan: |
| 1495 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1496 | if partial: |
| 1497 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1498 | if encap: |
| 1499 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1500 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1501 | # Check whether the user appended the port |
| 1502 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1503 | |
| 1504 | if portIngressList is None: |
| 1505 | for ingressDevice in ingressDeviceList: |
| 1506 | if "/" in ingressDevice: |
| 1507 | cmd += " " + str( ingressDevice ) |
| 1508 | else: |
| 1509 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1510 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1511 | # TODO: perhaps more meaningful return |
| 1512 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1513 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1514 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1515 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1516 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1517 | cmd += " " + \ |
| 1518 | str( ingressDevice ) + "/" +\ |
| 1519 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1520 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1521 | main.log.error( "Device list and port list does not " + |
| 1522 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1523 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1524 | if "/" in egressDevice: |
| 1525 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1526 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1527 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1528 | main.log.error( "You must specify " + |
| 1529 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1530 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1531 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1532 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1533 | str( egressDevice ) + "/" +\ |
| 1534 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1535 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1536 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1537 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1538 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1539 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1540 | "intent" ) |
| 1541 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1542 | else: |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1543 | match = re.search('id=0x([\da-f]+),', handle) |
| 1544 | if match: |
| 1545 | return match.group()[3:-1] |
| 1546 | else: |
| 1547 | main.log.error( "Error, intent ID not found" ) |
| 1548 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1549 | except AssertionError: |
| 1550 | main.log.exception( "" ) |
| 1551 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1552 | except TypeError: |
| 1553 | main.log.exception( self.name + ": Object not as expected" ) |
| 1554 | return None |
| 1555 | except pexpect.EOF: |
| 1556 | main.log.error( self.name + ": EOF exception found" ) |
| 1557 | main.log.error( self.name + ": " + self.handle.before ) |
| 1558 | main.cleanup() |
| 1559 | main.exit() |
| 1560 | except Exception: |
| 1561 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1562 | main.cleanup() |
| 1563 | main.exit() |
| 1564 | |
| 1565 | def addSinglepointToMultipointIntent( |
| 1566 | self, |
| 1567 | ingressDevice, |
| 1568 | egressDeviceList, |
| 1569 | portIngress="", |
| 1570 | portEgressList=None, |
| 1571 | ethType="", |
| 1572 | ethSrc="", |
| 1573 | ethDst="", |
| 1574 | bandwidth="", |
| 1575 | lambdaAlloc=False, |
| 1576 | ipProto="", |
| 1577 | ipSrc="", |
| 1578 | ipDst="", |
| 1579 | tcpSrc="", |
| 1580 | tcpDst="", |
| 1581 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1582 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1583 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1584 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1585 | partial=False, |
| 1586 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1587 | """ |
| 1588 | Note: |
| 1589 | This function assumes the format of all egress devices |
| 1590 | is same. That is, all egress devices include port numbers |
| 1591 | with a "/" or all egress devices could specify device |
| 1592 | ids and port numbers seperately. |
| 1593 | Required: |
| 1594 | * EgressDeviceList: List of device ids of egress device |
| 1595 | ( Atleast 2 eress devices required in the list ) |
| 1596 | * ingressDevice: device id of ingress device |
| 1597 | Optional: |
| 1598 | * ethType: specify ethType |
| 1599 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1600 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1601 | * bandwidth: specify bandwidth capacity of link |
| 1602 | * lambdaAlloc: if True, intent will allocate lambda |
| 1603 | for the specified intent |
| 1604 | * ipProto: specify ip protocol |
| 1605 | * ipSrc: specify ip source address |
| 1606 | * ipDst: specify ip destination address |
| 1607 | * tcpSrc: specify tcp source port |
| 1608 | * tcpDst: specify tcp destination port |
| 1609 | * setEthSrc: action to Rewrite Source MAC Address |
| 1610 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1611 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1612 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1613 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1614 | Description: |
| 1615 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1616 | specifying device id's and optional fields |
| 1617 | Returns: |
| 1618 | A string of the intent id or None on error |
| 1619 | |
| 1620 | NOTE: This function may change depending on the |
| 1621 | options developers provide for singlepoint-to-multipoint |
| 1622 | intent via cli |
| 1623 | """ |
| 1624 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1625 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1626 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1627 | if ethType: |
| 1628 | cmd += " --ethType " + str( ethType ) |
| 1629 | if ethSrc: |
| 1630 | cmd += " --ethSrc " + str( ethSrc ) |
| 1631 | if ethDst: |
| 1632 | cmd += " --ethDst " + str( ethDst ) |
| 1633 | if bandwidth: |
| 1634 | cmd += " --bandwidth " + str( bandwidth ) |
| 1635 | if lambdaAlloc: |
| 1636 | cmd += " --lambda " |
| 1637 | if ipProto: |
| 1638 | cmd += " --ipProto " + str( ipProto ) |
| 1639 | if ipSrc: |
| 1640 | cmd += " --ipSrc " + str( ipSrc ) |
| 1641 | if ipDst: |
| 1642 | cmd += " --ipDst " + str( ipDst ) |
| 1643 | if tcpSrc: |
| 1644 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1645 | if tcpDst: |
| 1646 | cmd += " --tcpDst " + str( tcpDst ) |
| 1647 | if setEthSrc: |
| 1648 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1649 | if setEthDst: |
| 1650 | cmd += " --setEthDst " + str( setEthDst ) |
| 1651 | if vlanId: |
| 1652 | cmd += " -v " + str( vlanId ) |
| 1653 | if setVlan: |
| 1654 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1655 | if partial: |
| 1656 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1657 | if encap: |
| 1658 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1659 | |
| 1660 | # Check whether the user appended the port |
| 1661 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1662 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1663 | if "/" in ingressDevice: |
| 1664 | cmd += " " + str( ingressDevice ) |
| 1665 | else: |
| 1666 | if not portIngress: |
| 1667 | main.log.error( "You must specify " + |
| 1668 | "the Ingress port" ) |
| 1669 | return main.FALSE |
| 1670 | |
| 1671 | cmd += " " +\ |
| 1672 | str( ingressDevice ) + "/" +\ |
| 1673 | str( portIngress ) |
| 1674 | |
| 1675 | if portEgressList is None: |
| 1676 | for egressDevice in egressDeviceList: |
| 1677 | if "/" in egressDevice: |
| 1678 | cmd += " " + str( egressDevice ) |
| 1679 | else: |
| 1680 | main.log.error( "You must specify " + |
| 1681 | "the egress port" ) |
| 1682 | # TODO: perhaps more meaningful return |
| 1683 | return main.FALSE |
| 1684 | else: |
| 1685 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1686 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1687 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1688 | cmd += " " + \ |
| 1689 | str( egressDevice ) + "/" +\ |
| 1690 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1691 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1692 | main.log.error( "Device list and port list does not " + |
| 1693 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1694 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1695 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1696 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1697 | # If error, return error message |
| 1698 | if re.search( "Error", handle ): |
| 1699 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1700 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1701 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1702 | else: |
| 1703 | match = re.search('id=0x([\da-f]+),', handle) |
| 1704 | if match: |
| 1705 | return match.group()[3:-1] |
| 1706 | else: |
| 1707 | main.log.error( "Error, intent ID not found" ) |
| 1708 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1709 | except AssertionError: |
| 1710 | main.log.exception( "" ) |
| 1711 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1712 | except TypeError: |
| 1713 | main.log.exception( self.name + ": Object not as expected" ) |
| 1714 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1715 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1716 | main.log.error( self.name + ": EOF exception found" ) |
| 1717 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1718 | main.cleanup() |
| 1719 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1720 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1721 | main.log.exception( self.name + ": Uncaught exception!" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1722 | main.cleanup() |
| 1723 | main.exit() |
| 1724 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1725 | def addMplsIntent( |
| 1726 | self, |
| 1727 | ingressDevice, |
| 1728 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1729 | ingressPort="", |
| 1730 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1731 | ethType="", |
| 1732 | ethSrc="", |
| 1733 | ethDst="", |
| 1734 | bandwidth="", |
| 1735 | lambdaAlloc=False, |
| 1736 | ipProto="", |
| 1737 | ipSrc="", |
| 1738 | ipDst="", |
| 1739 | tcpSrc="", |
| 1740 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1741 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1742 | egressLabel="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1743 | priority=""): |
| 1744 | """ |
| 1745 | Required: |
| 1746 | * ingressDevice: device id of ingress device |
| 1747 | * egressDevice: device id of egress device |
| 1748 | Optional: |
| 1749 | * ethType: specify ethType |
| 1750 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1751 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1752 | * bandwidth: specify bandwidth capacity of link |
| 1753 | * lambdaAlloc: if True, intent will allocate lambda |
| 1754 | for the specified intent |
| 1755 | * ipProto: specify ip protocol |
| 1756 | * ipSrc: specify ip source address |
| 1757 | * ipDst: specify ip destination address |
| 1758 | * tcpSrc: specify tcp source port |
| 1759 | * tcpDst: specify tcp destination port |
| 1760 | * ingressLabel: Ingress MPLS label |
| 1761 | * egressLabel: Egress MPLS label |
| 1762 | Description: |
| 1763 | Adds MPLS intent by |
| 1764 | specifying device id's and optional fields |
| 1765 | Returns: |
| 1766 | A string of the intent id or None on error |
| 1767 | |
| 1768 | NOTE: This function may change depending on the |
| 1769 | options developers provide for MPLS |
| 1770 | intent via cli |
| 1771 | """ |
| 1772 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1773 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1774 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1775 | if ethType: |
| 1776 | cmd += " --ethType " + str( ethType ) |
| 1777 | if ethSrc: |
| 1778 | cmd += " --ethSrc " + str( ethSrc ) |
| 1779 | if ethDst: |
| 1780 | cmd += " --ethDst " + str( ethDst ) |
| 1781 | if bandwidth: |
| 1782 | cmd += " --bandwidth " + str( bandwidth ) |
| 1783 | if lambdaAlloc: |
| 1784 | cmd += " --lambda " |
| 1785 | if ipProto: |
| 1786 | cmd += " --ipProto " + str( ipProto ) |
| 1787 | if ipSrc: |
| 1788 | cmd += " --ipSrc " + str( ipSrc ) |
| 1789 | if ipDst: |
| 1790 | cmd += " --ipDst " + str( ipDst ) |
| 1791 | if tcpSrc: |
| 1792 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1793 | if tcpDst: |
| 1794 | cmd += " --tcpDst " + str( tcpDst ) |
| 1795 | if ingressLabel: |
| 1796 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1797 | if egressLabel: |
| 1798 | cmd += " --egressLabel " + str( egressLabel ) |
| 1799 | if priority: |
| 1800 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1801 | |
| 1802 | # Check whether the user appended the port |
| 1803 | # or provided it as an input |
| 1804 | if "/" in ingressDevice: |
| 1805 | cmd += " " + str( ingressDevice ) |
| 1806 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1807 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1808 | main.log.error( "You must specify the ingress port" ) |
| 1809 | return None |
| 1810 | |
| 1811 | cmd += " " + \ |
| 1812 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1813 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1814 | |
| 1815 | if "/" in egressDevice: |
| 1816 | cmd += " " + str( egressDevice ) |
| 1817 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1818 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1819 | main.log.error( "You must specify the egress port" ) |
| 1820 | return None |
| 1821 | |
| 1822 | cmd += " " +\ |
| 1823 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1824 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1825 | |
| 1826 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1827 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1828 | # If error, return error message |
| 1829 | if re.search( "Error", handle ): |
| 1830 | main.log.error( "Error in adding mpls intent" ) |
| 1831 | return None |
| 1832 | else: |
| 1833 | # TODO: print out all the options in this message? |
| 1834 | main.log.info( "MPLS intent installed between " + |
| 1835 | str( ingressDevice ) + " and " + |
| 1836 | str( egressDevice ) ) |
| 1837 | match = re.search('id=0x([\da-f]+),', handle) |
| 1838 | if match: |
| 1839 | return match.group()[3:-1] |
| 1840 | else: |
| 1841 | main.log.error( "Error, intent ID not found" ) |
| 1842 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1843 | except AssertionError: |
| 1844 | main.log.exception( "" ) |
| 1845 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1846 | except TypeError: |
| 1847 | main.log.exception( self.name + ": Object not as expected" ) |
| 1848 | return None |
| 1849 | except pexpect.EOF: |
| 1850 | main.log.error( self.name + ": EOF exception found" ) |
| 1851 | main.log.error( self.name + ": " + self.handle.before ) |
| 1852 | main.cleanup() |
| 1853 | main.exit() |
| 1854 | except Exception: |
| 1855 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1856 | main.cleanup() |
| 1857 | main.exit() |
| 1858 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1859 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1860 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1861 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1862 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1863 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1864 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1865 | -p or --purge: Purge the intent from the store after removal |
| 1866 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1867 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 1868 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1869 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1870 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1871 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1872 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1873 | if purge: |
| 1874 | cmdStr += " -p" |
| 1875 | if sync: |
| 1876 | cmdStr += " -s" |
| 1877 | |
| 1878 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1879 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1880 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1881 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1882 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1883 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1884 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1885 | # TODO: Should this be main.TRUE |
| 1886 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1887 | except AssertionError: |
| 1888 | main.log.exception( "" ) |
| 1889 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1890 | except TypeError: |
| 1891 | main.log.exception( self.name + ": Object not as expected" ) |
| 1892 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1893 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1894 | main.log.error( self.name + ": EOF exception found" ) |
| 1895 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1896 | main.cleanup() |
| 1897 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1898 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1899 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1900 | main.cleanup() |
| 1901 | main.exit() |
| 1902 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1903 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1904 | """ |
| 1905 | Description: |
| 1906 | Remove all the intents |
| 1907 | Optional args:- |
| 1908 | -s or --sync: Waits for the removal before returning |
| 1909 | -p or --purge: Purge the intent from the store after removal |
| 1910 | Returns: |
| 1911 | Returns main.TRUE if all intents are removed, otherwise returns |
| 1912 | main.FALSE; Returns None for exception |
| 1913 | """ |
| 1914 | try: |
| 1915 | cmdStr = "remove-intent" |
| 1916 | if purge: |
| 1917 | cmdStr += " -p" |
| 1918 | if sync: |
| 1919 | cmdStr += " -s" |
| 1920 | |
| 1921 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1922 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1923 | assert "Command not found:" not in handle, handle |
| 1924 | if re.search( "Error", handle ): |
| 1925 | main.log.error( "Error in removing intent" ) |
| 1926 | return main.FALSE |
| 1927 | else: |
| 1928 | return main.TRUE |
| 1929 | except AssertionError: |
| 1930 | main.log.exception( "" ) |
| 1931 | return None |
| 1932 | except TypeError: |
| 1933 | main.log.exception( self.name + ": Object not as expected" ) |
| 1934 | return None |
| 1935 | except pexpect.EOF: |
| 1936 | main.log.error( self.name + ": EOF exception found" ) |
| 1937 | main.log.error( self.name + ": " + self.handle.before ) |
| 1938 | main.cleanup() |
| 1939 | main.exit() |
| 1940 | except Exception: |
| 1941 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1942 | main.cleanup() |
| 1943 | main.exit() |
| 1944 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 1945 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1946 | """ |
| 1947 | Purges all WITHDRAWN Intents |
| 1948 | """ |
| 1949 | try: |
| 1950 | cmdStr = "purge-intents" |
| 1951 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1952 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1953 | if re.search( "Error", handle ): |
| 1954 | main.log.error( "Error in purging intents" ) |
| 1955 | return main.FALSE |
| 1956 | else: |
| 1957 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1958 | except AssertionError: |
| 1959 | main.log.exception( "" ) |
| 1960 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1961 | except TypeError: |
| 1962 | main.log.exception( self.name + ": Object not as expected" ) |
| 1963 | return None |
| 1964 | except pexpect.EOF: |
| 1965 | main.log.error( self.name + ": EOF exception found" ) |
| 1966 | main.log.error( self.name + ": " + self.handle.before ) |
| 1967 | main.cleanup() |
| 1968 | main.exit() |
| 1969 | except Exception: |
| 1970 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1971 | main.cleanup() |
| 1972 | main.exit() |
| 1973 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1974 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1975 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1976 | NOTE: This method should be used after installing application: |
| 1977 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1978 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1979 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1980 | Description: |
| 1981 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1982 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1983 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1984 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1985 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1986 | cmdStr += " -j" |
| 1987 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1988 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1989 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1990 | except AssertionError: |
| 1991 | main.log.exception( "" ) |
| 1992 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1993 | except TypeError: |
| 1994 | main.log.exception( self.name + ": Object not as expected" ) |
| 1995 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1996 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1997 | main.log.error( self.name + ": EOF exception found" ) |
| 1998 | main.log.error( self.name + ": " + self.handle.before ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1999 | main.cleanup() |
| 2000 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2001 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2002 | main.log.exception( self.name + ": Uncaught exception!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2003 | main.cleanup() |
| 2004 | main.exit() |
| 2005 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2006 | def ipv4RouteNumber( self ): |
| 2007 | """ |
| 2008 | NOTE: This method should be used after installing application: |
| 2009 | onos-app-sdnip |
| 2010 | Description: |
| 2011 | Obtain the total IPv4 routes number in the system |
| 2012 | """ |
| 2013 | try: |
| 2014 | cmdStr = "routes -s -j" |
| 2015 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2016 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2017 | jsonResult = json.loads( handle ) |
| 2018 | return jsonResult['totalRoutes4'] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2019 | except AssertionError: |
| 2020 | main.log.exception( "" ) |
| 2021 | return None |
| 2022 | except ( TypeError, ValueError ): |
| 2023 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2024 | return None |
| 2025 | except pexpect.EOF: |
| 2026 | main.log.error( self.name + ": EOF exception found" ) |
| 2027 | main.log.error( self.name + ": " + self.handle.before ) |
| 2028 | main.cleanup() |
| 2029 | main.exit() |
| 2030 | except Exception: |
| 2031 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2032 | main.cleanup() |
| 2033 | main.exit() |
| 2034 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2035 | def intents( self, jsonFormat = True, summary = False, **intentargs): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2036 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2037 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2038 | Obtain intents from the ONOS cli. |
| 2039 | Optional: |
| 2040 | * jsonFormat: Enable output formatting in json, default to True |
| 2041 | * summary: Whether only output the intent summary, defaults to False |
| 2042 | * type: Only output a certain type of intent. This options is valid |
| 2043 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2044 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2045 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2046 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2047 | if summary: |
| 2048 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2049 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2050 | cmdStr += " -j" |
| 2051 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2052 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2053 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2054 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2055 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2056 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2057 | intentType = "" |
| 2058 | # IF we want the summary of a specific intent type |
| 2059 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2060 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2061 | if intentType in jsonResult.keys(): |
| 2062 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2063 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2064 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2065 | return handle |
| 2066 | else: |
| 2067 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2068 | except AssertionError: |
| 2069 | main.log.exception( "" ) |
| 2070 | return None |
| 2071 | except ( TypeError, ValueError ): |
| 2072 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2073 | return None |
| 2074 | except pexpect.EOF: |
| 2075 | main.log.error( self.name + ": EOF exception found" ) |
| 2076 | main.log.error( self.name + ": " + self.handle.before ) |
| 2077 | main.cleanup() |
| 2078 | main.exit() |
| 2079 | except Exception: |
| 2080 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2081 | main.cleanup() |
| 2082 | main.exit() |
| 2083 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2084 | def getIntentState(self, intentsId, intentsJson=None): |
| 2085 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2086 | Description: |
| 2087 | Gets intent state. Accepts a single intent ID (string type) or a |
| 2088 | list of intent IDs. |
| 2089 | Parameters: |
| 2090 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2091 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2092 | Returns: |
| 2093 | Returns the state (string type) of the ID if a single intent ID is |
| 2094 | accepted. |
| 2095 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2096 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2097 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2098 | """ |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2099 | try: |
| 2100 | state = "State is Undefined" |
| 2101 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2102 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2103 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2104 | rawJson = intentsJson |
| 2105 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2106 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2107 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2108 | if intentsId == intent[ 'id' ]: |
| 2109 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2110 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2111 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
| 2112 | " on the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2113 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2114 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2115 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2116 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2117 | stateDict = {} |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2118 | for intents in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2119 | if intentsId[ i ] == intents[ 'id' ]: |
| 2120 | stateDict[ 'state' ] = intents[ 'state' ] |
| 2121 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2122 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2123 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2124 | if len( intentsId ) != len( dictList ): |
| 2125 | main.log.info( "Cannot find some of the intent ID state" ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2126 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2127 | else: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2128 | main.log.info( "Invalid intents ID entry" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2129 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2130 | except ( TypeError, ValueError ): |
| 2131 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2132 | return None |
| 2133 | except pexpect.EOF: |
| 2134 | main.log.error( self.name + ": EOF exception found" ) |
| 2135 | main.log.error( self.name + ": " + self.handle.before ) |
| 2136 | main.cleanup() |
| 2137 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2138 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2139 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2140 | main.cleanup() |
| 2141 | main.exit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2142 | |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2143 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2144 | """ |
| 2145 | Description: |
| 2146 | Check intents state |
| 2147 | Required: |
| 2148 | intentsId - List of intents ID to be checked |
| 2149 | Optional: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2150 | expectedState - Check the expected state(s) of each intents |
| 2151 | state in the list. |
| 2152 | *NOTE: You can pass in a list of expected state, |
| 2153 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2154 | Return: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2155 | Returns main.TRUE only if all intent are the same as expected states |
| 2156 | , otherwise, returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2157 | """ |
| 2158 | try: |
| 2159 | # Generating a dictionary: intent id as a key and state as value |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2160 | returnValue = main.TRUE |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2161 | intentsDict = self.getIntentState( intentsId ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2162 | if len( intentsId ) != len( intentsDict ): |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2163 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2164 | "getting intents state" ) |
| 2165 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2166 | |
| 2167 | if isinstance( expectedState, types.StringType ): |
| 2168 | for intents in intentsDict: |
| 2169 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2170 | main.log.debug( self.name + " : Intent ID - " + |
| 2171 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2172 | " actual state = " + |
| 2173 | intents.get( 'state' ) |
| 2174 | + " does not equal expected state = " |
| 2175 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2176 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2177 | |
| 2178 | elif isinstance( expectedState, types.ListType ): |
| 2179 | for intents in intentsDict: |
| 2180 | if not any( state == intents.get( 'state' ) for state in |
| 2181 | expectedState ): |
| 2182 | main.log.debug( self.name + " : Intent ID - " + |
| 2183 | intents.get( 'id' ) + |
| 2184 | " actual state = " + |
| 2185 | intents.get( 'state' ) + |
| 2186 | " does not equal expected states = " |
| 2187 | + str( expectedState ) ) |
| 2188 | returnValue = main.FALSE |
| 2189 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2190 | if returnValue == main.TRUE: |
| 2191 | main.log.info( self.name + ": All " + |
| 2192 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2193 | " intents are in " + str( expectedState ) + |
| 2194 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2195 | return returnValue |
| 2196 | except TypeError: |
| 2197 | main.log.exception( self.name + ": Object not as expected" ) |
| 2198 | return None |
| 2199 | except pexpect.EOF: |
| 2200 | main.log.error( self.name + ": EOF exception found" ) |
| 2201 | main.log.error( self.name + ": " + self.handle.before ) |
| 2202 | main.cleanup() |
| 2203 | main.exit() |
| 2204 | except Exception: |
| 2205 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2206 | main.cleanup() |
| 2207 | main.exit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2208 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2209 | def compareIntent( self, intentDict ): |
| 2210 | """ |
| 2211 | Description: |
| 2212 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2213 | Return: |
| 2214 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2215 | Arguments: |
| 2216 | intentDict: a dictionary which maps intent ids to intent states |
| 2217 | """ |
| 2218 | try: |
| 2219 | intentsRaw = self.intents() |
| 2220 | intentsJson = json.loads( intentsRaw ) |
| 2221 | intentDictONOS = {} |
| 2222 | for intent in intentsJson: |
| 2223 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2224 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2225 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2226 | main.log.warn( self.name + ": expected intent count does not match that in ONOS, " + |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2227 | str( len( intentDict ) ) + " expected and " + |
| 2228 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2229 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2230 | for intentID in intentDict.keys(): |
| 2231 | if not intentID in intentDictONOS.keys(): |
| 2232 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2233 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2234 | else: |
| 2235 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2236 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2237 | " expected state is " + intentDict[ intentID ] + |
| 2238 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2239 | returnValue = main.FALSE |
| 2240 | intentDictONOS.pop( intentID ) |
| 2241 | if len( intentDictONOS ) > 0: |
| 2242 | returnValue = main.FALSE |
| 2243 | for intentID in intentDictONOS.keys(): |
| 2244 | main.log.debug( self.name + ": find extra intent in ONOS: intent ID " + intentID ) |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2245 | if returnValue == main.TRUE: |
| 2246 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2247 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2248 | except KeyError: |
| 2249 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2250 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2251 | except ( TypeError, ValueError ): |
| 2252 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2253 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2254 | except pexpect.EOF: |
| 2255 | main.log.error( self.name + ": EOF exception found" ) |
| 2256 | main.log.error( self.name + ": " + self.handle.before ) |
| 2257 | main.cleanup() |
| 2258 | main.exit() |
| 2259 | except Exception: |
| 2260 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2261 | main.cleanup() |
| 2262 | main.exit() |
| 2263 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2264 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2265 | """ |
| 2266 | Description: |
| 2267 | Check the number of installed intents. |
| 2268 | Optional: |
| 2269 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2270 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2271 | Return: |
| 2272 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2273 | , otherwise, returns main.FALSE. |
| 2274 | """ |
| 2275 | |
| 2276 | try: |
| 2277 | cmd = "intents -s -j" |
| 2278 | |
| 2279 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2280 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2281 | if response == None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2282 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2283 | response = json.loads( response ) |
| 2284 | |
| 2285 | # get total and installed number, see if they are match |
| 2286 | allState = response.get( 'all' ) |
| 2287 | if allState.get('total') == allState.get('installed'): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2288 | 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] | 2289 | return main.TRUE |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2290 | 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] | 2291 | return main.FALSE |
| 2292 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2293 | except ( TypeError, ValueError ): |
| 2294 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2295 | return None |
| 2296 | except pexpect.EOF: |
| 2297 | main.log.error( self.name + ": EOF exception found" ) |
| 2298 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2299 | if noExit: |
| 2300 | return main.FALSE |
| 2301 | else: |
| 2302 | main.cleanup() |
| 2303 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2304 | except Exception: |
| 2305 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2306 | if noExit: |
| 2307 | return main.FALSE |
| 2308 | else: |
| 2309 | main.cleanup() |
| 2310 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2311 | except pexpect.TIMEOUT: |
| 2312 | main.log.error( self.name + ": ONOS timeout" ) |
| 2313 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2314 | |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2315 | def flows( self, state="", jsonFormat=True, timeout=60, noExit=False, noCore=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2316 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2317 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2318 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2319 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2320 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2321 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2322 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2323 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2324 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2325 | if jsonFormat: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2326 | cmdStr += " -j " |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2327 | if noCore: |
| 2328 | cmdStr += " -n " |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2329 | cmdStr += state |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2330 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2331 | assert "Command not found:" not in handle, handle |
| 2332 | if re.search( "Error:", handle ): |
| 2333 | main.log.error( self.name + ": flows() response: " + |
| 2334 | str( handle ) ) |
| 2335 | return handle |
| 2336 | except AssertionError: |
| 2337 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2338 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2339 | except TypeError: |
| 2340 | main.log.exception( self.name + ": Object not as expected" ) |
| 2341 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2342 | except pexpect.TIMEOUT: |
| 2343 | main.log.error( self.name + ": ONOS timeout" ) |
| 2344 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2345 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2346 | main.log.error( self.name + ": EOF exception found" ) |
| 2347 | main.log.error( self.name + ": " + self.handle.before ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2348 | main.cleanup() |
| 2349 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2350 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2351 | main.log.exception( self.name + ": Uncaught exception!" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2352 | main.cleanup() |
| 2353 | main.exit() |
| 2354 | |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2355 | def checkFlowCount(self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2356 | count = self.getTotalFlowsNum( timeout=timeout ) |
| 2357 | count = int (count) if count else 0 |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2358 | return count if (count > min) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2359 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2360 | def checkFlowsState( self, isPENDING=True, timeout=60,noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2361 | """ |
| 2362 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2363 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2364 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2365 | if the count of those states is 0, which means all current flows |
| 2366 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2367 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2368 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2369 | Return: |
| 2370 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2371 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2372 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2373 | """ |
| 2374 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2375 | states = ["PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED"] |
| 2376 | checkedStates = [] |
| 2377 | statesCount = [0, 0, 0, 0] |
| 2378 | for s in states: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2379 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2380 | if rawFlows: |
| 2381 | # if we didn't get flows or flows function return None, we should return |
| 2382 | # main.Flase |
| 2383 | checkedStates.append( json.loads( rawFlows ) ) |
| 2384 | else: |
| 2385 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2386 | for i in range( len( states ) ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2387 | for c in checkedStates[i]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2388 | try: |
| 2389 | statesCount[i] += int( c.get( "flowCount" ) ) |
| 2390 | except TypeError: |
| 2391 | main.log.exception( "Json object not as expected" ) |
| 2392 | main.log.info( states[i] + " flows: " + str( statesCount[i] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2393 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2394 | # We want to count PENDING_ADD if isPENDING is true |
| 2395 | if isPENDING: |
| 2396 | if statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2397 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2398 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2399 | if statesCount[0] + statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2400 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2401 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2402 | except ( TypeError, ValueError ): |
| 2403 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2404 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2405 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2406 | except AssertionError: |
| 2407 | main.log.exception( "" ) |
| 2408 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2409 | except pexpect.EOF: |
| 2410 | main.log.error( self.name + ": EOF exception found" ) |
| 2411 | main.log.error( self.name + ": " + self.handle.before ) |
| 2412 | main.cleanup() |
| 2413 | main.exit() |
| 2414 | except Exception: |
| 2415 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2416 | main.cleanup() |
| 2417 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2418 | except pexpect.TIMEOUT: |
| 2419 | main.log.error( self.name + ": ONOS timeout" ) |
| 2420 | return None |
| 2421 | |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2422 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2423 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2424 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2425 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2426 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2427 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2428 | a specific point-to-point intent definition |
| 2429 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2430 | * ingress: specify source dpid |
| 2431 | * egress: specify destination dpid |
| 2432 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2433 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2434 | * offset: the keyOffset is where the next batch of intents |
| 2435 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2436 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2437 | * getResponse: If set to True, function will return ONOS response. |
| 2438 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2439 | Returns: If failed to push test intents, it will returen None, |
| 2440 | if successful, return true. |
| 2441 | Timeout expection will return None, |
| 2442 | TypeError will return false |
| 2443 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2444 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2445 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2446 | if background: |
| 2447 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2448 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2449 | back = "" |
| 2450 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2451 | ingress, |
| 2452 | egress, |
| 2453 | batchSize, |
| 2454 | offset, |
| 2455 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2456 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2457 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2458 | main.log.info( response ) |
| 2459 | if response == None: |
| 2460 | return None |
| 2461 | |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2462 | if getResponse: |
| 2463 | return response |
| 2464 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2465 | # TODO: We should handle if there is failure in installation |
| 2466 | return main.TRUE |
| 2467 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2468 | except AssertionError: |
| 2469 | main.log.exception( "" ) |
| 2470 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2471 | except pexpect.TIMEOUT: |
| 2472 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2473 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2474 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2475 | main.log.error( self.name + ": EOF exception found" ) |
| 2476 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2477 | main.cleanup() |
| 2478 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2479 | except TypeError: |
| 2480 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2481 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2482 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2483 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2484 | main.cleanup() |
| 2485 | main.exit() |
| 2486 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2487 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2488 | """ |
| 2489 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2490 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2491 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2492 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2493 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2494 | """ |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2495 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2496 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2497 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2498 | cmd = "flows -c added" |
| 2499 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2500 | if rawFlows: |
| 2501 | rawFlows = rawFlows.split("\n") |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2502 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2503 | for l in rawFlows: |
| 2504 | totalFlows += int(l.split("Count=")[1]) |
| 2505 | else: |
| 2506 | main.log.error("Response not as expected!") |
| 2507 | return None |
| 2508 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2509 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2510 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2511 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2512 | return None |
| 2513 | except pexpect.EOF: |
| 2514 | main.log.error( self.name + ": EOF exception found" ) |
| 2515 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2516 | if not noExit: |
| 2517 | main.cleanup() |
| 2518 | main.exit() |
| 2519 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2520 | except Exception: |
| 2521 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2522 | if not noExit: |
| 2523 | main.cleanup() |
| 2524 | main.exit() |
| 2525 | return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2526 | except pexpect.TIMEOUT: |
| 2527 | main.log.error( self.name + ": ONOS timeout" ) |
| 2528 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2529 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2530 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2531 | """ |
| 2532 | Description: |
| 2533 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2534 | Optional: |
| 2535 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2536 | Return: |
| 2537 | The number of intents |
| 2538 | """ |
| 2539 | try: |
| 2540 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2541 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2542 | if response == None: |
| 2543 | return -1 |
| 2544 | response = json.loads( response ) |
| 2545 | return int( response.get("intents") ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2546 | except ( TypeError, ValueError ): |
| 2547 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2548 | return None |
| 2549 | except pexpect.EOF: |
| 2550 | main.log.error( self.name + ": EOF exception found" ) |
| 2551 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2552 | if noExit: |
| 2553 | return -1 |
| 2554 | else: |
| 2555 | main.cleanup() |
| 2556 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2557 | except Exception: |
| 2558 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2559 | if noExit: |
| 2560 | return -1 |
| 2561 | else: |
| 2562 | main.cleanup() |
| 2563 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2564 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2565 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2566 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2567 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2568 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2569 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2570 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2571 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2572 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2573 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2574 | cmdStr += " -j" |
| 2575 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2576 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2577 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2578 | except AssertionError: |
| 2579 | main.log.exception( "" ) |
| 2580 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2581 | except TypeError: |
| 2582 | main.log.exception( self.name + ": Object not as expected" ) |
| 2583 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2584 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2585 | main.log.error( self.name + ": EOF exception found" ) |
| 2586 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2587 | main.cleanup() |
| 2588 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2589 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2590 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2591 | main.cleanup() |
| 2592 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2593 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2594 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2595 | """ |
| 2596 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2597 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2598 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2599 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2600 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2601 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2602 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2603 | cmdStr += " -j" |
| 2604 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2605 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2606 | if handle: |
| 2607 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2608 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2609 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2610 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2611 | else: |
| 2612 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2613 | except AssertionError: |
| 2614 | main.log.exception( "" ) |
| 2615 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2616 | except TypeError: |
| 2617 | main.log.exception( self.name + ": Object not as expected" ) |
| 2618 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2619 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2620 | main.log.error( self.name + ": EOF exception found" ) |
| 2621 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2622 | main.cleanup() |
| 2623 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2624 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2625 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2626 | main.cleanup() |
| 2627 | main.exit() |
| 2628 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2629 | # Wrapper functions **************** |
| 2630 | # Wrapper functions use existing driver |
| 2631 | # functions and extends their use case. |
| 2632 | # For example, we may use the output of |
| 2633 | # a normal driver function, and parse it |
| 2634 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2635 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2636 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2637 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2638 | Description: |
| 2639 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2640 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2641 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2642 | # Obtain output of intents function |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2643 | intentsStr = self.intents(jsonFormat=False) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2644 | intentIdList = [] |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2645 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2646 | # Parse the intents output for ID's |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2647 | intentsList = [ s.strip() for s in intentsStr.splitlines() ] |
| 2648 | for intents in intentsList: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 2649 | match = re.search('id=0x([\da-f]+),', intents) |
| 2650 | if match: |
| 2651 | tmpId = match.group()[3:-1] |
| 2652 | intentIdList.append( tmpId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2653 | return intentIdList |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2654 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2655 | except TypeError: |
| 2656 | main.log.exception( self.name + ": Object not as expected" ) |
| 2657 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2658 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2659 | main.log.error( self.name + ": EOF exception found" ) |
| 2660 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2661 | main.cleanup() |
| 2662 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2663 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2664 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2665 | main.cleanup() |
| 2666 | main.exit() |
| 2667 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2668 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2669 | """ |
| 2670 | Determine the number of flow rules for the given device id that are |
| 2671 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2672 | Params: |
| 2673 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2674 | """ |
| 2675 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2676 | if core: |
| 2677 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2678 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2679 | else: |
| 2680 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2681 | "grep 'state=ADDED' | wc -l" |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2682 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2683 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2684 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2685 | except AssertionError: |
| 2686 | main.log.exception( "" ) |
| 2687 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2688 | except pexpect.EOF: |
| 2689 | main.log.error( self.name + ": EOF exception found" ) |
| 2690 | main.log.error( self.name + ": " + self.handle.before ) |
| 2691 | main.cleanup() |
| 2692 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2693 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2694 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2695 | main.cleanup() |
| 2696 | main.exit() |
| 2697 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2698 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2699 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2700 | Use 'devices' function to obtain list of all devices |
| 2701 | and parse the result to obtain a list of all device |
| 2702 | id's. Returns this list. Returns empty list if no |
| 2703 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2704 | List is ordered sequentially |
| 2705 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2706 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2707 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2708 | the ids. By obtaining the list of device ids on the fly, |
| 2709 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2710 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2711 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2712 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2713 | devicesStr = self.devices( jsonFormat=False ) |
| 2714 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2715 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2716 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2717 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2718 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2719 | |
| 2720 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2721 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2722 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2723 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2724 | # Split list further into arguments before and after string |
| 2725 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2726 | # append to idList |
| 2727 | for arg in tempList: |
| 2728 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 2729 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2730 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2731 | except TypeError: |
| 2732 | main.log.exception( self.name + ": Object not as expected" ) |
| 2733 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2734 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2735 | main.log.error( self.name + ": EOF exception found" ) |
| 2736 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2737 | main.cleanup() |
| 2738 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2739 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2740 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2741 | main.cleanup() |
| 2742 | main.exit() |
| 2743 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2744 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2745 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2746 | Uses 'nodes' function to obtain list of all nodes |
| 2747 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2748 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2749 | Returns: |
| 2750 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2751 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2752 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2753 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2754 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2755 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 2756 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2757 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2758 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2759 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2760 | nodesJson = json.loads( nodesStr ) |
| 2761 | idList = [ node.get('id') for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2762 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2763 | except ( TypeError, ValueError ): |
| 2764 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2765 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2766 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2767 | main.log.error( self.name + ": EOF exception found" ) |
| 2768 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2769 | main.cleanup() |
| 2770 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2771 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2772 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2773 | main.cleanup() |
| 2774 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2775 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2776 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2777 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2778 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 2779 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2780 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2781 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2782 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2783 | return None |
| 2784 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2785 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2786 | rawDevices = self.devices() |
| 2787 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2788 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2789 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2790 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 2791 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2792 | return device |
| 2793 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2794 | except ( TypeError, ValueError ): |
| 2795 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2796 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2797 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2798 | main.log.error( self.name + ": EOF exception found" ) |
| 2799 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2800 | main.cleanup() |
| 2801 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2802 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2803 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2804 | main.cleanup() |
| 2805 | main.exit() |
| 2806 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2807 | def getTopology( self, topologyOutput ): |
| 2808 | """ |
| 2809 | Definition: |
| 2810 | Loads a json topology output |
| 2811 | Return: |
| 2812 | topology = current ONOS topology |
| 2813 | """ |
| 2814 | import json |
| 2815 | try: |
| 2816 | # either onos:topology or 'topology' will work in CLI |
| 2817 | topology = json.loads(topologyOutput) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2818 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2819 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2820 | except ( TypeError, ValueError ): |
| 2821 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 2822 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2823 | except pexpect.EOF: |
| 2824 | main.log.error( self.name + ": EOF exception found" ) |
| 2825 | main.log.error( self.name + ": " + self.handle.before ) |
| 2826 | main.cleanup() |
| 2827 | main.exit() |
| 2828 | except Exception: |
| 2829 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2830 | main.cleanup() |
| 2831 | main.exit() |
| 2832 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2833 | def checkStatus(self, numoswitch, numolink, numoctrl = -1, logLevel="info"): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2834 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2835 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2836 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2837 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2838 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2839 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2840 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2841 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2842 | logLevel = level to log to. |
| 2843 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2844 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2845 | Returns: main.TRUE if the number of switches and links are correct, |
| 2846 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2847 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2848 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2849 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2850 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 2851 | summary = self.summary() |
| 2852 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 2853 | except ( TypeError, ValueError ): |
| 2854 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 2855 | return main.ERROR |
| 2856 | try: |
| 2857 | topology = self.getTopology( self.topology() ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2858 | if topology == {} or topology == None or summary == {} or summary == None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2859 | return main.ERROR |
| 2860 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2861 | # Is the number of switches is what we expected |
| 2862 | devices = topology.get( 'devices', False ) |
| 2863 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2864 | nodes = summary.get( 'nodes', False ) |
| 2865 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2866 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2867 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2868 | # Is the number of links is what we expected |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2869 | linkCheck = ( int( links ) == int( numolink ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2870 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 2871 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2872 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2873 | output = output + "The number of links and switches match "\ |
| 2874 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2875 | result = main.TRUE |
| 2876 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2877 | output = output + \ |
| 2878 | "The number of links and switches does not match " + \ |
| 2879 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2880 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2881 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 2882 | output = output + " (%i expected) " % int( numoswitch ) |
| 2883 | output = output + "and %i links " % int( links ) |
| 2884 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 2885 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2886 | output = output + "and %i controllers " % int( nodes ) |
| 2887 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2888 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2889 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2890 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2891 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2892 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2893 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2894 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2895 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2896 | main.log.error( self.name + ": EOF exception found" ) |
| 2897 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2898 | main.cleanup() |
| 2899 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2900 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2901 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2902 | main.cleanup() |
| 2903 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2904 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2905 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2906 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2907 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2908 | deviceId must be the id of a device as seen in the onos devices command |
| 2909 | 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] | 2910 | role must be either master, standby, or none |
| 2911 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2912 | Returns: |
| 2913 | main.TRUE or main.FALSE based on argument verification and |
| 2914 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2915 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2916 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2917 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2918 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2919 | cmdStr = "device-role " +\ |
| 2920 | str( deviceId ) + " " +\ |
| 2921 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2922 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2923 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2924 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2925 | if re.search( "Error", handle ): |
| 2926 | # end color output to escape any colours |
| 2927 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2928 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2929 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2930 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2931 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2932 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2933 | main.log.error( "Invalid 'role' given to device_role(). " + |
| 2934 | "Value was '" + str(role) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2935 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2936 | except AssertionError: |
| 2937 | main.log.exception( "" ) |
| 2938 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2939 | except TypeError: |
| 2940 | main.log.exception( self.name + ": Object not as expected" ) |
| 2941 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2942 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2943 | main.log.error( self.name + ": EOF exception found" ) |
| 2944 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2945 | main.cleanup() |
| 2946 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2947 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2948 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2949 | main.cleanup() |
| 2950 | main.exit() |
| 2951 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2952 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2953 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2954 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2955 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2956 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2957 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2958 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2959 | cmdStr = "clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2960 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2961 | cmdStr += " -j" |
| 2962 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2963 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2964 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2965 | except AssertionError: |
| 2966 | main.log.exception( "" ) |
| 2967 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2968 | except TypeError: |
| 2969 | main.log.exception( self.name + ": Object not as expected" ) |
| 2970 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2971 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2972 | main.log.error( self.name + ": EOF exception found" ) |
| 2973 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2974 | main.cleanup() |
| 2975 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2976 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2977 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2978 | main.cleanup() |
| 2979 | main.exit() |
| 2980 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2981 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2982 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2983 | CLI command to get the current leader for the Election test application |
| 2984 | NOTE: Requires installation of the onos-app-election feature |
| 2985 | Returns: Node IP of the leader if one exists |
| 2986 | None if none exists |
| 2987 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2988 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2989 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2990 | cmdStr = "election-test-leader" |
| 2991 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2992 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2993 | # Leader |
| 2994 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2995 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2996 | nodeSearch = re.search( leaderPattern, response ) |
| 2997 | if nodeSearch: |
| 2998 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2999 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3000 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3001 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3002 | # no leader |
| 3003 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3004 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3005 | nullSearch = re.search( nullPattern, response ) |
| 3006 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3007 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3008 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3009 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3010 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3011 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 3012 | ": " + "unexpected response" ) |
| 3013 | main.log.error( repr( response ) ) |
| 3014 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3015 | except AssertionError: |
| 3016 | main.log.exception( "" ) |
| 3017 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3018 | except TypeError: |
| 3019 | main.log.exception( self.name + ": Object not as expected" ) |
| 3020 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3021 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3022 | main.log.error( self.name + ": EOF exception found" ) |
| 3023 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3024 | main.cleanup() |
| 3025 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3026 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3027 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3028 | main.cleanup() |
| 3029 | main.exit() |
| 3030 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3031 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3032 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3033 | CLI command to run for leadership of the Election test application. |
| 3034 | NOTE: Requires installation of the onos-app-election feature |
| 3035 | Returns: Main.TRUE on success |
| 3036 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3037 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3038 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3039 | cmdStr = "election-test-run" |
| 3040 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3041 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3042 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3043 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3044 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3045 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3046 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3047 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3048 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3049 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3050 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3051 | main.log.error( "Error in electionTestRun on " + self.name + |
| 3052 | ": " + "unexpected response" ) |
| 3053 | main.log.error( repr( response ) ) |
| 3054 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3055 | except AssertionError: |
| 3056 | main.log.exception( "" ) |
| 3057 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3058 | except TypeError: |
| 3059 | main.log.exception( self.name + ": Object not as expected" ) |
| 3060 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3061 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3062 | main.log.error( self.name + ": EOF exception found" ) |
| 3063 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3064 | main.cleanup() |
| 3065 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3066 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3067 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3068 | main.cleanup() |
| 3069 | main.exit() |
| 3070 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3071 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3072 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3073 | * CLI command to withdraw the local node from leadership election for |
| 3074 | * the Election test application. |
| 3075 | #NOTE: Requires installation of the onos-app-election feature |
| 3076 | Returns: Main.TRUE on success |
| 3077 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3078 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3079 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3080 | cmdStr = "election-test-withdraw" |
| 3081 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3082 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3083 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3084 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3085 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3086 | if re.search( successPattern, response ): |
| 3087 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3088 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3089 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3090 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3091 | main.log.error( "Error in electionTestWithdraw on " + |
| 3092 | self.name + ": " + "unexpected response" ) |
| 3093 | main.log.error( repr( response ) ) |
| 3094 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3095 | except AssertionError: |
| 3096 | main.log.exception( "" ) |
| 3097 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3098 | except TypeError: |
| 3099 | main.log.exception( self.name + ": Object not as expected" ) |
| 3100 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3101 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3102 | main.log.error( self.name + ": EOF exception found" ) |
| 3103 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3104 | main.cleanup() |
| 3105 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3106 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3107 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3108 | main.cleanup() |
| 3109 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3110 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3111 | def getDevicePortsEnabledCount( self, dpid ): |
| 3112 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3113 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3114 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3115 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3116 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3117 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 3118 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3119 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3120 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3121 | if re.search( "No such device", output ): |
| 3122 | main.log.error( "Error in getting ports" ) |
| 3123 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3124 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3125 | except AssertionError: |
| 3126 | main.log.exception( "" ) |
| 3127 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3128 | except TypeError: |
| 3129 | main.log.exception( self.name + ": Object not as expected" ) |
| 3130 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3131 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3132 | main.log.error( self.name + ": EOF exception found" ) |
| 3133 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3134 | main.cleanup() |
| 3135 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3136 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3137 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3138 | main.cleanup() |
| 3139 | main.exit() |
| 3140 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3141 | def getDeviceLinksActiveCount( self, dpid ): |
| 3142 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3143 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3144 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3145 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3146 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3147 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3148 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3149 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3150 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3151 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3152 | main.log.error( "Error in getting ports " ) |
| 3153 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3154 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3155 | except AssertionError: |
| 3156 | main.log.exception( "" ) |
| 3157 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3158 | except TypeError: |
| 3159 | main.log.exception( self.name + ": Object not as expected" ) |
| 3160 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3161 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3162 | main.log.error( self.name + ": EOF exception found" ) |
| 3163 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3164 | main.cleanup() |
| 3165 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3166 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3167 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3168 | main.cleanup() |
| 3169 | main.exit() |
| 3170 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3171 | def getAllIntentIds( self ): |
| 3172 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3173 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3174 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3175 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3176 | cmdStr = "onos:intents | grep id=" |
| 3177 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3178 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3179 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3180 | if re.search( "Error", output ): |
| 3181 | main.log.error( "Error in getting ports" ) |
| 3182 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3183 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3184 | except AssertionError: |
| 3185 | main.log.exception( "" ) |
| 3186 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3187 | except TypeError: |
| 3188 | main.log.exception( self.name + ": Object not as expected" ) |
| 3189 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3190 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3191 | main.log.error( self.name + ": EOF exception found" ) |
| 3192 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3193 | main.cleanup() |
| 3194 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3195 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3196 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3197 | main.cleanup() |
| 3198 | main.exit() |
| 3199 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3200 | def intentSummary( self ): |
| 3201 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3202 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3203 | """ |
| 3204 | try: |
| 3205 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3206 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3207 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3208 | states.append( intent.get( 'state', None ) ) |
| 3209 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3210 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3211 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3212 | except ( TypeError, ValueError ): |
| 3213 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3214 | return None |
| 3215 | except pexpect.EOF: |
| 3216 | main.log.error( self.name + ": EOF exception found" ) |
| 3217 | main.log.error( self.name + ": " + self.handle.before ) |
| 3218 | main.cleanup() |
| 3219 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3220 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3221 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3222 | main.cleanup() |
| 3223 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3224 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3225 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3226 | """ |
| 3227 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3228 | Optional argument: |
| 3229 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3230 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3231 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3232 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3233 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3234 | cmdStr += " -j" |
| 3235 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3236 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3237 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3238 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3239 | except AssertionError: |
| 3240 | main.log.exception( "" ) |
| 3241 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3242 | except TypeError: |
| 3243 | main.log.exception( self.name + ": Object not as expected" ) |
| 3244 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3245 | except pexpect.EOF: |
| 3246 | main.log.error( self.name + ": EOF exception found" ) |
| 3247 | main.log.error( self.name + ": " + self.handle.before ) |
| 3248 | main.cleanup() |
| 3249 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3250 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3251 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3252 | main.cleanup() |
| 3253 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3254 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3255 | def leaderCandidates( self, jsonFormat=True ): |
| 3256 | """ |
| 3257 | Returns the output of the leaders -c command. |
| 3258 | Optional argument: |
| 3259 | * jsonFormat - boolean indicating if you want output in json |
| 3260 | """ |
| 3261 | try: |
| 3262 | cmdStr = "onos:leaders -c" |
| 3263 | if jsonFormat: |
| 3264 | cmdStr += " -j" |
| 3265 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3266 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3267 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3268 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3269 | except AssertionError: |
| 3270 | main.log.exception( "" ) |
| 3271 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3272 | except TypeError: |
| 3273 | main.log.exception( self.name + ": Object not as expected" ) |
| 3274 | return None |
| 3275 | except pexpect.EOF: |
| 3276 | main.log.error( self.name + ": EOF exception found" ) |
| 3277 | main.log.error( self.name + ": " + self.handle.before ) |
| 3278 | main.cleanup() |
| 3279 | main.exit() |
| 3280 | except Exception: |
| 3281 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3282 | main.cleanup() |
| 3283 | main.exit() |
| 3284 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3285 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3286 | """ |
| 3287 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
| 3288 | topic parameter and an empty list if the topic doesn't exist |
| 3289 | If no leader is elected leader in the returned list will be "none" |
| 3290 | Returns None if there is a type error processing the json object |
| 3291 | """ |
| 3292 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3293 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3294 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3295 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3296 | assert "Command not found:" not in rawOutput, rawOutput |
| 3297 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3298 | results = [] |
| 3299 | for dict in output: |
| 3300 | if dict["topic"] == topic: |
| 3301 | leader = dict["leader"] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3302 | candidates = re.split( ", ", dict["candidates"][1:-1] ) |
| 3303 | results.append( leader ) |
| 3304 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3305 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3306 | except AssertionError: |
| 3307 | main.log.exception( "" ) |
| 3308 | return None |
| 3309 | except ( TypeError, ValueError ): |
| 3310 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3311 | return None |
| 3312 | except pexpect.EOF: |
| 3313 | main.log.error( self.name + ": EOF exception found" ) |
| 3314 | main.log.error( self.name + ": " + self.handle.before ) |
| 3315 | main.cleanup() |
| 3316 | main.exit() |
| 3317 | except Exception: |
| 3318 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3319 | main.cleanup() |
| 3320 | main.exit() |
| 3321 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3322 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3323 | """ |
| 3324 | Returns the output of the intent Pending map. |
| 3325 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3326 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3327 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3328 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3329 | cmdStr += " -j" |
| 3330 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3331 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3332 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3333 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3334 | except AssertionError: |
| 3335 | main.log.exception( "" ) |
| 3336 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3337 | except TypeError: |
| 3338 | main.log.exception( self.name + ": Object not as expected" ) |
| 3339 | return None |
| 3340 | except pexpect.EOF: |
| 3341 | main.log.error( self.name + ": EOF exception found" ) |
| 3342 | main.log.error( self.name + ": " + self.handle.before ) |
| 3343 | main.cleanup() |
| 3344 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3345 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3346 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3347 | main.cleanup() |
| 3348 | main.exit() |
| 3349 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3350 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3351 | """ |
| 3352 | Returns the output of the raft partitions command for ONOS. |
| 3353 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3354 | # Sample JSON |
| 3355 | # { |
| 3356 | # "leader": "tcp://10.128.30.11:7238", |
| 3357 | # "members": [ |
| 3358 | # "tcp://10.128.30.11:7238", |
| 3359 | # "tcp://10.128.30.17:7238", |
| 3360 | # "tcp://10.128.30.13:7238", |
| 3361 | # ], |
| 3362 | # "name": "p1", |
| 3363 | # "term": 3 |
| 3364 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3365 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3366 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3367 | if candidates: |
| 3368 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3369 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3370 | cmdStr += " -j" |
| 3371 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3372 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3373 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3374 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3375 | except AssertionError: |
| 3376 | main.log.exception( "" ) |
| 3377 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3378 | except TypeError: |
| 3379 | main.log.exception( self.name + ": Object not as expected" ) |
| 3380 | return None |
| 3381 | except pexpect.EOF: |
| 3382 | main.log.error( self.name + ": EOF exception found" ) |
| 3383 | main.log.error( self.name + ": " + self.handle.before ) |
| 3384 | main.cleanup() |
| 3385 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3386 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3387 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3388 | main.cleanup() |
| 3389 | main.exit() |
| 3390 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3391 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3392 | """ |
| 3393 | Returns the output of the apps command for ONOS. This command lists |
| 3394 | information about installed ONOS applications |
| 3395 | """ |
| 3396 | # Sample JSON object |
| 3397 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
| 3398 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3399 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
| 3400 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
| 3401 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3402 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3403 | if summary: |
| 3404 | cmdStr += " -s" |
| 3405 | if active: |
| 3406 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3407 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3408 | cmdStr += " -j" |
| 3409 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3410 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3411 | assert "Command not found:" not in output, output |
| 3412 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3413 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3414 | # FIXME: look at specific exceptions/Errors |
| 3415 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3416 | main.log.exception( "Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3417 | return None |
| 3418 | except TypeError: |
| 3419 | main.log.exception( self.name + ": Object not as expected" ) |
| 3420 | return None |
| 3421 | except pexpect.EOF: |
| 3422 | main.log.error( self.name + ": EOF exception found" ) |
| 3423 | main.log.error( self.name + ": " + self.handle.before ) |
| 3424 | main.cleanup() |
| 3425 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3426 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3427 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3428 | main.cleanup() |
| 3429 | main.exit() |
| 3430 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3431 | def appStatus( self, appName ): |
| 3432 | """ |
| 3433 | Uses the onos:apps cli command to return the status of an application. |
| 3434 | Returns: |
| 3435 | "ACTIVE" - If app is installed and activated |
| 3436 | "INSTALLED" - If app is installed and deactivated |
| 3437 | "UNINSTALLED" - If app is not installed |
| 3438 | None - on error |
| 3439 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3440 | try: |
| 3441 | if not isinstance( appName, types.StringType ): |
| 3442 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3443 | " a string" ) |
| 3444 | return None |
| 3445 | output = self.apps( jsonFormat=True ) |
| 3446 | appsJson = json.loads( output ) |
| 3447 | state = None |
| 3448 | for app in appsJson: |
| 3449 | if appName == app.get('name'): |
| 3450 | state = app.get('state') |
| 3451 | break |
| 3452 | if state == "ACTIVE" or state == "INSTALLED": |
| 3453 | return state |
| 3454 | elif state is None: |
| 3455 | return "UNINSTALLED" |
| 3456 | elif state: |
| 3457 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3458 | str( state ) ) |
| 3459 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3460 | except ( TypeError, ValueError ): |
| 3461 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3462 | return None |
| 3463 | except pexpect.EOF: |
| 3464 | main.log.error( self.name + ": EOF exception found" ) |
| 3465 | main.log.error( self.name + ": " + self.handle.before ) |
| 3466 | main.cleanup() |
| 3467 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3468 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3469 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3470 | main.cleanup() |
| 3471 | main.exit() |
| 3472 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3473 | def app( self, appName, option ): |
| 3474 | """ |
| 3475 | Interacts with the app command for ONOS. This command manages |
| 3476 | application inventory. |
| 3477 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3478 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3479 | # Validate argument types |
| 3480 | valid = True |
| 3481 | if not isinstance( appName, types.StringType ): |
| 3482 | main.log.error( self.name + ".app(): appName must be a " + |
| 3483 | "string" ) |
| 3484 | valid = False |
| 3485 | if not isinstance( option, types.StringType ): |
| 3486 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3487 | valid = False |
| 3488 | if not valid: |
| 3489 | return main.FALSE |
| 3490 | # Validate Option |
| 3491 | option = option.lower() |
| 3492 | # NOTE: Install may become a valid option |
| 3493 | if option == "activate": |
| 3494 | pass |
| 3495 | elif option == "deactivate": |
| 3496 | pass |
| 3497 | elif option == "uninstall": |
| 3498 | pass |
| 3499 | else: |
| 3500 | # Invalid option |
| 3501 | main.log.error( "The ONOS app command argument only takes " + |
| 3502 | "the values: (activate|deactivate|uninstall)" + |
| 3503 | "; was given '" + option + "'") |
| 3504 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3505 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3506 | output = self.sendline( cmdStr ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3507 | if "Error executing command" in output: |
| 3508 | main.log.error( "Error in processing onos:app command: " + |
| 3509 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3510 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3511 | elif "No such application" in output: |
| 3512 | main.log.error( "The application '" + appName + |
| 3513 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3514 | return main.FALSE |
| 3515 | elif "Command not found:" in output: |
| 3516 | main.log.error( "Error in processing onos:app command: " + |
| 3517 | str( output ) ) |
| 3518 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3519 | elif "Unsupported command:" in output: |
| 3520 | main.log.error( "Incorrect command given to 'app': " + |
| 3521 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3522 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3523 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3524 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3525 | return main.TRUE |
| 3526 | except TypeError: |
| 3527 | main.log.exception( self.name + ": Object not as expected" ) |
| 3528 | return main.ERROR |
| 3529 | except pexpect.EOF: |
| 3530 | main.log.error( self.name + ": EOF exception found" ) |
| 3531 | main.log.error( self.name + ": " + self.handle.before ) |
| 3532 | main.cleanup() |
| 3533 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3534 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3535 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3536 | main.cleanup() |
| 3537 | main.exit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3538 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3539 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3540 | """ |
| 3541 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3542 | appName is the hierarchical app name, not the feature name |
| 3543 | If check is True, method will check the status of the app after the |
| 3544 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3545 | Returns main.TRUE if the command was successfully sent |
| 3546 | main.FALSE if the cli responded with an error or given |
| 3547 | incorrect input |
| 3548 | """ |
| 3549 | try: |
| 3550 | if not isinstance( appName, types.StringType ): |
| 3551 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3552 | " a string" ) |
| 3553 | return main.FALSE |
| 3554 | status = self.appStatus( appName ) |
| 3555 | if status == "INSTALLED": |
| 3556 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3557 | if check and response == main.TRUE: |
| 3558 | for i in range(10): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3559 | status = self.appStatus( appName ) |
| 3560 | if status == "ACTIVE": |
| 3561 | return main.TRUE |
| 3562 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3563 | main.log.debug( "The state of application " + |
| 3564 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3565 | time.sleep( 1 ) |
| 3566 | return main.FALSE |
| 3567 | else: # not 'check' or command didn't succeed |
| 3568 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3569 | elif status == "ACTIVE": |
| 3570 | return main.TRUE |
| 3571 | elif status == "UNINSTALLED": |
| 3572 | main.log.error( self.name + ": Tried to activate the " + |
| 3573 | "application '" + appName + "' which is not " + |
| 3574 | "installed." ) |
| 3575 | else: |
| 3576 | main.log.error( "Unexpected return value from appStatus: " + |
| 3577 | str( status ) ) |
| 3578 | return main.ERROR |
| 3579 | except TypeError: |
| 3580 | main.log.exception( self.name + ": Object not as expected" ) |
| 3581 | return main.ERROR |
| 3582 | except pexpect.EOF: |
| 3583 | main.log.error( self.name + ": EOF exception found" ) |
| 3584 | main.log.error( self.name + ": " + self.handle.before ) |
| 3585 | main.cleanup() |
| 3586 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3587 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3588 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3589 | main.cleanup() |
| 3590 | main.exit() |
| 3591 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3592 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3593 | """ |
| 3594 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3595 | appName is the hierarchical app name, not the feature name |
| 3596 | If check is True, method will check the status of the app after the |
| 3597 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3598 | Returns main.TRUE if the command was successfully sent |
| 3599 | main.FALSE if the cli responded with an error or given |
| 3600 | incorrect input |
| 3601 | """ |
| 3602 | try: |
| 3603 | if not isinstance( appName, types.StringType ): |
| 3604 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3605 | "be a string" ) |
| 3606 | return main.FALSE |
| 3607 | status = self.appStatus( appName ) |
| 3608 | if status == "INSTALLED": |
| 3609 | return main.TRUE |
| 3610 | elif status == "ACTIVE": |
| 3611 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3612 | if check and response == main.TRUE: |
| 3613 | for i in range(10): # try 10 times then give up |
| 3614 | status = self.appStatus( appName ) |
| 3615 | if status == "INSTALLED": |
| 3616 | return main.TRUE |
| 3617 | else: |
| 3618 | time.sleep( 1 ) |
| 3619 | return main.FALSE |
| 3620 | else: # not check or command didn't succeed |
| 3621 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3622 | elif status == "UNINSTALLED": |
| 3623 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3624 | "application '" + appName + "' which is not " + |
| 3625 | "installed." ) |
| 3626 | return main.TRUE |
| 3627 | else: |
| 3628 | main.log.error( "Unexpected return value from appStatus: " + |
| 3629 | str( status ) ) |
| 3630 | return main.ERROR |
| 3631 | except TypeError: |
| 3632 | main.log.exception( self.name + ": Object not as expected" ) |
| 3633 | return main.ERROR |
| 3634 | except pexpect.EOF: |
| 3635 | main.log.error( self.name + ": EOF exception found" ) |
| 3636 | main.log.error( self.name + ": " + self.handle.before ) |
| 3637 | main.cleanup() |
| 3638 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3639 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3640 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3641 | main.cleanup() |
| 3642 | main.exit() |
| 3643 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3644 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3645 | """ |
| 3646 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3647 | appName is the hierarchical app name, not the feature name |
| 3648 | If check is True, method will check the status of the app after the |
| 3649 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3650 | Returns main.TRUE if the command was successfully sent |
| 3651 | main.FALSE if the cli responded with an error or given |
| 3652 | incorrect input |
| 3653 | """ |
| 3654 | # TODO: check with Thomas about the state machine for apps |
| 3655 | try: |
| 3656 | if not isinstance( appName, types.StringType ): |
| 3657 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3658 | "be a string" ) |
| 3659 | return main.FALSE |
| 3660 | status = self.appStatus( appName ) |
| 3661 | if status == "INSTALLED": |
| 3662 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3663 | if check and response == main.TRUE: |
| 3664 | for i in range(10): # try 10 times then give up |
| 3665 | status = self.appStatus( appName ) |
| 3666 | if status == "UNINSTALLED": |
| 3667 | return main.TRUE |
| 3668 | else: |
| 3669 | time.sleep( 1 ) |
| 3670 | return main.FALSE |
| 3671 | else: # not check or command didn't succeed |
| 3672 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3673 | elif status == "ACTIVE": |
| 3674 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3675 | "application '" + appName + "' which is " + |
| 3676 | "currently active." ) |
| 3677 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3678 | if check and response == main.TRUE: |
| 3679 | for i in range(10): # try 10 times then give up |
| 3680 | status = self.appStatus( appName ) |
| 3681 | if status == "UNINSTALLED": |
| 3682 | return main.TRUE |
| 3683 | else: |
| 3684 | time.sleep( 1 ) |
| 3685 | return main.FALSE |
| 3686 | else: # not check or command didn't succeed |
| 3687 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3688 | elif status == "UNINSTALLED": |
| 3689 | return main.TRUE |
| 3690 | else: |
| 3691 | main.log.error( "Unexpected return value from appStatus: " + |
| 3692 | str( status ) ) |
| 3693 | return main.ERROR |
| 3694 | except TypeError: |
| 3695 | main.log.exception( self.name + ": Object not as expected" ) |
| 3696 | return main.ERROR |
| 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() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3702 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3703 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3704 | main.cleanup() |
| 3705 | main.exit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3706 | |
| 3707 | def appIDs( self, jsonFormat=True ): |
| 3708 | """ |
| 3709 | Show the mappings between app id and app names given by the 'app-ids' |
| 3710 | cli command |
| 3711 | """ |
| 3712 | try: |
| 3713 | cmdStr = "app-ids" |
| 3714 | if jsonFormat: |
| 3715 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3716 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3717 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3718 | assert "Command not found:" not in output, output |
| 3719 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3720 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3721 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3722 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3723 | return None |
| 3724 | except TypeError: |
| 3725 | main.log.exception( self.name + ": Object not as expected" ) |
| 3726 | return None |
| 3727 | except pexpect.EOF: |
| 3728 | main.log.error( self.name + ": EOF exception found" ) |
| 3729 | main.log.error( self.name + ": " + self.handle.before ) |
| 3730 | main.cleanup() |
| 3731 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3732 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3733 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3734 | main.cleanup() |
| 3735 | main.exit() |
| 3736 | |
| 3737 | def appToIDCheck( self ): |
| 3738 | """ |
| 3739 | This method will check that each application's ID listed in 'apps' is |
| 3740 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3741 | there are no duplicate IDs issued. Note that an app ID should be |
| 3742 | a globaly unique numerical identifier for app/app-like features. Once |
| 3743 | an ID is registered, the ID is never freed up so that if an app is |
| 3744 | reinstalled it will have the same ID. |
| 3745 | |
| 3746 | Returns: main.TRUE if the check passes and |
| 3747 | main.FALSE if the check fails or |
| 3748 | main.ERROR if there is some error in processing the test |
| 3749 | """ |
| 3750 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3751 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3752 | rawJson = self.appIDs( jsonFormat=True ) |
| 3753 | if rawJson: |
| 3754 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3755 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3756 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3757 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3758 | rawJson = self.apps( jsonFormat=True ) |
| 3759 | if rawJson: |
| 3760 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3761 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3762 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3763 | bail = True |
| 3764 | if bail: |
| 3765 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3766 | result = main.TRUE |
| 3767 | for app in apps: |
| 3768 | appID = app.get( 'id' ) |
| 3769 | if appID is None: |
| 3770 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3771 | result = main.FALSE |
| 3772 | appName = app.get( 'name' ) |
| 3773 | if appName is None: |
| 3774 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3775 | result = main.FALSE |
| 3776 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3777 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3778 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3779 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3780 | if not current: # if ids doesn't have this id |
| 3781 | result = main.FALSE |
| 3782 | main.log.error( "'app-ids' does not have the ID for " + |
| 3783 | str( appName ) + " that apps does." ) |
| 3784 | elif len( current ) > 1: |
| 3785 | # there is more than one app with this ID |
| 3786 | result = main.FALSE |
| 3787 | # We will log this later in the method |
| 3788 | elif not current[0][ 'name' ] == appName: |
| 3789 | currentName = current[0][ 'name' ] |
| 3790 | result = main.FALSE |
| 3791 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3792 | " registered under id:" + str( appID ) + |
| 3793 | " but 'apps' has " + str( appName ) ) |
| 3794 | else: |
| 3795 | pass # id and name match! |
| 3796 | # now make sure that app-ids has no duplicates |
| 3797 | idsList = [] |
| 3798 | namesList = [] |
| 3799 | for item in ids: |
| 3800 | idsList.append( item[ 'id' ] ) |
| 3801 | namesList.append( item[ 'name' ] ) |
| 3802 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3803 | len( namesList ) != len( set( namesList ) ): |
| 3804 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3805 | + json.dumps( ids, |
| 3806 | sort_keys=True, |
| 3807 | indent=4, |
| 3808 | separators=( ',', ': ' ) ) ) |
| 3809 | result = main.FALSE |
| 3810 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3811 | except ( TypeError, ValueError ): |
| 3812 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3813 | return main.ERROR |
| 3814 | except pexpect.EOF: |
| 3815 | main.log.error( self.name + ": EOF exception found" ) |
| 3816 | main.log.error( self.name + ": " + self.handle.before ) |
| 3817 | main.cleanup() |
| 3818 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3819 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3820 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3821 | main.cleanup() |
| 3822 | main.exit() |
| 3823 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3824 | def getCfg( self, component=None, propName=None, short=False, |
| 3825 | jsonFormat=True ): |
| 3826 | """ |
| 3827 | Get configuration settings from onos cli |
| 3828 | Optional arguments: |
| 3829 | component - Optionally only list configurations for a specific |
| 3830 | component. If None, all components with configurations |
| 3831 | are displayed. Case Sensitive string. |
| 3832 | propName - If component is specified, propName option will show |
| 3833 | only this specific configuration from that component. |
| 3834 | Case Sensitive string. |
| 3835 | jsonFormat - Returns output as json. Note that this will override |
| 3836 | the short option |
| 3837 | short - Short, less verbose, version of configurations. |
| 3838 | This is overridden by the json option |
| 3839 | returns: |
| 3840 | Output from cli as a string or None on error |
| 3841 | """ |
| 3842 | try: |
| 3843 | baseStr = "cfg" |
| 3844 | cmdStr = " get" |
| 3845 | componentStr = "" |
| 3846 | if component: |
| 3847 | componentStr += " " + component |
| 3848 | if propName: |
| 3849 | componentStr += " " + propName |
| 3850 | if jsonFormat: |
| 3851 | baseStr += " -j" |
| 3852 | elif short: |
| 3853 | baseStr += " -s" |
| 3854 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3855 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3856 | assert "Command not found:" not in output, output |
| 3857 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3858 | return output |
| 3859 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3860 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3861 | return None |
| 3862 | except TypeError: |
| 3863 | main.log.exception( self.name + ": Object not as expected" ) |
| 3864 | return None |
| 3865 | except pexpect.EOF: |
| 3866 | main.log.error( self.name + ": EOF exception found" ) |
| 3867 | main.log.error( self.name + ": " + self.handle.before ) |
| 3868 | main.cleanup() |
| 3869 | main.exit() |
| 3870 | except Exception: |
| 3871 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3872 | main.cleanup() |
| 3873 | main.exit() |
| 3874 | |
| 3875 | def setCfg( self, component, propName, value=None, check=True ): |
| 3876 | """ |
| 3877 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3878 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3879 | component - The case sensitive name of the component whose |
| 3880 | property is to be set |
| 3881 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3882 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3883 | value - The value to set the property to. If None, will unset the |
| 3884 | property and revert it to it's default value(if applicable) |
| 3885 | check - Boolean, Check whether the option was successfully set this |
| 3886 | only applies when a value is given. |
| 3887 | returns: |
| 3888 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3889 | will return main.TRUE unless there is an error |
| 3890 | """ |
| 3891 | try: |
| 3892 | baseStr = "cfg" |
| 3893 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3894 | if value is not None: |
| 3895 | cmdStr += " " + str( value ) |
| 3896 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3897 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3898 | assert "Command not found:" not in output, output |
| 3899 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3900 | if value and check: |
| 3901 | results = self.getCfg( component=str( component ), |
| 3902 | propName=str( propName ), |
| 3903 | jsonFormat=True ) |
| 3904 | # Check if current value is what we just set |
| 3905 | try: |
| 3906 | jsonOutput = json.loads( results ) |
| 3907 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3908 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3909 | main.log.exception( "Error parsing cfg output" ) |
| 3910 | main.log.error( "output:" + repr( results ) ) |
| 3911 | return main.FALSE |
| 3912 | if current == str( value ): |
| 3913 | return main.TRUE |
| 3914 | return main.FALSE |
| 3915 | return main.TRUE |
| 3916 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3917 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3918 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3919 | except ( TypeError, ValueError ): |
| 3920 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3921 | return main.FALSE |
| 3922 | except pexpect.EOF: |
| 3923 | main.log.error( self.name + ": EOF exception found" ) |
| 3924 | main.log.error( self.name + ": " + self.handle.before ) |
| 3925 | main.cleanup() |
| 3926 | main.exit() |
| 3927 | except Exception: |
| 3928 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3929 | main.cleanup() |
| 3930 | main.exit() |
| 3931 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3932 | def setTestAdd( self, setName, values ): |
| 3933 | """ |
| 3934 | CLI command to add elements to a distributed set. |
| 3935 | Arguments: |
| 3936 | setName - The name of the set to add to. |
| 3937 | values - The value(s) to add to the set, space seperated. |
| 3938 | Example usages: |
| 3939 | setTestAdd( "set1", "a b c" ) |
| 3940 | setTestAdd( "set2", "1" ) |
| 3941 | returns: |
| 3942 | main.TRUE on success OR |
| 3943 | main.FALSE if elements were already in the set OR |
| 3944 | main.ERROR on error |
| 3945 | """ |
| 3946 | try: |
| 3947 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
| 3948 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3949 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3950 | assert "Command not found:" not in output, output |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3951 | try: |
| 3952 | # TODO: Maybe make this less hardcoded |
| 3953 | # ConsistentMap Exceptions |
| 3954 | assert "org.onosproject.store.service" not in output |
| 3955 | # Node not leader |
| 3956 | assert "java.lang.IllegalStateException" not in output |
| 3957 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3958 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3959 | "command: " + str( output ) ) |
| 3960 | retryTime = 30 # Conservative time, given by Madan |
| 3961 | main.log.info( "Waiting " + str( retryTime ) + |
| 3962 | "seconds before retrying." ) |
| 3963 | time.sleep( retryTime ) # Due to change in mastership |
| 3964 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3965 | assert output is not None, "Error in sendline" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3966 | assert "Error executing command" not in output |
| 3967 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3968 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
| 3969 | main.log.info( self.name + ": " + output ) |
| 3970 | if re.search( positiveMatch, output): |
| 3971 | return main.TRUE |
| 3972 | elif re.search( negativeMatch, output): |
| 3973 | return main.FALSE |
| 3974 | else: |
| 3975 | main.log.error( self.name + ": setTestAdd did not" + |
| 3976 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3977 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3978 | return main.ERROR |
| 3979 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3980 | main.log.exception( "Error in processing '" + cmdStr + "' command. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3981 | return main.ERROR |
| 3982 | except TypeError: |
| 3983 | main.log.exception( self.name + ": Object not as expected" ) |
| 3984 | return main.ERROR |
| 3985 | except pexpect.EOF: |
| 3986 | main.log.error( self.name + ": EOF exception found" ) |
| 3987 | main.log.error( self.name + ": " + self.handle.before ) |
| 3988 | main.cleanup() |
| 3989 | main.exit() |
| 3990 | except Exception: |
| 3991 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3992 | main.cleanup() |
| 3993 | main.exit() |
| 3994 | |
| 3995 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 3996 | """ |
| 3997 | CLI command to remove elements from a distributed set. |
| 3998 | Required arguments: |
| 3999 | setName - The name of the set to remove from. |
| 4000 | values - The value(s) to remove from the set, space seperated. |
| 4001 | Optional arguments: |
| 4002 | clear - Clear all elements from the set |
| 4003 | retain - Retain only the given values. (intersection of the |
| 4004 | original set and the given set) |
| 4005 | returns: |
| 4006 | main.TRUE on success OR |
| 4007 | main.FALSE if the set was not changed OR |
| 4008 | main.ERROR on error |
| 4009 | """ |
| 4010 | try: |
| 4011 | cmdStr = "set-test-remove " |
| 4012 | if clear: |
| 4013 | cmdStr += "-c " + str( setName ) |
| 4014 | elif retain: |
| 4015 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4016 | else: |
| 4017 | cmdStr += str( setName ) + " " + str( values ) |
| 4018 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4019 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4020 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4021 | # TODO: Maybe make this less hardcoded |
| 4022 | # ConsistentMap Exceptions |
| 4023 | assert "org.onosproject.store.service" not in output |
| 4024 | # Node not leader |
| 4025 | assert "java.lang.IllegalStateException" not in output |
| 4026 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4027 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4028 | "command: " + str( output ) ) |
| 4029 | retryTime = 30 # Conservative time, given by Madan |
| 4030 | main.log.info( "Waiting " + str( retryTime ) + |
| 4031 | "seconds before retrying." ) |
| 4032 | time.sleep( retryTime ) # Due to change in mastership |
| 4033 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4034 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4035 | assert "Command not found:" not in output, output |
| 4036 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4037 | main.log.info( self.name + ": " + output ) |
| 4038 | if clear: |
| 4039 | pattern = "Set " + str( setName ) + " cleared" |
| 4040 | if re.search( pattern, output ): |
| 4041 | return main.TRUE |
| 4042 | elif retain: |
| 4043 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4044 | "only elements of set \[(.*)\]" |
| 4045 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4046 | "retaining only elements of the set " +\ |
| 4047 | "\[(.*)\]" |
| 4048 | if re.search( positivePattern, output ): |
| 4049 | return main.TRUE |
| 4050 | elif re.search( negativePattern, output ): |
| 4051 | return main.FALSE |
| 4052 | else: |
| 4053 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4054 | str( setName ) |
| 4055 | if ( len( values.split() ) == 1 ): |
| 4056 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4057 | str( setName ) |
| 4058 | else: |
| 4059 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4060 | str( setName ) |
| 4061 | if re.search( positivePattern, output ): |
| 4062 | return main.TRUE |
| 4063 | elif re.search( negativePattern, output ): |
| 4064 | return main.FALSE |
| 4065 | main.log.error( self.name + ": setTestRemove did not" + |
| 4066 | " match expected output" ) |
| 4067 | main.log.debug( self.name + " expected: " + pattern ) |
| 4068 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4069 | return main.ERROR |
| 4070 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4071 | main.log.exception( "Error in processing '" + cmdStr + "' commandr. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4072 | return main.ERROR |
| 4073 | except TypeError: |
| 4074 | main.log.exception( self.name + ": Object not as expected" ) |
| 4075 | return main.ERROR |
| 4076 | except pexpect.EOF: |
| 4077 | main.log.error( self.name + ": EOF exception found" ) |
| 4078 | main.log.error( self.name + ": " + self.handle.before ) |
| 4079 | main.cleanup() |
| 4080 | main.exit() |
| 4081 | except Exception: |
| 4082 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4083 | main.cleanup() |
| 4084 | main.exit() |
| 4085 | |
| 4086 | def setTestGet( self, setName, values="" ): |
| 4087 | """ |
| 4088 | CLI command to get the elements in a distributed set. |
| 4089 | Required arguments: |
| 4090 | setName - The name of the set to remove from. |
| 4091 | Optional arguments: |
| 4092 | values - The value(s) to check if in the set, space seperated. |
| 4093 | returns: |
| 4094 | main.ERROR on error OR |
| 4095 | A list of elements in the set if no optional arguments are |
| 4096 | supplied OR |
| 4097 | A tuple containing the list then: |
| 4098 | main.FALSE if the given values are not in the set OR |
| 4099 | main.TRUE if the given values are in the set OR |
| 4100 | """ |
| 4101 | try: |
| 4102 | values = str( values ).strip() |
| 4103 | setName = str( setName ).strip() |
| 4104 | length = len( values.split() ) |
| 4105 | containsCheck = None |
| 4106 | # Patterns to match |
| 4107 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4108 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4109 | containsTrue = "Set " + setName + " contains the value " + values |
| 4110 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4111 | values |
| 4112 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4113 | setPattern |
| 4114 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4115 | " subset " + setPattern |
| 4116 | |
| 4117 | cmdStr = "set-test-get " |
| 4118 | cmdStr += setName + " " + values |
| 4119 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4120 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4121 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4122 | # TODO: Maybe make this less hardcoded |
| 4123 | # ConsistentMap Exceptions |
| 4124 | assert "org.onosproject.store.service" not in output |
| 4125 | # Node not leader |
| 4126 | assert "java.lang.IllegalStateException" not in output |
| 4127 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4128 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4129 | "command: " + str( output ) ) |
| 4130 | retryTime = 30 # Conservative time, given by Madan |
| 4131 | main.log.info( "Waiting " + str( retryTime ) + |
| 4132 | "seconds before retrying." ) |
| 4133 | time.sleep( retryTime ) # Due to change in mastership |
| 4134 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4135 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4136 | assert "Command not found:" not in output, output |
| 4137 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4138 | main.log.info( self.name + ": " + output ) |
| 4139 | |
| 4140 | if length == 0: |
| 4141 | match = re.search( pattern, output ) |
| 4142 | else: # if given values |
| 4143 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4144 | patternTrue = pattern + "\r\n" + containsTrue |
| 4145 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4146 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4147 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4148 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4149 | matchTrue = re.search( patternTrue, output ) |
| 4150 | matchFalse = re.search( patternFalse, output ) |
| 4151 | if matchTrue: |
| 4152 | containsCheck = main.TRUE |
| 4153 | match = matchTrue |
| 4154 | elif matchFalse: |
| 4155 | containsCheck = main.FALSE |
| 4156 | match = matchFalse |
| 4157 | else: |
| 4158 | main.log.error( self.name + " setTestGet did not match " +\ |
| 4159 | "expected output" ) |
| 4160 | main.log.debug( self.name + " expected: " + pattern ) |
| 4161 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4162 | match = None |
| 4163 | if match: |
| 4164 | setMatch = match.group( 1 ) |
| 4165 | if setMatch == '': |
| 4166 | setList = [] |
| 4167 | else: |
| 4168 | setList = setMatch.split( ", " ) |
| 4169 | if length > 0: |
| 4170 | return ( setList, containsCheck ) |
| 4171 | else: |
| 4172 | return setList |
| 4173 | else: # no match |
| 4174 | main.log.error( self.name + ": setTestGet 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 main.ERROR |
| 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 | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4181 | return main.ERROR |
| 4182 | except TypeError: |
| 4183 | main.log.exception( self.name + ": Object not as expected" ) |
| 4184 | return main.ERROR |
| 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 | |
| 4195 | def setTestSize( self, setName ): |
| 4196 | """ |
| 4197 | CLI command to get the elements in a distributed set. |
| 4198 | Required arguments: |
| 4199 | setName - The name of the set to remove from. |
| 4200 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4201 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4202 | None on error |
| 4203 | """ |
| 4204 | try: |
| 4205 | # TODO: Should this check against the number of elements returned |
| 4206 | # and then return true/false based on that? |
| 4207 | setName = str( setName ).strip() |
| 4208 | # Patterns to match |
| 4209 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4210 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4211 | setPattern |
| 4212 | cmdStr = "set-test-get -s " |
| 4213 | cmdStr += setName |
| 4214 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4215 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4216 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4217 | # TODO: Maybe make this less hardcoded |
| 4218 | # ConsistentMap Exceptions |
| 4219 | assert "org.onosproject.store.service" not in output |
| 4220 | # Node not leader |
| 4221 | assert "java.lang.IllegalStateException" not in output |
| 4222 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4223 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4224 | "command: " + str( output ) ) |
| 4225 | retryTime = 30 # Conservative time, given by Madan |
| 4226 | main.log.info( "Waiting " + str( retryTime ) + |
| 4227 | "seconds before retrying." ) |
| 4228 | time.sleep( retryTime ) # Due to change in mastership |
| 4229 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4230 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4231 | assert "Command not found:" not in output, output |
| 4232 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4233 | main.log.info( self.name + ": " + output ) |
| 4234 | match = re.search( pattern, output ) |
| 4235 | if match: |
| 4236 | setSize = int( match.group( 1 ) ) |
| 4237 | setMatch = match.group( 2 ) |
| 4238 | if len( setMatch.split() ) == setSize: |
| 4239 | main.log.info( "The size returned by " + self.name + |
| 4240 | " matches the number of elements in " + |
| 4241 | "the returned set" ) |
| 4242 | else: |
| 4243 | main.log.error( "The size returned by " + self.name + |
| 4244 | " does not match the number of " + |
| 4245 | "elements in the returned set." ) |
| 4246 | return setSize |
| 4247 | else: # no match |
| 4248 | main.log.error( self.name + ": setTestGet did not" + |
| 4249 | " match expected output" ) |
| 4250 | main.log.debug( self.name + " expected: " + pattern ) |
| 4251 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4252 | return None |
| 4253 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4254 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 4255 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4256 | except TypeError: |
| 4257 | main.log.exception( self.name + ": Object not as expected" ) |
| 4258 | return None |
| 4259 | except pexpect.EOF: |
| 4260 | main.log.error( self.name + ": EOF exception found" ) |
| 4261 | main.log.error( self.name + ": " + self.handle.before ) |
| 4262 | main.cleanup() |
| 4263 | main.exit() |
| 4264 | except Exception: |
| 4265 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4266 | main.cleanup() |
| 4267 | main.exit() |
| 4268 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4269 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4270 | """ |
| 4271 | Command to list the various counters in the system. |
| 4272 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4273 | if jsonFormat, a string of the json object returned by the cli |
| 4274 | command |
| 4275 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4276 | None on error |
| 4277 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4278 | try: |
| 4279 | counters = {} |
| 4280 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4281 | if jsonFormat: |
| 4282 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4283 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4284 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4285 | assert "Command not found:" not in output, output |
| 4286 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4287 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4288 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4289 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4290 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4291 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4292 | except TypeError: |
| 4293 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4294 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4295 | except pexpect.EOF: |
| 4296 | main.log.error( self.name + ": EOF exception found" ) |
| 4297 | main.log.error( self.name + ": " + self.handle.before ) |
| 4298 | main.cleanup() |
| 4299 | main.exit() |
| 4300 | except Exception: |
| 4301 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4302 | main.cleanup() |
| 4303 | main.exit() |
| 4304 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4305 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4306 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4307 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4308 | Required arguments: |
| 4309 | counter - The name of the counter to increment. |
| 4310 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4311 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4312 | returns: |
| 4313 | integer value of the counter or |
| 4314 | None on Error |
| 4315 | """ |
| 4316 | try: |
| 4317 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4318 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4319 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4320 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4321 | if delta != 1: |
| 4322 | cmdStr += " " + str( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4323 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4324 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4325 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4326 | # TODO: Maybe make this less hardcoded |
| 4327 | # ConsistentMap Exceptions |
| 4328 | assert "org.onosproject.store.service" not in output |
| 4329 | # Node not leader |
| 4330 | assert "java.lang.IllegalStateException" not in output |
| 4331 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4332 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4333 | "command: " + str( output ) ) |
| 4334 | retryTime = 30 # Conservative time, given by Madan |
| 4335 | main.log.info( "Waiting " + str( retryTime ) + |
| 4336 | "seconds before retrying." ) |
| 4337 | time.sleep( retryTime ) # Due to change in mastership |
| 4338 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4339 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4340 | assert "Command not found:" not in output, output |
| 4341 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4342 | main.log.info( self.name + ": " + output ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4343 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4344 | match = re.search( pattern, output ) |
| 4345 | if match: |
| 4346 | return int( match.group( 1 ) ) |
| 4347 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4348 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4349 | " match expected output." ) |
| 4350 | main.log.debug( self.name + " expected: " + pattern ) |
| 4351 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4352 | return None |
| 4353 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4354 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4355 | return None |
| 4356 | except TypeError: |
| 4357 | main.log.exception( self.name + ": Object not as expected" ) |
| 4358 | return None |
| 4359 | except pexpect.EOF: |
| 4360 | main.log.error( self.name + ": EOF exception found" ) |
| 4361 | main.log.error( self.name + ": " + self.handle.before ) |
| 4362 | main.cleanup() |
| 4363 | main.exit() |
| 4364 | except Exception: |
| 4365 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4366 | main.cleanup() |
| 4367 | main.exit() |
| 4368 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4369 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4370 | """ |
| 4371 | CLI command to get a distributed counter then add a delta to it. |
| 4372 | Required arguments: |
| 4373 | counter - The name of the counter to increment. |
| 4374 | Optional arguments: |
| 4375 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4376 | returns: |
| 4377 | integer value of the counter or |
| 4378 | None on Error |
| 4379 | """ |
| 4380 | try: |
| 4381 | counter = str( counter ) |
| 4382 | delta = int( delta ) |
| 4383 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4384 | cmdStr += counter |
| 4385 | if delta != 1: |
| 4386 | cmdStr += " " + str( delta ) |
| 4387 | output = self.sendline( cmdStr ) |
| 4388 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4389 | assert output is not None, "Error in sendline" |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4390 | # TODO: Maybe make this less hardcoded |
| 4391 | # ConsistentMap Exceptions |
| 4392 | assert "org.onosproject.store.service" not in output |
| 4393 | # Node not leader |
| 4394 | assert "java.lang.IllegalStateException" not in output |
| 4395 | except AssertionError: |
| 4396 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4397 | "command: " + str( output ) ) |
| 4398 | retryTime = 30 # Conservative time, given by Madan |
| 4399 | main.log.info( "Waiting " + str( retryTime ) + |
| 4400 | "seconds before retrying." ) |
| 4401 | time.sleep( retryTime ) # Due to change in mastership |
| 4402 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4403 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4404 | assert "Command not found:" not in output, output |
| 4405 | assert "Error executing command" not in output, output |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4406 | main.log.info( self.name + ": " + output ) |
| 4407 | pattern = counter + " was updated to (-?\d+)" |
| 4408 | match = re.search( pattern, output ) |
| 4409 | if match: |
| 4410 | return int( match.group( 1 ) ) |
| 4411 | else: |
| 4412 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4413 | " match expected output." ) |
| 4414 | main.log.debug( self.name + " expected: " + pattern ) |
| 4415 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4416 | return None |
| 4417 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4418 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4419 | return None |
| 4420 | except TypeError: |
| 4421 | main.log.exception( self.name + ": Object not as expected" ) |
| 4422 | return None |
| 4423 | except pexpect.EOF: |
| 4424 | main.log.error( self.name + ": EOF exception found" ) |
| 4425 | main.log.error( self.name + ": " + self.handle.before ) |
| 4426 | main.cleanup() |
| 4427 | main.exit() |
| 4428 | except Exception: |
| 4429 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4430 | main.cleanup() |
| 4431 | main.exit() |
| 4432 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4433 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4434 | """ |
| 4435 | Description: Execute summary command in onos |
| 4436 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4437 | no output |
| 4438 | |
| 4439 | """ |
| 4440 | try: |
| 4441 | cmdStr = "summary" |
| 4442 | if jsonFormat: |
| 4443 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4444 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4445 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4446 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4447 | assert "Error:" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4448 | if not handle: |
| 4449 | main.log.error( self.name + ": There is no output in " + |
| 4450 | "summary command" ) |
| 4451 | return main.FALSE |
| 4452 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4453 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4454 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4455 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4456 | except TypeError: |
| 4457 | main.log.exception( self.name + ": Object not as expected" ) |
| 4458 | return None |
| 4459 | except pexpect.EOF: |
| 4460 | main.log.error( self.name + ": EOF exception found" ) |
| 4461 | main.log.error( self.name + ": " + self.handle.before ) |
| 4462 | main.cleanup() |
| 4463 | main.exit() |
| 4464 | except Exception: |
| 4465 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4466 | main.cleanup() |
| 4467 | main.exit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4468 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4469 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4470 | """ |
| 4471 | CLI command to get the value of a key in a consistent map using |
| 4472 | transactions. This a test function and can only get keys from the |
| 4473 | test map hard coded into the cli command |
| 4474 | Required arguments: |
| 4475 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4476 | returns: |
| 4477 | The string value of the key or |
| 4478 | None on Error |
| 4479 | """ |
| 4480 | try: |
| 4481 | keyName = str( keyName ) |
| 4482 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4483 | cmdStr += keyName |
| 4484 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4485 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4486 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4487 | try: |
| 4488 | # TODO: Maybe make this less hardcoded |
| 4489 | # ConsistentMap Exceptions |
| 4490 | assert "org.onosproject.store.service" not in output |
| 4491 | # Node not leader |
| 4492 | assert "java.lang.IllegalStateException" not in output |
| 4493 | except AssertionError: |
| 4494 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4495 | "command: " + str( output ) ) |
| 4496 | return None |
| 4497 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4498 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4499 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4500 | return None |
| 4501 | else: |
| 4502 | match = re.search( pattern, output ) |
| 4503 | if match: |
| 4504 | return match.groupdict()[ 'value' ] |
| 4505 | else: |
| 4506 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4507 | " match expected output." ) |
| 4508 | main.log.debug( self.name + " expected: " + pattern ) |
| 4509 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4510 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4511 | except AssertionError: |
| 4512 | main.log.exception( "" ) |
| 4513 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4514 | except TypeError: |
| 4515 | main.log.exception( self.name + ": Object not as expected" ) |
| 4516 | return None |
| 4517 | except pexpect.EOF: |
| 4518 | main.log.error( self.name + ": EOF exception found" ) |
| 4519 | main.log.error( self.name + ": " + self.handle.before ) |
| 4520 | main.cleanup() |
| 4521 | main.exit() |
| 4522 | except Exception: |
| 4523 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4524 | main.cleanup() |
| 4525 | main.exit() |
| 4526 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4527 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4528 | """ |
| 4529 | CLI command to put a value into 'numKeys' number of keys in a |
| 4530 | consistent map using transactions. This a test function and can only |
| 4531 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4532 | Required arguments: |
| 4533 | numKeys - Number of keys to add the value to |
| 4534 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4535 | returns: |
| 4536 | A dictionary whose keys are the name of the keys put into the map |
| 4537 | and the values of the keys are dictionaries whose key-values are |
| 4538 | 'value': value put into map and optionaly |
| 4539 | 'oldValue': Previous value in the key or |
| 4540 | None on Error |
| 4541 | |
| 4542 | Example output |
| 4543 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4544 | 'Key2': {'value': 'Testing'} } |
| 4545 | """ |
| 4546 | try: |
| 4547 | numKeys = str( numKeys ) |
| 4548 | value = str( value ) |
| 4549 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4550 | cmdStr += numKeys + " " + value |
| 4551 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4552 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4553 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4554 | try: |
| 4555 | # TODO: Maybe make this less hardcoded |
| 4556 | # ConsistentMap Exceptions |
| 4557 | assert "org.onosproject.store.service" not in output |
| 4558 | # Node not leader |
| 4559 | assert "java.lang.IllegalStateException" not in output |
| 4560 | except AssertionError: |
| 4561 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4562 | "command: " + str( output ) ) |
| 4563 | return None |
| 4564 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4565 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4566 | results = {} |
| 4567 | for line in output.splitlines(): |
| 4568 | new = re.search( newPattern, line ) |
| 4569 | updated = re.search( updatedPattern, line ) |
| 4570 | if new: |
| 4571 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4572 | elif updated: |
| 4573 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4574 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4575 | else: |
| 4576 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4577 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4578 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4579 | newPattern, |
| 4580 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4581 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4582 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4583 | except AssertionError: |
| 4584 | main.log.exception( "" ) |
| 4585 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4586 | except TypeError: |
| 4587 | main.log.exception( self.name + ": Object not as expected" ) |
| 4588 | return None |
| 4589 | except pexpect.EOF: |
| 4590 | main.log.error( self.name + ": EOF exception found" ) |
| 4591 | main.log.error( self.name + ": " + self.handle.before ) |
| 4592 | main.cleanup() |
| 4593 | main.exit() |
| 4594 | except Exception: |
| 4595 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4596 | main.cleanup() |
| 4597 | main.exit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4598 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4599 | def maps( self, jsonFormat=True ): |
| 4600 | """ |
| 4601 | Description: Returns result of onos:maps |
| 4602 | Optional: |
| 4603 | * jsonFormat: enable json formatting of output |
| 4604 | """ |
| 4605 | try: |
| 4606 | cmdStr = "maps" |
| 4607 | if jsonFormat: |
| 4608 | cmdStr += " -j" |
| 4609 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4610 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4611 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4612 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4613 | except AssertionError: |
| 4614 | main.log.exception( "" ) |
| 4615 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4616 | except TypeError: |
| 4617 | main.log.exception( self.name + ": Object not as expected" ) |
| 4618 | return None |
| 4619 | except pexpect.EOF: |
| 4620 | main.log.error( self.name + ": EOF exception found" ) |
| 4621 | main.log.error( self.name + ": " + self.handle.before ) |
| 4622 | main.cleanup() |
| 4623 | main.exit() |
| 4624 | except Exception: |
| 4625 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4626 | main.cleanup() |
| 4627 | main.exit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4628 | |
| 4629 | def getSwController( self, uri, jsonFormat=True ): |
| 4630 | """ |
| 4631 | Descrition: Gets the controller information from the device |
| 4632 | """ |
| 4633 | try: |
| 4634 | cmd = "device-controllers " |
| 4635 | if jsonFormat: |
| 4636 | cmd += "-j " |
| 4637 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4638 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4639 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4640 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4641 | except AssertionError: |
| 4642 | main.log.exception( "" ) |
| 4643 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4644 | except TypeError: |
| 4645 | main.log.exception( self.name + ": Object not as expected" ) |
| 4646 | return None |
| 4647 | except pexpect.EOF: |
| 4648 | main.log.error( self.name + ": EOF exception found" ) |
| 4649 | main.log.error( self.name + ": " + self.handle.before ) |
| 4650 | main.cleanup() |
| 4651 | main.exit() |
| 4652 | except Exception: |
| 4653 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4654 | main.cleanup() |
| 4655 | main.exit() |
| 4656 | |
| 4657 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4658 | """ |
| 4659 | Descrition: sets the controller(s) for the specified device |
| 4660 | |
| 4661 | Parameters: |
| 4662 | Required: uri - String: The uri of the device(switch). |
| 4663 | ip - String or List: The ip address of the controller. |
| 4664 | This parameter can be formed in a couple of different ways. |
| 4665 | VALID: |
| 4666 | 10.0.0.1 - just the ip address |
| 4667 | tcp:10.0.0.1 - the protocol and the ip address |
| 4668 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4669 | so that you can add controllers with different |
| 4670 | protocols and ports |
| 4671 | INVALID: |
| 4672 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4673 | |
| 4674 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4675 | port - The port number. |
| 4676 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4677 | |
| 4678 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4679 | """ |
| 4680 | try: |
| 4681 | cmd = "device-setcontrollers" |
| 4682 | |
| 4683 | if jsonFormat: |
| 4684 | cmd += " -j" |
| 4685 | cmd += " " + uri |
| 4686 | if isinstance( ip, str ): |
| 4687 | ip = [ip] |
| 4688 | for item in ip: |
| 4689 | if ":" in item: |
| 4690 | sitem = item.split( ":" ) |
| 4691 | if len(sitem) == 3: |
| 4692 | cmd += " " + item |
| 4693 | elif "." in sitem[1]: |
| 4694 | cmd += " {}:{}".format(item, port) |
| 4695 | else: |
| 4696 | main.log.error( "Malformed entry: " + item ) |
| 4697 | raise TypeError |
| 4698 | else: |
| 4699 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4700 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4701 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4702 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4703 | if "Error" in response: |
| 4704 | main.log.error( response ) |
| 4705 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4706 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4707 | except AssertionError: |
| 4708 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4709 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4710 | except TypeError: |
| 4711 | main.log.exception( self.name + ": Object not as expected" ) |
| 4712 | return main.FALSE |
| 4713 | except pexpect.EOF: |
| 4714 | main.log.error( self.name + ": EOF exception found" ) |
| 4715 | main.log.error( self.name + ": " + self.handle.before ) |
| 4716 | main.cleanup() |
| 4717 | main.exit() |
| 4718 | except Exception: |
| 4719 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4720 | main.cleanup() |
| 4721 | main.exit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4722 | |
| 4723 | def removeDevice( self, device ): |
| 4724 | ''' |
| 4725 | Description: |
| 4726 | Remove a device from ONOS by passing the uri of the device(s). |
| 4727 | Parameters: |
| 4728 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
| 4729 | Returns: |
| 4730 | Returns main.FALSE if an exception is thrown or an error is present |
| 4731 | in the response. Otherwise, returns main.TRUE. |
| 4732 | NOTE: |
| 4733 | If a host cannot be removed, then this function will return main.FALSE |
| 4734 | ''' |
| 4735 | try: |
| 4736 | if type( device ) is str: |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 4737 | deviceStr = device |
| 4738 | device = [] |
| 4739 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4740 | |
| 4741 | for d in device: |
| 4742 | time.sleep( 1 ) |
| 4743 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4744 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4745 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4746 | if "Error" in response: |
| 4747 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4748 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4749 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4750 | except AssertionError: |
| 4751 | main.log.exception( "" ) |
| 4752 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4753 | except TypeError: |
| 4754 | main.log.exception( self.name + ": Object not as expected" ) |
| 4755 | return main.FALSE |
| 4756 | except pexpect.EOF: |
| 4757 | main.log.error( self.name + ": EOF exception found" ) |
| 4758 | main.log.error( self.name + ": " + self.handle.before ) |
| 4759 | main.cleanup() |
| 4760 | main.exit() |
| 4761 | except Exception: |
| 4762 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4763 | main.cleanup() |
| 4764 | main.exit() |
| 4765 | |
| 4766 | def removeHost( self, host ): |
| 4767 | ''' |
| 4768 | Description: |
| 4769 | Remove a host from ONOS by passing the id of the host(s) |
| 4770 | Parameters: |
| 4771 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
| 4772 | Returns: |
| 4773 | Returns main.FALSE if an exception is thrown or an error is present |
| 4774 | in the response. Otherwise, returns main.TRUE. |
| 4775 | NOTE: |
| 4776 | If a host cannot be removed, then this function will return main.FALSE |
| 4777 | ''' |
| 4778 | try: |
| 4779 | if type( host ) is str: |
| 4780 | host = list( host ) |
| 4781 | |
| 4782 | for h in host: |
| 4783 | time.sleep( 1 ) |
| 4784 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4785 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4786 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4787 | if "Error" in response: |
| 4788 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 4789 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4790 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4791 | except AssertionError: |
| 4792 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4793 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4794 | except TypeError: |
| 4795 | main.log.exception( self.name + ": Object not as expected" ) |
| 4796 | return main.FALSE |
| 4797 | except pexpect.EOF: |
| 4798 | main.log.error( self.name + ": EOF exception found" ) |
| 4799 | main.log.error( self.name + ": " + self.handle.before ) |
| 4800 | main.cleanup() |
| 4801 | main.exit() |
| 4802 | except Exception: |
| 4803 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4804 | main.cleanup() |
| 4805 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4806 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4807 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4808 | ''' |
| 4809 | Description: |
| 4810 | Bring link down or up in the null-provider. |
| 4811 | params: |
| 4812 | begin - (string) One end of a device or switch. |
| 4813 | end - (string) the other end of the device or switch |
| 4814 | returns: |
| 4815 | main.TRUE if no exceptions were thrown and no Errors are |
| 4816 | present in the resoponse. Otherwise, returns main.FALSE |
| 4817 | ''' |
| 4818 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4819 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4820 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4821 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4822 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4823 | if "Error" in response or "Failure" in response: |
| 4824 | main.log.error( response ) |
| 4825 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4826 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4827 | except AssertionError: |
| 4828 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4829 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4830 | except TypeError: |
| 4831 | main.log.exception( self.name + ": Object not as expected" ) |
| 4832 | return main.FALSE |
| 4833 | except pexpect.EOF: |
| 4834 | main.log.error( self.name + ": EOF exception found" ) |
| 4835 | main.log.error( self.name + ": " + self.handle.before ) |
| 4836 | main.cleanup() |
| 4837 | main.exit() |
| 4838 | except Exception: |
| 4839 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4840 | main.cleanup() |
| 4841 | main.exit() |
| 4842 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4843 | def portstate( self, dpid, port, state ): |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4844 | ''' |
| 4845 | Description: |
| 4846 | Changes the state of port in an OF switch by means of the |
| 4847 | PORTSTATUS OF messages. |
| 4848 | params: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4849 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 4850 | port - (string) target port in the device. Ex: '2' |
| 4851 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4852 | returns: |
| 4853 | main.TRUE if no exceptions were thrown and no Errors are |
| 4854 | present in the resoponse. Otherwise, returns main.FALSE |
| 4855 | ''' |
| 4856 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4857 | state = state.lower() |
| 4858 | assert state == 'enable' or state == 'disable', "Unknown state" |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4859 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
| 4860 | response = self.sendline( cmd, showResponse=True ) |
| 4861 | assert response is not None, "Error in sendline" |
| 4862 | assert "Command not found:" not in response, response |
| 4863 | if "Error" in response or "Failure" in response: |
| 4864 | main.log.error( response ) |
| 4865 | return main.FALSE |
| 4866 | return main.TRUE |
| 4867 | except AssertionError: |
| 4868 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4869 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4870 | except TypeError: |
| 4871 | main.log.exception( self.name + ": Object not as expected" ) |
| 4872 | return main.FALSE |
| 4873 | except pexpect.EOF: |
| 4874 | main.log.error( self.name + ": EOF exception found" ) |
| 4875 | main.log.error( self.name + ": " + self.handle.before ) |
| 4876 | main.cleanup() |
| 4877 | main.exit() |
| 4878 | except Exception: |
| 4879 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4880 | main.cleanup() |
| 4881 | main.exit() |
| 4882 | |
| 4883 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 4884 | """ |
| 4885 | Set the logging level to lvl for a specific app |
| 4886 | returns main.TRUE on success |
| 4887 | returns main.FALSE if Error occurred |
| 4888 | if noExit is True, TestON will not exit, but clean up |
| 4889 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 4890 | Level defaults to INFO |
| 4891 | """ |
| 4892 | try: |
| 4893 | self.handle.sendline( "log:set %s %s" %( level, app ) ) |
| 4894 | self.handle.expect( "onos>" ) |
| 4895 | |
| 4896 | response = self.handle.before |
| 4897 | if re.search( "Error", response ): |
| 4898 | return main.FALSE |
| 4899 | return main.TRUE |
| 4900 | except pexpect.TIMEOUT: |
| 4901 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 4902 | main.cleanup() |
| 4903 | main.exit() |
| 4904 | except pexpect.EOF: |
| 4905 | main.log.error( self.name + ": EOF exception found" ) |
| 4906 | main.log.error( self.name + ": " + self.handle.before ) |
| 4907 | main.cleanup() |
| 4908 | main.exit() |
| 4909 | except Exception: |
| 4910 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4911 | main.cleanup() |
| 4912 | main.exit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4913 | |
| 4914 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 4915 | """ |
| 4916 | Return a dictionary which describes the latest network topology data as a |
| 4917 | graph. |
| 4918 | An example of the dictionary: |
| 4919 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 4920 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 4921 | Each vertex should at least have an 'edges' attribute which describes the |
| 4922 | adjacency information. The value of 'edges' attribute is also represented by |
| 4923 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 4924 | list of attributes. |
| 4925 | An example of the edges dictionary: |
| 4926 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 4927 | vertex3: { 'port': ..., 'weight': ... } } |
| 4928 | If includeHost == True, all hosts (and host-switch links) will be included |
| 4929 | in topology data. |
| 4930 | """ |
| 4931 | graphDict = {} |
| 4932 | try: |
| 4933 | links = self.links() |
| 4934 | links = json.loads( links ) |
| 4935 | devices = self.devices() |
| 4936 | devices = json.loads( devices ) |
| 4937 | idToDevice = {} |
| 4938 | for device in devices: |
| 4939 | idToDevice[ device[ 'id' ] ] = device |
| 4940 | if includeHost: |
| 4941 | hosts = self.hosts() |
| 4942 | # FIXME: support 'includeHost' argument |
| 4943 | for link in links: |
| 4944 | nodeA = link[ 'src' ][ 'device' ] |
| 4945 | nodeB = link[ 'dst' ][ 'device' ] |
| 4946 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
| 4947 | if not nodeA in graphDict.keys(): |
| 4948 | graphDict[ nodeA ] = { 'edges':{}, |
| 4949 | 'dpid':idToDevice[ nodeA ][ 'id' ][3:], |
| 4950 | 'type':idToDevice[ nodeA ][ 'type' ], |
| 4951 | 'available':idToDevice[ nodeA ][ 'available' ], |
| 4952 | 'role':idToDevice[ nodeA ][ 'role' ], |
| 4953 | 'mfr':idToDevice[ nodeA ][ 'mfr' ], |
| 4954 | 'hw':idToDevice[ nodeA ][ 'hw' ], |
| 4955 | 'sw':idToDevice[ nodeA ][ 'sw' ], |
| 4956 | 'serial':idToDevice[ nodeA ][ 'serial' ], |
| 4957 | 'chassisId':idToDevice[ nodeA ][ 'chassisId' ], |
| 4958 | 'annotations':idToDevice[ nodeA ][ 'annotations' ]} |
| 4959 | else: |
| 4960 | # Assert nodeB is not connected to any current links of nodeA |
| 4961 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 4962 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port':link[ 'src' ][ 'port' ], |
| 4963 | 'type':link[ 'type' ], |
| 4964 | 'state':link[ 'state' ] } |
| 4965 | return graphDict |
| 4966 | except ( TypeError, ValueError ): |
| 4967 | main.log.exception( self.name + ": Object not as expected" ) |
| 4968 | return None |
| 4969 | except KeyError: |
| 4970 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4971 | return None |
| 4972 | except AssertionError: |
| 4973 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4974 | return None |
| 4975 | except pexpect.EOF: |
| 4976 | main.log.error( self.name + ": EOF exception found" ) |
| 4977 | main.log.error( self.name + ": " + self.handle.before ) |
| 4978 | return None |
| 4979 | except Exception: |
| 4980 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4981 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 4982 | |
| 4983 | def getIntentPerfSummary( self ): |
| 4984 | ''' |
| 4985 | Send command to check intent-perf summary |
| 4986 | Returns: dictionary for intent-perf summary |
| 4987 | if something wrong, function will return None |
| 4988 | ''' |
| 4989 | cmd = "intent-perf -s" |
| 4990 | respDic = {} |
| 4991 | resp = self.sendline( cmd ) |
| 4992 | try: |
| 4993 | # Generate the dictionary to return |
| 4994 | for l in resp.split( "\n" ): |
| 4995 | # Delete any white space in line |
| 4996 | temp = re.sub( r'\s+', '', l ) |
| 4997 | temp = temp.split( ":" ) |
| 4998 | respDic[ temp[0] ] = temp[ 1 ] |
| 4999 | |
| 5000 | except (TypeError, ValueError): |
| 5001 | main.log.exception( self.name + ": Object not as expected" ) |
| 5002 | return None |
| 5003 | except KeyError: |
| 5004 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5005 | return None |
| 5006 | except AssertionError: |
| 5007 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5008 | return None |
| 5009 | except pexpect.EOF: |
| 5010 | main.log.error( self.name + ": EOF exception found" ) |
| 5011 | main.log.error( self.name + ": " + self.handle.before ) |
| 5012 | return None |
| 5013 | except Exception: |
| 5014 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5015 | return None |
| 5016 | return respDic |
| 5017 | |
Chiyu Cheng | e2b48e4 | 2016-11-17 18:11:36 -0800 | [diff] [blame^] | 5018 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5019 | """ |
| 5020 | Searches the latest ONOS log file for the given search term and |
| 5021 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5022 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5023 | Arguments: |
Chiyu Cheng | e2b48e4 | 2016-11-17 18:11:36 -0800 | [diff] [blame^] | 5024 | searchTerm: |
| 5025 | The string to grep from the ONOS log. |
| 5026 | startLine: |
| 5027 | The term that decides which line is the start to search the searchTerm in |
| 5028 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5029 | logNum: |
| 5030 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5031 | information.Because of this, search mutiply logs is necessary to capture |
| 5032 | the right result. logNum is the number of karaf logs that we need to search |
| 5033 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5034 | mode: |
| 5035 | all: return all the strings that contain the search term |
| 5036 | last: return the last string that contains the search term |
| 5037 | first: return the first string that contains the search term |
Chiyu Cheng | e2b48e4 | 2016-11-17 18:11:36 -0800 | [diff] [blame^] | 5038 | num: return the number of times that the searchTerm appears in the log |
| 5039 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5040 | """ |
| 5041 | try: |
| 5042 | assert type( searchTerm ) is str |
Chiyu Cheng | e2b48e4 | 2016-11-17 18:11:36 -0800 | [diff] [blame^] | 5043 | #Build the log paths string |
| 5044 | logPath = '/opt/onos/log/karaf.log.' |
| 5045 | logPaths = '/opt/onos/log/karaf.log' |
| 5046 | for i in range( 1, logNum ): |
| 5047 | logPaths = logPath + str( i ) + " " + logPaths |
| 5048 | cmd = "cat " + logPaths |
| 5049 | if mode == 'all': |
| 5050 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5051 | if mode == 'last': |
Chiyu Cheng | e2b48e4 | 2016-11-17 18:11:36 -0800 | [diff] [blame^] | 5052 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5053 | if mode == 'first': |
Chiyu Cheng | e2b48e4 | 2016-11-17 18:11:36 -0800 | [diff] [blame^] | 5054 | if startLine != '': |
| 5055 | # 100000000 is just a extreme large number to make sure this function can grep all the lines after startLine |
| 5056 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\' | grep \'" + searchTerm + "\'" + "| head -n 1" |
| 5057 | else: |
| 5058 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5059 | if mode == 'num': |
Chiyu Cheng | e2b48e4 | 2016-11-17 18:11:36 -0800 | [diff] [blame^] | 5060 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
| 5061 | num = self.sendstartTerm=startTerm1, line( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5062 | return num |
Chiyu Cheng | e2b48e4 | 2016-11-17 18:11:36 -0800 | [diff] [blame^] | 5063 | if mode == 'total': |
| 5064 | totalLines = self.sendline( "cat /opt/onos/log/karaf.log | wc -l" ) |
| 5065 | return int(totalLines) |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5066 | before = self.sendline( cmd ) |
| 5067 | before = before.splitlines() |
| 5068 | # make sure the returned list only contains the search term |
| 5069 | returnLines = [line for line in before if searchTerm in line] |
| 5070 | return returnLines |
| 5071 | except AssertionError: |
| 5072 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5073 | return None |
| 5074 | except pexpect.EOF: |
| 5075 | main.log.error( self.name + ": EOF exception found" ) |
| 5076 | main.log.error( self.name + ": " + self.handle.before ) |
| 5077 | main.cleanup() |
| 5078 | main.exit() |
| 5079 | except pexpect.TIMEOUT: |
| 5080 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5081 | main.log.error( self.name + ": " + self.handle.before ) |
| 5082 | main.cleanup() |
| 5083 | main.exit() |
| 5084 | except Exception: |
| 5085 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5086 | main.cleanup() |
| 5087 | main.exit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5088 | |
| 5089 | def vplsShow( self, jsonFormat=True ): |
| 5090 | """ |
| 5091 | Description: Returns result of onos:vpls show, which should list the |
| 5092 | configured VPLS networks and the assigned interfaces. |
| 5093 | Optional: |
| 5094 | * jsonFormat: enable json formatting of output |
| 5095 | Returns: |
| 5096 | The output of the command or None on error. |
| 5097 | """ |
| 5098 | try: |
| 5099 | cmdStr = "vpls show" |
| 5100 | if jsonFormat: |
| 5101 | raise NotImplementedError |
| 5102 | cmdStr += " -j" |
| 5103 | handle = self.sendline( cmdStr ) |
| 5104 | assert handle is not None, "Error in sendline" |
| 5105 | assert "Command not found:" not in handle, handle |
| 5106 | return handle |
| 5107 | except AssertionError: |
| 5108 | main.log.exception( "" ) |
| 5109 | return None |
| 5110 | except TypeError: |
| 5111 | main.log.exception( self.name + ": Object not as expected" ) |
| 5112 | return None |
| 5113 | except pexpect.EOF: |
| 5114 | main.log.error( self.name + ": EOF exception found" ) |
| 5115 | main.log.error( self.name + ": " + self.handle.before ) |
| 5116 | main.cleanup() |
| 5117 | main.exit() |
| 5118 | except NotImplementedError: |
| 5119 | main.log.exception( self.name + ": Json output not supported") |
| 5120 | return None |
| 5121 | except Exception: |
| 5122 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5123 | main.cleanup() |
| 5124 | main.exit() |
| 5125 | |
| 5126 | def parseVplsShow( self ): |
| 5127 | """ |
| 5128 | Parse the cli output of 'vpls show' into json output. This is required |
| 5129 | as there is currently no json output available. |
| 5130 | """ |
| 5131 | try: |
| 5132 | output = [] |
| 5133 | raw = self.vplsShow( jsonFormat=False ) |
| 5134 | namePat = "VPLS name: (?P<name>\w+)" |
| 5135 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5136 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5137 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5138 | mIter = re.finditer( pattern, raw ) |
| 5139 | for match in mIter: |
| 5140 | item = {} |
| 5141 | item[ 'name' ] = match.group( 'name' ) |
| 5142 | ifaces = match.group( 'interfaces' ).split( ', ') |
| 5143 | if ifaces == [ "" ]: |
| 5144 | ifaces = [] |
| 5145 | item[ 'interfaces' ] = ifaces |
| 5146 | encap = match.group( 'encap' ) |
| 5147 | if encap != 'NONE': |
| 5148 | item[ 'encapsulation' ] = encap.lower() |
| 5149 | output.append( item ) |
| 5150 | return output |
| 5151 | except Exception: |
| 5152 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5153 | main.cleanup() |
| 5154 | main.exit() |
| 5155 | |
| 5156 | def vplsList( self, jsonFormat=True ): |
| 5157 | """ |
| 5158 | Description: Returns result of onos:vpls list, which should list the |
| 5159 | configured VPLS networks. |
| 5160 | Optional: |
| 5161 | * jsonFormat: enable json formatting of output |
| 5162 | """ |
| 5163 | try: |
| 5164 | cmdStr = "vpls list" |
| 5165 | if jsonFormat: |
| 5166 | raise NotImplementedError |
| 5167 | cmdStr += " -j" |
| 5168 | handle = self.sendline( cmdStr ) |
| 5169 | assert handle is not None, "Error in sendline" |
| 5170 | assert "Command not found:" not in handle, handle |
| 5171 | return handle |
| 5172 | except AssertionError: |
| 5173 | main.log.exception( "" ) |
| 5174 | return None |
| 5175 | except TypeError: |
| 5176 | main.log.exception( self.name + ": Object not as expected" ) |
| 5177 | return None |
| 5178 | except pexpect.EOF: |
| 5179 | main.log.error( self.name + ": EOF exception found" ) |
| 5180 | main.log.error( self.name + ": " + self.handle.before ) |
| 5181 | main.cleanup() |
| 5182 | main.exit() |
| 5183 | except NotImplementedError: |
| 5184 | main.log.exception( self.name + ": Json output not supported") |
| 5185 | return None |
| 5186 | except Exception: |
| 5187 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5188 | main.cleanup() |
| 5189 | main.exit() |
| 5190 | |
| 5191 | def vplsCreate( self, network ): |
| 5192 | """ |
| 5193 | CLI command to create a new VPLS network. |
| 5194 | Required arguments: |
| 5195 | network - String name of the network to create. |
| 5196 | returns: |
| 5197 | main.TRUE on success and main.FALSE on failure |
| 5198 | """ |
| 5199 | try: |
| 5200 | network = str( network ) |
| 5201 | cmdStr = "vpls create " |
| 5202 | cmdStr += network |
| 5203 | output = self.sendline( cmdStr ) |
| 5204 | assert output is not None, "Error in sendline" |
| 5205 | assert "Command not found:" not in output, output |
| 5206 | assert "Error executing command" not in output, output |
| 5207 | assert "VPLS already exists:" not in output, output |
| 5208 | return main.TRUE |
| 5209 | except AssertionError: |
| 5210 | main.log.exception( "" ) |
| 5211 | return main.FALSE |
| 5212 | except TypeError: |
| 5213 | main.log.exception( self.name + ": Object not as expected" ) |
| 5214 | return main.FALSE |
| 5215 | except pexpect.EOF: |
| 5216 | main.log.error( self.name + ": EOF exception found" ) |
| 5217 | main.log.error( self.name + ": " + self.handle.before ) |
| 5218 | main.cleanup() |
| 5219 | main.exit() |
| 5220 | except Exception: |
| 5221 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5222 | main.cleanup() |
| 5223 | main.exit() |
| 5224 | |
| 5225 | def vplsDelete( self, network ): |
| 5226 | """ |
| 5227 | CLI command to delete a VPLS network. |
| 5228 | Required arguments: |
| 5229 | network - Name of the network to delete. |
| 5230 | returns: |
| 5231 | main.TRUE on success and main.FALSE on failure |
| 5232 | """ |
| 5233 | try: |
| 5234 | network = str( network ) |
| 5235 | cmdStr = "vpls delete " |
| 5236 | cmdStr += network |
| 5237 | output = self.sendline( cmdStr ) |
| 5238 | assert output is not None, "Error in sendline" |
| 5239 | assert "Command not found:" not in output, output |
| 5240 | assert "Error executing command" not in output, output |
| 5241 | assert " not found" not in output, output |
| 5242 | return main.TRUE |
| 5243 | except AssertionError: |
| 5244 | main.log.exception( "" ) |
| 5245 | return main.FALSE |
| 5246 | except TypeError: |
| 5247 | main.log.exception( self.name + ": Object not as expected" ) |
| 5248 | return main.FALSE |
| 5249 | except pexpect.EOF: |
| 5250 | main.log.error( self.name + ": EOF exception found" ) |
| 5251 | main.log.error( self.name + ": " + self.handle.before ) |
| 5252 | main.cleanup() |
| 5253 | main.exit() |
| 5254 | except Exception: |
| 5255 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5256 | main.cleanup() |
| 5257 | main.exit() |
| 5258 | |
| 5259 | def vplsAddIface( self, network, iface ): |
| 5260 | """ |
| 5261 | CLI command to add an interface to a VPLS network. |
| 5262 | Required arguments: |
| 5263 | network - Name of the network to add the interface to. |
| 5264 | iface - The ONOS name for an interface. |
| 5265 | returns: |
| 5266 | main.TRUE on success and main.FALSE on failure |
| 5267 | """ |
| 5268 | try: |
| 5269 | network = str( network ) |
| 5270 | iface = str( iface ) |
| 5271 | cmdStr = "vpls add-if " |
| 5272 | cmdStr += network + " " + iface |
| 5273 | output = self.sendline( cmdStr ) |
| 5274 | assert output is not None, "Error in sendline" |
| 5275 | assert "Command not found:" not in output, output |
| 5276 | assert "Error executing command" not in output, output |
| 5277 | assert "already associated to network" not in output, output |
| 5278 | assert "Interface cannot be added." not in output, output |
| 5279 | return main.TRUE |
| 5280 | except AssertionError: |
| 5281 | main.log.exception( "" ) |
| 5282 | return main.FALSE |
| 5283 | except TypeError: |
| 5284 | main.log.exception( self.name + ": Object not as expected" ) |
| 5285 | return main.FALSE |
| 5286 | except pexpect.EOF: |
| 5287 | main.log.error( self.name + ": EOF exception found" ) |
| 5288 | main.log.error( self.name + ": " + self.handle.before ) |
| 5289 | main.cleanup() |
| 5290 | main.exit() |
| 5291 | except Exception: |
| 5292 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5293 | main.cleanup() |
| 5294 | main.exit() |
| 5295 | |
| 5296 | def vplsRemIface( self, network, iface ): |
| 5297 | """ |
| 5298 | CLI command to remove an interface from a VPLS network. |
| 5299 | Required arguments: |
| 5300 | network - Name of the network to remove the interface from. |
| 5301 | iface - Name of the interface to remove. |
| 5302 | returns: |
| 5303 | main.TRUE on success and main.FALSE on failure |
| 5304 | """ |
| 5305 | try: |
| 5306 | iface = str( iface ) |
| 5307 | cmdStr = "vpls rem-if " |
| 5308 | cmdStr += network + " " + iface |
| 5309 | output = self.sendline( cmdStr ) |
| 5310 | assert output is not None, "Error in sendline" |
| 5311 | assert "Command not found:" not in output, output |
| 5312 | assert "Error executing command" not in output, output |
| 5313 | assert "is not configured" not in output, output |
| 5314 | return main.TRUE |
| 5315 | except AssertionError: |
| 5316 | main.log.exception( "" ) |
| 5317 | return main.FALSE |
| 5318 | except TypeError: |
| 5319 | main.log.exception( self.name + ": Object not as expected" ) |
| 5320 | return main.FALSE |
| 5321 | except pexpect.EOF: |
| 5322 | main.log.error( self.name + ": EOF exception found" ) |
| 5323 | main.log.error( self.name + ": " + self.handle.before ) |
| 5324 | main.cleanup() |
| 5325 | main.exit() |
| 5326 | except Exception: |
| 5327 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5328 | main.cleanup() |
| 5329 | main.exit() |
| 5330 | |
| 5331 | def vplsClean( self ): |
| 5332 | """ |
| 5333 | Description: Clears the VPLS app configuration. |
| 5334 | Returns: main.TRUE on success and main.FALSE on failure |
| 5335 | """ |
| 5336 | try: |
| 5337 | cmdStr = "vpls clean" |
| 5338 | handle = self.sendline( cmdStr ) |
| 5339 | assert handle is not None, "Error in sendline" |
| 5340 | assert "Command not found:" not in handle, handle |
| 5341 | return handle |
| 5342 | except AssertionError: |
| 5343 | main.log.exception( "" ) |
| 5344 | return main.FALSE |
| 5345 | except TypeError: |
| 5346 | main.log.exception( self.name + ": Object not as expected" ) |
| 5347 | return main.FALSE |
| 5348 | except pexpect.EOF: |
| 5349 | main.log.error( self.name + ": EOF exception found" ) |
| 5350 | main.log.error( self.name + ": " + self.handle.before ) |
| 5351 | main.cleanup() |
| 5352 | main.exit() |
| 5353 | except Exception: |
| 5354 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5355 | main.cleanup() |
| 5356 | main.exit() |
| 5357 | |
| 5358 | def vplsSetEncap( self, network, encapType ): |
| 5359 | """ |
| 5360 | CLI command to add an interface to a VPLS network. |
| 5361 | Required arguments: |
| 5362 | network - Name of the network to create. |
| 5363 | encapType - Type of encapsulation. |
| 5364 | returns: |
| 5365 | main.TRUE on success and main.FALSE on failure |
| 5366 | """ |
| 5367 | try: |
| 5368 | network = str( network ) |
| 5369 | encapType = str( encapType ).upper() |
| 5370 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5371 | cmdStr = "vpls set-encap " |
| 5372 | cmdStr += network + " " + encapType |
| 5373 | output = self.sendline( cmdStr ) |
| 5374 | assert output is not None, "Error in sendline" |
| 5375 | assert "Command not found:" not in output, output |
| 5376 | assert "Error executing command" not in output, output |
| 5377 | assert "already associated to network" not in output, output |
| 5378 | assert "Encapsulation type " not in output, output |
| 5379 | return main.TRUE |
| 5380 | except AssertionError: |
| 5381 | main.log.exception( "" ) |
| 5382 | return main.FALSE |
| 5383 | except TypeError: |
| 5384 | main.log.exception( self.name + ": Object not as expected" ) |
| 5385 | return main.FALSE |
| 5386 | except pexpect.EOF: |
| 5387 | main.log.error( self.name + ": EOF exception found" ) |
| 5388 | main.log.error( self.name + ": " + self.handle.before ) |
| 5389 | main.cleanup() |
| 5390 | main.exit() |
| 5391 | except Exception: |
| 5392 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5393 | main.cleanup() |
| 5394 | main.exit() |
| 5395 | |
| 5396 | def interfaces( self, jsonFormat=True ): |
| 5397 | """ |
| 5398 | Description: Returns result of interfaces command. |
| 5399 | Optional: |
| 5400 | * jsonFormat: enable json formatting of output |
| 5401 | Returns: |
| 5402 | The output of the command or None on error. |
| 5403 | """ |
| 5404 | try: |
| 5405 | cmdStr = "interfaces" |
| 5406 | if jsonFormat: |
| 5407 | #raise NotImplementedError |
| 5408 | cmdStr += " -j" |
| 5409 | handle = self.sendline( cmdStr ) |
| 5410 | assert handle is not None, "Error in sendline" |
| 5411 | assert "Command not found:" not in handle, handle |
| 5412 | return handle |
| 5413 | except AssertionError: |
| 5414 | main.log.exception( "" ) |
| 5415 | return None |
| 5416 | except TypeError: |
| 5417 | main.log.exception( self.name + ": Object not as expected" ) |
| 5418 | return None |
| 5419 | except pexpect.EOF: |
| 5420 | main.log.error( self.name + ": EOF exception found" ) |
| 5421 | main.log.error( self.name + ": " + self.handle.before ) |
| 5422 | main.cleanup() |
| 5423 | main.exit() |
| 5424 | except NotImplementedError: |
| 5425 | main.log.exception( self.name + ": Json output not supported") |
| 5426 | return None |
| 5427 | except Exception: |
| 5428 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5429 | main.cleanup() |
| 5430 | main.exit() |
Chiyu Cheng | e2b48e4 | 2016-11-17 18:11:36 -0800 | [diff] [blame^] | 5431 | |
| 5432 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
| 5433 | ''' |
| 5434 | Get the timestamp of searchTerm from karaf log. |
| 5435 | |
| 5436 | Arguments: |
| 5437 | splitTerm_before and splitTerm_after: |
| 5438 | |
| 5439 | The terms that split the string that contains the timeStamp of |
| 5440 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5441 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5442 | and the splitTerm_after is "x" |
| 5443 | |
| 5444 | others: |
| 5445 | |
| 5446 | plz look at the "logsearch" Function in onosclidriver.py |
| 5447 | |
| 5448 | |
| 5449 | ''' |
| 5450 | if logNum < 0: |
| 5451 | main.log.error("Get wrong log number ") |
| 5452 | return main.ERROR |
| 5453 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
| 5454 | if len(lines) == 0: |
| 5455 | main.log.warn( "Captured timestamp string is empty" ) |
| 5456 | return main.ERROR |
| 5457 | lines = lines[ 0 ] |
| 5458 | try: |
| 5459 | assert type(lines) is str |
| 5460 | # get the target value |
| 5461 | line = lines.split( splitTerm_before ) |
| 5462 | key = line[ 1 ].split( splitTerm_after ) |
| 5463 | return int( key[ 0 ] ) |
| 5464 | except IndexError: |
| 5465 | main.log.warn( "Index Error!" ) |
| 5466 | return main.ERROR |
| 5467 | except AssertionError: |
| 5468 | main.log.warn( "Search Term Not Found " ) |
| 5469 | return main.ERROR |