andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 4 | This driver enters the onos> prompt to issue commands. |
| 5 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 6 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 7 | functions and document properly. |
| 8 | |
| 9 | If you are a contributor to the driver, please |
| 10 | list your email here for future contact: |
| 11 | |
| 12 | jhall@onlab.us |
| 13 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 14 | shreya@onlab.us |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 15 | |
| 16 | OCT 13 2014 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 17 | Modified 2016 by ON.Lab |
| 18 | |
| 19 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 20 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 21 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 22 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 23 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 24 | import pexpect |
| 25 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 26 | import json |
| 27 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 28 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 29 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 30 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 31 | from core.graph import Graph |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 32 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 33 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 34 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 35 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 36 | def __init__( self ): |
| 37 | """ |
| 38 | Initialize client |
| 39 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 40 | self.name = None |
| 41 | self.home = None |
| 42 | self.handle = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 43 | self.graph = Graph() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | super( CLI, self ).__init__() |
| 45 | |
| 46 | def connect( self, **connectargs ): |
| 47 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 48 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 49 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 50 | try: |
| 51 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 53 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 54 | for key in self.options: |
| 55 | if key == "home": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 56 | self.home = self.options[ 'home' ] |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 57 | break |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 58 | if self.home is None or self.home == "": |
Jon Hall | e94919c | 2015-03-23 11:42:57 -0700 | [diff] [blame] | 59 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 60 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 61 | for key in self.options: |
| 62 | if key == 'onosIp': |
| 63 | self.onosIp = self.options[ 'onosIp' ] |
| 64 | break |
| 65 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 66 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 67 | |
| 68 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 69 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 70 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 71 | else: |
| 72 | main.log.info( self.name + |
| 73 | ": Trying to connect to " + |
| 74 | self.ip_address ) |
| 75 | |
| 76 | except KeyError: |
| 77 | main.log.info( "Invalid host name," + |
| 78 | " connecting to local host instead" ) |
| 79 | self.ip_address = 'localhost' |
| 80 | except Exception as inst: |
| 81 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 82 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 83 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 84 | user_name=self.user_name, |
| 85 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 86 | port=self.port, |
| 87 | pwd=self.pwd, |
| 88 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 89 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 90 | self.handle.sendline( "cd " + self.home ) |
| 91 | self.handle.expect( "\$" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 92 | if self.handle: |
| 93 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | else: |
| 95 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 96 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 97 | except TypeError: |
| 98 | main.log.exception( self.name + ": Object not as expected" ) |
| 99 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 100 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 101 | main.log.error( self.name + ": EOF exception found" ) |
| 102 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 103 | main.cleanup() |
| 104 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 105 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 106 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 107 | main.cleanup() |
| 108 | main.exit() |
| 109 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 110 | def disconnect( self ): |
| 111 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 112 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 113 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 114 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 115 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 116 | if self.handle: |
| 117 | i = self.logout() |
| 118 | if i == main.TRUE: |
| 119 | self.handle.sendline( "" ) |
| 120 | self.handle.expect( "\$" ) |
| 121 | self.handle.sendline( "exit" ) |
| 122 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 123 | except TypeError: |
| 124 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 125 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 126 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 127 | main.log.error( self.name + ": EOF exception found" ) |
| 128 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 129 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 130 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 131 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 132 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 133 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | response = main.FALSE |
| 135 | return response |
| 136 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 137 | def logout( self ): |
| 138 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 139 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 140 | Returns main.TRUE if exited CLI and |
| 141 | main.FALSE on timeout (not guranteed you are disconnected) |
| 142 | None on TypeError |
| 143 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 144 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 145 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 146 | if self.handle: |
| 147 | self.handle.sendline( "" ) |
| 148 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ], |
| 149 | timeout=10 ) |
| 150 | if i == 0: # In ONOS CLI |
| 151 | self.handle.sendline( "logout" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 152 | j = self.handle.expect( [ "\$", |
| 153 | "Command not found:", |
| 154 | pexpect.TIMEOUT ] ) |
| 155 | if j == 0: # Successfully logged out |
| 156 | return main.TRUE |
| 157 | elif j == 1 or j == 2: |
| 158 | # ONOS didn't fully load, and logout command isn't working |
| 159 | # or the command timed out |
| 160 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 161 | try: |
| 162 | self.handle.expect( "\$" ) |
| 163 | except pexpect.TIMEOUT: |
| 164 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 165 | return main.TRUE |
| 166 | else: # some other output |
| 167 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 168 | repr( self.handle.before ) ) |
| 169 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 170 | elif i == 1: # not in CLI |
| 171 | return main.TRUE |
| 172 | elif i == 3: # Timeout |
| 173 | return main.FALSE |
| 174 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 175 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 176 | except TypeError: |
| 177 | main.log.exception( self.name + ": Object not as expected" ) |
| 178 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 179 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 181 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 182 | main.cleanup() |
| 183 | main.exit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 184 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 185 | main.log.error( self.name + |
| 186 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 187 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 188 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 189 | main.cleanup() |
| 190 | main.exit() |
| 191 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 192 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 193 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 194 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 196 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 197 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 198 | try: |
| 199 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 200 | main.log.error( "Must define cellname" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 201 | main.cleanup() |
| 202 | main.exit() |
| 203 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 204 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 205 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 206 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 207 | # and that this driver will have to change accordingly |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 208 | self.handle.expect(str(cellname)) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 209 | handleBefore = self.handle.before |
| 210 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 211 | # Get the rest of the handle |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 212 | self.handle.sendline("") |
| 213 | self.handle.expect("\$") |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 214 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 215 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 216 | main.log.info( "Cell call returned: " + handleBefore + |
| 217 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 218 | |
| 219 | return main.TRUE |
| 220 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 221 | except TypeError: |
| 222 | main.log.exception( self.name + ": Object not as expected" ) |
| 223 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 224 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 225 | main.log.error( self.name + ": eof exception found" ) |
| 226 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 227 | main.cleanup() |
| 228 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 229 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 230 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 231 | main.cleanup() |
| 232 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 233 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 234 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 235 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 236 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 237 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 238 | by user would be used to set the current karaf shell idle timeout. |
| 239 | Note that when ever this property is modified the shell will exit and |
Hari Krishna | d7b9c20 | 2015-01-05 10:38:14 -0800 | [diff] [blame] | 240 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 241 | Below is an example to start a session with 60 seconds idle timeout |
| 242 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 243 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 244 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 245 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 246 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 247 | Note: karafTimeout is left as str so that this could be read |
| 248 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 249 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 250 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 251 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 252 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 253 | self.handle.sendline( "" ) |
| 254 | x = self.handle.expect( [ |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 255 | "\$", "onos>" ], commandlineTimeout) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 256 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 257 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 258 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 259 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 260 | # Not in CLI so login |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 261 | if waitForStart: |
| 262 | # Wait for onos start ( -w ) and enter onos cli |
| 263 | startCliCommand = "onos -w " |
| 264 | else: |
| 265 | startCliCommand = "onos " |
| 266 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | i = self.handle.expect( [ |
| 268 | "onos>", |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 269 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 270 | |
| 271 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 272 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 273 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 274 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 275 | "config:property-set -p org.apache.karaf.shell\ |
| 276 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 277 | karafTimeout ) |
| 278 | self.handle.expect( "\$" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 279 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 280 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 281 | return main.TRUE |
| 282 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 283 | # If failed, send ctrl+c to process and try again |
| 284 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 285 | self.handle.send( "\x03" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 286 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 287 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 288 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 289 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 290 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 291 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 292 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 293 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 294 | "config:property-set -p org.apache.karaf.shell\ |
| 295 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 296 | karafTimeout ) |
| 297 | self.handle.expect( "\$" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 298 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 299 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 300 | return main.TRUE |
| 301 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 302 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 303 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 304 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 305 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 306 | except TypeError: |
| 307 | main.log.exception( self.name + ": Object not as expected" ) |
| 308 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 309 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 310 | main.log.error( self.name + ": EOF exception found" ) |
| 311 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 312 | main.cleanup() |
| 313 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 314 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 315 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 316 | main.cleanup() |
| 317 | main.exit() |
| 318 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 319 | def startCellCli( self, karafTimeout="", |
| 320 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 321 | """ |
| 322 | Start CLI on onos ecll handle. |
| 323 | |
| 324 | karafTimeout is an optional argument. karafTimeout value passed |
| 325 | by user would be used to set the current karaf shell idle timeout. |
| 326 | Note that when ever this property is modified the shell will exit and |
| 327 | the subsequent login would reflect new idle timeout. |
| 328 | Below is an example to start a session with 60 seconds idle timeout |
| 329 | ( input value is in milliseconds ): |
| 330 | |
| 331 | tValue = "60000" |
| 332 | |
| 333 | Note: karafTimeout is left as str so that this could be read |
| 334 | and passed to startOnosCli from PARAMS file as str. |
| 335 | """ |
| 336 | |
| 337 | try: |
| 338 | self.handle.sendline( "" ) |
| 339 | x = self.handle.expect( [ |
| 340 | "\$", "onos>" ], commandlineTimeout) |
| 341 | |
| 342 | if x == 1: |
| 343 | main.log.info( "ONOS cli is already running" ) |
| 344 | return main.TRUE |
| 345 | |
| 346 | # Wait for onos start ( -w ) and enter onos cli |
| 347 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 348 | i = self.handle.expect( [ |
| 349 | "onos>", |
| 350 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 351 | |
| 352 | if i == 0: |
| 353 | main.log.info( self.name + " CLI Started successfully" ) |
| 354 | if karafTimeout: |
| 355 | self.handle.sendline( |
| 356 | "config:property-set -p org.apache.karaf.shell\ |
| 357 | sshIdleTimeout " + |
| 358 | karafTimeout ) |
| 359 | self.handle.expect( "\$" ) |
| 360 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 361 | self.handle.expect( "onos>" ) |
| 362 | return main.TRUE |
| 363 | else: |
| 364 | # If failed, send ctrl+c to process and try again |
| 365 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 366 | self.handle.send( "\x03" ) |
| 367 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 368 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 369 | timeout=30 ) |
| 370 | if i == 0: |
| 371 | main.log.info( self.name + " CLI Started " + |
| 372 | "successfully after retry attempt" ) |
| 373 | if karafTimeout: |
| 374 | self.handle.sendline( |
| 375 | "config:property-set -p org.apache.karaf.shell\ |
| 376 | sshIdleTimeout " + |
| 377 | karafTimeout ) |
| 378 | self.handle.expect( "\$" ) |
| 379 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 380 | self.handle.expect( "onos>" ) |
| 381 | return main.TRUE |
| 382 | else: |
| 383 | main.log.error( "Connection to CLI " + |
| 384 | self.name + " timeout" ) |
| 385 | return main.FALSE |
| 386 | |
| 387 | except TypeError: |
| 388 | main.log.exception( self.name + ": Object not as expected" ) |
| 389 | return None |
| 390 | except pexpect.EOF: |
| 391 | main.log.error( self.name + ": EOF exception found" ) |
| 392 | main.log.error( self.name + ": " + self.handle.before ) |
| 393 | main.cleanup() |
| 394 | main.exit() |
| 395 | except Exception: |
| 396 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 397 | main.cleanup() |
| 398 | main.exit() |
| 399 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 400 | def log( self, cmdStr, level="",noExit=False): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 401 | """ |
| 402 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 403 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 404 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 405 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 406 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 407 | Level defaults to INFO |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 408 | """ |
| 409 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 410 | lvlStr = "" |
| 411 | if level: |
| 412 | lvlStr = "--level=" + level |
| 413 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 414 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 415 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 416 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 417 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 418 | response = self.handle.before |
| 419 | if re.search( "Error", response ): |
| 420 | return main.FALSE |
| 421 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 422 | except pexpect.TIMEOUT: |
| 423 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 424 | if noExit: |
| 425 | main.cleanup() |
| 426 | return None |
| 427 | else: |
| 428 | main.cleanup() |
| 429 | main.exit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 430 | except pexpect.EOF: |
| 431 | main.log.error( self.name + ": EOF exception found" ) |
| 432 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 433 | if noExit: |
| 434 | main.cleanup() |
| 435 | return None |
| 436 | else: |
| 437 | main.cleanup() |
| 438 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 439 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 440 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 441 | if noExit: |
| 442 | main.cleanup() |
| 443 | return None |
| 444 | else: |
| 445 | main.cleanup() |
| 446 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 447 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 448 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 449 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 450 | Send a completely user specified string to |
| 451 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 452 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 453 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 454 | if noExit is True, TestON will not exit, and return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 455 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 456 | Warning: There are no sanity checking to commands |
| 457 | sent using this method. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 458 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 459 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 460 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 461 | # Try to reconnect if disconnected from cli |
| 462 | self.handle.sendline( "" ) |
| 463 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ] ) |
| 464 | if i == 1: |
| 465 | main.log.error( self.name + ": onos cli session closed. ") |
| 466 | if self.onosIp: |
| 467 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 468 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 469 | if reconnectResult: |
| 470 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 471 | else: |
| 472 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 473 | if noExit: |
| 474 | return None |
| 475 | else: |
| 476 | main.cleanup() |
| 477 | main.exit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 478 | else: |
| 479 | main.cleanup() |
| 480 | main.exit() |
| 481 | if i == 2: |
| 482 | self.handle.sendline( "" ) |
| 483 | self.handle.expect( "onos>" ) |
| 484 | |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 485 | if debug: |
| 486 | # NOTE: This adds and average of .4 seconds per call |
| 487 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 488 | self.log( logStr,noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 489 | self.handle.sendline( cmdStr ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 490 | i = self.handle.expect( ["onos>", "\$"], timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 491 | response = self.handle.before |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 492 | # TODO: do something with i |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 493 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
| 494 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 495 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 496 | main.log.debug( self.name + ": Raw output" ) |
| 497 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 498 | |
| 499 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 500 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 501 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 502 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 503 | main.log.debug( self.name + ": ansiEscape output" ) |
| 504 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 505 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 506 | # Remove extra return chars that get added |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 507 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 508 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 509 | main.log.debug( self.name + ": Removed extra returns " + |
| 510 | "from output" ) |
| 511 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 512 | |
| 513 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 514 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 515 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 516 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 517 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 518 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 519 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 520 | output = response.split( cmdStr.strip(), 1 ) |
| 521 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 522 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 523 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 524 | main.log.debug( self.name + ": " + repr( r ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 525 | output = output[1].strip() |
| 526 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 527 | main.log.info( "Response from ONOS: {}".format( output ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 528 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 529 | except pexpect.TIMEOUT: |
| 530 | main.log.error( self.name + ":ONOS timeout" ) |
| 531 | if debug: |
| 532 | main.log.debug( self.handle.before ) |
| 533 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 534 | except IndexError: |
| 535 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 536 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 537 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 538 | except TypeError: |
| 539 | main.log.exception( self.name + ": Object not as expected" ) |
| 540 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 541 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 542 | main.log.error( self.name + ": EOF exception found" ) |
| 543 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 544 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 545 | return None |
| 546 | else: |
| 547 | main.cleanup() |
| 548 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 549 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 550 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 551 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 552 | return None |
| 553 | else: |
| 554 | main.cleanup() |
| 555 | main.exit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 556 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 557 | # IMPORTANT NOTE: |
| 558 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 559 | # the cli command changing 'a:b' with 'aB'. |
| 560 | # Ex ) onos:topology > onosTopology |
| 561 | # onos:links > onosLinks |
| 562 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 563 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 564 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 565 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 566 | Adds a new cluster node by ID and address information. |
| 567 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 568 | * nodeId |
| 569 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 570 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 571 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 572 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 573 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 574 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 575 | str( ONOSIp ) + " " + str( tcpPort ) |
| 576 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 577 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 578 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 579 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 580 | main.log.error( "Error in adding node" ) |
| 581 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 582 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 583 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 584 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 585 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 586 | except AssertionError: |
| 587 | main.log.exception( "" ) |
| 588 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 589 | except TypeError: |
| 590 | main.log.exception( self.name + ": Object not as expected" ) |
| 591 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 592 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 593 | main.log.error( self.name + ": EOF exception found" ) |
| 594 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 595 | main.cleanup() |
| 596 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 597 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 598 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 599 | main.cleanup() |
| 600 | main.exit() |
| 601 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 602 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 603 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 604 | Removes a cluster by ID |
| 605 | Issues command: 'remove-node [<node-id>]' |
| 606 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 607 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 608 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 609 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 610 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 611 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 612 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 613 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 614 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 615 | if re.search( "Error", handle ): |
| 616 | main.log.error( "Error in removing node" ) |
| 617 | main.log.error( handle ) |
| 618 | return main.FALSE |
| 619 | else: |
| 620 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 621 | except AssertionError: |
| 622 | main.log.exception( "" ) |
| 623 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 624 | except TypeError: |
| 625 | main.log.exception( self.name + ": Object not as expected" ) |
| 626 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 627 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 628 | main.log.error( self.name + ": EOF exception found" ) |
| 629 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 630 | main.cleanup() |
| 631 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 632 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 633 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 634 | main.cleanup() |
| 635 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 636 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 637 | def nodes( self, jsonFormat=True): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 638 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 639 | List the nodes currently visible |
| 640 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 641 | Optional argument: |
| 642 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 643 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 644 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 645 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 646 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 647 | cmdStr += " -j" |
| 648 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 649 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 650 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 651 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 652 | except AssertionError: |
| 653 | main.log.exception( "" ) |
| 654 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 655 | except TypeError: |
| 656 | main.log.exception( self.name + ": Object not as expected" ) |
| 657 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 658 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 659 | main.log.error( self.name + ": EOF exception found" ) |
| 660 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 661 | main.cleanup() |
| 662 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 663 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 664 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 665 | main.cleanup() |
| 666 | main.exit() |
| 667 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 668 | def topology( self ): |
| 669 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 670 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 671 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 672 | Return: |
| 673 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 674 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 675 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 676 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 677 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 678 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 679 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 680 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 681 | except AssertionError: |
| 682 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 683 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 684 | except TypeError: |
| 685 | main.log.exception( self.name + ": Object not as expected" ) |
| 686 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 687 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 688 | main.log.error( self.name + ": EOF exception found" ) |
| 689 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 690 | main.cleanup() |
| 691 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 692 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 693 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 694 | main.cleanup() |
| 695 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 696 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 697 | def deviceRemove( self, deviceId ): |
| 698 | """ |
| 699 | Removes particular device from storage |
| 700 | |
| 701 | TODO: refactor this function |
| 702 | """ |
| 703 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 704 | cmdStr = "device-remove " + str( deviceId ) |
| 705 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 706 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 707 | if re.search( "Error", handle ): |
| 708 | main.log.error( "Error in removing device" ) |
| 709 | main.log.error( handle ) |
| 710 | return main.FALSE |
| 711 | else: |
| 712 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 713 | except AssertionError: |
| 714 | main.log.exception( "" ) |
| 715 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 716 | except TypeError: |
| 717 | main.log.exception( self.name + ": Object not as expected" ) |
| 718 | return None |
| 719 | except pexpect.EOF: |
| 720 | main.log.error( self.name + ": EOF exception found" ) |
| 721 | main.log.error( self.name + ": " + self.handle.before ) |
| 722 | main.cleanup() |
| 723 | main.exit() |
| 724 | except Exception: |
| 725 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 726 | main.cleanup() |
| 727 | main.exit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 728 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 729 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 730 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 731 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 732 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 733 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 734 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 735 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 736 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 737 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 738 | cmdStr += " -j" |
| 739 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 740 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 741 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 742 | except AssertionError: |
| 743 | main.log.exception( "" ) |
| 744 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 745 | except TypeError: |
| 746 | main.log.exception( self.name + ": Object not as expected" ) |
| 747 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 748 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 749 | main.log.error( self.name + ": EOF exception found" ) |
| 750 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 751 | main.cleanup() |
| 752 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 753 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 754 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 755 | main.cleanup() |
| 756 | main.exit() |
| 757 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 758 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 759 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 760 | This balances the devices across all controllers |
| 761 | by issuing command: 'onos> onos:balance-masters' |
| 762 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 763 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 764 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 765 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 766 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 767 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 768 | if re.search( "Error", handle ): |
| 769 | main.log.error( "Error in balancing masters" ) |
| 770 | main.log.error( handle ) |
| 771 | return main.FALSE |
| 772 | else: |
| 773 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 774 | except AssertionError: |
| 775 | main.log.exception( "" ) |
| 776 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 777 | except TypeError: |
| 778 | main.log.exception( self.name + ": Object not as expected" ) |
| 779 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 780 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 781 | main.log.error( self.name + ": EOF exception found" ) |
| 782 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 783 | main.cleanup() |
| 784 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 785 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 786 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 787 | main.cleanup() |
| 788 | main.exit() |
| 789 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 790 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 791 | """ |
| 792 | Returns the output of the masters command. |
| 793 | Optional argument: |
| 794 | * jsonFormat - boolean indicating if you want output in json |
| 795 | """ |
| 796 | try: |
| 797 | cmdStr = "onos:masters" |
| 798 | if jsonFormat: |
| 799 | cmdStr += " -j" |
| 800 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 801 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 802 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 803 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 804 | except AssertionError: |
| 805 | main.log.exception( "" ) |
| 806 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 807 | except TypeError: |
| 808 | main.log.exception( self.name + ": Object not as expected" ) |
| 809 | return None |
| 810 | except pexpect.EOF: |
| 811 | main.log.error( self.name + ": EOF exception found" ) |
| 812 | main.log.error( self.name + ": " + self.handle.before ) |
| 813 | main.cleanup() |
| 814 | main.exit() |
| 815 | except Exception: |
| 816 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 817 | main.cleanup() |
| 818 | main.exit() |
| 819 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 820 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 821 | """ |
| 822 | Uses the master command to check that the devices' leadership |
| 823 | is evenly divided |
| 824 | |
| 825 | Dependencies: checkMasters() and summary() |
| 826 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 827 | Returns main.TRUE if the devices are balanced |
| 828 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 829 | Exits on Exception |
| 830 | Returns None on TypeError |
| 831 | """ |
| 832 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 833 | summaryOutput = self.summary() |
| 834 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 835 | except ( TypeError, ValueError ): |
| 836 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 837 | return None |
| 838 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 839 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 840 | mastersOutput = self.checkMasters() |
| 841 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 842 | first = masters[ 0 ][ "size" ] |
| 843 | for master in masters: |
| 844 | totalOwnedDevices += master[ "size" ] |
| 845 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 846 | main.log.error( "Mastership not balanced" ) |
| 847 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 848 | return main.FALSE |
| 849 | main.log.info( "Mastership balanced between " \ |
| 850 | + str( len(masters) ) + " masters" ) |
| 851 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 852 | except ( TypeError, ValueError ): |
| 853 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 854 | return None |
| 855 | except pexpect.EOF: |
| 856 | main.log.error( self.name + ": EOF exception found" ) |
| 857 | main.log.error( self.name + ": " + self.handle.before ) |
| 858 | main.cleanup() |
| 859 | main.exit() |
| 860 | except Exception: |
| 861 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 862 | main.cleanup() |
| 863 | main.exit() |
| 864 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 865 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 866 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 867 | Lists all core links |
| 868 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 869 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 870 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 871 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 872 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 873 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 874 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 875 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 876 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 877 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 878 | except AssertionError: |
| 879 | main.log.exception( "" ) |
| 880 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 881 | except TypeError: |
| 882 | main.log.exception( self.name + ": Object not as expected" ) |
| 883 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 884 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 885 | main.log.error( self.name + ": EOF exception found" ) |
| 886 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 887 | main.cleanup() |
| 888 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 889 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 890 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 891 | main.cleanup() |
| 892 | main.exit() |
| 893 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 894 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 895 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 896 | Lists all ports |
| 897 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 898 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 899 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 900 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 901 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 902 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 903 | cmdStr += " -j" |
| 904 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 905 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 906 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 907 | except AssertionError: |
| 908 | main.log.exception( "" ) |
| 909 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 910 | except TypeError: |
| 911 | main.log.exception( self.name + ": Object not as expected" ) |
| 912 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 913 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 914 | main.log.error( self.name + ": EOF exception found" ) |
| 915 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 916 | main.cleanup() |
| 917 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 918 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 919 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 920 | main.cleanup() |
| 921 | main.exit() |
| 922 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 923 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 924 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 925 | Lists all devices and the controllers with roles assigned to them |
| 926 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 927 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 928 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 929 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 930 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 931 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 932 | cmdStr += " -j" |
| 933 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 934 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 935 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 936 | except AssertionError: |
| 937 | main.log.exception( "" ) |
| 938 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 939 | except TypeError: |
| 940 | main.log.exception( self.name + ": Object not as expected" ) |
| 941 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 942 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 943 | main.log.error( self.name + ": EOF exception found" ) |
| 944 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 945 | main.cleanup() |
| 946 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 947 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 948 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 949 | main.cleanup() |
| 950 | main.exit() |
| 951 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 952 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 953 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 954 | Given the a string containing the json representation of the "roles" |
| 955 | cli command and a partial or whole device id, returns a json object |
| 956 | containing the roles output for the first device whose id contains |
| 957 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 958 | |
| 959 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 960 | A dict of the role assignments for the given device or |
| 961 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 962 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 963 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 964 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 965 | return None |
| 966 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 967 | rawRoles = self.roles() |
| 968 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 969 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 970 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 971 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 972 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 973 | return device |
| 974 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 975 | except ( TypeError, ValueError ): |
| 976 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 977 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 978 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 979 | main.log.error( self.name + ": EOF exception found" ) |
| 980 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 981 | main.cleanup() |
| 982 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 983 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 984 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 985 | main.cleanup() |
| 986 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 987 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 988 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 989 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 990 | Iterates through each device and checks if there is a master assigned |
| 991 | Returns: main.TRUE if each device has a master |
| 992 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 993 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 994 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 995 | rawRoles = self.roles() |
| 996 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 997 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 998 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 999 | # print device |
| 1000 | if device[ 'master' ] == "none": |
| 1001 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1002 | return main.FALSE |
| 1003 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1004 | except ( TypeError, ValueError ): |
| 1005 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1006 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1007 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1008 | main.log.error( self.name + ": EOF exception found" ) |
| 1009 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1010 | main.cleanup() |
| 1011 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1012 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1013 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1014 | main.cleanup() |
| 1015 | main.exit() |
| 1016 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1017 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1018 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1019 | Returns string of paths, and the cost. |
| 1020 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1021 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1022 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1023 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1024 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1025 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1026 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1027 | main.log.error( "Error in getting paths" ) |
| 1028 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1029 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1030 | path = handle.split( ";" )[ 0 ] |
| 1031 | cost = handle.split( ";" )[ 1 ] |
| 1032 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1033 | except AssertionError: |
| 1034 | main.log.exception( "" ) |
| 1035 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1036 | except TypeError: |
| 1037 | main.log.exception( self.name + ": Object not as expected" ) |
| 1038 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1039 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1040 | main.log.error( self.name + ": EOF exception found" ) |
| 1041 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1042 | main.cleanup() |
| 1043 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1044 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1045 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1046 | main.cleanup() |
| 1047 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1048 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1049 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1050 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1051 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1052 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1053 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1054 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1055 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1056 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1057 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1058 | cmdStr += " -j" |
| 1059 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1060 | if handle: |
| 1061 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1062 | # TODO: Maybe make this less hardcoded |
| 1063 | # ConsistentMap Exceptions |
| 1064 | assert "org.onosproject.store.service" not in handle |
| 1065 | # Node not leader |
| 1066 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1067 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1068 | except AssertionError: |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1069 | main.log.exception( "Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1070 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1071 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1072 | except TypeError: |
| 1073 | main.log.exception( self.name + ": Object not as expected" ) |
| 1074 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1075 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1076 | main.log.error( self.name + ": EOF exception found" ) |
| 1077 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1078 | main.cleanup() |
| 1079 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1080 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1081 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1082 | main.cleanup() |
| 1083 | main.exit() |
| 1084 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1085 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1086 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1087 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1088 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1089 | Note: mac must be a colon separated mac address, but could be a |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1090 | partial mac address |
| 1091 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1092 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1093 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1094 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1095 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1096 | return None |
| 1097 | else: |
| 1098 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1099 | rawHosts = self.hosts() |
| 1100 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1101 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1102 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1103 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1104 | if not host: |
| 1105 | pass |
| 1106 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1107 | return host |
| 1108 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1109 | except ( TypeError, ValueError ): |
| 1110 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1111 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1112 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1113 | main.log.error( self.name + ": EOF exception found" ) |
| 1114 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1115 | main.cleanup() |
| 1116 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1117 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1118 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1119 | main.cleanup() |
| 1120 | main.exit() |
| 1121 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1122 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1123 | """ |
| 1124 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1125 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1126 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1127 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1128 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1129 | IMPORTANT: |
| 1130 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1131 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1132 | Furthermore, it assumes that value of VLAN is '-1' |
| 1133 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1134 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1135 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1136 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1137 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1138 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1139 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1140 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1141 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1142 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1143 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1144 | i = iter( str( hostHex ) ) |
| 1145 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1146 | hostHex = hostHex + "/-1" |
| 1147 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1148 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1149 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1150 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1151 | except TypeError: |
| 1152 | main.log.exception( self.name + ": Object not as expected" ) |
| 1153 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1154 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1155 | main.log.error( self.name + ": EOF exception found" ) |
| 1156 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1157 | main.cleanup() |
| 1158 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1159 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1160 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1161 | main.cleanup() |
| 1162 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1163 | |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1164 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1165 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1166 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1167 | * hostIdOne: ONOS host id for host1 |
| 1168 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1169 | Optional: |
| 1170 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1171 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1172 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1173 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1174 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1175 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1176 | Returns: |
| 1177 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1178 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1179 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1180 | cmdStr = "add-host-intent " |
| 1181 | if vlanId: |
| 1182 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1183 | if setVlan: |
| 1184 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1185 | if encap: |
| 1186 | cmdStr += "--encapsulation " + str( encap ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1187 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1188 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1189 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1190 | if re.search( "Error", handle ): |
| 1191 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1192 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1193 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1194 | else: |
| 1195 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1196 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
| 1197 | match = re.search('id=0x([\da-f]+),', handle) |
| 1198 | if match: |
| 1199 | return match.group()[3:-1] |
| 1200 | else: |
| 1201 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1202 | main.log.debug( "Response from ONOS was: " + |
| 1203 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1204 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1205 | except AssertionError: |
| 1206 | main.log.exception( "" ) |
| 1207 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1208 | except TypeError: |
| 1209 | main.log.exception( self.name + ": Object not as expected" ) |
| 1210 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1211 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1212 | main.log.error( self.name + ": EOF exception found" ) |
| 1213 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1214 | main.cleanup() |
| 1215 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1216 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1217 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1218 | main.cleanup() |
| 1219 | main.exit() |
| 1220 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1221 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1222 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1223 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1224 | * ingressDevice: device id of ingress device |
| 1225 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1226 | Optional: |
| 1227 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1228 | Description: |
| 1229 | Adds an optical intent by specifying an ingress and egress device |
| 1230 | Returns: |
| 1231 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1232 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1233 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1234 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1235 | " " + str( egressDevice ) |
| 1236 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1237 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1238 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1239 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1240 | main.log.error( "Error in adding Optical intent" ) |
| 1241 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1242 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1243 | main.log.info( "Optical intent installed between " + |
| 1244 | str( ingressDevice ) + " and " + |
| 1245 | str( egressDevice ) ) |
| 1246 | match = re.search('id=0x([\da-f]+),', handle) |
| 1247 | if match: |
| 1248 | return match.group()[3:-1] |
| 1249 | else: |
| 1250 | main.log.error( "Error, intent ID not found" ) |
| 1251 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1252 | except AssertionError: |
| 1253 | main.log.exception( "" ) |
| 1254 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1255 | except TypeError: |
| 1256 | main.log.exception( self.name + ": Object not as expected" ) |
| 1257 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1258 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1259 | main.log.error( self.name + ": EOF exception found" ) |
| 1260 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1261 | main.cleanup() |
| 1262 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1263 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1264 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1265 | main.cleanup() |
| 1266 | main.exit() |
| 1267 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1268 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1269 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1270 | ingressDevice, |
| 1271 | egressDevice, |
| 1272 | portIngress="", |
| 1273 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1274 | ethType="", |
| 1275 | ethSrc="", |
| 1276 | ethDst="", |
| 1277 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1278 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1279 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1280 | ipProto="", |
| 1281 | ipSrc="", |
| 1282 | ipDst="", |
| 1283 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1284 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1285 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1286 | setVlan="", |
| 1287 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1288 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1289 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1290 | * ingressDevice: device id of ingress device |
| 1291 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1292 | Optional: |
| 1293 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1294 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1295 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1296 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1297 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1298 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1299 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1300 | * ipSrc: specify ip source address |
| 1301 | * ipDst: specify ip destination address |
| 1302 | * tcpSrc: specify tcp source port |
| 1303 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1304 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1305 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1306 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1307 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1308 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1309 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1310 | Returns: |
| 1311 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1312 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1313 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1314 | options developers provide for point-to-point |
| 1315 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1316 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1317 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1318 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1319 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1320 | if ethType: |
| 1321 | cmd += " --ethType " + str( ethType ) |
| 1322 | if ethSrc: |
| 1323 | cmd += " --ethSrc " + str( ethSrc ) |
| 1324 | if ethDst: |
| 1325 | cmd += " --ethDst " + str( ethDst ) |
| 1326 | if bandwidth: |
| 1327 | cmd += " --bandwidth " + str( bandwidth ) |
| 1328 | if lambdaAlloc: |
| 1329 | cmd += " --lambda " |
| 1330 | if ipProto: |
| 1331 | cmd += " --ipProto " + str( ipProto ) |
| 1332 | if ipSrc: |
| 1333 | cmd += " --ipSrc " + str( ipSrc ) |
| 1334 | if ipDst: |
| 1335 | cmd += " --ipDst " + str( ipDst ) |
| 1336 | if tcpSrc: |
| 1337 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1338 | if tcpDst: |
| 1339 | cmd += " --tcpDst " + str( tcpDst ) |
| 1340 | if vlanId: |
| 1341 | cmd += " -v " + str( vlanId ) |
| 1342 | if setVlan: |
| 1343 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1344 | if encap: |
| 1345 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1346 | if protected: |
| 1347 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1348 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1349 | # Check whether the user appended the port |
| 1350 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1351 | if "/" in ingressDevice: |
| 1352 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1353 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1354 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1355 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1356 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1357 | # Would it make sense to throw an exception and exit |
| 1358 | # the test? |
| 1359 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1360 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1361 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1362 | str( ingressDevice ) + "/" +\ |
| 1363 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1364 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1365 | if "/" in egressDevice: |
| 1366 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1367 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1368 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1369 | main.log.error( "You must specify the egress port" ) |
| 1370 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1371 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1372 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1373 | str( egressDevice ) + "/" +\ |
| 1374 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1375 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1376 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1377 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1378 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1379 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1380 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1381 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1382 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1383 | # TODO: print out all the options in this message? |
| 1384 | main.log.info( "Point-to-point intent installed between " + |
| 1385 | str( ingressDevice ) + " and " + |
| 1386 | str( egressDevice ) ) |
| 1387 | match = re.search('id=0x([\da-f]+),', handle) |
| 1388 | if match: |
| 1389 | return match.group()[3:-1] |
| 1390 | else: |
| 1391 | main.log.error( "Error, intent ID not found" ) |
| 1392 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1393 | except AssertionError: |
| 1394 | main.log.exception( "" ) |
| 1395 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1396 | except TypeError: |
| 1397 | main.log.exception( self.name + ": Object not as expected" ) |
| 1398 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1399 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1400 | main.log.error( self.name + ": EOF exception found" ) |
| 1401 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1402 | main.cleanup() |
| 1403 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1404 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1405 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1406 | main.cleanup() |
| 1407 | main.exit() |
| 1408 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1409 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1410 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1411 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1412 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1413 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1414 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1415 | ethType="", |
| 1416 | ethSrc="", |
| 1417 | ethDst="", |
| 1418 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1419 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1420 | ipProto="", |
| 1421 | ipSrc="", |
| 1422 | ipDst="", |
| 1423 | tcpSrc="", |
| 1424 | tcpDst="", |
| 1425 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1426 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1427 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1428 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1429 | partial=False, |
| 1430 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1431 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1432 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1433 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1434 | is same. That is, all ingress devices include port numbers |
| 1435 | with a "/" or all ingress devices could specify device |
| 1436 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1437 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1438 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1439 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1440 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1441 | Optional: |
| 1442 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1443 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1444 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1445 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1446 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1447 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1448 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1449 | * ipSrc: specify ip source address |
| 1450 | * ipDst: specify ip destination address |
| 1451 | * tcpSrc: specify tcp source port |
| 1452 | * tcpDst: specify tcp destination port |
| 1453 | * setEthSrc: action to Rewrite Source MAC Address |
| 1454 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1455 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1456 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1457 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1458 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1459 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1460 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1461 | Returns: |
| 1462 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1463 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1464 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1465 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1466 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1467 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1468 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1469 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1470 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1471 | if ethType: |
| 1472 | cmd += " --ethType " + str( ethType ) |
| 1473 | if ethSrc: |
| 1474 | cmd += " --ethSrc " + str( ethSrc ) |
| 1475 | if ethDst: |
| 1476 | cmd += " --ethDst " + str( ethDst ) |
| 1477 | if bandwidth: |
| 1478 | cmd += " --bandwidth " + str( bandwidth ) |
| 1479 | if lambdaAlloc: |
| 1480 | cmd += " --lambda " |
| 1481 | if ipProto: |
| 1482 | cmd += " --ipProto " + str( ipProto ) |
| 1483 | if ipSrc: |
| 1484 | cmd += " --ipSrc " + str( ipSrc ) |
| 1485 | if ipDst: |
| 1486 | cmd += " --ipDst " + str( ipDst ) |
| 1487 | if tcpSrc: |
| 1488 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1489 | if tcpDst: |
| 1490 | cmd += " --tcpDst " + str( tcpDst ) |
| 1491 | if setEthSrc: |
| 1492 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1493 | if setEthDst: |
| 1494 | cmd += " --setEthDst " + str( setEthDst ) |
| 1495 | if vlanId: |
| 1496 | cmd += " -v " + str( vlanId ) |
| 1497 | if setVlan: |
| 1498 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1499 | if partial: |
| 1500 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1501 | if encap: |
| 1502 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1503 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1504 | # Check whether the user appended the port |
| 1505 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1506 | |
| 1507 | if portIngressList is None: |
| 1508 | for ingressDevice in ingressDeviceList: |
| 1509 | if "/" in ingressDevice: |
| 1510 | cmd += " " + str( ingressDevice ) |
| 1511 | else: |
| 1512 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1513 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1514 | # TODO: perhaps more meaningful return |
| 1515 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1516 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1517 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1518 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1519 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1520 | cmd += " " + \ |
| 1521 | str( ingressDevice ) + "/" +\ |
| 1522 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1523 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1524 | main.log.error( "Device list and port list does not " + |
| 1525 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1526 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1527 | if "/" in egressDevice: |
| 1528 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1529 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1530 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1531 | main.log.error( "You must specify " + |
| 1532 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1533 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1534 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1535 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1536 | str( egressDevice ) + "/" +\ |
| 1537 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1538 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1539 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1540 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1541 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1542 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1543 | "intent" ) |
| 1544 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1545 | else: |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1546 | match = re.search('id=0x([\da-f]+),', handle) |
| 1547 | if match: |
| 1548 | return match.group()[3:-1] |
| 1549 | else: |
| 1550 | main.log.error( "Error, intent ID not found" ) |
| 1551 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1552 | except AssertionError: |
| 1553 | main.log.exception( "" ) |
| 1554 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1555 | except TypeError: |
| 1556 | main.log.exception( self.name + ": Object not as expected" ) |
| 1557 | return None |
| 1558 | except pexpect.EOF: |
| 1559 | main.log.error( self.name + ": EOF exception found" ) |
| 1560 | main.log.error( self.name + ": " + self.handle.before ) |
| 1561 | main.cleanup() |
| 1562 | main.exit() |
| 1563 | except Exception: |
| 1564 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1565 | main.cleanup() |
| 1566 | main.exit() |
| 1567 | |
| 1568 | def addSinglepointToMultipointIntent( |
| 1569 | self, |
| 1570 | ingressDevice, |
| 1571 | egressDeviceList, |
| 1572 | portIngress="", |
| 1573 | portEgressList=None, |
| 1574 | ethType="", |
| 1575 | ethSrc="", |
| 1576 | ethDst="", |
| 1577 | bandwidth="", |
| 1578 | lambdaAlloc=False, |
| 1579 | ipProto="", |
| 1580 | ipSrc="", |
| 1581 | ipDst="", |
| 1582 | tcpSrc="", |
| 1583 | tcpDst="", |
| 1584 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1585 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1586 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1587 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1588 | partial=False, |
| 1589 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1590 | """ |
| 1591 | Note: |
| 1592 | This function assumes the format of all egress devices |
| 1593 | is same. That is, all egress devices include port numbers |
| 1594 | with a "/" or all egress devices could specify device |
| 1595 | ids and port numbers seperately. |
| 1596 | Required: |
| 1597 | * EgressDeviceList: List of device ids of egress device |
| 1598 | ( Atleast 2 eress devices required in the list ) |
| 1599 | * ingressDevice: device id of ingress device |
| 1600 | Optional: |
| 1601 | * ethType: specify ethType |
| 1602 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1603 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1604 | * bandwidth: specify bandwidth capacity of link |
| 1605 | * lambdaAlloc: if True, intent will allocate lambda |
| 1606 | for the specified intent |
| 1607 | * ipProto: specify ip protocol |
| 1608 | * ipSrc: specify ip source address |
| 1609 | * ipDst: specify ip destination address |
| 1610 | * tcpSrc: specify tcp source port |
| 1611 | * tcpDst: specify tcp destination port |
| 1612 | * setEthSrc: action to Rewrite Source MAC Address |
| 1613 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1614 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1615 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1616 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1617 | Description: |
| 1618 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1619 | specifying device id's and optional fields |
| 1620 | Returns: |
| 1621 | A string of the intent id or None on error |
| 1622 | |
| 1623 | NOTE: This function may change depending on the |
| 1624 | options developers provide for singlepoint-to-multipoint |
| 1625 | intent via cli |
| 1626 | """ |
| 1627 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1628 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1629 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1630 | if ethType: |
| 1631 | cmd += " --ethType " + str( ethType ) |
| 1632 | if ethSrc: |
| 1633 | cmd += " --ethSrc " + str( ethSrc ) |
| 1634 | if ethDst: |
| 1635 | cmd += " --ethDst " + str( ethDst ) |
| 1636 | if bandwidth: |
| 1637 | cmd += " --bandwidth " + str( bandwidth ) |
| 1638 | if lambdaAlloc: |
| 1639 | cmd += " --lambda " |
| 1640 | if ipProto: |
| 1641 | cmd += " --ipProto " + str( ipProto ) |
| 1642 | if ipSrc: |
| 1643 | cmd += " --ipSrc " + str( ipSrc ) |
| 1644 | if ipDst: |
| 1645 | cmd += " --ipDst " + str( ipDst ) |
| 1646 | if tcpSrc: |
| 1647 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1648 | if tcpDst: |
| 1649 | cmd += " --tcpDst " + str( tcpDst ) |
| 1650 | if setEthSrc: |
| 1651 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1652 | if setEthDst: |
| 1653 | cmd += " --setEthDst " + str( setEthDst ) |
| 1654 | if vlanId: |
| 1655 | cmd += " -v " + str( vlanId ) |
| 1656 | if setVlan: |
| 1657 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1658 | if partial: |
| 1659 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1660 | if encap: |
| 1661 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1662 | |
| 1663 | # Check whether the user appended the port |
| 1664 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1665 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1666 | if "/" in ingressDevice: |
| 1667 | cmd += " " + str( ingressDevice ) |
| 1668 | else: |
| 1669 | if not portIngress: |
| 1670 | main.log.error( "You must specify " + |
| 1671 | "the Ingress port" ) |
| 1672 | return main.FALSE |
| 1673 | |
| 1674 | cmd += " " +\ |
| 1675 | str( ingressDevice ) + "/" +\ |
| 1676 | str( portIngress ) |
| 1677 | |
| 1678 | if portEgressList is None: |
| 1679 | for egressDevice in egressDeviceList: |
| 1680 | if "/" in egressDevice: |
| 1681 | cmd += " " + str( egressDevice ) |
| 1682 | else: |
| 1683 | main.log.error( "You must specify " + |
| 1684 | "the egress port" ) |
| 1685 | # TODO: perhaps more meaningful return |
| 1686 | return main.FALSE |
| 1687 | else: |
| 1688 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1689 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1690 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1691 | cmd += " " + \ |
| 1692 | str( egressDevice ) + "/" +\ |
| 1693 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1694 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1695 | main.log.error( "Device list and port list does not " + |
| 1696 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1697 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1698 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1699 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1700 | # If error, return error message |
| 1701 | if re.search( "Error", handle ): |
| 1702 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1703 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1704 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1705 | else: |
| 1706 | match = re.search('id=0x([\da-f]+),', handle) |
| 1707 | if match: |
| 1708 | return match.group()[3:-1] |
| 1709 | else: |
| 1710 | main.log.error( "Error, intent ID not found" ) |
| 1711 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1712 | except AssertionError: |
| 1713 | main.log.exception( "" ) |
| 1714 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1715 | except TypeError: |
| 1716 | main.log.exception( self.name + ": Object not as expected" ) |
| 1717 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1718 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1719 | main.log.error( self.name + ": EOF exception found" ) |
| 1720 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1721 | main.cleanup() |
| 1722 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1723 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1724 | main.log.exception( self.name + ": Uncaught exception!" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1725 | main.cleanup() |
| 1726 | main.exit() |
| 1727 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1728 | def addMplsIntent( |
| 1729 | self, |
| 1730 | ingressDevice, |
| 1731 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1732 | ingressPort="", |
| 1733 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1734 | ethType="", |
| 1735 | ethSrc="", |
| 1736 | ethDst="", |
| 1737 | bandwidth="", |
| 1738 | lambdaAlloc=False, |
| 1739 | ipProto="", |
| 1740 | ipSrc="", |
| 1741 | ipDst="", |
| 1742 | tcpSrc="", |
| 1743 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1744 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1745 | egressLabel="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1746 | priority=""): |
| 1747 | """ |
| 1748 | Required: |
| 1749 | * ingressDevice: device id of ingress device |
| 1750 | * egressDevice: device id of egress device |
| 1751 | Optional: |
| 1752 | * ethType: specify ethType |
| 1753 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1754 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1755 | * bandwidth: specify bandwidth capacity of link |
| 1756 | * lambdaAlloc: if True, intent will allocate lambda |
| 1757 | for the specified intent |
| 1758 | * ipProto: specify ip protocol |
| 1759 | * ipSrc: specify ip source address |
| 1760 | * ipDst: specify ip destination address |
| 1761 | * tcpSrc: specify tcp source port |
| 1762 | * tcpDst: specify tcp destination port |
| 1763 | * ingressLabel: Ingress MPLS label |
| 1764 | * egressLabel: Egress MPLS label |
| 1765 | Description: |
| 1766 | Adds MPLS intent by |
| 1767 | specifying device id's and optional fields |
| 1768 | Returns: |
| 1769 | A string of the intent id or None on error |
| 1770 | |
| 1771 | NOTE: This function may change depending on the |
| 1772 | options developers provide for MPLS |
| 1773 | intent via cli |
| 1774 | """ |
| 1775 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1776 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1777 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1778 | if ethType: |
| 1779 | cmd += " --ethType " + str( ethType ) |
| 1780 | if ethSrc: |
| 1781 | cmd += " --ethSrc " + str( ethSrc ) |
| 1782 | if ethDst: |
| 1783 | cmd += " --ethDst " + str( ethDst ) |
| 1784 | if bandwidth: |
| 1785 | cmd += " --bandwidth " + str( bandwidth ) |
| 1786 | if lambdaAlloc: |
| 1787 | cmd += " --lambda " |
| 1788 | if ipProto: |
| 1789 | cmd += " --ipProto " + str( ipProto ) |
| 1790 | if ipSrc: |
| 1791 | cmd += " --ipSrc " + str( ipSrc ) |
| 1792 | if ipDst: |
| 1793 | cmd += " --ipDst " + str( ipDst ) |
| 1794 | if tcpSrc: |
| 1795 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1796 | if tcpDst: |
| 1797 | cmd += " --tcpDst " + str( tcpDst ) |
| 1798 | if ingressLabel: |
| 1799 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1800 | if egressLabel: |
| 1801 | cmd += " --egressLabel " + str( egressLabel ) |
| 1802 | if priority: |
| 1803 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1804 | |
| 1805 | # Check whether the user appended the port |
| 1806 | # or provided it as an input |
| 1807 | if "/" in ingressDevice: |
| 1808 | cmd += " " + str( ingressDevice ) |
| 1809 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1810 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1811 | main.log.error( "You must specify the ingress port" ) |
| 1812 | return None |
| 1813 | |
| 1814 | cmd += " " + \ |
| 1815 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1816 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1817 | |
| 1818 | if "/" in egressDevice: |
| 1819 | cmd += " " + str( egressDevice ) |
| 1820 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1821 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1822 | main.log.error( "You must specify the egress port" ) |
| 1823 | return None |
| 1824 | |
| 1825 | cmd += " " +\ |
| 1826 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1827 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1828 | |
| 1829 | handle = self.sendline( cmd ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1830 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1831 | # If error, return error message |
| 1832 | if re.search( "Error", handle ): |
| 1833 | main.log.error( "Error in adding mpls intent" ) |
| 1834 | return None |
| 1835 | else: |
| 1836 | # TODO: print out all the options in this message? |
| 1837 | main.log.info( "MPLS intent installed between " + |
| 1838 | str( ingressDevice ) + " and " + |
| 1839 | str( egressDevice ) ) |
| 1840 | match = re.search('id=0x([\da-f]+),', handle) |
| 1841 | if match: |
| 1842 | return match.group()[3:-1] |
| 1843 | else: |
| 1844 | main.log.error( "Error, intent ID not found" ) |
| 1845 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1846 | except AssertionError: |
| 1847 | main.log.exception( "" ) |
| 1848 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1849 | except TypeError: |
| 1850 | main.log.exception( self.name + ": Object not as expected" ) |
| 1851 | return None |
| 1852 | except pexpect.EOF: |
| 1853 | main.log.error( self.name + ": EOF exception found" ) |
| 1854 | main.log.error( self.name + ": " + self.handle.before ) |
| 1855 | main.cleanup() |
| 1856 | main.exit() |
| 1857 | except Exception: |
| 1858 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1859 | main.cleanup() |
| 1860 | main.exit() |
| 1861 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1862 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1863 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1864 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1865 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1866 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1867 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1868 | -p or --purge: Purge the intent from the store after removal |
| 1869 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1870 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 1871 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1872 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1873 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1874 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1875 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1876 | if purge: |
| 1877 | cmdStr += " -p" |
| 1878 | if sync: |
| 1879 | cmdStr += " -s" |
| 1880 | |
| 1881 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1882 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1883 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1884 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1885 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1886 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1887 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1888 | # TODO: Should this be main.TRUE |
| 1889 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1890 | except AssertionError: |
| 1891 | main.log.exception( "" ) |
| 1892 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1893 | except TypeError: |
| 1894 | main.log.exception( self.name + ": Object not as expected" ) |
| 1895 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1896 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1897 | main.log.error( self.name + ": EOF exception found" ) |
| 1898 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1899 | main.cleanup() |
| 1900 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1901 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1902 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1903 | main.cleanup() |
| 1904 | main.exit() |
| 1905 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1906 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1907 | """ |
| 1908 | Description: |
| 1909 | Remove all the intents |
| 1910 | Optional args:- |
| 1911 | -s or --sync: Waits for the removal before returning |
| 1912 | -p or --purge: Purge the intent from the store after removal |
| 1913 | Returns: |
| 1914 | Returns main.TRUE if all intents are removed, otherwise returns |
| 1915 | main.FALSE; Returns None for exception |
| 1916 | """ |
| 1917 | try: |
| 1918 | cmdStr = "remove-intent" |
| 1919 | if purge: |
| 1920 | cmdStr += " -p" |
| 1921 | if sync: |
| 1922 | cmdStr += " -s" |
| 1923 | |
| 1924 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1925 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1926 | assert "Command not found:" not in handle, handle |
| 1927 | if re.search( "Error", handle ): |
| 1928 | main.log.error( "Error in removing intent" ) |
| 1929 | return main.FALSE |
| 1930 | else: |
| 1931 | return main.TRUE |
| 1932 | except AssertionError: |
| 1933 | main.log.exception( "" ) |
| 1934 | return None |
| 1935 | except TypeError: |
| 1936 | main.log.exception( self.name + ": Object not as expected" ) |
| 1937 | return None |
| 1938 | except pexpect.EOF: |
| 1939 | main.log.error( self.name + ": EOF exception found" ) |
| 1940 | main.log.error( self.name + ": " + self.handle.before ) |
| 1941 | main.cleanup() |
| 1942 | main.exit() |
| 1943 | except Exception: |
| 1944 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1945 | main.cleanup() |
| 1946 | main.exit() |
| 1947 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 1948 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1949 | """ |
| 1950 | Purges all WITHDRAWN Intents |
| 1951 | """ |
| 1952 | try: |
| 1953 | cmdStr = "purge-intents" |
| 1954 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1955 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1956 | if re.search( "Error", handle ): |
| 1957 | main.log.error( "Error in purging intents" ) |
| 1958 | return main.FALSE |
| 1959 | else: |
| 1960 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1961 | except AssertionError: |
| 1962 | main.log.exception( "" ) |
| 1963 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1964 | except TypeError: |
| 1965 | main.log.exception( self.name + ": Object not as expected" ) |
| 1966 | return None |
| 1967 | except pexpect.EOF: |
| 1968 | main.log.error( self.name + ": EOF exception found" ) |
| 1969 | main.log.error( self.name + ": " + self.handle.before ) |
| 1970 | main.cleanup() |
| 1971 | main.exit() |
| 1972 | except Exception: |
| 1973 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1974 | main.cleanup() |
| 1975 | main.exit() |
| 1976 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1977 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1978 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1979 | NOTE: This method should be used after installing application: |
| 1980 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1981 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1982 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1983 | Description: |
| 1984 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1985 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1986 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1987 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1988 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1989 | cmdStr += " -j" |
| 1990 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1991 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1992 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1993 | except AssertionError: |
| 1994 | main.log.exception( "" ) |
| 1995 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1996 | except TypeError: |
| 1997 | main.log.exception( self.name + ": Object not as expected" ) |
| 1998 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1999 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2000 | main.log.error( self.name + ": EOF exception found" ) |
| 2001 | main.log.error( self.name + ": " + self.handle.before ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2002 | main.cleanup() |
| 2003 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2004 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2005 | main.log.exception( self.name + ": Uncaught exception!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2006 | main.cleanup() |
| 2007 | main.exit() |
| 2008 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2009 | def ipv4RouteNumber( self ): |
| 2010 | """ |
| 2011 | NOTE: This method should be used after installing application: |
| 2012 | onos-app-sdnip |
| 2013 | Description: |
| 2014 | Obtain the total IPv4 routes number in the system |
| 2015 | """ |
| 2016 | try: |
| 2017 | cmdStr = "routes -s -j" |
| 2018 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2019 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2020 | jsonResult = json.loads( handle ) |
| 2021 | return jsonResult['totalRoutes4'] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2022 | except AssertionError: |
| 2023 | main.log.exception( "" ) |
| 2024 | return None |
| 2025 | except ( TypeError, ValueError ): |
| 2026 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2027 | return None |
| 2028 | except pexpect.EOF: |
| 2029 | main.log.error( self.name + ": EOF exception found" ) |
| 2030 | main.log.error( self.name + ": " + self.handle.before ) |
| 2031 | main.cleanup() |
| 2032 | main.exit() |
| 2033 | except Exception: |
| 2034 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2035 | main.cleanup() |
| 2036 | main.exit() |
| 2037 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2038 | def intents( self, jsonFormat = True, summary = False, **intentargs): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2039 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2040 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2041 | Obtain intents from the ONOS cli. |
| 2042 | Optional: |
| 2043 | * jsonFormat: Enable output formatting in json, default to True |
| 2044 | * summary: Whether only output the intent summary, defaults to False |
| 2045 | * type: Only output a certain type of intent. This options is valid |
| 2046 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2047 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2048 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2049 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2050 | if summary: |
| 2051 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2052 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2053 | cmdStr += " -j" |
| 2054 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2055 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2056 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2057 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2058 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2059 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2060 | intentType = "" |
| 2061 | # IF we want the summary of a specific intent type |
| 2062 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2063 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2064 | if intentType in jsonResult.keys(): |
| 2065 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2066 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2067 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2068 | return handle |
| 2069 | else: |
| 2070 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2071 | except AssertionError: |
| 2072 | main.log.exception( "" ) |
| 2073 | return None |
| 2074 | except ( TypeError, ValueError ): |
| 2075 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2076 | return None |
| 2077 | except pexpect.EOF: |
| 2078 | main.log.error( self.name + ": EOF exception found" ) |
| 2079 | main.log.error( self.name + ": " + self.handle.before ) |
| 2080 | main.cleanup() |
| 2081 | main.exit() |
| 2082 | except Exception: |
| 2083 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2084 | main.cleanup() |
| 2085 | main.exit() |
| 2086 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2087 | def getIntentState(self, intentsId, intentsJson=None): |
| 2088 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2089 | Description: |
| 2090 | Gets intent state. Accepts a single intent ID (string type) or a |
| 2091 | list of intent IDs. |
| 2092 | Parameters: |
| 2093 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2094 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2095 | Returns: |
| 2096 | Returns the state (string type) of the ID if a single intent ID is |
| 2097 | accepted. |
| 2098 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2099 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2100 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2101 | """ |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2102 | try: |
| 2103 | state = "State is Undefined" |
| 2104 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2105 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2106 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2107 | rawJson = intentsJson |
| 2108 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2109 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2110 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2111 | if intentsId == intent[ 'id' ]: |
| 2112 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2113 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2114 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
| 2115 | " on the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2116 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2117 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2118 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2119 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2120 | stateDict = {} |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2121 | for intents in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2122 | if intentsId[ i ] == intents[ 'id' ]: |
| 2123 | stateDict[ 'state' ] = intents[ 'state' ] |
| 2124 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2125 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2126 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2127 | if len( intentsId ) != len( dictList ): |
| 2128 | main.log.info( "Cannot find some of the intent ID state" ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2129 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2130 | else: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2131 | main.log.info( "Invalid intents ID entry" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2132 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2133 | except ( TypeError, ValueError ): |
| 2134 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2135 | return None |
| 2136 | except pexpect.EOF: |
| 2137 | main.log.error( self.name + ": EOF exception found" ) |
| 2138 | main.log.error( self.name + ": " + self.handle.before ) |
| 2139 | main.cleanup() |
| 2140 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2141 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2142 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2143 | main.cleanup() |
| 2144 | main.exit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2145 | |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2146 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2147 | """ |
| 2148 | Description: |
| 2149 | Check intents state |
| 2150 | Required: |
| 2151 | intentsId - List of intents ID to be checked |
| 2152 | Optional: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2153 | expectedState - Check the expected state(s) of each intents |
| 2154 | state in the list. |
| 2155 | *NOTE: You can pass in a list of expected state, |
| 2156 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2157 | Return: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2158 | Returns main.TRUE only if all intent are the same as expected states |
| 2159 | , otherwise, returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2160 | """ |
| 2161 | try: |
| 2162 | # Generating a dictionary: intent id as a key and state as value |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2163 | returnValue = main.TRUE |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2164 | intentsDict = self.getIntentState( intentsId ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2165 | if len( intentsId ) != len( intentsDict ): |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2166 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2167 | "getting intents state" ) |
| 2168 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2169 | |
| 2170 | if isinstance( expectedState, types.StringType ): |
| 2171 | for intents in intentsDict: |
| 2172 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2173 | main.log.debug( self.name + " : Intent ID - " + |
| 2174 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2175 | " actual state = " + |
| 2176 | intents.get( 'state' ) |
| 2177 | + " does not equal expected state = " |
| 2178 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2179 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2180 | |
| 2181 | elif isinstance( expectedState, types.ListType ): |
| 2182 | for intents in intentsDict: |
| 2183 | if not any( state == intents.get( 'state' ) for state in |
| 2184 | expectedState ): |
| 2185 | main.log.debug( self.name + " : Intent ID - " + |
| 2186 | intents.get( 'id' ) + |
| 2187 | " actual state = " + |
| 2188 | intents.get( 'state' ) + |
| 2189 | " does not equal expected states = " |
| 2190 | + str( expectedState ) ) |
| 2191 | returnValue = main.FALSE |
| 2192 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2193 | if returnValue == main.TRUE: |
| 2194 | main.log.info( self.name + ": All " + |
| 2195 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2196 | " intents are in " + str( expectedState ) + |
| 2197 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2198 | return returnValue |
| 2199 | except TypeError: |
| 2200 | main.log.exception( self.name + ": Object not as expected" ) |
| 2201 | return None |
| 2202 | except pexpect.EOF: |
| 2203 | main.log.error( self.name + ": EOF exception found" ) |
| 2204 | main.log.error( self.name + ": " + self.handle.before ) |
| 2205 | main.cleanup() |
| 2206 | main.exit() |
| 2207 | except Exception: |
| 2208 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2209 | main.cleanup() |
| 2210 | main.exit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2211 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2212 | def compareIntent( self, intentDict ): |
| 2213 | """ |
| 2214 | Description: |
| 2215 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2216 | Return: |
| 2217 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2218 | Arguments: |
| 2219 | intentDict: a dictionary which maps intent ids to intent states |
| 2220 | """ |
| 2221 | try: |
| 2222 | intentsRaw = self.intents() |
| 2223 | intentsJson = json.loads( intentsRaw ) |
| 2224 | intentDictONOS = {} |
| 2225 | for intent in intentsJson: |
| 2226 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2227 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2228 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2229 | 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] | 2230 | str( len( intentDict ) ) + " expected and " + |
| 2231 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2232 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2233 | for intentID in intentDict.keys(): |
| 2234 | if not intentID in intentDictONOS.keys(): |
| 2235 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2236 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2237 | else: |
| 2238 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2239 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2240 | " expected state is " + intentDict[ intentID ] + |
| 2241 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2242 | returnValue = main.FALSE |
| 2243 | intentDictONOS.pop( intentID ) |
| 2244 | if len( intentDictONOS ) > 0: |
| 2245 | returnValue = main.FALSE |
| 2246 | for intentID in intentDictONOS.keys(): |
| 2247 | 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] | 2248 | if returnValue == main.TRUE: |
| 2249 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2250 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2251 | except KeyError: |
| 2252 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2253 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2254 | except ( TypeError, ValueError ): |
| 2255 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2256 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2257 | except pexpect.EOF: |
| 2258 | main.log.error( self.name + ": EOF exception found" ) |
| 2259 | main.log.error( self.name + ": " + self.handle.before ) |
| 2260 | main.cleanup() |
| 2261 | main.exit() |
| 2262 | except Exception: |
| 2263 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2264 | main.cleanup() |
| 2265 | main.exit() |
| 2266 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2267 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2268 | """ |
| 2269 | Description: |
| 2270 | Check the number of installed intents. |
| 2271 | Optional: |
| 2272 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2273 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2274 | Return: |
| 2275 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2276 | , otherwise, returns main.FALSE. |
| 2277 | """ |
| 2278 | |
| 2279 | try: |
| 2280 | cmd = "intents -s -j" |
| 2281 | |
| 2282 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2283 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2284 | if response == None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2285 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2286 | response = json.loads( response ) |
| 2287 | |
| 2288 | # get total and installed number, see if they are match |
| 2289 | allState = response.get( 'all' ) |
| 2290 | if allState.get('total') == allState.get('installed'): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2291 | 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] | 2292 | return main.TRUE |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2293 | 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] | 2294 | return main.FALSE |
| 2295 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2296 | except ( TypeError, ValueError ): |
| 2297 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2298 | return None |
| 2299 | except pexpect.EOF: |
| 2300 | main.log.error( self.name + ": EOF exception found" ) |
| 2301 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2302 | if noExit: |
| 2303 | return main.FALSE |
| 2304 | else: |
| 2305 | main.cleanup() |
| 2306 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2307 | except Exception: |
| 2308 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2309 | if noExit: |
| 2310 | return main.FALSE |
| 2311 | else: |
| 2312 | main.cleanup() |
| 2313 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2314 | except pexpect.TIMEOUT: |
| 2315 | main.log.error( self.name + ": ONOS timeout" ) |
| 2316 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2317 | |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2318 | def flows( self, state="", jsonFormat=True, timeout=60, noExit=False, noCore=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2319 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2320 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2321 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2322 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2323 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2324 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2325 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2326 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2327 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2328 | if jsonFormat: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2329 | cmdStr += " -j " |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2330 | if noCore: |
| 2331 | cmdStr += " -n " |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2332 | cmdStr += state |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2333 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2334 | assert "Command not found:" not in handle, handle |
| 2335 | if re.search( "Error:", handle ): |
| 2336 | main.log.error( self.name + ": flows() response: " + |
| 2337 | str( handle ) ) |
| 2338 | return handle |
| 2339 | except AssertionError: |
| 2340 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2341 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2342 | except TypeError: |
| 2343 | main.log.exception( self.name + ": Object not as expected" ) |
| 2344 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2345 | except pexpect.TIMEOUT: |
| 2346 | main.log.error( self.name + ": ONOS timeout" ) |
| 2347 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2348 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2349 | main.log.error( self.name + ": EOF exception found" ) |
| 2350 | main.log.error( self.name + ": " + self.handle.before ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2351 | main.cleanup() |
| 2352 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2353 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2354 | main.log.exception( self.name + ": Uncaught exception!" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2355 | main.cleanup() |
| 2356 | main.exit() |
| 2357 | |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2358 | def checkFlowCount(self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2359 | count = self.getTotalFlowsNum( timeout=timeout ) |
| 2360 | count = int (count) if count else 0 |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2361 | return count if (count > min) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2362 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2363 | def checkFlowsState( self, isPENDING=True, timeout=60,noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2364 | """ |
| 2365 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2366 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2367 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2368 | if the count of those states is 0, which means all current flows |
| 2369 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2370 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2371 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2372 | Return: |
| 2373 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2374 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2375 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2376 | """ |
| 2377 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2378 | states = ["PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED"] |
| 2379 | checkedStates = [] |
| 2380 | statesCount = [0, 0, 0, 0] |
| 2381 | for s in states: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2382 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2383 | if rawFlows: |
| 2384 | # if we didn't get flows or flows function return None, we should return |
| 2385 | # main.Flase |
| 2386 | checkedStates.append( json.loads( rawFlows ) ) |
| 2387 | else: |
| 2388 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2389 | for i in range( len( states ) ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2390 | for c in checkedStates[i]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2391 | try: |
| 2392 | statesCount[i] += int( c.get( "flowCount" ) ) |
| 2393 | except TypeError: |
| 2394 | main.log.exception( "Json object not as expected" ) |
| 2395 | main.log.info( states[i] + " flows: " + str( statesCount[i] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2396 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2397 | # We want to count PENDING_ADD if isPENDING is true |
| 2398 | if isPENDING: |
| 2399 | if statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2400 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2401 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2402 | if statesCount[0] + statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2403 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2404 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2405 | except ( TypeError, ValueError ): |
| 2406 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2407 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2408 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2409 | except AssertionError: |
| 2410 | main.log.exception( "" ) |
| 2411 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2412 | except pexpect.EOF: |
| 2413 | main.log.error( self.name + ": EOF exception found" ) |
| 2414 | main.log.error( self.name + ": " + self.handle.before ) |
| 2415 | main.cleanup() |
| 2416 | main.exit() |
| 2417 | except Exception: |
| 2418 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2419 | main.cleanup() |
| 2420 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2421 | except pexpect.TIMEOUT: |
| 2422 | main.log.error( self.name + ": ONOS timeout" ) |
| 2423 | return None |
| 2424 | |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2425 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2426 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2427 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2428 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2429 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2430 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2431 | a specific point-to-point intent definition |
| 2432 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2433 | * ingress: specify source dpid |
| 2434 | * egress: specify destination dpid |
| 2435 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2436 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2437 | * offset: the keyOffset is where the next batch of intents |
| 2438 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2439 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2440 | * getResponse: If set to True, function will return ONOS response. |
| 2441 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2442 | Returns: If failed to push test intents, it will returen None, |
| 2443 | if successful, return true. |
| 2444 | Timeout expection will return None, |
| 2445 | TypeError will return false |
| 2446 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2447 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2448 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2449 | if background: |
| 2450 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2451 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2452 | back = "" |
| 2453 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2454 | ingress, |
| 2455 | egress, |
| 2456 | batchSize, |
| 2457 | offset, |
| 2458 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2459 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2460 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2461 | main.log.info( response ) |
| 2462 | if response == None: |
| 2463 | return None |
| 2464 | |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2465 | if getResponse: |
| 2466 | return response |
| 2467 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2468 | # TODO: We should handle if there is failure in installation |
| 2469 | return main.TRUE |
| 2470 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2471 | except AssertionError: |
| 2472 | main.log.exception( "" ) |
| 2473 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2474 | except pexpect.TIMEOUT: |
| 2475 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2476 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2477 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2478 | main.log.error( self.name + ": EOF exception found" ) |
| 2479 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2480 | main.cleanup() |
| 2481 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2482 | except TypeError: |
| 2483 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2484 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2485 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2486 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2487 | main.cleanup() |
| 2488 | main.exit() |
| 2489 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2490 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2491 | """ |
| 2492 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2493 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2494 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2495 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2496 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2497 | """ |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2498 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2499 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2500 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2501 | cmd = "flows -c added" |
| 2502 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2503 | if rawFlows: |
| 2504 | rawFlows = rawFlows.split("\n") |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2505 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2506 | for l in rawFlows: |
| 2507 | totalFlows += int(l.split("Count=")[1]) |
| 2508 | else: |
| 2509 | main.log.error("Response not as expected!") |
| 2510 | return None |
| 2511 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2512 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2513 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2514 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2515 | return None |
| 2516 | except pexpect.EOF: |
| 2517 | main.log.error( self.name + ": EOF exception found" ) |
| 2518 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2519 | if not noExit: |
| 2520 | main.cleanup() |
| 2521 | main.exit() |
| 2522 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2523 | except Exception: |
| 2524 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2525 | if not noExit: |
| 2526 | main.cleanup() |
| 2527 | main.exit() |
| 2528 | return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2529 | except pexpect.TIMEOUT: |
| 2530 | main.log.error( self.name + ": ONOS timeout" ) |
| 2531 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2532 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2533 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2534 | """ |
| 2535 | Description: |
| 2536 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2537 | Optional: |
| 2538 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2539 | Return: |
| 2540 | The number of intents |
| 2541 | """ |
| 2542 | try: |
| 2543 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2544 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2545 | if response == None: |
| 2546 | return -1 |
| 2547 | response = json.loads( response ) |
| 2548 | return int( response.get("intents") ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2549 | except ( TypeError, ValueError ): |
| 2550 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2551 | return None |
| 2552 | except pexpect.EOF: |
| 2553 | main.log.error( self.name + ": EOF exception found" ) |
| 2554 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2555 | if noExit: |
| 2556 | return -1 |
| 2557 | else: |
| 2558 | main.cleanup() |
| 2559 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2560 | except Exception: |
| 2561 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2562 | if noExit: |
| 2563 | return -1 |
| 2564 | else: |
| 2565 | main.cleanup() |
| 2566 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2567 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2568 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2569 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2570 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2571 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2572 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2573 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2574 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2575 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2576 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2577 | cmdStr += " -j" |
| 2578 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2579 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2580 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2581 | except AssertionError: |
| 2582 | main.log.exception( "" ) |
| 2583 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2584 | except TypeError: |
| 2585 | main.log.exception( self.name + ": Object not as expected" ) |
| 2586 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2587 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2588 | main.log.error( self.name + ": EOF exception found" ) |
| 2589 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2590 | main.cleanup() |
| 2591 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2592 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2593 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2594 | main.cleanup() |
| 2595 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2596 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2597 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2598 | """ |
| 2599 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2600 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2601 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2602 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2603 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2604 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2605 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2606 | cmdStr += " -j" |
| 2607 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2608 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2609 | if handle: |
| 2610 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2611 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2612 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2613 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2614 | else: |
| 2615 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2616 | except AssertionError: |
| 2617 | main.log.exception( "" ) |
| 2618 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2619 | except TypeError: |
| 2620 | main.log.exception( self.name + ": Object not as expected" ) |
| 2621 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2622 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2623 | main.log.error( self.name + ": EOF exception found" ) |
| 2624 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2625 | main.cleanup() |
| 2626 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2627 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2628 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2629 | main.cleanup() |
| 2630 | main.exit() |
| 2631 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2632 | # Wrapper functions **************** |
| 2633 | # Wrapper functions use existing driver |
| 2634 | # functions and extends their use case. |
| 2635 | # For example, we may use the output of |
| 2636 | # a normal driver function, and parse it |
| 2637 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2638 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2639 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2640 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2641 | Description: |
| 2642 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2643 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2644 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2645 | # Obtain output of intents function |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2646 | intentsStr = self.intents(jsonFormat=True) |
| 2647 | # Convert to a dictionary |
| 2648 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2649 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2650 | for intent in intents: |
| 2651 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2652 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2653 | except TypeError: |
| 2654 | main.log.exception( self.name + ": Object not as expected" ) |
| 2655 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2656 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2657 | main.log.error( self.name + ": EOF exception found" ) |
| 2658 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2659 | main.cleanup() |
| 2660 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2661 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2662 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2663 | main.cleanup() |
| 2664 | main.exit() |
| 2665 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2666 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2667 | """ |
| 2668 | Determine the number of flow rules for the given device id that are |
| 2669 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2670 | Params: |
| 2671 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2672 | """ |
| 2673 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2674 | if core: |
| 2675 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2676 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2677 | else: |
| 2678 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2679 | "grep 'state=ADDED' | wc -l" |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2680 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2681 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2682 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2683 | except AssertionError: |
| 2684 | main.log.exception( "" ) |
| 2685 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2686 | except pexpect.EOF: |
| 2687 | main.log.error( self.name + ": EOF exception found" ) |
| 2688 | main.log.error( self.name + ": " + self.handle.before ) |
| 2689 | main.cleanup() |
| 2690 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2691 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2692 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2693 | main.cleanup() |
| 2694 | main.exit() |
| 2695 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2696 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2697 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2698 | Use 'devices' function to obtain list of all devices |
| 2699 | and parse the result to obtain a list of all device |
| 2700 | id's. Returns this list. Returns empty list if no |
| 2701 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2702 | List is ordered sequentially |
| 2703 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2704 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2705 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2706 | the ids. By obtaining the list of device ids on the fly, |
| 2707 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2708 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2709 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2710 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2711 | devicesStr = self.devices( jsonFormat=False ) |
| 2712 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2713 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2714 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2715 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2716 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2717 | |
| 2718 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2719 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2720 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2721 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2722 | # Split list further into arguments before and after string |
| 2723 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2724 | # append to idList |
| 2725 | for arg in tempList: |
| 2726 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 2727 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2728 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2729 | except TypeError: |
| 2730 | main.log.exception( self.name + ": Object not as expected" ) |
| 2731 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2732 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2733 | main.log.error( self.name + ": EOF exception found" ) |
| 2734 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2735 | main.cleanup() |
| 2736 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2737 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2738 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2739 | main.cleanup() |
| 2740 | main.exit() |
| 2741 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2742 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2743 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2744 | Uses 'nodes' function to obtain list of all nodes |
| 2745 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2746 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2747 | Returns: |
| 2748 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2749 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2750 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2751 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2752 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2753 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 2754 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2755 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2756 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2757 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2758 | nodesJson = json.loads( nodesStr ) |
| 2759 | idList = [ node.get('id') for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2760 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2761 | except ( TypeError, ValueError ): |
| 2762 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2763 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2764 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2765 | main.log.error( self.name + ": EOF exception found" ) |
| 2766 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2767 | main.cleanup() |
| 2768 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2769 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2770 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2771 | main.cleanup() |
| 2772 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2773 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2774 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2775 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2776 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 2777 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2778 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2779 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2780 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2781 | return None |
| 2782 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2783 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2784 | rawDevices = self.devices() |
| 2785 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2786 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2787 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2788 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 2789 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2790 | return device |
| 2791 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2792 | except ( TypeError, ValueError ): |
| 2793 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2794 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2795 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2796 | main.log.error( self.name + ": EOF exception found" ) |
| 2797 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2798 | main.cleanup() |
| 2799 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2800 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2801 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2802 | main.cleanup() |
| 2803 | main.exit() |
| 2804 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2805 | def getTopology( self, topologyOutput ): |
| 2806 | """ |
| 2807 | Definition: |
| 2808 | Loads a json topology output |
| 2809 | Return: |
| 2810 | topology = current ONOS topology |
| 2811 | """ |
| 2812 | import json |
| 2813 | try: |
| 2814 | # either onos:topology or 'topology' will work in CLI |
| 2815 | topology = json.loads(topologyOutput) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2816 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2817 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2818 | except ( TypeError, ValueError ): |
| 2819 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 2820 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2821 | except pexpect.EOF: |
| 2822 | main.log.error( self.name + ": EOF exception found" ) |
| 2823 | main.log.error( self.name + ": " + self.handle.before ) |
| 2824 | main.cleanup() |
| 2825 | main.exit() |
| 2826 | except Exception: |
| 2827 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2828 | main.cleanup() |
| 2829 | main.exit() |
| 2830 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2831 | def checkStatus(self, numoswitch, numolink, numoctrl = -1, logLevel="info"): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2832 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2833 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2834 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2835 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2836 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2837 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2838 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2839 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2840 | logLevel = level to log to. |
| 2841 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2842 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2843 | Returns: main.TRUE if the number of switches and links are correct, |
| 2844 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2845 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2846 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2847 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2848 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 2849 | summary = self.summary() |
| 2850 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 2851 | except ( TypeError, ValueError ): |
| 2852 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 2853 | return main.ERROR |
| 2854 | try: |
| 2855 | topology = self.getTopology( self.topology() ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2856 | if topology == {} or topology == None or summary == {} or summary == None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2857 | return main.ERROR |
| 2858 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2859 | # Is the number of switches is what we expected |
| 2860 | devices = topology.get( 'devices', False ) |
| 2861 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2862 | nodes = summary.get( 'nodes', False ) |
| 2863 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2864 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2865 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2866 | # Is the number of links is what we expected |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2867 | linkCheck = ( int( links ) == int( numolink ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2868 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 2869 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2870 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2871 | output = output + "The number of links and switches match "\ |
| 2872 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2873 | result = main.TRUE |
| 2874 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2875 | output = output + \ |
| 2876 | "The number of links and switches does not match " + \ |
| 2877 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2878 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2879 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 2880 | output = output + " (%i expected) " % int( numoswitch ) |
| 2881 | output = output + "and %i links " % int( links ) |
| 2882 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 2883 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2884 | output = output + "and %i controllers " % int( nodes ) |
| 2885 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2886 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2887 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2888 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2889 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2890 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2891 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2892 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2893 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2894 | main.log.error( self.name + ": EOF exception found" ) |
| 2895 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2896 | main.cleanup() |
| 2897 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2898 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2899 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2900 | main.cleanup() |
| 2901 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2902 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2903 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2904 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2905 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2906 | deviceId must be the id of a device as seen in the onos devices command |
| 2907 | 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] | 2908 | role must be either master, standby, or none |
| 2909 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2910 | Returns: |
| 2911 | main.TRUE or main.FALSE based on argument verification and |
| 2912 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2913 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2914 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2915 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2916 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2917 | cmdStr = "device-role " +\ |
| 2918 | str( deviceId ) + " " +\ |
| 2919 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2920 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2921 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2922 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2923 | if re.search( "Error", handle ): |
| 2924 | # end color output to escape any colours |
| 2925 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2926 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2927 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2928 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2929 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2930 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2931 | main.log.error( "Invalid 'role' given to device_role(). " + |
| 2932 | "Value was '" + str(role) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2933 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2934 | except AssertionError: |
| 2935 | main.log.exception( "" ) |
| 2936 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2937 | except TypeError: |
| 2938 | main.log.exception( self.name + ": Object not as expected" ) |
| 2939 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2940 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2941 | main.log.error( self.name + ": EOF exception found" ) |
| 2942 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2943 | main.cleanup() |
| 2944 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2945 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2946 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2947 | main.cleanup() |
| 2948 | main.exit() |
| 2949 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2950 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2951 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2952 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2953 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2954 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2955 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2956 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2957 | cmdStr = "clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2958 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2959 | cmdStr += " -j" |
| 2960 | handle = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2961 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2962 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2963 | except AssertionError: |
| 2964 | main.log.exception( "" ) |
| 2965 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2966 | except TypeError: |
| 2967 | main.log.exception( self.name + ": Object not as expected" ) |
| 2968 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2969 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2970 | main.log.error( self.name + ": EOF exception found" ) |
| 2971 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2972 | main.cleanup() |
| 2973 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2974 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2975 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2976 | main.cleanup() |
| 2977 | main.exit() |
| 2978 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2979 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2980 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2981 | CLI command to get the current leader for the Election test application |
| 2982 | NOTE: Requires installation of the onos-app-election feature |
| 2983 | Returns: Node IP of the leader if one exists |
| 2984 | None if none exists |
| 2985 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2986 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2987 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2988 | cmdStr = "election-test-leader" |
| 2989 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2990 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2991 | # Leader |
| 2992 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2993 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2994 | nodeSearch = re.search( leaderPattern, response ) |
| 2995 | if nodeSearch: |
| 2996 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2997 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2998 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 2999 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3000 | # no leader |
| 3001 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3002 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3003 | nullSearch = re.search( nullPattern, response ) |
| 3004 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3005 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3006 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3007 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3008 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3009 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 3010 | ": " + "unexpected response" ) |
| 3011 | main.log.error( repr( response ) ) |
| 3012 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3013 | except AssertionError: |
| 3014 | main.log.exception( "" ) |
| 3015 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3016 | except TypeError: |
| 3017 | main.log.exception( self.name + ": Object not as expected" ) |
| 3018 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3019 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3020 | main.log.error( self.name + ": EOF exception found" ) |
| 3021 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3022 | main.cleanup() |
| 3023 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3024 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3025 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3026 | main.cleanup() |
| 3027 | main.exit() |
| 3028 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3029 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3030 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3031 | CLI command to run for leadership of the Election test application. |
| 3032 | NOTE: Requires installation of the onos-app-election feature |
| 3033 | Returns: Main.TRUE on success |
| 3034 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3035 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3036 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3037 | cmdStr = "election-test-run" |
| 3038 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3039 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3040 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3041 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3042 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3043 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3044 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3045 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3046 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3047 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3048 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3049 | main.log.error( "Error in electionTestRun on " + self.name + |
| 3050 | ": " + "unexpected response" ) |
| 3051 | main.log.error( repr( response ) ) |
| 3052 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3053 | except AssertionError: |
| 3054 | main.log.exception( "" ) |
| 3055 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3056 | except TypeError: |
| 3057 | main.log.exception( self.name + ": Object not as expected" ) |
| 3058 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3059 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3060 | main.log.error( self.name + ": EOF exception found" ) |
| 3061 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3062 | main.cleanup() |
| 3063 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3064 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3065 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3066 | main.cleanup() |
| 3067 | main.exit() |
| 3068 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3069 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3070 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3071 | * CLI command to withdraw the local node from leadership election for |
| 3072 | * the Election test application. |
| 3073 | #NOTE: Requires installation of the onos-app-election feature |
| 3074 | Returns: Main.TRUE on success |
| 3075 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3076 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3077 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3078 | cmdStr = "election-test-withdraw" |
| 3079 | response = self.sendline( cmdStr ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3080 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3081 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3082 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3083 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3084 | if re.search( successPattern, response ): |
| 3085 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3086 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3087 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3088 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3089 | main.log.error( "Error in electionTestWithdraw on " + |
| 3090 | self.name + ": " + "unexpected response" ) |
| 3091 | main.log.error( repr( response ) ) |
| 3092 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3093 | except AssertionError: |
| 3094 | main.log.exception( "" ) |
| 3095 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3096 | except TypeError: |
| 3097 | main.log.exception( self.name + ": Object not as expected" ) |
| 3098 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3099 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3100 | main.log.error( self.name + ": EOF exception found" ) |
| 3101 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3102 | main.cleanup() |
| 3103 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3104 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3105 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3106 | main.cleanup() |
| 3107 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3108 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3109 | def getDevicePortsEnabledCount( self, dpid ): |
| 3110 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3111 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3112 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3113 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3114 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3115 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 3116 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3117 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3118 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3119 | if re.search( "No such device", output ): |
| 3120 | main.log.error( "Error in getting ports" ) |
| 3121 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3122 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3123 | except AssertionError: |
| 3124 | main.log.exception( "" ) |
| 3125 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3126 | except TypeError: |
| 3127 | main.log.exception( self.name + ": Object not as expected" ) |
| 3128 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3129 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3130 | main.log.error( self.name + ": EOF exception found" ) |
| 3131 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3132 | main.cleanup() |
| 3133 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3134 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3135 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3136 | main.cleanup() |
| 3137 | main.exit() |
| 3138 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3139 | def getDeviceLinksActiveCount( self, dpid ): |
| 3140 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3141 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3142 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3143 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3144 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3145 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3146 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3147 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3148 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3149 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3150 | main.log.error( "Error in getting ports " ) |
| 3151 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3152 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3153 | except AssertionError: |
| 3154 | main.log.exception( "" ) |
| 3155 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3156 | except TypeError: |
| 3157 | main.log.exception( self.name + ": Object not as expected" ) |
| 3158 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3159 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3160 | main.log.error( self.name + ": EOF exception found" ) |
| 3161 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3162 | main.cleanup() |
| 3163 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3164 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3165 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3166 | main.cleanup() |
| 3167 | main.exit() |
| 3168 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3169 | def getAllIntentIds( self ): |
| 3170 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3171 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3172 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3173 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3174 | cmdStr = "onos:intents | grep id=" |
| 3175 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3176 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3177 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3178 | if re.search( "Error", output ): |
| 3179 | main.log.error( "Error in getting ports" ) |
| 3180 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3181 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3182 | except AssertionError: |
| 3183 | main.log.exception( "" ) |
| 3184 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3185 | except TypeError: |
| 3186 | main.log.exception( self.name + ": Object not as expected" ) |
| 3187 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3188 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3189 | main.log.error( self.name + ": EOF exception found" ) |
| 3190 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3191 | main.cleanup() |
| 3192 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3193 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3194 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3195 | main.cleanup() |
| 3196 | main.exit() |
| 3197 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3198 | def intentSummary( self ): |
| 3199 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3200 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3201 | """ |
| 3202 | try: |
| 3203 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3204 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3205 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3206 | states.append( intent.get( 'state', None ) ) |
| 3207 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3208 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3209 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3210 | except ( TypeError, ValueError ): |
| 3211 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3212 | return None |
| 3213 | except pexpect.EOF: |
| 3214 | main.log.error( self.name + ": EOF exception found" ) |
| 3215 | main.log.error( self.name + ": " + self.handle.before ) |
| 3216 | main.cleanup() |
| 3217 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3218 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3219 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3220 | main.cleanup() |
| 3221 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3222 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3223 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3224 | """ |
| 3225 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3226 | Optional argument: |
| 3227 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3228 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3229 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3230 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3231 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3232 | cmdStr += " -j" |
| 3233 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3234 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3235 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3236 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3237 | except AssertionError: |
| 3238 | main.log.exception( "" ) |
| 3239 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3240 | except TypeError: |
| 3241 | main.log.exception( self.name + ": Object not as expected" ) |
| 3242 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3243 | except pexpect.EOF: |
| 3244 | main.log.error( self.name + ": EOF exception found" ) |
| 3245 | main.log.error( self.name + ": " + self.handle.before ) |
| 3246 | main.cleanup() |
| 3247 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3248 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3249 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3250 | main.cleanup() |
| 3251 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3252 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3253 | def leaderCandidates( self, jsonFormat=True ): |
| 3254 | """ |
| 3255 | Returns the output of the leaders -c command. |
| 3256 | Optional argument: |
| 3257 | * jsonFormat - boolean indicating if you want output in json |
| 3258 | """ |
| 3259 | try: |
| 3260 | cmdStr = "onos:leaders -c" |
| 3261 | if jsonFormat: |
| 3262 | cmdStr += " -j" |
| 3263 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3264 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3265 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3266 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3267 | except AssertionError: |
| 3268 | main.log.exception( "" ) |
| 3269 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3270 | except TypeError: |
| 3271 | main.log.exception( self.name + ": Object not as expected" ) |
| 3272 | return None |
| 3273 | except pexpect.EOF: |
| 3274 | main.log.error( self.name + ": EOF exception found" ) |
| 3275 | main.log.error( self.name + ": " + self.handle.before ) |
| 3276 | main.cleanup() |
| 3277 | main.exit() |
| 3278 | except Exception: |
| 3279 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3280 | main.cleanup() |
| 3281 | main.exit() |
| 3282 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3283 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3284 | """ |
| 3285 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
| 3286 | topic parameter and an empty list if the topic doesn't exist |
| 3287 | If no leader is elected leader in the returned list will be "none" |
| 3288 | Returns None if there is a type error processing the json object |
| 3289 | """ |
| 3290 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3291 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3292 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3293 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3294 | assert "Command not found:" not in rawOutput, rawOutput |
| 3295 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3296 | results = [] |
| 3297 | for dict in output: |
| 3298 | if dict["topic"] == topic: |
| 3299 | leader = dict["leader"] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3300 | candidates = re.split( ", ", dict["candidates"][1:-1] ) |
| 3301 | results.append( leader ) |
| 3302 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3303 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3304 | except AssertionError: |
| 3305 | main.log.exception( "" ) |
| 3306 | return None |
| 3307 | except ( TypeError, ValueError ): |
| 3308 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3309 | return None |
| 3310 | except pexpect.EOF: |
| 3311 | main.log.error( self.name + ": EOF exception found" ) |
| 3312 | main.log.error( self.name + ": " + self.handle.before ) |
| 3313 | main.cleanup() |
| 3314 | main.exit() |
| 3315 | except Exception: |
| 3316 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3317 | main.cleanup() |
| 3318 | main.exit() |
| 3319 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3320 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3321 | """ |
| 3322 | Returns the output of the intent Pending map. |
| 3323 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3324 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3325 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3326 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3327 | cmdStr += " -j" |
| 3328 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3329 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3330 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3331 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3332 | except AssertionError: |
| 3333 | main.log.exception( "" ) |
| 3334 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3335 | except TypeError: |
| 3336 | main.log.exception( self.name + ": Object not as expected" ) |
| 3337 | return None |
| 3338 | except pexpect.EOF: |
| 3339 | main.log.error( self.name + ": EOF exception found" ) |
| 3340 | main.log.error( self.name + ": " + self.handle.before ) |
| 3341 | main.cleanup() |
| 3342 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3343 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3344 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3345 | main.cleanup() |
| 3346 | main.exit() |
| 3347 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3348 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3349 | """ |
| 3350 | Returns the output of the raft partitions command for ONOS. |
| 3351 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3352 | # Sample JSON |
| 3353 | # { |
| 3354 | # "leader": "tcp://10.128.30.11:7238", |
| 3355 | # "members": [ |
| 3356 | # "tcp://10.128.30.11:7238", |
| 3357 | # "tcp://10.128.30.17:7238", |
| 3358 | # "tcp://10.128.30.13:7238", |
| 3359 | # ], |
| 3360 | # "name": "p1", |
| 3361 | # "term": 3 |
| 3362 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3363 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3364 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3365 | if candidates: |
| 3366 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3367 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3368 | cmdStr += " -j" |
| 3369 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3370 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3371 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3372 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3373 | except AssertionError: |
| 3374 | main.log.exception( "" ) |
| 3375 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3376 | except TypeError: |
| 3377 | main.log.exception( self.name + ": Object not as expected" ) |
| 3378 | return None |
| 3379 | except pexpect.EOF: |
| 3380 | main.log.error( self.name + ": EOF exception found" ) |
| 3381 | main.log.error( self.name + ": " + self.handle.before ) |
| 3382 | main.cleanup() |
| 3383 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3384 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3385 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3386 | main.cleanup() |
| 3387 | main.exit() |
| 3388 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3389 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3390 | """ |
| 3391 | Returns the output of the apps command for ONOS. This command lists |
| 3392 | information about installed ONOS applications |
| 3393 | """ |
| 3394 | # Sample JSON object |
| 3395 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
| 3396 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3397 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
| 3398 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
| 3399 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3400 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3401 | if summary: |
| 3402 | cmdStr += " -s" |
| 3403 | if active: |
| 3404 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3405 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3406 | cmdStr += " -j" |
| 3407 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3408 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3409 | assert "Command not found:" not in output, output |
| 3410 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3411 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3412 | # FIXME: look at specific exceptions/Errors |
| 3413 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3414 | main.log.exception( "Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3415 | return None |
| 3416 | except TypeError: |
| 3417 | main.log.exception( self.name + ": Object not as expected" ) |
| 3418 | return None |
| 3419 | except pexpect.EOF: |
| 3420 | main.log.error( self.name + ": EOF exception found" ) |
| 3421 | main.log.error( self.name + ": " + self.handle.before ) |
| 3422 | main.cleanup() |
| 3423 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3424 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3425 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3426 | main.cleanup() |
| 3427 | main.exit() |
| 3428 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3429 | def appStatus( self, appName ): |
| 3430 | """ |
| 3431 | Uses the onos:apps cli command to return the status of an application. |
| 3432 | Returns: |
| 3433 | "ACTIVE" - If app is installed and activated |
| 3434 | "INSTALLED" - If app is installed and deactivated |
| 3435 | "UNINSTALLED" - If app is not installed |
| 3436 | None - on error |
| 3437 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3438 | try: |
| 3439 | if not isinstance( appName, types.StringType ): |
| 3440 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3441 | " a string" ) |
| 3442 | return None |
| 3443 | output = self.apps( jsonFormat=True ) |
| 3444 | appsJson = json.loads( output ) |
| 3445 | state = None |
| 3446 | for app in appsJson: |
| 3447 | if appName == app.get('name'): |
| 3448 | state = app.get('state') |
| 3449 | break |
| 3450 | if state == "ACTIVE" or state == "INSTALLED": |
| 3451 | return state |
| 3452 | elif state is None: |
| 3453 | return "UNINSTALLED" |
| 3454 | elif state: |
| 3455 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3456 | str( state ) ) |
| 3457 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3458 | except ( TypeError, ValueError ): |
| 3459 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3460 | return None |
| 3461 | except pexpect.EOF: |
| 3462 | main.log.error( self.name + ": EOF exception found" ) |
| 3463 | main.log.error( self.name + ": " + self.handle.before ) |
| 3464 | main.cleanup() |
| 3465 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3466 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3467 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3468 | main.cleanup() |
| 3469 | main.exit() |
| 3470 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3471 | def app( self, appName, option ): |
| 3472 | """ |
| 3473 | Interacts with the app command for ONOS. This command manages |
| 3474 | application inventory. |
| 3475 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3476 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3477 | # Validate argument types |
| 3478 | valid = True |
| 3479 | if not isinstance( appName, types.StringType ): |
| 3480 | main.log.error( self.name + ".app(): appName must be a " + |
| 3481 | "string" ) |
| 3482 | valid = False |
| 3483 | if not isinstance( option, types.StringType ): |
| 3484 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3485 | valid = False |
| 3486 | if not valid: |
| 3487 | return main.FALSE |
| 3488 | # Validate Option |
| 3489 | option = option.lower() |
| 3490 | # NOTE: Install may become a valid option |
| 3491 | if option == "activate": |
| 3492 | pass |
| 3493 | elif option == "deactivate": |
| 3494 | pass |
| 3495 | elif option == "uninstall": |
| 3496 | pass |
| 3497 | else: |
| 3498 | # Invalid option |
| 3499 | main.log.error( "The ONOS app command argument only takes " + |
| 3500 | "the values: (activate|deactivate|uninstall)" + |
| 3501 | "; was given '" + option + "'") |
| 3502 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3503 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3504 | output = self.sendline( cmdStr ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3505 | if "Error executing command" in output: |
| 3506 | main.log.error( "Error in processing onos:app command: " + |
| 3507 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3508 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3509 | elif "No such application" in output: |
| 3510 | main.log.error( "The application '" + appName + |
| 3511 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3512 | return main.FALSE |
| 3513 | elif "Command not found:" in output: |
| 3514 | main.log.error( "Error in processing onos:app command: " + |
| 3515 | str( output ) ) |
| 3516 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3517 | elif "Unsupported command:" in output: |
| 3518 | main.log.error( "Incorrect command given to 'app': " + |
| 3519 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3520 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3521 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3522 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3523 | return main.TRUE |
| 3524 | except TypeError: |
| 3525 | main.log.exception( self.name + ": Object not as expected" ) |
| 3526 | return main.ERROR |
| 3527 | except pexpect.EOF: |
| 3528 | main.log.error( self.name + ": EOF exception found" ) |
| 3529 | main.log.error( self.name + ": " + self.handle.before ) |
| 3530 | main.cleanup() |
| 3531 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3532 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3533 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3534 | main.cleanup() |
| 3535 | main.exit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3536 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3537 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3538 | """ |
| 3539 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3540 | appName is the hierarchical app name, not the feature name |
| 3541 | If check is True, method will check the status of the app after the |
| 3542 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3543 | Returns main.TRUE if the command was successfully sent |
| 3544 | main.FALSE if the cli responded with an error or given |
| 3545 | incorrect input |
| 3546 | """ |
| 3547 | try: |
| 3548 | if not isinstance( appName, types.StringType ): |
| 3549 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3550 | " a string" ) |
| 3551 | return main.FALSE |
| 3552 | status = self.appStatus( appName ) |
| 3553 | if status == "INSTALLED": |
| 3554 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3555 | if check and response == main.TRUE: |
| 3556 | for i in range(10): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3557 | status = self.appStatus( appName ) |
| 3558 | if status == "ACTIVE": |
| 3559 | return main.TRUE |
| 3560 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3561 | main.log.debug( "The state of application " + |
| 3562 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3563 | time.sleep( 1 ) |
| 3564 | return main.FALSE |
| 3565 | else: # not 'check' or command didn't succeed |
| 3566 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3567 | elif status == "ACTIVE": |
| 3568 | return main.TRUE |
| 3569 | elif status == "UNINSTALLED": |
| 3570 | main.log.error( self.name + ": Tried to activate the " + |
| 3571 | "application '" + appName + "' which is not " + |
| 3572 | "installed." ) |
| 3573 | else: |
| 3574 | main.log.error( "Unexpected return value from appStatus: " + |
| 3575 | str( status ) ) |
| 3576 | return main.ERROR |
| 3577 | except TypeError: |
| 3578 | main.log.exception( self.name + ": Object not as expected" ) |
| 3579 | return main.ERROR |
| 3580 | except pexpect.EOF: |
| 3581 | main.log.error( self.name + ": EOF exception found" ) |
| 3582 | main.log.error( self.name + ": " + self.handle.before ) |
| 3583 | main.cleanup() |
| 3584 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3585 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3586 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3587 | main.cleanup() |
| 3588 | main.exit() |
| 3589 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3590 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3591 | """ |
| 3592 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3593 | appName is the hierarchical app name, not the feature name |
| 3594 | If check is True, method will check the status of the app after the |
| 3595 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3596 | Returns main.TRUE if the command was successfully sent |
| 3597 | main.FALSE if the cli responded with an error or given |
| 3598 | incorrect input |
| 3599 | """ |
| 3600 | try: |
| 3601 | if not isinstance( appName, types.StringType ): |
| 3602 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3603 | "be a string" ) |
| 3604 | return main.FALSE |
| 3605 | status = self.appStatus( appName ) |
| 3606 | if status == "INSTALLED": |
| 3607 | return main.TRUE |
| 3608 | elif status == "ACTIVE": |
| 3609 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3610 | if check and response == main.TRUE: |
| 3611 | for i in range(10): # try 10 times then give up |
| 3612 | status = self.appStatus( appName ) |
| 3613 | if status == "INSTALLED": |
| 3614 | return main.TRUE |
| 3615 | else: |
| 3616 | time.sleep( 1 ) |
| 3617 | return main.FALSE |
| 3618 | else: # not check or command didn't succeed |
| 3619 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3620 | elif status == "UNINSTALLED": |
| 3621 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3622 | "application '" + appName + "' which is not " + |
| 3623 | "installed." ) |
| 3624 | return main.TRUE |
| 3625 | else: |
| 3626 | main.log.error( "Unexpected return value from appStatus: " + |
| 3627 | str( status ) ) |
| 3628 | return main.ERROR |
| 3629 | except TypeError: |
| 3630 | main.log.exception( self.name + ": Object not as expected" ) |
| 3631 | return main.ERROR |
| 3632 | except pexpect.EOF: |
| 3633 | main.log.error( self.name + ": EOF exception found" ) |
| 3634 | main.log.error( self.name + ": " + self.handle.before ) |
| 3635 | main.cleanup() |
| 3636 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3637 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3638 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3639 | main.cleanup() |
| 3640 | main.exit() |
| 3641 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3642 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3643 | """ |
| 3644 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3645 | appName is the hierarchical app name, not the feature name |
| 3646 | If check is True, method will check the status of the app after the |
| 3647 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3648 | Returns main.TRUE if the command was successfully sent |
| 3649 | main.FALSE if the cli responded with an error or given |
| 3650 | incorrect input |
| 3651 | """ |
| 3652 | # TODO: check with Thomas about the state machine for apps |
| 3653 | try: |
| 3654 | if not isinstance( appName, types.StringType ): |
| 3655 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3656 | "be a string" ) |
| 3657 | return main.FALSE |
| 3658 | status = self.appStatus( appName ) |
| 3659 | if status == "INSTALLED": |
| 3660 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3661 | if check and response == main.TRUE: |
| 3662 | for i in range(10): # try 10 times then give up |
| 3663 | status = self.appStatus( appName ) |
| 3664 | if status == "UNINSTALLED": |
| 3665 | return main.TRUE |
| 3666 | else: |
| 3667 | time.sleep( 1 ) |
| 3668 | return main.FALSE |
| 3669 | else: # not check or command didn't succeed |
| 3670 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3671 | elif status == "ACTIVE": |
| 3672 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3673 | "application '" + appName + "' which is " + |
| 3674 | "currently active." ) |
| 3675 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3676 | if check and response == main.TRUE: |
| 3677 | for i in range(10): # try 10 times then give up |
| 3678 | status = self.appStatus( appName ) |
| 3679 | if status == "UNINSTALLED": |
| 3680 | return main.TRUE |
| 3681 | else: |
| 3682 | time.sleep( 1 ) |
| 3683 | return main.FALSE |
| 3684 | else: # not check or command didn't succeed |
| 3685 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3686 | elif status == "UNINSTALLED": |
| 3687 | return main.TRUE |
| 3688 | else: |
| 3689 | main.log.error( "Unexpected return value from appStatus: " + |
| 3690 | str( status ) ) |
| 3691 | return main.ERROR |
| 3692 | except TypeError: |
| 3693 | main.log.exception( self.name + ": Object not as expected" ) |
| 3694 | return main.ERROR |
| 3695 | except pexpect.EOF: |
| 3696 | main.log.error( self.name + ": EOF exception found" ) |
| 3697 | main.log.error( self.name + ": " + self.handle.before ) |
| 3698 | main.cleanup() |
| 3699 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3700 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3701 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3702 | main.cleanup() |
| 3703 | main.exit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3704 | |
| 3705 | def appIDs( self, jsonFormat=True ): |
| 3706 | """ |
| 3707 | Show the mappings between app id and app names given by the 'app-ids' |
| 3708 | cli command |
| 3709 | """ |
| 3710 | try: |
| 3711 | cmdStr = "app-ids" |
| 3712 | if jsonFormat: |
| 3713 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3714 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3715 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3716 | assert "Command not found:" not in output, output |
| 3717 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3718 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3719 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3720 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3721 | return None |
| 3722 | except TypeError: |
| 3723 | main.log.exception( self.name + ": Object not as expected" ) |
| 3724 | return None |
| 3725 | except pexpect.EOF: |
| 3726 | main.log.error( self.name + ": EOF exception found" ) |
| 3727 | main.log.error( self.name + ": " + self.handle.before ) |
| 3728 | main.cleanup() |
| 3729 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3730 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3731 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3732 | main.cleanup() |
| 3733 | main.exit() |
| 3734 | |
| 3735 | def appToIDCheck( self ): |
| 3736 | """ |
| 3737 | This method will check that each application's ID listed in 'apps' is |
| 3738 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3739 | there are no duplicate IDs issued. Note that an app ID should be |
| 3740 | a globaly unique numerical identifier for app/app-like features. Once |
| 3741 | an ID is registered, the ID is never freed up so that if an app is |
| 3742 | reinstalled it will have the same ID. |
| 3743 | |
| 3744 | Returns: main.TRUE if the check passes and |
| 3745 | main.FALSE if the check fails or |
| 3746 | main.ERROR if there is some error in processing the test |
| 3747 | """ |
| 3748 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3749 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3750 | rawJson = self.appIDs( jsonFormat=True ) |
| 3751 | if rawJson: |
| 3752 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3753 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3754 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3755 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3756 | rawJson = self.apps( jsonFormat=True ) |
| 3757 | if rawJson: |
| 3758 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3759 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3760 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3761 | bail = True |
| 3762 | if bail: |
| 3763 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3764 | result = main.TRUE |
| 3765 | for app in apps: |
| 3766 | appID = app.get( 'id' ) |
| 3767 | if appID is None: |
| 3768 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3769 | result = main.FALSE |
| 3770 | appName = app.get( 'name' ) |
| 3771 | if appName is None: |
| 3772 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3773 | result = main.FALSE |
| 3774 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3775 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3776 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3777 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3778 | if not current: # if ids doesn't have this id |
| 3779 | result = main.FALSE |
| 3780 | main.log.error( "'app-ids' does not have the ID for " + |
| 3781 | str( appName ) + " that apps does." ) |
| 3782 | elif len( current ) > 1: |
| 3783 | # there is more than one app with this ID |
| 3784 | result = main.FALSE |
| 3785 | # We will log this later in the method |
| 3786 | elif not current[0][ 'name' ] == appName: |
| 3787 | currentName = current[0][ 'name' ] |
| 3788 | result = main.FALSE |
| 3789 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3790 | " registered under id:" + str( appID ) + |
| 3791 | " but 'apps' has " + str( appName ) ) |
| 3792 | else: |
| 3793 | pass # id and name match! |
| 3794 | # now make sure that app-ids has no duplicates |
| 3795 | idsList = [] |
| 3796 | namesList = [] |
| 3797 | for item in ids: |
| 3798 | idsList.append( item[ 'id' ] ) |
| 3799 | namesList.append( item[ 'name' ] ) |
| 3800 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3801 | len( namesList ) != len( set( namesList ) ): |
| 3802 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3803 | + json.dumps( ids, |
| 3804 | sort_keys=True, |
| 3805 | indent=4, |
| 3806 | separators=( ',', ': ' ) ) ) |
| 3807 | result = main.FALSE |
| 3808 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3809 | except ( TypeError, ValueError ): |
| 3810 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3811 | return main.ERROR |
| 3812 | except pexpect.EOF: |
| 3813 | main.log.error( self.name + ": EOF exception found" ) |
| 3814 | main.log.error( self.name + ": " + self.handle.before ) |
| 3815 | main.cleanup() |
| 3816 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3817 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3818 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3819 | main.cleanup() |
| 3820 | main.exit() |
| 3821 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3822 | def getCfg( self, component=None, propName=None, short=False, |
| 3823 | jsonFormat=True ): |
| 3824 | """ |
| 3825 | Get configuration settings from onos cli |
| 3826 | Optional arguments: |
| 3827 | component - Optionally only list configurations for a specific |
| 3828 | component. If None, all components with configurations |
| 3829 | are displayed. Case Sensitive string. |
| 3830 | propName - If component is specified, propName option will show |
| 3831 | only this specific configuration from that component. |
| 3832 | Case Sensitive string. |
| 3833 | jsonFormat - Returns output as json. Note that this will override |
| 3834 | the short option |
| 3835 | short - Short, less verbose, version of configurations. |
| 3836 | This is overridden by the json option |
| 3837 | returns: |
| 3838 | Output from cli as a string or None on error |
| 3839 | """ |
| 3840 | try: |
| 3841 | baseStr = "cfg" |
| 3842 | cmdStr = " get" |
| 3843 | componentStr = "" |
| 3844 | if component: |
| 3845 | componentStr += " " + component |
| 3846 | if propName: |
| 3847 | componentStr += " " + propName |
| 3848 | if jsonFormat: |
| 3849 | baseStr += " -j" |
| 3850 | elif short: |
| 3851 | baseStr += " -s" |
| 3852 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3853 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3854 | assert "Command not found:" not in output, output |
| 3855 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3856 | return output |
| 3857 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3858 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3859 | return None |
| 3860 | except TypeError: |
| 3861 | main.log.exception( self.name + ": Object not as expected" ) |
| 3862 | return None |
| 3863 | except pexpect.EOF: |
| 3864 | main.log.error( self.name + ": EOF exception found" ) |
| 3865 | main.log.error( self.name + ": " + self.handle.before ) |
| 3866 | main.cleanup() |
| 3867 | main.exit() |
| 3868 | except Exception: |
| 3869 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3870 | main.cleanup() |
| 3871 | main.exit() |
| 3872 | |
| 3873 | def setCfg( self, component, propName, value=None, check=True ): |
| 3874 | """ |
| 3875 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3876 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3877 | component - The case sensitive name of the component whose |
| 3878 | property is to be set |
| 3879 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3880 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3881 | value - The value to set the property to. If None, will unset the |
| 3882 | property and revert it to it's default value(if applicable) |
| 3883 | check - Boolean, Check whether the option was successfully set this |
| 3884 | only applies when a value is given. |
| 3885 | returns: |
| 3886 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3887 | will return main.TRUE unless there is an error |
| 3888 | """ |
| 3889 | try: |
| 3890 | baseStr = "cfg" |
| 3891 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3892 | if value is not None: |
| 3893 | cmdStr += " " + str( value ) |
| 3894 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3895 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3896 | assert "Command not found:" not in output, output |
| 3897 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3898 | if value and check: |
| 3899 | results = self.getCfg( component=str( component ), |
| 3900 | propName=str( propName ), |
| 3901 | jsonFormat=True ) |
| 3902 | # Check if current value is what we just set |
| 3903 | try: |
| 3904 | jsonOutput = json.loads( results ) |
| 3905 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3906 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3907 | main.log.exception( "Error parsing cfg output" ) |
| 3908 | main.log.error( "output:" + repr( results ) ) |
| 3909 | return main.FALSE |
| 3910 | if current == str( value ): |
| 3911 | return main.TRUE |
| 3912 | return main.FALSE |
| 3913 | return main.TRUE |
| 3914 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3915 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3916 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3917 | except ( TypeError, ValueError ): |
| 3918 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3919 | return main.FALSE |
| 3920 | except pexpect.EOF: |
| 3921 | main.log.error( self.name + ": EOF exception found" ) |
| 3922 | main.log.error( self.name + ": " + self.handle.before ) |
| 3923 | main.cleanup() |
| 3924 | main.exit() |
| 3925 | except Exception: |
| 3926 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3927 | main.cleanup() |
| 3928 | main.exit() |
| 3929 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3930 | def setTestAdd( self, setName, values ): |
| 3931 | """ |
| 3932 | CLI command to add elements to a distributed set. |
| 3933 | Arguments: |
| 3934 | setName - The name of the set to add to. |
| 3935 | values - The value(s) to add to the set, space seperated. |
| 3936 | Example usages: |
| 3937 | setTestAdd( "set1", "a b c" ) |
| 3938 | setTestAdd( "set2", "1" ) |
| 3939 | returns: |
| 3940 | main.TRUE on success OR |
| 3941 | main.FALSE if elements were already in the set OR |
| 3942 | main.ERROR on error |
| 3943 | """ |
| 3944 | try: |
| 3945 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
| 3946 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3947 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3948 | assert "Command not found:" not in output, output |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3949 | try: |
| 3950 | # TODO: Maybe make this less hardcoded |
| 3951 | # ConsistentMap Exceptions |
| 3952 | assert "org.onosproject.store.service" not in output |
| 3953 | # Node not leader |
| 3954 | assert "java.lang.IllegalStateException" not in output |
| 3955 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3956 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3957 | "command: " + str( output ) ) |
| 3958 | retryTime = 30 # Conservative time, given by Madan |
| 3959 | main.log.info( "Waiting " + str( retryTime ) + |
| 3960 | "seconds before retrying." ) |
| 3961 | time.sleep( retryTime ) # Due to change in mastership |
| 3962 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3963 | assert output is not None, "Error in sendline" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3964 | assert "Error executing command" not in output |
| 3965 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3966 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
| 3967 | main.log.info( self.name + ": " + output ) |
| 3968 | if re.search( positiveMatch, output): |
| 3969 | return main.TRUE |
| 3970 | elif re.search( negativeMatch, output): |
| 3971 | return main.FALSE |
| 3972 | else: |
| 3973 | main.log.error( self.name + ": setTestAdd did not" + |
| 3974 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3975 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 3976 | return main.ERROR |
| 3977 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3978 | main.log.exception( "Error in processing '" + cmdStr + "' command. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3979 | return main.ERROR |
| 3980 | except TypeError: |
| 3981 | main.log.exception( self.name + ": Object not as expected" ) |
| 3982 | return main.ERROR |
| 3983 | except pexpect.EOF: |
| 3984 | main.log.error( self.name + ": EOF exception found" ) |
| 3985 | main.log.error( self.name + ": " + self.handle.before ) |
| 3986 | main.cleanup() |
| 3987 | main.exit() |
| 3988 | except Exception: |
| 3989 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3990 | main.cleanup() |
| 3991 | main.exit() |
| 3992 | |
| 3993 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 3994 | """ |
| 3995 | CLI command to remove elements from a distributed set. |
| 3996 | Required arguments: |
| 3997 | setName - The name of the set to remove from. |
| 3998 | values - The value(s) to remove from the set, space seperated. |
| 3999 | Optional arguments: |
| 4000 | clear - Clear all elements from the set |
| 4001 | retain - Retain only the given values. (intersection of the |
| 4002 | original set and the given set) |
| 4003 | returns: |
| 4004 | main.TRUE on success OR |
| 4005 | main.FALSE if the set was not changed OR |
| 4006 | main.ERROR on error |
| 4007 | """ |
| 4008 | try: |
| 4009 | cmdStr = "set-test-remove " |
| 4010 | if clear: |
| 4011 | cmdStr += "-c " + str( setName ) |
| 4012 | elif retain: |
| 4013 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4014 | else: |
| 4015 | cmdStr += str( setName ) + " " + str( values ) |
| 4016 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4017 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4018 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4019 | # TODO: Maybe make this less hardcoded |
| 4020 | # ConsistentMap Exceptions |
| 4021 | assert "org.onosproject.store.service" not in output |
| 4022 | # Node not leader |
| 4023 | assert "java.lang.IllegalStateException" not in output |
| 4024 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4025 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4026 | "command: " + str( output ) ) |
| 4027 | retryTime = 30 # Conservative time, given by Madan |
| 4028 | main.log.info( "Waiting " + str( retryTime ) + |
| 4029 | "seconds before retrying." ) |
| 4030 | time.sleep( retryTime ) # Due to change in mastership |
| 4031 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4032 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4033 | assert "Command not found:" not in output, output |
| 4034 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4035 | main.log.info( self.name + ": " + output ) |
| 4036 | if clear: |
| 4037 | pattern = "Set " + str( setName ) + " cleared" |
| 4038 | if re.search( pattern, output ): |
| 4039 | return main.TRUE |
| 4040 | elif retain: |
| 4041 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4042 | "only elements of set \[(.*)\]" |
| 4043 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4044 | "retaining only elements of the set " +\ |
| 4045 | "\[(.*)\]" |
| 4046 | if re.search( positivePattern, output ): |
| 4047 | return main.TRUE |
| 4048 | elif re.search( negativePattern, output ): |
| 4049 | return main.FALSE |
| 4050 | else: |
| 4051 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4052 | str( setName ) |
| 4053 | if ( len( values.split() ) == 1 ): |
| 4054 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4055 | str( setName ) |
| 4056 | else: |
| 4057 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4058 | str( setName ) |
| 4059 | if re.search( positivePattern, output ): |
| 4060 | return main.TRUE |
| 4061 | elif re.search( negativePattern, output ): |
| 4062 | return main.FALSE |
| 4063 | main.log.error( self.name + ": setTestRemove did not" + |
| 4064 | " match expected output" ) |
| 4065 | main.log.debug( self.name + " expected: " + pattern ) |
| 4066 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4067 | return main.ERROR |
| 4068 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4069 | main.log.exception( "Error in processing '" + cmdStr + "' commandr. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4070 | return main.ERROR |
| 4071 | except TypeError: |
| 4072 | main.log.exception( self.name + ": Object not as expected" ) |
| 4073 | return main.ERROR |
| 4074 | except pexpect.EOF: |
| 4075 | main.log.error( self.name + ": EOF exception found" ) |
| 4076 | main.log.error( self.name + ": " + self.handle.before ) |
| 4077 | main.cleanup() |
| 4078 | main.exit() |
| 4079 | except Exception: |
| 4080 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4081 | main.cleanup() |
| 4082 | main.exit() |
| 4083 | |
| 4084 | def setTestGet( self, setName, values="" ): |
| 4085 | """ |
| 4086 | CLI command to get the elements in a distributed set. |
| 4087 | Required arguments: |
| 4088 | setName - The name of the set to remove from. |
| 4089 | Optional arguments: |
| 4090 | values - The value(s) to check if in the set, space seperated. |
| 4091 | returns: |
| 4092 | main.ERROR on error OR |
| 4093 | A list of elements in the set if no optional arguments are |
| 4094 | supplied OR |
| 4095 | A tuple containing the list then: |
| 4096 | main.FALSE if the given values are not in the set OR |
| 4097 | main.TRUE if the given values are in the set OR |
| 4098 | """ |
| 4099 | try: |
| 4100 | values = str( values ).strip() |
| 4101 | setName = str( setName ).strip() |
| 4102 | length = len( values.split() ) |
| 4103 | containsCheck = None |
| 4104 | # Patterns to match |
| 4105 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4106 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4107 | containsTrue = "Set " + setName + " contains the value " + values |
| 4108 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4109 | values |
| 4110 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4111 | setPattern |
| 4112 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4113 | " subset " + setPattern |
| 4114 | |
| 4115 | cmdStr = "set-test-get " |
| 4116 | cmdStr += setName + " " + values |
| 4117 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4118 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4119 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4120 | # TODO: Maybe make this less hardcoded |
| 4121 | # ConsistentMap Exceptions |
| 4122 | assert "org.onosproject.store.service" not in output |
| 4123 | # Node not leader |
| 4124 | assert "java.lang.IllegalStateException" not in output |
| 4125 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4126 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4127 | "command: " + str( output ) ) |
| 4128 | retryTime = 30 # Conservative time, given by Madan |
| 4129 | main.log.info( "Waiting " + str( retryTime ) + |
| 4130 | "seconds before retrying." ) |
| 4131 | time.sleep( retryTime ) # Due to change in mastership |
| 4132 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4133 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4134 | assert "Command not found:" not in output, output |
| 4135 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4136 | main.log.info( self.name + ": " + output ) |
| 4137 | |
| 4138 | if length == 0: |
| 4139 | match = re.search( pattern, output ) |
| 4140 | else: # if given values |
| 4141 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4142 | patternTrue = pattern + "\r\n" + containsTrue |
| 4143 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4144 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4145 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4146 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4147 | matchTrue = re.search( patternTrue, output ) |
| 4148 | matchFalse = re.search( patternFalse, output ) |
| 4149 | if matchTrue: |
| 4150 | containsCheck = main.TRUE |
| 4151 | match = matchTrue |
| 4152 | elif matchFalse: |
| 4153 | containsCheck = main.FALSE |
| 4154 | match = matchFalse |
| 4155 | else: |
| 4156 | main.log.error( self.name + " setTestGet did not match " +\ |
| 4157 | "expected output" ) |
| 4158 | main.log.debug( self.name + " expected: " + pattern ) |
| 4159 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4160 | match = None |
| 4161 | if match: |
| 4162 | setMatch = match.group( 1 ) |
| 4163 | if setMatch == '': |
| 4164 | setList = [] |
| 4165 | else: |
| 4166 | setList = setMatch.split( ", " ) |
| 4167 | if length > 0: |
| 4168 | return ( setList, containsCheck ) |
| 4169 | else: |
| 4170 | return setList |
| 4171 | else: # no match |
| 4172 | main.log.error( self.name + ": setTestGet did not" + |
| 4173 | " match expected output" ) |
| 4174 | main.log.debug( self.name + " expected: " + pattern ) |
| 4175 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4176 | return main.ERROR |
| 4177 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4178 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4179 | return main.ERROR |
| 4180 | except TypeError: |
| 4181 | main.log.exception( self.name + ": Object not as expected" ) |
| 4182 | return main.ERROR |
| 4183 | except pexpect.EOF: |
| 4184 | main.log.error( self.name + ": EOF exception found" ) |
| 4185 | main.log.error( self.name + ": " + self.handle.before ) |
| 4186 | main.cleanup() |
| 4187 | main.exit() |
| 4188 | except Exception: |
| 4189 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4190 | main.cleanup() |
| 4191 | main.exit() |
| 4192 | |
| 4193 | def setTestSize( self, setName ): |
| 4194 | """ |
| 4195 | CLI command to get the elements in a distributed set. |
| 4196 | Required arguments: |
| 4197 | setName - The name of the set to remove from. |
| 4198 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4199 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4200 | None on error |
| 4201 | """ |
| 4202 | try: |
| 4203 | # TODO: Should this check against the number of elements returned |
| 4204 | # and then return true/false based on that? |
| 4205 | setName = str( setName ).strip() |
| 4206 | # Patterns to match |
| 4207 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4208 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4209 | setPattern |
| 4210 | cmdStr = "set-test-get -s " |
| 4211 | cmdStr += setName |
| 4212 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4213 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4214 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4215 | # TODO: Maybe make this less hardcoded |
| 4216 | # ConsistentMap Exceptions |
| 4217 | assert "org.onosproject.store.service" not in output |
| 4218 | # Node not leader |
| 4219 | assert "java.lang.IllegalStateException" not in output |
| 4220 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4221 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4222 | "command: " + str( output ) ) |
| 4223 | retryTime = 30 # Conservative time, given by Madan |
| 4224 | main.log.info( "Waiting " + str( retryTime ) + |
| 4225 | "seconds before retrying." ) |
| 4226 | time.sleep( retryTime ) # Due to change in mastership |
| 4227 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4228 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4229 | assert "Command not found:" not in output, output |
| 4230 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4231 | main.log.info( self.name + ": " + output ) |
| 4232 | match = re.search( pattern, output ) |
| 4233 | if match: |
| 4234 | setSize = int( match.group( 1 ) ) |
| 4235 | setMatch = match.group( 2 ) |
| 4236 | if len( setMatch.split() ) == setSize: |
| 4237 | main.log.info( "The size returned by " + self.name + |
| 4238 | " matches the number of elements in " + |
| 4239 | "the returned set" ) |
| 4240 | else: |
| 4241 | main.log.error( "The size returned by " + self.name + |
| 4242 | " does not match the number of " + |
| 4243 | "elements in the returned set." ) |
| 4244 | return setSize |
| 4245 | else: # no match |
| 4246 | main.log.error( self.name + ": setTestGet did not" + |
| 4247 | " match expected output" ) |
| 4248 | main.log.debug( self.name + " expected: " + pattern ) |
| 4249 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4250 | return None |
| 4251 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4252 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 4253 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4254 | except TypeError: |
| 4255 | main.log.exception( self.name + ": Object not as expected" ) |
| 4256 | return None |
| 4257 | except pexpect.EOF: |
| 4258 | main.log.error( self.name + ": EOF exception found" ) |
| 4259 | main.log.error( self.name + ": " + self.handle.before ) |
| 4260 | main.cleanup() |
| 4261 | main.exit() |
| 4262 | except Exception: |
| 4263 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4264 | main.cleanup() |
| 4265 | main.exit() |
| 4266 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4267 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4268 | """ |
| 4269 | Command to list the various counters in the system. |
| 4270 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4271 | if jsonFormat, a string of the json object returned by the cli |
| 4272 | command |
| 4273 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4274 | None on error |
| 4275 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4276 | try: |
| 4277 | counters = {} |
| 4278 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4279 | if jsonFormat: |
| 4280 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4281 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4282 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4283 | assert "Command not found:" not in output, output |
| 4284 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4285 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4286 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4287 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4288 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4289 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4290 | except TypeError: |
| 4291 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4292 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4293 | except pexpect.EOF: |
| 4294 | main.log.error( self.name + ": EOF exception found" ) |
| 4295 | main.log.error( self.name + ": " + self.handle.before ) |
| 4296 | main.cleanup() |
| 4297 | main.exit() |
| 4298 | except Exception: |
| 4299 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4300 | main.cleanup() |
| 4301 | main.exit() |
| 4302 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4303 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4304 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4305 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4306 | Required arguments: |
| 4307 | counter - The name of the counter to increment. |
| 4308 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4309 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4310 | returns: |
| 4311 | integer value of the counter or |
| 4312 | None on Error |
| 4313 | """ |
| 4314 | try: |
| 4315 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4316 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4317 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4318 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4319 | if delta != 1: |
| 4320 | cmdStr += " " + str( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4321 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4322 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4323 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4324 | # TODO: Maybe make this less hardcoded |
| 4325 | # ConsistentMap Exceptions |
| 4326 | assert "org.onosproject.store.service" not in output |
| 4327 | # Node not leader |
| 4328 | assert "java.lang.IllegalStateException" not in output |
| 4329 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4330 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4331 | "command: " + str( output ) ) |
| 4332 | retryTime = 30 # Conservative time, given by Madan |
| 4333 | main.log.info( "Waiting " + str( retryTime ) + |
| 4334 | "seconds before retrying." ) |
| 4335 | time.sleep( retryTime ) # Due to change in mastership |
| 4336 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4337 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4338 | assert "Command not found:" not in output, output |
| 4339 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4340 | main.log.info( self.name + ": " + output ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4341 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4342 | match = re.search( pattern, output ) |
| 4343 | if match: |
| 4344 | return int( match.group( 1 ) ) |
| 4345 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4346 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4347 | " match expected output." ) |
| 4348 | main.log.debug( self.name + " expected: " + pattern ) |
| 4349 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4350 | return None |
| 4351 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4352 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4353 | return None |
| 4354 | except TypeError: |
| 4355 | main.log.exception( self.name + ": Object not as expected" ) |
| 4356 | return None |
| 4357 | except pexpect.EOF: |
| 4358 | main.log.error( self.name + ": EOF exception found" ) |
| 4359 | main.log.error( self.name + ": " + self.handle.before ) |
| 4360 | main.cleanup() |
| 4361 | main.exit() |
| 4362 | except Exception: |
| 4363 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4364 | main.cleanup() |
| 4365 | main.exit() |
| 4366 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4367 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4368 | """ |
| 4369 | CLI command to get a distributed counter then add a delta to it. |
| 4370 | Required arguments: |
| 4371 | counter - The name of the counter to increment. |
| 4372 | Optional arguments: |
| 4373 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4374 | returns: |
| 4375 | integer value of the counter or |
| 4376 | None on Error |
| 4377 | """ |
| 4378 | try: |
| 4379 | counter = str( counter ) |
| 4380 | delta = int( delta ) |
| 4381 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4382 | cmdStr += counter |
| 4383 | if delta != 1: |
| 4384 | cmdStr += " " + str( delta ) |
| 4385 | output = self.sendline( cmdStr ) |
| 4386 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4387 | assert output is not None, "Error in sendline" |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4388 | # TODO: Maybe make this less hardcoded |
| 4389 | # ConsistentMap Exceptions |
| 4390 | assert "org.onosproject.store.service" not in output |
| 4391 | # Node not leader |
| 4392 | assert "java.lang.IllegalStateException" not in output |
| 4393 | except AssertionError: |
| 4394 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4395 | "command: " + str( output ) ) |
| 4396 | retryTime = 30 # Conservative time, given by Madan |
| 4397 | main.log.info( "Waiting " + str( retryTime ) + |
| 4398 | "seconds before retrying." ) |
| 4399 | time.sleep( retryTime ) # Due to change in mastership |
| 4400 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4401 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4402 | assert "Command not found:" not in output, output |
| 4403 | assert "Error executing command" not in output, output |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4404 | main.log.info( self.name + ": " + output ) |
| 4405 | pattern = counter + " was updated to (-?\d+)" |
| 4406 | match = re.search( pattern, output ) |
| 4407 | if match: |
| 4408 | return int( match.group( 1 ) ) |
| 4409 | else: |
| 4410 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4411 | " match expected output." ) |
| 4412 | main.log.debug( self.name + " expected: " + pattern ) |
| 4413 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4414 | return None |
| 4415 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4416 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4417 | return None |
| 4418 | except TypeError: |
| 4419 | main.log.exception( self.name + ": Object not as expected" ) |
| 4420 | return None |
| 4421 | except pexpect.EOF: |
| 4422 | main.log.error( self.name + ": EOF exception found" ) |
| 4423 | main.log.error( self.name + ": " + self.handle.before ) |
| 4424 | main.cleanup() |
| 4425 | main.exit() |
| 4426 | except Exception: |
| 4427 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4428 | main.cleanup() |
| 4429 | main.exit() |
| 4430 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4431 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4432 | """ |
| 4433 | Description: Execute summary command in onos |
| 4434 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4435 | no output |
| 4436 | |
| 4437 | """ |
| 4438 | try: |
| 4439 | cmdStr = "summary" |
| 4440 | if jsonFormat: |
| 4441 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4442 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4443 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4444 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4445 | assert "Error:" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4446 | if not handle: |
| 4447 | main.log.error( self.name + ": There is no output in " + |
| 4448 | "summary command" ) |
| 4449 | return main.FALSE |
| 4450 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4451 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4452 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4453 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4454 | except TypeError: |
| 4455 | main.log.exception( self.name + ": Object not as expected" ) |
| 4456 | return None |
| 4457 | except pexpect.EOF: |
| 4458 | main.log.error( self.name + ": EOF exception found" ) |
| 4459 | main.log.error( self.name + ": " + self.handle.before ) |
| 4460 | main.cleanup() |
| 4461 | main.exit() |
| 4462 | except Exception: |
| 4463 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4464 | main.cleanup() |
| 4465 | main.exit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4466 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4467 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4468 | """ |
| 4469 | CLI command to get the value of a key in a consistent map using |
| 4470 | transactions. This a test function and can only get keys from the |
| 4471 | test map hard coded into the cli command |
| 4472 | Required arguments: |
| 4473 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4474 | returns: |
| 4475 | The string value of the key or |
| 4476 | None on Error |
| 4477 | """ |
| 4478 | try: |
| 4479 | keyName = str( keyName ) |
| 4480 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4481 | cmdStr += keyName |
| 4482 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4483 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4484 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4485 | try: |
| 4486 | # TODO: Maybe make this less hardcoded |
| 4487 | # ConsistentMap Exceptions |
| 4488 | assert "org.onosproject.store.service" not in output |
| 4489 | # Node not leader |
| 4490 | assert "java.lang.IllegalStateException" not in output |
| 4491 | except AssertionError: |
| 4492 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4493 | "command: " + str( output ) ) |
| 4494 | return None |
| 4495 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4496 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4497 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4498 | return None |
| 4499 | else: |
| 4500 | match = re.search( pattern, output ) |
| 4501 | if match: |
| 4502 | return match.groupdict()[ 'value' ] |
| 4503 | else: |
| 4504 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4505 | " match expected output." ) |
| 4506 | main.log.debug( self.name + " expected: " + pattern ) |
| 4507 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4508 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4509 | except AssertionError: |
| 4510 | main.log.exception( "" ) |
| 4511 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4512 | except TypeError: |
| 4513 | main.log.exception( self.name + ": Object not as expected" ) |
| 4514 | return None |
| 4515 | except pexpect.EOF: |
| 4516 | main.log.error( self.name + ": EOF exception found" ) |
| 4517 | main.log.error( self.name + ": " + self.handle.before ) |
| 4518 | main.cleanup() |
| 4519 | main.exit() |
| 4520 | except Exception: |
| 4521 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4522 | main.cleanup() |
| 4523 | main.exit() |
| 4524 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4525 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4526 | """ |
| 4527 | CLI command to put a value into 'numKeys' number of keys in a |
| 4528 | consistent map using transactions. This a test function and can only |
| 4529 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4530 | Required arguments: |
| 4531 | numKeys - Number of keys to add the value to |
| 4532 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4533 | returns: |
| 4534 | A dictionary whose keys are the name of the keys put into the map |
| 4535 | and the values of the keys are dictionaries whose key-values are |
| 4536 | 'value': value put into map and optionaly |
| 4537 | 'oldValue': Previous value in the key or |
| 4538 | None on Error |
| 4539 | |
| 4540 | Example output |
| 4541 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4542 | 'Key2': {'value': 'Testing'} } |
| 4543 | """ |
| 4544 | try: |
| 4545 | numKeys = str( numKeys ) |
| 4546 | value = str( value ) |
| 4547 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4548 | cmdStr += numKeys + " " + value |
| 4549 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4550 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4551 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4552 | try: |
| 4553 | # TODO: Maybe make this less hardcoded |
| 4554 | # ConsistentMap Exceptions |
| 4555 | assert "org.onosproject.store.service" not in output |
| 4556 | # Node not leader |
| 4557 | assert "java.lang.IllegalStateException" not in output |
| 4558 | except AssertionError: |
| 4559 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4560 | "command: " + str( output ) ) |
| 4561 | return None |
| 4562 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4563 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4564 | results = {} |
| 4565 | for line in output.splitlines(): |
| 4566 | new = re.search( newPattern, line ) |
| 4567 | updated = re.search( updatedPattern, line ) |
| 4568 | if new: |
| 4569 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4570 | elif updated: |
| 4571 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4572 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4573 | else: |
| 4574 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4575 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4576 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4577 | newPattern, |
| 4578 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4579 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4580 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4581 | except AssertionError: |
| 4582 | main.log.exception( "" ) |
| 4583 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4584 | except TypeError: |
| 4585 | main.log.exception( self.name + ": Object not as expected" ) |
| 4586 | return None |
| 4587 | except pexpect.EOF: |
| 4588 | main.log.error( self.name + ": EOF exception found" ) |
| 4589 | main.log.error( self.name + ": " + self.handle.before ) |
| 4590 | main.cleanup() |
| 4591 | main.exit() |
| 4592 | except Exception: |
| 4593 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4594 | main.cleanup() |
| 4595 | main.exit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4596 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4597 | def maps( self, jsonFormat=True ): |
| 4598 | """ |
| 4599 | Description: Returns result of onos:maps |
| 4600 | Optional: |
| 4601 | * jsonFormat: enable json formatting of output |
| 4602 | """ |
| 4603 | try: |
| 4604 | cmdStr = "maps" |
| 4605 | if jsonFormat: |
| 4606 | cmdStr += " -j" |
| 4607 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4608 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4609 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4610 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4611 | except AssertionError: |
| 4612 | main.log.exception( "" ) |
| 4613 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4614 | except TypeError: |
| 4615 | main.log.exception( self.name + ": Object not as expected" ) |
| 4616 | return None |
| 4617 | except pexpect.EOF: |
| 4618 | main.log.error( self.name + ": EOF exception found" ) |
| 4619 | main.log.error( self.name + ": " + self.handle.before ) |
| 4620 | main.cleanup() |
| 4621 | main.exit() |
| 4622 | except Exception: |
| 4623 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4624 | main.cleanup() |
| 4625 | main.exit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4626 | |
| 4627 | def getSwController( self, uri, jsonFormat=True ): |
| 4628 | """ |
| 4629 | Descrition: Gets the controller information from the device |
| 4630 | """ |
| 4631 | try: |
| 4632 | cmd = "device-controllers " |
| 4633 | if jsonFormat: |
| 4634 | cmd += "-j " |
| 4635 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4636 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4637 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4638 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4639 | except AssertionError: |
| 4640 | main.log.exception( "" ) |
| 4641 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4642 | except TypeError: |
| 4643 | main.log.exception( self.name + ": Object not as expected" ) |
| 4644 | return None |
| 4645 | except pexpect.EOF: |
| 4646 | main.log.error( self.name + ": EOF exception found" ) |
| 4647 | main.log.error( self.name + ": " + self.handle.before ) |
| 4648 | main.cleanup() |
| 4649 | main.exit() |
| 4650 | except Exception: |
| 4651 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4652 | main.cleanup() |
| 4653 | main.exit() |
| 4654 | |
| 4655 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4656 | """ |
| 4657 | Descrition: sets the controller(s) for the specified device |
| 4658 | |
| 4659 | Parameters: |
| 4660 | Required: uri - String: The uri of the device(switch). |
| 4661 | ip - String or List: The ip address of the controller. |
| 4662 | This parameter can be formed in a couple of different ways. |
| 4663 | VALID: |
| 4664 | 10.0.0.1 - just the ip address |
| 4665 | tcp:10.0.0.1 - the protocol and the ip address |
| 4666 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4667 | so that you can add controllers with different |
| 4668 | protocols and ports |
| 4669 | INVALID: |
| 4670 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4671 | |
| 4672 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4673 | port - The port number. |
| 4674 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4675 | |
| 4676 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4677 | """ |
| 4678 | try: |
| 4679 | cmd = "device-setcontrollers" |
| 4680 | |
| 4681 | if jsonFormat: |
| 4682 | cmd += " -j" |
| 4683 | cmd += " " + uri |
| 4684 | if isinstance( ip, str ): |
| 4685 | ip = [ip] |
| 4686 | for item in ip: |
| 4687 | if ":" in item: |
| 4688 | sitem = item.split( ":" ) |
| 4689 | if len(sitem) == 3: |
| 4690 | cmd += " " + item |
| 4691 | elif "." in sitem[1]: |
| 4692 | cmd += " {}:{}".format(item, port) |
| 4693 | else: |
| 4694 | main.log.error( "Malformed entry: " + item ) |
| 4695 | raise TypeError |
| 4696 | else: |
| 4697 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4698 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4699 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4700 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4701 | if "Error" in response: |
| 4702 | main.log.error( response ) |
| 4703 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4704 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4705 | except AssertionError: |
| 4706 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4707 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4708 | except TypeError: |
| 4709 | main.log.exception( self.name + ": Object not as expected" ) |
| 4710 | return main.FALSE |
| 4711 | except pexpect.EOF: |
| 4712 | main.log.error( self.name + ": EOF exception found" ) |
| 4713 | main.log.error( self.name + ": " + self.handle.before ) |
| 4714 | main.cleanup() |
| 4715 | main.exit() |
| 4716 | except Exception: |
| 4717 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4718 | main.cleanup() |
| 4719 | main.exit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4720 | |
| 4721 | def removeDevice( self, device ): |
| 4722 | ''' |
| 4723 | Description: |
| 4724 | Remove a device from ONOS by passing the uri of the device(s). |
| 4725 | Parameters: |
| 4726 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
| 4727 | Returns: |
| 4728 | Returns main.FALSE if an exception is thrown or an error is present |
| 4729 | in the response. Otherwise, returns main.TRUE. |
| 4730 | NOTE: |
| 4731 | If a host cannot be removed, then this function will return main.FALSE |
| 4732 | ''' |
| 4733 | try: |
| 4734 | if type( device ) is str: |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 4735 | deviceStr = device |
| 4736 | device = [] |
| 4737 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4738 | |
| 4739 | for d in device: |
| 4740 | time.sleep( 1 ) |
| 4741 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4742 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4743 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4744 | if "Error" in response: |
| 4745 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4746 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4747 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4748 | except AssertionError: |
| 4749 | main.log.exception( "" ) |
| 4750 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4751 | except TypeError: |
| 4752 | main.log.exception( self.name + ": Object not as expected" ) |
| 4753 | return main.FALSE |
| 4754 | except pexpect.EOF: |
| 4755 | main.log.error( self.name + ": EOF exception found" ) |
| 4756 | main.log.error( self.name + ": " + self.handle.before ) |
| 4757 | main.cleanup() |
| 4758 | main.exit() |
| 4759 | except Exception: |
| 4760 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4761 | main.cleanup() |
| 4762 | main.exit() |
| 4763 | |
| 4764 | def removeHost( self, host ): |
| 4765 | ''' |
| 4766 | Description: |
| 4767 | Remove a host from ONOS by passing the id of the host(s) |
| 4768 | Parameters: |
| 4769 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
| 4770 | Returns: |
| 4771 | Returns main.FALSE if an exception is thrown or an error is present |
| 4772 | in the response. Otherwise, returns main.TRUE. |
| 4773 | NOTE: |
| 4774 | If a host cannot be removed, then this function will return main.FALSE |
| 4775 | ''' |
| 4776 | try: |
| 4777 | if type( host ) is str: |
| 4778 | host = list( host ) |
| 4779 | |
| 4780 | for h in host: |
| 4781 | time.sleep( 1 ) |
| 4782 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4783 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4784 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4785 | if "Error" in response: |
| 4786 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 4787 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4788 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4789 | except AssertionError: |
| 4790 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4791 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4792 | except TypeError: |
| 4793 | main.log.exception( self.name + ": Object not as expected" ) |
| 4794 | return main.FALSE |
| 4795 | except pexpect.EOF: |
| 4796 | main.log.error( self.name + ": EOF exception found" ) |
| 4797 | main.log.error( self.name + ": " + self.handle.before ) |
| 4798 | main.cleanup() |
| 4799 | main.exit() |
| 4800 | except Exception: |
| 4801 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4802 | main.cleanup() |
| 4803 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4804 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4805 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4806 | ''' |
| 4807 | Description: |
| 4808 | Bring link down or up in the null-provider. |
| 4809 | params: |
| 4810 | begin - (string) One end of a device or switch. |
| 4811 | end - (string) the other end of the device or switch |
| 4812 | returns: |
| 4813 | main.TRUE if no exceptions were thrown and no Errors are |
| 4814 | present in the resoponse. Otherwise, returns main.FALSE |
| 4815 | ''' |
| 4816 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4817 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4818 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4819 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4820 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4821 | if "Error" in response or "Failure" in response: |
| 4822 | main.log.error( response ) |
| 4823 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4824 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4825 | except AssertionError: |
| 4826 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4827 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4828 | except TypeError: |
| 4829 | main.log.exception( self.name + ": Object not as expected" ) |
| 4830 | return main.FALSE |
| 4831 | except pexpect.EOF: |
| 4832 | main.log.error( self.name + ": EOF exception found" ) |
| 4833 | main.log.error( self.name + ": " + self.handle.before ) |
| 4834 | main.cleanup() |
| 4835 | main.exit() |
| 4836 | except Exception: |
| 4837 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4838 | main.cleanup() |
| 4839 | main.exit() |
| 4840 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4841 | def portstate( self, dpid, port, state ): |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4842 | ''' |
| 4843 | Description: |
| 4844 | Changes the state of port in an OF switch by means of the |
| 4845 | PORTSTATUS OF messages. |
| 4846 | params: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4847 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 4848 | port - (string) target port in the device. Ex: '2' |
| 4849 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4850 | returns: |
| 4851 | main.TRUE if no exceptions were thrown and no Errors are |
| 4852 | present in the resoponse. Otherwise, returns main.FALSE |
| 4853 | ''' |
| 4854 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4855 | state = state.lower() |
| 4856 | assert state == 'enable' or state == 'disable', "Unknown state" |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4857 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
| 4858 | response = self.sendline( cmd, showResponse=True ) |
| 4859 | assert response is not None, "Error in sendline" |
| 4860 | assert "Command not found:" not in response, response |
| 4861 | if "Error" in response or "Failure" in response: |
| 4862 | main.log.error( response ) |
| 4863 | return main.FALSE |
| 4864 | return main.TRUE |
| 4865 | except AssertionError: |
| 4866 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4867 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4868 | except TypeError: |
| 4869 | main.log.exception( self.name + ": Object not as expected" ) |
| 4870 | return main.FALSE |
| 4871 | except pexpect.EOF: |
| 4872 | main.log.error( self.name + ": EOF exception found" ) |
| 4873 | main.log.error( self.name + ": " + self.handle.before ) |
| 4874 | main.cleanup() |
| 4875 | main.exit() |
| 4876 | except Exception: |
| 4877 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4878 | main.cleanup() |
| 4879 | main.exit() |
| 4880 | |
| 4881 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 4882 | """ |
| 4883 | Set the logging level to lvl for a specific app |
| 4884 | returns main.TRUE on success |
| 4885 | returns main.FALSE if Error occurred |
| 4886 | if noExit is True, TestON will not exit, but clean up |
| 4887 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 4888 | Level defaults to INFO |
| 4889 | """ |
| 4890 | try: |
| 4891 | self.handle.sendline( "log:set %s %s" %( level, app ) ) |
| 4892 | self.handle.expect( "onos>" ) |
| 4893 | |
| 4894 | response = self.handle.before |
| 4895 | if re.search( "Error", response ): |
| 4896 | return main.FALSE |
| 4897 | return main.TRUE |
| 4898 | except pexpect.TIMEOUT: |
| 4899 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 4900 | main.cleanup() |
| 4901 | main.exit() |
| 4902 | except pexpect.EOF: |
| 4903 | main.log.error( self.name + ": EOF exception found" ) |
| 4904 | main.log.error( self.name + ": " + self.handle.before ) |
| 4905 | main.cleanup() |
| 4906 | main.exit() |
| 4907 | except Exception: |
| 4908 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4909 | main.cleanup() |
| 4910 | main.exit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4911 | |
| 4912 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 4913 | """ |
| 4914 | Return a dictionary which describes the latest network topology data as a |
| 4915 | graph. |
| 4916 | An example of the dictionary: |
| 4917 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 4918 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 4919 | Each vertex should at least have an 'edges' attribute which describes the |
| 4920 | adjacency information. The value of 'edges' attribute is also represented by |
| 4921 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 4922 | list of attributes. |
| 4923 | An example of the edges dictionary: |
| 4924 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 4925 | vertex3: { 'port': ..., 'weight': ... } } |
| 4926 | If includeHost == True, all hosts (and host-switch links) will be included |
| 4927 | in topology data. |
| 4928 | """ |
| 4929 | graphDict = {} |
| 4930 | try: |
| 4931 | links = self.links() |
| 4932 | links = json.loads( links ) |
| 4933 | devices = self.devices() |
| 4934 | devices = json.loads( devices ) |
| 4935 | idToDevice = {} |
| 4936 | for device in devices: |
| 4937 | idToDevice[ device[ 'id' ] ] = device |
| 4938 | if includeHost: |
| 4939 | hosts = self.hosts() |
| 4940 | # FIXME: support 'includeHost' argument |
| 4941 | for link in links: |
| 4942 | nodeA = link[ 'src' ][ 'device' ] |
| 4943 | nodeB = link[ 'dst' ][ 'device' ] |
| 4944 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
| 4945 | if not nodeA in graphDict.keys(): |
| 4946 | graphDict[ nodeA ] = { 'edges':{}, |
| 4947 | 'dpid':idToDevice[ nodeA ][ 'id' ][3:], |
| 4948 | 'type':idToDevice[ nodeA ][ 'type' ], |
| 4949 | 'available':idToDevice[ nodeA ][ 'available' ], |
| 4950 | 'role':idToDevice[ nodeA ][ 'role' ], |
| 4951 | 'mfr':idToDevice[ nodeA ][ 'mfr' ], |
| 4952 | 'hw':idToDevice[ nodeA ][ 'hw' ], |
| 4953 | 'sw':idToDevice[ nodeA ][ 'sw' ], |
| 4954 | 'serial':idToDevice[ nodeA ][ 'serial' ], |
| 4955 | 'chassisId':idToDevice[ nodeA ][ 'chassisId' ], |
| 4956 | 'annotations':idToDevice[ nodeA ][ 'annotations' ]} |
| 4957 | else: |
| 4958 | # Assert nodeB is not connected to any current links of nodeA |
| 4959 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 4960 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port':link[ 'src' ][ 'port' ], |
| 4961 | 'type':link[ 'type' ], |
| 4962 | 'state':link[ 'state' ] } |
| 4963 | return graphDict |
| 4964 | except ( TypeError, ValueError ): |
| 4965 | main.log.exception( self.name + ": Object not as expected" ) |
| 4966 | return None |
| 4967 | except KeyError: |
| 4968 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4969 | return None |
| 4970 | except AssertionError: |
| 4971 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4972 | return None |
| 4973 | except pexpect.EOF: |
| 4974 | main.log.error( self.name + ": EOF exception found" ) |
| 4975 | main.log.error( self.name + ": " + self.handle.before ) |
| 4976 | return None |
| 4977 | except Exception: |
| 4978 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4979 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 4980 | |
| 4981 | def getIntentPerfSummary( self ): |
| 4982 | ''' |
| 4983 | Send command to check intent-perf summary |
| 4984 | Returns: dictionary for intent-perf summary |
| 4985 | if something wrong, function will return None |
| 4986 | ''' |
| 4987 | cmd = "intent-perf -s" |
| 4988 | respDic = {} |
| 4989 | resp = self.sendline( cmd ) |
| 4990 | try: |
| 4991 | # Generate the dictionary to return |
| 4992 | for l in resp.split( "\n" ): |
| 4993 | # Delete any white space in line |
| 4994 | temp = re.sub( r'\s+', '', l ) |
| 4995 | temp = temp.split( ":" ) |
| 4996 | respDic[ temp[0] ] = temp[ 1 ] |
| 4997 | |
| 4998 | except (TypeError, ValueError): |
| 4999 | main.log.exception( self.name + ": Object not as expected" ) |
| 5000 | return None |
| 5001 | except KeyError: |
| 5002 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5003 | return None |
| 5004 | except AssertionError: |
| 5005 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5006 | return None |
| 5007 | except pexpect.EOF: |
| 5008 | main.log.error( self.name + ": EOF exception found" ) |
| 5009 | main.log.error( self.name + ": " + self.handle.before ) |
| 5010 | return None |
| 5011 | except Exception: |
| 5012 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5013 | return None |
| 5014 | return respDic |
| 5015 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5016 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5017 | """ |
| 5018 | Searches the latest ONOS log file for the given search term and |
| 5019 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5020 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5021 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5022 | searchTerm: |
| 5023 | The string to grep from the ONOS log. |
| 5024 | startLine: |
| 5025 | The term that decides which line is the start to search the searchTerm in |
| 5026 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5027 | logNum: |
| 5028 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5029 | information.Because of this, search mutiply logs is necessary to capture |
| 5030 | the right result. logNum is the number of karaf logs that we need to search |
| 5031 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5032 | mode: |
| 5033 | all: return all the strings that contain the search term |
| 5034 | last: return the last string that contains the search term |
| 5035 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5036 | num: return the number of times that the searchTerm appears in the log |
| 5037 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5038 | """ |
| 5039 | try: |
| 5040 | assert type( searchTerm ) is str |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5041 | #Build the log paths string |
| 5042 | logPath = '/opt/onos/log/karaf.log.' |
| 5043 | logPaths = '/opt/onos/log/karaf.log' |
| 5044 | for i in range( 1, logNum ): |
| 5045 | logPaths = logPath + str( i ) + " " + logPaths |
| 5046 | cmd = "cat " + logPaths |
| 5047 | if mode == 'all': |
| 5048 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5049 | if mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5050 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5051 | if mode == 'first': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5052 | if startLine != '': |
| 5053 | # 100000000 is just a extreme large number to make sure this function can grep all the lines after startLine |
| 5054 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\' | grep \'" + searchTerm + "\'" + "| head -n 1" |
| 5055 | else: |
| 5056 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5057 | if mode == 'num': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5058 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
You Wang | 118ba58 | 2017-01-02 17:14:43 -0800 | [diff] [blame] | 5059 | num = self.sendline( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5060 | return num |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5061 | if mode == 'total': |
| 5062 | totalLines = self.sendline( "cat /opt/onos/log/karaf.log | wc -l" ) |
| 5063 | return int(totalLines) |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5064 | before = self.sendline( cmd ) |
| 5065 | before = before.splitlines() |
| 5066 | # make sure the returned list only contains the search term |
| 5067 | returnLines = [line for line in before if searchTerm in line] |
| 5068 | return returnLines |
| 5069 | except AssertionError: |
| 5070 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5071 | return None |
| 5072 | except pexpect.EOF: |
| 5073 | main.log.error( self.name + ": EOF exception found" ) |
| 5074 | main.log.error( self.name + ": " + self.handle.before ) |
| 5075 | main.cleanup() |
| 5076 | main.exit() |
| 5077 | except pexpect.TIMEOUT: |
| 5078 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5079 | main.log.error( self.name + ": " + self.handle.before ) |
| 5080 | main.cleanup() |
| 5081 | main.exit() |
| 5082 | except Exception: |
| 5083 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5084 | main.cleanup() |
| 5085 | main.exit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5086 | |
| 5087 | def vplsShow( self, jsonFormat=True ): |
| 5088 | """ |
| 5089 | Description: Returns result of onos:vpls show, which should list the |
| 5090 | configured VPLS networks and the assigned interfaces. |
| 5091 | Optional: |
| 5092 | * jsonFormat: enable json formatting of output |
| 5093 | Returns: |
| 5094 | The output of the command or None on error. |
| 5095 | """ |
| 5096 | try: |
| 5097 | cmdStr = "vpls show" |
| 5098 | if jsonFormat: |
| 5099 | raise NotImplementedError |
| 5100 | cmdStr += " -j" |
| 5101 | handle = self.sendline( cmdStr ) |
| 5102 | assert handle is not None, "Error in sendline" |
| 5103 | assert "Command not found:" not in handle, handle |
| 5104 | return handle |
| 5105 | except AssertionError: |
| 5106 | main.log.exception( "" ) |
| 5107 | return None |
| 5108 | except TypeError: |
| 5109 | main.log.exception( self.name + ": Object not as expected" ) |
| 5110 | return None |
| 5111 | except pexpect.EOF: |
| 5112 | main.log.error( self.name + ": EOF exception found" ) |
| 5113 | main.log.error( self.name + ": " + self.handle.before ) |
| 5114 | main.cleanup() |
| 5115 | main.exit() |
| 5116 | except NotImplementedError: |
| 5117 | main.log.exception( self.name + ": Json output not supported") |
| 5118 | return None |
| 5119 | except Exception: |
| 5120 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5121 | main.cleanup() |
| 5122 | main.exit() |
| 5123 | |
| 5124 | def parseVplsShow( self ): |
| 5125 | """ |
| 5126 | Parse the cli output of 'vpls show' into json output. This is required |
| 5127 | as there is currently no json output available. |
| 5128 | """ |
| 5129 | try: |
| 5130 | output = [] |
| 5131 | raw = self.vplsShow( jsonFormat=False ) |
| 5132 | namePat = "VPLS name: (?P<name>\w+)" |
| 5133 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5134 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5135 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5136 | mIter = re.finditer( pattern, raw ) |
| 5137 | for match in mIter: |
| 5138 | item = {} |
| 5139 | item[ 'name' ] = match.group( 'name' ) |
| 5140 | ifaces = match.group( 'interfaces' ).split( ', ') |
| 5141 | if ifaces == [ "" ]: |
| 5142 | ifaces = [] |
| 5143 | item[ 'interfaces' ] = ifaces |
| 5144 | encap = match.group( 'encap' ) |
| 5145 | if encap != 'NONE': |
| 5146 | item[ 'encapsulation' ] = encap.lower() |
| 5147 | output.append( item ) |
| 5148 | return output |
| 5149 | except Exception: |
| 5150 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5151 | main.cleanup() |
| 5152 | main.exit() |
| 5153 | |
| 5154 | def vplsList( self, jsonFormat=True ): |
| 5155 | """ |
| 5156 | Description: Returns result of onos:vpls list, which should list the |
| 5157 | configured VPLS networks. |
| 5158 | Optional: |
| 5159 | * jsonFormat: enable json formatting of output |
| 5160 | """ |
| 5161 | try: |
| 5162 | cmdStr = "vpls list" |
| 5163 | if jsonFormat: |
| 5164 | raise NotImplementedError |
| 5165 | cmdStr += " -j" |
| 5166 | handle = self.sendline( cmdStr ) |
| 5167 | assert handle is not None, "Error in sendline" |
| 5168 | assert "Command not found:" not in handle, handle |
| 5169 | return handle |
| 5170 | except AssertionError: |
| 5171 | main.log.exception( "" ) |
| 5172 | return None |
| 5173 | except TypeError: |
| 5174 | main.log.exception( self.name + ": Object not as expected" ) |
| 5175 | return None |
| 5176 | except pexpect.EOF: |
| 5177 | main.log.error( self.name + ": EOF exception found" ) |
| 5178 | main.log.error( self.name + ": " + self.handle.before ) |
| 5179 | main.cleanup() |
| 5180 | main.exit() |
| 5181 | except NotImplementedError: |
| 5182 | main.log.exception( self.name + ": Json output not supported") |
| 5183 | return None |
| 5184 | except Exception: |
| 5185 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5186 | main.cleanup() |
| 5187 | main.exit() |
| 5188 | |
| 5189 | def vplsCreate( self, network ): |
| 5190 | """ |
| 5191 | CLI command to create a new VPLS network. |
| 5192 | Required arguments: |
| 5193 | network - String name of the network to create. |
| 5194 | returns: |
| 5195 | main.TRUE on success and main.FALSE on failure |
| 5196 | """ |
| 5197 | try: |
| 5198 | network = str( network ) |
| 5199 | cmdStr = "vpls create " |
| 5200 | cmdStr += network |
| 5201 | output = self.sendline( cmdStr ) |
| 5202 | assert output is not None, "Error in sendline" |
| 5203 | assert "Command not found:" not in output, output |
| 5204 | assert "Error executing command" not in output, output |
| 5205 | assert "VPLS already exists:" not in output, output |
| 5206 | return main.TRUE |
| 5207 | except AssertionError: |
| 5208 | main.log.exception( "" ) |
| 5209 | return main.FALSE |
| 5210 | except TypeError: |
| 5211 | main.log.exception( self.name + ": Object not as expected" ) |
| 5212 | return main.FALSE |
| 5213 | except pexpect.EOF: |
| 5214 | main.log.error( self.name + ": EOF exception found" ) |
| 5215 | main.log.error( self.name + ": " + self.handle.before ) |
| 5216 | main.cleanup() |
| 5217 | main.exit() |
| 5218 | except Exception: |
| 5219 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5220 | main.cleanup() |
| 5221 | main.exit() |
| 5222 | |
| 5223 | def vplsDelete( self, network ): |
| 5224 | """ |
| 5225 | CLI command to delete a VPLS network. |
| 5226 | Required arguments: |
| 5227 | network - Name of the network to delete. |
| 5228 | returns: |
| 5229 | main.TRUE on success and main.FALSE on failure |
| 5230 | """ |
| 5231 | try: |
| 5232 | network = str( network ) |
| 5233 | cmdStr = "vpls delete " |
| 5234 | cmdStr += network |
| 5235 | output = self.sendline( cmdStr ) |
| 5236 | assert output is not None, "Error in sendline" |
| 5237 | assert "Command not found:" not in output, output |
| 5238 | assert "Error executing command" not in output, output |
| 5239 | assert " not found" not in output, output |
| 5240 | return main.TRUE |
| 5241 | except AssertionError: |
| 5242 | main.log.exception( "" ) |
| 5243 | return main.FALSE |
| 5244 | except TypeError: |
| 5245 | main.log.exception( self.name + ": Object not as expected" ) |
| 5246 | return main.FALSE |
| 5247 | except pexpect.EOF: |
| 5248 | main.log.error( self.name + ": EOF exception found" ) |
| 5249 | main.log.error( self.name + ": " + self.handle.before ) |
| 5250 | main.cleanup() |
| 5251 | main.exit() |
| 5252 | except Exception: |
| 5253 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5254 | main.cleanup() |
| 5255 | main.exit() |
| 5256 | |
| 5257 | def vplsAddIface( self, network, iface ): |
| 5258 | """ |
| 5259 | CLI command to add an interface to a VPLS network. |
| 5260 | Required arguments: |
| 5261 | network - Name of the network to add the interface to. |
| 5262 | iface - The ONOS name for an interface. |
| 5263 | returns: |
| 5264 | main.TRUE on success and main.FALSE on failure |
| 5265 | """ |
| 5266 | try: |
| 5267 | network = str( network ) |
| 5268 | iface = str( iface ) |
| 5269 | cmdStr = "vpls add-if " |
| 5270 | cmdStr += network + " " + iface |
| 5271 | output = self.sendline( cmdStr ) |
| 5272 | assert output is not None, "Error in sendline" |
| 5273 | assert "Command not found:" not in output, output |
| 5274 | assert "Error executing command" not in output, output |
| 5275 | assert "already associated to network" not in output, output |
| 5276 | assert "Interface cannot be added." not in output, output |
| 5277 | return main.TRUE |
| 5278 | except AssertionError: |
| 5279 | main.log.exception( "" ) |
| 5280 | return main.FALSE |
| 5281 | except TypeError: |
| 5282 | main.log.exception( self.name + ": Object not as expected" ) |
| 5283 | return main.FALSE |
| 5284 | except pexpect.EOF: |
| 5285 | main.log.error( self.name + ": EOF exception found" ) |
| 5286 | main.log.error( self.name + ": " + self.handle.before ) |
| 5287 | main.cleanup() |
| 5288 | main.exit() |
| 5289 | except Exception: |
| 5290 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5291 | main.cleanup() |
| 5292 | main.exit() |
| 5293 | |
| 5294 | def vplsRemIface( self, network, iface ): |
| 5295 | """ |
| 5296 | CLI command to remove an interface from a VPLS network. |
| 5297 | Required arguments: |
| 5298 | network - Name of the network to remove the interface from. |
| 5299 | iface - Name of the interface to remove. |
| 5300 | returns: |
| 5301 | main.TRUE on success and main.FALSE on failure |
| 5302 | """ |
| 5303 | try: |
| 5304 | iface = str( iface ) |
| 5305 | cmdStr = "vpls rem-if " |
| 5306 | cmdStr += network + " " + iface |
| 5307 | output = self.sendline( cmdStr ) |
| 5308 | assert output is not None, "Error in sendline" |
| 5309 | assert "Command not found:" not in output, output |
| 5310 | assert "Error executing command" not in output, output |
| 5311 | assert "is not configured" not in output, output |
| 5312 | return main.TRUE |
| 5313 | except AssertionError: |
| 5314 | main.log.exception( "" ) |
| 5315 | return main.FALSE |
| 5316 | except TypeError: |
| 5317 | main.log.exception( self.name + ": Object not as expected" ) |
| 5318 | return main.FALSE |
| 5319 | except pexpect.EOF: |
| 5320 | main.log.error( self.name + ": EOF exception found" ) |
| 5321 | main.log.error( self.name + ": " + self.handle.before ) |
| 5322 | main.cleanup() |
| 5323 | main.exit() |
| 5324 | except Exception: |
| 5325 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5326 | main.cleanup() |
| 5327 | main.exit() |
| 5328 | |
| 5329 | def vplsClean( self ): |
| 5330 | """ |
| 5331 | Description: Clears the VPLS app configuration. |
| 5332 | Returns: main.TRUE on success and main.FALSE on failure |
| 5333 | """ |
| 5334 | try: |
| 5335 | cmdStr = "vpls clean" |
| 5336 | handle = self.sendline( cmdStr ) |
| 5337 | assert handle is not None, "Error in sendline" |
| 5338 | assert "Command not found:" not in handle, handle |
| 5339 | return handle |
| 5340 | except AssertionError: |
| 5341 | main.log.exception( "" ) |
| 5342 | return main.FALSE |
| 5343 | except TypeError: |
| 5344 | main.log.exception( self.name + ": Object not as expected" ) |
| 5345 | return main.FALSE |
| 5346 | except pexpect.EOF: |
| 5347 | main.log.error( self.name + ": EOF exception found" ) |
| 5348 | main.log.error( self.name + ": " + self.handle.before ) |
| 5349 | main.cleanup() |
| 5350 | main.exit() |
| 5351 | except Exception: |
| 5352 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5353 | main.cleanup() |
| 5354 | main.exit() |
| 5355 | |
| 5356 | def vplsSetEncap( self, network, encapType ): |
| 5357 | """ |
| 5358 | CLI command to add an interface to a VPLS network. |
| 5359 | Required arguments: |
| 5360 | network - Name of the network to create. |
| 5361 | encapType - Type of encapsulation. |
| 5362 | returns: |
| 5363 | main.TRUE on success and main.FALSE on failure |
| 5364 | """ |
| 5365 | try: |
| 5366 | network = str( network ) |
| 5367 | encapType = str( encapType ).upper() |
| 5368 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5369 | cmdStr = "vpls set-encap " |
| 5370 | cmdStr += network + " " + encapType |
| 5371 | output = self.sendline( cmdStr ) |
| 5372 | assert output is not None, "Error in sendline" |
| 5373 | assert "Command not found:" not in output, output |
| 5374 | assert "Error executing command" not in output, output |
| 5375 | assert "already associated to network" not in output, output |
| 5376 | assert "Encapsulation type " not in output, output |
| 5377 | return main.TRUE |
| 5378 | except AssertionError: |
| 5379 | main.log.exception( "" ) |
| 5380 | return main.FALSE |
| 5381 | except TypeError: |
| 5382 | main.log.exception( self.name + ": Object not as expected" ) |
| 5383 | return main.FALSE |
| 5384 | except pexpect.EOF: |
| 5385 | main.log.error( self.name + ": EOF exception found" ) |
| 5386 | main.log.error( self.name + ": " + self.handle.before ) |
| 5387 | main.cleanup() |
| 5388 | main.exit() |
| 5389 | except Exception: |
| 5390 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5391 | main.cleanup() |
| 5392 | main.exit() |
| 5393 | |
| 5394 | def interfaces( self, jsonFormat=True ): |
| 5395 | """ |
| 5396 | Description: Returns result of interfaces command. |
| 5397 | Optional: |
| 5398 | * jsonFormat: enable json formatting of output |
| 5399 | Returns: |
| 5400 | The output of the command or None on error. |
| 5401 | """ |
| 5402 | try: |
| 5403 | cmdStr = "interfaces" |
| 5404 | if jsonFormat: |
| 5405 | #raise NotImplementedError |
| 5406 | cmdStr += " -j" |
| 5407 | handle = self.sendline( cmdStr ) |
| 5408 | assert handle is not None, "Error in sendline" |
| 5409 | assert "Command not found:" not in handle, handle |
| 5410 | return handle |
| 5411 | except AssertionError: |
| 5412 | main.log.exception( "" ) |
| 5413 | return None |
| 5414 | except TypeError: |
| 5415 | main.log.exception( self.name + ": Object not as expected" ) |
| 5416 | return None |
| 5417 | except pexpect.EOF: |
| 5418 | main.log.error( self.name + ": EOF exception found" ) |
| 5419 | main.log.error( self.name + ": " + self.handle.before ) |
| 5420 | main.cleanup() |
| 5421 | main.exit() |
| 5422 | except NotImplementedError: |
| 5423 | main.log.exception( self.name + ": Json output not supported") |
| 5424 | return None |
| 5425 | except Exception: |
| 5426 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5427 | main.cleanup() |
| 5428 | main.exit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5429 | |
| 5430 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
| 5431 | ''' |
| 5432 | Get the timestamp of searchTerm from karaf log. |
| 5433 | |
| 5434 | Arguments: |
| 5435 | splitTerm_before and splitTerm_after: |
| 5436 | |
| 5437 | The terms that split the string that contains the timeStamp of |
| 5438 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5439 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5440 | and the splitTerm_after is "x" |
| 5441 | |
| 5442 | others: |
| 5443 | |
| 5444 | plz look at the "logsearch" Function in onosclidriver.py |
| 5445 | |
| 5446 | |
| 5447 | ''' |
| 5448 | if logNum < 0: |
| 5449 | main.log.error("Get wrong log number ") |
| 5450 | return main.ERROR |
| 5451 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
| 5452 | if len(lines) == 0: |
| 5453 | main.log.warn( "Captured timestamp string is empty" ) |
| 5454 | return main.ERROR |
| 5455 | lines = lines[ 0 ] |
| 5456 | try: |
| 5457 | assert type(lines) is str |
| 5458 | # get the target value |
| 5459 | line = lines.split( splitTerm_before ) |
| 5460 | key = line[ 1 ].split( splitTerm_after ) |
| 5461 | return int( key[ 0 ] ) |
| 5462 | except IndexError: |
| 5463 | main.log.warn( "Index Error!" ) |
| 5464 | return main.ERROR |
| 5465 | except AssertionError: |
| 5466 | main.log.warn( "Search Term Not Found " ) |
| 5467 | return main.ERROR |