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 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | OCT 13 2014 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5 | Copyright 2014 Open Networking Foundation (ONF) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 6 | |
| 7 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 8 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 9 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 10 | |
| 11 | TestON is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 14 | (at your option) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 15 | |
| 16 | TestON is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 23 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 25 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 26 | This driver enters the onos> prompt to issue commands. |
| 27 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 28 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 29 | functions and document properly. |
| 30 | |
| 31 | If you are a contributor to the driver, please |
| 32 | list your email here for future contact: |
| 33 | |
| 34 | jhall@onlab.us |
| 35 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 36 | shreya@onlab.us |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 37 | jeremyr@opennetworking.org |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 38 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 39 | import pexpect |
| 40 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 41 | import json |
| 42 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 43 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 44 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 45 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 46 | from core.graph import Graph |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 47 | from cStringIO import StringIO |
| 48 | from itertools import izip |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 49 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 51 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | def __init__( self ): |
| 53 | """ |
| 54 | Initialize client |
| 55 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 56 | self.name = None |
| 57 | self.home = None |
| 58 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 59 | self.karafUser = None |
| 60 | self.karafPass = None |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 61 | self.dockerPrompt = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 62 | self.graph = Graph() |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 63 | super( OnosCliDriver, self ).__init__() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 64 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 65 | def checkOptions( self, var, defaultVar ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 66 | if var is None or var == "": |
| 67 | return defaultVar |
| 68 | return var |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 69 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 70 | def connect( self, **connectargs ): |
| 71 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 72 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 73 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 74 | try: |
| 75 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | vars( self )[ key ] = connectargs[ key ] |
Author Name | e1c7998 | 2019-06-04 23:38:42 +0100 | [diff] [blame] | 77 | self.karafPrompt = self.user_name + "@root >" |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 78 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 79 | for key in self.options: |
| 80 | if key == "home": |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 81 | self.home = self.options[ key ] |
| 82 | elif key == "karaf_username": |
| 83 | self.karafUser = self.options[ key ] |
| 84 | elif key == "karaf_password": |
| 85 | self.karafPass = self.options[ key ] |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 86 | elif key == "docker_prompt": |
| 87 | self.dockerPrompt = self.options[ key ] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 88 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 89 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 90 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 91 | self.dockerPrompt = self.checkOptions( self.dockerPrompt, "~/onos#" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 92 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 93 | for key in self.options: |
| 94 | if key == 'onosIp': |
| 95 | self.onosIp = self.options[ 'onosIp' ] |
| 96 | break |
| 97 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 98 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 99 | |
| 100 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 101 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 102 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 103 | else: |
| 104 | main.log.info( self.name + |
| 105 | ": Trying to connect to " + |
| 106 | self.ip_address ) |
| 107 | |
| 108 | except KeyError: |
| 109 | main.log.info( "Invalid host name," + |
| 110 | " connecting to local host instead" ) |
| 111 | self.ip_address = 'localhost' |
| 112 | except Exception as inst: |
| 113 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 114 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 115 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 116 | user_name=self.user_name, |
| 117 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 118 | port=self.port, |
| 119 | pwd=self.pwd, |
| 120 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 121 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 122 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 123 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 124 | if self.handle: |
| 125 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 126 | else: |
| 127 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 128 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 129 | except TypeError: |
| 130 | main.log.exception( self.name + ": Object not as expected" ) |
| 131 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 132 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 133 | main.log.error( self.name + ": EOF exception found" ) |
| 134 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 135 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 136 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 137 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 138 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 139 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 140 | def disconnect( self ): |
| 141 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 142 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 143 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 144 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 145 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 146 | if self.handle: |
| 147 | i = self.logout() |
| 148 | if i == main.TRUE: |
| 149 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 150 | for l in range( 3 ): |
| 151 | p = self.handle.expect( [ self.prompt, self.dockerPrompt ] ) |
| 152 | if p == 1: |
| 153 | self.inDocker = False |
| 154 | self.handle.sendline( "exit" ) |
| 155 | j = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=3 ) |
| 156 | if j == 0: |
| 157 | return response |
| 158 | response = main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 159 | except TypeError: |
| 160 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 161 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 162 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 163 | main.log.error( self.name + ": EOF exception found" ) |
| 164 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 165 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 166 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 167 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 168 | except Exception: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 169 | main.log.exception( self.name + ": disconnection failed from the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 170 | response = main.FALSE |
| 171 | return response |
| 172 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 173 | def logout( self ): |
| 174 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 175 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 176 | Returns main.TRUE if exited CLI and |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 177 | main.FALSE on timeout (not guranteed you are disconnected) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 178 | None on TypeError |
| 179 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 180 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 181 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 182 | if self.handle: |
| 183 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 184 | i = self.handle.expect( [ self.karafPrompt, |
| 185 | self.Prompt(), |
| 186 | pexpect.TIMEOUT ], |
| 187 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 188 | timeout=10 ) |
| 189 | if i == 0: # In ONOS CLI |
| 190 | self.handle.sendline( "logout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 191 | j = self.handle.expect( [ self.Prompt(), |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 192 | "Command not found:", |
| 193 | pexpect.TIMEOUT ] ) |
| 194 | if j == 0: # Successfully logged out |
| 195 | return main.TRUE |
| 196 | elif j == 1 or j == 2: |
| 197 | # ONOS didn't fully load, and logout command isn't working |
| 198 | # or the command timed out |
| 199 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 200 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 201 | self.handle.expect( self.Prompt() ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 202 | except pexpect.TIMEOUT: |
| 203 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 204 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 205 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 206 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 207 | repr( self.handle.before ) ) |
| 208 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 209 | elif i == 1: # not in CLI |
| 210 | return main.TRUE |
steven30801 | e42f1fb | 2019-01-17 11:31:45 +0800 | [diff] [blame] | 211 | elif i == 2: # Timeout |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 212 | return main.FALSE |
| 213 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 214 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 215 | except TypeError: |
| 216 | main.log.exception( self.name + ": Object not as expected" ) |
| 217 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 218 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 219 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 220 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 221 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 222 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 223 | main.log.error( self.name + |
| 224 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 225 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 226 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 227 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 228 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 229 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 230 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 231 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 232 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 233 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 234 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 235 | try: |
| 236 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 237 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 238 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 239 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 240 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 241 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 242 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 243 | # and that this driver will have to change accordingly |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 244 | self.handle.expect( str( cellname ) ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 245 | handleBefore = self.handle.before |
| 246 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 247 | # Get the rest of the handle |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 248 | self.handle.sendline( "" ) |
| 249 | self.handle.expect( self.prompt ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 250 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 251 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 252 | main.log.info( "Cell call returned: " + handleBefore + |
| 253 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 254 | |
| 255 | return main.TRUE |
| 256 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 257 | except TypeError: |
| 258 | main.log.exception( self.name + ": Object not as expected" ) |
| 259 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 260 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 261 | main.log.error( self.name + ": eof exception found" ) |
| 262 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 263 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 264 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 265 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 266 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 268 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 269 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 270 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 271 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 272 | by user would be used to set the current karaf shell idle timeout. |
| 273 | 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] | 274 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 275 | Below is an example to start a session with 60 seconds idle timeout |
| 276 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 277 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 278 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 279 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 280 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 281 | Note: karafTimeout is left as str so that this could be read |
| 282 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 283 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 284 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 285 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 286 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 287 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 288 | x = self.handle.expect( [ self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 289 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 290 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 291 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 292 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 293 | # Not in CLI so login |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 294 | if self.inDocker: |
| 295 | # The Docker does not have all the wrapper scripts |
| 296 | startCliCommand = "ssh -p 8101 -o StrictHostKeyChecking=no %s@localhost" % self.karafUser |
| 297 | elif waitForStart: |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 298 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 299 | startCliCommand = "onos-wait-for-start " + str( ONOSIp ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 300 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 301 | startCliCommand = "onos " + str( ONOSIp ) |
| 302 | self.handle.sendline( startCliCommand ) |
| 303 | tries = 0 |
| 304 | while tries < 5: |
| 305 | i = self.handle.expect( [ |
| 306 | self.karafPrompt, |
| 307 | "Password:", |
| 308 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 309 | |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 310 | if i == 0: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 311 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 312 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 313 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 314 | "config:property-set -p org.apache.karaf.shell\ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 315 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 316 | karafTimeout ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 317 | self.handle.expect( self.karafPrompt ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 318 | return main.TRUE |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 319 | elif i == 1: |
| 320 | main.log.info( str( ONOSIp ) + " CLI asking for password" ) |
| 321 | main.log.debug( "Sending %s" % self.karafPass ) |
| 322 | self.handle.sendline( self.karafPass ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 323 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 324 | # If failed, send ctrl+c to process and try again |
| 325 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 326 | self.handle.send( "\x03" ) |
| 327 | self.handle.sendline( startCliCommand ) |
| 328 | tries += 1 |
| 329 | main.log.error( "Connection to CLI " + str( ONOSIp ) + " timeout" ) |
| 330 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 331 | except TypeError: |
| 332 | main.log.exception( self.name + ": Object not as expected" ) |
| 333 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 334 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 335 | main.log.error( self.name + ": EOF exception found" ) |
| 336 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 337 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 338 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 339 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 340 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 341 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 342 | def startCellCli( self, karafTimeout="", |
| 343 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 344 | """ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 345 | Start CLI on onos cell handle. |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 346 | |
| 347 | karafTimeout is an optional argument. karafTimeout value passed |
| 348 | by user would be used to set the current karaf shell idle timeout. |
| 349 | Note that when ever this property is modified the shell will exit and |
| 350 | the subsequent login would reflect new idle timeout. |
| 351 | Below is an example to start a session with 60 seconds idle timeout |
| 352 | ( input value is in milliseconds ): |
| 353 | |
| 354 | tValue = "60000" |
| 355 | |
| 356 | Note: karafTimeout is left as str so that this could be read |
| 357 | and passed to startOnosCli from PARAMS file as str. |
| 358 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 359 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 360 | try: |
| 361 | self.handle.sendline( "" ) |
| 362 | x = self.handle.expect( [ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 363 | self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 364 | |
| 365 | if x == 1: |
| 366 | main.log.info( "ONOS cli is already running" ) |
| 367 | return main.TRUE |
| 368 | |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 369 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 370 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 371 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 372 | self.karafPrompt, |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 373 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 374 | |
| 375 | if i == 0: |
| 376 | main.log.info( self.name + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 377 | if karafTimeout: # FIXME: This doesn't look right |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 378 | self.handle.sendline( |
| 379 | "config:property-set -p org.apache.karaf.shell\ |
| 380 | sshIdleTimeout " + |
| 381 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 382 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 383 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 384 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 385 | return main.TRUE |
| 386 | else: |
| 387 | # If failed, send ctrl+c to process and try again |
| 388 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 389 | self.handle.send( "\x03" ) |
| 390 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 391 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 392 | timeout=30 ) |
| 393 | if i == 0: |
| 394 | main.log.info( self.name + " CLI Started " + |
| 395 | "successfully after retry attempt" ) |
| 396 | if karafTimeout: |
| 397 | self.handle.sendline( |
| 398 | "config:property-set -p org.apache.karaf.shell\ |
| 399 | sshIdleTimeout " + |
| 400 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 401 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 402 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 403 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 404 | return main.TRUE |
| 405 | else: |
| 406 | main.log.error( "Connection to CLI " + |
| 407 | self.name + " timeout" ) |
| 408 | return main.FALSE |
| 409 | |
| 410 | except TypeError: |
| 411 | main.log.exception( self.name + ": Object not as expected" ) |
| 412 | return None |
| 413 | except pexpect.EOF: |
| 414 | main.log.error( self.name + ": EOF exception found" ) |
| 415 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 416 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 417 | except Exception: |
| 418 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 419 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 420 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 421 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 422 | """ |
| 423 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 424 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 425 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 426 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 427 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 428 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 429 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 430 | """ |
| 431 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 432 | lvlStr = "" |
| 433 | if level: |
| 434 | lvlStr = "--level=" + level |
| 435 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 436 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 437 | self.handle.expect( "log:log" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 438 | self.handle.expect( self.karafPrompt ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 439 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 440 | response = self.handle.before |
| 441 | if re.search( "Error", response ): |
Jon Hall | dac3eae | 2020-06-05 12:04:06 -0700 | [diff] [blame] | 442 | main.log.debug( response ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 443 | return main.FALSE |
| 444 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 445 | except pexpect.TIMEOUT: |
| 446 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 447 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 448 | if noExit: |
| 449 | main.cleanup() |
| 450 | return None |
| 451 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 452 | main.cleanAndExit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 453 | except pexpect.EOF: |
| 454 | main.log.error( self.name + ": EOF exception found" ) |
| 455 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 456 | if noExit: |
| 457 | main.cleanup() |
| 458 | return None |
| 459 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 460 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 461 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 462 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 463 | if noExit: |
| 464 | main.cleanup() |
| 465 | return None |
| 466 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 467 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 468 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 469 | def clearBuffer( self, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 470 | """ |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 471 | Test cli connection and clear any left over output in the buffer |
| 472 | Optional Arguments: |
| 473 | debug - Defaults to False. If True, will enable debug logging. |
| 474 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 475 | before a timeout. |
| 476 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 477 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 478 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 479 | # Try to reconnect if disconnected from cli |
| 480 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 481 | i = self.handle.expect( [ self.karafPrompt, |
| 482 | self.Prompt(), |
| 483 | pexpect.TIMEOUT ] ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 484 | response = self.handle.before |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 485 | if i == 1: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 486 | main.log.error( self.name + ": onos cli session closed. " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 487 | if self.onosIp: |
| 488 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 489 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 490 | if reconnectResult: |
| 491 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 492 | else: |
| 493 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 494 | if noExit: |
| 495 | return None |
| 496 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 497 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 498 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 499 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 500 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 501 | main.log.warn( "Timeout when testing cli responsiveness" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 502 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 503 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 504 | self.handle.expect( self.karafPrompt ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 505 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 506 | response += self.handle.before |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 507 | if debug: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 508 | main.log.debug( self.name + ": Raw output from sending ''" ) |
| 509 | main.log.debug( self.name + ": " + repr( response ) ) |
| 510 | except pexpect.TIMEOUT: |
| 511 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 512 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 513 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 514 | self.handle.expect( self.karafPrompt ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 515 | return None |
| 516 | except pexpect.EOF: |
| 517 | main.log.error( self.name + ": EOF exception found" ) |
| 518 | main.log.error( self.name + ": " + self.handle.before ) |
| 519 | if noExit: |
| 520 | return None |
| 521 | else: |
| 522 | main.cleanAndExit() |
| 523 | except Exception: |
| 524 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 525 | if noExit: |
| 526 | return None |
| 527 | else: |
| 528 | main.cleanAndExit() |
| 529 | |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 530 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True, expectJson=False ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 531 | """ |
| 532 | A wrapper around pexpect's sendline/expect. Will return all the output from a given command |
| 533 | |
| 534 | Required Arguments: |
| 535 | cmdStr - String to send to the pexpect session |
| 536 | |
| 537 | Optional Arguments: |
| 538 | showResponse - Defaults to False. If True will log the response. |
| 539 | debug - Defaults to False. If True, will enable debug logging. |
| 540 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 541 | before a timeout. |
| 542 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 543 | closed channel, but instead return None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 544 | relaxedRegex - Defaults to True. If there is a pipe in the command send, will only try to match the last part of the piped command. |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 545 | |
| 546 | Warning: There are no sanity checking to commands sent using this method. |
| 547 | |
| 548 | """ |
| 549 | try: |
| 550 | # Try to reconnect if disconnected from cli |
| 551 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
| 552 | if debug: |
| 553 | # NOTE: This adds an average of .4 seconds per call |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 554 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 555 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 556 | self.handle.sendline( cmdStr ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 557 | self.handle.expect( self.karafPrompt, timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 558 | response = self.handle.before |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 559 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 560 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 561 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 562 | main.log.debug( self.name + ": Raw output" ) |
| 563 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 564 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 565 | response = self.cleanOutput( response, debug=debug ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 566 | # Remove control codes from karaf 4.2.1 |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 567 | karafEscape = re.compile( r"('(0|1)~\'|\r\r\r\n\x1b\[A\x1b\[79C(x|\s)?|\x1b(>|=~?)|\x1b\[90m~)" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 568 | response = karafEscape.sub( '', response ) |
| 569 | if debug: |
| 570 | main.log.debug( self.name + ": karafEscape output" ) |
| 571 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 572 | # Remove ANSI color control strings from output |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 573 | response = self.cleanOutput( response, debug ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 574 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 575 | # Remove ANSI color control strings from output |
Jon Hall | cf31d0f | 2018-12-13 11:18:48 -0800 | [diff] [blame] | 576 | # NOTE: karaf is sometimes adding a single character then two |
| 577 | # backspaces and sometimes adding 2 characters with 2 backspaces?? |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 578 | backspaceEscape = re.compile( r'((.|\s)\x08)' ) |
| 579 | unchanged = False |
| 580 | while not unchanged: |
| 581 | old = response |
| 582 | response = backspaceEscape.sub( '', response, count=1 ) |
| 583 | if debug: |
| 584 | main.log.debug( self.name + ": backspaceEscape output" ) |
| 585 | main.log.debug( self.name + ": " + repr( response ) ) |
| 586 | unchanged = old == response |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 587 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 588 | # Remove extra return chars that get added |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 589 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 590 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 591 | main.log.debug( self.name + ": Removed extra returns " + |
| 592 | "from output" ) |
| 593 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 594 | |
| 595 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 596 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 597 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 598 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 599 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 600 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 601 | # parse for just the output, remove the cmd from response |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 602 | cmdPattern = cmdStr.strip() |
| 603 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 604 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 605 | if relaxedRegex: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 606 | cmdPattern = cmdPattern.split( '|' )[ -1 ].strip() |
| 607 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 608 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 609 | # This was added because karaf 4.2 is stripping some characters from the command echo |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 610 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 611 | if expectJson: |
| 612 | main.log.warn( "Relaxed Regex: Searching for a json string amongst the output" ) |
| 613 | jsonPattern = r'\{.*\}' |
| 614 | match = re.search( jsonPattern, output[ 0 ] ) |
| 615 | if match: |
| 616 | output = [ '' , match.group( 0 ) ] # We expect a list with the second element to be the output |
Jon Hall | 39e3ffe | 2018-12-05 11:40:29 -0800 | [diff] [blame] | 617 | if len( output ) < 2: |
| 618 | main.log.warn( "Relaxing regex match to last 5 characters of the sent command" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 619 | cmdPattern = cmdPattern[ -5: ] |
| 620 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 621 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
| 622 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 623 | else: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 624 | output = response.split( cmdPattern, 1 ) |
| 625 | if len( output ) < 2: # TODO: Should we do this without the relaxedRegex flag? |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 626 | main.log.warn( "Relaxing regex match to last 5 characters of the sent command" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 627 | output = response.split( cmdPattern[ -5: ], 1 ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 628 | if output: |
| 629 | if debug: |
| 630 | main.log.debug( self.name + ": split output" ) |
| 631 | for r in output: |
| 632 | main.log.debug( self.name + ": " + repr( r ) ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 633 | if len( output ) == 1: |
| 634 | main.log.error( "Could not remove sent command echo from output" ) |
| 635 | return output |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 636 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 637 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 638 | main.log.info( "Response from ONOS: {}".format( output ) ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 639 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 640 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 641 | except pexpect.TIMEOUT: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 642 | main.log.error( self.name + ": ONOS timeout" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 643 | if debug: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 644 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 6b5e5ba | 2019-01-31 15:32:40 -0800 | [diff] [blame] | 645 | self.exitFromCmd( self.karafPrompt, 100 ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 646 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 647 | except IndexError: |
| 648 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 649 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 650 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 651 | except TypeError: |
| 652 | main.log.exception( self.name + ": Object not as expected" ) |
| 653 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 654 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 655 | main.log.error( self.name + ": EOF exception found" ) |
| 656 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 657 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 658 | return None |
| 659 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 660 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 661 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 662 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 663 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 664 | return None |
| 665 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 666 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 667 | |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 668 | def lineCount( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
| 669 | """ |
| 670 | A wrapper around sendline(). Will return the number of lines returned or None on error |
| 671 | |
| 672 | Required Arguments: |
| 673 | cmdStr - String to send to the pexpect session |
| 674 | |
| 675 | Optional Arguments: |
| 676 | showResponse - Defaults to False. If True will log the response. |
| 677 | debug - Defaults to False. If True, will enable debug logging. |
| 678 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 679 | before a timeout. |
| 680 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 681 | closed channel, but instead return None |
| 682 | relaxedRegex - Defaults to True. If there is a pipe in the command send, will only try to match the last part of the piped command. |
| 683 | |
| 684 | Warning: There are no sanity checking to commands sent using this method. |
| 685 | |
| 686 | """ |
| 687 | try: |
| 688 | numLines = self.sendline( cmdStr, showResponse, debug, timeout, noExit, relaxedRegex ) |
You Wang | 0ce8e0c | 2019-02-22 12:22:26 -0800 | [diff] [blame] | 689 | parsed = re.search( "(\d+)", numLines ) |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 690 | if not parsed: |
| 691 | main.log.error( "Warning, output of karaf's wc may have changed" ) |
| 692 | return None |
| 693 | return parsed.group( 1 ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 694 | except IndexError: |
| 695 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 696 | main.log.debug( "response: {}".format( repr( numLines ) ) ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 697 | return None |
| 698 | except TypeError: |
| 699 | main.log.exception( self.name + ": Object not as expected" ) |
| 700 | return None |
| 701 | except Exception: |
| 702 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 703 | if noExit: |
| 704 | return None |
| 705 | else: |
| 706 | main.cleanAndExit() |
| 707 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 708 | # IMPORTANT NOTE: |
| 709 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 710 | # the cli command changing 'a:b' with 'aB'. |
| 711 | # Ex ) onos:topology > onosTopology |
| 712 | # onos:links > onosLinks |
| 713 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 714 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 715 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 716 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 717 | Adds a new cluster node by ID and address information. |
| 718 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 719 | * nodeId |
| 720 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 721 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 722 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 723 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 724 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 725 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 726 | str( ONOSIp ) + " " + str( tcpPort ) |
| 727 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 728 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 729 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 730 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 731 | main.log.error( self.name + ": Error in adding node" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 732 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 733 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 734 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 735 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 736 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 737 | except AssertionError: |
| 738 | main.log.exception( "" ) |
| 739 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 740 | except TypeError: |
| 741 | main.log.exception( self.name + ": Object not as expected" ) |
| 742 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 743 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 744 | main.log.error( self.name + ": EOF exception found" ) |
| 745 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 746 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 747 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 748 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 749 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 750 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 751 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 752 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 753 | Removes a cluster by ID |
| 754 | Issues command: 'remove-node [<node-id>]' |
| 755 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 756 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 757 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 758 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 759 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 760 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 761 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 762 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 763 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 764 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 765 | main.log.error( self.name + ": Error in removing node" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 766 | main.log.error( handle ) |
| 767 | return main.FALSE |
| 768 | else: |
| 769 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 770 | except AssertionError: |
| 771 | main.log.exception( "" ) |
| 772 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 773 | except TypeError: |
| 774 | main.log.exception( self.name + ": Object not as expected" ) |
| 775 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 776 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 777 | main.log.error( self.name + ": EOF exception found" ) |
| 778 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 779 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 780 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 781 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 782 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 783 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 784 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 785 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 786 | List the nodes currently visible |
| 787 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 788 | Optional argument: |
| 789 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 790 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 791 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 792 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 793 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 794 | cmdStr += " -j" |
| 795 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 796 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 797 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 798 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 799 | except AssertionError: |
| 800 | main.log.exception( "" ) |
| 801 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 802 | except TypeError: |
| 803 | main.log.exception( self.name + ": Object not as expected" ) |
| 804 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 805 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 806 | main.log.error( self.name + ": EOF exception found" ) |
| 807 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 808 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 809 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 810 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 811 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 812 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 813 | def topology( self ): |
| 814 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 815 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 816 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 817 | Return: |
| 818 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 819 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 820 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 821 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 822 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 823 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 824 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 825 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 826 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 827 | except AssertionError: |
| 828 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 829 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 830 | except TypeError: |
| 831 | main.log.exception( self.name + ": Object not as expected" ) |
| 832 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 833 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 834 | main.log.error( self.name + ": EOF exception found" ) |
| 835 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 836 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 837 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 838 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 839 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 840 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 841 | def deviceRemove( self, deviceId ): |
| 842 | """ |
| 843 | Removes particular device from storage |
| 844 | |
| 845 | TODO: refactor this function |
| 846 | """ |
| 847 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 848 | cmdStr = "device-remove " + str( deviceId ) |
| 849 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 850 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 851 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 852 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 853 | main.log.error( self.name + ": Error in removing device" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 854 | main.log.error( handle ) |
| 855 | return main.FALSE |
| 856 | else: |
| 857 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 858 | except AssertionError: |
| 859 | main.log.exception( "" ) |
| 860 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 861 | except TypeError: |
| 862 | main.log.exception( self.name + ": Object not as expected" ) |
| 863 | return None |
| 864 | except pexpect.EOF: |
| 865 | main.log.error( self.name + ": EOF exception found" ) |
| 866 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 867 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 868 | except Exception: |
| 869 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 870 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 871 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 872 | def devices( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 873 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 874 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 875 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 876 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 877 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 878 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 879 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 880 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 881 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 882 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 883 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 884 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 885 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 886 | except AssertionError: |
| 887 | main.log.exception( "" ) |
| 888 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 889 | except TypeError: |
| 890 | main.log.exception( self.name + ": Object not as expected" ) |
| 891 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 892 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 893 | main.log.error( self.name + ": EOF exception found" ) |
| 894 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 895 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 896 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 897 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 898 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 899 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 900 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 901 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 902 | This balances the devices across all controllers |
| 903 | by issuing command: 'onos> onos:balance-masters' |
| 904 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 905 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 906 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 907 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 908 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 909 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 910 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 911 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 912 | main.log.error( self.name + ": Error in balancing masters" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 913 | main.log.error( handle ) |
| 914 | return main.FALSE |
| 915 | else: |
| 916 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 917 | except AssertionError: |
| 918 | main.log.exception( "" ) |
| 919 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 920 | except TypeError: |
| 921 | main.log.exception( self.name + ": Object not as expected" ) |
| 922 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 923 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 924 | main.log.error( self.name + ": EOF exception found" ) |
| 925 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 926 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 927 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 928 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 929 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 930 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 931 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 932 | """ |
| 933 | Returns the output of the masters command. |
| 934 | Optional argument: |
| 935 | * jsonFormat - boolean indicating if you want output in json |
| 936 | """ |
| 937 | try: |
| 938 | cmdStr = "onos:masters" |
| 939 | if jsonFormat: |
| 940 | cmdStr += " -j" |
| 941 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 942 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 943 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 944 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 945 | except AssertionError: |
| 946 | main.log.exception( "" ) |
| 947 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 948 | except TypeError: |
| 949 | main.log.exception( self.name + ": Object not as expected" ) |
| 950 | return None |
| 951 | except pexpect.EOF: |
| 952 | main.log.error( self.name + ": EOF exception found" ) |
| 953 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 954 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 955 | except Exception: |
| 956 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 957 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 958 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 959 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 960 | """ |
| 961 | Uses the master command to check that the devices' leadership |
| 962 | is evenly divided |
| 963 | |
| 964 | Dependencies: checkMasters() and summary() |
| 965 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 966 | Returns main.TRUE if the devices are balanced |
| 967 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 968 | Exits on Exception |
| 969 | Returns None on TypeError |
| 970 | """ |
| 971 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 972 | summaryOutput = self.summary() |
| 973 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 974 | except ( TypeError, ValueError ): |
| 975 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 976 | return None |
| 977 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 978 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 979 | mastersOutput = self.checkMasters() |
| 980 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 981 | first = masters[ 0 ][ "size" ] |
| 982 | for master in masters: |
| 983 | totalOwnedDevices += master[ "size" ] |
| 984 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 985 | main.log.error( "Mastership not balanced" ) |
| 986 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 987 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 988 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 989 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 990 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 991 | except ( TypeError, ValueError ): |
| 992 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 993 | return None |
| 994 | except pexpect.EOF: |
| 995 | main.log.error( self.name + ": EOF exception found" ) |
| 996 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 997 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 998 | except Exception: |
| 999 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1000 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1001 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1002 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1003 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1004 | Lists all core links |
| 1005 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1006 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1007 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1008 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1009 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1010 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1011 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1012 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1013 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1014 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1015 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1016 | except AssertionError: |
| 1017 | main.log.exception( "" ) |
| 1018 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1019 | except TypeError: |
| 1020 | main.log.exception( self.name + ": Object not as expected" ) |
| 1021 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1022 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1023 | main.log.error( self.name + ": EOF exception found" ) |
| 1024 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1025 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1026 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1027 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1028 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1029 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1030 | def ports( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1031 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1032 | Lists all ports |
| 1033 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1034 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1035 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1036 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1037 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1038 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1039 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1040 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1041 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1042 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1043 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1044 | except AssertionError: |
| 1045 | main.log.exception( "" ) |
| 1046 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1047 | except TypeError: |
| 1048 | main.log.exception( self.name + ": Object not as expected" ) |
| 1049 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1050 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1051 | main.log.error( self.name + ": EOF exception found" ) |
| 1052 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1053 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1054 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1055 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1056 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1057 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1058 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1059 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1060 | Lists all devices and the controllers with roles assigned to them |
| 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 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1064 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1065 | cmdStr = "roles" |
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 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1069 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1070 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1071 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1072 | except AssertionError: |
| 1073 | main.log.exception( "" ) |
| 1074 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1075 | except TypeError: |
| 1076 | main.log.exception( self.name + ": Object not as expected" ) |
| 1077 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1078 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1079 | main.log.error( self.name + ": EOF exception found" ) |
| 1080 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1081 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1082 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1083 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1084 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1085 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1086 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1087 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1088 | Given the a string containing the json representation of the "roles" |
| 1089 | cli command and a partial or whole device id, returns a json object |
| 1090 | containing the roles output for the first device whose id contains |
| 1091 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1092 | |
| 1093 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1094 | A dict of the role assignments for the given device or |
| 1095 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1096 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1097 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1098 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1099 | return None |
| 1100 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1101 | rawRoles = self.roles() |
| 1102 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1103 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1104 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1105 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1106 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1107 | return device |
| 1108 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1109 | except ( TypeError, ValueError ): |
| 1110 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1111 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 1112 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1113 | main.log.error( self.name + ": EOF exception found" ) |
| 1114 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1115 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1116 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1117 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1118 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1119 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1120 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1121 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1122 | Iterates through each device and checks if there is a master assigned |
| 1123 | Returns: main.TRUE if each device has a master |
| 1124 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1125 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1126 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1127 | rawRoles = self.roles() |
| 1128 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1129 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1130 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1131 | # print device |
| 1132 | if device[ 'master' ] == "none": |
| 1133 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1134 | return main.FALSE |
| 1135 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1136 | except ( TypeError, ValueError ): |
| 1137 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1138 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1139 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1140 | main.log.error( self.name + ": EOF exception found" ) |
| 1141 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1142 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1143 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1144 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1145 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1146 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1147 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1148 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1149 | Returns string of paths, and the cost. |
| 1150 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1151 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1152 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1153 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1154 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1155 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1156 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1157 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1158 | main.log.error( self.name + ": Error in getting paths" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1159 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1160 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1161 | path = handle.split( ";" )[ 0 ] |
| 1162 | cost = handle.split( ";" )[ 1 ] |
| 1163 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1164 | except AssertionError: |
| 1165 | main.log.exception( "" ) |
| 1166 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1167 | except TypeError: |
| 1168 | main.log.exception( self.name + ": Object not as expected" ) |
| 1169 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1170 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1171 | main.log.error( self.name + ": EOF exception found" ) |
| 1172 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1173 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1174 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1175 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1176 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1177 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1178 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1179 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1180 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1181 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1182 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1183 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1184 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1185 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1186 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1187 | cmdStr += " -j" |
| 1188 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1189 | if handle: |
| 1190 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1191 | # TODO: Maybe make this less hardcoded |
| 1192 | # ConsistentMap Exceptions |
| 1193 | assert "org.onosproject.store.service" not in handle |
| 1194 | # Node not leader |
| 1195 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1196 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1197 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1198 | main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1199 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1200 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1201 | except TypeError: |
| 1202 | main.log.exception( self.name + ": Object not as expected" ) |
| 1203 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1204 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1205 | main.log.error( self.name + ": EOF exception found" ) |
| 1206 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1207 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1208 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1209 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1210 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1211 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1212 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1213 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1214 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1215 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1216 | 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] | 1217 | partial mac address |
| 1218 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1219 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1220 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1221 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1222 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1223 | return None |
| 1224 | else: |
| 1225 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1226 | rawHosts = self.hosts() |
| 1227 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1228 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1229 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1230 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1231 | if not host: |
| 1232 | pass |
| 1233 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1234 | return host |
| 1235 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1236 | except ( TypeError, ValueError ): |
| 1237 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1238 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1239 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1240 | main.log.error( self.name + ": EOF exception found" ) |
| 1241 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1242 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1243 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1244 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1245 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1246 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1247 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1248 | """ |
| 1249 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1250 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1251 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1252 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1253 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1254 | IMPORTANT: |
| 1255 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1256 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1257 | Furthermore, it assumes that value of VLAN is '-1' |
| 1258 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1259 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1260 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1261 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1262 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1263 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1264 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1265 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1266 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1267 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1268 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1269 | i = iter( str( hostHex ) ) |
| 1270 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1271 | hostHex = hostHex + "/-1" |
| 1272 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1273 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1274 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1275 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1276 | except TypeError: |
| 1277 | main.log.exception( self.name + ": Object not as expected" ) |
| 1278 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1279 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1280 | main.log.error( self.name + ": EOF exception found" ) |
| 1281 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1282 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1283 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1284 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1285 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1286 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1287 | def verifyHostLocation( self, hostIp, location ): |
| 1288 | """ |
| 1289 | Description: |
| 1290 | Verify the host given is discovered in all locations expected |
| 1291 | Required: |
| 1292 | hostIp: IP address of the host |
| 1293 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1294 | Could be a string or list |
| 1295 | Returns: |
| 1296 | main.TRUE if host is discovered on all locations provided |
| 1297 | main.FALSE otherwise |
| 1298 | """ |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1299 | locations = [ location ] if isinstance( location, str ) else location |
| 1300 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1301 | try: |
| 1302 | hosts = self.hosts() |
| 1303 | hosts = json.loads( hosts ) |
| 1304 | targetHost = None |
| 1305 | for host in hosts: |
| 1306 | if hostIp in host[ "ipAddresses" ]: |
| 1307 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1308 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1309 | result = main.TRUE |
| 1310 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1311 | for loc in locations: |
| 1312 | discovered = False |
| 1313 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1314 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1315 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1316 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1317 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1318 | break |
| 1319 | if discovered: |
| 1320 | locationsDiscovered.remove( locDiscovered ) |
| 1321 | else: |
| 1322 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1323 | result = main.FALSE |
| 1324 | if locationsDiscovered: |
| 1325 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1326 | result = main.FALSE |
| 1327 | return result |
| 1328 | except KeyError: |
| 1329 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1330 | return None |
| 1331 | except pexpect.EOF: |
| 1332 | main.log.error( self.name + ": EOF exception found" ) |
| 1333 | main.log.error( self.name + ": " + self.handle.before ) |
| 1334 | main.cleanAndExit() |
| 1335 | except Exception: |
| 1336 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1337 | return None |
| 1338 | |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1339 | def verifyHostIp( self, hostList=[], prefix="" ): |
| 1340 | """ |
| 1341 | Description: |
| 1342 | Verify that all hosts have IP address assigned to them |
| 1343 | Optional: |
| 1344 | hostList: If specified, verifications only happen to the hosts |
| 1345 | in hostList |
| 1346 | prefix: at least one of the ip address assigned to the host |
| 1347 | needs to have the specified prefix |
| 1348 | Returns: |
| 1349 | main.TRUE if all hosts have specific IP address assigned; |
| 1350 | main.FALSE otherwise |
| 1351 | """ |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1352 | try: |
| 1353 | hosts = self.hosts() |
| 1354 | hosts = json.loads( hosts ) |
| 1355 | if not hostList: |
| 1356 | hostList = [ host[ "id" ] for host in hosts ] |
| 1357 | for host in hosts: |
| 1358 | hostId = host[ "id" ] |
| 1359 | if hostId not in hostList: |
| 1360 | continue |
| 1361 | ipList = host[ "ipAddresses" ] |
| 1362 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1363 | if not ipList: |
| 1364 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1365 | else: |
| 1366 | if not any( ip.startswith( str( prefix ) ) for ip in ipList ): |
| 1367 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1368 | else: |
| 1369 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1370 | hostList.remove( hostId ) |
| 1371 | if hostList: |
| 1372 | main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 1373 | # Print info for debugging |
| 1374 | main.log.debug( self.name + ": hosts output: " + str( hosts ) ) |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1375 | return main.FALSE |
| 1376 | else: |
| 1377 | return main.TRUE |
| 1378 | except KeyError: |
| 1379 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1380 | return None |
| 1381 | except pexpect.EOF: |
| 1382 | main.log.error( self.name + ": EOF exception found" ) |
| 1383 | main.log.error( self.name + ": " + self.handle.before ) |
| 1384 | main.cleanAndExit() |
| 1385 | except Exception: |
| 1386 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1387 | return None |
| 1388 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1389 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1390 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1391 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1392 | * hostIdOne: ONOS host id for host1 |
| 1393 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1394 | Optional: |
| 1395 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1396 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1397 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1398 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1399 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1400 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1401 | Returns: |
| 1402 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1403 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1404 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1405 | cmdStr = "add-host-intent " |
| 1406 | if vlanId: |
| 1407 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1408 | if setVlan: |
| 1409 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1410 | if encap: |
| 1411 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1412 | if bandwidth: |
| 1413 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1414 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1415 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1416 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1417 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1418 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1419 | main.log.error( self.name + ": Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1420 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1421 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1422 | else: |
| 1423 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1424 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1425 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1426 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1427 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1428 | else: |
| 1429 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1430 | main.log.debug( "Response from ONOS was: " + |
| 1431 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1432 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1433 | except AssertionError: |
| 1434 | main.log.exception( "" ) |
| 1435 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1436 | except TypeError: |
| 1437 | main.log.exception( self.name + ": Object not as expected" ) |
| 1438 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1439 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1440 | main.log.error( self.name + ": EOF exception found" ) |
| 1441 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1442 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1443 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1444 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1445 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1446 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1447 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1448 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1449 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1450 | * ingressDevice: device id of ingress device |
| 1451 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1452 | Optional: |
| 1453 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1454 | Description: |
| 1455 | Adds an optical intent by specifying an ingress and egress device |
| 1456 | Returns: |
| 1457 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1458 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1459 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1460 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1461 | " " + str( egressDevice ) |
| 1462 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1463 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1464 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1465 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1466 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1467 | main.log.error( self.name + ": Error in adding Optical intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1468 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1469 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1470 | main.log.info( "Optical intent installed between " + |
| 1471 | str( ingressDevice ) + " and " + |
| 1472 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1473 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1474 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1475 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1476 | else: |
| 1477 | main.log.error( "Error, intent ID not found" ) |
| 1478 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1479 | except AssertionError: |
| 1480 | main.log.exception( "" ) |
| 1481 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1482 | except TypeError: |
| 1483 | main.log.exception( self.name + ": Object not as expected" ) |
| 1484 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1485 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1486 | main.log.error( self.name + ": EOF exception found" ) |
| 1487 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1488 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1489 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1490 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1491 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1492 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1493 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1494 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1495 | ingressDevice, |
| 1496 | egressDevice, |
| 1497 | portIngress="", |
| 1498 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1499 | ethType="", |
| 1500 | ethSrc="", |
| 1501 | ethDst="", |
| 1502 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1503 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1504 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1505 | ipProto="", |
| 1506 | ipSrc="", |
| 1507 | ipDst="", |
| 1508 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1509 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1510 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1511 | setVlan="", |
| 1512 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1513 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1514 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1515 | * ingressDevice: device id of ingress device |
| 1516 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1517 | Optional: |
| 1518 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1519 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1520 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1521 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1522 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1523 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1524 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1525 | * ipSrc: specify ip source address |
| 1526 | * ipDst: specify ip destination address |
| 1527 | * tcpSrc: specify tcp source port |
| 1528 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1529 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1530 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1531 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1532 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1533 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1534 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1535 | Returns: |
| 1536 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1537 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1538 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1539 | options developers provide for point-to-point |
| 1540 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1541 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1542 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1543 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1544 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1545 | if ethType: |
| 1546 | cmd += " --ethType " + str( ethType ) |
| 1547 | if ethSrc: |
| 1548 | cmd += " --ethSrc " + str( ethSrc ) |
| 1549 | if ethDst: |
| 1550 | cmd += " --ethDst " + str( ethDst ) |
| 1551 | if bandwidth: |
| 1552 | cmd += " --bandwidth " + str( bandwidth ) |
| 1553 | if lambdaAlloc: |
| 1554 | cmd += " --lambda " |
| 1555 | if ipProto: |
| 1556 | cmd += " --ipProto " + str( ipProto ) |
| 1557 | if ipSrc: |
| 1558 | cmd += " --ipSrc " + str( ipSrc ) |
| 1559 | if ipDst: |
| 1560 | cmd += " --ipDst " + str( ipDst ) |
| 1561 | if tcpSrc: |
| 1562 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1563 | if tcpDst: |
| 1564 | cmd += " --tcpDst " + str( tcpDst ) |
| 1565 | if vlanId: |
| 1566 | cmd += " -v " + str( vlanId ) |
| 1567 | if setVlan: |
| 1568 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1569 | if encap: |
| 1570 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1571 | if protected: |
| 1572 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1573 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1574 | # Check whether the user appended the port |
| 1575 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1576 | if "/" in ingressDevice: |
| 1577 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1578 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1579 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1580 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1581 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1582 | # Would it make sense to throw an exception and exit |
| 1583 | # the test? |
| 1584 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1585 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1586 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1587 | str( ingressDevice ) + "/" +\ |
| 1588 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1589 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1590 | if "/" in egressDevice: |
| 1591 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1592 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1593 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1594 | main.log.error( "You must specify the egress port" ) |
| 1595 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1596 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1597 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1598 | str( egressDevice ) + "/" +\ |
| 1599 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1600 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1601 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1602 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1603 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1604 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1605 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1606 | main.log.error( self.name + ": Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1607 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1608 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1609 | # TODO: print out all the options in this message? |
| 1610 | main.log.info( "Point-to-point intent installed between " + |
| 1611 | str( ingressDevice ) + " and " + |
| 1612 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1613 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1614 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1615 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1616 | else: |
| 1617 | main.log.error( "Error, intent ID not found" ) |
| 1618 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1619 | except AssertionError: |
| 1620 | main.log.exception( "" ) |
| 1621 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1622 | except TypeError: |
| 1623 | main.log.exception( self.name + ": Object not as expected" ) |
| 1624 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1625 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1626 | main.log.error( self.name + ": EOF exception found" ) |
| 1627 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1628 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1629 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1630 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1631 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1632 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1633 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1634 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1635 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1636 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1637 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1638 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1639 | ethType="", |
| 1640 | ethSrc="", |
| 1641 | ethDst="", |
| 1642 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1643 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1644 | ipProto="", |
| 1645 | ipSrc="", |
| 1646 | ipDst="", |
| 1647 | tcpSrc="", |
| 1648 | tcpDst="", |
| 1649 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1650 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1651 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1652 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1653 | partial=False, |
| 1654 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1655 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1656 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1657 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1658 | is same. That is, all ingress devices include port numbers |
| 1659 | with a "/" or all ingress devices could specify device |
| 1660 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1661 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1662 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1663 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1664 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1665 | Optional: |
| 1666 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1667 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1668 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1669 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1670 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1671 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1672 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1673 | * ipSrc: specify ip source address |
| 1674 | * ipDst: specify ip destination address |
| 1675 | * tcpSrc: specify tcp source port |
| 1676 | * tcpDst: specify tcp destination port |
| 1677 | * setEthSrc: action to Rewrite Source MAC Address |
| 1678 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1679 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1680 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1681 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1682 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1683 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1684 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1685 | Returns: |
| 1686 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1687 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1688 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1689 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1690 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1691 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1692 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1693 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1694 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1695 | if ethType: |
| 1696 | cmd += " --ethType " + str( ethType ) |
| 1697 | if ethSrc: |
| 1698 | cmd += " --ethSrc " + str( ethSrc ) |
| 1699 | if ethDst: |
| 1700 | cmd += " --ethDst " + str( ethDst ) |
| 1701 | if bandwidth: |
| 1702 | cmd += " --bandwidth " + str( bandwidth ) |
| 1703 | if lambdaAlloc: |
| 1704 | cmd += " --lambda " |
| 1705 | if ipProto: |
| 1706 | cmd += " --ipProto " + str( ipProto ) |
| 1707 | if ipSrc: |
| 1708 | cmd += " --ipSrc " + str( ipSrc ) |
| 1709 | if ipDst: |
| 1710 | cmd += " --ipDst " + str( ipDst ) |
| 1711 | if tcpSrc: |
| 1712 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1713 | if tcpDst: |
| 1714 | cmd += " --tcpDst " + str( tcpDst ) |
| 1715 | if setEthSrc: |
| 1716 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1717 | if setEthDst: |
| 1718 | cmd += " --setEthDst " + str( setEthDst ) |
| 1719 | if vlanId: |
| 1720 | cmd += " -v " + str( vlanId ) |
| 1721 | if setVlan: |
| 1722 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1723 | if partial: |
| 1724 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1725 | if encap: |
| 1726 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1727 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1728 | # Check whether the user appended the port |
| 1729 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1730 | |
| 1731 | if portIngressList is None: |
| 1732 | for ingressDevice in ingressDeviceList: |
| 1733 | if "/" in ingressDevice: |
| 1734 | cmd += " " + str( ingressDevice ) |
| 1735 | else: |
| 1736 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1737 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1738 | # TODO: perhaps more meaningful return |
| 1739 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1740 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1741 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1742 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1743 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1744 | cmd += " " + \ |
| 1745 | str( ingressDevice ) + "/" +\ |
| 1746 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1747 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1748 | main.log.error( "Device list and port list does not " + |
| 1749 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1750 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1751 | if "/" in egressDevice: |
| 1752 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1753 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1754 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1755 | main.log.error( "You must specify " + |
| 1756 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1757 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1758 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1759 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1760 | str( egressDevice ) + "/" +\ |
| 1761 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1762 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1763 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1764 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1765 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1766 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1767 | main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1768 | "intent" ) |
| 1769 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1770 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1771 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1772 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1773 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1774 | else: |
| 1775 | main.log.error( "Error, intent ID not found" ) |
| 1776 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1777 | except AssertionError: |
| 1778 | main.log.exception( "" ) |
| 1779 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1780 | except TypeError: |
| 1781 | main.log.exception( self.name + ": Object not as expected" ) |
| 1782 | return None |
| 1783 | except pexpect.EOF: |
| 1784 | main.log.error( self.name + ": EOF exception found" ) |
| 1785 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1786 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1787 | except Exception: |
| 1788 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1789 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1790 | |
| 1791 | def addSinglepointToMultipointIntent( |
| 1792 | self, |
| 1793 | ingressDevice, |
| 1794 | egressDeviceList, |
| 1795 | portIngress="", |
| 1796 | portEgressList=None, |
| 1797 | ethType="", |
| 1798 | ethSrc="", |
| 1799 | ethDst="", |
| 1800 | bandwidth="", |
| 1801 | lambdaAlloc=False, |
| 1802 | ipProto="", |
| 1803 | ipSrc="", |
| 1804 | ipDst="", |
| 1805 | tcpSrc="", |
| 1806 | tcpDst="", |
| 1807 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1808 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1809 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1810 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1811 | partial=False, |
| 1812 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1813 | """ |
| 1814 | Note: |
| 1815 | This function assumes the format of all egress devices |
| 1816 | is same. That is, all egress devices include port numbers |
| 1817 | with a "/" or all egress devices could specify device |
| 1818 | ids and port numbers seperately. |
| 1819 | Required: |
| 1820 | * EgressDeviceList: List of device ids of egress device |
| 1821 | ( Atleast 2 eress devices required in the list ) |
| 1822 | * ingressDevice: device id of ingress device |
| 1823 | Optional: |
| 1824 | * ethType: specify ethType |
| 1825 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1826 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1827 | * bandwidth: specify bandwidth capacity of link |
| 1828 | * lambdaAlloc: if True, intent will allocate lambda |
| 1829 | for the specified intent |
| 1830 | * ipProto: specify ip protocol |
| 1831 | * ipSrc: specify ip source address |
| 1832 | * ipDst: specify ip destination address |
| 1833 | * tcpSrc: specify tcp source port |
| 1834 | * tcpDst: specify tcp destination port |
| 1835 | * setEthSrc: action to Rewrite Source MAC Address |
| 1836 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1837 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1838 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1839 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1840 | Description: |
| 1841 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1842 | specifying device id's and optional fields |
| 1843 | Returns: |
| 1844 | A string of the intent id or None on error |
| 1845 | |
| 1846 | NOTE: This function may change depending on the |
| 1847 | options developers provide for singlepoint-to-multipoint |
| 1848 | intent via cli |
| 1849 | """ |
| 1850 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1851 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1852 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1853 | if ethType: |
| 1854 | cmd += " --ethType " + str( ethType ) |
| 1855 | if ethSrc: |
| 1856 | cmd += " --ethSrc " + str( ethSrc ) |
| 1857 | if ethDst: |
| 1858 | cmd += " --ethDst " + str( ethDst ) |
| 1859 | if bandwidth: |
| 1860 | cmd += " --bandwidth " + str( bandwidth ) |
| 1861 | if lambdaAlloc: |
| 1862 | cmd += " --lambda " |
| 1863 | if ipProto: |
| 1864 | cmd += " --ipProto " + str( ipProto ) |
| 1865 | if ipSrc: |
| 1866 | cmd += " --ipSrc " + str( ipSrc ) |
| 1867 | if ipDst: |
| 1868 | cmd += " --ipDst " + str( ipDst ) |
| 1869 | if tcpSrc: |
| 1870 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1871 | if tcpDst: |
| 1872 | cmd += " --tcpDst " + str( tcpDst ) |
| 1873 | if setEthSrc: |
| 1874 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1875 | if setEthDst: |
| 1876 | cmd += " --setEthDst " + str( setEthDst ) |
| 1877 | if vlanId: |
| 1878 | cmd += " -v " + str( vlanId ) |
| 1879 | if setVlan: |
| 1880 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1881 | if partial: |
| 1882 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1883 | if encap: |
| 1884 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1885 | |
| 1886 | # Check whether the user appended the port |
| 1887 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1888 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1889 | if "/" in ingressDevice: |
| 1890 | cmd += " " + str( ingressDevice ) |
| 1891 | else: |
| 1892 | if not portIngress: |
| 1893 | main.log.error( "You must specify " + |
| 1894 | "the Ingress port" ) |
| 1895 | return main.FALSE |
| 1896 | |
| 1897 | cmd += " " +\ |
| 1898 | str( ingressDevice ) + "/" +\ |
| 1899 | str( portIngress ) |
| 1900 | |
| 1901 | if portEgressList is None: |
| 1902 | for egressDevice in egressDeviceList: |
| 1903 | if "/" in egressDevice: |
| 1904 | cmd += " " + str( egressDevice ) |
| 1905 | else: |
| 1906 | main.log.error( "You must specify " + |
| 1907 | "the egress port" ) |
| 1908 | # TODO: perhaps more meaningful return |
| 1909 | return main.FALSE |
| 1910 | else: |
| 1911 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1912 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1913 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1914 | cmd += " " + \ |
| 1915 | str( egressDevice ) + "/" +\ |
| 1916 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1917 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1918 | main.log.error( "Device list and port list does not " + |
| 1919 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1920 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1921 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1922 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1923 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1924 | # If error, return error message |
| 1925 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1926 | main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " + |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1927 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1928 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1929 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1930 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1931 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1932 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1933 | else: |
| 1934 | main.log.error( "Error, intent ID not found" ) |
| 1935 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1936 | except AssertionError: |
| 1937 | main.log.exception( "" ) |
| 1938 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1939 | except TypeError: |
| 1940 | main.log.exception( self.name + ": Object not as expected" ) |
| 1941 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1942 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1943 | main.log.error( self.name + ": EOF exception found" ) |
| 1944 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1945 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1946 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1947 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1948 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1949 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1950 | def addMplsIntent( |
| 1951 | self, |
| 1952 | ingressDevice, |
| 1953 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1954 | ingressPort="", |
| 1955 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1956 | ethType="", |
| 1957 | ethSrc="", |
| 1958 | ethDst="", |
| 1959 | bandwidth="", |
| 1960 | lambdaAlloc=False, |
| 1961 | ipProto="", |
| 1962 | ipSrc="", |
| 1963 | ipDst="", |
| 1964 | tcpSrc="", |
| 1965 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1966 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1967 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1968 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1969 | """ |
| 1970 | Required: |
| 1971 | * ingressDevice: device id of ingress device |
| 1972 | * egressDevice: device id of egress device |
| 1973 | Optional: |
| 1974 | * ethType: specify ethType |
| 1975 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1976 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1977 | * bandwidth: specify bandwidth capacity of link |
| 1978 | * lambdaAlloc: if True, intent will allocate lambda |
| 1979 | for the specified intent |
| 1980 | * ipProto: specify ip protocol |
| 1981 | * ipSrc: specify ip source address |
| 1982 | * ipDst: specify ip destination address |
| 1983 | * tcpSrc: specify tcp source port |
| 1984 | * tcpDst: specify tcp destination port |
| 1985 | * ingressLabel: Ingress MPLS label |
| 1986 | * egressLabel: Egress MPLS label |
| 1987 | Description: |
| 1988 | Adds MPLS intent by |
| 1989 | specifying device id's and optional fields |
| 1990 | Returns: |
| 1991 | A string of the intent id or None on error |
| 1992 | |
| 1993 | NOTE: This function may change depending on the |
| 1994 | options developers provide for MPLS |
| 1995 | intent via cli |
| 1996 | """ |
| 1997 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1998 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1999 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 2000 | if ethType: |
| 2001 | cmd += " --ethType " + str( ethType ) |
| 2002 | if ethSrc: |
| 2003 | cmd += " --ethSrc " + str( ethSrc ) |
| 2004 | if ethDst: |
| 2005 | cmd += " --ethDst " + str( ethDst ) |
| 2006 | if bandwidth: |
| 2007 | cmd += " --bandwidth " + str( bandwidth ) |
| 2008 | if lambdaAlloc: |
| 2009 | cmd += " --lambda " |
| 2010 | if ipProto: |
| 2011 | cmd += " --ipProto " + str( ipProto ) |
| 2012 | if ipSrc: |
| 2013 | cmd += " --ipSrc " + str( ipSrc ) |
| 2014 | if ipDst: |
| 2015 | cmd += " --ipDst " + str( ipDst ) |
| 2016 | if tcpSrc: |
| 2017 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 2018 | if tcpDst: |
| 2019 | cmd += " --tcpDst " + str( tcpDst ) |
| 2020 | if ingressLabel: |
| 2021 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 2022 | if egressLabel: |
| 2023 | cmd += " --egressLabel " + str( egressLabel ) |
| 2024 | if priority: |
| 2025 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2026 | |
| 2027 | # Check whether the user appended the port |
| 2028 | # or provided it as an input |
| 2029 | if "/" in ingressDevice: |
| 2030 | cmd += " " + str( ingressDevice ) |
| 2031 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2032 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2033 | main.log.error( "You must specify the ingress port" ) |
| 2034 | return None |
| 2035 | |
| 2036 | cmd += " " + \ |
| 2037 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2038 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2039 | |
| 2040 | if "/" in egressDevice: |
| 2041 | cmd += " " + str( egressDevice ) |
| 2042 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2043 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2044 | main.log.error( "You must specify the egress port" ) |
| 2045 | return None |
| 2046 | |
| 2047 | cmd += " " +\ |
| 2048 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2049 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2050 | |
| 2051 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2052 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2053 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2054 | # If error, return error message |
| 2055 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2056 | main.log.error( self.name + ": Error in adding mpls intent" ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2057 | return None |
| 2058 | else: |
| 2059 | # TODO: print out all the options in this message? |
| 2060 | main.log.info( "MPLS intent installed between " + |
| 2061 | str( ingressDevice ) + " and " + |
| 2062 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2063 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2064 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2065 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2066 | else: |
| 2067 | main.log.error( "Error, intent ID not found" ) |
| 2068 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2069 | except AssertionError: |
| 2070 | main.log.exception( "" ) |
| 2071 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2072 | except TypeError: |
| 2073 | main.log.exception( self.name + ": Object not as expected" ) |
| 2074 | return None |
| 2075 | except pexpect.EOF: |
| 2076 | main.log.error( self.name + ": EOF exception found" ) |
| 2077 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2078 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2079 | except Exception: |
| 2080 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2081 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2082 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2083 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 2084 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2085 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2086 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2087 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2088 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2089 | -p or --purge: Purge the intent from the store after removal |
| 2090 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2091 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 2092 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2093 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2094 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2095 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2096 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2097 | if purge: |
| 2098 | cmdStr += " -p" |
| 2099 | if sync: |
| 2100 | cmdStr += " -s" |
| 2101 | |
| 2102 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2103 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2104 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2105 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2106 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2107 | main.log.error( self.name + ": Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2108 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2109 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2110 | # TODO: Should this be main.TRUE |
| 2111 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2112 | except AssertionError: |
| 2113 | main.log.exception( "" ) |
| 2114 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2115 | except TypeError: |
| 2116 | main.log.exception( self.name + ": Object not as expected" ) |
| 2117 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2118 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2119 | main.log.error( self.name + ": EOF exception found" ) |
| 2120 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2121 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2122 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2123 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2124 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2125 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2126 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2127 | """ |
| 2128 | Description: |
| 2129 | Remove all the intents |
| 2130 | Optional args:- |
| 2131 | -s or --sync: Waits for the removal before returning |
| 2132 | -p or --purge: Purge the intent from the store after removal |
| 2133 | Returns: |
| 2134 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2135 | main.FALSE; Returns None for exception |
| 2136 | """ |
| 2137 | try: |
| 2138 | cmdStr = "remove-intent" |
| 2139 | if purge: |
| 2140 | cmdStr += " -p" |
| 2141 | if sync: |
| 2142 | cmdStr += " -s" |
| 2143 | |
| 2144 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2145 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2146 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2147 | assert "Command not found:" not in handle, handle |
| 2148 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2149 | main.log.error( self.name + ": Error in removing intent" ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2150 | return main.FALSE |
| 2151 | else: |
| 2152 | return main.TRUE |
| 2153 | except AssertionError: |
| 2154 | main.log.exception( "" ) |
| 2155 | return None |
| 2156 | except TypeError: |
| 2157 | main.log.exception( self.name + ": Object not as expected" ) |
| 2158 | return None |
| 2159 | except pexpect.EOF: |
| 2160 | main.log.error( self.name + ": EOF exception found" ) |
| 2161 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2162 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2163 | except Exception: |
| 2164 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2165 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2166 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2167 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2168 | """ |
| 2169 | Purges all WITHDRAWN Intents |
| 2170 | """ |
| 2171 | try: |
| 2172 | cmdStr = "purge-intents" |
| 2173 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2174 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2175 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2176 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2177 | main.log.error( self.name + ": Error in purging intents" ) |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2178 | return main.FALSE |
| 2179 | else: |
| 2180 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2181 | except AssertionError: |
| 2182 | main.log.exception( "" ) |
| 2183 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2184 | except TypeError: |
| 2185 | main.log.exception( self.name + ": Object not as expected" ) |
| 2186 | return None |
| 2187 | except pexpect.EOF: |
| 2188 | main.log.error( self.name + ": EOF exception found" ) |
| 2189 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2190 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2191 | except Exception: |
| 2192 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2193 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2194 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2195 | def wipeout( self ): |
| 2196 | """ |
| 2197 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2198 | """ |
| 2199 | try: |
| 2200 | cmdStr = "wipe-out please" |
| 2201 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2202 | assert handle is not None, "Error in sendline" |
| 2203 | assert "Command not found:" not in handle, handle |
| 2204 | return main.TRUE |
| 2205 | except AssertionError: |
| 2206 | main.log.exception( "" ) |
| 2207 | return None |
| 2208 | except TypeError: |
| 2209 | main.log.exception( self.name + ": Object not as expected" ) |
| 2210 | return None |
| 2211 | except pexpect.EOF: |
| 2212 | main.log.error( self.name + ": EOF exception found" ) |
| 2213 | main.log.error( self.name + ": " + self.handle.before ) |
| 2214 | main.cleanAndExit() |
| 2215 | except Exception: |
| 2216 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2217 | main.cleanAndExit() |
| 2218 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2219 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2220 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2221 | NOTE: This method should be used after installing application: |
| 2222 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2223 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2224 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2225 | Description: |
| 2226 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2227 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2228 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2229 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2230 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2231 | cmdStr += " -j" |
| 2232 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2233 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2234 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2235 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2236 | except AssertionError: |
| 2237 | main.log.exception( "" ) |
| 2238 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2239 | except TypeError: |
| 2240 | main.log.exception( self.name + ": Object not as expected" ) |
| 2241 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2242 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2243 | main.log.error( self.name + ": EOF exception found" ) |
| 2244 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2245 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2246 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2247 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2248 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2249 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2250 | def ipv4RouteNumber( self ): |
| 2251 | """ |
| 2252 | NOTE: This method should be used after installing application: |
| 2253 | onos-app-sdnip |
| 2254 | Description: |
| 2255 | Obtain the total IPv4 routes number in the system |
| 2256 | """ |
| 2257 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2258 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2259 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2260 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2261 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2262 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2263 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2264 | except AssertionError: |
| 2265 | main.log.exception( "" ) |
| 2266 | return None |
| 2267 | except ( TypeError, ValueError ): |
| 2268 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2269 | return None |
| 2270 | except pexpect.EOF: |
| 2271 | main.log.error( self.name + ": EOF exception found" ) |
| 2272 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2273 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2274 | except Exception: |
| 2275 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2276 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2277 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2278 | # =============Function to check Bandwidth allocation======== |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2279 | def allocations( self, jsonFormat = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2280 | """ |
| 2281 | Description: |
| 2282 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2283 | """ |
| 2284 | try: |
| 2285 | cmdStr = "allocations" |
| 2286 | if jsonFormat: |
| 2287 | cmdStr += " -j" |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2288 | handle = self.sendline( cmdStr, timeout=300 ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2289 | assert handle is not None, "Error in sendline" |
| 2290 | assert "Command not found:" not in handle, handle |
| 2291 | return handle |
| 2292 | except AssertionError: |
| 2293 | main.log.exception( "" ) |
| 2294 | return None |
| 2295 | except ( TypeError, ValueError ): |
| 2296 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2297 | return None |
| 2298 | except pexpect.EOF: |
| 2299 | main.log.error( self.name + ": EOF exception found" ) |
| 2300 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2301 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2302 | except Exception: |
| 2303 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2304 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2305 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2306 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2307 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2308 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2309 | Obtain intents from the ONOS cli. |
| 2310 | Optional: |
| 2311 | * jsonFormat: Enable output formatting in json, default to True |
| 2312 | * summary: Whether only output the intent summary, defaults to False |
| 2313 | * type: Only output a certain type of intent. This options is valid |
| 2314 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2315 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2316 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2317 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2318 | if summary: |
| 2319 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2320 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2321 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2322 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2323 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2324 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2325 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2326 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2327 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2328 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2329 | intentType = "" |
| 2330 | # IF we want the summary of a specific intent type |
| 2331 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2332 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2333 | if intentType in jsonResult.keys(): |
| 2334 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2335 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2336 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2337 | return handle |
| 2338 | else: |
| 2339 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2340 | except AssertionError: |
| 2341 | main.log.exception( "" ) |
| 2342 | return None |
| 2343 | except ( TypeError, ValueError ): |
| 2344 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2345 | return None |
| 2346 | except pexpect.EOF: |
| 2347 | main.log.error( self.name + ": EOF exception found" ) |
| 2348 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2349 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2350 | except Exception: |
| 2351 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2352 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2353 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2354 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2355 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2356 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2357 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2358 | list of intent IDs. |
| 2359 | Parameters: |
| 2360 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2361 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2362 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2363 | Returns the state (string type) of the ID if a single intent ID is |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2364 | accepted. |
| 2365 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2366 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2367 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2368 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2369 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2370 | try: |
| 2371 | state = "State is Undefined" |
| 2372 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2373 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2374 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2375 | rawJson = intentsJson |
| 2376 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2377 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2378 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2379 | if intentsId == intent[ 'id' ]: |
| 2380 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2381 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2382 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2383 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2384 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2385 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2386 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2387 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2388 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2389 | for intent in parsedIntentsJson: |
| 2390 | if intentsId[ i ] == intent[ 'id' ]: |
| 2391 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2392 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2393 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2394 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2395 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2396 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2397 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2398 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2399 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2400 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2401 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2402 | except ( TypeError, ValueError ): |
| 2403 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2404 | return None |
| 2405 | except pexpect.EOF: |
| 2406 | main.log.error( self.name + ": EOF exception found" ) |
| 2407 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2408 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2409 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2410 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2411 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2412 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2413 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2414 | """ |
| 2415 | Description: |
| 2416 | Check intents state |
| 2417 | Required: |
| 2418 | intentsId - List of intents ID to be checked |
| 2419 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2420 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2421 | state in the list. |
| 2422 | *NOTE: You can pass in a list of expected state, |
| 2423 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2424 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2425 | Returns main.TRUE only if all intent are the same as expected states, |
| 2426 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2427 | """ |
| 2428 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2429 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2430 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2431 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2432 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2433 | intentsDict = [] |
| 2434 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2435 | if isinstance( intentsId, types.StringType ) \ |
| 2436 | and intent.get( 'id' ) == intentsId: |
| 2437 | intentsDict.append( intent ) |
| 2438 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2439 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2440 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2441 | |
| 2442 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2443 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2444 | "getting intents state" ) |
| 2445 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2446 | |
| 2447 | if isinstance( expectedState, types.StringType ): |
| 2448 | for intents in intentsDict: |
| 2449 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2450 | main.log.debug( self.name + " : Intent ID - " + |
| 2451 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2452 | " actual state = " + |
| 2453 | intents.get( 'state' ) |
| 2454 | + " does not equal expected state = " |
| 2455 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2456 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2457 | elif isinstance( expectedState, types.ListType ): |
| 2458 | for intents in intentsDict: |
| 2459 | if not any( state == intents.get( 'state' ) for state in |
| 2460 | expectedState ): |
| 2461 | main.log.debug( self.name + " : Intent ID - " + |
| 2462 | intents.get( 'id' ) + |
| 2463 | " actual state = " + |
| 2464 | intents.get( 'state' ) + |
| 2465 | " does not equal expected states = " |
| 2466 | + str( expectedState ) ) |
| 2467 | returnValue = main.FALSE |
| 2468 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2469 | if returnValue == main.TRUE: |
| 2470 | main.log.info( self.name + ": All " + |
| 2471 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2472 | " intents are in " + str( expectedState ) + |
| 2473 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2474 | return returnValue |
| 2475 | except TypeError: |
| 2476 | main.log.exception( self.name + ": Object not as expected" ) |
| 2477 | return None |
| 2478 | except pexpect.EOF: |
| 2479 | main.log.error( self.name + ": EOF exception found" ) |
| 2480 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2481 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2482 | except Exception: |
| 2483 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2484 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2485 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2486 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2487 | """ |
| 2488 | Description: |
| 2489 | Compare the allocated bandwidth with the given allocations |
| 2490 | Required: |
| 2491 | expectedAllocations - The expected ONOS output of the allocations command |
| 2492 | Return: |
| 2493 | Returns main.TRUE only if all intent are the same as expected states, |
| 2494 | otherwise returns main.FALSE. |
| 2495 | """ |
| 2496 | # FIXME: Convert these string comparisons to object comparisons |
| 2497 | try: |
| 2498 | returnValue = main.TRUE |
| 2499 | bandwidthFailed = False |
| 2500 | rawAlloc = self.allocations() |
| 2501 | expectedFormat = StringIO( expectedAllocations ) |
| 2502 | ONOSOutput = StringIO( rawAlloc ) |
| 2503 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2504 | str( expectedFormat ) ) ) |
| 2505 | |
| 2506 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2507 | actual = actual.rstrip() |
| 2508 | expected = expected.rstrip() |
| 2509 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2510 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2511 | marker1 = actual.find( 'allocated' ) |
| 2512 | m1 = actual[ :marker1 ] |
| 2513 | marker2 = expected.find( 'allocated' ) |
| 2514 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2515 | if m1 != m2: |
| 2516 | bandwidthFailed = True |
| 2517 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2518 | bandwidthFailed = True |
| 2519 | expectedFormat.close() |
| 2520 | ONOSOutput.close() |
| 2521 | |
| 2522 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2523 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2524 | returnValue = main.FALSE |
| 2525 | return returnValue |
| 2526 | except TypeError: |
| 2527 | main.log.exception( self.name + ": Object not as expected" ) |
| 2528 | return None |
| 2529 | except pexpect.EOF: |
| 2530 | main.log.error( self.name + ": EOF exception found" ) |
| 2531 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2532 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2533 | except Exception: |
| 2534 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2535 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2536 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2537 | def compareIntent( self, intentDict ): |
| 2538 | """ |
| 2539 | Description: |
| 2540 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2541 | Return: |
| 2542 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2543 | Arguments: |
| 2544 | intentDict: a dictionary which maps intent ids to intent states |
| 2545 | """ |
| 2546 | try: |
| 2547 | intentsRaw = self.intents() |
| 2548 | intentsJson = json.loads( intentsRaw ) |
| 2549 | intentDictONOS = {} |
| 2550 | for intent in intentsJson: |
| 2551 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2552 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2553 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2554 | 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] | 2555 | str( len( intentDict ) ) + " expected and " + |
| 2556 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2557 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2558 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2559 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2560 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2561 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2562 | else: |
| 2563 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2564 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2565 | " expected state is " + intentDict[ intentID ] + |
| 2566 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2567 | returnValue = main.FALSE |
| 2568 | intentDictONOS.pop( intentID ) |
| 2569 | if len( intentDictONOS ) > 0: |
| 2570 | returnValue = main.FALSE |
| 2571 | for intentID in intentDictONOS.keys(): |
| 2572 | 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] | 2573 | if returnValue == main.TRUE: |
| 2574 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2575 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2576 | except KeyError: |
| 2577 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2578 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2579 | except ( TypeError, ValueError ): |
| 2580 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2581 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2582 | except pexpect.EOF: |
| 2583 | main.log.error( self.name + ": EOF exception found" ) |
| 2584 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2585 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2586 | except Exception: |
| 2587 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2588 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2589 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2590 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2591 | """ |
| 2592 | Description: |
| 2593 | Check the number of installed intents. |
| 2594 | Optional: |
| 2595 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2596 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2597 | Return: |
| 2598 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2599 | , otherwise, returns main.FALSE. |
| 2600 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2601 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2602 | try: |
| 2603 | cmd = "intents -s -j" |
| 2604 | |
| 2605 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2606 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2607 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2608 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2609 | response = json.loads( response ) |
| 2610 | |
| 2611 | # get total and installed number, see if they are match |
| 2612 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2613 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2614 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2615 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2616 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2617 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2618 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2619 | return main.FALSE |
| 2620 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2621 | except ( TypeError, ValueError ): |
| 2622 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2623 | return None |
| 2624 | except pexpect.EOF: |
| 2625 | main.log.error( self.name + ": EOF exception found" ) |
| 2626 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2627 | if noExit: |
| 2628 | return main.FALSE |
| 2629 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2630 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2631 | except pexpect.TIMEOUT: |
| 2632 | main.log.error( self.name + ": ONOS timeout" ) |
| 2633 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2634 | except Exception: |
| 2635 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2636 | if noExit: |
| 2637 | return main.FALSE |
| 2638 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2639 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2640 | |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2641 | def flows( self, state="any", jsonFormat=True, timeout=60, noExit=False, noCore=False, device=""): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2642 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2643 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2644 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2645 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2646 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2647 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2648 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2649 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2650 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2651 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2652 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2653 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2654 | cmdStr += " -n" |
| 2655 | cmdStr += " " + state |
| 2656 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2657 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2658 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2659 | assert "Command not found:" not in handle, handle |
| 2660 | if re.search( "Error:", handle ): |
| 2661 | main.log.error( self.name + ": flows() response: " + |
| 2662 | str( handle ) ) |
| 2663 | return handle |
| 2664 | except AssertionError: |
| 2665 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2666 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2667 | except TypeError: |
| 2668 | main.log.exception( self.name + ": Object not as expected" ) |
| 2669 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2670 | except pexpect.TIMEOUT: |
| 2671 | main.log.error( self.name + ": ONOS timeout" ) |
| 2672 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2673 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2674 | main.log.error( self.name + ": EOF exception found" ) |
| 2675 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2676 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2677 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2678 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2679 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2680 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2681 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2682 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2683 | count = int( count ) if count else 0 |
steven30801 | eccfe21 | 2019-01-24 13:00:42 +0800 | [diff] [blame] | 2684 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2685 | return count if ( count > min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2686 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2687 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2688 | """ |
| 2689 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2690 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2691 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2692 | if the count of those states is 0, which means all current flows |
| 2693 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2694 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2695 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2696 | Return: |
| 2697 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2698 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2699 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2700 | """ |
| 2701 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2702 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2703 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2704 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2705 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2706 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2707 | if rawFlows: |
| 2708 | # if we didn't get flows or flows function return None, we should return |
| 2709 | # main.Flase |
| 2710 | checkedStates.append( json.loads( rawFlows ) ) |
| 2711 | else: |
| 2712 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2713 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2714 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2715 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2716 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2717 | except TypeError: |
| 2718 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2719 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2720 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2721 | # We want to count PENDING_ADD if isPENDING is true |
| 2722 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2723 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2724 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2725 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2726 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2727 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2728 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2729 | except ( TypeError, ValueError ): |
| 2730 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2731 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2732 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2733 | except AssertionError: |
| 2734 | main.log.exception( "" ) |
| 2735 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2736 | except pexpect.TIMEOUT: |
| 2737 | main.log.error( self.name + ": ONOS timeout" ) |
| 2738 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2739 | except pexpect.EOF: |
| 2740 | main.log.error( self.name + ": EOF exception found" ) |
| 2741 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2742 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2743 | except Exception: |
| 2744 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2745 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2746 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2747 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2748 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2749 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2750 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2751 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2752 | a specific point-to-point intent definition |
| 2753 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2754 | * ingress: specify source dpid |
| 2755 | * egress: specify destination dpid |
| 2756 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2757 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2758 | * offset: the keyOffset is where the next batch of intents |
| 2759 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2760 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2761 | * getResponse: If set to True, function will return ONOS response. |
| 2762 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2763 | Returns: If failed to push test intents, it will returen None, |
| 2764 | if successful, return true. |
| 2765 | Timeout expection will return None, |
| 2766 | TypeError will return false |
| 2767 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2768 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2769 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2770 | if background: |
| 2771 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2772 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2773 | back = "" |
| 2774 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2775 | ingress, |
| 2776 | egress, |
| 2777 | batchSize, |
| 2778 | offset, |
| 2779 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2780 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2781 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2782 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2783 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2784 | if getResponse: |
| 2785 | return response |
| 2786 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2787 | # TODO: We should handle if there is failure in installation |
| 2788 | return main.TRUE |
| 2789 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2790 | except AssertionError: |
| 2791 | main.log.exception( "" ) |
| 2792 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2793 | except pexpect.TIMEOUT: |
| 2794 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2795 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2796 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2797 | main.log.error( self.name + ": EOF exception found" ) |
| 2798 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2799 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2800 | except TypeError: |
| 2801 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2802 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2803 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2804 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2805 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2806 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2807 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2808 | """ |
| 2809 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2810 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2811 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2812 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2813 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2814 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2815 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2816 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2817 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2818 | cmd = "flows -c added" |
| 2819 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2820 | if rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2821 | rawFlows = rawFlows.split( "\n" ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2822 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2823 | for l in rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2824 | totalFlows += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2825 | else: |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 2826 | main.log.warn( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2827 | return None |
| 2828 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2829 | |
You Wang | d3097f7 | 2018-12-12 11:56:03 -0800 | [diff] [blame] | 2830 | except IndexError: |
| 2831 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
| 2832 | main.log.debug( "rawFlows: {}".format( rawFlows ) ) |
| 2833 | return None |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2834 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2835 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2836 | return None |
| 2837 | except pexpect.EOF: |
| 2838 | main.log.error( self.name + ": EOF exception found" ) |
| 2839 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2840 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2841 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2842 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2843 | except pexpect.TIMEOUT: |
| 2844 | main.log.error( self.name + ": ONOS timeout" ) |
| 2845 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2846 | except Exception: |
| 2847 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2848 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2849 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2850 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2851 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2852 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2853 | """ |
| 2854 | Description: |
| 2855 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2856 | Optional: |
| 2857 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2858 | Return: |
| 2859 | The number of intents |
| 2860 | """ |
| 2861 | try: |
| 2862 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2863 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2864 | if response is None: |
| 2865 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2866 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2867 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2868 | except ( TypeError, ValueError ): |
| 2869 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2870 | return None |
| 2871 | except pexpect.EOF: |
| 2872 | main.log.error( self.name + ": EOF exception found" ) |
| 2873 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2874 | if noExit: |
| 2875 | return -1 |
| 2876 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2877 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2878 | except Exception: |
| 2879 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2880 | if noExit: |
| 2881 | return -1 |
| 2882 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2883 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2884 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2885 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2886 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2887 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2888 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2889 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2890 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2891 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2892 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2893 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2894 | cmdStr += " -j" |
| 2895 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2896 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2897 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2898 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2899 | except AssertionError: |
| 2900 | main.log.exception( "" ) |
| 2901 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2902 | except TypeError: |
| 2903 | main.log.exception( self.name + ": Object not as expected" ) |
| 2904 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2905 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2906 | main.log.error( self.name + ": EOF exception found" ) |
| 2907 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2908 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2909 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2910 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2911 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2912 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2913 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2914 | """ |
| 2915 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2916 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2917 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2918 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2919 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2920 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2921 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2922 | cmdStr += " -j" |
| 2923 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2924 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2925 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2926 | if handle: |
| 2927 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2928 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2929 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2930 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2931 | else: |
| 2932 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2933 | except AssertionError: |
| 2934 | main.log.exception( "" ) |
| 2935 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2936 | except TypeError: |
| 2937 | main.log.exception( self.name + ": Object not as expected" ) |
| 2938 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2939 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2940 | main.log.error( self.name + ": EOF exception found" ) |
| 2941 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2942 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2943 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2944 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2945 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2946 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2947 | # Wrapper functions **************** |
| 2948 | # Wrapper functions use existing driver |
| 2949 | # functions and extends their use case. |
| 2950 | # For example, we may use the output of |
| 2951 | # a normal driver function, and parse it |
| 2952 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2953 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2954 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2955 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2956 | Description: |
| 2957 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2958 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2959 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2960 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2961 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 2962 | if intentsStr is None: |
| 2963 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2964 | # Convert to a dictionary |
| 2965 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2966 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2967 | for intent in intents: |
| 2968 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2969 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2970 | except TypeError: |
| 2971 | main.log.exception( self.name + ": Object not as expected" ) |
| 2972 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2973 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2974 | main.log.error( self.name + ": EOF exception found" ) |
| 2975 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2976 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2977 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2978 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2979 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2980 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2981 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2982 | """ |
| 2983 | Determine the number of flow rules for the given device id that are |
| 2984 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2985 | Params: |
| 2986 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2987 | """ |
| 2988 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2989 | if core: |
| 2990 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2991 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2992 | else: |
| 2993 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2994 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 2995 | handle = self.lineCount( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2996 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2997 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2998 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2999 | except AssertionError: |
| 3000 | main.log.exception( "" ) |
| 3001 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3002 | except pexpect.EOF: |
| 3003 | main.log.error( self.name + ": EOF exception found" ) |
| 3004 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3005 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3006 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3007 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3008 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 3009 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3010 | def groupAddedCount( self, deviceId, core=False ): |
| 3011 | """ |
| 3012 | Determine the number of group rules for the given device id that are |
| 3013 | in the added state |
| 3014 | Params: |
| 3015 | core: if True, only return the number of core groups added |
| 3016 | """ |
| 3017 | try: |
| 3018 | if core: |
| 3019 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3020 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3021 | else: |
| 3022 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3023 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3024 | handle = self.lineCount( cmdStr ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3025 | assert handle is not None, "Error in sendline" |
| 3026 | assert "Command not found:" not in handle, handle |
| 3027 | return handle |
| 3028 | except AssertionError: |
| 3029 | main.log.exception( "" ) |
| 3030 | return None |
| 3031 | except pexpect.EOF: |
| 3032 | main.log.error( self.name + ": EOF exception found" ) |
| 3033 | main.log.error( self.name + ": " + self.handle.before ) |
| 3034 | main.cleanAndExit() |
| 3035 | except Exception: |
| 3036 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3037 | main.cleanAndExit() |
| 3038 | |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3039 | def addStaticRoute( self, subnet, intf): |
| 3040 | """ |
| 3041 | Adds a static route to onos. |
| 3042 | Params: |
| 3043 | subnet: The subnet reaching through this route |
| 3044 | intf: The interface this route is reachable through |
| 3045 | """ |
| 3046 | try: |
| 3047 | cmdStr = "route-add " + subnet + " " + intf |
| 3048 | handle = self.sendline( cmdStr ) |
| 3049 | assert handle is not None, "Error in sendline" |
| 3050 | assert "Command not found:" not in handle, handle |
| 3051 | return handle |
| 3052 | except AssertionError: |
| 3053 | main.log.exception( "" ) |
| 3054 | return None |
| 3055 | except pexpect.EOF: |
| 3056 | main.log.error( self.name + ": EOF exception found" ) |
| 3057 | main.log.error( self.name + ": " + self.handle.before ) |
| 3058 | main.cleanAndExit() |
| 3059 | except Exception: |
| 3060 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3061 | main.cleanAndExit() |
| 3062 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3063 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 3064 | """ |
| 3065 | Description: |
| 3066 | Check whether the number of groups for the given device id that |
| 3067 | are in ADDED state is bigger than minGroupCount. |
| 3068 | Required: |
| 3069 | * deviceId: device id to check the number of added group rules |
| 3070 | Optional: |
| 3071 | * minGroupCount: the number of groups to compare |
| 3072 | * core: if True, only check the number of core groups added |
| 3073 | * comparison: if 0, compare with greater than minFlowCount |
| 3074 | * if 1, compare with equal to minFlowCount |
| 3075 | Return: |
| 3076 | Returns the number of groups if it is bigger than minGroupCount, |
| 3077 | returns main.FALSE otherwise. |
| 3078 | """ |
| 3079 | count = self.groupAddedCount( deviceId, core ) |
| 3080 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3081 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3082 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 3083 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3084 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3085 | """ |
| 3086 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3087 | deviceId: Id of the device from which we retrieve groups |
| 3088 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3089 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3090 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3091 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 3092 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3093 | assert handle is not None, "Error in sendline" |
| 3094 | assert "Command not found:" not in handle, handle |
| 3095 | return handle |
| 3096 | except AssertionError: |
| 3097 | main.log.exception( "" ) |
| 3098 | return None |
| 3099 | except TypeError: |
| 3100 | main.log.exception( self.name + ": Object not as expected" ) |
| 3101 | return None |
| 3102 | except pexpect.EOF: |
| 3103 | main.log.error( self.name + ": EOF exception found" ) |
| 3104 | main.log.error( self.name + ": " + self.handle.before ) |
| 3105 | main.cleanAndExit() |
| 3106 | except Exception: |
| 3107 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3108 | main.cleanAndExit() |
| 3109 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3110 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3111 | """ |
| 3112 | Description: |
| 3113 | Check whether the number of flow rules for the given device id that |
| 3114 | are in ADDED state is bigger than minFlowCount. |
| 3115 | Required: |
| 3116 | * deviceId: device id to check the number of added flow rules |
| 3117 | Optional: |
| 3118 | * minFlowCount: the number of flow rules to compare |
| 3119 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3120 | * comparison: if 0, compare with greater than minFlowCount |
| 3121 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3122 | Return: |
| 3123 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 3124 | returns main.FALSE otherwise. |
| 3125 | """ |
| 3126 | count = self.flowAddedCount( deviceId, core ) |
| 3127 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3128 | main.log.debug( "found {} flows".format( count ) ) |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3129 | return count if ((count > expectedFlowCount) if (comparison == 0) else (count == expectedFlowCount)) else main.FALSE |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3130 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3131 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3132 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3133 | Use 'devices' function to obtain list of all devices |
| 3134 | and parse the result to obtain a list of all device |
| 3135 | id's. Returns this list. Returns empty list if no |
| 3136 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3137 | List is ordered sequentially |
| 3138 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3139 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3140 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3141 | the ids. By obtaining the list of device ids on the fly, |
| 3142 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3143 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3144 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3145 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3146 | devicesStr = self.devices( jsonFormat=False ) |
| 3147 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3148 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3149 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3150 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3151 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3152 | |
| 3153 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3154 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3155 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3156 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3157 | # Split list further into arguments before and after string |
| 3158 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3159 | # append to idList |
| 3160 | for arg in tempList: |
| 3161 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3162 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3163 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3164 | except TypeError: |
| 3165 | main.log.exception( self.name + ": Object not as expected" ) |
| 3166 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3167 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3168 | main.log.error( self.name + ": EOF exception found" ) |
| 3169 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3170 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3171 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3172 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3173 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3174 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3175 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3176 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3177 | Uses 'nodes' function to obtain list of all nodes |
| 3178 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3179 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3180 | Returns: |
| 3181 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3182 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3183 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3184 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3185 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3186 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3187 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3188 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3189 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3190 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3191 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3192 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3193 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3194 | except ( TypeError, ValueError ): |
| 3195 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3196 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3197 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3198 | main.log.error( self.name + ": EOF exception found" ) |
| 3199 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3200 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3201 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3202 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3203 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3204 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3205 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3206 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3207 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3208 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3209 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3210 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3211 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3212 | return None |
| 3213 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3214 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3215 | rawDevices = self.devices() |
| 3216 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3217 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3218 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3219 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3220 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3221 | return device |
| 3222 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3223 | except ( TypeError, ValueError ): |
| 3224 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3225 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3226 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3227 | main.log.error( self.name + ": EOF exception found" ) |
| 3228 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3229 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3230 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3231 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3232 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3233 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3234 | def getTopology( self, topologyOutput ): |
| 3235 | """ |
| 3236 | Definition: |
| 3237 | Loads a json topology output |
| 3238 | Return: |
| 3239 | topology = current ONOS topology |
| 3240 | """ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3241 | try: |
| 3242 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3243 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3244 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3245 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3246 | except ( TypeError, ValueError ): |
| 3247 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3248 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3249 | except pexpect.EOF: |
| 3250 | main.log.error( self.name + ": EOF exception found" ) |
| 3251 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3252 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3253 | except Exception: |
| 3254 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3255 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3256 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3257 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3258 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3259 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3260 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3261 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3262 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3263 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3264 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3265 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3266 | logLevel = level to log to. |
| 3267 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3268 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3269 | Returns: main.TRUE if the number of switches and links are correct, |
| 3270 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3271 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3272 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3273 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3274 | summary = self.summary() |
| 3275 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3276 | except ( TypeError, ValueError ): |
| 3277 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3278 | return main.ERROR |
| 3279 | try: |
| 3280 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3281 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3282 | return main.ERROR |
| 3283 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3284 | # Is the number of switches is what we expected |
| 3285 | devices = topology.get( 'devices', False ) |
| 3286 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3287 | nodes = summary.get( 'nodes', False ) |
| 3288 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3289 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3290 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3291 | # Is the number of links is what we expected |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3292 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3293 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 3294 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3295 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3296 | output = output + "The number of links and switches match "\ |
| 3297 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3298 | result = main.TRUE |
| 3299 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3300 | output = output + \ |
| 3301 | "The number of links and switches does not match " + \ |
| 3302 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3303 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3304 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 3305 | output = output + " (%i expected) " % int( numoswitch ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3306 | if int( numolink ) > 0: |
| 3307 | output = output + "and %i links " % int( links ) |
| 3308 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 3309 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3310 | output = output + "and %i controllers " % int( nodes ) |
| 3311 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3312 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3313 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3314 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3315 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3316 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3317 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3318 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3319 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3320 | main.log.error( self.name + ": EOF exception found" ) |
| 3321 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3322 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3323 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3324 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3325 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3326 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3327 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3328 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3329 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3330 | deviceId must be the id of a device as seen in the onos devices command |
| 3331 | 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] | 3332 | role must be either master, standby, or none |
| 3333 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3334 | Returns: |
| 3335 | main.TRUE or main.FALSE based on argument verification and |
| 3336 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3337 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3338 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3339 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3340 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3341 | cmdStr = "device-role " +\ |
| 3342 | str( deviceId ) + " " +\ |
| 3343 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3344 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3345 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3346 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3347 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3348 | if re.search( "Error", handle ): |
| 3349 | # end color output to escape any colours |
| 3350 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3351 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3352 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3353 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3354 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3355 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3356 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3357 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3358 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3359 | except AssertionError: |
| 3360 | main.log.exception( "" ) |
| 3361 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3362 | except TypeError: |
| 3363 | main.log.exception( self.name + ": Object not as expected" ) |
| 3364 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3365 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3366 | main.log.error( self.name + ": EOF exception found" ) |
| 3367 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3368 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3369 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3370 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3371 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3372 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3373 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3374 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3375 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3376 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3377 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3378 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3379 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3380 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3381 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3382 | cmdStr += " -j" |
| 3383 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3384 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3385 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3386 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3387 | except AssertionError: |
| 3388 | main.log.exception( "" ) |
| 3389 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3390 | except TypeError: |
| 3391 | main.log.exception( self.name + ": Object not as expected" ) |
| 3392 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3393 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3394 | main.log.error( self.name + ": EOF exception found" ) |
| 3395 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3396 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3397 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3398 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3399 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3400 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3401 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3402 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3403 | CLI command to get the current leader for the Election test application |
| 3404 | NOTE: Requires installation of the onos-app-election feature |
| 3405 | Returns: Node IP of the leader if one exists |
| 3406 | None if none exists |
| 3407 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3408 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3409 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3410 | cmdStr = "election-test-leader" |
| 3411 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3412 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3413 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3414 | # Leader |
| 3415 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3416 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3417 | nodeSearch = re.search( leaderPattern, response ) |
| 3418 | if nodeSearch: |
| 3419 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3420 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3421 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3422 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3423 | # no leader |
| 3424 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3425 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3426 | nullSearch = re.search( nullPattern, response ) |
| 3427 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3428 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3429 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3430 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3431 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3432 | main.log.error( self.name + ": Error in electionTestLeader on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3433 | ": " + "unexpected response" ) |
| 3434 | main.log.error( repr( response ) ) |
| 3435 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3436 | except AssertionError: |
| 3437 | main.log.exception( "" ) |
| 3438 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3439 | except TypeError: |
| 3440 | main.log.exception( self.name + ": Object not as expected" ) |
| 3441 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3442 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3443 | main.log.error( self.name + ": EOF exception found" ) |
| 3444 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3445 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3446 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3447 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3448 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3449 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3450 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3451 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3452 | CLI command to run for leadership of the Election test application. |
| 3453 | NOTE: Requires installation of the onos-app-election feature |
| 3454 | Returns: Main.TRUE on success |
| 3455 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3456 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3457 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3458 | cmdStr = "election-test-run" |
| 3459 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3460 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3461 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3462 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3463 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3464 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3465 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3466 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3467 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3468 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3469 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3470 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3471 | main.log.error( self.name + ": Error in electionTestRun on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3472 | ": " + "unexpected response" ) |
| 3473 | main.log.error( repr( response ) ) |
| 3474 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3475 | except AssertionError: |
| 3476 | main.log.exception( "" ) |
| 3477 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3478 | except TypeError: |
| 3479 | main.log.exception( self.name + ": Object not as expected" ) |
| 3480 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3481 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3482 | main.log.error( self.name + ": EOF exception found" ) |
| 3483 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3484 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3485 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3486 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3487 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3488 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3489 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3490 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3491 | * CLI command to withdraw the local node from leadership election for |
| 3492 | * the Election test application. |
| 3493 | #NOTE: Requires installation of the onos-app-election feature |
| 3494 | Returns: Main.TRUE on success |
| 3495 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3496 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3497 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3498 | cmdStr = "election-test-withdraw" |
| 3499 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3500 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3501 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3502 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3503 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3504 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3505 | if re.search( successPattern, response ): |
| 3506 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3507 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3508 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3509 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3510 | main.log.error( self.name + ": Error in electionTestWithdraw on " + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3511 | self.name + ": " + "unexpected response" ) |
| 3512 | main.log.error( repr( response ) ) |
| 3513 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3514 | except AssertionError: |
| 3515 | main.log.exception( "" ) |
| 3516 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3517 | except TypeError: |
| 3518 | main.log.exception( self.name + ": Object not as expected" ) |
| 3519 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3520 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3521 | main.log.error( self.name + ": EOF exception found" ) |
| 3522 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3523 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3524 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3525 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3526 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3527 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3528 | def getDevicePortsEnabledCount( self, dpid ): |
| 3529 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3530 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3531 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3532 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3533 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3534 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3535 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3536 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3537 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3538 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3539 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3540 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3541 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3542 | except AssertionError: |
| 3543 | main.log.exception( "" ) |
| 3544 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3545 | except TypeError: |
| 3546 | main.log.exception( self.name + ": Object not as expected" ) |
| 3547 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3548 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3549 | main.log.error( self.name + ": EOF exception found" ) |
| 3550 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3551 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3552 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3553 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3554 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3555 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3556 | def getDeviceLinksActiveCount( self, dpid ): |
| 3557 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3558 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3559 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3560 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3561 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3562 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3563 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3564 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3565 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3566 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3567 | main.log.error( self.name + ": Error in getting ports " ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3568 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3569 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3570 | except AssertionError: |
| 3571 | main.log.exception( "" ) |
| 3572 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3573 | except TypeError: |
| 3574 | main.log.exception( self.name + ": Object not as expected" ) |
| 3575 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3576 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3577 | main.log.error( self.name + ": EOF exception found" ) |
| 3578 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3579 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3580 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3581 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3582 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3583 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3584 | def getAllIntentIds( self ): |
| 3585 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3586 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3587 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3588 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3589 | cmdStr = "onos:intents | grep id=" |
| 3590 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3591 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3592 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3593 | if re.search( "Error", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3594 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3595 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3596 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3597 | except AssertionError: |
| 3598 | main.log.exception( "" ) |
| 3599 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3600 | except TypeError: |
| 3601 | main.log.exception( self.name + ": Object not as expected" ) |
| 3602 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3603 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3604 | main.log.error( self.name + ": EOF exception found" ) |
| 3605 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3606 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3607 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3608 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3609 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3610 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3611 | def intentSummary( self ): |
| 3612 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3613 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3614 | """ |
| 3615 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3616 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3617 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3618 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3619 | states.append( intent.get( 'state', None ) ) |
| 3620 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3621 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3622 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3623 | except ( TypeError, ValueError ): |
| 3624 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3625 | return None |
| 3626 | except pexpect.EOF: |
| 3627 | main.log.error( self.name + ": EOF exception found" ) |
| 3628 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3629 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3630 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3631 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3632 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3633 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3634 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3635 | """ |
| 3636 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3637 | Optional argument: |
| 3638 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3639 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3640 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3641 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3642 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3643 | cmdStr += " -j" |
| 3644 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3645 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3646 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3647 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3648 | except AssertionError: |
| 3649 | main.log.exception( "" ) |
| 3650 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3651 | except TypeError: |
| 3652 | main.log.exception( self.name + ": Object not as expected" ) |
| 3653 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3654 | except pexpect.EOF: |
| 3655 | main.log.error( self.name + ": EOF exception found" ) |
| 3656 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3657 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3658 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3659 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3660 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3661 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3662 | def leaderCandidates( self, jsonFormat=True ): |
| 3663 | """ |
| 3664 | Returns the output of the leaders -c command. |
| 3665 | Optional argument: |
| 3666 | * jsonFormat - boolean indicating if you want output in json |
| 3667 | """ |
| 3668 | try: |
| 3669 | cmdStr = "onos:leaders -c" |
| 3670 | if jsonFormat: |
| 3671 | cmdStr += " -j" |
| 3672 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3673 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3674 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3675 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3676 | except AssertionError: |
| 3677 | main.log.exception( "" ) |
| 3678 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3679 | except TypeError: |
| 3680 | main.log.exception( self.name + ": Object not as expected" ) |
| 3681 | return None |
| 3682 | except pexpect.EOF: |
| 3683 | main.log.error( self.name + ": EOF exception found" ) |
| 3684 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3685 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3686 | except Exception: |
| 3687 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3688 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3689 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3690 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3691 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3692 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3693 | topic parameter and an empty list if the topic doesn't exist |
| 3694 | If no leader is elected leader in the returned list will be "none" |
| 3695 | Returns None if there is a type error processing the json object |
| 3696 | """ |
| 3697 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3698 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3699 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3700 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3701 | assert "Command not found:" not in rawOutput, rawOutput |
| 3702 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3703 | results = [] |
| 3704 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3705 | if dict[ "topic" ] == topic: |
| 3706 | leader = dict[ "leader" ] |
| 3707 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3708 | results.append( leader ) |
| 3709 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3710 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3711 | except AssertionError: |
| 3712 | main.log.exception( "" ) |
| 3713 | return None |
| 3714 | except ( TypeError, ValueError ): |
| 3715 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3716 | return None |
| 3717 | except pexpect.EOF: |
| 3718 | main.log.error( self.name + ": EOF exception found" ) |
| 3719 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3720 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3721 | except Exception: |
| 3722 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3723 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3724 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3725 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3726 | """ |
| 3727 | Returns the output of the intent Pending map. |
| 3728 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3729 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3730 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3731 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3732 | cmdStr += " -j" |
| 3733 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3734 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3735 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3736 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3737 | except AssertionError: |
| 3738 | main.log.exception( "" ) |
| 3739 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3740 | except TypeError: |
| 3741 | main.log.exception( self.name + ": Object not as expected" ) |
| 3742 | return None |
| 3743 | except pexpect.EOF: |
| 3744 | main.log.error( self.name + ": EOF exception found" ) |
| 3745 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3746 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3747 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3748 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3749 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3750 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3751 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3752 | """ |
| 3753 | Returns the output of the raft partitions command for ONOS. |
| 3754 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3755 | # Sample JSON |
| 3756 | # { |
| 3757 | # "leader": "tcp://10.128.30.11:7238", |
| 3758 | # "members": [ |
| 3759 | # "tcp://10.128.30.11:7238", |
| 3760 | # "tcp://10.128.30.17:7238", |
| 3761 | # "tcp://10.128.30.13:7238", |
| 3762 | # ], |
| 3763 | # "name": "p1", |
| 3764 | # "term": 3 |
| 3765 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3766 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3767 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3768 | if candidates: |
| 3769 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3770 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3771 | cmdStr += " -j" |
| 3772 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3773 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3774 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3775 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3776 | except AssertionError: |
| 3777 | main.log.exception( "" ) |
| 3778 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3779 | except TypeError: |
| 3780 | main.log.exception( self.name + ": Object not as expected" ) |
| 3781 | return None |
| 3782 | except pexpect.EOF: |
| 3783 | main.log.error( self.name + ": EOF exception found" ) |
| 3784 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3785 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3786 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3787 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3788 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3789 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3790 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3791 | """ |
| 3792 | Returns the output of the apps command for ONOS. This command lists |
| 3793 | information about installed ONOS applications |
| 3794 | """ |
| 3795 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3796 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3797 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3798 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3799 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3800 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3801 | cmdStr = "onos:apps" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3802 | expectJson = False |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3803 | if summary: |
| 3804 | cmdStr += " -s" |
| 3805 | if active: |
| 3806 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3807 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3808 | cmdStr += " -j" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3809 | expectJson = True |
| 3810 | output = self.sendline( cmdStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3811 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3812 | assert "Command not found:" not in output, output |
| 3813 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3814 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3815 | # FIXME: look at specific exceptions/Errors |
| 3816 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3817 | main.log.exception( self.name + ": Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3818 | return None |
| 3819 | except TypeError: |
| 3820 | main.log.exception( self.name + ": Object not as expected" ) |
| 3821 | return None |
| 3822 | except pexpect.EOF: |
| 3823 | main.log.error( self.name + ": EOF exception found" ) |
| 3824 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3825 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3826 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3827 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3828 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3829 | |
You Wang | cdc51fe | 2018-08-12 17:14:56 -0700 | [diff] [blame] | 3830 | def appStatus( self, appName ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3831 | """ |
| 3832 | Uses the onos:apps cli command to return the status of an application. |
| 3833 | Returns: |
| 3834 | "ACTIVE" - If app is installed and activated |
| 3835 | "INSTALLED" - If app is installed and deactivated |
| 3836 | "UNINSTALLED" - If app is not installed |
| 3837 | None - on error |
| 3838 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3839 | try: |
| 3840 | if not isinstance( appName, types.StringType ): |
| 3841 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3842 | " a string" ) |
| 3843 | return None |
| 3844 | output = self.apps( jsonFormat=True ) |
| 3845 | appsJson = json.loads( output ) |
| 3846 | state = None |
| 3847 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3848 | if appName == app.get( 'name' ): |
| 3849 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3850 | break |
| 3851 | if state == "ACTIVE" or state == "INSTALLED": |
| 3852 | return state |
| 3853 | elif state is None: |
You Wang | 0d9f2c0 | 2018-08-10 14:56:32 -0700 | [diff] [blame] | 3854 | main.log.warn( "{} app not found".format( appName ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3855 | return "UNINSTALLED" |
| 3856 | elif state: |
| 3857 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3858 | str( state ) ) |
| 3859 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3860 | except ( TypeError, ValueError ): |
| 3861 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3862 | return None |
| 3863 | except pexpect.EOF: |
| 3864 | main.log.error( self.name + ": EOF exception found" ) |
| 3865 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3866 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3867 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3868 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3869 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3870 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3871 | def app( self, appName, option ): |
| 3872 | """ |
| 3873 | Interacts with the app command for ONOS. This command manages |
| 3874 | application inventory. |
| 3875 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3876 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3877 | # Validate argument types |
| 3878 | valid = True |
| 3879 | if not isinstance( appName, types.StringType ): |
| 3880 | main.log.error( self.name + ".app(): appName must be a " + |
| 3881 | "string" ) |
| 3882 | valid = False |
| 3883 | if not isinstance( option, types.StringType ): |
| 3884 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3885 | valid = False |
| 3886 | if not valid: |
| 3887 | return main.FALSE |
| 3888 | # Validate Option |
| 3889 | option = option.lower() |
| 3890 | # NOTE: Install may become a valid option |
| 3891 | if option == "activate": |
| 3892 | pass |
| 3893 | elif option == "deactivate": |
| 3894 | pass |
| 3895 | elif option == "uninstall": |
| 3896 | pass |
| 3897 | else: |
| 3898 | # Invalid option |
| 3899 | main.log.error( "The ONOS app command argument only takes " + |
| 3900 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3901 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3902 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3903 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3904 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3905 | assert output is not None, "Error in sendline" |
| 3906 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3907 | if "Error executing command" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3908 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3909 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3910 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3911 | elif "No such application" in output: |
| 3912 | main.log.error( "The application '" + appName + |
| 3913 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3914 | return main.FALSE |
| 3915 | elif "Command not found:" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3916 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3917 | str( output ) ) |
| 3918 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3919 | elif "Unsupported command:" in output: |
| 3920 | main.log.error( "Incorrect command given to 'app': " + |
| 3921 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3922 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3923 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3924 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3925 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3926 | except AssertionError: |
| 3927 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3928 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3929 | except TypeError: |
| 3930 | main.log.exception( self.name + ": Object not as expected" ) |
| 3931 | return main.ERROR |
| 3932 | except pexpect.EOF: |
| 3933 | main.log.error( self.name + ": EOF exception found" ) |
| 3934 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3935 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3936 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3937 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3938 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3939 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3940 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3941 | """ |
| 3942 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3943 | appName is the hierarchical app name, not the feature name |
| 3944 | If check is True, method will check the status of the app after the |
| 3945 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3946 | Returns main.TRUE if the command was successfully sent |
| 3947 | main.FALSE if the cli responded with an error or given |
| 3948 | incorrect input |
| 3949 | """ |
| 3950 | try: |
| 3951 | if not isinstance( appName, types.StringType ): |
| 3952 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3953 | " a string" ) |
| 3954 | return main.FALSE |
| 3955 | status = self.appStatus( appName ) |
| 3956 | if status == "INSTALLED": |
| 3957 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3958 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3959 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3960 | status = self.appStatus( appName ) |
| 3961 | if status == "ACTIVE": |
| 3962 | return main.TRUE |
| 3963 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3964 | main.log.debug( "The state of application " + |
| 3965 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3966 | time.sleep( 1 ) |
| 3967 | return main.FALSE |
| 3968 | else: # not 'check' or command didn't succeed |
| 3969 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3970 | elif status == "ACTIVE": |
| 3971 | return main.TRUE |
| 3972 | elif status == "UNINSTALLED": |
| 3973 | main.log.error( self.name + ": Tried to activate the " + |
| 3974 | "application '" + appName + "' which is not " + |
| 3975 | "installed." ) |
| 3976 | else: |
| 3977 | main.log.error( "Unexpected return value from appStatus: " + |
| 3978 | str( status ) ) |
| 3979 | return main.ERROR |
| 3980 | except TypeError: |
| 3981 | main.log.exception( self.name + ": Object not as expected" ) |
| 3982 | return main.ERROR |
| 3983 | except pexpect.EOF: |
| 3984 | main.log.error( self.name + ": EOF exception found" ) |
| 3985 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3986 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3987 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3988 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3989 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3990 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3991 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3992 | """ |
| 3993 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3994 | appName is the hierarchical app name, not the feature name |
| 3995 | If check is True, method will check the status of the app after the |
| 3996 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3997 | Returns main.TRUE if the command was successfully sent |
| 3998 | main.FALSE if the cli responded with an error or given |
| 3999 | incorrect input |
| 4000 | """ |
| 4001 | try: |
| 4002 | if not isinstance( appName, types.StringType ): |
| 4003 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 4004 | "be a string" ) |
| 4005 | return main.FALSE |
| 4006 | status = self.appStatus( appName ) |
| 4007 | if status == "INSTALLED": |
| 4008 | return main.TRUE |
| 4009 | elif status == "ACTIVE": |
| 4010 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4011 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4012 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4013 | status = self.appStatus( appName ) |
| 4014 | if status == "INSTALLED": |
| 4015 | return main.TRUE |
| 4016 | else: |
| 4017 | time.sleep( 1 ) |
| 4018 | return main.FALSE |
| 4019 | else: # not check or command didn't succeed |
| 4020 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4021 | elif status == "UNINSTALLED": |
| 4022 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 4023 | "application '" + appName + "' which is not " + |
| 4024 | "installed." ) |
| 4025 | return main.TRUE |
| 4026 | else: |
| 4027 | main.log.error( "Unexpected return value from appStatus: " + |
| 4028 | str( status ) ) |
| 4029 | return main.ERROR |
| 4030 | except TypeError: |
| 4031 | main.log.exception( self.name + ": Object not as expected" ) |
| 4032 | return main.ERROR |
| 4033 | except pexpect.EOF: |
| 4034 | main.log.error( self.name + ": EOF exception found" ) |
| 4035 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4036 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4037 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4038 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4039 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4040 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4041 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4042 | """ |
| 4043 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4044 | appName is the hierarchical app name, not the feature name |
| 4045 | If check is True, method will check the status of the app after the |
| 4046 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4047 | Returns main.TRUE if the command was successfully sent |
| 4048 | main.FALSE if the cli responded with an error or given |
| 4049 | incorrect input |
| 4050 | """ |
| 4051 | # TODO: check with Thomas about the state machine for apps |
| 4052 | try: |
| 4053 | if not isinstance( appName, types.StringType ): |
| 4054 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 4055 | "be a string" ) |
| 4056 | return main.FALSE |
| 4057 | status = self.appStatus( appName ) |
| 4058 | if status == "INSTALLED": |
| 4059 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4060 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4061 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4062 | status = self.appStatus( appName ) |
| 4063 | if status == "UNINSTALLED": |
| 4064 | return main.TRUE |
| 4065 | else: |
| 4066 | time.sleep( 1 ) |
| 4067 | return main.FALSE |
| 4068 | else: # not check or command didn't succeed |
| 4069 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4070 | elif status == "ACTIVE": |
| 4071 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 4072 | "application '" + appName + "' which is " + |
| 4073 | "currently active." ) |
| 4074 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4075 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4076 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4077 | status = self.appStatus( appName ) |
| 4078 | if status == "UNINSTALLED": |
| 4079 | return main.TRUE |
| 4080 | else: |
| 4081 | time.sleep( 1 ) |
| 4082 | return main.FALSE |
| 4083 | else: # not check or command didn't succeed |
| 4084 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4085 | elif status == "UNINSTALLED": |
| 4086 | return main.TRUE |
| 4087 | else: |
| 4088 | main.log.error( "Unexpected return value from appStatus: " + |
| 4089 | str( status ) ) |
| 4090 | return main.ERROR |
| 4091 | except TypeError: |
| 4092 | main.log.exception( self.name + ": Object not as expected" ) |
| 4093 | return main.ERROR |
| 4094 | except pexpect.EOF: |
| 4095 | main.log.error( self.name + ": EOF exception found" ) |
| 4096 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4097 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4098 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4099 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4100 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4101 | |
| 4102 | def appIDs( self, jsonFormat=True ): |
| 4103 | """ |
| 4104 | Show the mappings between app id and app names given by the 'app-ids' |
| 4105 | cli command |
| 4106 | """ |
| 4107 | try: |
| 4108 | cmdStr = "app-ids" |
| 4109 | if jsonFormat: |
| 4110 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4111 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4112 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4113 | assert "Command not found:" not in output, output |
| 4114 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4115 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4116 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4117 | main.log.exception( self.name + ": Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4118 | return None |
| 4119 | except TypeError: |
| 4120 | main.log.exception( self.name + ": Object not as expected" ) |
| 4121 | return None |
| 4122 | except pexpect.EOF: |
| 4123 | main.log.error( self.name + ": EOF exception found" ) |
| 4124 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4125 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4126 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4127 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4128 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4129 | |
| 4130 | def appToIDCheck( self ): |
| 4131 | """ |
| 4132 | This method will check that each application's ID listed in 'apps' is |
| 4133 | the same as the ID listed in 'app-ids'. The check will also check that |
| 4134 | there are no duplicate IDs issued. Note that an app ID should be |
| 4135 | a globaly unique numerical identifier for app/app-like features. Once |
| 4136 | an ID is registered, the ID is never freed up so that if an app is |
| 4137 | reinstalled it will have the same ID. |
| 4138 | |
| 4139 | Returns: main.TRUE if the check passes and |
| 4140 | main.FALSE if the check fails or |
| 4141 | main.ERROR if there is some error in processing the test |
| 4142 | """ |
| 4143 | try: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4144 | # Grab IDs |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4145 | rawJson = self.appIDs( jsonFormat=True ) |
| 4146 | if rawJson: |
| 4147 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4148 | else: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4149 | main.log.error( "app-ids returned nothing: " + repr( rawJson ) ) |
| 4150 | return main.FALSE |
| 4151 | |
| 4152 | # Grab Apps |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4153 | rawJson = self.apps( jsonFormat=True ) |
| 4154 | if rawJson: |
| 4155 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4156 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4157 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4158 | return main.FALSE |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4159 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4160 | result = main.TRUE |
| 4161 | for app in apps: |
| 4162 | appID = app.get( 'id' ) |
| 4163 | if appID is None: |
| 4164 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4165 | result = main.FALSE |
| 4166 | appName = app.get( 'name' ) |
| 4167 | if appName is None: |
| 4168 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4169 | result = main.FALSE |
| 4170 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4171 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4172 | if not current: # if ids doesn't have this id |
| 4173 | result = main.FALSE |
| 4174 | main.log.error( "'app-ids' does not have the ID for " + |
| 4175 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4176 | main.log.debug( "apps command returned: " + str( app ) + |
| 4177 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4178 | elif len( current ) > 1: |
| 4179 | # there is more than one app with this ID |
| 4180 | result = main.FALSE |
| 4181 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4182 | elif not current[ 0 ][ 'name' ] == appName: |
| 4183 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4184 | result = main.FALSE |
| 4185 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4186 | " registered under id:" + str( appID ) + |
| 4187 | " but 'apps' has " + str( appName ) ) |
| 4188 | else: |
| 4189 | pass # id and name match! |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4190 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4191 | # now make sure that app-ids has no duplicates |
| 4192 | idsList = [] |
| 4193 | namesList = [] |
| 4194 | for item in ids: |
| 4195 | idsList.append( item[ 'id' ] ) |
| 4196 | namesList.append( item[ 'name' ] ) |
| 4197 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4198 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4199 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4200 | + json.dumps( ids, |
| 4201 | sort_keys=True, |
| 4202 | indent=4, |
| 4203 | separators=( ',', ': ' ) ) ) |
| 4204 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4205 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4206 | except ( TypeError, ValueError ): |
| 4207 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4208 | return main.ERROR |
| 4209 | except pexpect.EOF: |
| 4210 | main.log.error( self.name + ": EOF exception found" ) |
| 4211 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4212 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4213 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4214 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4215 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4216 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4217 | def getCfg( self, component=None, propName=None, short=False, |
| 4218 | jsonFormat=True ): |
| 4219 | """ |
| 4220 | Get configuration settings from onos cli |
| 4221 | Optional arguments: |
| 4222 | component - Optionally only list configurations for a specific |
| 4223 | component. If None, all components with configurations |
| 4224 | are displayed. Case Sensitive string. |
| 4225 | propName - If component is specified, propName option will show |
| 4226 | only this specific configuration from that component. |
| 4227 | Case Sensitive string. |
| 4228 | jsonFormat - Returns output as json. Note that this will override |
| 4229 | the short option |
| 4230 | short - Short, less verbose, version of configurations. |
| 4231 | This is overridden by the json option |
| 4232 | returns: |
| 4233 | Output from cli as a string or None on error |
| 4234 | """ |
| 4235 | try: |
| 4236 | baseStr = "cfg" |
| 4237 | cmdStr = " get" |
| 4238 | componentStr = "" |
| 4239 | if component: |
| 4240 | componentStr += " " + component |
| 4241 | if propName: |
| 4242 | componentStr += " " + propName |
| 4243 | if jsonFormat: |
| 4244 | baseStr += " -j" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4245 | expectJson = True |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4246 | elif short: |
| 4247 | baseStr += " -s" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4248 | expectJson = False |
| 4249 | output = self.sendline( baseStr + cmdStr + componentStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4250 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4251 | assert "Command not found:" not in output, output |
| 4252 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4253 | return output |
| 4254 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4255 | main.log.exception( self.name + ": Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4256 | return None |
| 4257 | except TypeError: |
| 4258 | main.log.exception( self.name + ": Object not as expected" ) |
| 4259 | return None |
| 4260 | except pexpect.EOF: |
| 4261 | main.log.error( self.name + ": EOF exception found" ) |
| 4262 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4263 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4264 | except Exception: |
| 4265 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4266 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4267 | |
| 4268 | def setCfg( self, component, propName, value=None, check=True ): |
| 4269 | """ |
| 4270 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4271 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4272 | component - The case sensitive name of the component whose |
| 4273 | property is to be set |
| 4274 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4275 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4276 | value - The value to set the property to. If None, will unset the |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4277 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4278 | check - Boolean, Check whether the option was successfully set this |
| 4279 | only applies when a value is given. |
| 4280 | returns: |
| 4281 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4282 | will return main.TRUE unless there is an error |
| 4283 | """ |
| 4284 | try: |
| 4285 | baseStr = "cfg" |
| 4286 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4287 | if value is not None: |
| 4288 | cmdStr += " " + str( value ) |
| 4289 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4290 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4291 | assert "Command not found:" not in output, output |
| 4292 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4293 | if value and check: |
| 4294 | results = self.getCfg( component=str( component ), |
| 4295 | propName=str( propName ), |
| 4296 | jsonFormat=True ) |
| 4297 | # Check if current value is what we just set |
| 4298 | try: |
| 4299 | jsonOutput = json.loads( results ) |
| 4300 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4301 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4302 | main.log.exception( "Error parsing cfg output" ) |
| 4303 | main.log.error( "output:" + repr( results ) ) |
| 4304 | return main.FALSE |
| 4305 | if current == str( value ): |
| 4306 | return main.TRUE |
| 4307 | return main.FALSE |
| 4308 | return main.TRUE |
| 4309 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4310 | main.log.exception( self.name + ": Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4311 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4312 | except ( TypeError, ValueError ): |
| 4313 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4314 | return main.FALSE |
| 4315 | except pexpect.EOF: |
| 4316 | main.log.error( self.name + ": EOF exception found" ) |
| 4317 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4318 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4319 | except Exception: |
| 4320 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4321 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4322 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4323 | def distPrimitivesSend( self, cmd ): |
| 4324 | """ |
| 4325 | Function to handle sending cli commands for the distributed primitives test app |
| 4326 | |
| 4327 | This command will catch some exceptions and retry the command on some |
| 4328 | specific store exceptions. |
| 4329 | |
| 4330 | Required arguments: |
| 4331 | cmd - The command to send to the cli |
| 4332 | returns: |
| 4333 | string containing the cli output |
| 4334 | None on Error |
| 4335 | """ |
| 4336 | try: |
| 4337 | output = self.sendline( cmd ) |
| 4338 | try: |
| 4339 | assert output is not None, "Error in sendline" |
| 4340 | # TODO: Maybe make this less hardcoded |
| 4341 | # ConsistentMap Exceptions |
| 4342 | assert "org.onosproject.store.service" not in output |
| 4343 | # Node not leader |
| 4344 | assert "java.lang.IllegalStateException" not in output |
| 4345 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4346 | main.log.error( self.name + ": Error in processing '" + cmd + "' " + |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4347 | "command: " + str( output ) ) |
| 4348 | retryTime = 30 # Conservative time, given by Madan |
| 4349 | main.log.info( "Waiting " + str( retryTime ) + |
| 4350 | "seconds before retrying." ) |
| 4351 | time.sleep( retryTime ) # Due to change in mastership |
| 4352 | output = self.sendline( cmd ) |
| 4353 | assert output is not None, "Error in sendline" |
| 4354 | assert "Command not found:" not in output, output |
| 4355 | assert "Error executing command" not in output, output |
| 4356 | main.log.info( self.name + ": " + output ) |
| 4357 | return output |
| 4358 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4359 | main.log.exception( self.name + ": Error in processing '" + cmd + "' command." ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4360 | return None |
| 4361 | except TypeError: |
| 4362 | main.log.exception( self.name + ": Object not as expected" ) |
| 4363 | return None |
| 4364 | except pexpect.EOF: |
| 4365 | main.log.error( self.name + ": EOF exception found" ) |
| 4366 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4367 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4368 | except Exception: |
| 4369 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4370 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4371 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4372 | def setTestAdd( self, setName, values ): |
| 4373 | """ |
| 4374 | CLI command to add elements to a distributed set. |
| 4375 | Arguments: |
| 4376 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4377 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4378 | Example usages: |
| 4379 | setTestAdd( "set1", "a b c" ) |
| 4380 | setTestAdd( "set2", "1" ) |
| 4381 | returns: |
| 4382 | main.TRUE on success OR |
| 4383 | main.FALSE if elements were already in the set OR |
| 4384 | main.ERROR on error |
| 4385 | """ |
| 4386 | try: |
| 4387 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4388 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4389 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4390 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4391 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4392 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4393 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4394 | return main.FALSE |
| 4395 | else: |
| 4396 | main.log.error( self.name + ": setTestAdd did not" + |
| 4397 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4398 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4399 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4400 | except TypeError: |
| 4401 | main.log.exception( self.name + ": Object not as expected" ) |
| 4402 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4403 | except Exception: |
| 4404 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4405 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4406 | |
| 4407 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4408 | """ |
| 4409 | CLI command to remove elements from a distributed set. |
| 4410 | Required arguments: |
| 4411 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4412 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4413 | Optional arguments: |
| 4414 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4415 | retain - Retain only the given values. (intersection of the |
| 4416 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4417 | returns: |
| 4418 | main.TRUE on success OR |
| 4419 | main.FALSE if the set was not changed OR |
| 4420 | main.ERROR on error |
| 4421 | """ |
| 4422 | try: |
| 4423 | cmdStr = "set-test-remove " |
| 4424 | if clear: |
| 4425 | cmdStr += "-c " + str( setName ) |
| 4426 | elif retain: |
| 4427 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4428 | else: |
| 4429 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4430 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4431 | if clear: |
| 4432 | pattern = "Set " + str( setName ) + " cleared" |
| 4433 | if re.search( pattern, output ): |
| 4434 | return main.TRUE |
| 4435 | elif retain: |
| 4436 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4437 | "only elements of set \[(.*)\]" |
| 4438 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4439 | "retaining only elements of the set " +\ |
| 4440 | "\[(.*)\]" |
| 4441 | if re.search( positivePattern, output ): |
| 4442 | return main.TRUE |
| 4443 | elif re.search( negativePattern, output ): |
| 4444 | return main.FALSE |
| 4445 | else: |
| 4446 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4447 | str( setName ) |
| 4448 | if ( len( values.split() ) == 1 ): |
| 4449 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4450 | str( setName ) |
| 4451 | else: |
| 4452 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4453 | str( setName ) |
| 4454 | if re.search( positivePattern, output ): |
| 4455 | return main.TRUE |
| 4456 | elif re.search( negativePattern, output ): |
| 4457 | return main.FALSE |
| 4458 | main.log.error( self.name + ": setTestRemove did not" + |
| 4459 | " match expected output" ) |
| 4460 | main.log.debug( self.name + " expected: " + pattern ) |
| 4461 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4462 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4463 | except TypeError: |
| 4464 | main.log.exception( self.name + ": Object not as expected" ) |
| 4465 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4466 | except Exception: |
| 4467 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4468 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4469 | |
| 4470 | def setTestGet( self, setName, values="" ): |
| 4471 | """ |
| 4472 | CLI command to get the elements in a distributed set. |
| 4473 | Required arguments: |
| 4474 | setName - The name of the set to remove from. |
| 4475 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4476 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4477 | returns: |
| 4478 | main.ERROR on error OR |
| 4479 | A list of elements in the set if no optional arguments are |
| 4480 | supplied OR |
| 4481 | A tuple containing the list then: |
| 4482 | main.FALSE if the given values are not in the set OR |
| 4483 | main.TRUE if the given values are in the set OR |
| 4484 | """ |
| 4485 | try: |
| 4486 | values = str( values ).strip() |
| 4487 | setName = str( setName ).strip() |
| 4488 | length = len( values.split() ) |
| 4489 | containsCheck = None |
| 4490 | # Patterns to match |
| 4491 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4492 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4493 | containsTrue = "Set " + setName + " contains the value " + values |
| 4494 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4495 | values |
| 4496 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4497 | setPattern |
| 4498 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4499 | " subset " + setPattern |
| 4500 | |
| 4501 | cmdStr = "set-test-get " |
| 4502 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4503 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4504 | if length == 0: |
| 4505 | match = re.search( pattern, output ) |
| 4506 | else: # if given values |
| 4507 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4508 | patternTrue = pattern + "\r\n" + containsTrue |
| 4509 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4510 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4511 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4512 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4513 | matchTrue = re.search( patternTrue, output ) |
| 4514 | matchFalse = re.search( patternFalse, output ) |
| 4515 | if matchTrue: |
| 4516 | containsCheck = main.TRUE |
| 4517 | match = matchTrue |
| 4518 | elif matchFalse: |
| 4519 | containsCheck = main.FALSE |
| 4520 | match = matchFalse |
| 4521 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4522 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4523 | "expected output" ) |
| 4524 | main.log.debug( self.name + " expected: " + pattern ) |
| 4525 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4526 | match = None |
| 4527 | if match: |
| 4528 | setMatch = match.group( 1 ) |
| 4529 | if setMatch == '': |
| 4530 | setList = [] |
| 4531 | else: |
| 4532 | setList = setMatch.split( ", " ) |
| 4533 | if length > 0: |
| 4534 | return ( setList, containsCheck ) |
| 4535 | else: |
| 4536 | return setList |
| 4537 | else: # no match |
| 4538 | main.log.error( self.name + ": setTestGet did not" + |
| 4539 | " match expected output" ) |
| 4540 | main.log.debug( self.name + " expected: " + pattern ) |
| 4541 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4542 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4543 | except TypeError: |
| 4544 | main.log.exception( self.name + ": Object not as expected" ) |
| 4545 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4546 | except Exception: |
| 4547 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4548 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4549 | |
| 4550 | def setTestSize( self, setName ): |
| 4551 | """ |
| 4552 | CLI command to get the elements in a distributed set. |
| 4553 | Required arguments: |
| 4554 | setName - The name of the set to remove from. |
| 4555 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4556 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4557 | None on error |
| 4558 | """ |
| 4559 | try: |
| 4560 | # TODO: Should this check against the number of elements returned |
| 4561 | # and then return true/false based on that? |
| 4562 | setName = str( setName ).strip() |
| 4563 | # Patterns to match |
| 4564 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4565 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4566 | setPattern |
| 4567 | cmdStr = "set-test-get -s " |
| 4568 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4569 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4570 | if output: |
| 4571 | match = re.search( pattern, output ) |
| 4572 | if match: |
| 4573 | setSize = int( match.group( 1 ) ) |
| 4574 | setMatch = match.group( 2 ) |
| 4575 | if len( setMatch.split() ) == setSize: |
| 4576 | main.log.info( "The size returned by " + self.name + |
| 4577 | " matches the number of elements in " + |
| 4578 | "the returned set" ) |
| 4579 | else: |
| 4580 | main.log.error( "The size returned by " + self.name + |
| 4581 | " does not match the number of " + |
| 4582 | "elements in the returned set." ) |
| 4583 | return setSize |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4584 | else: # no match |
| 4585 | main.log.error( self.name + ": setTestGet did not" + |
| 4586 | " match expected output" ) |
| 4587 | main.log.debug( self.name + " expected: " + pattern ) |
| 4588 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4589 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4590 | except TypeError: |
| 4591 | main.log.exception( self.name + ": Object not as expected" ) |
| 4592 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4593 | except Exception: |
| 4594 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4595 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4596 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4597 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4598 | """ |
| 4599 | Command to list the various counters in the system. |
| 4600 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4601 | if jsonFormat, a string of the json object returned by the cli |
| 4602 | command |
| 4603 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4604 | None on error |
| 4605 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4606 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4607 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4608 | if jsonFormat: |
| 4609 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4610 | output = self.sendline( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4611 | main.log.debug( self.name + ": Counters unparsed: " + output ) |
| 4612 | output = output.split( "\r\n" )[ -1 ] |
| 4613 | main.log.debug( self.name + ": Counters parsed: " + output ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4614 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4615 | assert "Command not found:" not in output, output |
| 4616 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4617 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4618 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4619 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4620 | main.log.exception( self.name + ": Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4621 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4622 | except TypeError: |
| 4623 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4624 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4625 | except pexpect.EOF: |
| 4626 | main.log.error( self.name + ": EOF exception found" ) |
| 4627 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4628 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4629 | except Exception: |
| 4630 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4631 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4632 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4633 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4634 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4635 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4636 | Required arguments: |
| 4637 | counter - The name of the counter to increment. |
| 4638 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4639 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4640 | returns: |
| 4641 | integer value of the counter or |
| 4642 | None on Error |
| 4643 | """ |
| 4644 | try: |
| 4645 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4646 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4647 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4648 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4649 | if delta != 1: |
| 4650 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4651 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4652 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4653 | match = re.search( pattern, output ) |
| 4654 | if match: |
| 4655 | return int( match.group( 1 ) ) |
| 4656 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4657 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4658 | " match expected output." ) |
| 4659 | main.log.debug( self.name + " expected: " + pattern ) |
| 4660 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4661 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4662 | except TypeError: |
| 4663 | main.log.exception( self.name + ": Object not as expected" ) |
| 4664 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4665 | except Exception: |
| 4666 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4667 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4668 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4669 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4670 | """ |
| 4671 | CLI command to get a distributed counter then add a delta to it. |
| 4672 | Required arguments: |
| 4673 | counter - The name of the counter to increment. |
| 4674 | Optional arguments: |
| 4675 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4676 | returns: |
| 4677 | integer value of the counter or |
| 4678 | None on Error |
| 4679 | """ |
| 4680 | try: |
| 4681 | counter = str( counter ) |
| 4682 | delta = int( delta ) |
| 4683 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4684 | cmdStr += counter |
| 4685 | if delta != 1: |
| 4686 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4687 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4688 | pattern = counter + " was updated to (-?\d+)" |
| 4689 | match = re.search( pattern, output ) |
| 4690 | if match: |
| 4691 | return int( match.group( 1 ) ) |
| 4692 | else: |
| 4693 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4694 | " match expected output." ) |
| 4695 | main.log.debug( self.name + " expected: " + pattern ) |
| 4696 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4697 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4698 | except TypeError: |
| 4699 | main.log.exception( self.name + ": Object not as expected" ) |
| 4700 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4701 | except Exception: |
| 4702 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4703 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4704 | |
| 4705 | def valueTestGet( self, valueName ): |
| 4706 | """ |
| 4707 | CLI command to get the value of an atomic value. |
| 4708 | Required arguments: |
| 4709 | valueName - The name of the value to get. |
| 4710 | returns: |
| 4711 | string value of the value or |
| 4712 | None on Error |
| 4713 | """ |
| 4714 | try: |
| 4715 | valueName = str( valueName ) |
| 4716 | cmdStr = "value-test " |
| 4717 | operation = "get" |
| 4718 | cmdStr = "value-test {} {}".format( valueName, |
| 4719 | operation ) |
| 4720 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4721 | main.log.debug( self.name + ": value test unparsed: " + output ) |
| 4722 | output = output.split( "\r\n" )[ -1 ] |
| 4723 | main.log.debug( self.name + ": value test parsed: " + output ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4724 | pattern = "(\w+)" |
| 4725 | match = re.search( pattern, output ) |
| 4726 | if match: |
| 4727 | return match.group( 1 ) |
| 4728 | else: |
| 4729 | main.log.error( self.name + ": valueTestGet did not" + |
| 4730 | " match expected output." ) |
| 4731 | main.log.debug( self.name + " expected: " + pattern ) |
| 4732 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4733 | return None |
| 4734 | except TypeError: |
| 4735 | main.log.exception( self.name + ": Object not as expected" ) |
| 4736 | return None |
| 4737 | except Exception: |
| 4738 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4739 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4740 | |
| 4741 | def valueTestSet( self, valueName, newValue ): |
| 4742 | """ |
| 4743 | CLI command to set the value of an atomic value. |
| 4744 | Required arguments: |
| 4745 | valueName - The name of the value to set. |
| 4746 | newValue - The value to assign to the given value. |
| 4747 | returns: |
| 4748 | main.TRUE on success or |
| 4749 | main.ERROR on Error |
| 4750 | """ |
| 4751 | try: |
| 4752 | valueName = str( valueName ) |
| 4753 | newValue = str( newValue ) |
| 4754 | operation = "set" |
| 4755 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4756 | operation, |
| 4757 | newValue ) |
| 4758 | output = self.distPrimitivesSend( cmdStr ) |
| 4759 | if output is not None: |
| 4760 | return main.TRUE |
| 4761 | else: |
| 4762 | return main.ERROR |
| 4763 | except TypeError: |
| 4764 | main.log.exception( self.name + ": Object not as expected" ) |
| 4765 | return main.ERROR |
| 4766 | except Exception: |
| 4767 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4768 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4769 | |
| 4770 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4771 | """ |
| 4772 | CLI command to compareAndSet the value of an atomic value. |
| 4773 | Required arguments: |
| 4774 | valueName - The name of the value. |
| 4775 | oldValue - Compare the current value of the atomic value to this |
| 4776 | newValue - If the value equals oldValue, set the value to newValue |
| 4777 | returns: |
| 4778 | main.TRUE on success or |
| 4779 | main.FALSE on failure or |
| 4780 | main.ERROR on Error |
| 4781 | """ |
| 4782 | try: |
| 4783 | valueName = str( valueName ) |
| 4784 | oldValue = str( oldValue ) |
| 4785 | newValue = str( newValue ) |
| 4786 | operation = "compareAndSet" |
| 4787 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4788 | operation, |
| 4789 | oldValue, |
| 4790 | newValue ) |
| 4791 | output = self.distPrimitivesSend( cmdStr ) |
| 4792 | pattern = "(\w+)" |
| 4793 | match = re.search( pattern, output ) |
| 4794 | if match: |
| 4795 | result = match.group( 1 ) |
| 4796 | if result == "true": |
| 4797 | return main.TRUE |
| 4798 | elif result == "false": |
| 4799 | return main.FALSE |
| 4800 | else: |
| 4801 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4802 | " match expected output." ) |
| 4803 | main.log.debug( self.name + " expected: " + pattern ) |
| 4804 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4805 | return main.ERROR |
| 4806 | except TypeError: |
| 4807 | main.log.exception( self.name + ": Object not as expected" ) |
| 4808 | return main.ERROR |
| 4809 | except Exception: |
| 4810 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4811 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4812 | |
| 4813 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4814 | """ |
| 4815 | CLI command to getAndSet the value of an atomic value. |
| 4816 | Required arguments: |
| 4817 | valueName - The name of the value to get. |
| 4818 | newValue - The value to assign to the given value |
| 4819 | returns: |
| 4820 | string value of the value or |
| 4821 | None on Error |
| 4822 | """ |
| 4823 | try: |
| 4824 | valueName = str( valueName ) |
| 4825 | cmdStr = "value-test " |
| 4826 | operation = "getAndSet" |
| 4827 | cmdStr += valueName + " " + operation |
| 4828 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4829 | operation, |
| 4830 | newValue ) |
| 4831 | output = self.distPrimitivesSend( cmdStr ) |
| 4832 | pattern = "(\w+)" |
| 4833 | match = re.search( pattern, output ) |
| 4834 | if match: |
| 4835 | return match.group( 1 ) |
| 4836 | else: |
| 4837 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4838 | " match expected output." ) |
| 4839 | main.log.debug( self.name + " expected: " + pattern ) |
| 4840 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4841 | return None |
| 4842 | except TypeError: |
| 4843 | main.log.exception( self.name + ": Object not as expected" ) |
| 4844 | return None |
| 4845 | except Exception: |
| 4846 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4847 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4848 | |
| 4849 | def valueTestDestroy( self, valueName ): |
| 4850 | """ |
| 4851 | CLI command to destroy an atomic value. |
| 4852 | Required arguments: |
| 4853 | valueName - The name of the value to destroy. |
| 4854 | returns: |
| 4855 | main.TRUE on success or |
| 4856 | main.ERROR on Error |
| 4857 | """ |
| 4858 | try: |
| 4859 | valueName = str( valueName ) |
| 4860 | cmdStr = "value-test " |
| 4861 | operation = "destroy" |
| 4862 | cmdStr += valueName + " " + operation |
| 4863 | output = self.distPrimitivesSend( cmdStr ) |
| 4864 | if output is not None: |
| 4865 | return main.TRUE |
| 4866 | else: |
| 4867 | return main.ERROR |
| 4868 | except TypeError: |
| 4869 | main.log.exception( self.name + ": Object not as expected" ) |
| 4870 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4871 | except Exception: |
| 4872 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4873 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4874 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4875 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4876 | """ |
| 4877 | Description: Execute summary command in onos |
| 4878 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4879 | no output |
| 4880 | |
| 4881 | """ |
| 4882 | try: |
| 4883 | cmdStr = "summary" |
| 4884 | if jsonFormat: |
| 4885 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4886 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4887 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4888 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4889 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 4890 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4891 | if not handle: |
| 4892 | main.log.error( self.name + ": There is no output in " + |
| 4893 | "summary command" ) |
| 4894 | return main.FALSE |
| 4895 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4896 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4897 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4898 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4899 | except TypeError: |
| 4900 | main.log.exception( self.name + ": Object not as expected" ) |
| 4901 | return None |
| 4902 | except pexpect.EOF: |
| 4903 | main.log.error( self.name + ": EOF exception found" ) |
| 4904 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4905 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4906 | except Exception: |
| 4907 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4908 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4909 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4910 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4911 | """ |
| 4912 | CLI command to get the value of a key in a consistent map using |
| 4913 | transactions. This a test function and can only get keys from the |
| 4914 | test map hard coded into the cli command |
| 4915 | Required arguments: |
| 4916 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4917 | returns: |
| 4918 | The string value of the key or |
| 4919 | None on Error |
| 4920 | """ |
| 4921 | try: |
| 4922 | keyName = str( keyName ) |
| 4923 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4924 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4925 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4926 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4927 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4928 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4929 | return None |
| 4930 | else: |
| 4931 | match = re.search( pattern, output ) |
| 4932 | if match: |
| 4933 | return match.groupdict()[ 'value' ] |
| 4934 | else: |
| 4935 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4936 | " match expected output." ) |
| 4937 | main.log.debug( self.name + " expected: " + pattern ) |
| 4938 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4939 | return None |
| 4940 | except TypeError: |
| 4941 | main.log.exception( self.name + ": Object not as expected" ) |
| 4942 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4943 | except Exception: |
| 4944 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4945 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4946 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4947 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4948 | """ |
| 4949 | CLI command to put a value into 'numKeys' number of keys in a |
| 4950 | consistent map using transactions. This a test function and can only |
| 4951 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4952 | Required arguments: |
| 4953 | numKeys - Number of keys to add the value to |
| 4954 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4955 | returns: |
| 4956 | A dictionary whose keys are the name of the keys put into the map |
| 4957 | and the values of the keys are dictionaries whose key-values are |
| 4958 | 'value': value put into map and optionaly |
| 4959 | 'oldValue': Previous value in the key or |
| 4960 | None on Error |
| 4961 | |
| 4962 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4963 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4964 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4965 | """ |
| 4966 | try: |
| 4967 | numKeys = str( numKeys ) |
| 4968 | value = str( value ) |
| 4969 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4970 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4971 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4972 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4973 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4974 | results = {} |
| 4975 | for line in output.splitlines(): |
| 4976 | new = re.search( newPattern, line ) |
| 4977 | updated = re.search( updatedPattern, line ) |
| 4978 | if new: |
| 4979 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4980 | elif updated: |
| 4981 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4982 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4983 | else: |
| 4984 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4985 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4986 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4987 | newPattern, |
| 4988 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4989 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4990 | return results |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4991 | except ( TypeError, AttributeError ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4992 | main.log.exception( self.name + ": Object not as expected" ) |
| 4993 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4994 | except Exception: |
| 4995 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4996 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4997 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4998 | def maps( self, jsonFormat=True ): |
| 4999 | """ |
| 5000 | Description: Returns result of onos:maps |
| 5001 | Optional: |
| 5002 | * jsonFormat: enable json formatting of output |
| 5003 | """ |
| 5004 | try: |
| 5005 | cmdStr = "maps" |
| 5006 | if jsonFormat: |
| 5007 | cmdStr += " -j" |
| 5008 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5009 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5010 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5011 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5012 | except AssertionError: |
| 5013 | main.log.exception( "" ) |
| 5014 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5015 | except TypeError: |
| 5016 | main.log.exception( self.name + ": Object not as expected" ) |
| 5017 | return None |
| 5018 | except pexpect.EOF: |
| 5019 | main.log.error( self.name + ": EOF exception found" ) |
| 5020 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5021 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5022 | except Exception: |
| 5023 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5024 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5025 | |
| 5026 | def getSwController( self, uri, jsonFormat=True ): |
| 5027 | """ |
| 5028 | Descrition: Gets the controller information from the device |
| 5029 | """ |
| 5030 | try: |
| 5031 | cmd = "device-controllers " |
| 5032 | if jsonFormat: |
| 5033 | cmd += "-j " |
| 5034 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5035 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5036 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5037 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5038 | except AssertionError: |
| 5039 | main.log.exception( "" ) |
| 5040 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5041 | except TypeError: |
| 5042 | main.log.exception( self.name + ": Object not as expected" ) |
| 5043 | return None |
| 5044 | except pexpect.EOF: |
| 5045 | main.log.error( self.name + ": EOF exception found" ) |
| 5046 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5047 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5048 | except Exception: |
| 5049 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5050 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5051 | |
| 5052 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 5053 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5054 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5055 | |
| 5056 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5057 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5058 | ip - String or List: The ip address of the controller. |
| 5059 | This parameter can be formed in a couple of different ways. |
| 5060 | VALID: |
| 5061 | 10.0.0.1 - just the ip address |
| 5062 | tcp:10.0.0.1 - the protocol and the ip address |
| 5063 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 5064 | so that you can add controllers with different |
| 5065 | protocols and ports |
| 5066 | INVALID: |
| 5067 | 10.0.0.1:6653 - this is not supported by ONOS |
| 5068 | |
| 5069 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 5070 | port - The port number. |
| 5071 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 5072 | |
| 5073 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 5074 | """ |
| 5075 | try: |
| 5076 | cmd = "device-setcontrollers" |
| 5077 | |
| 5078 | if jsonFormat: |
| 5079 | cmd += " -j" |
| 5080 | cmd += " " + uri |
| 5081 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5082 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5083 | for item in ip: |
| 5084 | if ":" in item: |
| 5085 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5086 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5087 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5088 | elif "." in sitem[ 1 ]: |
| 5089 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5090 | else: |
| 5091 | main.log.error( "Malformed entry: " + item ) |
| 5092 | raise TypeError |
| 5093 | else: |
| 5094 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5095 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5096 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5097 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5098 | if "Error" in response: |
| 5099 | main.log.error( response ) |
| 5100 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5101 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5102 | except AssertionError: |
| 5103 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5104 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5105 | except TypeError: |
| 5106 | main.log.exception( self.name + ": Object not as expected" ) |
| 5107 | return main.FALSE |
| 5108 | except pexpect.EOF: |
| 5109 | main.log.error( self.name + ": EOF exception found" ) |
| 5110 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5111 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5112 | except Exception: |
| 5113 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5114 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5115 | |
| 5116 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5117 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5118 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5119 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5120 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5121 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5122 | Returns: |
| 5123 | Returns main.FALSE if an exception is thrown or an error is present |
| 5124 | in the response. Otherwise, returns main.TRUE. |
| 5125 | NOTE: |
| 5126 | If a host cannot be removed, then this function will return main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5127 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5128 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5129 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 5130 | deviceStr = device |
| 5131 | device = [] |
| 5132 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5133 | |
| 5134 | for d in device: |
| 5135 | time.sleep( 1 ) |
| 5136 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5137 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5138 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5139 | if "Error" in response: |
| 5140 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 5141 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5142 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5143 | except AssertionError: |
| 5144 | main.log.exception( "" ) |
| 5145 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5146 | except TypeError: |
| 5147 | main.log.exception( self.name + ": Object not as expected" ) |
| 5148 | return main.FALSE |
| 5149 | except pexpect.EOF: |
| 5150 | main.log.error( self.name + ": EOF exception found" ) |
| 5151 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5152 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5153 | except Exception: |
| 5154 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5155 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5156 | |
| 5157 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5158 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5159 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5160 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5161 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5162 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5163 | Returns: |
| 5164 | Returns main.FALSE if an exception is thrown or an error is present |
| 5165 | in the response. Otherwise, returns main.TRUE. |
| 5166 | NOTE: |
| 5167 | If a host cannot be removed, then this function will return main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5168 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5169 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5170 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5171 | host = list( host ) |
| 5172 | |
| 5173 | for h in host: |
| 5174 | time.sleep( 1 ) |
| 5175 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5176 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5177 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5178 | if "Error" in response: |
| 5179 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5180 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5181 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5182 | except AssertionError: |
| 5183 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5184 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5185 | except TypeError: |
| 5186 | main.log.exception( self.name + ": Object not as expected" ) |
| 5187 | return main.FALSE |
| 5188 | except pexpect.EOF: |
| 5189 | main.log.error( self.name + ": EOF exception found" ) |
| 5190 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5191 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5192 | except Exception: |
| 5193 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5194 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5195 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5196 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5197 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5198 | Description: |
| 5199 | Bring link down or up in the null-provider. |
| 5200 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5201 | begin - (string) One end of a device or switch. |
| 5202 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5203 | returns: |
| 5204 | main.TRUE if no exceptions were thrown and no Errors are |
| 5205 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5206 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5207 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5208 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5209 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5210 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5211 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5212 | if "Error" in response or "Failure" in response: |
| 5213 | main.log.error( response ) |
| 5214 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5215 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5216 | except AssertionError: |
| 5217 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5218 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5219 | except TypeError: |
| 5220 | main.log.exception( self.name + ": Object not as expected" ) |
| 5221 | return main.FALSE |
| 5222 | except pexpect.EOF: |
| 5223 | main.log.error( self.name + ": EOF exception found" ) |
| 5224 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5225 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5226 | except Exception: |
| 5227 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5228 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5229 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5230 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5231 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5232 | Description: |
| 5233 | Changes the state of port in an OF switch by means of the |
| 5234 | PORTSTATUS OF messages. |
| 5235 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5236 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5237 | port - (string) target port in the device. Ex: '2' |
| 5238 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5239 | returns: |
| 5240 | main.TRUE if no exceptions were thrown and no Errors are |
| 5241 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5242 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5243 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5244 | state = state.lower() |
| 5245 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5246 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5247 | response = self.sendline( cmd, showResponse=True ) |
| 5248 | assert response is not None, "Error in sendline" |
| 5249 | assert "Command not found:" not in response, response |
| 5250 | if "Error" in response or "Failure" in response: |
| 5251 | main.log.error( response ) |
| 5252 | return main.FALSE |
| 5253 | return main.TRUE |
| 5254 | except AssertionError: |
| 5255 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5256 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5257 | except TypeError: |
| 5258 | main.log.exception( self.name + ": Object not as expected" ) |
| 5259 | return main.FALSE |
| 5260 | except pexpect.EOF: |
| 5261 | main.log.error( self.name + ": EOF exception found" ) |
| 5262 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5263 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5264 | except Exception: |
| 5265 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5266 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5267 | |
| 5268 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5269 | """ |
| 5270 | Set the logging level to lvl for a specific app |
| 5271 | returns main.TRUE on success |
| 5272 | returns main.FALSE if Error occurred |
| 5273 | if noExit is True, TestON will not exit, but clean up |
| 5274 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5275 | Level defaults to INFO |
| 5276 | """ |
| 5277 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5278 | self.handle.sendline( "log:set %s %s" % ( level, app ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 5279 | self.handle.expect( self.karafPrompt ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5280 | |
| 5281 | response = self.handle.before |
| 5282 | if re.search( "Error", response ): |
| 5283 | return main.FALSE |
| 5284 | return main.TRUE |
| 5285 | except pexpect.TIMEOUT: |
| 5286 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5287 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5288 | except pexpect.EOF: |
| 5289 | main.log.error( self.name + ": EOF exception found" ) |
| 5290 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5291 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5292 | except Exception: |
| 5293 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5294 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5295 | |
| 5296 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5297 | """ |
| 5298 | Return a dictionary which describes the latest network topology data as a |
| 5299 | graph. |
| 5300 | An example of the dictionary: |
| 5301 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5302 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5303 | Each vertex should at least have an 'edges' attribute which describes the |
| 5304 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5305 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5306 | list of attributes. |
| 5307 | An example of the edges dictionary: |
| 5308 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5309 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5310 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5311 | in topology data. |
| 5312 | """ |
| 5313 | graphDict = {} |
| 5314 | try: |
| 5315 | links = self.links() |
| 5316 | links = json.loads( links ) |
| 5317 | devices = self.devices() |
| 5318 | devices = json.loads( devices ) |
| 5319 | idToDevice = {} |
| 5320 | for device in devices: |
| 5321 | idToDevice[ device[ 'id' ] ] = device |
| 5322 | if includeHost: |
| 5323 | hosts = self.hosts() |
| 5324 | # FIXME: support 'includeHost' argument |
| 5325 | for link in links: |
| 5326 | nodeA = link[ 'src' ][ 'device' ] |
| 5327 | nodeB = link[ 'dst' ][ 'device' ] |
| 5328 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5329 | if nodeA not in graphDict.keys(): |
| 5330 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5331 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5332 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5333 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5334 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5335 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5336 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5337 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5338 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5339 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5340 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5341 | else: |
| 5342 | # Assert nodeB is not connected to any current links of nodeA |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 5343 | # assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 5344 | pass |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5345 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5346 | 'type': link[ 'type' ], |
| 5347 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5348 | return graphDict |
| 5349 | except ( TypeError, ValueError ): |
| 5350 | main.log.exception( self.name + ": Object not as expected" ) |
| 5351 | return None |
| 5352 | except KeyError: |
| 5353 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5354 | return None |
| 5355 | except AssertionError: |
| 5356 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5357 | return None |
| 5358 | except pexpect.EOF: |
| 5359 | main.log.error( self.name + ": EOF exception found" ) |
| 5360 | main.log.error( self.name + ": " + self.handle.before ) |
| 5361 | return None |
| 5362 | except Exception: |
| 5363 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5364 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5365 | |
| 5366 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5367 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5368 | Send command to check intent-perf summary |
| 5369 | Returns: dictionary for intent-perf summary |
| 5370 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5371 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5372 | cmd = "intent-perf -s" |
| 5373 | respDic = {} |
| 5374 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5375 | assert resp is not None, "Error in sendline" |
| 5376 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5377 | try: |
| 5378 | # Generate the dictionary to return |
| 5379 | for l in resp.split( "\n" ): |
| 5380 | # Delete any white space in line |
| 5381 | temp = re.sub( r'\s+', '', l ) |
| 5382 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5383 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5384 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5385 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5386 | main.log.exception( self.name + ": Object not as expected" ) |
| 5387 | return None |
| 5388 | except KeyError: |
| 5389 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5390 | return None |
| 5391 | except AssertionError: |
| 5392 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5393 | return None |
| 5394 | except pexpect.EOF: |
| 5395 | main.log.error( self.name + ": EOF exception found" ) |
| 5396 | main.log.error( self.name + ": " + self.handle.before ) |
| 5397 | return None |
| 5398 | except Exception: |
| 5399 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5400 | return None |
| 5401 | return respDic |
| 5402 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5403 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5404 | """ |
| 5405 | Searches the latest ONOS log file for the given search term and |
| 5406 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5407 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5408 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5409 | searchTerm: |
| 5410 | The string to grep from the ONOS log. |
| 5411 | startLine: |
| 5412 | The term that decides which line is the start to search the searchTerm in |
| 5413 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5414 | logNum: |
| 5415 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5416 | information.Because of this, search mutiply logs is necessary to capture |
| 5417 | the right result. logNum is the number of karaf logs that we need to search |
| 5418 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5419 | mode: |
| 5420 | all: return all the strings that contain the search term |
| 5421 | last: return the last string that contains the search term |
| 5422 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5423 | num: return the number of times that the searchTerm appears in the log |
| 5424 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5425 | """ |
| 5426 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5427 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5428 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5429 | logPath = '/opt/onos/log/karaf.log.' |
| 5430 | logPaths = '/opt/onos/log/karaf.log' |
| 5431 | for i in range( 1, logNum ): |
| 5432 | logPaths = logPath + str( i ) + " " + logPaths |
| 5433 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5434 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5435 | # 100000000 is just a extreme large number to make sure this function can |
| 5436 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5437 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5438 | if mode == 'all': |
| 5439 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5440 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5441 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5442 | elif mode == 'first': |
| 5443 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5444 | elif mode == 'num': |
You Wang | d91a70f | 2019-01-03 15:28:10 -0800 | [diff] [blame] | 5445 | cmd = cmd + " | grep \'" + searchTerm + "\' | wc -l" |
| 5446 | num = self.lineCount( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5447 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5448 | elif mode == 'total': |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 5449 | totalLines = self.lineCount( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5450 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5451 | else: |
| 5452 | main.log.error( self.name + " unsupported mode" ) |
| 5453 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5454 | before = self.sendline( cmd ) |
| 5455 | before = before.splitlines() |
| 5456 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5457 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5458 | return returnLines |
| 5459 | except AssertionError: |
| 5460 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5461 | return None |
| 5462 | except pexpect.EOF: |
| 5463 | main.log.error( self.name + ": EOF exception found" ) |
| 5464 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5465 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5466 | except pexpect.TIMEOUT: |
| 5467 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5468 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5469 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5470 | except Exception: |
| 5471 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5472 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5473 | |
| 5474 | def vplsShow( self, jsonFormat=True ): |
| 5475 | """ |
| 5476 | Description: Returns result of onos:vpls show, which should list the |
| 5477 | configured VPLS networks and the assigned interfaces. |
| 5478 | Optional: |
| 5479 | * jsonFormat: enable json formatting of output |
| 5480 | Returns: |
| 5481 | The output of the command or None on error. |
| 5482 | """ |
| 5483 | try: |
| 5484 | cmdStr = "vpls show" |
| 5485 | if jsonFormat: |
| 5486 | raise NotImplementedError |
| 5487 | cmdStr += " -j" |
| 5488 | handle = self.sendline( cmdStr ) |
| 5489 | assert handle is not None, "Error in sendline" |
| 5490 | assert "Command not found:" not in handle, handle |
| 5491 | return handle |
| 5492 | except AssertionError: |
| 5493 | main.log.exception( "" ) |
| 5494 | return None |
| 5495 | except TypeError: |
| 5496 | main.log.exception( self.name + ": Object not as expected" ) |
| 5497 | return None |
| 5498 | except pexpect.EOF: |
| 5499 | main.log.error( self.name + ": EOF exception found" ) |
| 5500 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5501 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5502 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5503 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5504 | return None |
| 5505 | except Exception: |
| 5506 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5507 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5508 | |
| 5509 | def parseVplsShow( self ): |
| 5510 | """ |
| 5511 | Parse the cli output of 'vpls show' into json output. This is required |
| 5512 | as there is currently no json output available. |
| 5513 | """ |
| 5514 | try: |
| 5515 | output = [] |
| 5516 | raw = self.vplsShow( jsonFormat=False ) |
| 5517 | namePat = "VPLS name: (?P<name>\w+)" |
| 5518 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5519 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5520 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5521 | mIter = re.finditer( pattern, raw ) |
| 5522 | for match in mIter: |
| 5523 | item = {} |
| 5524 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5525 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5526 | if ifaces == [ "" ]: |
| 5527 | ifaces = [] |
| 5528 | item[ 'interfaces' ] = ifaces |
| 5529 | encap = match.group( 'encap' ) |
| 5530 | if encap != 'NONE': |
| 5531 | item[ 'encapsulation' ] = encap.lower() |
| 5532 | output.append( item ) |
| 5533 | return output |
| 5534 | except Exception: |
| 5535 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5536 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5537 | |
| 5538 | def vplsList( self, jsonFormat=True ): |
| 5539 | """ |
| 5540 | Description: Returns result of onos:vpls list, which should list the |
| 5541 | configured VPLS networks. |
| 5542 | Optional: |
| 5543 | * jsonFormat: enable json formatting of output |
| 5544 | """ |
| 5545 | try: |
| 5546 | cmdStr = "vpls list" |
| 5547 | if jsonFormat: |
| 5548 | raise NotImplementedError |
| 5549 | cmdStr += " -j" |
| 5550 | handle = self.sendline( cmdStr ) |
| 5551 | assert handle is not None, "Error in sendline" |
| 5552 | assert "Command not found:" not in handle, handle |
| 5553 | return handle |
| 5554 | except AssertionError: |
| 5555 | main.log.exception( "" ) |
| 5556 | return None |
| 5557 | except TypeError: |
| 5558 | main.log.exception( self.name + ": Object not as expected" ) |
| 5559 | return None |
| 5560 | except pexpect.EOF: |
| 5561 | main.log.error( self.name + ": EOF exception found" ) |
| 5562 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5563 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5564 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5565 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5566 | return None |
| 5567 | except Exception: |
| 5568 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5569 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5570 | |
| 5571 | def vplsCreate( self, network ): |
| 5572 | """ |
| 5573 | CLI command to create a new VPLS network. |
| 5574 | Required arguments: |
| 5575 | network - String name of the network to create. |
| 5576 | returns: |
| 5577 | main.TRUE on success and main.FALSE on failure |
| 5578 | """ |
| 5579 | try: |
| 5580 | network = str( network ) |
| 5581 | cmdStr = "vpls create " |
| 5582 | cmdStr += network |
| 5583 | output = self.sendline( cmdStr ) |
| 5584 | assert output is not None, "Error in sendline" |
| 5585 | assert "Command not found:" not in output, output |
| 5586 | assert "Error executing command" not in output, output |
| 5587 | assert "VPLS already exists:" not in output, output |
| 5588 | return main.TRUE |
| 5589 | except AssertionError: |
| 5590 | main.log.exception( "" ) |
| 5591 | return main.FALSE |
| 5592 | except TypeError: |
| 5593 | main.log.exception( self.name + ": Object not as expected" ) |
| 5594 | return main.FALSE |
| 5595 | except pexpect.EOF: |
| 5596 | main.log.error( self.name + ": EOF exception found" ) |
| 5597 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5598 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5599 | except Exception: |
| 5600 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5601 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5602 | |
| 5603 | def vplsDelete( self, network ): |
| 5604 | """ |
| 5605 | CLI command to delete a VPLS network. |
| 5606 | Required arguments: |
| 5607 | network - Name of the network to delete. |
| 5608 | returns: |
| 5609 | main.TRUE on success and main.FALSE on failure |
| 5610 | """ |
| 5611 | try: |
| 5612 | network = str( network ) |
| 5613 | cmdStr = "vpls delete " |
| 5614 | cmdStr += network |
| 5615 | output = self.sendline( cmdStr ) |
| 5616 | assert output is not None, "Error in sendline" |
| 5617 | assert "Command not found:" not in output, output |
| 5618 | assert "Error executing command" not in output, output |
| 5619 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5620 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5621 | return main.TRUE |
| 5622 | except AssertionError: |
| 5623 | main.log.exception( "" ) |
| 5624 | return main.FALSE |
| 5625 | except TypeError: |
| 5626 | main.log.exception( self.name + ": Object not as expected" ) |
| 5627 | return main.FALSE |
| 5628 | except pexpect.EOF: |
| 5629 | main.log.error( self.name + ": EOF exception found" ) |
| 5630 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5631 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5632 | except Exception: |
| 5633 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5634 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5635 | |
| 5636 | def vplsAddIface( self, network, iface ): |
| 5637 | """ |
| 5638 | CLI command to add an interface to a VPLS network. |
| 5639 | Required arguments: |
| 5640 | network - Name of the network to add the interface to. |
| 5641 | iface - The ONOS name for an interface. |
| 5642 | returns: |
| 5643 | main.TRUE on success and main.FALSE on failure |
| 5644 | """ |
| 5645 | try: |
| 5646 | network = str( network ) |
| 5647 | iface = str( iface ) |
| 5648 | cmdStr = "vpls add-if " |
| 5649 | cmdStr += network + " " + iface |
| 5650 | output = self.sendline( cmdStr ) |
| 5651 | assert output is not None, "Error in sendline" |
| 5652 | assert "Command not found:" not in output, output |
| 5653 | assert "Error executing command" not in output, output |
| 5654 | assert "already associated to network" not in output, output |
| 5655 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5656 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5657 | return main.TRUE |
| 5658 | except AssertionError: |
| 5659 | main.log.exception( "" ) |
| 5660 | return main.FALSE |
| 5661 | except TypeError: |
| 5662 | main.log.exception( self.name + ": Object not as expected" ) |
| 5663 | return main.FALSE |
| 5664 | except pexpect.EOF: |
| 5665 | main.log.error( self.name + ": EOF exception found" ) |
| 5666 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5667 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5668 | except Exception: |
| 5669 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5670 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5671 | |
| 5672 | def vplsRemIface( self, network, iface ): |
| 5673 | """ |
| 5674 | CLI command to remove an interface from a VPLS network. |
| 5675 | Required arguments: |
| 5676 | network - Name of the network to remove the interface from. |
| 5677 | iface - Name of the interface to remove. |
| 5678 | returns: |
| 5679 | main.TRUE on success and main.FALSE on failure |
| 5680 | """ |
| 5681 | try: |
| 5682 | iface = str( iface ) |
| 5683 | cmdStr = "vpls rem-if " |
| 5684 | cmdStr += network + " " + iface |
| 5685 | output = self.sendline( cmdStr ) |
| 5686 | assert output is not None, "Error in sendline" |
| 5687 | assert "Command not found:" not in output, output |
| 5688 | assert "Error executing command" not in output, output |
| 5689 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5690 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5691 | return main.TRUE |
| 5692 | except AssertionError: |
| 5693 | main.log.exception( "" ) |
| 5694 | return main.FALSE |
| 5695 | except TypeError: |
| 5696 | main.log.exception( self.name + ": Object not as expected" ) |
| 5697 | return main.FALSE |
| 5698 | except pexpect.EOF: |
| 5699 | main.log.error( self.name + ": EOF exception found" ) |
| 5700 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5701 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5702 | except Exception: |
| 5703 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5704 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5705 | |
| 5706 | def vplsClean( self ): |
| 5707 | """ |
| 5708 | Description: Clears the VPLS app configuration. |
| 5709 | Returns: main.TRUE on success and main.FALSE on failure |
| 5710 | """ |
| 5711 | try: |
| 5712 | cmdStr = "vpls clean" |
| 5713 | handle = self.sendline( cmdStr ) |
| 5714 | assert handle is not None, "Error in sendline" |
| 5715 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5716 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5717 | return handle |
| 5718 | except AssertionError: |
| 5719 | main.log.exception( "" ) |
| 5720 | return main.FALSE |
| 5721 | except TypeError: |
| 5722 | main.log.exception( self.name + ": Object not as expected" ) |
| 5723 | return main.FALSE |
| 5724 | except pexpect.EOF: |
| 5725 | main.log.error( self.name + ": EOF exception found" ) |
| 5726 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5727 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5728 | except Exception: |
| 5729 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5730 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5731 | |
| 5732 | def vplsSetEncap( self, network, encapType ): |
| 5733 | """ |
| 5734 | CLI command to add an interface to a VPLS network. |
| 5735 | Required arguments: |
| 5736 | network - Name of the network to create. |
| 5737 | encapType - Type of encapsulation. |
| 5738 | returns: |
| 5739 | main.TRUE on success and main.FALSE on failure |
| 5740 | """ |
| 5741 | try: |
| 5742 | network = str( network ) |
| 5743 | encapType = str( encapType ).upper() |
| 5744 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5745 | cmdStr = "vpls set-encap " |
| 5746 | cmdStr += network + " " + encapType |
| 5747 | output = self.sendline( cmdStr ) |
| 5748 | assert output is not None, "Error in sendline" |
| 5749 | assert "Command not found:" not in output, output |
| 5750 | assert "Error executing command" not in output, output |
| 5751 | assert "already associated to network" not in output, output |
| 5752 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5753 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5754 | return main.TRUE |
| 5755 | except AssertionError: |
| 5756 | main.log.exception( "" ) |
| 5757 | return main.FALSE |
| 5758 | except TypeError: |
| 5759 | main.log.exception( self.name + ": Object not as expected" ) |
| 5760 | return main.FALSE |
| 5761 | except pexpect.EOF: |
| 5762 | main.log.error( self.name + ": EOF exception found" ) |
| 5763 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5764 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5765 | except Exception: |
| 5766 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5767 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5768 | |
| 5769 | def interfaces( self, jsonFormat=True ): |
| 5770 | """ |
| 5771 | Description: Returns result of interfaces command. |
| 5772 | Optional: |
| 5773 | * jsonFormat: enable json formatting of output |
| 5774 | Returns: |
| 5775 | The output of the command or None on error. |
| 5776 | """ |
| 5777 | try: |
| 5778 | cmdStr = "interfaces" |
| 5779 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5780 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5781 | cmdStr += " -j" |
| 5782 | handle = self.sendline( cmdStr ) |
| 5783 | assert handle is not None, "Error in sendline" |
| 5784 | assert "Command not found:" not in handle, handle |
| 5785 | return handle |
| 5786 | except AssertionError: |
| 5787 | main.log.exception( "" ) |
| 5788 | return None |
| 5789 | except TypeError: |
| 5790 | main.log.exception( self.name + ": Object not as expected" ) |
| 5791 | return None |
| 5792 | except pexpect.EOF: |
| 5793 | main.log.error( self.name + ": EOF exception found" ) |
| 5794 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5795 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5796 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5797 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5798 | return None |
| 5799 | except Exception: |
| 5800 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5801 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5802 | |
| 5803 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5804 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5805 | Get the timestamp of searchTerm from karaf log. |
| 5806 | |
| 5807 | Arguments: |
| 5808 | splitTerm_before and splitTerm_after: |
| 5809 | |
| 5810 | The terms that split the string that contains the timeStamp of |
| 5811 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5812 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5813 | and the splitTerm_after is "x" |
| 5814 | |
| 5815 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5816 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5817 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5818 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5819 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5820 | return main.ERROR |
| 5821 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5822 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5823 | main.log.warn( "Captured timestamp string is empty" ) |
| 5824 | return main.ERROR |
| 5825 | lines = lines[ 0 ] |
| 5826 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5827 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5828 | # get the target value |
| 5829 | line = lines.split( splitTerm_before ) |
| 5830 | key = line[ 1 ].split( splitTerm_after ) |
| 5831 | return int( key[ 0 ] ) |
| 5832 | except IndexError: |
| 5833 | main.log.warn( "Index Error!" ) |
| 5834 | return main.ERROR |
| 5835 | except AssertionError: |
| 5836 | main.log.warn( "Search Term Not Found " ) |
| 5837 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5838 | |
| 5839 | def workQueueAdd( self, queueName, value ): |
| 5840 | """ |
| 5841 | CLI command to add a string to the specified Work Queue. |
| 5842 | This function uses the distributed primitives test app, which |
| 5843 | gives some cli access to distributed primitives for testing |
| 5844 | purposes only. |
| 5845 | |
| 5846 | Required arguments: |
| 5847 | queueName - The name of the queue to add to |
| 5848 | value - The value to add to the queue |
| 5849 | returns: |
| 5850 | main.TRUE on success, main.FALSE on failure and |
| 5851 | main.ERROR on error. |
| 5852 | """ |
| 5853 | try: |
| 5854 | queueName = str( queueName ) |
| 5855 | value = str( value ) |
| 5856 | prefix = "work-queue-test" |
| 5857 | operation = "add" |
| 5858 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5859 | output = self.distPrimitivesSend( cmdStr ) |
| 5860 | if "Invalid operation name" in output: |
| 5861 | main.log.warn( output ) |
| 5862 | return main.ERROR |
| 5863 | elif "Done" in output: |
| 5864 | return main.TRUE |
| 5865 | except TypeError: |
| 5866 | main.log.exception( self.name + ": Object not as expected" ) |
| 5867 | return main.ERROR |
| 5868 | except Exception: |
| 5869 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5870 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5871 | |
| 5872 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5873 | """ |
| 5874 | CLI command to add two strings to the specified Work Queue. |
| 5875 | This function uses the distributed primitives test app, which |
| 5876 | gives some cli access to distributed primitives for testing |
| 5877 | purposes only. |
| 5878 | |
| 5879 | Required arguments: |
| 5880 | queueName - The name of the queue to add to |
| 5881 | value1 - The first value to add to the queue |
| 5882 | value2 - The second value to add to the queue |
| 5883 | returns: |
| 5884 | main.TRUE on success, main.FALSE on failure and |
| 5885 | main.ERROR on error. |
| 5886 | """ |
| 5887 | try: |
| 5888 | queueName = str( queueName ) |
| 5889 | value1 = str( value1 ) |
| 5890 | value2 = str( value2 ) |
| 5891 | prefix = "work-queue-test" |
| 5892 | operation = "addMultiple" |
| 5893 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 5894 | output = self.distPrimitivesSend( cmdStr ) |
| 5895 | if "Invalid operation name" in output: |
| 5896 | main.log.warn( output ) |
| 5897 | return main.ERROR |
| 5898 | elif "Done" in output: |
| 5899 | return main.TRUE |
| 5900 | except TypeError: |
| 5901 | main.log.exception( self.name + ": Object not as expected" ) |
| 5902 | return main.ERROR |
| 5903 | except Exception: |
| 5904 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5905 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5906 | |
| 5907 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 5908 | """ |
| 5909 | CLI command to take a value from the specified Work Queue and compelte it. |
| 5910 | This function uses the distributed primitives test app, which |
| 5911 | gives some cli access to distributed primitives for testing |
| 5912 | purposes only. |
| 5913 | |
| 5914 | Required arguments: |
| 5915 | queueName - The name of the queue to add to |
| 5916 | number - The number of items to take and complete |
| 5917 | returns: |
| 5918 | main.TRUE on success, main.FALSE on failure and |
| 5919 | main.ERROR on error. |
| 5920 | """ |
| 5921 | try: |
| 5922 | queueName = str( queueName ) |
| 5923 | number = str( int( number ) ) |
| 5924 | prefix = "work-queue-test" |
| 5925 | operation = "takeAndComplete" |
| 5926 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 5927 | output = self.distPrimitivesSend( cmdStr ) |
| 5928 | if "Invalid operation name" in output: |
| 5929 | main.log.warn( output ) |
| 5930 | return main.ERROR |
| 5931 | elif "Done" in output: |
| 5932 | return main.TRUE |
| 5933 | except TypeError: |
| 5934 | main.log.exception( self.name + ": Object not as expected" ) |
| 5935 | return main.ERROR |
| 5936 | except Exception: |
| 5937 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5938 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5939 | |
| 5940 | def workQueueDestroy( self, queueName ): |
| 5941 | """ |
| 5942 | CLI command to destroy the specified Work Queue. |
| 5943 | This function uses the distributed primitives test app, which |
| 5944 | gives some cli access to distributed primitives for testing |
| 5945 | purposes only. |
| 5946 | |
| 5947 | Required arguments: |
| 5948 | queueName - The name of the queue to add to |
| 5949 | returns: |
| 5950 | main.TRUE on success, main.FALSE on failure and |
| 5951 | main.ERROR on error. |
| 5952 | """ |
| 5953 | try: |
| 5954 | queueName = str( queueName ) |
| 5955 | prefix = "work-queue-test" |
| 5956 | operation = "destroy" |
| 5957 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5958 | output = self.distPrimitivesSend( cmdStr ) |
| 5959 | if "Invalid operation name" in output: |
| 5960 | main.log.warn( output ) |
| 5961 | return main.ERROR |
| 5962 | return main.TRUE |
| 5963 | except TypeError: |
| 5964 | main.log.exception( self.name + ": Object not as expected" ) |
| 5965 | return main.ERROR |
| 5966 | except Exception: |
| 5967 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5968 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5969 | |
| 5970 | def workQueueTotalPending( self, queueName ): |
| 5971 | """ |
| 5972 | CLI command to get the Total Pending items of the specified Work Queue. |
| 5973 | This function uses the distributed primitives test app, which |
| 5974 | gives some cli access to distributed primitives for testing |
| 5975 | purposes only. |
| 5976 | |
| 5977 | Required arguments: |
| 5978 | queueName - The name of the queue to add to |
| 5979 | returns: |
| 5980 | The number of Pending items in the specified work queue or |
| 5981 | None on error |
| 5982 | """ |
| 5983 | try: |
| 5984 | queueName = str( queueName ) |
| 5985 | prefix = "work-queue-test" |
| 5986 | operation = "totalPending" |
| 5987 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5988 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 5989 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 5990 | output = output.split( "\r\n" )[ -1 ] |
| 5991 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5992 | pattern = r'\d+' |
| 5993 | if "Invalid operation name" in output: |
| 5994 | main.log.warn( output ) |
| 5995 | return None |
| 5996 | else: |
| 5997 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5998 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5999 | except ( AttributeError, TypeError ): |
| 6000 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6001 | return None |
| 6002 | except Exception: |
| 6003 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6004 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6005 | |
| 6006 | def workQueueTotalCompleted( self, queueName ): |
| 6007 | """ |
| 6008 | CLI command to get the Total Completed items of the specified Work Queue. |
| 6009 | This function uses the distributed primitives test app, which |
| 6010 | gives some cli access to distributed primitives for testing |
| 6011 | purposes only. |
| 6012 | |
| 6013 | Required arguments: |
| 6014 | queueName - The name of the queue to add to |
| 6015 | returns: |
| 6016 | The number of complete items in the specified work queue or |
| 6017 | None on error |
| 6018 | """ |
| 6019 | try: |
| 6020 | queueName = str( queueName ) |
| 6021 | prefix = "work-queue-test" |
| 6022 | operation = "totalCompleted" |
| 6023 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6024 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6025 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6026 | output = output.split( "\r\n" )[ -1 ] |
| 6027 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6028 | pattern = r'\d+' |
| 6029 | if "Invalid operation name" in output: |
| 6030 | main.log.warn( output ) |
| 6031 | return None |
| 6032 | else: |
| 6033 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6034 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6035 | except ( AttributeError, TypeError ): |
| 6036 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6037 | return None |
| 6038 | except Exception: |
| 6039 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6040 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6041 | |
| 6042 | def workQueueTotalInProgress( self, queueName ): |
| 6043 | """ |
| 6044 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 6045 | This function uses the distributed primitives test app, which |
| 6046 | gives some cli access to distributed primitives for testing |
| 6047 | purposes only. |
| 6048 | |
| 6049 | Required arguments: |
| 6050 | queueName - The name of the queue to add to |
| 6051 | returns: |
| 6052 | The number of In Progress items in the specified work queue or |
| 6053 | None on error |
| 6054 | """ |
| 6055 | try: |
| 6056 | queueName = str( queueName ) |
| 6057 | prefix = "work-queue-test" |
| 6058 | operation = "totalInProgress" |
| 6059 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6060 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6061 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6062 | output = output.split( "\r\n" )[ -1 ] |
| 6063 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6064 | pattern = r'\d+' |
| 6065 | if "Invalid operation name" in output: |
| 6066 | main.log.warn( output ) |
| 6067 | return None |
| 6068 | else: |
| 6069 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6070 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6071 | except ( AttributeError, TypeError ): |
| 6072 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6073 | return None |
| 6074 | except Exception: |
| 6075 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6076 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 6077 | |
| 6078 | def events( self, args='-a' ): |
| 6079 | """ |
| 6080 | Description: Returns events -a command output |
| 6081 | Optional: |
| 6082 | add other arguments |
| 6083 | """ |
| 6084 | try: |
| 6085 | cmdStr = "events" |
| 6086 | if args: |
| 6087 | cmdStr += " " + args |
| 6088 | handle = self.sendline( cmdStr ) |
| 6089 | assert handle is not None, "Error in sendline" |
| 6090 | assert "Command not found:" not in handle, handle |
| 6091 | return handle |
| 6092 | except AssertionError: |
| 6093 | main.log.exception( "" ) |
| 6094 | return None |
| 6095 | except TypeError: |
| 6096 | main.log.exception( self.name + ": Object not as expected" ) |
| 6097 | return None |
| 6098 | except pexpect.EOF: |
| 6099 | main.log.error( self.name + ": EOF exception found" ) |
| 6100 | main.log.error( self.name + ": " + self.handle.before ) |
| 6101 | main.cleanAndExit() |
| 6102 | except Exception: |
| 6103 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6104 | main.cleanAndExit() |
| 6105 | |
| 6106 | def getMaster( self, deviceID ): |
| 6107 | """ |
| 6108 | Description: Obtains current master using "roles" command for a specific deviceID |
| 6109 | """ |
| 6110 | try: |
| 6111 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 6112 | except AssertionError: |
| 6113 | main.log.exception( "" ) |
| 6114 | return None |
| 6115 | except TypeError: |
| 6116 | main.log.exception( self.name + ": Object not as expected" ) |
| 6117 | return None |
| 6118 | except pexpect.EOF: |
| 6119 | main.log.error( self.name + ": EOF exception found" ) |
| 6120 | main.log.error( self.name + ": " + self.handle.before ) |
| 6121 | main.cleanAndExit() |
| 6122 | except Exception: |
| 6123 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 6124 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 6125 | |
| 6126 | def issu( self ): |
| 6127 | """ |
| 6128 | Short summary of In-Service Software Upgrade status |
| 6129 | |
| 6130 | Returns the output of the cli command or None on Error |
| 6131 | """ |
| 6132 | try: |
| 6133 | cmdStr = "issu" |
| 6134 | handle = self.sendline( cmdStr ) |
| 6135 | assert handle is not None, "Error in sendline" |
| 6136 | assert "Command not found:" not in handle, handle |
| 6137 | assert "Unsupported command:" not in handle, handle |
| 6138 | return handle |
| 6139 | except AssertionError: |
| 6140 | main.log.exception( "" ) |
| 6141 | return None |
| 6142 | except TypeError: |
| 6143 | main.log.exception( self.name + ": Object not as expected" ) |
| 6144 | return None |
| 6145 | except pexpect.EOF: |
| 6146 | main.log.error( self.name + ": EOF exception found" ) |
| 6147 | main.log.error( self.name + ": " + self.handle.before ) |
| 6148 | main.cleanAndExit() |
| 6149 | except Exception: |
| 6150 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6151 | main.cleanAndExit() |
| 6152 | |
| 6153 | def issuInit( self ): |
| 6154 | """ |
| 6155 | Initiates an In-Service Software Upgrade |
| 6156 | |
| 6157 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6158 | """ |
| 6159 | try: |
| 6160 | cmdStr = "issu init" |
| 6161 | handle = self.sendline( cmdStr ) |
| 6162 | assert handle is not None, "Error in sendline" |
| 6163 | assert "Command not found:" not in handle, handle |
| 6164 | assert "Unsupported command:" not in handle, handle |
| 6165 | if "Initialized" in handle: |
| 6166 | return main.TRUE |
| 6167 | else: |
| 6168 | return main.FALSE |
| 6169 | except AssertionError: |
| 6170 | main.log.exception( "" ) |
| 6171 | return main.ERROR |
| 6172 | except TypeError: |
| 6173 | main.log.exception( self.name + ": Object not as expected" ) |
| 6174 | return main.ERROR |
| 6175 | except pexpect.EOF: |
| 6176 | main.log.error( self.name + ": EOF exception found" ) |
| 6177 | main.log.error( self.name + ": " + self.handle.before ) |
| 6178 | main.cleanAndExit() |
| 6179 | except Exception: |
| 6180 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6181 | main.cleanAndExit() |
| 6182 | |
| 6183 | def issuUpgrade( self ): |
| 6184 | """ |
| 6185 | Transitions stores to upgraded nodes |
| 6186 | |
| 6187 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6188 | """ |
| 6189 | try: |
| 6190 | cmdStr = "issu upgrade" |
| 6191 | handle = self.sendline( cmdStr ) |
| 6192 | assert handle is not None, "Error in sendline" |
| 6193 | assert "Command not found:" not in handle, handle |
| 6194 | assert "Unsupported command:" not in handle, handle |
| 6195 | if "Upgraded" in handle: |
| 6196 | return main.TRUE |
| 6197 | else: |
| 6198 | return main.FALSE |
| 6199 | except AssertionError: |
| 6200 | main.log.exception( "" ) |
| 6201 | return main.ERROR |
| 6202 | except TypeError: |
| 6203 | main.log.exception( self.name + ": Object not as expected" ) |
| 6204 | return main.ERROR |
| 6205 | except pexpect.EOF: |
| 6206 | main.log.error( self.name + ": EOF exception found" ) |
| 6207 | main.log.error( self.name + ": " + self.handle.before ) |
| 6208 | main.cleanAndExit() |
| 6209 | except Exception: |
| 6210 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6211 | main.cleanAndExit() |
| 6212 | |
| 6213 | def issuCommit( self ): |
| 6214 | """ |
| 6215 | Finalizes an In-Service Software Upgrade |
| 6216 | |
| 6217 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6218 | """ |
| 6219 | try: |
| 6220 | cmdStr = "issu commit" |
| 6221 | handle = self.sendline( cmdStr ) |
| 6222 | assert handle is not None, "Error in sendline" |
| 6223 | assert "Command not found:" not in handle, handle |
| 6224 | assert "Unsupported command:" not in handle, handle |
| 6225 | # TODO: Check the version returned by this command |
| 6226 | if "Committed version" in handle: |
| 6227 | return main.TRUE |
| 6228 | else: |
| 6229 | return main.FALSE |
| 6230 | except AssertionError: |
| 6231 | main.log.exception( "" ) |
| 6232 | return main.ERROR |
| 6233 | except TypeError: |
| 6234 | main.log.exception( self.name + ": Object not as expected" ) |
| 6235 | return main.ERROR |
| 6236 | except pexpect.EOF: |
| 6237 | main.log.error( self.name + ": EOF exception found" ) |
| 6238 | main.log.error( self.name + ": " + self.handle.before ) |
| 6239 | main.cleanAndExit() |
| 6240 | except Exception: |
| 6241 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6242 | main.cleanAndExit() |
| 6243 | |
| 6244 | def issuRollback( self ): |
| 6245 | """ |
| 6246 | Rolls back an In-Service Software Upgrade |
| 6247 | |
| 6248 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6249 | """ |
| 6250 | try: |
| 6251 | cmdStr = "issu rollback" |
| 6252 | handle = self.sendline( cmdStr ) |
| 6253 | assert handle is not None, "Error in sendline" |
| 6254 | assert "Command not found:" not in handle, handle |
| 6255 | assert "Unsupported command:" not in handle, handle |
| 6256 | # TODO: Check the version returned by this command |
| 6257 | if "Rolled back to version" in handle: |
| 6258 | return main.TRUE |
| 6259 | else: |
| 6260 | return main.FALSE |
| 6261 | except AssertionError: |
| 6262 | main.log.exception( "" ) |
| 6263 | return main.ERROR |
| 6264 | except TypeError: |
| 6265 | main.log.exception( self.name + ": Object not as expected" ) |
| 6266 | return main.ERROR |
| 6267 | except pexpect.EOF: |
| 6268 | main.log.error( self.name + ": EOF exception found" ) |
| 6269 | main.log.error( self.name + ": " + self.handle.before ) |
| 6270 | main.cleanAndExit() |
| 6271 | except Exception: |
| 6272 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6273 | main.cleanAndExit() |
| 6274 | |
| 6275 | def issuReset( self ): |
| 6276 | """ |
| 6277 | Resets the In-Service Software Upgrade status after a rollback |
| 6278 | |
| 6279 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6280 | """ |
| 6281 | try: |
| 6282 | cmdStr = "issu reset" |
| 6283 | handle = self.sendline( cmdStr ) |
| 6284 | assert handle is not None, "Error in sendline" |
| 6285 | assert "Command not found:" not in handle, handle |
| 6286 | assert "Unsupported command:" not in handle, handle |
| 6287 | # TODO: Check the version returned by this command |
| 6288 | if "Reset version" in handle: |
| 6289 | return main.TRUE |
| 6290 | else: |
| 6291 | return main.FALSE |
| 6292 | except AssertionError: |
| 6293 | main.log.exception( "" ) |
| 6294 | return main.ERROR |
| 6295 | except TypeError: |
| 6296 | main.log.exception( self.name + ": Object not as expected" ) |
| 6297 | return main.ERROR |
| 6298 | except pexpect.EOF: |
| 6299 | main.log.error( self.name + ": EOF exception found" ) |
| 6300 | main.log.error( self.name + ": " + self.handle.before ) |
| 6301 | main.cleanAndExit() |
| 6302 | except Exception: |
| 6303 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6304 | main.cleanAndExit() |
| 6305 | |
| 6306 | def issuStatus( self ): |
| 6307 | """ |
| 6308 | Status of an In-Service Software Upgrade |
| 6309 | |
| 6310 | Returns the output of the cli command or None on Error |
| 6311 | """ |
| 6312 | try: |
| 6313 | cmdStr = "issu status" |
| 6314 | handle = self.sendline( cmdStr ) |
| 6315 | assert handle is not None, "Error in sendline" |
| 6316 | assert "Command not found:" not in handle, handle |
| 6317 | assert "Unsupported command:" not in handle, handle |
| 6318 | return handle |
| 6319 | except AssertionError: |
| 6320 | main.log.exception( "" ) |
| 6321 | return None |
| 6322 | except TypeError: |
| 6323 | main.log.exception( self.name + ": Object not as expected" ) |
| 6324 | return None |
| 6325 | except pexpect.EOF: |
| 6326 | main.log.error( self.name + ": EOF exception found" ) |
| 6327 | main.log.error( self.name + ": " + self.handle.before ) |
| 6328 | main.cleanAndExit() |
| 6329 | except Exception: |
| 6330 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6331 | main.cleanAndExit() |
| 6332 | |
| 6333 | def issuVersion( self ): |
| 6334 | """ |
| 6335 | Get the version of an In-Service Software Upgrade |
| 6336 | |
| 6337 | Returns the output of the cli command or None on Error |
| 6338 | """ |
| 6339 | try: |
| 6340 | cmdStr = "issu version" |
| 6341 | handle = self.sendline( cmdStr ) |
| 6342 | assert handle is not None, "Error in sendline" |
| 6343 | assert "Command not found:" not in handle, handle |
| 6344 | assert "Unsupported command:" not in handle, handle |
| 6345 | return handle |
| 6346 | except AssertionError: |
| 6347 | main.log.exception( "" ) |
| 6348 | return None |
| 6349 | except TypeError: |
| 6350 | main.log.exception( self.name + ": Object not as expected" ) |
| 6351 | return None |
| 6352 | except pexpect.EOF: |
| 6353 | main.log.error( self.name + ": EOF exception found" ) |
| 6354 | main.log.error( self.name + ": " + self.handle.before ) |
| 6355 | main.cleanAndExit() |
| 6356 | except Exception: |
| 6357 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6358 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6359 | |
| 6360 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6361 | """ |
| 6362 | Create a multicast route by calling 'mcast-join' command |
| 6363 | sIP: source IP of the multicast route |
| 6364 | groupIP: group IP of the multicast route |
| 6365 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6366 | dPorts: a list of destination ports of the multicast route |
| 6367 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6368 | """ |
| 6369 | try: |
| 6370 | cmdStr = "mcast-join" |
| 6371 | cmdStr += " " + str( sIP ) |
| 6372 | cmdStr += " " + str( groupIP ) |
| 6373 | cmdStr += " " + str( sPort ) |
| 6374 | assert isinstance( dPorts, list ) |
| 6375 | for dPort in dPorts: |
| 6376 | cmdStr += " " + str( dPort ) |
| 6377 | handle = self.sendline( cmdStr ) |
| 6378 | assert handle is not None, "Error in sendline" |
| 6379 | assert "Command not found:" not in handle, handle |
| 6380 | assert "Unsupported command:" not in handle, handle |
| 6381 | assert "Error executing command" not in handle, handle |
| 6382 | if "Added the mcast route" in handle: |
| 6383 | return main.TRUE |
| 6384 | else: |
| 6385 | return main.FALSE |
| 6386 | except AssertionError: |
| 6387 | main.log.exception( "" ) |
| 6388 | return None |
| 6389 | except TypeError: |
| 6390 | main.log.exception( self.name + ": Object not as expected" ) |
| 6391 | return None |
| 6392 | except pexpect.EOF: |
| 6393 | main.log.error( self.name + ": EOF exception found" ) |
| 6394 | main.log.error( self.name + ": " + self.handle.before ) |
| 6395 | main.cleanAndExit() |
| 6396 | except Exception: |
| 6397 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6398 | main.cleanAndExit() |
| 6399 | |
| 6400 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6401 | """ |
| 6402 | Delete a multicast route by calling 'mcast-delete' command |
| 6403 | sIP: source IP of the multicast route |
| 6404 | groupIP: group IP of the multicast route |
| 6405 | dPorts: a list of destination ports of the multicast route |
| 6406 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6407 | """ |
| 6408 | try: |
| 6409 | cmdStr = "mcast-delete" |
| 6410 | cmdStr += " " + str( sIP ) |
| 6411 | cmdStr += " " + str( groupIP ) |
| 6412 | assert isinstance( dPorts, list ) |
| 6413 | for dPort in dPorts: |
| 6414 | cmdStr += " " + str( dPort ) |
| 6415 | handle = self.sendline( cmdStr ) |
| 6416 | assert handle is not None, "Error in sendline" |
| 6417 | assert "Command not found:" not in handle, handle |
| 6418 | assert "Unsupported command:" not in handle, handle |
| 6419 | assert "Error executing command" not in handle, handle |
| 6420 | if "Updated the mcast route" in handle: |
| 6421 | return main.TRUE |
| 6422 | else: |
| 6423 | return main.FALSE |
| 6424 | except AssertionError: |
| 6425 | main.log.exception( "" ) |
| 6426 | return None |
| 6427 | except TypeError: |
| 6428 | main.log.exception( self.name + ": Object not as expected" ) |
| 6429 | return None |
| 6430 | except pexpect.EOF: |
| 6431 | main.log.error( self.name + ": EOF exception found" ) |
| 6432 | main.log.error( self.name + ": " + self.handle.before ) |
| 6433 | main.cleanAndExit() |
| 6434 | except Exception: |
| 6435 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6436 | main.cleanAndExit() |
| 6437 | |
| 6438 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6439 | """ |
| 6440 | Create a multicast route by calling 'mcast-host-join' command |
| 6441 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6442 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6443 | srcs: a list of HostId of the sources e.g. ["00:AA:00:00:00:01/None"] |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6444 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6445 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6446 | """ |
| 6447 | try: |
| 6448 | cmdStr = "mcast-host-join" |
| 6449 | cmdStr += " -sAddr " + str( sAddr ) |
| 6450 | cmdStr += " -gAddr " + str( gAddr ) |
| 6451 | assert isinstance( srcs, list ) |
| 6452 | for src in srcs: |
| 6453 | cmdStr += " -srcs " + str( src ) |
| 6454 | assert isinstance( sinks, list ) |
| 6455 | for sink in sinks: |
| 6456 | cmdStr += " -sinks " + str( sink ) |
| 6457 | handle = self.sendline( cmdStr ) |
| 6458 | assert handle is not None, "Error in sendline" |
| 6459 | assert "Command not found:" not in handle, handle |
| 6460 | assert "Unsupported command:" not in handle, handle |
| 6461 | assert "Error executing command" not in handle, handle |
| 6462 | if "Added the mcast route" in handle: |
| 6463 | return main.TRUE |
| 6464 | else: |
| 6465 | return main.FALSE |
| 6466 | except AssertionError: |
| 6467 | main.log.exception( "" ) |
| 6468 | return None |
| 6469 | except TypeError: |
| 6470 | main.log.exception( self.name + ": Object not as expected" ) |
| 6471 | return None |
| 6472 | except pexpect.EOF: |
| 6473 | main.log.error( self.name + ": EOF exception found" ) |
| 6474 | main.log.error( self.name + ": " + self.handle.before ) |
| 6475 | main.cleanAndExit() |
| 6476 | except Exception: |
| 6477 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6478 | main.cleanAndExit() |
| 6479 | |
| 6480 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6481 | """ |
| 6482 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6483 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6484 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6485 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6486 | will delete the route if not specified |
| 6487 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6488 | """ |
| 6489 | try: |
| 6490 | cmdStr = "mcast-host-delete" |
| 6491 | cmdStr += " -sAddr " + str( sAddr ) |
| 6492 | cmdStr += " -gAddr " + str( gAddr ) |
| 6493 | if host: |
| 6494 | cmdStr += " -h " + str( host ) |
| 6495 | handle = self.sendline( cmdStr ) |
| 6496 | assert handle is not None, "Error in sendline" |
| 6497 | assert "Command not found:" not in handle, handle |
| 6498 | assert "Unsupported command:" not in handle, handle |
| 6499 | assert "Error executing command" not in handle, handle |
| 6500 | if "Updated the mcast route" in handle: |
| 6501 | return main.TRUE |
| 6502 | elif "Deleted the mcast route" in handle: |
| 6503 | return main.TRUE |
| 6504 | else: |
| 6505 | return main.FALSE |
| 6506 | except AssertionError: |
| 6507 | main.log.exception( "" ) |
| 6508 | return None |
| 6509 | except TypeError: |
| 6510 | main.log.exception( self.name + ": Object not as expected" ) |
| 6511 | return None |
| 6512 | except pexpect.EOF: |
| 6513 | main.log.error( self.name + ": EOF exception found" ) |
| 6514 | main.log.error( self.name + ": " + self.handle.before ) |
| 6515 | main.cleanAndExit() |
| 6516 | except Exception: |
| 6517 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6518 | main.cleanAndExit() |
| 6519 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6520 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6521 | """ |
| 6522 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6523 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6524 | gAddr: specifies multicast group address |
| 6525 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6526 | will delete the route if not specified |
| 6527 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6528 | """ |
| 6529 | try: |
| 6530 | cmdStr = "mcast-sink-delete" |
| 6531 | cmdStr += " -sAddr " + str( sAddr ) |
| 6532 | cmdStr += " -gAddr " + str( gAddr ) |
| 6533 | if sink: |
| 6534 | cmdStr += " -s " + str( sink ) |
| 6535 | handle = self.sendline( cmdStr ) |
| 6536 | assert handle is not None, "Error in sendline" |
| 6537 | assert "Command not found:" not in handle, handle |
| 6538 | assert "Unsupported command:" not in handle, handle |
| 6539 | assert "Error executing command" not in handle, handle |
| 6540 | if "Updated the mcast route" in handle: |
| 6541 | return main.TRUE |
| 6542 | elif "Deleted the mcast route" in handle: |
| 6543 | return main.TRUE |
| 6544 | else: |
| 6545 | return main.FALSE |
| 6546 | except AssertionError: |
| 6547 | main.log.exception( "" ) |
| 6548 | return None |
| 6549 | except TypeError: |
| 6550 | main.log.exception( self.name + ": Object not as expected" ) |
| 6551 | return None |
| 6552 | except pexpect.EOF: |
| 6553 | main.log.error( self.name + ": EOF exception found" ) |
| 6554 | main.log.error( self.name + ": " + self.handle.before ) |
| 6555 | main.cleanAndExit() |
| 6556 | except Exception: |
| 6557 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6558 | main.cleanAndExit() |
| 6559 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6560 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6561 | """ |
| 6562 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6563 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6564 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6565 | srcs: a list of host IDs of the sources e.g. ["00:AA:00:00:01:05/40"], |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6566 | will delete the route if not specified |
| 6567 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6568 | """ |
| 6569 | try: |
| 6570 | cmdStr = "mcast-source-delete" |
| 6571 | cmdStr += " -sAddr " + str( sAddr ) |
| 6572 | cmdStr += " -gAddr " + str( gAddr ) |
| 6573 | if srcs: |
| 6574 | assert isinstance( srcs, list ) |
| 6575 | for src in srcs: |
| 6576 | cmdStr += " -src " + str( src ) |
| 6577 | handle = self.sendline( cmdStr ) |
| 6578 | assert handle is not None, "Error in sendline" |
| 6579 | assert "Command not found:" not in handle, handle |
| 6580 | assert "Unsupported command:" not in handle, handle |
| 6581 | assert "Error executing command" not in handle, handle |
| 6582 | if "Updated the mcast route" in handle: |
| 6583 | return main.TRUE |
| 6584 | elif "Deleted the mcast route" in handle: |
| 6585 | return main.TRUE |
| 6586 | else: |
| 6587 | return main.FALSE |
| 6588 | except AssertionError: |
| 6589 | main.log.exception( "" ) |
| 6590 | return None |
| 6591 | except TypeError: |
| 6592 | main.log.exception( self.name + ": Object not as expected" ) |
| 6593 | return None |
| 6594 | except pexpect.EOF: |
| 6595 | main.log.error( self.name + ": EOF exception found" ) |
| 6596 | main.log.error( self.name + ": " + self.handle.before ) |
| 6597 | main.cleanAndExit() |
| 6598 | except Exception: |
| 6599 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6600 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6601 | |
| 6602 | def netcfg( self, jsonFormat=True, args="" ): |
| 6603 | """ |
| 6604 | Run netcfg cli command with given args |
| 6605 | """ |
| 6606 | try: |
| 6607 | cmdStr = "netcfg" |
| 6608 | if jsonFormat: |
| 6609 | cmdStr = cmdStr + " -j" |
| 6610 | if args: |
| 6611 | cmdStr = cmdStr + " " + str( args ) |
| 6612 | handle = self.sendline( cmdStr ) |
| 6613 | assert handle is not None, "Error in sendline" |
| 6614 | assert "Command not found:" not in handle, handle |
| 6615 | assert "Unsupported command:" not in handle, handle |
| 6616 | assert "Error executing command" not in handle, handle |
| 6617 | return handle |
| 6618 | except AssertionError: |
| 6619 | main.log.exception( "" ) |
| 6620 | return None |
| 6621 | except TypeError: |
| 6622 | main.log.exception( self.name + ": Object not as expected" ) |
| 6623 | return None |
| 6624 | except pexpect.EOF: |
| 6625 | main.log.error( self.name + ": EOF exception found" ) |
| 6626 | main.log.error( self.name + ": " + self.handle.before ) |
| 6627 | main.cleanAndExit() |
| 6628 | except Exception: |
| 6629 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6630 | main.cleanAndExit() |
| 6631 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6632 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6633 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6634 | Compose and return a list of t3-troubleshoot cli commands for given source and destination addresses |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6635 | Options: |
| 6636 | sAddr: IP address of the source host |
| 6637 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6638 | ipv6: True if hosts are IPv6 |
| 6639 | verbose: return verbose t3 output if True |
| 6640 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6641 | """ |
| 6642 | try: |
| 6643 | # Collect information of both hosts from onos |
| 6644 | hosts = self.hosts() |
| 6645 | hosts = json.loads( hosts ) |
| 6646 | sHost = None |
| 6647 | dHost = None |
| 6648 | for host in hosts: |
| 6649 | if sAddr in host[ "ipAddresses" ]: |
| 6650 | sHost = host |
| 6651 | elif dAddr in host[ "ipAddresses" ]: |
| 6652 | dHost = host |
| 6653 | if sHost and dHost: |
| 6654 | break |
| 6655 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6656 | cmdList = [] |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6657 | if simple: |
| 6658 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6659 | cmdStr = "t3-troubleshoot-simple" |
| 6660 | if verbose: |
| 6661 | cmdStr += " -vv" |
| 6662 | if ipv6: |
| 6663 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6664 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6665 | cmdList.append( cmdStr ) |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6666 | else: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6667 | for location in sHost[ "locations" ]: |
| 6668 | cmdStr = "t3-troubleshoot" |
| 6669 | if verbose: |
| 6670 | cmdStr += " -vv" |
| 6671 | if ipv6: |
| 6672 | cmdStr += " -et ipv6" |
| 6673 | cmdStr += " -s " + str( sAddr ) |
| 6674 | cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] ) |
| 6675 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6676 | if sHost[ "vlan" ] != "None": |
| 6677 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6678 | cmdStr += " -d " + str( dAddr ) |
| 6679 | netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) ) |
| 6680 | netcfg = json.loads( netcfg ) |
| 6681 | assert netcfg, "Failed to get netcfg" |
| 6682 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
| 6683 | cmdList.append( cmdStr ) |
| 6684 | return cmdList |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6685 | except AssertionError: |
| 6686 | main.log.exception( "" ) |
| 6687 | return None |
| 6688 | except ( KeyError, TypeError ): |
| 6689 | main.log.exception( self.name + ": Object not as expected" ) |
| 6690 | return None |
| 6691 | except Exception: |
| 6692 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6693 | main.cleanAndExit() |
| 6694 | |
| 6695 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6696 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6697 | Run t3-troubleshoot cli commands for all posible routes given source and destination addresses |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6698 | Options: |
| 6699 | sAddr: IP address of the source host |
| 6700 | dAddr: IP address of the destination host |
| 6701 | """ |
| 6702 | try: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6703 | cmdList = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6704 | assert cmdList is not None, "composeT3Command returned None" |
| 6705 | t3Output = "" |
| 6706 | for cmdStr in cmdList: |
| 6707 | handle = self.sendline( cmdStr ) |
| 6708 | assert handle is not None, "Error in sendline" |
| 6709 | assert "Command not found:" not in handle, handle |
| 6710 | assert "Unsupported command:" not in handle, handle |
| 6711 | assert "Error executing command" not in handle, handle |
| 6712 | assert "Tracing packet" in handle |
| 6713 | t3Output += handle |
| 6714 | return t3Output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6715 | except AssertionError: |
| 6716 | main.log.exception( "" ) |
| 6717 | return None |
| 6718 | except pexpect.EOF: |
| 6719 | main.log.error( self.name + ": EOF exception found" ) |
| 6720 | main.log.error( self.name + ": " + self.handle.before ) |
| 6721 | main.cleanAndExit() |
| 6722 | except Exception: |
| 6723 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6724 | main.cleanAndExit() |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6725 | |
| 6726 | def prepareForCLI( self, debug=True, maxRetries=120 ): |
| 6727 | """ |
| 6728 | Prepare docker container to connect to onos cli |
| 6729 | """ |
| 6730 | try: |
| 6731 | # Wait for log files to be created |
| 6732 | ready = 0 |
| 6733 | retries = 0 |
| 6734 | while not ready and retries < maxRetries: |
| 6735 | retries += 1 |
| 6736 | self.handle.sendline( "ls -al /root/onos/apache-karaf-*/data/log" ) |
| 6737 | ready = self.handle.expect( [ "No such file or directory", self.dockerPrompt ] ) |
| 6738 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6739 | if not ready: |
| 6740 | self.handle.expect( self.dockerPrompt ) |
| 6741 | time.sleep( 1 ) |
| 6742 | #main.log.debug( "%s: It took %s tries for onos log folder to %sbe created" % |
| 6743 | # ( self.name, retries, "" if ready else "NOT " ) ) |
| 6744 | |
| 6745 | cmdList = [] |
| 6746 | cmdList.append( "apt-get update" ) |
| 6747 | cmdList.append( "apt-get install -y openssh-server" ) |
| 6748 | # Some built in scripts are hardcoded |
| 6749 | cmdList.append( "ln -s /root/onos /opt/onos" ) |
| 6750 | cmdList.append( "ln -s /root/onos/apache-karaf-*/data/log /opt/onos/log" ) |
| 6751 | cmdList.append( "ls -al /opt/onos" ) |
| 6752 | output = "" |
| 6753 | for cmdStr in cmdList: |
| 6754 | self.handle.sendline( cmdStr ) |
| 6755 | self.handle.expect( self.dockerPrompt ) |
| 6756 | self.handle.sendline( "" ) |
| 6757 | self.handle.expect( self.dockerPrompt ) |
| 6758 | handle = self.handle.before |
| 6759 | assert "command not found" not in handle, handle |
| 6760 | assert "No such file or directory" not in handle, handle |
| 6761 | output += handle |
| 6762 | if debug: |
| 6763 | main.log.debug( "%s: %s" % ( self.name, output ) ) |
| 6764 | return output |
| 6765 | except AssertionError: |
| 6766 | main.log.exception( "" ) |
| 6767 | return None |
| 6768 | except pexpect.EOF: |
| 6769 | main.log.error( self.name + ": EOF exception found" ) |
| 6770 | main.log.error( self.name + ": " + self.handle.before ) |
| 6771 | main.cleanAndExit() |
| 6772 | except Exception: |
| 6773 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6774 | main.cleanAndExit() |
| 6775 | |
| 6776 | def onosSecureSSH( self, userName="onos", userPWD="rocks" ): |
| 6777 | """ |
| 6778 | Enables secure access to ONOS console |
| 6779 | by removing default users & keys. |
| 6780 | |
| 6781 | bin/onos-user-password onos rocks |
| 6782 | |
| 6783 | Returns: main.TRUE on success and main.FALSE on failure |
| 6784 | """ |
| 6785 | |
| 6786 | try: |
| 6787 | self.handle.sendline( "" ) |
| 6788 | self.handle.expect( self.dockerPrompt ) |
| 6789 | |
| 6790 | self.handle.sendline( "[ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q" ) |
| 6791 | self.handle.expect( self.dockerPrompt ) |
| 6792 | main.log.debug( "%s: %s%s" % ( self.name, self.handle.before, self.handle.after ) ) |
| 6793 | |
| 6794 | self.handle.sendline( "bin/onos-user-key $(id -un) $(cut -d\\\\ -f2 ~/.ssh/id_rsa.pub)" ) |
| 6795 | self.handle.expect( pexpect.TIMEOUT, timeout=10 ) |
| 6796 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6797 | |
| 6798 | self.handle.sendline( "bin/onos-user-password %s %s" % ( userName, userPWD ) ) |
| 6799 | i = self.handle.expect( [ "usage", |
| 6800 | self.dockerPrompt, |
| 6801 | pexpect.TIMEOUT ] ) |
| 6802 | if i == 0: |
| 6803 | # malformed command |
| 6804 | main.log.warn( self.name + ": Could not parse onos-user-password command" ) |
| 6805 | self.handle.expect( self.dockerPrompt ) |
| 6806 | return main.FALSE |
| 6807 | elif i == 1: |
| 6808 | # Process started |
| 6809 | main.log.info( self.name + ": SSH password added for user " + userName ) |
| 6810 | return main.TRUE |
| 6811 | elif i == 2: |
| 6812 | # timeout |
| 6813 | main.log.error( self.name + ": Failed to secure onos ssh " ) |
| 6814 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6815 | except pexpect.EOF: |
| 6816 | main.log.error( self.name + ": EOF exception found" ) |
| 6817 | main.log.error( self.name + ": " + self.handle.before ) |
| 6818 | main.cleanAndExit() |
| 6819 | except Exception: |
| 6820 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6821 | main.cleanAndExit() |