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 |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 61 | self.graph = Graph() |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 62 | super( OnosCliDriver, self ).__init__() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 63 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 64 | def checkOptions( self, var, defaultVar ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 65 | if var is None or var == "": |
| 66 | return defaultVar |
| 67 | return var |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 68 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 69 | def connect( self, **connectargs ): |
| 70 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 71 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 72 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 73 | try: |
| 74 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 75 | vars( self )[ key ] = connectargs[ key ] |
Author Name | e1c7998 | 2019-06-04 23:38:42 +0100 | [diff] [blame] | 76 | self.karafPrompt = self.user_name + "@root >" |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 77 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 78 | for key in self.options: |
| 79 | if key == "home": |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 80 | self.home = self.options[ key ] |
| 81 | elif key == "karaf_username": |
| 82 | self.karafUser = self.options[ key ] |
| 83 | elif key == "karaf_password": |
| 84 | self.karafPass = self.options[ key ] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 85 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 86 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 87 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 88 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 89 | for key in self.options: |
| 90 | if key == 'onosIp': |
| 91 | self.onosIp = self.options[ 'onosIp' ] |
| 92 | break |
| 93 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 95 | |
| 96 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 97 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 98 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 99 | else: |
| 100 | main.log.info( self.name + |
| 101 | ": Trying to connect to " + |
| 102 | self.ip_address ) |
| 103 | |
| 104 | except KeyError: |
| 105 | main.log.info( "Invalid host name," + |
| 106 | " connecting to local host instead" ) |
| 107 | self.ip_address = 'localhost' |
| 108 | except Exception as inst: |
| 109 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 110 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 111 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 112 | user_name=self.user_name, |
| 113 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 114 | port=self.port, |
| 115 | pwd=self.pwd, |
| 116 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 117 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 118 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 119 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 120 | if self.handle: |
| 121 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 122 | else: |
| 123 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 124 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 125 | except TypeError: |
| 126 | main.log.exception( self.name + ": Object not as expected" ) |
| 127 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 128 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 129 | main.log.error( self.name + ": EOF exception found" ) |
| 130 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 131 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 132 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 133 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 134 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 135 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 136 | def disconnect( self ): |
| 137 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 138 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 139 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 140 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 141 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 142 | if self.handle: |
| 143 | i = self.logout() |
| 144 | if i == main.TRUE: |
| 145 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 146 | self.handle.expect( self.prompt ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 147 | self.handle.sendline( "exit" ) |
| 148 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 149 | except TypeError: |
| 150 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 151 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 152 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 153 | main.log.error( self.name + ": EOF exception found" ) |
| 154 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 155 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 156 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 157 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 158 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 159 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 160 | response = main.FALSE |
| 161 | return response |
| 162 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 163 | def logout( self ): |
| 164 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 165 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 166 | Returns main.TRUE if exited CLI and |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 167 | main.FALSE on timeout (not guranteed you are disconnected) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 168 | None on TypeError |
| 169 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 170 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 171 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 172 | if self.handle: |
| 173 | self.handle.sendline( "" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 174 | i = self.handle.expect( [ self.karafPrompt, self.prompt, pexpect.TIMEOUT ], |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 175 | timeout=10 ) |
| 176 | if i == 0: # In ONOS CLI |
| 177 | self.handle.sendline( "logout" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 178 | j = self.handle.expect( [ self.prompt, |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 179 | "Command not found:", |
| 180 | pexpect.TIMEOUT ] ) |
| 181 | if j == 0: # Successfully logged out |
| 182 | return main.TRUE |
| 183 | elif j == 1 or j == 2: |
| 184 | # ONOS didn't fully load, and logout command isn't working |
| 185 | # or the command timed out |
| 186 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 187 | try: |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 188 | self.handle.expect( self.prompt ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 189 | except pexpect.TIMEOUT: |
| 190 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 191 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 192 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 193 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 194 | repr( self.handle.before ) ) |
| 195 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 196 | elif i == 1: # not in CLI |
| 197 | return main.TRUE |
steven30801 | e42f1fb | 2019-01-17 11:31:45 +0800 | [diff] [blame] | 198 | elif i == 2: # Timeout |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 199 | return main.FALSE |
| 200 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 201 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 202 | except TypeError: |
| 203 | main.log.exception( self.name + ": Object not as expected" ) |
| 204 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 205 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 206 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 207 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 208 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 209 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 210 | main.log.error( self.name + |
| 211 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 212 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 213 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 214 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 215 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 216 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 217 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 218 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 219 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 220 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 221 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 222 | try: |
| 223 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 224 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 225 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 226 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 227 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 228 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 229 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 230 | # and that this driver will have to change accordingly |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 231 | self.handle.expect( str( cellname ) ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 232 | handleBefore = self.handle.before |
| 233 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 234 | # Get the rest of the handle |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 235 | self.handle.sendline( "" ) |
| 236 | self.handle.expect( self.prompt ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 237 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 238 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 239 | main.log.info( "Cell call returned: " + handleBefore + |
| 240 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 241 | |
| 242 | return main.TRUE |
| 243 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 244 | except TypeError: |
| 245 | main.log.exception( self.name + ": Object not as expected" ) |
| 246 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 247 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 248 | main.log.error( self.name + ": eof exception found" ) |
| 249 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 250 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 251 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 252 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 253 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 254 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 255 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 256 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 257 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 258 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 259 | by user would be used to set the current karaf shell idle timeout. |
| 260 | 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] | 261 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 262 | Below is an example to start a session with 60 seconds idle timeout |
| 263 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 264 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 265 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 266 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 268 | Note: karafTimeout is left as str so that this could be read |
| 269 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 270 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 271 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 272 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 273 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 274 | self.handle.sendline( "" ) |
| 275 | x = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 276 | self.prompt, self.karafPrompt ], commandlineTimeout ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 277 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 278 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 279 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 280 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 281 | # Not in CLI so login |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 282 | if waitForStart: |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 283 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
| 284 | startCliCommand = "onos-wait-for-start " |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 285 | else: |
| 286 | startCliCommand = "onos " |
| 287 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 288 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 289 | self.karafPrompt, |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 290 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 291 | |
| 292 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 293 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 294 | if karafTimeout: # FIXME: This doesn't look right |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 295 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 296 | "config:property-set -p org.apache.karaf.shell\ |
| 297 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 298 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 299 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 300 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 301 | self.handle.expect( self.karafPrompt ) |
| 302 | main.log.debug( self.handle.before ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 303 | return main.TRUE |
| 304 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 305 | # If failed, send ctrl+c to process and try again |
| 306 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 307 | self.handle.send( "\x03" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 308 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 309 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 310 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 311 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 312 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 313 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 314 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 315 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 316 | "config:property-set -p org.apache.karaf.shell\ |
| 317 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 318 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 319 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 320 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 321 | self.handle.expect( self.karafPrompt ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 322 | return main.TRUE |
| 323 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 324 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 325 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 326 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 327 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 328 | except TypeError: |
| 329 | main.log.exception( self.name + ": Object not as expected" ) |
| 330 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 331 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 332 | main.log.error( self.name + ": EOF exception found" ) |
| 333 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 334 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 335 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 336 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 337 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 338 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 339 | def startCellCli( self, karafTimeout="", |
| 340 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 341 | """ |
| 342 | Start CLI on onos ecll handle. |
| 343 | |
| 344 | karafTimeout is an optional argument. karafTimeout value passed |
| 345 | by user would be used to set the current karaf shell idle timeout. |
| 346 | Note that when ever this property is modified the shell will exit and |
| 347 | the subsequent login would reflect new idle timeout. |
| 348 | Below is an example to start a session with 60 seconds idle timeout |
| 349 | ( input value is in milliseconds ): |
| 350 | |
| 351 | tValue = "60000" |
| 352 | |
| 353 | Note: karafTimeout is left as str so that this could be read |
| 354 | and passed to startOnosCli from PARAMS file as str. |
| 355 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 356 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 357 | try: |
| 358 | self.handle.sendline( "" ) |
| 359 | x = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 360 | self.prompt, self.karafPrompt ], commandlineTimeout ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 361 | |
| 362 | if x == 1: |
| 363 | main.log.info( "ONOS cli is already running" ) |
| 364 | return main.TRUE |
| 365 | |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 366 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 367 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 368 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 369 | self.karafPrompt, |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 370 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 371 | |
| 372 | if i == 0: |
| 373 | main.log.info( self.name + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 374 | if karafTimeout: # FIXME: This doesn't look right |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 375 | self.handle.sendline( |
| 376 | "config:property-set -p org.apache.karaf.shell\ |
| 377 | sshIdleTimeout " + |
| 378 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 379 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 380 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 381 | self.handle.expect( self.karafPrompt ) |
| 382 | main.log.debug( self.handle.before ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 383 | return main.TRUE |
| 384 | else: |
| 385 | # If failed, send ctrl+c to process and try again |
| 386 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 387 | self.handle.send( "\x03" ) |
| 388 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 389 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 390 | timeout=30 ) |
| 391 | if i == 0: |
| 392 | main.log.info( self.name + " CLI Started " + |
| 393 | "successfully after retry attempt" ) |
| 394 | if karafTimeout: |
| 395 | self.handle.sendline( |
| 396 | "config:property-set -p org.apache.karaf.shell\ |
| 397 | sshIdleTimeout " + |
| 398 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 399 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 400 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 401 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 402 | return main.TRUE |
| 403 | else: |
| 404 | main.log.error( "Connection to CLI " + |
| 405 | self.name + " timeout" ) |
| 406 | return main.FALSE |
| 407 | |
| 408 | except TypeError: |
| 409 | main.log.exception( self.name + ": Object not as expected" ) |
| 410 | return None |
| 411 | except pexpect.EOF: |
| 412 | main.log.error( self.name + ": EOF exception found" ) |
| 413 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 414 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 415 | except Exception: |
| 416 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 417 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 418 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 419 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 420 | """ |
| 421 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 422 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 423 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 424 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 425 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 426 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 427 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 428 | """ |
| 429 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 430 | lvlStr = "" |
| 431 | if level: |
| 432 | lvlStr = "--level=" + level |
| 433 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 434 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 435 | self.handle.expect( "log:log" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 436 | self.handle.expect( self.karafPrompt ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 437 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 438 | response = self.handle.before |
| 439 | if re.search( "Error", response ): |
| 440 | return main.FALSE |
| 441 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 442 | except pexpect.TIMEOUT: |
| 443 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 444 | if noExit: |
| 445 | main.cleanup() |
| 446 | return None |
| 447 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 448 | main.cleanAndExit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 449 | except pexpect.EOF: |
| 450 | main.log.error( self.name + ": EOF exception found" ) |
| 451 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 452 | if noExit: |
| 453 | main.cleanup() |
| 454 | return None |
| 455 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 456 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 457 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 458 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 459 | if noExit: |
| 460 | main.cleanup() |
| 461 | return None |
| 462 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 463 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 464 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 465 | def clearBuffer( self, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 466 | """ |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 467 | Test cli connection and clear any left over output in the buffer |
| 468 | Optional Arguments: |
| 469 | debug - Defaults to False. If True, will enable debug logging. |
| 470 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 471 | before a timeout. |
| 472 | 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] | 473 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 474 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 475 | # Try to reconnect if disconnected from cli |
| 476 | self.handle.sendline( "" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 477 | i = self.handle.expect( [ self.karafPrompt, self.prompt, pexpect.TIMEOUT ] ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 478 | response = self.handle.before |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 479 | if i == 1: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 480 | main.log.error( self.name + ": onos cli session closed. " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 481 | if self.onosIp: |
| 482 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 483 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 484 | if reconnectResult: |
| 485 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 486 | else: |
| 487 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 488 | if noExit: |
| 489 | return None |
| 490 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 491 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 492 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 493 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 494 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 495 | main.log.warn( "Timeout when testing cli responsiveness" ) |
| 496 | main.log.debug( self.handle.before ) |
| 497 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 498 | self.handle.expect( self.karafPrompt ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 499 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 500 | response += self.handle.before |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 501 | if debug: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 502 | main.log.debug( self.name + ": Raw output from sending ''" ) |
| 503 | main.log.debug( self.name + ": " + repr( response ) ) |
| 504 | except pexpect.TIMEOUT: |
| 505 | main.log.error( self.name + ": ONOS timeout" ) |
| 506 | main.log.debug( self.handle.before ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 507 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 508 | self.handle.expect( self.karafPrompt ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 509 | return None |
| 510 | except pexpect.EOF: |
| 511 | main.log.error( self.name + ": EOF exception found" ) |
| 512 | main.log.error( self.name + ": " + self.handle.before ) |
| 513 | if noExit: |
| 514 | return None |
| 515 | else: |
| 516 | main.cleanAndExit() |
| 517 | except Exception: |
| 518 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 519 | if noExit: |
| 520 | return None |
| 521 | else: |
| 522 | main.cleanAndExit() |
| 523 | |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 524 | 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] | 525 | """ |
| 526 | A wrapper around pexpect's sendline/expect. Will return all the output from a given command |
| 527 | |
| 528 | Required Arguments: |
| 529 | cmdStr - String to send to the pexpect session |
| 530 | |
| 531 | Optional Arguments: |
| 532 | showResponse - Defaults to False. If True will log the response. |
| 533 | debug - Defaults to False. If True, will enable debug logging. |
| 534 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 535 | before a timeout. |
| 536 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 537 | closed channel, but instead return None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 538 | 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] | 539 | |
| 540 | Warning: There are no sanity checking to commands sent using this method. |
| 541 | |
| 542 | """ |
| 543 | try: |
| 544 | # Try to reconnect if disconnected from cli |
| 545 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
| 546 | if debug: |
| 547 | # NOTE: This adds an average of .4 seconds per call |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 548 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 549 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 550 | self.handle.sendline( cmdStr ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 551 | self.handle.expect( self.karafPrompt, timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 552 | response = self.handle.before |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 553 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 554 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 555 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 556 | main.log.debug( self.name + ": Raw output" ) |
| 557 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 558 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 559 | # Remove control codes from karaf 4.2.1 |
Jon Hall | cf31d0f | 2018-12-13 11:18:48 -0800 | [diff] [blame] | 560 | 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] | 561 | response = karafEscape.sub( '', response ) |
| 562 | if debug: |
| 563 | main.log.debug( self.name + ": karafEscape output" ) |
| 564 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 565 | # Remove ANSI color control strings from output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 566 | ansiEscape = re.compile( r'((\x9b|\x1b\[)[0-?]*[ -/]*[@-~])' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 567 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 568 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 569 | main.log.debug( self.name + ": ansiEscape output" ) |
| 570 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 571 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 572 | # Remove ANSI color control strings from output |
Jon Hall | cf31d0f | 2018-12-13 11:18:48 -0800 | [diff] [blame] | 573 | # NOTE: karaf is sometimes adding a single character then two |
| 574 | # backspaces and sometimes adding 2 characters with 2 backspaces?? |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 575 | backspaceEscape = re.compile( r'((..\x08\x08)|(.|\s)\x08)' ) |
| 576 | response = backspaceEscape.sub( '', response ) |
| 577 | if debug: |
| 578 | main.log.debug( self.name + ": backspaceEscape output" ) |
| 579 | main.log.debug( self.name + ": " + repr( response ) ) |
| 580 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 581 | # Remove extra return chars that get added |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 582 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 583 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 584 | main.log.debug( self.name + ": Removed extra returns " + |
| 585 | "from output" ) |
| 586 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 587 | |
| 588 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 589 | response = response.strip() |
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 + ": parsed and stripped output" ) |
| 592 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 593 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 594 | # parse for just the output, remove the cmd from response |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 595 | cmdPattern = cmdStr.strip() |
| 596 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 597 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 598 | if relaxedRegex: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 599 | cmdPattern = cmdPattern.split( '|' )[ -1 ].strip() |
| 600 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 601 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 602 | # 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] | 603 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 604 | if expectJson: |
| 605 | main.log.warn( "Relaxed Regex: Searching for a json string amongst the output" ) |
| 606 | jsonPattern = r'\{.*\}' |
| 607 | match = re.search( jsonPattern, output[ 0 ] ) |
| 608 | if match: |
| 609 | 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] | 610 | if len( output ) < 2: |
| 611 | 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] | 612 | cmdPattern = cmdPattern[ -5: ] |
| 613 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 614 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
| 615 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 616 | else: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 617 | output = response.split( cmdPattern, 1 ) |
| 618 | 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] | 619 | 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] | 620 | output = response.split( cmdPattern[ -5: ], 1 ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 621 | if output: |
| 622 | if debug: |
| 623 | main.log.debug( self.name + ": split output" ) |
| 624 | for r in output: |
| 625 | main.log.debug( self.name + ": " + repr( r ) ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 626 | if len( output ) == 1: |
| 627 | main.log.error( "Could not remove sent command echo from output" ) |
| 628 | return output |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 629 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 630 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 631 | main.log.info( "Response from ONOS: {}".format( output ) ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 632 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 633 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 634 | except pexpect.TIMEOUT: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 635 | main.log.error( self.name + ": ONOS timeout" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 636 | if debug: |
| 637 | main.log.debug( self.handle.before ) |
You Wang | 6b5e5ba | 2019-01-31 15:32:40 -0800 | [diff] [blame] | 638 | self.exitFromCmd( self.karafPrompt, 100 ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 639 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 640 | except IndexError: |
| 641 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 642 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 643 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 644 | except TypeError: |
| 645 | main.log.exception( self.name + ": Object not as expected" ) |
| 646 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 647 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 648 | main.log.error( self.name + ": EOF exception found" ) |
| 649 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 650 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 651 | return None |
| 652 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 653 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 654 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 655 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 656 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 657 | return None |
| 658 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 659 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 660 | |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 661 | def lineCount( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
| 662 | """ |
| 663 | A wrapper around sendline(). Will return the number of lines returned or None on error |
| 664 | |
| 665 | Required Arguments: |
| 666 | cmdStr - String to send to the pexpect session |
| 667 | |
| 668 | Optional Arguments: |
| 669 | showResponse - Defaults to False. If True will log the response. |
| 670 | debug - Defaults to False. If True, will enable debug logging. |
| 671 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 672 | before a timeout. |
| 673 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 674 | closed channel, but instead return None |
| 675 | 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. |
| 676 | |
| 677 | Warning: There are no sanity checking to commands sent using this method. |
| 678 | |
| 679 | """ |
| 680 | try: |
| 681 | numLines = self.sendline( cmdStr, showResponse, debug, timeout, noExit, relaxedRegex ) |
You Wang | 0ce8e0c | 2019-02-22 12:22:26 -0800 | [diff] [blame] | 682 | parsed = re.search( "(\d+)", numLines ) |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 683 | if not parsed: |
| 684 | main.log.error( "Warning, output of karaf's wc may have changed" ) |
| 685 | return None |
| 686 | return parsed.group( 1 ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 687 | except IndexError: |
| 688 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 689 | main.log.debug( "response: {}".format( repr( numLines ) ) ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 690 | return None |
| 691 | except TypeError: |
| 692 | main.log.exception( self.name + ": Object not as expected" ) |
| 693 | return None |
| 694 | except Exception: |
| 695 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 696 | if noExit: |
| 697 | return None |
| 698 | else: |
| 699 | main.cleanAndExit() |
| 700 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 701 | # IMPORTANT NOTE: |
| 702 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 703 | # the cli command changing 'a:b' with 'aB'. |
| 704 | # Ex ) onos:topology > onosTopology |
| 705 | # onos:links > onosLinks |
| 706 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 707 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 708 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 709 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 710 | Adds a new cluster node by ID and address information. |
| 711 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 712 | * nodeId |
| 713 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 714 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 715 | * 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 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 718 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 719 | str( ONOSIp ) + " " + str( tcpPort ) |
| 720 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 721 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 722 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 723 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 724 | main.log.error( self.name + ": Error in adding node" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 725 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 726 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 727 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 728 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 729 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 730 | except AssertionError: |
| 731 | main.log.exception( "" ) |
| 732 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 733 | except TypeError: |
| 734 | main.log.exception( self.name + ": Object not as expected" ) |
| 735 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 736 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 737 | main.log.error( self.name + ": EOF exception found" ) |
| 738 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 739 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 740 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 741 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 742 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 743 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 744 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 745 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 746 | Removes a cluster by ID |
| 747 | Issues command: 'remove-node [<node-id>]' |
| 748 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 749 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 750 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 751 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 752 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 753 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 754 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 755 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 756 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 757 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 758 | main.log.error( self.name + ": Error in removing node" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 759 | main.log.error( handle ) |
| 760 | return main.FALSE |
| 761 | else: |
| 762 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 763 | except AssertionError: |
| 764 | main.log.exception( "" ) |
| 765 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 766 | except TypeError: |
| 767 | main.log.exception( self.name + ": Object not as expected" ) |
| 768 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 769 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 770 | main.log.error( self.name + ": EOF exception found" ) |
| 771 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 772 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 773 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 774 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 775 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 776 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 777 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 778 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 779 | List the nodes currently visible |
| 780 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 781 | Optional argument: |
| 782 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 783 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 784 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 785 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 786 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 787 | cmdStr += " -j" |
| 788 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 789 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 790 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 791 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 792 | except AssertionError: |
| 793 | main.log.exception( "" ) |
| 794 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 795 | except TypeError: |
| 796 | main.log.exception( self.name + ": Object not as expected" ) |
| 797 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 798 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 799 | main.log.error( self.name + ": EOF exception found" ) |
| 800 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 801 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 802 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 803 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 804 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 805 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 806 | def topology( self ): |
| 807 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 808 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 809 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 810 | Return: |
| 811 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 812 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 813 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 814 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 815 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 816 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 817 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 818 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 819 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 820 | except AssertionError: |
| 821 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 822 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 823 | except TypeError: |
| 824 | main.log.exception( self.name + ": Object not as expected" ) |
| 825 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 826 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 827 | main.log.error( self.name + ": EOF exception found" ) |
| 828 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 829 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 830 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 831 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 832 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 833 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 834 | def deviceRemove( self, deviceId ): |
| 835 | """ |
| 836 | Removes particular device from storage |
| 837 | |
| 838 | TODO: refactor this function |
| 839 | """ |
| 840 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 841 | cmdStr = "device-remove " + str( deviceId ) |
| 842 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 843 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 844 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 845 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 846 | main.log.error( self.name + ": Error in removing device" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 847 | main.log.error( handle ) |
| 848 | return main.FALSE |
| 849 | else: |
| 850 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 851 | except AssertionError: |
| 852 | main.log.exception( "" ) |
| 853 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 854 | except TypeError: |
| 855 | main.log.exception( self.name + ": Object not as expected" ) |
| 856 | return None |
| 857 | except pexpect.EOF: |
| 858 | main.log.error( self.name + ": EOF exception found" ) |
| 859 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 860 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 861 | except Exception: |
| 862 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 863 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 864 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 865 | def devices( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 866 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 867 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 868 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 869 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 870 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 871 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 872 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 873 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 874 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 875 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 876 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 877 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 878 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 879 | except AssertionError: |
| 880 | main.log.exception( "" ) |
| 881 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 882 | except TypeError: |
| 883 | main.log.exception( self.name + ": Object not as expected" ) |
| 884 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 885 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 886 | main.log.error( self.name + ": EOF exception found" ) |
| 887 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 888 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 889 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 890 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 891 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 892 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 893 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 894 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 895 | This balances the devices across all controllers |
| 896 | by issuing command: 'onos> onos:balance-masters' |
| 897 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 898 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 899 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 900 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 901 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 902 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 903 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 904 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 905 | main.log.error( self.name + ": Error in balancing masters" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 906 | main.log.error( handle ) |
| 907 | return main.FALSE |
| 908 | else: |
| 909 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 910 | except AssertionError: |
| 911 | main.log.exception( "" ) |
| 912 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 913 | except TypeError: |
| 914 | main.log.exception( self.name + ": Object not as expected" ) |
| 915 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 916 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 917 | main.log.error( self.name + ": EOF exception found" ) |
| 918 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 919 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 920 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 921 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 922 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 923 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 924 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 925 | """ |
| 926 | Returns the output of the masters command. |
| 927 | Optional argument: |
| 928 | * jsonFormat - boolean indicating if you want output in json |
| 929 | """ |
| 930 | try: |
| 931 | cmdStr = "onos:masters" |
| 932 | if jsonFormat: |
| 933 | cmdStr += " -j" |
| 934 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 935 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 936 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 937 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 938 | except AssertionError: |
| 939 | main.log.exception( "" ) |
| 940 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 941 | except TypeError: |
| 942 | main.log.exception( self.name + ": Object not as expected" ) |
| 943 | return None |
| 944 | except pexpect.EOF: |
| 945 | main.log.error( self.name + ": EOF exception found" ) |
| 946 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 947 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 948 | except Exception: |
| 949 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 950 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 951 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 952 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 953 | """ |
| 954 | Uses the master command to check that the devices' leadership |
| 955 | is evenly divided |
| 956 | |
| 957 | Dependencies: checkMasters() and summary() |
| 958 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 959 | Returns main.TRUE if the devices are balanced |
| 960 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 961 | Exits on Exception |
| 962 | Returns None on TypeError |
| 963 | """ |
| 964 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 965 | summaryOutput = self.summary() |
| 966 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 967 | except ( TypeError, ValueError ): |
| 968 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 969 | return None |
| 970 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 971 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 972 | mastersOutput = self.checkMasters() |
| 973 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 974 | first = masters[ 0 ][ "size" ] |
| 975 | for master in masters: |
| 976 | totalOwnedDevices += master[ "size" ] |
| 977 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 978 | main.log.error( "Mastership not balanced" ) |
| 979 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 980 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 981 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 982 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 983 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 984 | except ( TypeError, ValueError ): |
| 985 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 986 | return None |
| 987 | except pexpect.EOF: |
| 988 | main.log.error( self.name + ": EOF exception found" ) |
| 989 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 990 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 991 | except Exception: |
| 992 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 993 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 994 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 995 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 996 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 997 | Lists all core links |
| 998 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 999 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1000 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1001 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1002 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1003 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1004 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1005 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1006 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1007 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1008 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1009 | except AssertionError: |
| 1010 | main.log.exception( "" ) |
| 1011 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1012 | except TypeError: |
| 1013 | main.log.exception( self.name + ": Object not as expected" ) |
| 1014 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1015 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1016 | main.log.error( self.name + ": EOF exception found" ) |
| 1017 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1018 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1019 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1020 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1021 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1022 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1023 | def ports( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1024 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1025 | Lists all ports |
| 1026 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1027 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1028 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1029 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1030 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1031 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1032 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1033 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1034 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1035 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1036 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1037 | except AssertionError: |
| 1038 | main.log.exception( "" ) |
| 1039 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1040 | except TypeError: |
| 1041 | main.log.exception( self.name + ": Object not as expected" ) |
| 1042 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1043 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1044 | main.log.error( self.name + ": EOF exception found" ) |
| 1045 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1046 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1047 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1048 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1049 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1050 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1051 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1052 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1053 | Lists all devices and the controllers with roles assigned to them |
| 1054 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1055 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1056 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1057 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1058 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1059 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1060 | cmdStr += " -j" |
| 1061 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1062 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1063 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1064 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1065 | except AssertionError: |
| 1066 | main.log.exception( "" ) |
| 1067 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1068 | except TypeError: |
| 1069 | main.log.exception( self.name + ": Object not as expected" ) |
| 1070 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1071 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1072 | main.log.error( self.name + ": EOF exception found" ) |
| 1073 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1074 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1075 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1076 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1077 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1078 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1079 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1080 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1081 | Given the a string containing the json representation of the "roles" |
| 1082 | cli command and a partial or whole device id, returns a json object |
| 1083 | containing the roles output for the first device whose id contains |
| 1084 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1085 | |
| 1086 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1087 | A dict of the role assignments for the given device or |
| 1088 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1089 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1090 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1091 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1092 | return None |
| 1093 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1094 | rawRoles = self.roles() |
| 1095 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1096 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1097 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1098 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1099 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1100 | return device |
| 1101 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1102 | except ( TypeError, ValueError ): |
| 1103 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1104 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 1105 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1106 | main.log.error( self.name + ": EOF exception found" ) |
| 1107 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1108 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1109 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1110 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1111 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1112 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1113 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1114 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1115 | Iterates through each device and checks if there is a master assigned |
| 1116 | Returns: main.TRUE if each device has a master |
| 1117 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1118 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1119 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1120 | rawRoles = self.roles() |
| 1121 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1122 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1123 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1124 | # print device |
| 1125 | if device[ 'master' ] == "none": |
| 1126 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1127 | return main.FALSE |
| 1128 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1129 | except ( TypeError, ValueError ): |
| 1130 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1131 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1132 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1133 | main.log.error( self.name + ": EOF exception found" ) |
| 1134 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1135 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1136 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1137 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1138 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1139 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1140 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1141 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1142 | Returns string of paths, and the cost. |
| 1143 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1144 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1145 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1146 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1147 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1148 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1149 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1150 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1151 | main.log.error( self.name + ": Error in getting paths" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1152 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1153 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1154 | path = handle.split( ";" )[ 0 ] |
| 1155 | cost = handle.split( ";" )[ 1 ] |
| 1156 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1157 | except AssertionError: |
| 1158 | main.log.exception( "" ) |
| 1159 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1160 | except TypeError: |
| 1161 | main.log.exception( self.name + ": Object not as expected" ) |
| 1162 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1163 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1164 | main.log.error( self.name + ": EOF exception found" ) |
| 1165 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1166 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1167 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1168 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1169 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1170 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1171 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1172 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1173 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1174 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1175 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1176 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1177 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1178 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1179 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1180 | cmdStr += " -j" |
| 1181 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1182 | if handle: |
| 1183 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1184 | # TODO: Maybe make this less hardcoded |
| 1185 | # ConsistentMap Exceptions |
| 1186 | assert "org.onosproject.store.service" not in handle |
| 1187 | # Node not leader |
| 1188 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1189 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1190 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1191 | main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1192 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1193 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1194 | except TypeError: |
| 1195 | main.log.exception( self.name + ": Object not as expected" ) |
| 1196 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1197 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1198 | main.log.error( self.name + ": EOF exception found" ) |
| 1199 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1200 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1201 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1202 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1203 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1204 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1205 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1206 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1207 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1208 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1209 | 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] | 1210 | partial mac address |
| 1211 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1212 | Return None if there is no match |
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 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1215 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1216 | return None |
| 1217 | else: |
| 1218 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1219 | rawHosts = self.hosts() |
| 1220 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1221 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1222 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1223 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1224 | if not host: |
| 1225 | pass |
| 1226 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1227 | return host |
| 1228 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1229 | except ( TypeError, ValueError ): |
| 1230 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1231 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1232 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1233 | main.log.error( self.name + ": EOF exception found" ) |
| 1234 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1235 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1236 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1237 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1238 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1239 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1240 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1241 | """ |
| 1242 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1243 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1244 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1245 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1246 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1247 | IMPORTANT: |
| 1248 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1249 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1250 | Furthermore, it assumes that value of VLAN is '-1' |
| 1251 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1252 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1253 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1254 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1255 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1256 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1257 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1258 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1259 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1260 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1261 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1262 | i = iter( str( hostHex ) ) |
| 1263 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1264 | hostHex = hostHex + "/-1" |
| 1265 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1266 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1267 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1268 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1269 | except TypeError: |
| 1270 | main.log.exception( self.name + ": Object not as expected" ) |
| 1271 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1272 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1273 | main.log.error( self.name + ": EOF exception found" ) |
| 1274 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1275 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1276 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1277 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1278 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1279 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1280 | def verifyHostLocation( self, hostIp, location ): |
| 1281 | """ |
| 1282 | Description: |
| 1283 | Verify the host given is discovered in all locations expected |
| 1284 | Required: |
| 1285 | hostIp: IP address of the host |
| 1286 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1287 | Could be a string or list |
| 1288 | Returns: |
| 1289 | main.TRUE if host is discovered on all locations provided |
| 1290 | main.FALSE otherwise |
| 1291 | """ |
| 1292 | import json |
| 1293 | locations = [ location ] if isinstance( location, str ) else location |
| 1294 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1295 | try: |
| 1296 | hosts = self.hosts() |
| 1297 | hosts = json.loads( hosts ) |
| 1298 | targetHost = None |
| 1299 | for host in hosts: |
| 1300 | if hostIp in host[ "ipAddresses" ]: |
| 1301 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1302 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1303 | result = main.TRUE |
| 1304 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1305 | for loc in locations: |
| 1306 | discovered = False |
| 1307 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1308 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1309 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1310 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1311 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1312 | break |
| 1313 | if discovered: |
| 1314 | locationsDiscovered.remove( locDiscovered ) |
| 1315 | else: |
| 1316 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1317 | result = main.FALSE |
| 1318 | if locationsDiscovered: |
| 1319 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1320 | result = main.FALSE |
| 1321 | return result |
| 1322 | except KeyError: |
| 1323 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1324 | return None |
| 1325 | except pexpect.EOF: |
| 1326 | main.log.error( self.name + ": EOF exception found" ) |
| 1327 | main.log.error( self.name + ": " + self.handle.before ) |
| 1328 | main.cleanAndExit() |
| 1329 | except Exception: |
| 1330 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1331 | return None |
| 1332 | |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1333 | def verifyHostIp( self, hostList=[], prefix="" ): |
| 1334 | """ |
| 1335 | Description: |
| 1336 | Verify that all hosts have IP address assigned to them |
| 1337 | Optional: |
| 1338 | hostList: If specified, verifications only happen to the hosts |
| 1339 | in hostList |
| 1340 | prefix: at least one of the ip address assigned to the host |
| 1341 | needs to have the specified prefix |
| 1342 | Returns: |
| 1343 | main.TRUE if all hosts have specific IP address assigned; |
| 1344 | main.FALSE otherwise |
| 1345 | """ |
| 1346 | import json |
| 1347 | try: |
| 1348 | hosts = self.hosts() |
| 1349 | hosts = json.loads( hosts ) |
| 1350 | if not hostList: |
| 1351 | hostList = [ host[ "id" ] for host in hosts ] |
| 1352 | for host in hosts: |
| 1353 | hostId = host[ "id" ] |
| 1354 | if hostId not in hostList: |
| 1355 | continue |
| 1356 | ipList = host[ "ipAddresses" ] |
| 1357 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1358 | if not ipList: |
| 1359 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1360 | else: |
| 1361 | if not any( ip.startswith( str( prefix ) ) for ip in ipList ): |
| 1362 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1363 | else: |
| 1364 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1365 | hostList.remove( hostId ) |
| 1366 | if hostList: |
| 1367 | main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) ) |
| 1368 | return main.FALSE |
| 1369 | else: |
| 1370 | return main.TRUE |
| 1371 | except KeyError: |
| 1372 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1373 | return None |
| 1374 | except pexpect.EOF: |
| 1375 | main.log.error( self.name + ": EOF exception found" ) |
| 1376 | main.log.error( self.name + ": " + self.handle.before ) |
| 1377 | main.cleanAndExit() |
| 1378 | except Exception: |
| 1379 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1380 | return None |
| 1381 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1382 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1383 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1384 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1385 | * hostIdOne: ONOS host id for host1 |
| 1386 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1387 | Optional: |
| 1388 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1389 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1390 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1391 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1392 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1393 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1394 | Returns: |
| 1395 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1396 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1397 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1398 | cmdStr = "add-host-intent " |
| 1399 | if vlanId: |
| 1400 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1401 | if setVlan: |
| 1402 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1403 | if encap: |
| 1404 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1405 | if bandwidth: |
| 1406 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1407 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1408 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1409 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1410 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1411 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1412 | main.log.error( self.name + ": Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1413 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1414 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1415 | else: |
| 1416 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1417 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1418 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1419 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1420 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1421 | else: |
| 1422 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1423 | main.log.debug( "Response from ONOS was: " + |
| 1424 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1425 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1426 | except AssertionError: |
| 1427 | main.log.exception( "" ) |
| 1428 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1429 | except TypeError: |
| 1430 | main.log.exception( self.name + ": Object not as expected" ) |
| 1431 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1432 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1433 | main.log.error( self.name + ": EOF exception found" ) |
| 1434 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1435 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1436 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1437 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1438 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1439 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1440 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1441 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1442 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1443 | * ingressDevice: device id of ingress device |
| 1444 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1445 | Optional: |
| 1446 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1447 | Description: |
| 1448 | Adds an optical intent by specifying an ingress and egress device |
| 1449 | Returns: |
| 1450 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1451 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1452 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1453 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1454 | " " + str( egressDevice ) |
| 1455 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1456 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1457 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1458 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1459 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1460 | main.log.error( self.name + ": Error in adding Optical intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1461 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1462 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1463 | main.log.info( "Optical intent installed between " + |
| 1464 | str( ingressDevice ) + " and " + |
| 1465 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1466 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1467 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1468 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1469 | else: |
| 1470 | main.log.error( "Error, intent ID not found" ) |
| 1471 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1472 | except AssertionError: |
| 1473 | main.log.exception( "" ) |
| 1474 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1475 | except TypeError: |
| 1476 | main.log.exception( self.name + ": Object not as expected" ) |
| 1477 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1478 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1479 | main.log.error( self.name + ": EOF exception found" ) |
| 1480 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1481 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1482 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1483 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1484 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1485 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1486 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1487 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1488 | ingressDevice, |
| 1489 | egressDevice, |
| 1490 | portIngress="", |
| 1491 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1492 | ethType="", |
| 1493 | ethSrc="", |
| 1494 | ethDst="", |
| 1495 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1496 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1497 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1498 | ipProto="", |
| 1499 | ipSrc="", |
| 1500 | ipDst="", |
| 1501 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1502 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1503 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1504 | setVlan="", |
| 1505 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1506 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1507 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1508 | * ingressDevice: device id of ingress device |
| 1509 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1510 | Optional: |
| 1511 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1512 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1513 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1514 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1515 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1516 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1517 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1518 | * ipSrc: specify ip source address |
| 1519 | * ipDst: specify ip destination address |
| 1520 | * tcpSrc: specify tcp source port |
| 1521 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1522 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1523 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1524 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1525 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1526 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1527 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1528 | Returns: |
| 1529 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1530 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1531 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1532 | options developers provide for point-to-point |
| 1533 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1534 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1535 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1536 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1537 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1538 | if ethType: |
| 1539 | cmd += " --ethType " + str( ethType ) |
| 1540 | if ethSrc: |
| 1541 | cmd += " --ethSrc " + str( ethSrc ) |
| 1542 | if ethDst: |
| 1543 | cmd += " --ethDst " + str( ethDst ) |
| 1544 | if bandwidth: |
| 1545 | cmd += " --bandwidth " + str( bandwidth ) |
| 1546 | if lambdaAlloc: |
| 1547 | cmd += " --lambda " |
| 1548 | if ipProto: |
| 1549 | cmd += " --ipProto " + str( ipProto ) |
| 1550 | if ipSrc: |
| 1551 | cmd += " --ipSrc " + str( ipSrc ) |
| 1552 | if ipDst: |
| 1553 | cmd += " --ipDst " + str( ipDst ) |
| 1554 | if tcpSrc: |
| 1555 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1556 | if tcpDst: |
| 1557 | cmd += " --tcpDst " + str( tcpDst ) |
| 1558 | if vlanId: |
| 1559 | cmd += " -v " + str( vlanId ) |
| 1560 | if setVlan: |
| 1561 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1562 | if encap: |
| 1563 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1564 | if protected: |
| 1565 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1566 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1567 | # Check whether the user appended the port |
| 1568 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1569 | if "/" in ingressDevice: |
| 1570 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1571 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1572 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1573 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1574 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1575 | # Would it make sense to throw an exception and exit |
| 1576 | # the test? |
| 1577 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1578 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1579 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1580 | str( ingressDevice ) + "/" +\ |
| 1581 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1582 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1583 | if "/" in egressDevice: |
| 1584 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1585 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1586 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1587 | main.log.error( "You must specify the egress port" ) |
| 1588 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1589 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1590 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1591 | str( egressDevice ) + "/" +\ |
| 1592 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1593 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1594 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1595 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1596 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1597 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1598 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1599 | main.log.error( self.name + ": Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1600 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1601 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1602 | # TODO: print out all the options in this message? |
| 1603 | main.log.info( "Point-to-point intent installed between " + |
| 1604 | str( ingressDevice ) + " and " + |
| 1605 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1606 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1607 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1608 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1609 | else: |
| 1610 | main.log.error( "Error, intent ID not found" ) |
| 1611 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1612 | except AssertionError: |
| 1613 | main.log.exception( "" ) |
| 1614 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1615 | except TypeError: |
| 1616 | main.log.exception( self.name + ": Object not as expected" ) |
| 1617 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1618 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1619 | main.log.error( self.name + ": EOF exception found" ) |
| 1620 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1621 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1622 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1623 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1624 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1625 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1626 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1627 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1628 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1629 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1630 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1631 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1632 | ethType="", |
| 1633 | ethSrc="", |
| 1634 | ethDst="", |
| 1635 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1636 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1637 | ipProto="", |
| 1638 | ipSrc="", |
| 1639 | ipDst="", |
| 1640 | tcpSrc="", |
| 1641 | tcpDst="", |
| 1642 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1643 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1644 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1645 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1646 | partial=False, |
| 1647 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1648 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1649 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1650 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1651 | is same. That is, all ingress devices include port numbers |
| 1652 | with a "/" or all ingress devices could specify device |
| 1653 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1654 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1655 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1656 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1657 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1658 | Optional: |
| 1659 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1660 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1661 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1662 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1663 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1664 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1665 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1666 | * ipSrc: specify ip source address |
| 1667 | * ipDst: specify ip destination address |
| 1668 | * tcpSrc: specify tcp source port |
| 1669 | * tcpDst: specify tcp destination port |
| 1670 | * setEthSrc: action to Rewrite Source MAC Address |
| 1671 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1672 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1673 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1674 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1675 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1676 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1677 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1678 | Returns: |
| 1679 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1680 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1681 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1682 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1683 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1684 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1685 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1686 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1687 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1688 | if ethType: |
| 1689 | cmd += " --ethType " + str( ethType ) |
| 1690 | if ethSrc: |
| 1691 | cmd += " --ethSrc " + str( ethSrc ) |
| 1692 | if ethDst: |
| 1693 | cmd += " --ethDst " + str( ethDst ) |
| 1694 | if bandwidth: |
| 1695 | cmd += " --bandwidth " + str( bandwidth ) |
| 1696 | if lambdaAlloc: |
| 1697 | cmd += " --lambda " |
| 1698 | if ipProto: |
| 1699 | cmd += " --ipProto " + str( ipProto ) |
| 1700 | if ipSrc: |
| 1701 | cmd += " --ipSrc " + str( ipSrc ) |
| 1702 | if ipDst: |
| 1703 | cmd += " --ipDst " + str( ipDst ) |
| 1704 | if tcpSrc: |
| 1705 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1706 | if tcpDst: |
| 1707 | cmd += " --tcpDst " + str( tcpDst ) |
| 1708 | if setEthSrc: |
| 1709 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1710 | if setEthDst: |
| 1711 | cmd += " --setEthDst " + str( setEthDst ) |
| 1712 | if vlanId: |
| 1713 | cmd += " -v " + str( vlanId ) |
| 1714 | if setVlan: |
| 1715 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1716 | if partial: |
| 1717 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1718 | if encap: |
| 1719 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1720 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1721 | # Check whether the user appended the port |
| 1722 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1723 | |
| 1724 | if portIngressList is None: |
| 1725 | for ingressDevice in ingressDeviceList: |
| 1726 | if "/" in ingressDevice: |
| 1727 | cmd += " " + str( ingressDevice ) |
| 1728 | else: |
| 1729 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1730 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1731 | # TODO: perhaps more meaningful return |
| 1732 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1733 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1734 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1735 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1736 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1737 | cmd += " " + \ |
| 1738 | str( ingressDevice ) + "/" +\ |
| 1739 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1740 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1741 | main.log.error( "Device list and port list does not " + |
| 1742 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1743 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1744 | if "/" in egressDevice: |
| 1745 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1746 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1747 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1748 | main.log.error( "You must specify " + |
| 1749 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1750 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1751 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1752 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1753 | str( egressDevice ) + "/" +\ |
| 1754 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1755 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1756 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1757 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1758 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1759 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1760 | main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1761 | "intent" ) |
| 1762 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1763 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1764 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1765 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1766 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1767 | else: |
| 1768 | main.log.error( "Error, intent ID not found" ) |
| 1769 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1770 | except AssertionError: |
| 1771 | main.log.exception( "" ) |
| 1772 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1773 | except TypeError: |
| 1774 | main.log.exception( self.name + ": Object not as expected" ) |
| 1775 | return None |
| 1776 | except pexpect.EOF: |
| 1777 | main.log.error( self.name + ": EOF exception found" ) |
| 1778 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1779 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1780 | except Exception: |
| 1781 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1782 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1783 | |
| 1784 | def addSinglepointToMultipointIntent( |
| 1785 | self, |
| 1786 | ingressDevice, |
| 1787 | egressDeviceList, |
| 1788 | portIngress="", |
| 1789 | portEgressList=None, |
| 1790 | ethType="", |
| 1791 | ethSrc="", |
| 1792 | ethDst="", |
| 1793 | bandwidth="", |
| 1794 | lambdaAlloc=False, |
| 1795 | ipProto="", |
| 1796 | ipSrc="", |
| 1797 | ipDst="", |
| 1798 | tcpSrc="", |
| 1799 | tcpDst="", |
| 1800 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1801 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1802 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1803 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1804 | partial=False, |
| 1805 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1806 | """ |
| 1807 | Note: |
| 1808 | This function assumes the format of all egress devices |
| 1809 | is same. That is, all egress devices include port numbers |
| 1810 | with a "/" or all egress devices could specify device |
| 1811 | ids and port numbers seperately. |
| 1812 | Required: |
| 1813 | * EgressDeviceList: List of device ids of egress device |
| 1814 | ( Atleast 2 eress devices required in the list ) |
| 1815 | * ingressDevice: device id of ingress device |
| 1816 | Optional: |
| 1817 | * ethType: specify ethType |
| 1818 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1819 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1820 | * bandwidth: specify bandwidth capacity of link |
| 1821 | * lambdaAlloc: if True, intent will allocate lambda |
| 1822 | for the specified intent |
| 1823 | * ipProto: specify ip protocol |
| 1824 | * ipSrc: specify ip source address |
| 1825 | * ipDst: specify ip destination address |
| 1826 | * tcpSrc: specify tcp source port |
| 1827 | * tcpDst: specify tcp destination port |
| 1828 | * setEthSrc: action to Rewrite Source MAC Address |
| 1829 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1830 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1831 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1832 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1833 | Description: |
| 1834 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1835 | specifying device id's and optional fields |
| 1836 | Returns: |
| 1837 | A string of the intent id or None on error |
| 1838 | |
| 1839 | NOTE: This function may change depending on the |
| 1840 | options developers provide for singlepoint-to-multipoint |
| 1841 | intent via cli |
| 1842 | """ |
| 1843 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1844 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1845 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1846 | if ethType: |
| 1847 | cmd += " --ethType " + str( ethType ) |
| 1848 | if ethSrc: |
| 1849 | cmd += " --ethSrc " + str( ethSrc ) |
| 1850 | if ethDst: |
| 1851 | cmd += " --ethDst " + str( ethDst ) |
| 1852 | if bandwidth: |
| 1853 | cmd += " --bandwidth " + str( bandwidth ) |
| 1854 | if lambdaAlloc: |
| 1855 | cmd += " --lambda " |
| 1856 | if ipProto: |
| 1857 | cmd += " --ipProto " + str( ipProto ) |
| 1858 | if ipSrc: |
| 1859 | cmd += " --ipSrc " + str( ipSrc ) |
| 1860 | if ipDst: |
| 1861 | cmd += " --ipDst " + str( ipDst ) |
| 1862 | if tcpSrc: |
| 1863 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1864 | if tcpDst: |
| 1865 | cmd += " --tcpDst " + str( tcpDst ) |
| 1866 | if setEthSrc: |
| 1867 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1868 | if setEthDst: |
| 1869 | cmd += " --setEthDst " + str( setEthDst ) |
| 1870 | if vlanId: |
| 1871 | cmd += " -v " + str( vlanId ) |
| 1872 | if setVlan: |
| 1873 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1874 | if partial: |
| 1875 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1876 | if encap: |
| 1877 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1878 | |
| 1879 | # Check whether the user appended the port |
| 1880 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1881 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1882 | if "/" in ingressDevice: |
| 1883 | cmd += " " + str( ingressDevice ) |
| 1884 | else: |
| 1885 | if not portIngress: |
| 1886 | main.log.error( "You must specify " + |
| 1887 | "the Ingress port" ) |
| 1888 | return main.FALSE |
| 1889 | |
| 1890 | cmd += " " +\ |
| 1891 | str( ingressDevice ) + "/" +\ |
| 1892 | str( portIngress ) |
| 1893 | |
| 1894 | if portEgressList is None: |
| 1895 | for egressDevice in egressDeviceList: |
| 1896 | if "/" in egressDevice: |
| 1897 | cmd += " " + str( egressDevice ) |
| 1898 | else: |
| 1899 | main.log.error( "You must specify " + |
| 1900 | "the egress port" ) |
| 1901 | # TODO: perhaps more meaningful return |
| 1902 | return main.FALSE |
| 1903 | else: |
| 1904 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1905 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1906 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1907 | cmd += " " + \ |
| 1908 | str( egressDevice ) + "/" +\ |
| 1909 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1910 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1911 | main.log.error( "Device list and port list does not " + |
| 1912 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1913 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1914 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1915 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1916 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1917 | # If error, return error message |
| 1918 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1919 | main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " + |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1920 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1921 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1922 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1923 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1924 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1925 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1926 | else: |
| 1927 | main.log.error( "Error, intent ID not found" ) |
| 1928 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1929 | except AssertionError: |
| 1930 | main.log.exception( "" ) |
| 1931 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1932 | except TypeError: |
| 1933 | main.log.exception( self.name + ": Object not as expected" ) |
| 1934 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1935 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1936 | main.log.error( self.name + ": EOF exception found" ) |
| 1937 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1938 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1939 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1940 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1941 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1942 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1943 | def addMplsIntent( |
| 1944 | self, |
| 1945 | ingressDevice, |
| 1946 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1947 | ingressPort="", |
| 1948 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1949 | ethType="", |
| 1950 | ethSrc="", |
| 1951 | ethDst="", |
| 1952 | bandwidth="", |
| 1953 | lambdaAlloc=False, |
| 1954 | ipProto="", |
| 1955 | ipSrc="", |
| 1956 | ipDst="", |
| 1957 | tcpSrc="", |
| 1958 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1959 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1960 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1961 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1962 | """ |
| 1963 | Required: |
| 1964 | * ingressDevice: device id of ingress device |
| 1965 | * egressDevice: device id of egress device |
| 1966 | Optional: |
| 1967 | * ethType: specify ethType |
| 1968 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1969 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1970 | * bandwidth: specify bandwidth capacity of link |
| 1971 | * lambdaAlloc: if True, intent will allocate lambda |
| 1972 | for the specified intent |
| 1973 | * ipProto: specify ip protocol |
| 1974 | * ipSrc: specify ip source address |
| 1975 | * ipDst: specify ip destination address |
| 1976 | * tcpSrc: specify tcp source port |
| 1977 | * tcpDst: specify tcp destination port |
| 1978 | * ingressLabel: Ingress MPLS label |
| 1979 | * egressLabel: Egress MPLS label |
| 1980 | Description: |
| 1981 | Adds MPLS intent by |
| 1982 | specifying device id's and optional fields |
| 1983 | Returns: |
| 1984 | A string of the intent id or None on error |
| 1985 | |
| 1986 | NOTE: This function may change depending on the |
| 1987 | options developers provide for MPLS |
| 1988 | intent via cli |
| 1989 | """ |
| 1990 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1991 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1992 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1993 | if ethType: |
| 1994 | cmd += " --ethType " + str( ethType ) |
| 1995 | if ethSrc: |
| 1996 | cmd += " --ethSrc " + str( ethSrc ) |
| 1997 | if ethDst: |
| 1998 | cmd += " --ethDst " + str( ethDst ) |
| 1999 | if bandwidth: |
| 2000 | cmd += " --bandwidth " + str( bandwidth ) |
| 2001 | if lambdaAlloc: |
| 2002 | cmd += " --lambda " |
| 2003 | if ipProto: |
| 2004 | cmd += " --ipProto " + str( ipProto ) |
| 2005 | if ipSrc: |
| 2006 | cmd += " --ipSrc " + str( ipSrc ) |
| 2007 | if ipDst: |
| 2008 | cmd += " --ipDst " + str( ipDst ) |
| 2009 | if tcpSrc: |
| 2010 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 2011 | if tcpDst: |
| 2012 | cmd += " --tcpDst " + str( tcpDst ) |
| 2013 | if ingressLabel: |
| 2014 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 2015 | if egressLabel: |
| 2016 | cmd += " --egressLabel " + str( egressLabel ) |
| 2017 | if priority: |
| 2018 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2019 | |
| 2020 | # Check whether the user appended the port |
| 2021 | # or provided it as an input |
| 2022 | if "/" in ingressDevice: |
| 2023 | cmd += " " + str( ingressDevice ) |
| 2024 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2025 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2026 | main.log.error( "You must specify the ingress port" ) |
| 2027 | return None |
| 2028 | |
| 2029 | cmd += " " + \ |
| 2030 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2031 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2032 | |
| 2033 | if "/" in egressDevice: |
| 2034 | cmd += " " + str( egressDevice ) |
| 2035 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2036 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2037 | main.log.error( "You must specify the egress port" ) |
| 2038 | return None |
| 2039 | |
| 2040 | cmd += " " +\ |
| 2041 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2042 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2043 | |
| 2044 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2045 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2046 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2047 | # If error, return error message |
| 2048 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2049 | main.log.error( self.name + ": Error in adding mpls intent" ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2050 | return None |
| 2051 | else: |
| 2052 | # TODO: print out all the options in this message? |
| 2053 | main.log.info( "MPLS intent installed between " + |
| 2054 | str( ingressDevice ) + " and " + |
| 2055 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2056 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2057 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2058 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2059 | else: |
| 2060 | main.log.error( "Error, intent ID not found" ) |
| 2061 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2062 | except AssertionError: |
| 2063 | main.log.exception( "" ) |
| 2064 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2065 | except TypeError: |
| 2066 | main.log.exception( self.name + ": Object not as expected" ) |
| 2067 | return None |
| 2068 | except pexpect.EOF: |
| 2069 | main.log.error( self.name + ": EOF exception found" ) |
| 2070 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2071 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2072 | except Exception: |
| 2073 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2074 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2075 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2076 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 2077 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2078 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2079 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2080 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2081 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2082 | -p or --purge: Purge the intent from the store after removal |
| 2083 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2084 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 2085 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2086 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2087 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2088 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2089 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2090 | if purge: |
| 2091 | cmdStr += " -p" |
| 2092 | if sync: |
| 2093 | cmdStr += " -s" |
| 2094 | |
| 2095 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2096 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2097 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2098 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2099 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2100 | main.log.error( self.name + ": Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2101 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2102 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2103 | # TODO: Should this be main.TRUE |
| 2104 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2105 | except AssertionError: |
| 2106 | main.log.exception( "" ) |
| 2107 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2108 | except TypeError: |
| 2109 | main.log.exception( self.name + ": Object not as expected" ) |
| 2110 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2111 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2112 | main.log.error( self.name + ": EOF exception found" ) |
| 2113 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2114 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2115 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2116 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2117 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2118 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2119 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2120 | """ |
| 2121 | Description: |
| 2122 | Remove all the intents |
| 2123 | Optional args:- |
| 2124 | -s or --sync: Waits for the removal before returning |
| 2125 | -p or --purge: Purge the intent from the store after removal |
| 2126 | Returns: |
| 2127 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2128 | main.FALSE; Returns None for exception |
| 2129 | """ |
| 2130 | try: |
| 2131 | cmdStr = "remove-intent" |
| 2132 | if purge: |
| 2133 | cmdStr += " -p" |
| 2134 | if sync: |
| 2135 | cmdStr += " -s" |
| 2136 | |
| 2137 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2138 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2139 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2140 | assert "Command not found:" not in handle, handle |
| 2141 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2142 | main.log.error( self.name + ": Error in removing intent" ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2143 | return main.FALSE |
| 2144 | else: |
| 2145 | return main.TRUE |
| 2146 | except AssertionError: |
| 2147 | main.log.exception( "" ) |
| 2148 | return None |
| 2149 | except TypeError: |
| 2150 | main.log.exception( self.name + ": Object not as expected" ) |
| 2151 | return None |
| 2152 | except pexpect.EOF: |
| 2153 | main.log.error( self.name + ": EOF exception found" ) |
| 2154 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2155 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2156 | except Exception: |
| 2157 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2158 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2159 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2160 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2161 | """ |
| 2162 | Purges all WITHDRAWN Intents |
| 2163 | """ |
| 2164 | try: |
| 2165 | cmdStr = "purge-intents" |
| 2166 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2167 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2168 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2169 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2170 | main.log.error( self.name + ": Error in purging intents" ) |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2171 | return main.FALSE |
| 2172 | else: |
| 2173 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2174 | except AssertionError: |
| 2175 | main.log.exception( "" ) |
| 2176 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2177 | except TypeError: |
| 2178 | main.log.exception( self.name + ": Object not as expected" ) |
| 2179 | return None |
| 2180 | except pexpect.EOF: |
| 2181 | main.log.error( self.name + ": EOF exception found" ) |
| 2182 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2183 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2184 | except Exception: |
| 2185 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2186 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2187 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2188 | def wipeout( self ): |
| 2189 | """ |
| 2190 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2191 | """ |
| 2192 | try: |
| 2193 | cmdStr = "wipe-out please" |
| 2194 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2195 | assert handle is not None, "Error in sendline" |
| 2196 | assert "Command not found:" not in handle, handle |
| 2197 | return main.TRUE |
| 2198 | except AssertionError: |
| 2199 | main.log.exception( "" ) |
| 2200 | return None |
| 2201 | except TypeError: |
| 2202 | main.log.exception( self.name + ": Object not as expected" ) |
| 2203 | return None |
| 2204 | except pexpect.EOF: |
| 2205 | main.log.error( self.name + ": EOF exception found" ) |
| 2206 | main.log.error( self.name + ": " + self.handle.before ) |
| 2207 | main.cleanAndExit() |
| 2208 | except Exception: |
| 2209 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2210 | main.cleanAndExit() |
| 2211 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2212 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2213 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2214 | NOTE: This method should be used after installing application: |
| 2215 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2216 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2217 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2218 | Description: |
| 2219 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2220 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2221 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2222 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2223 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2224 | cmdStr += " -j" |
| 2225 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2226 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2227 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2228 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2229 | except AssertionError: |
| 2230 | main.log.exception( "" ) |
| 2231 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2232 | except TypeError: |
| 2233 | main.log.exception( self.name + ": Object not as expected" ) |
| 2234 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2235 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2236 | main.log.error( self.name + ": EOF exception found" ) |
| 2237 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2238 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2239 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2240 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2241 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2242 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2243 | def ipv4RouteNumber( self ): |
| 2244 | """ |
| 2245 | NOTE: This method should be used after installing application: |
| 2246 | onos-app-sdnip |
| 2247 | Description: |
| 2248 | Obtain the total IPv4 routes number in the system |
| 2249 | """ |
| 2250 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2251 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2252 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2253 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2254 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2255 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2256 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2257 | except AssertionError: |
| 2258 | main.log.exception( "" ) |
| 2259 | return None |
| 2260 | except ( TypeError, ValueError ): |
| 2261 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2262 | return None |
| 2263 | except pexpect.EOF: |
| 2264 | main.log.error( self.name + ": EOF exception found" ) |
| 2265 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2266 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2267 | except Exception: |
| 2268 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2269 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2270 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2271 | # =============Function to check Bandwidth allocation======== |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2272 | def allocations( self, jsonFormat = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2273 | """ |
| 2274 | Description: |
| 2275 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2276 | """ |
| 2277 | try: |
| 2278 | cmdStr = "allocations" |
| 2279 | if jsonFormat: |
| 2280 | cmdStr += " -j" |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2281 | handle = self.sendline( cmdStr, timeout=300 ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2282 | assert handle is not None, "Error in sendline" |
| 2283 | assert "Command not found:" not in handle, handle |
| 2284 | return handle |
| 2285 | except AssertionError: |
| 2286 | main.log.exception( "" ) |
| 2287 | return None |
| 2288 | except ( TypeError, ValueError ): |
| 2289 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2290 | return None |
| 2291 | except pexpect.EOF: |
| 2292 | main.log.error( self.name + ": EOF exception found" ) |
| 2293 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2294 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2295 | except Exception: |
| 2296 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2297 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2298 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2299 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2300 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2301 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2302 | Obtain intents from the ONOS cli. |
| 2303 | Optional: |
| 2304 | * jsonFormat: Enable output formatting in json, default to True |
| 2305 | * summary: Whether only output the intent summary, defaults to False |
| 2306 | * type: Only output a certain type of intent. This options is valid |
| 2307 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2308 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2309 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2310 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2311 | if summary: |
| 2312 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2313 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2314 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2315 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2316 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2317 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2318 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2319 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2320 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2321 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2322 | intentType = "" |
| 2323 | # IF we want the summary of a specific intent type |
| 2324 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2325 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2326 | if intentType in jsonResult.keys(): |
| 2327 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2328 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2329 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2330 | return handle |
| 2331 | else: |
| 2332 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2333 | except AssertionError: |
| 2334 | main.log.exception( "" ) |
| 2335 | return None |
| 2336 | except ( TypeError, ValueError ): |
| 2337 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2338 | return None |
| 2339 | except pexpect.EOF: |
| 2340 | main.log.error( self.name + ": EOF exception found" ) |
| 2341 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2342 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2343 | except Exception: |
| 2344 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2345 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2346 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2347 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2348 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2349 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2350 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2351 | list of intent IDs. |
| 2352 | Parameters: |
| 2353 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2354 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2355 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2356 | 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] | 2357 | accepted. |
| 2358 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2359 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2360 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2361 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2362 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2363 | try: |
| 2364 | state = "State is Undefined" |
| 2365 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2366 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2367 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2368 | rawJson = intentsJson |
| 2369 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2370 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2371 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2372 | if intentsId == intent[ 'id' ]: |
| 2373 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2374 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2375 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2376 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2377 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2378 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2379 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2380 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2381 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2382 | for intent in parsedIntentsJson: |
| 2383 | if intentsId[ i ] == intent[ 'id' ]: |
| 2384 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2385 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2386 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2387 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2388 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2389 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2390 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2391 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2392 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2393 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2394 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2395 | except ( TypeError, ValueError ): |
| 2396 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2397 | return None |
| 2398 | except pexpect.EOF: |
| 2399 | main.log.error( self.name + ": EOF exception found" ) |
| 2400 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2401 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2402 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2403 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2404 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2405 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2406 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2407 | """ |
| 2408 | Description: |
| 2409 | Check intents state |
| 2410 | Required: |
| 2411 | intentsId - List of intents ID to be checked |
| 2412 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2413 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2414 | state in the list. |
| 2415 | *NOTE: You can pass in a list of expected state, |
| 2416 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2417 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2418 | Returns main.TRUE only if all intent are the same as expected states, |
| 2419 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2420 | """ |
| 2421 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2422 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2423 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2424 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2425 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2426 | intentsDict = [] |
| 2427 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2428 | if isinstance( intentsId, types.StringType ) \ |
| 2429 | and intent.get( 'id' ) == intentsId: |
| 2430 | intentsDict.append( intent ) |
| 2431 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2432 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2433 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2434 | |
| 2435 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2436 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2437 | "getting intents state" ) |
| 2438 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2439 | |
| 2440 | if isinstance( expectedState, types.StringType ): |
| 2441 | for intents in intentsDict: |
| 2442 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2443 | main.log.debug( self.name + " : Intent ID - " + |
| 2444 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2445 | " actual state = " + |
| 2446 | intents.get( 'state' ) |
| 2447 | + " does not equal expected state = " |
| 2448 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2449 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2450 | elif isinstance( expectedState, types.ListType ): |
| 2451 | for intents in intentsDict: |
| 2452 | if not any( state == intents.get( 'state' ) for state in |
| 2453 | expectedState ): |
| 2454 | main.log.debug( self.name + " : Intent ID - " + |
| 2455 | intents.get( 'id' ) + |
| 2456 | " actual state = " + |
| 2457 | intents.get( 'state' ) + |
| 2458 | " does not equal expected states = " |
| 2459 | + str( expectedState ) ) |
| 2460 | returnValue = main.FALSE |
| 2461 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2462 | if returnValue == main.TRUE: |
| 2463 | main.log.info( self.name + ": All " + |
| 2464 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2465 | " intents are in " + str( expectedState ) + |
| 2466 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2467 | return returnValue |
| 2468 | except TypeError: |
| 2469 | main.log.exception( self.name + ": Object not as expected" ) |
| 2470 | return None |
| 2471 | except pexpect.EOF: |
| 2472 | main.log.error( self.name + ": EOF exception found" ) |
| 2473 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2474 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2475 | except Exception: |
| 2476 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2477 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2478 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2479 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2480 | """ |
| 2481 | Description: |
| 2482 | Compare the allocated bandwidth with the given allocations |
| 2483 | Required: |
| 2484 | expectedAllocations - The expected ONOS output of the allocations command |
| 2485 | Return: |
| 2486 | Returns main.TRUE only if all intent are the same as expected states, |
| 2487 | otherwise returns main.FALSE. |
| 2488 | """ |
| 2489 | # FIXME: Convert these string comparisons to object comparisons |
| 2490 | try: |
| 2491 | returnValue = main.TRUE |
| 2492 | bandwidthFailed = False |
| 2493 | rawAlloc = self.allocations() |
| 2494 | expectedFormat = StringIO( expectedAllocations ) |
| 2495 | ONOSOutput = StringIO( rawAlloc ) |
| 2496 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2497 | str( expectedFormat ) ) ) |
| 2498 | |
| 2499 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2500 | actual = actual.rstrip() |
| 2501 | expected = expected.rstrip() |
| 2502 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2503 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2504 | marker1 = actual.find( 'allocated' ) |
| 2505 | m1 = actual[ :marker1 ] |
| 2506 | marker2 = expected.find( 'allocated' ) |
| 2507 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2508 | if m1 != m2: |
| 2509 | bandwidthFailed = True |
| 2510 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2511 | bandwidthFailed = True |
| 2512 | expectedFormat.close() |
| 2513 | ONOSOutput.close() |
| 2514 | |
| 2515 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2516 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2517 | returnValue = main.FALSE |
| 2518 | return returnValue |
| 2519 | except TypeError: |
| 2520 | main.log.exception( self.name + ": Object not as expected" ) |
| 2521 | return None |
| 2522 | except pexpect.EOF: |
| 2523 | main.log.error( self.name + ": EOF exception found" ) |
| 2524 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2525 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2526 | except Exception: |
| 2527 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2528 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2529 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2530 | def compareIntent( self, intentDict ): |
| 2531 | """ |
| 2532 | Description: |
| 2533 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2534 | Return: |
| 2535 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2536 | Arguments: |
| 2537 | intentDict: a dictionary which maps intent ids to intent states |
| 2538 | """ |
| 2539 | try: |
| 2540 | intentsRaw = self.intents() |
| 2541 | intentsJson = json.loads( intentsRaw ) |
| 2542 | intentDictONOS = {} |
| 2543 | for intent in intentsJson: |
| 2544 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2545 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2546 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2547 | 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] | 2548 | str( len( intentDict ) ) + " expected and " + |
| 2549 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2550 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2551 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2552 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2553 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2554 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2555 | else: |
| 2556 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2557 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2558 | " expected state is " + intentDict[ intentID ] + |
| 2559 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2560 | returnValue = main.FALSE |
| 2561 | intentDictONOS.pop( intentID ) |
| 2562 | if len( intentDictONOS ) > 0: |
| 2563 | returnValue = main.FALSE |
| 2564 | for intentID in intentDictONOS.keys(): |
| 2565 | 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] | 2566 | if returnValue == main.TRUE: |
| 2567 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2568 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2569 | except KeyError: |
| 2570 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2571 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2572 | except ( TypeError, ValueError ): |
| 2573 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2574 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2575 | except pexpect.EOF: |
| 2576 | main.log.error( self.name + ": EOF exception found" ) |
| 2577 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2578 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2579 | except Exception: |
| 2580 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2581 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2582 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2583 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2584 | """ |
| 2585 | Description: |
| 2586 | Check the number of installed intents. |
| 2587 | Optional: |
| 2588 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2589 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2590 | Return: |
| 2591 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2592 | , otherwise, returns main.FALSE. |
| 2593 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2594 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2595 | try: |
| 2596 | cmd = "intents -s -j" |
| 2597 | |
| 2598 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2599 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2600 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2601 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2602 | response = json.loads( response ) |
| 2603 | |
| 2604 | # get total and installed number, see if they are match |
| 2605 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2606 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2607 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2608 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2609 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2610 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2611 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2612 | return main.FALSE |
| 2613 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2614 | except ( TypeError, ValueError ): |
| 2615 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2616 | return None |
| 2617 | except pexpect.EOF: |
| 2618 | main.log.error( self.name + ": EOF exception found" ) |
| 2619 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2620 | if noExit: |
| 2621 | return main.FALSE |
| 2622 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2623 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2624 | except pexpect.TIMEOUT: |
| 2625 | main.log.error( self.name + ": ONOS timeout" ) |
| 2626 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2627 | except Exception: |
| 2628 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2629 | if noExit: |
| 2630 | return main.FALSE |
| 2631 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2632 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2633 | |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2634 | 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] | 2635 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2636 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2637 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2638 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2639 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2640 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2641 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2642 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2643 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2644 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2645 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2646 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2647 | cmdStr += " -n" |
| 2648 | cmdStr += " " + state |
| 2649 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2650 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2651 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2652 | assert "Command not found:" not in handle, handle |
| 2653 | if re.search( "Error:", handle ): |
| 2654 | main.log.error( self.name + ": flows() response: " + |
| 2655 | str( handle ) ) |
| 2656 | return handle |
| 2657 | except AssertionError: |
| 2658 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2659 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2660 | except TypeError: |
| 2661 | main.log.exception( self.name + ": Object not as expected" ) |
| 2662 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2663 | except pexpect.TIMEOUT: |
| 2664 | main.log.error( self.name + ": ONOS timeout" ) |
| 2665 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2666 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2667 | main.log.error( self.name + ": EOF exception found" ) |
| 2668 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2669 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2670 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2671 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2672 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2673 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2674 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2675 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2676 | count = int( count ) if count else 0 |
steven30801 | eccfe21 | 2019-01-24 13:00:42 +0800 | [diff] [blame] | 2677 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2678 | return count if ( count > min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2679 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2680 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2681 | """ |
| 2682 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2683 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2684 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2685 | if the count of those states is 0, which means all current flows |
| 2686 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2687 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2688 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2689 | Return: |
| 2690 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2691 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2692 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2693 | """ |
| 2694 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2695 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2696 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2697 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2698 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2699 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2700 | if rawFlows: |
| 2701 | # if we didn't get flows or flows function return None, we should return |
| 2702 | # main.Flase |
| 2703 | checkedStates.append( json.loads( rawFlows ) ) |
| 2704 | else: |
| 2705 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2706 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2707 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2708 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2709 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2710 | except TypeError: |
| 2711 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2712 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2713 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2714 | # We want to count PENDING_ADD if isPENDING is true |
| 2715 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2716 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2717 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2718 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2719 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2720 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2721 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2722 | except ( TypeError, ValueError ): |
| 2723 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2724 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2725 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2726 | except AssertionError: |
| 2727 | main.log.exception( "" ) |
| 2728 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2729 | except pexpect.TIMEOUT: |
| 2730 | main.log.error( self.name + ": ONOS timeout" ) |
| 2731 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2732 | except pexpect.EOF: |
| 2733 | main.log.error( self.name + ": EOF exception found" ) |
| 2734 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2735 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2736 | except Exception: |
| 2737 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2738 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2739 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2740 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2741 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2742 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2743 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2744 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2745 | a specific point-to-point intent definition |
| 2746 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2747 | * ingress: specify source dpid |
| 2748 | * egress: specify destination dpid |
| 2749 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2750 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2751 | * offset: the keyOffset is where the next batch of intents |
| 2752 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2753 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2754 | * getResponse: If set to True, function will return ONOS response. |
| 2755 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2756 | Returns: If failed to push test intents, it will returen None, |
| 2757 | if successful, return true. |
| 2758 | Timeout expection will return None, |
| 2759 | TypeError will return false |
| 2760 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2761 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2762 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2763 | if background: |
| 2764 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2765 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2766 | back = "" |
| 2767 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2768 | ingress, |
| 2769 | egress, |
| 2770 | batchSize, |
| 2771 | offset, |
| 2772 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2773 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2774 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2775 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2776 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2777 | if getResponse: |
| 2778 | return response |
| 2779 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2780 | # TODO: We should handle if there is failure in installation |
| 2781 | return main.TRUE |
| 2782 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2783 | except AssertionError: |
| 2784 | main.log.exception( "" ) |
| 2785 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2786 | except pexpect.TIMEOUT: |
| 2787 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2788 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2789 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2790 | main.log.error( self.name + ": EOF exception found" ) |
| 2791 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2792 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2793 | except TypeError: |
| 2794 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2795 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2796 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2797 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2798 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2799 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2800 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2801 | """ |
| 2802 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2803 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2804 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2805 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2806 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2807 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2808 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2809 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2810 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2811 | cmd = "flows -c added" |
| 2812 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2813 | if rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2814 | rawFlows = rawFlows.split( "\n" ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2815 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2816 | for l in rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2817 | totalFlows += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2818 | else: |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 2819 | main.log.warn( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2820 | return None |
| 2821 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2822 | |
You Wang | d3097f7 | 2018-12-12 11:56:03 -0800 | [diff] [blame] | 2823 | except IndexError: |
| 2824 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
| 2825 | main.log.debug( "rawFlows: {}".format( rawFlows ) ) |
| 2826 | return None |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2827 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2828 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2829 | return None |
| 2830 | except pexpect.EOF: |
| 2831 | main.log.error( self.name + ": EOF exception found" ) |
| 2832 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2833 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2834 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2835 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2836 | except pexpect.TIMEOUT: |
| 2837 | main.log.error( self.name + ": ONOS timeout" ) |
| 2838 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2839 | except Exception: |
| 2840 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2841 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2842 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2843 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2844 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2845 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2846 | """ |
| 2847 | Description: |
| 2848 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2849 | Optional: |
| 2850 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2851 | Return: |
| 2852 | The number of intents |
| 2853 | """ |
| 2854 | try: |
| 2855 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2856 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2857 | if response is None: |
| 2858 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2859 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2860 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2861 | except ( TypeError, ValueError ): |
| 2862 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2863 | return None |
| 2864 | except pexpect.EOF: |
| 2865 | main.log.error( self.name + ": EOF exception found" ) |
| 2866 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2867 | if noExit: |
| 2868 | return -1 |
| 2869 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2870 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2871 | except Exception: |
| 2872 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2873 | if noExit: |
| 2874 | return -1 |
| 2875 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2876 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2877 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2878 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2879 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2880 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2881 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2882 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2883 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2884 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2885 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2886 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2887 | cmdStr += " -j" |
| 2888 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2889 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2890 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2891 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2892 | except AssertionError: |
| 2893 | main.log.exception( "" ) |
| 2894 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2895 | except TypeError: |
| 2896 | main.log.exception( self.name + ": Object not as expected" ) |
| 2897 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2898 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2899 | main.log.error( self.name + ": EOF exception found" ) |
| 2900 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2901 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2902 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2903 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2904 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2905 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2906 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2907 | """ |
| 2908 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2909 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2910 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2911 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2912 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2913 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2914 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2915 | cmdStr += " -j" |
| 2916 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2917 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2918 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2919 | if handle: |
| 2920 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2921 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2922 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2923 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2924 | else: |
| 2925 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2926 | except AssertionError: |
| 2927 | main.log.exception( "" ) |
| 2928 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2929 | except TypeError: |
| 2930 | main.log.exception( self.name + ": Object not as expected" ) |
| 2931 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2932 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2933 | main.log.error( self.name + ": EOF exception found" ) |
| 2934 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2935 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2936 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2937 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2938 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2939 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2940 | # Wrapper functions **************** |
| 2941 | # Wrapper functions use existing driver |
| 2942 | # functions and extends their use case. |
| 2943 | # For example, we may use the output of |
| 2944 | # a normal driver function, and parse it |
| 2945 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2946 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2947 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2948 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2949 | Description: |
| 2950 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2951 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2952 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2953 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2954 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 2955 | if intentsStr is None: |
| 2956 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2957 | # Convert to a dictionary |
| 2958 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2959 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2960 | for intent in intents: |
| 2961 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2962 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2963 | except TypeError: |
| 2964 | main.log.exception( self.name + ": Object not as expected" ) |
| 2965 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2966 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2967 | main.log.error( self.name + ": EOF exception found" ) |
| 2968 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2969 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2970 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2971 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2972 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2973 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2974 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2975 | """ |
| 2976 | Determine the number of flow rules for the given device id that are |
| 2977 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2978 | Params: |
| 2979 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2980 | """ |
| 2981 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2982 | if core: |
| 2983 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2984 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2985 | else: |
| 2986 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2987 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 2988 | handle = self.lineCount( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2989 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2990 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2991 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2992 | except AssertionError: |
| 2993 | main.log.exception( "" ) |
| 2994 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2995 | except pexpect.EOF: |
| 2996 | main.log.error( self.name + ": EOF exception found" ) |
| 2997 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2998 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2999 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3000 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3001 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 3002 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3003 | def groupAddedCount( self, deviceId, core=False ): |
| 3004 | """ |
| 3005 | Determine the number of group rules for the given device id that are |
| 3006 | in the added state |
| 3007 | Params: |
| 3008 | core: if True, only return the number of core groups added |
| 3009 | """ |
| 3010 | try: |
| 3011 | if core: |
| 3012 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3013 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3014 | else: |
| 3015 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3016 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3017 | handle = self.lineCount( cmdStr ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3018 | assert handle is not None, "Error in sendline" |
| 3019 | assert "Command not found:" not in handle, handle |
| 3020 | return handle |
| 3021 | except AssertionError: |
| 3022 | main.log.exception( "" ) |
| 3023 | return None |
| 3024 | except pexpect.EOF: |
| 3025 | main.log.error( self.name + ": EOF exception found" ) |
| 3026 | main.log.error( self.name + ": " + self.handle.before ) |
| 3027 | main.cleanAndExit() |
| 3028 | except Exception: |
| 3029 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3030 | main.cleanAndExit() |
| 3031 | |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3032 | def addStaticRoute( self, subnet, intf): |
| 3033 | """ |
| 3034 | Adds a static route to onos. |
| 3035 | Params: |
| 3036 | subnet: The subnet reaching through this route |
| 3037 | intf: The interface this route is reachable through |
| 3038 | """ |
| 3039 | try: |
| 3040 | cmdStr = "route-add " + subnet + " " + intf |
| 3041 | handle = self.sendline( cmdStr ) |
| 3042 | assert handle is not None, "Error in sendline" |
| 3043 | assert "Command not found:" not in handle, handle |
| 3044 | return handle |
| 3045 | except AssertionError: |
| 3046 | main.log.exception( "" ) |
| 3047 | return None |
| 3048 | except pexpect.EOF: |
| 3049 | main.log.error( self.name + ": EOF exception found" ) |
| 3050 | main.log.error( self.name + ": " + self.handle.before ) |
| 3051 | main.cleanAndExit() |
| 3052 | except Exception: |
| 3053 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3054 | main.cleanAndExit() |
| 3055 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3056 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 3057 | """ |
| 3058 | Description: |
| 3059 | Check whether the number of groups for the given device id that |
| 3060 | are in ADDED state is bigger than minGroupCount. |
| 3061 | Required: |
| 3062 | * deviceId: device id to check the number of added group rules |
| 3063 | Optional: |
| 3064 | * minGroupCount: the number of groups to compare |
| 3065 | * core: if True, only check the number of core groups added |
| 3066 | * comparison: if 0, compare with greater than minFlowCount |
| 3067 | * if 1, compare with equal to minFlowCount |
| 3068 | Return: |
| 3069 | Returns the number of groups if it is bigger than minGroupCount, |
| 3070 | returns main.FALSE otherwise. |
| 3071 | """ |
| 3072 | count = self.groupAddedCount( deviceId, core ) |
| 3073 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3074 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3075 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 3076 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3077 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3078 | """ |
| 3079 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3080 | deviceId: Id of the device from which we retrieve groups |
| 3081 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3082 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3083 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3084 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 3085 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3086 | assert handle is not None, "Error in sendline" |
| 3087 | assert "Command not found:" not in handle, handle |
| 3088 | return handle |
| 3089 | except AssertionError: |
| 3090 | main.log.exception( "" ) |
| 3091 | return None |
| 3092 | except TypeError: |
| 3093 | main.log.exception( self.name + ": Object not as expected" ) |
| 3094 | return None |
| 3095 | except pexpect.EOF: |
| 3096 | main.log.error( self.name + ": EOF exception found" ) |
| 3097 | main.log.error( self.name + ": " + self.handle.before ) |
| 3098 | main.cleanAndExit() |
| 3099 | except Exception: |
| 3100 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3101 | main.cleanAndExit() |
| 3102 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3103 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3104 | """ |
| 3105 | Description: |
| 3106 | Check whether the number of flow rules for the given device id that |
| 3107 | are in ADDED state is bigger than minFlowCount. |
| 3108 | Required: |
| 3109 | * deviceId: device id to check the number of added flow rules |
| 3110 | Optional: |
| 3111 | * minFlowCount: the number of flow rules to compare |
| 3112 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3113 | * comparison: if 0, compare with greater than minFlowCount |
| 3114 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3115 | Return: |
| 3116 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 3117 | returns main.FALSE otherwise. |
| 3118 | """ |
| 3119 | count = self.flowAddedCount( deviceId, core ) |
| 3120 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3121 | main.log.debug( "found {} flows".format( count ) ) |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3122 | 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] | 3123 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3124 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3125 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3126 | Use 'devices' function to obtain list of all devices |
| 3127 | and parse the result to obtain a list of all device |
| 3128 | id's. Returns this list. Returns empty list if no |
| 3129 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3130 | List is ordered sequentially |
| 3131 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3132 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3133 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3134 | the ids. By obtaining the list of device ids on the fly, |
| 3135 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3136 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3137 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3138 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3139 | devicesStr = self.devices( jsonFormat=False ) |
| 3140 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3141 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3142 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3143 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3144 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3145 | |
| 3146 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3147 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3148 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3149 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3150 | # Split list further into arguments before and after string |
| 3151 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3152 | # append to idList |
| 3153 | for arg in tempList: |
| 3154 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3155 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3156 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3157 | except TypeError: |
| 3158 | main.log.exception( self.name + ": Object not as expected" ) |
| 3159 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3160 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3161 | main.log.error( self.name + ": EOF exception found" ) |
| 3162 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3163 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3164 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3165 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3166 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3167 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3168 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3169 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3170 | Uses 'nodes' function to obtain list of all nodes |
| 3171 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3172 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3173 | Returns: |
| 3174 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3175 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3176 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3177 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3178 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3179 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3180 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3181 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3182 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3183 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3184 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3185 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3186 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3187 | except ( TypeError, ValueError ): |
| 3188 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3189 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3190 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3191 | main.log.error( self.name + ": EOF exception found" ) |
| 3192 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3193 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3194 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3195 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3196 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3197 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3198 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3199 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3200 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3201 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3202 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3203 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3204 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3205 | return None |
| 3206 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3207 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3208 | rawDevices = self.devices() |
| 3209 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3210 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3211 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3212 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3213 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3214 | return device |
| 3215 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3216 | except ( TypeError, ValueError ): |
| 3217 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3218 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3219 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3220 | main.log.error( self.name + ": EOF exception found" ) |
| 3221 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3222 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3223 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3224 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3225 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3226 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3227 | def getTopology( self, topologyOutput ): |
| 3228 | """ |
| 3229 | Definition: |
| 3230 | Loads a json topology output |
| 3231 | Return: |
| 3232 | topology = current ONOS topology |
| 3233 | """ |
| 3234 | import json |
| 3235 | try: |
| 3236 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3237 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3238 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3239 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3240 | except ( TypeError, ValueError ): |
| 3241 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3242 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3243 | except pexpect.EOF: |
| 3244 | main.log.error( self.name + ": EOF exception found" ) |
| 3245 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3246 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3247 | except Exception: |
| 3248 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3249 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3250 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3251 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3252 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3253 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3254 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3255 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3256 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3257 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3258 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3259 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3260 | logLevel = level to log to. |
| 3261 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3262 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3263 | Returns: main.TRUE if the number of switches and links are correct, |
| 3264 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3265 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3266 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3267 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3268 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3269 | summary = self.summary() |
| 3270 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3271 | except ( TypeError, ValueError ): |
| 3272 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3273 | return main.ERROR |
| 3274 | try: |
| 3275 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3276 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3277 | return main.ERROR |
| 3278 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3279 | # Is the number of switches is what we expected |
| 3280 | devices = topology.get( 'devices', False ) |
| 3281 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3282 | nodes = summary.get( 'nodes', False ) |
| 3283 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3284 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3285 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3286 | # Is the number of links is what we expected |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3287 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3288 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 3289 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3290 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3291 | output = output + "The number of links and switches match "\ |
| 3292 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3293 | result = main.TRUE |
| 3294 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3295 | output = output + \ |
| 3296 | "The number of links and switches does not match " + \ |
| 3297 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3298 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3299 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 3300 | output = output + " (%i expected) " % int( numoswitch ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3301 | if int( numolink ) > 0: |
| 3302 | output = output + "and %i links " % int( links ) |
| 3303 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 3304 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3305 | output = output + "and %i controllers " % int( nodes ) |
| 3306 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3307 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3308 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3309 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3310 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3311 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3312 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3313 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3314 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3315 | main.log.error( self.name + ": EOF exception found" ) |
| 3316 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3317 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3318 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3319 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3320 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3321 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3322 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3323 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3324 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3325 | deviceId must be the id of a device as seen in the onos devices command |
| 3326 | 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] | 3327 | role must be either master, standby, or none |
| 3328 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3329 | Returns: |
| 3330 | main.TRUE or main.FALSE based on argument verification and |
| 3331 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3332 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3333 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3334 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3335 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3336 | cmdStr = "device-role " +\ |
| 3337 | str( deviceId ) + " " +\ |
| 3338 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3339 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3340 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3341 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3342 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3343 | if re.search( "Error", handle ): |
| 3344 | # end color output to escape any colours |
| 3345 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3346 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3347 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3348 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3349 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3350 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3351 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3352 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3353 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3354 | except AssertionError: |
| 3355 | main.log.exception( "" ) |
| 3356 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3357 | except TypeError: |
| 3358 | main.log.exception( self.name + ": Object not as expected" ) |
| 3359 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3360 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3361 | main.log.error( self.name + ": EOF exception found" ) |
| 3362 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3363 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3364 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3365 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3366 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3367 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3368 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3369 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3370 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3371 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3372 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3373 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3374 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3375 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3376 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3377 | cmdStr += " -j" |
| 3378 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3379 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3380 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3381 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3382 | except AssertionError: |
| 3383 | main.log.exception( "" ) |
| 3384 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3385 | except TypeError: |
| 3386 | main.log.exception( self.name + ": Object not as expected" ) |
| 3387 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3388 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3389 | main.log.error( self.name + ": EOF exception found" ) |
| 3390 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3391 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3392 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3393 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3394 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3395 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3396 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3397 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3398 | CLI command to get the current leader for the Election test application |
| 3399 | NOTE: Requires installation of the onos-app-election feature |
| 3400 | Returns: Node IP of the leader if one exists |
| 3401 | None if none exists |
| 3402 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3403 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3404 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3405 | cmdStr = "election-test-leader" |
| 3406 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3407 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3408 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3409 | # Leader |
| 3410 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3411 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3412 | nodeSearch = re.search( leaderPattern, response ) |
| 3413 | if nodeSearch: |
| 3414 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3415 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3416 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3417 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3418 | # no leader |
| 3419 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3420 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3421 | nullSearch = re.search( nullPattern, response ) |
| 3422 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3423 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3424 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3425 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3426 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3427 | main.log.error( self.name + ": Error in electionTestLeader on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3428 | ": " + "unexpected response" ) |
| 3429 | main.log.error( repr( response ) ) |
| 3430 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3431 | except AssertionError: |
| 3432 | main.log.exception( "" ) |
| 3433 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3434 | except TypeError: |
| 3435 | main.log.exception( self.name + ": Object not as expected" ) |
| 3436 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3437 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3438 | main.log.error( self.name + ": EOF exception found" ) |
| 3439 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3440 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3441 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3442 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3443 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3444 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3445 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3446 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3447 | CLI command to run for leadership of the Election test application. |
| 3448 | NOTE: Requires installation of the onos-app-election feature |
| 3449 | Returns: Main.TRUE on success |
| 3450 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3451 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3452 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3453 | cmdStr = "election-test-run" |
| 3454 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3455 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3456 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3457 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3458 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3459 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3460 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3461 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3462 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3463 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3464 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3465 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3466 | main.log.error( self.name + ": Error in electionTestRun on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3467 | ": " + "unexpected response" ) |
| 3468 | main.log.error( repr( response ) ) |
| 3469 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3470 | except AssertionError: |
| 3471 | main.log.exception( "" ) |
| 3472 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3473 | except TypeError: |
| 3474 | main.log.exception( self.name + ": Object not as expected" ) |
| 3475 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3476 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3477 | main.log.error( self.name + ": EOF exception found" ) |
| 3478 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3479 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3480 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3481 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3482 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3483 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3484 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3485 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3486 | * CLI command to withdraw the local node from leadership election for |
| 3487 | * the Election test application. |
| 3488 | #NOTE: Requires installation of the onos-app-election feature |
| 3489 | Returns: Main.TRUE on success |
| 3490 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3491 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3492 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3493 | cmdStr = "election-test-withdraw" |
| 3494 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3495 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3496 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3497 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3498 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3499 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3500 | if re.search( successPattern, response ): |
| 3501 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3502 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3503 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3504 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3505 | main.log.error( self.name + ": Error in electionTestWithdraw on " + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3506 | self.name + ": " + "unexpected response" ) |
| 3507 | main.log.error( repr( response ) ) |
| 3508 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3509 | except AssertionError: |
| 3510 | main.log.exception( "" ) |
| 3511 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3512 | except TypeError: |
| 3513 | main.log.exception( self.name + ": Object not as expected" ) |
| 3514 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3515 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3516 | main.log.error( self.name + ": EOF exception found" ) |
| 3517 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3518 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3519 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3520 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3521 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3522 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3523 | def getDevicePortsEnabledCount( self, dpid ): |
| 3524 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3525 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3526 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3527 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3528 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3529 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3530 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3531 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3532 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3533 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3534 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3535 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3536 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3537 | except AssertionError: |
| 3538 | main.log.exception( "" ) |
| 3539 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3540 | except TypeError: |
| 3541 | main.log.exception( self.name + ": Object not as expected" ) |
| 3542 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3543 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3544 | main.log.error( self.name + ": EOF exception found" ) |
| 3545 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3546 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3547 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3548 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3549 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3550 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3551 | def getDeviceLinksActiveCount( self, dpid ): |
| 3552 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3553 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3554 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3555 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3556 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3557 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3558 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3559 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3560 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3561 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3562 | main.log.error( self.name + ": Error in getting ports " ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3563 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3564 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3565 | except AssertionError: |
| 3566 | main.log.exception( "" ) |
| 3567 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3568 | except TypeError: |
| 3569 | main.log.exception( self.name + ": Object not as expected" ) |
| 3570 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3571 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3572 | main.log.error( self.name + ": EOF exception found" ) |
| 3573 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3574 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3575 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3576 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3577 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3578 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3579 | def getAllIntentIds( self ): |
| 3580 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3581 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3582 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3583 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3584 | cmdStr = "onos:intents | grep id=" |
| 3585 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3586 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3587 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3588 | if re.search( "Error", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3589 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3590 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3591 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3592 | except AssertionError: |
| 3593 | main.log.exception( "" ) |
| 3594 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3595 | except TypeError: |
| 3596 | main.log.exception( self.name + ": Object not as expected" ) |
| 3597 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3598 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3599 | main.log.error( self.name + ": EOF exception found" ) |
| 3600 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3601 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3602 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3603 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3604 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3605 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3606 | def intentSummary( self ): |
| 3607 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3608 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3609 | """ |
| 3610 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3611 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3612 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3613 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3614 | states.append( intent.get( 'state', None ) ) |
| 3615 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3616 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3617 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3618 | except ( TypeError, ValueError ): |
| 3619 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3620 | return None |
| 3621 | except pexpect.EOF: |
| 3622 | main.log.error( self.name + ": EOF exception found" ) |
| 3623 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3624 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3625 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3626 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3627 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3628 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3629 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3630 | """ |
| 3631 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3632 | Optional argument: |
| 3633 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3634 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3635 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3636 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3637 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3638 | cmdStr += " -j" |
| 3639 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3640 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3641 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3642 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3643 | except AssertionError: |
| 3644 | main.log.exception( "" ) |
| 3645 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3646 | except TypeError: |
| 3647 | main.log.exception( self.name + ": Object not as expected" ) |
| 3648 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3649 | except pexpect.EOF: |
| 3650 | main.log.error( self.name + ": EOF exception found" ) |
| 3651 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3652 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3653 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3654 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3655 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3656 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3657 | def leaderCandidates( self, jsonFormat=True ): |
| 3658 | """ |
| 3659 | Returns the output of the leaders -c command. |
| 3660 | Optional argument: |
| 3661 | * jsonFormat - boolean indicating if you want output in json |
| 3662 | """ |
| 3663 | try: |
| 3664 | cmdStr = "onos:leaders -c" |
| 3665 | if jsonFormat: |
| 3666 | cmdStr += " -j" |
| 3667 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3668 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3669 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3670 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3671 | except AssertionError: |
| 3672 | main.log.exception( "" ) |
| 3673 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3674 | except TypeError: |
| 3675 | main.log.exception( self.name + ": Object not as expected" ) |
| 3676 | return None |
| 3677 | except pexpect.EOF: |
| 3678 | main.log.error( self.name + ": EOF exception found" ) |
| 3679 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3680 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3681 | except Exception: |
| 3682 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3683 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3684 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3685 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3686 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3687 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3688 | topic parameter and an empty list if the topic doesn't exist |
| 3689 | If no leader is elected leader in the returned list will be "none" |
| 3690 | Returns None if there is a type error processing the json object |
| 3691 | """ |
| 3692 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3693 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3694 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3695 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3696 | assert "Command not found:" not in rawOutput, rawOutput |
| 3697 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3698 | results = [] |
| 3699 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3700 | if dict[ "topic" ] == topic: |
| 3701 | leader = dict[ "leader" ] |
| 3702 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3703 | results.append( leader ) |
| 3704 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3705 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3706 | except AssertionError: |
| 3707 | main.log.exception( "" ) |
| 3708 | return None |
| 3709 | except ( TypeError, ValueError ): |
| 3710 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3711 | return None |
| 3712 | except pexpect.EOF: |
| 3713 | main.log.error( self.name + ": EOF exception found" ) |
| 3714 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3715 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3716 | except Exception: |
| 3717 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3718 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3719 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3720 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3721 | """ |
| 3722 | Returns the output of the intent Pending map. |
| 3723 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3724 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3725 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3726 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3727 | cmdStr += " -j" |
| 3728 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3729 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3730 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3731 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3732 | except AssertionError: |
| 3733 | main.log.exception( "" ) |
| 3734 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3735 | except TypeError: |
| 3736 | main.log.exception( self.name + ": Object not as expected" ) |
| 3737 | return None |
| 3738 | except pexpect.EOF: |
| 3739 | main.log.error( self.name + ": EOF exception found" ) |
| 3740 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3741 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3742 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3743 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3744 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3745 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3746 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3747 | """ |
| 3748 | Returns the output of the raft partitions command for ONOS. |
| 3749 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3750 | # Sample JSON |
| 3751 | # { |
| 3752 | # "leader": "tcp://10.128.30.11:7238", |
| 3753 | # "members": [ |
| 3754 | # "tcp://10.128.30.11:7238", |
| 3755 | # "tcp://10.128.30.17:7238", |
| 3756 | # "tcp://10.128.30.13:7238", |
| 3757 | # ], |
| 3758 | # "name": "p1", |
| 3759 | # "term": 3 |
| 3760 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3761 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3762 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3763 | if candidates: |
| 3764 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3765 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3766 | cmdStr += " -j" |
| 3767 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3768 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3769 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3770 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3771 | except AssertionError: |
| 3772 | main.log.exception( "" ) |
| 3773 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3774 | except TypeError: |
| 3775 | main.log.exception( self.name + ": Object not as expected" ) |
| 3776 | return None |
| 3777 | except pexpect.EOF: |
| 3778 | main.log.error( self.name + ": EOF exception found" ) |
| 3779 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3780 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3781 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3782 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3783 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3784 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3785 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3786 | """ |
| 3787 | Returns the output of the apps command for ONOS. This command lists |
| 3788 | information about installed ONOS applications |
| 3789 | """ |
| 3790 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3791 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3792 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3793 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3794 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3795 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3796 | cmdStr = "onos:apps" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3797 | expectJson = False |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3798 | if summary: |
| 3799 | cmdStr += " -s" |
| 3800 | if active: |
| 3801 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3802 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3803 | cmdStr += " -j" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3804 | expectJson = True |
| 3805 | output = self.sendline( cmdStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3806 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3807 | assert "Command not found:" not in output, output |
| 3808 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3809 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3810 | # FIXME: look at specific exceptions/Errors |
| 3811 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3812 | main.log.exception( self.name + ": Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3813 | return None |
| 3814 | except TypeError: |
| 3815 | main.log.exception( self.name + ": Object not as expected" ) |
| 3816 | return None |
| 3817 | except pexpect.EOF: |
| 3818 | main.log.error( self.name + ": EOF exception found" ) |
| 3819 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3820 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3821 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3822 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3823 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3824 | |
You Wang | cdc51fe | 2018-08-12 17:14:56 -0700 | [diff] [blame] | 3825 | def appStatus( self, appName ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3826 | """ |
| 3827 | Uses the onos:apps cli command to return the status of an application. |
| 3828 | Returns: |
| 3829 | "ACTIVE" - If app is installed and activated |
| 3830 | "INSTALLED" - If app is installed and deactivated |
| 3831 | "UNINSTALLED" - If app is not installed |
| 3832 | None - on error |
| 3833 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3834 | try: |
| 3835 | if not isinstance( appName, types.StringType ): |
| 3836 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3837 | " a string" ) |
| 3838 | return None |
| 3839 | output = self.apps( jsonFormat=True ) |
| 3840 | appsJson = json.loads( output ) |
| 3841 | state = None |
| 3842 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3843 | if appName == app.get( 'name' ): |
| 3844 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3845 | break |
| 3846 | if state == "ACTIVE" or state == "INSTALLED": |
| 3847 | return state |
| 3848 | elif state is None: |
You Wang | 0d9f2c0 | 2018-08-10 14:56:32 -0700 | [diff] [blame] | 3849 | main.log.warn( "{} app not found".format( appName ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3850 | return "UNINSTALLED" |
| 3851 | elif state: |
| 3852 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3853 | str( state ) ) |
| 3854 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3855 | except ( TypeError, ValueError ): |
| 3856 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3857 | return None |
| 3858 | except pexpect.EOF: |
| 3859 | main.log.error( self.name + ": EOF exception found" ) |
| 3860 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3861 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3862 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3863 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3864 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3865 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3866 | def app( self, appName, option ): |
| 3867 | """ |
| 3868 | Interacts with the app command for ONOS. This command manages |
| 3869 | application inventory. |
| 3870 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3871 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3872 | # Validate argument types |
| 3873 | valid = True |
| 3874 | if not isinstance( appName, types.StringType ): |
| 3875 | main.log.error( self.name + ".app(): appName must be a " + |
| 3876 | "string" ) |
| 3877 | valid = False |
| 3878 | if not isinstance( option, types.StringType ): |
| 3879 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3880 | valid = False |
| 3881 | if not valid: |
| 3882 | return main.FALSE |
| 3883 | # Validate Option |
| 3884 | option = option.lower() |
| 3885 | # NOTE: Install may become a valid option |
| 3886 | if option == "activate": |
| 3887 | pass |
| 3888 | elif option == "deactivate": |
| 3889 | pass |
| 3890 | elif option == "uninstall": |
| 3891 | pass |
| 3892 | else: |
| 3893 | # Invalid option |
| 3894 | main.log.error( "The ONOS app command argument only takes " + |
| 3895 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3896 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3897 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3898 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3899 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3900 | assert output is not None, "Error in sendline" |
| 3901 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3902 | if "Error executing command" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3903 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3904 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3905 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3906 | elif "No such application" in output: |
| 3907 | main.log.error( "The application '" + appName + |
| 3908 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3909 | return main.FALSE |
| 3910 | elif "Command not found:" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3911 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3912 | str( output ) ) |
| 3913 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3914 | elif "Unsupported command:" in output: |
| 3915 | main.log.error( "Incorrect command given to 'app': " + |
| 3916 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3917 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3918 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3919 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3920 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3921 | except AssertionError: |
| 3922 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3923 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3924 | except TypeError: |
| 3925 | main.log.exception( self.name + ": Object not as expected" ) |
| 3926 | return main.ERROR |
| 3927 | except pexpect.EOF: |
| 3928 | main.log.error( self.name + ": EOF exception found" ) |
| 3929 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3930 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3931 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3932 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3933 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3934 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3935 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3936 | """ |
| 3937 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3938 | appName is the hierarchical app name, not the feature name |
| 3939 | If check is True, method will check the status of the app after the |
| 3940 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3941 | Returns main.TRUE if the command was successfully sent |
| 3942 | main.FALSE if the cli responded with an error or given |
| 3943 | incorrect input |
| 3944 | """ |
| 3945 | try: |
| 3946 | if not isinstance( appName, types.StringType ): |
| 3947 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3948 | " a string" ) |
| 3949 | return main.FALSE |
| 3950 | status = self.appStatus( appName ) |
| 3951 | if status == "INSTALLED": |
| 3952 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3953 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3954 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3955 | status = self.appStatus( appName ) |
| 3956 | if status == "ACTIVE": |
| 3957 | return main.TRUE |
| 3958 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3959 | main.log.debug( "The state of application " + |
| 3960 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3961 | time.sleep( 1 ) |
| 3962 | return main.FALSE |
| 3963 | else: # not 'check' or command didn't succeed |
| 3964 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3965 | elif status == "ACTIVE": |
| 3966 | return main.TRUE |
| 3967 | elif status == "UNINSTALLED": |
| 3968 | main.log.error( self.name + ": Tried to activate the " + |
| 3969 | "application '" + appName + "' which is not " + |
| 3970 | "installed." ) |
| 3971 | else: |
| 3972 | main.log.error( "Unexpected return value from appStatus: " + |
| 3973 | str( status ) ) |
| 3974 | return main.ERROR |
| 3975 | except TypeError: |
| 3976 | main.log.exception( self.name + ": Object not as expected" ) |
| 3977 | return main.ERROR |
| 3978 | except pexpect.EOF: |
| 3979 | main.log.error( self.name + ": EOF exception found" ) |
| 3980 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3981 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3982 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3983 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3984 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3985 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3986 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3987 | """ |
| 3988 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3989 | appName is the hierarchical app name, not the feature name |
| 3990 | If check is True, method will check the status of the app after the |
| 3991 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3992 | Returns main.TRUE if the command was successfully sent |
| 3993 | main.FALSE if the cli responded with an error or given |
| 3994 | incorrect input |
| 3995 | """ |
| 3996 | try: |
| 3997 | if not isinstance( appName, types.StringType ): |
| 3998 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3999 | "be a string" ) |
| 4000 | return main.FALSE |
| 4001 | status = self.appStatus( appName ) |
| 4002 | if status == "INSTALLED": |
| 4003 | return main.TRUE |
| 4004 | elif status == "ACTIVE": |
| 4005 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4006 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4007 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4008 | status = self.appStatus( appName ) |
| 4009 | if status == "INSTALLED": |
| 4010 | return main.TRUE |
| 4011 | else: |
| 4012 | time.sleep( 1 ) |
| 4013 | return main.FALSE |
| 4014 | else: # not check or command didn't succeed |
| 4015 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4016 | elif status == "UNINSTALLED": |
| 4017 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 4018 | "application '" + appName + "' which is not " + |
| 4019 | "installed." ) |
| 4020 | return main.TRUE |
| 4021 | else: |
| 4022 | main.log.error( "Unexpected return value from appStatus: " + |
| 4023 | str( status ) ) |
| 4024 | return main.ERROR |
| 4025 | except TypeError: |
| 4026 | main.log.exception( self.name + ": Object not as expected" ) |
| 4027 | return main.ERROR |
| 4028 | except pexpect.EOF: |
| 4029 | main.log.error( self.name + ": EOF exception found" ) |
| 4030 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4031 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4032 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4033 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4034 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4035 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4036 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4037 | """ |
| 4038 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4039 | appName is the hierarchical app name, not the feature name |
| 4040 | If check is True, method will check the status of the app after the |
| 4041 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4042 | Returns main.TRUE if the command was successfully sent |
| 4043 | main.FALSE if the cli responded with an error or given |
| 4044 | incorrect input |
| 4045 | """ |
| 4046 | # TODO: check with Thomas about the state machine for apps |
| 4047 | try: |
| 4048 | if not isinstance( appName, types.StringType ): |
| 4049 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 4050 | "be a string" ) |
| 4051 | return main.FALSE |
| 4052 | status = self.appStatus( appName ) |
| 4053 | if status == "INSTALLED": |
| 4054 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4055 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4056 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4057 | status = self.appStatus( appName ) |
| 4058 | if status == "UNINSTALLED": |
| 4059 | return main.TRUE |
| 4060 | else: |
| 4061 | time.sleep( 1 ) |
| 4062 | return main.FALSE |
| 4063 | else: # not check or command didn't succeed |
| 4064 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4065 | elif status == "ACTIVE": |
| 4066 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 4067 | "application '" + appName + "' which is " + |
| 4068 | "currently active." ) |
| 4069 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4070 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4071 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4072 | status = self.appStatus( appName ) |
| 4073 | if status == "UNINSTALLED": |
| 4074 | return main.TRUE |
| 4075 | else: |
| 4076 | time.sleep( 1 ) |
| 4077 | return main.FALSE |
| 4078 | else: # not check or command didn't succeed |
| 4079 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4080 | elif status == "UNINSTALLED": |
| 4081 | return main.TRUE |
| 4082 | else: |
| 4083 | main.log.error( "Unexpected return value from appStatus: " + |
| 4084 | str( status ) ) |
| 4085 | return main.ERROR |
| 4086 | except TypeError: |
| 4087 | main.log.exception( self.name + ": Object not as expected" ) |
| 4088 | return main.ERROR |
| 4089 | except pexpect.EOF: |
| 4090 | main.log.error( self.name + ": EOF exception found" ) |
| 4091 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4092 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4093 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4094 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4095 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4096 | |
| 4097 | def appIDs( self, jsonFormat=True ): |
| 4098 | """ |
| 4099 | Show the mappings between app id and app names given by the 'app-ids' |
| 4100 | cli command |
| 4101 | """ |
| 4102 | try: |
| 4103 | cmdStr = "app-ids" |
| 4104 | if jsonFormat: |
| 4105 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4106 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4107 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4108 | assert "Command not found:" not in output, output |
| 4109 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4110 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4111 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4112 | main.log.exception( self.name + ": Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4113 | return None |
| 4114 | except TypeError: |
| 4115 | main.log.exception( self.name + ": Object not as expected" ) |
| 4116 | return None |
| 4117 | except pexpect.EOF: |
| 4118 | main.log.error( self.name + ": EOF exception found" ) |
| 4119 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4120 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4121 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4122 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4123 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4124 | |
| 4125 | def appToIDCheck( self ): |
| 4126 | """ |
| 4127 | This method will check that each application's ID listed in 'apps' is |
| 4128 | the same as the ID listed in 'app-ids'. The check will also check that |
| 4129 | there are no duplicate IDs issued. Note that an app ID should be |
| 4130 | a globaly unique numerical identifier for app/app-like features. Once |
| 4131 | an ID is registered, the ID is never freed up so that if an app is |
| 4132 | reinstalled it will have the same ID. |
| 4133 | |
| 4134 | Returns: main.TRUE if the check passes and |
| 4135 | main.FALSE if the check fails or |
| 4136 | main.ERROR if there is some error in processing the test |
| 4137 | """ |
| 4138 | try: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4139 | # Grab IDs |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4140 | rawJson = self.appIDs( jsonFormat=True ) |
| 4141 | if rawJson: |
| 4142 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4143 | else: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4144 | main.log.error( "app-ids returned nothing: " + repr( rawJson ) ) |
| 4145 | return main.FALSE |
| 4146 | |
| 4147 | # Grab Apps |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4148 | rawJson = self.apps( jsonFormat=True ) |
| 4149 | if rawJson: |
| 4150 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4151 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4152 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4153 | return main.FALSE |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4154 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4155 | result = main.TRUE |
| 4156 | for app in apps: |
| 4157 | appID = app.get( 'id' ) |
| 4158 | if appID is None: |
| 4159 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4160 | result = main.FALSE |
| 4161 | appName = app.get( 'name' ) |
| 4162 | if appName is None: |
| 4163 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4164 | result = main.FALSE |
| 4165 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4166 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4167 | if not current: # if ids doesn't have this id |
| 4168 | result = main.FALSE |
| 4169 | main.log.error( "'app-ids' does not have the ID for " + |
| 4170 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4171 | main.log.debug( "apps command returned: " + str( app ) + |
| 4172 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4173 | elif len( current ) > 1: |
| 4174 | # there is more than one app with this ID |
| 4175 | result = main.FALSE |
| 4176 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4177 | elif not current[ 0 ][ 'name' ] == appName: |
| 4178 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4179 | result = main.FALSE |
| 4180 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4181 | " registered under id:" + str( appID ) + |
| 4182 | " but 'apps' has " + str( appName ) ) |
| 4183 | else: |
| 4184 | pass # id and name match! |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4185 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4186 | # now make sure that app-ids has no duplicates |
| 4187 | idsList = [] |
| 4188 | namesList = [] |
| 4189 | for item in ids: |
| 4190 | idsList.append( item[ 'id' ] ) |
| 4191 | namesList.append( item[ 'name' ] ) |
| 4192 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4193 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4194 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4195 | + json.dumps( ids, |
| 4196 | sort_keys=True, |
| 4197 | indent=4, |
| 4198 | separators=( ',', ': ' ) ) ) |
| 4199 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4200 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4201 | except ( TypeError, ValueError ): |
| 4202 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4203 | return main.ERROR |
| 4204 | except pexpect.EOF: |
| 4205 | main.log.error( self.name + ": EOF exception found" ) |
| 4206 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4207 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4208 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4209 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4210 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4211 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4212 | def getCfg( self, component=None, propName=None, short=False, |
| 4213 | jsonFormat=True ): |
| 4214 | """ |
| 4215 | Get configuration settings from onos cli |
| 4216 | Optional arguments: |
| 4217 | component - Optionally only list configurations for a specific |
| 4218 | component. If None, all components with configurations |
| 4219 | are displayed. Case Sensitive string. |
| 4220 | propName - If component is specified, propName option will show |
| 4221 | only this specific configuration from that component. |
| 4222 | Case Sensitive string. |
| 4223 | jsonFormat - Returns output as json. Note that this will override |
| 4224 | the short option |
| 4225 | short - Short, less verbose, version of configurations. |
| 4226 | This is overridden by the json option |
| 4227 | returns: |
| 4228 | Output from cli as a string or None on error |
| 4229 | """ |
| 4230 | try: |
| 4231 | baseStr = "cfg" |
| 4232 | cmdStr = " get" |
| 4233 | componentStr = "" |
| 4234 | if component: |
| 4235 | componentStr += " " + component |
| 4236 | if propName: |
| 4237 | componentStr += " " + propName |
| 4238 | if jsonFormat: |
| 4239 | baseStr += " -j" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4240 | expectJson = True |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4241 | elif short: |
| 4242 | baseStr += " -s" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4243 | expectJson = False |
| 4244 | output = self.sendline( baseStr + cmdStr + componentStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4245 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4246 | assert "Command not found:" not in output, output |
| 4247 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4248 | return output |
| 4249 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4250 | main.log.exception( self.name + ": Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4251 | return None |
| 4252 | except TypeError: |
| 4253 | main.log.exception( self.name + ": Object not as expected" ) |
| 4254 | return None |
| 4255 | except pexpect.EOF: |
| 4256 | main.log.error( self.name + ": EOF exception found" ) |
| 4257 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4258 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4259 | except Exception: |
| 4260 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4261 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4262 | |
| 4263 | def setCfg( self, component, propName, value=None, check=True ): |
| 4264 | """ |
| 4265 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4266 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4267 | component - The case sensitive name of the component whose |
| 4268 | property is to be set |
| 4269 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4270 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4271 | 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] | 4272 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4273 | check - Boolean, Check whether the option was successfully set this |
| 4274 | only applies when a value is given. |
| 4275 | returns: |
| 4276 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4277 | will return main.TRUE unless there is an error |
| 4278 | """ |
| 4279 | try: |
| 4280 | baseStr = "cfg" |
| 4281 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4282 | if value is not None: |
| 4283 | cmdStr += " " + str( value ) |
| 4284 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4285 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4286 | assert "Command not found:" not in output, output |
| 4287 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4288 | if value and check: |
| 4289 | results = self.getCfg( component=str( component ), |
| 4290 | propName=str( propName ), |
| 4291 | jsonFormat=True ) |
| 4292 | # Check if current value is what we just set |
| 4293 | try: |
| 4294 | jsonOutput = json.loads( results ) |
| 4295 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4296 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4297 | main.log.exception( "Error parsing cfg output" ) |
| 4298 | main.log.error( "output:" + repr( results ) ) |
| 4299 | return main.FALSE |
| 4300 | if current == str( value ): |
| 4301 | return main.TRUE |
| 4302 | return main.FALSE |
| 4303 | return main.TRUE |
| 4304 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4305 | main.log.exception( self.name + ": Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4306 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4307 | except ( TypeError, ValueError ): |
| 4308 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4309 | return main.FALSE |
| 4310 | except pexpect.EOF: |
| 4311 | main.log.error( self.name + ": EOF exception found" ) |
| 4312 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4313 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4314 | except Exception: |
| 4315 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4316 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4317 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4318 | def distPrimitivesSend( self, cmd ): |
| 4319 | """ |
| 4320 | Function to handle sending cli commands for the distributed primitives test app |
| 4321 | |
| 4322 | This command will catch some exceptions and retry the command on some |
| 4323 | specific store exceptions. |
| 4324 | |
| 4325 | Required arguments: |
| 4326 | cmd - The command to send to the cli |
| 4327 | returns: |
| 4328 | string containing the cli output |
| 4329 | None on Error |
| 4330 | """ |
| 4331 | try: |
| 4332 | output = self.sendline( cmd ) |
| 4333 | try: |
| 4334 | assert output is not None, "Error in sendline" |
| 4335 | # TODO: Maybe make this less hardcoded |
| 4336 | # ConsistentMap Exceptions |
| 4337 | assert "org.onosproject.store.service" not in output |
| 4338 | # Node not leader |
| 4339 | assert "java.lang.IllegalStateException" not in output |
| 4340 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4341 | main.log.error( self.name + ": Error in processing '" + cmd + "' " + |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4342 | "command: " + str( output ) ) |
| 4343 | retryTime = 30 # Conservative time, given by Madan |
| 4344 | main.log.info( "Waiting " + str( retryTime ) + |
| 4345 | "seconds before retrying." ) |
| 4346 | time.sleep( retryTime ) # Due to change in mastership |
| 4347 | output = self.sendline( cmd ) |
| 4348 | assert output is not None, "Error in sendline" |
| 4349 | assert "Command not found:" not in output, output |
| 4350 | assert "Error executing command" not in output, output |
| 4351 | main.log.info( self.name + ": " + output ) |
| 4352 | return output |
| 4353 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4354 | main.log.exception( self.name + ": Error in processing '" + cmd + "' command." ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4355 | return None |
| 4356 | except TypeError: |
| 4357 | main.log.exception( self.name + ": Object not as expected" ) |
| 4358 | return None |
| 4359 | except pexpect.EOF: |
| 4360 | main.log.error( self.name + ": EOF exception found" ) |
| 4361 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4362 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4363 | except Exception: |
| 4364 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4365 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4366 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4367 | def setTestAdd( self, setName, values ): |
| 4368 | """ |
| 4369 | CLI command to add elements to a distributed set. |
| 4370 | Arguments: |
| 4371 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4372 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4373 | Example usages: |
| 4374 | setTestAdd( "set1", "a b c" ) |
| 4375 | setTestAdd( "set2", "1" ) |
| 4376 | returns: |
| 4377 | main.TRUE on success OR |
| 4378 | main.FALSE if elements were already in the set OR |
| 4379 | main.ERROR on error |
| 4380 | """ |
| 4381 | try: |
| 4382 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4383 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4384 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4385 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4386 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4387 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4388 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4389 | return main.FALSE |
| 4390 | else: |
| 4391 | main.log.error( self.name + ": setTestAdd did not" + |
| 4392 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4393 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4394 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4395 | except TypeError: |
| 4396 | main.log.exception( self.name + ": Object not as expected" ) |
| 4397 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4398 | except Exception: |
| 4399 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4400 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4401 | |
| 4402 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4403 | """ |
| 4404 | CLI command to remove elements from a distributed set. |
| 4405 | Required arguments: |
| 4406 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4407 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4408 | Optional arguments: |
| 4409 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4410 | retain - Retain only the given values. (intersection of the |
| 4411 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4412 | returns: |
| 4413 | main.TRUE on success OR |
| 4414 | main.FALSE if the set was not changed OR |
| 4415 | main.ERROR on error |
| 4416 | """ |
| 4417 | try: |
| 4418 | cmdStr = "set-test-remove " |
| 4419 | if clear: |
| 4420 | cmdStr += "-c " + str( setName ) |
| 4421 | elif retain: |
| 4422 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4423 | else: |
| 4424 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4425 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4426 | if clear: |
| 4427 | pattern = "Set " + str( setName ) + " cleared" |
| 4428 | if re.search( pattern, output ): |
| 4429 | return main.TRUE |
| 4430 | elif retain: |
| 4431 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4432 | "only elements of set \[(.*)\]" |
| 4433 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4434 | "retaining only elements of the set " +\ |
| 4435 | "\[(.*)\]" |
| 4436 | if re.search( positivePattern, output ): |
| 4437 | return main.TRUE |
| 4438 | elif re.search( negativePattern, output ): |
| 4439 | return main.FALSE |
| 4440 | else: |
| 4441 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4442 | str( setName ) |
| 4443 | if ( len( values.split() ) == 1 ): |
| 4444 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4445 | str( setName ) |
| 4446 | else: |
| 4447 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4448 | str( setName ) |
| 4449 | if re.search( positivePattern, output ): |
| 4450 | return main.TRUE |
| 4451 | elif re.search( negativePattern, output ): |
| 4452 | return main.FALSE |
| 4453 | main.log.error( self.name + ": setTestRemove did not" + |
| 4454 | " match expected output" ) |
| 4455 | main.log.debug( self.name + " expected: " + pattern ) |
| 4456 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4457 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4458 | except TypeError: |
| 4459 | main.log.exception( self.name + ": Object not as expected" ) |
| 4460 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4461 | except Exception: |
| 4462 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4463 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4464 | |
| 4465 | def setTestGet( self, setName, values="" ): |
| 4466 | """ |
| 4467 | CLI command to get the elements in a distributed set. |
| 4468 | Required arguments: |
| 4469 | setName - The name of the set to remove from. |
| 4470 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4471 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4472 | returns: |
| 4473 | main.ERROR on error OR |
| 4474 | A list of elements in the set if no optional arguments are |
| 4475 | supplied OR |
| 4476 | A tuple containing the list then: |
| 4477 | main.FALSE if the given values are not in the set OR |
| 4478 | main.TRUE if the given values are in the set OR |
| 4479 | """ |
| 4480 | try: |
| 4481 | values = str( values ).strip() |
| 4482 | setName = str( setName ).strip() |
| 4483 | length = len( values.split() ) |
| 4484 | containsCheck = None |
| 4485 | # Patterns to match |
| 4486 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4487 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4488 | containsTrue = "Set " + setName + " contains the value " + values |
| 4489 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4490 | values |
| 4491 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4492 | setPattern |
| 4493 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4494 | " subset " + setPattern |
| 4495 | |
| 4496 | cmdStr = "set-test-get " |
| 4497 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4498 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4499 | if length == 0: |
| 4500 | match = re.search( pattern, output ) |
| 4501 | else: # if given values |
| 4502 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4503 | patternTrue = pattern + "\r\n" + containsTrue |
| 4504 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4505 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4506 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4507 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4508 | matchTrue = re.search( patternTrue, output ) |
| 4509 | matchFalse = re.search( patternFalse, output ) |
| 4510 | if matchTrue: |
| 4511 | containsCheck = main.TRUE |
| 4512 | match = matchTrue |
| 4513 | elif matchFalse: |
| 4514 | containsCheck = main.FALSE |
| 4515 | match = matchFalse |
| 4516 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4517 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4518 | "expected output" ) |
| 4519 | main.log.debug( self.name + " expected: " + pattern ) |
| 4520 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4521 | match = None |
| 4522 | if match: |
| 4523 | setMatch = match.group( 1 ) |
| 4524 | if setMatch == '': |
| 4525 | setList = [] |
| 4526 | else: |
| 4527 | setList = setMatch.split( ", " ) |
| 4528 | if length > 0: |
| 4529 | return ( setList, containsCheck ) |
| 4530 | else: |
| 4531 | return setList |
| 4532 | else: # no match |
| 4533 | main.log.error( self.name + ": setTestGet did not" + |
| 4534 | " match expected output" ) |
| 4535 | main.log.debug( self.name + " expected: " + pattern ) |
| 4536 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4537 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4538 | except TypeError: |
| 4539 | main.log.exception( self.name + ": Object not as expected" ) |
| 4540 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4541 | except Exception: |
| 4542 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4543 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4544 | |
| 4545 | def setTestSize( self, setName ): |
| 4546 | """ |
| 4547 | CLI command to get the elements in a distributed set. |
| 4548 | Required arguments: |
| 4549 | setName - The name of the set to remove from. |
| 4550 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4551 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4552 | None on error |
| 4553 | """ |
| 4554 | try: |
| 4555 | # TODO: Should this check against the number of elements returned |
| 4556 | # and then return true/false based on that? |
| 4557 | setName = str( setName ).strip() |
| 4558 | # Patterns to match |
| 4559 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4560 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4561 | setPattern |
| 4562 | cmdStr = "set-test-get -s " |
| 4563 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4564 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4565 | if output: |
| 4566 | match = re.search( pattern, output ) |
| 4567 | if match: |
| 4568 | setSize = int( match.group( 1 ) ) |
| 4569 | setMatch = match.group( 2 ) |
| 4570 | if len( setMatch.split() ) == setSize: |
| 4571 | main.log.info( "The size returned by " + self.name + |
| 4572 | " matches the number of elements in " + |
| 4573 | "the returned set" ) |
| 4574 | else: |
| 4575 | main.log.error( "The size returned by " + self.name + |
| 4576 | " does not match the number of " + |
| 4577 | "elements in the returned set." ) |
| 4578 | return setSize |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4579 | else: # no match |
| 4580 | main.log.error( self.name + ": setTestGet did not" + |
| 4581 | " match expected output" ) |
| 4582 | main.log.debug( self.name + " expected: " + pattern ) |
| 4583 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4584 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4585 | except TypeError: |
| 4586 | main.log.exception( self.name + ": Object not as expected" ) |
| 4587 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4588 | except Exception: |
| 4589 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4590 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4591 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4592 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4593 | """ |
| 4594 | Command to list the various counters in the system. |
| 4595 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4596 | if jsonFormat, a string of the json object returned by the cli |
| 4597 | command |
| 4598 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4599 | None on error |
| 4600 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4601 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4602 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4603 | if jsonFormat: |
| 4604 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4605 | output = self.sendline( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4606 | main.log.debug( self.name + ": Counters unparsed: " + output ) |
| 4607 | output = output.split( "\r\n" )[ -1 ] |
| 4608 | main.log.debug( self.name + ": Counters parsed: " + output ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4609 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4610 | assert "Command not found:" not in output, output |
| 4611 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4612 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4613 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4614 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4615 | main.log.exception( self.name + ": Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4616 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4617 | except TypeError: |
| 4618 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4619 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4620 | except pexpect.EOF: |
| 4621 | main.log.error( self.name + ": EOF exception found" ) |
| 4622 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4623 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4624 | except Exception: |
| 4625 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4626 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4627 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4628 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4629 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4630 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4631 | Required arguments: |
| 4632 | counter - The name of the counter to increment. |
| 4633 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4634 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4635 | returns: |
| 4636 | integer value of the counter or |
| 4637 | None on Error |
| 4638 | """ |
| 4639 | try: |
| 4640 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4641 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4642 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4643 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4644 | if delta != 1: |
| 4645 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4646 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4647 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4648 | match = re.search( pattern, output ) |
| 4649 | if match: |
| 4650 | return int( match.group( 1 ) ) |
| 4651 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4652 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4653 | " match expected output." ) |
| 4654 | main.log.debug( self.name + " expected: " + pattern ) |
| 4655 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4656 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4657 | except TypeError: |
| 4658 | main.log.exception( self.name + ": Object not as expected" ) |
| 4659 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4660 | except Exception: |
| 4661 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4662 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4663 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4664 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4665 | """ |
| 4666 | CLI command to get a distributed counter then add a delta to it. |
| 4667 | Required arguments: |
| 4668 | counter - The name of the counter to increment. |
| 4669 | Optional arguments: |
| 4670 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4671 | returns: |
| 4672 | integer value of the counter or |
| 4673 | None on Error |
| 4674 | """ |
| 4675 | try: |
| 4676 | counter = str( counter ) |
| 4677 | delta = int( delta ) |
| 4678 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4679 | cmdStr += counter |
| 4680 | if delta != 1: |
| 4681 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4682 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4683 | pattern = counter + " was updated to (-?\d+)" |
| 4684 | match = re.search( pattern, output ) |
| 4685 | if match: |
| 4686 | return int( match.group( 1 ) ) |
| 4687 | else: |
| 4688 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4689 | " match expected output." ) |
| 4690 | main.log.debug( self.name + " expected: " + pattern ) |
| 4691 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4692 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4693 | except TypeError: |
| 4694 | main.log.exception( self.name + ": Object not as expected" ) |
| 4695 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4696 | except Exception: |
| 4697 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4698 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4699 | |
| 4700 | def valueTestGet( self, valueName ): |
| 4701 | """ |
| 4702 | CLI command to get the value of an atomic value. |
| 4703 | Required arguments: |
| 4704 | valueName - The name of the value to get. |
| 4705 | returns: |
| 4706 | string value of the value or |
| 4707 | None on Error |
| 4708 | """ |
| 4709 | try: |
| 4710 | valueName = str( valueName ) |
| 4711 | cmdStr = "value-test " |
| 4712 | operation = "get" |
| 4713 | cmdStr = "value-test {} {}".format( valueName, |
| 4714 | operation ) |
| 4715 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4716 | main.log.debug( self.name + ": value test unparsed: " + output ) |
| 4717 | output = output.split( "\r\n" )[ -1 ] |
| 4718 | main.log.debug( self.name + ": value test parsed: " + output ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4719 | pattern = "(\w+)" |
| 4720 | match = re.search( pattern, output ) |
| 4721 | if match: |
| 4722 | return match.group( 1 ) |
| 4723 | else: |
| 4724 | main.log.error( self.name + ": valueTestGet did not" + |
| 4725 | " match expected output." ) |
| 4726 | main.log.debug( self.name + " expected: " + pattern ) |
| 4727 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4728 | return None |
| 4729 | except TypeError: |
| 4730 | main.log.exception( self.name + ": Object not as expected" ) |
| 4731 | return None |
| 4732 | except Exception: |
| 4733 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4734 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4735 | |
| 4736 | def valueTestSet( self, valueName, newValue ): |
| 4737 | """ |
| 4738 | CLI command to set the value of an atomic value. |
| 4739 | Required arguments: |
| 4740 | valueName - The name of the value to set. |
| 4741 | newValue - The value to assign to the given value. |
| 4742 | returns: |
| 4743 | main.TRUE on success or |
| 4744 | main.ERROR on Error |
| 4745 | """ |
| 4746 | try: |
| 4747 | valueName = str( valueName ) |
| 4748 | newValue = str( newValue ) |
| 4749 | operation = "set" |
| 4750 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4751 | operation, |
| 4752 | newValue ) |
| 4753 | output = self.distPrimitivesSend( cmdStr ) |
| 4754 | if output is not None: |
| 4755 | return main.TRUE |
| 4756 | else: |
| 4757 | return main.ERROR |
| 4758 | except TypeError: |
| 4759 | main.log.exception( self.name + ": Object not as expected" ) |
| 4760 | return main.ERROR |
| 4761 | except Exception: |
| 4762 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4763 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4764 | |
| 4765 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4766 | """ |
| 4767 | CLI command to compareAndSet the value of an atomic value. |
| 4768 | Required arguments: |
| 4769 | valueName - The name of the value. |
| 4770 | oldValue - Compare the current value of the atomic value to this |
| 4771 | newValue - If the value equals oldValue, set the value to newValue |
| 4772 | returns: |
| 4773 | main.TRUE on success or |
| 4774 | main.FALSE on failure or |
| 4775 | main.ERROR on Error |
| 4776 | """ |
| 4777 | try: |
| 4778 | valueName = str( valueName ) |
| 4779 | oldValue = str( oldValue ) |
| 4780 | newValue = str( newValue ) |
| 4781 | operation = "compareAndSet" |
| 4782 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4783 | operation, |
| 4784 | oldValue, |
| 4785 | newValue ) |
| 4786 | output = self.distPrimitivesSend( cmdStr ) |
| 4787 | pattern = "(\w+)" |
| 4788 | match = re.search( pattern, output ) |
| 4789 | if match: |
| 4790 | result = match.group( 1 ) |
| 4791 | if result == "true": |
| 4792 | return main.TRUE |
| 4793 | elif result == "false": |
| 4794 | return main.FALSE |
| 4795 | else: |
| 4796 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4797 | " match expected output." ) |
| 4798 | main.log.debug( self.name + " expected: " + pattern ) |
| 4799 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4800 | return main.ERROR |
| 4801 | except TypeError: |
| 4802 | main.log.exception( self.name + ": Object not as expected" ) |
| 4803 | return main.ERROR |
| 4804 | except Exception: |
| 4805 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4806 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4807 | |
| 4808 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4809 | """ |
| 4810 | CLI command to getAndSet the value of an atomic value. |
| 4811 | Required arguments: |
| 4812 | valueName - The name of the value to get. |
| 4813 | newValue - The value to assign to the given value |
| 4814 | returns: |
| 4815 | string value of the value or |
| 4816 | None on Error |
| 4817 | """ |
| 4818 | try: |
| 4819 | valueName = str( valueName ) |
| 4820 | cmdStr = "value-test " |
| 4821 | operation = "getAndSet" |
| 4822 | cmdStr += valueName + " " + operation |
| 4823 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4824 | operation, |
| 4825 | newValue ) |
| 4826 | output = self.distPrimitivesSend( cmdStr ) |
| 4827 | pattern = "(\w+)" |
| 4828 | match = re.search( pattern, output ) |
| 4829 | if match: |
| 4830 | return match.group( 1 ) |
| 4831 | else: |
| 4832 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4833 | " match expected output." ) |
| 4834 | main.log.debug( self.name + " expected: " + pattern ) |
| 4835 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4836 | return None |
| 4837 | except TypeError: |
| 4838 | main.log.exception( self.name + ": Object not as expected" ) |
| 4839 | return None |
| 4840 | except Exception: |
| 4841 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4842 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4843 | |
| 4844 | def valueTestDestroy( self, valueName ): |
| 4845 | """ |
| 4846 | CLI command to destroy an atomic value. |
| 4847 | Required arguments: |
| 4848 | valueName - The name of the value to destroy. |
| 4849 | returns: |
| 4850 | main.TRUE on success or |
| 4851 | main.ERROR on Error |
| 4852 | """ |
| 4853 | try: |
| 4854 | valueName = str( valueName ) |
| 4855 | cmdStr = "value-test " |
| 4856 | operation = "destroy" |
| 4857 | cmdStr += valueName + " " + operation |
| 4858 | output = self.distPrimitivesSend( cmdStr ) |
| 4859 | if output is not None: |
| 4860 | return main.TRUE |
| 4861 | else: |
| 4862 | return main.ERROR |
| 4863 | except TypeError: |
| 4864 | main.log.exception( self.name + ": Object not as expected" ) |
| 4865 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4866 | except Exception: |
| 4867 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4868 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4869 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4870 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4871 | """ |
| 4872 | Description: Execute summary command in onos |
| 4873 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4874 | no output |
| 4875 | |
| 4876 | """ |
| 4877 | try: |
| 4878 | cmdStr = "summary" |
| 4879 | if jsonFormat: |
| 4880 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4881 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4882 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4883 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4884 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 4885 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4886 | if not handle: |
| 4887 | main.log.error( self.name + ": There is no output in " + |
| 4888 | "summary command" ) |
| 4889 | return main.FALSE |
| 4890 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4891 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4892 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4893 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4894 | except TypeError: |
| 4895 | main.log.exception( self.name + ": Object not as expected" ) |
| 4896 | return None |
| 4897 | except pexpect.EOF: |
| 4898 | main.log.error( self.name + ": EOF exception found" ) |
| 4899 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4900 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4901 | except Exception: |
| 4902 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4903 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4904 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4905 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4906 | """ |
| 4907 | CLI command to get the value of a key in a consistent map using |
| 4908 | transactions. This a test function and can only get keys from the |
| 4909 | test map hard coded into the cli command |
| 4910 | Required arguments: |
| 4911 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4912 | returns: |
| 4913 | The string value of the key or |
| 4914 | None on Error |
| 4915 | """ |
| 4916 | try: |
| 4917 | keyName = str( keyName ) |
| 4918 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4919 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4920 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4921 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4922 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4923 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4924 | return None |
| 4925 | else: |
| 4926 | match = re.search( pattern, output ) |
| 4927 | if match: |
| 4928 | return match.groupdict()[ 'value' ] |
| 4929 | else: |
| 4930 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4931 | " match expected output." ) |
| 4932 | main.log.debug( self.name + " expected: " + pattern ) |
| 4933 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4934 | return None |
| 4935 | except TypeError: |
| 4936 | main.log.exception( self.name + ": Object not as expected" ) |
| 4937 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4938 | except Exception: |
| 4939 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4940 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4941 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4942 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4943 | """ |
| 4944 | CLI command to put a value into 'numKeys' number of keys in a |
| 4945 | consistent map using transactions. This a test function and can only |
| 4946 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4947 | Required arguments: |
| 4948 | numKeys - Number of keys to add the value to |
| 4949 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4950 | returns: |
| 4951 | A dictionary whose keys are the name of the keys put into the map |
| 4952 | and the values of the keys are dictionaries whose key-values are |
| 4953 | 'value': value put into map and optionaly |
| 4954 | 'oldValue': Previous value in the key or |
| 4955 | None on Error |
| 4956 | |
| 4957 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4958 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4959 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4960 | """ |
| 4961 | try: |
| 4962 | numKeys = str( numKeys ) |
| 4963 | value = str( value ) |
| 4964 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4965 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4966 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4967 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4968 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4969 | results = {} |
| 4970 | for line in output.splitlines(): |
| 4971 | new = re.search( newPattern, line ) |
| 4972 | updated = re.search( updatedPattern, line ) |
| 4973 | if new: |
| 4974 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4975 | elif updated: |
| 4976 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4977 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4978 | else: |
| 4979 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4980 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4981 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4982 | newPattern, |
| 4983 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4984 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4985 | return results |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4986 | except ( TypeError, AttributeError ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4987 | main.log.exception( self.name + ": Object not as expected" ) |
| 4988 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4989 | except Exception: |
| 4990 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4991 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4992 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4993 | def maps( self, jsonFormat=True ): |
| 4994 | """ |
| 4995 | Description: Returns result of onos:maps |
| 4996 | Optional: |
| 4997 | * jsonFormat: enable json formatting of output |
| 4998 | """ |
| 4999 | try: |
| 5000 | cmdStr = "maps" |
| 5001 | if jsonFormat: |
| 5002 | cmdStr += " -j" |
| 5003 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5004 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5005 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5006 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5007 | except AssertionError: |
| 5008 | main.log.exception( "" ) |
| 5009 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5010 | except TypeError: |
| 5011 | main.log.exception( self.name + ": Object not as expected" ) |
| 5012 | return None |
| 5013 | except pexpect.EOF: |
| 5014 | main.log.error( self.name + ": EOF exception found" ) |
| 5015 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5016 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5017 | except Exception: |
| 5018 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5019 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5020 | |
| 5021 | def getSwController( self, uri, jsonFormat=True ): |
| 5022 | """ |
| 5023 | Descrition: Gets the controller information from the device |
| 5024 | """ |
| 5025 | try: |
| 5026 | cmd = "device-controllers " |
| 5027 | if jsonFormat: |
| 5028 | cmd += "-j " |
| 5029 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5030 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5031 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5032 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5033 | except AssertionError: |
| 5034 | main.log.exception( "" ) |
| 5035 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5036 | except TypeError: |
| 5037 | main.log.exception( self.name + ": Object not as expected" ) |
| 5038 | return None |
| 5039 | except pexpect.EOF: |
| 5040 | main.log.error( self.name + ": EOF exception found" ) |
| 5041 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5042 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5043 | except Exception: |
| 5044 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5045 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5046 | |
| 5047 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 5048 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5049 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5050 | |
| 5051 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5052 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5053 | ip - String or List: The ip address of the controller. |
| 5054 | This parameter can be formed in a couple of different ways. |
| 5055 | VALID: |
| 5056 | 10.0.0.1 - just the ip address |
| 5057 | tcp:10.0.0.1 - the protocol and the ip address |
| 5058 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 5059 | so that you can add controllers with different |
| 5060 | protocols and ports |
| 5061 | INVALID: |
| 5062 | 10.0.0.1:6653 - this is not supported by ONOS |
| 5063 | |
| 5064 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 5065 | port - The port number. |
| 5066 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 5067 | |
| 5068 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 5069 | """ |
| 5070 | try: |
| 5071 | cmd = "device-setcontrollers" |
| 5072 | |
| 5073 | if jsonFormat: |
| 5074 | cmd += " -j" |
| 5075 | cmd += " " + uri |
| 5076 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5077 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5078 | for item in ip: |
| 5079 | if ":" in item: |
| 5080 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5081 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5082 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5083 | elif "." in sitem[ 1 ]: |
| 5084 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5085 | else: |
| 5086 | main.log.error( "Malformed entry: " + item ) |
| 5087 | raise TypeError |
| 5088 | else: |
| 5089 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5090 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5091 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5092 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5093 | if "Error" in response: |
| 5094 | main.log.error( response ) |
| 5095 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5096 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5097 | except AssertionError: |
| 5098 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5099 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5100 | except TypeError: |
| 5101 | main.log.exception( self.name + ": Object not as expected" ) |
| 5102 | return main.FALSE |
| 5103 | except pexpect.EOF: |
| 5104 | main.log.error( self.name + ": EOF exception found" ) |
| 5105 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5106 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5107 | except Exception: |
| 5108 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5109 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5110 | |
| 5111 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5112 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5113 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5114 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5115 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5116 | 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] | 5117 | Returns: |
| 5118 | Returns main.FALSE if an exception is thrown or an error is present |
| 5119 | in the response. Otherwise, returns main.TRUE. |
| 5120 | NOTE: |
| 5121 | 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] | 5122 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5123 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5124 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 5125 | deviceStr = device |
| 5126 | device = [] |
| 5127 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5128 | |
| 5129 | for d in device: |
| 5130 | time.sleep( 1 ) |
| 5131 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5132 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5133 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5134 | if "Error" in response: |
| 5135 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 5136 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5137 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5138 | except AssertionError: |
| 5139 | main.log.exception( "" ) |
| 5140 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5141 | except TypeError: |
| 5142 | main.log.exception( self.name + ": Object not as expected" ) |
| 5143 | return main.FALSE |
| 5144 | except pexpect.EOF: |
| 5145 | main.log.error( self.name + ": EOF exception found" ) |
| 5146 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5147 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5148 | except Exception: |
| 5149 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5150 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5151 | |
| 5152 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5153 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5154 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5155 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5156 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5157 | 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] | 5158 | Returns: |
| 5159 | Returns main.FALSE if an exception is thrown or an error is present |
| 5160 | in the response. Otherwise, returns main.TRUE. |
| 5161 | NOTE: |
| 5162 | 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] | 5163 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5164 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5165 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5166 | host = list( host ) |
| 5167 | |
| 5168 | for h in host: |
| 5169 | time.sleep( 1 ) |
| 5170 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5171 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5172 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5173 | if "Error" in response: |
| 5174 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5175 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5176 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5177 | except AssertionError: |
| 5178 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5179 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5180 | except TypeError: |
| 5181 | main.log.exception( self.name + ": Object not as expected" ) |
| 5182 | return main.FALSE |
| 5183 | except pexpect.EOF: |
| 5184 | main.log.error( self.name + ": EOF exception found" ) |
| 5185 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5186 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5187 | except Exception: |
| 5188 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5189 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5190 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5191 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5192 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5193 | Description: |
| 5194 | Bring link down or up in the null-provider. |
| 5195 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5196 | begin - (string) One end of a device or switch. |
| 5197 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5198 | returns: |
| 5199 | main.TRUE if no exceptions were thrown and no Errors are |
| 5200 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5201 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5202 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5203 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5204 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5205 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5206 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5207 | if "Error" in response or "Failure" in response: |
| 5208 | main.log.error( response ) |
| 5209 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5210 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5211 | except AssertionError: |
| 5212 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5213 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5214 | except TypeError: |
| 5215 | main.log.exception( self.name + ": Object not as expected" ) |
| 5216 | return main.FALSE |
| 5217 | except pexpect.EOF: |
| 5218 | main.log.error( self.name + ": EOF exception found" ) |
| 5219 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5220 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5221 | except Exception: |
| 5222 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5223 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5224 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5225 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5226 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5227 | Description: |
| 5228 | Changes the state of port in an OF switch by means of the |
| 5229 | PORTSTATUS OF messages. |
| 5230 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5231 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5232 | port - (string) target port in the device. Ex: '2' |
| 5233 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5234 | returns: |
| 5235 | main.TRUE if no exceptions were thrown and no Errors are |
| 5236 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5237 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5238 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5239 | state = state.lower() |
| 5240 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5241 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5242 | response = self.sendline( cmd, showResponse=True ) |
| 5243 | assert response is not None, "Error in sendline" |
| 5244 | assert "Command not found:" not in response, response |
| 5245 | if "Error" in response or "Failure" in response: |
| 5246 | main.log.error( response ) |
| 5247 | return main.FALSE |
| 5248 | return main.TRUE |
| 5249 | except AssertionError: |
| 5250 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5251 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5252 | except TypeError: |
| 5253 | main.log.exception( self.name + ": Object not as expected" ) |
| 5254 | return main.FALSE |
| 5255 | except pexpect.EOF: |
| 5256 | main.log.error( self.name + ": EOF exception found" ) |
| 5257 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5258 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5259 | except Exception: |
| 5260 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5261 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5262 | |
| 5263 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5264 | """ |
| 5265 | Set the logging level to lvl for a specific app |
| 5266 | returns main.TRUE on success |
| 5267 | returns main.FALSE if Error occurred |
| 5268 | if noExit is True, TestON will not exit, but clean up |
| 5269 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5270 | Level defaults to INFO |
| 5271 | """ |
| 5272 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5273 | self.handle.sendline( "log:set %s %s" % ( level, app ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 5274 | self.handle.expect( self.karafPrompt ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5275 | |
| 5276 | response = self.handle.before |
| 5277 | if re.search( "Error", response ): |
| 5278 | return main.FALSE |
| 5279 | return main.TRUE |
| 5280 | except pexpect.TIMEOUT: |
| 5281 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5282 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5283 | except pexpect.EOF: |
| 5284 | main.log.error( self.name + ": EOF exception found" ) |
| 5285 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5286 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5287 | except Exception: |
| 5288 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5289 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5290 | |
| 5291 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5292 | """ |
| 5293 | Return a dictionary which describes the latest network topology data as a |
| 5294 | graph. |
| 5295 | An example of the dictionary: |
| 5296 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5297 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5298 | Each vertex should at least have an 'edges' attribute which describes the |
| 5299 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5300 | 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] | 5301 | list of attributes. |
| 5302 | An example of the edges dictionary: |
| 5303 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5304 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5305 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5306 | in topology data. |
| 5307 | """ |
| 5308 | graphDict = {} |
| 5309 | try: |
| 5310 | links = self.links() |
| 5311 | links = json.loads( links ) |
| 5312 | devices = self.devices() |
| 5313 | devices = json.loads( devices ) |
| 5314 | idToDevice = {} |
| 5315 | for device in devices: |
| 5316 | idToDevice[ device[ 'id' ] ] = device |
| 5317 | if includeHost: |
| 5318 | hosts = self.hosts() |
| 5319 | # FIXME: support 'includeHost' argument |
| 5320 | for link in links: |
| 5321 | nodeA = link[ 'src' ][ 'device' ] |
| 5322 | nodeB = link[ 'dst' ][ 'device' ] |
| 5323 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5324 | if nodeA not in graphDict.keys(): |
| 5325 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5326 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5327 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5328 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5329 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5330 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5331 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5332 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5333 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5334 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5335 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5336 | else: |
| 5337 | # Assert nodeB is not connected to any current links of nodeA |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 5338 | # assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 5339 | pass |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5340 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5341 | 'type': link[ 'type' ], |
| 5342 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5343 | return graphDict |
| 5344 | except ( TypeError, ValueError ): |
| 5345 | main.log.exception( self.name + ": Object not as expected" ) |
| 5346 | return None |
| 5347 | except KeyError: |
| 5348 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5349 | return None |
| 5350 | except AssertionError: |
| 5351 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5352 | return None |
| 5353 | except pexpect.EOF: |
| 5354 | main.log.error( self.name + ": EOF exception found" ) |
| 5355 | main.log.error( self.name + ": " + self.handle.before ) |
| 5356 | return None |
| 5357 | except Exception: |
| 5358 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5359 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5360 | |
| 5361 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5362 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5363 | Send command to check intent-perf summary |
| 5364 | Returns: dictionary for intent-perf summary |
| 5365 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5366 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5367 | cmd = "intent-perf -s" |
| 5368 | respDic = {} |
| 5369 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5370 | assert resp is not None, "Error in sendline" |
| 5371 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5372 | try: |
| 5373 | # Generate the dictionary to return |
| 5374 | for l in resp.split( "\n" ): |
| 5375 | # Delete any white space in line |
| 5376 | temp = re.sub( r'\s+', '', l ) |
| 5377 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5378 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5379 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5380 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5381 | main.log.exception( self.name + ": Object not as expected" ) |
| 5382 | return None |
| 5383 | except KeyError: |
| 5384 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5385 | return None |
| 5386 | except AssertionError: |
| 5387 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5388 | return None |
| 5389 | except pexpect.EOF: |
| 5390 | main.log.error( self.name + ": EOF exception found" ) |
| 5391 | main.log.error( self.name + ": " + self.handle.before ) |
| 5392 | return None |
| 5393 | except Exception: |
| 5394 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5395 | return None |
| 5396 | return respDic |
| 5397 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5398 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5399 | """ |
| 5400 | Searches the latest ONOS log file for the given search term and |
| 5401 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5402 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5403 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5404 | searchTerm: |
| 5405 | The string to grep from the ONOS log. |
| 5406 | startLine: |
| 5407 | The term that decides which line is the start to search the searchTerm in |
| 5408 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5409 | logNum: |
| 5410 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5411 | information.Because of this, search mutiply logs is necessary to capture |
| 5412 | the right result. logNum is the number of karaf logs that we need to search |
| 5413 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5414 | mode: |
| 5415 | all: return all the strings that contain the search term |
| 5416 | last: return the last string that contains the search term |
| 5417 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5418 | num: return the number of times that the searchTerm appears in the log |
| 5419 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5420 | """ |
| 5421 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5422 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5423 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5424 | logPath = '/opt/onos/log/karaf.log.' |
| 5425 | logPaths = '/opt/onos/log/karaf.log' |
| 5426 | for i in range( 1, logNum ): |
| 5427 | logPaths = logPath + str( i ) + " " + logPaths |
| 5428 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5429 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5430 | # 100000000 is just a extreme large number to make sure this function can |
| 5431 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5432 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5433 | if mode == 'all': |
| 5434 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5435 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5436 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5437 | elif mode == 'first': |
| 5438 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5439 | elif mode == 'num': |
You Wang | d91a70f | 2019-01-03 15:28:10 -0800 | [diff] [blame] | 5440 | cmd = cmd + " | grep \'" + searchTerm + "\' | wc -l" |
| 5441 | num = self.lineCount( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5442 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5443 | elif mode == 'total': |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 5444 | totalLines = self.lineCount( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5445 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5446 | else: |
| 5447 | main.log.error( self.name + " unsupported mode" ) |
| 5448 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5449 | before = self.sendline( cmd ) |
| 5450 | before = before.splitlines() |
| 5451 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5452 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5453 | return returnLines |
| 5454 | except AssertionError: |
| 5455 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5456 | return None |
| 5457 | except pexpect.EOF: |
| 5458 | main.log.error( self.name + ": EOF exception found" ) |
| 5459 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5460 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5461 | except pexpect.TIMEOUT: |
| 5462 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5463 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5464 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5465 | except Exception: |
| 5466 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5467 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5468 | |
| 5469 | def vplsShow( self, jsonFormat=True ): |
| 5470 | """ |
| 5471 | Description: Returns result of onos:vpls show, which should list the |
| 5472 | configured VPLS networks and the assigned interfaces. |
| 5473 | Optional: |
| 5474 | * jsonFormat: enable json formatting of output |
| 5475 | Returns: |
| 5476 | The output of the command or None on error. |
| 5477 | """ |
| 5478 | try: |
| 5479 | cmdStr = "vpls show" |
| 5480 | if jsonFormat: |
| 5481 | raise NotImplementedError |
| 5482 | cmdStr += " -j" |
| 5483 | handle = self.sendline( cmdStr ) |
| 5484 | assert handle is not None, "Error in sendline" |
| 5485 | assert "Command not found:" not in handle, handle |
| 5486 | return handle |
| 5487 | except AssertionError: |
| 5488 | main.log.exception( "" ) |
| 5489 | return None |
| 5490 | except TypeError: |
| 5491 | main.log.exception( self.name + ": Object not as expected" ) |
| 5492 | return None |
| 5493 | except pexpect.EOF: |
| 5494 | main.log.error( self.name + ": EOF exception found" ) |
| 5495 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5496 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5497 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5498 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5499 | return None |
| 5500 | except Exception: |
| 5501 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5502 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5503 | |
| 5504 | def parseVplsShow( self ): |
| 5505 | """ |
| 5506 | Parse the cli output of 'vpls show' into json output. This is required |
| 5507 | as there is currently no json output available. |
| 5508 | """ |
| 5509 | try: |
| 5510 | output = [] |
| 5511 | raw = self.vplsShow( jsonFormat=False ) |
| 5512 | namePat = "VPLS name: (?P<name>\w+)" |
| 5513 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5514 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5515 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5516 | mIter = re.finditer( pattern, raw ) |
| 5517 | for match in mIter: |
| 5518 | item = {} |
| 5519 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5520 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5521 | if ifaces == [ "" ]: |
| 5522 | ifaces = [] |
| 5523 | item[ 'interfaces' ] = ifaces |
| 5524 | encap = match.group( 'encap' ) |
| 5525 | if encap != 'NONE': |
| 5526 | item[ 'encapsulation' ] = encap.lower() |
| 5527 | output.append( item ) |
| 5528 | return output |
| 5529 | except Exception: |
| 5530 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5531 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5532 | |
| 5533 | def vplsList( self, jsonFormat=True ): |
| 5534 | """ |
| 5535 | Description: Returns result of onos:vpls list, which should list the |
| 5536 | configured VPLS networks. |
| 5537 | Optional: |
| 5538 | * jsonFormat: enable json formatting of output |
| 5539 | """ |
| 5540 | try: |
| 5541 | cmdStr = "vpls list" |
| 5542 | if jsonFormat: |
| 5543 | raise NotImplementedError |
| 5544 | cmdStr += " -j" |
| 5545 | handle = self.sendline( cmdStr ) |
| 5546 | assert handle is not None, "Error in sendline" |
| 5547 | assert "Command not found:" not in handle, handle |
| 5548 | return handle |
| 5549 | except AssertionError: |
| 5550 | main.log.exception( "" ) |
| 5551 | return None |
| 5552 | except TypeError: |
| 5553 | main.log.exception( self.name + ": Object not as expected" ) |
| 5554 | return None |
| 5555 | except pexpect.EOF: |
| 5556 | main.log.error( self.name + ": EOF exception found" ) |
| 5557 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5558 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5559 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5560 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5561 | return None |
| 5562 | except Exception: |
| 5563 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5564 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5565 | |
| 5566 | def vplsCreate( self, network ): |
| 5567 | """ |
| 5568 | CLI command to create a new VPLS network. |
| 5569 | Required arguments: |
| 5570 | network - String name of the network to create. |
| 5571 | returns: |
| 5572 | main.TRUE on success and main.FALSE on failure |
| 5573 | """ |
| 5574 | try: |
| 5575 | network = str( network ) |
| 5576 | cmdStr = "vpls create " |
| 5577 | cmdStr += network |
| 5578 | output = self.sendline( cmdStr ) |
| 5579 | assert output is not None, "Error in sendline" |
| 5580 | assert "Command not found:" not in output, output |
| 5581 | assert "Error executing command" not in output, output |
| 5582 | assert "VPLS already exists:" not in output, output |
| 5583 | return main.TRUE |
| 5584 | except AssertionError: |
| 5585 | main.log.exception( "" ) |
| 5586 | return main.FALSE |
| 5587 | except TypeError: |
| 5588 | main.log.exception( self.name + ": Object not as expected" ) |
| 5589 | return main.FALSE |
| 5590 | except pexpect.EOF: |
| 5591 | main.log.error( self.name + ": EOF exception found" ) |
| 5592 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5593 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5594 | except Exception: |
| 5595 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5596 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5597 | |
| 5598 | def vplsDelete( self, network ): |
| 5599 | """ |
| 5600 | CLI command to delete a VPLS network. |
| 5601 | Required arguments: |
| 5602 | network - Name of the network to delete. |
| 5603 | returns: |
| 5604 | main.TRUE on success and main.FALSE on failure |
| 5605 | """ |
| 5606 | try: |
| 5607 | network = str( network ) |
| 5608 | cmdStr = "vpls delete " |
| 5609 | cmdStr += network |
| 5610 | output = self.sendline( cmdStr ) |
| 5611 | assert output is not None, "Error in sendline" |
| 5612 | assert "Command not found:" not in output, output |
| 5613 | assert "Error executing command" not in output, output |
| 5614 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5615 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5616 | return main.TRUE |
| 5617 | except AssertionError: |
| 5618 | main.log.exception( "" ) |
| 5619 | return main.FALSE |
| 5620 | except TypeError: |
| 5621 | main.log.exception( self.name + ": Object not as expected" ) |
| 5622 | return main.FALSE |
| 5623 | except pexpect.EOF: |
| 5624 | main.log.error( self.name + ": EOF exception found" ) |
| 5625 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5626 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5627 | except Exception: |
| 5628 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5629 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5630 | |
| 5631 | def vplsAddIface( self, network, iface ): |
| 5632 | """ |
| 5633 | CLI command to add an interface to a VPLS network. |
| 5634 | Required arguments: |
| 5635 | network - Name of the network to add the interface to. |
| 5636 | iface - The ONOS name for an interface. |
| 5637 | returns: |
| 5638 | main.TRUE on success and main.FALSE on failure |
| 5639 | """ |
| 5640 | try: |
| 5641 | network = str( network ) |
| 5642 | iface = str( iface ) |
| 5643 | cmdStr = "vpls add-if " |
| 5644 | cmdStr += network + " " + iface |
| 5645 | output = self.sendline( cmdStr ) |
| 5646 | assert output is not None, "Error in sendline" |
| 5647 | assert "Command not found:" not in output, output |
| 5648 | assert "Error executing command" not in output, output |
| 5649 | assert "already associated to network" not in output, output |
| 5650 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5651 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5652 | return main.TRUE |
| 5653 | except AssertionError: |
| 5654 | main.log.exception( "" ) |
| 5655 | return main.FALSE |
| 5656 | except TypeError: |
| 5657 | main.log.exception( self.name + ": Object not as expected" ) |
| 5658 | return main.FALSE |
| 5659 | except pexpect.EOF: |
| 5660 | main.log.error( self.name + ": EOF exception found" ) |
| 5661 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5662 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5663 | except Exception: |
| 5664 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5665 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5666 | |
| 5667 | def vplsRemIface( self, network, iface ): |
| 5668 | """ |
| 5669 | CLI command to remove an interface from a VPLS network. |
| 5670 | Required arguments: |
| 5671 | network - Name of the network to remove the interface from. |
| 5672 | iface - Name of the interface to remove. |
| 5673 | returns: |
| 5674 | main.TRUE on success and main.FALSE on failure |
| 5675 | """ |
| 5676 | try: |
| 5677 | iface = str( iface ) |
| 5678 | cmdStr = "vpls rem-if " |
| 5679 | cmdStr += network + " " + iface |
| 5680 | output = self.sendline( cmdStr ) |
| 5681 | assert output is not None, "Error in sendline" |
| 5682 | assert "Command not found:" not in output, output |
| 5683 | assert "Error executing command" not in output, output |
| 5684 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5685 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5686 | return main.TRUE |
| 5687 | except AssertionError: |
| 5688 | main.log.exception( "" ) |
| 5689 | return main.FALSE |
| 5690 | except TypeError: |
| 5691 | main.log.exception( self.name + ": Object not as expected" ) |
| 5692 | return main.FALSE |
| 5693 | except pexpect.EOF: |
| 5694 | main.log.error( self.name + ": EOF exception found" ) |
| 5695 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5696 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5697 | except Exception: |
| 5698 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5699 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5700 | |
| 5701 | def vplsClean( self ): |
| 5702 | """ |
| 5703 | Description: Clears the VPLS app configuration. |
| 5704 | Returns: main.TRUE on success and main.FALSE on failure |
| 5705 | """ |
| 5706 | try: |
| 5707 | cmdStr = "vpls clean" |
| 5708 | handle = self.sendline( cmdStr ) |
| 5709 | assert handle is not None, "Error in sendline" |
| 5710 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5711 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5712 | return handle |
| 5713 | except AssertionError: |
| 5714 | main.log.exception( "" ) |
| 5715 | return main.FALSE |
| 5716 | except TypeError: |
| 5717 | main.log.exception( self.name + ": Object not as expected" ) |
| 5718 | return main.FALSE |
| 5719 | except pexpect.EOF: |
| 5720 | main.log.error( self.name + ": EOF exception found" ) |
| 5721 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5722 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5723 | except Exception: |
| 5724 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5725 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5726 | |
| 5727 | def vplsSetEncap( self, network, encapType ): |
| 5728 | """ |
| 5729 | CLI command to add an interface to a VPLS network. |
| 5730 | Required arguments: |
| 5731 | network - Name of the network to create. |
| 5732 | encapType - Type of encapsulation. |
| 5733 | returns: |
| 5734 | main.TRUE on success and main.FALSE on failure |
| 5735 | """ |
| 5736 | try: |
| 5737 | network = str( network ) |
| 5738 | encapType = str( encapType ).upper() |
| 5739 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5740 | cmdStr = "vpls set-encap " |
| 5741 | cmdStr += network + " " + encapType |
| 5742 | output = self.sendline( cmdStr ) |
| 5743 | assert output is not None, "Error in sendline" |
| 5744 | assert "Command not found:" not in output, output |
| 5745 | assert "Error executing command" not in output, output |
| 5746 | assert "already associated to network" not in output, output |
| 5747 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5748 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5749 | return main.TRUE |
| 5750 | except AssertionError: |
| 5751 | main.log.exception( "" ) |
| 5752 | return main.FALSE |
| 5753 | except TypeError: |
| 5754 | main.log.exception( self.name + ": Object not as expected" ) |
| 5755 | return main.FALSE |
| 5756 | except pexpect.EOF: |
| 5757 | main.log.error( self.name + ": EOF exception found" ) |
| 5758 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5759 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5760 | except Exception: |
| 5761 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5762 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5763 | |
| 5764 | def interfaces( self, jsonFormat=True ): |
| 5765 | """ |
| 5766 | Description: Returns result of interfaces command. |
| 5767 | Optional: |
| 5768 | * jsonFormat: enable json formatting of output |
| 5769 | Returns: |
| 5770 | The output of the command or None on error. |
| 5771 | """ |
| 5772 | try: |
| 5773 | cmdStr = "interfaces" |
| 5774 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5775 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5776 | cmdStr += " -j" |
| 5777 | handle = self.sendline( cmdStr ) |
| 5778 | assert handle is not None, "Error in sendline" |
| 5779 | assert "Command not found:" not in handle, handle |
| 5780 | return handle |
| 5781 | except AssertionError: |
| 5782 | main.log.exception( "" ) |
| 5783 | return None |
| 5784 | except TypeError: |
| 5785 | main.log.exception( self.name + ": Object not as expected" ) |
| 5786 | return None |
| 5787 | except pexpect.EOF: |
| 5788 | main.log.error( self.name + ": EOF exception found" ) |
| 5789 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5790 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5791 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5792 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5793 | return None |
| 5794 | except Exception: |
| 5795 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5796 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5797 | |
| 5798 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5799 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5800 | Get the timestamp of searchTerm from karaf log. |
| 5801 | |
| 5802 | Arguments: |
| 5803 | splitTerm_before and splitTerm_after: |
| 5804 | |
| 5805 | The terms that split the string that contains the timeStamp of |
| 5806 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5807 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5808 | and the splitTerm_after is "x" |
| 5809 | |
| 5810 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5811 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5812 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5813 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5814 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5815 | return main.ERROR |
| 5816 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5817 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5818 | main.log.warn( "Captured timestamp string is empty" ) |
| 5819 | return main.ERROR |
| 5820 | lines = lines[ 0 ] |
| 5821 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5822 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5823 | # get the target value |
| 5824 | line = lines.split( splitTerm_before ) |
| 5825 | key = line[ 1 ].split( splitTerm_after ) |
| 5826 | return int( key[ 0 ] ) |
| 5827 | except IndexError: |
| 5828 | main.log.warn( "Index Error!" ) |
| 5829 | return main.ERROR |
| 5830 | except AssertionError: |
| 5831 | main.log.warn( "Search Term Not Found " ) |
| 5832 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5833 | |
| 5834 | def workQueueAdd( self, queueName, value ): |
| 5835 | """ |
| 5836 | CLI command to add a string to the specified Work Queue. |
| 5837 | This function uses the distributed primitives test app, which |
| 5838 | gives some cli access to distributed primitives for testing |
| 5839 | purposes only. |
| 5840 | |
| 5841 | Required arguments: |
| 5842 | queueName - The name of the queue to add to |
| 5843 | value - The value to add to the queue |
| 5844 | returns: |
| 5845 | main.TRUE on success, main.FALSE on failure and |
| 5846 | main.ERROR on error. |
| 5847 | """ |
| 5848 | try: |
| 5849 | queueName = str( queueName ) |
| 5850 | value = str( value ) |
| 5851 | prefix = "work-queue-test" |
| 5852 | operation = "add" |
| 5853 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5854 | output = self.distPrimitivesSend( cmdStr ) |
| 5855 | if "Invalid operation name" in output: |
| 5856 | main.log.warn( output ) |
| 5857 | return main.ERROR |
| 5858 | elif "Done" in output: |
| 5859 | return main.TRUE |
| 5860 | except TypeError: |
| 5861 | main.log.exception( self.name + ": Object not as expected" ) |
| 5862 | return main.ERROR |
| 5863 | except Exception: |
| 5864 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5865 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5866 | |
| 5867 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5868 | """ |
| 5869 | CLI command to add two strings to the specified Work Queue. |
| 5870 | This function uses the distributed primitives test app, which |
| 5871 | gives some cli access to distributed primitives for testing |
| 5872 | purposes only. |
| 5873 | |
| 5874 | Required arguments: |
| 5875 | queueName - The name of the queue to add to |
| 5876 | value1 - The first value to add to the queue |
| 5877 | value2 - The second value to add to the queue |
| 5878 | returns: |
| 5879 | main.TRUE on success, main.FALSE on failure and |
| 5880 | main.ERROR on error. |
| 5881 | """ |
| 5882 | try: |
| 5883 | queueName = str( queueName ) |
| 5884 | value1 = str( value1 ) |
| 5885 | value2 = str( value2 ) |
| 5886 | prefix = "work-queue-test" |
| 5887 | operation = "addMultiple" |
| 5888 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 5889 | output = self.distPrimitivesSend( cmdStr ) |
| 5890 | if "Invalid operation name" in output: |
| 5891 | main.log.warn( output ) |
| 5892 | return main.ERROR |
| 5893 | elif "Done" in output: |
| 5894 | return main.TRUE |
| 5895 | except TypeError: |
| 5896 | main.log.exception( self.name + ": Object not as expected" ) |
| 5897 | return main.ERROR |
| 5898 | except Exception: |
| 5899 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5900 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5901 | |
| 5902 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 5903 | """ |
| 5904 | CLI command to take a value from the specified Work Queue and compelte it. |
| 5905 | This function uses the distributed primitives test app, which |
| 5906 | gives some cli access to distributed primitives for testing |
| 5907 | purposes only. |
| 5908 | |
| 5909 | Required arguments: |
| 5910 | queueName - The name of the queue to add to |
| 5911 | number - The number of items to take and complete |
| 5912 | returns: |
| 5913 | main.TRUE on success, main.FALSE on failure and |
| 5914 | main.ERROR on error. |
| 5915 | """ |
| 5916 | try: |
| 5917 | queueName = str( queueName ) |
| 5918 | number = str( int( number ) ) |
| 5919 | prefix = "work-queue-test" |
| 5920 | operation = "takeAndComplete" |
| 5921 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 5922 | output = self.distPrimitivesSend( cmdStr ) |
| 5923 | if "Invalid operation name" in output: |
| 5924 | main.log.warn( output ) |
| 5925 | return main.ERROR |
| 5926 | elif "Done" in output: |
| 5927 | return main.TRUE |
| 5928 | except TypeError: |
| 5929 | main.log.exception( self.name + ": Object not as expected" ) |
| 5930 | return main.ERROR |
| 5931 | except Exception: |
| 5932 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5933 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5934 | |
| 5935 | def workQueueDestroy( self, queueName ): |
| 5936 | """ |
| 5937 | CLI command to destroy the specified Work Queue. |
| 5938 | This function uses the distributed primitives test app, which |
| 5939 | gives some cli access to distributed primitives for testing |
| 5940 | purposes only. |
| 5941 | |
| 5942 | Required arguments: |
| 5943 | queueName - The name of the queue to add to |
| 5944 | returns: |
| 5945 | main.TRUE on success, main.FALSE on failure and |
| 5946 | main.ERROR on error. |
| 5947 | """ |
| 5948 | try: |
| 5949 | queueName = str( queueName ) |
| 5950 | prefix = "work-queue-test" |
| 5951 | operation = "destroy" |
| 5952 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5953 | output = self.distPrimitivesSend( cmdStr ) |
| 5954 | if "Invalid operation name" in output: |
| 5955 | main.log.warn( output ) |
| 5956 | return main.ERROR |
| 5957 | return main.TRUE |
| 5958 | except TypeError: |
| 5959 | main.log.exception( self.name + ": Object not as expected" ) |
| 5960 | return main.ERROR |
| 5961 | except Exception: |
| 5962 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5963 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5964 | |
| 5965 | def workQueueTotalPending( self, queueName ): |
| 5966 | """ |
| 5967 | CLI command to get the Total Pending items of the specified Work Queue. |
| 5968 | This function uses the distributed primitives test app, which |
| 5969 | gives some cli access to distributed primitives for testing |
| 5970 | purposes only. |
| 5971 | |
| 5972 | Required arguments: |
| 5973 | queueName - The name of the queue to add to |
| 5974 | returns: |
| 5975 | The number of Pending items in the specified work queue or |
| 5976 | None on error |
| 5977 | """ |
| 5978 | try: |
| 5979 | queueName = str( queueName ) |
| 5980 | prefix = "work-queue-test" |
| 5981 | operation = "totalPending" |
| 5982 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5983 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 5984 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 5985 | output = output.split( "\r\n" )[ -1 ] |
| 5986 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5987 | pattern = r'\d+' |
| 5988 | if "Invalid operation name" in output: |
| 5989 | main.log.warn( output ) |
| 5990 | return None |
| 5991 | else: |
| 5992 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5993 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5994 | except ( AttributeError, TypeError ): |
| 5995 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5996 | return None |
| 5997 | except Exception: |
| 5998 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5999 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6000 | |
| 6001 | def workQueueTotalCompleted( self, queueName ): |
| 6002 | """ |
| 6003 | CLI command to get the Total Completed items of the specified Work Queue. |
| 6004 | This function uses the distributed primitives test app, which |
| 6005 | gives some cli access to distributed primitives for testing |
| 6006 | purposes only. |
| 6007 | |
| 6008 | Required arguments: |
| 6009 | queueName - The name of the queue to add to |
| 6010 | returns: |
| 6011 | The number of complete items in the specified work queue or |
| 6012 | None on error |
| 6013 | """ |
| 6014 | try: |
| 6015 | queueName = str( queueName ) |
| 6016 | prefix = "work-queue-test" |
| 6017 | operation = "totalCompleted" |
| 6018 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6019 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6020 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6021 | output = output.split( "\r\n" )[ -1 ] |
| 6022 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6023 | pattern = r'\d+' |
| 6024 | if "Invalid operation name" in output: |
| 6025 | main.log.warn( output ) |
| 6026 | return None |
| 6027 | else: |
| 6028 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6029 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6030 | except ( AttributeError, TypeError ): |
| 6031 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6032 | return None |
| 6033 | except Exception: |
| 6034 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6035 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6036 | |
| 6037 | def workQueueTotalInProgress( self, queueName ): |
| 6038 | """ |
| 6039 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 6040 | This function uses the distributed primitives test app, which |
| 6041 | gives some cli access to distributed primitives for testing |
| 6042 | purposes only. |
| 6043 | |
| 6044 | Required arguments: |
| 6045 | queueName - The name of the queue to add to |
| 6046 | returns: |
| 6047 | The number of In Progress items in the specified work queue or |
| 6048 | None on error |
| 6049 | """ |
| 6050 | try: |
| 6051 | queueName = str( queueName ) |
| 6052 | prefix = "work-queue-test" |
| 6053 | operation = "totalInProgress" |
| 6054 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6055 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6056 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6057 | output = output.split( "\r\n" )[ -1 ] |
| 6058 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6059 | pattern = r'\d+' |
| 6060 | if "Invalid operation name" in output: |
| 6061 | main.log.warn( output ) |
| 6062 | return None |
| 6063 | else: |
| 6064 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6065 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6066 | except ( AttributeError, TypeError ): |
| 6067 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6068 | return None |
| 6069 | except Exception: |
| 6070 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6071 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 6072 | |
| 6073 | def events( self, args='-a' ): |
| 6074 | """ |
| 6075 | Description: Returns events -a command output |
| 6076 | Optional: |
| 6077 | add other arguments |
| 6078 | """ |
| 6079 | try: |
| 6080 | cmdStr = "events" |
| 6081 | if args: |
| 6082 | cmdStr += " " + args |
| 6083 | handle = self.sendline( cmdStr ) |
| 6084 | assert handle is not None, "Error in sendline" |
| 6085 | assert "Command not found:" not in handle, handle |
| 6086 | return handle |
| 6087 | except AssertionError: |
| 6088 | main.log.exception( "" ) |
| 6089 | return None |
| 6090 | except TypeError: |
| 6091 | main.log.exception( self.name + ": Object not as expected" ) |
| 6092 | return None |
| 6093 | except pexpect.EOF: |
| 6094 | main.log.error( self.name + ": EOF exception found" ) |
| 6095 | main.log.error( self.name + ": " + self.handle.before ) |
| 6096 | main.cleanAndExit() |
| 6097 | except Exception: |
| 6098 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6099 | main.cleanAndExit() |
| 6100 | |
| 6101 | def getMaster( self, deviceID ): |
| 6102 | """ |
| 6103 | Description: Obtains current master using "roles" command for a specific deviceID |
| 6104 | """ |
| 6105 | try: |
| 6106 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 6107 | except AssertionError: |
| 6108 | main.log.exception( "" ) |
| 6109 | return None |
| 6110 | except TypeError: |
| 6111 | main.log.exception( self.name + ": Object not as expected" ) |
| 6112 | return None |
| 6113 | except pexpect.EOF: |
| 6114 | main.log.error( self.name + ": EOF exception found" ) |
| 6115 | main.log.error( self.name + ": " + self.handle.before ) |
| 6116 | main.cleanAndExit() |
| 6117 | except Exception: |
| 6118 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 6119 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 6120 | |
| 6121 | def issu( self ): |
| 6122 | """ |
| 6123 | Short summary of In-Service Software Upgrade status |
| 6124 | |
| 6125 | Returns the output of the cli command or None on Error |
| 6126 | """ |
| 6127 | try: |
| 6128 | cmdStr = "issu" |
| 6129 | handle = self.sendline( cmdStr ) |
| 6130 | assert handle is not None, "Error in sendline" |
| 6131 | assert "Command not found:" not in handle, handle |
| 6132 | assert "Unsupported command:" not in handle, handle |
| 6133 | return handle |
| 6134 | except AssertionError: |
| 6135 | main.log.exception( "" ) |
| 6136 | return None |
| 6137 | except TypeError: |
| 6138 | main.log.exception( self.name + ": Object not as expected" ) |
| 6139 | return None |
| 6140 | except pexpect.EOF: |
| 6141 | main.log.error( self.name + ": EOF exception found" ) |
| 6142 | main.log.error( self.name + ": " + self.handle.before ) |
| 6143 | main.cleanAndExit() |
| 6144 | except Exception: |
| 6145 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6146 | main.cleanAndExit() |
| 6147 | |
| 6148 | def issuInit( self ): |
| 6149 | """ |
| 6150 | Initiates an In-Service Software Upgrade |
| 6151 | |
| 6152 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6153 | """ |
| 6154 | try: |
| 6155 | cmdStr = "issu init" |
| 6156 | handle = self.sendline( cmdStr ) |
| 6157 | assert handle is not None, "Error in sendline" |
| 6158 | assert "Command not found:" not in handle, handle |
| 6159 | assert "Unsupported command:" not in handle, handle |
| 6160 | if "Initialized" in handle: |
| 6161 | return main.TRUE |
| 6162 | else: |
| 6163 | return main.FALSE |
| 6164 | except AssertionError: |
| 6165 | main.log.exception( "" ) |
| 6166 | return main.ERROR |
| 6167 | except TypeError: |
| 6168 | main.log.exception( self.name + ": Object not as expected" ) |
| 6169 | return main.ERROR |
| 6170 | except pexpect.EOF: |
| 6171 | main.log.error( self.name + ": EOF exception found" ) |
| 6172 | main.log.error( self.name + ": " + self.handle.before ) |
| 6173 | main.cleanAndExit() |
| 6174 | except Exception: |
| 6175 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6176 | main.cleanAndExit() |
| 6177 | |
| 6178 | def issuUpgrade( self ): |
| 6179 | """ |
| 6180 | Transitions stores to upgraded nodes |
| 6181 | |
| 6182 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6183 | """ |
| 6184 | try: |
| 6185 | cmdStr = "issu upgrade" |
| 6186 | handle = self.sendline( cmdStr ) |
| 6187 | assert handle is not None, "Error in sendline" |
| 6188 | assert "Command not found:" not in handle, handle |
| 6189 | assert "Unsupported command:" not in handle, handle |
| 6190 | if "Upgraded" in handle: |
| 6191 | return main.TRUE |
| 6192 | else: |
| 6193 | return main.FALSE |
| 6194 | except AssertionError: |
| 6195 | main.log.exception( "" ) |
| 6196 | return main.ERROR |
| 6197 | except TypeError: |
| 6198 | main.log.exception( self.name + ": Object not as expected" ) |
| 6199 | return main.ERROR |
| 6200 | except pexpect.EOF: |
| 6201 | main.log.error( self.name + ": EOF exception found" ) |
| 6202 | main.log.error( self.name + ": " + self.handle.before ) |
| 6203 | main.cleanAndExit() |
| 6204 | except Exception: |
| 6205 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6206 | main.cleanAndExit() |
| 6207 | |
| 6208 | def issuCommit( self ): |
| 6209 | """ |
| 6210 | Finalizes an In-Service Software Upgrade |
| 6211 | |
| 6212 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6213 | """ |
| 6214 | try: |
| 6215 | cmdStr = "issu commit" |
| 6216 | handle = self.sendline( cmdStr ) |
| 6217 | assert handle is not None, "Error in sendline" |
| 6218 | assert "Command not found:" not in handle, handle |
| 6219 | assert "Unsupported command:" not in handle, handle |
| 6220 | # TODO: Check the version returned by this command |
| 6221 | if "Committed version" in handle: |
| 6222 | return main.TRUE |
| 6223 | else: |
| 6224 | return main.FALSE |
| 6225 | except AssertionError: |
| 6226 | main.log.exception( "" ) |
| 6227 | return main.ERROR |
| 6228 | except TypeError: |
| 6229 | main.log.exception( self.name + ": Object not as expected" ) |
| 6230 | return main.ERROR |
| 6231 | except pexpect.EOF: |
| 6232 | main.log.error( self.name + ": EOF exception found" ) |
| 6233 | main.log.error( self.name + ": " + self.handle.before ) |
| 6234 | main.cleanAndExit() |
| 6235 | except Exception: |
| 6236 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6237 | main.cleanAndExit() |
| 6238 | |
| 6239 | def issuRollback( self ): |
| 6240 | """ |
| 6241 | Rolls back an In-Service Software Upgrade |
| 6242 | |
| 6243 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6244 | """ |
| 6245 | try: |
| 6246 | cmdStr = "issu rollback" |
| 6247 | handle = self.sendline( cmdStr ) |
| 6248 | assert handle is not None, "Error in sendline" |
| 6249 | assert "Command not found:" not in handle, handle |
| 6250 | assert "Unsupported command:" not in handle, handle |
| 6251 | # TODO: Check the version returned by this command |
| 6252 | if "Rolled back to version" in handle: |
| 6253 | return main.TRUE |
| 6254 | else: |
| 6255 | return main.FALSE |
| 6256 | except AssertionError: |
| 6257 | main.log.exception( "" ) |
| 6258 | return main.ERROR |
| 6259 | except TypeError: |
| 6260 | main.log.exception( self.name + ": Object not as expected" ) |
| 6261 | return main.ERROR |
| 6262 | except pexpect.EOF: |
| 6263 | main.log.error( self.name + ": EOF exception found" ) |
| 6264 | main.log.error( self.name + ": " + self.handle.before ) |
| 6265 | main.cleanAndExit() |
| 6266 | except Exception: |
| 6267 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6268 | main.cleanAndExit() |
| 6269 | |
| 6270 | def issuReset( self ): |
| 6271 | """ |
| 6272 | Resets the In-Service Software Upgrade status after a rollback |
| 6273 | |
| 6274 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6275 | """ |
| 6276 | try: |
| 6277 | cmdStr = "issu reset" |
| 6278 | handle = self.sendline( cmdStr ) |
| 6279 | assert handle is not None, "Error in sendline" |
| 6280 | assert "Command not found:" not in handle, handle |
| 6281 | assert "Unsupported command:" not in handle, handle |
| 6282 | # TODO: Check the version returned by this command |
| 6283 | if "Reset version" in handle: |
| 6284 | return main.TRUE |
| 6285 | else: |
| 6286 | return main.FALSE |
| 6287 | except AssertionError: |
| 6288 | main.log.exception( "" ) |
| 6289 | return main.ERROR |
| 6290 | except TypeError: |
| 6291 | main.log.exception( self.name + ": Object not as expected" ) |
| 6292 | return main.ERROR |
| 6293 | except pexpect.EOF: |
| 6294 | main.log.error( self.name + ": EOF exception found" ) |
| 6295 | main.log.error( self.name + ": " + self.handle.before ) |
| 6296 | main.cleanAndExit() |
| 6297 | except Exception: |
| 6298 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6299 | main.cleanAndExit() |
| 6300 | |
| 6301 | def issuStatus( self ): |
| 6302 | """ |
| 6303 | Status of an In-Service Software Upgrade |
| 6304 | |
| 6305 | Returns the output of the cli command or None on Error |
| 6306 | """ |
| 6307 | try: |
| 6308 | cmdStr = "issu status" |
| 6309 | handle = self.sendline( cmdStr ) |
| 6310 | assert handle is not None, "Error in sendline" |
| 6311 | assert "Command not found:" not in handle, handle |
| 6312 | assert "Unsupported command:" not in handle, handle |
| 6313 | return handle |
| 6314 | except AssertionError: |
| 6315 | main.log.exception( "" ) |
| 6316 | return None |
| 6317 | except TypeError: |
| 6318 | main.log.exception( self.name + ": Object not as expected" ) |
| 6319 | return None |
| 6320 | except pexpect.EOF: |
| 6321 | main.log.error( self.name + ": EOF exception found" ) |
| 6322 | main.log.error( self.name + ": " + self.handle.before ) |
| 6323 | main.cleanAndExit() |
| 6324 | except Exception: |
| 6325 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6326 | main.cleanAndExit() |
| 6327 | |
| 6328 | def issuVersion( self ): |
| 6329 | """ |
| 6330 | Get the version of an In-Service Software Upgrade |
| 6331 | |
| 6332 | Returns the output of the cli command or None on Error |
| 6333 | """ |
| 6334 | try: |
| 6335 | cmdStr = "issu version" |
| 6336 | handle = self.sendline( cmdStr ) |
| 6337 | assert handle is not None, "Error in sendline" |
| 6338 | assert "Command not found:" not in handle, handle |
| 6339 | assert "Unsupported command:" not in handle, handle |
| 6340 | return handle |
| 6341 | except AssertionError: |
| 6342 | main.log.exception( "" ) |
| 6343 | return None |
| 6344 | except TypeError: |
| 6345 | main.log.exception( self.name + ": Object not as expected" ) |
| 6346 | return None |
| 6347 | except pexpect.EOF: |
| 6348 | main.log.error( self.name + ": EOF exception found" ) |
| 6349 | main.log.error( self.name + ": " + self.handle.before ) |
| 6350 | main.cleanAndExit() |
| 6351 | except Exception: |
| 6352 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6353 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6354 | |
| 6355 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6356 | """ |
| 6357 | Create a multicast route by calling 'mcast-join' command |
| 6358 | sIP: source IP of the multicast route |
| 6359 | groupIP: group IP of the multicast route |
| 6360 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6361 | dPorts: a list of destination ports of the multicast route |
| 6362 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6363 | """ |
| 6364 | try: |
| 6365 | cmdStr = "mcast-join" |
| 6366 | cmdStr += " " + str( sIP ) |
| 6367 | cmdStr += " " + str( groupIP ) |
| 6368 | cmdStr += " " + str( sPort ) |
| 6369 | assert isinstance( dPorts, list ) |
| 6370 | for dPort in dPorts: |
| 6371 | cmdStr += " " + str( dPort ) |
| 6372 | handle = self.sendline( cmdStr ) |
| 6373 | assert handle is not None, "Error in sendline" |
| 6374 | assert "Command not found:" not in handle, handle |
| 6375 | assert "Unsupported command:" not in handle, handle |
| 6376 | assert "Error executing command" not in handle, handle |
| 6377 | if "Added the mcast route" in handle: |
| 6378 | return main.TRUE |
| 6379 | else: |
| 6380 | return main.FALSE |
| 6381 | except AssertionError: |
| 6382 | main.log.exception( "" ) |
| 6383 | return None |
| 6384 | except TypeError: |
| 6385 | main.log.exception( self.name + ": Object not as expected" ) |
| 6386 | return None |
| 6387 | except pexpect.EOF: |
| 6388 | main.log.error( self.name + ": EOF exception found" ) |
| 6389 | main.log.error( self.name + ": " + self.handle.before ) |
| 6390 | main.cleanAndExit() |
| 6391 | except Exception: |
| 6392 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6393 | main.cleanAndExit() |
| 6394 | |
| 6395 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6396 | """ |
| 6397 | Delete a multicast route by calling 'mcast-delete' command |
| 6398 | sIP: source IP of the multicast route |
| 6399 | groupIP: group IP of the multicast route |
| 6400 | dPorts: a list of destination ports of the multicast route |
| 6401 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6402 | """ |
| 6403 | try: |
| 6404 | cmdStr = "mcast-delete" |
| 6405 | cmdStr += " " + str( sIP ) |
| 6406 | cmdStr += " " + str( groupIP ) |
| 6407 | assert isinstance( dPorts, list ) |
| 6408 | for dPort in dPorts: |
| 6409 | cmdStr += " " + str( dPort ) |
| 6410 | handle = self.sendline( cmdStr ) |
| 6411 | assert handle is not None, "Error in sendline" |
| 6412 | assert "Command not found:" not in handle, handle |
| 6413 | assert "Unsupported command:" not in handle, handle |
| 6414 | assert "Error executing command" not in handle, handle |
| 6415 | if "Updated the mcast route" in handle: |
| 6416 | return main.TRUE |
| 6417 | else: |
| 6418 | return main.FALSE |
| 6419 | except AssertionError: |
| 6420 | main.log.exception( "" ) |
| 6421 | return None |
| 6422 | except TypeError: |
| 6423 | main.log.exception( self.name + ": Object not as expected" ) |
| 6424 | return None |
| 6425 | except pexpect.EOF: |
| 6426 | main.log.error( self.name + ": EOF exception found" ) |
| 6427 | main.log.error( self.name + ": " + self.handle.before ) |
| 6428 | main.cleanAndExit() |
| 6429 | except Exception: |
| 6430 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6431 | main.cleanAndExit() |
| 6432 | |
| 6433 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6434 | """ |
| 6435 | Create a multicast route by calling 'mcast-host-join' command |
| 6436 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6437 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6438 | 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] | 6439 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6440 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6441 | """ |
| 6442 | try: |
| 6443 | cmdStr = "mcast-host-join" |
| 6444 | cmdStr += " -sAddr " + str( sAddr ) |
| 6445 | cmdStr += " -gAddr " + str( gAddr ) |
| 6446 | assert isinstance( srcs, list ) |
| 6447 | for src in srcs: |
| 6448 | cmdStr += " -srcs " + str( src ) |
| 6449 | assert isinstance( sinks, list ) |
| 6450 | for sink in sinks: |
| 6451 | cmdStr += " -sinks " + str( sink ) |
| 6452 | handle = self.sendline( cmdStr ) |
| 6453 | assert handle is not None, "Error in sendline" |
| 6454 | assert "Command not found:" not in handle, handle |
| 6455 | assert "Unsupported command:" not in handle, handle |
| 6456 | assert "Error executing command" not in handle, handle |
| 6457 | if "Added the mcast route" in handle: |
| 6458 | return main.TRUE |
| 6459 | else: |
| 6460 | return main.FALSE |
| 6461 | except AssertionError: |
| 6462 | main.log.exception( "" ) |
| 6463 | return None |
| 6464 | except TypeError: |
| 6465 | main.log.exception( self.name + ": Object not as expected" ) |
| 6466 | return None |
| 6467 | except pexpect.EOF: |
| 6468 | main.log.error( self.name + ": EOF exception found" ) |
| 6469 | main.log.error( self.name + ": " + self.handle.before ) |
| 6470 | main.cleanAndExit() |
| 6471 | except Exception: |
| 6472 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6473 | main.cleanAndExit() |
| 6474 | |
| 6475 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6476 | """ |
| 6477 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6478 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6479 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6480 | 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] | 6481 | will delete the route if not specified |
| 6482 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6483 | """ |
| 6484 | try: |
| 6485 | cmdStr = "mcast-host-delete" |
| 6486 | cmdStr += " -sAddr " + str( sAddr ) |
| 6487 | cmdStr += " -gAddr " + str( gAddr ) |
| 6488 | if host: |
| 6489 | cmdStr += " -h " + str( host ) |
| 6490 | handle = self.sendline( cmdStr ) |
| 6491 | assert handle is not None, "Error in sendline" |
| 6492 | assert "Command not found:" not in handle, handle |
| 6493 | assert "Unsupported command:" not in handle, handle |
| 6494 | assert "Error executing command" not in handle, handle |
| 6495 | if "Updated the mcast route" in handle: |
| 6496 | return main.TRUE |
| 6497 | elif "Deleted the mcast route" in handle: |
| 6498 | return main.TRUE |
| 6499 | else: |
| 6500 | return main.FALSE |
| 6501 | except AssertionError: |
| 6502 | main.log.exception( "" ) |
| 6503 | return None |
| 6504 | except TypeError: |
| 6505 | main.log.exception( self.name + ": Object not as expected" ) |
| 6506 | return None |
| 6507 | except pexpect.EOF: |
| 6508 | main.log.error( self.name + ": EOF exception found" ) |
| 6509 | main.log.error( self.name + ": " + self.handle.before ) |
| 6510 | main.cleanAndExit() |
| 6511 | except Exception: |
| 6512 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6513 | main.cleanAndExit() |
| 6514 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6515 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6516 | """ |
| 6517 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6518 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6519 | gAddr: specifies multicast group address |
| 6520 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6521 | will delete the route if not specified |
| 6522 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6523 | """ |
| 6524 | try: |
| 6525 | cmdStr = "mcast-sink-delete" |
| 6526 | cmdStr += " -sAddr " + str( sAddr ) |
| 6527 | cmdStr += " -gAddr " + str( gAddr ) |
| 6528 | if sink: |
| 6529 | cmdStr += " -s " + str( sink ) |
| 6530 | handle = self.sendline( cmdStr ) |
| 6531 | assert handle is not None, "Error in sendline" |
| 6532 | assert "Command not found:" not in handle, handle |
| 6533 | assert "Unsupported command:" not in handle, handle |
| 6534 | assert "Error executing command" not in handle, handle |
| 6535 | if "Updated the mcast route" in handle: |
| 6536 | return main.TRUE |
| 6537 | elif "Deleted the mcast route" in handle: |
| 6538 | return main.TRUE |
| 6539 | else: |
| 6540 | return main.FALSE |
| 6541 | except AssertionError: |
| 6542 | main.log.exception( "" ) |
| 6543 | return None |
| 6544 | except TypeError: |
| 6545 | main.log.exception( self.name + ": Object not as expected" ) |
| 6546 | return None |
| 6547 | except pexpect.EOF: |
| 6548 | main.log.error( self.name + ": EOF exception found" ) |
| 6549 | main.log.error( self.name + ": " + self.handle.before ) |
| 6550 | main.cleanAndExit() |
| 6551 | except Exception: |
| 6552 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6553 | main.cleanAndExit() |
| 6554 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6555 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6556 | """ |
| 6557 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6558 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6559 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6560 | 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] | 6561 | will delete the route if not specified |
| 6562 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6563 | """ |
| 6564 | try: |
| 6565 | cmdStr = "mcast-source-delete" |
| 6566 | cmdStr += " -sAddr " + str( sAddr ) |
| 6567 | cmdStr += " -gAddr " + str( gAddr ) |
| 6568 | if srcs: |
| 6569 | assert isinstance( srcs, list ) |
| 6570 | for src in srcs: |
| 6571 | cmdStr += " -src " + str( src ) |
| 6572 | handle = self.sendline( cmdStr ) |
| 6573 | assert handle is not None, "Error in sendline" |
| 6574 | assert "Command not found:" not in handle, handle |
| 6575 | assert "Unsupported command:" not in handle, handle |
| 6576 | assert "Error executing command" not in handle, handle |
| 6577 | if "Updated the mcast route" in handle: |
| 6578 | return main.TRUE |
| 6579 | elif "Deleted the mcast route" in handle: |
| 6580 | return main.TRUE |
| 6581 | else: |
| 6582 | return main.FALSE |
| 6583 | except AssertionError: |
| 6584 | main.log.exception( "" ) |
| 6585 | return None |
| 6586 | except TypeError: |
| 6587 | main.log.exception( self.name + ": Object not as expected" ) |
| 6588 | return None |
| 6589 | except pexpect.EOF: |
| 6590 | main.log.error( self.name + ": EOF exception found" ) |
| 6591 | main.log.error( self.name + ": " + self.handle.before ) |
| 6592 | main.cleanAndExit() |
| 6593 | except Exception: |
| 6594 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6595 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6596 | |
| 6597 | def netcfg( self, jsonFormat=True, args="" ): |
| 6598 | """ |
| 6599 | Run netcfg cli command with given args |
| 6600 | """ |
| 6601 | try: |
| 6602 | cmdStr = "netcfg" |
| 6603 | if jsonFormat: |
| 6604 | cmdStr = cmdStr + " -j" |
| 6605 | if args: |
| 6606 | cmdStr = cmdStr + " " + str( args ) |
| 6607 | handle = self.sendline( cmdStr ) |
| 6608 | assert handle is not None, "Error in sendline" |
| 6609 | assert "Command not found:" not in handle, handle |
| 6610 | assert "Unsupported command:" not in handle, handle |
| 6611 | assert "Error executing command" not in handle, handle |
| 6612 | return handle |
| 6613 | except AssertionError: |
| 6614 | main.log.exception( "" ) |
| 6615 | return None |
| 6616 | except TypeError: |
| 6617 | main.log.exception( self.name + ": Object not as expected" ) |
| 6618 | return None |
| 6619 | except pexpect.EOF: |
| 6620 | main.log.error( self.name + ": EOF exception found" ) |
| 6621 | main.log.error( self.name + ": " + self.handle.before ) |
| 6622 | main.cleanAndExit() |
| 6623 | except Exception: |
| 6624 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6625 | main.cleanAndExit() |
| 6626 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6627 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6628 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6629 | 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] | 6630 | Options: |
| 6631 | sAddr: IP address of the source host |
| 6632 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6633 | ipv6: True if hosts are IPv6 |
| 6634 | verbose: return verbose t3 output if True |
| 6635 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6636 | """ |
| 6637 | try: |
| 6638 | # Collect information of both hosts from onos |
| 6639 | hosts = self.hosts() |
| 6640 | hosts = json.loads( hosts ) |
| 6641 | sHost = None |
| 6642 | dHost = None |
| 6643 | for host in hosts: |
| 6644 | if sAddr in host[ "ipAddresses" ]: |
| 6645 | sHost = host |
| 6646 | elif dAddr in host[ "ipAddresses" ]: |
| 6647 | dHost = host |
| 6648 | if sHost and dHost: |
| 6649 | break |
| 6650 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6651 | cmdList = [] |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6652 | if simple: |
| 6653 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6654 | cmdStr = "t3-troubleshoot-simple" |
| 6655 | if verbose: |
| 6656 | cmdStr += " -vv" |
| 6657 | if ipv6: |
| 6658 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6659 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6660 | cmdList.append( cmdStr ) |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6661 | else: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6662 | for location in sHost[ "locations" ]: |
| 6663 | cmdStr = "t3-troubleshoot" |
| 6664 | if verbose: |
| 6665 | cmdStr += " -vv" |
| 6666 | if ipv6: |
| 6667 | cmdStr += " -et ipv6" |
| 6668 | cmdStr += " -s " + str( sAddr ) |
| 6669 | cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] ) |
| 6670 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6671 | if sHost[ "vlan" ] != "None": |
| 6672 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6673 | cmdStr += " -d " + str( dAddr ) |
| 6674 | netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) ) |
| 6675 | netcfg = json.loads( netcfg ) |
| 6676 | assert netcfg, "Failed to get netcfg" |
| 6677 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
| 6678 | cmdList.append( cmdStr ) |
| 6679 | return cmdList |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6680 | except AssertionError: |
| 6681 | main.log.exception( "" ) |
| 6682 | return None |
| 6683 | except ( KeyError, TypeError ): |
| 6684 | main.log.exception( self.name + ": Object not as expected" ) |
| 6685 | return None |
| 6686 | except Exception: |
| 6687 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6688 | main.cleanAndExit() |
| 6689 | |
| 6690 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6691 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6692 | 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] | 6693 | Options: |
| 6694 | sAddr: IP address of the source host |
| 6695 | dAddr: IP address of the destination host |
| 6696 | """ |
| 6697 | try: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6698 | cmdList = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6699 | assert cmdList is not None, "composeT3Command returned None" |
| 6700 | t3Output = "" |
| 6701 | for cmdStr in cmdList: |
| 6702 | handle = self.sendline( cmdStr ) |
| 6703 | assert handle is not None, "Error in sendline" |
| 6704 | assert "Command not found:" not in handle, handle |
| 6705 | assert "Unsupported command:" not in handle, handle |
| 6706 | assert "Error executing command" not in handle, handle |
| 6707 | assert "Tracing packet" in handle |
| 6708 | t3Output += handle |
| 6709 | return t3Output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6710 | except AssertionError: |
| 6711 | main.log.exception( "" ) |
| 6712 | return None |
| 6713 | except pexpect.EOF: |
| 6714 | main.log.error( self.name + ": EOF exception found" ) |
| 6715 | main.log.error( self.name + ": " + self.handle.before ) |
| 6716 | main.cleanAndExit() |
| 6717 | except Exception: |
| 6718 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6719 | main.cleanAndExit() |