andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | OCT 13 2014 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5 | Copyright 2014 Open Networking Foundation (ONF) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 6 | |
| 7 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 8 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 9 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 10 | |
| 11 | TestON is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 14 | (at your option) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 15 | |
| 16 | TestON is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 23 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 25 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 26 | This driver enters the onos> prompt to issue commands. |
| 27 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 28 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 29 | functions and document properly. |
| 30 | |
| 31 | If you are a contributor to the driver, please |
| 32 | list your email here for future contact: |
| 33 | |
| 34 | jhall@onlab.us |
| 35 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 36 | shreya@onlab.us |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 37 | jeremyr@opennetworking.org |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 38 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 39 | import pexpect |
| 40 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 41 | import json |
| 42 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 43 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 44 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 45 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 46 | from core.graph import Graph |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 47 | from cStringIO import StringIO |
| 48 | from itertools import izip |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 49 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 51 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | def __init__( self ): |
| 53 | """ |
| 54 | Initialize client |
| 55 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 56 | self.name = None |
| 57 | self.home = None |
| 58 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 59 | self.karafUser = None |
| 60 | self.karafPass = None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 61 | self.karafPrompt = "sdn@root >" # FIXME: make configurable |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 62 | self.graph = Graph() |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 63 | super( OnosCliDriver, self ).__init__() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 64 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 65 | def checkOptions( self, var, defaultVar ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 66 | if var is None or var == "": |
| 67 | return defaultVar |
| 68 | return var |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 69 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 70 | def connect( self, **connectargs ): |
| 71 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 72 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 73 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 74 | try: |
| 75 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | vars( self )[ key ] = connectargs[ key ] |
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 ] |
| 85 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 86 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 87 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 88 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 89 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 90 | for key in self.options: |
| 91 | if key == 'onosIp': |
| 92 | self.onosIp = self.options[ 'onosIp' ] |
| 93 | break |
| 94 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 95 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 96 | |
| 97 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 98 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 99 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 100 | else: |
| 101 | main.log.info( self.name + |
| 102 | ": Trying to connect to " + |
| 103 | self.ip_address ) |
| 104 | |
| 105 | except KeyError: |
| 106 | main.log.info( "Invalid host name," + |
| 107 | " connecting to local host instead" ) |
| 108 | self.ip_address = 'localhost' |
| 109 | except Exception as inst: |
| 110 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 111 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 112 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 113 | user_name=self.user_name, |
| 114 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 115 | port=self.port, |
| 116 | pwd=self.pwd, |
| 117 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 118 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 119 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 120 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 121 | if self.handle: |
| 122 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 123 | else: |
| 124 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 125 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 126 | except TypeError: |
| 127 | main.log.exception( self.name + ": Object not as expected" ) |
| 128 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 129 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 130 | main.log.error( self.name + ": EOF exception found" ) |
| 131 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 132 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 133 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 134 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 135 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 136 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 137 | def disconnect( self ): |
| 138 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 139 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 140 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 141 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 142 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 143 | if self.handle: |
| 144 | i = self.logout() |
| 145 | if i == main.TRUE: |
| 146 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 147 | self.handle.expect( self.prompt ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 148 | self.handle.sendline( "exit" ) |
| 149 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 150 | except TypeError: |
| 151 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 152 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 153 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 154 | main.log.error( self.name + ": EOF exception found" ) |
| 155 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 156 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 157 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 158 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 159 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 160 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 161 | response = main.FALSE |
| 162 | return response |
| 163 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 164 | def logout( self ): |
| 165 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 166 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 167 | Returns main.TRUE if exited CLI and |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 168 | main.FALSE on timeout (not guranteed you are disconnected) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 169 | None on TypeError |
| 170 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 171 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 172 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 173 | if self.handle: |
| 174 | self.handle.sendline( "" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 175 | i = self.handle.expect( [ self.karafPrompt, self.prompt, pexpect.TIMEOUT ], |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 176 | timeout=10 ) |
| 177 | if i == 0: # In ONOS CLI |
| 178 | self.handle.sendline( "logout" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 179 | j = self.handle.expect( [ self.prompt, |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 180 | "Command not found:", |
| 181 | pexpect.TIMEOUT ] ) |
| 182 | if j == 0: # Successfully logged out |
| 183 | return main.TRUE |
| 184 | elif j == 1 or j == 2: |
| 185 | # ONOS didn't fully load, and logout command isn't working |
| 186 | # or the command timed out |
| 187 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 188 | try: |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 189 | self.handle.expect( self.prompt ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 190 | except pexpect.TIMEOUT: |
| 191 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 192 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 193 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 194 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 195 | repr( self.handle.before ) ) |
| 196 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 197 | elif i == 1: # not in CLI |
| 198 | return main.TRUE |
| 199 | elif i == 3: # Timeout |
| 200 | return main.FALSE |
| 201 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 202 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 203 | except TypeError: |
| 204 | main.log.exception( self.name + ": Object not as expected" ) |
| 205 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 206 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 207 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 208 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 209 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 210 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 211 | main.log.error( self.name + |
| 212 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 213 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 214 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 215 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 216 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 217 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 218 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 219 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 220 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 221 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 222 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 223 | try: |
| 224 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 225 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 226 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 227 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 228 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 229 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 230 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 231 | # and that this driver will have to change accordingly |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 232 | self.handle.expect( str( cellname ) ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 233 | handleBefore = self.handle.before |
| 234 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 235 | # Get the rest of the handle |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 236 | self.handle.sendline( "" ) |
| 237 | self.handle.expect( self.prompt ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 238 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 239 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 240 | main.log.info( "Cell call returned: " + handleBefore + |
| 241 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 242 | |
| 243 | return main.TRUE |
| 244 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 245 | except TypeError: |
| 246 | main.log.exception( self.name + ": Object not as expected" ) |
| 247 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 248 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 249 | main.log.error( self.name + ": eof exception found" ) |
| 250 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 251 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 252 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 253 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 254 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 255 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 256 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 257 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 258 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 259 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 260 | by user would be used to set the current karaf shell idle timeout. |
| 261 | 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] | 262 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 263 | Below is an example to start a session with 60 seconds idle timeout |
| 264 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 265 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 266 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 267 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 268 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 269 | Note: karafTimeout is left as str so that this could be read |
| 270 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 271 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 272 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 273 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 274 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 275 | self.handle.sendline( "" ) |
| 276 | x = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 277 | self.prompt, self.karafPrompt ], commandlineTimeout ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 278 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 279 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 280 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 281 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 282 | # Not in CLI so login |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 283 | if waitForStart: |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 284 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
| 285 | startCliCommand = "onos-wait-for-start " |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 286 | else: |
| 287 | startCliCommand = "onos " |
| 288 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 289 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 290 | self.karafPrompt, |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 291 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 292 | |
| 293 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 294 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 295 | if karafTimeout: # FIXME: This doesn't look right |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 296 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 297 | "config:property-set -p org.apache.karaf.shell\ |
| 298 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 299 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 300 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 301 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 302 | self.handle.expect( self.karafPrompt ) |
| 303 | main.log.debug( self.handle.before ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 304 | return main.TRUE |
| 305 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 306 | # If failed, send ctrl+c to process and try again |
| 307 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 308 | self.handle.send( "\x03" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 309 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 310 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 311 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 312 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 313 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 314 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 315 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 316 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 317 | "config:property-set -p org.apache.karaf.shell\ |
| 318 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 319 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 320 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 321 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 322 | self.handle.expect( self.karafPrompt ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 323 | return main.TRUE |
| 324 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 325 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 326 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 327 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 328 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 329 | except TypeError: |
| 330 | main.log.exception( self.name + ": Object not as expected" ) |
| 331 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 332 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 333 | main.log.error( self.name + ": EOF exception found" ) |
| 334 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 335 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 336 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 337 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 338 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 339 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 340 | def startCellCli( self, karafTimeout="", |
| 341 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 342 | """ |
| 343 | Start CLI on onos ecll handle. |
| 344 | |
| 345 | karafTimeout is an optional argument. karafTimeout value passed |
| 346 | by user would be used to set the current karaf shell idle timeout. |
| 347 | Note that when ever this property is modified the shell will exit and |
| 348 | the subsequent login would reflect new idle timeout. |
| 349 | Below is an example to start a session with 60 seconds idle timeout |
| 350 | ( input value is in milliseconds ): |
| 351 | |
| 352 | tValue = "60000" |
| 353 | |
| 354 | Note: karafTimeout is left as str so that this could be read |
| 355 | and passed to startOnosCli from PARAMS file as str. |
| 356 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 357 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 358 | try: |
| 359 | self.handle.sendline( "" ) |
| 360 | x = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 361 | self.prompt, self.karafPrompt ], commandlineTimeout ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 362 | |
| 363 | if x == 1: |
| 364 | main.log.info( "ONOS cli is already running" ) |
| 365 | return main.TRUE |
| 366 | |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 367 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 368 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 369 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 370 | self.karafPrompt, |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 371 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 372 | |
| 373 | if i == 0: |
| 374 | main.log.info( self.name + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 375 | if karafTimeout: # FIXME: This doesn't look right |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 376 | self.handle.sendline( |
| 377 | "config:property-set -p org.apache.karaf.shell\ |
| 378 | sshIdleTimeout " + |
| 379 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 380 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 381 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 382 | self.handle.expect( self.karafPrompt ) |
| 383 | main.log.debug( self.handle.before ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 384 | return main.TRUE |
| 385 | else: |
| 386 | # If failed, send ctrl+c to process and try again |
| 387 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 388 | self.handle.send( "\x03" ) |
| 389 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 390 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 391 | timeout=30 ) |
| 392 | if i == 0: |
| 393 | main.log.info( self.name + " CLI Started " + |
| 394 | "successfully after retry attempt" ) |
| 395 | if karafTimeout: |
| 396 | self.handle.sendline( |
| 397 | "config:property-set -p org.apache.karaf.shell\ |
| 398 | sshIdleTimeout " + |
| 399 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 400 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 401 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 402 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 403 | return main.TRUE |
| 404 | else: |
| 405 | main.log.error( "Connection to CLI " + |
| 406 | self.name + " timeout" ) |
| 407 | return main.FALSE |
| 408 | |
| 409 | except TypeError: |
| 410 | main.log.exception( self.name + ": Object not as expected" ) |
| 411 | return None |
| 412 | except pexpect.EOF: |
| 413 | main.log.error( self.name + ": EOF exception found" ) |
| 414 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 415 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 416 | except Exception: |
| 417 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 418 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 419 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 420 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 421 | """ |
| 422 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 423 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 424 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 425 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 426 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 427 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 428 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 429 | """ |
| 430 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 431 | lvlStr = "" |
| 432 | if level: |
| 433 | lvlStr = "--level=" + level |
| 434 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 435 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 436 | self.handle.expect( "log:log" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 437 | self.handle.expect( self.karafPrompt ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 438 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 439 | response = self.handle.before |
| 440 | if re.search( "Error", response ): |
| 441 | return main.FALSE |
| 442 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 443 | except pexpect.TIMEOUT: |
| 444 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 445 | if noExit: |
| 446 | main.cleanup() |
| 447 | return None |
| 448 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 449 | main.cleanAndExit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 450 | except pexpect.EOF: |
| 451 | main.log.error( self.name + ": EOF exception found" ) |
| 452 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 453 | if noExit: |
| 454 | main.cleanup() |
| 455 | return None |
| 456 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 457 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 458 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 459 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 460 | if noExit: |
| 461 | main.cleanup() |
| 462 | return None |
| 463 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 464 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 465 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 466 | def clearBuffer( self, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 467 | """ |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 468 | Test cli connection and clear any left over output in the buffer |
| 469 | Optional Arguments: |
| 470 | debug - Defaults to False. If True, will enable debug logging. |
| 471 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 472 | before a timeout. |
| 473 | 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] | 474 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 475 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 476 | # Try to reconnect if disconnected from cli |
| 477 | self.handle.sendline( "" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 478 | i = self.handle.expect( [ self.karafPrompt, self.prompt, pexpect.TIMEOUT ] ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 479 | response = self.handle.before |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 480 | if i == 1: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 481 | main.log.error( self.name + ": onos cli session closed. " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 482 | if self.onosIp: |
| 483 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 484 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 485 | if reconnectResult: |
| 486 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 487 | else: |
| 488 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 489 | if noExit: |
| 490 | return None |
| 491 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 492 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 493 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 494 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 495 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 496 | main.log.warn( "Timeout when testing cli responsiveness" ) |
| 497 | main.log.debug( self.handle.before ) |
| 498 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 499 | self.handle.expect( self.karafPrompt ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 500 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 501 | response += self.handle.before |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 502 | if debug: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 503 | main.log.debug( self.name + ": Raw output from sending ''" ) |
| 504 | main.log.debug( self.name + ": " + repr( response ) ) |
| 505 | except pexpect.TIMEOUT: |
| 506 | main.log.error( self.name + ": ONOS timeout" ) |
| 507 | main.log.debug( self.handle.before ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 508 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 509 | self.handle.expect( self.karafPrompt ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 510 | return None |
| 511 | except pexpect.EOF: |
| 512 | main.log.error( self.name + ": EOF exception found" ) |
| 513 | main.log.error( self.name + ": " + self.handle.before ) |
| 514 | if noExit: |
| 515 | return None |
| 516 | else: |
| 517 | main.cleanAndExit() |
| 518 | except Exception: |
| 519 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 520 | if noExit: |
| 521 | return None |
| 522 | else: |
| 523 | main.cleanAndExit() |
| 524 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 525 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 526 | """ |
| 527 | A wrapper around pexpect's sendline/expect. Will return all the output from a given command |
| 528 | |
| 529 | Required Arguments: |
| 530 | cmdStr - String to send to the pexpect session |
| 531 | |
| 532 | Optional Arguments: |
| 533 | showResponse - Defaults to False. If True will log the response. |
| 534 | debug - Defaults to False. If True, will enable debug logging. |
| 535 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 536 | before a timeout. |
| 537 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 538 | closed channel, but instead return None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 539 | 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] | 540 | |
| 541 | Warning: There are no sanity checking to commands sent using this method. |
| 542 | |
| 543 | """ |
| 544 | try: |
| 545 | # Try to reconnect if disconnected from cli |
| 546 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
| 547 | if debug: |
| 548 | # NOTE: This adds an average of .4 seconds per call |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 549 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 550 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 551 | self.handle.sendline( cmdStr ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 552 | self.handle.expect( self.karafPrompt, timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 553 | response = self.handle.before |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 554 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 555 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 556 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 557 | main.log.debug( self.name + ": Raw output" ) |
| 558 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 559 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 560 | # Remove control codes from karaf 4.2.1 |
| 561 | karafEscape = re.compile( r"('(0|1)~\'|\r\r\r\n\x1b\[A\x1b\[79Cx|\x1b(>|=)|\x1b\[90m~)" ) |
| 562 | response = karafEscape.sub( '', response ) |
| 563 | if debug: |
| 564 | main.log.debug( self.name + ": karafEscape output" ) |
| 565 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 566 | # Remove ANSI color control strings from output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 567 | ansiEscape = re.compile( r'((\x9b|\x1b\[)[0-?]*[ -/]*[@-~])' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 568 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 569 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 570 | main.log.debug( self.name + ": ansiEscape output" ) |
| 571 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 572 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 573 | # Remove ANSI color control strings from output |
| 574 | backspaceEscape = re.compile( r'((..\x08\x08)|(.|\s)\x08)' ) |
| 575 | response = backspaceEscape.sub( '', response ) |
| 576 | if debug: |
| 577 | main.log.debug( self.name + ": backspaceEscape output" ) |
| 578 | main.log.debug( self.name + ": " + repr( response ) ) |
| 579 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 580 | # Remove extra return chars that get added |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 581 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 582 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 583 | main.log.debug( self.name + ": Removed extra returns " + |
| 584 | "from output" ) |
| 585 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 586 | |
| 587 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 588 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 589 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 590 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 591 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 592 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 593 | # parse for just the output, remove the cmd from response |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 594 | if relaxedRegex: |
| 595 | # This was added because karaf 4.2 is stripping some characters from the command echo |
| 596 | endStr = cmdStr.split( '|' )[-1] |
| 597 | main.log.warn( endStr ) |
| 598 | output = response.split( endStr.strip(), 1 ) |
| 599 | else: |
| 600 | output = response.split( cmdStr.strip(), 1 ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 601 | if output: |
| 602 | if debug: |
| 603 | main.log.debug( self.name + ": split output" ) |
| 604 | for r in output: |
| 605 | main.log.debug( self.name + ": " + repr( r ) ) |
| 606 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 607 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 608 | main.log.info( "Response from ONOS: {}".format( output ) ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 609 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 610 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 611 | except pexpect.TIMEOUT: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 612 | main.log.error( self.name + ": ONOS timeout" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 613 | if debug: |
| 614 | main.log.debug( self.handle.before ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 615 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 616 | self.handle.expect( self.karafPrompt ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 617 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 618 | except IndexError: |
| 619 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 620 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 621 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 622 | except TypeError: |
| 623 | main.log.exception( self.name + ": Object not as expected" ) |
| 624 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 625 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 626 | main.log.error( self.name + ": EOF exception found" ) |
| 627 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 628 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 629 | return None |
| 630 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 631 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 632 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 633 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 634 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 635 | return None |
| 636 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 637 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 638 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 639 | # IMPORTANT NOTE: |
| 640 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 641 | # the cli command changing 'a:b' with 'aB'. |
| 642 | # Ex ) onos:topology > onosTopology |
| 643 | # onos:links > onosLinks |
| 644 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 645 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 646 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 647 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 648 | Adds a new cluster node by ID and address information. |
| 649 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 650 | * nodeId |
| 651 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 652 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 653 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 654 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 655 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 656 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 657 | str( ONOSIp ) + " " + str( tcpPort ) |
| 658 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 659 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 660 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 661 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 662 | main.log.error( self.name + ": Error in adding node" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 663 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 664 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 665 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 666 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 667 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 668 | except AssertionError: |
| 669 | main.log.exception( "" ) |
| 670 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 671 | except TypeError: |
| 672 | main.log.exception( self.name + ": Object not as expected" ) |
| 673 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 674 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 675 | main.log.error( self.name + ": EOF exception found" ) |
| 676 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 677 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 678 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 679 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 680 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 681 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 682 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 683 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 684 | Removes a cluster by ID |
| 685 | Issues command: 'remove-node [<node-id>]' |
| 686 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 687 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 688 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 689 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 690 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 691 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 692 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 693 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 694 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 695 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 696 | main.log.error( self.name + ": Error in removing node" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 697 | main.log.error( handle ) |
| 698 | return main.FALSE |
| 699 | else: |
| 700 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 701 | except AssertionError: |
| 702 | main.log.exception( "" ) |
| 703 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 704 | except TypeError: |
| 705 | main.log.exception( self.name + ": Object not as expected" ) |
| 706 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 707 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 708 | main.log.error( self.name + ": EOF exception found" ) |
| 709 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 710 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 711 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 712 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 713 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 714 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 715 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 716 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 717 | List the nodes currently visible |
| 718 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 719 | Optional argument: |
| 720 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 721 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 722 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 723 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 724 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 725 | cmdStr += " -j" |
| 726 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 727 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 728 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 729 | return output |
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 | 7c21157 | 2014-10-15 16:45:20 -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 | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 743 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 744 | def topology( self ): |
| 745 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 746 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 747 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 748 | Return: |
| 749 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 750 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 751 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 752 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 753 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 754 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 755 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 756 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 757 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 758 | except AssertionError: |
| 759 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 760 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 761 | except TypeError: |
| 762 | main.log.exception( self.name + ": Object not as expected" ) |
| 763 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 764 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 765 | main.log.error( self.name + ": EOF exception found" ) |
| 766 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 767 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 768 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 769 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 770 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 771 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 772 | def deviceRemove( self, deviceId ): |
| 773 | """ |
| 774 | Removes particular device from storage |
| 775 | |
| 776 | TODO: refactor this function |
| 777 | """ |
| 778 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 779 | cmdStr = "device-remove " + str( deviceId ) |
| 780 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 781 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 782 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 783 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 784 | main.log.error( self.name + ": Error in removing device" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 785 | main.log.error( handle ) |
| 786 | return main.FALSE |
| 787 | else: |
| 788 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 789 | except AssertionError: |
| 790 | main.log.exception( "" ) |
| 791 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 792 | except TypeError: |
| 793 | main.log.exception( self.name + ": Object not as expected" ) |
| 794 | return None |
| 795 | except pexpect.EOF: |
| 796 | main.log.error( self.name + ": EOF exception found" ) |
| 797 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 798 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 799 | except Exception: |
| 800 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 801 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 802 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 803 | def devices( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 804 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 805 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 806 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 807 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 808 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 809 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 810 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 811 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 812 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 813 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 814 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 815 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 816 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 817 | except AssertionError: |
| 818 | main.log.exception( "" ) |
| 819 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 820 | except TypeError: |
| 821 | main.log.exception( self.name + ": Object not as expected" ) |
| 822 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 823 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 824 | main.log.error( self.name + ": EOF exception found" ) |
| 825 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 826 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 827 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 828 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 829 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 830 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 831 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 832 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 833 | This balances the devices across all controllers |
| 834 | by issuing command: 'onos> onos:balance-masters' |
| 835 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 836 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 837 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 838 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 839 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 840 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 841 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 842 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 843 | main.log.error( self.name + ": Error in balancing masters" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 844 | main.log.error( handle ) |
| 845 | return main.FALSE |
| 846 | else: |
| 847 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 848 | except AssertionError: |
| 849 | main.log.exception( "" ) |
| 850 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 851 | except TypeError: |
| 852 | main.log.exception( self.name + ": Object not as expected" ) |
| 853 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 854 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 855 | main.log.error( self.name + ": EOF exception found" ) |
| 856 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 857 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 858 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 859 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 860 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 861 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 862 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 863 | """ |
| 864 | Returns the output of the masters command. |
| 865 | Optional argument: |
| 866 | * jsonFormat - boolean indicating if you want output in json |
| 867 | """ |
| 868 | try: |
| 869 | cmdStr = "onos:masters" |
| 870 | if jsonFormat: |
| 871 | cmdStr += " -j" |
| 872 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 873 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 874 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 875 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 876 | except AssertionError: |
| 877 | main.log.exception( "" ) |
| 878 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 879 | except TypeError: |
| 880 | main.log.exception( self.name + ": Object not as expected" ) |
| 881 | return None |
| 882 | except pexpect.EOF: |
| 883 | main.log.error( self.name + ": EOF exception found" ) |
| 884 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 885 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 886 | except Exception: |
| 887 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 888 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 889 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 890 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 891 | """ |
| 892 | Uses the master command to check that the devices' leadership |
| 893 | is evenly divided |
| 894 | |
| 895 | Dependencies: checkMasters() and summary() |
| 896 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 897 | Returns main.TRUE if the devices are balanced |
| 898 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 899 | Exits on Exception |
| 900 | Returns None on TypeError |
| 901 | """ |
| 902 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 903 | summaryOutput = self.summary() |
| 904 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 905 | except ( TypeError, ValueError ): |
| 906 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 907 | return None |
| 908 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 909 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 910 | mastersOutput = self.checkMasters() |
| 911 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 912 | first = masters[ 0 ][ "size" ] |
| 913 | for master in masters: |
| 914 | totalOwnedDevices += master[ "size" ] |
| 915 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 916 | main.log.error( "Mastership not balanced" ) |
| 917 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 918 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 919 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 920 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 921 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 922 | except ( TypeError, ValueError ): |
| 923 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 924 | return None |
| 925 | except pexpect.EOF: |
| 926 | main.log.error( self.name + ": EOF exception found" ) |
| 927 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 928 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 929 | except Exception: |
| 930 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 931 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 932 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 933 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 934 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 935 | Lists all core links |
| 936 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 937 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 938 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 939 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 940 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 941 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 942 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 943 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 944 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 945 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 946 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 947 | except AssertionError: |
| 948 | main.log.exception( "" ) |
| 949 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 950 | except TypeError: |
| 951 | main.log.exception( self.name + ": Object not as expected" ) |
| 952 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 953 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 954 | main.log.error( self.name + ": EOF exception found" ) |
| 955 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 956 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 957 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 958 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 959 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 960 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 961 | def ports( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 962 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 963 | Lists all ports |
| 964 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 965 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 966 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 967 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 968 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 969 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 970 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 971 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 972 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 973 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 974 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 975 | except AssertionError: |
| 976 | main.log.exception( "" ) |
| 977 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 978 | except TypeError: |
| 979 | main.log.exception( self.name + ": Object not as expected" ) |
| 980 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 981 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 982 | main.log.error( self.name + ": EOF exception found" ) |
| 983 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 984 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 985 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 986 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 987 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 988 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 989 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 990 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 991 | Lists all devices and the controllers with roles assigned to them |
| 992 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 993 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 994 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 995 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 996 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 997 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 998 | cmdStr += " -j" |
| 999 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1000 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1001 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1002 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1003 | except AssertionError: |
| 1004 | main.log.exception( "" ) |
| 1005 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1006 | except TypeError: |
| 1007 | main.log.exception( self.name + ": Object not as expected" ) |
| 1008 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1009 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1010 | main.log.error( self.name + ": EOF exception found" ) |
| 1011 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1012 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1013 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1014 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1015 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1016 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1017 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1018 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1019 | Given the a string containing the json representation of the "roles" |
| 1020 | cli command and a partial or whole device id, returns a json object |
| 1021 | containing the roles output for the first device whose id contains |
| 1022 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1023 | |
| 1024 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1025 | A dict of the role assignments for the given device or |
| 1026 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1027 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1028 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1029 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1030 | return None |
| 1031 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1032 | rawRoles = self.roles() |
| 1033 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1034 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1035 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1036 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1037 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1038 | return device |
| 1039 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1040 | except ( TypeError, ValueError ): |
| 1041 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1042 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -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 | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1050 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1051 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1052 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1053 | Iterates through each device and checks if there is a master assigned |
| 1054 | Returns: main.TRUE if each device has a master |
| 1055 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1056 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1057 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1058 | rawRoles = self.roles() |
| 1059 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1060 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1061 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1062 | # print device |
| 1063 | if device[ 'master' ] == "none": |
| 1064 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1065 | return main.FALSE |
| 1066 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1067 | except ( TypeError, ValueError ): |
| 1068 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1069 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1070 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1071 | main.log.error( self.name + ": EOF exception found" ) |
| 1072 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1073 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1074 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1075 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1076 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1077 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1078 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1079 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1080 | Returns string of paths, and the cost. |
| 1081 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1082 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1083 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1084 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1085 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1086 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1087 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1088 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1089 | main.log.error( self.name + ": Error in getting paths" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1090 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1091 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1092 | path = handle.split( ";" )[ 0 ] |
| 1093 | cost = handle.split( ";" )[ 1 ] |
| 1094 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1095 | except AssertionError: |
| 1096 | main.log.exception( "" ) |
| 1097 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1098 | except TypeError: |
| 1099 | main.log.exception( self.name + ": Object not as expected" ) |
| 1100 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1101 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1102 | main.log.error( self.name + ": EOF exception found" ) |
| 1103 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1104 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1105 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1106 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1107 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1108 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1109 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1110 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1111 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1112 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1113 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1114 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1115 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1116 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1117 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1118 | cmdStr += " -j" |
| 1119 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1120 | if handle: |
| 1121 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1122 | # TODO: Maybe make this less hardcoded |
| 1123 | # ConsistentMap Exceptions |
| 1124 | assert "org.onosproject.store.service" not in handle |
| 1125 | # Node not leader |
| 1126 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1127 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1128 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1129 | main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1130 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1131 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1132 | except TypeError: |
| 1133 | main.log.exception( self.name + ": Object not as expected" ) |
| 1134 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1135 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1136 | main.log.error( self.name + ": EOF exception found" ) |
| 1137 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1138 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1139 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1140 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1141 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1142 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1143 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1144 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1145 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1146 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1147 | 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] | 1148 | partial mac address |
| 1149 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1150 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1151 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1152 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1153 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1154 | return None |
| 1155 | else: |
| 1156 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1157 | rawHosts = self.hosts() |
| 1158 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1159 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1160 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1161 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1162 | if not host: |
| 1163 | pass |
| 1164 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1165 | return host |
| 1166 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1167 | except ( TypeError, ValueError ): |
| 1168 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1169 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1170 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1171 | main.log.error( self.name + ": EOF exception found" ) |
| 1172 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1173 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1174 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1175 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1176 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1177 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1178 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1179 | """ |
| 1180 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1181 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1182 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1183 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1184 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1185 | IMPORTANT: |
| 1186 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1187 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1188 | Furthermore, it assumes that value of VLAN is '-1' |
| 1189 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1190 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1191 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1192 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1193 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1194 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1195 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1196 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1197 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1198 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1199 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1200 | i = iter( str( hostHex ) ) |
| 1201 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1202 | hostHex = hostHex + "/-1" |
| 1203 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1204 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1205 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1206 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1207 | except TypeError: |
| 1208 | main.log.exception( self.name + ": Object not as expected" ) |
| 1209 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1210 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1211 | main.log.error( self.name + ": EOF exception found" ) |
| 1212 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1213 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1214 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1215 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1216 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1217 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1218 | def verifyHostLocation( self, hostIp, location ): |
| 1219 | """ |
| 1220 | Description: |
| 1221 | Verify the host given is discovered in all locations expected |
| 1222 | Required: |
| 1223 | hostIp: IP address of the host |
| 1224 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1225 | Could be a string or list |
| 1226 | Returns: |
| 1227 | main.TRUE if host is discovered on all locations provided |
| 1228 | main.FALSE otherwise |
| 1229 | """ |
| 1230 | import json |
| 1231 | locations = [ location ] if isinstance( location, str ) else location |
| 1232 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1233 | try: |
| 1234 | hosts = self.hosts() |
| 1235 | hosts = json.loads( hosts ) |
| 1236 | targetHost = None |
| 1237 | for host in hosts: |
| 1238 | if hostIp in host[ "ipAddresses" ]: |
| 1239 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1240 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1241 | result = main.TRUE |
| 1242 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1243 | for loc in locations: |
| 1244 | discovered = False |
| 1245 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1246 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1247 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1248 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1249 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1250 | break |
| 1251 | if discovered: |
| 1252 | locationsDiscovered.remove( locDiscovered ) |
| 1253 | else: |
| 1254 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1255 | result = main.FALSE |
| 1256 | if locationsDiscovered: |
| 1257 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1258 | result = main.FALSE |
| 1259 | return result |
| 1260 | except KeyError: |
| 1261 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1262 | return None |
| 1263 | except pexpect.EOF: |
| 1264 | main.log.error( self.name + ": EOF exception found" ) |
| 1265 | main.log.error( self.name + ": " + self.handle.before ) |
| 1266 | main.cleanAndExit() |
| 1267 | except Exception: |
| 1268 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1269 | return None |
| 1270 | |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1271 | def verifyHostIp( self, hostList=[], prefix="" ): |
| 1272 | """ |
| 1273 | Description: |
| 1274 | Verify that all hosts have IP address assigned to them |
| 1275 | Optional: |
| 1276 | hostList: If specified, verifications only happen to the hosts |
| 1277 | in hostList |
| 1278 | prefix: at least one of the ip address assigned to the host |
| 1279 | needs to have the specified prefix |
| 1280 | Returns: |
| 1281 | main.TRUE if all hosts have specific IP address assigned; |
| 1282 | main.FALSE otherwise |
| 1283 | """ |
| 1284 | import json |
| 1285 | try: |
| 1286 | hosts = self.hosts() |
| 1287 | hosts = json.loads( hosts ) |
| 1288 | if not hostList: |
| 1289 | hostList = [ host[ "id" ] for host in hosts ] |
| 1290 | for host in hosts: |
| 1291 | hostId = host[ "id" ] |
| 1292 | if hostId not in hostList: |
| 1293 | continue |
| 1294 | ipList = host[ "ipAddresses" ] |
| 1295 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1296 | if not ipList: |
| 1297 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1298 | else: |
| 1299 | if not any( ip.startswith( str( prefix ) ) for ip in ipList ): |
| 1300 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1301 | else: |
| 1302 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1303 | hostList.remove( hostId ) |
| 1304 | if hostList: |
| 1305 | main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) ) |
| 1306 | return main.FALSE |
| 1307 | else: |
| 1308 | return main.TRUE |
| 1309 | except KeyError: |
| 1310 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1311 | return None |
| 1312 | except pexpect.EOF: |
| 1313 | main.log.error( self.name + ": EOF exception found" ) |
| 1314 | main.log.error( self.name + ": " + self.handle.before ) |
| 1315 | main.cleanAndExit() |
| 1316 | except Exception: |
| 1317 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1318 | return None |
| 1319 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1320 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1321 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1322 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1323 | * hostIdOne: ONOS host id for host1 |
| 1324 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1325 | Optional: |
| 1326 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1327 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1328 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1329 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1330 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1331 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1332 | Returns: |
| 1333 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1334 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1335 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1336 | cmdStr = "add-host-intent " |
| 1337 | if vlanId: |
| 1338 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1339 | if setVlan: |
| 1340 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1341 | if encap: |
| 1342 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1343 | if bandwidth: |
| 1344 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1345 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1346 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1347 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1348 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1349 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1350 | main.log.error( self.name + ": Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1351 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1352 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1353 | else: |
| 1354 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1355 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1356 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1357 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1358 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1359 | else: |
| 1360 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1361 | main.log.debug( "Response from ONOS was: " + |
| 1362 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1363 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1364 | except AssertionError: |
| 1365 | main.log.exception( "" ) |
| 1366 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1367 | except TypeError: |
| 1368 | main.log.exception( self.name + ": Object not as expected" ) |
| 1369 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1370 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1371 | main.log.error( self.name + ": EOF exception found" ) |
| 1372 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1373 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1374 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1375 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1376 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1377 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1378 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1379 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1380 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1381 | * ingressDevice: device id of ingress device |
| 1382 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1383 | Optional: |
| 1384 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1385 | Description: |
| 1386 | Adds an optical intent by specifying an ingress and egress device |
| 1387 | Returns: |
| 1388 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1389 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1390 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1391 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1392 | " " + str( egressDevice ) |
| 1393 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1394 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1395 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1396 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1397 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1398 | main.log.error( self.name + ": Error in adding Optical intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1399 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1400 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1401 | main.log.info( "Optical intent installed between " + |
| 1402 | str( ingressDevice ) + " and " + |
| 1403 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1404 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1405 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1406 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1407 | else: |
| 1408 | main.log.error( "Error, intent ID not found" ) |
| 1409 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1410 | except AssertionError: |
| 1411 | main.log.exception( "" ) |
| 1412 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1413 | except TypeError: |
| 1414 | main.log.exception( self.name + ": Object not as expected" ) |
| 1415 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1416 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1417 | main.log.error( self.name + ": EOF exception found" ) |
| 1418 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1419 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1420 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1421 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1422 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1423 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1424 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1425 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1426 | ingressDevice, |
| 1427 | egressDevice, |
| 1428 | portIngress="", |
| 1429 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1430 | ethType="", |
| 1431 | ethSrc="", |
| 1432 | ethDst="", |
| 1433 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1434 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1435 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1436 | ipProto="", |
| 1437 | ipSrc="", |
| 1438 | ipDst="", |
| 1439 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1440 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1441 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1442 | setVlan="", |
| 1443 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1444 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1445 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1446 | * ingressDevice: device id of ingress device |
| 1447 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1448 | Optional: |
| 1449 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1450 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1451 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1452 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1453 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1454 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1455 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1456 | * ipSrc: specify ip source address |
| 1457 | * ipDst: specify ip destination address |
| 1458 | * tcpSrc: specify tcp source port |
| 1459 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1460 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1461 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1462 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1463 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1464 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1465 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1466 | Returns: |
| 1467 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1468 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1469 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1470 | options developers provide for point-to-point |
| 1471 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1472 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1473 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1474 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1475 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1476 | if ethType: |
| 1477 | cmd += " --ethType " + str( ethType ) |
| 1478 | if ethSrc: |
| 1479 | cmd += " --ethSrc " + str( ethSrc ) |
| 1480 | if ethDst: |
| 1481 | cmd += " --ethDst " + str( ethDst ) |
| 1482 | if bandwidth: |
| 1483 | cmd += " --bandwidth " + str( bandwidth ) |
| 1484 | if lambdaAlloc: |
| 1485 | cmd += " --lambda " |
| 1486 | if ipProto: |
| 1487 | cmd += " --ipProto " + str( ipProto ) |
| 1488 | if ipSrc: |
| 1489 | cmd += " --ipSrc " + str( ipSrc ) |
| 1490 | if ipDst: |
| 1491 | cmd += " --ipDst " + str( ipDst ) |
| 1492 | if tcpSrc: |
| 1493 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1494 | if tcpDst: |
| 1495 | cmd += " --tcpDst " + str( tcpDst ) |
| 1496 | if vlanId: |
| 1497 | cmd += " -v " + str( vlanId ) |
| 1498 | if setVlan: |
| 1499 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1500 | if encap: |
| 1501 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1502 | if protected: |
| 1503 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1504 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1505 | # Check whether the user appended the port |
| 1506 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1507 | if "/" in ingressDevice: |
| 1508 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1509 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1510 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1511 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1512 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1513 | # Would it make sense to throw an exception and exit |
| 1514 | # the test? |
| 1515 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1516 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1517 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1518 | str( ingressDevice ) + "/" +\ |
| 1519 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1520 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1521 | if "/" in egressDevice: |
| 1522 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1523 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1524 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1525 | main.log.error( "You must specify the egress port" ) |
| 1526 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1527 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1528 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1529 | str( egressDevice ) + "/" +\ |
| 1530 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1531 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1532 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1533 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1534 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1535 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1536 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1537 | main.log.error( self.name + ": Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1538 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1539 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1540 | # TODO: print out all the options in this message? |
| 1541 | main.log.info( "Point-to-point intent installed between " + |
| 1542 | str( ingressDevice ) + " and " + |
| 1543 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1544 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1545 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1546 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1547 | else: |
| 1548 | main.log.error( "Error, intent ID not found" ) |
| 1549 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1550 | except AssertionError: |
| 1551 | main.log.exception( "" ) |
| 1552 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1553 | except TypeError: |
| 1554 | main.log.exception( self.name + ": Object not as expected" ) |
| 1555 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1556 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1557 | main.log.error( self.name + ": EOF exception found" ) |
| 1558 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1559 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1560 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1561 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1562 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1563 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1564 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1565 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1566 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1567 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1568 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1569 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1570 | ethType="", |
| 1571 | ethSrc="", |
| 1572 | ethDst="", |
| 1573 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1574 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1575 | ipProto="", |
| 1576 | ipSrc="", |
| 1577 | ipDst="", |
| 1578 | tcpSrc="", |
| 1579 | tcpDst="", |
| 1580 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1581 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1582 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1583 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1584 | partial=False, |
| 1585 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1586 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1587 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1588 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1589 | is same. That is, all ingress devices include port numbers |
| 1590 | with a "/" or all ingress devices could specify device |
| 1591 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1592 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1593 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1594 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1595 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1596 | Optional: |
| 1597 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1598 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1599 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1600 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1601 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1602 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1603 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1604 | * ipSrc: specify ip source address |
| 1605 | * ipDst: specify ip destination address |
| 1606 | * tcpSrc: specify tcp source port |
| 1607 | * tcpDst: specify tcp destination port |
| 1608 | * setEthSrc: action to Rewrite Source MAC Address |
| 1609 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1610 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1611 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1612 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1613 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1614 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1615 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1616 | Returns: |
| 1617 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1618 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1619 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1620 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1621 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1622 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1623 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1624 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1625 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1626 | if ethType: |
| 1627 | cmd += " --ethType " + str( ethType ) |
| 1628 | if ethSrc: |
| 1629 | cmd += " --ethSrc " + str( ethSrc ) |
| 1630 | if ethDst: |
| 1631 | cmd += " --ethDst " + str( ethDst ) |
| 1632 | if bandwidth: |
| 1633 | cmd += " --bandwidth " + str( bandwidth ) |
| 1634 | if lambdaAlloc: |
| 1635 | cmd += " --lambda " |
| 1636 | if ipProto: |
| 1637 | cmd += " --ipProto " + str( ipProto ) |
| 1638 | if ipSrc: |
| 1639 | cmd += " --ipSrc " + str( ipSrc ) |
| 1640 | if ipDst: |
| 1641 | cmd += " --ipDst " + str( ipDst ) |
| 1642 | if tcpSrc: |
| 1643 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1644 | if tcpDst: |
| 1645 | cmd += " --tcpDst " + str( tcpDst ) |
| 1646 | if setEthSrc: |
| 1647 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1648 | if setEthDst: |
| 1649 | cmd += " --setEthDst " + str( setEthDst ) |
| 1650 | if vlanId: |
| 1651 | cmd += " -v " + str( vlanId ) |
| 1652 | if setVlan: |
| 1653 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1654 | if partial: |
| 1655 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1656 | if encap: |
| 1657 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1658 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1659 | # Check whether the user appended the port |
| 1660 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1661 | |
| 1662 | if portIngressList is None: |
| 1663 | for ingressDevice in ingressDeviceList: |
| 1664 | if "/" in ingressDevice: |
| 1665 | cmd += " " + str( ingressDevice ) |
| 1666 | else: |
| 1667 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1668 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1669 | # TODO: perhaps more meaningful return |
| 1670 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1671 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1672 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1673 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1674 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1675 | cmd += " " + \ |
| 1676 | str( ingressDevice ) + "/" +\ |
| 1677 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1678 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1679 | main.log.error( "Device list and port list does not " + |
| 1680 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1681 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1682 | if "/" in egressDevice: |
| 1683 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1684 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1685 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1686 | main.log.error( "You must specify " + |
| 1687 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1688 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1689 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1690 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1691 | str( egressDevice ) + "/" +\ |
| 1692 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1693 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1694 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1695 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1696 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1697 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1698 | main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1699 | "intent" ) |
| 1700 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1701 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1702 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1703 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1704 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1705 | else: |
| 1706 | main.log.error( "Error, intent ID not found" ) |
| 1707 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1708 | except AssertionError: |
| 1709 | main.log.exception( "" ) |
| 1710 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1711 | except TypeError: |
| 1712 | main.log.exception( self.name + ": Object not as expected" ) |
| 1713 | return None |
| 1714 | except pexpect.EOF: |
| 1715 | main.log.error( self.name + ": EOF exception found" ) |
| 1716 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1717 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1718 | except Exception: |
| 1719 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1720 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1721 | |
| 1722 | def addSinglepointToMultipointIntent( |
| 1723 | self, |
| 1724 | ingressDevice, |
| 1725 | egressDeviceList, |
| 1726 | portIngress="", |
| 1727 | portEgressList=None, |
| 1728 | ethType="", |
| 1729 | ethSrc="", |
| 1730 | ethDst="", |
| 1731 | bandwidth="", |
| 1732 | lambdaAlloc=False, |
| 1733 | ipProto="", |
| 1734 | ipSrc="", |
| 1735 | ipDst="", |
| 1736 | tcpSrc="", |
| 1737 | tcpDst="", |
| 1738 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1739 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1740 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1741 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1742 | partial=False, |
| 1743 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1744 | """ |
| 1745 | Note: |
| 1746 | This function assumes the format of all egress devices |
| 1747 | is same. That is, all egress devices include port numbers |
| 1748 | with a "/" or all egress devices could specify device |
| 1749 | ids and port numbers seperately. |
| 1750 | Required: |
| 1751 | * EgressDeviceList: List of device ids of egress device |
| 1752 | ( Atleast 2 eress devices required in the list ) |
| 1753 | * ingressDevice: device id of ingress device |
| 1754 | Optional: |
| 1755 | * ethType: specify ethType |
| 1756 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1757 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1758 | * bandwidth: specify bandwidth capacity of link |
| 1759 | * lambdaAlloc: if True, intent will allocate lambda |
| 1760 | for the specified intent |
| 1761 | * ipProto: specify ip protocol |
| 1762 | * ipSrc: specify ip source address |
| 1763 | * ipDst: specify ip destination address |
| 1764 | * tcpSrc: specify tcp source port |
| 1765 | * tcpDst: specify tcp destination port |
| 1766 | * setEthSrc: action to Rewrite Source MAC Address |
| 1767 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1768 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1769 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1770 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1771 | Description: |
| 1772 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1773 | specifying device id's and optional fields |
| 1774 | Returns: |
| 1775 | A string of the intent id or None on error |
| 1776 | |
| 1777 | NOTE: This function may change depending on the |
| 1778 | options developers provide for singlepoint-to-multipoint |
| 1779 | intent via cli |
| 1780 | """ |
| 1781 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1782 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1783 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1784 | if ethType: |
| 1785 | cmd += " --ethType " + str( ethType ) |
| 1786 | if ethSrc: |
| 1787 | cmd += " --ethSrc " + str( ethSrc ) |
| 1788 | if ethDst: |
| 1789 | cmd += " --ethDst " + str( ethDst ) |
| 1790 | if bandwidth: |
| 1791 | cmd += " --bandwidth " + str( bandwidth ) |
| 1792 | if lambdaAlloc: |
| 1793 | cmd += " --lambda " |
| 1794 | if ipProto: |
| 1795 | cmd += " --ipProto " + str( ipProto ) |
| 1796 | if ipSrc: |
| 1797 | cmd += " --ipSrc " + str( ipSrc ) |
| 1798 | if ipDst: |
| 1799 | cmd += " --ipDst " + str( ipDst ) |
| 1800 | if tcpSrc: |
| 1801 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1802 | if tcpDst: |
| 1803 | cmd += " --tcpDst " + str( tcpDst ) |
| 1804 | if setEthSrc: |
| 1805 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1806 | if setEthDst: |
| 1807 | cmd += " --setEthDst " + str( setEthDst ) |
| 1808 | if vlanId: |
| 1809 | cmd += " -v " + str( vlanId ) |
| 1810 | if setVlan: |
| 1811 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1812 | if partial: |
| 1813 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1814 | if encap: |
| 1815 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1816 | |
| 1817 | # Check whether the user appended the port |
| 1818 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1819 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1820 | if "/" in ingressDevice: |
| 1821 | cmd += " " + str( ingressDevice ) |
| 1822 | else: |
| 1823 | if not portIngress: |
| 1824 | main.log.error( "You must specify " + |
| 1825 | "the Ingress port" ) |
| 1826 | return main.FALSE |
| 1827 | |
| 1828 | cmd += " " +\ |
| 1829 | str( ingressDevice ) + "/" +\ |
| 1830 | str( portIngress ) |
| 1831 | |
| 1832 | if portEgressList is None: |
| 1833 | for egressDevice in egressDeviceList: |
| 1834 | if "/" in egressDevice: |
| 1835 | cmd += " " + str( egressDevice ) |
| 1836 | else: |
| 1837 | main.log.error( "You must specify " + |
| 1838 | "the egress port" ) |
| 1839 | # TODO: perhaps more meaningful return |
| 1840 | return main.FALSE |
| 1841 | else: |
| 1842 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1843 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1844 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1845 | cmd += " " + \ |
| 1846 | str( egressDevice ) + "/" +\ |
| 1847 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1848 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1849 | main.log.error( "Device list and port list does not " + |
| 1850 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1851 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1852 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1853 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1854 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1855 | # If error, return error message |
| 1856 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1857 | main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " + |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1858 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1859 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1860 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1861 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1862 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1863 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1864 | else: |
| 1865 | main.log.error( "Error, intent ID not found" ) |
| 1866 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1867 | except AssertionError: |
| 1868 | main.log.exception( "" ) |
| 1869 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1870 | except TypeError: |
| 1871 | main.log.exception( self.name + ": Object not as expected" ) |
| 1872 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1873 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1874 | main.log.error( self.name + ": EOF exception found" ) |
| 1875 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1876 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1877 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1878 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1879 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1880 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1881 | def addMplsIntent( |
| 1882 | self, |
| 1883 | ingressDevice, |
| 1884 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1885 | ingressPort="", |
| 1886 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1887 | ethType="", |
| 1888 | ethSrc="", |
| 1889 | ethDst="", |
| 1890 | bandwidth="", |
| 1891 | lambdaAlloc=False, |
| 1892 | ipProto="", |
| 1893 | ipSrc="", |
| 1894 | ipDst="", |
| 1895 | tcpSrc="", |
| 1896 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1897 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1898 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1899 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1900 | """ |
| 1901 | Required: |
| 1902 | * ingressDevice: device id of ingress device |
| 1903 | * egressDevice: device id of egress device |
| 1904 | Optional: |
| 1905 | * ethType: specify ethType |
| 1906 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1907 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1908 | * bandwidth: specify bandwidth capacity of link |
| 1909 | * lambdaAlloc: if True, intent will allocate lambda |
| 1910 | for the specified intent |
| 1911 | * ipProto: specify ip protocol |
| 1912 | * ipSrc: specify ip source address |
| 1913 | * ipDst: specify ip destination address |
| 1914 | * tcpSrc: specify tcp source port |
| 1915 | * tcpDst: specify tcp destination port |
| 1916 | * ingressLabel: Ingress MPLS label |
| 1917 | * egressLabel: Egress MPLS label |
| 1918 | Description: |
| 1919 | Adds MPLS intent by |
| 1920 | specifying device id's and optional fields |
| 1921 | Returns: |
| 1922 | A string of the intent id or None on error |
| 1923 | |
| 1924 | NOTE: This function may change depending on the |
| 1925 | options developers provide for MPLS |
| 1926 | intent via cli |
| 1927 | """ |
| 1928 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1929 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1930 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1931 | if ethType: |
| 1932 | cmd += " --ethType " + str( ethType ) |
| 1933 | if ethSrc: |
| 1934 | cmd += " --ethSrc " + str( ethSrc ) |
| 1935 | if ethDst: |
| 1936 | cmd += " --ethDst " + str( ethDst ) |
| 1937 | if bandwidth: |
| 1938 | cmd += " --bandwidth " + str( bandwidth ) |
| 1939 | if lambdaAlloc: |
| 1940 | cmd += " --lambda " |
| 1941 | if ipProto: |
| 1942 | cmd += " --ipProto " + str( ipProto ) |
| 1943 | if ipSrc: |
| 1944 | cmd += " --ipSrc " + str( ipSrc ) |
| 1945 | if ipDst: |
| 1946 | cmd += " --ipDst " + str( ipDst ) |
| 1947 | if tcpSrc: |
| 1948 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1949 | if tcpDst: |
| 1950 | cmd += " --tcpDst " + str( tcpDst ) |
| 1951 | if ingressLabel: |
| 1952 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1953 | if egressLabel: |
| 1954 | cmd += " --egressLabel " + str( egressLabel ) |
| 1955 | if priority: |
| 1956 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1957 | |
| 1958 | # Check whether the user appended the port |
| 1959 | # or provided it as an input |
| 1960 | if "/" in ingressDevice: |
| 1961 | cmd += " " + str( ingressDevice ) |
| 1962 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1963 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1964 | main.log.error( "You must specify the ingress port" ) |
| 1965 | return None |
| 1966 | |
| 1967 | cmd += " " + \ |
| 1968 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1969 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1970 | |
| 1971 | if "/" in egressDevice: |
| 1972 | cmd += " " + str( egressDevice ) |
| 1973 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1974 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1975 | main.log.error( "You must specify the egress port" ) |
| 1976 | return None |
| 1977 | |
| 1978 | cmd += " " +\ |
| 1979 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1980 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1981 | |
| 1982 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1983 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1984 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1985 | # If error, return error message |
| 1986 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1987 | main.log.error( self.name + ": Error in adding mpls intent" ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1988 | return None |
| 1989 | else: |
| 1990 | # TODO: print out all the options in this message? |
| 1991 | main.log.info( "MPLS intent installed between " + |
| 1992 | str( ingressDevice ) + " and " + |
| 1993 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1994 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1995 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1996 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1997 | else: |
| 1998 | main.log.error( "Error, intent ID not found" ) |
| 1999 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2000 | except AssertionError: |
| 2001 | main.log.exception( "" ) |
| 2002 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2003 | except TypeError: |
| 2004 | main.log.exception( self.name + ": Object not as expected" ) |
| 2005 | return None |
| 2006 | except pexpect.EOF: |
| 2007 | main.log.error( self.name + ": EOF exception found" ) |
| 2008 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2009 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2010 | except Exception: |
| 2011 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2012 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2013 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2014 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 2015 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2016 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2017 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2018 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2019 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2020 | -p or --purge: Purge the intent from the store after removal |
| 2021 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2022 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 2023 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2024 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2025 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2026 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2027 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2028 | if purge: |
| 2029 | cmdStr += " -p" |
| 2030 | if sync: |
| 2031 | cmdStr += " -s" |
| 2032 | |
| 2033 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2034 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2035 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2036 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2037 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2038 | main.log.error( self.name + ": Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2039 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2040 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2041 | # TODO: Should this be main.TRUE |
| 2042 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2043 | except AssertionError: |
| 2044 | main.log.exception( "" ) |
| 2045 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2046 | except TypeError: |
| 2047 | main.log.exception( self.name + ": Object not as expected" ) |
| 2048 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2049 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2050 | main.log.error( self.name + ": EOF exception found" ) |
| 2051 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2052 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2053 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2054 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2055 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2056 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2057 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2058 | """ |
| 2059 | Description: |
| 2060 | Remove all the intents |
| 2061 | Optional args:- |
| 2062 | -s or --sync: Waits for the removal before returning |
| 2063 | -p or --purge: Purge the intent from the store after removal |
| 2064 | Returns: |
| 2065 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2066 | main.FALSE; Returns None for exception |
| 2067 | """ |
| 2068 | try: |
| 2069 | cmdStr = "remove-intent" |
| 2070 | if purge: |
| 2071 | cmdStr += " -p" |
| 2072 | if sync: |
| 2073 | cmdStr += " -s" |
| 2074 | |
| 2075 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2076 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2077 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2078 | assert "Command not found:" not in handle, handle |
| 2079 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2080 | main.log.error( self.name + ": Error in removing intent" ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2081 | return main.FALSE |
| 2082 | else: |
| 2083 | return main.TRUE |
| 2084 | except AssertionError: |
| 2085 | main.log.exception( "" ) |
| 2086 | return None |
| 2087 | except TypeError: |
| 2088 | main.log.exception( self.name + ": Object not as expected" ) |
| 2089 | return None |
| 2090 | except pexpect.EOF: |
| 2091 | main.log.error( self.name + ": EOF exception found" ) |
| 2092 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2093 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2094 | except Exception: |
| 2095 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2096 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2097 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2098 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2099 | """ |
| 2100 | Purges all WITHDRAWN Intents |
| 2101 | """ |
| 2102 | try: |
| 2103 | cmdStr = "purge-intents" |
| 2104 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2105 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2106 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2107 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2108 | main.log.error( self.name + ": Error in purging intents" ) |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2109 | return main.FALSE |
| 2110 | else: |
| 2111 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2112 | except AssertionError: |
| 2113 | main.log.exception( "" ) |
| 2114 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2115 | except TypeError: |
| 2116 | main.log.exception( self.name + ": Object not as expected" ) |
| 2117 | return None |
| 2118 | except pexpect.EOF: |
| 2119 | main.log.error( self.name + ": EOF exception found" ) |
| 2120 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2121 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2122 | except Exception: |
| 2123 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2124 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2125 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2126 | def wipeout( self ): |
| 2127 | """ |
| 2128 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2129 | """ |
| 2130 | try: |
| 2131 | cmdStr = "wipe-out please" |
| 2132 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2133 | assert handle is not None, "Error in sendline" |
| 2134 | assert "Command not found:" not in handle, handle |
| 2135 | return main.TRUE |
| 2136 | except AssertionError: |
| 2137 | main.log.exception( "" ) |
| 2138 | return None |
| 2139 | except TypeError: |
| 2140 | main.log.exception( self.name + ": Object not as expected" ) |
| 2141 | return None |
| 2142 | except pexpect.EOF: |
| 2143 | main.log.error( self.name + ": EOF exception found" ) |
| 2144 | main.log.error( self.name + ": " + self.handle.before ) |
| 2145 | main.cleanAndExit() |
| 2146 | except Exception: |
| 2147 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2148 | main.cleanAndExit() |
| 2149 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2150 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2151 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2152 | NOTE: This method should be used after installing application: |
| 2153 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2154 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2155 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2156 | Description: |
| 2157 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2158 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2159 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2160 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2161 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2162 | cmdStr += " -j" |
| 2163 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2164 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2165 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2166 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2167 | except AssertionError: |
| 2168 | main.log.exception( "" ) |
| 2169 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2170 | except TypeError: |
| 2171 | main.log.exception( self.name + ": Object not as expected" ) |
| 2172 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2173 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2174 | main.log.error( self.name + ": EOF exception found" ) |
| 2175 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2176 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2177 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2178 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2179 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2180 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2181 | def ipv4RouteNumber( self ): |
| 2182 | """ |
| 2183 | NOTE: This method should be used after installing application: |
| 2184 | onos-app-sdnip |
| 2185 | Description: |
| 2186 | Obtain the total IPv4 routes number in the system |
| 2187 | """ |
| 2188 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2189 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2190 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2191 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2192 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2193 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2194 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2195 | except AssertionError: |
| 2196 | main.log.exception( "" ) |
| 2197 | return None |
| 2198 | except ( TypeError, ValueError ): |
| 2199 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2200 | return None |
| 2201 | except pexpect.EOF: |
| 2202 | main.log.error( self.name + ": EOF exception found" ) |
| 2203 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2204 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2205 | except Exception: |
| 2206 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2207 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2208 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2209 | # =============Function to check Bandwidth allocation======== |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2210 | def allocations( self, jsonFormat = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2211 | """ |
| 2212 | Description: |
| 2213 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2214 | """ |
| 2215 | try: |
| 2216 | cmdStr = "allocations" |
| 2217 | if jsonFormat: |
| 2218 | cmdStr += " -j" |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2219 | handle = self.sendline( cmdStr, timeout=300 ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2220 | assert handle is not None, "Error in sendline" |
| 2221 | assert "Command not found:" not in handle, handle |
| 2222 | return handle |
| 2223 | except AssertionError: |
| 2224 | main.log.exception( "" ) |
| 2225 | return None |
| 2226 | except ( TypeError, ValueError ): |
| 2227 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2228 | return None |
| 2229 | except pexpect.EOF: |
| 2230 | main.log.error( self.name + ": EOF exception found" ) |
| 2231 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2232 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2233 | except Exception: |
| 2234 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2235 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2236 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2237 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2238 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2239 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2240 | Obtain intents from the ONOS cli. |
| 2241 | Optional: |
| 2242 | * jsonFormat: Enable output formatting in json, default to True |
| 2243 | * summary: Whether only output the intent summary, defaults to False |
| 2244 | * type: Only output a certain type of intent. This options is valid |
| 2245 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2246 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2247 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2248 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2249 | if summary: |
| 2250 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2251 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2252 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2253 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2254 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2255 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2256 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2257 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2258 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2259 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2260 | intentType = "" |
| 2261 | # IF we want the summary of a specific intent type |
| 2262 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2263 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2264 | if intentType in jsonResult.keys(): |
| 2265 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2266 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2267 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2268 | return handle |
| 2269 | else: |
| 2270 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2271 | except AssertionError: |
| 2272 | main.log.exception( "" ) |
| 2273 | return None |
| 2274 | except ( TypeError, ValueError ): |
| 2275 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2276 | return None |
| 2277 | except pexpect.EOF: |
| 2278 | main.log.error( self.name + ": EOF exception found" ) |
| 2279 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2280 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2281 | except Exception: |
| 2282 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2283 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2284 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2285 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2286 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2287 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2288 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2289 | list of intent IDs. |
| 2290 | Parameters: |
| 2291 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2292 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2293 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2294 | 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] | 2295 | accepted. |
| 2296 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2297 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2298 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2299 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2300 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2301 | try: |
| 2302 | state = "State is Undefined" |
| 2303 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2304 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2305 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2306 | rawJson = intentsJson |
| 2307 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2308 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2309 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2310 | if intentsId == intent[ 'id' ]: |
| 2311 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2312 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2313 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2314 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2315 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2316 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2317 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2318 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2319 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2320 | for intent in parsedIntentsJson: |
| 2321 | if intentsId[ i ] == intent[ 'id' ]: |
| 2322 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2323 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2324 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2325 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2326 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2327 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2328 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2329 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2330 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2331 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2332 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2333 | except ( TypeError, ValueError ): |
| 2334 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2335 | return None |
| 2336 | except pexpect.EOF: |
| 2337 | main.log.error( self.name + ": EOF exception found" ) |
| 2338 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2339 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2340 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2341 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2342 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2343 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2344 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2345 | """ |
| 2346 | Description: |
| 2347 | Check intents state |
| 2348 | Required: |
| 2349 | intentsId - List of intents ID to be checked |
| 2350 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2351 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2352 | state in the list. |
| 2353 | *NOTE: You can pass in a list of expected state, |
| 2354 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2355 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2356 | Returns main.TRUE only if all intent are the same as expected states, |
| 2357 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2358 | """ |
| 2359 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2360 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2361 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2362 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2363 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2364 | intentsDict = [] |
| 2365 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2366 | if isinstance( intentsId, types.StringType ) \ |
| 2367 | and intent.get( 'id' ) == intentsId: |
| 2368 | intentsDict.append( intent ) |
| 2369 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2370 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2371 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2372 | |
| 2373 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2374 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2375 | "getting intents state" ) |
| 2376 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2377 | |
| 2378 | if isinstance( expectedState, types.StringType ): |
| 2379 | for intents in intentsDict: |
| 2380 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2381 | main.log.debug( self.name + " : Intent ID - " + |
| 2382 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2383 | " actual state = " + |
| 2384 | intents.get( 'state' ) |
| 2385 | + " does not equal expected state = " |
| 2386 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2387 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2388 | elif isinstance( expectedState, types.ListType ): |
| 2389 | for intents in intentsDict: |
| 2390 | if not any( state == intents.get( 'state' ) for state in |
| 2391 | expectedState ): |
| 2392 | main.log.debug( self.name + " : Intent ID - " + |
| 2393 | intents.get( 'id' ) + |
| 2394 | " actual state = " + |
| 2395 | intents.get( 'state' ) + |
| 2396 | " does not equal expected states = " |
| 2397 | + str( expectedState ) ) |
| 2398 | returnValue = main.FALSE |
| 2399 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2400 | if returnValue == main.TRUE: |
| 2401 | main.log.info( self.name + ": All " + |
| 2402 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2403 | " intents are in " + str( expectedState ) + |
| 2404 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2405 | return returnValue |
| 2406 | except TypeError: |
| 2407 | main.log.exception( self.name + ": Object not as expected" ) |
| 2408 | return None |
| 2409 | except pexpect.EOF: |
| 2410 | main.log.error( self.name + ": EOF exception found" ) |
| 2411 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2412 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2413 | except Exception: |
| 2414 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2415 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2416 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2417 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2418 | """ |
| 2419 | Description: |
| 2420 | Compare the allocated bandwidth with the given allocations |
| 2421 | Required: |
| 2422 | expectedAllocations - The expected ONOS output of the allocations command |
| 2423 | Return: |
| 2424 | Returns main.TRUE only if all intent are the same as expected states, |
| 2425 | otherwise returns main.FALSE. |
| 2426 | """ |
| 2427 | # FIXME: Convert these string comparisons to object comparisons |
| 2428 | try: |
| 2429 | returnValue = main.TRUE |
| 2430 | bandwidthFailed = False |
| 2431 | rawAlloc = self.allocations() |
| 2432 | expectedFormat = StringIO( expectedAllocations ) |
| 2433 | ONOSOutput = StringIO( rawAlloc ) |
| 2434 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2435 | str( expectedFormat ) ) ) |
| 2436 | |
| 2437 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2438 | actual = actual.rstrip() |
| 2439 | expected = expected.rstrip() |
| 2440 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2441 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2442 | marker1 = actual.find( 'allocated' ) |
| 2443 | m1 = actual[ :marker1 ] |
| 2444 | marker2 = expected.find( 'allocated' ) |
| 2445 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2446 | if m1 != m2: |
| 2447 | bandwidthFailed = True |
| 2448 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2449 | bandwidthFailed = True |
| 2450 | expectedFormat.close() |
| 2451 | ONOSOutput.close() |
| 2452 | |
| 2453 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2454 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2455 | returnValue = main.FALSE |
| 2456 | return returnValue |
| 2457 | except TypeError: |
| 2458 | main.log.exception( self.name + ": Object not as expected" ) |
| 2459 | return None |
| 2460 | except pexpect.EOF: |
| 2461 | main.log.error( self.name + ": EOF exception found" ) |
| 2462 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2463 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2464 | except Exception: |
| 2465 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2466 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2467 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2468 | def compareIntent( self, intentDict ): |
| 2469 | """ |
| 2470 | Description: |
| 2471 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2472 | Return: |
| 2473 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2474 | Arguments: |
| 2475 | intentDict: a dictionary which maps intent ids to intent states |
| 2476 | """ |
| 2477 | try: |
| 2478 | intentsRaw = self.intents() |
| 2479 | intentsJson = json.loads( intentsRaw ) |
| 2480 | intentDictONOS = {} |
| 2481 | for intent in intentsJson: |
| 2482 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2483 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2484 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2485 | 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] | 2486 | str( len( intentDict ) ) + " expected and " + |
| 2487 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2488 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2489 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2490 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2491 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2492 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2493 | else: |
| 2494 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2495 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2496 | " expected state is " + intentDict[ intentID ] + |
| 2497 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2498 | returnValue = main.FALSE |
| 2499 | intentDictONOS.pop( intentID ) |
| 2500 | if len( intentDictONOS ) > 0: |
| 2501 | returnValue = main.FALSE |
| 2502 | for intentID in intentDictONOS.keys(): |
| 2503 | 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] | 2504 | if returnValue == main.TRUE: |
| 2505 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2506 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2507 | except KeyError: |
| 2508 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2509 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2510 | except ( TypeError, ValueError ): |
| 2511 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2512 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2513 | except pexpect.EOF: |
| 2514 | main.log.error( self.name + ": EOF exception found" ) |
| 2515 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2516 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2517 | except Exception: |
| 2518 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2519 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2520 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2521 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2522 | """ |
| 2523 | Description: |
| 2524 | Check the number of installed intents. |
| 2525 | Optional: |
| 2526 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2527 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2528 | Return: |
| 2529 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2530 | , otherwise, returns main.FALSE. |
| 2531 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2532 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2533 | try: |
| 2534 | cmd = "intents -s -j" |
| 2535 | |
| 2536 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2537 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2538 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2539 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2540 | response = json.loads( response ) |
| 2541 | |
| 2542 | # get total and installed number, see if they are match |
| 2543 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2544 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2545 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2546 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2547 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2548 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2549 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2550 | return main.FALSE |
| 2551 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2552 | except ( TypeError, ValueError ): |
| 2553 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2554 | return None |
| 2555 | except pexpect.EOF: |
| 2556 | main.log.error( self.name + ": EOF exception found" ) |
| 2557 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2558 | if noExit: |
| 2559 | return main.FALSE |
| 2560 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2561 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2562 | except pexpect.TIMEOUT: |
| 2563 | main.log.error( self.name + ": ONOS timeout" ) |
| 2564 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2565 | except Exception: |
| 2566 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2567 | if noExit: |
| 2568 | return main.FALSE |
| 2569 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2570 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2571 | |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2572 | 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] | 2573 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2574 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2575 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2576 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2577 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2578 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2579 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2580 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2581 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2582 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2583 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2584 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2585 | cmdStr += " -n" |
| 2586 | cmdStr += " " + state |
| 2587 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2588 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2589 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2590 | assert "Command not found:" not in handle, handle |
| 2591 | if re.search( "Error:", handle ): |
| 2592 | main.log.error( self.name + ": flows() response: " + |
| 2593 | str( handle ) ) |
| 2594 | return handle |
| 2595 | except AssertionError: |
| 2596 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2597 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2598 | except TypeError: |
| 2599 | main.log.exception( self.name + ": Object not as expected" ) |
| 2600 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2601 | except pexpect.TIMEOUT: |
| 2602 | main.log.error( self.name + ": ONOS timeout" ) |
| 2603 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2604 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2605 | main.log.error( self.name + ": EOF exception found" ) |
| 2606 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2607 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2608 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2609 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2610 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2611 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2612 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2613 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2614 | count = int( count ) if count else 0 |
| 2615 | return count if ( count > min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2616 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2617 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2618 | """ |
| 2619 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2620 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2621 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2622 | if the count of those states is 0, which means all current flows |
| 2623 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2624 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2625 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2626 | Return: |
| 2627 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2628 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2629 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2630 | """ |
| 2631 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2632 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2633 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2634 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2635 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2636 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2637 | if rawFlows: |
| 2638 | # if we didn't get flows or flows function return None, we should return |
| 2639 | # main.Flase |
| 2640 | checkedStates.append( json.loads( rawFlows ) ) |
| 2641 | else: |
| 2642 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2643 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2644 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2645 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2646 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2647 | except TypeError: |
| 2648 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2649 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2650 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2651 | # We want to count PENDING_ADD if isPENDING is true |
| 2652 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2653 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2654 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2655 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2656 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2657 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2658 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2659 | except ( TypeError, ValueError ): |
| 2660 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2661 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2662 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2663 | except AssertionError: |
| 2664 | main.log.exception( "" ) |
| 2665 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2666 | except pexpect.TIMEOUT: |
| 2667 | main.log.error( self.name + ": ONOS timeout" ) |
| 2668 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2669 | except pexpect.EOF: |
| 2670 | main.log.error( self.name + ": EOF exception found" ) |
| 2671 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2672 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2673 | except Exception: |
| 2674 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2675 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2676 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2677 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2678 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2679 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2680 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2681 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2682 | a specific point-to-point intent definition |
| 2683 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2684 | * ingress: specify source dpid |
| 2685 | * egress: specify destination dpid |
| 2686 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2687 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2688 | * offset: the keyOffset is where the next batch of intents |
| 2689 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2690 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2691 | * getResponse: If set to True, function will return ONOS response. |
| 2692 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2693 | Returns: If failed to push test intents, it will returen None, |
| 2694 | if successful, return true. |
| 2695 | Timeout expection will return None, |
| 2696 | TypeError will return false |
| 2697 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2698 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2699 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2700 | if background: |
| 2701 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2702 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2703 | back = "" |
| 2704 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2705 | ingress, |
| 2706 | egress, |
| 2707 | batchSize, |
| 2708 | offset, |
| 2709 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2710 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2711 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2712 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2713 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2714 | if getResponse: |
| 2715 | return response |
| 2716 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2717 | # TODO: We should handle if there is failure in installation |
| 2718 | return main.TRUE |
| 2719 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2720 | except AssertionError: |
| 2721 | main.log.exception( "" ) |
| 2722 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2723 | except pexpect.TIMEOUT: |
| 2724 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2725 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2726 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2727 | main.log.error( self.name + ": EOF exception found" ) |
| 2728 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2729 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2730 | except TypeError: |
| 2731 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2732 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2733 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2734 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2735 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2736 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2737 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2738 | """ |
| 2739 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2740 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2741 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2742 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2743 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2744 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2745 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2746 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2747 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2748 | cmd = "flows -c added" |
| 2749 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2750 | if rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2751 | rawFlows = rawFlows.split( "\n" ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2752 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2753 | for l in rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2754 | totalFlows += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2755 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2756 | main.log.error( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2757 | return None |
| 2758 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2759 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2760 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2761 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2762 | return None |
| 2763 | except pexpect.EOF: |
| 2764 | main.log.error( self.name + ": EOF exception found" ) |
| 2765 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2766 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2767 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2768 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2769 | except pexpect.TIMEOUT: |
| 2770 | main.log.error( self.name + ": ONOS timeout" ) |
| 2771 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2772 | except Exception: |
| 2773 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2774 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2775 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2776 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2777 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2778 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2779 | """ |
| 2780 | Description: |
| 2781 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2782 | Optional: |
| 2783 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2784 | Return: |
| 2785 | The number of intents |
| 2786 | """ |
| 2787 | try: |
| 2788 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2789 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2790 | if response is None: |
| 2791 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2792 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2793 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2794 | except ( TypeError, ValueError ): |
| 2795 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2796 | return None |
| 2797 | except pexpect.EOF: |
| 2798 | main.log.error( self.name + ": EOF exception found" ) |
| 2799 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2800 | if noExit: |
| 2801 | return -1 |
| 2802 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2803 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2804 | except Exception: |
| 2805 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2806 | if noExit: |
| 2807 | return -1 |
| 2808 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2809 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2810 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2811 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2812 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2813 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2814 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2815 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2816 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2817 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2818 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2819 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2820 | cmdStr += " -j" |
| 2821 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2822 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2823 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2824 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2825 | except AssertionError: |
| 2826 | main.log.exception( "" ) |
| 2827 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2828 | except TypeError: |
| 2829 | main.log.exception( self.name + ": Object not as expected" ) |
| 2830 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2831 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2832 | main.log.error( self.name + ": EOF exception found" ) |
| 2833 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2834 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2835 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2836 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2837 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2838 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2839 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2840 | """ |
| 2841 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2842 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2843 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2844 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2845 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2846 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2847 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2848 | cmdStr += " -j" |
| 2849 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2850 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2851 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2852 | if handle: |
| 2853 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2854 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2855 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2856 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2857 | else: |
| 2858 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2859 | except AssertionError: |
| 2860 | main.log.exception( "" ) |
| 2861 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2862 | except TypeError: |
| 2863 | main.log.exception( self.name + ": Object not as expected" ) |
| 2864 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2865 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2866 | main.log.error( self.name + ": EOF exception found" ) |
| 2867 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2868 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2869 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2870 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2871 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2872 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2873 | # Wrapper functions **************** |
| 2874 | # Wrapper functions use existing driver |
| 2875 | # functions and extends their use case. |
| 2876 | # For example, we may use the output of |
| 2877 | # a normal driver function, and parse it |
| 2878 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2879 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2880 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2881 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2882 | Description: |
| 2883 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2884 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2885 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2886 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2887 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 2888 | if intentsStr is None: |
| 2889 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2890 | # Convert to a dictionary |
| 2891 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2892 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2893 | for intent in intents: |
| 2894 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2895 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2896 | except TypeError: |
| 2897 | main.log.exception( self.name + ": Object not as expected" ) |
| 2898 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2899 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2900 | main.log.error( self.name + ": EOF exception found" ) |
| 2901 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2902 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2903 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2904 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2905 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2906 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2907 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2908 | """ |
| 2909 | Determine the number of flow rules for the given device id that are |
| 2910 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2911 | Params: |
| 2912 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2913 | """ |
| 2914 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2915 | if core: |
| 2916 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2917 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2918 | else: |
| 2919 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2920 | "grep 'state=ADDED' | wc -l" |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2921 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2922 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2923 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2924 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2925 | except AssertionError: |
| 2926 | main.log.exception( "" ) |
| 2927 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2928 | except pexpect.EOF: |
| 2929 | main.log.error( self.name + ": EOF exception found" ) |
| 2930 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2931 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2932 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2933 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2934 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2935 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2936 | def groupAddedCount( self, deviceId, core=False ): |
| 2937 | """ |
| 2938 | Determine the number of group rules for the given device id that are |
| 2939 | in the added state |
| 2940 | Params: |
| 2941 | core: if True, only return the number of core groups added |
| 2942 | """ |
| 2943 | try: |
| 2944 | if core: |
| 2945 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 2946 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2947 | else: |
| 2948 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 2949 | "grep 'state=ADDED' | wc -l" |
| 2950 | handle = self.sendline( cmdStr ) |
| 2951 | assert handle is not None, "Error in sendline" |
| 2952 | assert "Command not found:" not in handle, handle |
| 2953 | return handle |
| 2954 | except AssertionError: |
| 2955 | main.log.exception( "" ) |
| 2956 | return None |
| 2957 | except pexpect.EOF: |
| 2958 | main.log.error( self.name + ": EOF exception found" ) |
| 2959 | main.log.error( self.name + ": " + self.handle.before ) |
| 2960 | main.cleanAndExit() |
| 2961 | except Exception: |
| 2962 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2963 | main.cleanAndExit() |
| 2964 | |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 2965 | def addStaticRoute( self, subnet, intf): |
| 2966 | """ |
| 2967 | Adds a static route to onos. |
| 2968 | Params: |
| 2969 | subnet: The subnet reaching through this route |
| 2970 | intf: The interface this route is reachable through |
| 2971 | """ |
| 2972 | try: |
| 2973 | cmdStr = "route-add " + subnet + " " + intf |
| 2974 | handle = self.sendline( cmdStr ) |
| 2975 | assert handle is not None, "Error in sendline" |
| 2976 | assert "Command not found:" not in handle, handle |
| 2977 | return handle |
| 2978 | except AssertionError: |
| 2979 | main.log.exception( "" ) |
| 2980 | return None |
| 2981 | except pexpect.EOF: |
| 2982 | main.log.error( self.name + ": EOF exception found" ) |
| 2983 | main.log.error( self.name + ": " + self.handle.before ) |
| 2984 | main.cleanAndExit() |
| 2985 | except Exception: |
| 2986 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2987 | main.cleanAndExit() |
| 2988 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2989 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 2990 | """ |
| 2991 | Description: |
| 2992 | Check whether the number of groups for the given device id that |
| 2993 | are in ADDED state is bigger than minGroupCount. |
| 2994 | Required: |
| 2995 | * deviceId: device id to check the number of added group rules |
| 2996 | Optional: |
| 2997 | * minGroupCount: the number of groups to compare |
| 2998 | * core: if True, only check the number of core groups added |
| 2999 | * comparison: if 0, compare with greater than minFlowCount |
| 3000 | * if 1, compare with equal to minFlowCount |
| 3001 | Return: |
| 3002 | Returns the number of groups if it is bigger than minGroupCount, |
| 3003 | returns main.FALSE otherwise. |
| 3004 | """ |
| 3005 | count = self.groupAddedCount( deviceId, core ) |
| 3006 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3007 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3008 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 3009 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3010 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3011 | """ |
| 3012 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3013 | deviceId: Id of the device from which we retrieve groups |
| 3014 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3015 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3016 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3017 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 3018 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3019 | assert handle is not None, "Error in sendline" |
| 3020 | assert "Command not found:" not in handle, handle |
| 3021 | return handle |
| 3022 | except AssertionError: |
| 3023 | main.log.exception( "" ) |
| 3024 | return None |
| 3025 | except TypeError: |
| 3026 | main.log.exception( self.name + ": Object not as expected" ) |
| 3027 | return None |
| 3028 | except pexpect.EOF: |
| 3029 | main.log.error( self.name + ": EOF exception found" ) |
| 3030 | main.log.error( self.name + ": " + self.handle.before ) |
| 3031 | main.cleanAndExit() |
| 3032 | except Exception: |
| 3033 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3034 | main.cleanAndExit() |
| 3035 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3036 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3037 | """ |
| 3038 | Description: |
| 3039 | Check whether the number of flow rules for the given device id that |
| 3040 | are in ADDED state is bigger than minFlowCount. |
| 3041 | Required: |
| 3042 | * deviceId: device id to check the number of added flow rules |
| 3043 | Optional: |
| 3044 | * minFlowCount: the number of flow rules to compare |
| 3045 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3046 | * comparison: if 0, compare with greater than minFlowCount |
| 3047 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3048 | Return: |
| 3049 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 3050 | returns main.FALSE otherwise. |
| 3051 | """ |
| 3052 | count = self.flowAddedCount( deviceId, core ) |
| 3053 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3054 | main.log.debug( "found {} flows".format( count ) ) |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3055 | 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] | 3056 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3057 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3058 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3059 | Use 'devices' function to obtain list of all devices |
| 3060 | and parse the result to obtain a list of all device |
| 3061 | id's. Returns this list. Returns empty list if no |
| 3062 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3063 | List is ordered sequentially |
| 3064 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3065 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3066 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3067 | the ids. By obtaining the list of device ids on the fly, |
| 3068 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3069 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3070 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3071 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3072 | devicesStr = self.devices( jsonFormat=False ) |
| 3073 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3074 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3075 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3076 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3077 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3078 | |
| 3079 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3080 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3081 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3082 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3083 | # Split list further into arguments before and after string |
| 3084 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3085 | # append to idList |
| 3086 | for arg in tempList: |
| 3087 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3088 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3089 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3090 | except TypeError: |
| 3091 | main.log.exception( self.name + ": Object not as expected" ) |
| 3092 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3093 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3094 | main.log.error( self.name + ": EOF exception found" ) |
| 3095 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3096 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3097 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3098 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3099 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3100 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3101 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3102 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3103 | Uses 'nodes' function to obtain list of all nodes |
| 3104 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3105 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3106 | Returns: |
| 3107 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3108 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3109 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3110 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3111 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3112 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3113 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3114 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3115 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3116 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3117 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3118 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3119 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3120 | except ( TypeError, ValueError ): |
| 3121 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3122 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3123 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3124 | main.log.error( self.name + ": EOF exception found" ) |
| 3125 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3126 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3127 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3128 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3129 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3130 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3131 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3132 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3133 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3134 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3135 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3136 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3137 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3138 | return None |
| 3139 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3140 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3141 | rawDevices = self.devices() |
| 3142 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3143 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3144 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3145 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3146 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3147 | return device |
| 3148 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3149 | except ( TypeError, ValueError ): |
| 3150 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3151 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3152 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3153 | main.log.error( self.name + ": EOF exception found" ) |
| 3154 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3155 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3156 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3157 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3158 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3159 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3160 | def getTopology( self, topologyOutput ): |
| 3161 | """ |
| 3162 | Definition: |
| 3163 | Loads a json topology output |
| 3164 | Return: |
| 3165 | topology = current ONOS topology |
| 3166 | """ |
| 3167 | import json |
| 3168 | try: |
| 3169 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3170 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3171 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3172 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3173 | except ( TypeError, ValueError ): |
| 3174 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3175 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3176 | except pexpect.EOF: |
| 3177 | main.log.error( self.name + ": EOF exception found" ) |
| 3178 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3179 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3180 | except Exception: |
| 3181 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3182 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3183 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3184 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3185 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3186 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3187 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3188 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3189 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3190 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3191 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3192 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3193 | logLevel = level to log to. |
| 3194 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3195 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3196 | Returns: main.TRUE if the number of switches and links are correct, |
| 3197 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3198 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3199 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3200 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3201 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3202 | summary = self.summary() |
| 3203 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3204 | except ( TypeError, ValueError ): |
| 3205 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3206 | return main.ERROR |
| 3207 | try: |
| 3208 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3209 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3210 | return main.ERROR |
| 3211 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3212 | # Is the number of switches is what we expected |
| 3213 | devices = topology.get( 'devices', False ) |
| 3214 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3215 | nodes = summary.get( 'nodes', False ) |
| 3216 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3217 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3218 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3219 | # Is the number of links is what we expected |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3220 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3221 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 3222 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3223 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3224 | output = output + "The number of links and switches match "\ |
| 3225 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3226 | result = main.TRUE |
| 3227 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3228 | output = output + \ |
| 3229 | "The number of links and switches does not match " + \ |
| 3230 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3231 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3232 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 3233 | output = output + " (%i expected) " % int( numoswitch ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3234 | if int( numolink ) > 0: |
| 3235 | output = output + "and %i links " % int( links ) |
| 3236 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 3237 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3238 | output = output + "and %i controllers " % int( nodes ) |
| 3239 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3240 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3241 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3242 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3243 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3244 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3245 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3246 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3247 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3248 | main.log.error( self.name + ": EOF exception found" ) |
| 3249 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3250 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3251 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3252 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3253 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3254 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3255 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3256 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3257 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3258 | deviceId must be the id of a device as seen in the onos devices command |
| 3259 | 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] | 3260 | role must be either master, standby, or none |
| 3261 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3262 | Returns: |
| 3263 | main.TRUE or main.FALSE based on argument verification and |
| 3264 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3265 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3266 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3267 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3268 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3269 | cmdStr = "device-role " +\ |
| 3270 | str( deviceId ) + " " +\ |
| 3271 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3272 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3273 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3274 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3275 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3276 | if re.search( "Error", handle ): |
| 3277 | # end color output to escape any colours |
| 3278 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3279 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3280 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3281 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3282 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3283 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3284 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3285 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3286 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3287 | except AssertionError: |
| 3288 | main.log.exception( "" ) |
| 3289 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3290 | except TypeError: |
| 3291 | main.log.exception( self.name + ": Object not as expected" ) |
| 3292 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3293 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3294 | main.log.error( self.name + ": EOF exception found" ) |
| 3295 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3296 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3297 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3298 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3299 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3300 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3301 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3302 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3303 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3304 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3305 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3306 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3307 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3308 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3309 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3310 | cmdStr += " -j" |
| 3311 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3312 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3313 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3314 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3315 | except AssertionError: |
| 3316 | main.log.exception( "" ) |
| 3317 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3318 | except TypeError: |
| 3319 | main.log.exception( self.name + ": Object not as expected" ) |
| 3320 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3321 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3322 | main.log.error( self.name + ": EOF exception found" ) |
| 3323 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3324 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3325 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3326 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3327 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3328 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3329 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3330 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3331 | CLI command to get the current leader for the Election test application |
| 3332 | NOTE: Requires installation of the onos-app-election feature |
| 3333 | Returns: Node IP of the leader if one exists |
| 3334 | None if none exists |
| 3335 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3336 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3337 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3338 | cmdStr = "election-test-leader" |
| 3339 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3340 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3341 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3342 | # Leader |
| 3343 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3344 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3345 | nodeSearch = re.search( leaderPattern, response ) |
| 3346 | if nodeSearch: |
| 3347 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3348 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3349 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3350 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3351 | # no leader |
| 3352 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3353 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3354 | nullSearch = re.search( nullPattern, response ) |
| 3355 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3356 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3357 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3358 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3359 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3360 | main.log.error( self.name + ": Error in electionTestLeader on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3361 | ": " + "unexpected response" ) |
| 3362 | main.log.error( repr( response ) ) |
| 3363 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3364 | except AssertionError: |
| 3365 | main.log.exception( "" ) |
| 3366 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3367 | except TypeError: |
| 3368 | main.log.exception( self.name + ": Object not as expected" ) |
| 3369 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3370 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3371 | main.log.error( self.name + ": EOF exception found" ) |
| 3372 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3373 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3374 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3375 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3376 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3377 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3378 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3379 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3380 | CLI command to run for leadership of the Election test application. |
| 3381 | NOTE: Requires installation of the onos-app-election feature |
| 3382 | Returns: Main.TRUE on success |
| 3383 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3384 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3385 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3386 | cmdStr = "election-test-run" |
| 3387 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3388 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3389 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3390 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3391 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3392 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3393 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3394 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3395 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3396 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3397 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3398 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3399 | main.log.error( self.name + ": Error in electionTestRun on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3400 | ": " + "unexpected response" ) |
| 3401 | main.log.error( repr( response ) ) |
| 3402 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3403 | except AssertionError: |
| 3404 | main.log.exception( "" ) |
| 3405 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3406 | except TypeError: |
| 3407 | main.log.exception( self.name + ": Object not as expected" ) |
| 3408 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3409 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3410 | main.log.error( self.name + ": EOF exception found" ) |
| 3411 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3412 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3413 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3414 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3415 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3416 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3417 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3418 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3419 | * CLI command to withdraw the local node from leadership election for |
| 3420 | * the Election test application. |
| 3421 | #NOTE: Requires installation of the onos-app-election feature |
| 3422 | Returns: Main.TRUE on success |
| 3423 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3424 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3425 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3426 | cmdStr = "election-test-withdraw" |
| 3427 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3428 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3429 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3430 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3431 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3432 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3433 | if re.search( successPattern, response ): |
| 3434 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3435 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3436 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3437 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3438 | main.log.error( self.name + ": Error in electionTestWithdraw on " + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3439 | self.name + ": " + "unexpected response" ) |
| 3440 | main.log.error( repr( response ) ) |
| 3441 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3442 | except AssertionError: |
| 3443 | main.log.exception( "" ) |
| 3444 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3445 | except TypeError: |
| 3446 | main.log.exception( self.name + ": Object not as expected" ) |
| 3447 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3448 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3449 | main.log.error( self.name + ": EOF exception found" ) |
| 3450 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3451 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3452 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3453 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3454 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3455 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3456 | def getDevicePortsEnabledCount( self, dpid ): |
| 3457 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3458 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3459 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3460 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3461 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3462 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 3463 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3464 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3465 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3466 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3467 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3468 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3469 | return output |
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 ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -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() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3483 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3484 | def getDeviceLinksActiveCount( self, dpid ): |
| 3485 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3486 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3487 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3488 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3489 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3490 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3491 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3492 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3493 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3494 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3495 | main.log.error( self.name + ": Error in getting ports " ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3496 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3497 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3498 | except AssertionError: |
| 3499 | main.log.exception( "" ) |
| 3500 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3501 | except TypeError: |
| 3502 | main.log.exception( self.name + ": Object not as expected" ) |
| 3503 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3504 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3505 | main.log.error( self.name + ": EOF exception found" ) |
| 3506 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3507 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3508 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3509 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3510 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3511 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3512 | def getAllIntentIds( self ): |
| 3513 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3514 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3515 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3516 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3517 | cmdStr = "onos:intents | grep id=" |
| 3518 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3519 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3520 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3521 | if re.search( "Error", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3522 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3523 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3524 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3525 | except AssertionError: |
| 3526 | main.log.exception( "" ) |
| 3527 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3528 | except TypeError: |
| 3529 | main.log.exception( self.name + ": Object not as expected" ) |
| 3530 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3531 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3532 | main.log.error( self.name + ": EOF exception found" ) |
| 3533 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3534 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3535 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3536 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3537 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3538 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3539 | def intentSummary( self ): |
| 3540 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3541 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3542 | """ |
| 3543 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3544 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3545 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3546 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3547 | states.append( intent.get( 'state', None ) ) |
| 3548 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3549 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3550 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3551 | except ( TypeError, ValueError ): |
| 3552 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3553 | return None |
| 3554 | except pexpect.EOF: |
| 3555 | main.log.error( self.name + ": EOF exception found" ) |
| 3556 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3557 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3558 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3559 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3560 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3561 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3562 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3563 | """ |
| 3564 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3565 | Optional argument: |
| 3566 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3567 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3568 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3569 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3570 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3571 | cmdStr += " -j" |
| 3572 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3573 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3574 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3575 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3576 | except AssertionError: |
| 3577 | main.log.exception( "" ) |
| 3578 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3579 | except TypeError: |
| 3580 | main.log.exception( self.name + ": Object not as expected" ) |
| 3581 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3582 | except pexpect.EOF: |
| 3583 | main.log.error( self.name + ": EOF exception found" ) |
| 3584 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3585 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3586 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3587 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3588 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3589 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3590 | def leaderCandidates( self, jsonFormat=True ): |
| 3591 | """ |
| 3592 | Returns the output of the leaders -c command. |
| 3593 | Optional argument: |
| 3594 | * jsonFormat - boolean indicating if you want output in json |
| 3595 | """ |
| 3596 | try: |
| 3597 | cmdStr = "onos:leaders -c" |
| 3598 | if jsonFormat: |
| 3599 | cmdStr += " -j" |
| 3600 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3601 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3602 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3603 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3604 | except AssertionError: |
| 3605 | main.log.exception( "" ) |
| 3606 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3607 | except TypeError: |
| 3608 | main.log.exception( self.name + ": Object not as expected" ) |
| 3609 | return None |
| 3610 | except pexpect.EOF: |
| 3611 | main.log.error( self.name + ": EOF exception found" ) |
| 3612 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3613 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3614 | except Exception: |
| 3615 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3616 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3617 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3618 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3619 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3620 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3621 | topic parameter and an empty list if the topic doesn't exist |
| 3622 | If no leader is elected leader in the returned list will be "none" |
| 3623 | Returns None if there is a type error processing the json object |
| 3624 | """ |
| 3625 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3626 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3627 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3628 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3629 | assert "Command not found:" not in rawOutput, rawOutput |
| 3630 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3631 | results = [] |
| 3632 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3633 | if dict[ "topic" ] == topic: |
| 3634 | leader = dict[ "leader" ] |
| 3635 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3636 | results.append( leader ) |
| 3637 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3638 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3639 | except AssertionError: |
| 3640 | main.log.exception( "" ) |
| 3641 | return None |
| 3642 | except ( TypeError, ValueError ): |
| 3643 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3644 | return None |
| 3645 | except pexpect.EOF: |
| 3646 | main.log.error( self.name + ": EOF exception found" ) |
| 3647 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3648 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3649 | except Exception: |
| 3650 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3651 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3652 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3653 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3654 | """ |
| 3655 | Returns the output of the intent Pending map. |
| 3656 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3657 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3658 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3659 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3660 | cmdStr += " -j" |
| 3661 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3662 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3663 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3664 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3665 | except AssertionError: |
| 3666 | main.log.exception( "" ) |
| 3667 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3668 | except TypeError: |
| 3669 | main.log.exception( self.name + ": Object not as expected" ) |
| 3670 | return None |
| 3671 | except pexpect.EOF: |
| 3672 | main.log.error( self.name + ": EOF exception found" ) |
| 3673 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3674 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3675 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3676 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3677 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3678 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3679 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3680 | """ |
| 3681 | Returns the output of the raft partitions command for ONOS. |
| 3682 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3683 | # Sample JSON |
| 3684 | # { |
| 3685 | # "leader": "tcp://10.128.30.11:7238", |
| 3686 | # "members": [ |
| 3687 | # "tcp://10.128.30.11:7238", |
| 3688 | # "tcp://10.128.30.17:7238", |
| 3689 | # "tcp://10.128.30.13:7238", |
| 3690 | # ], |
| 3691 | # "name": "p1", |
| 3692 | # "term": 3 |
| 3693 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3694 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3695 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3696 | if candidates: |
| 3697 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3698 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3699 | cmdStr += " -j" |
| 3700 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3701 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3702 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3703 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3704 | except AssertionError: |
| 3705 | main.log.exception( "" ) |
| 3706 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3707 | except TypeError: |
| 3708 | main.log.exception( self.name + ": Object not as expected" ) |
| 3709 | return None |
| 3710 | except pexpect.EOF: |
| 3711 | main.log.error( self.name + ": EOF exception found" ) |
| 3712 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3713 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3714 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3715 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3716 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3717 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3718 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3719 | """ |
| 3720 | Returns the output of the apps command for ONOS. This command lists |
| 3721 | information about installed ONOS applications |
| 3722 | """ |
| 3723 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3724 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3725 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3726 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3727 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3728 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3729 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3730 | if summary: |
| 3731 | cmdStr += " -s" |
| 3732 | if active: |
| 3733 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3734 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3735 | cmdStr += " -j" |
| 3736 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3737 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3738 | assert "Command not found:" not in output, output |
| 3739 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3740 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3741 | # FIXME: look at specific exceptions/Errors |
| 3742 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3743 | main.log.exception( self.name + ": Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3744 | return None |
| 3745 | except TypeError: |
| 3746 | main.log.exception( self.name + ": Object not as expected" ) |
| 3747 | return None |
| 3748 | except pexpect.EOF: |
| 3749 | main.log.error( self.name + ": EOF exception found" ) |
| 3750 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3751 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3752 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3753 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3754 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3755 | |
You Wang | cdc51fe | 2018-08-12 17:14:56 -0700 | [diff] [blame] | 3756 | def appStatus( self, appName ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3757 | """ |
| 3758 | Uses the onos:apps cli command to return the status of an application. |
| 3759 | Returns: |
| 3760 | "ACTIVE" - If app is installed and activated |
| 3761 | "INSTALLED" - If app is installed and deactivated |
| 3762 | "UNINSTALLED" - If app is not installed |
| 3763 | None - on error |
| 3764 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3765 | try: |
| 3766 | if not isinstance( appName, types.StringType ): |
| 3767 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3768 | " a string" ) |
| 3769 | return None |
| 3770 | output = self.apps( jsonFormat=True ) |
| 3771 | appsJson = json.loads( output ) |
| 3772 | state = None |
| 3773 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3774 | if appName == app.get( 'name' ): |
| 3775 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3776 | break |
| 3777 | if state == "ACTIVE" or state == "INSTALLED": |
| 3778 | return state |
| 3779 | elif state is None: |
You Wang | 0d9f2c0 | 2018-08-10 14:56:32 -0700 | [diff] [blame] | 3780 | main.log.warn( "{} app not found".format( appName ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3781 | return "UNINSTALLED" |
| 3782 | elif state: |
| 3783 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3784 | str( state ) ) |
| 3785 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3786 | except ( TypeError, ValueError ): |
| 3787 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3788 | return None |
| 3789 | except pexpect.EOF: |
| 3790 | main.log.error( self.name + ": EOF exception found" ) |
| 3791 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3792 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3793 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3794 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3795 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3796 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3797 | def app( self, appName, option ): |
| 3798 | """ |
| 3799 | Interacts with the app command for ONOS. This command manages |
| 3800 | application inventory. |
| 3801 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3802 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3803 | # Validate argument types |
| 3804 | valid = True |
| 3805 | if not isinstance( appName, types.StringType ): |
| 3806 | main.log.error( self.name + ".app(): appName must be a " + |
| 3807 | "string" ) |
| 3808 | valid = False |
| 3809 | if not isinstance( option, types.StringType ): |
| 3810 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3811 | valid = False |
| 3812 | if not valid: |
| 3813 | return main.FALSE |
| 3814 | # Validate Option |
| 3815 | option = option.lower() |
| 3816 | # NOTE: Install may become a valid option |
| 3817 | if option == "activate": |
| 3818 | pass |
| 3819 | elif option == "deactivate": |
| 3820 | pass |
| 3821 | elif option == "uninstall": |
| 3822 | pass |
| 3823 | else: |
| 3824 | # Invalid option |
| 3825 | main.log.error( "The ONOS app command argument only takes " + |
| 3826 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3827 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3828 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3829 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3830 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3831 | assert output is not None, "Error in sendline" |
| 3832 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3833 | if "Error executing command" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3834 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3835 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3836 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3837 | elif "No such application" in output: |
| 3838 | main.log.error( "The application '" + appName + |
| 3839 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3840 | return main.FALSE |
| 3841 | elif "Command not found:" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3842 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3843 | str( output ) ) |
| 3844 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3845 | elif "Unsupported command:" in output: |
| 3846 | main.log.error( "Incorrect command given to 'app': " + |
| 3847 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3848 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3849 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3850 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3851 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3852 | except AssertionError: |
| 3853 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3854 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3855 | except TypeError: |
| 3856 | main.log.exception( self.name + ": Object not as expected" ) |
| 3857 | return main.ERROR |
| 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 | be37960 | 2015-03-24 13:39:32 -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 | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3866 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3867 | """ |
| 3868 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3869 | appName is the hierarchical app name, not the feature name |
| 3870 | If check is True, method will check the status of the app after the |
| 3871 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3872 | Returns main.TRUE if the command was successfully sent |
| 3873 | main.FALSE if the cli responded with an error or given |
| 3874 | incorrect input |
| 3875 | """ |
| 3876 | try: |
| 3877 | if not isinstance( appName, types.StringType ): |
| 3878 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3879 | " a string" ) |
| 3880 | return main.FALSE |
| 3881 | status = self.appStatus( appName ) |
| 3882 | if status == "INSTALLED": |
| 3883 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3884 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3885 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3886 | status = self.appStatus( appName ) |
| 3887 | if status == "ACTIVE": |
| 3888 | return main.TRUE |
| 3889 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3890 | main.log.debug( "The state of application " + |
| 3891 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3892 | time.sleep( 1 ) |
| 3893 | return main.FALSE |
| 3894 | else: # not 'check' or command didn't succeed |
| 3895 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3896 | elif status == "ACTIVE": |
| 3897 | return main.TRUE |
| 3898 | elif status == "UNINSTALLED": |
| 3899 | main.log.error( self.name + ": Tried to activate the " + |
| 3900 | "application '" + appName + "' which is not " + |
| 3901 | "installed." ) |
| 3902 | else: |
| 3903 | main.log.error( "Unexpected return value from appStatus: " + |
| 3904 | str( status ) ) |
| 3905 | return main.ERROR |
| 3906 | except TypeError: |
| 3907 | main.log.exception( self.name + ": Object not as expected" ) |
| 3908 | return main.ERROR |
| 3909 | except pexpect.EOF: |
| 3910 | main.log.error( self.name + ": EOF exception found" ) |
| 3911 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3912 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3913 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3914 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3915 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3916 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3917 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3918 | """ |
| 3919 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3920 | appName is the hierarchical app name, not the feature name |
| 3921 | If check is True, method will check the status of the app after the |
| 3922 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3923 | Returns main.TRUE if the command was successfully sent |
| 3924 | main.FALSE if the cli responded with an error or given |
| 3925 | incorrect input |
| 3926 | """ |
| 3927 | try: |
| 3928 | if not isinstance( appName, types.StringType ): |
| 3929 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3930 | "be a string" ) |
| 3931 | return main.FALSE |
| 3932 | status = self.appStatus( appName ) |
| 3933 | if status == "INSTALLED": |
| 3934 | return main.TRUE |
| 3935 | elif status == "ACTIVE": |
| 3936 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3937 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3938 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3939 | status = self.appStatus( appName ) |
| 3940 | if status == "INSTALLED": |
| 3941 | return main.TRUE |
| 3942 | else: |
| 3943 | time.sleep( 1 ) |
| 3944 | return main.FALSE |
| 3945 | else: # not check or command didn't succeed |
| 3946 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3947 | elif status == "UNINSTALLED": |
| 3948 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3949 | "application '" + appName + "' which is not " + |
| 3950 | "installed." ) |
| 3951 | return main.TRUE |
| 3952 | else: |
| 3953 | main.log.error( "Unexpected return value from appStatus: " + |
| 3954 | str( status ) ) |
| 3955 | return main.ERROR |
| 3956 | except TypeError: |
| 3957 | main.log.exception( self.name + ": Object not as expected" ) |
| 3958 | return main.ERROR |
| 3959 | except pexpect.EOF: |
| 3960 | main.log.error( self.name + ": EOF exception found" ) |
| 3961 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3962 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3963 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3964 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3965 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3966 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3967 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3968 | """ |
| 3969 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3970 | appName is the hierarchical app name, not the feature name |
| 3971 | If check is True, method will check the status of the app after the |
| 3972 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3973 | Returns main.TRUE if the command was successfully sent |
| 3974 | main.FALSE if the cli responded with an error or given |
| 3975 | incorrect input |
| 3976 | """ |
| 3977 | # TODO: check with Thomas about the state machine for apps |
| 3978 | try: |
| 3979 | if not isinstance( appName, types.StringType ): |
| 3980 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3981 | "be a string" ) |
| 3982 | return main.FALSE |
| 3983 | status = self.appStatus( appName ) |
| 3984 | if status == "INSTALLED": |
| 3985 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3986 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3987 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3988 | status = self.appStatus( appName ) |
| 3989 | if status == "UNINSTALLED": |
| 3990 | return main.TRUE |
| 3991 | else: |
| 3992 | time.sleep( 1 ) |
| 3993 | return main.FALSE |
| 3994 | else: # not check or command didn't succeed |
| 3995 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3996 | elif status == "ACTIVE": |
| 3997 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3998 | "application '" + appName + "' which is " + |
| 3999 | "currently active." ) |
| 4000 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4001 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4002 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4003 | status = self.appStatus( appName ) |
| 4004 | if status == "UNINSTALLED": |
| 4005 | return main.TRUE |
| 4006 | else: |
| 4007 | time.sleep( 1 ) |
| 4008 | return main.FALSE |
| 4009 | else: # not check or command didn't succeed |
| 4010 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4011 | elif status == "UNINSTALLED": |
| 4012 | return main.TRUE |
| 4013 | else: |
| 4014 | main.log.error( "Unexpected return value from appStatus: " + |
| 4015 | str( status ) ) |
| 4016 | return main.ERROR |
| 4017 | except TypeError: |
| 4018 | main.log.exception( self.name + ": Object not as expected" ) |
| 4019 | return main.ERROR |
| 4020 | except pexpect.EOF: |
| 4021 | main.log.error( self.name + ": EOF exception found" ) |
| 4022 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4023 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4024 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4025 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4026 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4027 | |
| 4028 | def appIDs( self, jsonFormat=True ): |
| 4029 | """ |
| 4030 | Show the mappings between app id and app names given by the 'app-ids' |
| 4031 | cli command |
| 4032 | """ |
| 4033 | try: |
| 4034 | cmdStr = "app-ids" |
| 4035 | if jsonFormat: |
| 4036 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4037 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4038 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4039 | assert "Command not found:" not in output, output |
| 4040 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4041 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4042 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4043 | main.log.exception( self.name + ": Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4044 | return None |
| 4045 | except TypeError: |
| 4046 | main.log.exception( self.name + ": Object not as expected" ) |
| 4047 | return None |
| 4048 | except pexpect.EOF: |
| 4049 | main.log.error( self.name + ": EOF exception found" ) |
| 4050 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4051 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4052 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4053 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4054 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4055 | |
| 4056 | def appToIDCheck( self ): |
| 4057 | """ |
| 4058 | This method will check that each application's ID listed in 'apps' is |
| 4059 | the same as the ID listed in 'app-ids'. The check will also check that |
| 4060 | there are no duplicate IDs issued. Note that an app ID should be |
| 4061 | a globaly unique numerical identifier for app/app-like features. Once |
| 4062 | an ID is registered, the ID is never freed up so that if an app is |
| 4063 | reinstalled it will have the same ID. |
| 4064 | |
| 4065 | Returns: main.TRUE if the check passes and |
| 4066 | main.FALSE if the check fails or |
| 4067 | main.ERROR if there is some error in processing the test |
| 4068 | """ |
| 4069 | try: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4070 | # Grab IDs |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4071 | rawJson = self.appIDs( jsonFormat=True ) |
| 4072 | if rawJson: |
| 4073 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4074 | else: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4075 | main.log.error( "app-ids returned nothing: " + repr( rawJson ) ) |
| 4076 | return main.FALSE |
| 4077 | |
| 4078 | # Grab Apps |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4079 | rawJson = self.apps( jsonFormat=True ) |
| 4080 | if rawJson: |
| 4081 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4082 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4083 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4084 | return main.FALSE |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4085 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4086 | result = main.TRUE |
| 4087 | for app in apps: |
| 4088 | appID = app.get( 'id' ) |
| 4089 | if appID is None: |
| 4090 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4091 | result = main.FALSE |
| 4092 | appName = app.get( 'name' ) |
| 4093 | if appName is None: |
| 4094 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4095 | result = main.FALSE |
| 4096 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4097 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4098 | if not current: # if ids doesn't have this id |
| 4099 | result = main.FALSE |
| 4100 | main.log.error( "'app-ids' does not have the ID for " + |
| 4101 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4102 | main.log.debug( "apps command returned: " + str( app ) + |
| 4103 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4104 | elif len( current ) > 1: |
| 4105 | # there is more than one app with this ID |
| 4106 | result = main.FALSE |
| 4107 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4108 | elif not current[ 0 ][ 'name' ] == appName: |
| 4109 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4110 | result = main.FALSE |
| 4111 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4112 | " registered under id:" + str( appID ) + |
| 4113 | " but 'apps' has " + str( appName ) ) |
| 4114 | else: |
| 4115 | pass # id and name match! |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4116 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4117 | # now make sure that app-ids has no duplicates |
| 4118 | idsList = [] |
| 4119 | namesList = [] |
| 4120 | for item in ids: |
| 4121 | idsList.append( item[ 'id' ] ) |
| 4122 | namesList.append( item[ 'name' ] ) |
| 4123 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4124 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4125 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4126 | + json.dumps( ids, |
| 4127 | sort_keys=True, |
| 4128 | indent=4, |
| 4129 | separators=( ',', ': ' ) ) ) |
| 4130 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4131 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4132 | except ( TypeError, ValueError ): |
| 4133 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4134 | return main.ERROR |
| 4135 | except pexpect.EOF: |
| 4136 | main.log.error( self.name + ": EOF exception found" ) |
| 4137 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4138 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4139 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4140 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4141 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4142 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4143 | def getCfg( self, component=None, propName=None, short=False, |
| 4144 | jsonFormat=True ): |
| 4145 | """ |
| 4146 | Get configuration settings from onos cli |
| 4147 | Optional arguments: |
| 4148 | component - Optionally only list configurations for a specific |
| 4149 | component. If None, all components with configurations |
| 4150 | are displayed. Case Sensitive string. |
| 4151 | propName - If component is specified, propName option will show |
| 4152 | only this specific configuration from that component. |
| 4153 | Case Sensitive string. |
| 4154 | jsonFormat - Returns output as json. Note that this will override |
| 4155 | the short option |
| 4156 | short - Short, less verbose, version of configurations. |
| 4157 | This is overridden by the json option |
| 4158 | returns: |
| 4159 | Output from cli as a string or None on error |
| 4160 | """ |
| 4161 | try: |
| 4162 | baseStr = "cfg" |
| 4163 | cmdStr = " get" |
| 4164 | componentStr = "" |
| 4165 | if component: |
| 4166 | componentStr += " " + component |
| 4167 | if propName: |
| 4168 | componentStr += " " + propName |
| 4169 | if jsonFormat: |
| 4170 | baseStr += " -j" |
| 4171 | elif short: |
| 4172 | baseStr += " -s" |
| 4173 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4174 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4175 | assert "Command not found:" not in output, output |
| 4176 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4177 | return output |
| 4178 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4179 | main.log.exception( self.name + ": Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4180 | return None |
| 4181 | except TypeError: |
| 4182 | main.log.exception( self.name + ": Object not as expected" ) |
| 4183 | return None |
| 4184 | except pexpect.EOF: |
| 4185 | main.log.error( self.name + ": EOF exception found" ) |
| 4186 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4187 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4188 | except Exception: |
| 4189 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4190 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4191 | |
| 4192 | def setCfg( self, component, propName, value=None, check=True ): |
| 4193 | """ |
| 4194 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4195 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4196 | component - The case sensitive name of the component whose |
| 4197 | property is to be set |
| 4198 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4199 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4200 | 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] | 4201 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4202 | check - Boolean, Check whether the option was successfully set this |
| 4203 | only applies when a value is given. |
| 4204 | returns: |
| 4205 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4206 | will return main.TRUE unless there is an error |
| 4207 | """ |
| 4208 | try: |
| 4209 | baseStr = "cfg" |
| 4210 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4211 | if value is not None: |
| 4212 | cmdStr += " " + str( value ) |
| 4213 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4214 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4215 | assert "Command not found:" not in output, output |
| 4216 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4217 | if value and check: |
| 4218 | results = self.getCfg( component=str( component ), |
| 4219 | propName=str( propName ), |
| 4220 | jsonFormat=True ) |
| 4221 | # Check if current value is what we just set |
| 4222 | try: |
| 4223 | jsonOutput = json.loads( results ) |
| 4224 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4225 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4226 | main.log.exception( "Error parsing cfg output" ) |
| 4227 | main.log.error( "output:" + repr( results ) ) |
| 4228 | return main.FALSE |
| 4229 | if current == str( value ): |
| 4230 | return main.TRUE |
| 4231 | return main.FALSE |
| 4232 | return main.TRUE |
| 4233 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4234 | main.log.exception( self.name + ": Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4235 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4236 | except ( TypeError, ValueError ): |
| 4237 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4238 | return main.FALSE |
| 4239 | except pexpect.EOF: |
| 4240 | main.log.error( self.name + ": EOF exception found" ) |
| 4241 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4242 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4243 | except Exception: |
| 4244 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4245 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4246 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4247 | def distPrimitivesSend( self, cmd ): |
| 4248 | """ |
| 4249 | Function to handle sending cli commands for the distributed primitives test app |
| 4250 | |
| 4251 | This command will catch some exceptions and retry the command on some |
| 4252 | specific store exceptions. |
| 4253 | |
| 4254 | Required arguments: |
| 4255 | cmd - The command to send to the cli |
| 4256 | returns: |
| 4257 | string containing the cli output |
| 4258 | None on Error |
| 4259 | """ |
| 4260 | try: |
| 4261 | output = self.sendline( cmd ) |
| 4262 | try: |
| 4263 | assert output is not None, "Error in sendline" |
| 4264 | # TODO: Maybe make this less hardcoded |
| 4265 | # ConsistentMap Exceptions |
| 4266 | assert "org.onosproject.store.service" not in output |
| 4267 | # Node not leader |
| 4268 | assert "java.lang.IllegalStateException" not in output |
| 4269 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4270 | main.log.error( self.name + ": Error in processing '" + cmd + "' " + |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4271 | "command: " + str( output ) ) |
| 4272 | retryTime = 30 # Conservative time, given by Madan |
| 4273 | main.log.info( "Waiting " + str( retryTime ) + |
| 4274 | "seconds before retrying." ) |
| 4275 | time.sleep( retryTime ) # Due to change in mastership |
| 4276 | output = self.sendline( cmd ) |
| 4277 | assert output is not None, "Error in sendline" |
| 4278 | assert "Command not found:" not in output, output |
| 4279 | assert "Error executing command" not in output, output |
| 4280 | main.log.info( self.name + ": " + output ) |
| 4281 | return output |
| 4282 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4283 | main.log.exception( self.name + ": Error in processing '" + cmd + "' command." ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4284 | return None |
| 4285 | except TypeError: |
| 4286 | main.log.exception( self.name + ": Object not as expected" ) |
| 4287 | return None |
| 4288 | except pexpect.EOF: |
| 4289 | main.log.error( self.name + ": EOF exception found" ) |
| 4290 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4291 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4292 | except Exception: |
| 4293 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4294 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4295 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4296 | def setTestAdd( self, setName, values ): |
| 4297 | """ |
| 4298 | CLI command to add elements to a distributed set. |
| 4299 | Arguments: |
| 4300 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4301 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4302 | Example usages: |
| 4303 | setTestAdd( "set1", "a b c" ) |
| 4304 | setTestAdd( "set2", "1" ) |
| 4305 | returns: |
| 4306 | main.TRUE on success OR |
| 4307 | main.FALSE if elements were already in the set OR |
| 4308 | main.ERROR on error |
| 4309 | """ |
| 4310 | try: |
| 4311 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4312 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4313 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4314 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4315 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4316 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4317 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4318 | return main.FALSE |
| 4319 | else: |
| 4320 | main.log.error( self.name + ": setTestAdd did not" + |
| 4321 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4322 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4323 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4324 | except TypeError: |
| 4325 | main.log.exception( self.name + ": Object not as expected" ) |
| 4326 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4327 | except Exception: |
| 4328 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4329 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4330 | |
| 4331 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4332 | """ |
| 4333 | CLI command to remove elements from a distributed set. |
| 4334 | Required arguments: |
| 4335 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4336 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4337 | Optional arguments: |
| 4338 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4339 | retain - Retain only the given values. (intersection of the |
| 4340 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4341 | returns: |
| 4342 | main.TRUE on success OR |
| 4343 | main.FALSE if the set was not changed OR |
| 4344 | main.ERROR on error |
| 4345 | """ |
| 4346 | try: |
| 4347 | cmdStr = "set-test-remove " |
| 4348 | if clear: |
| 4349 | cmdStr += "-c " + str( setName ) |
| 4350 | elif retain: |
| 4351 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4352 | else: |
| 4353 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4354 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4355 | if clear: |
| 4356 | pattern = "Set " + str( setName ) + " cleared" |
| 4357 | if re.search( pattern, output ): |
| 4358 | return main.TRUE |
| 4359 | elif retain: |
| 4360 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4361 | "only elements of set \[(.*)\]" |
| 4362 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4363 | "retaining only elements of the set " +\ |
| 4364 | "\[(.*)\]" |
| 4365 | if re.search( positivePattern, output ): |
| 4366 | return main.TRUE |
| 4367 | elif re.search( negativePattern, output ): |
| 4368 | return main.FALSE |
| 4369 | else: |
| 4370 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4371 | str( setName ) |
| 4372 | if ( len( values.split() ) == 1 ): |
| 4373 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4374 | str( setName ) |
| 4375 | else: |
| 4376 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4377 | str( setName ) |
| 4378 | if re.search( positivePattern, output ): |
| 4379 | return main.TRUE |
| 4380 | elif re.search( negativePattern, output ): |
| 4381 | return main.FALSE |
| 4382 | main.log.error( self.name + ": setTestRemove did not" + |
| 4383 | " match expected output" ) |
| 4384 | main.log.debug( self.name + " expected: " + pattern ) |
| 4385 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4386 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4387 | except TypeError: |
| 4388 | main.log.exception( self.name + ": Object not as expected" ) |
| 4389 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4390 | except Exception: |
| 4391 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4392 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4393 | |
| 4394 | def setTestGet( self, setName, values="" ): |
| 4395 | """ |
| 4396 | CLI command to get the elements in a distributed set. |
| 4397 | Required arguments: |
| 4398 | setName - The name of the set to remove from. |
| 4399 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4400 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4401 | returns: |
| 4402 | main.ERROR on error OR |
| 4403 | A list of elements in the set if no optional arguments are |
| 4404 | supplied OR |
| 4405 | A tuple containing the list then: |
| 4406 | main.FALSE if the given values are not in the set OR |
| 4407 | main.TRUE if the given values are in the set OR |
| 4408 | """ |
| 4409 | try: |
| 4410 | values = str( values ).strip() |
| 4411 | setName = str( setName ).strip() |
| 4412 | length = len( values.split() ) |
| 4413 | containsCheck = None |
| 4414 | # Patterns to match |
| 4415 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4416 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4417 | containsTrue = "Set " + setName + " contains the value " + values |
| 4418 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4419 | values |
| 4420 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4421 | setPattern |
| 4422 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4423 | " subset " + setPattern |
| 4424 | |
| 4425 | cmdStr = "set-test-get " |
| 4426 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4427 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4428 | if length == 0: |
| 4429 | match = re.search( pattern, output ) |
| 4430 | else: # if given values |
| 4431 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4432 | patternTrue = pattern + "\r\n" + containsTrue |
| 4433 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4434 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4435 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4436 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4437 | matchTrue = re.search( patternTrue, output ) |
| 4438 | matchFalse = re.search( patternFalse, output ) |
| 4439 | if matchTrue: |
| 4440 | containsCheck = main.TRUE |
| 4441 | match = matchTrue |
| 4442 | elif matchFalse: |
| 4443 | containsCheck = main.FALSE |
| 4444 | match = matchFalse |
| 4445 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4446 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4447 | "expected output" ) |
| 4448 | main.log.debug( self.name + " expected: " + pattern ) |
| 4449 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4450 | match = None |
| 4451 | if match: |
| 4452 | setMatch = match.group( 1 ) |
| 4453 | if setMatch == '': |
| 4454 | setList = [] |
| 4455 | else: |
| 4456 | setList = setMatch.split( ", " ) |
| 4457 | if length > 0: |
| 4458 | return ( setList, containsCheck ) |
| 4459 | else: |
| 4460 | return setList |
| 4461 | else: # no match |
| 4462 | main.log.error( self.name + ": setTestGet did not" + |
| 4463 | " match expected output" ) |
| 4464 | main.log.debug( self.name + " expected: " + pattern ) |
| 4465 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4466 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4467 | except TypeError: |
| 4468 | main.log.exception( self.name + ": Object not as expected" ) |
| 4469 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4470 | except Exception: |
| 4471 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4472 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4473 | |
| 4474 | def setTestSize( self, setName ): |
| 4475 | """ |
| 4476 | CLI command to get the elements in a distributed set. |
| 4477 | Required arguments: |
| 4478 | setName - The name of the set to remove from. |
| 4479 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4480 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4481 | None on error |
| 4482 | """ |
| 4483 | try: |
| 4484 | # TODO: Should this check against the number of elements returned |
| 4485 | # and then return true/false based on that? |
| 4486 | setName = str( setName ).strip() |
| 4487 | # Patterns to match |
| 4488 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4489 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4490 | setPattern |
| 4491 | cmdStr = "set-test-get -s " |
| 4492 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4493 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4494 | if output: |
| 4495 | match = re.search( pattern, output ) |
| 4496 | if match: |
| 4497 | setSize = int( match.group( 1 ) ) |
| 4498 | setMatch = match.group( 2 ) |
| 4499 | if len( setMatch.split() ) == setSize: |
| 4500 | main.log.info( "The size returned by " + self.name + |
| 4501 | " matches the number of elements in " + |
| 4502 | "the returned set" ) |
| 4503 | else: |
| 4504 | main.log.error( "The size returned by " + self.name + |
| 4505 | " does not match the number of " + |
| 4506 | "elements in the returned set." ) |
| 4507 | return setSize |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4508 | else: # no match |
| 4509 | main.log.error( self.name + ": setTestGet did not" + |
| 4510 | " match expected output" ) |
| 4511 | main.log.debug( self.name + " expected: " + pattern ) |
| 4512 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4513 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4514 | except TypeError: |
| 4515 | main.log.exception( self.name + ": Object not as expected" ) |
| 4516 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4517 | except Exception: |
| 4518 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4519 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4520 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4521 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4522 | """ |
| 4523 | Command to list the various counters in the system. |
| 4524 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4525 | if jsonFormat, a string of the json object returned by the cli |
| 4526 | command |
| 4527 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4528 | None on error |
| 4529 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4530 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4531 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4532 | if jsonFormat: |
| 4533 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4534 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4535 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4536 | assert "Command not found:" not in output, output |
| 4537 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4538 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4539 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4540 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4541 | main.log.exception( self.name + ": Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4542 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4543 | except TypeError: |
| 4544 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4545 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4546 | except pexpect.EOF: |
| 4547 | main.log.error( self.name + ": EOF exception found" ) |
| 4548 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4549 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4550 | except Exception: |
| 4551 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4552 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4553 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4554 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4555 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4556 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4557 | Required arguments: |
| 4558 | counter - The name of the counter to increment. |
| 4559 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4560 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4561 | returns: |
| 4562 | integer value of the counter or |
| 4563 | None on Error |
| 4564 | """ |
| 4565 | try: |
| 4566 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4567 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4568 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4569 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4570 | if delta != 1: |
| 4571 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4572 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4573 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4574 | match = re.search( pattern, output ) |
| 4575 | if match: |
| 4576 | return int( match.group( 1 ) ) |
| 4577 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4578 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4579 | " match expected output." ) |
| 4580 | main.log.debug( self.name + " expected: " + pattern ) |
| 4581 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4582 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4583 | except TypeError: |
| 4584 | main.log.exception( self.name + ": Object not as expected" ) |
| 4585 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4586 | except Exception: |
| 4587 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4588 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4589 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4590 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4591 | """ |
| 4592 | CLI command to get a distributed counter then add a delta to it. |
| 4593 | Required arguments: |
| 4594 | counter - The name of the counter to increment. |
| 4595 | Optional arguments: |
| 4596 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4597 | returns: |
| 4598 | integer value of the counter or |
| 4599 | None on Error |
| 4600 | """ |
| 4601 | try: |
| 4602 | counter = str( counter ) |
| 4603 | delta = int( delta ) |
| 4604 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4605 | cmdStr += counter |
| 4606 | if delta != 1: |
| 4607 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4608 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4609 | pattern = counter + " was updated to (-?\d+)" |
| 4610 | match = re.search( pattern, output ) |
| 4611 | if match: |
| 4612 | return int( match.group( 1 ) ) |
| 4613 | else: |
| 4614 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4615 | " match expected output." ) |
| 4616 | main.log.debug( self.name + " expected: " + pattern ) |
| 4617 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4618 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4619 | except TypeError: |
| 4620 | main.log.exception( self.name + ": Object not as expected" ) |
| 4621 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4622 | except Exception: |
| 4623 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4624 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4625 | |
| 4626 | def valueTestGet( self, valueName ): |
| 4627 | """ |
| 4628 | CLI command to get the value of an atomic value. |
| 4629 | Required arguments: |
| 4630 | valueName - The name of the value to get. |
| 4631 | returns: |
| 4632 | string value of the value or |
| 4633 | None on Error |
| 4634 | """ |
| 4635 | try: |
| 4636 | valueName = str( valueName ) |
| 4637 | cmdStr = "value-test " |
| 4638 | operation = "get" |
| 4639 | cmdStr = "value-test {} {}".format( valueName, |
| 4640 | operation ) |
| 4641 | output = self.distPrimitivesSend( cmdStr ) |
| 4642 | pattern = "(\w+)" |
| 4643 | match = re.search( pattern, output ) |
| 4644 | if match: |
| 4645 | return match.group( 1 ) |
| 4646 | else: |
| 4647 | main.log.error( self.name + ": valueTestGet did not" + |
| 4648 | " match expected output." ) |
| 4649 | main.log.debug( self.name + " expected: " + pattern ) |
| 4650 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4651 | return None |
| 4652 | except TypeError: |
| 4653 | main.log.exception( self.name + ": Object not as expected" ) |
| 4654 | return None |
| 4655 | except Exception: |
| 4656 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4657 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4658 | |
| 4659 | def valueTestSet( self, valueName, newValue ): |
| 4660 | """ |
| 4661 | CLI command to set the value of an atomic value. |
| 4662 | Required arguments: |
| 4663 | valueName - The name of the value to set. |
| 4664 | newValue - The value to assign to the given value. |
| 4665 | returns: |
| 4666 | main.TRUE on success or |
| 4667 | main.ERROR on Error |
| 4668 | """ |
| 4669 | try: |
| 4670 | valueName = str( valueName ) |
| 4671 | newValue = str( newValue ) |
| 4672 | operation = "set" |
| 4673 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4674 | operation, |
| 4675 | newValue ) |
| 4676 | output = self.distPrimitivesSend( cmdStr ) |
| 4677 | if output is not None: |
| 4678 | return main.TRUE |
| 4679 | else: |
| 4680 | return main.ERROR |
| 4681 | except TypeError: |
| 4682 | main.log.exception( self.name + ": Object not as expected" ) |
| 4683 | return main.ERROR |
| 4684 | except Exception: |
| 4685 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4686 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4687 | |
| 4688 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4689 | """ |
| 4690 | CLI command to compareAndSet the value of an atomic value. |
| 4691 | Required arguments: |
| 4692 | valueName - The name of the value. |
| 4693 | oldValue - Compare the current value of the atomic value to this |
| 4694 | newValue - If the value equals oldValue, set the value to newValue |
| 4695 | returns: |
| 4696 | main.TRUE on success or |
| 4697 | main.FALSE on failure or |
| 4698 | main.ERROR on Error |
| 4699 | """ |
| 4700 | try: |
| 4701 | valueName = str( valueName ) |
| 4702 | oldValue = str( oldValue ) |
| 4703 | newValue = str( newValue ) |
| 4704 | operation = "compareAndSet" |
| 4705 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4706 | operation, |
| 4707 | oldValue, |
| 4708 | newValue ) |
| 4709 | output = self.distPrimitivesSend( cmdStr ) |
| 4710 | pattern = "(\w+)" |
| 4711 | match = re.search( pattern, output ) |
| 4712 | if match: |
| 4713 | result = match.group( 1 ) |
| 4714 | if result == "true": |
| 4715 | return main.TRUE |
| 4716 | elif result == "false": |
| 4717 | return main.FALSE |
| 4718 | else: |
| 4719 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4720 | " match expected output." ) |
| 4721 | main.log.debug( self.name + " expected: " + pattern ) |
| 4722 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4723 | return main.ERROR |
| 4724 | except TypeError: |
| 4725 | main.log.exception( self.name + ": Object not as expected" ) |
| 4726 | return main.ERROR |
| 4727 | except Exception: |
| 4728 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4729 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4730 | |
| 4731 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4732 | """ |
| 4733 | CLI command to getAndSet the value of an atomic value. |
| 4734 | Required arguments: |
| 4735 | valueName - The name of the value to get. |
| 4736 | newValue - The value to assign to the given value |
| 4737 | returns: |
| 4738 | string value of the value or |
| 4739 | None on Error |
| 4740 | """ |
| 4741 | try: |
| 4742 | valueName = str( valueName ) |
| 4743 | cmdStr = "value-test " |
| 4744 | operation = "getAndSet" |
| 4745 | cmdStr += valueName + " " + operation |
| 4746 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4747 | operation, |
| 4748 | newValue ) |
| 4749 | output = self.distPrimitivesSend( cmdStr ) |
| 4750 | pattern = "(\w+)" |
| 4751 | match = re.search( pattern, output ) |
| 4752 | if match: |
| 4753 | return match.group( 1 ) |
| 4754 | else: |
| 4755 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4756 | " match expected output." ) |
| 4757 | main.log.debug( self.name + " expected: " + pattern ) |
| 4758 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4759 | return None |
| 4760 | except TypeError: |
| 4761 | main.log.exception( self.name + ": Object not as expected" ) |
| 4762 | return None |
| 4763 | except Exception: |
| 4764 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4765 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4766 | |
| 4767 | def valueTestDestroy( self, valueName ): |
| 4768 | """ |
| 4769 | CLI command to destroy an atomic value. |
| 4770 | Required arguments: |
| 4771 | valueName - The name of the value to destroy. |
| 4772 | returns: |
| 4773 | main.TRUE on success or |
| 4774 | main.ERROR on Error |
| 4775 | """ |
| 4776 | try: |
| 4777 | valueName = str( valueName ) |
| 4778 | cmdStr = "value-test " |
| 4779 | operation = "destroy" |
| 4780 | cmdStr += valueName + " " + operation |
| 4781 | output = self.distPrimitivesSend( cmdStr ) |
| 4782 | if output is not None: |
| 4783 | return main.TRUE |
| 4784 | else: |
| 4785 | return main.ERROR |
| 4786 | except TypeError: |
| 4787 | main.log.exception( self.name + ": Object not as expected" ) |
| 4788 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4789 | except Exception: |
| 4790 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4791 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4792 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4793 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4794 | """ |
| 4795 | Description: Execute summary command in onos |
| 4796 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4797 | no output |
| 4798 | |
| 4799 | """ |
| 4800 | try: |
| 4801 | cmdStr = "summary" |
| 4802 | if jsonFormat: |
| 4803 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4804 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4805 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4806 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4807 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 4808 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4809 | if not handle: |
| 4810 | main.log.error( self.name + ": There is no output in " + |
| 4811 | "summary command" ) |
| 4812 | return main.FALSE |
| 4813 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4814 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4815 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4816 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4817 | except TypeError: |
| 4818 | main.log.exception( self.name + ": Object not as expected" ) |
| 4819 | return None |
| 4820 | except pexpect.EOF: |
| 4821 | main.log.error( self.name + ": EOF exception found" ) |
| 4822 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4823 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4824 | except Exception: |
| 4825 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4826 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4827 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4828 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4829 | """ |
| 4830 | CLI command to get the value of a key in a consistent map using |
| 4831 | transactions. This a test function and can only get keys from the |
| 4832 | test map hard coded into the cli command |
| 4833 | Required arguments: |
| 4834 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4835 | returns: |
| 4836 | The string value of the key or |
| 4837 | None on Error |
| 4838 | """ |
| 4839 | try: |
| 4840 | keyName = str( keyName ) |
| 4841 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4842 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4843 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4844 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4845 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4846 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4847 | return None |
| 4848 | else: |
| 4849 | match = re.search( pattern, output ) |
| 4850 | if match: |
| 4851 | return match.groupdict()[ 'value' ] |
| 4852 | else: |
| 4853 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4854 | " match expected output." ) |
| 4855 | main.log.debug( self.name + " expected: " + pattern ) |
| 4856 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4857 | return None |
| 4858 | except TypeError: |
| 4859 | main.log.exception( self.name + ": Object not as expected" ) |
| 4860 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4861 | except Exception: |
| 4862 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4863 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4864 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4865 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4866 | """ |
| 4867 | CLI command to put a value into 'numKeys' number of keys in a |
| 4868 | consistent map using transactions. This a test function and can only |
| 4869 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4870 | Required arguments: |
| 4871 | numKeys - Number of keys to add the value to |
| 4872 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4873 | returns: |
| 4874 | A dictionary whose keys are the name of the keys put into the map |
| 4875 | and the values of the keys are dictionaries whose key-values are |
| 4876 | 'value': value put into map and optionaly |
| 4877 | 'oldValue': Previous value in the key or |
| 4878 | None on Error |
| 4879 | |
| 4880 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4881 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4882 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4883 | """ |
| 4884 | try: |
| 4885 | numKeys = str( numKeys ) |
| 4886 | value = str( value ) |
| 4887 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4888 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4889 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4890 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4891 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4892 | results = {} |
| 4893 | for line in output.splitlines(): |
| 4894 | new = re.search( newPattern, line ) |
| 4895 | updated = re.search( updatedPattern, line ) |
| 4896 | if new: |
| 4897 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4898 | elif updated: |
| 4899 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4900 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4901 | else: |
| 4902 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4903 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4904 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4905 | newPattern, |
| 4906 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4907 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4908 | return results |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4909 | except ( TypeError, AttributeError ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4910 | main.log.exception( self.name + ": Object not as expected" ) |
| 4911 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4912 | except Exception: |
| 4913 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4914 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4915 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4916 | def maps( self, jsonFormat=True ): |
| 4917 | """ |
| 4918 | Description: Returns result of onos:maps |
| 4919 | Optional: |
| 4920 | * jsonFormat: enable json formatting of output |
| 4921 | """ |
| 4922 | try: |
| 4923 | cmdStr = "maps" |
| 4924 | if jsonFormat: |
| 4925 | cmdStr += " -j" |
| 4926 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4927 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4928 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4929 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4930 | except AssertionError: |
| 4931 | main.log.exception( "" ) |
| 4932 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4933 | except TypeError: |
| 4934 | main.log.exception( self.name + ": Object not as expected" ) |
| 4935 | return None |
| 4936 | except pexpect.EOF: |
| 4937 | main.log.error( self.name + ": EOF exception found" ) |
| 4938 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4939 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4940 | except Exception: |
| 4941 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4942 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4943 | |
| 4944 | def getSwController( self, uri, jsonFormat=True ): |
| 4945 | """ |
| 4946 | Descrition: Gets the controller information from the device |
| 4947 | """ |
| 4948 | try: |
| 4949 | cmd = "device-controllers " |
| 4950 | if jsonFormat: |
| 4951 | cmd += "-j " |
| 4952 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4953 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4954 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4955 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4956 | except AssertionError: |
| 4957 | main.log.exception( "" ) |
| 4958 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4959 | except TypeError: |
| 4960 | main.log.exception( self.name + ": Object not as expected" ) |
| 4961 | return None |
| 4962 | except pexpect.EOF: |
| 4963 | main.log.error( self.name + ": EOF exception found" ) |
| 4964 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4965 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4966 | except Exception: |
| 4967 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4968 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4969 | |
| 4970 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4971 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4972 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4973 | |
| 4974 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4975 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4976 | ip - String or List: The ip address of the controller. |
| 4977 | This parameter can be formed in a couple of different ways. |
| 4978 | VALID: |
| 4979 | 10.0.0.1 - just the ip address |
| 4980 | tcp:10.0.0.1 - the protocol and the ip address |
| 4981 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4982 | so that you can add controllers with different |
| 4983 | protocols and ports |
| 4984 | INVALID: |
| 4985 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4986 | |
| 4987 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4988 | port - The port number. |
| 4989 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4990 | |
| 4991 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4992 | """ |
| 4993 | try: |
| 4994 | cmd = "device-setcontrollers" |
| 4995 | |
| 4996 | if jsonFormat: |
| 4997 | cmd += " -j" |
| 4998 | cmd += " " + uri |
| 4999 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5000 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5001 | for item in ip: |
| 5002 | if ":" in item: |
| 5003 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5004 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5005 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5006 | elif "." in sitem[ 1 ]: |
| 5007 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5008 | else: |
| 5009 | main.log.error( "Malformed entry: " + item ) |
| 5010 | raise TypeError |
| 5011 | else: |
| 5012 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5013 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5014 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5015 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5016 | if "Error" in response: |
| 5017 | main.log.error( response ) |
| 5018 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5019 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5020 | except AssertionError: |
| 5021 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5022 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5023 | except TypeError: |
| 5024 | main.log.exception( self.name + ": Object not as expected" ) |
| 5025 | return main.FALSE |
| 5026 | except pexpect.EOF: |
| 5027 | main.log.error( self.name + ": EOF exception found" ) |
| 5028 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5029 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5030 | except Exception: |
| 5031 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5032 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5033 | |
| 5034 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5035 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5036 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5037 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5038 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5039 | 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] | 5040 | Returns: |
| 5041 | Returns main.FALSE if an exception is thrown or an error is present |
| 5042 | in the response. Otherwise, returns main.TRUE. |
| 5043 | NOTE: |
| 5044 | 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] | 5045 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5046 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5047 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 5048 | deviceStr = device |
| 5049 | device = [] |
| 5050 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5051 | |
| 5052 | for d in device: |
| 5053 | time.sleep( 1 ) |
| 5054 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5055 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5056 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5057 | if "Error" in response: |
| 5058 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 5059 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5060 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5061 | except AssertionError: |
| 5062 | main.log.exception( "" ) |
| 5063 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5064 | except TypeError: |
| 5065 | main.log.exception( self.name + ": Object not as expected" ) |
| 5066 | return main.FALSE |
| 5067 | except pexpect.EOF: |
| 5068 | main.log.error( self.name + ": EOF exception found" ) |
| 5069 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5070 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5071 | except Exception: |
| 5072 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5073 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5074 | |
| 5075 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5076 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5077 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5078 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5079 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5080 | 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] | 5081 | Returns: |
| 5082 | Returns main.FALSE if an exception is thrown or an error is present |
| 5083 | in the response. Otherwise, returns main.TRUE. |
| 5084 | NOTE: |
| 5085 | 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] | 5086 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5087 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5088 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5089 | host = list( host ) |
| 5090 | |
| 5091 | for h in host: |
| 5092 | time.sleep( 1 ) |
| 5093 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5094 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5095 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5096 | if "Error" in response: |
| 5097 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5098 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5099 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5100 | except AssertionError: |
| 5101 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5102 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5103 | except TypeError: |
| 5104 | main.log.exception( self.name + ": Object not as expected" ) |
| 5105 | return main.FALSE |
| 5106 | except pexpect.EOF: |
| 5107 | main.log.error( self.name + ": EOF exception found" ) |
| 5108 | main.log.error( self.name + ": " + self.handle.before ) |
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 | except Exception: |
| 5111 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5112 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5113 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5114 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5115 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5116 | Description: |
| 5117 | Bring link down or up in the null-provider. |
| 5118 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5119 | begin - (string) One end of a device or switch. |
| 5120 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5121 | returns: |
| 5122 | main.TRUE if no exceptions were thrown and no Errors are |
| 5123 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5124 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5125 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5126 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5127 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5128 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5129 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5130 | if "Error" in response or "Failure" in response: |
| 5131 | main.log.error( response ) |
| 5132 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5133 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5134 | except AssertionError: |
| 5135 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5136 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5137 | except TypeError: |
| 5138 | main.log.exception( self.name + ": Object not as expected" ) |
| 5139 | return main.FALSE |
| 5140 | except pexpect.EOF: |
| 5141 | main.log.error( self.name + ": EOF exception found" ) |
| 5142 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5143 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5144 | except Exception: |
| 5145 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5146 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5147 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5148 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5149 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5150 | Description: |
| 5151 | Changes the state of port in an OF switch by means of the |
| 5152 | PORTSTATUS OF messages. |
| 5153 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5154 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5155 | port - (string) target port in the device. Ex: '2' |
| 5156 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5157 | returns: |
| 5158 | main.TRUE if no exceptions were thrown and no Errors are |
| 5159 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5160 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5161 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5162 | state = state.lower() |
| 5163 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5164 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5165 | response = self.sendline( cmd, showResponse=True ) |
| 5166 | assert response is not None, "Error in sendline" |
| 5167 | assert "Command not found:" not in response, response |
| 5168 | if "Error" in response or "Failure" in response: |
| 5169 | main.log.error( response ) |
| 5170 | return main.FALSE |
| 5171 | return main.TRUE |
| 5172 | except AssertionError: |
| 5173 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5174 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5175 | except TypeError: |
| 5176 | main.log.exception( self.name + ": Object not as expected" ) |
| 5177 | return main.FALSE |
| 5178 | except pexpect.EOF: |
| 5179 | main.log.error( self.name + ": EOF exception found" ) |
| 5180 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5181 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5182 | except Exception: |
| 5183 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5184 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5185 | |
| 5186 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5187 | """ |
| 5188 | Set the logging level to lvl for a specific app |
| 5189 | returns main.TRUE on success |
| 5190 | returns main.FALSE if Error occurred |
| 5191 | if noExit is True, TestON will not exit, but clean up |
| 5192 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5193 | Level defaults to INFO |
| 5194 | """ |
| 5195 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5196 | self.handle.sendline( "log:set %s %s" % ( level, app ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 5197 | self.handle.expect( self.karafPrompt ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5198 | |
| 5199 | response = self.handle.before |
| 5200 | if re.search( "Error", response ): |
| 5201 | return main.FALSE |
| 5202 | return main.TRUE |
| 5203 | except pexpect.TIMEOUT: |
| 5204 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5205 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5206 | except pexpect.EOF: |
| 5207 | main.log.error( self.name + ": EOF exception found" ) |
| 5208 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5209 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5210 | except Exception: |
| 5211 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5212 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5213 | |
| 5214 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5215 | """ |
| 5216 | Return a dictionary which describes the latest network topology data as a |
| 5217 | graph. |
| 5218 | An example of the dictionary: |
| 5219 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5220 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5221 | Each vertex should at least have an 'edges' attribute which describes the |
| 5222 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5223 | 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] | 5224 | list of attributes. |
| 5225 | An example of the edges dictionary: |
| 5226 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5227 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5228 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5229 | in topology data. |
| 5230 | """ |
| 5231 | graphDict = {} |
| 5232 | try: |
| 5233 | links = self.links() |
| 5234 | links = json.loads( links ) |
| 5235 | devices = self.devices() |
| 5236 | devices = json.loads( devices ) |
| 5237 | idToDevice = {} |
| 5238 | for device in devices: |
| 5239 | idToDevice[ device[ 'id' ] ] = device |
| 5240 | if includeHost: |
| 5241 | hosts = self.hosts() |
| 5242 | # FIXME: support 'includeHost' argument |
| 5243 | for link in links: |
| 5244 | nodeA = link[ 'src' ][ 'device' ] |
| 5245 | nodeB = link[ 'dst' ][ 'device' ] |
| 5246 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5247 | if nodeA not in graphDict.keys(): |
| 5248 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5249 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5250 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5251 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5252 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5253 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5254 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5255 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5256 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5257 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5258 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5259 | else: |
| 5260 | # Assert nodeB is not connected to any current links of nodeA |
| 5261 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5262 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5263 | 'type': link[ 'type' ], |
| 5264 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5265 | return graphDict |
| 5266 | except ( TypeError, ValueError ): |
| 5267 | main.log.exception( self.name + ": Object not as expected" ) |
| 5268 | return None |
| 5269 | except KeyError: |
| 5270 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5271 | return None |
| 5272 | except AssertionError: |
| 5273 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5274 | return None |
| 5275 | except pexpect.EOF: |
| 5276 | main.log.error( self.name + ": EOF exception found" ) |
| 5277 | main.log.error( self.name + ": " + self.handle.before ) |
| 5278 | return None |
| 5279 | except Exception: |
| 5280 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5281 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5282 | |
| 5283 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5284 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5285 | Send command to check intent-perf summary |
| 5286 | Returns: dictionary for intent-perf summary |
| 5287 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5288 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5289 | cmd = "intent-perf -s" |
| 5290 | respDic = {} |
| 5291 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5292 | assert resp is not None, "Error in sendline" |
| 5293 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5294 | try: |
| 5295 | # Generate the dictionary to return |
| 5296 | for l in resp.split( "\n" ): |
| 5297 | # Delete any white space in line |
| 5298 | temp = re.sub( r'\s+', '', l ) |
| 5299 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5300 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5301 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5302 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5303 | main.log.exception( self.name + ": Object not as expected" ) |
| 5304 | return None |
| 5305 | except KeyError: |
| 5306 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5307 | return None |
| 5308 | except AssertionError: |
| 5309 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5310 | return None |
| 5311 | except pexpect.EOF: |
| 5312 | main.log.error( self.name + ": EOF exception found" ) |
| 5313 | main.log.error( self.name + ": " + self.handle.before ) |
| 5314 | return None |
| 5315 | except Exception: |
| 5316 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5317 | return None |
| 5318 | return respDic |
| 5319 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5320 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5321 | """ |
| 5322 | Searches the latest ONOS log file for the given search term and |
| 5323 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5324 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5325 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5326 | searchTerm: |
| 5327 | The string to grep from the ONOS log. |
| 5328 | startLine: |
| 5329 | The term that decides which line is the start to search the searchTerm in |
| 5330 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5331 | logNum: |
| 5332 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5333 | information.Because of this, search mutiply logs is necessary to capture |
| 5334 | the right result. logNum is the number of karaf logs that we need to search |
| 5335 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5336 | mode: |
| 5337 | all: return all the strings that contain the search term |
| 5338 | last: return the last string that contains the search term |
| 5339 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5340 | num: return the number of times that the searchTerm appears in the log |
| 5341 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5342 | """ |
| 5343 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5344 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5345 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5346 | logPath = '/opt/onos/log/karaf.log.' |
| 5347 | logPaths = '/opt/onos/log/karaf.log' |
| 5348 | for i in range( 1, logNum ): |
| 5349 | logPaths = logPath + str( i ) + " " + logPaths |
| 5350 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5351 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5352 | # 100000000 is just a extreme large number to make sure this function can |
| 5353 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5354 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5355 | if mode == 'all': |
| 5356 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5357 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5358 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5359 | elif mode == 'first': |
| 5360 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5361 | elif mode == 'num': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5362 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
You Wang | 118ba58 | 2017-01-02 17:14:43 -0800 | [diff] [blame] | 5363 | num = self.sendline( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5364 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5365 | elif mode == 'total': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5366 | totalLines = self.sendline( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5367 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5368 | else: |
| 5369 | main.log.error( self.name + " unsupported mode" ) |
| 5370 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5371 | before = self.sendline( cmd ) |
| 5372 | before = before.splitlines() |
| 5373 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5374 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5375 | return returnLines |
| 5376 | except AssertionError: |
| 5377 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5378 | return None |
| 5379 | except pexpect.EOF: |
| 5380 | main.log.error( self.name + ": EOF exception found" ) |
| 5381 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5382 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5383 | except pexpect.TIMEOUT: |
| 5384 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5385 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5386 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5387 | except Exception: |
| 5388 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5389 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5390 | |
| 5391 | def vplsShow( self, jsonFormat=True ): |
| 5392 | """ |
| 5393 | Description: Returns result of onos:vpls show, which should list the |
| 5394 | configured VPLS networks and the assigned interfaces. |
| 5395 | Optional: |
| 5396 | * jsonFormat: enable json formatting of output |
| 5397 | Returns: |
| 5398 | The output of the command or None on error. |
| 5399 | """ |
| 5400 | try: |
| 5401 | cmdStr = "vpls show" |
| 5402 | if jsonFormat: |
| 5403 | raise NotImplementedError |
| 5404 | cmdStr += " -j" |
| 5405 | handle = self.sendline( cmdStr ) |
| 5406 | assert handle is not None, "Error in sendline" |
| 5407 | assert "Command not found:" not in handle, handle |
| 5408 | return handle |
| 5409 | except AssertionError: |
| 5410 | main.log.exception( "" ) |
| 5411 | return None |
| 5412 | except TypeError: |
| 5413 | main.log.exception( self.name + ": Object not as expected" ) |
| 5414 | return None |
| 5415 | except pexpect.EOF: |
| 5416 | main.log.error( self.name + ": EOF exception found" ) |
| 5417 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5418 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5419 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5420 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5421 | return None |
| 5422 | except Exception: |
| 5423 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5424 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5425 | |
| 5426 | def parseVplsShow( self ): |
| 5427 | """ |
| 5428 | Parse the cli output of 'vpls show' into json output. This is required |
| 5429 | as there is currently no json output available. |
| 5430 | """ |
| 5431 | try: |
| 5432 | output = [] |
| 5433 | raw = self.vplsShow( jsonFormat=False ) |
| 5434 | namePat = "VPLS name: (?P<name>\w+)" |
| 5435 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5436 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5437 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5438 | mIter = re.finditer( pattern, raw ) |
| 5439 | for match in mIter: |
| 5440 | item = {} |
| 5441 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5442 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5443 | if ifaces == [ "" ]: |
| 5444 | ifaces = [] |
| 5445 | item[ 'interfaces' ] = ifaces |
| 5446 | encap = match.group( 'encap' ) |
| 5447 | if encap != 'NONE': |
| 5448 | item[ 'encapsulation' ] = encap.lower() |
| 5449 | output.append( item ) |
| 5450 | return output |
| 5451 | except Exception: |
| 5452 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5453 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5454 | |
| 5455 | def vplsList( self, jsonFormat=True ): |
| 5456 | """ |
| 5457 | Description: Returns result of onos:vpls list, which should list the |
| 5458 | configured VPLS networks. |
| 5459 | Optional: |
| 5460 | * jsonFormat: enable json formatting of output |
| 5461 | """ |
| 5462 | try: |
| 5463 | cmdStr = "vpls list" |
| 5464 | if jsonFormat: |
| 5465 | raise NotImplementedError |
| 5466 | cmdStr += " -j" |
| 5467 | handle = self.sendline( cmdStr ) |
| 5468 | assert handle is not None, "Error in sendline" |
| 5469 | assert "Command not found:" not in handle, handle |
| 5470 | return handle |
| 5471 | except AssertionError: |
| 5472 | main.log.exception( "" ) |
| 5473 | return None |
| 5474 | except TypeError: |
| 5475 | main.log.exception( self.name + ": Object not as expected" ) |
| 5476 | return None |
| 5477 | except pexpect.EOF: |
| 5478 | main.log.error( self.name + ": EOF exception found" ) |
| 5479 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5480 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5481 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5482 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5483 | return None |
| 5484 | except Exception: |
| 5485 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5486 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5487 | |
| 5488 | def vplsCreate( self, network ): |
| 5489 | """ |
| 5490 | CLI command to create a new VPLS network. |
| 5491 | Required arguments: |
| 5492 | network - String name of the network to create. |
| 5493 | returns: |
| 5494 | main.TRUE on success and main.FALSE on failure |
| 5495 | """ |
| 5496 | try: |
| 5497 | network = str( network ) |
| 5498 | cmdStr = "vpls create " |
| 5499 | cmdStr += network |
| 5500 | output = self.sendline( cmdStr ) |
| 5501 | assert output is not None, "Error in sendline" |
| 5502 | assert "Command not found:" not in output, output |
| 5503 | assert "Error executing command" not in output, output |
| 5504 | assert "VPLS already exists:" not in output, output |
| 5505 | return main.TRUE |
| 5506 | except AssertionError: |
| 5507 | main.log.exception( "" ) |
| 5508 | return main.FALSE |
| 5509 | except TypeError: |
| 5510 | main.log.exception( self.name + ": Object not as expected" ) |
| 5511 | return main.FALSE |
| 5512 | except pexpect.EOF: |
| 5513 | main.log.error( self.name + ": EOF exception found" ) |
| 5514 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5515 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5516 | except Exception: |
| 5517 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5518 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5519 | |
| 5520 | def vplsDelete( self, network ): |
| 5521 | """ |
| 5522 | CLI command to delete a VPLS network. |
| 5523 | Required arguments: |
| 5524 | network - Name of the network to delete. |
| 5525 | returns: |
| 5526 | main.TRUE on success and main.FALSE on failure |
| 5527 | """ |
| 5528 | try: |
| 5529 | network = str( network ) |
| 5530 | cmdStr = "vpls delete " |
| 5531 | cmdStr += network |
| 5532 | output = self.sendline( cmdStr ) |
| 5533 | assert output is not None, "Error in sendline" |
| 5534 | assert "Command not found:" not in output, output |
| 5535 | assert "Error executing command" not in output, output |
| 5536 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5537 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5538 | return main.TRUE |
| 5539 | except AssertionError: |
| 5540 | main.log.exception( "" ) |
| 5541 | return main.FALSE |
| 5542 | except TypeError: |
| 5543 | main.log.exception( self.name + ": Object not as expected" ) |
| 5544 | return main.FALSE |
| 5545 | except pexpect.EOF: |
| 5546 | main.log.error( self.name + ": EOF exception found" ) |
| 5547 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5548 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5549 | except Exception: |
| 5550 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5551 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5552 | |
| 5553 | def vplsAddIface( self, network, iface ): |
| 5554 | """ |
| 5555 | CLI command to add an interface to a VPLS network. |
| 5556 | Required arguments: |
| 5557 | network - Name of the network to add the interface to. |
| 5558 | iface - The ONOS name for an interface. |
| 5559 | returns: |
| 5560 | main.TRUE on success and main.FALSE on failure |
| 5561 | """ |
| 5562 | try: |
| 5563 | network = str( network ) |
| 5564 | iface = str( iface ) |
| 5565 | cmdStr = "vpls add-if " |
| 5566 | cmdStr += network + " " + iface |
| 5567 | output = self.sendline( cmdStr ) |
| 5568 | assert output is not None, "Error in sendline" |
| 5569 | assert "Command not found:" not in output, output |
| 5570 | assert "Error executing command" not in output, output |
| 5571 | assert "already associated to network" not in output, output |
| 5572 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5573 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5574 | return main.TRUE |
| 5575 | except AssertionError: |
| 5576 | main.log.exception( "" ) |
| 5577 | return main.FALSE |
| 5578 | except TypeError: |
| 5579 | main.log.exception( self.name + ": Object not as expected" ) |
| 5580 | return main.FALSE |
| 5581 | except pexpect.EOF: |
| 5582 | main.log.error( self.name + ": EOF exception found" ) |
| 5583 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5584 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5585 | except Exception: |
| 5586 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5587 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5588 | |
| 5589 | def vplsRemIface( self, network, iface ): |
| 5590 | """ |
| 5591 | CLI command to remove an interface from a VPLS network. |
| 5592 | Required arguments: |
| 5593 | network - Name of the network to remove the interface from. |
| 5594 | iface - Name of the interface to remove. |
| 5595 | returns: |
| 5596 | main.TRUE on success and main.FALSE on failure |
| 5597 | """ |
| 5598 | try: |
| 5599 | iface = str( iface ) |
| 5600 | cmdStr = "vpls rem-if " |
| 5601 | cmdStr += network + " " + iface |
| 5602 | output = self.sendline( cmdStr ) |
| 5603 | assert output is not None, "Error in sendline" |
| 5604 | assert "Command not found:" not in output, output |
| 5605 | assert "Error executing command" not in output, output |
| 5606 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5607 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5608 | return main.TRUE |
| 5609 | except AssertionError: |
| 5610 | main.log.exception( "" ) |
| 5611 | return main.FALSE |
| 5612 | except TypeError: |
| 5613 | main.log.exception( self.name + ": Object not as expected" ) |
| 5614 | return main.FALSE |
| 5615 | except pexpect.EOF: |
| 5616 | main.log.error( self.name + ": EOF exception found" ) |
| 5617 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5618 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5619 | except Exception: |
| 5620 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5621 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5622 | |
| 5623 | def vplsClean( self ): |
| 5624 | """ |
| 5625 | Description: Clears the VPLS app configuration. |
| 5626 | Returns: main.TRUE on success and main.FALSE on failure |
| 5627 | """ |
| 5628 | try: |
| 5629 | cmdStr = "vpls clean" |
| 5630 | handle = self.sendline( cmdStr ) |
| 5631 | assert handle is not None, "Error in sendline" |
| 5632 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5633 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5634 | return handle |
| 5635 | except AssertionError: |
| 5636 | main.log.exception( "" ) |
| 5637 | return main.FALSE |
| 5638 | except TypeError: |
| 5639 | main.log.exception( self.name + ": Object not as expected" ) |
| 5640 | return main.FALSE |
| 5641 | except pexpect.EOF: |
| 5642 | main.log.error( self.name + ": EOF exception found" ) |
| 5643 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5644 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5645 | except Exception: |
| 5646 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5647 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5648 | |
| 5649 | def vplsSetEncap( self, network, encapType ): |
| 5650 | """ |
| 5651 | CLI command to add an interface to a VPLS network. |
| 5652 | Required arguments: |
| 5653 | network - Name of the network to create. |
| 5654 | encapType - Type of encapsulation. |
| 5655 | returns: |
| 5656 | main.TRUE on success and main.FALSE on failure |
| 5657 | """ |
| 5658 | try: |
| 5659 | network = str( network ) |
| 5660 | encapType = str( encapType ).upper() |
| 5661 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5662 | cmdStr = "vpls set-encap " |
| 5663 | cmdStr += network + " " + encapType |
| 5664 | output = self.sendline( cmdStr ) |
| 5665 | assert output is not None, "Error in sendline" |
| 5666 | assert "Command not found:" not in output, output |
| 5667 | assert "Error executing command" not in output, output |
| 5668 | assert "already associated to network" not in output, output |
| 5669 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5670 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5671 | return main.TRUE |
| 5672 | except AssertionError: |
| 5673 | main.log.exception( "" ) |
| 5674 | return main.FALSE |
| 5675 | except TypeError: |
| 5676 | main.log.exception( self.name + ": Object not as expected" ) |
| 5677 | return main.FALSE |
| 5678 | except pexpect.EOF: |
| 5679 | main.log.error( self.name + ": EOF exception found" ) |
| 5680 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5681 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5682 | except Exception: |
| 5683 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5684 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5685 | |
| 5686 | def interfaces( self, jsonFormat=True ): |
| 5687 | """ |
| 5688 | Description: Returns result of interfaces command. |
| 5689 | Optional: |
| 5690 | * jsonFormat: enable json formatting of output |
| 5691 | Returns: |
| 5692 | The output of the command or None on error. |
| 5693 | """ |
| 5694 | try: |
| 5695 | cmdStr = "interfaces" |
| 5696 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5697 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5698 | cmdStr += " -j" |
| 5699 | handle = self.sendline( cmdStr ) |
| 5700 | assert handle is not None, "Error in sendline" |
| 5701 | assert "Command not found:" not in handle, handle |
| 5702 | return handle |
| 5703 | except AssertionError: |
| 5704 | main.log.exception( "" ) |
| 5705 | return None |
| 5706 | except TypeError: |
| 5707 | main.log.exception( self.name + ": Object not as expected" ) |
| 5708 | return None |
| 5709 | except pexpect.EOF: |
| 5710 | main.log.error( self.name + ": EOF exception found" ) |
| 5711 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5712 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5713 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5714 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5715 | return None |
| 5716 | except Exception: |
| 5717 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5718 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5719 | |
| 5720 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5721 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5722 | Get the timestamp of searchTerm from karaf log. |
| 5723 | |
| 5724 | Arguments: |
| 5725 | splitTerm_before and splitTerm_after: |
| 5726 | |
| 5727 | The terms that split the string that contains the timeStamp of |
| 5728 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5729 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5730 | and the splitTerm_after is "x" |
| 5731 | |
| 5732 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5733 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5734 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5735 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5736 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5737 | return main.ERROR |
| 5738 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5739 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5740 | main.log.warn( "Captured timestamp string is empty" ) |
| 5741 | return main.ERROR |
| 5742 | lines = lines[ 0 ] |
| 5743 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5744 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5745 | # get the target value |
| 5746 | line = lines.split( splitTerm_before ) |
| 5747 | key = line[ 1 ].split( splitTerm_after ) |
| 5748 | return int( key[ 0 ] ) |
| 5749 | except IndexError: |
| 5750 | main.log.warn( "Index Error!" ) |
| 5751 | return main.ERROR |
| 5752 | except AssertionError: |
| 5753 | main.log.warn( "Search Term Not Found " ) |
| 5754 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5755 | |
| 5756 | def workQueueAdd( self, queueName, value ): |
| 5757 | """ |
| 5758 | CLI command to add a string to the specified Work Queue. |
| 5759 | This function uses the distributed primitives test app, which |
| 5760 | gives some cli access to distributed primitives for testing |
| 5761 | purposes only. |
| 5762 | |
| 5763 | Required arguments: |
| 5764 | queueName - The name of the queue to add to |
| 5765 | value - The value to add to the queue |
| 5766 | returns: |
| 5767 | main.TRUE on success, main.FALSE on failure and |
| 5768 | main.ERROR on error. |
| 5769 | """ |
| 5770 | try: |
| 5771 | queueName = str( queueName ) |
| 5772 | value = str( value ) |
| 5773 | prefix = "work-queue-test" |
| 5774 | operation = "add" |
| 5775 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5776 | output = self.distPrimitivesSend( cmdStr ) |
| 5777 | if "Invalid operation name" in output: |
| 5778 | main.log.warn( output ) |
| 5779 | return main.ERROR |
| 5780 | elif "Done" in output: |
| 5781 | return main.TRUE |
| 5782 | except TypeError: |
| 5783 | main.log.exception( self.name + ": Object not as expected" ) |
| 5784 | return main.ERROR |
| 5785 | except Exception: |
| 5786 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5787 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5788 | |
| 5789 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5790 | """ |
| 5791 | CLI command to add two strings to the specified Work Queue. |
| 5792 | This function uses the distributed primitives test app, which |
| 5793 | gives some cli access to distributed primitives for testing |
| 5794 | purposes only. |
| 5795 | |
| 5796 | Required arguments: |
| 5797 | queueName - The name of the queue to add to |
| 5798 | value1 - The first value to add to the queue |
| 5799 | value2 - The second value to add to the queue |
| 5800 | returns: |
| 5801 | main.TRUE on success, main.FALSE on failure and |
| 5802 | main.ERROR on error. |
| 5803 | """ |
| 5804 | try: |
| 5805 | queueName = str( queueName ) |
| 5806 | value1 = str( value1 ) |
| 5807 | value2 = str( value2 ) |
| 5808 | prefix = "work-queue-test" |
| 5809 | operation = "addMultiple" |
| 5810 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 5811 | output = self.distPrimitivesSend( cmdStr ) |
| 5812 | if "Invalid operation name" in output: |
| 5813 | main.log.warn( output ) |
| 5814 | return main.ERROR |
| 5815 | elif "Done" in output: |
| 5816 | return main.TRUE |
| 5817 | except TypeError: |
| 5818 | main.log.exception( self.name + ": Object not as expected" ) |
| 5819 | return main.ERROR |
| 5820 | except Exception: |
| 5821 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5822 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5823 | |
| 5824 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 5825 | """ |
| 5826 | CLI command to take a value from the specified Work Queue and compelte it. |
| 5827 | This function uses the distributed primitives test app, which |
| 5828 | gives some cli access to distributed primitives for testing |
| 5829 | purposes only. |
| 5830 | |
| 5831 | Required arguments: |
| 5832 | queueName - The name of the queue to add to |
| 5833 | number - The number of items to take and complete |
| 5834 | returns: |
| 5835 | main.TRUE on success, main.FALSE on failure and |
| 5836 | main.ERROR on error. |
| 5837 | """ |
| 5838 | try: |
| 5839 | queueName = str( queueName ) |
| 5840 | number = str( int( number ) ) |
| 5841 | prefix = "work-queue-test" |
| 5842 | operation = "takeAndComplete" |
| 5843 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 5844 | output = self.distPrimitivesSend( cmdStr ) |
| 5845 | if "Invalid operation name" in output: |
| 5846 | main.log.warn( output ) |
| 5847 | return main.ERROR |
| 5848 | elif "Done" in output: |
| 5849 | return main.TRUE |
| 5850 | except TypeError: |
| 5851 | main.log.exception( self.name + ": Object not as expected" ) |
| 5852 | return main.ERROR |
| 5853 | except Exception: |
| 5854 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5855 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5856 | |
| 5857 | def workQueueDestroy( self, queueName ): |
| 5858 | """ |
| 5859 | CLI command to destroy the specified Work Queue. |
| 5860 | This function uses the distributed primitives test app, which |
| 5861 | gives some cli access to distributed primitives for testing |
| 5862 | purposes only. |
| 5863 | |
| 5864 | Required arguments: |
| 5865 | queueName - The name of the queue to add to |
| 5866 | returns: |
| 5867 | main.TRUE on success, main.FALSE on failure and |
| 5868 | main.ERROR on error. |
| 5869 | """ |
| 5870 | try: |
| 5871 | queueName = str( queueName ) |
| 5872 | prefix = "work-queue-test" |
| 5873 | operation = "destroy" |
| 5874 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5875 | output = self.distPrimitivesSend( cmdStr ) |
| 5876 | if "Invalid operation name" in output: |
| 5877 | main.log.warn( output ) |
| 5878 | return main.ERROR |
| 5879 | return main.TRUE |
| 5880 | except TypeError: |
| 5881 | main.log.exception( self.name + ": Object not as expected" ) |
| 5882 | return main.ERROR |
| 5883 | except Exception: |
| 5884 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5885 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5886 | |
| 5887 | def workQueueTotalPending( self, queueName ): |
| 5888 | """ |
| 5889 | CLI command to get the Total Pending items of the specified Work Queue. |
| 5890 | This function uses the distributed primitives test app, which |
| 5891 | gives some cli access to distributed primitives for testing |
| 5892 | purposes only. |
| 5893 | |
| 5894 | Required arguments: |
| 5895 | queueName - The name of the queue to add to |
| 5896 | returns: |
| 5897 | The number of Pending items in the specified work queue or |
| 5898 | None on error |
| 5899 | """ |
| 5900 | try: |
| 5901 | queueName = str( queueName ) |
| 5902 | prefix = "work-queue-test" |
| 5903 | operation = "totalPending" |
| 5904 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5905 | output = self.distPrimitivesSend( cmdStr ) |
| 5906 | pattern = r'\d+' |
| 5907 | if "Invalid operation name" in output: |
| 5908 | main.log.warn( output ) |
| 5909 | return None |
| 5910 | else: |
| 5911 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5912 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5913 | except ( AttributeError, TypeError ): |
| 5914 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5915 | return None |
| 5916 | except Exception: |
| 5917 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5918 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5919 | |
| 5920 | def workQueueTotalCompleted( self, queueName ): |
| 5921 | """ |
| 5922 | CLI command to get the Total Completed items of the specified Work Queue. |
| 5923 | This function uses the distributed primitives test app, which |
| 5924 | gives some cli access to distributed primitives for testing |
| 5925 | purposes only. |
| 5926 | |
| 5927 | Required arguments: |
| 5928 | queueName - The name of the queue to add to |
| 5929 | returns: |
| 5930 | The number of complete items in the specified work queue or |
| 5931 | None on error |
| 5932 | """ |
| 5933 | try: |
| 5934 | queueName = str( queueName ) |
| 5935 | prefix = "work-queue-test" |
| 5936 | operation = "totalCompleted" |
| 5937 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5938 | output = self.distPrimitivesSend( cmdStr ) |
| 5939 | pattern = r'\d+' |
| 5940 | if "Invalid operation name" in output: |
| 5941 | main.log.warn( output ) |
| 5942 | return None |
| 5943 | else: |
| 5944 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5945 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5946 | except ( AttributeError, TypeError ): |
| 5947 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5948 | return None |
| 5949 | except Exception: |
| 5950 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5951 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5952 | |
| 5953 | def workQueueTotalInProgress( self, queueName ): |
| 5954 | """ |
| 5955 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 5956 | This function uses the distributed primitives test app, which |
| 5957 | gives some cli access to distributed primitives for testing |
| 5958 | purposes only. |
| 5959 | |
| 5960 | Required arguments: |
| 5961 | queueName - The name of the queue to add to |
| 5962 | returns: |
| 5963 | The number of In Progress items in the specified work queue or |
| 5964 | None on error |
| 5965 | """ |
| 5966 | try: |
| 5967 | queueName = str( queueName ) |
| 5968 | prefix = "work-queue-test" |
| 5969 | operation = "totalInProgress" |
| 5970 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5971 | output = self.distPrimitivesSend( cmdStr ) |
| 5972 | pattern = r'\d+' |
| 5973 | if "Invalid operation name" in output: |
| 5974 | main.log.warn( output ) |
| 5975 | return None |
| 5976 | else: |
| 5977 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5978 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5979 | except ( AttributeError, TypeError ): |
| 5980 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5981 | return None |
| 5982 | except Exception: |
| 5983 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5984 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 5985 | |
| 5986 | def events( self, args='-a' ): |
| 5987 | """ |
| 5988 | Description: Returns events -a command output |
| 5989 | Optional: |
| 5990 | add other arguments |
| 5991 | """ |
| 5992 | try: |
| 5993 | cmdStr = "events" |
| 5994 | if args: |
| 5995 | cmdStr += " " + args |
| 5996 | handle = self.sendline( cmdStr ) |
| 5997 | assert handle is not None, "Error in sendline" |
| 5998 | assert "Command not found:" not in handle, handle |
| 5999 | return handle |
| 6000 | except AssertionError: |
| 6001 | main.log.exception( "" ) |
| 6002 | return None |
| 6003 | except TypeError: |
| 6004 | main.log.exception( self.name + ": Object not as expected" ) |
| 6005 | return None |
| 6006 | except pexpect.EOF: |
| 6007 | main.log.error( self.name + ": EOF exception found" ) |
| 6008 | main.log.error( self.name + ": " + self.handle.before ) |
| 6009 | main.cleanAndExit() |
| 6010 | except Exception: |
| 6011 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6012 | main.cleanAndExit() |
| 6013 | |
| 6014 | def getMaster( self, deviceID ): |
| 6015 | """ |
| 6016 | Description: Obtains current master using "roles" command for a specific deviceID |
| 6017 | """ |
| 6018 | try: |
| 6019 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 6020 | except AssertionError: |
| 6021 | main.log.exception( "" ) |
| 6022 | return None |
| 6023 | except TypeError: |
| 6024 | main.log.exception( self.name + ": Object not as expected" ) |
| 6025 | return None |
| 6026 | except pexpect.EOF: |
| 6027 | main.log.error( self.name + ": EOF exception found" ) |
| 6028 | main.log.error( self.name + ": " + self.handle.before ) |
| 6029 | main.cleanAndExit() |
| 6030 | except Exception: |
| 6031 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 6032 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 6033 | |
| 6034 | def issu( self ): |
| 6035 | """ |
| 6036 | Short summary of In-Service Software Upgrade status |
| 6037 | |
| 6038 | Returns the output of the cli command or None on Error |
| 6039 | """ |
| 6040 | try: |
| 6041 | cmdStr = "issu" |
| 6042 | handle = self.sendline( cmdStr ) |
| 6043 | assert handle is not None, "Error in sendline" |
| 6044 | assert "Command not found:" not in handle, handle |
| 6045 | assert "Unsupported command:" not in handle, handle |
| 6046 | return handle |
| 6047 | except AssertionError: |
| 6048 | main.log.exception( "" ) |
| 6049 | return None |
| 6050 | except TypeError: |
| 6051 | main.log.exception( self.name + ": Object not as expected" ) |
| 6052 | return None |
| 6053 | except pexpect.EOF: |
| 6054 | main.log.error( self.name + ": EOF exception found" ) |
| 6055 | main.log.error( self.name + ": " + self.handle.before ) |
| 6056 | main.cleanAndExit() |
| 6057 | except Exception: |
| 6058 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6059 | main.cleanAndExit() |
| 6060 | |
| 6061 | def issuInit( self ): |
| 6062 | """ |
| 6063 | Initiates an In-Service Software Upgrade |
| 6064 | |
| 6065 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6066 | """ |
| 6067 | try: |
| 6068 | cmdStr = "issu init" |
| 6069 | handle = self.sendline( cmdStr ) |
| 6070 | assert handle is not None, "Error in sendline" |
| 6071 | assert "Command not found:" not in handle, handle |
| 6072 | assert "Unsupported command:" not in handle, handle |
| 6073 | if "Initialized" in handle: |
| 6074 | return main.TRUE |
| 6075 | else: |
| 6076 | return main.FALSE |
| 6077 | except AssertionError: |
| 6078 | main.log.exception( "" ) |
| 6079 | return main.ERROR |
| 6080 | except TypeError: |
| 6081 | main.log.exception( self.name + ": Object not as expected" ) |
| 6082 | return main.ERROR |
| 6083 | except pexpect.EOF: |
| 6084 | main.log.error( self.name + ": EOF exception found" ) |
| 6085 | main.log.error( self.name + ": " + self.handle.before ) |
| 6086 | main.cleanAndExit() |
| 6087 | except Exception: |
| 6088 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6089 | main.cleanAndExit() |
| 6090 | |
| 6091 | def issuUpgrade( self ): |
| 6092 | """ |
| 6093 | Transitions stores to upgraded nodes |
| 6094 | |
| 6095 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6096 | """ |
| 6097 | try: |
| 6098 | cmdStr = "issu upgrade" |
| 6099 | handle = self.sendline( cmdStr ) |
| 6100 | assert handle is not None, "Error in sendline" |
| 6101 | assert "Command not found:" not in handle, handle |
| 6102 | assert "Unsupported command:" not in handle, handle |
| 6103 | if "Upgraded" in handle: |
| 6104 | return main.TRUE |
| 6105 | else: |
| 6106 | return main.FALSE |
| 6107 | except AssertionError: |
| 6108 | main.log.exception( "" ) |
| 6109 | return main.ERROR |
| 6110 | except TypeError: |
| 6111 | main.log.exception( self.name + ": Object not as expected" ) |
| 6112 | return main.ERROR |
| 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!" ) |
| 6119 | main.cleanAndExit() |
| 6120 | |
| 6121 | def issuCommit( self ): |
| 6122 | """ |
| 6123 | Finalizes an In-Service Software Upgrade |
| 6124 | |
| 6125 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6126 | """ |
| 6127 | try: |
| 6128 | cmdStr = "issu commit" |
| 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 | # TODO: Check the version returned by this command |
| 6134 | if "Committed version" in handle: |
| 6135 | return main.TRUE |
| 6136 | else: |
| 6137 | return main.FALSE |
| 6138 | except AssertionError: |
| 6139 | main.log.exception( "" ) |
| 6140 | return main.ERROR |
| 6141 | except TypeError: |
| 6142 | main.log.exception( self.name + ": Object not as expected" ) |
| 6143 | return main.ERROR |
| 6144 | except pexpect.EOF: |
| 6145 | main.log.error( self.name + ": EOF exception found" ) |
| 6146 | main.log.error( self.name + ": " + self.handle.before ) |
| 6147 | main.cleanAndExit() |
| 6148 | except Exception: |
| 6149 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6150 | main.cleanAndExit() |
| 6151 | |
| 6152 | def issuRollback( self ): |
| 6153 | """ |
| 6154 | Rolls back an In-Service Software Upgrade |
| 6155 | |
| 6156 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6157 | """ |
| 6158 | try: |
| 6159 | cmdStr = "issu rollback" |
| 6160 | handle = self.sendline( cmdStr ) |
| 6161 | assert handle is not None, "Error in sendline" |
| 6162 | assert "Command not found:" not in handle, handle |
| 6163 | assert "Unsupported command:" not in handle, handle |
| 6164 | # TODO: Check the version returned by this command |
| 6165 | if "Rolled back to version" in handle: |
| 6166 | return main.TRUE |
| 6167 | else: |
| 6168 | return main.FALSE |
| 6169 | except AssertionError: |
| 6170 | main.log.exception( "" ) |
| 6171 | return main.ERROR |
| 6172 | except TypeError: |
| 6173 | main.log.exception( self.name + ": Object not as expected" ) |
| 6174 | return main.ERROR |
| 6175 | except pexpect.EOF: |
| 6176 | main.log.error( self.name + ": EOF exception found" ) |
| 6177 | main.log.error( self.name + ": " + self.handle.before ) |
| 6178 | main.cleanAndExit() |
| 6179 | except Exception: |
| 6180 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6181 | main.cleanAndExit() |
| 6182 | |
| 6183 | def issuReset( self ): |
| 6184 | """ |
| 6185 | Resets the In-Service Software Upgrade status after a rollback |
| 6186 | |
| 6187 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6188 | """ |
| 6189 | try: |
| 6190 | cmdStr = "issu reset" |
| 6191 | handle = self.sendline( cmdStr ) |
| 6192 | assert handle is not None, "Error in sendline" |
| 6193 | assert "Command not found:" not in handle, handle |
| 6194 | assert "Unsupported command:" not in handle, handle |
| 6195 | # TODO: Check the version returned by this command |
| 6196 | if "Reset version" in handle: |
| 6197 | return main.TRUE |
| 6198 | else: |
| 6199 | return main.FALSE |
| 6200 | except AssertionError: |
| 6201 | main.log.exception( "" ) |
| 6202 | return main.ERROR |
| 6203 | except TypeError: |
| 6204 | main.log.exception( self.name + ": Object not as expected" ) |
| 6205 | return main.ERROR |
| 6206 | except pexpect.EOF: |
| 6207 | main.log.error( self.name + ": EOF exception found" ) |
| 6208 | main.log.error( self.name + ": " + self.handle.before ) |
| 6209 | main.cleanAndExit() |
| 6210 | except Exception: |
| 6211 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6212 | main.cleanAndExit() |
| 6213 | |
| 6214 | def issuStatus( self ): |
| 6215 | """ |
| 6216 | Status of an In-Service Software Upgrade |
| 6217 | |
| 6218 | Returns the output of the cli command or None on Error |
| 6219 | """ |
| 6220 | try: |
| 6221 | cmdStr = "issu status" |
| 6222 | handle = self.sendline( cmdStr ) |
| 6223 | assert handle is not None, "Error in sendline" |
| 6224 | assert "Command not found:" not in handle, handle |
| 6225 | assert "Unsupported command:" not in handle, handle |
| 6226 | return handle |
| 6227 | except AssertionError: |
| 6228 | main.log.exception( "" ) |
| 6229 | return None |
| 6230 | except TypeError: |
| 6231 | main.log.exception( self.name + ": Object not as expected" ) |
| 6232 | return None |
| 6233 | except pexpect.EOF: |
| 6234 | main.log.error( self.name + ": EOF exception found" ) |
| 6235 | main.log.error( self.name + ": " + self.handle.before ) |
| 6236 | main.cleanAndExit() |
| 6237 | except Exception: |
| 6238 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6239 | main.cleanAndExit() |
| 6240 | |
| 6241 | def issuVersion( self ): |
| 6242 | """ |
| 6243 | Get the version of an In-Service Software Upgrade |
| 6244 | |
| 6245 | Returns the output of the cli command or None on Error |
| 6246 | """ |
| 6247 | try: |
| 6248 | cmdStr = "issu version" |
| 6249 | handle = self.sendline( cmdStr ) |
| 6250 | assert handle is not None, "Error in sendline" |
| 6251 | assert "Command not found:" not in handle, handle |
| 6252 | assert "Unsupported command:" not in handle, handle |
| 6253 | return handle |
| 6254 | except AssertionError: |
| 6255 | main.log.exception( "" ) |
| 6256 | return None |
| 6257 | except TypeError: |
| 6258 | main.log.exception( self.name + ": Object not as expected" ) |
| 6259 | return None |
| 6260 | except pexpect.EOF: |
| 6261 | main.log.error( self.name + ": EOF exception found" ) |
| 6262 | main.log.error( self.name + ": " + self.handle.before ) |
| 6263 | main.cleanAndExit() |
| 6264 | except Exception: |
| 6265 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6266 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6267 | |
| 6268 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6269 | """ |
| 6270 | Create a multicast route by calling 'mcast-join' command |
| 6271 | sIP: source IP of the multicast route |
| 6272 | groupIP: group IP of the multicast route |
| 6273 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6274 | dPorts: a list of destination ports of the multicast route |
| 6275 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6276 | """ |
| 6277 | try: |
| 6278 | cmdStr = "mcast-join" |
| 6279 | cmdStr += " " + str( sIP ) |
| 6280 | cmdStr += " " + str( groupIP ) |
| 6281 | cmdStr += " " + str( sPort ) |
| 6282 | assert isinstance( dPorts, list ) |
| 6283 | for dPort in dPorts: |
| 6284 | cmdStr += " " + str( dPort ) |
| 6285 | handle = self.sendline( cmdStr ) |
| 6286 | assert handle is not None, "Error in sendline" |
| 6287 | assert "Command not found:" not in handle, handle |
| 6288 | assert "Unsupported command:" not in handle, handle |
| 6289 | assert "Error executing command" not in handle, handle |
| 6290 | if "Added the mcast route" in handle: |
| 6291 | return main.TRUE |
| 6292 | else: |
| 6293 | return main.FALSE |
| 6294 | except AssertionError: |
| 6295 | main.log.exception( "" ) |
| 6296 | return None |
| 6297 | except TypeError: |
| 6298 | main.log.exception( self.name + ": Object not as expected" ) |
| 6299 | return None |
| 6300 | except pexpect.EOF: |
| 6301 | main.log.error( self.name + ": EOF exception found" ) |
| 6302 | main.log.error( self.name + ": " + self.handle.before ) |
| 6303 | main.cleanAndExit() |
| 6304 | except Exception: |
| 6305 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6306 | main.cleanAndExit() |
| 6307 | |
| 6308 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6309 | """ |
| 6310 | Delete a multicast route by calling 'mcast-delete' command |
| 6311 | sIP: source IP of the multicast route |
| 6312 | groupIP: group IP of the multicast route |
| 6313 | dPorts: a list of destination ports of the multicast route |
| 6314 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6315 | """ |
| 6316 | try: |
| 6317 | cmdStr = "mcast-delete" |
| 6318 | cmdStr += " " + str( sIP ) |
| 6319 | cmdStr += " " + str( groupIP ) |
| 6320 | assert isinstance( dPorts, list ) |
| 6321 | for dPort in dPorts: |
| 6322 | cmdStr += " " + str( dPort ) |
| 6323 | handle = self.sendline( cmdStr ) |
| 6324 | assert handle is not None, "Error in sendline" |
| 6325 | assert "Command not found:" not in handle, handle |
| 6326 | assert "Unsupported command:" not in handle, handle |
| 6327 | assert "Error executing command" not in handle, handle |
| 6328 | if "Updated the mcast route" in handle: |
| 6329 | return main.TRUE |
| 6330 | else: |
| 6331 | return main.FALSE |
| 6332 | except AssertionError: |
| 6333 | main.log.exception( "" ) |
| 6334 | return None |
| 6335 | except TypeError: |
| 6336 | main.log.exception( self.name + ": Object not as expected" ) |
| 6337 | return None |
| 6338 | except pexpect.EOF: |
| 6339 | main.log.error( self.name + ": EOF exception found" ) |
| 6340 | main.log.error( self.name + ": " + self.handle.before ) |
| 6341 | main.cleanAndExit() |
| 6342 | except Exception: |
| 6343 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6344 | main.cleanAndExit() |
| 6345 | |
| 6346 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6347 | """ |
| 6348 | Create a multicast route by calling 'mcast-host-join' command |
| 6349 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6350 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6351 | 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] | 6352 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6353 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6354 | """ |
| 6355 | try: |
| 6356 | cmdStr = "mcast-host-join" |
| 6357 | cmdStr += " -sAddr " + str( sAddr ) |
| 6358 | cmdStr += " -gAddr " + str( gAddr ) |
| 6359 | assert isinstance( srcs, list ) |
| 6360 | for src in srcs: |
| 6361 | cmdStr += " -srcs " + str( src ) |
| 6362 | assert isinstance( sinks, list ) |
| 6363 | for sink in sinks: |
| 6364 | cmdStr += " -sinks " + str( sink ) |
| 6365 | handle = self.sendline( cmdStr ) |
| 6366 | assert handle is not None, "Error in sendline" |
| 6367 | assert "Command not found:" not in handle, handle |
| 6368 | assert "Unsupported command:" not in handle, handle |
| 6369 | assert "Error executing command" not in handle, handle |
| 6370 | if "Added the mcast route" in handle: |
| 6371 | return main.TRUE |
| 6372 | else: |
| 6373 | return main.FALSE |
| 6374 | except AssertionError: |
| 6375 | main.log.exception( "" ) |
| 6376 | return None |
| 6377 | except TypeError: |
| 6378 | main.log.exception( self.name + ": Object not as expected" ) |
| 6379 | return None |
| 6380 | except pexpect.EOF: |
| 6381 | main.log.error( self.name + ": EOF exception found" ) |
| 6382 | main.log.error( self.name + ": " + self.handle.before ) |
| 6383 | main.cleanAndExit() |
| 6384 | except Exception: |
| 6385 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6386 | main.cleanAndExit() |
| 6387 | |
| 6388 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6389 | """ |
| 6390 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6391 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6392 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6393 | 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] | 6394 | will delete the route if not specified |
| 6395 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6396 | """ |
| 6397 | try: |
| 6398 | cmdStr = "mcast-host-delete" |
| 6399 | cmdStr += " -sAddr " + str( sAddr ) |
| 6400 | cmdStr += " -gAddr " + str( gAddr ) |
| 6401 | if host: |
| 6402 | cmdStr += " -h " + str( host ) |
| 6403 | handle = self.sendline( cmdStr ) |
| 6404 | assert handle is not None, "Error in sendline" |
| 6405 | assert "Command not found:" not in handle, handle |
| 6406 | assert "Unsupported command:" not in handle, handle |
| 6407 | assert "Error executing command" not in handle, handle |
| 6408 | if "Updated the mcast route" in handle: |
| 6409 | return main.TRUE |
| 6410 | elif "Deleted the mcast route" in handle: |
| 6411 | return main.TRUE |
| 6412 | else: |
| 6413 | return main.FALSE |
| 6414 | except AssertionError: |
| 6415 | main.log.exception( "" ) |
| 6416 | return None |
| 6417 | except TypeError: |
| 6418 | main.log.exception( self.name + ": Object not as expected" ) |
| 6419 | return None |
| 6420 | except pexpect.EOF: |
| 6421 | main.log.error( self.name + ": EOF exception found" ) |
| 6422 | main.log.error( self.name + ": " + self.handle.before ) |
| 6423 | main.cleanAndExit() |
| 6424 | except Exception: |
| 6425 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6426 | main.cleanAndExit() |
| 6427 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6428 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6429 | """ |
| 6430 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6431 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6432 | gAddr: specifies multicast group address |
| 6433 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6434 | will delete the route if not specified |
| 6435 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6436 | """ |
| 6437 | try: |
| 6438 | cmdStr = "mcast-sink-delete" |
| 6439 | cmdStr += " -sAddr " + str( sAddr ) |
| 6440 | cmdStr += " -gAddr " + str( gAddr ) |
| 6441 | if sink: |
| 6442 | cmdStr += " -s " + str( sink ) |
| 6443 | handle = self.sendline( cmdStr ) |
| 6444 | assert handle is not None, "Error in sendline" |
| 6445 | assert "Command not found:" not in handle, handle |
| 6446 | assert "Unsupported command:" not in handle, handle |
| 6447 | assert "Error executing command" not in handle, handle |
| 6448 | if "Updated the mcast route" in handle: |
| 6449 | return main.TRUE |
| 6450 | elif "Deleted the mcast route" in handle: |
| 6451 | return main.TRUE |
| 6452 | else: |
| 6453 | return main.FALSE |
| 6454 | except AssertionError: |
| 6455 | main.log.exception( "" ) |
| 6456 | return None |
| 6457 | except TypeError: |
| 6458 | main.log.exception( self.name + ": Object not as expected" ) |
| 6459 | return None |
| 6460 | except pexpect.EOF: |
| 6461 | main.log.error( self.name + ": EOF exception found" ) |
| 6462 | main.log.error( self.name + ": " + self.handle.before ) |
| 6463 | main.cleanAndExit() |
| 6464 | except Exception: |
| 6465 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6466 | main.cleanAndExit() |
| 6467 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6468 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6469 | """ |
| 6470 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6471 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6472 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6473 | 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] | 6474 | will delete the route if not specified |
| 6475 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6476 | """ |
| 6477 | try: |
| 6478 | cmdStr = "mcast-source-delete" |
| 6479 | cmdStr += " -sAddr " + str( sAddr ) |
| 6480 | cmdStr += " -gAddr " + str( gAddr ) |
| 6481 | if srcs: |
| 6482 | assert isinstance( srcs, list ) |
| 6483 | for src in srcs: |
| 6484 | cmdStr += " -src " + str( src ) |
| 6485 | handle = self.sendline( cmdStr ) |
| 6486 | assert handle is not None, "Error in sendline" |
| 6487 | assert "Command not found:" not in handle, handle |
| 6488 | assert "Unsupported command:" not in handle, handle |
| 6489 | assert "Error executing command" not in handle, handle |
| 6490 | if "Updated the mcast route" in handle: |
| 6491 | return main.TRUE |
| 6492 | elif "Deleted the mcast route" in handle: |
| 6493 | return main.TRUE |
| 6494 | else: |
| 6495 | return main.FALSE |
| 6496 | except AssertionError: |
| 6497 | main.log.exception( "" ) |
| 6498 | return None |
| 6499 | except TypeError: |
| 6500 | main.log.exception( self.name + ": Object not as expected" ) |
| 6501 | return None |
| 6502 | except pexpect.EOF: |
| 6503 | main.log.error( self.name + ": EOF exception found" ) |
| 6504 | main.log.error( self.name + ": " + self.handle.before ) |
| 6505 | main.cleanAndExit() |
| 6506 | except Exception: |
| 6507 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6508 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6509 | |
| 6510 | def netcfg( self, jsonFormat=True, args="" ): |
| 6511 | """ |
| 6512 | Run netcfg cli command with given args |
| 6513 | """ |
| 6514 | try: |
| 6515 | cmdStr = "netcfg" |
| 6516 | if jsonFormat: |
| 6517 | cmdStr = cmdStr + " -j" |
| 6518 | if args: |
| 6519 | cmdStr = cmdStr + " " + str( args ) |
| 6520 | handle = self.sendline( cmdStr ) |
| 6521 | assert handle is not None, "Error in sendline" |
| 6522 | assert "Command not found:" not in handle, handle |
| 6523 | assert "Unsupported command:" not in handle, handle |
| 6524 | assert "Error executing command" not in handle, handle |
| 6525 | return handle |
| 6526 | except AssertionError: |
| 6527 | main.log.exception( "" ) |
| 6528 | return None |
| 6529 | except TypeError: |
| 6530 | main.log.exception( self.name + ": Object not as expected" ) |
| 6531 | return None |
| 6532 | except pexpect.EOF: |
| 6533 | main.log.error( self.name + ": EOF exception found" ) |
| 6534 | main.log.error( self.name + ": " + self.handle.before ) |
| 6535 | main.cleanAndExit() |
| 6536 | except Exception: |
| 6537 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6538 | main.cleanAndExit() |
| 6539 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6540 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6541 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6542 | 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] | 6543 | Options: |
| 6544 | sAddr: IP address of the source host |
| 6545 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6546 | ipv6: True if hosts are IPv6 |
| 6547 | verbose: return verbose t3 output if True |
| 6548 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6549 | """ |
| 6550 | try: |
| 6551 | # Collect information of both hosts from onos |
| 6552 | hosts = self.hosts() |
| 6553 | hosts = json.loads( hosts ) |
| 6554 | sHost = None |
| 6555 | dHost = None |
| 6556 | for host in hosts: |
| 6557 | if sAddr in host[ "ipAddresses" ]: |
| 6558 | sHost = host |
| 6559 | elif dAddr in host[ "ipAddresses" ]: |
| 6560 | dHost = host |
| 6561 | if sHost and dHost: |
| 6562 | break |
| 6563 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6564 | cmdList = [] |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6565 | if simple: |
| 6566 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6567 | cmdStr = "t3-troubleshoot-simple" |
| 6568 | if verbose: |
| 6569 | cmdStr += " -vv" |
| 6570 | if ipv6: |
| 6571 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6572 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6573 | cmdList.append( cmdStr ) |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6574 | else: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6575 | for location in sHost[ "locations" ]: |
| 6576 | cmdStr = "t3-troubleshoot" |
| 6577 | if verbose: |
| 6578 | cmdStr += " -vv" |
| 6579 | if ipv6: |
| 6580 | cmdStr += " -et ipv6" |
| 6581 | cmdStr += " -s " + str( sAddr ) |
| 6582 | cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] ) |
| 6583 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6584 | if sHost[ "vlan" ] != "None": |
| 6585 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6586 | cmdStr += " -d " + str( dAddr ) |
| 6587 | netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) ) |
| 6588 | netcfg = json.loads( netcfg ) |
| 6589 | assert netcfg, "Failed to get netcfg" |
| 6590 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
| 6591 | cmdList.append( cmdStr ) |
| 6592 | return cmdList |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6593 | except AssertionError: |
| 6594 | main.log.exception( "" ) |
| 6595 | return None |
| 6596 | except ( KeyError, TypeError ): |
| 6597 | main.log.exception( self.name + ": Object not as expected" ) |
| 6598 | return None |
| 6599 | except Exception: |
| 6600 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6601 | main.cleanAndExit() |
| 6602 | |
| 6603 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6604 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6605 | 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] | 6606 | Options: |
| 6607 | sAddr: IP address of the source host |
| 6608 | dAddr: IP address of the destination host |
| 6609 | """ |
| 6610 | try: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6611 | cmdList = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6612 | assert cmdList is not None, "composeT3Command returned None" |
| 6613 | t3Output = "" |
| 6614 | for cmdStr in cmdList: |
| 6615 | handle = self.sendline( cmdStr ) |
| 6616 | assert handle is not None, "Error in sendline" |
| 6617 | assert "Command not found:" not in handle, handle |
| 6618 | assert "Unsupported command:" not in handle, handle |
| 6619 | assert "Error executing command" not in handle, handle |
| 6620 | assert "Tracing packet" in handle |
| 6621 | t3Output += handle |
| 6622 | return t3Output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6623 | except AssertionError: |
| 6624 | main.log.exception( "" ) |
| 6625 | return None |
| 6626 | except pexpect.EOF: |
| 6627 | main.log.error( self.name + ": EOF exception found" ) |
| 6628 | main.log.error( self.name + ": " + self.handle.before ) |
| 6629 | main.cleanAndExit() |
| 6630 | except Exception: |
| 6631 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6632 | main.cleanAndExit() |