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 |
Jon Hall | 39e3ffe | 2018-12-05 11:40:29 -0800 | [diff] [blame] | 561 | karafEscape = re.compile( r"('(0|1)~\'|\r\r\r\n\x1b\[A\x1b\[79C(x)?|\x1b(>|=)|\x1b\[90m~)" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 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 | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 594 | cmdPattern = cmdStr.strip() |
| 595 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 596 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 597 | if relaxedRegex: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 598 | cmdPattern = cmdPattern.split( '|' )[ -1 ].strip() |
| 599 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 600 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 601 | # This was added because karaf 4.2 is stripping some characters from the command echo |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 602 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 39e3ffe | 2018-12-05 11:40:29 -0800 | [diff] [blame] | 603 | if len( output ) < 2: |
| 604 | main.log.warn( "Relaxing regex match to last 5 characters of the sent command" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 605 | cmdPattern = cmdPattern[ -5: ] |
| 606 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 607 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
| 608 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 609 | else: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 610 | output = response.split( cmdPattern, 1 ) |
| 611 | if len( output ) < 2: # TODO: Should we do this without the relaxedRegex flag? |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 612 | main.log.warn( "Relaxing regex match to last 5 characters of the sent command" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 613 | output = response.split( cmdPattern[ -5: ], 1 ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 614 | if output: |
| 615 | if debug: |
| 616 | main.log.debug( self.name + ": split output" ) |
| 617 | for r in output: |
| 618 | main.log.debug( self.name + ": " + repr( r ) ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 619 | if len( output ) == 1: |
| 620 | main.log.error( "Could not remove sent command echo from output" ) |
| 621 | return output |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 622 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 623 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 624 | main.log.info( "Response from ONOS: {}".format( output ) ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 625 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 626 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 627 | except pexpect.TIMEOUT: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 628 | main.log.error( self.name + ": ONOS timeout" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 629 | if debug: |
| 630 | main.log.debug( self.handle.before ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 631 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 632 | self.handle.expect( self.karafPrompt ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 633 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 634 | except IndexError: |
| 635 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 636 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 637 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 638 | except TypeError: |
| 639 | main.log.exception( self.name + ": Object not as expected" ) |
| 640 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 641 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 642 | main.log.error( self.name + ": EOF exception found" ) |
| 643 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 644 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 645 | return None |
| 646 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 647 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 648 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 649 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 650 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 651 | return None |
| 652 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 653 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 654 | |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 655 | def lineCount( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
| 656 | """ |
| 657 | A wrapper around sendline(). Will return the number of lines returned or None on error |
| 658 | |
| 659 | Required Arguments: |
| 660 | cmdStr - String to send to the pexpect session |
| 661 | |
| 662 | Optional Arguments: |
| 663 | showResponse - Defaults to False. If True will log the response. |
| 664 | debug - Defaults to False. If True, will enable debug logging. |
| 665 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 666 | before a timeout. |
| 667 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 668 | closed channel, but instead return None |
| 669 | 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. |
| 670 | |
| 671 | Warning: There are no sanity checking to commands sent using this method. |
| 672 | |
| 673 | """ |
| 674 | try: |
| 675 | numLines = self.sendline( cmdStr, showResponse, debug, timeout, noExit, relaxedRegex ) |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 676 | parsed = re.search( "(\d+)\s+(\d+)", numLines ) |
| 677 | if not parsed: |
| 678 | main.log.error( "Warning, output of karaf's wc may have changed" ) |
| 679 | return None |
| 680 | return parsed.group( 1 ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 681 | except IndexError: |
| 682 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 683 | main.log.debug( "response: {}".format( repr( numLines ) ) ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 684 | return None |
| 685 | except TypeError: |
| 686 | main.log.exception( self.name + ": Object not as expected" ) |
| 687 | return None |
| 688 | except Exception: |
| 689 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 690 | if noExit: |
| 691 | return None |
| 692 | else: |
| 693 | main.cleanAndExit() |
| 694 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 695 | # IMPORTANT NOTE: |
| 696 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 697 | # the cli command changing 'a:b' with 'aB'. |
| 698 | # Ex ) onos:topology > onosTopology |
| 699 | # onos:links > onosLinks |
| 700 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 701 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 702 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 703 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 704 | Adds a new cluster node by ID and address information. |
| 705 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 706 | * nodeId |
| 707 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 708 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 709 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 710 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 711 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 712 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 713 | str( ONOSIp ) + " " + str( tcpPort ) |
| 714 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 715 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 716 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 717 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 718 | main.log.error( self.name + ": Error in adding node" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 719 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 720 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 721 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 722 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 723 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 724 | except AssertionError: |
| 725 | main.log.exception( "" ) |
| 726 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 727 | except TypeError: |
| 728 | main.log.exception( self.name + ": Object not as expected" ) |
| 729 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 730 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 731 | main.log.error( self.name + ": EOF exception found" ) |
| 732 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 733 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 734 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 735 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 736 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 737 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 738 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 739 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 740 | Removes a cluster by ID |
| 741 | Issues command: 'remove-node [<node-id>]' |
| 742 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 743 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 744 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 745 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 746 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 747 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 748 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 749 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 750 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 751 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 752 | main.log.error( self.name + ": Error in removing node" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 753 | main.log.error( handle ) |
| 754 | return main.FALSE |
| 755 | else: |
| 756 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 757 | except AssertionError: |
| 758 | main.log.exception( "" ) |
| 759 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 760 | except TypeError: |
| 761 | main.log.exception( self.name + ": Object not as expected" ) |
| 762 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 763 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 764 | main.log.error( self.name + ": EOF exception found" ) |
| 765 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 766 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 767 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 768 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 769 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 770 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 771 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 772 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 773 | List the nodes currently visible |
| 774 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 775 | Optional argument: |
| 776 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 777 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 778 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 779 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 780 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 781 | cmdStr += " -j" |
| 782 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 783 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 784 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 785 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 786 | except AssertionError: |
| 787 | main.log.exception( "" ) |
| 788 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 789 | except TypeError: |
| 790 | main.log.exception( self.name + ": Object not as expected" ) |
| 791 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 792 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 793 | main.log.error( self.name + ": EOF exception found" ) |
| 794 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 795 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 796 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 797 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 798 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 799 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 800 | def topology( self ): |
| 801 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 802 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 803 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 804 | Return: |
| 805 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 806 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 807 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 808 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 809 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 810 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 811 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 812 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 813 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 814 | except AssertionError: |
| 815 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 816 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 817 | except TypeError: |
| 818 | main.log.exception( self.name + ": Object not as expected" ) |
| 819 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 820 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 821 | main.log.error( self.name + ": EOF exception found" ) |
| 822 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 823 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 824 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 825 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 826 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 827 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 828 | def deviceRemove( self, deviceId ): |
| 829 | """ |
| 830 | Removes particular device from storage |
| 831 | |
| 832 | TODO: refactor this function |
| 833 | """ |
| 834 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 835 | cmdStr = "device-remove " + str( deviceId ) |
| 836 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 837 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 838 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 839 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 840 | main.log.error( self.name + ": Error in removing device" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 841 | main.log.error( handle ) |
| 842 | return main.FALSE |
| 843 | else: |
| 844 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 845 | except AssertionError: |
| 846 | main.log.exception( "" ) |
| 847 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 848 | except TypeError: |
| 849 | main.log.exception( self.name + ": Object not as expected" ) |
| 850 | return None |
| 851 | except pexpect.EOF: |
| 852 | main.log.error( self.name + ": EOF exception found" ) |
| 853 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 854 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 855 | except Exception: |
| 856 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 857 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 858 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 859 | def devices( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 860 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 861 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 862 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 863 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 864 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 865 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 866 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 867 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 868 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 869 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 870 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 871 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 872 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 873 | except AssertionError: |
| 874 | main.log.exception( "" ) |
| 875 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 876 | except TypeError: |
| 877 | main.log.exception( self.name + ": Object not as expected" ) |
| 878 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 879 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 880 | main.log.error( self.name + ": EOF exception found" ) |
| 881 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 882 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 883 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 884 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 885 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 886 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 887 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 888 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 889 | This balances the devices across all controllers |
| 890 | by issuing command: 'onos> onos:balance-masters' |
| 891 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 892 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 893 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 894 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 895 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 896 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 897 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 898 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 899 | main.log.error( self.name + ": Error in balancing masters" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 900 | main.log.error( handle ) |
| 901 | return main.FALSE |
| 902 | else: |
| 903 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 904 | except AssertionError: |
| 905 | main.log.exception( "" ) |
| 906 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 907 | except TypeError: |
| 908 | main.log.exception( self.name + ": Object not as expected" ) |
| 909 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 910 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 911 | main.log.error( self.name + ": EOF exception found" ) |
| 912 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 913 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 914 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 915 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 916 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 917 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 918 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 919 | """ |
| 920 | Returns the output of the masters command. |
| 921 | Optional argument: |
| 922 | * jsonFormat - boolean indicating if you want output in json |
| 923 | """ |
| 924 | try: |
| 925 | cmdStr = "onos:masters" |
| 926 | if jsonFormat: |
| 927 | cmdStr += " -j" |
| 928 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 929 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 930 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 931 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 932 | except AssertionError: |
| 933 | main.log.exception( "" ) |
| 934 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 935 | except TypeError: |
| 936 | main.log.exception( self.name + ": Object not as expected" ) |
| 937 | return None |
| 938 | except pexpect.EOF: |
| 939 | main.log.error( self.name + ": EOF exception found" ) |
| 940 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 941 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 942 | except Exception: |
| 943 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 944 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 945 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 946 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 947 | """ |
| 948 | Uses the master command to check that the devices' leadership |
| 949 | is evenly divided |
| 950 | |
| 951 | Dependencies: checkMasters() and summary() |
| 952 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 953 | Returns main.TRUE if the devices are balanced |
| 954 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 955 | Exits on Exception |
| 956 | Returns None on TypeError |
| 957 | """ |
| 958 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 959 | summaryOutput = self.summary() |
| 960 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 961 | except ( TypeError, ValueError ): |
| 962 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 963 | return None |
| 964 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 965 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 966 | mastersOutput = self.checkMasters() |
| 967 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 968 | first = masters[ 0 ][ "size" ] |
| 969 | for master in masters: |
| 970 | totalOwnedDevices += master[ "size" ] |
| 971 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 972 | main.log.error( "Mastership not balanced" ) |
| 973 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 974 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 975 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 976 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 977 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 978 | except ( TypeError, ValueError ): |
| 979 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 980 | return None |
| 981 | except pexpect.EOF: |
| 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() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 985 | except Exception: |
| 986 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 987 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 988 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 989 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 990 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 991 | Lists all core links |
| 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 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 995 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 996 | cmdStr = "links" |
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" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 999 | handle = self.sendline( cmdStr, timeout=timeout ) |
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 | e821748 | 2014-10-17 13:49:14 -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 | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1016 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1017 | def ports( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1018 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1019 | Lists all ports |
| 1020 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1021 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1022 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1023 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1024 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1025 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1026 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1027 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1028 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1029 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1030 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1031 | except AssertionError: |
| 1032 | main.log.exception( "" ) |
| 1033 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1034 | except TypeError: |
| 1035 | main.log.exception( self.name + ": Object not as expected" ) |
| 1036 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1037 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1038 | main.log.error( self.name + ": EOF exception found" ) |
| 1039 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1040 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1041 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1042 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1043 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1044 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1045 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1046 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1047 | Lists all devices and the controllers with roles assigned to them |
| 1048 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1049 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1050 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1051 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1052 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1053 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1054 | cmdStr += " -j" |
| 1055 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1056 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1057 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1058 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1059 | except AssertionError: |
| 1060 | main.log.exception( "" ) |
| 1061 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1062 | except TypeError: |
| 1063 | main.log.exception( self.name + ": Object not as expected" ) |
| 1064 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1065 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1066 | main.log.error( self.name + ": EOF exception found" ) |
| 1067 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1068 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1069 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1070 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1071 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1072 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1073 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1074 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1075 | Given the a string containing the json representation of the "roles" |
| 1076 | cli command and a partial or whole device id, returns a json object |
| 1077 | containing the roles output for the first device whose id contains |
| 1078 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1079 | |
| 1080 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1081 | A dict of the role assignments for the given device or |
| 1082 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1083 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1084 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1085 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1086 | return None |
| 1087 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1088 | rawRoles = self.roles() |
| 1089 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1090 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1091 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1092 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1093 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1094 | return device |
| 1095 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1096 | except ( TypeError, ValueError ): |
| 1097 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1098 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 1099 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1100 | main.log.error( self.name + ": EOF exception found" ) |
| 1101 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1102 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1103 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1104 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1105 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1106 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1107 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1108 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1109 | Iterates through each device and checks if there is a master assigned |
| 1110 | Returns: main.TRUE if each device has a master |
| 1111 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1112 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1113 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1114 | rawRoles = self.roles() |
| 1115 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1116 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1117 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1118 | # print device |
| 1119 | if device[ 'master' ] == "none": |
| 1120 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1121 | return main.FALSE |
| 1122 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1123 | except ( TypeError, ValueError ): |
| 1124 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1125 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1126 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1127 | main.log.error( self.name + ": EOF exception found" ) |
| 1128 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1129 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1130 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1131 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1132 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1133 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1134 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1135 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1136 | Returns string of paths, and the cost. |
| 1137 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1138 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1139 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1140 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1141 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1142 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1143 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1144 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1145 | main.log.error( self.name + ": Error in getting paths" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1146 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1147 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1148 | path = handle.split( ";" )[ 0 ] |
| 1149 | cost = handle.split( ";" )[ 1 ] |
| 1150 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1151 | except AssertionError: |
| 1152 | main.log.exception( "" ) |
| 1153 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1154 | except TypeError: |
| 1155 | main.log.exception( self.name + ": Object not as expected" ) |
| 1156 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1157 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1158 | main.log.error( self.name + ": EOF exception found" ) |
| 1159 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1160 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1161 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1162 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1163 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1164 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1165 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1166 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1167 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1168 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1169 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1170 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1171 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1172 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1173 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1174 | cmdStr += " -j" |
| 1175 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1176 | if handle: |
| 1177 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1178 | # TODO: Maybe make this less hardcoded |
| 1179 | # ConsistentMap Exceptions |
| 1180 | assert "org.onosproject.store.service" not in handle |
| 1181 | # Node not leader |
| 1182 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1183 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1184 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1185 | main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1186 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1187 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1188 | except TypeError: |
| 1189 | main.log.exception( self.name + ": Object not as expected" ) |
| 1190 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1191 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1192 | main.log.error( self.name + ": EOF exception found" ) |
| 1193 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1194 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1195 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1196 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1197 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1198 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1199 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1200 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1201 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1202 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1203 | 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] | 1204 | partial mac address |
| 1205 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1206 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1207 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1208 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1209 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1210 | return None |
| 1211 | else: |
| 1212 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1213 | rawHosts = self.hosts() |
| 1214 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1215 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1216 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1217 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1218 | if not host: |
| 1219 | pass |
| 1220 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1221 | return host |
| 1222 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1223 | except ( TypeError, ValueError ): |
| 1224 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1225 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1226 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1227 | main.log.error( self.name + ": EOF exception found" ) |
| 1228 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1229 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1230 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1231 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1232 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1233 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1234 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1235 | """ |
| 1236 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1237 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1238 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1239 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1240 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1241 | IMPORTANT: |
| 1242 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1243 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1244 | Furthermore, it assumes that value of VLAN is '-1' |
| 1245 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1246 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1247 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1248 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1249 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1250 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1251 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1252 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1253 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1254 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1255 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1256 | i = iter( str( hostHex ) ) |
| 1257 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1258 | hostHex = hostHex + "/-1" |
| 1259 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1260 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1261 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1262 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1263 | except TypeError: |
| 1264 | main.log.exception( self.name + ": Object not as expected" ) |
| 1265 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1266 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1267 | main.log.error( self.name + ": EOF exception found" ) |
| 1268 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1269 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1270 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1271 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1272 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1273 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1274 | def verifyHostLocation( self, hostIp, location ): |
| 1275 | """ |
| 1276 | Description: |
| 1277 | Verify the host given is discovered in all locations expected |
| 1278 | Required: |
| 1279 | hostIp: IP address of the host |
| 1280 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1281 | Could be a string or list |
| 1282 | Returns: |
| 1283 | main.TRUE if host is discovered on all locations provided |
| 1284 | main.FALSE otherwise |
| 1285 | """ |
| 1286 | import json |
| 1287 | locations = [ location ] if isinstance( location, str ) else location |
| 1288 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1289 | try: |
| 1290 | hosts = self.hosts() |
| 1291 | hosts = json.loads( hosts ) |
| 1292 | targetHost = None |
| 1293 | for host in hosts: |
| 1294 | if hostIp in host[ "ipAddresses" ]: |
| 1295 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1296 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1297 | result = main.TRUE |
| 1298 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1299 | for loc in locations: |
| 1300 | discovered = False |
| 1301 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1302 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1303 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1304 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1305 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1306 | break |
| 1307 | if discovered: |
| 1308 | locationsDiscovered.remove( locDiscovered ) |
| 1309 | else: |
| 1310 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1311 | result = main.FALSE |
| 1312 | if locationsDiscovered: |
| 1313 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1314 | result = main.FALSE |
| 1315 | return result |
| 1316 | except KeyError: |
| 1317 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1318 | return None |
| 1319 | except pexpect.EOF: |
| 1320 | main.log.error( self.name + ": EOF exception found" ) |
| 1321 | main.log.error( self.name + ": " + self.handle.before ) |
| 1322 | main.cleanAndExit() |
| 1323 | except Exception: |
| 1324 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1325 | return None |
| 1326 | |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1327 | def verifyHostIp( self, hostList=[], prefix="" ): |
| 1328 | """ |
| 1329 | Description: |
| 1330 | Verify that all hosts have IP address assigned to them |
| 1331 | Optional: |
| 1332 | hostList: If specified, verifications only happen to the hosts |
| 1333 | in hostList |
| 1334 | prefix: at least one of the ip address assigned to the host |
| 1335 | needs to have the specified prefix |
| 1336 | Returns: |
| 1337 | main.TRUE if all hosts have specific IP address assigned; |
| 1338 | main.FALSE otherwise |
| 1339 | """ |
| 1340 | import json |
| 1341 | try: |
| 1342 | hosts = self.hosts() |
| 1343 | hosts = json.loads( hosts ) |
| 1344 | if not hostList: |
| 1345 | hostList = [ host[ "id" ] for host in hosts ] |
| 1346 | for host in hosts: |
| 1347 | hostId = host[ "id" ] |
| 1348 | if hostId not in hostList: |
| 1349 | continue |
| 1350 | ipList = host[ "ipAddresses" ] |
| 1351 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1352 | if not ipList: |
| 1353 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1354 | else: |
| 1355 | if not any( ip.startswith( str( prefix ) ) for ip in ipList ): |
| 1356 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1357 | else: |
| 1358 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1359 | hostList.remove( hostId ) |
| 1360 | if hostList: |
| 1361 | main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) ) |
| 1362 | return main.FALSE |
| 1363 | else: |
| 1364 | return main.TRUE |
| 1365 | except KeyError: |
| 1366 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1367 | return None |
| 1368 | except pexpect.EOF: |
| 1369 | main.log.error( self.name + ": EOF exception found" ) |
| 1370 | main.log.error( self.name + ": " + self.handle.before ) |
| 1371 | main.cleanAndExit() |
| 1372 | except Exception: |
| 1373 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1374 | return None |
| 1375 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1376 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1377 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1378 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1379 | * hostIdOne: ONOS host id for host1 |
| 1380 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1381 | Optional: |
| 1382 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1383 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1384 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1385 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1386 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1387 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1388 | Returns: |
| 1389 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1390 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1391 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1392 | cmdStr = "add-host-intent " |
| 1393 | if vlanId: |
| 1394 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1395 | if setVlan: |
| 1396 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1397 | if encap: |
| 1398 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1399 | if bandwidth: |
| 1400 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1401 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1402 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1403 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1404 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1405 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1406 | main.log.error( self.name + ": Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1407 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1408 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1409 | else: |
| 1410 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1411 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1412 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1413 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1414 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1415 | else: |
| 1416 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1417 | main.log.debug( "Response from ONOS was: " + |
| 1418 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1419 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1420 | except AssertionError: |
| 1421 | main.log.exception( "" ) |
| 1422 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1423 | except TypeError: |
| 1424 | main.log.exception( self.name + ": Object not as expected" ) |
| 1425 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1426 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1427 | main.log.error( self.name + ": EOF exception found" ) |
| 1428 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1429 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1430 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1431 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1432 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1433 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1434 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1435 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1436 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1437 | * ingressDevice: device id of ingress device |
| 1438 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1439 | Optional: |
| 1440 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1441 | Description: |
| 1442 | Adds an optical intent by specifying an ingress and egress device |
| 1443 | Returns: |
| 1444 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1445 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1446 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1447 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1448 | " " + str( egressDevice ) |
| 1449 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1450 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1451 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1452 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1453 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1454 | main.log.error( self.name + ": Error in adding Optical intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1455 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1456 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1457 | main.log.info( "Optical intent installed between " + |
| 1458 | str( ingressDevice ) + " and " + |
| 1459 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1460 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1461 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1462 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1463 | else: |
| 1464 | main.log.error( "Error, intent ID not found" ) |
| 1465 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1466 | except AssertionError: |
| 1467 | main.log.exception( "" ) |
| 1468 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1469 | except TypeError: |
| 1470 | main.log.exception( self.name + ": Object not as expected" ) |
| 1471 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1472 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1473 | main.log.error( self.name + ": EOF exception found" ) |
| 1474 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1475 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1476 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1477 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1478 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1479 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1480 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1481 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1482 | ingressDevice, |
| 1483 | egressDevice, |
| 1484 | portIngress="", |
| 1485 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1486 | ethType="", |
| 1487 | ethSrc="", |
| 1488 | ethDst="", |
| 1489 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1490 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1491 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1492 | ipProto="", |
| 1493 | ipSrc="", |
| 1494 | ipDst="", |
| 1495 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1496 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1497 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1498 | setVlan="", |
| 1499 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1500 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1501 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1502 | * ingressDevice: device id of ingress device |
| 1503 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1504 | Optional: |
| 1505 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1506 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1507 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1508 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1509 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1510 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1511 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1512 | * ipSrc: specify ip source address |
| 1513 | * ipDst: specify ip destination address |
| 1514 | * tcpSrc: specify tcp source port |
| 1515 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1516 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1517 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1518 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1519 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1520 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1521 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1522 | Returns: |
| 1523 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1524 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1525 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1526 | options developers provide for point-to-point |
| 1527 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1528 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1529 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1530 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1531 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1532 | if ethType: |
| 1533 | cmd += " --ethType " + str( ethType ) |
| 1534 | if ethSrc: |
| 1535 | cmd += " --ethSrc " + str( ethSrc ) |
| 1536 | if ethDst: |
| 1537 | cmd += " --ethDst " + str( ethDst ) |
| 1538 | if bandwidth: |
| 1539 | cmd += " --bandwidth " + str( bandwidth ) |
| 1540 | if lambdaAlloc: |
| 1541 | cmd += " --lambda " |
| 1542 | if ipProto: |
| 1543 | cmd += " --ipProto " + str( ipProto ) |
| 1544 | if ipSrc: |
| 1545 | cmd += " --ipSrc " + str( ipSrc ) |
| 1546 | if ipDst: |
| 1547 | cmd += " --ipDst " + str( ipDst ) |
| 1548 | if tcpSrc: |
| 1549 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1550 | if tcpDst: |
| 1551 | cmd += " --tcpDst " + str( tcpDst ) |
| 1552 | if vlanId: |
| 1553 | cmd += " -v " + str( vlanId ) |
| 1554 | if setVlan: |
| 1555 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1556 | if encap: |
| 1557 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1558 | if protected: |
| 1559 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1560 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1561 | # Check whether the user appended the port |
| 1562 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1563 | if "/" in ingressDevice: |
| 1564 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1565 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1566 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1567 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1568 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1569 | # Would it make sense to throw an exception and exit |
| 1570 | # the test? |
| 1571 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1572 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1573 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1574 | str( ingressDevice ) + "/" +\ |
| 1575 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1576 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1577 | if "/" in egressDevice: |
| 1578 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1579 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1580 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1581 | main.log.error( "You must specify the egress port" ) |
| 1582 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1583 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1584 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1585 | str( egressDevice ) + "/" +\ |
| 1586 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1587 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1588 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1589 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1590 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1591 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1592 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1593 | main.log.error( self.name + ": Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1594 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1595 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1596 | # TODO: print out all the options in this message? |
| 1597 | main.log.info( "Point-to-point intent installed between " + |
| 1598 | str( ingressDevice ) + " and " + |
| 1599 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1600 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1601 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1602 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1603 | else: |
| 1604 | main.log.error( "Error, intent ID not found" ) |
| 1605 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1606 | except AssertionError: |
| 1607 | main.log.exception( "" ) |
| 1608 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1609 | except TypeError: |
| 1610 | main.log.exception( self.name + ": Object not as expected" ) |
| 1611 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1612 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1613 | main.log.error( self.name + ": EOF exception found" ) |
| 1614 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1615 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1616 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1617 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1618 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1619 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1620 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1621 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1622 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1623 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1624 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1625 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1626 | ethType="", |
| 1627 | ethSrc="", |
| 1628 | ethDst="", |
| 1629 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1630 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1631 | ipProto="", |
| 1632 | ipSrc="", |
| 1633 | ipDst="", |
| 1634 | tcpSrc="", |
| 1635 | tcpDst="", |
| 1636 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1637 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1638 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1639 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1640 | partial=False, |
| 1641 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1642 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1643 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1644 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1645 | is same. That is, all ingress devices include port numbers |
| 1646 | with a "/" or all ingress devices could specify device |
| 1647 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1648 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1649 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1650 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1651 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1652 | Optional: |
| 1653 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1654 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1655 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1656 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1657 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1658 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1659 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1660 | * ipSrc: specify ip source address |
| 1661 | * ipDst: specify ip destination address |
| 1662 | * tcpSrc: specify tcp source port |
| 1663 | * tcpDst: specify tcp destination port |
| 1664 | * setEthSrc: action to Rewrite Source MAC Address |
| 1665 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1666 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1667 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1668 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1669 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1670 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1671 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1672 | Returns: |
| 1673 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1674 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1675 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1676 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1677 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1678 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1679 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1680 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1681 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1682 | if ethType: |
| 1683 | cmd += " --ethType " + str( ethType ) |
| 1684 | if ethSrc: |
| 1685 | cmd += " --ethSrc " + str( ethSrc ) |
| 1686 | if ethDst: |
| 1687 | cmd += " --ethDst " + str( ethDst ) |
| 1688 | if bandwidth: |
| 1689 | cmd += " --bandwidth " + str( bandwidth ) |
| 1690 | if lambdaAlloc: |
| 1691 | cmd += " --lambda " |
| 1692 | if ipProto: |
| 1693 | cmd += " --ipProto " + str( ipProto ) |
| 1694 | if ipSrc: |
| 1695 | cmd += " --ipSrc " + str( ipSrc ) |
| 1696 | if ipDst: |
| 1697 | cmd += " --ipDst " + str( ipDst ) |
| 1698 | if tcpSrc: |
| 1699 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1700 | if tcpDst: |
| 1701 | cmd += " --tcpDst " + str( tcpDst ) |
| 1702 | if setEthSrc: |
| 1703 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1704 | if setEthDst: |
| 1705 | cmd += " --setEthDst " + str( setEthDst ) |
| 1706 | if vlanId: |
| 1707 | cmd += " -v " + str( vlanId ) |
| 1708 | if setVlan: |
| 1709 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1710 | if partial: |
| 1711 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1712 | if encap: |
| 1713 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1714 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1715 | # Check whether the user appended the port |
| 1716 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1717 | |
| 1718 | if portIngressList is None: |
| 1719 | for ingressDevice in ingressDeviceList: |
| 1720 | if "/" in ingressDevice: |
| 1721 | cmd += " " + str( ingressDevice ) |
| 1722 | else: |
| 1723 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1724 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1725 | # TODO: perhaps more meaningful return |
| 1726 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1727 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1728 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1729 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1730 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1731 | cmd += " " + \ |
| 1732 | str( ingressDevice ) + "/" +\ |
| 1733 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1734 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1735 | main.log.error( "Device list and port list does not " + |
| 1736 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1737 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1738 | if "/" in egressDevice: |
| 1739 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1740 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1741 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1742 | main.log.error( "You must specify " + |
| 1743 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1744 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1745 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1746 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1747 | str( egressDevice ) + "/" +\ |
| 1748 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1749 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1750 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1751 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1752 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1753 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1754 | main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1755 | "intent" ) |
| 1756 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1757 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1758 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1759 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1760 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1761 | else: |
| 1762 | main.log.error( "Error, intent ID not found" ) |
| 1763 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1764 | except AssertionError: |
| 1765 | main.log.exception( "" ) |
| 1766 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1767 | except TypeError: |
| 1768 | main.log.exception( self.name + ": Object not as expected" ) |
| 1769 | return None |
| 1770 | except pexpect.EOF: |
| 1771 | main.log.error( self.name + ": EOF exception found" ) |
| 1772 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1773 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1774 | except Exception: |
| 1775 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1776 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1777 | |
| 1778 | def addSinglepointToMultipointIntent( |
| 1779 | self, |
| 1780 | ingressDevice, |
| 1781 | egressDeviceList, |
| 1782 | portIngress="", |
| 1783 | portEgressList=None, |
| 1784 | ethType="", |
| 1785 | ethSrc="", |
| 1786 | ethDst="", |
| 1787 | bandwidth="", |
| 1788 | lambdaAlloc=False, |
| 1789 | ipProto="", |
| 1790 | ipSrc="", |
| 1791 | ipDst="", |
| 1792 | tcpSrc="", |
| 1793 | tcpDst="", |
| 1794 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1795 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1796 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1797 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1798 | partial=False, |
| 1799 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1800 | """ |
| 1801 | Note: |
| 1802 | This function assumes the format of all egress devices |
| 1803 | is same. That is, all egress devices include port numbers |
| 1804 | with a "/" or all egress devices could specify device |
| 1805 | ids and port numbers seperately. |
| 1806 | Required: |
| 1807 | * EgressDeviceList: List of device ids of egress device |
| 1808 | ( Atleast 2 eress devices required in the list ) |
| 1809 | * ingressDevice: device id of ingress device |
| 1810 | Optional: |
| 1811 | * ethType: specify ethType |
| 1812 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1813 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1814 | * bandwidth: specify bandwidth capacity of link |
| 1815 | * lambdaAlloc: if True, intent will allocate lambda |
| 1816 | for the specified intent |
| 1817 | * ipProto: specify ip protocol |
| 1818 | * ipSrc: specify ip source address |
| 1819 | * ipDst: specify ip destination address |
| 1820 | * tcpSrc: specify tcp source port |
| 1821 | * tcpDst: specify tcp destination port |
| 1822 | * setEthSrc: action to Rewrite Source MAC Address |
| 1823 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1824 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1825 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1826 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1827 | Description: |
| 1828 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1829 | specifying device id's and optional fields |
| 1830 | Returns: |
| 1831 | A string of the intent id or None on error |
| 1832 | |
| 1833 | NOTE: This function may change depending on the |
| 1834 | options developers provide for singlepoint-to-multipoint |
| 1835 | intent via cli |
| 1836 | """ |
| 1837 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1838 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1839 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1840 | if ethType: |
| 1841 | cmd += " --ethType " + str( ethType ) |
| 1842 | if ethSrc: |
| 1843 | cmd += " --ethSrc " + str( ethSrc ) |
| 1844 | if ethDst: |
| 1845 | cmd += " --ethDst " + str( ethDst ) |
| 1846 | if bandwidth: |
| 1847 | cmd += " --bandwidth " + str( bandwidth ) |
| 1848 | if lambdaAlloc: |
| 1849 | cmd += " --lambda " |
| 1850 | if ipProto: |
| 1851 | cmd += " --ipProto " + str( ipProto ) |
| 1852 | if ipSrc: |
| 1853 | cmd += " --ipSrc " + str( ipSrc ) |
| 1854 | if ipDst: |
| 1855 | cmd += " --ipDst " + str( ipDst ) |
| 1856 | if tcpSrc: |
| 1857 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1858 | if tcpDst: |
| 1859 | cmd += " --tcpDst " + str( tcpDst ) |
| 1860 | if setEthSrc: |
| 1861 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1862 | if setEthDst: |
| 1863 | cmd += " --setEthDst " + str( setEthDst ) |
| 1864 | if vlanId: |
| 1865 | cmd += " -v " + str( vlanId ) |
| 1866 | if setVlan: |
| 1867 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1868 | if partial: |
| 1869 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1870 | if encap: |
| 1871 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1872 | |
| 1873 | # Check whether the user appended the port |
| 1874 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1875 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1876 | if "/" in ingressDevice: |
| 1877 | cmd += " " + str( ingressDevice ) |
| 1878 | else: |
| 1879 | if not portIngress: |
| 1880 | main.log.error( "You must specify " + |
| 1881 | "the Ingress port" ) |
| 1882 | return main.FALSE |
| 1883 | |
| 1884 | cmd += " " +\ |
| 1885 | str( ingressDevice ) + "/" +\ |
| 1886 | str( portIngress ) |
| 1887 | |
| 1888 | if portEgressList is None: |
| 1889 | for egressDevice in egressDeviceList: |
| 1890 | if "/" in egressDevice: |
| 1891 | cmd += " " + str( egressDevice ) |
| 1892 | else: |
| 1893 | main.log.error( "You must specify " + |
| 1894 | "the egress port" ) |
| 1895 | # TODO: perhaps more meaningful return |
| 1896 | return main.FALSE |
| 1897 | else: |
| 1898 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1899 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1900 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1901 | cmd += " " + \ |
| 1902 | str( egressDevice ) + "/" +\ |
| 1903 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1904 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1905 | main.log.error( "Device list and port list does not " + |
| 1906 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1907 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1908 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1909 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1910 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1911 | # If error, return error message |
| 1912 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1913 | main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " + |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1914 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1915 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1916 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1917 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1918 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1919 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1920 | else: |
| 1921 | main.log.error( "Error, intent ID not found" ) |
| 1922 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1923 | except AssertionError: |
| 1924 | main.log.exception( "" ) |
| 1925 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1926 | except TypeError: |
| 1927 | main.log.exception( self.name + ": Object not as expected" ) |
| 1928 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1929 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1930 | main.log.error( self.name + ": EOF exception found" ) |
| 1931 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1932 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1933 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1934 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1935 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1936 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1937 | def addMplsIntent( |
| 1938 | self, |
| 1939 | ingressDevice, |
| 1940 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1941 | ingressPort="", |
| 1942 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1943 | ethType="", |
| 1944 | ethSrc="", |
| 1945 | ethDst="", |
| 1946 | bandwidth="", |
| 1947 | lambdaAlloc=False, |
| 1948 | ipProto="", |
| 1949 | ipSrc="", |
| 1950 | ipDst="", |
| 1951 | tcpSrc="", |
| 1952 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1953 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1954 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1955 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1956 | """ |
| 1957 | Required: |
| 1958 | * ingressDevice: device id of ingress device |
| 1959 | * egressDevice: device id of egress device |
| 1960 | Optional: |
| 1961 | * ethType: specify ethType |
| 1962 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1963 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1964 | * bandwidth: specify bandwidth capacity of link |
| 1965 | * lambdaAlloc: if True, intent will allocate lambda |
| 1966 | for the specified intent |
| 1967 | * ipProto: specify ip protocol |
| 1968 | * ipSrc: specify ip source address |
| 1969 | * ipDst: specify ip destination address |
| 1970 | * tcpSrc: specify tcp source port |
| 1971 | * tcpDst: specify tcp destination port |
| 1972 | * ingressLabel: Ingress MPLS label |
| 1973 | * egressLabel: Egress MPLS label |
| 1974 | Description: |
| 1975 | Adds MPLS intent by |
| 1976 | specifying device id's and optional fields |
| 1977 | Returns: |
| 1978 | A string of the intent id or None on error |
| 1979 | |
| 1980 | NOTE: This function may change depending on the |
| 1981 | options developers provide for MPLS |
| 1982 | intent via cli |
| 1983 | """ |
| 1984 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1985 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1986 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1987 | if ethType: |
| 1988 | cmd += " --ethType " + str( ethType ) |
| 1989 | if ethSrc: |
| 1990 | cmd += " --ethSrc " + str( ethSrc ) |
| 1991 | if ethDst: |
| 1992 | cmd += " --ethDst " + str( ethDst ) |
| 1993 | if bandwidth: |
| 1994 | cmd += " --bandwidth " + str( bandwidth ) |
| 1995 | if lambdaAlloc: |
| 1996 | cmd += " --lambda " |
| 1997 | if ipProto: |
| 1998 | cmd += " --ipProto " + str( ipProto ) |
| 1999 | if ipSrc: |
| 2000 | cmd += " --ipSrc " + str( ipSrc ) |
| 2001 | if ipDst: |
| 2002 | cmd += " --ipDst " + str( ipDst ) |
| 2003 | if tcpSrc: |
| 2004 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 2005 | if tcpDst: |
| 2006 | cmd += " --tcpDst " + str( tcpDst ) |
| 2007 | if ingressLabel: |
| 2008 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 2009 | if egressLabel: |
| 2010 | cmd += " --egressLabel " + str( egressLabel ) |
| 2011 | if priority: |
| 2012 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2013 | |
| 2014 | # Check whether the user appended the port |
| 2015 | # or provided it as an input |
| 2016 | if "/" in ingressDevice: |
| 2017 | cmd += " " + str( ingressDevice ) |
| 2018 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2019 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2020 | main.log.error( "You must specify the ingress port" ) |
| 2021 | return None |
| 2022 | |
| 2023 | cmd += " " + \ |
| 2024 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2025 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2026 | |
| 2027 | if "/" in egressDevice: |
| 2028 | cmd += " " + str( egressDevice ) |
| 2029 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2030 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2031 | main.log.error( "You must specify the egress port" ) |
| 2032 | return None |
| 2033 | |
| 2034 | cmd += " " +\ |
| 2035 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2036 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2037 | |
| 2038 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2039 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2040 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2041 | # If error, return error message |
| 2042 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2043 | main.log.error( self.name + ": Error in adding mpls intent" ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2044 | return None |
| 2045 | else: |
| 2046 | # TODO: print out all the options in this message? |
| 2047 | main.log.info( "MPLS intent installed between " + |
| 2048 | str( ingressDevice ) + " and " + |
| 2049 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2050 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2051 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2052 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2053 | else: |
| 2054 | main.log.error( "Error, intent ID not found" ) |
| 2055 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2056 | except AssertionError: |
| 2057 | main.log.exception( "" ) |
| 2058 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2059 | except TypeError: |
| 2060 | main.log.exception( self.name + ": Object not as expected" ) |
| 2061 | return None |
| 2062 | except pexpect.EOF: |
| 2063 | main.log.error( self.name + ": EOF exception found" ) |
| 2064 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2065 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2066 | except Exception: |
| 2067 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2068 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2069 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2070 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 2071 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2072 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2073 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2074 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2075 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2076 | -p or --purge: Purge the intent from the store after removal |
| 2077 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2078 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 2079 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2080 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2081 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2082 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2083 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2084 | if purge: |
| 2085 | cmdStr += " -p" |
| 2086 | if sync: |
| 2087 | cmdStr += " -s" |
| 2088 | |
| 2089 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2090 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2091 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2092 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2093 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2094 | main.log.error( self.name + ": Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2095 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2096 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2097 | # TODO: Should this be main.TRUE |
| 2098 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2099 | except AssertionError: |
| 2100 | main.log.exception( "" ) |
| 2101 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2102 | except TypeError: |
| 2103 | main.log.exception( self.name + ": Object not as expected" ) |
| 2104 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2105 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2106 | main.log.error( self.name + ": EOF exception found" ) |
| 2107 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2108 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2109 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2110 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2111 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2112 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2113 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2114 | """ |
| 2115 | Description: |
| 2116 | Remove all the intents |
| 2117 | Optional args:- |
| 2118 | -s or --sync: Waits for the removal before returning |
| 2119 | -p or --purge: Purge the intent from the store after removal |
| 2120 | Returns: |
| 2121 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2122 | main.FALSE; Returns None for exception |
| 2123 | """ |
| 2124 | try: |
| 2125 | cmdStr = "remove-intent" |
| 2126 | if purge: |
| 2127 | cmdStr += " -p" |
| 2128 | if sync: |
| 2129 | cmdStr += " -s" |
| 2130 | |
| 2131 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2132 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2133 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2134 | assert "Command not found:" not in handle, handle |
| 2135 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2136 | main.log.error( self.name + ": Error in removing intent" ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2137 | return main.FALSE |
| 2138 | else: |
| 2139 | return main.TRUE |
| 2140 | except AssertionError: |
| 2141 | main.log.exception( "" ) |
| 2142 | return None |
| 2143 | except TypeError: |
| 2144 | main.log.exception( self.name + ": Object not as expected" ) |
| 2145 | return None |
| 2146 | except pexpect.EOF: |
| 2147 | main.log.error( self.name + ": EOF exception found" ) |
| 2148 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2149 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2150 | except Exception: |
| 2151 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2152 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2153 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2154 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2155 | """ |
| 2156 | Purges all WITHDRAWN Intents |
| 2157 | """ |
| 2158 | try: |
| 2159 | cmdStr = "purge-intents" |
| 2160 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2161 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2162 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2163 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2164 | main.log.error( self.name + ": Error in purging intents" ) |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2165 | return main.FALSE |
| 2166 | else: |
| 2167 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2168 | except AssertionError: |
| 2169 | main.log.exception( "" ) |
| 2170 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2171 | except TypeError: |
| 2172 | main.log.exception( self.name + ": Object not as expected" ) |
| 2173 | return None |
| 2174 | except pexpect.EOF: |
| 2175 | main.log.error( self.name + ": EOF exception found" ) |
| 2176 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2177 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2178 | except Exception: |
| 2179 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2180 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2181 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2182 | def wipeout( self ): |
| 2183 | """ |
| 2184 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2185 | """ |
| 2186 | try: |
| 2187 | cmdStr = "wipe-out please" |
| 2188 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2189 | assert handle is not None, "Error in sendline" |
| 2190 | assert "Command not found:" not in handle, handle |
| 2191 | return main.TRUE |
| 2192 | except AssertionError: |
| 2193 | main.log.exception( "" ) |
| 2194 | return None |
| 2195 | except TypeError: |
| 2196 | main.log.exception( self.name + ": Object not as expected" ) |
| 2197 | return None |
| 2198 | except pexpect.EOF: |
| 2199 | main.log.error( self.name + ": EOF exception found" ) |
| 2200 | main.log.error( self.name + ": " + self.handle.before ) |
| 2201 | main.cleanAndExit() |
| 2202 | except Exception: |
| 2203 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2204 | main.cleanAndExit() |
| 2205 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2206 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2207 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2208 | NOTE: This method should be used after installing application: |
| 2209 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2210 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2211 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2212 | Description: |
| 2213 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2214 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2215 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2216 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2217 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2218 | cmdStr += " -j" |
| 2219 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2220 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2221 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2222 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2223 | except AssertionError: |
| 2224 | main.log.exception( "" ) |
| 2225 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2226 | except TypeError: |
| 2227 | main.log.exception( self.name + ": Object not as expected" ) |
| 2228 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2229 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 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() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2233 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2234 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2235 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2236 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2237 | def ipv4RouteNumber( self ): |
| 2238 | """ |
| 2239 | NOTE: This method should be used after installing application: |
| 2240 | onos-app-sdnip |
| 2241 | Description: |
| 2242 | Obtain the total IPv4 routes number in the system |
| 2243 | """ |
| 2244 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2245 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2246 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2247 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2248 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2249 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2250 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2251 | except AssertionError: |
| 2252 | main.log.exception( "" ) |
| 2253 | return None |
| 2254 | except ( TypeError, ValueError ): |
| 2255 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2256 | return None |
| 2257 | except pexpect.EOF: |
| 2258 | main.log.error( self.name + ": EOF exception found" ) |
| 2259 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2260 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2261 | except Exception: |
| 2262 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2263 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2264 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2265 | # =============Function to check Bandwidth allocation======== |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2266 | def allocations( self, jsonFormat = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2267 | """ |
| 2268 | Description: |
| 2269 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2270 | """ |
| 2271 | try: |
| 2272 | cmdStr = "allocations" |
| 2273 | if jsonFormat: |
| 2274 | cmdStr += " -j" |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2275 | handle = self.sendline( cmdStr, timeout=300 ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2276 | assert handle is not None, "Error in sendline" |
| 2277 | assert "Command not found:" not in handle, handle |
| 2278 | return handle |
| 2279 | except AssertionError: |
| 2280 | main.log.exception( "" ) |
| 2281 | return None |
| 2282 | except ( TypeError, ValueError ): |
| 2283 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2284 | return None |
| 2285 | except pexpect.EOF: |
| 2286 | main.log.error( self.name + ": EOF exception found" ) |
| 2287 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2288 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2289 | except Exception: |
| 2290 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2291 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2292 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2293 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2294 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2295 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2296 | Obtain intents from the ONOS cli. |
| 2297 | Optional: |
| 2298 | * jsonFormat: Enable output formatting in json, default to True |
| 2299 | * summary: Whether only output the intent summary, defaults to False |
| 2300 | * type: Only output a certain type of intent. This options is valid |
| 2301 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2302 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2303 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2304 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2305 | if summary: |
| 2306 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2307 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2308 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2309 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2310 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2311 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2312 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2313 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2314 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2315 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2316 | intentType = "" |
| 2317 | # IF we want the summary of a specific intent type |
| 2318 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2319 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2320 | if intentType in jsonResult.keys(): |
| 2321 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2322 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2323 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2324 | return handle |
| 2325 | else: |
| 2326 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2327 | except AssertionError: |
| 2328 | main.log.exception( "" ) |
| 2329 | return None |
| 2330 | except ( TypeError, ValueError ): |
| 2331 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2332 | return None |
| 2333 | except pexpect.EOF: |
| 2334 | main.log.error( self.name + ": EOF exception found" ) |
| 2335 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2336 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2337 | except Exception: |
| 2338 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2339 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2340 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2341 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2342 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2343 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2344 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2345 | list of intent IDs. |
| 2346 | Parameters: |
| 2347 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2348 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2349 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2350 | 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] | 2351 | accepted. |
| 2352 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2353 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2354 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2355 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2356 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2357 | try: |
| 2358 | state = "State is Undefined" |
| 2359 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2360 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2361 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2362 | rawJson = intentsJson |
| 2363 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2364 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2365 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2366 | if intentsId == intent[ 'id' ]: |
| 2367 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2368 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2369 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2370 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2371 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2372 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2373 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2374 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2375 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2376 | for intent in parsedIntentsJson: |
| 2377 | if intentsId[ i ] == intent[ 'id' ]: |
| 2378 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2379 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2380 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2381 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2382 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2383 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2384 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2385 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2386 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2387 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2388 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2389 | except ( TypeError, ValueError ): |
| 2390 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2391 | return None |
| 2392 | except pexpect.EOF: |
| 2393 | main.log.error( self.name + ": EOF exception found" ) |
| 2394 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2395 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2396 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2397 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2398 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2399 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2400 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2401 | """ |
| 2402 | Description: |
| 2403 | Check intents state |
| 2404 | Required: |
| 2405 | intentsId - List of intents ID to be checked |
| 2406 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2407 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2408 | state in the list. |
| 2409 | *NOTE: You can pass in a list of expected state, |
| 2410 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2411 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2412 | Returns main.TRUE only if all intent are the same as expected states, |
| 2413 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2414 | """ |
| 2415 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2416 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2417 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2418 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2419 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2420 | intentsDict = [] |
| 2421 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2422 | if isinstance( intentsId, types.StringType ) \ |
| 2423 | and intent.get( 'id' ) == intentsId: |
| 2424 | intentsDict.append( intent ) |
| 2425 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2426 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2427 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2428 | |
| 2429 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2430 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2431 | "getting intents state" ) |
| 2432 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2433 | |
| 2434 | if isinstance( expectedState, types.StringType ): |
| 2435 | for intents in intentsDict: |
| 2436 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2437 | main.log.debug( self.name + " : Intent ID - " + |
| 2438 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2439 | " actual state = " + |
| 2440 | intents.get( 'state' ) |
| 2441 | + " does not equal expected state = " |
| 2442 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2443 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2444 | elif isinstance( expectedState, types.ListType ): |
| 2445 | for intents in intentsDict: |
| 2446 | if not any( state == intents.get( 'state' ) for state in |
| 2447 | expectedState ): |
| 2448 | main.log.debug( self.name + " : Intent ID - " + |
| 2449 | intents.get( 'id' ) + |
| 2450 | " actual state = " + |
| 2451 | intents.get( 'state' ) + |
| 2452 | " does not equal expected states = " |
| 2453 | + str( expectedState ) ) |
| 2454 | returnValue = main.FALSE |
| 2455 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2456 | if returnValue == main.TRUE: |
| 2457 | main.log.info( self.name + ": All " + |
| 2458 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2459 | " intents are in " + str( expectedState ) + |
| 2460 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2461 | return returnValue |
| 2462 | except TypeError: |
| 2463 | main.log.exception( self.name + ": Object not as expected" ) |
| 2464 | return None |
| 2465 | except pexpect.EOF: |
| 2466 | main.log.error( self.name + ": EOF exception found" ) |
| 2467 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2468 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2469 | except Exception: |
| 2470 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2471 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2472 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2473 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2474 | """ |
| 2475 | Description: |
| 2476 | Compare the allocated bandwidth with the given allocations |
| 2477 | Required: |
| 2478 | expectedAllocations - The expected ONOS output of the allocations command |
| 2479 | Return: |
| 2480 | Returns main.TRUE only if all intent are the same as expected states, |
| 2481 | otherwise returns main.FALSE. |
| 2482 | """ |
| 2483 | # FIXME: Convert these string comparisons to object comparisons |
| 2484 | try: |
| 2485 | returnValue = main.TRUE |
| 2486 | bandwidthFailed = False |
| 2487 | rawAlloc = self.allocations() |
| 2488 | expectedFormat = StringIO( expectedAllocations ) |
| 2489 | ONOSOutput = StringIO( rawAlloc ) |
| 2490 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2491 | str( expectedFormat ) ) ) |
| 2492 | |
| 2493 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2494 | actual = actual.rstrip() |
| 2495 | expected = expected.rstrip() |
| 2496 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2497 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2498 | marker1 = actual.find( 'allocated' ) |
| 2499 | m1 = actual[ :marker1 ] |
| 2500 | marker2 = expected.find( 'allocated' ) |
| 2501 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2502 | if m1 != m2: |
| 2503 | bandwidthFailed = True |
| 2504 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2505 | bandwidthFailed = True |
| 2506 | expectedFormat.close() |
| 2507 | ONOSOutput.close() |
| 2508 | |
| 2509 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2510 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2511 | returnValue = main.FALSE |
| 2512 | return returnValue |
| 2513 | except TypeError: |
| 2514 | main.log.exception( self.name + ": Object not as expected" ) |
| 2515 | return None |
| 2516 | except pexpect.EOF: |
| 2517 | main.log.error( self.name + ": EOF exception found" ) |
| 2518 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2519 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2520 | except Exception: |
| 2521 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2522 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2523 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2524 | def compareIntent( self, intentDict ): |
| 2525 | """ |
| 2526 | Description: |
| 2527 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2528 | Return: |
| 2529 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2530 | Arguments: |
| 2531 | intentDict: a dictionary which maps intent ids to intent states |
| 2532 | """ |
| 2533 | try: |
| 2534 | intentsRaw = self.intents() |
| 2535 | intentsJson = json.loads( intentsRaw ) |
| 2536 | intentDictONOS = {} |
| 2537 | for intent in intentsJson: |
| 2538 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2539 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2540 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2541 | 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] | 2542 | str( len( intentDict ) ) + " expected and " + |
| 2543 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2544 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2545 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2546 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2547 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2548 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2549 | else: |
| 2550 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2551 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2552 | " expected state is " + intentDict[ intentID ] + |
| 2553 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2554 | returnValue = main.FALSE |
| 2555 | intentDictONOS.pop( intentID ) |
| 2556 | if len( intentDictONOS ) > 0: |
| 2557 | returnValue = main.FALSE |
| 2558 | for intentID in intentDictONOS.keys(): |
| 2559 | 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] | 2560 | if returnValue == main.TRUE: |
| 2561 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2562 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2563 | except KeyError: |
| 2564 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2565 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2566 | except ( TypeError, ValueError ): |
| 2567 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2568 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2569 | except pexpect.EOF: |
| 2570 | main.log.error( self.name + ": EOF exception found" ) |
| 2571 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2572 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2573 | except Exception: |
| 2574 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2575 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2576 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2577 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2578 | """ |
| 2579 | Description: |
| 2580 | Check the number of installed intents. |
| 2581 | Optional: |
| 2582 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2583 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2584 | Return: |
| 2585 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2586 | , otherwise, returns main.FALSE. |
| 2587 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2588 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2589 | try: |
| 2590 | cmd = "intents -s -j" |
| 2591 | |
| 2592 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2593 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2594 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2595 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2596 | response = json.loads( response ) |
| 2597 | |
| 2598 | # get total and installed number, see if they are match |
| 2599 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2600 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2601 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2602 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2603 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2604 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2605 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2606 | return main.FALSE |
| 2607 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2608 | except ( TypeError, ValueError ): |
| 2609 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2610 | return None |
| 2611 | except pexpect.EOF: |
| 2612 | main.log.error( self.name + ": EOF exception found" ) |
| 2613 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2614 | if noExit: |
| 2615 | return main.FALSE |
| 2616 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2617 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2618 | except pexpect.TIMEOUT: |
| 2619 | main.log.error( self.name + ": ONOS timeout" ) |
| 2620 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2621 | except Exception: |
| 2622 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2623 | if noExit: |
| 2624 | return main.FALSE |
| 2625 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2626 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2627 | |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2628 | 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] | 2629 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2630 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2631 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2632 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2633 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2634 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2635 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2636 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2637 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2638 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2639 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2640 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2641 | cmdStr += " -n" |
| 2642 | cmdStr += " " + state |
| 2643 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2644 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2645 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2646 | assert "Command not found:" not in handle, handle |
| 2647 | if re.search( "Error:", handle ): |
| 2648 | main.log.error( self.name + ": flows() response: " + |
| 2649 | str( handle ) ) |
| 2650 | return handle |
| 2651 | except AssertionError: |
| 2652 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2653 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2654 | except TypeError: |
| 2655 | main.log.exception( self.name + ": Object not as expected" ) |
| 2656 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2657 | except pexpect.TIMEOUT: |
| 2658 | main.log.error( self.name + ": ONOS timeout" ) |
| 2659 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2660 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2661 | main.log.error( self.name + ": EOF exception found" ) |
| 2662 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2663 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2664 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2665 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2666 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2667 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2668 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2669 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2670 | count = int( count ) if count else 0 |
| 2671 | return count if ( count > min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2672 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2673 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2674 | """ |
| 2675 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2676 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2677 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2678 | if the count of those states is 0, which means all current flows |
| 2679 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2680 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2681 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2682 | Return: |
| 2683 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2684 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2685 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2686 | """ |
| 2687 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2688 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2689 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2690 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2691 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2692 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2693 | if rawFlows: |
| 2694 | # if we didn't get flows or flows function return None, we should return |
| 2695 | # main.Flase |
| 2696 | checkedStates.append( json.loads( rawFlows ) ) |
| 2697 | else: |
| 2698 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2699 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2700 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2701 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2702 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2703 | except TypeError: |
| 2704 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2705 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2706 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2707 | # We want to count PENDING_ADD if isPENDING is true |
| 2708 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2709 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2710 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2711 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2712 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2713 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2714 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2715 | except ( TypeError, ValueError ): |
| 2716 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2717 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2718 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2719 | except AssertionError: |
| 2720 | main.log.exception( "" ) |
| 2721 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2722 | except pexpect.TIMEOUT: |
| 2723 | main.log.error( self.name + ": ONOS timeout" ) |
| 2724 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2725 | except pexpect.EOF: |
| 2726 | main.log.error( self.name + ": EOF exception found" ) |
| 2727 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2728 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2729 | except Exception: |
| 2730 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2731 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2732 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2733 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2734 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2735 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2736 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2737 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2738 | a specific point-to-point intent definition |
| 2739 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2740 | * ingress: specify source dpid |
| 2741 | * egress: specify destination dpid |
| 2742 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2743 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2744 | * offset: the keyOffset is where the next batch of intents |
| 2745 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2746 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2747 | * getResponse: If set to True, function will return ONOS response. |
| 2748 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2749 | Returns: If failed to push test intents, it will returen None, |
| 2750 | if successful, return true. |
| 2751 | Timeout expection will return None, |
| 2752 | TypeError will return false |
| 2753 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2754 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2755 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2756 | if background: |
| 2757 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2758 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2759 | back = "" |
| 2760 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2761 | ingress, |
| 2762 | egress, |
| 2763 | batchSize, |
| 2764 | offset, |
| 2765 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2766 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2767 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2768 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2769 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2770 | if getResponse: |
| 2771 | return response |
| 2772 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2773 | # TODO: We should handle if there is failure in installation |
| 2774 | return main.TRUE |
| 2775 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2776 | except AssertionError: |
| 2777 | main.log.exception( "" ) |
| 2778 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2779 | except pexpect.TIMEOUT: |
| 2780 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2781 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2782 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2783 | main.log.error( self.name + ": EOF exception found" ) |
| 2784 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2785 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2786 | except TypeError: |
| 2787 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2788 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2789 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2790 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2791 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2792 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2793 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2794 | """ |
| 2795 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2796 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2797 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2798 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2799 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2800 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2801 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2802 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2803 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2804 | cmd = "flows -c added" |
| 2805 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2806 | if rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2807 | rawFlows = rawFlows.split( "\n" ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2808 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2809 | for l in rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2810 | totalFlows += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2811 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2812 | main.log.error( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2813 | return None |
| 2814 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2815 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2816 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2817 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2818 | return None |
| 2819 | except pexpect.EOF: |
| 2820 | main.log.error( self.name + ": EOF exception found" ) |
| 2821 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2822 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2823 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2824 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2825 | except pexpect.TIMEOUT: |
| 2826 | main.log.error( self.name + ": ONOS timeout" ) |
| 2827 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2828 | except Exception: |
| 2829 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2830 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2831 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2832 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2833 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2834 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2835 | """ |
| 2836 | Description: |
| 2837 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2838 | Optional: |
| 2839 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2840 | Return: |
| 2841 | The number of intents |
| 2842 | """ |
| 2843 | try: |
| 2844 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2845 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2846 | if response is None: |
| 2847 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2848 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2849 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2850 | except ( TypeError, ValueError ): |
| 2851 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2852 | return None |
| 2853 | except pexpect.EOF: |
| 2854 | main.log.error( self.name + ": EOF exception found" ) |
| 2855 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2856 | if noExit: |
| 2857 | return -1 |
| 2858 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2859 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2860 | except Exception: |
| 2861 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2862 | if noExit: |
| 2863 | return -1 |
| 2864 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2865 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2866 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2867 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2868 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2869 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2870 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2871 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2872 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2873 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2874 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2875 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2876 | cmdStr += " -j" |
| 2877 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2878 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2879 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2880 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2881 | except AssertionError: |
| 2882 | main.log.exception( "" ) |
| 2883 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2884 | except TypeError: |
| 2885 | main.log.exception( self.name + ": Object not as expected" ) |
| 2886 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2887 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2888 | main.log.error( self.name + ": EOF exception found" ) |
| 2889 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2890 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2891 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2892 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2893 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2894 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2895 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2896 | """ |
| 2897 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2898 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2899 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2900 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2901 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2902 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2903 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2904 | cmdStr += " -j" |
| 2905 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2906 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2907 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2908 | if handle: |
| 2909 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2910 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2911 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2912 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2913 | else: |
| 2914 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2915 | except AssertionError: |
| 2916 | main.log.exception( "" ) |
| 2917 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2918 | except TypeError: |
| 2919 | main.log.exception( self.name + ": Object not as expected" ) |
| 2920 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2921 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2922 | main.log.error( self.name + ": EOF exception found" ) |
| 2923 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2924 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2925 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2926 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2927 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2928 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2929 | # Wrapper functions **************** |
| 2930 | # Wrapper functions use existing driver |
| 2931 | # functions and extends their use case. |
| 2932 | # For example, we may use the output of |
| 2933 | # a normal driver function, and parse it |
| 2934 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2935 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2936 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2937 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2938 | Description: |
| 2939 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2940 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2941 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2942 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2943 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 2944 | if intentsStr is None: |
| 2945 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2946 | # Convert to a dictionary |
| 2947 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2948 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2949 | for intent in intents: |
| 2950 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2951 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2952 | except TypeError: |
| 2953 | main.log.exception( self.name + ": Object not as expected" ) |
| 2954 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2955 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2956 | main.log.error( self.name + ": EOF exception found" ) |
| 2957 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2958 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2959 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2960 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2961 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2962 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2963 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2964 | """ |
| 2965 | Determine the number of flow rules for the given device id that are |
| 2966 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2967 | Params: |
| 2968 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2969 | """ |
| 2970 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2971 | if core: |
| 2972 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2973 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2974 | else: |
| 2975 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2976 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 2977 | handle = self.lineCount( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2978 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2979 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2980 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2981 | except AssertionError: |
| 2982 | main.log.exception( "" ) |
| 2983 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2984 | except pexpect.EOF: |
| 2985 | main.log.error( self.name + ": EOF exception found" ) |
| 2986 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2987 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2988 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2989 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2990 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2991 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2992 | def groupAddedCount( self, deviceId, core=False ): |
| 2993 | """ |
| 2994 | Determine the number of group rules for the given device id that are |
| 2995 | in the added state |
| 2996 | Params: |
| 2997 | core: if True, only return the number of core groups added |
| 2998 | """ |
| 2999 | try: |
| 3000 | if core: |
| 3001 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3002 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3003 | else: |
| 3004 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3005 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3006 | handle = self.lineCount( cmdStr ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3007 | assert handle is not None, "Error in sendline" |
| 3008 | assert "Command not found:" not in handle, handle |
| 3009 | return handle |
| 3010 | except AssertionError: |
| 3011 | main.log.exception( "" ) |
| 3012 | return None |
| 3013 | except pexpect.EOF: |
| 3014 | main.log.error( self.name + ": EOF exception found" ) |
| 3015 | main.log.error( self.name + ": " + self.handle.before ) |
| 3016 | main.cleanAndExit() |
| 3017 | except Exception: |
| 3018 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3019 | main.cleanAndExit() |
| 3020 | |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3021 | def addStaticRoute( self, subnet, intf): |
| 3022 | """ |
| 3023 | Adds a static route to onos. |
| 3024 | Params: |
| 3025 | subnet: The subnet reaching through this route |
| 3026 | intf: The interface this route is reachable through |
| 3027 | """ |
| 3028 | try: |
| 3029 | cmdStr = "route-add " + subnet + " " + intf |
| 3030 | handle = self.sendline( cmdStr ) |
| 3031 | assert handle is not None, "Error in sendline" |
| 3032 | assert "Command not found:" not in handle, handle |
| 3033 | return handle |
| 3034 | except AssertionError: |
| 3035 | main.log.exception( "" ) |
| 3036 | return None |
| 3037 | except pexpect.EOF: |
| 3038 | main.log.error( self.name + ": EOF exception found" ) |
| 3039 | main.log.error( self.name + ": " + self.handle.before ) |
| 3040 | main.cleanAndExit() |
| 3041 | except Exception: |
| 3042 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3043 | main.cleanAndExit() |
| 3044 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3045 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 3046 | """ |
| 3047 | Description: |
| 3048 | Check whether the number of groups for the given device id that |
| 3049 | are in ADDED state is bigger than minGroupCount. |
| 3050 | Required: |
| 3051 | * deviceId: device id to check the number of added group rules |
| 3052 | Optional: |
| 3053 | * minGroupCount: the number of groups to compare |
| 3054 | * core: if True, only check the number of core groups added |
| 3055 | * comparison: if 0, compare with greater than minFlowCount |
| 3056 | * if 1, compare with equal to minFlowCount |
| 3057 | Return: |
| 3058 | Returns the number of groups if it is bigger than minGroupCount, |
| 3059 | returns main.FALSE otherwise. |
| 3060 | """ |
| 3061 | count = self.groupAddedCount( deviceId, core ) |
| 3062 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3063 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3064 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 3065 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3066 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3067 | """ |
| 3068 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3069 | deviceId: Id of the device from which we retrieve groups |
| 3070 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3071 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3072 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3073 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 3074 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3075 | assert handle is not None, "Error in sendline" |
| 3076 | assert "Command not found:" not in handle, handle |
| 3077 | return handle |
| 3078 | except AssertionError: |
| 3079 | main.log.exception( "" ) |
| 3080 | return None |
| 3081 | except TypeError: |
| 3082 | main.log.exception( self.name + ": Object not as expected" ) |
| 3083 | return None |
| 3084 | except pexpect.EOF: |
| 3085 | main.log.error( self.name + ": EOF exception found" ) |
| 3086 | main.log.error( self.name + ": " + self.handle.before ) |
| 3087 | main.cleanAndExit() |
| 3088 | except Exception: |
| 3089 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3090 | main.cleanAndExit() |
| 3091 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3092 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3093 | """ |
| 3094 | Description: |
| 3095 | Check whether the number of flow rules for the given device id that |
| 3096 | are in ADDED state is bigger than minFlowCount. |
| 3097 | Required: |
| 3098 | * deviceId: device id to check the number of added flow rules |
| 3099 | Optional: |
| 3100 | * minFlowCount: the number of flow rules to compare |
| 3101 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3102 | * comparison: if 0, compare with greater than minFlowCount |
| 3103 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3104 | Return: |
| 3105 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 3106 | returns main.FALSE otherwise. |
| 3107 | """ |
| 3108 | count = self.flowAddedCount( deviceId, core ) |
| 3109 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3110 | main.log.debug( "found {} flows".format( count ) ) |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3111 | 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] | 3112 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3113 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3114 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3115 | Use 'devices' function to obtain list of all devices |
| 3116 | and parse the result to obtain a list of all device |
| 3117 | id's. Returns this list. Returns empty list if no |
| 3118 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3119 | List is ordered sequentially |
| 3120 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3121 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3122 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3123 | the ids. By obtaining the list of device ids on the fly, |
| 3124 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3125 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3126 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3127 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3128 | devicesStr = self.devices( jsonFormat=False ) |
| 3129 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3130 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3131 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3132 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3133 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3134 | |
| 3135 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3136 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3137 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3138 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3139 | # Split list further into arguments before and after string |
| 3140 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3141 | # append to idList |
| 3142 | for arg in tempList: |
| 3143 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3144 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3145 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3146 | except TypeError: |
| 3147 | main.log.exception( self.name + ": Object not as expected" ) |
| 3148 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3149 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3150 | main.log.error( self.name + ": EOF exception found" ) |
| 3151 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3152 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3153 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3154 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3155 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3156 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3157 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3158 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3159 | Uses 'nodes' function to obtain list of all nodes |
| 3160 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3161 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3162 | Returns: |
| 3163 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3164 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3165 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3166 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3167 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3168 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3169 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3170 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3171 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3172 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3173 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3174 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3175 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3176 | except ( TypeError, ValueError ): |
| 3177 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3178 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3179 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3180 | main.log.error( self.name + ": EOF exception found" ) |
| 3181 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3182 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3183 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3184 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3185 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3186 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3187 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3188 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3189 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3190 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3191 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3192 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3193 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3194 | return None |
| 3195 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3196 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3197 | rawDevices = self.devices() |
| 3198 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3199 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3200 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3201 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3202 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3203 | return device |
| 3204 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3205 | except ( TypeError, ValueError ): |
| 3206 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3207 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3208 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3209 | main.log.error( self.name + ": EOF exception found" ) |
| 3210 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3211 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3212 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3213 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3214 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3215 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3216 | def getTopology( self, topologyOutput ): |
| 3217 | """ |
| 3218 | Definition: |
| 3219 | Loads a json topology output |
| 3220 | Return: |
| 3221 | topology = current ONOS topology |
| 3222 | """ |
| 3223 | import json |
| 3224 | try: |
| 3225 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3226 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3227 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3228 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3229 | except ( TypeError, ValueError ): |
| 3230 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3231 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3232 | except pexpect.EOF: |
| 3233 | main.log.error( self.name + ": EOF exception found" ) |
| 3234 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3235 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3236 | except Exception: |
| 3237 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3238 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3239 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3240 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3241 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3242 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3243 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3244 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3245 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3246 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3247 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3248 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3249 | logLevel = level to log to. |
| 3250 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3251 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3252 | Returns: main.TRUE if the number of switches and links are correct, |
| 3253 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3254 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3255 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3256 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3257 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3258 | summary = self.summary() |
| 3259 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3260 | except ( TypeError, ValueError ): |
| 3261 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3262 | return main.ERROR |
| 3263 | try: |
| 3264 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3265 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3266 | return main.ERROR |
| 3267 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3268 | # Is the number of switches is what we expected |
| 3269 | devices = topology.get( 'devices', False ) |
| 3270 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3271 | nodes = summary.get( 'nodes', False ) |
| 3272 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3273 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3274 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3275 | # Is the number of links is what we expected |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3276 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3277 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 3278 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3279 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3280 | output = output + "The number of links and switches match "\ |
| 3281 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3282 | result = main.TRUE |
| 3283 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3284 | output = output + \ |
| 3285 | "The number of links and switches does not match " + \ |
| 3286 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3287 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3288 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 3289 | output = output + " (%i expected) " % int( numoswitch ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3290 | if int( numolink ) > 0: |
| 3291 | output = output + "and %i links " % int( links ) |
| 3292 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 3293 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3294 | output = output + "and %i controllers " % int( nodes ) |
| 3295 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3296 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3297 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3298 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3299 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3300 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3301 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3302 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3303 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3304 | main.log.error( self.name + ": EOF exception found" ) |
| 3305 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3306 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3307 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3308 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3309 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3310 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3311 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3312 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3313 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3314 | deviceId must be the id of a device as seen in the onos devices command |
| 3315 | 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] | 3316 | role must be either master, standby, or none |
| 3317 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3318 | Returns: |
| 3319 | main.TRUE or main.FALSE based on argument verification and |
| 3320 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3321 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3322 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3323 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3324 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3325 | cmdStr = "device-role " +\ |
| 3326 | str( deviceId ) + " " +\ |
| 3327 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3328 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3329 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3330 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3331 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3332 | if re.search( "Error", handle ): |
| 3333 | # end color output to escape any colours |
| 3334 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3335 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3336 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3337 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3338 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3339 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3340 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3341 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3342 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3343 | except AssertionError: |
| 3344 | main.log.exception( "" ) |
| 3345 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3346 | except TypeError: |
| 3347 | main.log.exception( self.name + ": Object not as expected" ) |
| 3348 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3349 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3350 | main.log.error( self.name + ": EOF exception found" ) |
| 3351 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3352 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3353 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3354 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3355 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3356 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3357 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3358 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3359 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3360 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3361 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3362 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3363 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3364 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3365 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3366 | cmdStr += " -j" |
| 3367 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3368 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3369 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3370 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3371 | except AssertionError: |
| 3372 | main.log.exception( "" ) |
| 3373 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3374 | except TypeError: |
| 3375 | main.log.exception( self.name + ": Object not as expected" ) |
| 3376 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3377 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3378 | main.log.error( self.name + ": EOF exception found" ) |
| 3379 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3380 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3381 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3382 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3383 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3384 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3385 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3386 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3387 | CLI command to get the current leader for the Election test application |
| 3388 | NOTE: Requires installation of the onos-app-election feature |
| 3389 | Returns: Node IP of the leader if one exists |
| 3390 | None if none exists |
| 3391 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3392 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3393 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3394 | cmdStr = "election-test-leader" |
| 3395 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3396 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3397 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3398 | # Leader |
| 3399 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3400 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3401 | nodeSearch = re.search( leaderPattern, response ) |
| 3402 | if nodeSearch: |
| 3403 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3404 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3405 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3406 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3407 | # no leader |
| 3408 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3409 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3410 | nullSearch = re.search( nullPattern, response ) |
| 3411 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3412 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3413 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3414 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3415 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3416 | main.log.error( self.name + ": Error in electionTestLeader on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3417 | ": " + "unexpected response" ) |
| 3418 | main.log.error( repr( response ) ) |
| 3419 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3420 | except AssertionError: |
| 3421 | main.log.exception( "" ) |
| 3422 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3423 | except TypeError: |
| 3424 | main.log.exception( self.name + ": Object not as expected" ) |
| 3425 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3426 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3427 | main.log.error( self.name + ": EOF exception found" ) |
| 3428 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3429 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3430 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3431 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3432 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3433 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3434 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3435 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3436 | CLI command to run for leadership of the Election test application. |
| 3437 | NOTE: Requires installation of the onos-app-election feature |
| 3438 | Returns: Main.TRUE on success |
| 3439 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3440 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3441 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3442 | cmdStr = "election-test-run" |
| 3443 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3444 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3445 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3446 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3447 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3448 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3449 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3450 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3451 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3452 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3453 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3454 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3455 | main.log.error( self.name + ": Error in electionTestRun on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3456 | ": " + "unexpected response" ) |
| 3457 | main.log.error( repr( response ) ) |
| 3458 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3459 | except AssertionError: |
| 3460 | main.log.exception( "" ) |
| 3461 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3462 | except TypeError: |
| 3463 | main.log.exception( self.name + ": Object not as expected" ) |
| 3464 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3465 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3466 | main.log.error( self.name + ": EOF exception found" ) |
| 3467 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3468 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3469 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3470 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3471 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3472 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3473 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3474 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3475 | * CLI command to withdraw the local node from leadership election for |
| 3476 | * the Election test application. |
| 3477 | #NOTE: Requires installation of the onos-app-election feature |
| 3478 | Returns: Main.TRUE on success |
| 3479 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3480 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3481 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3482 | cmdStr = "election-test-withdraw" |
| 3483 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3484 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3485 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3486 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3487 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3488 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3489 | if re.search( successPattern, response ): |
| 3490 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3491 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3492 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3493 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3494 | main.log.error( self.name + ": Error in electionTestWithdraw on " + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3495 | self.name + ": " + "unexpected response" ) |
| 3496 | main.log.error( repr( response ) ) |
| 3497 | return main.FALSE |
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 main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -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() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3511 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3512 | def getDevicePortsEnabledCount( self, dpid ): |
| 3513 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3514 | Get the count of all enabled ports on a particular device/switch |
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: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3517 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3518 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3519 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3520 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3521 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3522 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3523 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3524 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3525 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3526 | except AssertionError: |
| 3527 | main.log.exception( "" ) |
| 3528 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3529 | except TypeError: |
| 3530 | main.log.exception( self.name + ": Object not as expected" ) |
| 3531 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3532 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3533 | main.log.error( self.name + ": EOF exception found" ) |
| 3534 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3535 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3536 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3537 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3538 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3539 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3540 | def getDeviceLinksActiveCount( self, dpid ): |
| 3541 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3542 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3543 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3544 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3545 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3546 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3547 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3548 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3549 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3550 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3551 | main.log.error( self.name + ": Error in getting ports " ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3552 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3553 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3554 | except AssertionError: |
| 3555 | main.log.exception( "" ) |
| 3556 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3557 | except TypeError: |
| 3558 | main.log.exception( self.name + ": Object not as expected" ) |
| 3559 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3560 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3561 | main.log.error( self.name + ": EOF exception found" ) |
| 3562 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3563 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3564 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3565 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3566 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3567 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3568 | def getAllIntentIds( self ): |
| 3569 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3570 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3571 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3572 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3573 | cmdStr = "onos:intents | grep id=" |
| 3574 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3575 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3576 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3577 | if re.search( "Error", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3578 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3579 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3580 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3581 | except AssertionError: |
| 3582 | main.log.exception( "" ) |
| 3583 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3584 | except TypeError: |
| 3585 | main.log.exception( self.name + ": Object not as expected" ) |
| 3586 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3587 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3588 | main.log.error( self.name + ": EOF exception found" ) |
| 3589 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3590 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3591 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3592 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3593 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3594 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3595 | def intentSummary( self ): |
| 3596 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3597 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3598 | """ |
| 3599 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3600 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3601 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3602 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3603 | states.append( intent.get( 'state', None ) ) |
| 3604 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3605 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3606 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3607 | except ( TypeError, ValueError ): |
| 3608 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 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() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3614 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3615 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3616 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3617 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3618 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3619 | """ |
| 3620 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3621 | Optional argument: |
| 3622 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3623 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3624 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3625 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3626 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3627 | cmdStr += " -j" |
| 3628 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3629 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3630 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3631 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3632 | except AssertionError: |
| 3633 | main.log.exception( "" ) |
| 3634 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3635 | except TypeError: |
| 3636 | main.log.exception( self.name + ": Object not as expected" ) |
| 3637 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3638 | except pexpect.EOF: |
| 3639 | main.log.error( self.name + ": EOF exception found" ) |
| 3640 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3641 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3642 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3643 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3644 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3645 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3646 | def leaderCandidates( self, jsonFormat=True ): |
| 3647 | """ |
| 3648 | Returns the output of the leaders -c command. |
| 3649 | Optional argument: |
| 3650 | * jsonFormat - boolean indicating if you want output in json |
| 3651 | """ |
| 3652 | try: |
| 3653 | cmdStr = "onos:leaders -c" |
| 3654 | if jsonFormat: |
| 3655 | cmdStr += " -j" |
| 3656 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3657 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3658 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3659 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3660 | except AssertionError: |
| 3661 | main.log.exception( "" ) |
| 3662 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3663 | except TypeError: |
| 3664 | main.log.exception( self.name + ": Object not as expected" ) |
| 3665 | return None |
| 3666 | except pexpect.EOF: |
| 3667 | main.log.error( self.name + ": EOF exception found" ) |
| 3668 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3669 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3670 | except Exception: |
| 3671 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3672 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3673 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3674 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3675 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3676 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3677 | topic parameter and an empty list if the topic doesn't exist |
| 3678 | If no leader is elected leader in the returned list will be "none" |
| 3679 | Returns None if there is a type error processing the json object |
| 3680 | """ |
| 3681 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3682 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3683 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3684 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3685 | assert "Command not found:" not in rawOutput, rawOutput |
| 3686 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3687 | results = [] |
| 3688 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3689 | if dict[ "topic" ] == topic: |
| 3690 | leader = dict[ "leader" ] |
| 3691 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3692 | results.append( leader ) |
| 3693 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3694 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3695 | except AssertionError: |
| 3696 | main.log.exception( "" ) |
| 3697 | return None |
| 3698 | except ( TypeError, ValueError ): |
| 3699 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3700 | return None |
| 3701 | except pexpect.EOF: |
| 3702 | main.log.error( self.name + ": EOF exception found" ) |
| 3703 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3704 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3705 | except Exception: |
| 3706 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3707 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3708 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3709 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3710 | """ |
| 3711 | Returns the output of the intent Pending map. |
| 3712 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3713 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3714 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3715 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3716 | cmdStr += " -j" |
| 3717 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3718 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3719 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3720 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3721 | except AssertionError: |
| 3722 | main.log.exception( "" ) |
| 3723 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3724 | except TypeError: |
| 3725 | main.log.exception( self.name + ": Object not as expected" ) |
| 3726 | return None |
| 3727 | except pexpect.EOF: |
| 3728 | main.log.error( self.name + ": EOF exception found" ) |
| 3729 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3730 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3731 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3732 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3733 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3734 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3735 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3736 | """ |
| 3737 | Returns the output of the raft partitions command for ONOS. |
| 3738 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3739 | # Sample JSON |
| 3740 | # { |
| 3741 | # "leader": "tcp://10.128.30.11:7238", |
| 3742 | # "members": [ |
| 3743 | # "tcp://10.128.30.11:7238", |
| 3744 | # "tcp://10.128.30.17:7238", |
| 3745 | # "tcp://10.128.30.13:7238", |
| 3746 | # ], |
| 3747 | # "name": "p1", |
| 3748 | # "term": 3 |
| 3749 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3750 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3751 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3752 | if candidates: |
| 3753 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3754 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3755 | cmdStr += " -j" |
| 3756 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3757 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3758 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3759 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3760 | except AssertionError: |
| 3761 | main.log.exception( "" ) |
| 3762 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3763 | except TypeError: |
| 3764 | main.log.exception( self.name + ": Object not as expected" ) |
| 3765 | return None |
| 3766 | except pexpect.EOF: |
| 3767 | main.log.error( self.name + ": EOF exception found" ) |
| 3768 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3769 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3770 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3771 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3772 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3773 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3774 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3775 | """ |
| 3776 | Returns the output of the apps command for ONOS. This command lists |
| 3777 | information about installed ONOS applications |
| 3778 | """ |
| 3779 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3780 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3781 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3782 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3783 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3784 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3785 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3786 | if summary: |
| 3787 | cmdStr += " -s" |
| 3788 | if active: |
| 3789 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3790 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3791 | cmdStr += " -j" |
| 3792 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3793 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3794 | assert "Command not found:" not in output, output |
| 3795 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3796 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3797 | # FIXME: look at specific exceptions/Errors |
| 3798 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3799 | main.log.exception( self.name + ": Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3800 | return None |
| 3801 | except TypeError: |
| 3802 | main.log.exception( self.name + ": Object not as expected" ) |
| 3803 | return None |
| 3804 | except pexpect.EOF: |
| 3805 | main.log.error( self.name + ": EOF exception found" ) |
| 3806 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3807 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3808 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3809 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3810 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3811 | |
You Wang | cdc51fe | 2018-08-12 17:14:56 -0700 | [diff] [blame] | 3812 | def appStatus( self, appName ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3813 | """ |
| 3814 | Uses the onos:apps cli command to return the status of an application. |
| 3815 | Returns: |
| 3816 | "ACTIVE" - If app is installed and activated |
| 3817 | "INSTALLED" - If app is installed and deactivated |
| 3818 | "UNINSTALLED" - If app is not installed |
| 3819 | None - on error |
| 3820 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3821 | try: |
| 3822 | if not isinstance( appName, types.StringType ): |
| 3823 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3824 | " a string" ) |
| 3825 | return None |
| 3826 | output = self.apps( jsonFormat=True ) |
| 3827 | appsJson = json.loads( output ) |
| 3828 | state = None |
| 3829 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3830 | if appName == app.get( 'name' ): |
| 3831 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3832 | break |
| 3833 | if state == "ACTIVE" or state == "INSTALLED": |
| 3834 | return state |
| 3835 | elif state is None: |
You Wang | 0d9f2c0 | 2018-08-10 14:56:32 -0700 | [diff] [blame] | 3836 | main.log.warn( "{} app not found".format( appName ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3837 | return "UNINSTALLED" |
| 3838 | elif state: |
| 3839 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3840 | str( state ) ) |
| 3841 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3842 | except ( TypeError, ValueError ): |
| 3843 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3844 | return None |
| 3845 | except pexpect.EOF: |
| 3846 | main.log.error( self.name + ": EOF exception found" ) |
| 3847 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3848 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3849 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3850 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3851 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3852 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3853 | def app( self, appName, option ): |
| 3854 | """ |
| 3855 | Interacts with the app command for ONOS. This command manages |
| 3856 | application inventory. |
| 3857 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3858 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3859 | # Validate argument types |
| 3860 | valid = True |
| 3861 | if not isinstance( appName, types.StringType ): |
| 3862 | main.log.error( self.name + ".app(): appName must be a " + |
| 3863 | "string" ) |
| 3864 | valid = False |
| 3865 | if not isinstance( option, types.StringType ): |
| 3866 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3867 | valid = False |
| 3868 | if not valid: |
| 3869 | return main.FALSE |
| 3870 | # Validate Option |
| 3871 | option = option.lower() |
| 3872 | # NOTE: Install may become a valid option |
| 3873 | if option == "activate": |
| 3874 | pass |
| 3875 | elif option == "deactivate": |
| 3876 | pass |
| 3877 | elif option == "uninstall": |
| 3878 | pass |
| 3879 | else: |
| 3880 | # Invalid option |
| 3881 | main.log.error( "The ONOS app command argument only takes " + |
| 3882 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3883 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3884 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3885 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3886 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3887 | assert output is not None, "Error in sendline" |
| 3888 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3889 | if "Error executing command" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3890 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3891 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3892 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3893 | elif "No such application" in output: |
| 3894 | main.log.error( "The application '" + appName + |
| 3895 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3896 | return main.FALSE |
| 3897 | elif "Command not found:" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3898 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3899 | str( output ) ) |
| 3900 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3901 | elif "Unsupported command:" in output: |
| 3902 | main.log.error( "Incorrect command given to 'app': " + |
| 3903 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3904 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3905 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3906 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3907 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3908 | except AssertionError: |
| 3909 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3910 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3911 | except TypeError: |
| 3912 | main.log.exception( self.name + ": Object not as expected" ) |
| 3913 | return main.ERROR |
| 3914 | except pexpect.EOF: |
| 3915 | main.log.error( self.name + ": EOF exception found" ) |
| 3916 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3917 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3918 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3919 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3920 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3921 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3922 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3923 | """ |
| 3924 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3925 | appName is the hierarchical app name, not the feature name |
| 3926 | If check is True, method will check the status of the app after the |
| 3927 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3928 | Returns main.TRUE if the command was successfully sent |
| 3929 | main.FALSE if the cli responded with an error or given |
| 3930 | incorrect input |
| 3931 | """ |
| 3932 | try: |
| 3933 | if not isinstance( appName, types.StringType ): |
| 3934 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3935 | " a string" ) |
| 3936 | return main.FALSE |
| 3937 | status = self.appStatus( appName ) |
| 3938 | if status == "INSTALLED": |
| 3939 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3940 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3941 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3942 | status = self.appStatus( appName ) |
| 3943 | if status == "ACTIVE": |
| 3944 | return main.TRUE |
| 3945 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3946 | main.log.debug( "The state of application " + |
| 3947 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3948 | time.sleep( 1 ) |
| 3949 | return main.FALSE |
| 3950 | else: # not 'check' or command didn't succeed |
| 3951 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3952 | elif status == "ACTIVE": |
| 3953 | return main.TRUE |
| 3954 | elif status == "UNINSTALLED": |
| 3955 | main.log.error( self.name + ": Tried to activate the " + |
| 3956 | "application '" + appName + "' which is not " + |
| 3957 | "installed." ) |
| 3958 | else: |
| 3959 | main.log.error( "Unexpected return value from appStatus: " + |
| 3960 | str( status ) ) |
| 3961 | return main.ERROR |
| 3962 | except TypeError: |
| 3963 | main.log.exception( self.name + ": Object not as expected" ) |
| 3964 | return main.ERROR |
| 3965 | except pexpect.EOF: |
| 3966 | main.log.error( self.name + ": EOF exception found" ) |
| 3967 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3968 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3969 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3970 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3971 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3972 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3973 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3974 | """ |
| 3975 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3976 | appName is the hierarchical app name, not the feature name |
| 3977 | If check is True, method will check the status of the app after the |
| 3978 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3979 | Returns main.TRUE if the command was successfully sent |
| 3980 | main.FALSE if the cli responded with an error or given |
| 3981 | incorrect input |
| 3982 | """ |
| 3983 | try: |
| 3984 | if not isinstance( appName, types.StringType ): |
| 3985 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3986 | "be a string" ) |
| 3987 | return main.FALSE |
| 3988 | status = self.appStatus( appName ) |
| 3989 | if status == "INSTALLED": |
| 3990 | return main.TRUE |
| 3991 | elif status == "ACTIVE": |
| 3992 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3993 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3994 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3995 | status = self.appStatus( appName ) |
| 3996 | if status == "INSTALLED": |
| 3997 | return main.TRUE |
| 3998 | else: |
| 3999 | time.sleep( 1 ) |
| 4000 | return main.FALSE |
| 4001 | else: # not check or command didn't succeed |
| 4002 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4003 | elif status == "UNINSTALLED": |
| 4004 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 4005 | "application '" + appName + "' which is not " + |
| 4006 | "installed." ) |
| 4007 | return main.TRUE |
| 4008 | else: |
| 4009 | main.log.error( "Unexpected return value from appStatus: " + |
| 4010 | str( status ) ) |
| 4011 | return main.ERROR |
| 4012 | except TypeError: |
| 4013 | main.log.exception( self.name + ": Object not as expected" ) |
| 4014 | return main.ERROR |
| 4015 | except pexpect.EOF: |
| 4016 | main.log.error( self.name + ": EOF exception found" ) |
| 4017 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4018 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4019 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4020 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4021 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4022 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4023 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4024 | """ |
| 4025 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4026 | appName is the hierarchical app name, not the feature name |
| 4027 | If check is True, method will check the status of the app after the |
| 4028 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4029 | Returns main.TRUE if the command was successfully sent |
| 4030 | main.FALSE if the cli responded with an error or given |
| 4031 | incorrect input |
| 4032 | """ |
| 4033 | # TODO: check with Thomas about the state machine for apps |
| 4034 | try: |
| 4035 | if not isinstance( appName, types.StringType ): |
| 4036 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 4037 | "be a string" ) |
| 4038 | return main.FALSE |
| 4039 | status = self.appStatus( appName ) |
| 4040 | if status == "INSTALLED": |
| 4041 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4042 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4043 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4044 | status = self.appStatus( appName ) |
| 4045 | if status == "UNINSTALLED": |
| 4046 | return main.TRUE |
| 4047 | else: |
| 4048 | time.sleep( 1 ) |
| 4049 | return main.FALSE |
| 4050 | else: # not check or command didn't succeed |
| 4051 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4052 | elif status == "ACTIVE": |
| 4053 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 4054 | "application '" + appName + "' which is " + |
| 4055 | "currently active." ) |
| 4056 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4057 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4058 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4059 | status = self.appStatus( appName ) |
| 4060 | if status == "UNINSTALLED": |
| 4061 | return main.TRUE |
| 4062 | else: |
| 4063 | time.sleep( 1 ) |
| 4064 | return main.FALSE |
| 4065 | else: # not check or command didn't succeed |
| 4066 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4067 | elif status == "UNINSTALLED": |
| 4068 | return main.TRUE |
| 4069 | else: |
| 4070 | main.log.error( "Unexpected return value from appStatus: " + |
| 4071 | str( status ) ) |
| 4072 | return main.ERROR |
| 4073 | except TypeError: |
| 4074 | main.log.exception( self.name + ": Object not as expected" ) |
| 4075 | return main.ERROR |
| 4076 | except pexpect.EOF: |
| 4077 | main.log.error( self.name + ": EOF exception found" ) |
| 4078 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4079 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4080 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4081 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4082 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4083 | |
| 4084 | def appIDs( self, jsonFormat=True ): |
| 4085 | """ |
| 4086 | Show the mappings between app id and app names given by the 'app-ids' |
| 4087 | cli command |
| 4088 | """ |
| 4089 | try: |
| 4090 | cmdStr = "app-ids" |
| 4091 | if jsonFormat: |
| 4092 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4093 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4094 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4095 | assert "Command not found:" not in output, output |
| 4096 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4097 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4098 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4099 | main.log.exception( self.name + ": Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4100 | return None |
| 4101 | except TypeError: |
| 4102 | main.log.exception( self.name + ": Object not as expected" ) |
| 4103 | return None |
| 4104 | except pexpect.EOF: |
| 4105 | main.log.error( self.name + ": EOF exception found" ) |
| 4106 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4107 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4108 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4109 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4110 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4111 | |
| 4112 | def appToIDCheck( self ): |
| 4113 | """ |
| 4114 | This method will check that each application's ID listed in 'apps' is |
| 4115 | the same as the ID listed in 'app-ids'. The check will also check that |
| 4116 | there are no duplicate IDs issued. Note that an app ID should be |
| 4117 | a globaly unique numerical identifier for app/app-like features. Once |
| 4118 | an ID is registered, the ID is never freed up so that if an app is |
| 4119 | reinstalled it will have the same ID. |
| 4120 | |
| 4121 | Returns: main.TRUE if the check passes and |
| 4122 | main.FALSE if the check fails or |
| 4123 | main.ERROR if there is some error in processing the test |
| 4124 | """ |
| 4125 | try: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4126 | # Grab IDs |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4127 | rawJson = self.appIDs( jsonFormat=True ) |
| 4128 | if rawJson: |
| 4129 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4130 | else: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4131 | main.log.error( "app-ids returned nothing: " + repr( rawJson ) ) |
| 4132 | return main.FALSE |
| 4133 | |
| 4134 | # Grab Apps |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4135 | rawJson = self.apps( jsonFormat=True ) |
| 4136 | if rawJson: |
| 4137 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4138 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4139 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4140 | return main.FALSE |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4141 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4142 | result = main.TRUE |
| 4143 | for app in apps: |
| 4144 | appID = app.get( 'id' ) |
| 4145 | if appID is None: |
| 4146 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4147 | result = main.FALSE |
| 4148 | appName = app.get( 'name' ) |
| 4149 | if appName is None: |
| 4150 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4151 | result = main.FALSE |
| 4152 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4153 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4154 | if not current: # if ids doesn't have this id |
| 4155 | result = main.FALSE |
| 4156 | main.log.error( "'app-ids' does not have the ID for " + |
| 4157 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4158 | main.log.debug( "apps command returned: " + str( app ) + |
| 4159 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4160 | elif len( current ) > 1: |
| 4161 | # there is more than one app with this ID |
| 4162 | result = main.FALSE |
| 4163 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4164 | elif not current[ 0 ][ 'name' ] == appName: |
| 4165 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4166 | result = main.FALSE |
| 4167 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4168 | " registered under id:" + str( appID ) + |
| 4169 | " but 'apps' has " + str( appName ) ) |
| 4170 | else: |
| 4171 | pass # id and name match! |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4172 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4173 | # now make sure that app-ids has no duplicates |
| 4174 | idsList = [] |
| 4175 | namesList = [] |
| 4176 | for item in ids: |
| 4177 | idsList.append( item[ 'id' ] ) |
| 4178 | namesList.append( item[ 'name' ] ) |
| 4179 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4180 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4181 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4182 | + json.dumps( ids, |
| 4183 | sort_keys=True, |
| 4184 | indent=4, |
| 4185 | separators=( ',', ': ' ) ) ) |
| 4186 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4187 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4188 | except ( TypeError, ValueError ): |
| 4189 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4190 | return main.ERROR |
| 4191 | except pexpect.EOF: |
| 4192 | main.log.error( self.name + ": EOF exception found" ) |
| 4193 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4194 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4195 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4196 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4197 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4198 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4199 | def getCfg( self, component=None, propName=None, short=False, |
| 4200 | jsonFormat=True ): |
| 4201 | """ |
| 4202 | Get configuration settings from onos cli |
| 4203 | Optional arguments: |
| 4204 | component - Optionally only list configurations for a specific |
| 4205 | component. If None, all components with configurations |
| 4206 | are displayed. Case Sensitive string. |
| 4207 | propName - If component is specified, propName option will show |
| 4208 | only this specific configuration from that component. |
| 4209 | Case Sensitive string. |
| 4210 | jsonFormat - Returns output as json. Note that this will override |
| 4211 | the short option |
| 4212 | short - Short, less verbose, version of configurations. |
| 4213 | This is overridden by the json option |
| 4214 | returns: |
| 4215 | Output from cli as a string or None on error |
| 4216 | """ |
| 4217 | try: |
| 4218 | baseStr = "cfg" |
| 4219 | cmdStr = " get" |
| 4220 | componentStr = "" |
| 4221 | if component: |
| 4222 | componentStr += " " + component |
| 4223 | if propName: |
| 4224 | componentStr += " " + propName |
| 4225 | if jsonFormat: |
| 4226 | baseStr += " -j" |
| 4227 | elif short: |
| 4228 | baseStr += " -s" |
| 4229 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4230 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4231 | assert "Command not found:" not in output, output |
| 4232 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4233 | return output |
| 4234 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4235 | main.log.exception( self.name + ": Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4236 | return None |
| 4237 | except TypeError: |
| 4238 | main.log.exception( self.name + ": Object not as expected" ) |
| 4239 | return None |
| 4240 | except pexpect.EOF: |
| 4241 | main.log.error( self.name + ": EOF exception found" ) |
| 4242 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4243 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4244 | except Exception: |
| 4245 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4246 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4247 | |
| 4248 | def setCfg( self, component, propName, value=None, check=True ): |
| 4249 | """ |
| 4250 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4251 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4252 | component - The case sensitive name of the component whose |
| 4253 | property is to be set |
| 4254 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4255 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4256 | 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] | 4257 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4258 | check - Boolean, Check whether the option was successfully set this |
| 4259 | only applies when a value is given. |
| 4260 | returns: |
| 4261 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4262 | will return main.TRUE unless there is an error |
| 4263 | """ |
| 4264 | try: |
| 4265 | baseStr = "cfg" |
| 4266 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4267 | if value is not None: |
| 4268 | cmdStr += " " + str( value ) |
| 4269 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4270 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4271 | assert "Command not found:" not in output, output |
| 4272 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4273 | if value and check: |
| 4274 | results = self.getCfg( component=str( component ), |
| 4275 | propName=str( propName ), |
| 4276 | jsonFormat=True ) |
| 4277 | # Check if current value is what we just set |
| 4278 | try: |
| 4279 | jsonOutput = json.loads( results ) |
| 4280 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4281 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4282 | main.log.exception( "Error parsing cfg output" ) |
| 4283 | main.log.error( "output:" + repr( results ) ) |
| 4284 | return main.FALSE |
| 4285 | if current == str( value ): |
| 4286 | return main.TRUE |
| 4287 | return main.FALSE |
| 4288 | return main.TRUE |
| 4289 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4290 | main.log.exception( self.name + ": Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4291 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4292 | except ( TypeError, ValueError ): |
| 4293 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4294 | return main.FALSE |
| 4295 | except pexpect.EOF: |
| 4296 | main.log.error( self.name + ": EOF exception found" ) |
| 4297 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4298 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4299 | except Exception: |
| 4300 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4301 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4302 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4303 | def distPrimitivesSend( self, cmd ): |
| 4304 | """ |
| 4305 | Function to handle sending cli commands for the distributed primitives test app |
| 4306 | |
| 4307 | This command will catch some exceptions and retry the command on some |
| 4308 | specific store exceptions. |
| 4309 | |
| 4310 | Required arguments: |
| 4311 | cmd - The command to send to the cli |
| 4312 | returns: |
| 4313 | string containing the cli output |
| 4314 | None on Error |
| 4315 | """ |
| 4316 | try: |
| 4317 | output = self.sendline( cmd ) |
| 4318 | try: |
| 4319 | assert output is not None, "Error in sendline" |
| 4320 | # TODO: Maybe make this less hardcoded |
| 4321 | # ConsistentMap Exceptions |
| 4322 | assert "org.onosproject.store.service" not in output |
| 4323 | # Node not leader |
| 4324 | assert "java.lang.IllegalStateException" not in output |
| 4325 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4326 | main.log.error( self.name + ": Error in processing '" + cmd + "' " + |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4327 | "command: " + str( output ) ) |
| 4328 | retryTime = 30 # Conservative time, given by Madan |
| 4329 | main.log.info( "Waiting " + str( retryTime ) + |
| 4330 | "seconds before retrying." ) |
| 4331 | time.sleep( retryTime ) # Due to change in mastership |
| 4332 | output = self.sendline( cmd ) |
| 4333 | assert output is not None, "Error in sendline" |
| 4334 | assert "Command not found:" not in output, output |
| 4335 | assert "Error executing command" not in output, output |
| 4336 | main.log.info( self.name + ": " + output ) |
| 4337 | return output |
| 4338 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4339 | main.log.exception( self.name + ": Error in processing '" + cmd + "' command." ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4340 | return None |
| 4341 | except TypeError: |
| 4342 | main.log.exception( self.name + ": Object not as expected" ) |
| 4343 | return None |
| 4344 | except pexpect.EOF: |
| 4345 | main.log.error( self.name + ": EOF exception found" ) |
| 4346 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4347 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4348 | except Exception: |
| 4349 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4350 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4351 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4352 | def setTestAdd( self, setName, values ): |
| 4353 | """ |
| 4354 | CLI command to add elements to a distributed set. |
| 4355 | Arguments: |
| 4356 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4357 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4358 | Example usages: |
| 4359 | setTestAdd( "set1", "a b c" ) |
| 4360 | setTestAdd( "set2", "1" ) |
| 4361 | returns: |
| 4362 | main.TRUE on success OR |
| 4363 | main.FALSE if elements were already in the set OR |
| 4364 | main.ERROR on error |
| 4365 | """ |
| 4366 | try: |
| 4367 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4368 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4369 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4370 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4371 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4372 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4373 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4374 | return main.FALSE |
| 4375 | else: |
| 4376 | main.log.error( self.name + ": setTestAdd did not" + |
| 4377 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4378 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4379 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4380 | except TypeError: |
| 4381 | main.log.exception( self.name + ": Object not as expected" ) |
| 4382 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4383 | except Exception: |
| 4384 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4385 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4386 | |
| 4387 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4388 | """ |
| 4389 | CLI command to remove elements from a distributed set. |
| 4390 | Required arguments: |
| 4391 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4392 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4393 | Optional arguments: |
| 4394 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4395 | retain - Retain only the given values. (intersection of the |
| 4396 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4397 | returns: |
| 4398 | main.TRUE on success OR |
| 4399 | main.FALSE if the set was not changed OR |
| 4400 | main.ERROR on error |
| 4401 | """ |
| 4402 | try: |
| 4403 | cmdStr = "set-test-remove " |
| 4404 | if clear: |
| 4405 | cmdStr += "-c " + str( setName ) |
| 4406 | elif retain: |
| 4407 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4408 | else: |
| 4409 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4410 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4411 | if clear: |
| 4412 | pattern = "Set " + str( setName ) + " cleared" |
| 4413 | if re.search( pattern, output ): |
| 4414 | return main.TRUE |
| 4415 | elif retain: |
| 4416 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4417 | "only elements of set \[(.*)\]" |
| 4418 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4419 | "retaining only elements of the set " +\ |
| 4420 | "\[(.*)\]" |
| 4421 | if re.search( positivePattern, output ): |
| 4422 | return main.TRUE |
| 4423 | elif re.search( negativePattern, output ): |
| 4424 | return main.FALSE |
| 4425 | else: |
| 4426 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4427 | str( setName ) |
| 4428 | if ( len( values.split() ) == 1 ): |
| 4429 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4430 | str( setName ) |
| 4431 | else: |
| 4432 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4433 | str( setName ) |
| 4434 | if re.search( positivePattern, output ): |
| 4435 | return main.TRUE |
| 4436 | elif re.search( negativePattern, output ): |
| 4437 | return main.FALSE |
| 4438 | main.log.error( self.name + ": setTestRemove did not" + |
| 4439 | " match expected output" ) |
| 4440 | main.log.debug( self.name + " expected: " + pattern ) |
| 4441 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4442 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4443 | except TypeError: |
| 4444 | main.log.exception( self.name + ": Object not as expected" ) |
| 4445 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4446 | except Exception: |
| 4447 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4448 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4449 | |
| 4450 | def setTestGet( self, setName, values="" ): |
| 4451 | """ |
| 4452 | CLI command to get the elements in a distributed set. |
| 4453 | Required arguments: |
| 4454 | setName - The name of the set to remove from. |
| 4455 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4456 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4457 | returns: |
| 4458 | main.ERROR on error OR |
| 4459 | A list of elements in the set if no optional arguments are |
| 4460 | supplied OR |
| 4461 | A tuple containing the list then: |
| 4462 | main.FALSE if the given values are not in the set OR |
| 4463 | main.TRUE if the given values are in the set OR |
| 4464 | """ |
| 4465 | try: |
| 4466 | values = str( values ).strip() |
| 4467 | setName = str( setName ).strip() |
| 4468 | length = len( values.split() ) |
| 4469 | containsCheck = None |
| 4470 | # Patterns to match |
| 4471 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4472 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4473 | containsTrue = "Set " + setName + " contains the value " + values |
| 4474 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4475 | values |
| 4476 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4477 | setPattern |
| 4478 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4479 | " subset " + setPattern |
| 4480 | |
| 4481 | cmdStr = "set-test-get " |
| 4482 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4483 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4484 | if length == 0: |
| 4485 | match = re.search( pattern, output ) |
| 4486 | else: # if given values |
| 4487 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4488 | patternTrue = pattern + "\r\n" + containsTrue |
| 4489 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4490 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4491 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4492 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4493 | matchTrue = re.search( patternTrue, output ) |
| 4494 | matchFalse = re.search( patternFalse, output ) |
| 4495 | if matchTrue: |
| 4496 | containsCheck = main.TRUE |
| 4497 | match = matchTrue |
| 4498 | elif matchFalse: |
| 4499 | containsCheck = main.FALSE |
| 4500 | match = matchFalse |
| 4501 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4502 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4503 | "expected output" ) |
| 4504 | main.log.debug( self.name + " expected: " + pattern ) |
| 4505 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4506 | match = None |
| 4507 | if match: |
| 4508 | setMatch = match.group( 1 ) |
| 4509 | if setMatch == '': |
| 4510 | setList = [] |
| 4511 | else: |
| 4512 | setList = setMatch.split( ", " ) |
| 4513 | if length > 0: |
| 4514 | return ( setList, containsCheck ) |
| 4515 | else: |
| 4516 | return setList |
| 4517 | else: # no match |
| 4518 | main.log.error( self.name + ": setTestGet did not" + |
| 4519 | " match expected output" ) |
| 4520 | main.log.debug( self.name + " expected: " + pattern ) |
| 4521 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4522 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4523 | except TypeError: |
| 4524 | main.log.exception( self.name + ": Object not as expected" ) |
| 4525 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4526 | except Exception: |
| 4527 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4528 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4529 | |
| 4530 | def setTestSize( self, setName ): |
| 4531 | """ |
| 4532 | CLI command to get the elements in a distributed set. |
| 4533 | Required arguments: |
| 4534 | setName - The name of the set to remove from. |
| 4535 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4536 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4537 | None on error |
| 4538 | """ |
| 4539 | try: |
| 4540 | # TODO: Should this check against the number of elements returned |
| 4541 | # and then return true/false based on that? |
| 4542 | setName = str( setName ).strip() |
| 4543 | # Patterns to match |
| 4544 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4545 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4546 | setPattern |
| 4547 | cmdStr = "set-test-get -s " |
| 4548 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4549 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4550 | if output: |
| 4551 | match = re.search( pattern, output ) |
| 4552 | if match: |
| 4553 | setSize = int( match.group( 1 ) ) |
| 4554 | setMatch = match.group( 2 ) |
| 4555 | if len( setMatch.split() ) == setSize: |
| 4556 | main.log.info( "The size returned by " + self.name + |
| 4557 | " matches the number of elements in " + |
| 4558 | "the returned set" ) |
| 4559 | else: |
| 4560 | main.log.error( "The size returned by " + self.name + |
| 4561 | " does not match the number of " + |
| 4562 | "elements in the returned set." ) |
| 4563 | return setSize |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4564 | else: # no match |
| 4565 | main.log.error( self.name + ": setTestGet did not" + |
| 4566 | " match expected output" ) |
| 4567 | main.log.debug( self.name + " expected: " + pattern ) |
| 4568 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4569 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4570 | except TypeError: |
| 4571 | main.log.exception( self.name + ": Object not as expected" ) |
| 4572 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4573 | except Exception: |
| 4574 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4575 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4576 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4577 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4578 | """ |
| 4579 | Command to list the various counters in the system. |
| 4580 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4581 | if jsonFormat, a string of the json object returned by the cli |
| 4582 | command |
| 4583 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4584 | None on error |
| 4585 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4586 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4587 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4588 | if jsonFormat: |
| 4589 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4590 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4591 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4592 | assert "Command not found:" not in output, output |
| 4593 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4594 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4595 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4596 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4597 | main.log.exception( self.name + ": Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4598 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4599 | except TypeError: |
| 4600 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4601 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4602 | except pexpect.EOF: |
| 4603 | main.log.error( self.name + ": EOF exception found" ) |
| 4604 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4605 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4606 | except Exception: |
| 4607 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4608 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4609 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4610 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4611 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4612 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4613 | Required arguments: |
| 4614 | counter - The name of the counter to increment. |
| 4615 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4616 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4617 | returns: |
| 4618 | integer value of the counter or |
| 4619 | None on Error |
| 4620 | """ |
| 4621 | try: |
| 4622 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4623 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4624 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4625 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4626 | if delta != 1: |
| 4627 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4628 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4629 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4630 | match = re.search( pattern, output ) |
| 4631 | if match: |
| 4632 | return int( match.group( 1 ) ) |
| 4633 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4634 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4635 | " match expected output." ) |
| 4636 | main.log.debug( self.name + " expected: " + pattern ) |
| 4637 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4638 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4639 | except TypeError: |
| 4640 | main.log.exception( self.name + ": Object not as expected" ) |
| 4641 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4642 | except Exception: |
| 4643 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4644 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4645 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4646 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4647 | """ |
| 4648 | CLI command to get a distributed counter then add a delta to it. |
| 4649 | Required arguments: |
| 4650 | counter - The name of the counter to increment. |
| 4651 | Optional arguments: |
| 4652 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4653 | returns: |
| 4654 | integer value of the counter or |
| 4655 | None on Error |
| 4656 | """ |
| 4657 | try: |
| 4658 | counter = str( counter ) |
| 4659 | delta = int( delta ) |
| 4660 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4661 | cmdStr += counter |
| 4662 | if delta != 1: |
| 4663 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4664 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4665 | pattern = counter + " was updated to (-?\d+)" |
| 4666 | match = re.search( pattern, output ) |
| 4667 | if match: |
| 4668 | return int( match.group( 1 ) ) |
| 4669 | else: |
| 4670 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4671 | " match expected output." ) |
| 4672 | main.log.debug( self.name + " expected: " + pattern ) |
| 4673 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4674 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4675 | except TypeError: |
| 4676 | main.log.exception( self.name + ": Object not as expected" ) |
| 4677 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4678 | except Exception: |
| 4679 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4680 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4681 | |
| 4682 | def valueTestGet( self, valueName ): |
| 4683 | """ |
| 4684 | CLI command to get the value of an atomic value. |
| 4685 | Required arguments: |
| 4686 | valueName - The name of the value to get. |
| 4687 | returns: |
| 4688 | string value of the value or |
| 4689 | None on Error |
| 4690 | """ |
| 4691 | try: |
| 4692 | valueName = str( valueName ) |
| 4693 | cmdStr = "value-test " |
| 4694 | operation = "get" |
| 4695 | cmdStr = "value-test {} {}".format( valueName, |
| 4696 | operation ) |
| 4697 | output = self.distPrimitivesSend( cmdStr ) |
| 4698 | pattern = "(\w+)" |
| 4699 | match = re.search( pattern, output ) |
| 4700 | if match: |
| 4701 | return match.group( 1 ) |
| 4702 | else: |
| 4703 | main.log.error( self.name + ": valueTestGet did not" + |
| 4704 | " match expected output." ) |
| 4705 | main.log.debug( self.name + " expected: " + pattern ) |
| 4706 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4707 | return None |
| 4708 | except TypeError: |
| 4709 | main.log.exception( self.name + ": Object not as expected" ) |
| 4710 | return None |
| 4711 | except Exception: |
| 4712 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4713 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4714 | |
| 4715 | def valueTestSet( self, valueName, newValue ): |
| 4716 | """ |
| 4717 | CLI command to set the value of an atomic value. |
| 4718 | Required arguments: |
| 4719 | valueName - The name of the value to set. |
| 4720 | newValue - The value to assign to the given value. |
| 4721 | returns: |
| 4722 | main.TRUE on success or |
| 4723 | main.ERROR on Error |
| 4724 | """ |
| 4725 | try: |
| 4726 | valueName = str( valueName ) |
| 4727 | newValue = str( newValue ) |
| 4728 | operation = "set" |
| 4729 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4730 | operation, |
| 4731 | newValue ) |
| 4732 | output = self.distPrimitivesSend( cmdStr ) |
| 4733 | if output is not None: |
| 4734 | return main.TRUE |
| 4735 | else: |
| 4736 | return main.ERROR |
| 4737 | except TypeError: |
| 4738 | main.log.exception( self.name + ": Object not as expected" ) |
| 4739 | return main.ERROR |
| 4740 | except Exception: |
| 4741 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4742 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4743 | |
| 4744 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4745 | """ |
| 4746 | CLI command to compareAndSet the value of an atomic value. |
| 4747 | Required arguments: |
| 4748 | valueName - The name of the value. |
| 4749 | oldValue - Compare the current value of the atomic value to this |
| 4750 | newValue - If the value equals oldValue, set the value to newValue |
| 4751 | returns: |
| 4752 | main.TRUE on success or |
| 4753 | main.FALSE on failure or |
| 4754 | main.ERROR on Error |
| 4755 | """ |
| 4756 | try: |
| 4757 | valueName = str( valueName ) |
| 4758 | oldValue = str( oldValue ) |
| 4759 | newValue = str( newValue ) |
| 4760 | operation = "compareAndSet" |
| 4761 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4762 | operation, |
| 4763 | oldValue, |
| 4764 | newValue ) |
| 4765 | output = self.distPrimitivesSend( cmdStr ) |
| 4766 | pattern = "(\w+)" |
| 4767 | match = re.search( pattern, output ) |
| 4768 | if match: |
| 4769 | result = match.group( 1 ) |
| 4770 | if result == "true": |
| 4771 | return main.TRUE |
| 4772 | elif result == "false": |
| 4773 | return main.FALSE |
| 4774 | else: |
| 4775 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4776 | " match expected output." ) |
| 4777 | main.log.debug( self.name + " expected: " + pattern ) |
| 4778 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4779 | return main.ERROR |
| 4780 | except TypeError: |
| 4781 | main.log.exception( self.name + ": Object not as expected" ) |
| 4782 | return main.ERROR |
| 4783 | except Exception: |
| 4784 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4785 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4786 | |
| 4787 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4788 | """ |
| 4789 | CLI command to getAndSet the value of an atomic value. |
| 4790 | Required arguments: |
| 4791 | valueName - The name of the value to get. |
| 4792 | newValue - The value to assign to the given value |
| 4793 | returns: |
| 4794 | string value of the value or |
| 4795 | None on Error |
| 4796 | """ |
| 4797 | try: |
| 4798 | valueName = str( valueName ) |
| 4799 | cmdStr = "value-test " |
| 4800 | operation = "getAndSet" |
| 4801 | cmdStr += valueName + " " + operation |
| 4802 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4803 | operation, |
| 4804 | newValue ) |
| 4805 | output = self.distPrimitivesSend( cmdStr ) |
| 4806 | pattern = "(\w+)" |
| 4807 | match = re.search( pattern, output ) |
| 4808 | if match: |
| 4809 | return match.group( 1 ) |
| 4810 | else: |
| 4811 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4812 | " match expected output." ) |
| 4813 | main.log.debug( self.name + " expected: " + pattern ) |
| 4814 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4815 | return None |
| 4816 | except TypeError: |
| 4817 | main.log.exception( self.name + ": Object not as expected" ) |
| 4818 | return None |
| 4819 | except Exception: |
| 4820 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4821 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4822 | |
| 4823 | def valueTestDestroy( self, valueName ): |
| 4824 | """ |
| 4825 | CLI command to destroy an atomic value. |
| 4826 | Required arguments: |
| 4827 | valueName - The name of the value to destroy. |
| 4828 | returns: |
| 4829 | main.TRUE on success or |
| 4830 | main.ERROR on Error |
| 4831 | """ |
| 4832 | try: |
| 4833 | valueName = str( valueName ) |
| 4834 | cmdStr = "value-test " |
| 4835 | operation = "destroy" |
| 4836 | cmdStr += valueName + " " + operation |
| 4837 | output = self.distPrimitivesSend( cmdStr ) |
| 4838 | if output is not None: |
| 4839 | return main.TRUE |
| 4840 | else: |
| 4841 | return main.ERROR |
| 4842 | except TypeError: |
| 4843 | main.log.exception( self.name + ": Object not as expected" ) |
| 4844 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4845 | except Exception: |
| 4846 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4847 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4848 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4849 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4850 | """ |
| 4851 | Description: Execute summary command in onos |
| 4852 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4853 | no output |
| 4854 | |
| 4855 | """ |
| 4856 | try: |
| 4857 | cmdStr = "summary" |
| 4858 | if jsonFormat: |
| 4859 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4860 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4861 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4862 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4863 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 4864 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4865 | if not handle: |
| 4866 | main.log.error( self.name + ": There is no output in " + |
| 4867 | "summary command" ) |
| 4868 | return main.FALSE |
| 4869 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4870 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4871 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4872 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4873 | except TypeError: |
| 4874 | main.log.exception( self.name + ": Object not as expected" ) |
| 4875 | return None |
| 4876 | except pexpect.EOF: |
| 4877 | main.log.error( self.name + ": EOF exception found" ) |
| 4878 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4879 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4880 | except Exception: |
| 4881 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4882 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4883 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4884 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4885 | """ |
| 4886 | CLI command to get the value of a key in a consistent map using |
| 4887 | transactions. This a test function and can only get keys from the |
| 4888 | test map hard coded into the cli command |
| 4889 | Required arguments: |
| 4890 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4891 | returns: |
| 4892 | The string value of the key or |
| 4893 | None on Error |
| 4894 | """ |
| 4895 | try: |
| 4896 | keyName = str( keyName ) |
| 4897 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4898 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4899 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4900 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4901 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4902 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4903 | return None |
| 4904 | else: |
| 4905 | match = re.search( pattern, output ) |
| 4906 | if match: |
| 4907 | return match.groupdict()[ 'value' ] |
| 4908 | else: |
| 4909 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4910 | " match expected output." ) |
| 4911 | main.log.debug( self.name + " expected: " + pattern ) |
| 4912 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4913 | return None |
| 4914 | except TypeError: |
| 4915 | main.log.exception( self.name + ": Object not as expected" ) |
| 4916 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4917 | except Exception: |
| 4918 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4919 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4920 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4921 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4922 | """ |
| 4923 | CLI command to put a value into 'numKeys' number of keys in a |
| 4924 | consistent map using transactions. This a test function and can only |
| 4925 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4926 | Required arguments: |
| 4927 | numKeys - Number of keys to add the value to |
| 4928 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4929 | returns: |
| 4930 | A dictionary whose keys are the name of the keys put into the map |
| 4931 | and the values of the keys are dictionaries whose key-values are |
| 4932 | 'value': value put into map and optionaly |
| 4933 | 'oldValue': Previous value in the key or |
| 4934 | None on Error |
| 4935 | |
| 4936 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4937 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4938 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4939 | """ |
| 4940 | try: |
| 4941 | numKeys = str( numKeys ) |
| 4942 | value = str( value ) |
| 4943 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4944 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4945 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4946 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4947 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4948 | results = {} |
| 4949 | for line in output.splitlines(): |
| 4950 | new = re.search( newPattern, line ) |
| 4951 | updated = re.search( updatedPattern, line ) |
| 4952 | if new: |
| 4953 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4954 | elif updated: |
| 4955 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4956 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4957 | else: |
| 4958 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4959 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4960 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4961 | newPattern, |
| 4962 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4963 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4964 | return results |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4965 | except ( TypeError, AttributeError ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4966 | main.log.exception( self.name + ": Object not as expected" ) |
| 4967 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4968 | except Exception: |
| 4969 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4970 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4971 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4972 | def maps( self, jsonFormat=True ): |
| 4973 | """ |
| 4974 | Description: Returns result of onos:maps |
| 4975 | Optional: |
| 4976 | * jsonFormat: enable json formatting of output |
| 4977 | """ |
| 4978 | try: |
| 4979 | cmdStr = "maps" |
| 4980 | if jsonFormat: |
| 4981 | cmdStr += " -j" |
| 4982 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4983 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4984 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4985 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4986 | except AssertionError: |
| 4987 | main.log.exception( "" ) |
| 4988 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4989 | except TypeError: |
| 4990 | main.log.exception( self.name + ": Object not as expected" ) |
| 4991 | return None |
| 4992 | except pexpect.EOF: |
| 4993 | main.log.error( self.name + ": EOF exception found" ) |
| 4994 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4995 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4996 | except Exception: |
| 4997 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4998 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4999 | |
| 5000 | def getSwController( self, uri, jsonFormat=True ): |
| 5001 | """ |
| 5002 | Descrition: Gets the controller information from the device |
| 5003 | """ |
| 5004 | try: |
| 5005 | cmd = "device-controllers " |
| 5006 | if jsonFormat: |
| 5007 | cmd += "-j " |
| 5008 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5009 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5010 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5011 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5012 | except AssertionError: |
| 5013 | main.log.exception( "" ) |
| 5014 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5015 | except TypeError: |
| 5016 | main.log.exception( self.name + ": Object not as expected" ) |
| 5017 | return None |
| 5018 | except pexpect.EOF: |
| 5019 | main.log.error( self.name + ": EOF exception found" ) |
| 5020 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5021 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5022 | except Exception: |
| 5023 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5024 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5025 | |
| 5026 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 5027 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5028 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5029 | |
| 5030 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5031 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5032 | ip - String or List: The ip address of the controller. |
| 5033 | This parameter can be formed in a couple of different ways. |
| 5034 | VALID: |
| 5035 | 10.0.0.1 - just the ip address |
| 5036 | tcp:10.0.0.1 - the protocol and the ip address |
| 5037 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 5038 | so that you can add controllers with different |
| 5039 | protocols and ports |
| 5040 | INVALID: |
| 5041 | 10.0.0.1:6653 - this is not supported by ONOS |
| 5042 | |
| 5043 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 5044 | port - The port number. |
| 5045 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 5046 | |
| 5047 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 5048 | """ |
| 5049 | try: |
| 5050 | cmd = "device-setcontrollers" |
| 5051 | |
| 5052 | if jsonFormat: |
| 5053 | cmd += " -j" |
| 5054 | cmd += " " + uri |
| 5055 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5056 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5057 | for item in ip: |
| 5058 | if ":" in item: |
| 5059 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5060 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5061 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5062 | elif "." in sitem[ 1 ]: |
| 5063 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5064 | else: |
| 5065 | main.log.error( "Malformed entry: " + item ) |
| 5066 | raise TypeError |
| 5067 | else: |
| 5068 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5069 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5070 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5071 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5072 | if "Error" in response: |
| 5073 | main.log.error( response ) |
| 5074 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5075 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5076 | except AssertionError: |
| 5077 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5078 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5079 | except TypeError: |
| 5080 | main.log.exception( self.name + ": Object not as expected" ) |
| 5081 | return main.FALSE |
| 5082 | except pexpect.EOF: |
| 5083 | main.log.error( self.name + ": EOF exception found" ) |
| 5084 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5085 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5086 | except Exception: |
| 5087 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5088 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5089 | |
| 5090 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5091 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5092 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5093 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5094 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5095 | 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] | 5096 | Returns: |
| 5097 | Returns main.FALSE if an exception is thrown or an error is present |
| 5098 | in the response. Otherwise, returns main.TRUE. |
| 5099 | NOTE: |
| 5100 | 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] | 5101 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5102 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5103 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 5104 | deviceStr = device |
| 5105 | device = [] |
| 5106 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5107 | |
| 5108 | for d in device: |
| 5109 | time.sleep( 1 ) |
| 5110 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5111 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5112 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5113 | if "Error" in response: |
| 5114 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 5115 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5116 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5117 | except AssertionError: |
| 5118 | main.log.exception( "" ) |
| 5119 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5120 | except TypeError: |
| 5121 | main.log.exception( self.name + ": Object not as expected" ) |
| 5122 | return main.FALSE |
| 5123 | except pexpect.EOF: |
| 5124 | main.log.error( self.name + ": EOF exception found" ) |
| 5125 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5126 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5127 | except Exception: |
| 5128 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5129 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5130 | |
| 5131 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5132 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5133 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5134 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5135 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5136 | 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] | 5137 | Returns: |
| 5138 | Returns main.FALSE if an exception is thrown or an error is present |
| 5139 | in the response. Otherwise, returns main.TRUE. |
| 5140 | NOTE: |
| 5141 | 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] | 5142 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5143 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5144 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5145 | host = list( host ) |
| 5146 | |
| 5147 | for h in host: |
| 5148 | time.sleep( 1 ) |
| 5149 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5150 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5151 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5152 | if "Error" in response: |
| 5153 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5154 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5155 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5156 | except AssertionError: |
| 5157 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5158 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5159 | except TypeError: |
| 5160 | main.log.exception( self.name + ": Object not as expected" ) |
| 5161 | return main.FALSE |
| 5162 | except pexpect.EOF: |
| 5163 | main.log.error( self.name + ": EOF exception found" ) |
| 5164 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5165 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5166 | except Exception: |
| 5167 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5168 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5169 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5170 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5171 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5172 | Description: |
| 5173 | Bring link down or up in the null-provider. |
| 5174 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5175 | begin - (string) One end of a device or switch. |
| 5176 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5177 | returns: |
| 5178 | main.TRUE if no exceptions were thrown and no Errors are |
| 5179 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5180 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5181 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5182 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5183 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5184 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5185 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5186 | if "Error" in response or "Failure" in response: |
| 5187 | main.log.error( response ) |
| 5188 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5189 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5190 | except AssertionError: |
| 5191 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5192 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5193 | except TypeError: |
| 5194 | main.log.exception( self.name + ": Object not as expected" ) |
| 5195 | return main.FALSE |
| 5196 | except pexpect.EOF: |
| 5197 | main.log.error( self.name + ": EOF exception found" ) |
| 5198 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5199 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5200 | except Exception: |
| 5201 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5202 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5203 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5204 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5205 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5206 | Description: |
| 5207 | Changes the state of port in an OF switch by means of the |
| 5208 | PORTSTATUS OF messages. |
| 5209 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5210 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5211 | port - (string) target port in the device. Ex: '2' |
| 5212 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5213 | returns: |
| 5214 | main.TRUE if no exceptions were thrown and no Errors are |
| 5215 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5216 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5217 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5218 | state = state.lower() |
| 5219 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5220 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5221 | response = self.sendline( cmd, showResponse=True ) |
| 5222 | assert response is not None, "Error in sendline" |
| 5223 | assert "Command not found:" not in response, response |
| 5224 | if "Error" in response or "Failure" in response: |
| 5225 | main.log.error( response ) |
| 5226 | return main.FALSE |
| 5227 | return main.TRUE |
| 5228 | except AssertionError: |
| 5229 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5230 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5231 | except TypeError: |
| 5232 | main.log.exception( self.name + ": Object not as expected" ) |
| 5233 | return main.FALSE |
| 5234 | except pexpect.EOF: |
| 5235 | main.log.error( self.name + ": EOF exception found" ) |
| 5236 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5237 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5238 | except Exception: |
| 5239 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5240 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5241 | |
| 5242 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5243 | """ |
| 5244 | Set the logging level to lvl for a specific app |
| 5245 | returns main.TRUE on success |
| 5246 | returns main.FALSE if Error occurred |
| 5247 | if noExit is True, TestON will not exit, but clean up |
| 5248 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5249 | Level defaults to INFO |
| 5250 | """ |
| 5251 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5252 | self.handle.sendline( "log:set %s %s" % ( level, app ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 5253 | self.handle.expect( self.karafPrompt ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5254 | |
| 5255 | response = self.handle.before |
| 5256 | if re.search( "Error", response ): |
| 5257 | return main.FALSE |
| 5258 | return main.TRUE |
| 5259 | except pexpect.TIMEOUT: |
| 5260 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5261 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5262 | except pexpect.EOF: |
| 5263 | main.log.error( self.name + ": EOF exception found" ) |
| 5264 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5265 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5266 | except Exception: |
| 5267 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5268 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5269 | |
| 5270 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5271 | """ |
| 5272 | Return a dictionary which describes the latest network topology data as a |
| 5273 | graph. |
| 5274 | An example of the dictionary: |
| 5275 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5276 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5277 | Each vertex should at least have an 'edges' attribute which describes the |
| 5278 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5279 | 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] | 5280 | list of attributes. |
| 5281 | An example of the edges dictionary: |
| 5282 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5283 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5284 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5285 | in topology data. |
| 5286 | """ |
| 5287 | graphDict = {} |
| 5288 | try: |
| 5289 | links = self.links() |
| 5290 | links = json.loads( links ) |
| 5291 | devices = self.devices() |
| 5292 | devices = json.loads( devices ) |
| 5293 | idToDevice = {} |
| 5294 | for device in devices: |
| 5295 | idToDevice[ device[ 'id' ] ] = device |
| 5296 | if includeHost: |
| 5297 | hosts = self.hosts() |
| 5298 | # FIXME: support 'includeHost' argument |
| 5299 | for link in links: |
| 5300 | nodeA = link[ 'src' ][ 'device' ] |
| 5301 | nodeB = link[ 'dst' ][ 'device' ] |
| 5302 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5303 | if nodeA not in graphDict.keys(): |
| 5304 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5305 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5306 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5307 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5308 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5309 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5310 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5311 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5312 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5313 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5314 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5315 | else: |
| 5316 | # Assert nodeB is not connected to any current links of nodeA |
| 5317 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5318 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5319 | 'type': link[ 'type' ], |
| 5320 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5321 | return graphDict |
| 5322 | except ( TypeError, ValueError ): |
| 5323 | main.log.exception( self.name + ": Object not as expected" ) |
| 5324 | return None |
| 5325 | except KeyError: |
| 5326 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5327 | return None |
| 5328 | except AssertionError: |
| 5329 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5330 | return None |
| 5331 | except pexpect.EOF: |
| 5332 | main.log.error( self.name + ": EOF exception found" ) |
| 5333 | main.log.error( self.name + ": " + self.handle.before ) |
| 5334 | return None |
| 5335 | except Exception: |
| 5336 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5337 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5338 | |
| 5339 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5340 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5341 | Send command to check intent-perf summary |
| 5342 | Returns: dictionary for intent-perf summary |
| 5343 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5344 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5345 | cmd = "intent-perf -s" |
| 5346 | respDic = {} |
| 5347 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5348 | assert resp is not None, "Error in sendline" |
| 5349 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5350 | try: |
| 5351 | # Generate the dictionary to return |
| 5352 | for l in resp.split( "\n" ): |
| 5353 | # Delete any white space in line |
| 5354 | temp = re.sub( r'\s+', '', l ) |
| 5355 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5356 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5357 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5358 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5359 | main.log.exception( self.name + ": Object not as expected" ) |
| 5360 | return None |
| 5361 | except KeyError: |
| 5362 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5363 | return None |
| 5364 | except AssertionError: |
| 5365 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5366 | return None |
| 5367 | except pexpect.EOF: |
| 5368 | main.log.error( self.name + ": EOF exception found" ) |
| 5369 | main.log.error( self.name + ": " + self.handle.before ) |
| 5370 | return None |
| 5371 | except Exception: |
| 5372 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5373 | return None |
| 5374 | return respDic |
| 5375 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5376 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5377 | """ |
| 5378 | Searches the latest ONOS log file for the given search term and |
| 5379 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5380 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5381 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5382 | searchTerm: |
| 5383 | The string to grep from the ONOS log. |
| 5384 | startLine: |
| 5385 | The term that decides which line is the start to search the searchTerm in |
| 5386 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5387 | logNum: |
| 5388 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5389 | information.Because of this, search mutiply logs is necessary to capture |
| 5390 | the right result. logNum is the number of karaf logs that we need to search |
| 5391 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5392 | mode: |
| 5393 | all: return all the strings that contain the search term |
| 5394 | last: return the last string that contains the search term |
| 5395 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5396 | num: return the number of times that the searchTerm appears in the log |
| 5397 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5398 | """ |
| 5399 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5400 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5401 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5402 | logPath = '/opt/onos/log/karaf.log.' |
| 5403 | logPaths = '/opt/onos/log/karaf.log' |
| 5404 | for i in range( 1, logNum ): |
| 5405 | logPaths = logPath + str( i ) + " " + logPaths |
| 5406 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5407 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5408 | # 100000000 is just a extreme large number to make sure this function can |
| 5409 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5410 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5411 | if mode == 'all': |
| 5412 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5413 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5414 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5415 | elif mode == 'first': |
| 5416 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5417 | elif mode == 'num': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5418 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
You Wang | 118ba58 | 2017-01-02 17:14:43 -0800 | [diff] [blame] | 5419 | num = self.sendline( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5420 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5421 | elif mode == 'total': |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 5422 | totalLines = self.lineCount( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5423 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5424 | else: |
| 5425 | main.log.error( self.name + " unsupported mode" ) |
| 5426 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5427 | before = self.sendline( cmd ) |
| 5428 | before = before.splitlines() |
| 5429 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5430 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5431 | return returnLines |
| 5432 | except AssertionError: |
| 5433 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5434 | return None |
| 5435 | except pexpect.EOF: |
| 5436 | main.log.error( self.name + ": EOF exception found" ) |
| 5437 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5438 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5439 | except pexpect.TIMEOUT: |
| 5440 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5441 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5442 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5443 | except Exception: |
| 5444 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5445 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5446 | |
| 5447 | def vplsShow( self, jsonFormat=True ): |
| 5448 | """ |
| 5449 | Description: Returns result of onos:vpls show, which should list the |
| 5450 | configured VPLS networks and the assigned interfaces. |
| 5451 | Optional: |
| 5452 | * jsonFormat: enable json formatting of output |
| 5453 | Returns: |
| 5454 | The output of the command or None on error. |
| 5455 | """ |
| 5456 | try: |
| 5457 | cmdStr = "vpls show" |
| 5458 | if jsonFormat: |
| 5459 | raise NotImplementedError |
| 5460 | cmdStr += " -j" |
| 5461 | handle = self.sendline( cmdStr ) |
| 5462 | assert handle is not None, "Error in sendline" |
| 5463 | assert "Command not found:" not in handle, handle |
| 5464 | return handle |
| 5465 | except AssertionError: |
| 5466 | main.log.exception( "" ) |
| 5467 | return None |
| 5468 | except TypeError: |
| 5469 | main.log.exception( self.name + ": Object not as expected" ) |
| 5470 | return None |
| 5471 | except pexpect.EOF: |
| 5472 | main.log.error( self.name + ": EOF exception found" ) |
| 5473 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5474 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5475 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5476 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5477 | return None |
| 5478 | except Exception: |
| 5479 | main.log.exception( self.name + ": Uncaught exception!" ) |
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 | |
| 5482 | def parseVplsShow( self ): |
| 5483 | """ |
| 5484 | Parse the cli output of 'vpls show' into json output. This is required |
| 5485 | as there is currently no json output available. |
| 5486 | """ |
| 5487 | try: |
| 5488 | output = [] |
| 5489 | raw = self.vplsShow( jsonFormat=False ) |
| 5490 | namePat = "VPLS name: (?P<name>\w+)" |
| 5491 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5492 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5493 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5494 | mIter = re.finditer( pattern, raw ) |
| 5495 | for match in mIter: |
| 5496 | item = {} |
| 5497 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5498 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5499 | if ifaces == [ "" ]: |
| 5500 | ifaces = [] |
| 5501 | item[ 'interfaces' ] = ifaces |
| 5502 | encap = match.group( 'encap' ) |
| 5503 | if encap != 'NONE': |
| 5504 | item[ 'encapsulation' ] = encap.lower() |
| 5505 | output.append( item ) |
| 5506 | return output |
| 5507 | except Exception: |
| 5508 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5509 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5510 | |
| 5511 | def vplsList( self, jsonFormat=True ): |
| 5512 | """ |
| 5513 | Description: Returns result of onos:vpls list, which should list the |
| 5514 | configured VPLS networks. |
| 5515 | Optional: |
| 5516 | * jsonFormat: enable json formatting of output |
| 5517 | """ |
| 5518 | try: |
| 5519 | cmdStr = "vpls list" |
| 5520 | if jsonFormat: |
| 5521 | raise NotImplementedError |
| 5522 | cmdStr += " -j" |
| 5523 | handle = self.sendline( cmdStr ) |
| 5524 | assert handle is not None, "Error in sendline" |
| 5525 | assert "Command not found:" not in handle, handle |
| 5526 | return handle |
| 5527 | except AssertionError: |
| 5528 | main.log.exception( "" ) |
| 5529 | return None |
| 5530 | except TypeError: |
| 5531 | main.log.exception( self.name + ": Object not as expected" ) |
| 5532 | return None |
| 5533 | except pexpect.EOF: |
| 5534 | main.log.error( self.name + ": EOF exception found" ) |
| 5535 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5536 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5537 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5538 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5539 | return None |
| 5540 | except Exception: |
| 5541 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5542 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5543 | |
| 5544 | def vplsCreate( self, network ): |
| 5545 | """ |
| 5546 | CLI command to create a new VPLS network. |
| 5547 | Required arguments: |
| 5548 | network - String name of the network to create. |
| 5549 | returns: |
| 5550 | main.TRUE on success and main.FALSE on failure |
| 5551 | """ |
| 5552 | try: |
| 5553 | network = str( network ) |
| 5554 | cmdStr = "vpls create " |
| 5555 | cmdStr += network |
| 5556 | output = self.sendline( cmdStr ) |
| 5557 | assert output is not None, "Error in sendline" |
| 5558 | assert "Command not found:" not in output, output |
| 5559 | assert "Error executing command" not in output, output |
| 5560 | assert "VPLS already exists:" not in output, output |
| 5561 | return main.TRUE |
| 5562 | except AssertionError: |
| 5563 | main.log.exception( "" ) |
| 5564 | return main.FALSE |
| 5565 | except TypeError: |
| 5566 | main.log.exception( self.name + ": Object not as expected" ) |
| 5567 | return main.FALSE |
| 5568 | except pexpect.EOF: |
| 5569 | main.log.error( self.name + ": EOF exception found" ) |
| 5570 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5571 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5572 | except Exception: |
| 5573 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5574 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5575 | |
| 5576 | def vplsDelete( self, network ): |
| 5577 | """ |
| 5578 | CLI command to delete a VPLS network. |
| 5579 | Required arguments: |
| 5580 | network - Name of the network to delete. |
| 5581 | returns: |
| 5582 | main.TRUE on success and main.FALSE on failure |
| 5583 | """ |
| 5584 | try: |
| 5585 | network = str( network ) |
| 5586 | cmdStr = "vpls delete " |
| 5587 | cmdStr += network |
| 5588 | output = self.sendline( cmdStr ) |
| 5589 | assert output is not None, "Error in sendline" |
| 5590 | assert "Command not found:" not in output, output |
| 5591 | assert "Error executing command" not in output, output |
| 5592 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5593 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5594 | return main.TRUE |
| 5595 | except AssertionError: |
| 5596 | main.log.exception( "" ) |
| 5597 | return main.FALSE |
| 5598 | except TypeError: |
| 5599 | main.log.exception( self.name + ": Object not as expected" ) |
| 5600 | return main.FALSE |
| 5601 | except pexpect.EOF: |
| 5602 | main.log.error( self.name + ": EOF exception found" ) |
| 5603 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5604 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5605 | except Exception: |
| 5606 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5607 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5608 | |
| 5609 | def vplsAddIface( self, network, iface ): |
| 5610 | """ |
| 5611 | CLI command to add an interface to a VPLS network. |
| 5612 | Required arguments: |
| 5613 | network - Name of the network to add the interface to. |
| 5614 | iface - The ONOS name for an interface. |
| 5615 | returns: |
| 5616 | main.TRUE on success and main.FALSE on failure |
| 5617 | """ |
| 5618 | try: |
| 5619 | network = str( network ) |
| 5620 | iface = str( iface ) |
| 5621 | cmdStr = "vpls add-if " |
| 5622 | cmdStr += network + " " + iface |
| 5623 | output = self.sendline( cmdStr ) |
| 5624 | assert output is not None, "Error in sendline" |
| 5625 | assert "Command not found:" not in output, output |
| 5626 | assert "Error executing command" not in output, output |
| 5627 | assert "already associated to network" not in output, output |
| 5628 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5629 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5630 | return main.TRUE |
| 5631 | except AssertionError: |
| 5632 | main.log.exception( "" ) |
| 5633 | return main.FALSE |
| 5634 | except TypeError: |
| 5635 | main.log.exception( self.name + ": Object not as expected" ) |
| 5636 | return main.FALSE |
| 5637 | except pexpect.EOF: |
| 5638 | main.log.error( self.name + ": EOF exception found" ) |
| 5639 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5640 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5641 | except Exception: |
| 5642 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5643 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5644 | |
| 5645 | def vplsRemIface( self, network, iface ): |
| 5646 | """ |
| 5647 | CLI command to remove an interface from a VPLS network. |
| 5648 | Required arguments: |
| 5649 | network - Name of the network to remove the interface from. |
| 5650 | iface - Name of the interface to remove. |
| 5651 | returns: |
| 5652 | main.TRUE on success and main.FALSE on failure |
| 5653 | """ |
| 5654 | try: |
| 5655 | iface = str( iface ) |
| 5656 | cmdStr = "vpls rem-if " |
| 5657 | cmdStr += network + " " + iface |
| 5658 | output = self.sendline( cmdStr ) |
| 5659 | assert output is not None, "Error in sendline" |
| 5660 | assert "Command not found:" not in output, output |
| 5661 | assert "Error executing command" not in output, output |
| 5662 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5663 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5664 | return main.TRUE |
| 5665 | except AssertionError: |
| 5666 | main.log.exception( "" ) |
| 5667 | return main.FALSE |
| 5668 | except TypeError: |
| 5669 | main.log.exception( self.name + ": Object not as expected" ) |
| 5670 | return main.FALSE |
| 5671 | except pexpect.EOF: |
| 5672 | main.log.error( self.name + ": EOF exception found" ) |
| 5673 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5674 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5675 | except Exception: |
| 5676 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5677 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5678 | |
| 5679 | def vplsClean( self ): |
| 5680 | """ |
| 5681 | Description: Clears the VPLS app configuration. |
| 5682 | Returns: main.TRUE on success and main.FALSE on failure |
| 5683 | """ |
| 5684 | try: |
| 5685 | cmdStr = "vpls clean" |
| 5686 | handle = self.sendline( cmdStr ) |
| 5687 | assert handle is not None, "Error in sendline" |
| 5688 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5689 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5690 | return handle |
| 5691 | except AssertionError: |
| 5692 | main.log.exception( "" ) |
| 5693 | return main.FALSE |
| 5694 | except TypeError: |
| 5695 | main.log.exception( self.name + ": Object not as expected" ) |
| 5696 | return main.FALSE |
| 5697 | except pexpect.EOF: |
| 5698 | main.log.error( self.name + ": EOF exception found" ) |
| 5699 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5700 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5701 | except Exception: |
| 5702 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5703 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5704 | |
| 5705 | def vplsSetEncap( self, network, encapType ): |
| 5706 | """ |
| 5707 | CLI command to add an interface to a VPLS network. |
| 5708 | Required arguments: |
| 5709 | network - Name of the network to create. |
| 5710 | encapType - Type of encapsulation. |
| 5711 | returns: |
| 5712 | main.TRUE on success and main.FALSE on failure |
| 5713 | """ |
| 5714 | try: |
| 5715 | network = str( network ) |
| 5716 | encapType = str( encapType ).upper() |
| 5717 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5718 | cmdStr = "vpls set-encap " |
| 5719 | cmdStr += network + " " + encapType |
| 5720 | output = self.sendline( cmdStr ) |
| 5721 | assert output is not None, "Error in sendline" |
| 5722 | assert "Command not found:" not in output, output |
| 5723 | assert "Error executing command" not in output, output |
| 5724 | assert "already associated to network" not in output, output |
| 5725 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5726 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5727 | return main.TRUE |
| 5728 | except AssertionError: |
| 5729 | main.log.exception( "" ) |
| 5730 | return main.FALSE |
| 5731 | except TypeError: |
| 5732 | main.log.exception( self.name + ": Object not as expected" ) |
| 5733 | return main.FALSE |
| 5734 | except pexpect.EOF: |
| 5735 | main.log.error( self.name + ": EOF exception found" ) |
| 5736 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5737 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5738 | except Exception: |
| 5739 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5740 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5741 | |
| 5742 | def interfaces( self, jsonFormat=True ): |
| 5743 | """ |
| 5744 | Description: Returns result of interfaces command. |
| 5745 | Optional: |
| 5746 | * jsonFormat: enable json formatting of output |
| 5747 | Returns: |
| 5748 | The output of the command or None on error. |
| 5749 | """ |
| 5750 | try: |
| 5751 | cmdStr = "interfaces" |
| 5752 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5753 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5754 | cmdStr += " -j" |
| 5755 | handle = self.sendline( cmdStr ) |
| 5756 | assert handle is not None, "Error in sendline" |
| 5757 | assert "Command not found:" not in handle, handle |
| 5758 | return handle |
| 5759 | except AssertionError: |
| 5760 | main.log.exception( "" ) |
| 5761 | return None |
| 5762 | except TypeError: |
| 5763 | main.log.exception( self.name + ": Object not as expected" ) |
| 5764 | return None |
| 5765 | except pexpect.EOF: |
| 5766 | main.log.error( self.name + ": EOF exception found" ) |
| 5767 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5768 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5769 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5770 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5771 | return None |
| 5772 | except Exception: |
| 5773 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5774 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5775 | |
| 5776 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5777 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5778 | Get the timestamp of searchTerm from karaf log. |
| 5779 | |
| 5780 | Arguments: |
| 5781 | splitTerm_before and splitTerm_after: |
| 5782 | |
| 5783 | The terms that split the string that contains the timeStamp of |
| 5784 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5785 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5786 | and the splitTerm_after is "x" |
| 5787 | |
| 5788 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5789 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5790 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5791 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5792 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5793 | return main.ERROR |
| 5794 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5795 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5796 | main.log.warn( "Captured timestamp string is empty" ) |
| 5797 | return main.ERROR |
| 5798 | lines = lines[ 0 ] |
| 5799 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5800 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5801 | # get the target value |
| 5802 | line = lines.split( splitTerm_before ) |
| 5803 | key = line[ 1 ].split( splitTerm_after ) |
| 5804 | return int( key[ 0 ] ) |
| 5805 | except IndexError: |
| 5806 | main.log.warn( "Index Error!" ) |
| 5807 | return main.ERROR |
| 5808 | except AssertionError: |
| 5809 | main.log.warn( "Search Term Not Found " ) |
| 5810 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5811 | |
| 5812 | def workQueueAdd( self, queueName, value ): |
| 5813 | """ |
| 5814 | CLI command to add a string to the specified Work Queue. |
| 5815 | This function uses the distributed primitives test app, which |
| 5816 | gives some cli access to distributed primitives for testing |
| 5817 | purposes only. |
| 5818 | |
| 5819 | Required arguments: |
| 5820 | queueName - The name of the queue to add to |
| 5821 | value - The value to add to the queue |
| 5822 | returns: |
| 5823 | main.TRUE on success, main.FALSE on failure and |
| 5824 | main.ERROR on error. |
| 5825 | """ |
| 5826 | try: |
| 5827 | queueName = str( queueName ) |
| 5828 | value = str( value ) |
| 5829 | prefix = "work-queue-test" |
| 5830 | operation = "add" |
| 5831 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5832 | output = self.distPrimitivesSend( cmdStr ) |
| 5833 | if "Invalid operation name" in output: |
| 5834 | main.log.warn( output ) |
| 5835 | return main.ERROR |
| 5836 | elif "Done" in output: |
| 5837 | return main.TRUE |
| 5838 | except TypeError: |
| 5839 | main.log.exception( self.name + ": Object not as expected" ) |
| 5840 | return main.ERROR |
| 5841 | except Exception: |
| 5842 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5843 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5844 | |
| 5845 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5846 | """ |
| 5847 | CLI command to add two strings to the specified Work Queue. |
| 5848 | This function uses the distributed primitives test app, which |
| 5849 | gives some cli access to distributed primitives for testing |
| 5850 | purposes only. |
| 5851 | |
| 5852 | Required arguments: |
| 5853 | queueName - The name of the queue to add to |
| 5854 | value1 - The first value to add to the queue |
| 5855 | value2 - The second value to add to the queue |
| 5856 | returns: |
| 5857 | main.TRUE on success, main.FALSE on failure and |
| 5858 | main.ERROR on error. |
| 5859 | """ |
| 5860 | try: |
| 5861 | queueName = str( queueName ) |
| 5862 | value1 = str( value1 ) |
| 5863 | value2 = str( value2 ) |
| 5864 | prefix = "work-queue-test" |
| 5865 | operation = "addMultiple" |
| 5866 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 5867 | output = self.distPrimitivesSend( cmdStr ) |
| 5868 | if "Invalid operation name" in output: |
| 5869 | main.log.warn( output ) |
| 5870 | return main.ERROR |
| 5871 | elif "Done" in output: |
| 5872 | return main.TRUE |
| 5873 | except TypeError: |
| 5874 | main.log.exception( self.name + ": Object not as expected" ) |
| 5875 | return main.ERROR |
| 5876 | except Exception: |
| 5877 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5878 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5879 | |
| 5880 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 5881 | """ |
| 5882 | CLI command to take a value from the specified Work Queue and compelte it. |
| 5883 | This function uses the distributed primitives test app, which |
| 5884 | gives some cli access to distributed primitives for testing |
| 5885 | purposes only. |
| 5886 | |
| 5887 | Required arguments: |
| 5888 | queueName - The name of the queue to add to |
| 5889 | number - The number of items to take and complete |
| 5890 | returns: |
| 5891 | main.TRUE on success, main.FALSE on failure and |
| 5892 | main.ERROR on error. |
| 5893 | """ |
| 5894 | try: |
| 5895 | queueName = str( queueName ) |
| 5896 | number = str( int( number ) ) |
| 5897 | prefix = "work-queue-test" |
| 5898 | operation = "takeAndComplete" |
| 5899 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 5900 | output = self.distPrimitivesSend( cmdStr ) |
| 5901 | if "Invalid operation name" in output: |
| 5902 | main.log.warn( output ) |
| 5903 | return main.ERROR |
| 5904 | elif "Done" in output: |
| 5905 | return main.TRUE |
| 5906 | except TypeError: |
| 5907 | main.log.exception( self.name + ": Object not as expected" ) |
| 5908 | return main.ERROR |
| 5909 | except Exception: |
| 5910 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5911 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5912 | |
| 5913 | def workQueueDestroy( self, queueName ): |
| 5914 | """ |
| 5915 | CLI command to destroy the specified Work Queue. |
| 5916 | This function uses the distributed primitives test app, which |
| 5917 | gives some cli access to distributed primitives for testing |
| 5918 | purposes only. |
| 5919 | |
| 5920 | Required arguments: |
| 5921 | queueName - The name of the queue to add to |
| 5922 | returns: |
| 5923 | main.TRUE on success, main.FALSE on failure and |
| 5924 | main.ERROR on error. |
| 5925 | """ |
| 5926 | try: |
| 5927 | queueName = str( queueName ) |
| 5928 | prefix = "work-queue-test" |
| 5929 | operation = "destroy" |
| 5930 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5931 | output = self.distPrimitivesSend( cmdStr ) |
| 5932 | if "Invalid operation name" in output: |
| 5933 | main.log.warn( output ) |
| 5934 | return main.ERROR |
| 5935 | return main.TRUE |
| 5936 | except TypeError: |
| 5937 | main.log.exception( self.name + ": Object not as expected" ) |
| 5938 | return main.ERROR |
| 5939 | except Exception: |
| 5940 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5941 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5942 | |
| 5943 | def workQueueTotalPending( self, queueName ): |
| 5944 | """ |
| 5945 | CLI command to get the Total Pending items of the specified Work Queue. |
| 5946 | This function uses the distributed primitives test app, which |
| 5947 | gives some cli access to distributed primitives for testing |
| 5948 | purposes only. |
| 5949 | |
| 5950 | Required arguments: |
| 5951 | queueName - The name of the queue to add to |
| 5952 | returns: |
| 5953 | The number of Pending items in the specified work queue or |
| 5954 | None on error |
| 5955 | """ |
| 5956 | try: |
| 5957 | queueName = str( queueName ) |
| 5958 | prefix = "work-queue-test" |
| 5959 | operation = "totalPending" |
| 5960 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5961 | output = self.distPrimitivesSend( cmdStr ) |
| 5962 | pattern = r'\d+' |
| 5963 | if "Invalid operation name" in output: |
| 5964 | main.log.warn( output ) |
| 5965 | return None |
| 5966 | else: |
| 5967 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5968 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5969 | except ( AttributeError, TypeError ): |
| 5970 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5971 | return None |
| 5972 | except Exception: |
| 5973 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5974 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5975 | |
| 5976 | def workQueueTotalCompleted( self, queueName ): |
| 5977 | """ |
| 5978 | CLI command to get the Total Completed items of the specified Work Queue. |
| 5979 | This function uses the distributed primitives test app, which |
| 5980 | gives some cli access to distributed primitives for testing |
| 5981 | purposes only. |
| 5982 | |
| 5983 | Required arguments: |
| 5984 | queueName - The name of the queue to add to |
| 5985 | returns: |
| 5986 | The number of complete items in the specified work queue or |
| 5987 | None on error |
| 5988 | """ |
| 5989 | try: |
| 5990 | queueName = str( queueName ) |
| 5991 | prefix = "work-queue-test" |
| 5992 | operation = "totalCompleted" |
| 5993 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5994 | output = self.distPrimitivesSend( cmdStr ) |
| 5995 | pattern = r'\d+' |
| 5996 | if "Invalid operation name" in output: |
| 5997 | main.log.warn( output ) |
| 5998 | return None |
| 5999 | else: |
| 6000 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6001 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6002 | except ( AttributeError, TypeError ): |
| 6003 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6004 | return None |
| 6005 | except Exception: |
| 6006 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6007 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6008 | |
| 6009 | def workQueueTotalInProgress( self, queueName ): |
| 6010 | """ |
| 6011 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 6012 | This function uses the distributed primitives test app, which |
| 6013 | gives some cli access to distributed primitives for testing |
| 6014 | purposes only. |
| 6015 | |
| 6016 | Required arguments: |
| 6017 | queueName - The name of the queue to add to |
| 6018 | returns: |
| 6019 | The number of In Progress items in the specified work queue or |
| 6020 | None on error |
| 6021 | """ |
| 6022 | try: |
| 6023 | queueName = str( queueName ) |
| 6024 | prefix = "work-queue-test" |
| 6025 | operation = "totalInProgress" |
| 6026 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6027 | output = self.distPrimitivesSend( cmdStr ) |
| 6028 | pattern = r'\d+' |
| 6029 | if "Invalid operation name" in output: |
| 6030 | main.log.warn( output ) |
| 6031 | return None |
| 6032 | else: |
| 6033 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6034 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6035 | except ( AttributeError, TypeError ): |
| 6036 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6037 | return None |
| 6038 | except Exception: |
| 6039 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6040 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 6041 | |
| 6042 | def events( self, args='-a' ): |
| 6043 | """ |
| 6044 | Description: Returns events -a command output |
| 6045 | Optional: |
| 6046 | add other arguments |
| 6047 | """ |
| 6048 | try: |
| 6049 | cmdStr = "events" |
| 6050 | if args: |
| 6051 | cmdStr += " " + args |
| 6052 | handle = self.sendline( cmdStr ) |
| 6053 | assert handle is not None, "Error in sendline" |
| 6054 | assert "Command not found:" not in handle, handle |
| 6055 | return handle |
| 6056 | except AssertionError: |
| 6057 | main.log.exception( "" ) |
| 6058 | return None |
| 6059 | except TypeError: |
| 6060 | main.log.exception( self.name + ": Object not as expected" ) |
| 6061 | return None |
| 6062 | except pexpect.EOF: |
| 6063 | main.log.error( self.name + ": EOF exception found" ) |
| 6064 | main.log.error( self.name + ": " + self.handle.before ) |
| 6065 | main.cleanAndExit() |
| 6066 | except Exception: |
| 6067 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6068 | main.cleanAndExit() |
| 6069 | |
| 6070 | def getMaster( self, deviceID ): |
| 6071 | """ |
| 6072 | Description: Obtains current master using "roles" command for a specific deviceID |
| 6073 | """ |
| 6074 | try: |
| 6075 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 6076 | except AssertionError: |
| 6077 | main.log.exception( "" ) |
| 6078 | return None |
| 6079 | except TypeError: |
| 6080 | main.log.exception( self.name + ": Object not as expected" ) |
| 6081 | return None |
| 6082 | except pexpect.EOF: |
| 6083 | main.log.error( self.name + ": EOF exception found" ) |
| 6084 | main.log.error( self.name + ": " + self.handle.before ) |
| 6085 | main.cleanAndExit() |
| 6086 | except Exception: |
| 6087 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 6088 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 6089 | |
| 6090 | def issu( self ): |
| 6091 | """ |
| 6092 | Short summary of In-Service Software Upgrade status |
| 6093 | |
| 6094 | Returns the output of the cli command or None on Error |
| 6095 | """ |
| 6096 | try: |
| 6097 | cmdStr = "issu" |
| 6098 | handle = self.sendline( cmdStr ) |
| 6099 | assert handle is not None, "Error in sendline" |
| 6100 | assert "Command not found:" not in handle, handle |
| 6101 | assert "Unsupported command:" not in handle, handle |
| 6102 | return handle |
| 6103 | except AssertionError: |
| 6104 | main.log.exception( "" ) |
| 6105 | return None |
| 6106 | except TypeError: |
| 6107 | main.log.exception( self.name + ": Object not as expected" ) |
| 6108 | return None |
| 6109 | except pexpect.EOF: |
| 6110 | main.log.error( self.name + ": EOF exception found" ) |
| 6111 | main.log.error( self.name + ": " + self.handle.before ) |
| 6112 | main.cleanAndExit() |
| 6113 | except Exception: |
| 6114 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6115 | main.cleanAndExit() |
| 6116 | |
| 6117 | def issuInit( self ): |
| 6118 | """ |
| 6119 | Initiates an In-Service Software Upgrade |
| 6120 | |
| 6121 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6122 | """ |
| 6123 | try: |
| 6124 | cmdStr = "issu init" |
| 6125 | handle = self.sendline( cmdStr ) |
| 6126 | assert handle is not None, "Error in sendline" |
| 6127 | assert "Command not found:" not in handle, handle |
| 6128 | assert "Unsupported command:" not in handle, handle |
| 6129 | if "Initialized" in handle: |
| 6130 | return main.TRUE |
| 6131 | else: |
| 6132 | return main.FALSE |
| 6133 | except AssertionError: |
| 6134 | main.log.exception( "" ) |
| 6135 | return main.ERROR |
| 6136 | except TypeError: |
| 6137 | main.log.exception( self.name + ": Object not as expected" ) |
| 6138 | return main.ERROR |
| 6139 | except pexpect.EOF: |
| 6140 | main.log.error( self.name + ": EOF exception found" ) |
| 6141 | main.log.error( self.name + ": " + self.handle.before ) |
| 6142 | main.cleanAndExit() |
| 6143 | except Exception: |
| 6144 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6145 | main.cleanAndExit() |
| 6146 | |
| 6147 | def issuUpgrade( self ): |
| 6148 | """ |
| 6149 | Transitions stores to upgraded nodes |
| 6150 | |
| 6151 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6152 | """ |
| 6153 | try: |
| 6154 | cmdStr = "issu upgrade" |
| 6155 | handle = self.sendline( cmdStr ) |
| 6156 | assert handle is not None, "Error in sendline" |
| 6157 | assert "Command not found:" not in handle, handle |
| 6158 | assert "Unsupported command:" not in handle, handle |
| 6159 | if "Upgraded" in handle: |
| 6160 | return main.TRUE |
| 6161 | else: |
| 6162 | return main.FALSE |
| 6163 | except AssertionError: |
| 6164 | main.log.exception( "" ) |
| 6165 | return main.ERROR |
| 6166 | except TypeError: |
| 6167 | main.log.exception( self.name + ": Object not as expected" ) |
| 6168 | return main.ERROR |
| 6169 | except pexpect.EOF: |
| 6170 | main.log.error( self.name + ": EOF exception found" ) |
| 6171 | main.log.error( self.name + ": " + self.handle.before ) |
| 6172 | main.cleanAndExit() |
| 6173 | except Exception: |
| 6174 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6175 | main.cleanAndExit() |
| 6176 | |
| 6177 | def issuCommit( self ): |
| 6178 | """ |
| 6179 | Finalizes an In-Service Software Upgrade |
| 6180 | |
| 6181 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6182 | """ |
| 6183 | try: |
| 6184 | cmdStr = "issu commit" |
| 6185 | handle = self.sendline( cmdStr ) |
| 6186 | assert handle is not None, "Error in sendline" |
| 6187 | assert "Command not found:" not in handle, handle |
| 6188 | assert "Unsupported command:" not in handle, handle |
| 6189 | # TODO: Check the version returned by this command |
| 6190 | if "Committed version" in handle: |
| 6191 | return main.TRUE |
| 6192 | else: |
| 6193 | return main.FALSE |
| 6194 | except AssertionError: |
| 6195 | main.log.exception( "" ) |
| 6196 | return main.ERROR |
| 6197 | except TypeError: |
| 6198 | main.log.exception( self.name + ": Object not as expected" ) |
| 6199 | return main.ERROR |
| 6200 | except pexpect.EOF: |
| 6201 | main.log.error( self.name + ": EOF exception found" ) |
| 6202 | main.log.error( self.name + ": " + self.handle.before ) |
| 6203 | main.cleanAndExit() |
| 6204 | except Exception: |
| 6205 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6206 | main.cleanAndExit() |
| 6207 | |
| 6208 | def issuRollback( self ): |
| 6209 | """ |
| 6210 | Rolls back an In-Service Software Upgrade |
| 6211 | |
| 6212 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6213 | """ |
| 6214 | try: |
| 6215 | cmdStr = "issu rollback" |
| 6216 | handle = self.sendline( cmdStr ) |
| 6217 | assert handle is not None, "Error in sendline" |
| 6218 | assert "Command not found:" not in handle, handle |
| 6219 | assert "Unsupported command:" not in handle, handle |
| 6220 | # TODO: Check the version returned by this command |
| 6221 | if "Rolled back to version" in handle: |
| 6222 | return main.TRUE |
| 6223 | else: |
| 6224 | return main.FALSE |
| 6225 | except AssertionError: |
| 6226 | main.log.exception( "" ) |
| 6227 | return main.ERROR |
| 6228 | except TypeError: |
| 6229 | main.log.exception( self.name + ": Object not as expected" ) |
| 6230 | return main.ERROR |
| 6231 | except pexpect.EOF: |
| 6232 | main.log.error( self.name + ": EOF exception found" ) |
| 6233 | main.log.error( self.name + ": " + self.handle.before ) |
| 6234 | main.cleanAndExit() |
| 6235 | except Exception: |
| 6236 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6237 | main.cleanAndExit() |
| 6238 | |
| 6239 | def issuReset( self ): |
| 6240 | """ |
| 6241 | Resets the In-Service Software Upgrade status after a rollback |
| 6242 | |
| 6243 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6244 | """ |
| 6245 | try: |
| 6246 | cmdStr = "issu reset" |
| 6247 | handle = self.sendline( cmdStr ) |
| 6248 | assert handle is not None, "Error in sendline" |
| 6249 | assert "Command not found:" not in handle, handle |
| 6250 | assert "Unsupported command:" not in handle, handle |
| 6251 | # TODO: Check the version returned by this command |
| 6252 | if "Reset version" in handle: |
| 6253 | return main.TRUE |
| 6254 | else: |
| 6255 | return main.FALSE |
| 6256 | except AssertionError: |
| 6257 | main.log.exception( "" ) |
| 6258 | return main.ERROR |
| 6259 | except TypeError: |
| 6260 | main.log.exception( self.name + ": Object not as expected" ) |
| 6261 | return main.ERROR |
| 6262 | except pexpect.EOF: |
| 6263 | main.log.error( self.name + ": EOF exception found" ) |
| 6264 | main.log.error( self.name + ": " + self.handle.before ) |
| 6265 | main.cleanAndExit() |
| 6266 | except Exception: |
| 6267 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6268 | main.cleanAndExit() |
| 6269 | |
| 6270 | def issuStatus( self ): |
| 6271 | """ |
| 6272 | Status of an In-Service Software Upgrade |
| 6273 | |
| 6274 | Returns the output of the cli command or None on Error |
| 6275 | """ |
| 6276 | try: |
| 6277 | cmdStr = "issu status" |
| 6278 | handle = self.sendline( cmdStr ) |
| 6279 | assert handle is not None, "Error in sendline" |
| 6280 | assert "Command not found:" not in handle, handle |
| 6281 | assert "Unsupported command:" not in handle, handle |
| 6282 | return handle |
| 6283 | except AssertionError: |
| 6284 | main.log.exception( "" ) |
| 6285 | return None |
| 6286 | except TypeError: |
| 6287 | main.log.exception( self.name + ": Object not as expected" ) |
| 6288 | return None |
| 6289 | except pexpect.EOF: |
| 6290 | main.log.error( self.name + ": EOF exception found" ) |
| 6291 | main.log.error( self.name + ": " + self.handle.before ) |
| 6292 | main.cleanAndExit() |
| 6293 | except Exception: |
| 6294 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6295 | main.cleanAndExit() |
| 6296 | |
| 6297 | def issuVersion( self ): |
| 6298 | """ |
| 6299 | Get the version of an In-Service Software Upgrade |
| 6300 | |
| 6301 | Returns the output of the cli command or None on Error |
| 6302 | """ |
| 6303 | try: |
| 6304 | cmdStr = "issu version" |
| 6305 | handle = self.sendline( cmdStr ) |
| 6306 | assert handle is not None, "Error in sendline" |
| 6307 | assert "Command not found:" not in handle, handle |
| 6308 | assert "Unsupported command:" not in handle, handle |
| 6309 | return handle |
| 6310 | except AssertionError: |
| 6311 | main.log.exception( "" ) |
| 6312 | return None |
| 6313 | except TypeError: |
| 6314 | main.log.exception( self.name + ": Object not as expected" ) |
| 6315 | return None |
| 6316 | except pexpect.EOF: |
| 6317 | main.log.error( self.name + ": EOF exception found" ) |
| 6318 | main.log.error( self.name + ": " + self.handle.before ) |
| 6319 | main.cleanAndExit() |
| 6320 | except Exception: |
| 6321 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6322 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6323 | |
| 6324 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6325 | """ |
| 6326 | Create a multicast route by calling 'mcast-join' command |
| 6327 | sIP: source IP of the multicast route |
| 6328 | groupIP: group IP of the multicast route |
| 6329 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6330 | dPorts: a list of destination ports of the multicast route |
| 6331 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6332 | """ |
| 6333 | try: |
| 6334 | cmdStr = "mcast-join" |
| 6335 | cmdStr += " " + str( sIP ) |
| 6336 | cmdStr += " " + str( groupIP ) |
| 6337 | cmdStr += " " + str( sPort ) |
| 6338 | assert isinstance( dPorts, list ) |
| 6339 | for dPort in dPorts: |
| 6340 | cmdStr += " " + str( dPort ) |
| 6341 | handle = self.sendline( cmdStr ) |
| 6342 | assert handle is not None, "Error in sendline" |
| 6343 | assert "Command not found:" not in handle, handle |
| 6344 | assert "Unsupported command:" not in handle, handle |
| 6345 | assert "Error executing command" not in handle, handle |
| 6346 | if "Added the mcast route" in handle: |
| 6347 | return main.TRUE |
| 6348 | else: |
| 6349 | return main.FALSE |
| 6350 | except AssertionError: |
| 6351 | main.log.exception( "" ) |
| 6352 | return None |
| 6353 | except TypeError: |
| 6354 | main.log.exception( self.name + ": Object not as expected" ) |
| 6355 | return None |
| 6356 | except pexpect.EOF: |
| 6357 | main.log.error( self.name + ": EOF exception found" ) |
| 6358 | main.log.error( self.name + ": " + self.handle.before ) |
| 6359 | main.cleanAndExit() |
| 6360 | except Exception: |
| 6361 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6362 | main.cleanAndExit() |
| 6363 | |
| 6364 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6365 | """ |
| 6366 | Delete a multicast route by calling 'mcast-delete' command |
| 6367 | sIP: source IP of the multicast route |
| 6368 | groupIP: group IP of the multicast route |
| 6369 | dPorts: a list of destination ports of the multicast route |
| 6370 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6371 | """ |
| 6372 | try: |
| 6373 | cmdStr = "mcast-delete" |
| 6374 | cmdStr += " " + str( sIP ) |
| 6375 | cmdStr += " " + str( groupIP ) |
| 6376 | assert isinstance( dPorts, list ) |
| 6377 | for dPort in dPorts: |
| 6378 | cmdStr += " " + str( dPort ) |
| 6379 | handle = self.sendline( cmdStr ) |
| 6380 | assert handle is not None, "Error in sendline" |
| 6381 | assert "Command not found:" not in handle, handle |
| 6382 | assert "Unsupported command:" not in handle, handle |
| 6383 | assert "Error executing command" not in handle, handle |
| 6384 | if "Updated the mcast route" in handle: |
| 6385 | return main.TRUE |
| 6386 | else: |
| 6387 | return main.FALSE |
| 6388 | except AssertionError: |
| 6389 | main.log.exception( "" ) |
| 6390 | return None |
| 6391 | except TypeError: |
| 6392 | main.log.exception( self.name + ": Object not as expected" ) |
| 6393 | return None |
| 6394 | except pexpect.EOF: |
| 6395 | main.log.error( self.name + ": EOF exception found" ) |
| 6396 | main.log.error( self.name + ": " + self.handle.before ) |
| 6397 | main.cleanAndExit() |
| 6398 | except Exception: |
| 6399 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6400 | main.cleanAndExit() |
| 6401 | |
| 6402 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6403 | """ |
| 6404 | Create a multicast route by calling 'mcast-host-join' command |
| 6405 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6406 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6407 | 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] | 6408 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6409 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6410 | """ |
| 6411 | try: |
| 6412 | cmdStr = "mcast-host-join" |
| 6413 | cmdStr += " -sAddr " + str( sAddr ) |
| 6414 | cmdStr += " -gAddr " + str( gAddr ) |
| 6415 | assert isinstance( srcs, list ) |
| 6416 | for src in srcs: |
| 6417 | cmdStr += " -srcs " + str( src ) |
| 6418 | assert isinstance( sinks, list ) |
| 6419 | for sink in sinks: |
| 6420 | cmdStr += " -sinks " + str( sink ) |
| 6421 | handle = self.sendline( cmdStr ) |
| 6422 | assert handle is not None, "Error in sendline" |
| 6423 | assert "Command not found:" not in handle, handle |
| 6424 | assert "Unsupported command:" not in handle, handle |
| 6425 | assert "Error executing command" not in handle, handle |
| 6426 | if "Added the mcast route" in handle: |
| 6427 | return main.TRUE |
| 6428 | else: |
| 6429 | return main.FALSE |
| 6430 | except AssertionError: |
| 6431 | main.log.exception( "" ) |
| 6432 | return None |
| 6433 | except TypeError: |
| 6434 | main.log.exception( self.name + ": Object not as expected" ) |
| 6435 | return None |
| 6436 | except pexpect.EOF: |
| 6437 | main.log.error( self.name + ": EOF exception found" ) |
| 6438 | main.log.error( self.name + ": " + self.handle.before ) |
| 6439 | main.cleanAndExit() |
| 6440 | except Exception: |
| 6441 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6442 | main.cleanAndExit() |
| 6443 | |
| 6444 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6445 | """ |
| 6446 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6447 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6448 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6449 | 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] | 6450 | will delete the route if not specified |
| 6451 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6452 | """ |
| 6453 | try: |
| 6454 | cmdStr = "mcast-host-delete" |
| 6455 | cmdStr += " -sAddr " + str( sAddr ) |
| 6456 | cmdStr += " -gAddr " + str( gAddr ) |
| 6457 | if host: |
| 6458 | cmdStr += " -h " + str( host ) |
| 6459 | handle = self.sendline( cmdStr ) |
| 6460 | assert handle is not None, "Error in sendline" |
| 6461 | assert "Command not found:" not in handle, handle |
| 6462 | assert "Unsupported command:" not in handle, handle |
| 6463 | assert "Error executing command" not in handle, handle |
| 6464 | if "Updated the mcast route" in handle: |
| 6465 | return main.TRUE |
| 6466 | elif "Deleted the mcast route" in handle: |
| 6467 | return main.TRUE |
| 6468 | else: |
| 6469 | return main.FALSE |
| 6470 | except AssertionError: |
| 6471 | main.log.exception( "" ) |
| 6472 | return None |
| 6473 | except TypeError: |
| 6474 | main.log.exception( self.name + ": Object not as expected" ) |
| 6475 | return None |
| 6476 | except pexpect.EOF: |
| 6477 | main.log.error( self.name + ": EOF exception found" ) |
| 6478 | main.log.error( self.name + ": " + self.handle.before ) |
| 6479 | main.cleanAndExit() |
| 6480 | except Exception: |
| 6481 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6482 | main.cleanAndExit() |
| 6483 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6484 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6485 | """ |
| 6486 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6487 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6488 | gAddr: specifies multicast group address |
| 6489 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6490 | will delete the route if not specified |
| 6491 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6492 | """ |
| 6493 | try: |
| 6494 | cmdStr = "mcast-sink-delete" |
| 6495 | cmdStr += " -sAddr " + str( sAddr ) |
| 6496 | cmdStr += " -gAddr " + str( gAddr ) |
| 6497 | if sink: |
| 6498 | cmdStr += " -s " + str( sink ) |
| 6499 | handle = self.sendline( cmdStr ) |
| 6500 | assert handle is not None, "Error in sendline" |
| 6501 | assert "Command not found:" not in handle, handle |
| 6502 | assert "Unsupported command:" not in handle, handle |
| 6503 | assert "Error executing command" not in handle, handle |
| 6504 | if "Updated the mcast route" in handle: |
| 6505 | return main.TRUE |
| 6506 | elif "Deleted the mcast route" in handle: |
| 6507 | return main.TRUE |
| 6508 | else: |
| 6509 | return main.FALSE |
| 6510 | except AssertionError: |
| 6511 | main.log.exception( "" ) |
| 6512 | return None |
| 6513 | except TypeError: |
| 6514 | main.log.exception( self.name + ": Object not as expected" ) |
| 6515 | return None |
| 6516 | except pexpect.EOF: |
| 6517 | main.log.error( self.name + ": EOF exception found" ) |
| 6518 | main.log.error( self.name + ": " + self.handle.before ) |
| 6519 | main.cleanAndExit() |
| 6520 | except Exception: |
| 6521 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6522 | main.cleanAndExit() |
| 6523 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6524 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6525 | """ |
| 6526 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6527 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6528 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6529 | 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] | 6530 | will delete the route if not specified |
| 6531 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6532 | """ |
| 6533 | try: |
| 6534 | cmdStr = "mcast-source-delete" |
| 6535 | cmdStr += " -sAddr " + str( sAddr ) |
| 6536 | cmdStr += " -gAddr " + str( gAddr ) |
| 6537 | if srcs: |
| 6538 | assert isinstance( srcs, list ) |
| 6539 | for src in srcs: |
| 6540 | cmdStr += " -src " + str( src ) |
| 6541 | handle = self.sendline( cmdStr ) |
| 6542 | assert handle is not None, "Error in sendline" |
| 6543 | assert "Command not found:" not in handle, handle |
| 6544 | assert "Unsupported command:" not in handle, handle |
| 6545 | assert "Error executing command" not in handle, handle |
| 6546 | if "Updated the mcast route" in handle: |
| 6547 | return main.TRUE |
| 6548 | elif "Deleted the mcast route" in handle: |
| 6549 | return main.TRUE |
| 6550 | else: |
| 6551 | return main.FALSE |
| 6552 | except AssertionError: |
| 6553 | main.log.exception( "" ) |
| 6554 | return None |
| 6555 | except TypeError: |
| 6556 | main.log.exception( self.name + ": Object not as expected" ) |
| 6557 | return None |
| 6558 | except pexpect.EOF: |
| 6559 | main.log.error( self.name + ": EOF exception found" ) |
| 6560 | main.log.error( self.name + ": " + self.handle.before ) |
| 6561 | main.cleanAndExit() |
| 6562 | except Exception: |
| 6563 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6564 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6565 | |
| 6566 | def netcfg( self, jsonFormat=True, args="" ): |
| 6567 | """ |
| 6568 | Run netcfg cli command with given args |
| 6569 | """ |
| 6570 | try: |
| 6571 | cmdStr = "netcfg" |
| 6572 | if jsonFormat: |
| 6573 | cmdStr = cmdStr + " -j" |
| 6574 | if args: |
| 6575 | cmdStr = cmdStr + " " + str( args ) |
| 6576 | handle = self.sendline( cmdStr ) |
| 6577 | assert handle is not None, "Error in sendline" |
| 6578 | assert "Command not found:" not in handle, handle |
| 6579 | assert "Unsupported command:" not in handle, handle |
| 6580 | assert "Error executing command" not in handle, handle |
| 6581 | return handle |
| 6582 | except AssertionError: |
| 6583 | main.log.exception( "" ) |
| 6584 | return None |
| 6585 | except TypeError: |
| 6586 | main.log.exception( self.name + ": Object not as expected" ) |
| 6587 | return None |
| 6588 | except pexpect.EOF: |
| 6589 | main.log.error( self.name + ": EOF exception found" ) |
| 6590 | main.log.error( self.name + ": " + self.handle.before ) |
| 6591 | main.cleanAndExit() |
| 6592 | except Exception: |
| 6593 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6594 | main.cleanAndExit() |
| 6595 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6596 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6597 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6598 | 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] | 6599 | Options: |
| 6600 | sAddr: IP address of the source host |
| 6601 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6602 | ipv6: True if hosts are IPv6 |
| 6603 | verbose: return verbose t3 output if True |
| 6604 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6605 | """ |
| 6606 | try: |
| 6607 | # Collect information of both hosts from onos |
| 6608 | hosts = self.hosts() |
| 6609 | hosts = json.loads( hosts ) |
| 6610 | sHost = None |
| 6611 | dHost = None |
| 6612 | for host in hosts: |
| 6613 | if sAddr in host[ "ipAddresses" ]: |
| 6614 | sHost = host |
| 6615 | elif dAddr in host[ "ipAddresses" ]: |
| 6616 | dHost = host |
| 6617 | if sHost and dHost: |
| 6618 | break |
| 6619 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6620 | cmdList = [] |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6621 | if simple: |
| 6622 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6623 | cmdStr = "t3-troubleshoot-simple" |
| 6624 | if verbose: |
| 6625 | cmdStr += " -vv" |
| 6626 | if ipv6: |
| 6627 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6628 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6629 | cmdList.append( cmdStr ) |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6630 | else: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6631 | for location in sHost[ "locations" ]: |
| 6632 | cmdStr = "t3-troubleshoot" |
| 6633 | if verbose: |
| 6634 | cmdStr += " -vv" |
| 6635 | if ipv6: |
| 6636 | cmdStr += " -et ipv6" |
| 6637 | cmdStr += " -s " + str( sAddr ) |
| 6638 | cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] ) |
| 6639 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6640 | if sHost[ "vlan" ] != "None": |
| 6641 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6642 | cmdStr += " -d " + str( dAddr ) |
| 6643 | netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) ) |
| 6644 | netcfg = json.loads( netcfg ) |
| 6645 | assert netcfg, "Failed to get netcfg" |
| 6646 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
| 6647 | cmdList.append( cmdStr ) |
| 6648 | return cmdList |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6649 | except AssertionError: |
| 6650 | main.log.exception( "" ) |
| 6651 | return None |
| 6652 | except ( KeyError, TypeError ): |
| 6653 | main.log.exception( self.name + ": Object not as expected" ) |
| 6654 | return None |
| 6655 | except Exception: |
| 6656 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6657 | main.cleanAndExit() |
| 6658 | |
| 6659 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6660 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6661 | 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] | 6662 | Options: |
| 6663 | sAddr: IP address of the source host |
| 6664 | dAddr: IP address of the destination host |
| 6665 | """ |
| 6666 | try: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6667 | cmdList = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6668 | assert cmdList is not None, "composeT3Command returned None" |
| 6669 | t3Output = "" |
| 6670 | for cmdStr in cmdList: |
| 6671 | handle = self.sendline( cmdStr ) |
| 6672 | assert handle is not None, "Error in sendline" |
| 6673 | assert "Command not found:" not in handle, handle |
| 6674 | assert "Unsupported command:" not in handle, handle |
| 6675 | assert "Error executing command" not in handle, handle |
| 6676 | assert "Tracing packet" in handle |
| 6677 | t3Output += handle |
| 6678 | return t3Output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6679 | except AssertionError: |
| 6680 | main.log.exception( "" ) |
| 6681 | return None |
| 6682 | except pexpect.EOF: |
| 6683 | main.log.error( self.name + ": EOF exception found" ) |
| 6684 | main.log.error( self.name + ": " + self.handle.before ) |
| 6685 | main.cleanAndExit() |
| 6686 | except Exception: |
| 6687 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6688 | main.cleanAndExit() |