andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 4 | This driver enters the onos> prompt to issue commands. |
| 5 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 6 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 7 | functions and document properly. |
| 8 | |
| 9 | If you are a contributor to the driver, please |
| 10 | list your email here for future contact: |
| 11 | |
| 12 | jhall@onlab.us |
| 13 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 14 | shreya@onlab.us |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 15 | |
| 16 | OCT 13 2014 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 17 | Modified 2016 by ON.Lab |
| 18 | |
| 19 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 20 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 21 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 22 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 23 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 24 | import pexpect |
| 25 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 26 | import json |
| 27 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 28 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 29 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 30 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 31 | from core.graph import Graph |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 32 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 33 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 34 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 35 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 36 | def __init__( self ): |
| 37 | """ |
| 38 | Initialize client |
| 39 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 40 | self.name = None |
| 41 | self.home = None |
| 42 | self.handle = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 43 | self.graph = Graph() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 44 | super( CLI, self ).__init__() |
| 45 | |
| 46 | def connect( self, **connectargs ): |
| 47 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 48 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 49 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 50 | try: |
| 51 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 53 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 54 | for key in self.options: |
| 55 | if key == "home": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 56 | self.home = self.options[ 'home' ] |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 57 | break |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 58 | if self.home is None or self.home == "": |
Jon Hall | e94919c | 2015-03-23 11:42:57 -0700 | [diff] [blame] | 59 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 60 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 61 | for key in self.options: |
| 62 | if key == 'onosIp': |
| 63 | self.onosIp = self.options[ 'onosIp' ] |
| 64 | break |
| 65 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 66 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 67 | |
| 68 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 69 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 70 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 71 | else: |
| 72 | main.log.info( self.name + |
| 73 | ": Trying to connect to " + |
| 74 | self.ip_address ) |
| 75 | |
| 76 | except KeyError: |
| 77 | main.log.info( "Invalid host name," + |
| 78 | " connecting to local host instead" ) |
| 79 | self.ip_address = 'localhost' |
| 80 | except Exception as inst: |
| 81 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 82 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 83 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 84 | user_name=self.user_name, |
| 85 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 86 | port=self.port, |
| 87 | pwd=self.pwd, |
| 88 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 89 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 90 | self.handle.sendline( "cd " + self.home ) |
| 91 | self.handle.expect( "\$" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 92 | if self.handle: |
| 93 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | else: |
| 95 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 96 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 97 | except TypeError: |
| 98 | main.log.exception( self.name + ": Object not as expected" ) |
| 99 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 100 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 101 | main.log.error( self.name + ": EOF exception found" ) |
| 102 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 103 | main.cleanup() |
| 104 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 105 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 106 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 107 | main.cleanup() |
| 108 | main.exit() |
| 109 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 110 | def disconnect( self ): |
| 111 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 112 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 113 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 114 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 115 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 116 | if self.handle: |
| 117 | i = self.logout() |
| 118 | if i == main.TRUE: |
| 119 | self.handle.sendline( "" ) |
| 120 | self.handle.expect( "\$" ) |
| 121 | self.handle.sendline( "exit" ) |
| 122 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 123 | except TypeError: |
| 124 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 125 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 126 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 127 | main.log.error( self.name + ": EOF exception found" ) |
| 128 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 129 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 130 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 131 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 132 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 133 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | response = main.FALSE |
| 135 | return response |
| 136 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 137 | def logout( self ): |
| 138 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 139 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 140 | Returns main.TRUE if exited CLI and |
| 141 | main.FALSE on timeout (not guranteed you are disconnected) |
| 142 | None on TypeError |
| 143 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 144 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 145 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 146 | if self.handle: |
| 147 | self.handle.sendline( "" ) |
| 148 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ], |
| 149 | timeout=10 ) |
| 150 | if i == 0: # In ONOS CLI |
| 151 | self.handle.sendline( "logout" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 152 | j = self.handle.expect( [ "\$", |
| 153 | "Command not found:", |
| 154 | pexpect.TIMEOUT ] ) |
| 155 | if j == 0: # Successfully logged out |
| 156 | return main.TRUE |
| 157 | elif j == 1 or j == 2: |
| 158 | # ONOS didn't fully load, and logout command isn't working |
| 159 | # or the command timed out |
| 160 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 161 | try: |
| 162 | self.handle.expect( "\$" ) |
| 163 | except pexpect.TIMEOUT: |
| 164 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 165 | return main.TRUE |
| 166 | else: # some other output |
| 167 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 168 | repr( self.handle.before ) ) |
| 169 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 170 | elif i == 1: # not in CLI |
| 171 | return main.TRUE |
| 172 | elif i == 3: # Timeout |
| 173 | return main.FALSE |
| 174 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 175 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 176 | except TypeError: |
| 177 | main.log.exception( self.name + ": Object not as expected" ) |
| 178 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 179 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 181 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 182 | main.cleanup() |
| 183 | main.exit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 184 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 185 | main.log.error( self.name + |
| 186 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 187 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 188 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 189 | main.cleanup() |
| 190 | main.exit() |
| 191 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 192 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 193 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 194 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 195 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 196 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 197 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 198 | try: |
| 199 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 200 | main.log.error( "Must define cellname" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 201 | main.cleanup() |
| 202 | main.exit() |
| 203 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 204 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 205 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 206 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 207 | # and that this driver will have to change accordingly |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 208 | self.handle.expect(str(cellname)) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 209 | handleBefore = self.handle.before |
| 210 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 211 | # Get the rest of the handle |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 212 | self.handle.sendline("") |
| 213 | self.handle.expect("\$") |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 214 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 215 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 216 | main.log.info( "Cell call returned: " + handleBefore + |
| 217 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 218 | |
| 219 | return main.TRUE |
| 220 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 221 | except TypeError: |
| 222 | main.log.exception( self.name + ": Object not as expected" ) |
| 223 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 224 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 225 | main.log.error( self.name + ": eof exception found" ) |
| 226 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 227 | main.cleanup() |
| 228 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 229 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 230 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 231 | main.cleanup() |
| 232 | main.exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 233 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 234 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 235 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 236 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 237 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 238 | by user would be used to set the current karaf shell idle timeout. |
| 239 | Note that when ever this property is modified the shell will exit and |
Hari Krishna | d7b9c20 | 2015-01-05 10:38:14 -0800 | [diff] [blame] | 240 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 241 | Below is an example to start a session with 60 seconds idle timeout |
| 242 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 243 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 244 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 245 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 246 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 247 | Note: karafTimeout is left as str so that this could be read |
| 248 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 249 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 250 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 251 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 252 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 253 | self.handle.sendline( "" ) |
| 254 | x = self.handle.expect( [ |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 255 | "\$", "onos>" ], commandlineTimeout) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 256 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 257 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 258 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 259 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 260 | # Not in CLI so login |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 261 | if waitForStart: |
| 262 | # Wait for onos start ( -w ) and enter onos cli |
| 263 | startCliCommand = "onos -w " |
| 264 | else: |
| 265 | startCliCommand = "onos " |
| 266 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | i = self.handle.expect( [ |
| 268 | "onos>", |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 269 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 270 | |
| 271 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 272 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 273 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 274 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 275 | "config:property-set -p org.apache.karaf.shell\ |
| 276 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 277 | karafTimeout ) |
| 278 | self.handle.expect( "\$" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 279 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 280 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 281 | return main.TRUE |
| 282 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 283 | # If failed, send ctrl+c to process and try again |
| 284 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 285 | self.handle.send( "\x03" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 286 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 287 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 288 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 289 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 290 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 291 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 292 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 293 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 294 | "config:property-set -p org.apache.karaf.shell\ |
| 295 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 296 | karafTimeout ) |
| 297 | self.handle.expect( "\$" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 298 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 299 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 300 | return main.TRUE |
| 301 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 302 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 303 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 304 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 305 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 306 | except TypeError: |
| 307 | main.log.exception( self.name + ": Object not as expected" ) |
| 308 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 309 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 310 | main.log.error( self.name + ": EOF exception found" ) |
| 311 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 312 | main.cleanup() |
| 313 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 314 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 315 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 316 | main.cleanup() |
| 317 | main.exit() |
| 318 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 319 | def startCellCli( self, karafTimeout="", |
| 320 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 321 | """ |
| 322 | Start CLI on onos ecll handle. |
| 323 | |
| 324 | karafTimeout is an optional argument. karafTimeout value passed |
| 325 | by user would be used to set the current karaf shell idle timeout. |
| 326 | Note that when ever this property is modified the shell will exit and |
| 327 | the subsequent login would reflect new idle timeout. |
| 328 | Below is an example to start a session with 60 seconds idle timeout |
| 329 | ( input value is in milliseconds ): |
| 330 | |
| 331 | tValue = "60000" |
| 332 | |
| 333 | Note: karafTimeout is left as str so that this could be read |
| 334 | and passed to startOnosCli from PARAMS file as str. |
| 335 | """ |
| 336 | |
| 337 | try: |
| 338 | self.handle.sendline( "" ) |
| 339 | x = self.handle.expect( [ |
| 340 | "\$", "onos>" ], commandlineTimeout) |
| 341 | |
| 342 | if x == 1: |
| 343 | main.log.info( "ONOS cli is already running" ) |
| 344 | return main.TRUE |
| 345 | |
| 346 | # Wait for onos start ( -w ) and enter onos cli |
| 347 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 348 | i = self.handle.expect( [ |
| 349 | "onos>", |
| 350 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 351 | |
| 352 | if i == 0: |
| 353 | main.log.info( self.name + " CLI Started successfully" ) |
| 354 | if karafTimeout: |
| 355 | self.handle.sendline( |
| 356 | "config:property-set -p org.apache.karaf.shell\ |
| 357 | sshIdleTimeout " + |
| 358 | karafTimeout ) |
| 359 | self.handle.expect( "\$" ) |
| 360 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 361 | self.handle.expect( "onos>" ) |
| 362 | return main.TRUE |
| 363 | else: |
| 364 | # If failed, send ctrl+c to process and try again |
| 365 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 366 | self.handle.send( "\x03" ) |
| 367 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 368 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 369 | timeout=30 ) |
| 370 | if i == 0: |
| 371 | main.log.info( self.name + " CLI Started " + |
| 372 | "successfully after retry attempt" ) |
| 373 | if karafTimeout: |
| 374 | self.handle.sendline( |
| 375 | "config:property-set -p org.apache.karaf.shell\ |
| 376 | sshIdleTimeout " + |
| 377 | karafTimeout ) |
| 378 | self.handle.expect( "\$" ) |
| 379 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 380 | self.handle.expect( "onos>" ) |
| 381 | return main.TRUE |
| 382 | else: |
| 383 | main.log.error( "Connection to CLI " + |
| 384 | self.name + " timeout" ) |
| 385 | return main.FALSE |
| 386 | |
| 387 | except TypeError: |
| 388 | main.log.exception( self.name + ": Object not as expected" ) |
| 389 | return None |
| 390 | except pexpect.EOF: |
| 391 | main.log.error( self.name + ": EOF exception found" ) |
| 392 | main.log.error( self.name + ": " + self.handle.before ) |
| 393 | main.cleanup() |
| 394 | main.exit() |
| 395 | except Exception: |
| 396 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 397 | main.cleanup() |
| 398 | main.exit() |
| 399 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 400 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 401 | """ |
| 402 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 403 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 404 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 405 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 406 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 407 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 408 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 409 | """ |
| 410 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 411 | lvlStr = "" |
| 412 | if level: |
| 413 | lvlStr = "--level=" + level |
| 414 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 415 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 416 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 417 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 418 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 419 | response = self.handle.before |
| 420 | if re.search( "Error", response ): |
| 421 | return main.FALSE |
| 422 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 423 | except pexpect.TIMEOUT: |
| 424 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 425 | if noExit: |
| 426 | main.cleanup() |
| 427 | return None |
| 428 | else: |
| 429 | main.cleanup() |
| 430 | main.exit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 431 | except pexpect.EOF: |
| 432 | main.log.error( self.name + ": EOF exception found" ) |
| 433 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 434 | if noExit: |
| 435 | main.cleanup() |
| 436 | return None |
| 437 | else: |
| 438 | main.cleanup() |
| 439 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 440 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 441 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 442 | if noExit: |
| 443 | main.cleanup() |
| 444 | return None |
| 445 | else: |
| 446 | main.cleanup() |
| 447 | main.exit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 448 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 449 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 450 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 451 | Send a completely user specified string to |
| 452 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 453 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 454 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 455 | if noExit is True, TestON will not exit, and return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 456 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 457 | Warning: There are no sanity checking to commands |
| 458 | sent using this method. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 459 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 460 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 461 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 462 | # Try to reconnect if disconnected from cli |
| 463 | self.handle.sendline( "" ) |
| 464 | i = self.handle.expect( [ "onos>", "\$", pexpect.TIMEOUT ] ) |
| 465 | if i == 1: |
| 466 | main.log.error( self.name + ": onos cli session closed. ") |
| 467 | if self.onosIp: |
| 468 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 469 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 470 | if reconnectResult: |
| 471 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 472 | else: |
| 473 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 474 | if noExit: |
| 475 | return None |
| 476 | else: |
| 477 | main.cleanup() |
| 478 | main.exit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 479 | else: |
| 480 | main.cleanup() |
| 481 | main.exit() |
| 482 | if i == 2: |
| 483 | self.handle.sendline( "" ) |
| 484 | self.handle.expect( "onos>" ) |
| 485 | |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 486 | if debug: |
| 487 | # NOTE: This adds and average of .4 seconds per call |
| 488 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 489 | self.log( logStr,noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 490 | self.handle.sendline( cmdStr ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 491 | i = self.handle.expect( ["onos>", "\$"], timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 492 | response = self.handle.before |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 493 | # TODO: do something with i |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 494 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
| 495 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 496 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 497 | main.log.debug( self.name + ": Raw output" ) |
| 498 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 499 | |
| 500 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 501 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 502 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 503 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 504 | main.log.debug( self.name + ": ansiEscape output" ) |
| 505 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 506 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 507 | # Remove extra return chars that get added |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 508 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 509 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 510 | main.log.debug( self.name + ": Removed extra returns " + |
| 511 | "from output" ) |
| 512 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 513 | |
| 514 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 515 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 516 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 517 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 518 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 519 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 520 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 521 | output = response.split( cmdStr.strip(), 1 ) |
| 522 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 523 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 524 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 525 | main.log.debug( self.name + ": " + repr( r ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 526 | output = output[1].strip() |
| 527 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 528 | main.log.info( "Response from ONOS: {}".format( output ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 529 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 530 | except pexpect.TIMEOUT: |
| 531 | main.log.error( self.name + ":ONOS timeout" ) |
| 532 | if debug: |
| 533 | main.log.debug( self.handle.before ) |
| 534 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 535 | except IndexError: |
| 536 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 537 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 538 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 539 | except TypeError: |
| 540 | main.log.exception( self.name + ": Object not as expected" ) |
| 541 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 542 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 543 | main.log.error( self.name + ": EOF exception found" ) |
| 544 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 545 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 546 | return None |
| 547 | else: |
| 548 | main.cleanup() |
| 549 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 550 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 551 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 552 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 553 | return None |
| 554 | else: |
| 555 | main.cleanup() |
| 556 | main.exit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 557 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 558 | # IMPORTANT NOTE: |
| 559 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 560 | # the cli command changing 'a:b' with 'aB'. |
| 561 | # Ex ) onos:topology > onosTopology |
| 562 | # onos:links > onosLinks |
| 563 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 564 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 565 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 566 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 567 | Adds a new cluster node by ID and address information. |
| 568 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 569 | * nodeId |
| 570 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 571 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 572 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 573 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 574 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 575 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 576 | str( ONOSIp ) + " " + str( tcpPort ) |
| 577 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 578 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 579 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 580 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 581 | main.log.error( "Error in adding node" ) |
| 582 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 583 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 584 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 585 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 586 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 587 | except AssertionError: |
| 588 | main.log.exception( "" ) |
| 589 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 590 | except TypeError: |
| 591 | main.log.exception( self.name + ": Object not as expected" ) |
| 592 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 593 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 594 | main.log.error( self.name + ": EOF exception found" ) |
| 595 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 596 | main.cleanup() |
| 597 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 598 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 599 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 600 | main.cleanup() |
| 601 | main.exit() |
| 602 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 603 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 604 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 605 | Removes a cluster by ID |
| 606 | Issues command: 'remove-node [<node-id>]' |
| 607 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 608 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 609 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 610 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 611 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 612 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 613 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 614 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 615 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 616 | if re.search( "Error", handle ): |
| 617 | main.log.error( "Error in removing node" ) |
| 618 | main.log.error( handle ) |
| 619 | return main.FALSE |
| 620 | else: |
| 621 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 622 | except AssertionError: |
| 623 | main.log.exception( "" ) |
| 624 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 625 | except TypeError: |
| 626 | main.log.exception( self.name + ": Object not as expected" ) |
| 627 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 628 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 629 | main.log.error( self.name + ": EOF exception found" ) |
| 630 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 631 | main.cleanup() |
| 632 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 633 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 634 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 635 | main.cleanup() |
| 636 | main.exit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 637 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 638 | def nodes( self, jsonFormat=True): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 639 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 640 | List the nodes currently visible |
| 641 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 642 | Optional argument: |
| 643 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 644 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 645 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 646 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 647 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 648 | cmdStr += " -j" |
| 649 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 650 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 651 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 652 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 653 | except AssertionError: |
| 654 | main.log.exception( "" ) |
| 655 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 656 | except TypeError: |
| 657 | main.log.exception( self.name + ": Object not as expected" ) |
| 658 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 659 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 660 | main.log.error( self.name + ": EOF exception found" ) |
| 661 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 662 | main.cleanup() |
| 663 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 664 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 665 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 666 | main.cleanup() |
| 667 | main.exit() |
| 668 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 669 | def topology( self ): |
| 670 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 671 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 672 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 673 | Return: |
| 674 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 675 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 676 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 677 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 678 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 679 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 680 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 681 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 682 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 683 | except AssertionError: |
| 684 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 685 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 686 | except TypeError: |
| 687 | main.log.exception( self.name + ": Object not as expected" ) |
| 688 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 689 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 690 | main.log.error( self.name + ": EOF exception found" ) |
| 691 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 692 | main.cleanup() |
| 693 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 694 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 695 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 696 | main.cleanup() |
| 697 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 698 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 699 | def deviceRemove( self, deviceId ): |
| 700 | """ |
| 701 | Removes particular device from storage |
| 702 | |
| 703 | TODO: refactor this function |
| 704 | """ |
| 705 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 706 | cmdStr = "device-remove " + str( deviceId ) |
| 707 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 708 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 709 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 710 | if re.search( "Error", handle ): |
| 711 | main.log.error( "Error in removing device" ) |
| 712 | main.log.error( handle ) |
| 713 | return main.FALSE |
| 714 | else: |
| 715 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 716 | except AssertionError: |
| 717 | main.log.exception( "" ) |
| 718 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 719 | except TypeError: |
| 720 | main.log.exception( self.name + ": Object not as expected" ) |
| 721 | return None |
| 722 | except pexpect.EOF: |
| 723 | main.log.error( self.name + ": EOF exception found" ) |
| 724 | main.log.error( self.name + ": " + self.handle.before ) |
| 725 | main.cleanup() |
| 726 | main.exit() |
| 727 | except Exception: |
| 728 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 729 | main.cleanup() |
| 730 | main.exit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 731 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 732 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 733 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 734 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 735 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 736 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 737 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 738 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 739 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 740 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 741 | cmdStr += " -j" |
| 742 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 743 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 744 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 745 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 746 | except AssertionError: |
| 747 | main.log.exception( "" ) |
| 748 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 749 | except TypeError: |
| 750 | main.log.exception( self.name + ": Object not as expected" ) |
| 751 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 752 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 753 | main.log.error( self.name + ": EOF exception found" ) |
| 754 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 755 | main.cleanup() |
| 756 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 757 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 758 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 759 | main.cleanup() |
| 760 | main.exit() |
| 761 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 762 | def balanceMasters( self ): |
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 | This balances the devices across all controllers |
| 765 | by issuing command: 'onos> onos:balance-masters' |
| 766 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 767 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 768 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 769 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 770 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 771 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 772 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 773 | if re.search( "Error", handle ): |
| 774 | main.log.error( "Error in balancing masters" ) |
| 775 | main.log.error( handle ) |
| 776 | return main.FALSE |
| 777 | else: |
| 778 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 779 | except AssertionError: |
| 780 | main.log.exception( "" ) |
| 781 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 782 | except TypeError: |
| 783 | main.log.exception( self.name + ": Object not as expected" ) |
| 784 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 785 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 786 | main.log.error( self.name + ": EOF exception found" ) |
| 787 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 788 | main.cleanup() |
| 789 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 790 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 791 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 792 | main.cleanup() |
| 793 | main.exit() |
| 794 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 795 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 796 | """ |
| 797 | Returns the output of the masters command. |
| 798 | Optional argument: |
| 799 | * jsonFormat - boolean indicating if you want output in json |
| 800 | """ |
| 801 | try: |
| 802 | cmdStr = "onos:masters" |
| 803 | if jsonFormat: |
| 804 | cmdStr += " -j" |
| 805 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 806 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 807 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 808 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 809 | except AssertionError: |
| 810 | main.log.exception( "" ) |
| 811 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 812 | except TypeError: |
| 813 | main.log.exception( self.name + ": Object not as expected" ) |
| 814 | return None |
| 815 | except pexpect.EOF: |
| 816 | main.log.error( self.name + ": EOF exception found" ) |
| 817 | main.log.error( self.name + ": " + self.handle.before ) |
| 818 | main.cleanup() |
| 819 | main.exit() |
| 820 | except Exception: |
| 821 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 822 | main.cleanup() |
| 823 | main.exit() |
| 824 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 825 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 826 | """ |
| 827 | Uses the master command to check that the devices' leadership |
| 828 | is evenly divided |
| 829 | |
| 830 | Dependencies: checkMasters() and summary() |
| 831 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 832 | Returns main.TRUE if the devices are balanced |
| 833 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 834 | Exits on Exception |
| 835 | Returns None on TypeError |
| 836 | """ |
| 837 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 838 | summaryOutput = self.summary() |
| 839 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 840 | except ( TypeError, ValueError ): |
| 841 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 842 | return None |
| 843 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 844 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 845 | mastersOutput = self.checkMasters() |
| 846 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 847 | first = masters[ 0 ][ "size" ] |
| 848 | for master in masters: |
| 849 | totalOwnedDevices += master[ "size" ] |
| 850 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 851 | main.log.error( "Mastership not balanced" ) |
| 852 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 853 | return main.FALSE |
| 854 | main.log.info( "Mastership balanced between " \ |
| 855 | + str( len(masters) ) + " masters" ) |
| 856 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 857 | except ( TypeError, ValueError ): |
| 858 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 859 | return None |
| 860 | except pexpect.EOF: |
| 861 | main.log.error( self.name + ": EOF exception found" ) |
| 862 | main.log.error( self.name + ": " + self.handle.before ) |
| 863 | main.cleanup() |
| 864 | main.exit() |
| 865 | except Exception: |
| 866 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 867 | main.cleanup() |
| 868 | main.exit() |
| 869 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 870 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 871 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 872 | Lists all core links |
| 873 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 874 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 875 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 876 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 877 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 878 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 879 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 880 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 881 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 882 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 883 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 884 | except AssertionError: |
| 885 | main.log.exception( "" ) |
| 886 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 887 | except TypeError: |
| 888 | main.log.exception( self.name + ": Object not as expected" ) |
| 889 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 890 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 891 | main.log.error( self.name + ": EOF exception found" ) |
| 892 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 893 | main.cleanup() |
| 894 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 895 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 896 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 897 | main.cleanup() |
| 898 | main.exit() |
| 899 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 900 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 901 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 902 | Lists all ports |
| 903 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 904 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 905 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 906 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 907 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 908 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 909 | cmdStr += " -j" |
| 910 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 911 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 912 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 913 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 914 | except AssertionError: |
| 915 | main.log.exception( "" ) |
| 916 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 917 | except TypeError: |
| 918 | main.log.exception( self.name + ": Object not as expected" ) |
| 919 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 920 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 921 | main.log.error( self.name + ": EOF exception found" ) |
| 922 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 923 | main.cleanup() |
| 924 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 925 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 926 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 927 | main.cleanup() |
| 928 | main.exit() |
| 929 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 930 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 931 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 932 | Lists all devices and the controllers with roles assigned to them |
| 933 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 934 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 935 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 936 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 937 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 938 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 939 | cmdStr += " -j" |
| 940 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 941 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 942 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 943 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 944 | except AssertionError: |
| 945 | main.log.exception( "" ) |
| 946 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 947 | except TypeError: |
| 948 | main.log.exception( self.name + ": Object not as expected" ) |
| 949 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 950 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 951 | main.log.error( self.name + ": EOF exception found" ) |
| 952 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 953 | main.cleanup() |
| 954 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 955 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 956 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 957 | main.cleanup() |
| 958 | main.exit() |
| 959 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 960 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 961 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 962 | Given the a string containing the json representation of the "roles" |
| 963 | cli command and a partial or whole device id, returns a json object |
| 964 | containing the roles output for the first device whose id contains |
| 965 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 966 | |
| 967 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 968 | A dict of the role assignments for the given device or |
| 969 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 970 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 971 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 972 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 973 | return None |
| 974 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 975 | rawRoles = self.roles() |
| 976 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 977 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 978 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 979 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 980 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 981 | return device |
| 982 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 983 | except ( TypeError, ValueError ): |
| 984 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 985 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 986 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 987 | main.log.error( self.name + ": EOF exception found" ) |
| 988 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 989 | main.cleanup() |
| 990 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 991 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 992 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 993 | main.cleanup() |
| 994 | main.exit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 995 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 996 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 997 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 998 | Iterates through each device and checks if there is a master assigned |
| 999 | Returns: main.TRUE if each device has a master |
| 1000 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1001 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1002 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1003 | rawRoles = self.roles() |
| 1004 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1005 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1006 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1007 | # print device |
| 1008 | if device[ 'master' ] == "none": |
| 1009 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1010 | return main.FALSE |
| 1011 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1012 | except ( TypeError, ValueError ): |
| 1013 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1014 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1015 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1016 | main.log.error( self.name + ": EOF exception found" ) |
| 1017 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1018 | main.cleanup() |
| 1019 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1020 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1021 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1022 | main.cleanup() |
| 1023 | main.exit() |
| 1024 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1025 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1026 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1027 | Returns string of paths, and the cost. |
| 1028 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1029 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1030 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1031 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1032 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1033 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1034 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1035 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1036 | main.log.error( "Error in getting paths" ) |
| 1037 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1038 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1039 | path = handle.split( ";" )[ 0 ] |
| 1040 | cost = handle.split( ";" )[ 1 ] |
| 1041 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1042 | except AssertionError: |
| 1043 | main.log.exception( "" ) |
| 1044 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1045 | except TypeError: |
| 1046 | main.log.exception( self.name + ": Object not as expected" ) |
| 1047 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1048 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1049 | main.log.error( self.name + ": EOF exception found" ) |
| 1050 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1051 | main.cleanup() |
| 1052 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1053 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1054 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1055 | main.cleanup() |
| 1056 | main.exit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1057 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1058 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1059 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1060 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1061 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1062 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1063 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1064 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1065 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1066 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1067 | cmdStr += " -j" |
| 1068 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1069 | if handle: |
| 1070 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1071 | # TODO: Maybe make this less hardcoded |
| 1072 | # ConsistentMap Exceptions |
| 1073 | assert "org.onosproject.store.service" not in handle |
| 1074 | # Node not leader |
| 1075 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1076 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1077 | except AssertionError: |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1078 | main.log.exception( "Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1079 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1080 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1081 | except TypeError: |
| 1082 | main.log.exception( self.name + ": Object not as expected" ) |
| 1083 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1084 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1085 | main.log.error( self.name + ": EOF exception found" ) |
| 1086 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1087 | main.cleanup() |
| 1088 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1089 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1090 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1091 | main.cleanup() |
| 1092 | main.exit() |
| 1093 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1094 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1095 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1096 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1097 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1098 | 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] | 1099 | partial mac address |
| 1100 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1101 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1102 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1103 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1104 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1105 | return None |
| 1106 | else: |
| 1107 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1108 | rawHosts = self.hosts() |
| 1109 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1110 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1111 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1112 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1113 | if not host: |
| 1114 | pass |
| 1115 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1116 | return host |
| 1117 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1118 | except ( TypeError, ValueError ): |
| 1119 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1120 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1121 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1122 | main.log.error( self.name + ": EOF exception found" ) |
| 1123 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1124 | main.cleanup() |
| 1125 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1126 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1127 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1128 | main.cleanup() |
| 1129 | main.exit() |
| 1130 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1131 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1132 | """ |
| 1133 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1134 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1135 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1136 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1137 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1138 | IMPORTANT: |
| 1139 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1140 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1141 | Furthermore, it assumes that value of VLAN is '-1' |
| 1142 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1143 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1144 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1145 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1146 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1147 | onosHostList = [] |
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 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1150 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1151 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1152 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1153 | i = iter( str( hostHex ) ) |
| 1154 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1155 | hostHex = hostHex + "/-1" |
| 1156 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1157 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1158 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1159 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1160 | except TypeError: |
| 1161 | main.log.exception( self.name + ": Object not as expected" ) |
| 1162 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1163 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1164 | main.log.error( self.name + ": EOF exception found" ) |
| 1165 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1166 | main.cleanup() |
| 1167 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1168 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1169 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1170 | main.cleanup() |
| 1171 | main.exit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1172 | |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1173 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1174 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1175 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1176 | * hostIdOne: ONOS host id for host1 |
| 1177 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1178 | Optional: |
| 1179 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1180 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1181 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1182 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1183 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1184 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1185 | Returns: |
| 1186 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1187 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1188 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1189 | cmdStr = "add-host-intent " |
| 1190 | if vlanId: |
| 1191 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1192 | if setVlan: |
| 1193 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1194 | if encap: |
| 1195 | cmdStr += "--encapsulation " + str( encap ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1196 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1197 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1198 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1199 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1200 | if re.search( "Error", handle ): |
| 1201 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1202 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1203 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1204 | else: |
| 1205 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1206 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
| 1207 | match = re.search('id=0x([\da-f]+),', handle) |
| 1208 | if match: |
| 1209 | return match.group()[3:-1] |
| 1210 | else: |
| 1211 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1212 | main.log.debug( "Response from ONOS was: " + |
| 1213 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1214 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1215 | except AssertionError: |
| 1216 | main.log.exception( "" ) |
| 1217 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1218 | except TypeError: |
| 1219 | main.log.exception( self.name + ": Object not as expected" ) |
| 1220 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1221 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1222 | main.log.error( self.name + ": EOF exception found" ) |
| 1223 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1224 | main.cleanup() |
| 1225 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1226 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1227 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1228 | main.cleanup() |
| 1229 | main.exit() |
| 1230 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1231 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1232 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1233 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1234 | * ingressDevice: device id of ingress device |
| 1235 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1236 | Optional: |
| 1237 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1238 | Description: |
| 1239 | Adds an optical intent by specifying an ingress and egress device |
| 1240 | Returns: |
| 1241 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1242 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1243 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1244 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1245 | " " + str( egressDevice ) |
| 1246 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1247 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1248 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1249 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1250 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1251 | main.log.error( "Error in adding Optical intent" ) |
| 1252 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1253 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1254 | main.log.info( "Optical intent installed between " + |
| 1255 | str( ingressDevice ) + " and " + |
| 1256 | str( egressDevice ) ) |
| 1257 | match = re.search('id=0x([\da-f]+),', handle) |
| 1258 | if match: |
| 1259 | return match.group()[3:-1] |
| 1260 | else: |
| 1261 | main.log.error( "Error, intent ID not found" ) |
| 1262 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1263 | except AssertionError: |
| 1264 | main.log.exception( "" ) |
| 1265 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1266 | except TypeError: |
| 1267 | main.log.exception( self.name + ": Object not as expected" ) |
| 1268 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1269 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1270 | main.log.error( self.name + ": EOF exception found" ) |
| 1271 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1272 | main.cleanup() |
| 1273 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1274 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1275 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1276 | main.cleanup() |
| 1277 | main.exit() |
| 1278 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1279 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1280 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1281 | ingressDevice, |
| 1282 | egressDevice, |
| 1283 | portIngress="", |
| 1284 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1285 | ethType="", |
| 1286 | ethSrc="", |
| 1287 | ethDst="", |
| 1288 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1289 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1290 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1291 | ipProto="", |
| 1292 | ipSrc="", |
| 1293 | ipDst="", |
| 1294 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1295 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1296 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1297 | setVlan="", |
| 1298 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1299 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1300 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1301 | * ingressDevice: device id of ingress device |
| 1302 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1303 | Optional: |
| 1304 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1305 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1306 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1307 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1308 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1309 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1310 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1311 | * ipSrc: specify ip source address |
| 1312 | * ipDst: specify ip destination address |
| 1313 | * tcpSrc: specify tcp source port |
| 1314 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1315 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1316 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1317 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1318 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1319 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1320 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1321 | Returns: |
| 1322 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1323 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1324 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1325 | options developers provide for point-to-point |
| 1326 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1327 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1328 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1329 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1330 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1331 | if ethType: |
| 1332 | cmd += " --ethType " + str( ethType ) |
| 1333 | if ethSrc: |
| 1334 | cmd += " --ethSrc " + str( ethSrc ) |
| 1335 | if ethDst: |
| 1336 | cmd += " --ethDst " + str( ethDst ) |
| 1337 | if bandwidth: |
| 1338 | cmd += " --bandwidth " + str( bandwidth ) |
| 1339 | if lambdaAlloc: |
| 1340 | cmd += " --lambda " |
| 1341 | if ipProto: |
| 1342 | cmd += " --ipProto " + str( ipProto ) |
| 1343 | if ipSrc: |
| 1344 | cmd += " --ipSrc " + str( ipSrc ) |
| 1345 | if ipDst: |
| 1346 | cmd += " --ipDst " + str( ipDst ) |
| 1347 | if tcpSrc: |
| 1348 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1349 | if tcpDst: |
| 1350 | cmd += " --tcpDst " + str( tcpDst ) |
| 1351 | if vlanId: |
| 1352 | cmd += " -v " + str( vlanId ) |
| 1353 | if setVlan: |
| 1354 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1355 | if encap: |
| 1356 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1357 | if protected: |
| 1358 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1359 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1360 | # Check whether the user appended the port |
| 1361 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1362 | if "/" in ingressDevice: |
| 1363 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1364 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1365 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1366 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1367 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1368 | # Would it make sense to throw an exception and exit |
| 1369 | # the test? |
| 1370 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [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( ingressDevice ) + "/" +\ |
| 1374 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1375 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1376 | if "/" in egressDevice: |
| 1377 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1378 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1379 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1380 | main.log.error( "You must specify the egress port" ) |
| 1381 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1382 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1383 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1384 | str( egressDevice ) + "/" +\ |
| 1385 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1386 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1387 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1388 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1389 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1390 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1391 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1392 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1393 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1394 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1395 | # TODO: print out all the options in this message? |
| 1396 | main.log.info( "Point-to-point intent installed between " + |
| 1397 | str( ingressDevice ) + " and " + |
| 1398 | str( egressDevice ) ) |
| 1399 | match = re.search('id=0x([\da-f]+),', handle) |
| 1400 | if match: |
| 1401 | return match.group()[3:-1] |
| 1402 | else: |
| 1403 | main.log.error( "Error, intent ID not found" ) |
| 1404 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1405 | except AssertionError: |
| 1406 | main.log.exception( "" ) |
| 1407 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1408 | except TypeError: |
| 1409 | main.log.exception( self.name + ": Object not as expected" ) |
| 1410 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1411 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1412 | main.log.error( self.name + ": EOF exception found" ) |
| 1413 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1414 | main.cleanup() |
| 1415 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1416 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1417 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1418 | main.cleanup() |
| 1419 | main.exit() |
| 1420 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1421 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1422 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1423 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1424 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1425 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1426 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1427 | ethType="", |
| 1428 | ethSrc="", |
| 1429 | ethDst="", |
| 1430 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1431 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1432 | ipProto="", |
| 1433 | ipSrc="", |
| 1434 | ipDst="", |
| 1435 | tcpSrc="", |
| 1436 | tcpDst="", |
| 1437 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1438 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1439 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1440 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1441 | partial=False, |
| 1442 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1443 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1444 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1445 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1446 | is same. That is, all ingress devices include port numbers |
| 1447 | with a "/" or all ingress devices could specify device |
| 1448 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1449 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1450 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1451 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1452 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1453 | Optional: |
| 1454 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1455 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1456 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1457 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1458 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1459 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1460 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1461 | * ipSrc: specify ip source address |
| 1462 | * ipDst: specify ip destination address |
| 1463 | * tcpSrc: specify tcp source port |
| 1464 | * tcpDst: specify tcp destination port |
| 1465 | * setEthSrc: action to Rewrite Source MAC Address |
| 1466 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1467 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1468 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1469 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1470 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1471 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1472 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1473 | Returns: |
| 1474 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1475 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1476 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1477 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1478 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1479 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1480 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1481 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1482 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1483 | if ethType: |
| 1484 | cmd += " --ethType " + str( ethType ) |
| 1485 | if ethSrc: |
| 1486 | cmd += " --ethSrc " + str( ethSrc ) |
| 1487 | if ethDst: |
| 1488 | cmd += " --ethDst " + str( ethDst ) |
| 1489 | if bandwidth: |
| 1490 | cmd += " --bandwidth " + str( bandwidth ) |
| 1491 | if lambdaAlloc: |
| 1492 | cmd += " --lambda " |
| 1493 | if ipProto: |
| 1494 | cmd += " --ipProto " + str( ipProto ) |
| 1495 | if ipSrc: |
| 1496 | cmd += " --ipSrc " + str( ipSrc ) |
| 1497 | if ipDst: |
| 1498 | cmd += " --ipDst " + str( ipDst ) |
| 1499 | if tcpSrc: |
| 1500 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1501 | if tcpDst: |
| 1502 | cmd += " --tcpDst " + str( tcpDst ) |
| 1503 | if setEthSrc: |
| 1504 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1505 | if setEthDst: |
| 1506 | cmd += " --setEthDst " + str( setEthDst ) |
| 1507 | if vlanId: |
| 1508 | cmd += " -v " + str( vlanId ) |
| 1509 | if setVlan: |
| 1510 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1511 | if partial: |
| 1512 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1513 | if encap: |
| 1514 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1515 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1516 | # Check whether the user appended the port |
| 1517 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1518 | |
| 1519 | if portIngressList is None: |
| 1520 | for ingressDevice in ingressDeviceList: |
| 1521 | if "/" in ingressDevice: |
| 1522 | cmd += " " + str( ingressDevice ) |
| 1523 | else: |
| 1524 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1525 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1526 | # TODO: perhaps more meaningful return |
| 1527 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1528 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1529 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1530 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1531 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1532 | cmd += " " + \ |
| 1533 | str( ingressDevice ) + "/" +\ |
| 1534 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1535 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1536 | main.log.error( "Device list and port list does not " + |
| 1537 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1538 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1539 | if "/" in egressDevice: |
| 1540 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1541 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1542 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1543 | main.log.error( "You must specify " + |
| 1544 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1545 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1546 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1547 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1548 | str( egressDevice ) + "/" +\ |
| 1549 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1550 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1551 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1552 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1553 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1554 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1555 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1556 | "intent" ) |
| 1557 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1558 | else: |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1559 | match = re.search('id=0x([\da-f]+),', handle) |
| 1560 | if match: |
| 1561 | return match.group()[3:-1] |
| 1562 | else: |
| 1563 | main.log.error( "Error, intent ID not found" ) |
| 1564 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1565 | except AssertionError: |
| 1566 | main.log.exception( "" ) |
| 1567 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1568 | except TypeError: |
| 1569 | main.log.exception( self.name + ": Object not as expected" ) |
| 1570 | return None |
| 1571 | except pexpect.EOF: |
| 1572 | main.log.error( self.name + ": EOF exception found" ) |
| 1573 | main.log.error( self.name + ": " + self.handle.before ) |
| 1574 | main.cleanup() |
| 1575 | main.exit() |
| 1576 | except Exception: |
| 1577 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1578 | main.cleanup() |
| 1579 | main.exit() |
| 1580 | |
| 1581 | def addSinglepointToMultipointIntent( |
| 1582 | self, |
| 1583 | ingressDevice, |
| 1584 | egressDeviceList, |
| 1585 | portIngress="", |
| 1586 | portEgressList=None, |
| 1587 | ethType="", |
| 1588 | ethSrc="", |
| 1589 | ethDst="", |
| 1590 | bandwidth="", |
| 1591 | lambdaAlloc=False, |
| 1592 | ipProto="", |
| 1593 | ipSrc="", |
| 1594 | ipDst="", |
| 1595 | tcpSrc="", |
| 1596 | tcpDst="", |
| 1597 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1598 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1599 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1600 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1601 | partial=False, |
| 1602 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1603 | """ |
| 1604 | Note: |
| 1605 | This function assumes the format of all egress devices |
| 1606 | is same. That is, all egress devices include port numbers |
| 1607 | with a "/" or all egress devices could specify device |
| 1608 | ids and port numbers seperately. |
| 1609 | Required: |
| 1610 | * EgressDeviceList: List of device ids of egress device |
| 1611 | ( Atleast 2 eress devices required in the list ) |
| 1612 | * ingressDevice: device id of ingress device |
| 1613 | Optional: |
| 1614 | * ethType: specify ethType |
| 1615 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1616 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1617 | * bandwidth: specify bandwidth capacity of link |
| 1618 | * lambdaAlloc: if True, intent will allocate lambda |
| 1619 | for the specified intent |
| 1620 | * ipProto: specify ip protocol |
| 1621 | * ipSrc: specify ip source address |
| 1622 | * ipDst: specify ip destination address |
| 1623 | * tcpSrc: specify tcp source port |
| 1624 | * tcpDst: specify tcp destination port |
| 1625 | * setEthSrc: action to Rewrite Source MAC Address |
| 1626 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1627 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1628 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1629 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1630 | Description: |
| 1631 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1632 | specifying device id's and optional fields |
| 1633 | Returns: |
| 1634 | A string of the intent id or None on error |
| 1635 | |
| 1636 | NOTE: This function may change depending on the |
| 1637 | options developers provide for singlepoint-to-multipoint |
| 1638 | intent via cli |
| 1639 | """ |
| 1640 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1641 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1642 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1643 | if ethType: |
| 1644 | cmd += " --ethType " + str( ethType ) |
| 1645 | if ethSrc: |
| 1646 | cmd += " --ethSrc " + str( ethSrc ) |
| 1647 | if ethDst: |
| 1648 | cmd += " --ethDst " + str( ethDst ) |
| 1649 | if bandwidth: |
| 1650 | cmd += " --bandwidth " + str( bandwidth ) |
| 1651 | if lambdaAlloc: |
| 1652 | cmd += " --lambda " |
| 1653 | if ipProto: |
| 1654 | cmd += " --ipProto " + str( ipProto ) |
| 1655 | if ipSrc: |
| 1656 | cmd += " --ipSrc " + str( ipSrc ) |
| 1657 | if ipDst: |
| 1658 | cmd += " --ipDst " + str( ipDst ) |
| 1659 | if tcpSrc: |
| 1660 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1661 | if tcpDst: |
| 1662 | cmd += " --tcpDst " + str( tcpDst ) |
| 1663 | if setEthSrc: |
| 1664 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1665 | if setEthDst: |
| 1666 | cmd += " --setEthDst " + str( setEthDst ) |
| 1667 | if vlanId: |
| 1668 | cmd += " -v " + str( vlanId ) |
| 1669 | if setVlan: |
| 1670 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1671 | if partial: |
| 1672 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1673 | if encap: |
| 1674 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1675 | |
| 1676 | # Check whether the user appended the port |
| 1677 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1678 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1679 | if "/" in ingressDevice: |
| 1680 | cmd += " " + str( ingressDevice ) |
| 1681 | else: |
| 1682 | if not portIngress: |
| 1683 | main.log.error( "You must specify " + |
| 1684 | "the Ingress port" ) |
| 1685 | return main.FALSE |
| 1686 | |
| 1687 | cmd += " " +\ |
| 1688 | str( ingressDevice ) + "/" +\ |
| 1689 | str( portIngress ) |
| 1690 | |
| 1691 | if portEgressList is None: |
| 1692 | for egressDevice in egressDeviceList: |
| 1693 | if "/" in egressDevice: |
| 1694 | cmd += " " + str( egressDevice ) |
| 1695 | else: |
| 1696 | main.log.error( "You must specify " + |
| 1697 | "the egress port" ) |
| 1698 | # TODO: perhaps more meaningful return |
| 1699 | return main.FALSE |
| 1700 | else: |
| 1701 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1702 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1703 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1704 | cmd += " " + \ |
| 1705 | str( egressDevice ) + "/" +\ |
| 1706 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1707 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1708 | main.log.error( "Device list and port list does not " + |
| 1709 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1710 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1711 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1712 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1713 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1714 | # If error, return error message |
| 1715 | if re.search( "Error", handle ): |
| 1716 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1717 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1718 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1719 | else: |
| 1720 | match = re.search('id=0x([\da-f]+),', handle) |
| 1721 | if match: |
| 1722 | return match.group()[3:-1] |
| 1723 | else: |
| 1724 | main.log.error( "Error, intent ID not found" ) |
| 1725 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1726 | except AssertionError: |
| 1727 | main.log.exception( "" ) |
| 1728 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1729 | except TypeError: |
| 1730 | main.log.exception( self.name + ": Object not as expected" ) |
| 1731 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1732 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1733 | main.log.error( self.name + ": EOF exception found" ) |
| 1734 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1735 | main.cleanup() |
| 1736 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1737 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1738 | main.log.exception( self.name + ": Uncaught exception!" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1739 | main.cleanup() |
| 1740 | main.exit() |
| 1741 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1742 | def addMplsIntent( |
| 1743 | self, |
| 1744 | ingressDevice, |
| 1745 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1746 | ingressPort="", |
| 1747 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1748 | ethType="", |
| 1749 | ethSrc="", |
| 1750 | ethDst="", |
| 1751 | bandwidth="", |
| 1752 | lambdaAlloc=False, |
| 1753 | ipProto="", |
| 1754 | ipSrc="", |
| 1755 | ipDst="", |
| 1756 | tcpSrc="", |
| 1757 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1758 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1759 | egressLabel="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1760 | priority=""): |
| 1761 | """ |
| 1762 | Required: |
| 1763 | * ingressDevice: device id of ingress device |
| 1764 | * egressDevice: device id of egress device |
| 1765 | Optional: |
| 1766 | * ethType: specify ethType |
| 1767 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1768 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1769 | * bandwidth: specify bandwidth capacity of link |
| 1770 | * lambdaAlloc: if True, intent will allocate lambda |
| 1771 | for the specified intent |
| 1772 | * ipProto: specify ip protocol |
| 1773 | * ipSrc: specify ip source address |
| 1774 | * ipDst: specify ip destination address |
| 1775 | * tcpSrc: specify tcp source port |
| 1776 | * tcpDst: specify tcp destination port |
| 1777 | * ingressLabel: Ingress MPLS label |
| 1778 | * egressLabel: Egress MPLS label |
| 1779 | Description: |
| 1780 | Adds MPLS intent by |
| 1781 | specifying device id's and optional fields |
| 1782 | Returns: |
| 1783 | A string of the intent id or None on error |
| 1784 | |
| 1785 | NOTE: This function may change depending on the |
| 1786 | options developers provide for MPLS |
| 1787 | intent via cli |
| 1788 | """ |
| 1789 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1790 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1791 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1792 | if ethType: |
| 1793 | cmd += " --ethType " + str( ethType ) |
| 1794 | if ethSrc: |
| 1795 | cmd += " --ethSrc " + str( ethSrc ) |
| 1796 | if ethDst: |
| 1797 | cmd += " --ethDst " + str( ethDst ) |
| 1798 | if bandwidth: |
| 1799 | cmd += " --bandwidth " + str( bandwidth ) |
| 1800 | if lambdaAlloc: |
| 1801 | cmd += " --lambda " |
| 1802 | if ipProto: |
| 1803 | cmd += " --ipProto " + str( ipProto ) |
| 1804 | if ipSrc: |
| 1805 | cmd += " --ipSrc " + str( ipSrc ) |
| 1806 | if ipDst: |
| 1807 | cmd += " --ipDst " + str( ipDst ) |
| 1808 | if tcpSrc: |
| 1809 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1810 | if tcpDst: |
| 1811 | cmd += " --tcpDst " + str( tcpDst ) |
| 1812 | if ingressLabel: |
| 1813 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1814 | if egressLabel: |
| 1815 | cmd += " --egressLabel " + str( egressLabel ) |
| 1816 | if priority: |
| 1817 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1818 | |
| 1819 | # Check whether the user appended the port |
| 1820 | # or provided it as an input |
| 1821 | if "/" in ingressDevice: |
| 1822 | cmd += " " + str( ingressDevice ) |
| 1823 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1824 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1825 | main.log.error( "You must specify the ingress port" ) |
| 1826 | return None |
| 1827 | |
| 1828 | cmd += " " + \ |
| 1829 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1830 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1831 | |
| 1832 | if "/" in egressDevice: |
| 1833 | cmd += " " + str( egressDevice ) |
| 1834 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1835 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1836 | main.log.error( "You must specify the egress port" ) |
| 1837 | return None |
| 1838 | |
| 1839 | cmd += " " +\ |
| 1840 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1841 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1842 | |
| 1843 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1844 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1845 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1846 | # If error, return error message |
| 1847 | if re.search( "Error", handle ): |
| 1848 | main.log.error( "Error in adding mpls intent" ) |
| 1849 | return None |
| 1850 | else: |
| 1851 | # TODO: print out all the options in this message? |
| 1852 | main.log.info( "MPLS intent installed between " + |
| 1853 | str( ingressDevice ) + " and " + |
| 1854 | str( egressDevice ) ) |
| 1855 | match = re.search('id=0x([\da-f]+),', handle) |
| 1856 | if match: |
| 1857 | return match.group()[3:-1] |
| 1858 | else: |
| 1859 | main.log.error( "Error, intent ID not found" ) |
| 1860 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1861 | except AssertionError: |
| 1862 | main.log.exception( "" ) |
| 1863 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1864 | except TypeError: |
| 1865 | main.log.exception( self.name + ": Object not as expected" ) |
| 1866 | return None |
| 1867 | except pexpect.EOF: |
| 1868 | main.log.error( self.name + ": EOF exception found" ) |
| 1869 | main.log.error( self.name + ": " + self.handle.before ) |
| 1870 | main.cleanup() |
| 1871 | main.exit() |
| 1872 | except Exception: |
| 1873 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1874 | main.cleanup() |
| 1875 | main.exit() |
| 1876 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1877 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1878 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1879 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1880 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1881 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1882 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1883 | -p or --purge: Purge the intent from the store after removal |
| 1884 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1885 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 1886 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1887 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1888 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1889 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1890 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1891 | if purge: |
| 1892 | cmdStr += " -p" |
| 1893 | if sync: |
| 1894 | cmdStr += " -s" |
| 1895 | |
| 1896 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1897 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1898 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1899 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1900 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1901 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1902 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1903 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1904 | # TODO: Should this be main.TRUE |
| 1905 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1906 | except AssertionError: |
| 1907 | main.log.exception( "" ) |
| 1908 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1909 | except TypeError: |
| 1910 | main.log.exception( self.name + ": Object not as expected" ) |
| 1911 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1912 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1913 | main.log.error( self.name + ": EOF exception found" ) |
| 1914 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1915 | main.cleanup() |
| 1916 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1917 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1918 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1919 | main.cleanup() |
| 1920 | main.exit() |
| 1921 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1922 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1923 | """ |
| 1924 | Description: |
| 1925 | Remove all the intents |
| 1926 | Optional args:- |
| 1927 | -s or --sync: Waits for the removal before returning |
| 1928 | -p or --purge: Purge the intent from the store after removal |
| 1929 | Returns: |
| 1930 | Returns main.TRUE if all intents are removed, otherwise returns |
| 1931 | main.FALSE; Returns None for exception |
| 1932 | """ |
| 1933 | try: |
| 1934 | cmdStr = "remove-intent" |
| 1935 | if purge: |
| 1936 | cmdStr += " -p" |
| 1937 | if sync: |
| 1938 | cmdStr += " -s" |
| 1939 | |
| 1940 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1941 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1942 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1943 | assert "Command not found:" not in handle, handle |
| 1944 | if re.search( "Error", handle ): |
| 1945 | main.log.error( "Error in removing intent" ) |
| 1946 | return main.FALSE |
| 1947 | else: |
| 1948 | return main.TRUE |
| 1949 | except AssertionError: |
| 1950 | main.log.exception( "" ) |
| 1951 | return None |
| 1952 | except TypeError: |
| 1953 | main.log.exception( self.name + ": Object not as expected" ) |
| 1954 | return None |
| 1955 | except pexpect.EOF: |
| 1956 | main.log.error( self.name + ": EOF exception found" ) |
| 1957 | main.log.error( self.name + ": " + self.handle.before ) |
| 1958 | main.cleanup() |
| 1959 | main.exit() |
| 1960 | except Exception: |
| 1961 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1962 | main.cleanup() |
| 1963 | main.exit() |
| 1964 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 1965 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1966 | """ |
| 1967 | Purges all WITHDRAWN Intents |
| 1968 | """ |
| 1969 | try: |
| 1970 | cmdStr = "purge-intents" |
| 1971 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1972 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1973 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1974 | if re.search( "Error", handle ): |
| 1975 | main.log.error( "Error in purging intents" ) |
| 1976 | return main.FALSE |
| 1977 | else: |
| 1978 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1979 | except AssertionError: |
| 1980 | main.log.exception( "" ) |
| 1981 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1982 | except TypeError: |
| 1983 | main.log.exception( self.name + ": Object not as expected" ) |
| 1984 | return None |
| 1985 | except pexpect.EOF: |
| 1986 | main.log.error( self.name + ": EOF exception found" ) |
| 1987 | main.log.error( self.name + ": " + self.handle.before ) |
| 1988 | main.cleanup() |
| 1989 | main.exit() |
| 1990 | except Exception: |
| 1991 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1992 | main.cleanup() |
| 1993 | main.exit() |
| 1994 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1995 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1996 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1997 | NOTE: This method should be used after installing application: |
| 1998 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1999 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2000 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2001 | Description: |
| 2002 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2003 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2004 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2005 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2006 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2007 | cmdStr += " -j" |
| 2008 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2009 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2010 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2011 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2012 | except AssertionError: |
| 2013 | main.log.exception( "" ) |
| 2014 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2015 | except TypeError: |
| 2016 | main.log.exception( self.name + ": Object not as expected" ) |
| 2017 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2018 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2019 | main.log.error( self.name + ": EOF exception found" ) |
| 2020 | main.log.error( self.name + ": " + self.handle.before ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2021 | main.cleanup() |
| 2022 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2023 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2024 | main.log.exception( self.name + ": Uncaught exception!" ) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2025 | main.cleanup() |
| 2026 | main.exit() |
| 2027 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2028 | def ipv4RouteNumber( self ): |
| 2029 | """ |
| 2030 | NOTE: This method should be used after installing application: |
| 2031 | onos-app-sdnip |
| 2032 | Description: |
| 2033 | Obtain the total IPv4 routes number in the system |
| 2034 | """ |
| 2035 | try: |
| 2036 | cmdStr = "routes -s -j" |
| 2037 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2038 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2039 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2040 | jsonResult = json.loads( handle ) |
| 2041 | return jsonResult['totalRoutes4'] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2042 | except AssertionError: |
| 2043 | main.log.exception( "" ) |
| 2044 | return None |
| 2045 | except ( TypeError, ValueError ): |
| 2046 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2047 | return None |
| 2048 | except pexpect.EOF: |
| 2049 | main.log.error( self.name + ": EOF exception found" ) |
| 2050 | main.log.error( self.name + ": " + self.handle.before ) |
| 2051 | main.cleanup() |
| 2052 | main.exit() |
| 2053 | except Exception: |
| 2054 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2055 | main.cleanup() |
| 2056 | main.exit() |
| 2057 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2058 | def intents( self, jsonFormat = True, summary = False, **intentargs): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2059 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2060 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2061 | Obtain intents from the ONOS cli. |
| 2062 | Optional: |
| 2063 | * jsonFormat: Enable output formatting in json, default to True |
| 2064 | * summary: Whether only output the intent summary, defaults to False |
| 2065 | * type: Only output a certain type of intent. This options is valid |
| 2066 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2067 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2068 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2069 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2070 | if summary: |
| 2071 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2072 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2073 | cmdStr += " -j" |
| 2074 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2075 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2076 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2077 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2078 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2079 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2080 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2081 | intentType = "" |
| 2082 | # IF we want the summary of a specific intent type |
| 2083 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2084 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2085 | if intentType in jsonResult.keys(): |
| 2086 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2087 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2088 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2089 | return handle |
| 2090 | else: |
| 2091 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2092 | except AssertionError: |
| 2093 | main.log.exception( "" ) |
| 2094 | return None |
| 2095 | except ( TypeError, ValueError ): |
| 2096 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2097 | return None |
| 2098 | except pexpect.EOF: |
| 2099 | main.log.error( self.name + ": EOF exception found" ) |
| 2100 | main.log.error( self.name + ": " + self.handle.before ) |
| 2101 | main.cleanup() |
| 2102 | main.exit() |
| 2103 | except Exception: |
| 2104 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2105 | main.cleanup() |
| 2106 | main.exit() |
| 2107 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2108 | def getIntentState(self, intentsId, intentsJson=None): |
| 2109 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2110 | Description: |
| 2111 | Gets intent state. Accepts a single intent ID (string type) or a |
| 2112 | list of intent IDs. |
| 2113 | Parameters: |
| 2114 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2115 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2116 | Returns: |
| 2117 | Returns the state (string type) of the ID if a single intent ID is |
| 2118 | accepted. |
| 2119 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2120 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2121 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2122 | """ |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2123 | try: |
| 2124 | state = "State is Undefined" |
| 2125 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2126 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2127 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2128 | rawJson = intentsJson |
| 2129 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2130 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2131 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2132 | if intentsId == intent[ 'id' ]: |
| 2133 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2134 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2135 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
| 2136 | " on the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2137 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2138 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2139 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2140 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2141 | stateDict = {} |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2142 | for intents in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2143 | if intentsId[ i ] == intents[ 'id' ]: |
| 2144 | stateDict[ 'state' ] = intents[ 'state' ] |
| 2145 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2146 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2147 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2148 | if len( intentsId ) != len( dictList ): |
| 2149 | main.log.info( "Cannot find some of the intent ID state" ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2150 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2151 | else: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2152 | main.log.info( "Invalid intents ID entry" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2153 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2154 | except ( TypeError, ValueError ): |
| 2155 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2156 | return None |
| 2157 | except pexpect.EOF: |
| 2158 | main.log.error( self.name + ": EOF exception found" ) |
| 2159 | main.log.error( self.name + ": " + self.handle.before ) |
| 2160 | main.cleanup() |
| 2161 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2162 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2163 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2164 | main.cleanup() |
| 2165 | main.exit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2166 | |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2167 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2168 | """ |
| 2169 | Description: |
| 2170 | Check intents state |
| 2171 | Required: |
| 2172 | intentsId - List of intents ID to be checked |
| 2173 | Optional: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2174 | expectedState - Check the expected state(s) of each intents |
| 2175 | state in the list. |
| 2176 | *NOTE: You can pass in a list of expected state, |
| 2177 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2178 | Return: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2179 | Returns main.TRUE only if all intent are the same as expected states |
| 2180 | , otherwise, returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2181 | """ |
| 2182 | try: |
| 2183 | # Generating a dictionary: intent id as a key and state as value |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2184 | returnValue = main.TRUE |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2185 | intentsDict = self.getIntentState( intentsId ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2186 | if len( intentsId ) != len( intentsDict ): |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2187 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2188 | "getting intents state" ) |
| 2189 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2190 | |
| 2191 | if isinstance( expectedState, types.StringType ): |
| 2192 | for intents in intentsDict: |
| 2193 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2194 | main.log.debug( self.name + " : Intent ID - " + |
| 2195 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2196 | " actual state = " + |
| 2197 | intents.get( 'state' ) |
| 2198 | + " does not equal expected state = " |
| 2199 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2200 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2201 | |
| 2202 | elif isinstance( expectedState, types.ListType ): |
| 2203 | for intents in intentsDict: |
| 2204 | if not any( state == intents.get( 'state' ) for state in |
| 2205 | expectedState ): |
| 2206 | main.log.debug( self.name + " : Intent ID - " + |
| 2207 | intents.get( 'id' ) + |
| 2208 | " actual state = " + |
| 2209 | intents.get( 'state' ) + |
| 2210 | " does not equal expected states = " |
| 2211 | + str( expectedState ) ) |
| 2212 | returnValue = main.FALSE |
| 2213 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2214 | if returnValue == main.TRUE: |
| 2215 | main.log.info( self.name + ": All " + |
| 2216 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2217 | " intents are in " + str( expectedState ) + |
| 2218 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2219 | return returnValue |
| 2220 | except TypeError: |
| 2221 | main.log.exception( self.name + ": Object not as expected" ) |
| 2222 | return None |
| 2223 | except pexpect.EOF: |
| 2224 | main.log.error( self.name + ": EOF exception found" ) |
| 2225 | main.log.error( self.name + ": " + self.handle.before ) |
| 2226 | main.cleanup() |
| 2227 | main.exit() |
| 2228 | except Exception: |
| 2229 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2230 | main.cleanup() |
| 2231 | main.exit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2232 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2233 | def compareIntent( self, intentDict ): |
| 2234 | """ |
| 2235 | Description: |
| 2236 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2237 | Return: |
| 2238 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2239 | Arguments: |
| 2240 | intentDict: a dictionary which maps intent ids to intent states |
| 2241 | """ |
| 2242 | try: |
| 2243 | intentsRaw = self.intents() |
| 2244 | intentsJson = json.loads( intentsRaw ) |
| 2245 | intentDictONOS = {} |
| 2246 | for intent in intentsJson: |
| 2247 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2248 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2249 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2250 | 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] | 2251 | str( len( intentDict ) ) + " expected and " + |
| 2252 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2253 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2254 | for intentID in intentDict.keys(): |
| 2255 | if not intentID in intentDictONOS.keys(): |
| 2256 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2257 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2258 | else: |
| 2259 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2260 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2261 | " expected state is " + intentDict[ intentID ] + |
| 2262 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2263 | returnValue = main.FALSE |
| 2264 | intentDictONOS.pop( intentID ) |
| 2265 | if len( intentDictONOS ) > 0: |
| 2266 | returnValue = main.FALSE |
| 2267 | for intentID in intentDictONOS.keys(): |
| 2268 | 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] | 2269 | if returnValue == main.TRUE: |
| 2270 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2271 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2272 | except KeyError: |
| 2273 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2274 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2275 | except ( TypeError, ValueError ): |
| 2276 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2277 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2278 | except pexpect.EOF: |
| 2279 | main.log.error( self.name + ": EOF exception found" ) |
| 2280 | main.log.error( self.name + ": " + self.handle.before ) |
| 2281 | main.cleanup() |
| 2282 | main.exit() |
| 2283 | except Exception: |
| 2284 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2285 | main.cleanup() |
| 2286 | main.exit() |
| 2287 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2288 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2289 | """ |
| 2290 | Description: |
| 2291 | Check the number of installed intents. |
| 2292 | Optional: |
| 2293 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2294 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2295 | Return: |
| 2296 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2297 | , otherwise, returns main.FALSE. |
| 2298 | """ |
| 2299 | |
| 2300 | try: |
| 2301 | cmd = "intents -s -j" |
| 2302 | |
| 2303 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2304 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2305 | if response == None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2306 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2307 | response = json.loads( response ) |
| 2308 | |
| 2309 | # get total and installed number, see if they are match |
| 2310 | allState = response.get( 'all' ) |
| 2311 | if allState.get('total') == allState.get('installed'): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2312 | 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] | 2313 | return main.TRUE |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2314 | 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] | 2315 | return main.FALSE |
| 2316 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2317 | except ( TypeError, ValueError ): |
| 2318 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2319 | return None |
| 2320 | except pexpect.EOF: |
| 2321 | main.log.error( self.name + ": EOF exception found" ) |
| 2322 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2323 | if noExit: |
| 2324 | return main.FALSE |
| 2325 | else: |
| 2326 | main.cleanup() |
| 2327 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2328 | except Exception: |
| 2329 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2330 | if noExit: |
| 2331 | return main.FALSE |
| 2332 | else: |
| 2333 | main.cleanup() |
| 2334 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2335 | except pexpect.TIMEOUT: |
| 2336 | main.log.error( self.name + ": ONOS timeout" ) |
| 2337 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2338 | |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2339 | def flows( self, state="", jsonFormat=True, timeout=60, noExit=False, noCore=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2340 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2341 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2342 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2343 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2344 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2345 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2346 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2347 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2348 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2349 | if jsonFormat: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2350 | cmdStr += " -j " |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2351 | if noCore: |
| 2352 | cmdStr += " -n " |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2353 | cmdStr += state |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2354 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2355 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2356 | assert "Command not found:" not in handle, handle |
| 2357 | if re.search( "Error:", handle ): |
| 2358 | main.log.error( self.name + ": flows() response: " + |
| 2359 | str( handle ) ) |
| 2360 | return handle |
| 2361 | except AssertionError: |
| 2362 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2363 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2364 | except TypeError: |
| 2365 | main.log.exception( self.name + ": Object not as expected" ) |
| 2366 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2367 | except pexpect.TIMEOUT: |
| 2368 | main.log.error( self.name + ": ONOS timeout" ) |
| 2369 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2370 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2371 | main.log.error( self.name + ": EOF exception found" ) |
| 2372 | main.log.error( self.name + ": " + self.handle.before ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2373 | main.cleanup() |
| 2374 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2375 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2376 | main.log.exception( self.name + ": Uncaught exception!" ) |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2377 | main.cleanup() |
| 2378 | main.exit() |
| 2379 | |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2380 | def checkFlowCount(self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2381 | count = self.getTotalFlowsNum( timeout=timeout ) |
| 2382 | count = int (count) if count else 0 |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2383 | return count if (count > min) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2384 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2385 | def checkFlowsState( self, isPENDING=True, timeout=60,noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2386 | """ |
| 2387 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2388 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2389 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2390 | if the count of those states is 0, which means all current flows |
| 2391 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2392 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2393 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2394 | Return: |
| 2395 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2396 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2397 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2398 | """ |
| 2399 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2400 | states = ["PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED"] |
| 2401 | checkedStates = [] |
| 2402 | statesCount = [0, 0, 0, 0] |
| 2403 | for s in states: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2404 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2405 | if rawFlows: |
| 2406 | # if we didn't get flows or flows function return None, we should return |
| 2407 | # main.Flase |
| 2408 | checkedStates.append( json.loads( rawFlows ) ) |
| 2409 | else: |
| 2410 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2411 | for i in range( len( states ) ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2412 | for c in checkedStates[i]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2413 | try: |
| 2414 | statesCount[i] += int( c.get( "flowCount" ) ) |
| 2415 | except TypeError: |
| 2416 | main.log.exception( "Json object not as expected" ) |
| 2417 | main.log.info( states[i] + " flows: " + str( statesCount[i] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2418 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2419 | # We want to count PENDING_ADD if isPENDING is true |
| 2420 | if isPENDING: |
| 2421 | if statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2422 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2423 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2424 | if statesCount[0] + statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2425 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2426 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2427 | except ( TypeError, ValueError ): |
| 2428 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2429 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2430 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2431 | except AssertionError: |
| 2432 | main.log.exception( "" ) |
| 2433 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2434 | except pexpect.EOF: |
| 2435 | main.log.error( self.name + ": EOF exception found" ) |
| 2436 | main.log.error( self.name + ": " + self.handle.before ) |
| 2437 | main.cleanup() |
| 2438 | main.exit() |
| 2439 | except Exception: |
| 2440 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2441 | main.cleanup() |
| 2442 | main.exit() |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2443 | except pexpect.TIMEOUT: |
| 2444 | main.log.error( self.name + ": ONOS timeout" ) |
| 2445 | return None |
| 2446 | |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2447 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2448 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2449 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2450 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2451 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2452 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2453 | a specific point-to-point intent definition |
| 2454 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2455 | * ingress: specify source dpid |
| 2456 | * egress: specify destination dpid |
| 2457 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2458 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2459 | * offset: the keyOffset is where the next batch of intents |
| 2460 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2461 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2462 | * getResponse: If set to True, function will return ONOS response. |
| 2463 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2464 | Returns: If failed to push test intents, it will returen None, |
| 2465 | if successful, return true. |
| 2466 | Timeout expection will return None, |
| 2467 | TypeError will return false |
| 2468 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2469 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2470 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2471 | if background: |
| 2472 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2473 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2474 | back = "" |
| 2475 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2476 | ingress, |
| 2477 | egress, |
| 2478 | batchSize, |
| 2479 | offset, |
| 2480 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2481 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2482 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2483 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2484 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2485 | if getResponse: |
| 2486 | return response |
| 2487 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2488 | # TODO: We should handle if there is failure in installation |
| 2489 | return main.TRUE |
| 2490 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2491 | except AssertionError: |
| 2492 | main.log.exception( "" ) |
| 2493 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2494 | except pexpect.TIMEOUT: |
| 2495 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2496 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2497 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2498 | main.log.error( self.name + ": EOF exception found" ) |
| 2499 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2500 | main.cleanup() |
| 2501 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2502 | except TypeError: |
| 2503 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2504 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2505 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2506 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2507 | main.cleanup() |
| 2508 | main.exit() |
| 2509 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2510 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2511 | """ |
| 2512 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2513 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2514 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2515 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2516 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2517 | """ |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2518 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2519 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2520 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2521 | cmd = "flows -c added" |
| 2522 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2523 | if rawFlows: |
| 2524 | rawFlows = rawFlows.split("\n") |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2525 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2526 | for l in rawFlows: |
| 2527 | totalFlows += int(l.split("Count=")[1]) |
| 2528 | else: |
| 2529 | main.log.error("Response not as expected!") |
| 2530 | return None |
| 2531 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2532 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2533 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2534 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2535 | return None |
| 2536 | except pexpect.EOF: |
| 2537 | main.log.error( self.name + ": EOF exception found" ) |
| 2538 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2539 | if not noExit: |
| 2540 | main.cleanup() |
| 2541 | main.exit() |
| 2542 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2543 | except Exception: |
| 2544 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2545 | if not noExit: |
| 2546 | main.cleanup() |
| 2547 | main.exit() |
| 2548 | return None |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2549 | except pexpect.TIMEOUT: |
| 2550 | main.log.error( self.name + ": ONOS timeout" ) |
| 2551 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2552 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2553 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2554 | """ |
| 2555 | Description: |
| 2556 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2557 | Optional: |
| 2558 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2559 | Return: |
| 2560 | The number of intents |
| 2561 | """ |
| 2562 | try: |
| 2563 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2564 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2565 | if response == None: |
| 2566 | return -1 |
| 2567 | response = json.loads( response ) |
| 2568 | return int( response.get("intents") ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2569 | except ( TypeError, ValueError ): |
| 2570 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2571 | return None |
| 2572 | except pexpect.EOF: |
| 2573 | main.log.error( self.name + ": EOF exception found" ) |
| 2574 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2575 | if noExit: |
| 2576 | return -1 |
| 2577 | else: |
| 2578 | main.cleanup() |
| 2579 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2580 | except Exception: |
| 2581 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2582 | if noExit: |
| 2583 | return -1 |
| 2584 | else: |
| 2585 | main.cleanup() |
| 2586 | main.exit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2587 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2588 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2589 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2590 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2591 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2592 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2593 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2594 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2595 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2596 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2597 | cmdStr += " -j" |
| 2598 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2599 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2600 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2601 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2602 | except AssertionError: |
| 2603 | main.log.exception( "" ) |
| 2604 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2605 | except TypeError: |
| 2606 | main.log.exception( self.name + ": Object not as expected" ) |
| 2607 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2608 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2609 | main.log.error( self.name + ": EOF exception found" ) |
| 2610 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2611 | main.cleanup() |
| 2612 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2613 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2614 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2615 | main.cleanup() |
| 2616 | main.exit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2617 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2618 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2619 | """ |
| 2620 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2621 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2622 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2623 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2624 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2625 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2626 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2627 | cmdStr += " -j" |
| 2628 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2629 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2630 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2631 | if handle: |
| 2632 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2633 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2634 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2635 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2636 | else: |
| 2637 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2638 | except AssertionError: |
| 2639 | main.log.exception( "" ) |
| 2640 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2641 | except TypeError: |
| 2642 | main.log.exception( self.name + ": Object not as expected" ) |
| 2643 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2644 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2645 | main.log.error( self.name + ": EOF exception found" ) |
| 2646 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2647 | main.cleanup() |
| 2648 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2649 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2650 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2651 | main.cleanup() |
| 2652 | main.exit() |
| 2653 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2654 | # Wrapper functions **************** |
| 2655 | # Wrapper functions use existing driver |
| 2656 | # functions and extends their use case. |
| 2657 | # For example, we may use the output of |
| 2658 | # a normal driver function, and parse it |
| 2659 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2660 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2661 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2662 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2663 | Description: |
| 2664 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2665 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2666 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2667 | # Obtain output of intents function |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2668 | intentsStr = self.intents(jsonFormat=True) |
| 2669 | # Convert to a dictionary |
| 2670 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2671 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2672 | for intent in intents: |
| 2673 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2674 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2675 | except TypeError: |
| 2676 | main.log.exception( self.name + ": Object not as expected" ) |
| 2677 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2678 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2679 | main.log.error( self.name + ": EOF exception found" ) |
| 2680 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2681 | main.cleanup() |
| 2682 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2683 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2684 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2685 | main.cleanup() |
| 2686 | main.exit() |
| 2687 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2688 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2689 | """ |
| 2690 | Determine the number of flow rules for the given device id that are |
| 2691 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2692 | Params: |
| 2693 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2694 | """ |
| 2695 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2696 | if core: |
| 2697 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2698 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2699 | else: |
| 2700 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2701 | "grep 'state=ADDED' | wc -l" |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2702 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2703 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2704 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2705 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2706 | except AssertionError: |
| 2707 | main.log.exception( "" ) |
| 2708 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2709 | except pexpect.EOF: |
| 2710 | main.log.error( self.name + ": EOF exception found" ) |
| 2711 | main.log.error( self.name + ": " + self.handle.before ) |
| 2712 | main.cleanup() |
| 2713 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2714 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2715 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2716 | main.cleanup() |
| 2717 | main.exit() |
| 2718 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2719 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2720 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2721 | Use 'devices' function to obtain list of all devices |
| 2722 | and parse the result to obtain a list of all device |
| 2723 | id's. Returns this list. Returns empty list if no |
| 2724 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2725 | List is ordered sequentially |
| 2726 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2727 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2728 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2729 | the ids. By obtaining the list of device ids on the fly, |
| 2730 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2731 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2732 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2733 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2734 | devicesStr = self.devices( jsonFormat=False ) |
| 2735 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2736 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2737 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2738 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2739 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2740 | |
| 2741 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2742 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2743 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2744 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2745 | # Split list further into arguments before and after string |
| 2746 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2747 | # append to idList |
| 2748 | for arg in tempList: |
| 2749 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 2750 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2751 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2752 | except TypeError: |
| 2753 | main.log.exception( self.name + ": Object not as expected" ) |
| 2754 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2755 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2756 | main.log.error( self.name + ": EOF exception found" ) |
| 2757 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2758 | main.cleanup() |
| 2759 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2760 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2761 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2762 | main.cleanup() |
| 2763 | main.exit() |
| 2764 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2765 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2766 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2767 | Uses 'nodes' function to obtain list of all nodes |
| 2768 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2769 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2770 | Returns: |
| 2771 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2772 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2773 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2774 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2775 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2776 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 2777 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2778 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2779 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2780 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2781 | nodesJson = json.loads( nodesStr ) |
| 2782 | idList = [ node.get('id') for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2783 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2784 | except ( TypeError, ValueError ): |
| 2785 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2786 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2787 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2788 | main.log.error( self.name + ": EOF exception found" ) |
| 2789 | main.log.error( self.name + ": " + self.handle.before ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2790 | main.cleanup() |
| 2791 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2792 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2793 | main.log.exception( self.name + ": Uncaught exception!" ) |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2794 | main.cleanup() |
| 2795 | main.exit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2796 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2797 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2798 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2799 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 2800 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2801 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2802 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2803 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2804 | return None |
| 2805 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2806 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2807 | rawDevices = self.devices() |
| 2808 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2809 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2810 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2811 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 2812 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2813 | return device |
| 2814 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2815 | except ( TypeError, ValueError ): |
| 2816 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2817 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2818 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2819 | main.log.error( self.name + ": EOF exception found" ) |
| 2820 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2821 | main.cleanup() |
| 2822 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2823 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2824 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2825 | main.cleanup() |
| 2826 | main.exit() |
| 2827 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2828 | def getTopology( self, topologyOutput ): |
| 2829 | """ |
| 2830 | Definition: |
| 2831 | Loads a json topology output |
| 2832 | Return: |
| 2833 | topology = current ONOS topology |
| 2834 | """ |
| 2835 | import json |
| 2836 | try: |
| 2837 | # either onos:topology or 'topology' will work in CLI |
| 2838 | topology = json.loads(topologyOutput) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2839 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2840 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2841 | except ( TypeError, ValueError ): |
| 2842 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 2843 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2844 | except pexpect.EOF: |
| 2845 | main.log.error( self.name + ": EOF exception found" ) |
| 2846 | main.log.error( self.name + ": " + self.handle.before ) |
| 2847 | main.cleanup() |
| 2848 | main.exit() |
| 2849 | except Exception: |
| 2850 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2851 | main.cleanup() |
| 2852 | main.exit() |
| 2853 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2854 | def checkStatus(self, numoswitch, numolink, numoctrl = -1, logLevel="info"): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2855 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2856 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2857 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2858 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2859 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2860 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2861 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2862 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2863 | logLevel = level to log to. |
| 2864 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2865 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2866 | Returns: main.TRUE if the number of switches and links are correct, |
| 2867 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2868 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2869 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2870 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2871 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 2872 | summary = self.summary() |
| 2873 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 2874 | except ( TypeError, ValueError ): |
| 2875 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 2876 | return main.ERROR |
| 2877 | try: |
| 2878 | topology = self.getTopology( self.topology() ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2879 | if topology == {} or topology == None or summary == {} or summary == None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2880 | return main.ERROR |
| 2881 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2882 | # Is the number of switches is what we expected |
| 2883 | devices = topology.get( 'devices', False ) |
| 2884 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2885 | nodes = summary.get( 'nodes', False ) |
| 2886 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2887 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2888 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2889 | # Is the number of links is what we expected |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2890 | linkCheck = ( int( links ) == int( numolink ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2891 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 2892 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2893 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2894 | output = output + "The number of links and switches match "\ |
| 2895 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2896 | result = main.TRUE |
| 2897 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2898 | output = output + \ |
| 2899 | "The number of links and switches does not match " + \ |
| 2900 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2901 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2902 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 2903 | output = output + " (%i expected) " % int( numoswitch ) |
| 2904 | output = output + "and %i links " % int( links ) |
| 2905 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 2906 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2907 | output = output + "and %i controllers " % int( nodes ) |
| 2908 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2909 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2910 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2911 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2912 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2913 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2914 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2915 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2916 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2917 | main.log.error( self.name + ": EOF exception found" ) |
| 2918 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2919 | main.cleanup() |
| 2920 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2921 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2922 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2923 | main.cleanup() |
| 2924 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2925 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2926 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2927 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2928 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2929 | deviceId must be the id of a device as seen in the onos devices command |
| 2930 | 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] | 2931 | role must be either master, standby, or none |
| 2932 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2933 | Returns: |
| 2934 | main.TRUE or main.FALSE based on argument verification and |
| 2935 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2936 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2937 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2938 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2939 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2940 | cmdStr = "device-role " +\ |
| 2941 | str( deviceId ) + " " +\ |
| 2942 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2943 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2944 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2945 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2946 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2947 | if re.search( "Error", handle ): |
| 2948 | # end color output to escape any colours |
| 2949 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2950 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2951 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2952 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2953 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2954 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2955 | main.log.error( "Invalid 'role' given to device_role(). " + |
| 2956 | "Value was '" + str(role) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2957 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2958 | except AssertionError: |
| 2959 | main.log.exception( "" ) |
| 2960 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2961 | except TypeError: |
| 2962 | main.log.exception( self.name + ": Object not as expected" ) |
| 2963 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2964 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2965 | main.log.error( self.name + ": EOF exception found" ) |
| 2966 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2967 | main.cleanup() |
| 2968 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2969 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2970 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2971 | main.cleanup() |
| 2972 | main.exit() |
| 2973 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2974 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2975 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2976 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2977 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2978 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2979 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2980 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2981 | cmdStr = "clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2982 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2983 | cmdStr += " -j" |
| 2984 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2985 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2986 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2987 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2988 | except AssertionError: |
| 2989 | main.log.exception( "" ) |
| 2990 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2991 | except TypeError: |
| 2992 | main.log.exception( self.name + ": Object not as expected" ) |
| 2993 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2994 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2995 | main.log.error( self.name + ": EOF exception found" ) |
| 2996 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2997 | main.cleanup() |
| 2998 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2999 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3000 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3001 | main.cleanup() |
| 3002 | main.exit() |
| 3003 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3004 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3005 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3006 | CLI command to get the current leader for the Election test application |
| 3007 | NOTE: Requires installation of the onos-app-election feature |
| 3008 | Returns: Node IP of the leader if one exists |
| 3009 | None if none exists |
| 3010 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3011 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3012 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3013 | cmdStr = "election-test-leader" |
| 3014 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3015 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3016 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3017 | # Leader |
| 3018 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3019 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3020 | nodeSearch = re.search( leaderPattern, response ) |
| 3021 | if nodeSearch: |
| 3022 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3023 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3024 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3025 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3026 | # no leader |
| 3027 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3028 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3029 | nullSearch = re.search( nullPattern, response ) |
| 3030 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3031 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3032 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3033 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3034 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3035 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 3036 | ": " + "unexpected response" ) |
| 3037 | main.log.error( repr( response ) ) |
| 3038 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3039 | except AssertionError: |
| 3040 | main.log.exception( "" ) |
| 3041 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3042 | except TypeError: |
| 3043 | main.log.exception( self.name + ": Object not as expected" ) |
| 3044 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3045 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3046 | main.log.error( self.name + ": EOF exception found" ) |
| 3047 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3048 | main.cleanup() |
| 3049 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3050 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3051 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3052 | main.cleanup() |
| 3053 | main.exit() |
| 3054 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3055 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3056 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3057 | CLI command to run for leadership of the Election test application. |
| 3058 | NOTE: Requires installation of the onos-app-election feature |
| 3059 | Returns: Main.TRUE on success |
| 3060 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3061 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3062 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3063 | cmdStr = "election-test-run" |
| 3064 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3065 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3066 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3067 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3068 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3069 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3070 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3071 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3072 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3073 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3074 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3075 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3076 | main.log.error( "Error in electionTestRun on " + self.name + |
| 3077 | ": " + "unexpected response" ) |
| 3078 | main.log.error( repr( response ) ) |
| 3079 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3080 | except AssertionError: |
| 3081 | main.log.exception( "" ) |
| 3082 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3083 | except TypeError: |
| 3084 | main.log.exception( self.name + ": Object not as expected" ) |
| 3085 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3086 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3087 | main.log.error( self.name + ": EOF exception found" ) |
| 3088 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3089 | main.cleanup() |
| 3090 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3091 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3092 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3093 | main.cleanup() |
| 3094 | main.exit() |
| 3095 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3096 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3097 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3098 | * CLI command to withdraw the local node from leadership election for |
| 3099 | * the Election test application. |
| 3100 | #NOTE: Requires installation of the onos-app-election feature |
| 3101 | Returns: Main.TRUE on success |
| 3102 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3103 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3104 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3105 | cmdStr = "election-test-withdraw" |
| 3106 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3107 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3108 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3109 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3110 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3111 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3112 | if re.search( successPattern, response ): |
| 3113 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3114 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3115 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3116 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3117 | main.log.error( "Error in electionTestWithdraw on " + |
| 3118 | self.name + ": " + "unexpected response" ) |
| 3119 | main.log.error( repr( response ) ) |
| 3120 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3121 | except AssertionError: |
| 3122 | main.log.exception( "" ) |
| 3123 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3124 | except TypeError: |
| 3125 | main.log.exception( self.name + ": Object not as expected" ) |
| 3126 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3127 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3128 | main.log.error( self.name + ": EOF exception found" ) |
| 3129 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3130 | main.cleanup() |
| 3131 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3132 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3133 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3134 | main.cleanup() |
| 3135 | main.exit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3136 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3137 | def getDevicePortsEnabledCount( self, dpid ): |
| 3138 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3139 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3140 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3141 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3142 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3143 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 3144 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3145 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3146 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3147 | if re.search( "No such device", output ): |
| 3148 | main.log.error( "Error in getting ports" ) |
| 3149 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3150 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3151 | except AssertionError: |
| 3152 | main.log.exception( "" ) |
| 3153 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3154 | except TypeError: |
| 3155 | main.log.exception( self.name + ": Object not as expected" ) |
| 3156 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3157 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3158 | main.log.error( self.name + ": EOF exception found" ) |
| 3159 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3160 | main.cleanup() |
| 3161 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3162 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3163 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3164 | main.cleanup() |
| 3165 | main.exit() |
| 3166 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3167 | def getDeviceLinksActiveCount( self, dpid ): |
| 3168 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3169 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3170 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3171 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3172 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3173 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3174 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3175 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3176 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3177 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3178 | main.log.error( "Error in getting ports " ) |
| 3179 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3180 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3181 | except AssertionError: |
| 3182 | main.log.exception( "" ) |
| 3183 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3184 | except TypeError: |
| 3185 | main.log.exception( self.name + ": Object not as expected" ) |
| 3186 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3187 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3188 | main.log.error( self.name + ": EOF exception found" ) |
| 3189 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3190 | main.cleanup() |
| 3191 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3192 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3193 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3194 | main.cleanup() |
| 3195 | main.exit() |
| 3196 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3197 | def getAllIntentIds( self ): |
| 3198 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3199 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3200 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3201 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3202 | cmdStr = "onos:intents | grep id=" |
| 3203 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3204 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3205 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3206 | if re.search( "Error", output ): |
| 3207 | main.log.error( "Error in getting ports" ) |
| 3208 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3209 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3210 | except AssertionError: |
| 3211 | main.log.exception( "" ) |
| 3212 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3213 | except TypeError: |
| 3214 | main.log.exception( self.name + ": Object not as expected" ) |
| 3215 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3216 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3217 | main.log.error( self.name + ": EOF exception found" ) |
| 3218 | main.log.error( self.name + ": " + self.handle.before ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3219 | main.cleanup() |
| 3220 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3221 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3222 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3223 | main.cleanup() |
| 3224 | main.exit() |
| 3225 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3226 | def intentSummary( self ): |
| 3227 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3228 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3229 | """ |
| 3230 | try: |
| 3231 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3232 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3233 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3234 | states.append( intent.get( 'state', None ) ) |
| 3235 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3236 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3237 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3238 | except ( TypeError, ValueError ): |
| 3239 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3240 | return None |
| 3241 | except pexpect.EOF: |
| 3242 | main.log.error( self.name + ": EOF exception found" ) |
| 3243 | main.log.error( self.name + ": " + self.handle.before ) |
| 3244 | main.cleanup() |
| 3245 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3246 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3247 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3248 | main.cleanup() |
| 3249 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3250 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3251 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3252 | """ |
| 3253 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3254 | Optional argument: |
| 3255 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3256 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3257 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3258 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3259 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3260 | cmdStr += " -j" |
| 3261 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3262 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3263 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3264 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3265 | except AssertionError: |
| 3266 | main.log.exception( "" ) |
| 3267 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3268 | except TypeError: |
| 3269 | main.log.exception( self.name + ": Object not as expected" ) |
| 3270 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3271 | except pexpect.EOF: |
| 3272 | main.log.error( self.name + ": EOF exception found" ) |
| 3273 | main.log.error( self.name + ": " + self.handle.before ) |
| 3274 | main.cleanup() |
| 3275 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3276 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3277 | main.log.exception( self.name + ": Uncaught exception!" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3278 | main.cleanup() |
| 3279 | main.exit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3280 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3281 | def leaderCandidates( self, jsonFormat=True ): |
| 3282 | """ |
| 3283 | Returns the output of the leaders -c command. |
| 3284 | Optional argument: |
| 3285 | * jsonFormat - boolean indicating if you want output in json |
| 3286 | """ |
| 3287 | try: |
| 3288 | cmdStr = "onos:leaders -c" |
| 3289 | if jsonFormat: |
| 3290 | cmdStr += " -j" |
| 3291 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3292 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3293 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3294 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3295 | except AssertionError: |
| 3296 | main.log.exception( "" ) |
| 3297 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3298 | except TypeError: |
| 3299 | main.log.exception( self.name + ": Object not as expected" ) |
| 3300 | return None |
| 3301 | except pexpect.EOF: |
| 3302 | main.log.error( self.name + ": EOF exception found" ) |
| 3303 | main.log.error( self.name + ": " + self.handle.before ) |
| 3304 | main.cleanup() |
| 3305 | main.exit() |
| 3306 | except Exception: |
| 3307 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3308 | main.cleanup() |
| 3309 | main.exit() |
| 3310 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3311 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3312 | """ |
| 3313 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
| 3314 | topic parameter and an empty list if the topic doesn't exist |
| 3315 | If no leader is elected leader in the returned list will be "none" |
| 3316 | Returns None if there is a type error processing the json object |
| 3317 | """ |
| 3318 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3319 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3320 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3321 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3322 | assert "Command not found:" not in rawOutput, rawOutput |
| 3323 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3324 | results = [] |
| 3325 | for dict in output: |
| 3326 | if dict["topic"] == topic: |
| 3327 | leader = dict["leader"] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3328 | candidates = re.split( ", ", dict["candidates"][1:-1] ) |
| 3329 | results.append( leader ) |
| 3330 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3331 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3332 | except AssertionError: |
| 3333 | main.log.exception( "" ) |
| 3334 | return None |
| 3335 | except ( TypeError, ValueError ): |
| 3336 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 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() |
| 3343 | except Exception: |
| 3344 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3345 | main.cleanup() |
| 3346 | main.exit() |
| 3347 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3348 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3349 | """ |
| 3350 | Returns the output of the intent Pending map. |
| 3351 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3352 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3353 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3354 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3355 | cmdStr += " -j" |
| 3356 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3357 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3358 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3359 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3360 | except AssertionError: |
| 3361 | main.log.exception( "" ) |
| 3362 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3363 | except TypeError: |
| 3364 | main.log.exception( self.name + ": Object not as expected" ) |
| 3365 | return None |
| 3366 | except pexpect.EOF: |
| 3367 | main.log.error( self.name + ": EOF exception found" ) |
| 3368 | main.log.error( self.name + ": " + self.handle.before ) |
| 3369 | main.cleanup() |
| 3370 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3371 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3372 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3373 | main.cleanup() |
| 3374 | main.exit() |
| 3375 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3376 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3377 | """ |
| 3378 | Returns the output of the raft partitions command for ONOS. |
| 3379 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3380 | # Sample JSON |
| 3381 | # { |
| 3382 | # "leader": "tcp://10.128.30.11:7238", |
| 3383 | # "members": [ |
| 3384 | # "tcp://10.128.30.11:7238", |
| 3385 | # "tcp://10.128.30.17:7238", |
| 3386 | # "tcp://10.128.30.13:7238", |
| 3387 | # ], |
| 3388 | # "name": "p1", |
| 3389 | # "term": 3 |
| 3390 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3391 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3392 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3393 | if candidates: |
| 3394 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3395 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3396 | cmdStr += " -j" |
| 3397 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3398 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3399 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3400 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3401 | except AssertionError: |
| 3402 | main.log.exception( "" ) |
| 3403 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3404 | except TypeError: |
| 3405 | main.log.exception( self.name + ": Object not as expected" ) |
| 3406 | return None |
| 3407 | except pexpect.EOF: |
| 3408 | main.log.error( self.name + ": EOF exception found" ) |
| 3409 | main.log.error( self.name + ": " + self.handle.before ) |
| 3410 | main.cleanup() |
| 3411 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3412 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3413 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3414 | main.cleanup() |
| 3415 | main.exit() |
| 3416 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3417 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3418 | """ |
| 3419 | Returns the output of the apps command for ONOS. This command lists |
| 3420 | information about installed ONOS applications |
| 3421 | """ |
| 3422 | # Sample JSON object |
| 3423 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
| 3424 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3425 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
| 3426 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
| 3427 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3428 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3429 | if summary: |
| 3430 | cmdStr += " -s" |
| 3431 | if active: |
| 3432 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3433 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3434 | cmdStr += " -j" |
| 3435 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3436 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3437 | assert "Command not found:" not in output, output |
| 3438 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3439 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3440 | # FIXME: look at specific exceptions/Errors |
| 3441 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3442 | main.log.exception( "Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3443 | return None |
| 3444 | except TypeError: |
| 3445 | main.log.exception( self.name + ": Object not as expected" ) |
| 3446 | return None |
| 3447 | except pexpect.EOF: |
| 3448 | main.log.error( self.name + ": EOF exception found" ) |
| 3449 | main.log.error( self.name + ": " + self.handle.before ) |
| 3450 | main.cleanup() |
| 3451 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3452 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3453 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3454 | main.cleanup() |
| 3455 | main.exit() |
| 3456 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3457 | def appStatus( self, appName ): |
| 3458 | """ |
| 3459 | Uses the onos:apps cli command to return the status of an application. |
| 3460 | Returns: |
| 3461 | "ACTIVE" - If app is installed and activated |
| 3462 | "INSTALLED" - If app is installed and deactivated |
| 3463 | "UNINSTALLED" - If app is not installed |
| 3464 | None - on error |
| 3465 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3466 | try: |
| 3467 | if not isinstance( appName, types.StringType ): |
| 3468 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3469 | " a string" ) |
| 3470 | return None |
| 3471 | output = self.apps( jsonFormat=True ) |
| 3472 | appsJson = json.loads( output ) |
| 3473 | state = None |
| 3474 | for app in appsJson: |
| 3475 | if appName == app.get('name'): |
| 3476 | state = app.get('state') |
| 3477 | break |
| 3478 | if state == "ACTIVE" or state == "INSTALLED": |
| 3479 | return state |
| 3480 | elif state is None: |
| 3481 | return "UNINSTALLED" |
| 3482 | elif state: |
| 3483 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3484 | str( state ) ) |
| 3485 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3486 | except ( TypeError, ValueError ): |
| 3487 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3488 | return None |
| 3489 | except pexpect.EOF: |
| 3490 | main.log.error( self.name + ": EOF exception found" ) |
| 3491 | main.log.error( self.name + ": " + self.handle.before ) |
| 3492 | main.cleanup() |
| 3493 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3494 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3495 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3496 | main.cleanup() |
| 3497 | main.exit() |
| 3498 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3499 | def app( self, appName, option ): |
| 3500 | """ |
| 3501 | Interacts with the app command for ONOS. This command manages |
| 3502 | application inventory. |
| 3503 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3504 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3505 | # Validate argument types |
| 3506 | valid = True |
| 3507 | if not isinstance( appName, types.StringType ): |
| 3508 | main.log.error( self.name + ".app(): appName must be a " + |
| 3509 | "string" ) |
| 3510 | valid = False |
| 3511 | if not isinstance( option, types.StringType ): |
| 3512 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3513 | valid = False |
| 3514 | if not valid: |
| 3515 | return main.FALSE |
| 3516 | # Validate Option |
| 3517 | option = option.lower() |
| 3518 | # NOTE: Install may become a valid option |
| 3519 | if option == "activate": |
| 3520 | pass |
| 3521 | elif option == "deactivate": |
| 3522 | pass |
| 3523 | elif option == "uninstall": |
| 3524 | pass |
| 3525 | else: |
| 3526 | # Invalid option |
| 3527 | main.log.error( "The ONOS app command argument only takes " + |
| 3528 | "the values: (activate|deactivate|uninstall)" + |
| 3529 | "; was given '" + option + "'") |
| 3530 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3531 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3532 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3533 | assert output is not None, "Error in sendline" |
| 3534 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3535 | if "Error executing command" in output: |
| 3536 | main.log.error( "Error in processing onos:app command: " + |
| 3537 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3538 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3539 | elif "No such application" in output: |
| 3540 | main.log.error( "The application '" + appName + |
| 3541 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3542 | return main.FALSE |
| 3543 | elif "Command not found:" in output: |
| 3544 | main.log.error( "Error in processing onos:app command: " + |
| 3545 | str( output ) ) |
| 3546 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3547 | elif "Unsupported command:" in output: |
| 3548 | main.log.error( "Incorrect command given to 'app': " + |
| 3549 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3550 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3551 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3552 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3553 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3554 | except AssertionError: |
| 3555 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3556 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3557 | except TypeError: |
| 3558 | main.log.exception( self.name + ": Object not as expected" ) |
| 3559 | return main.ERROR |
| 3560 | except pexpect.EOF: |
| 3561 | main.log.error( self.name + ": EOF exception found" ) |
| 3562 | main.log.error( self.name + ": " + self.handle.before ) |
| 3563 | main.cleanup() |
| 3564 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3565 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3566 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3567 | main.cleanup() |
| 3568 | main.exit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3569 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3570 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3571 | """ |
| 3572 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3573 | appName is the hierarchical app name, not the feature name |
| 3574 | If check is True, method will check the status of the app after the |
| 3575 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3576 | Returns main.TRUE if the command was successfully sent |
| 3577 | main.FALSE if the cli responded with an error or given |
| 3578 | incorrect input |
| 3579 | """ |
| 3580 | try: |
| 3581 | if not isinstance( appName, types.StringType ): |
| 3582 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3583 | " a string" ) |
| 3584 | return main.FALSE |
| 3585 | status = self.appStatus( appName ) |
| 3586 | if status == "INSTALLED": |
| 3587 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3588 | if check and response == main.TRUE: |
| 3589 | for i in range(10): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3590 | status = self.appStatus( appName ) |
| 3591 | if status == "ACTIVE": |
| 3592 | return main.TRUE |
| 3593 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3594 | main.log.debug( "The state of application " + |
| 3595 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3596 | time.sleep( 1 ) |
| 3597 | return main.FALSE |
| 3598 | else: # not 'check' or command didn't succeed |
| 3599 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3600 | elif status == "ACTIVE": |
| 3601 | return main.TRUE |
| 3602 | elif status == "UNINSTALLED": |
| 3603 | main.log.error( self.name + ": Tried to activate the " + |
| 3604 | "application '" + appName + "' which is not " + |
| 3605 | "installed." ) |
| 3606 | else: |
| 3607 | main.log.error( "Unexpected return value from appStatus: " + |
| 3608 | str( status ) ) |
| 3609 | return main.ERROR |
| 3610 | except TypeError: |
| 3611 | main.log.exception( self.name + ": Object not as expected" ) |
| 3612 | return main.ERROR |
| 3613 | except pexpect.EOF: |
| 3614 | main.log.error( self.name + ": EOF exception found" ) |
| 3615 | main.log.error( self.name + ": " + self.handle.before ) |
| 3616 | main.cleanup() |
| 3617 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3618 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3619 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3620 | main.cleanup() |
| 3621 | main.exit() |
| 3622 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3623 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3624 | """ |
| 3625 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3626 | appName is the hierarchical app name, not the feature name |
| 3627 | If check is True, method will check the status of the app after the |
| 3628 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3629 | Returns main.TRUE if the command was successfully sent |
| 3630 | main.FALSE if the cli responded with an error or given |
| 3631 | incorrect input |
| 3632 | """ |
| 3633 | try: |
| 3634 | if not isinstance( appName, types.StringType ): |
| 3635 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3636 | "be a string" ) |
| 3637 | return main.FALSE |
| 3638 | status = self.appStatus( appName ) |
| 3639 | if status == "INSTALLED": |
| 3640 | return main.TRUE |
| 3641 | elif status == "ACTIVE": |
| 3642 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3643 | if check and response == main.TRUE: |
| 3644 | for i in range(10): # try 10 times then give up |
| 3645 | status = self.appStatus( appName ) |
| 3646 | if status == "INSTALLED": |
| 3647 | return main.TRUE |
| 3648 | else: |
| 3649 | time.sleep( 1 ) |
| 3650 | return main.FALSE |
| 3651 | else: # not check or command didn't succeed |
| 3652 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3653 | elif status == "UNINSTALLED": |
| 3654 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3655 | "application '" + appName + "' which is not " + |
| 3656 | "installed." ) |
| 3657 | return main.TRUE |
| 3658 | else: |
| 3659 | main.log.error( "Unexpected return value from appStatus: " + |
| 3660 | str( status ) ) |
| 3661 | return main.ERROR |
| 3662 | except TypeError: |
| 3663 | main.log.exception( self.name + ": Object not as expected" ) |
| 3664 | return main.ERROR |
| 3665 | except pexpect.EOF: |
| 3666 | main.log.error( self.name + ": EOF exception found" ) |
| 3667 | main.log.error( self.name + ": " + self.handle.before ) |
| 3668 | main.cleanup() |
| 3669 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3670 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3671 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3672 | main.cleanup() |
| 3673 | main.exit() |
| 3674 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3675 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3676 | """ |
| 3677 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3678 | appName is the hierarchical app name, not the feature name |
| 3679 | If check is True, method will check the status of the app after the |
| 3680 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3681 | Returns main.TRUE if the command was successfully sent |
| 3682 | main.FALSE if the cli responded with an error or given |
| 3683 | incorrect input |
| 3684 | """ |
| 3685 | # TODO: check with Thomas about the state machine for apps |
| 3686 | try: |
| 3687 | if not isinstance( appName, types.StringType ): |
| 3688 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3689 | "be a string" ) |
| 3690 | return main.FALSE |
| 3691 | status = self.appStatus( appName ) |
| 3692 | if status == "INSTALLED": |
| 3693 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3694 | if check and response == main.TRUE: |
| 3695 | for i in range(10): # try 10 times then give up |
| 3696 | status = self.appStatus( appName ) |
| 3697 | if status == "UNINSTALLED": |
| 3698 | return main.TRUE |
| 3699 | else: |
| 3700 | time.sleep( 1 ) |
| 3701 | return main.FALSE |
| 3702 | else: # not check or command didn't succeed |
| 3703 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3704 | elif status == "ACTIVE": |
| 3705 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3706 | "application '" + appName + "' which is " + |
| 3707 | "currently active." ) |
| 3708 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3709 | if check and response == main.TRUE: |
| 3710 | for i in range(10): # try 10 times then give up |
| 3711 | status = self.appStatus( appName ) |
| 3712 | if status == "UNINSTALLED": |
| 3713 | return main.TRUE |
| 3714 | else: |
| 3715 | time.sleep( 1 ) |
| 3716 | return main.FALSE |
| 3717 | else: # not check or command didn't succeed |
| 3718 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3719 | elif status == "UNINSTALLED": |
| 3720 | return main.TRUE |
| 3721 | else: |
| 3722 | main.log.error( "Unexpected return value from appStatus: " + |
| 3723 | str( status ) ) |
| 3724 | return main.ERROR |
| 3725 | except TypeError: |
| 3726 | main.log.exception( self.name + ": Object not as expected" ) |
| 3727 | return main.ERROR |
| 3728 | except pexpect.EOF: |
| 3729 | main.log.error( self.name + ": EOF exception found" ) |
| 3730 | main.log.error( self.name + ": " + self.handle.before ) |
| 3731 | main.cleanup() |
| 3732 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3733 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3734 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3735 | main.cleanup() |
| 3736 | main.exit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3737 | |
| 3738 | def appIDs( self, jsonFormat=True ): |
| 3739 | """ |
| 3740 | Show the mappings between app id and app names given by the 'app-ids' |
| 3741 | cli command |
| 3742 | """ |
| 3743 | try: |
| 3744 | cmdStr = "app-ids" |
| 3745 | if jsonFormat: |
| 3746 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3747 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3748 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3749 | assert "Command not found:" not in output, output |
| 3750 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3751 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3752 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3753 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3754 | return None |
| 3755 | except TypeError: |
| 3756 | main.log.exception( self.name + ": Object not as expected" ) |
| 3757 | return None |
| 3758 | except pexpect.EOF: |
| 3759 | main.log.error( self.name + ": EOF exception found" ) |
| 3760 | main.log.error( self.name + ": " + self.handle.before ) |
| 3761 | main.cleanup() |
| 3762 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3763 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3764 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3765 | main.cleanup() |
| 3766 | main.exit() |
| 3767 | |
| 3768 | def appToIDCheck( self ): |
| 3769 | """ |
| 3770 | This method will check that each application's ID listed in 'apps' is |
| 3771 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3772 | there are no duplicate IDs issued. Note that an app ID should be |
| 3773 | a globaly unique numerical identifier for app/app-like features. Once |
| 3774 | an ID is registered, the ID is never freed up so that if an app is |
| 3775 | reinstalled it will have the same ID. |
| 3776 | |
| 3777 | Returns: main.TRUE if the check passes and |
| 3778 | main.FALSE if the check fails or |
| 3779 | main.ERROR if there is some error in processing the test |
| 3780 | """ |
| 3781 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3782 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3783 | rawJson = self.appIDs( jsonFormat=True ) |
| 3784 | if rawJson: |
| 3785 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3786 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3787 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3788 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3789 | rawJson = self.apps( jsonFormat=True ) |
| 3790 | if rawJson: |
| 3791 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3792 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3793 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3794 | bail = True |
| 3795 | if bail: |
| 3796 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3797 | result = main.TRUE |
| 3798 | for app in apps: |
| 3799 | appID = app.get( 'id' ) |
| 3800 | if appID is None: |
| 3801 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3802 | result = main.FALSE |
| 3803 | appName = app.get( 'name' ) |
| 3804 | if appName is None: |
| 3805 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3806 | result = main.FALSE |
| 3807 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3808 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3809 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3810 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3811 | if not current: # if ids doesn't have this id |
| 3812 | result = main.FALSE |
| 3813 | main.log.error( "'app-ids' does not have the ID for " + |
| 3814 | str( appName ) + " that apps does." ) |
| 3815 | elif len( current ) > 1: |
| 3816 | # there is more than one app with this ID |
| 3817 | result = main.FALSE |
| 3818 | # We will log this later in the method |
| 3819 | elif not current[0][ 'name' ] == appName: |
| 3820 | currentName = current[0][ 'name' ] |
| 3821 | result = main.FALSE |
| 3822 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3823 | " registered under id:" + str( appID ) + |
| 3824 | " but 'apps' has " + str( appName ) ) |
| 3825 | else: |
| 3826 | pass # id and name match! |
| 3827 | # now make sure that app-ids has no duplicates |
| 3828 | idsList = [] |
| 3829 | namesList = [] |
| 3830 | for item in ids: |
| 3831 | idsList.append( item[ 'id' ] ) |
| 3832 | namesList.append( item[ 'name' ] ) |
| 3833 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3834 | len( namesList ) != len( set( namesList ) ): |
| 3835 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3836 | + json.dumps( ids, |
| 3837 | sort_keys=True, |
| 3838 | indent=4, |
| 3839 | separators=( ',', ': ' ) ) ) |
| 3840 | result = main.FALSE |
| 3841 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3842 | except ( TypeError, ValueError ): |
| 3843 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3844 | return main.ERROR |
| 3845 | except pexpect.EOF: |
| 3846 | main.log.error( self.name + ": EOF exception found" ) |
| 3847 | main.log.error( self.name + ": " + self.handle.before ) |
| 3848 | main.cleanup() |
| 3849 | main.exit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3850 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3851 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3852 | main.cleanup() |
| 3853 | main.exit() |
| 3854 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3855 | def getCfg( self, component=None, propName=None, short=False, |
| 3856 | jsonFormat=True ): |
| 3857 | """ |
| 3858 | Get configuration settings from onos cli |
| 3859 | Optional arguments: |
| 3860 | component - Optionally only list configurations for a specific |
| 3861 | component. If None, all components with configurations |
| 3862 | are displayed. Case Sensitive string. |
| 3863 | propName - If component is specified, propName option will show |
| 3864 | only this specific configuration from that component. |
| 3865 | Case Sensitive string. |
| 3866 | jsonFormat - Returns output as json. Note that this will override |
| 3867 | the short option |
| 3868 | short - Short, less verbose, version of configurations. |
| 3869 | This is overridden by the json option |
| 3870 | returns: |
| 3871 | Output from cli as a string or None on error |
| 3872 | """ |
| 3873 | try: |
| 3874 | baseStr = "cfg" |
| 3875 | cmdStr = " get" |
| 3876 | componentStr = "" |
| 3877 | if component: |
| 3878 | componentStr += " " + component |
| 3879 | if propName: |
| 3880 | componentStr += " " + propName |
| 3881 | if jsonFormat: |
| 3882 | baseStr += " -j" |
| 3883 | elif short: |
| 3884 | baseStr += " -s" |
| 3885 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3886 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3887 | assert "Command not found:" not in output, output |
| 3888 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3889 | return output |
| 3890 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3891 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3892 | return None |
| 3893 | except TypeError: |
| 3894 | main.log.exception( self.name + ": Object not as expected" ) |
| 3895 | return None |
| 3896 | except pexpect.EOF: |
| 3897 | main.log.error( self.name + ": EOF exception found" ) |
| 3898 | main.log.error( self.name + ": " + self.handle.before ) |
| 3899 | main.cleanup() |
| 3900 | main.exit() |
| 3901 | except Exception: |
| 3902 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3903 | main.cleanup() |
| 3904 | main.exit() |
| 3905 | |
| 3906 | def setCfg( self, component, propName, value=None, check=True ): |
| 3907 | """ |
| 3908 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3909 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3910 | component - The case sensitive name of the component whose |
| 3911 | property is to be set |
| 3912 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3913 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3914 | value - The value to set the property to. If None, will unset the |
| 3915 | property and revert it to it's default value(if applicable) |
| 3916 | check - Boolean, Check whether the option was successfully set this |
| 3917 | only applies when a value is given. |
| 3918 | returns: |
| 3919 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3920 | will return main.TRUE unless there is an error |
| 3921 | """ |
| 3922 | try: |
| 3923 | baseStr = "cfg" |
| 3924 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3925 | if value is not None: |
| 3926 | cmdStr += " " + str( value ) |
| 3927 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3928 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3929 | assert "Command not found:" not in output, output |
| 3930 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3931 | if value and check: |
| 3932 | results = self.getCfg( component=str( component ), |
| 3933 | propName=str( propName ), |
| 3934 | jsonFormat=True ) |
| 3935 | # Check if current value is what we just set |
| 3936 | try: |
| 3937 | jsonOutput = json.loads( results ) |
| 3938 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3939 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3940 | main.log.exception( "Error parsing cfg output" ) |
| 3941 | main.log.error( "output:" + repr( results ) ) |
| 3942 | return main.FALSE |
| 3943 | if current == str( value ): |
| 3944 | return main.TRUE |
| 3945 | return main.FALSE |
| 3946 | return main.TRUE |
| 3947 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3948 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3949 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3950 | except ( TypeError, ValueError ): |
| 3951 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3952 | return main.FALSE |
| 3953 | except pexpect.EOF: |
| 3954 | main.log.error( self.name + ": EOF exception found" ) |
| 3955 | main.log.error( self.name + ": " + self.handle.before ) |
| 3956 | main.cleanup() |
| 3957 | main.exit() |
| 3958 | except Exception: |
| 3959 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3960 | main.cleanup() |
| 3961 | main.exit() |
| 3962 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3963 | def setTestAdd( self, setName, values ): |
| 3964 | """ |
| 3965 | CLI command to add elements to a distributed set. |
| 3966 | Arguments: |
| 3967 | setName - The name of the set to add to. |
| 3968 | values - The value(s) to add to the set, space seperated. |
| 3969 | Example usages: |
| 3970 | setTestAdd( "set1", "a b c" ) |
| 3971 | setTestAdd( "set2", "1" ) |
| 3972 | returns: |
| 3973 | main.TRUE on success OR |
| 3974 | main.FALSE if elements were already in the set OR |
| 3975 | main.ERROR on error |
| 3976 | """ |
| 3977 | try: |
| 3978 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
| 3979 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3980 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3981 | assert "Command not found:" not in output, output |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3982 | try: |
| 3983 | # TODO: Maybe make this less hardcoded |
| 3984 | # ConsistentMap Exceptions |
| 3985 | assert "org.onosproject.store.service" not in output |
| 3986 | # Node not leader |
| 3987 | assert "java.lang.IllegalStateException" not in output |
| 3988 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 3989 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 3990 | "command: " + str( output ) ) |
| 3991 | retryTime = 30 # Conservative time, given by Madan |
| 3992 | main.log.info( "Waiting " + str( retryTime ) + |
| 3993 | "seconds before retrying." ) |
| 3994 | time.sleep( retryTime ) # Due to change in mastership |
| 3995 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3996 | assert output is not None, "Error in sendline" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3997 | assert "Error executing command" not in output |
| 3998 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3999 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
| 4000 | main.log.info( self.name + ": " + output ) |
| 4001 | if re.search( positiveMatch, output): |
| 4002 | return main.TRUE |
| 4003 | elif re.search( negativeMatch, output): |
| 4004 | return main.FALSE |
| 4005 | else: |
| 4006 | main.log.error( self.name + ": setTestAdd did not" + |
| 4007 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4008 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4009 | return main.ERROR |
| 4010 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4011 | main.log.exception( "Error in processing '" + cmdStr + "' command. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4012 | return main.ERROR |
| 4013 | except TypeError: |
| 4014 | main.log.exception( self.name + ": Object not as expected" ) |
| 4015 | return main.ERROR |
| 4016 | except pexpect.EOF: |
| 4017 | main.log.error( self.name + ": EOF exception found" ) |
| 4018 | main.log.error( self.name + ": " + self.handle.before ) |
| 4019 | main.cleanup() |
| 4020 | main.exit() |
| 4021 | except Exception: |
| 4022 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4023 | main.cleanup() |
| 4024 | main.exit() |
| 4025 | |
| 4026 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4027 | """ |
| 4028 | CLI command to remove elements from a distributed set. |
| 4029 | Required arguments: |
| 4030 | setName - The name of the set to remove from. |
| 4031 | values - The value(s) to remove from the set, space seperated. |
| 4032 | Optional arguments: |
| 4033 | clear - Clear all elements from the set |
| 4034 | retain - Retain only the given values. (intersection of the |
| 4035 | original set and the given set) |
| 4036 | returns: |
| 4037 | main.TRUE on success OR |
| 4038 | main.FALSE if the set was not changed OR |
| 4039 | main.ERROR on error |
| 4040 | """ |
| 4041 | try: |
| 4042 | cmdStr = "set-test-remove " |
| 4043 | if clear: |
| 4044 | cmdStr += "-c " + str( setName ) |
| 4045 | elif retain: |
| 4046 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4047 | else: |
| 4048 | cmdStr += str( setName ) + " " + str( values ) |
| 4049 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4050 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4051 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4052 | # TODO: Maybe make this less hardcoded |
| 4053 | # ConsistentMap Exceptions |
| 4054 | assert "org.onosproject.store.service" not in output |
| 4055 | # Node not leader |
| 4056 | assert "java.lang.IllegalStateException" not in output |
| 4057 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4058 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4059 | "command: " + str( output ) ) |
| 4060 | retryTime = 30 # Conservative time, given by Madan |
| 4061 | main.log.info( "Waiting " + str( retryTime ) + |
| 4062 | "seconds before retrying." ) |
| 4063 | time.sleep( retryTime ) # Due to change in mastership |
| 4064 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4065 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4066 | assert "Command not found:" not in output, output |
| 4067 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4068 | main.log.info( self.name + ": " + output ) |
| 4069 | if clear: |
| 4070 | pattern = "Set " + str( setName ) + " cleared" |
| 4071 | if re.search( pattern, output ): |
| 4072 | return main.TRUE |
| 4073 | elif retain: |
| 4074 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4075 | "only elements of set \[(.*)\]" |
| 4076 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4077 | "retaining only elements of the set " +\ |
| 4078 | "\[(.*)\]" |
| 4079 | if re.search( positivePattern, output ): |
| 4080 | return main.TRUE |
| 4081 | elif re.search( negativePattern, output ): |
| 4082 | return main.FALSE |
| 4083 | else: |
| 4084 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4085 | str( setName ) |
| 4086 | if ( len( values.split() ) == 1 ): |
| 4087 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4088 | str( setName ) |
| 4089 | else: |
| 4090 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4091 | str( setName ) |
| 4092 | if re.search( positivePattern, output ): |
| 4093 | return main.TRUE |
| 4094 | elif re.search( negativePattern, output ): |
| 4095 | return main.FALSE |
| 4096 | main.log.error( self.name + ": setTestRemove did not" + |
| 4097 | " match expected output" ) |
| 4098 | main.log.debug( self.name + " expected: " + pattern ) |
| 4099 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4100 | return main.ERROR |
| 4101 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4102 | main.log.exception( "Error in processing '" + cmdStr + "' commandr. " ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4103 | return main.ERROR |
| 4104 | except TypeError: |
| 4105 | main.log.exception( self.name + ": Object not as expected" ) |
| 4106 | return main.ERROR |
| 4107 | except pexpect.EOF: |
| 4108 | main.log.error( self.name + ": EOF exception found" ) |
| 4109 | main.log.error( self.name + ": " + self.handle.before ) |
| 4110 | main.cleanup() |
| 4111 | main.exit() |
| 4112 | except Exception: |
| 4113 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4114 | main.cleanup() |
| 4115 | main.exit() |
| 4116 | |
| 4117 | def setTestGet( self, setName, values="" ): |
| 4118 | """ |
| 4119 | CLI command to get the elements in a distributed set. |
| 4120 | Required arguments: |
| 4121 | setName - The name of the set to remove from. |
| 4122 | Optional arguments: |
| 4123 | values - The value(s) to check if in the set, space seperated. |
| 4124 | returns: |
| 4125 | main.ERROR on error OR |
| 4126 | A list of elements in the set if no optional arguments are |
| 4127 | supplied OR |
| 4128 | A tuple containing the list then: |
| 4129 | main.FALSE if the given values are not in the set OR |
| 4130 | main.TRUE if the given values are in the set OR |
| 4131 | """ |
| 4132 | try: |
| 4133 | values = str( values ).strip() |
| 4134 | setName = str( setName ).strip() |
| 4135 | length = len( values.split() ) |
| 4136 | containsCheck = None |
| 4137 | # Patterns to match |
| 4138 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4139 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4140 | containsTrue = "Set " + setName + " contains the value " + values |
| 4141 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4142 | values |
| 4143 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4144 | setPattern |
| 4145 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4146 | " subset " + setPattern |
| 4147 | |
| 4148 | cmdStr = "set-test-get " |
| 4149 | cmdStr += setName + " " + values |
| 4150 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4151 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4152 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4153 | # TODO: Maybe make this less hardcoded |
| 4154 | # ConsistentMap Exceptions |
| 4155 | assert "org.onosproject.store.service" not in output |
| 4156 | # Node not leader |
| 4157 | assert "java.lang.IllegalStateException" not in output |
| 4158 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4159 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4160 | "command: " + str( output ) ) |
| 4161 | retryTime = 30 # Conservative time, given by Madan |
| 4162 | main.log.info( "Waiting " + str( retryTime ) + |
| 4163 | "seconds before retrying." ) |
| 4164 | time.sleep( retryTime ) # Due to change in mastership |
| 4165 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4166 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4167 | assert "Command not found:" not in output, output |
| 4168 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4169 | main.log.info( self.name + ": " + output ) |
| 4170 | |
| 4171 | if length == 0: |
| 4172 | match = re.search( pattern, output ) |
| 4173 | else: # if given values |
| 4174 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4175 | patternTrue = pattern + "\r\n" + containsTrue |
| 4176 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4177 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4178 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4179 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4180 | matchTrue = re.search( patternTrue, output ) |
| 4181 | matchFalse = re.search( patternFalse, output ) |
| 4182 | if matchTrue: |
| 4183 | containsCheck = main.TRUE |
| 4184 | match = matchTrue |
| 4185 | elif matchFalse: |
| 4186 | containsCheck = main.FALSE |
| 4187 | match = matchFalse |
| 4188 | else: |
| 4189 | main.log.error( self.name + " setTestGet did not match " +\ |
| 4190 | "expected output" ) |
| 4191 | main.log.debug( self.name + " expected: " + pattern ) |
| 4192 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4193 | match = None |
| 4194 | if match: |
| 4195 | setMatch = match.group( 1 ) |
| 4196 | if setMatch == '': |
| 4197 | setList = [] |
| 4198 | else: |
| 4199 | setList = setMatch.split( ", " ) |
| 4200 | if length > 0: |
| 4201 | return ( setList, containsCheck ) |
| 4202 | else: |
| 4203 | return setList |
| 4204 | else: # no match |
| 4205 | main.log.error( self.name + ": setTestGet did not" + |
| 4206 | " match expected output" ) |
| 4207 | main.log.debug( self.name + " expected: " + pattern ) |
| 4208 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4209 | return main.ERROR |
| 4210 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4211 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4212 | return main.ERROR |
| 4213 | except TypeError: |
| 4214 | main.log.exception( self.name + ": Object not as expected" ) |
| 4215 | return main.ERROR |
| 4216 | except pexpect.EOF: |
| 4217 | main.log.error( self.name + ": EOF exception found" ) |
| 4218 | main.log.error( self.name + ": " + self.handle.before ) |
| 4219 | main.cleanup() |
| 4220 | main.exit() |
| 4221 | except Exception: |
| 4222 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4223 | main.cleanup() |
| 4224 | main.exit() |
| 4225 | |
| 4226 | def setTestSize( self, setName ): |
| 4227 | """ |
| 4228 | CLI command to get the elements in a distributed set. |
| 4229 | Required arguments: |
| 4230 | setName - The name of the set to remove from. |
| 4231 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4232 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4233 | None on error |
| 4234 | """ |
| 4235 | try: |
| 4236 | # TODO: Should this check against the number of elements returned |
| 4237 | # and then return true/false based on that? |
| 4238 | setName = str( setName ).strip() |
| 4239 | # Patterns to match |
| 4240 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4241 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4242 | setPattern |
| 4243 | cmdStr = "set-test-get -s " |
| 4244 | cmdStr += setName |
| 4245 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4246 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4247 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4248 | # TODO: Maybe make this less hardcoded |
| 4249 | # ConsistentMap Exceptions |
| 4250 | assert "org.onosproject.store.service" not in output |
| 4251 | # Node not leader |
| 4252 | assert "java.lang.IllegalStateException" not in output |
| 4253 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4254 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4255 | "command: " + str( output ) ) |
| 4256 | retryTime = 30 # Conservative time, given by Madan |
| 4257 | main.log.info( "Waiting " + str( retryTime ) + |
| 4258 | "seconds before retrying." ) |
| 4259 | time.sleep( retryTime ) # Due to change in mastership |
| 4260 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4261 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4262 | assert "Command not found:" not in output, output |
| 4263 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4264 | main.log.info( self.name + ": " + output ) |
| 4265 | match = re.search( pattern, output ) |
| 4266 | if match: |
| 4267 | setSize = int( match.group( 1 ) ) |
| 4268 | setMatch = match.group( 2 ) |
| 4269 | if len( setMatch.split() ) == setSize: |
| 4270 | main.log.info( "The size returned by " + self.name + |
| 4271 | " matches the number of elements in " + |
| 4272 | "the returned set" ) |
| 4273 | else: |
| 4274 | main.log.error( "The size returned by " + self.name + |
| 4275 | " does not match the number of " + |
| 4276 | "elements in the returned set." ) |
| 4277 | return setSize |
| 4278 | else: # no match |
| 4279 | main.log.error( self.name + ": setTestGet did not" + |
| 4280 | " match expected output" ) |
| 4281 | main.log.debug( self.name + " expected: " + pattern ) |
| 4282 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4283 | return None |
| 4284 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4285 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 4286 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4287 | except TypeError: |
| 4288 | main.log.exception( self.name + ": Object not as expected" ) |
| 4289 | return None |
| 4290 | except pexpect.EOF: |
| 4291 | main.log.error( self.name + ": EOF exception found" ) |
| 4292 | main.log.error( self.name + ": " + self.handle.before ) |
| 4293 | main.cleanup() |
| 4294 | main.exit() |
| 4295 | except Exception: |
| 4296 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4297 | main.cleanup() |
| 4298 | main.exit() |
| 4299 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4300 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4301 | """ |
| 4302 | Command to list the various counters in the system. |
| 4303 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4304 | if jsonFormat, a string of the json object returned by the cli |
| 4305 | command |
| 4306 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4307 | None on error |
| 4308 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4309 | try: |
| 4310 | counters = {} |
| 4311 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4312 | if jsonFormat: |
| 4313 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4314 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4315 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4316 | assert "Command not found:" not in output, output |
| 4317 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4318 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4319 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4320 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4321 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4322 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4323 | except TypeError: |
| 4324 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4325 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4326 | except pexpect.EOF: |
| 4327 | main.log.error( self.name + ": EOF exception found" ) |
| 4328 | main.log.error( self.name + ": " + self.handle.before ) |
| 4329 | main.cleanup() |
| 4330 | main.exit() |
| 4331 | except Exception: |
| 4332 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4333 | main.cleanup() |
| 4334 | main.exit() |
| 4335 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4336 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4337 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4338 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4339 | Required arguments: |
| 4340 | counter - The name of the counter to increment. |
| 4341 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4342 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4343 | returns: |
| 4344 | integer value of the counter or |
| 4345 | None on Error |
| 4346 | """ |
| 4347 | try: |
| 4348 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4349 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4350 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4351 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4352 | if delta != 1: |
| 4353 | cmdStr += " " + str( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4354 | output = self.sendline( cmdStr ) |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4355 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4356 | assert output is not None, "Error in sendline" |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4357 | # TODO: Maybe make this less hardcoded |
| 4358 | # ConsistentMap Exceptions |
| 4359 | assert "org.onosproject.store.service" not in output |
| 4360 | # Node not leader |
| 4361 | assert "java.lang.IllegalStateException" not in output |
| 4362 | except AssertionError: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4363 | main.log.error( "Error in processing '" + cmdStr + "' " + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4364 | "command: " + str( output ) ) |
| 4365 | retryTime = 30 # Conservative time, given by Madan |
| 4366 | main.log.info( "Waiting " + str( retryTime ) + |
| 4367 | "seconds before retrying." ) |
| 4368 | time.sleep( retryTime ) # Due to change in mastership |
| 4369 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4370 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4371 | assert "Command not found:" not in output, output |
| 4372 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4373 | main.log.info( self.name + ": " + output ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4374 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4375 | match = re.search( pattern, output ) |
| 4376 | if match: |
| 4377 | return int( match.group( 1 ) ) |
| 4378 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4379 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4380 | " match expected output." ) |
| 4381 | main.log.debug( self.name + " expected: " + pattern ) |
| 4382 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4383 | return None |
| 4384 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4385 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4386 | return None |
| 4387 | except TypeError: |
| 4388 | main.log.exception( self.name + ": Object not as expected" ) |
| 4389 | return None |
| 4390 | except pexpect.EOF: |
| 4391 | main.log.error( self.name + ": EOF exception found" ) |
| 4392 | main.log.error( self.name + ": " + self.handle.before ) |
| 4393 | main.cleanup() |
| 4394 | main.exit() |
| 4395 | except Exception: |
| 4396 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4397 | main.cleanup() |
| 4398 | main.exit() |
| 4399 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4400 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4401 | """ |
| 4402 | CLI command to get a distributed counter then add a delta to it. |
| 4403 | Required arguments: |
| 4404 | counter - The name of the counter to increment. |
| 4405 | Optional arguments: |
| 4406 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4407 | returns: |
| 4408 | integer value of the counter or |
| 4409 | None on Error |
| 4410 | """ |
| 4411 | try: |
| 4412 | counter = str( counter ) |
| 4413 | delta = int( delta ) |
| 4414 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4415 | cmdStr += counter |
| 4416 | if delta != 1: |
| 4417 | cmdStr += " " + str( delta ) |
| 4418 | output = self.sendline( cmdStr ) |
| 4419 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4420 | assert output is not None, "Error in sendline" |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4421 | # TODO: Maybe make this less hardcoded |
| 4422 | # ConsistentMap Exceptions |
| 4423 | assert "org.onosproject.store.service" not in output |
| 4424 | # Node not leader |
| 4425 | assert "java.lang.IllegalStateException" not in output |
| 4426 | except AssertionError: |
| 4427 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4428 | "command: " + str( output ) ) |
| 4429 | retryTime = 30 # Conservative time, given by Madan |
| 4430 | main.log.info( "Waiting " + str( retryTime ) + |
| 4431 | "seconds before retrying." ) |
| 4432 | time.sleep( retryTime ) # Due to change in mastership |
| 4433 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4434 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4435 | assert "Command not found:" not in output, output |
| 4436 | assert "Error executing command" not in output, output |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4437 | main.log.info( self.name + ": " + output ) |
| 4438 | pattern = counter + " was updated to (-?\d+)" |
| 4439 | match = re.search( pattern, output ) |
| 4440 | if match: |
| 4441 | return int( match.group( 1 ) ) |
| 4442 | else: |
| 4443 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4444 | " match expected output." ) |
| 4445 | main.log.debug( self.name + " expected: " + pattern ) |
| 4446 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4447 | return None |
| 4448 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4449 | main.log.exception( "Error in processing '" + cmdStr + "' command." ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4450 | return None |
| 4451 | except TypeError: |
| 4452 | main.log.exception( self.name + ": Object not as expected" ) |
| 4453 | return None |
| 4454 | except pexpect.EOF: |
| 4455 | main.log.error( self.name + ": EOF exception found" ) |
| 4456 | main.log.error( self.name + ": " + self.handle.before ) |
| 4457 | main.cleanup() |
| 4458 | main.exit() |
| 4459 | except Exception: |
| 4460 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4461 | main.cleanup() |
| 4462 | main.exit() |
| 4463 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4464 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4465 | """ |
| 4466 | Description: Execute summary command in onos |
| 4467 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4468 | no output |
| 4469 | |
| 4470 | """ |
| 4471 | try: |
| 4472 | cmdStr = "summary" |
| 4473 | if jsonFormat: |
| 4474 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4475 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4476 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4477 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4478 | assert "Error:" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4479 | if not handle: |
| 4480 | main.log.error( self.name + ": There is no output in " + |
| 4481 | "summary command" ) |
| 4482 | return main.FALSE |
| 4483 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4484 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4485 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4486 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4487 | except TypeError: |
| 4488 | main.log.exception( self.name + ": Object not as expected" ) |
| 4489 | return None |
| 4490 | except pexpect.EOF: |
| 4491 | main.log.error( self.name + ": EOF exception found" ) |
| 4492 | main.log.error( self.name + ": " + self.handle.before ) |
| 4493 | main.cleanup() |
| 4494 | main.exit() |
| 4495 | except Exception: |
| 4496 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4497 | main.cleanup() |
| 4498 | main.exit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4499 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4500 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4501 | """ |
| 4502 | CLI command to get the value of a key in a consistent map using |
| 4503 | transactions. This a test function and can only get keys from the |
| 4504 | test map hard coded into the cli command |
| 4505 | Required arguments: |
| 4506 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4507 | returns: |
| 4508 | The string value of the key or |
| 4509 | None on Error |
| 4510 | """ |
| 4511 | try: |
| 4512 | keyName = str( keyName ) |
| 4513 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4514 | cmdStr += keyName |
| 4515 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4516 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4517 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4518 | try: |
| 4519 | # TODO: Maybe make this less hardcoded |
| 4520 | # ConsistentMap Exceptions |
| 4521 | assert "org.onosproject.store.service" not in output |
| 4522 | # Node not leader |
| 4523 | assert "java.lang.IllegalStateException" not in output |
| 4524 | except AssertionError: |
| 4525 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4526 | "command: " + str( output ) ) |
| 4527 | return None |
| 4528 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4529 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4530 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4531 | return None |
| 4532 | else: |
| 4533 | match = re.search( pattern, output ) |
| 4534 | if match: |
| 4535 | return match.groupdict()[ 'value' ] |
| 4536 | else: |
| 4537 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4538 | " match expected output." ) |
| 4539 | main.log.debug( self.name + " expected: " + pattern ) |
| 4540 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4541 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4542 | except AssertionError: |
| 4543 | main.log.exception( "" ) |
| 4544 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4545 | except TypeError: |
| 4546 | main.log.exception( self.name + ": Object not as expected" ) |
| 4547 | return None |
| 4548 | except pexpect.EOF: |
| 4549 | main.log.error( self.name + ": EOF exception found" ) |
| 4550 | main.log.error( self.name + ": " + self.handle.before ) |
| 4551 | main.cleanup() |
| 4552 | main.exit() |
| 4553 | except Exception: |
| 4554 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4555 | main.cleanup() |
| 4556 | main.exit() |
| 4557 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4558 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4559 | """ |
| 4560 | CLI command to put a value into 'numKeys' number of keys in a |
| 4561 | consistent map using transactions. This a test function and can only |
| 4562 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4563 | Required arguments: |
| 4564 | numKeys - Number of keys to add the value to |
| 4565 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4566 | returns: |
| 4567 | A dictionary whose keys are the name of the keys put into the map |
| 4568 | and the values of the keys are dictionaries whose key-values are |
| 4569 | 'value': value put into map and optionaly |
| 4570 | 'oldValue': Previous value in the key or |
| 4571 | None on Error |
| 4572 | |
| 4573 | Example output |
| 4574 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4575 | 'Key2': {'value': 'Testing'} } |
| 4576 | """ |
| 4577 | try: |
| 4578 | numKeys = str( numKeys ) |
| 4579 | value = str( value ) |
| 4580 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4581 | cmdStr += numKeys + " " + value |
| 4582 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4583 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4584 | assert "Command not found:" not in output, output |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4585 | try: |
| 4586 | # TODO: Maybe make this less hardcoded |
| 4587 | # ConsistentMap Exceptions |
| 4588 | assert "org.onosproject.store.service" not in output |
| 4589 | # Node not leader |
| 4590 | assert "java.lang.IllegalStateException" not in output |
| 4591 | except AssertionError: |
| 4592 | main.log.error( "Error in processing '" + cmdStr + "' " + |
| 4593 | "command: " + str( output ) ) |
| 4594 | return None |
| 4595 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4596 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4597 | results = {} |
| 4598 | for line in output.splitlines(): |
| 4599 | new = re.search( newPattern, line ) |
| 4600 | updated = re.search( updatedPattern, line ) |
| 4601 | if new: |
| 4602 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4603 | elif updated: |
| 4604 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4605 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4606 | else: |
| 4607 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4608 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4609 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4610 | newPattern, |
| 4611 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4612 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4613 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4614 | except AssertionError: |
| 4615 | main.log.exception( "" ) |
| 4616 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4617 | except TypeError: |
| 4618 | main.log.exception( self.name + ": Object not as expected" ) |
| 4619 | return None |
| 4620 | except pexpect.EOF: |
| 4621 | main.log.error( self.name + ": EOF exception found" ) |
| 4622 | main.log.error( self.name + ": " + self.handle.before ) |
| 4623 | main.cleanup() |
| 4624 | main.exit() |
| 4625 | except Exception: |
| 4626 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4627 | main.cleanup() |
| 4628 | main.exit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4629 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4630 | def maps( self, jsonFormat=True ): |
| 4631 | """ |
| 4632 | Description: Returns result of onos:maps |
| 4633 | Optional: |
| 4634 | * jsonFormat: enable json formatting of output |
| 4635 | """ |
| 4636 | try: |
| 4637 | cmdStr = "maps" |
| 4638 | if jsonFormat: |
| 4639 | cmdStr += " -j" |
| 4640 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4641 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4642 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4643 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4644 | except AssertionError: |
| 4645 | main.log.exception( "" ) |
| 4646 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4647 | except TypeError: |
| 4648 | main.log.exception( self.name + ": Object not as expected" ) |
| 4649 | return None |
| 4650 | except pexpect.EOF: |
| 4651 | main.log.error( self.name + ": EOF exception found" ) |
| 4652 | main.log.error( self.name + ": " + self.handle.before ) |
| 4653 | main.cleanup() |
| 4654 | main.exit() |
| 4655 | except Exception: |
| 4656 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4657 | main.cleanup() |
| 4658 | main.exit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4659 | |
| 4660 | def getSwController( self, uri, jsonFormat=True ): |
| 4661 | """ |
| 4662 | Descrition: Gets the controller information from the device |
| 4663 | """ |
| 4664 | try: |
| 4665 | cmd = "device-controllers " |
| 4666 | if jsonFormat: |
| 4667 | cmd += "-j " |
| 4668 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4669 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4670 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4671 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4672 | except AssertionError: |
| 4673 | main.log.exception( "" ) |
| 4674 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4675 | except TypeError: |
| 4676 | main.log.exception( self.name + ": Object not as expected" ) |
| 4677 | return None |
| 4678 | except pexpect.EOF: |
| 4679 | main.log.error( self.name + ": EOF exception found" ) |
| 4680 | main.log.error( self.name + ": " + self.handle.before ) |
| 4681 | main.cleanup() |
| 4682 | main.exit() |
| 4683 | except Exception: |
| 4684 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4685 | main.cleanup() |
| 4686 | main.exit() |
| 4687 | |
| 4688 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4689 | """ |
| 4690 | Descrition: sets the controller(s) for the specified device |
| 4691 | |
| 4692 | Parameters: |
| 4693 | Required: uri - String: The uri of the device(switch). |
| 4694 | ip - String or List: The ip address of the controller. |
| 4695 | This parameter can be formed in a couple of different ways. |
| 4696 | VALID: |
| 4697 | 10.0.0.1 - just the ip address |
| 4698 | tcp:10.0.0.1 - the protocol and the ip address |
| 4699 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4700 | so that you can add controllers with different |
| 4701 | protocols and ports |
| 4702 | INVALID: |
| 4703 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4704 | |
| 4705 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4706 | port - The port number. |
| 4707 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4708 | |
| 4709 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4710 | """ |
| 4711 | try: |
| 4712 | cmd = "device-setcontrollers" |
| 4713 | |
| 4714 | if jsonFormat: |
| 4715 | cmd += " -j" |
| 4716 | cmd += " " + uri |
| 4717 | if isinstance( ip, str ): |
| 4718 | ip = [ip] |
| 4719 | for item in ip: |
| 4720 | if ":" in item: |
| 4721 | sitem = item.split( ":" ) |
| 4722 | if len(sitem) == 3: |
| 4723 | cmd += " " + item |
| 4724 | elif "." in sitem[1]: |
| 4725 | cmd += " {}:{}".format(item, port) |
| 4726 | else: |
| 4727 | main.log.error( "Malformed entry: " + item ) |
| 4728 | raise TypeError |
| 4729 | else: |
| 4730 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4731 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4732 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4733 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4734 | if "Error" in response: |
| 4735 | main.log.error( response ) |
| 4736 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4737 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4738 | except AssertionError: |
| 4739 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4740 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4741 | except TypeError: |
| 4742 | main.log.exception( self.name + ": Object not as expected" ) |
| 4743 | return main.FALSE |
| 4744 | except pexpect.EOF: |
| 4745 | main.log.error( self.name + ": EOF exception found" ) |
| 4746 | main.log.error( self.name + ": " + self.handle.before ) |
| 4747 | main.cleanup() |
| 4748 | main.exit() |
| 4749 | except Exception: |
| 4750 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4751 | main.cleanup() |
| 4752 | main.exit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4753 | |
| 4754 | def removeDevice( self, device ): |
| 4755 | ''' |
| 4756 | Description: |
| 4757 | Remove a device from ONOS by passing the uri of the device(s). |
| 4758 | Parameters: |
| 4759 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
| 4760 | Returns: |
| 4761 | Returns main.FALSE if an exception is thrown or an error is present |
| 4762 | in the response. Otherwise, returns main.TRUE. |
| 4763 | NOTE: |
| 4764 | If a host cannot be removed, then this function will return main.FALSE |
| 4765 | ''' |
| 4766 | try: |
| 4767 | if type( device ) is str: |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 4768 | deviceStr = device |
| 4769 | device = [] |
| 4770 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4771 | |
| 4772 | for d in device: |
| 4773 | time.sleep( 1 ) |
| 4774 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4775 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4776 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4777 | if "Error" in response: |
| 4778 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4779 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4780 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4781 | except AssertionError: |
| 4782 | main.log.exception( "" ) |
| 4783 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4784 | except TypeError: |
| 4785 | main.log.exception( self.name + ": Object not as expected" ) |
| 4786 | return main.FALSE |
| 4787 | except pexpect.EOF: |
| 4788 | main.log.error( self.name + ": EOF exception found" ) |
| 4789 | main.log.error( self.name + ": " + self.handle.before ) |
| 4790 | main.cleanup() |
| 4791 | main.exit() |
| 4792 | except Exception: |
| 4793 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4794 | main.cleanup() |
| 4795 | main.exit() |
| 4796 | |
| 4797 | def removeHost( self, host ): |
| 4798 | ''' |
| 4799 | Description: |
| 4800 | Remove a host from ONOS by passing the id of the host(s) |
| 4801 | Parameters: |
| 4802 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
| 4803 | Returns: |
| 4804 | Returns main.FALSE if an exception is thrown or an error is present |
| 4805 | in the response. Otherwise, returns main.TRUE. |
| 4806 | NOTE: |
| 4807 | If a host cannot be removed, then this function will return main.FALSE |
| 4808 | ''' |
| 4809 | try: |
| 4810 | if type( host ) is str: |
| 4811 | host = list( host ) |
| 4812 | |
| 4813 | for h in host: |
| 4814 | time.sleep( 1 ) |
| 4815 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4816 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4817 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4818 | if "Error" in response: |
| 4819 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 4820 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4821 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4822 | except AssertionError: |
| 4823 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4824 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4825 | except TypeError: |
| 4826 | main.log.exception( self.name + ": Object not as expected" ) |
| 4827 | return main.FALSE |
| 4828 | except pexpect.EOF: |
| 4829 | main.log.error( self.name + ": EOF exception found" ) |
| 4830 | main.log.error( self.name + ": " + self.handle.before ) |
| 4831 | main.cleanup() |
| 4832 | main.exit() |
| 4833 | except Exception: |
| 4834 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4835 | main.cleanup() |
| 4836 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4837 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4838 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4839 | ''' |
| 4840 | Description: |
| 4841 | Bring link down or up in the null-provider. |
| 4842 | params: |
| 4843 | begin - (string) One end of a device or switch. |
| 4844 | end - (string) the other end of the device or switch |
| 4845 | returns: |
| 4846 | main.TRUE if no exceptions were thrown and no Errors are |
| 4847 | present in the resoponse. Otherwise, returns main.FALSE |
| 4848 | ''' |
| 4849 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4850 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4851 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4852 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4853 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4854 | if "Error" in response or "Failure" in response: |
| 4855 | main.log.error( response ) |
| 4856 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4857 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4858 | except AssertionError: |
| 4859 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4860 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4861 | except TypeError: |
| 4862 | main.log.exception( self.name + ": Object not as expected" ) |
| 4863 | return main.FALSE |
| 4864 | except pexpect.EOF: |
| 4865 | main.log.error( self.name + ": EOF exception found" ) |
| 4866 | main.log.error( self.name + ": " + self.handle.before ) |
| 4867 | main.cleanup() |
| 4868 | main.exit() |
| 4869 | except Exception: |
| 4870 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4871 | main.cleanup() |
| 4872 | main.exit() |
| 4873 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4874 | def portstate( self, dpid, port, state ): |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4875 | ''' |
| 4876 | Description: |
| 4877 | Changes the state of port in an OF switch by means of the |
| 4878 | PORTSTATUS OF messages. |
| 4879 | params: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4880 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 4881 | port - (string) target port in the device. Ex: '2' |
| 4882 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4883 | returns: |
| 4884 | main.TRUE if no exceptions were thrown and no Errors are |
| 4885 | present in the resoponse. Otherwise, returns main.FALSE |
| 4886 | ''' |
| 4887 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4888 | state = state.lower() |
| 4889 | assert state == 'enable' or state == 'disable', "Unknown state" |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4890 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
| 4891 | response = self.sendline( cmd, showResponse=True ) |
| 4892 | assert response is not None, "Error in sendline" |
| 4893 | assert "Command not found:" not in response, response |
| 4894 | if "Error" in response or "Failure" in response: |
| 4895 | main.log.error( response ) |
| 4896 | return main.FALSE |
| 4897 | return main.TRUE |
| 4898 | except AssertionError: |
| 4899 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4900 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4901 | except TypeError: |
| 4902 | main.log.exception( self.name + ": Object not as expected" ) |
| 4903 | return main.FALSE |
| 4904 | except pexpect.EOF: |
| 4905 | main.log.error( self.name + ": EOF exception found" ) |
| 4906 | main.log.error( self.name + ": " + self.handle.before ) |
| 4907 | main.cleanup() |
| 4908 | main.exit() |
| 4909 | except Exception: |
| 4910 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4911 | main.cleanup() |
| 4912 | main.exit() |
| 4913 | |
| 4914 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 4915 | """ |
| 4916 | Set the logging level to lvl for a specific app |
| 4917 | returns main.TRUE on success |
| 4918 | returns main.FALSE if Error occurred |
| 4919 | if noExit is True, TestON will not exit, but clean up |
| 4920 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 4921 | Level defaults to INFO |
| 4922 | """ |
| 4923 | try: |
| 4924 | self.handle.sendline( "log:set %s %s" %( level, app ) ) |
| 4925 | self.handle.expect( "onos>" ) |
| 4926 | |
| 4927 | response = self.handle.before |
| 4928 | if re.search( "Error", response ): |
| 4929 | return main.FALSE |
| 4930 | return main.TRUE |
| 4931 | except pexpect.TIMEOUT: |
| 4932 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 4933 | main.cleanup() |
| 4934 | main.exit() |
| 4935 | except pexpect.EOF: |
| 4936 | main.log.error( self.name + ": EOF exception found" ) |
| 4937 | main.log.error( self.name + ": " + self.handle.before ) |
| 4938 | main.cleanup() |
| 4939 | main.exit() |
| 4940 | except Exception: |
| 4941 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4942 | main.cleanup() |
| 4943 | main.exit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4944 | |
| 4945 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 4946 | """ |
| 4947 | Return a dictionary which describes the latest network topology data as a |
| 4948 | graph. |
| 4949 | An example of the dictionary: |
| 4950 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 4951 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 4952 | Each vertex should at least have an 'edges' attribute which describes the |
| 4953 | adjacency information. The value of 'edges' attribute is also represented by |
| 4954 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 4955 | list of attributes. |
| 4956 | An example of the edges dictionary: |
| 4957 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 4958 | vertex3: { 'port': ..., 'weight': ... } } |
| 4959 | If includeHost == True, all hosts (and host-switch links) will be included |
| 4960 | in topology data. |
| 4961 | """ |
| 4962 | graphDict = {} |
| 4963 | try: |
| 4964 | links = self.links() |
| 4965 | links = json.loads( links ) |
| 4966 | devices = self.devices() |
| 4967 | devices = json.loads( devices ) |
| 4968 | idToDevice = {} |
| 4969 | for device in devices: |
| 4970 | idToDevice[ device[ 'id' ] ] = device |
| 4971 | if includeHost: |
| 4972 | hosts = self.hosts() |
| 4973 | # FIXME: support 'includeHost' argument |
| 4974 | for link in links: |
| 4975 | nodeA = link[ 'src' ][ 'device' ] |
| 4976 | nodeB = link[ 'dst' ][ 'device' ] |
| 4977 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
| 4978 | if not nodeA in graphDict.keys(): |
| 4979 | graphDict[ nodeA ] = { 'edges':{}, |
| 4980 | 'dpid':idToDevice[ nodeA ][ 'id' ][3:], |
| 4981 | 'type':idToDevice[ nodeA ][ 'type' ], |
| 4982 | 'available':idToDevice[ nodeA ][ 'available' ], |
| 4983 | 'role':idToDevice[ nodeA ][ 'role' ], |
| 4984 | 'mfr':idToDevice[ nodeA ][ 'mfr' ], |
| 4985 | 'hw':idToDevice[ nodeA ][ 'hw' ], |
| 4986 | 'sw':idToDevice[ nodeA ][ 'sw' ], |
| 4987 | 'serial':idToDevice[ nodeA ][ 'serial' ], |
| 4988 | 'chassisId':idToDevice[ nodeA ][ 'chassisId' ], |
| 4989 | 'annotations':idToDevice[ nodeA ][ 'annotations' ]} |
| 4990 | else: |
| 4991 | # Assert nodeB is not connected to any current links of nodeA |
| 4992 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 4993 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port':link[ 'src' ][ 'port' ], |
| 4994 | 'type':link[ 'type' ], |
| 4995 | 'state':link[ 'state' ] } |
| 4996 | return graphDict |
| 4997 | except ( TypeError, ValueError ): |
| 4998 | main.log.exception( self.name + ": Object not as expected" ) |
| 4999 | return None |
| 5000 | except KeyError: |
| 5001 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5002 | return None |
| 5003 | except AssertionError: |
| 5004 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5005 | return None |
| 5006 | except pexpect.EOF: |
| 5007 | main.log.error( self.name + ": EOF exception found" ) |
| 5008 | main.log.error( self.name + ": " + self.handle.before ) |
| 5009 | return None |
| 5010 | except Exception: |
| 5011 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5012 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5013 | |
| 5014 | def getIntentPerfSummary( self ): |
| 5015 | ''' |
| 5016 | Send command to check intent-perf summary |
| 5017 | Returns: dictionary for intent-perf summary |
| 5018 | if something wrong, function will return None |
| 5019 | ''' |
| 5020 | cmd = "intent-perf -s" |
| 5021 | respDic = {} |
| 5022 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5023 | assert resp is not None, "Error in sendline" |
| 5024 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5025 | try: |
| 5026 | # Generate the dictionary to return |
| 5027 | for l in resp.split( "\n" ): |
| 5028 | # Delete any white space in line |
| 5029 | temp = re.sub( r'\s+', '', l ) |
| 5030 | temp = temp.split( ":" ) |
| 5031 | respDic[ temp[0] ] = temp[ 1 ] |
| 5032 | |
| 5033 | except (TypeError, ValueError): |
| 5034 | main.log.exception( self.name + ": Object not as expected" ) |
| 5035 | return None |
| 5036 | except KeyError: |
| 5037 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5038 | return None |
| 5039 | except AssertionError: |
| 5040 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5041 | return None |
| 5042 | except pexpect.EOF: |
| 5043 | main.log.error( self.name + ": EOF exception found" ) |
| 5044 | main.log.error( self.name + ": " + self.handle.before ) |
| 5045 | return None |
| 5046 | except Exception: |
| 5047 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5048 | return None |
| 5049 | return respDic |
| 5050 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5051 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5052 | """ |
| 5053 | Searches the latest ONOS log file for the given search term and |
| 5054 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5055 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5056 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5057 | searchTerm: |
| 5058 | The string to grep from the ONOS log. |
| 5059 | startLine: |
| 5060 | The term that decides which line is the start to search the searchTerm in |
| 5061 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5062 | logNum: |
| 5063 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5064 | information.Because of this, search mutiply logs is necessary to capture |
| 5065 | the right result. logNum is the number of karaf logs that we need to search |
| 5066 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5067 | mode: |
| 5068 | all: return all the strings that contain the search term |
| 5069 | last: return the last string that contains the search term |
| 5070 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5071 | num: return the number of times that the searchTerm appears in the log |
| 5072 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5073 | """ |
| 5074 | try: |
| 5075 | assert type( searchTerm ) is str |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5076 | #Build the log paths string |
| 5077 | logPath = '/opt/onos/log/karaf.log.' |
| 5078 | logPaths = '/opt/onos/log/karaf.log' |
| 5079 | for i in range( 1, logNum ): |
| 5080 | logPaths = logPath + str( i ) + " " + logPaths |
| 5081 | cmd = "cat " + logPaths |
| 5082 | if mode == 'all': |
| 5083 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5084 | if mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5085 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5086 | if mode == 'first': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5087 | if startLine != '': |
| 5088 | # 100000000 is just a extreme large number to make sure this function can grep all the lines after startLine |
| 5089 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\' | grep \'" + searchTerm + "\'" + "| head -n 1" |
| 5090 | else: |
| 5091 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5092 | if mode == 'num': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5093 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
You Wang | 118ba58 | 2017-01-02 17:14:43 -0800 | [diff] [blame] | 5094 | num = self.sendline( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5095 | return num |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5096 | if mode == 'total': |
| 5097 | totalLines = self.sendline( "cat /opt/onos/log/karaf.log | wc -l" ) |
| 5098 | return int(totalLines) |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5099 | before = self.sendline( cmd ) |
| 5100 | before = before.splitlines() |
| 5101 | # make sure the returned list only contains the search term |
| 5102 | returnLines = [line for line in before if searchTerm in line] |
| 5103 | return returnLines |
| 5104 | except AssertionError: |
| 5105 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5106 | return None |
| 5107 | except pexpect.EOF: |
| 5108 | main.log.error( self.name + ": EOF exception found" ) |
| 5109 | main.log.error( self.name + ": " + self.handle.before ) |
| 5110 | main.cleanup() |
| 5111 | main.exit() |
| 5112 | except pexpect.TIMEOUT: |
| 5113 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5114 | main.log.error( self.name + ": " + self.handle.before ) |
| 5115 | main.cleanup() |
| 5116 | main.exit() |
| 5117 | except Exception: |
| 5118 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5119 | main.cleanup() |
| 5120 | main.exit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5121 | |
| 5122 | def vplsShow( self, jsonFormat=True ): |
| 5123 | """ |
| 5124 | Description: Returns result of onos:vpls show, which should list the |
| 5125 | configured VPLS networks and the assigned interfaces. |
| 5126 | Optional: |
| 5127 | * jsonFormat: enable json formatting of output |
| 5128 | Returns: |
| 5129 | The output of the command or None on error. |
| 5130 | """ |
| 5131 | try: |
| 5132 | cmdStr = "vpls show" |
| 5133 | if jsonFormat: |
| 5134 | raise NotImplementedError |
| 5135 | cmdStr += " -j" |
| 5136 | handle = self.sendline( cmdStr ) |
| 5137 | assert handle is not None, "Error in sendline" |
| 5138 | assert "Command not found:" not in handle, handle |
| 5139 | return handle |
| 5140 | except AssertionError: |
| 5141 | main.log.exception( "" ) |
| 5142 | return None |
| 5143 | except TypeError: |
| 5144 | main.log.exception( self.name + ": Object not as expected" ) |
| 5145 | return None |
| 5146 | except pexpect.EOF: |
| 5147 | main.log.error( self.name + ": EOF exception found" ) |
| 5148 | main.log.error( self.name + ": " + self.handle.before ) |
| 5149 | main.cleanup() |
| 5150 | main.exit() |
| 5151 | except NotImplementedError: |
| 5152 | main.log.exception( self.name + ": Json output not supported") |
| 5153 | return None |
| 5154 | except Exception: |
| 5155 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5156 | main.cleanup() |
| 5157 | main.exit() |
| 5158 | |
| 5159 | def parseVplsShow( self ): |
| 5160 | """ |
| 5161 | Parse the cli output of 'vpls show' into json output. This is required |
| 5162 | as there is currently no json output available. |
| 5163 | """ |
| 5164 | try: |
| 5165 | output = [] |
| 5166 | raw = self.vplsShow( jsonFormat=False ) |
| 5167 | namePat = "VPLS name: (?P<name>\w+)" |
| 5168 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5169 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5170 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5171 | mIter = re.finditer( pattern, raw ) |
| 5172 | for match in mIter: |
| 5173 | item = {} |
| 5174 | item[ 'name' ] = match.group( 'name' ) |
| 5175 | ifaces = match.group( 'interfaces' ).split( ', ') |
| 5176 | if ifaces == [ "" ]: |
| 5177 | ifaces = [] |
| 5178 | item[ 'interfaces' ] = ifaces |
| 5179 | encap = match.group( 'encap' ) |
| 5180 | if encap != 'NONE': |
| 5181 | item[ 'encapsulation' ] = encap.lower() |
| 5182 | output.append( item ) |
| 5183 | return output |
| 5184 | except Exception: |
| 5185 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5186 | main.cleanup() |
| 5187 | main.exit() |
| 5188 | |
| 5189 | def vplsList( self, jsonFormat=True ): |
| 5190 | """ |
| 5191 | Description: Returns result of onos:vpls list, which should list the |
| 5192 | configured VPLS networks. |
| 5193 | Optional: |
| 5194 | * jsonFormat: enable json formatting of output |
| 5195 | """ |
| 5196 | try: |
| 5197 | cmdStr = "vpls list" |
| 5198 | if jsonFormat: |
| 5199 | raise NotImplementedError |
| 5200 | cmdStr += " -j" |
| 5201 | handle = self.sendline( cmdStr ) |
| 5202 | assert handle is not None, "Error in sendline" |
| 5203 | assert "Command not found:" not in handle, handle |
| 5204 | return handle |
| 5205 | except AssertionError: |
| 5206 | main.log.exception( "" ) |
| 5207 | return None |
| 5208 | except TypeError: |
| 5209 | main.log.exception( self.name + ": Object not as expected" ) |
| 5210 | return None |
| 5211 | except pexpect.EOF: |
| 5212 | main.log.error( self.name + ": EOF exception found" ) |
| 5213 | main.log.error( self.name + ": " + self.handle.before ) |
| 5214 | main.cleanup() |
| 5215 | main.exit() |
| 5216 | except NotImplementedError: |
| 5217 | main.log.exception( self.name + ": Json output not supported") |
| 5218 | return None |
| 5219 | except Exception: |
| 5220 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5221 | main.cleanup() |
| 5222 | main.exit() |
| 5223 | |
| 5224 | def vplsCreate( self, network ): |
| 5225 | """ |
| 5226 | CLI command to create a new VPLS network. |
| 5227 | Required arguments: |
| 5228 | network - String name of the network to create. |
| 5229 | returns: |
| 5230 | main.TRUE on success and main.FALSE on failure |
| 5231 | """ |
| 5232 | try: |
| 5233 | network = str( network ) |
| 5234 | cmdStr = "vpls create " |
| 5235 | cmdStr += network |
| 5236 | output = self.sendline( cmdStr ) |
| 5237 | assert output is not None, "Error in sendline" |
| 5238 | assert "Command not found:" not in output, output |
| 5239 | assert "Error executing command" not in output, output |
| 5240 | assert "VPLS already exists:" not in output, output |
| 5241 | return main.TRUE |
| 5242 | except AssertionError: |
| 5243 | main.log.exception( "" ) |
| 5244 | return main.FALSE |
| 5245 | except TypeError: |
| 5246 | main.log.exception( self.name + ": Object not as expected" ) |
| 5247 | return main.FALSE |
| 5248 | except pexpect.EOF: |
| 5249 | main.log.error( self.name + ": EOF exception found" ) |
| 5250 | main.log.error( self.name + ": " + self.handle.before ) |
| 5251 | main.cleanup() |
| 5252 | main.exit() |
| 5253 | except Exception: |
| 5254 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5255 | main.cleanup() |
| 5256 | main.exit() |
| 5257 | |
| 5258 | def vplsDelete( self, network ): |
| 5259 | """ |
| 5260 | CLI command to delete a VPLS network. |
| 5261 | Required arguments: |
| 5262 | network - Name of the network to delete. |
| 5263 | returns: |
| 5264 | main.TRUE on success and main.FALSE on failure |
| 5265 | """ |
| 5266 | try: |
| 5267 | network = str( network ) |
| 5268 | cmdStr = "vpls delete " |
| 5269 | cmdStr += network |
| 5270 | output = self.sendline( cmdStr ) |
| 5271 | assert output is not None, "Error in sendline" |
| 5272 | assert "Command not found:" not in output, output |
| 5273 | assert "Error executing command" not in output, output |
| 5274 | assert " not found" not in output, output |
| 5275 | return main.TRUE |
| 5276 | except AssertionError: |
| 5277 | main.log.exception( "" ) |
| 5278 | return main.FALSE |
| 5279 | except TypeError: |
| 5280 | main.log.exception( self.name + ": Object not as expected" ) |
| 5281 | return main.FALSE |
| 5282 | except pexpect.EOF: |
| 5283 | main.log.error( self.name + ": EOF exception found" ) |
| 5284 | main.log.error( self.name + ": " + self.handle.before ) |
| 5285 | main.cleanup() |
| 5286 | main.exit() |
| 5287 | except Exception: |
| 5288 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5289 | main.cleanup() |
| 5290 | main.exit() |
| 5291 | |
| 5292 | def vplsAddIface( self, network, iface ): |
| 5293 | """ |
| 5294 | CLI command to add an interface to a VPLS network. |
| 5295 | Required arguments: |
| 5296 | network - Name of the network to add the interface to. |
| 5297 | iface - The ONOS name for an interface. |
| 5298 | returns: |
| 5299 | main.TRUE on success and main.FALSE on failure |
| 5300 | """ |
| 5301 | try: |
| 5302 | network = str( network ) |
| 5303 | iface = str( iface ) |
| 5304 | cmdStr = "vpls add-if " |
| 5305 | cmdStr += network + " " + iface |
| 5306 | output = self.sendline( cmdStr ) |
| 5307 | assert output is not None, "Error in sendline" |
| 5308 | assert "Command not found:" not in output, output |
| 5309 | assert "Error executing command" not in output, output |
| 5310 | assert "already associated to network" not in output, output |
| 5311 | assert "Interface cannot be added." 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 vplsRemIface( self, network, iface ): |
| 5330 | """ |
| 5331 | CLI command to remove an interface from a VPLS network. |
| 5332 | Required arguments: |
| 5333 | network - Name of the network to remove the interface from. |
| 5334 | iface - Name of the interface to remove. |
| 5335 | returns: |
| 5336 | main.TRUE on success and main.FALSE on failure |
| 5337 | """ |
| 5338 | try: |
| 5339 | iface = str( iface ) |
| 5340 | cmdStr = "vpls rem-if " |
| 5341 | cmdStr += network + " " + iface |
| 5342 | output = self.sendline( cmdStr ) |
| 5343 | assert output is not None, "Error in sendline" |
| 5344 | assert "Command not found:" not in output, output |
| 5345 | assert "Error executing command" not in output, output |
| 5346 | assert "is not configured" not in output, output |
| 5347 | return main.TRUE |
| 5348 | except AssertionError: |
| 5349 | main.log.exception( "" ) |
| 5350 | return main.FALSE |
| 5351 | except TypeError: |
| 5352 | main.log.exception( self.name + ": Object not as expected" ) |
| 5353 | return main.FALSE |
| 5354 | except pexpect.EOF: |
| 5355 | main.log.error( self.name + ": EOF exception found" ) |
| 5356 | main.log.error( self.name + ": " + self.handle.before ) |
| 5357 | main.cleanup() |
| 5358 | main.exit() |
| 5359 | except Exception: |
| 5360 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5361 | main.cleanup() |
| 5362 | main.exit() |
| 5363 | |
| 5364 | def vplsClean( self ): |
| 5365 | """ |
| 5366 | Description: Clears the VPLS app configuration. |
| 5367 | Returns: main.TRUE on success and main.FALSE on failure |
| 5368 | """ |
| 5369 | try: |
| 5370 | cmdStr = "vpls clean" |
| 5371 | handle = self.sendline( cmdStr ) |
| 5372 | assert handle is not None, "Error in sendline" |
| 5373 | assert "Command not found:" not in handle, handle |
| 5374 | return handle |
| 5375 | except AssertionError: |
| 5376 | main.log.exception( "" ) |
| 5377 | return main.FALSE |
| 5378 | except TypeError: |
| 5379 | main.log.exception( self.name + ": Object not as expected" ) |
| 5380 | return main.FALSE |
| 5381 | except pexpect.EOF: |
| 5382 | main.log.error( self.name + ": EOF exception found" ) |
| 5383 | main.log.error( self.name + ": " + self.handle.before ) |
| 5384 | main.cleanup() |
| 5385 | main.exit() |
| 5386 | except Exception: |
| 5387 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5388 | main.cleanup() |
| 5389 | main.exit() |
| 5390 | |
| 5391 | def vplsSetEncap( self, network, encapType ): |
| 5392 | """ |
| 5393 | CLI command to add an interface to a VPLS network. |
| 5394 | Required arguments: |
| 5395 | network - Name of the network to create. |
| 5396 | encapType - Type of encapsulation. |
| 5397 | returns: |
| 5398 | main.TRUE on success and main.FALSE on failure |
| 5399 | """ |
| 5400 | try: |
| 5401 | network = str( network ) |
| 5402 | encapType = str( encapType ).upper() |
| 5403 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5404 | cmdStr = "vpls set-encap " |
| 5405 | cmdStr += network + " " + encapType |
| 5406 | output = self.sendline( cmdStr ) |
| 5407 | assert output is not None, "Error in sendline" |
| 5408 | assert "Command not found:" not in output, output |
| 5409 | assert "Error executing command" not in output, output |
| 5410 | assert "already associated to network" not in output, output |
| 5411 | assert "Encapsulation type " not in output, output |
| 5412 | return main.TRUE |
| 5413 | except AssertionError: |
| 5414 | main.log.exception( "" ) |
| 5415 | return main.FALSE |
| 5416 | except TypeError: |
| 5417 | main.log.exception( self.name + ": Object not as expected" ) |
| 5418 | return main.FALSE |
| 5419 | except pexpect.EOF: |
| 5420 | main.log.error( self.name + ": EOF exception found" ) |
| 5421 | main.log.error( self.name + ": " + self.handle.before ) |
| 5422 | main.cleanup() |
| 5423 | main.exit() |
| 5424 | except Exception: |
| 5425 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5426 | main.cleanup() |
| 5427 | main.exit() |
| 5428 | |
| 5429 | def interfaces( self, jsonFormat=True ): |
| 5430 | """ |
| 5431 | Description: Returns result of interfaces command. |
| 5432 | Optional: |
| 5433 | * jsonFormat: enable json formatting of output |
| 5434 | Returns: |
| 5435 | The output of the command or None on error. |
| 5436 | """ |
| 5437 | try: |
| 5438 | cmdStr = "interfaces" |
| 5439 | if jsonFormat: |
| 5440 | #raise NotImplementedError |
| 5441 | cmdStr += " -j" |
| 5442 | handle = self.sendline( cmdStr ) |
| 5443 | assert handle is not None, "Error in sendline" |
| 5444 | assert "Command not found:" not in handle, handle |
| 5445 | return handle |
| 5446 | except AssertionError: |
| 5447 | main.log.exception( "" ) |
| 5448 | return None |
| 5449 | except TypeError: |
| 5450 | main.log.exception( self.name + ": Object not as expected" ) |
| 5451 | return None |
| 5452 | except pexpect.EOF: |
| 5453 | main.log.error( self.name + ": EOF exception found" ) |
| 5454 | main.log.error( self.name + ": " + self.handle.before ) |
| 5455 | main.cleanup() |
| 5456 | main.exit() |
| 5457 | except NotImplementedError: |
| 5458 | main.log.exception( self.name + ": Json output not supported") |
| 5459 | return None |
| 5460 | except Exception: |
| 5461 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5462 | main.cleanup() |
| 5463 | main.exit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5464 | |
| 5465 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
| 5466 | ''' |
| 5467 | Get the timestamp of searchTerm from karaf log. |
| 5468 | |
| 5469 | Arguments: |
| 5470 | splitTerm_before and splitTerm_after: |
| 5471 | |
| 5472 | The terms that split the string that contains the timeStamp of |
| 5473 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5474 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5475 | and the splitTerm_after is "x" |
| 5476 | |
| 5477 | others: |
| 5478 | |
| 5479 | plz look at the "logsearch" Function in onosclidriver.py |
| 5480 | |
| 5481 | |
| 5482 | ''' |
| 5483 | if logNum < 0: |
| 5484 | main.log.error("Get wrong log number ") |
| 5485 | return main.ERROR |
| 5486 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
| 5487 | if len(lines) == 0: |
| 5488 | main.log.warn( "Captured timestamp string is empty" ) |
| 5489 | return main.ERROR |
| 5490 | lines = lines[ 0 ] |
| 5491 | try: |
| 5492 | assert type(lines) is str |
| 5493 | # get the target value |
| 5494 | line = lines.split( splitTerm_before ) |
| 5495 | key = line[ 1 ].split( splitTerm_after ) |
| 5496 | return int( key[ 0 ] ) |
| 5497 | except IndexError: |
| 5498 | main.log.warn( "Index Error!" ) |
| 5499 | return main.ERROR |
| 5500 | except AssertionError: |
| 5501 | main.log.warn( "Search Term Not Found " ) |
| 5502 | return main.ERROR |