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 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [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 |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 408 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 409 | """ |
| 410 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 411 | lvlStr = "" |
| 412 | if level: |
| 413 | lvlStr = "--level=" + level |
| 414 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 415 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 416 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 417 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 418 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 419 | response = self.handle.before |
| 420 | if re.search( "Error", response ): |
| 421 | return main.FALSE |
| 422 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 423 | except pexpect.TIMEOUT: |
| 424 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 425 | if noExit: |
| 426 | main.cleanup() |
| 427 | return None |
| 428 | else: |
| 429 | main.cleanup() |
| 430 | main.exit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 431 | except pexpect.EOF: |
| 432 | main.log.error( self.name + ": EOF exception found" ) |
| 433 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 434 | if noExit: |
| 435 | main.cleanup() |
| 436 | return None |
| 437 | else: |
| 438 | main.cleanup() |
| 439 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 440 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 441 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 442 | if noExit: |
| 443 | main.cleanup() |
| 444 | return None |
| 445 | else: |
| 446 | main.cleanup() |
| 447 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 448 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 449 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 450 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 451 | Send a completely user specified string to |
| 452 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 453 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 454 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 455 | if noExit is True, TestON will not exit, and return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 456 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 457 | Warning: There are no sanity checking to commands |
| 458 | sent using this method. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 459 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 460 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 461 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 462 | # Try to reconnect if disconnected from cli |
| 463 | self.handle.sendline( "" ) |
| 464 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ] ) |
| 465 | if i == 1: |
| 466 | main.log.error( self.name + ": onos cli session closed. ") |
| 467 | if self.onosIp: |
| 468 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 469 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 470 | if reconnectResult: |
| 471 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 472 | else: |
| 473 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 474 | if noExit: |
| 475 | return None |
| 476 | else: |
| 477 | main.cleanup() |
| 478 | main.exit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 479 | else: |
| 480 | main.cleanup() |
| 481 | main.exit() |
| 482 | if i == 2: |
| 483 | self.handle.sendline( "" ) |
| 484 | self.handle.expect( "onos>" ) |
| 485 | |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 486 | if debug: |
| 487 | # NOTE: This adds and average of .4 seconds per call |
| 488 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 489 | self.log( logStr,noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 490 | self.handle.sendline( cmdStr ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 491 | i = self.handle.expect( ["onos>", "\$"], timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 492 | response = self.handle.before |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 493 | # TODO: do something with i |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 494 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
| 495 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 496 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 497 | main.log.debug( self.name + ": Raw output" ) |
| 498 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 499 | |
| 500 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 501 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 502 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 503 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 504 | main.log.debug( self.name + ": ansiEscape output" ) |
| 505 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 506 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 507 | # Remove extra return chars that get added |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 508 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 509 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 510 | main.log.debug( self.name + ": Removed extra returns " + |
| 511 | "from output" ) |
| 512 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 513 | |
| 514 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 515 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 516 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 517 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 518 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 519 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 520 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 521 | output = response.split( cmdStr.strip(), 1 ) |
| 522 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 523 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 524 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 525 | main.log.debug( self.name + ": " + repr( r ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 526 | output = output[1].strip() |
| 527 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 528 | main.log.info( "Response from ONOS: {}".format( output ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 529 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 530 | except pexpect.TIMEOUT: |
| 531 | main.log.error( self.name + ":ONOS timeout" ) |
| 532 | if debug: |
| 533 | main.log.debug( self.handle.before ) |
| 534 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 535 | except IndexError: |
| 536 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 537 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 538 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 539 | except TypeError: |
| 540 | main.log.exception( self.name + ": Object not as expected" ) |
| 541 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 542 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 543 | main.log.error( self.name + ": EOF exception found" ) |
| 544 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 545 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 546 | return None |
| 547 | else: |
| 548 | main.cleanup() |
| 549 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 550 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 551 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 552 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 553 | return None |
| 554 | else: |
| 555 | main.cleanup() |
| 556 | main.exit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 557 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 558 | # IMPORTANT NOTE: |
| 559 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 560 | # the cli command changing 'a:b' with 'aB'. |
| 561 | # Ex ) onos:topology > onosTopology |
| 562 | # onos:links > onosLinks |
| 563 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 564 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 565 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 566 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 567 | Adds a new cluster node by ID and address information. |
| 568 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 569 | * nodeId |
| 570 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 571 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 572 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 573 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 574 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 575 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 576 | str( ONOSIp ) + " " + str( tcpPort ) |
| 577 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 578 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 579 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 580 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 581 | main.log.error( "Error in adding node" ) |
| 582 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 583 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 584 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 585 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 586 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 587 | except AssertionError: |
| 588 | main.log.exception( "" ) |
| 589 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 590 | except TypeError: |
| 591 | main.log.exception( self.name + ": Object not as expected" ) |
| 592 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 593 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 594 | main.log.error( self.name + ": EOF exception found" ) |
| 595 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 596 | main.cleanup() |
| 597 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 598 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 599 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 600 | main.cleanup() |
| 601 | main.exit() |
| 602 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 603 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 604 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 605 | Removes a cluster by ID |
| 606 | Issues command: 'remove-node [<node-id>]' |
| 607 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 608 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 609 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 610 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 611 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 612 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 613 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 614 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 615 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 616 | if re.search( "Error", handle ): |
| 617 | main.log.error( "Error in removing node" ) |
| 618 | main.log.error( handle ) |
| 619 | return main.FALSE |
| 620 | else: |
| 621 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 622 | except AssertionError: |
| 623 | main.log.exception( "" ) |
| 624 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 625 | except TypeError: |
| 626 | main.log.exception( self.name + ": Object not as expected" ) |
| 627 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 628 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 629 | main.log.error( self.name + ": EOF exception found" ) |
| 630 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 631 | main.cleanup() |
| 632 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 633 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 634 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 635 | main.cleanup() |
| 636 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 637 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 638 | def nodes( self, jsonFormat=True): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 639 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 640 | List the nodes currently visible |
| 641 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 642 | Optional argument: |
| 643 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 644 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 645 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 646 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 647 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 648 | cmdStr += " -j" |
| 649 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 650 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 651 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 652 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 653 | except AssertionError: |
| 654 | main.log.exception( "" ) |
| 655 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 656 | except TypeError: |
| 657 | main.log.exception( self.name + ": Object not as expected" ) |
| 658 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 659 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 660 | main.log.error( self.name + ": EOF exception found" ) |
| 661 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 662 | main.cleanup() |
| 663 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 664 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 665 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 666 | main.cleanup() |
| 667 | main.exit() |
| 668 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 669 | def topology( self ): |
| 670 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 671 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 672 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 673 | Return: |
| 674 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 675 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 676 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 677 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 678 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 679 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 680 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 681 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 682 | except AssertionError: |
| 683 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 684 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 685 | except TypeError: |
| 686 | main.log.exception( self.name + ": Object not as expected" ) |
| 687 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 688 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 689 | main.log.error( self.name + ": EOF exception found" ) |
| 690 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 691 | main.cleanup() |
| 692 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 693 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 694 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 695 | main.cleanup() |
| 696 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 697 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 698 | def deviceRemove( self, deviceId ): |
| 699 | """ |
| 700 | Removes particular device from storage |
| 701 | |
| 702 | TODO: refactor this function |
| 703 | """ |
| 704 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 705 | cmdStr = "device-remove " + str( deviceId ) |
| 706 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 707 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 708 | if re.search( "Error", handle ): |
| 709 | main.log.error( "Error in removing device" ) |
| 710 | main.log.error( handle ) |
| 711 | return main.FALSE |
| 712 | else: |
| 713 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 714 | except AssertionError: |
| 715 | main.log.exception( "" ) |
| 716 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 717 | except TypeError: |
| 718 | main.log.exception( self.name + ": Object not as expected" ) |
| 719 | return None |
| 720 | except pexpect.EOF: |
| 721 | main.log.error( self.name + ": EOF exception found" ) |
| 722 | main.log.error( self.name + ": " + self.handle.before ) |
| 723 | main.cleanup() |
| 724 | main.exit() |
| 725 | except Exception: |
| 726 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 727 | main.cleanup() |
| 728 | main.exit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 729 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 730 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 731 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 732 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 733 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 734 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 735 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 736 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 737 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 738 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 739 | cmdStr += " -j" |
| 740 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 741 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 742 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 743 | except AssertionError: |
| 744 | main.log.exception( "" ) |
| 745 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 746 | except TypeError: |
| 747 | main.log.exception( self.name + ": Object not as expected" ) |
| 748 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 749 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 750 | main.log.error( self.name + ": EOF exception found" ) |
| 751 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 752 | main.cleanup() |
| 753 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 754 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 755 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 756 | main.cleanup() |
| 757 | main.exit() |
| 758 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 759 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 760 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 761 | This balances the devices across all controllers |
| 762 | by issuing command: 'onos> onos:balance-masters' |
| 763 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 764 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 765 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 766 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 767 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 768 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 769 | if re.search( "Error", handle ): |
| 770 | main.log.error( "Error in balancing masters" ) |
| 771 | main.log.error( handle ) |
| 772 | return main.FALSE |
| 773 | else: |
| 774 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 775 | except AssertionError: |
| 776 | main.log.exception( "" ) |
| 777 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 778 | except TypeError: |
| 779 | main.log.exception( self.name + ": Object not as expected" ) |
| 780 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 781 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 782 | main.log.error( self.name + ": EOF exception found" ) |
| 783 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 784 | main.cleanup() |
| 785 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 786 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 787 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 788 | main.cleanup() |
| 789 | main.exit() |
| 790 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 791 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 792 | """ |
| 793 | Returns the output of the masters command. |
| 794 | Optional argument: |
| 795 | * jsonFormat - boolean indicating if you want output in json |
| 796 | """ |
| 797 | try: |
| 798 | cmdStr = "onos:masters" |
| 799 | if jsonFormat: |
| 800 | cmdStr += " -j" |
| 801 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 802 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 803 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 804 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 805 | except AssertionError: |
| 806 | main.log.exception( "" ) |
| 807 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 808 | except TypeError: |
| 809 | main.log.exception( self.name + ": Object not as expected" ) |
| 810 | return None |
| 811 | except pexpect.EOF: |
| 812 | main.log.error( self.name + ": EOF exception found" ) |
| 813 | main.log.error( self.name + ": " + self.handle.before ) |
| 814 | main.cleanup() |
| 815 | main.exit() |
| 816 | except Exception: |
| 817 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 818 | main.cleanup() |
| 819 | main.exit() |
| 820 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 821 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 822 | """ |
| 823 | Uses the master command to check that the devices' leadership |
| 824 | is evenly divided |
| 825 | |
| 826 | Dependencies: checkMasters() and summary() |
| 827 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 828 | Returns main.TRUE if the devices are balanced |
| 829 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 830 | Exits on Exception |
| 831 | Returns None on TypeError |
| 832 | """ |
| 833 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 834 | summaryOutput = self.summary() |
| 835 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 836 | except ( TypeError, ValueError ): |
| 837 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 838 | return None |
| 839 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 840 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 841 | mastersOutput = self.checkMasters() |
| 842 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 843 | first = masters[ 0 ][ "size" ] |
| 844 | for master in masters: |
| 845 | totalOwnedDevices += master[ "size" ] |
| 846 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 847 | main.log.error( "Mastership not balanced" ) |
| 848 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 849 | return main.FALSE |
| 850 | main.log.info( "Mastership balanced between " \ |
| 851 | + str( len(masters) ) + " masters" ) |
| 852 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 853 | except ( TypeError, ValueError ): |
| 854 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 855 | return None |
| 856 | except pexpect.EOF: |
| 857 | main.log.error( self.name + ": EOF exception found" ) |
| 858 | main.log.error( self.name + ": " + self.handle.before ) |
| 859 | main.cleanup() |
| 860 | main.exit() |
| 861 | except Exception: |
| 862 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 863 | main.cleanup() |
| 864 | main.exit() |
| 865 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 866 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 867 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 868 | Lists all core links |
| 869 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 870 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 871 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 872 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 873 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 874 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 875 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 876 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 877 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 878 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 879 | except AssertionError: |
| 880 | main.log.exception( "" ) |
| 881 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 882 | except TypeError: |
| 883 | main.log.exception( self.name + ": Object not as expected" ) |
| 884 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 885 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 886 | main.log.error( self.name + ": EOF exception found" ) |
| 887 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 888 | main.cleanup() |
| 889 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 890 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 891 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 892 | main.cleanup() |
| 893 | main.exit() |
| 894 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 895 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 896 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 897 | Lists all ports |
| 898 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 899 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 900 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 901 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 902 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 903 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 904 | cmdStr += " -j" |
| 905 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 906 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 907 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 908 | except AssertionError: |
| 909 | main.log.exception( "" ) |
| 910 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 911 | except TypeError: |
| 912 | main.log.exception( self.name + ": Object not as expected" ) |
| 913 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 914 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 915 | main.log.error( self.name + ": EOF exception found" ) |
| 916 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 917 | main.cleanup() |
| 918 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 919 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 920 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 921 | main.cleanup() |
| 922 | main.exit() |
| 923 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 924 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 925 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 926 | Lists all devices and the controllers with roles assigned to them |
| 927 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 928 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 929 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 930 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 931 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 932 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 933 | cmdStr += " -j" |
| 934 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 935 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 936 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 937 | except AssertionError: |
| 938 | main.log.exception( "" ) |
| 939 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 940 | except TypeError: |
| 941 | main.log.exception( self.name + ": Object not as expected" ) |
| 942 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 943 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 944 | main.log.error( self.name + ": EOF exception found" ) |
| 945 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 946 | main.cleanup() |
| 947 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 948 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 949 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 950 | main.cleanup() |
| 951 | main.exit() |
| 952 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 953 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 954 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 955 | Given the a string containing the json representation of the "roles" |
| 956 | cli command and a partial or whole device id, returns a json object |
| 957 | containing the roles output for the first device whose id contains |
| 958 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 959 | |
| 960 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 961 | A dict of the role assignments for the given device or |
| 962 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 963 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 964 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 965 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 966 | return None |
| 967 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 968 | rawRoles = self.roles() |
| 969 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 970 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 971 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 972 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 973 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 974 | return device |
| 975 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 976 | except ( TypeError, ValueError ): |
| 977 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 978 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 979 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 980 | main.log.error( self.name + ": EOF exception found" ) |
| 981 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 982 | main.cleanup() |
| 983 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 984 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 985 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 986 | main.cleanup() |
| 987 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 988 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 989 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 990 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 991 | Iterates through each device and checks if there is a master assigned |
| 992 | Returns: main.TRUE if each device has a master |
| 993 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 994 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 995 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 996 | rawRoles = self.roles() |
| 997 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 998 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 999 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1000 | # print device |
| 1001 | if device[ 'master' ] == "none": |
| 1002 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1003 | return main.FALSE |
| 1004 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1005 | except ( TypeError, ValueError ): |
| 1006 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1007 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1008 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1009 | main.log.error( self.name + ": EOF exception found" ) |
| 1010 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1011 | main.cleanup() |
| 1012 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1013 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1014 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1015 | main.cleanup() |
| 1016 | main.exit() |
| 1017 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1018 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1019 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1020 | Returns string of paths, and the cost. |
| 1021 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1022 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1023 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1024 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1025 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1026 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1027 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1028 | main.log.error( "Error in getting paths" ) |
| 1029 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1030 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1031 | path = handle.split( ";" )[ 0 ] |
| 1032 | cost = handle.split( ";" )[ 1 ] |
| 1033 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1034 | except AssertionError: |
| 1035 | main.log.exception( "" ) |
| 1036 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1037 | except TypeError: |
| 1038 | main.log.exception( self.name + ": Object not as expected" ) |
| 1039 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1040 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1041 | main.log.error( self.name + ": EOF exception found" ) |
| 1042 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1043 | main.cleanup() |
| 1044 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1045 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1046 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1047 | main.cleanup() |
| 1048 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1049 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1050 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1051 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1052 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1053 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1054 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1055 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1056 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1057 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1058 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1059 | cmdStr += " -j" |
| 1060 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1061 | if handle: |
| 1062 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1063 | # TODO: Maybe make this less hardcoded |
| 1064 | # ConsistentMap Exceptions |
| 1065 | assert "org.onosproject.store.service" not in handle |
| 1066 | # Node not leader |
| 1067 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1068 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1069 | except AssertionError: |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1070 | main.log.exception( "Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1071 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1072 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1073 | except TypeError: |
| 1074 | main.log.exception( self.name + ": Object not as expected" ) |
| 1075 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1076 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1077 | main.log.error( self.name + ": EOF exception found" ) |
| 1078 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1079 | main.cleanup() |
| 1080 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1081 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1082 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1083 | main.cleanup() |
| 1084 | main.exit() |
| 1085 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1086 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1087 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1088 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1089 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1090 | 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] | 1091 | partial mac address |
| 1092 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1093 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1094 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1095 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1096 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1097 | return None |
| 1098 | else: |
| 1099 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1100 | rawHosts = self.hosts() |
| 1101 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1102 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1103 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1104 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1105 | if not host: |
| 1106 | pass |
| 1107 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1108 | return host |
| 1109 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1110 | except ( TypeError, ValueError ): |
| 1111 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1112 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1113 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1114 | main.log.error( self.name + ": EOF exception found" ) |
| 1115 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1116 | main.cleanup() |
| 1117 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1118 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1119 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1120 | main.cleanup() |
| 1121 | main.exit() |
| 1122 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1123 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1124 | """ |
| 1125 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1126 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1127 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1128 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1129 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1130 | IMPORTANT: |
| 1131 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1132 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1133 | Furthermore, it assumes that value of VLAN is '-1' |
| 1134 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1135 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1136 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1137 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1138 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1139 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1140 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1141 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1142 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1143 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1144 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1145 | i = iter( str( hostHex ) ) |
| 1146 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1147 | hostHex = hostHex + "/-1" |
| 1148 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1149 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1150 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1151 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1152 | except TypeError: |
| 1153 | main.log.exception( self.name + ": Object not as expected" ) |
| 1154 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1155 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1156 | main.log.error( self.name + ": EOF exception found" ) |
| 1157 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1158 | main.cleanup() |
| 1159 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1160 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1161 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1162 | main.cleanup() |
| 1163 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1164 | |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1165 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1166 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1167 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1168 | * hostIdOne: ONOS host id for host1 |
| 1169 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1170 | Optional: |
| 1171 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1172 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1173 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1174 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1175 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1176 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1177 | Returns: |
| 1178 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1179 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1180 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1181 | cmdStr = "add-host-intent " |
| 1182 | if vlanId: |
| 1183 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1184 | if setVlan: |
| 1185 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1186 | if encap: |
| 1187 | cmdStr += "--encapsulation " + str( encap ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1188 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1189 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1190 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1191 | if re.search( "Error", handle ): |
| 1192 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1193 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1194 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1195 | else: |
| 1196 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1197 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
| 1198 | match = re.search('id=0x([\da-f]+),', handle) |
| 1199 | if match: |
| 1200 | return match.group()[3:-1] |
| 1201 | else: |
| 1202 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1203 | main.log.debug( "Response from ONOS was: " + |
| 1204 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1205 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1206 | except AssertionError: |
| 1207 | main.log.exception( "" ) |
| 1208 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1209 | except TypeError: |
| 1210 | main.log.exception( self.name + ": Object not as expected" ) |
| 1211 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1212 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1213 | main.log.error( self.name + ": EOF exception found" ) |
| 1214 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1215 | main.cleanup() |
| 1216 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1217 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1218 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1219 | main.cleanup() |
| 1220 | main.exit() |
| 1221 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1222 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1223 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1224 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1225 | * ingressDevice: device id of ingress device |
| 1226 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1227 | Optional: |
| 1228 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1229 | Description: |
| 1230 | Adds an optical intent by specifying an ingress and egress device |
| 1231 | Returns: |
| 1232 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1233 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1234 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1235 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1236 | " " + str( egressDevice ) |
| 1237 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1238 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1239 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1240 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1241 | main.log.error( "Error in adding Optical intent" ) |
| 1242 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1243 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1244 | main.log.info( "Optical intent installed between " + |
| 1245 | str( ingressDevice ) + " and " + |
| 1246 | str( egressDevice ) ) |
| 1247 | match = re.search('id=0x([\da-f]+),', handle) |
| 1248 | if match: |
| 1249 | return match.group()[3:-1] |
| 1250 | else: |
| 1251 | main.log.error( "Error, intent ID not found" ) |
| 1252 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1253 | except AssertionError: |
| 1254 | main.log.exception( "" ) |
| 1255 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1256 | except TypeError: |
| 1257 | main.log.exception( self.name + ": Object not as expected" ) |
| 1258 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1259 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1260 | main.log.error( self.name + ": EOF exception found" ) |
| 1261 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1262 | main.cleanup() |
| 1263 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1264 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1265 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1266 | main.cleanup() |
| 1267 | main.exit() |
| 1268 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1269 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1270 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1271 | ingressDevice, |
| 1272 | egressDevice, |
| 1273 | portIngress="", |
| 1274 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1275 | ethType="", |
| 1276 | ethSrc="", |
| 1277 | ethDst="", |
| 1278 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1279 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1280 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1281 | ipProto="", |
| 1282 | ipSrc="", |
| 1283 | ipDst="", |
| 1284 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1285 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1286 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1287 | setVlan="", |
| 1288 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1289 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1290 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1291 | * ingressDevice: device id of ingress device |
| 1292 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1293 | Optional: |
| 1294 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1295 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1296 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1297 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1298 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1299 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1300 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1301 | * ipSrc: specify ip source address |
| 1302 | * ipDst: specify ip destination address |
| 1303 | * tcpSrc: specify tcp source port |
| 1304 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1305 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1306 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1307 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1308 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1309 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1310 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1311 | Returns: |
| 1312 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1313 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1314 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1315 | options developers provide for point-to-point |
| 1316 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1317 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1318 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1319 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1320 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1321 | if ethType: |
| 1322 | cmd += " --ethType " + str( ethType ) |
| 1323 | if ethSrc: |
| 1324 | cmd += " --ethSrc " + str( ethSrc ) |
| 1325 | if ethDst: |
| 1326 | cmd += " --ethDst " + str( ethDst ) |
| 1327 | if bandwidth: |
| 1328 | cmd += " --bandwidth " + str( bandwidth ) |
| 1329 | if lambdaAlloc: |
| 1330 | cmd += " --lambda " |
| 1331 | if ipProto: |
| 1332 | cmd += " --ipProto " + str( ipProto ) |
| 1333 | if ipSrc: |
| 1334 | cmd += " --ipSrc " + str( ipSrc ) |
| 1335 | if ipDst: |
| 1336 | cmd += " --ipDst " + str( ipDst ) |
| 1337 | if tcpSrc: |
| 1338 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1339 | if tcpDst: |
| 1340 | cmd += " --tcpDst " + str( tcpDst ) |
| 1341 | if vlanId: |
| 1342 | cmd += " -v " + str( vlanId ) |
| 1343 | if setVlan: |
| 1344 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1345 | if encap: |
| 1346 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1347 | if protected: |
| 1348 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1349 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1350 | # Check whether the user appended the port |
| 1351 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1352 | if "/" in ingressDevice: |
| 1353 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1354 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1355 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1356 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1357 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1358 | # Would it make sense to throw an exception and exit |
| 1359 | # the test? |
| 1360 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1361 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1362 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1363 | str( ingressDevice ) + "/" +\ |
| 1364 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1365 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1366 | if "/" in egressDevice: |
| 1367 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1368 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1369 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1370 | main.log.error( "You must specify the egress port" ) |
| 1371 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1372 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1373 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1374 | str( egressDevice ) + "/" +\ |
| 1375 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1376 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1377 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1378 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1379 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1380 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1381 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1382 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1383 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1384 | # TODO: print out all the options in this message? |
| 1385 | main.log.info( "Point-to-point intent installed between " + |
| 1386 | str( ingressDevice ) + " and " + |
| 1387 | str( egressDevice ) ) |
| 1388 | match = re.search('id=0x([\da-f]+),', handle) |
| 1389 | if match: |
| 1390 | return match.group()[3:-1] |
| 1391 | else: |
| 1392 | main.log.error( "Error, intent ID not found" ) |
| 1393 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1394 | except AssertionError: |
| 1395 | main.log.exception( "" ) |
| 1396 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1397 | except TypeError: |
| 1398 | main.log.exception( self.name + ": Object not as expected" ) |
| 1399 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1400 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1401 | main.log.error( self.name + ": EOF exception found" ) |
| 1402 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1403 | main.cleanup() |
| 1404 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1405 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1406 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1407 | main.cleanup() |
| 1408 | main.exit() |
| 1409 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1410 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1411 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1412 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1413 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1414 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1415 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1416 | ethType="", |
| 1417 | ethSrc="", |
| 1418 | ethDst="", |
| 1419 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1420 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1421 | ipProto="", |
| 1422 | ipSrc="", |
| 1423 | ipDst="", |
| 1424 | tcpSrc="", |
| 1425 | tcpDst="", |
| 1426 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1427 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1428 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1429 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1430 | partial=False, |
| 1431 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1432 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1433 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1434 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1435 | is same. That is, all ingress devices include port numbers |
| 1436 | with a "/" or all ingress devices could specify device |
| 1437 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1438 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1439 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1440 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1441 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1442 | Optional: |
| 1443 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1444 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1445 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1446 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1447 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1448 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1449 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1450 | * ipSrc: specify ip source address |
| 1451 | * ipDst: specify ip destination address |
| 1452 | * tcpSrc: specify tcp source port |
| 1453 | * tcpDst: specify tcp destination port |
| 1454 | * setEthSrc: action to Rewrite Source MAC Address |
| 1455 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1456 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1457 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1458 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1459 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1460 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1461 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1462 | Returns: |
| 1463 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1464 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1465 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1466 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1467 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1468 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1469 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1470 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1471 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1472 | if ethType: |
| 1473 | cmd += " --ethType " + str( ethType ) |
| 1474 | if ethSrc: |
| 1475 | cmd += " --ethSrc " + str( ethSrc ) |
| 1476 | if ethDst: |
| 1477 | cmd += " --ethDst " + str( ethDst ) |
| 1478 | if bandwidth: |
| 1479 | cmd += " --bandwidth " + str( bandwidth ) |
| 1480 | if lambdaAlloc: |
| 1481 | cmd += " --lambda " |
| 1482 | if ipProto: |
| 1483 | cmd += " --ipProto " + str( ipProto ) |
| 1484 | if ipSrc: |
| 1485 | cmd += " --ipSrc " + str( ipSrc ) |
| 1486 | if ipDst: |
| 1487 | cmd += " --ipDst " + str( ipDst ) |
| 1488 | if tcpSrc: |
| 1489 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1490 | if tcpDst: |
| 1491 | cmd += " --tcpDst " + str( tcpDst ) |
| 1492 | if setEthSrc: |
| 1493 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1494 | if setEthDst: |
| 1495 | cmd += " --setEthDst " + str( setEthDst ) |
| 1496 | if vlanId: |
| 1497 | cmd += " -v " + str( vlanId ) |
| 1498 | if setVlan: |
| 1499 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1500 | if partial: |
| 1501 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1502 | if encap: |
| 1503 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1504 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1505 | # Check whether the user appended the port |
| 1506 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1507 | |
| 1508 | if portIngressList is None: |
| 1509 | for ingressDevice in ingressDeviceList: |
| 1510 | if "/" in ingressDevice: |
| 1511 | cmd += " " + str( ingressDevice ) |
| 1512 | else: |
| 1513 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1514 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1515 | # TODO: perhaps more meaningful return |
| 1516 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1517 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1518 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1519 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1520 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1521 | cmd += " " + \ |
| 1522 | str( ingressDevice ) + "/" +\ |
| 1523 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1524 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1525 | main.log.error( "Device list and port list does not " + |
| 1526 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1527 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1528 | if "/" in egressDevice: |
| 1529 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1530 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1531 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1532 | main.log.error( "You must specify " + |
| 1533 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1534 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1535 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1536 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1537 | str( egressDevice ) + "/" +\ |
| 1538 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1539 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1540 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1541 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1542 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1543 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1544 | "intent" ) |
| 1545 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1546 | else: |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1547 | match = re.search('id=0x([\da-f]+),', handle) |
| 1548 | if match: |
| 1549 | return match.group()[3:-1] |
| 1550 | else: |
| 1551 | main.log.error( "Error, intent ID not found" ) |
| 1552 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1553 | except AssertionError: |
| 1554 | main.log.exception( "" ) |
| 1555 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1556 | except TypeError: |
| 1557 | main.log.exception( self.name + ": Object not as expected" ) |
| 1558 | return None |
| 1559 | except pexpect.EOF: |
| 1560 | main.log.error( self.name + ": EOF exception found" ) |
| 1561 | main.log.error( self.name + ": " + self.handle.before ) |
| 1562 | main.cleanup() |
| 1563 | main.exit() |
| 1564 | except Exception: |
| 1565 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1566 | main.cleanup() |
| 1567 | main.exit() |
| 1568 | |
| 1569 | def addSinglepointToMultipointIntent( |
| 1570 | self, |
| 1571 | ingressDevice, |
| 1572 | egressDeviceList, |
| 1573 | portIngress="", |
| 1574 | portEgressList=None, |
| 1575 | ethType="", |
| 1576 | ethSrc="", |
| 1577 | ethDst="", |
| 1578 | bandwidth="", |
| 1579 | lambdaAlloc=False, |
| 1580 | ipProto="", |
| 1581 | ipSrc="", |
| 1582 | ipDst="", |
| 1583 | tcpSrc="", |
| 1584 | tcpDst="", |
| 1585 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1586 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1587 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1588 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1589 | partial=False, |
| 1590 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1591 | """ |
| 1592 | Note: |
| 1593 | This function assumes the format of all egress devices |
| 1594 | is same. That is, all egress devices include port numbers |
| 1595 | with a "/" or all egress devices could specify device |
| 1596 | ids and port numbers seperately. |
| 1597 | Required: |
| 1598 | * EgressDeviceList: List of device ids of egress device |
| 1599 | ( Atleast 2 eress devices required in the list ) |
| 1600 | * ingressDevice: device id of ingress device |
| 1601 | Optional: |
| 1602 | * ethType: specify ethType |
| 1603 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1604 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1605 | * bandwidth: specify bandwidth capacity of link |
| 1606 | * lambdaAlloc: if True, intent will allocate lambda |
| 1607 | for the specified intent |
| 1608 | * ipProto: specify ip protocol |
| 1609 | * ipSrc: specify ip source address |
| 1610 | * ipDst: specify ip destination address |
| 1611 | * tcpSrc: specify tcp source port |
| 1612 | * tcpDst: specify tcp destination port |
| 1613 | * setEthSrc: action to Rewrite Source MAC Address |
| 1614 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1615 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1616 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1617 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1618 | Description: |
| 1619 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1620 | specifying device id's and optional fields |
| 1621 | Returns: |
| 1622 | A string of the intent id or None on error |
| 1623 | |
| 1624 | NOTE: This function may change depending on the |
| 1625 | options developers provide for singlepoint-to-multipoint |
| 1626 | intent via cli |
| 1627 | """ |
| 1628 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1629 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1630 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1631 | if ethType: |
| 1632 | cmd += " --ethType " + str( ethType ) |
| 1633 | if ethSrc: |
| 1634 | cmd += " --ethSrc " + str( ethSrc ) |
| 1635 | if ethDst: |
| 1636 | cmd += " --ethDst " + str( ethDst ) |
| 1637 | if bandwidth: |
| 1638 | cmd += " --bandwidth " + str( bandwidth ) |
| 1639 | if lambdaAlloc: |
| 1640 | cmd += " --lambda " |
| 1641 | if ipProto: |
| 1642 | cmd += " --ipProto " + str( ipProto ) |
| 1643 | if ipSrc: |
| 1644 | cmd += " --ipSrc " + str( ipSrc ) |
| 1645 | if ipDst: |
| 1646 | cmd += " --ipDst " + str( ipDst ) |
| 1647 | if tcpSrc: |
| 1648 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1649 | if tcpDst: |
| 1650 | cmd += " --tcpDst " + str( tcpDst ) |
| 1651 | if setEthSrc: |
| 1652 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1653 | if setEthDst: |
| 1654 | cmd += " --setEthDst " + str( setEthDst ) |
| 1655 | if vlanId: |
| 1656 | cmd += " -v " + str( vlanId ) |
| 1657 | if setVlan: |
| 1658 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1659 | if partial: |
| 1660 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1661 | if encap: |
| 1662 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1663 | |
| 1664 | # Check whether the user appended the port |
| 1665 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1666 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1667 | if "/" in ingressDevice: |
| 1668 | cmd += " " + str( ingressDevice ) |
| 1669 | else: |
| 1670 | if not portIngress: |
| 1671 | main.log.error( "You must specify " + |
| 1672 | "the Ingress port" ) |
| 1673 | return main.FALSE |
| 1674 | |
| 1675 | cmd += " " +\ |
| 1676 | str( ingressDevice ) + "/" +\ |
| 1677 | str( portIngress ) |
| 1678 | |
| 1679 | if portEgressList is None: |
| 1680 | for egressDevice in egressDeviceList: |
| 1681 | if "/" in egressDevice: |
| 1682 | cmd += " " + str( egressDevice ) |
| 1683 | else: |
| 1684 | main.log.error( "You must specify " + |
| 1685 | "the egress port" ) |
| 1686 | # TODO: perhaps more meaningful return |
| 1687 | return main.FALSE |
| 1688 | else: |
| 1689 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1690 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1691 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1692 | cmd += " " + \ |
| 1693 | str( egressDevice ) + "/" +\ |
| 1694 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1695 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1696 | main.log.error( "Device list and port list does not " + |
| 1697 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1698 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1699 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1700 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1701 | # If error, return error message |
| 1702 | if re.search( "Error", handle ): |
| 1703 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1704 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1705 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1706 | else: |
| 1707 | match = re.search('id=0x([\da-f]+),', handle) |
| 1708 | if match: |
| 1709 | return match.group()[3:-1] |
| 1710 | else: |
| 1711 | main.log.error( "Error, intent ID not found" ) |
| 1712 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1713 | except AssertionError: |
| 1714 | main.log.exception( "" ) |
| 1715 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1716 | except TypeError: |
| 1717 | main.log.exception( self.name + ": Object not as expected" ) |
| 1718 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1719 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1720 | main.log.error( self.name + ": EOF exception found" ) |
| 1721 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1722 | main.cleanup() |
| 1723 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1724 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1725 | main.log.exception( self.name + ": Uncaught exception!" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1726 | main.cleanup() |
| 1727 | main.exit() |
| 1728 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1729 | def addMplsIntent( |
| 1730 | self, |
| 1731 | ingressDevice, |
| 1732 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1733 | ingressPort="", |
| 1734 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1735 | ethType="", |
| 1736 | ethSrc="", |
| 1737 | ethDst="", |
| 1738 | bandwidth="", |
| 1739 | lambdaAlloc=False, |
| 1740 | ipProto="", |
| 1741 | ipSrc="", |
| 1742 | ipDst="", |
| 1743 | tcpSrc="", |
| 1744 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1745 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1746 | egressLabel="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1747 | priority=""): |
| 1748 | """ |
| 1749 | Required: |
| 1750 | * ingressDevice: device id of ingress device |
| 1751 | * egressDevice: device id of egress device |
| 1752 | Optional: |
| 1753 | * ethType: specify ethType |
| 1754 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1755 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1756 | * bandwidth: specify bandwidth capacity of link |
| 1757 | * lambdaAlloc: if True, intent will allocate lambda |
| 1758 | for the specified intent |
| 1759 | * ipProto: specify ip protocol |
| 1760 | * ipSrc: specify ip source address |
| 1761 | * ipDst: specify ip destination address |
| 1762 | * tcpSrc: specify tcp source port |
| 1763 | * tcpDst: specify tcp destination port |
| 1764 | * ingressLabel: Ingress MPLS label |
| 1765 | * egressLabel: Egress MPLS label |
| 1766 | Description: |
| 1767 | Adds MPLS intent by |
| 1768 | specifying device id's and optional fields |
| 1769 | Returns: |
| 1770 | A string of the intent id or None on error |
| 1771 | |
| 1772 | NOTE: This function may change depending on the |
| 1773 | options developers provide for MPLS |
| 1774 | intent via cli |
| 1775 | """ |
| 1776 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1777 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1778 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1779 | if ethType: |
| 1780 | cmd += " --ethType " + str( ethType ) |
| 1781 | if ethSrc: |
| 1782 | cmd += " --ethSrc " + str( ethSrc ) |
| 1783 | if ethDst: |
| 1784 | cmd += " --ethDst " + str( ethDst ) |
| 1785 | if bandwidth: |
| 1786 | cmd += " --bandwidth " + str( bandwidth ) |
| 1787 | if lambdaAlloc: |
| 1788 | cmd += " --lambda " |
| 1789 | if ipProto: |
| 1790 | cmd += " --ipProto " + str( ipProto ) |
| 1791 | if ipSrc: |
| 1792 | cmd += " --ipSrc " + str( ipSrc ) |
| 1793 | if ipDst: |
| 1794 | cmd += " --ipDst " + str( ipDst ) |
| 1795 | if tcpSrc: |
| 1796 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1797 | if tcpDst: |
| 1798 | cmd += " --tcpDst " + str( tcpDst ) |
| 1799 | if ingressLabel: |
| 1800 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1801 | if egressLabel: |
| 1802 | cmd += " --egressLabel " + str( egressLabel ) |
| 1803 | if priority: |
| 1804 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1805 | |
| 1806 | # Check whether the user appended the port |
| 1807 | # or provided it as an input |
| 1808 | if "/" in ingressDevice: |
| 1809 | cmd += " " + str( ingressDevice ) |
| 1810 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1811 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1812 | main.log.error( "You must specify the ingress port" ) |
| 1813 | return None |
| 1814 | |
| 1815 | cmd += " " + \ |
| 1816 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1817 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1818 | |
| 1819 | if "/" in egressDevice: |
| 1820 | cmd += " " + str( egressDevice ) |
| 1821 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1822 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1823 | main.log.error( "You must specify the egress port" ) |
| 1824 | return None |
| 1825 | |
| 1826 | cmd += " " +\ |
| 1827 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1828 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1829 | |
| 1830 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1831 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1832 | # If error, return error message |
| 1833 | if re.search( "Error", handle ): |
| 1834 | main.log.error( "Error in adding mpls intent" ) |
| 1835 | return None |
| 1836 | else: |
| 1837 | # TODO: print out all the options in this message? |
| 1838 | main.log.info( "MPLS intent installed between " + |
| 1839 | str( ingressDevice ) + " and " + |
| 1840 | str( egressDevice ) ) |
| 1841 | match = re.search('id=0x([\da-f]+),', handle) |
| 1842 | if match: |
| 1843 | return match.group()[3:-1] |
| 1844 | else: |
| 1845 | main.log.error( "Error, intent ID not found" ) |
| 1846 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1847 | except AssertionError: |
| 1848 | main.log.exception( "" ) |
| 1849 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1850 | except TypeError: |
| 1851 | main.log.exception( self.name + ": Object not as expected" ) |
| 1852 | return None |
| 1853 | except pexpect.EOF: |
| 1854 | main.log.error( self.name + ": EOF exception found" ) |
| 1855 | main.log.error( self.name + ": " + self.handle.before ) |
| 1856 | main.cleanup() |
| 1857 | main.exit() |
| 1858 | except Exception: |
| 1859 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1860 | main.cleanup() |
| 1861 | main.exit() |
| 1862 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1863 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1864 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1865 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1866 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1867 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1868 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1869 | -p or --purge: Purge the intent from the store after removal |
| 1870 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1871 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 1872 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1873 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1874 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1875 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1876 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1877 | if purge: |
| 1878 | cmdStr += " -p" |
| 1879 | if sync: |
| 1880 | cmdStr += " -s" |
| 1881 | |
| 1882 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1883 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1884 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1885 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1886 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1887 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1888 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1889 | # TODO: Should this be main.TRUE |
| 1890 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1891 | except AssertionError: |
| 1892 | main.log.exception( "" ) |
| 1893 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1894 | except TypeError: |
| 1895 | main.log.exception( self.name + ": Object not as expected" ) |
| 1896 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1897 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1898 | main.log.error( self.name + ": EOF exception found" ) |
| 1899 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1900 | main.cleanup() |
| 1901 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1902 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1903 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1904 | main.cleanup() |
| 1905 | main.exit() |
| 1906 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1907 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1908 | """ |
| 1909 | Description: |
| 1910 | Remove all the intents |
| 1911 | Optional args:- |
| 1912 | -s or --sync: Waits for the removal before returning |
| 1913 | -p or --purge: Purge the intent from the store after removal |
| 1914 | Returns: |
| 1915 | Returns main.TRUE if all intents are removed, otherwise returns |
| 1916 | main.FALSE; Returns None for exception |
| 1917 | """ |
| 1918 | try: |
| 1919 | cmdStr = "remove-intent" |
| 1920 | if purge: |
| 1921 | cmdStr += " -p" |
| 1922 | if sync: |
| 1923 | cmdStr += " -s" |
| 1924 | |
| 1925 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1926 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1927 | assert "Command not found:" not in handle, handle |
| 1928 | if re.search( "Error", handle ): |
| 1929 | main.log.error( "Error in removing intent" ) |
| 1930 | return main.FALSE |
| 1931 | else: |
| 1932 | return main.TRUE |
| 1933 | except AssertionError: |
| 1934 | main.log.exception( "" ) |
| 1935 | return None |
| 1936 | except TypeError: |
| 1937 | main.log.exception( self.name + ": Object not as expected" ) |
| 1938 | return None |
| 1939 | except pexpect.EOF: |
| 1940 | main.log.error( self.name + ": EOF exception found" ) |
| 1941 | main.log.error( self.name + ": " + self.handle.before ) |
| 1942 | main.cleanup() |
| 1943 | main.exit() |
| 1944 | except Exception: |
| 1945 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1946 | main.cleanup() |
| 1947 | main.exit() |
| 1948 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 1949 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1950 | """ |
| 1951 | Purges all WITHDRAWN Intents |
| 1952 | """ |
| 1953 | try: |
| 1954 | cmdStr = "purge-intents" |
| 1955 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1956 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1957 | if re.search( "Error", handle ): |
| 1958 | main.log.error( "Error in purging intents" ) |
| 1959 | return main.FALSE |
| 1960 | else: |
| 1961 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1962 | except AssertionError: |
| 1963 | main.log.exception( "" ) |
| 1964 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1965 | except TypeError: |
| 1966 | main.log.exception( self.name + ": Object not as expected" ) |
| 1967 | return None |
| 1968 | except pexpect.EOF: |
| 1969 | main.log.error( self.name + ": EOF exception found" ) |
| 1970 | main.log.error( self.name + ": " + self.handle.before ) |
| 1971 | main.cleanup() |
| 1972 | main.exit() |
| 1973 | except Exception: |
| 1974 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1975 | main.cleanup() |
| 1976 | main.exit() |
| 1977 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1978 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1979 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1980 | NOTE: This method should be used after installing application: |
| 1981 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1982 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1983 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1984 | Description: |
| 1985 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1986 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1987 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1988 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1989 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1990 | cmdStr += " -j" |
| 1991 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1992 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1993 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1994 | except AssertionError: |
| 1995 | main.log.exception( "" ) |
| 1996 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1997 | except TypeError: |
| 1998 | main.log.exception( self.name + ": Object not as expected" ) |
| 1999 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2000 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2001 | main.log.error( self.name + ": EOF exception found" ) |
| 2002 | main.log.error( self.name + ": " + self.handle.before ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2003 | main.cleanup() |
| 2004 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2005 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2006 | main.log.exception( self.name + ": Uncaught exception!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2007 | main.cleanup() |
| 2008 | main.exit() |
| 2009 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2010 | def ipv4RouteNumber( self ): |
| 2011 | """ |
| 2012 | NOTE: This method should be used after installing application: |
| 2013 | onos-app-sdnip |
| 2014 | Description: |
| 2015 | Obtain the total IPv4 routes number in the system |
| 2016 | """ |
| 2017 | try: |
| 2018 | cmdStr = "routes -s -j" |
| 2019 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2020 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2021 | jsonResult = json.loads( handle ) |
| 2022 | return jsonResult['totalRoutes4'] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2023 | except AssertionError: |
| 2024 | main.log.exception( "" ) |
| 2025 | return None |
| 2026 | except ( TypeError, ValueError ): |
| 2027 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2028 | return None |
| 2029 | except pexpect.EOF: |
| 2030 | main.log.error( self.name + ": EOF exception found" ) |
| 2031 | main.log.error( self.name + ": " + self.handle.before ) |
| 2032 | main.cleanup() |
| 2033 | main.exit() |
| 2034 | except Exception: |
| 2035 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2036 | main.cleanup() |
| 2037 | main.exit() |
| 2038 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2039 | def intents( self, jsonFormat = True, summary = False, **intentargs): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2040 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2041 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2042 | Obtain intents from the ONOS cli. |
| 2043 | Optional: |
| 2044 | * jsonFormat: Enable output formatting in json, default to True |
| 2045 | * summary: Whether only output the intent summary, defaults to False |
| 2046 | * type: Only output a certain type of intent. This options is valid |
| 2047 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2048 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2049 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2050 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2051 | if summary: |
| 2052 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2053 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2054 | cmdStr += " -j" |
| 2055 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2056 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2057 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2058 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2059 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2060 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2061 | intentType = "" |
| 2062 | # IF we want the summary of a specific intent type |
| 2063 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2064 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2065 | if intentType in jsonResult.keys(): |
| 2066 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2067 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2068 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2069 | return handle |
| 2070 | else: |
| 2071 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2072 | except AssertionError: |
| 2073 | main.log.exception( "" ) |
| 2074 | return None |
| 2075 | except ( TypeError, ValueError ): |
| 2076 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2077 | return None |
| 2078 | except pexpect.EOF: |
| 2079 | main.log.error( self.name + ": EOF exception found" ) |
| 2080 | main.log.error( self.name + ": " + self.handle.before ) |
| 2081 | main.cleanup() |
| 2082 | main.exit() |
| 2083 | except Exception: |
| 2084 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2085 | main.cleanup() |
| 2086 | main.exit() |
| 2087 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2088 | def getIntentState(self, intentsId, intentsJson=None): |
| 2089 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2090 | Description: |
| 2091 | Gets intent state. Accepts a single intent ID (string type) or a |
| 2092 | list of intent IDs. |
| 2093 | Parameters: |
| 2094 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2095 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2096 | Returns: |
| 2097 | Returns the state (string type) of the ID if a single intent ID is |
| 2098 | accepted. |
| 2099 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2100 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2101 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2102 | """ |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2103 | try: |
| 2104 | state = "State is Undefined" |
| 2105 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2106 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2107 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2108 | rawJson = intentsJson |
| 2109 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2110 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2111 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2112 | if intentsId == intent[ 'id' ]: |
| 2113 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2114 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2115 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
| 2116 | " on the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2117 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2118 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2119 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2120 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2121 | stateDict = {} |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2122 | for intents in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2123 | if intentsId[ i ] == intents[ 'id' ]: |
| 2124 | stateDict[ 'state' ] = intents[ 'state' ] |
| 2125 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2126 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2127 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2128 | if len( intentsId ) != len( dictList ): |
| 2129 | main.log.info( "Cannot find some of the intent ID state" ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2130 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2131 | else: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2132 | main.log.info( "Invalid intents ID entry" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2133 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2134 | except ( TypeError, ValueError ): |
| 2135 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2136 | return None |
| 2137 | except pexpect.EOF: |
| 2138 | main.log.error( self.name + ": EOF exception found" ) |
| 2139 | main.log.error( self.name + ": " + self.handle.before ) |
| 2140 | main.cleanup() |
| 2141 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2142 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2143 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2144 | main.cleanup() |
| 2145 | main.exit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2146 | |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2147 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2148 | """ |
| 2149 | Description: |
| 2150 | Check intents state |
| 2151 | Required: |
| 2152 | intentsId - List of intents ID to be checked |
| 2153 | Optional: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2154 | expectedState - Check the expected state(s) of each intents |
| 2155 | state in the list. |
| 2156 | *NOTE: You can pass in a list of expected state, |
| 2157 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2158 | Return: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2159 | Returns main.TRUE only if all intent are the same as expected states |
| 2160 | , otherwise, returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2161 | """ |
| 2162 | try: |
| 2163 | # Generating a dictionary: intent id as a key and state as value |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2164 | returnValue = main.TRUE |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2165 | intentsDict = self.getIntentState( intentsId ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2166 | if len( intentsId ) != len( intentsDict ): |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2167 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2168 | "getting intents state" ) |
| 2169 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2170 | |
| 2171 | if isinstance( expectedState, types.StringType ): |
| 2172 | for intents in intentsDict: |
| 2173 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2174 | main.log.debug( self.name + " : Intent ID - " + |
| 2175 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2176 | " actual state = " + |
| 2177 | intents.get( 'state' ) |
| 2178 | + " does not equal expected state = " |
| 2179 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2180 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2181 | |
| 2182 | elif isinstance( expectedState, types.ListType ): |
| 2183 | for intents in intentsDict: |
| 2184 | if not any( state == intents.get( 'state' ) for state in |
| 2185 | expectedState ): |
| 2186 | main.log.debug( self.name + " : Intent ID - " + |
| 2187 | intents.get( 'id' ) + |
| 2188 | " actual state = " + |
| 2189 | intents.get( 'state' ) + |
| 2190 | " does not equal expected states = " |
| 2191 | + str( expectedState ) ) |
| 2192 | returnValue = main.FALSE |
| 2193 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2194 | if returnValue == main.TRUE: |
| 2195 | main.log.info( self.name + ": All " + |
| 2196 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2197 | " intents are in " + str( expectedState ) + |
| 2198 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2199 | return returnValue |
| 2200 | except TypeError: |
| 2201 | main.log.exception( self.name + ": Object not as expected" ) |
| 2202 | return None |
| 2203 | except pexpect.EOF: |
| 2204 | main.log.error( self.name + ": EOF exception found" ) |
| 2205 | main.log.error( self.name + ": " + self.handle.before ) |
| 2206 | main.cleanup() |
| 2207 | main.exit() |
| 2208 | except Exception: |
| 2209 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2210 | main.cleanup() |
| 2211 | main.exit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2212 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2213 | def compareIntent( self, intentDict ): |
| 2214 | """ |
| 2215 | Description: |
| 2216 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2217 | Return: |
| 2218 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2219 | Arguments: |
| 2220 | intentDict: a dictionary which maps intent ids to intent states |
| 2221 | """ |
| 2222 | try: |
| 2223 | intentsRaw = self.intents() |
| 2224 | intentsJson = json.loads( intentsRaw ) |
| 2225 | intentDictONOS = {} |
| 2226 | for intent in intentsJson: |
| 2227 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2228 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2229 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2230 | 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] | 2231 | str( len( intentDict ) ) + " expected and " + |
| 2232 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2233 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2234 | for intentID in intentDict.keys(): |
| 2235 | if not intentID in intentDictONOS.keys(): |
| 2236 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2237 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2238 | else: |
| 2239 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2240 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2241 | " expected state is " + intentDict[ intentID ] + |
| 2242 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2243 | returnValue = main.FALSE |
| 2244 | intentDictONOS.pop( intentID ) |
| 2245 | if len( intentDictONOS ) > 0: |
| 2246 | returnValue = main.FALSE |
| 2247 | for intentID in intentDictONOS.keys(): |
| 2248 | 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] | 2249 | if returnValue == main.TRUE: |
| 2250 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2251 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2252 | except KeyError: |
| 2253 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2254 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2255 | except ( TypeError, ValueError ): |
| 2256 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2257 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2258 | except pexpect.EOF: |
| 2259 | main.log.error( self.name + ": EOF exception found" ) |
| 2260 | main.log.error( self.name + ": " + self.handle.before ) |
| 2261 | main.cleanup() |
| 2262 | main.exit() |
| 2263 | except Exception: |
| 2264 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2265 | main.cleanup() |
| 2266 | main.exit() |
| 2267 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2268 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2269 | """ |
| 2270 | Description: |
| 2271 | Check the number of installed intents. |
| 2272 | Optional: |
| 2273 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2274 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2275 | Return: |
| 2276 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2277 | , otherwise, returns main.FALSE. |
| 2278 | """ |
| 2279 | |
| 2280 | try: |
| 2281 | cmd = "intents -s -j" |
| 2282 | |
| 2283 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2284 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2285 | if response == None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2286 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2287 | response = json.loads( response ) |
| 2288 | |
| 2289 | # get total and installed number, see if they are match |
| 2290 | allState = response.get( 'all' ) |
| 2291 | if allState.get('total') == allState.get('installed'): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2292 | 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] | 2293 | return main.TRUE |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2294 | 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] | 2295 | return main.FALSE |
| 2296 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2297 | except ( TypeError, ValueError ): |
| 2298 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2299 | return None |
| 2300 | except pexpect.EOF: |
| 2301 | main.log.error( self.name + ": EOF exception found" ) |
| 2302 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2303 | if noExit: |
| 2304 | return main.FALSE |
| 2305 | else: |
| 2306 | main.cleanup() |
| 2307 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2308 | except Exception: |
| 2309 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2310 | if noExit: |
| 2311 | return main.FALSE |
| 2312 | else: |
| 2313 | main.cleanup() |
| 2314 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2315 | except pexpect.TIMEOUT: |
| 2316 | main.log.error( self.name + ": ONOS timeout" ) |
| 2317 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2318 | |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2319 | def flows( self, state="", jsonFormat=True, timeout=60, noExit=False, noCore=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2320 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2321 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2322 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2323 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2324 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2325 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2326 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2327 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2328 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2329 | if jsonFormat: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2330 | cmdStr += " -j " |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2331 | if noCore: |
| 2332 | cmdStr += " -n " |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2333 | cmdStr += state |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2334 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2335 | assert "Command not found:" not in handle, handle |
| 2336 | if re.search( "Error:", handle ): |
| 2337 | main.log.error( self.name + ": flows() response: " + |
| 2338 | str( handle ) ) |
| 2339 | return handle |
| 2340 | except AssertionError: |
| 2341 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2342 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2343 | except TypeError: |
| 2344 | main.log.exception( self.name + ": Object not as expected" ) |
| 2345 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2346 | except pexpect.TIMEOUT: |
| 2347 | main.log.error( self.name + ": ONOS timeout" ) |
| 2348 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2349 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2350 | main.log.error( self.name + ": EOF exception found" ) |
| 2351 | main.log.error( self.name + ": " + self.handle.before ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2352 | main.cleanup() |
| 2353 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2354 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2355 | main.log.exception( self.name + ": Uncaught exception!" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2356 | main.cleanup() |
| 2357 | main.exit() |
| 2358 | |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2359 | def checkFlowCount(self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2360 | count = self.getTotalFlowsNum( timeout=timeout ) |
| 2361 | count = int (count) if count else 0 |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2362 | return count if (count > min) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2363 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2364 | def checkFlowsState( self, isPENDING=True, timeout=60,noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2365 | """ |
| 2366 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2367 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2368 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2369 | if the count of those states is 0, which means all current flows |
| 2370 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2371 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2372 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2373 | Return: |
| 2374 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2375 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2376 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2377 | """ |
| 2378 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2379 | states = ["PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED"] |
| 2380 | checkedStates = [] |
| 2381 | statesCount = [0, 0, 0, 0] |
| 2382 | for s in states: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2383 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2384 | if rawFlows: |
| 2385 | # if we didn't get flows or flows function return None, we should return |
| 2386 | # main.Flase |
| 2387 | checkedStates.append( json.loads( rawFlows ) ) |
| 2388 | else: |
| 2389 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2390 | for i in range( len( states ) ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2391 | for c in checkedStates[i]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2392 | try: |
| 2393 | statesCount[i] += int( c.get( "flowCount" ) ) |
| 2394 | except TypeError: |
| 2395 | main.log.exception( "Json object not as expected" ) |
| 2396 | main.log.info( states[i] + " flows: " + str( statesCount[i] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2397 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2398 | # We want to count PENDING_ADD if isPENDING is true |
| 2399 | if isPENDING: |
| 2400 | if statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2401 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2402 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2403 | if statesCount[0] + statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2404 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2405 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2406 | except ( TypeError, ValueError ): |
| 2407 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2408 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2409 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2410 | except AssertionError: |
| 2411 | main.log.exception( "" ) |
| 2412 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2413 | except pexpect.EOF: |
| 2414 | main.log.error( self.name + ": EOF exception found" ) |
| 2415 | main.log.error( self.name + ": " + self.handle.before ) |
| 2416 | main.cleanup() |
| 2417 | main.exit() |
| 2418 | except Exception: |
| 2419 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2420 | main.cleanup() |
| 2421 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2422 | except pexpect.TIMEOUT: |
| 2423 | main.log.error( self.name + ": ONOS timeout" ) |
| 2424 | return None |
| 2425 | |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2426 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2427 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2428 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2429 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2430 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2431 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2432 | a specific point-to-point intent definition |
| 2433 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2434 | * ingress: specify source dpid |
| 2435 | * egress: specify destination dpid |
| 2436 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2437 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2438 | * offset: the keyOffset is where the next batch of intents |
| 2439 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2440 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2441 | * getResponse: If set to True, function will return ONOS response. |
| 2442 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2443 | Returns: If failed to push test intents, it will returen None, |
| 2444 | if successful, return true. |
| 2445 | Timeout expection will return None, |
| 2446 | TypeError will return false |
| 2447 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2448 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2449 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2450 | if background: |
| 2451 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2452 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2453 | back = "" |
| 2454 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2455 | ingress, |
| 2456 | egress, |
| 2457 | batchSize, |
| 2458 | offset, |
| 2459 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2460 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2461 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2462 | main.log.info( response ) |
| 2463 | if response == None: |
| 2464 | return None |
| 2465 | |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2466 | if getResponse: |
| 2467 | return response |
| 2468 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2469 | # TODO: We should handle if there is failure in installation |
| 2470 | return main.TRUE |
| 2471 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2472 | except AssertionError: |
| 2473 | main.log.exception( "" ) |
| 2474 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2475 | except pexpect.TIMEOUT: |
| 2476 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2477 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2478 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2479 | main.log.error( self.name + ": EOF exception found" ) |
| 2480 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2481 | main.cleanup() |
| 2482 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2483 | except TypeError: |
| 2484 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2485 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2486 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2487 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2488 | main.cleanup() |
| 2489 | main.exit() |
| 2490 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2491 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2492 | """ |
| 2493 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2494 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2495 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2496 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2497 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2498 | """ |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2499 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2500 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2501 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2502 | cmd = "flows -c added" |
| 2503 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2504 | if rawFlows: |
| 2505 | rawFlows = rawFlows.split("\n") |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2506 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2507 | for l in rawFlows: |
| 2508 | totalFlows += int(l.split("Count=")[1]) |
| 2509 | else: |
| 2510 | main.log.error("Response not as expected!") |
| 2511 | return None |
| 2512 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2513 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2514 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2515 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2516 | return None |
| 2517 | except pexpect.EOF: |
| 2518 | main.log.error( self.name + ": EOF exception found" ) |
| 2519 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2520 | if not noExit: |
| 2521 | main.cleanup() |
| 2522 | main.exit() |
| 2523 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2524 | except Exception: |
| 2525 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2526 | if not noExit: |
| 2527 | main.cleanup() |
| 2528 | main.exit() |
| 2529 | return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2530 | except pexpect.TIMEOUT: |
| 2531 | main.log.error( self.name + ": ONOS timeout" ) |
| 2532 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2533 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2534 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2535 | """ |
| 2536 | Description: |
| 2537 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2538 | Optional: |
| 2539 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2540 | Return: |
| 2541 | The number of intents |
| 2542 | """ |
| 2543 | try: |
| 2544 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2545 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2546 | if response == None: |
| 2547 | return -1 |
| 2548 | response = json.loads( response ) |
| 2549 | return int( response.get("intents") ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2550 | except ( TypeError, ValueError ): |
| 2551 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2552 | return None |
| 2553 | except pexpect.EOF: |
| 2554 | main.log.error( self.name + ": EOF exception found" ) |
| 2555 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2556 | if noExit: |
| 2557 | return -1 |
| 2558 | else: |
| 2559 | main.cleanup() |
| 2560 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2561 | except Exception: |
| 2562 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2563 | if noExit: |
| 2564 | return -1 |
| 2565 | else: |
| 2566 | main.cleanup() |
| 2567 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2568 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2569 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2570 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2571 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2572 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2573 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2574 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2575 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2576 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2577 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2578 | cmdStr += " -j" |
| 2579 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2580 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2581 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2582 | except AssertionError: |
| 2583 | main.log.exception( "" ) |
| 2584 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2585 | except TypeError: |
| 2586 | main.log.exception( self.name + ": Object not as expected" ) |
| 2587 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2588 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2589 | main.log.error( self.name + ": EOF exception found" ) |
| 2590 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2591 | main.cleanup() |
| 2592 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2593 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2594 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2595 | main.cleanup() |
| 2596 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2597 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2598 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2599 | """ |
| 2600 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2601 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2602 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2603 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2604 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2605 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2606 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2607 | cmdStr += " -j" |
| 2608 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2609 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2610 | if handle: |
| 2611 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2612 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2613 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2614 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2615 | else: |
| 2616 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2617 | except AssertionError: |
| 2618 | main.log.exception( "" ) |
| 2619 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2620 | except TypeError: |
| 2621 | main.log.exception( self.name + ": Object not as expected" ) |
| 2622 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2623 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2624 | main.log.error( self.name + ": EOF exception found" ) |
| 2625 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2626 | main.cleanup() |
| 2627 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2628 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2629 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2630 | main.cleanup() |
| 2631 | main.exit() |
| 2632 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2633 | # Wrapper functions **************** |
| 2634 | # Wrapper functions use existing driver |
| 2635 | # functions and extends their use case. |
| 2636 | # For example, we may use the output of |
| 2637 | # a normal driver function, and parse it |
| 2638 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2639 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2640 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2641 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2642 | Description: |
| 2643 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2644 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2645 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2646 | # Obtain output of intents function |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2647 | intentsStr = self.intents(jsonFormat=True) |
| 2648 | # Convert to a dictionary |
| 2649 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2650 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2651 | for intent in intents: |
| 2652 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2653 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2654 | except TypeError: |
| 2655 | main.log.exception( self.name + ": Object not as expected" ) |
| 2656 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2657 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2658 | main.log.error( self.name + ": EOF exception found" ) |
| 2659 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2660 | main.cleanup() |
| 2661 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2662 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2663 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2664 | main.cleanup() |
| 2665 | main.exit() |
| 2666 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2667 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2668 | """ |
| 2669 | Determine the number of flow rules for the given device id that are |
| 2670 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2671 | Params: |
| 2672 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2673 | """ |
| 2674 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2675 | if core: |
| 2676 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2677 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2678 | else: |
| 2679 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2680 | "grep 'state=ADDED' | wc -l" |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2681 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2682 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2683 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2684 | except AssertionError: |
| 2685 | main.log.exception( "" ) |
| 2686 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2687 | except pexpect.EOF: |
| 2688 | main.log.error( self.name + ": EOF exception found" ) |
| 2689 | main.log.error( self.name + ": " + self.handle.before ) |
| 2690 | main.cleanup() |
| 2691 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2692 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2693 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2694 | main.cleanup() |
| 2695 | main.exit() |
| 2696 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2697 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2698 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2699 | Use 'devices' function to obtain list of all devices |
| 2700 | and parse the result to obtain a list of all device |
| 2701 | id's. Returns this list. Returns empty list if no |
| 2702 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2703 | List is ordered sequentially |
| 2704 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2705 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2706 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2707 | the ids. By obtaining the list of device ids on the fly, |
| 2708 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2709 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2710 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2711 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2712 | devicesStr = self.devices( jsonFormat=False ) |
| 2713 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2714 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2715 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2716 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2717 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2718 | |
| 2719 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2720 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2721 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2722 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2723 | # Split list further into arguments before and after string |
| 2724 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2725 | # append to idList |
| 2726 | for arg in tempList: |
| 2727 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 2728 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2729 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2730 | except TypeError: |
| 2731 | main.log.exception( self.name + ": Object not as expected" ) |
| 2732 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2733 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2734 | main.log.error( self.name + ": EOF exception found" ) |
| 2735 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2736 | main.cleanup() |
| 2737 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2738 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2739 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2740 | main.cleanup() |
| 2741 | main.exit() |
| 2742 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2743 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2744 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2745 | Uses 'nodes' function to obtain list of all nodes |
| 2746 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2747 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2748 | Returns: |
| 2749 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2750 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2751 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2752 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2753 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2754 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 2755 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2756 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2757 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2758 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2759 | nodesJson = json.loads( nodesStr ) |
| 2760 | idList = [ node.get('id') for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2761 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2762 | except ( TypeError, ValueError ): |
| 2763 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2764 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2765 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2766 | main.log.error( self.name + ": EOF exception found" ) |
| 2767 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2768 | main.cleanup() |
| 2769 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2770 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2771 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2772 | main.cleanup() |
| 2773 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2774 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2775 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2776 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2777 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 2778 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2779 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2780 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2781 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2782 | return None |
| 2783 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2784 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2785 | rawDevices = self.devices() |
| 2786 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2787 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2788 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2789 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 2790 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2791 | return device |
| 2792 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2793 | except ( TypeError, ValueError ): |
| 2794 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2795 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2796 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2797 | main.log.error( self.name + ": EOF exception found" ) |
| 2798 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2799 | main.cleanup() |
| 2800 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2801 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2802 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2803 | main.cleanup() |
| 2804 | main.exit() |
| 2805 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2806 | def getTopology( self, topologyOutput ): |
| 2807 | """ |
| 2808 | Definition: |
| 2809 | Loads a json topology output |
| 2810 | Return: |
| 2811 | topology = current ONOS topology |
| 2812 | """ |
| 2813 | import json |
| 2814 | try: |
| 2815 | # either onos:topology or 'topology' will work in CLI |
| 2816 | topology = json.loads(topologyOutput) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2817 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2818 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2819 | except ( TypeError, ValueError ): |
| 2820 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 2821 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2822 | except pexpect.EOF: |
| 2823 | main.log.error( self.name + ": EOF exception found" ) |
| 2824 | main.log.error( self.name + ": " + self.handle.before ) |
| 2825 | main.cleanup() |
| 2826 | main.exit() |
| 2827 | except Exception: |
| 2828 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2829 | main.cleanup() |
| 2830 | main.exit() |
| 2831 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2832 | def checkStatus(self, numoswitch, numolink, numoctrl = -1, logLevel="info"): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2833 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2834 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2835 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2836 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2837 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2838 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2839 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2840 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2841 | logLevel = level to log to. |
| 2842 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2843 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2844 | Returns: main.TRUE if the number of switches and links are correct, |
| 2845 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2846 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2847 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2848 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2849 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 2850 | summary = self.summary() |
| 2851 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 2852 | except ( TypeError, ValueError ): |
| 2853 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 2854 | return main.ERROR |
| 2855 | try: |
| 2856 | topology = self.getTopology( self.topology() ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2857 | if topology == {} or topology == None or summary == {} or summary == None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2858 | return main.ERROR |
| 2859 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2860 | # Is the number of switches is what we expected |
| 2861 | devices = topology.get( 'devices', False ) |
| 2862 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2863 | nodes = summary.get( 'nodes', False ) |
| 2864 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2865 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2866 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2867 | # Is the number of links is what we expected |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2868 | linkCheck = ( int( links ) == int( numolink ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2869 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 2870 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2871 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2872 | output = output + "The number of links and switches match "\ |
| 2873 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2874 | result = main.TRUE |
| 2875 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2876 | output = output + \ |
| 2877 | "The number of links and switches does not match " + \ |
| 2878 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2879 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2880 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 2881 | output = output + " (%i expected) " % int( numoswitch ) |
| 2882 | output = output + "and %i links " % int( links ) |
| 2883 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 2884 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2885 | output = output + "and %i controllers " % int( nodes ) |
| 2886 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2887 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2888 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2889 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2890 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2891 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2892 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2893 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2894 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2895 | main.log.error( self.name + ": EOF exception found" ) |
| 2896 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2897 | main.cleanup() |
| 2898 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2899 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2900 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2901 | main.cleanup() |
| 2902 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2903 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2904 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2905 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2906 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2907 | deviceId must be the id of a device as seen in the onos devices command |
| 2908 | 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] | 2909 | role must be either master, standby, or none |
| 2910 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2911 | Returns: |
| 2912 | main.TRUE or main.FALSE based on argument verification and |
| 2913 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2914 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2915 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2916 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2917 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2918 | cmdStr = "device-role " +\ |
| 2919 | str( deviceId ) + " " +\ |
| 2920 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2921 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2922 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2923 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2924 | if re.search( "Error", handle ): |
| 2925 | # end color output to escape any colours |
| 2926 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2927 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2928 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2929 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2930 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2931 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2932 | main.log.error( "Invalid 'role' given to device_role(). " + |
| 2933 | "Value was '" + str(role) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2934 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2935 | except AssertionError: |
| 2936 | main.log.exception( "" ) |
| 2937 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2938 | except TypeError: |
| 2939 | main.log.exception( self.name + ": Object not as expected" ) |
| 2940 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2941 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2942 | main.log.error( self.name + ": EOF exception found" ) |
| 2943 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2944 | main.cleanup() |
| 2945 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2946 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2947 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2948 | main.cleanup() |
| 2949 | main.exit() |
| 2950 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2951 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2952 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2953 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2954 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2955 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2956 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2957 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2958 | cmdStr = "clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2959 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2960 | cmdStr += " -j" |
| 2961 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2962 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2963 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2964 | except AssertionError: |
| 2965 | main.log.exception( "" ) |
| 2966 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2967 | except TypeError: |
| 2968 | main.log.exception( self.name + ": Object not as expected" ) |
| 2969 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2970 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2971 | main.log.error( self.name + ": EOF exception found" ) |
| 2972 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2973 | main.cleanup() |
| 2974 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2975 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2976 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2977 | main.cleanup() |
| 2978 | main.exit() |
| 2979 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2980 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2981 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2982 | CLI command to get the current leader for the Election test application |
| 2983 | NOTE: Requires installation of the onos-app-election feature |
| 2984 | Returns: Node IP of the leader if one exists |
| 2985 | None if none exists |
| 2986 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2987 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2988 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2989 | cmdStr = "election-test-leader" |
| 2990 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2991 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2992 | # Leader |
| 2993 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2994 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2995 | nodeSearch = re.search( leaderPattern, response ) |
| 2996 | if nodeSearch: |
| 2997 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2998 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2999 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3000 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3001 | # no leader |
| 3002 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3003 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3004 | nullSearch = re.search( nullPattern, response ) |
| 3005 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3006 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3007 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3008 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3009 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3010 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 3011 | ": " + "unexpected response" ) |
| 3012 | main.log.error( repr( response ) ) |
| 3013 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3014 | except AssertionError: |
| 3015 | main.log.exception( "" ) |
| 3016 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3017 | except TypeError: |
| 3018 | main.log.exception( self.name + ": Object not as expected" ) |
| 3019 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3020 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3021 | main.log.error( self.name + ": EOF exception found" ) |
| 3022 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3023 | main.cleanup() |
| 3024 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3025 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3026 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3027 | main.cleanup() |
| 3028 | main.exit() |
| 3029 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3030 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3031 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3032 | CLI command to run for leadership of the Election test application. |
| 3033 | NOTE: Requires installation of the onos-app-election feature |
| 3034 | Returns: Main.TRUE on success |
| 3035 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3036 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3037 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3038 | cmdStr = "election-test-run" |
| 3039 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3040 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3041 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3042 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3043 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3044 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3045 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3046 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3047 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3048 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3049 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3050 | main.log.error( "Error in electionTestRun on " + self.name + |
| 3051 | ": " + "unexpected response" ) |
| 3052 | main.log.error( repr( response ) ) |
| 3053 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3054 | except AssertionError: |
| 3055 | main.log.exception( "" ) |
| 3056 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3057 | except TypeError: |
| 3058 | main.log.exception( self.name + ": Object not as expected" ) |
| 3059 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3060 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3061 | main.log.error( self.name + ": EOF exception found" ) |
| 3062 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3063 | main.cleanup() |
| 3064 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3065 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3066 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3067 | main.cleanup() |
| 3068 | main.exit() |
| 3069 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3070 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3071 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3072 | * CLI command to withdraw the local node from leadership election for |
| 3073 | * the Election test application. |
| 3074 | #NOTE: Requires installation of the onos-app-election feature |
| 3075 | Returns: Main.TRUE on success |
| 3076 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3077 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3078 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3079 | cmdStr = "election-test-withdraw" |
| 3080 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3081 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3082 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3083 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3084 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3085 | if re.search( successPattern, response ): |
| 3086 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3087 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3088 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3089 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3090 | main.log.error( "Error in electionTestWithdraw on " + |
| 3091 | self.name + ": " + "unexpected response" ) |
| 3092 | main.log.error( repr( response ) ) |
| 3093 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3094 | except AssertionError: |
| 3095 | main.log.exception( "" ) |
| 3096 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3097 | except TypeError: |
| 3098 | main.log.exception( self.name + ": Object not as expected" ) |
| 3099 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3100 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3101 | main.log.error( self.name + ": EOF exception found" ) |
| 3102 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3103 | main.cleanup() |
| 3104 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3105 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3106 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3107 | main.cleanup() |
| 3108 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3109 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3110 | def getDevicePortsEnabledCount( self, dpid ): |
| 3111 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3112 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3113 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3114 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3115 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3116 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 3117 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3118 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3119 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3120 | if re.search( "No such device", output ): |
| 3121 | main.log.error( "Error in getting ports" ) |
| 3122 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3123 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3124 | except AssertionError: |
| 3125 | main.log.exception( "" ) |
| 3126 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3127 | except TypeError: |
| 3128 | main.log.exception( self.name + ": Object not as expected" ) |
| 3129 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3130 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3131 | main.log.error( self.name + ": EOF exception found" ) |
| 3132 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3133 | main.cleanup() |
| 3134 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3135 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3136 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3137 | main.cleanup() |
| 3138 | main.exit() |
| 3139 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3140 | def getDeviceLinksActiveCount( self, dpid ): |
| 3141 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3142 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3143 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3144 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3145 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3146 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3147 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3148 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3149 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3150 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3151 | main.log.error( "Error in getting ports " ) |
| 3152 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3153 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3154 | except AssertionError: |
| 3155 | main.log.exception( "" ) |
| 3156 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3157 | except TypeError: |
| 3158 | main.log.exception( self.name + ": Object not as expected" ) |
| 3159 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3160 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3161 | main.log.error( self.name + ": EOF exception found" ) |
| 3162 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3163 | main.cleanup() |
| 3164 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3165 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3166 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3167 | main.cleanup() |
| 3168 | main.exit() |
| 3169 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3170 | def getAllIntentIds( self ): |
| 3171 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3172 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3173 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3174 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3175 | cmdStr = "onos:intents | grep id=" |
| 3176 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3177 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3178 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3179 | if re.search( "Error", output ): |
| 3180 | main.log.error( "Error in getting ports" ) |
| 3181 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3182 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3183 | except AssertionError: |
| 3184 | main.log.exception( "" ) |
| 3185 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3186 | except TypeError: |
| 3187 | main.log.exception( self.name + ": Object not as expected" ) |
| 3188 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3189 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3190 | main.log.error( self.name + ": EOF exception found" ) |
| 3191 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3192 | main.cleanup() |
| 3193 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3194 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3195 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3196 | main.cleanup() |
| 3197 | main.exit() |
| 3198 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3199 | def intentSummary( self ): |
| 3200 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3201 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3202 | """ |
| 3203 | try: |
| 3204 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3205 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3206 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3207 | states.append( intent.get( 'state', None ) ) |
| 3208 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3209 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3210 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3211 | except ( TypeError, ValueError ): |
| 3212 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3213 | return None |
| 3214 | except pexpect.EOF: |
| 3215 | main.log.error( self.name + ": EOF exception found" ) |
| 3216 | main.log.error( self.name + ": " + self.handle.before ) |
| 3217 | main.cleanup() |
| 3218 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3219 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3220 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3221 | main.cleanup() |
| 3222 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3223 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3224 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3225 | """ |
| 3226 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3227 | Optional argument: |
| 3228 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3229 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3230 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3231 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3232 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3233 | cmdStr += " -j" |
| 3234 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3235 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3236 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3237 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3238 | except AssertionError: |
| 3239 | main.log.exception( "" ) |
| 3240 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3241 | except TypeError: |
| 3242 | main.log.exception( self.name + ": Object not as expected" ) |
| 3243 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3244 | except pexpect.EOF: |
| 3245 | main.log.error( self.name + ": EOF exception found" ) |
| 3246 | main.log.error( self.name + ": " + self.handle.before ) |
| 3247 | main.cleanup() |
| 3248 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3249 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3250 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3251 | main.cleanup() |
| 3252 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3253 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3254 | def leaderCandidates( self, jsonFormat=True ): |
| 3255 | """ |
| 3256 | Returns the output of the leaders -c command. |
| 3257 | Optional argument: |
| 3258 | * jsonFormat - boolean indicating if you want output in json |
| 3259 | """ |
| 3260 | try: |
| 3261 | cmdStr = "onos:leaders -c" |
| 3262 | if jsonFormat: |
| 3263 | cmdStr += " -j" |
| 3264 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3265 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3266 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3267 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3268 | except AssertionError: |
| 3269 | main.log.exception( "" ) |
| 3270 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3271 | except TypeError: |
| 3272 | main.log.exception( self.name + ": Object not as expected" ) |
| 3273 | return None |
| 3274 | except pexpect.EOF: |
| 3275 | main.log.error( self.name + ": EOF exception found" ) |
| 3276 | main.log.error( self.name + ": " + self.handle.before ) |
| 3277 | main.cleanup() |
| 3278 | main.exit() |
| 3279 | except Exception: |
| 3280 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3281 | main.cleanup() |
| 3282 | main.exit() |
| 3283 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3284 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3285 | """ |
| 3286 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
| 3287 | topic parameter and an empty list if the topic doesn't exist |
| 3288 | If no leader is elected leader in the returned list will be "none" |
| 3289 | Returns None if there is a type error processing the json object |
| 3290 | """ |
| 3291 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3292 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3293 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3294 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3295 | assert "Command not found:" not in rawOutput, rawOutput |
| 3296 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3297 | results = [] |
| 3298 | for dict in output: |
| 3299 | if dict["topic"] == topic: |
| 3300 | leader = dict["leader"] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3301 | candidates = re.split( ", ", dict["candidates"][1:-1] ) |
| 3302 | results.append( leader ) |
| 3303 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3304 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3305 | except AssertionError: |
| 3306 | main.log.exception( "" ) |
| 3307 | return None |
| 3308 | except ( TypeError, ValueError ): |
| 3309 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3310 | return None |
| 3311 | except pexpect.EOF: |
| 3312 | main.log.error( self.name + ": EOF exception found" ) |
| 3313 | main.log.error( self.name + ": " + self.handle.before ) |
| 3314 | main.cleanup() |
| 3315 | main.exit() |
| 3316 | except Exception: |
| 3317 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3318 | main.cleanup() |
| 3319 | main.exit() |
| 3320 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3321 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3322 | """ |
| 3323 | Returns the output of the intent Pending map. |
| 3324 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3325 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3326 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3327 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3328 | cmdStr += " -j" |
| 3329 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3330 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3331 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3332 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3333 | except AssertionError: |
| 3334 | main.log.exception( "" ) |
| 3335 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3336 | except TypeError: |
| 3337 | main.log.exception( self.name + ": Object not as expected" ) |
| 3338 | return None |
| 3339 | except pexpect.EOF: |
| 3340 | main.log.error( self.name + ": EOF exception found" ) |
| 3341 | main.log.error( self.name + ": " + self.handle.before ) |
| 3342 | main.cleanup() |
| 3343 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3344 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3345 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3346 | main.cleanup() |
| 3347 | main.exit() |
| 3348 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3349 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3350 | """ |
| 3351 | Returns the output of the raft partitions command for ONOS. |
| 3352 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3353 | # Sample JSON |
| 3354 | # { |
| 3355 | # "leader": "tcp://10.128.30.11:7238", |
| 3356 | # "members": [ |
| 3357 | # "tcp://10.128.30.11:7238", |
| 3358 | # "tcp://10.128.30.17:7238", |
| 3359 | # "tcp://10.128.30.13:7238", |
| 3360 | # ], |
| 3361 | # "name": "p1", |
| 3362 | # "term": 3 |
| 3363 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3364 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3365 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3366 | if candidates: |
| 3367 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3368 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3369 | cmdStr += " -j" |
| 3370 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3371 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3372 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3373 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3374 | except AssertionError: |
| 3375 | main.log.exception( "" ) |
| 3376 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3377 | except TypeError: |
| 3378 | main.log.exception( self.name + ": Object not as expected" ) |
| 3379 | return None |
| 3380 | except pexpect.EOF: |
| 3381 | main.log.error( self.name + ": EOF exception found" ) |
| 3382 | main.log.error( self.name + ": " + self.handle.before ) |
| 3383 | main.cleanup() |
| 3384 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3385 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3386 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3387 | main.cleanup() |
| 3388 | main.exit() |
| 3389 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3390 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3391 | """ |
| 3392 | Returns the output of the apps command for ONOS. This command lists |
| 3393 | information about installed ONOS applications |
| 3394 | """ |
| 3395 | # Sample JSON object |
| 3396 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
| 3397 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3398 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
| 3399 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
| 3400 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3401 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3402 | if summary: |
| 3403 | cmdStr += " -s" |
| 3404 | if active: |
| 3405 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3406 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3407 | cmdStr += " -j" |
| 3408 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3409 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3410 | assert "Command not found:" not in output, output |
| 3411 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3412 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3413 | # FIXME: look at specific exceptions/Errors |
| 3414 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3415 | main.log.exception( "Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3416 | return None |
| 3417 | except TypeError: |
| 3418 | main.log.exception( self.name + ": Object not as expected" ) |
| 3419 | return None |
| 3420 | except pexpect.EOF: |
| 3421 | main.log.error( self.name + ": EOF exception found" ) |
| 3422 | main.log.error( self.name + ": " + self.handle.before ) |
| 3423 | main.cleanup() |
| 3424 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3425 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3426 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3427 | main.cleanup() |
| 3428 | main.exit() |
| 3429 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3430 | def appStatus( self, appName ): |
| 3431 | """ |
| 3432 | Uses the onos:apps cli command to return the status of an application. |
| 3433 | Returns: |
| 3434 | "ACTIVE" - If app is installed and activated |
| 3435 | "INSTALLED" - If app is installed and deactivated |
| 3436 | "UNINSTALLED" - If app is not installed |
| 3437 | None - on error |
| 3438 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3439 | try: |
| 3440 | if not isinstance( appName, types.StringType ): |
| 3441 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3442 | " a string" ) |
| 3443 | return None |
| 3444 | output = self.apps( jsonFormat=True ) |
| 3445 | appsJson = json.loads( output ) |
| 3446 | state = None |
| 3447 | for app in appsJson: |
| 3448 | if appName == app.get('name'): |
| 3449 | state = app.get('state') |
| 3450 | break |
| 3451 | if state == "ACTIVE" or state == "INSTALLED": |
| 3452 | return state |
| 3453 | elif state is None: |
| 3454 | return "UNINSTALLED" |
| 3455 | elif state: |
| 3456 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3457 | str( state ) ) |
| 3458 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3459 | except ( TypeError, ValueError ): |
| 3460 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3461 | return None |
| 3462 | except pexpect.EOF: |
| 3463 | main.log.error( self.name + ": EOF exception found" ) |
| 3464 | main.log.error( self.name + ": " + self.handle.before ) |
| 3465 | main.cleanup() |
| 3466 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3467 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3468 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3469 | main.cleanup() |
| 3470 | main.exit() |
| 3471 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3472 | def app( self, appName, option ): |
| 3473 | """ |
| 3474 | Interacts with the app command for ONOS. This command manages |
| 3475 | application inventory. |
| 3476 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3477 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3478 | # Validate argument types |
| 3479 | valid = True |
| 3480 | if not isinstance( appName, types.StringType ): |
| 3481 | main.log.error( self.name + ".app(): appName must be a " + |
| 3482 | "string" ) |
| 3483 | valid = False |
| 3484 | if not isinstance( option, types.StringType ): |
| 3485 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3486 | valid = False |
| 3487 | if not valid: |
| 3488 | return main.FALSE |
| 3489 | # Validate Option |
| 3490 | option = option.lower() |
| 3491 | # NOTE: Install may become a valid option |
| 3492 | if option == "activate": |
| 3493 | pass |
| 3494 | elif option == "deactivate": |
| 3495 | pass |
| 3496 | elif option == "uninstall": |
| 3497 | pass |
| 3498 | else: |
| 3499 | # Invalid option |
| 3500 | main.log.error( "The ONOS app command argument only takes " + |
| 3501 | "the values: (activate|deactivate|uninstall)" + |
| 3502 | "; was given '" + option + "'") |
| 3503 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3504 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3505 | output = self.sendline( cmdStr ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3506 | if "Error executing command" in output: |
| 3507 | main.log.error( "Error in processing onos:app command: " + |
| 3508 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3509 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3510 | elif "No such application" in output: |
| 3511 | main.log.error( "The application '" + appName + |
| 3512 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3513 | return main.FALSE |
| 3514 | elif "Command not found:" in output: |
| 3515 | main.log.error( "Error in processing onos:app command: " + |
| 3516 | str( output ) ) |
| 3517 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3518 | elif "Unsupported command:" in output: |
| 3519 | main.log.error( "Incorrect command given to 'app': " + |
| 3520 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3521 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3522 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3523 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3524 | return main.TRUE |
| 3525 | except TypeError: |
| 3526 | main.log.exception( self.name + ": Object not as expected" ) |
| 3527 | return main.ERROR |
| 3528 | except pexpect.EOF: |
| 3529 | main.log.error( self.name + ": EOF exception found" ) |
| 3530 | main.log.error( self.name + ": " + self.handle.before ) |
| 3531 | main.cleanup() |
| 3532 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3533 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3534 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3535 | main.cleanup() |
| 3536 | main.exit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3537 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3538 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3539 | """ |
| 3540 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3541 | appName is the hierarchical app name, not the feature name |
| 3542 | If check is True, method will check the status of the app after the |
| 3543 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3544 | Returns main.TRUE if the command was successfully sent |
| 3545 | main.FALSE if the cli responded with an error or given |
| 3546 | incorrect input |
| 3547 | """ |
| 3548 | try: |
| 3549 | if not isinstance( appName, types.StringType ): |
| 3550 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3551 | " a string" ) |
| 3552 | return main.FALSE |
| 3553 | status = self.appStatus( appName ) |
| 3554 | if status == "INSTALLED": |
| 3555 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3556 | if check and response == main.TRUE: |
| 3557 | for i in range(10): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3558 | status = self.appStatus( appName ) |
| 3559 | if status == "ACTIVE": |
| 3560 | return main.TRUE |
| 3561 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3562 | main.log.debug( "The state of application " + |
| 3563 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3564 | time.sleep( 1 ) |
| 3565 | return main.FALSE |
| 3566 | else: # not 'check' or command didn't succeed |
| 3567 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3568 | elif status == "ACTIVE": |
| 3569 | return main.TRUE |
| 3570 | elif status == "UNINSTALLED": |
| 3571 | main.log.error( self.name + ": Tried to activate the " + |
| 3572 | "application '" + appName + "' which is not " + |
| 3573 | "installed." ) |
| 3574 | else: |
| 3575 | main.log.error( "Unexpected return value from appStatus: " + |
| 3576 | str( status ) ) |
| 3577 | return main.ERROR |
| 3578 | except TypeError: |
| 3579 | main.log.exception( self.name + ": Object not as expected" ) |
| 3580 | return main.ERROR |
| 3581 | except pexpect.EOF: |
| 3582 | main.log.error( self.name + ": EOF exception found" ) |
| 3583 | main.log.error( self.name + ": " + self.handle.before ) |
| 3584 | main.cleanup() |
| 3585 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3586 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3587 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3588 | main.cleanup() |
| 3589 | main.exit() |
| 3590 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3591 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3592 | """ |
| 3593 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3594 | appName is the hierarchical app name, not the feature name |
| 3595 | If check is True, method will check the status of the app after the |
| 3596 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3597 | Returns main.TRUE if the command was successfully sent |
| 3598 | main.FALSE if the cli responded with an error or given |
| 3599 | incorrect input |
| 3600 | """ |
| 3601 | try: |
| 3602 | if not isinstance( appName, types.StringType ): |
| 3603 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3604 | "be a string" ) |
| 3605 | return main.FALSE |
| 3606 | status = self.appStatus( appName ) |
| 3607 | if status == "INSTALLED": |
| 3608 | return main.TRUE |
| 3609 | elif status == "ACTIVE": |
| 3610 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3611 | if check and response == main.TRUE: |
| 3612 | for i in range(10): # try 10 times then give up |
| 3613 | status = self.appStatus( appName ) |
| 3614 | if status == "INSTALLED": |
| 3615 | return main.TRUE |
| 3616 | else: |
| 3617 | time.sleep( 1 ) |
| 3618 | return main.FALSE |
| 3619 | else: # not check or command didn't succeed |
| 3620 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3621 | elif status == "UNINSTALLED": |
| 3622 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3623 | "application '" + appName + "' which is not " + |
| 3624 | "installed." ) |
| 3625 | return main.TRUE |
| 3626 | else: |
| 3627 | main.log.error( "Unexpected return value from appStatus: " + |
| 3628 | str( status ) ) |
| 3629 | return main.ERROR |
| 3630 | except TypeError: |
| 3631 | main.log.exception( self.name + ": Object not as expected" ) |
| 3632 | return main.ERROR |
| 3633 | except pexpect.EOF: |
| 3634 | main.log.error( self.name + ": EOF exception found" ) |
| 3635 | main.log.error( self.name + ": " + self.handle.before ) |
| 3636 | main.cleanup() |
| 3637 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3638 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3639 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3640 | main.cleanup() |
| 3641 | main.exit() |
| 3642 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3643 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3644 | """ |
| 3645 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3646 | appName is the hierarchical app name, not the feature name |
| 3647 | If check is True, method will check the status of the app after the |
| 3648 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3649 | Returns main.TRUE if the command was successfully sent |
| 3650 | main.FALSE if the cli responded with an error or given |
| 3651 | incorrect input |
| 3652 | """ |
| 3653 | # TODO: check with Thomas about the state machine for apps |
| 3654 | try: |
| 3655 | if not isinstance( appName, types.StringType ): |
| 3656 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3657 | "be a string" ) |
| 3658 | return main.FALSE |
| 3659 | status = self.appStatus( appName ) |
| 3660 | if status == "INSTALLED": |
| 3661 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3662 | if check and response == main.TRUE: |
| 3663 | for i in range(10): # try 10 times then give up |
| 3664 | status = self.appStatus( appName ) |
| 3665 | if status == "UNINSTALLED": |
| 3666 | return main.TRUE |
| 3667 | else: |
| 3668 | time.sleep( 1 ) |
| 3669 | return main.FALSE |
| 3670 | else: # not check or command didn't succeed |
| 3671 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3672 | elif status == "ACTIVE": |
| 3673 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3674 | "application '" + appName + "' which is " + |
| 3675 | "currently active." ) |
| 3676 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3677 | if check and response == main.TRUE: |
| 3678 | for i in range(10): # try 10 times then give up |
| 3679 | status = self.appStatus( appName ) |
| 3680 | if status == "UNINSTALLED": |
| 3681 | return main.TRUE |
| 3682 | else: |
| 3683 | time.sleep( 1 ) |
| 3684 | return main.FALSE |
| 3685 | else: # not check or command didn't succeed |
| 3686 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3687 | elif status == "UNINSTALLED": |
| 3688 | return main.TRUE |
| 3689 | else: |
| 3690 | main.log.error( "Unexpected return value from appStatus: " + |
| 3691 | str( status ) ) |
| 3692 | return main.ERROR |
| 3693 | except TypeError: |
| 3694 | main.log.exception( self.name + ": Object not as expected" ) |
| 3695 | return main.ERROR |
| 3696 | except pexpect.EOF: |
| 3697 | main.log.error( self.name + ": EOF exception found" ) |
| 3698 | main.log.error( self.name + ": " + self.handle.before ) |
| 3699 | main.cleanup() |
| 3700 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3701 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3702 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3703 | main.cleanup() |
| 3704 | main.exit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3705 | |
| 3706 | def appIDs( self, jsonFormat=True ): |
| 3707 | """ |
| 3708 | Show the mappings between app id and app names given by the 'app-ids' |
| 3709 | cli command |
| 3710 | """ |
| 3711 | try: |
| 3712 | cmdStr = "app-ids" |
| 3713 | if jsonFormat: |
| 3714 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3715 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3716 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3717 | assert "Command not found:" not in output, output |
| 3718 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3719 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3720 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3721 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3722 | return None |
| 3723 | except TypeError: |
| 3724 | main.log.exception( self.name + ": Object not as expected" ) |
| 3725 | return None |
| 3726 | except pexpect.EOF: |
| 3727 | main.log.error( self.name + ": EOF exception found" ) |
| 3728 | main.log.error( self.name + ": " + self.handle.before ) |
| 3729 | main.cleanup() |
| 3730 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3731 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3732 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3733 | main.cleanup() |
| 3734 | main.exit() |
| 3735 | |
| 3736 | def appToIDCheck( self ): |
| 3737 | """ |
| 3738 | This method will check that each application's ID listed in 'apps' is |
| 3739 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3740 | there are no duplicate IDs issued. Note that an app ID should be |
| 3741 | a globaly unique numerical identifier for app/app-like features. Once |
| 3742 | an ID is registered, the ID is never freed up so that if an app is |
| 3743 | reinstalled it will have the same ID. |
| 3744 | |
| 3745 | Returns: main.TRUE if the check passes and |
| 3746 | main.FALSE if the check fails or |
| 3747 | main.ERROR if there is some error in processing the test |
| 3748 | """ |
| 3749 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3750 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3751 | rawJson = self.appIDs( jsonFormat=True ) |
| 3752 | if rawJson: |
| 3753 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3754 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3755 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3756 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3757 | rawJson = self.apps( jsonFormat=True ) |
| 3758 | if rawJson: |
| 3759 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3760 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3761 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3762 | bail = True |
| 3763 | if bail: |
| 3764 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3765 | result = main.TRUE |
| 3766 | for app in apps: |
| 3767 | appID = app.get( 'id' ) |
| 3768 | if appID is None: |
| 3769 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3770 | result = main.FALSE |
| 3771 | appName = app.get( 'name' ) |
| 3772 | if appName is None: |
| 3773 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3774 | result = main.FALSE |
| 3775 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3776 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3777 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3778 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3779 | if not current: # if ids doesn't have this id |
| 3780 | result = main.FALSE |
| 3781 | main.log.error( "'app-ids' does not have the ID for " + |
| 3782 | str( appName ) + " that apps does." ) |
| 3783 | elif len( current ) > 1: |
| 3784 | # there is more than one app with this ID |
| 3785 | result = main.FALSE |
| 3786 | # We will log this later in the method |
| 3787 | elif not current[0][ 'name' ] == appName: |
| 3788 | currentName = current[0][ 'name' ] |
| 3789 | result = main.FALSE |
| 3790 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3791 | " registered under id:" + str( appID ) + |
| 3792 | " but 'apps' has " + str( appName ) ) |
| 3793 | else: |
| 3794 | pass # id and name match! |
| 3795 | # now make sure that app-ids has no duplicates |
| 3796 | idsList = [] |
| 3797 | namesList = [] |
| 3798 | for item in ids: |
| 3799 | idsList.append( item[ 'id' ] ) |
| 3800 | namesList.append( item[ 'name' ] ) |
| 3801 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3802 | len( namesList ) != len( set( namesList ) ): |
| 3803 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3804 | + json.dumps( ids, |
| 3805 | sort_keys=True, |
| 3806 | indent=4, |
| 3807 | separators=( ',', ': ' ) ) ) |
| 3808 | result = main.FALSE |
| 3809 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3810 | except ( TypeError, ValueError ): |
| 3811 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3812 | return main.ERROR |
| 3813 | except pexpect.EOF: |
| 3814 | main.log.error( self.name + ": EOF exception found" ) |
| 3815 | main.log.error( self.name + ": " + self.handle.before ) |
| 3816 | main.cleanup() |
| 3817 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3818 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3819 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3820 | main.cleanup() |
| 3821 | main.exit() |
| 3822 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3823 | def getCfg( self, component=None, propName=None, short=False, |
| 3824 | jsonFormat=True ): |
| 3825 | """ |
| 3826 | Get configuration settings from onos cli |
| 3827 | Optional arguments: |
| 3828 | component - Optionally only list configurations for a specific |
| 3829 | component. If None, all components with configurations |
| 3830 | are displayed. Case Sensitive string. |
| 3831 | propName - If component is specified, propName option will show |
| 3832 | only this specific configuration from that component. |
| 3833 | Case Sensitive string. |
| 3834 | jsonFormat - Returns output as json. Note that this will override |
| 3835 | the short option |
| 3836 | short - Short, less verbose, version of configurations. |
| 3837 | This is overridden by the json option |
| 3838 | returns: |
| 3839 | Output from cli as a string or None on error |
| 3840 | """ |
| 3841 | try: |
| 3842 | baseStr = "cfg" |
| 3843 | cmdStr = " get" |
| 3844 | componentStr = "" |
| 3845 | if component: |
| 3846 | componentStr += " " + component |
| 3847 | if propName: |
| 3848 | componentStr += " " + propName |
| 3849 | if jsonFormat: |
| 3850 | baseStr += " -j" |
| 3851 | elif short: |
| 3852 | baseStr += " -s" |
| 3853 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3854 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3855 | assert "Command not found:" not in output, output |
| 3856 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3857 | return output |
| 3858 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3859 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3860 | return None |
| 3861 | except TypeError: |
| 3862 | main.log.exception( self.name + ": Object not as expected" ) |
| 3863 | return None |
| 3864 | except pexpect.EOF: |
| 3865 | main.log.error( self.name + ": EOF exception found" ) |
| 3866 | main.log.error( self.name + ": " + self.handle.before ) |
| 3867 | main.cleanup() |
| 3868 | main.exit() |
| 3869 | except Exception: |
| 3870 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3871 | main.cleanup() |
| 3872 | main.exit() |
| 3873 | |
| 3874 | def setCfg( self, component, propName, value=None, check=True ): |
| 3875 | """ |
| 3876 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3877 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3878 | component - The case sensitive name of the component whose |
| 3879 | property is to be set |
| 3880 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3881 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3882 | value - The value to set the property to. If None, will unset the |
| 3883 | property and revert it to it's default value(if applicable) |
| 3884 | check - Boolean, Check whether the option was successfully set this |
| 3885 | only applies when a value is given. |
| 3886 | returns: |
| 3887 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3888 | will return main.TRUE unless there is an error |
| 3889 | """ |
| 3890 | try: |
| 3891 | baseStr = "cfg" |
| 3892 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3893 | if value is not None: |
| 3894 | cmdStr += " " + str( value ) |
| 3895 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3896 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3897 | assert "Command not found:" not in output, output |
| 3898 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3899 | if value and check: |
| 3900 | results = self.getCfg( component=str( component ), |
| 3901 | propName=str( propName ), |
| 3902 | jsonFormat=True ) |
| 3903 | # Check if current value is what we just set |
| 3904 | try: |
| 3905 | jsonOutput = json.loads( results ) |
| 3906 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3907 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3908 | main.log.exception( "Error parsing cfg output" ) |
| 3909 | main.log.error( "output:" + repr( results ) ) |
| 3910 | return main.FALSE |
| 3911 | if current == str( value ): |
| 3912 | return main.TRUE |
| 3913 | return main.FALSE |
| 3914 | return main.TRUE |
| 3915 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3916 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3917 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3918 | except ( TypeError, ValueError ): |
| 3919 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3920 | return main.FALSE |
| 3921 | except pexpect.EOF: |
| 3922 | main.log.error( self.name + ": EOF exception found" ) |
| 3923 | main.log.error( self.name + ": " + self.handle.before ) |
| 3924 | main.cleanup() |
| 3925 | main.exit() |
| 3926 | except Exception: |
| 3927 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3928 | main.cleanup() |
| 3929 | main.exit() |
| 3930 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3931 | def setTestAdd( self, setName, values ): |
| 3932 | """ |
| 3933 | CLI command to add elements to a distributed set. |
| 3934 | Arguments: |
| 3935 | setName - The name of the set to add to. |
| 3936 | values - The value(s) to add to the set, space seperated. |
| 3937 | Example usages: |
| 3938 | setTestAdd( "set1", "a b c" ) |
| 3939 | setTestAdd( "set2", "1" ) |
| 3940 | returns: |
| 3941 | main.TRUE on success OR |
| 3942 | main.FALSE if elements were already in the set OR |
| 3943 | main.ERROR on error |
| 3944 | """ |
| 3945 | try: |
| 3946 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
| 3947 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3948 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3949 | assert "Command not found:" not in output, output |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3950 | try: |
| 3951 | # TODO: Maybe make this less hardcoded |
| 3952 | # ConsistentMap Exceptions |
| 3953 | assert "org.onosproject.store.service" not in output |
| 3954 | # Node not leader |
| 3955 | assert "java.lang.IllegalStateException" not in output |
| 3956 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3957 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3958 | "command: " + str( output ) ) |
| 3959 | retryTime = 30 # Conservative time, given by Madan |
| 3960 | main.log.info( "Waiting " + str( retryTime ) + |
| 3961 | "seconds before retrying." ) |
| 3962 | time.sleep( retryTime ) # Due to change in mastership |
| 3963 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3964 | assert output is not None, "Error in sendline" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3965 | assert "Error executing command" not in output |
| 3966 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3967 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
| 3968 | main.log.info( self.name + ": " + output ) |
| 3969 | if re.search( positiveMatch, output): |
| 3970 | return main.TRUE |
| 3971 | elif re.search( negativeMatch, output): |
| 3972 | return main.FALSE |
| 3973 | else: |
| 3974 | main.log.error( self.name + ": setTestAdd did not" + |
| 3975 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3976 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3977 | return main.ERROR |
| 3978 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3979 | main.log.exception( "Error in processing '" + cmdStr + "' command. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3980 | return main.ERROR |
| 3981 | except TypeError: |
| 3982 | main.log.exception( self.name + ": Object not as expected" ) |
| 3983 | return main.ERROR |
| 3984 | except pexpect.EOF: |
| 3985 | main.log.error( self.name + ": EOF exception found" ) |
| 3986 | main.log.error( self.name + ": " + self.handle.before ) |
| 3987 | main.cleanup() |
| 3988 | main.exit() |
| 3989 | except Exception: |
| 3990 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3991 | main.cleanup() |
| 3992 | main.exit() |
| 3993 | |
| 3994 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 3995 | """ |
| 3996 | CLI command to remove elements from a distributed set. |
| 3997 | Required arguments: |
| 3998 | setName - The name of the set to remove from. |
| 3999 | values - The value(s) to remove from the set, space seperated. |
| 4000 | Optional arguments: |
| 4001 | clear - Clear all elements from the set |
| 4002 | retain - Retain only the given values. (intersection of the |
| 4003 | original set and the given set) |
| 4004 | returns: |
| 4005 | main.TRUE on success OR |
| 4006 | main.FALSE if the set was not changed OR |
| 4007 | main.ERROR on error |
| 4008 | """ |
| 4009 | try: |
| 4010 | cmdStr = "set-test-remove " |
| 4011 | if clear: |
| 4012 | cmdStr += "-c " + str( setName ) |
| 4013 | elif retain: |
| 4014 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4015 | else: |
| 4016 | cmdStr += str( setName ) + " " + str( values ) |
| 4017 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4018 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4019 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4020 | # TODO: Maybe make this less hardcoded |
| 4021 | # ConsistentMap Exceptions |
| 4022 | assert "org.onosproject.store.service" not in output |
| 4023 | # Node not leader |
| 4024 | assert "java.lang.IllegalStateException" not in output |
| 4025 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4026 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4027 | "command: " + str( output ) ) |
| 4028 | retryTime = 30 # Conservative time, given by Madan |
| 4029 | main.log.info( "Waiting " + str( retryTime ) + |
| 4030 | "seconds before retrying." ) |
| 4031 | time.sleep( retryTime ) # Due to change in mastership |
| 4032 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4033 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4034 | assert "Command not found:" not in output, output |
| 4035 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4036 | main.log.info( self.name + ": " + output ) |
| 4037 | if clear: |
| 4038 | pattern = "Set " + str( setName ) + " cleared" |
| 4039 | if re.search( pattern, output ): |
| 4040 | return main.TRUE |
| 4041 | elif retain: |
| 4042 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4043 | "only elements of set \[(.*)\]" |
| 4044 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4045 | "retaining only elements of the set " +\ |
| 4046 | "\[(.*)\]" |
| 4047 | if re.search( positivePattern, output ): |
| 4048 | return main.TRUE |
| 4049 | elif re.search( negativePattern, output ): |
| 4050 | return main.FALSE |
| 4051 | else: |
| 4052 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4053 | str( setName ) |
| 4054 | if ( len( values.split() ) == 1 ): |
| 4055 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4056 | str( setName ) |
| 4057 | else: |
| 4058 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4059 | str( setName ) |
| 4060 | if re.search( positivePattern, output ): |
| 4061 | return main.TRUE |
| 4062 | elif re.search( negativePattern, output ): |
| 4063 | return main.FALSE |
| 4064 | main.log.error( self.name + ": setTestRemove did not" + |
| 4065 | " match expected output" ) |
| 4066 | main.log.debug( self.name + " expected: " + pattern ) |
| 4067 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4068 | return main.ERROR |
| 4069 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4070 | main.log.exception( "Error in processing '" + cmdStr + "' commandr. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4071 | return main.ERROR |
| 4072 | except TypeError: |
| 4073 | main.log.exception( self.name + ": Object not as expected" ) |
| 4074 | return main.ERROR |
| 4075 | except pexpect.EOF: |
| 4076 | main.log.error( self.name + ": EOF exception found" ) |
| 4077 | main.log.error( self.name + ": " + self.handle.before ) |
| 4078 | main.cleanup() |
| 4079 | main.exit() |
| 4080 | except Exception: |
| 4081 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4082 | main.cleanup() |
| 4083 | main.exit() |
| 4084 | |
| 4085 | def setTestGet( self, setName, values="" ): |
| 4086 | """ |
| 4087 | CLI command to get the elements in a distributed set. |
| 4088 | Required arguments: |
| 4089 | setName - The name of the set to remove from. |
| 4090 | Optional arguments: |
| 4091 | values - The value(s) to check if in the set, space seperated. |
| 4092 | returns: |
| 4093 | main.ERROR on error OR |
| 4094 | A list of elements in the set if no optional arguments are |
| 4095 | supplied OR |
| 4096 | A tuple containing the list then: |
| 4097 | main.FALSE if the given values are not in the set OR |
| 4098 | main.TRUE if the given values are in the set OR |
| 4099 | """ |
| 4100 | try: |
| 4101 | values = str( values ).strip() |
| 4102 | setName = str( setName ).strip() |
| 4103 | length = len( values.split() ) |
| 4104 | containsCheck = None |
| 4105 | # Patterns to match |
| 4106 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4107 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4108 | containsTrue = "Set " + setName + " contains the value " + values |
| 4109 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4110 | values |
| 4111 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4112 | setPattern |
| 4113 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4114 | " subset " + setPattern |
| 4115 | |
| 4116 | cmdStr = "set-test-get " |
| 4117 | cmdStr += setName + " " + values |
| 4118 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4119 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4120 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4121 | # TODO: Maybe make this less hardcoded |
| 4122 | # ConsistentMap Exceptions |
| 4123 | assert "org.onosproject.store.service" not in output |
| 4124 | # Node not leader |
| 4125 | assert "java.lang.IllegalStateException" not in output |
| 4126 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4127 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4128 | "command: " + str( output ) ) |
| 4129 | retryTime = 30 # Conservative time, given by Madan |
| 4130 | main.log.info( "Waiting " + str( retryTime ) + |
| 4131 | "seconds before retrying." ) |
| 4132 | time.sleep( retryTime ) # Due to change in mastership |
| 4133 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4134 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4135 | assert "Command not found:" not in output, output |
| 4136 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4137 | main.log.info( self.name + ": " + output ) |
| 4138 | |
| 4139 | if length == 0: |
| 4140 | match = re.search( pattern, output ) |
| 4141 | else: # if given values |
| 4142 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4143 | patternTrue = pattern + "\r\n" + containsTrue |
| 4144 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4145 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4146 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4147 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4148 | matchTrue = re.search( patternTrue, output ) |
| 4149 | matchFalse = re.search( patternFalse, output ) |
| 4150 | if matchTrue: |
| 4151 | containsCheck = main.TRUE |
| 4152 | match = matchTrue |
| 4153 | elif matchFalse: |
| 4154 | containsCheck = main.FALSE |
| 4155 | match = matchFalse |
| 4156 | else: |
| 4157 | main.log.error( self.name + " setTestGet did not match " +\ |
| 4158 | "expected output" ) |
| 4159 | main.log.debug( self.name + " expected: " + pattern ) |
| 4160 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4161 | match = None |
| 4162 | if match: |
| 4163 | setMatch = match.group( 1 ) |
| 4164 | if setMatch == '': |
| 4165 | setList = [] |
| 4166 | else: |
| 4167 | setList = setMatch.split( ", " ) |
| 4168 | if length > 0: |
| 4169 | return ( setList, containsCheck ) |
| 4170 | else: |
| 4171 | return setList |
| 4172 | else: # no match |
| 4173 | main.log.error( self.name + ": setTestGet did not" + |
| 4174 | " match expected output" ) |
| 4175 | main.log.debug( self.name + " expected: " + pattern ) |
| 4176 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4177 | return main.ERROR |
| 4178 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4179 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4180 | return main.ERROR |
| 4181 | except TypeError: |
| 4182 | main.log.exception( self.name + ": Object not as expected" ) |
| 4183 | return main.ERROR |
| 4184 | except pexpect.EOF: |
| 4185 | main.log.error( self.name + ": EOF exception found" ) |
| 4186 | main.log.error( self.name + ": " + self.handle.before ) |
| 4187 | main.cleanup() |
| 4188 | main.exit() |
| 4189 | except Exception: |
| 4190 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4191 | main.cleanup() |
| 4192 | main.exit() |
| 4193 | |
| 4194 | def setTestSize( self, setName ): |
| 4195 | """ |
| 4196 | CLI command to get the elements in a distributed set. |
| 4197 | Required arguments: |
| 4198 | setName - The name of the set to remove from. |
| 4199 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4200 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4201 | None on error |
| 4202 | """ |
| 4203 | try: |
| 4204 | # TODO: Should this check against the number of elements returned |
| 4205 | # and then return true/false based on that? |
| 4206 | setName = str( setName ).strip() |
| 4207 | # Patterns to match |
| 4208 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4209 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4210 | setPattern |
| 4211 | cmdStr = "set-test-get -s " |
| 4212 | cmdStr += setName |
| 4213 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4214 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4215 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4216 | # TODO: Maybe make this less hardcoded |
| 4217 | # ConsistentMap Exceptions |
| 4218 | assert "org.onosproject.store.service" not in output |
| 4219 | # Node not leader |
| 4220 | assert "java.lang.IllegalStateException" not in output |
| 4221 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4222 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4223 | "command: " + str( output ) ) |
| 4224 | retryTime = 30 # Conservative time, given by Madan |
| 4225 | main.log.info( "Waiting " + str( retryTime ) + |
| 4226 | "seconds before retrying." ) |
| 4227 | time.sleep( retryTime ) # Due to change in mastership |
| 4228 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4229 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4230 | assert "Command not found:" not in output, output |
| 4231 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4232 | main.log.info( self.name + ": " + output ) |
| 4233 | match = re.search( pattern, output ) |
| 4234 | if match: |
| 4235 | setSize = int( match.group( 1 ) ) |
| 4236 | setMatch = match.group( 2 ) |
| 4237 | if len( setMatch.split() ) == setSize: |
| 4238 | main.log.info( "The size returned by " + self.name + |
| 4239 | " matches the number of elements in " + |
| 4240 | "the returned set" ) |
| 4241 | else: |
| 4242 | main.log.error( "The size returned by " + self.name + |
| 4243 | " does not match the number of " + |
| 4244 | "elements in the returned set." ) |
| 4245 | return setSize |
| 4246 | else: # no match |
| 4247 | main.log.error( self.name + ": setTestGet did not" + |
| 4248 | " match expected output" ) |
| 4249 | main.log.debug( self.name + " expected: " + pattern ) |
| 4250 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4251 | return None |
| 4252 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4253 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 4254 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4255 | except TypeError: |
| 4256 | main.log.exception( self.name + ": Object not as expected" ) |
| 4257 | return None |
| 4258 | except pexpect.EOF: |
| 4259 | main.log.error( self.name + ": EOF exception found" ) |
| 4260 | main.log.error( self.name + ": " + self.handle.before ) |
| 4261 | main.cleanup() |
| 4262 | main.exit() |
| 4263 | except Exception: |
| 4264 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4265 | main.cleanup() |
| 4266 | main.exit() |
| 4267 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4268 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4269 | """ |
| 4270 | Command to list the various counters in the system. |
| 4271 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4272 | if jsonFormat, a string of the json object returned by the cli |
| 4273 | command |
| 4274 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4275 | None on error |
| 4276 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4277 | try: |
| 4278 | counters = {} |
| 4279 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4280 | if jsonFormat: |
| 4281 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4282 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4283 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4284 | assert "Command not found:" not in output, output |
| 4285 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4286 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4287 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4288 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4289 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4290 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4291 | except TypeError: |
| 4292 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4293 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4294 | except pexpect.EOF: |
| 4295 | main.log.error( self.name + ": EOF exception found" ) |
| 4296 | main.log.error( self.name + ": " + self.handle.before ) |
| 4297 | main.cleanup() |
| 4298 | main.exit() |
| 4299 | except Exception: |
| 4300 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4301 | main.cleanup() |
| 4302 | main.exit() |
| 4303 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4304 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4305 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4306 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4307 | Required arguments: |
| 4308 | counter - The name of the counter to increment. |
| 4309 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4310 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4311 | returns: |
| 4312 | integer value of the counter or |
| 4313 | None on Error |
| 4314 | """ |
| 4315 | try: |
| 4316 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4317 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4318 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4319 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4320 | if delta != 1: |
| 4321 | cmdStr += " " + str( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4322 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4323 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4324 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4325 | # TODO: Maybe make this less hardcoded |
| 4326 | # ConsistentMap Exceptions |
| 4327 | assert "org.onosproject.store.service" not in output |
| 4328 | # Node not leader |
| 4329 | assert "java.lang.IllegalStateException" not in output |
| 4330 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4331 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4332 | "command: " + str( output ) ) |
| 4333 | retryTime = 30 # Conservative time, given by Madan |
| 4334 | main.log.info( "Waiting " + str( retryTime ) + |
| 4335 | "seconds before retrying." ) |
| 4336 | time.sleep( retryTime ) # Due to change in mastership |
| 4337 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4338 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4339 | assert "Command not found:" not in output, output |
| 4340 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4341 | main.log.info( self.name + ": " + output ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4342 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4343 | match = re.search( pattern, output ) |
| 4344 | if match: |
| 4345 | return int( match.group( 1 ) ) |
| 4346 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4347 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4348 | " match expected output." ) |
| 4349 | main.log.debug( self.name + " expected: " + pattern ) |
| 4350 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4351 | return None |
| 4352 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4353 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4354 | return None |
| 4355 | except TypeError: |
| 4356 | main.log.exception( self.name + ": Object not as expected" ) |
| 4357 | return None |
| 4358 | except pexpect.EOF: |
| 4359 | main.log.error( self.name + ": EOF exception found" ) |
| 4360 | main.log.error( self.name + ": " + self.handle.before ) |
| 4361 | main.cleanup() |
| 4362 | main.exit() |
| 4363 | except Exception: |
| 4364 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4365 | main.cleanup() |
| 4366 | main.exit() |
| 4367 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4368 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4369 | """ |
| 4370 | CLI command to get a distributed counter then add a delta to it. |
| 4371 | Required arguments: |
| 4372 | counter - The name of the counter to increment. |
| 4373 | Optional arguments: |
| 4374 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4375 | returns: |
| 4376 | integer value of the counter or |
| 4377 | None on Error |
| 4378 | """ |
| 4379 | try: |
| 4380 | counter = str( counter ) |
| 4381 | delta = int( delta ) |
| 4382 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4383 | cmdStr += counter |
| 4384 | if delta != 1: |
| 4385 | cmdStr += " " + str( delta ) |
| 4386 | output = self.sendline( cmdStr ) |
| 4387 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4388 | assert output is not None, "Error in sendline" |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4389 | # TODO: Maybe make this less hardcoded |
| 4390 | # ConsistentMap Exceptions |
| 4391 | assert "org.onosproject.store.service" not in output |
| 4392 | # Node not leader |
| 4393 | assert "java.lang.IllegalStateException" not in output |
| 4394 | except AssertionError: |
| 4395 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4396 | "command: " + str( output ) ) |
| 4397 | retryTime = 30 # Conservative time, given by Madan |
| 4398 | main.log.info( "Waiting " + str( retryTime ) + |
| 4399 | "seconds before retrying." ) |
| 4400 | time.sleep( retryTime ) # Due to change in mastership |
| 4401 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4402 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4403 | assert "Command not found:" not in output, output |
| 4404 | assert "Error executing command" not in output, output |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4405 | main.log.info( self.name + ": " + output ) |
| 4406 | pattern = counter + " was updated to (-?\d+)" |
| 4407 | match = re.search( pattern, output ) |
| 4408 | if match: |
| 4409 | return int( match.group( 1 ) ) |
| 4410 | else: |
| 4411 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4412 | " match expected output." ) |
| 4413 | main.log.debug( self.name + " expected: " + pattern ) |
| 4414 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4415 | return None |
| 4416 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4417 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4418 | return None |
| 4419 | except TypeError: |
| 4420 | main.log.exception( self.name + ": Object not as expected" ) |
| 4421 | return None |
| 4422 | except pexpect.EOF: |
| 4423 | main.log.error( self.name + ": EOF exception found" ) |
| 4424 | main.log.error( self.name + ": " + self.handle.before ) |
| 4425 | main.cleanup() |
| 4426 | main.exit() |
| 4427 | except Exception: |
| 4428 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4429 | main.cleanup() |
| 4430 | main.exit() |
| 4431 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4432 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4433 | """ |
| 4434 | Description: Execute summary command in onos |
| 4435 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4436 | no output |
| 4437 | |
| 4438 | """ |
| 4439 | try: |
| 4440 | cmdStr = "summary" |
| 4441 | if jsonFormat: |
| 4442 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4443 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4444 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4445 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4446 | assert "Error:" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4447 | if not handle: |
| 4448 | main.log.error( self.name + ": There is no output in " + |
| 4449 | "summary command" ) |
| 4450 | return main.FALSE |
| 4451 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4452 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4453 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4454 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4455 | except TypeError: |
| 4456 | main.log.exception( self.name + ": Object not as expected" ) |
| 4457 | return None |
| 4458 | except pexpect.EOF: |
| 4459 | main.log.error( self.name + ": EOF exception found" ) |
| 4460 | main.log.error( self.name + ": " + self.handle.before ) |
| 4461 | main.cleanup() |
| 4462 | main.exit() |
| 4463 | except Exception: |
| 4464 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4465 | main.cleanup() |
| 4466 | main.exit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4467 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4468 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4469 | """ |
| 4470 | CLI command to get the value of a key in a consistent map using |
| 4471 | transactions. This a test function and can only get keys from the |
| 4472 | test map hard coded into the cli command |
| 4473 | Required arguments: |
| 4474 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4475 | returns: |
| 4476 | The string value of the key or |
| 4477 | None on Error |
| 4478 | """ |
| 4479 | try: |
| 4480 | keyName = str( keyName ) |
| 4481 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4482 | cmdStr += keyName |
| 4483 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4484 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4485 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4486 | try: |
| 4487 | # TODO: Maybe make this less hardcoded |
| 4488 | # ConsistentMap Exceptions |
| 4489 | assert "org.onosproject.store.service" not in output |
| 4490 | # Node not leader |
| 4491 | assert "java.lang.IllegalStateException" not in output |
| 4492 | except AssertionError: |
| 4493 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4494 | "command: " + str( output ) ) |
| 4495 | return None |
| 4496 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4497 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4498 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4499 | return None |
| 4500 | else: |
| 4501 | match = re.search( pattern, output ) |
| 4502 | if match: |
| 4503 | return match.groupdict()[ 'value' ] |
| 4504 | else: |
| 4505 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4506 | " match expected output." ) |
| 4507 | main.log.debug( self.name + " expected: " + pattern ) |
| 4508 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4509 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4510 | except AssertionError: |
| 4511 | main.log.exception( "" ) |
| 4512 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4513 | except TypeError: |
| 4514 | main.log.exception( self.name + ": Object not as expected" ) |
| 4515 | return None |
| 4516 | except pexpect.EOF: |
| 4517 | main.log.error( self.name + ": EOF exception found" ) |
| 4518 | main.log.error( self.name + ": " + self.handle.before ) |
| 4519 | main.cleanup() |
| 4520 | main.exit() |
| 4521 | except Exception: |
| 4522 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4523 | main.cleanup() |
| 4524 | main.exit() |
| 4525 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4526 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4527 | """ |
| 4528 | CLI command to put a value into 'numKeys' number of keys in a |
| 4529 | consistent map using transactions. This a test function and can only |
| 4530 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4531 | Required arguments: |
| 4532 | numKeys - Number of keys to add the value to |
| 4533 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4534 | returns: |
| 4535 | A dictionary whose keys are the name of the keys put into the map |
| 4536 | and the values of the keys are dictionaries whose key-values are |
| 4537 | 'value': value put into map and optionaly |
| 4538 | 'oldValue': Previous value in the key or |
| 4539 | None on Error |
| 4540 | |
| 4541 | Example output |
| 4542 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4543 | 'Key2': {'value': 'Testing'} } |
| 4544 | """ |
| 4545 | try: |
| 4546 | numKeys = str( numKeys ) |
| 4547 | value = str( value ) |
| 4548 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4549 | cmdStr += numKeys + " " + value |
| 4550 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4551 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4552 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4553 | try: |
| 4554 | # TODO: Maybe make this less hardcoded |
| 4555 | # ConsistentMap Exceptions |
| 4556 | assert "org.onosproject.store.service" not in output |
| 4557 | # Node not leader |
| 4558 | assert "java.lang.IllegalStateException" not in output |
| 4559 | except AssertionError: |
| 4560 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4561 | "command: " + str( output ) ) |
| 4562 | return None |
| 4563 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4564 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4565 | results = {} |
| 4566 | for line in output.splitlines(): |
| 4567 | new = re.search( newPattern, line ) |
| 4568 | updated = re.search( updatedPattern, line ) |
| 4569 | if new: |
| 4570 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4571 | elif updated: |
| 4572 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4573 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4574 | else: |
| 4575 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4576 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4577 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4578 | newPattern, |
| 4579 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4580 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4581 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4582 | except AssertionError: |
| 4583 | main.log.exception( "" ) |
| 4584 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4585 | except TypeError: |
| 4586 | main.log.exception( self.name + ": Object not as expected" ) |
| 4587 | return None |
| 4588 | except pexpect.EOF: |
| 4589 | main.log.error( self.name + ": EOF exception found" ) |
| 4590 | main.log.error( self.name + ": " + self.handle.before ) |
| 4591 | main.cleanup() |
| 4592 | main.exit() |
| 4593 | except Exception: |
| 4594 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4595 | main.cleanup() |
| 4596 | main.exit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4597 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4598 | def maps( self, jsonFormat=True ): |
| 4599 | """ |
| 4600 | Description: Returns result of onos:maps |
| 4601 | Optional: |
| 4602 | * jsonFormat: enable json formatting of output |
| 4603 | """ |
| 4604 | try: |
| 4605 | cmdStr = "maps" |
| 4606 | if jsonFormat: |
| 4607 | cmdStr += " -j" |
| 4608 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4609 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4610 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4611 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4612 | except AssertionError: |
| 4613 | main.log.exception( "" ) |
| 4614 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4615 | except TypeError: |
| 4616 | main.log.exception( self.name + ": Object not as expected" ) |
| 4617 | return None |
| 4618 | except pexpect.EOF: |
| 4619 | main.log.error( self.name + ": EOF exception found" ) |
| 4620 | main.log.error( self.name + ": " + self.handle.before ) |
| 4621 | main.cleanup() |
| 4622 | main.exit() |
| 4623 | except Exception: |
| 4624 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4625 | main.cleanup() |
| 4626 | main.exit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4627 | |
| 4628 | def getSwController( self, uri, jsonFormat=True ): |
| 4629 | """ |
| 4630 | Descrition: Gets the controller information from the device |
| 4631 | """ |
| 4632 | try: |
| 4633 | cmd = "device-controllers " |
| 4634 | if jsonFormat: |
| 4635 | cmd += "-j " |
| 4636 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4637 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4638 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4639 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4640 | except AssertionError: |
| 4641 | main.log.exception( "" ) |
| 4642 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4643 | except TypeError: |
| 4644 | main.log.exception( self.name + ": Object not as expected" ) |
| 4645 | return None |
| 4646 | except pexpect.EOF: |
| 4647 | main.log.error( self.name + ": EOF exception found" ) |
| 4648 | main.log.error( self.name + ": " + self.handle.before ) |
| 4649 | main.cleanup() |
| 4650 | main.exit() |
| 4651 | except Exception: |
| 4652 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4653 | main.cleanup() |
| 4654 | main.exit() |
| 4655 | |
| 4656 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4657 | """ |
| 4658 | Descrition: sets the controller(s) for the specified device |
| 4659 | |
| 4660 | Parameters: |
| 4661 | Required: uri - String: The uri of the device(switch). |
| 4662 | ip - String or List: The ip address of the controller. |
| 4663 | This parameter can be formed in a couple of different ways. |
| 4664 | VALID: |
| 4665 | 10.0.0.1 - just the ip address |
| 4666 | tcp:10.0.0.1 - the protocol and the ip address |
| 4667 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4668 | so that you can add controllers with different |
| 4669 | protocols and ports |
| 4670 | INVALID: |
| 4671 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4672 | |
| 4673 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4674 | port - The port number. |
| 4675 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4676 | |
| 4677 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4678 | """ |
| 4679 | try: |
| 4680 | cmd = "device-setcontrollers" |
| 4681 | |
| 4682 | if jsonFormat: |
| 4683 | cmd += " -j" |
| 4684 | cmd += " " + uri |
| 4685 | if isinstance( ip, str ): |
| 4686 | ip = [ip] |
| 4687 | for item in ip: |
| 4688 | if ":" in item: |
| 4689 | sitem = item.split( ":" ) |
| 4690 | if len(sitem) == 3: |
| 4691 | cmd += " " + item |
| 4692 | elif "." in sitem[1]: |
| 4693 | cmd += " {}:{}".format(item, port) |
| 4694 | else: |
| 4695 | main.log.error( "Malformed entry: " + item ) |
| 4696 | raise TypeError |
| 4697 | else: |
| 4698 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4699 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4700 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4701 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4702 | if "Error" in response: |
| 4703 | main.log.error( response ) |
| 4704 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4705 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4706 | except AssertionError: |
| 4707 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4708 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4709 | except TypeError: |
| 4710 | main.log.exception( self.name + ": Object not as expected" ) |
| 4711 | return main.FALSE |
| 4712 | except pexpect.EOF: |
| 4713 | main.log.error( self.name + ": EOF exception found" ) |
| 4714 | main.log.error( self.name + ": " + self.handle.before ) |
| 4715 | main.cleanup() |
| 4716 | main.exit() |
| 4717 | except Exception: |
| 4718 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4719 | main.cleanup() |
| 4720 | main.exit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4721 | |
| 4722 | def removeDevice( self, device ): |
| 4723 | ''' |
| 4724 | Description: |
| 4725 | Remove a device from ONOS by passing the uri of the device(s). |
| 4726 | Parameters: |
| 4727 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
| 4728 | Returns: |
| 4729 | Returns main.FALSE if an exception is thrown or an error is present |
| 4730 | in the response. Otherwise, returns main.TRUE. |
| 4731 | NOTE: |
| 4732 | If a host cannot be removed, then this function will return main.FALSE |
| 4733 | ''' |
| 4734 | try: |
| 4735 | if type( device ) is str: |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 4736 | deviceStr = device |
| 4737 | device = [] |
| 4738 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4739 | |
| 4740 | for d in device: |
| 4741 | time.sleep( 1 ) |
| 4742 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4743 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4744 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4745 | if "Error" in response: |
| 4746 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4747 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4748 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4749 | except AssertionError: |
| 4750 | main.log.exception( "" ) |
| 4751 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4752 | except TypeError: |
| 4753 | main.log.exception( self.name + ": Object not as expected" ) |
| 4754 | return main.FALSE |
| 4755 | except pexpect.EOF: |
| 4756 | main.log.error( self.name + ": EOF exception found" ) |
| 4757 | main.log.error( self.name + ": " + self.handle.before ) |
| 4758 | main.cleanup() |
| 4759 | main.exit() |
| 4760 | except Exception: |
| 4761 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4762 | main.cleanup() |
| 4763 | main.exit() |
| 4764 | |
| 4765 | def removeHost( self, host ): |
| 4766 | ''' |
| 4767 | Description: |
| 4768 | Remove a host from ONOS by passing the id of the host(s) |
| 4769 | Parameters: |
| 4770 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
| 4771 | Returns: |
| 4772 | Returns main.FALSE if an exception is thrown or an error is present |
| 4773 | in the response. Otherwise, returns main.TRUE. |
| 4774 | NOTE: |
| 4775 | If a host cannot be removed, then this function will return main.FALSE |
| 4776 | ''' |
| 4777 | try: |
| 4778 | if type( host ) is str: |
| 4779 | host = list( host ) |
| 4780 | |
| 4781 | for h in host: |
| 4782 | time.sleep( 1 ) |
| 4783 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4784 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4785 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4786 | if "Error" in response: |
| 4787 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 4788 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4789 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4790 | except AssertionError: |
| 4791 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4792 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4793 | except TypeError: |
| 4794 | main.log.exception( self.name + ": Object not as expected" ) |
| 4795 | return main.FALSE |
| 4796 | except pexpect.EOF: |
| 4797 | main.log.error( self.name + ": EOF exception found" ) |
| 4798 | main.log.error( self.name + ": " + self.handle.before ) |
| 4799 | main.cleanup() |
| 4800 | main.exit() |
| 4801 | except Exception: |
| 4802 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4803 | main.cleanup() |
| 4804 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4805 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4806 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4807 | ''' |
| 4808 | Description: |
| 4809 | Bring link down or up in the null-provider. |
| 4810 | params: |
| 4811 | begin - (string) One end of a device or switch. |
| 4812 | end - (string) the other end of the device or switch |
| 4813 | returns: |
| 4814 | main.TRUE if no exceptions were thrown and no Errors are |
| 4815 | present in the resoponse. Otherwise, returns main.FALSE |
| 4816 | ''' |
| 4817 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4818 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4819 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4820 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4821 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4822 | if "Error" in response or "Failure" in response: |
| 4823 | main.log.error( response ) |
| 4824 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4825 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4826 | except AssertionError: |
| 4827 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4828 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4829 | except TypeError: |
| 4830 | main.log.exception( self.name + ": Object not as expected" ) |
| 4831 | return main.FALSE |
| 4832 | except pexpect.EOF: |
| 4833 | main.log.error( self.name + ": EOF exception found" ) |
| 4834 | main.log.error( self.name + ": " + self.handle.before ) |
| 4835 | main.cleanup() |
| 4836 | main.exit() |
| 4837 | except Exception: |
| 4838 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4839 | main.cleanup() |
| 4840 | main.exit() |
| 4841 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4842 | def portstate( self, dpid, port, state ): |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4843 | ''' |
| 4844 | Description: |
| 4845 | Changes the state of port in an OF switch by means of the |
| 4846 | PORTSTATUS OF messages. |
| 4847 | params: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4848 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 4849 | port - (string) target port in the device. Ex: '2' |
| 4850 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4851 | returns: |
| 4852 | main.TRUE if no exceptions were thrown and no Errors are |
| 4853 | present in the resoponse. Otherwise, returns main.FALSE |
| 4854 | ''' |
| 4855 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4856 | state = state.lower() |
| 4857 | assert state == 'enable' or state == 'disable', "Unknown state" |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4858 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
| 4859 | response = self.sendline( cmd, showResponse=True ) |
| 4860 | assert response is not None, "Error in sendline" |
| 4861 | assert "Command not found:" not in response, response |
| 4862 | if "Error" in response or "Failure" in response: |
| 4863 | main.log.error( response ) |
| 4864 | return main.FALSE |
| 4865 | return main.TRUE |
| 4866 | except AssertionError: |
| 4867 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4868 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4869 | except TypeError: |
| 4870 | main.log.exception( self.name + ": Object not as expected" ) |
| 4871 | return main.FALSE |
| 4872 | except pexpect.EOF: |
| 4873 | main.log.error( self.name + ": EOF exception found" ) |
| 4874 | main.log.error( self.name + ": " + self.handle.before ) |
| 4875 | main.cleanup() |
| 4876 | main.exit() |
| 4877 | except Exception: |
| 4878 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4879 | main.cleanup() |
| 4880 | main.exit() |
| 4881 | |
| 4882 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 4883 | """ |
| 4884 | Set the logging level to lvl for a specific app |
| 4885 | returns main.TRUE on success |
| 4886 | returns main.FALSE if Error occurred |
| 4887 | if noExit is True, TestON will not exit, but clean up |
| 4888 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 4889 | Level defaults to INFO |
| 4890 | """ |
| 4891 | try: |
| 4892 | self.handle.sendline( "log:set %s %s" %( level, app ) ) |
| 4893 | self.handle.expect( "onos>" ) |
| 4894 | |
| 4895 | response = self.handle.before |
| 4896 | if re.search( "Error", response ): |
| 4897 | return main.FALSE |
| 4898 | return main.TRUE |
| 4899 | except pexpect.TIMEOUT: |
| 4900 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 4901 | main.cleanup() |
| 4902 | main.exit() |
| 4903 | except pexpect.EOF: |
| 4904 | main.log.error( self.name + ": EOF exception found" ) |
| 4905 | main.log.error( self.name + ": " + self.handle.before ) |
| 4906 | main.cleanup() |
| 4907 | main.exit() |
| 4908 | except Exception: |
| 4909 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4910 | main.cleanup() |
| 4911 | main.exit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4912 | |
| 4913 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 4914 | """ |
| 4915 | Return a dictionary which describes the latest network topology data as a |
| 4916 | graph. |
| 4917 | An example of the dictionary: |
| 4918 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 4919 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 4920 | Each vertex should at least have an 'edges' attribute which describes the |
| 4921 | adjacency information. The value of 'edges' attribute is also represented by |
| 4922 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 4923 | list of attributes. |
| 4924 | An example of the edges dictionary: |
| 4925 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 4926 | vertex3: { 'port': ..., 'weight': ... } } |
| 4927 | If includeHost == True, all hosts (and host-switch links) will be included |
| 4928 | in topology data. |
| 4929 | """ |
| 4930 | graphDict = {} |
| 4931 | try: |
| 4932 | links = self.links() |
| 4933 | links = json.loads( links ) |
| 4934 | devices = self.devices() |
| 4935 | devices = json.loads( devices ) |
| 4936 | idToDevice = {} |
| 4937 | for device in devices: |
| 4938 | idToDevice[ device[ 'id' ] ] = device |
| 4939 | if includeHost: |
| 4940 | hosts = self.hosts() |
| 4941 | # FIXME: support 'includeHost' argument |
| 4942 | for link in links: |
| 4943 | nodeA = link[ 'src' ][ 'device' ] |
| 4944 | nodeB = link[ 'dst' ][ 'device' ] |
| 4945 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
| 4946 | if not nodeA in graphDict.keys(): |
| 4947 | graphDict[ nodeA ] = { 'edges':{}, |
| 4948 | 'dpid':idToDevice[ nodeA ][ 'id' ][3:], |
| 4949 | 'type':idToDevice[ nodeA ][ 'type' ], |
| 4950 | 'available':idToDevice[ nodeA ][ 'available' ], |
| 4951 | 'role':idToDevice[ nodeA ][ 'role' ], |
| 4952 | 'mfr':idToDevice[ nodeA ][ 'mfr' ], |
| 4953 | 'hw':idToDevice[ nodeA ][ 'hw' ], |
| 4954 | 'sw':idToDevice[ nodeA ][ 'sw' ], |
| 4955 | 'serial':idToDevice[ nodeA ][ 'serial' ], |
| 4956 | 'chassisId':idToDevice[ nodeA ][ 'chassisId' ], |
| 4957 | 'annotations':idToDevice[ nodeA ][ 'annotations' ]} |
| 4958 | else: |
| 4959 | # Assert nodeB is not connected to any current links of nodeA |
| 4960 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 4961 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port':link[ 'src' ][ 'port' ], |
| 4962 | 'type':link[ 'type' ], |
| 4963 | 'state':link[ 'state' ] } |
| 4964 | return graphDict |
| 4965 | except ( TypeError, ValueError ): |
| 4966 | main.log.exception( self.name + ": Object not as expected" ) |
| 4967 | return None |
| 4968 | except KeyError: |
| 4969 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4970 | return None |
| 4971 | except AssertionError: |
| 4972 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4973 | return None |
| 4974 | except pexpect.EOF: |
| 4975 | main.log.error( self.name + ": EOF exception found" ) |
| 4976 | main.log.error( self.name + ": " + self.handle.before ) |
| 4977 | return None |
| 4978 | except Exception: |
| 4979 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4980 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 4981 | |
| 4982 | def getIntentPerfSummary( self ): |
| 4983 | ''' |
| 4984 | Send command to check intent-perf summary |
| 4985 | Returns: dictionary for intent-perf summary |
| 4986 | if something wrong, function will return None |
| 4987 | ''' |
| 4988 | cmd = "intent-perf -s" |
| 4989 | respDic = {} |
| 4990 | resp = self.sendline( cmd ) |
| 4991 | try: |
| 4992 | # Generate the dictionary to return |
| 4993 | for l in resp.split( "\n" ): |
| 4994 | # Delete any white space in line |
| 4995 | temp = re.sub( r'\s+', '', l ) |
| 4996 | temp = temp.split( ":" ) |
| 4997 | respDic[ temp[0] ] = temp[ 1 ] |
| 4998 | |
| 4999 | except (TypeError, ValueError): |
| 5000 | main.log.exception( self.name + ": Object not as expected" ) |
| 5001 | return None |
| 5002 | except KeyError: |
| 5003 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5004 | return None |
| 5005 | except AssertionError: |
| 5006 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5007 | return None |
| 5008 | except pexpect.EOF: |
| 5009 | main.log.error( self.name + ": EOF exception found" ) |
| 5010 | main.log.error( self.name + ": " + self.handle.before ) |
| 5011 | return None |
| 5012 | except Exception: |
| 5013 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5014 | return None |
| 5015 | return respDic |
| 5016 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5017 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5018 | """ |
| 5019 | Searches the latest ONOS log file for the given search term and |
| 5020 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5021 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5022 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5023 | searchTerm: |
| 5024 | The string to grep from the ONOS log. |
| 5025 | startLine: |
| 5026 | The term that decides which line is the start to search the searchTerm in |
| 5027 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5028 | logNum: |
| 5029 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5030 | information.Because of this, search mutiply logs is necessary to capture |
| 5031 | the right result. logNum is the number of karaf logs that we need to search |
| 5032 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5033 | mode: |
| 5034 | all: return all the strings that contain the search term |
| 5035 | last: return the last string that contains the search term |
| 5036 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5037 | num: return the number of times that the searchTerm appears in the log |
| 5038 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5039 | """ |
| 5040 | try: |
| 5041 | assert type( searchTerm ) is str |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5042 | #Build the log paths string |
| 5043 | logPath = '/opt/onos/log/karaf.log.' |
| 5044 | logPaths = '/opt/onos/log/karaf.log' |
| 5045 | for i in range( 1, logNum ): |
| 5046 | logPaths = logPath + str( i ) + " " + logPaths |
| 5047 | cmd = "cat " + logPaths |
| 5048 | if mode == 'all': |
| 5049 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5050 | if mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5051 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5052 | if mode == 'first': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5053 | if startLine != '': |
| 5054 | # 100000000 is just a extreme large number to make sure this function can grep all the lines after startLine |
| 5055 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\' | grep \'" + searchTerm + "\'" + "| head -n 1" |
| 5056 | else: |
| 5057 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5058 | if mode == 'num': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5059 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
You Wang | 118ba58 | 2017-01-02 17:14:43 -0800 | [diff] [blame] | 5060 | num = self.sendline( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5061 | return num |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5062 | if mode == 'total': |
| 5063 | totalLines = self.sendline( "cat /opt/onos/log/karaf.log | wc -l" ) |
| 5064 | return int(totalLines) |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5065 | before = self.sendline( cmd ) |
| 5066 | before = before.splitlines() |
| 5067 | # make sure the returned list only contains the search term |
| 5068 | returnLines = [line for line in before if searchTerm in line] |
| 5069 | return returnLines |
| 5070 | except AssertionError: |
| 5071 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5072 | return None |
| 5073 | except pexpect.EOF: |
| 5074 | main.log.error( self.name + ": EOF exception found" ) |
| 5075 | main.log.error( self.name + ": " + self.handle.before ) |
| 5076 | main.cleanup() |
| 5077 | main.exit() |
| 5078 | except pexpect.TIMEOUT: |
| 5079 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5080 | main.log.error( self.name + ": " + self.handle.before ) |
| 5081 | main.cleanup() |
| 5082 | main.exit() |
| 5083 | except Exception: |
| 5084 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5085 | main.cleanup() |
| 5086 | main.exit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5087 | |
| 5088 | def vplsShow( self, jsonFormat=True ): |
| 5089 | """ |
| 5090 | Description: Returns result of onos:vpls show, which should list the |
| 5091 | configured VPLS networks and the assigned interfaces. |
| 5092 | Optional: |
| 5093 | * jsonFormat: enable json formatting of output |
| 5094 | Returns: |
| 5095 | The output of the command or None on error. |
| 5096 | """ |
| 5097 | try: |
| 5098 | cmdStr = "vpls show" |
| 5099 | if jsonFormat: |
| 5100 | raise NotImplementedError |
| 5101 | cmdStr += " -j" |
| 5102 | handle = self.sendline( cmdStr ) |
| 5103 | assert handle is not None, "Error in sendline" |
| 5104 | assert "Command not found:" not in handle, handle |
| 5105 | return handle |
| 5106 | except AssertionError: |
| 5107 | main.log.exception( "" ) |
| 5108 | return None |
| 5109 | except TypeError: |
| 5110 | main.log.exception( self.name + ": Object not as expected" ) |
| 5111 | return None |
| 5112 | except pexpect.EOF: |
| 5113 | main.log.error( self.name + ": EOF exception found" ) |
| 5114 | main.log.error( self.name + ": " + self.handle.before ) |
| 5115 | main.cleanup() |
| 5116 | main.exit() |
| 5117 | except NotImplementedError: |
| 5118 | main.log.exception( self.name + ": Json output not supported") |
| 5119 | return None |
| 5120 | except Exception: |
| 5121 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5122 | main.cleanup() |
| 5123 | main.exit() |
| 5124 | |
| 5125 | def parseVplsShow( self ): |
| 5126 | """ |
| 5127 | Parse the cli output of 'vpls show' into json output. This is required |
| 5128 | as there is currently no json output available. |
| 5129 | """ |
| 5130 | try: |
| 5131 | output = [] |
| 5132 | raw = self.vplsShow( jsonFormat=False ) |
| 5133 | namePat = "VPLS name: (?P<name>\w+)" |
| 5134 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5135 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5136 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5137 | mIter = re.finditer( pattern, raw ) |
| 5138 | for match in mIter: |
| 5139 | item = {} |
| 5140 | item[ 'name' ] = match.group( 'name' ) |
| 5141 | ifaces = match.group( 'interfaces' ).split( ', ') |
| 5142 | if ifaces == [ "" ]: |
| 5143 | ifaces = [] |
| 5144 | item[ 'interfaces' ] = ifaces |
| 5145 | encap = match.group( 'encap' ) |
| 5146 | if encap != 'NONE': |
| 5147 | item[ 'encapsulation' ] = encap.lower() |
| 5148 | output.append( item ) |
| 5149 | return output |
| 5150 | except Exception: |
| 5151 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5152 | main.cleanup() |
| 5153 | main.exit() |
| 5154 | |
| 5155 | def vplsList( self, jsonFormat=True ): |
| 5156 | """ |
| 5157 | Description: Returns result of onos:vpls list, which should list the |
| 5158 | configured VPLS networks. |
| 5159 | Optional: |
| 5160 | * jsonFormat: enable json formatting of output |
| 5161 | """ |
| 5162 | try: |
| 5163 | cmdStr = "vpls list" |
| 5164 | if jsonFormat: |
| 5165 | raise NotImplementedError |
| 5166 | cmdStr += " -j" |
| 5167 | handle = self.sendline( cmdStr ) |
| 5168 | assert handle is not None, "Error in sendline" |
| 5169 | assert "Command not found:" not in handle, handle |
| 5170 | return handle |
| 5171 | except AssertionError: |
| 5172 | main.log.exception( "" ) |
| 5173 | return None |
| 5174 | except TypeError: |
| 5175 | main.log.exception( self.name + ": Object not as expected" ) |
| 5176 | return None |
| 5177 | except pexpect.EOF: |
| 5178 | main.log.error( self.name + ": EOF exception found" ) |
| 5179 | main.log.error( self.name + ": " + self.handle.before ) |
| 5180 | main.cleanup() |
| 5181 | main.exit() |
| 5182 | except NotImplementedError: |
| 5183 | main.log.exception( self.name + ": Json output not supported") |
| 5184 | return None |
| 5185 | except Exception: |
| 5186 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5187 | main.cleanup() |
| 5188 | main.exit() |
| 5189 | |
| 5190 | def vplsCreate( self, network ): |
| 5191 | """ |
| 5192 | CLI command to create a new VPLS network. |
| 5193 | Required arguments: |
| 5194 | network - String name of the network to create. |
| 5195 | returns: |
| 5196 | main.TRUE on success and main.FALSE on failure |
| 5197 | """ |
| 5198 | try: |
| 5199 | network = str( network ) |
| 5200 | cmdStr = "vpls create " |
| 5201 | cmdStr += network |
| 5202 | output = self.sendline( cmdStr ) |
| 5203 | assert output is not None, "Error in sendline" |
| 5204 | assert "Command not found:" not in output, output |
| 5205 | assert "Error executing command" not in output, output |
| 5206 | assert "VPLS already exists:" not in output, output |
| 5207 | return main.TRUE |
| 5208 | except AssertionError: |
| 5209 | main.log.exception( "" ) |
| 5210 | return main.FALSE |
| 5211 | except TypeError: |
| 5212 | main.log.exception( self.name + ": Object not as expected" ) |
| 5213 | return main.FALSE |
| 5214 | except pexpect.EOF: |
| 5215 | main.log.error( self.name + ": EOF exception found" ) |
| 5216 | main.log.error( self.name + ": " + self.handle.before ) |
| 5217 | main.cleanup() |
| 5218 | main.exit() |
| 5219 | except Exception: |
| 5220 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5221 | main.cleanup() |
| 5222 | main.exit() |
| 5223 | |
| 5224 | def vplsDelete( self, network ): |
| 5225 | """ |
| 5226 | CLI command to delete a VPLS network. |
| 5227 | Required arguments: |
| 5228 | network - Name of the network to delete. |
| 5229 | returns: |
| 5230 | main.TRUE on success and main.FALSE on failure |
| 5231 | """ |
| 5232 | try: |
| 5233 | network = str( network ) |
| 5234 | cmdStr = "vpls delete " |
| 5235 | cmdStr += network |
| 5236 | output = self.sendline( cmdStr ) |
| 5237 | assert output is not None, "Error in sendline" |
| 5238 | assert "Command not found:" not in output, output |
| 5239 | assert "Error executing command" not in output, output |
| 5240 | assert " not found" not in output, output |
| 5241 | return main.TRUE |
| 5242 | except AssertionError: |
| 5243 | main.log.exception( "" ) |
| 5244 | return main.FALSE |
| 5245 | except TypeError: |
| 5246 | main.log.exception( self.name + ": Object not as expected" ) |
| 5247 | return main.FALSE |
| 5248 | except pexpect.EOF: |
| 5249 | main.log.error( self.name + ": EOF exception found" ) |
| 5250 | main.log.error( self.name + ": " + self.handle.before ) |
| 5251 | main.cleanup() |
| 5252 | main.exit() |
| 5253 | except Exception: |
| 5254 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5255 | main.cleanup() |
| 5256 | main.exit() |
| 5257 | |
| 5258 | def vplsAddIface( self, network, iface ): |
| 5259 | """ |
| 5260 | CLI command to add an interface to a VPLS network. |
| 5261 | Required arguments: |
| 5262 | network - Name of the network to add the interface to. |
| 5263 | iface - The ONOS name for an interface. |
| 5264 | returns: |
| 5265 | main.TRUE on success and main.FALSE on failure |
| 5266 | """ |
| 5267 | try: |
| 5268 | network = str( network ) |
| 5269 | iface = str( iface ) |
| 5270 | cmdStr = "vpls add-if " |
| 5271 | cmdStr += network + " " + iface |
| 5272 | output = self.sendline( cmdStr ) |
| 5273 | assert output is not None, "Error in sendline" |
| 5274 | assert "Command not found:" not in output, output |
| 5275 | assert "Error executing command" not in output, output |
| 5276 | assert "already associated to network" not in output, output |
| 5277 | assert "Interface cannot be added." not in output, output |
| 5278 | return main.TRUE |
| 5279 | except AssertionError: |
| 5280 | main.log.exception( "" ) |
| 5281 | return main.FALSE |
| 5282 | except TypeError: |
| 5283 | main.log.exception( self.name + ": Object not as expected" ) |
| 5284 | return main.FALSE |
| 5285 | except pexpect.EOF: |
| 5286 | main.log.error( self.name + ": EOF exception found" ) |
| 5287 | main.log.error( self.name + ": " + self.handle.before ) |
| 5288 | main.cleanup() |
| 5289 | main.exit() |
| 5290 | except Exception: |
| 5291 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5292 | main.cleanup() |
| 5293 | main.exit() |
| 5294 | |
| 5295 | def vplsRemIface( self, network, iface ): |
| 5296 | """ |
| 5297 | CLI command to remove an interface from a VPLS network. |
| 5298 | Required arguments: |
| 5299 | network - Name of the network to remove the interface from. |
| 5300 | iface - Name of the interface to remove. |
| 5301 | returns: |
| 5302 | main.TRUE on success and main.FALSE on failure |
| 5303 | """ |
| 5304 | try: |
| 5305 | iface = str( iface ) |
| 5306 | cmdStr = "vpls rem-if " |
| 5307 | cmdStr += network + " " + iface |
| 5308 | output = self.sendline( cmdStr ) |
| 5309 | assert output is not None, "Error in sendline" |
| 5310 | assert "Command not found:" not in output, output |
| 5311 | assert "Error executing command" not in output, output |
| 5312 | assert "is not configured" not in output, output |
| 5313 | return main.TRUE |
| 5314 | except AssertionError: |
| 5315 | main.log.exception( "" ) |
| 5316 | return main.FALSE |
| 5317 | except TypeError: |
| 5318 | main.log.exception( self.name + ": Object not as expected" ) |
| 5319 | return main.FALSE |
| 5320 | except pexpect.EOF: |
| 5321 | main.log.error( self.name + ": EOF exception found" ) |
| 5322 | main.log.error( self.name + ": " + self.handle.before ) |
| 5323 | main.cleanup() |
| 5324 | main.exit() |
| 5325 | except Exception: |
| 5326 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5327 | main.cleanup() |
| 5328 | main.exit() |
| 5329 | |
| 5330 | def vplsClean( self ): |
| 5331 | """ |
| 5332 | Description: Clears the VPLS app configuration. |
| 5333 | Returns: main.TRUE on success and main.FALSE on failure |
| 5334 | """ |
| 5335 | try: |
| 5336 | cmdStr = "vpls clean" |
| 5337 | handle = self.sendline( cmdStr ) |
| 5338 | assert handle is not None, "Error in sendline" |
| 5339 | assert "Command not found:" not in handle, handle |
| 5340 | return handle |
| 5341 | except AssertionError: |
| 5342 | main.log.exception( "" ) |
| 5343 | return main.FALSE |
| 5344 | except TypeError: |
| 5345 | main.log.exception( self.name + ": Object not as expected" ) |
| 5346 | return main.FALSE |
| 5347 | except pexpect.EOF: |
| 5348 | main.log.error( self.name + ": EOF exception found" ) |
| 5349 | main.log.error( self.name + ": " + self.handle.before ) |
| 5350 | main.cleanup() |
| 5351 | main.exit() |
| 5352 | except Exception: |
| 5353 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5354 | main.cleanup() |
| 5355 | main.exit() |
| 5356 | |
| 5357 | def vplsSetEncap( self, network, encapType ): |
| 5358 | """ |
| 5359 | CLI command to add an interface to a VPLS network. |
| 5360 | Required arguments: |
| 5361 | network - Name of the network to create. |
| 5362 | encapType - Type of encapsulation. |
| 5363 | returns: |
| 5364 | main.TRUE on success and main.FALSE on failure |
| 5365 | """ |
| 5366 | try: |
| 5367 | network = str( network ) |
| 5368 | encapType = str( encapType ).upper() |
| 5369 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5370 | cmdStr = "vpls set-encap " |
| 5371 | cmdStr += network + " " + encapType |
| 5372 | output = self.sendline( cmdStr ) |
| 5373 | assert output is not None, "Error in sendline" |
| 5374 | assert "Command not found:" not in output, output |
| 5375 | assert "Error executing command" not in output, output |
| 5376 | assert "already associated to network" not in output, output |
| 5377 | assert "Encapsulation type " not in output, output |
| 5378 | return main.TRUE |
| 5379 | except AssertionError: |
| 5380 | main.log.exception( "" ) |
| 5381 | return main.FALSE |
| 5382 | except TypeError: |
| 5383 | main.log.exception( self.name + ": Object not as expected" ) |
| 5384 | return main.FALSE |
| 5385 | except pexpect.EOF: |
| 5386 | main.log.error( self.name + ": EOF exception found" ) |
| 5387 | main.log.error( self.name + ": " + self.handle.before ) |
| 5388 | main.cleanup() |
| 5389 | main.exit() |
| 5390 | except Exception: |
| 5391 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5392 | main.cleanup() |
| 5393 | main.exit() |
| 5394 | |
| 5395 | def interfaces( self, jsonFormat=True ): |
| 5396 | """ |
| 5397 | Description: Returns result of interfaces command. |
| 5398 | Optional: |
| 5399 | * jsonFormat: enable json formatting of output |
| 5400 | Returns: |
| 5401 | The output of the command or None on error. |
| 5402 | """ |
| 5403 | try: |
| 5404 | cmdStr = "interfaces" |
| 5405 | if jsonFormat: |
| 5406 | #raise NotImplementedError |
| 5407 | cmdStr += " -j" |
| 5408 | handle = self.sendline( cmdStr ) |
| 5409 | assert handle is not None, "Error in sendline" |
| 5410 | assert "Command not found:" not in handle, handle |
| 5411 | return handle |
| 5412 | except AssertionError: |
| 5413 | main.log.exception( "" ) |
| 5414 | return None |
| 5415 | except TypeError: |
| 5416 | main.log.exception( self.name + ": Object not as expected" ) |
| 5417 | return None |
| 5418 | except pexpect.EOF: |
| 5419 | main.log.error( self.name + ": EOF exception found" ) |
| 5420 | main.log.error( self.name + ": " + self.handle.before ) |
| 5421 | main.cleanup() |
| 5422 | main.exit() |
| 5423 | except NotImplementedError: |
| 5424 | main.log.exception( self.name + ": Json output not supported") |
| 5425 | return None |
| 5426 | except Exception: |
| 5427 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5428 | main.cleanup() |
| 5429 | main.exit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5430 | |
| 5431 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
| 5432 | ''' |
| 5433 | Get the timestamp of searchTerm from karaf log. |
| 5434 | |
| 5435 | Arguments: |
| 5436 | splitTerm_before and splitTerm_after: |
| 5437 | |
| 5438 | The terms that split the string that contains the timeStamp of |
| 5439 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5440 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5441 | and the splitTerm_after is "x" |
| 5442 | |
| 5443 | others: |
| 5444 | |
| 5445 | plz look at the "logsearch" Function in onosclidriver.py |
| 5446 | |
| 5447 | |
| 5448 | ''' |
| 5449 | if logNum < 0: |
| 5450 | main.log.error("Get wrong log number ") |
| 5451 | return main.ERROR |
| 5452 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
| 5453 | if len(lines) == 0: |
| 5454 | main.log.warn( "Captured timestamp string is empty" ) |
| 5455 | return main.ERROR |
| 5456 | lines = lines[ 0 ] |
| 5457 | try: |
| 5458 | assert type(lines) is str |
| 5459 | # get the target value |
| 5460 | line = lines.split( splitTerm_before ) |
| 5461 | key = line[ 1 ].split( splitTerm_after ) |
| 5462 | return int( key[ 0 ] ) |
| 5463 | except IndexError: |
| 5464 | main.log.warn( "Index Error!" ) |
| 5465 | return main.ERROR |
| 5466 | except AssertionError: |
| 5467 | main.log.warn( "Search Term Not Found " ) |
| 5468 | return main.ERROR |