andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | OCT 13 2014 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5 | Copyright 2014 Open Networking Foundation (ONF) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 6 | |
| 7 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 8 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 9 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 10 | |
| 11 | TestON is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 14 | (at your option) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 15 | |
| 16 | TestON is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 23 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 25 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 26 | This driver enters the onos> prompt to issue commands. |
| 27 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 28 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 29 | functions and document properly. |
| 30 | |
| 31 | If you are a contributor to the driver, please |
| 32 | list your email here for future contact: |
| 33 | |
| 34 | jhall@onlab.us |
| 35 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 36 | shreya@onlab.us |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 37 | jeremyr@opennetworking.org |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 38 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 39 | import pexpect |
| 40 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 41 | import json |
| 42 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 43 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 44 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 45 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 46 | from core.graph import Graph |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 47 | from cStringIO import StringIO |
| 48 | from itertools import izip |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 49 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 51 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | def __init__( self ): |
| 53 | """ |
| 54 | Initialize client |
| 55 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 56 | self.name = None |
| 57 | self.home = None |
| 58 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 59 | self.karafUser = None |
| 60 | self.karafPass = None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 61 | self.karafPrompt = "sdn@root >" # FIXME: make configurable |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 62 | self.graph = Graph() |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 63 | super( OnosCliDriver, self ).__init__() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 64 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 65 | def checkOptions( self, var, defaultVar ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 66 | if var is None or var == "": |
| 67 | return defaultVar |
| 68 | return var |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 69 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 70 | def connect( self, **connectargs ): |
| 71 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 72 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 73 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 74 | try: |
| 75 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 77 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 78 | for key in self.options: |
| 79 | if key == "home": |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 80 | self.home = self.options[ key ] |
| 81 | elif key == "karaf_username": |
| 82 | self.karafUser = self.options[ key ] |
| 83 | elif key == "karaf_password": |
| 84 | self.karafPass = self.options[ key ] |
| 85 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 86 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 87 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 88 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 89 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 90 | for key in self.options: |
| 91 | if key == 'onosIp': |
| 92 | self.onosIp = self.options[ 'onosIp' ] |
| 93 | break |
| 94 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 95 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 96 | |
| 97 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 98 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 99 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 100 | else: |
| 101 | main.log.info( self.name + |
| 102 | ": Trying to connect to " + |
| 103 | self.ip_address ) |
| 104 | |
| 105 | except KeyError: |
| 106 | main.log.info( "Invalid host name," + |
| 107 | " connecting to local host instead" ) |
| 108 | self.ip_address = 'localhost' |
| 109 | except Exception as inst: |
| 110 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 111 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 112 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 113 | user_name=self.user_name, |
| 114 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 115 | port=self.port, |
| 116 | pwd=self.pwd, |
| 117 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 118 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 119 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 120 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 121 | if self.handle: |
| 122 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 123 | else: |
| 124 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 125 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 126 | except TypeError: |
| 127 | main.log.exception( self.name + ": Object not as expected" ) |
| 128 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 129 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 130 | main.log.error( self.name + ": EOF exception found" ) |
| 131 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 132 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 133 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 134 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 135 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 136 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 137 | def disconnect( self ): |
| 138 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 139 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 140 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 141 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 142 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 143 | if self.handle: |
| 144 | i = self.logout() |
| 145 | if i == main.TRUE: |
| 146 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 147 | self.handle.expect( self.prompt ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 148 | self.handle.sendline( "exit" ) |
| 149 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 150 | except TypeError: |
| 151 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 152 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 153 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 154 | main.log.error( self.name + ": EOF exception found" ) |
| 155 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 156 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 157 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 158 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 159 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 160 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 161 | response = main.FALSE |
| 162 | return response |
| 163 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 164 | def logout( self ): |
| 165 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 166 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 167 | Returns main.TRUE if exited CLI and |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 168 | main.FALSE on timeout (not guranteed you are disconnected) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 169 | None on TypeError |
| 170 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 171 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 172 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 173 | if self.handle: |
| 174 | self.handle.sendline( "" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 175 | i = self.handle.expect( [ self.karafPrompt, self.prompt, pexpect.TIMEOUT ], |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 176 | timeout=10 ) |
| 177 | if i == 0: # In ONOS CLI |
| 178 | self.handle.sendline( "logout" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 179 | j = self.handle.expect( [ self.prompt, |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 180 | "Command not found:", |
| 181 | pexpect.TIMEOUT ] ) |
| 182 | if j == 0: # Successfully logged out |
| 183 | return main.TRUE |
| 184 | elif j == 1 or j == 2: |
| 185 | # ONOS didn't fully load, and logout command isn't working |
| 186 | # or the command timed out |
| 187 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 188 | try: |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 189 | self.handle.expect( self.prompt ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 190 | except pexpect.TIMEOUT: |
| 191 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 192 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 193 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 194 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 195 | repr( self.handle.before ) ) |
| 196 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 197 | elif i == 1: # not in CLI |
| 198 | return main.TRUE |
| 199 | elif i == 3: # Timeout |
| 200 | return main.FALSE |
| 201 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 202 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 203 | except TypeError: |
| 204 | main.log.exception( self.name + ": Object not as expected" ) |
| 205 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 206 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 207 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 208 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 209 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 210 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 211 | main.log.error( self.name + |
| 212 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 213 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 214 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 215 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 216 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 217 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 218 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 219 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 220 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 221 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 222 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 223 | try: |
| 224 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 225 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 226 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 227 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 228 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 229 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 230 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 231 | # and that this driver will have to change accordingly |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 232 | self.handle.expect( str( cellname ) ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 233 | handleBefore = self.handle.before |
| 234 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 235 | # Get the rest of the handle |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 236 | self.handle.sendline( "" ) |
| 237 | self.handle.expect( self.prompt ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 238 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 239 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 240 | main.log.info( "Cell call returned: " + handleBefore + |
| 241 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 242 | |
| 243 | return main.TRUE |
| 244 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 245 | except TypeError: |
| 246 | main.log.exception( self.name + ": Object not as expected" ) |
| 247 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 248 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 249 | main.log.error( self.name + ": eof exception found" ) |
| 250 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 251 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 252 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 253 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 254 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 255 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 256 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 257 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 258 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 259 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 260 | by user would be used to set the current karaf shell idle timeout. |
| 261 | Note that when ever this property is modified the shell will exit and |
Hari Krishna | d7b9c20 | 2015-01-05 10:38:14 -0800 | [diff] [blame] | 262 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 263 | Below is an example to start a session with 60 seconds idle timeout |
| 264 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 265 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 266 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 267 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 268 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 269 | Note: karafTimeout is left as str so that this could be read |
| 270 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 271 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 272 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 273 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 274 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 275 | self.handle.sendline( "" ) |
| 276 | x = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 277 | self.prompt, self.karafPrompt ], commandlineTimeout ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 278 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 279 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 280 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 281 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 282 | # Not in CLI so login |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 283 | if waitForStart: |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 284 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
| 285 | startCliCommand = "onos-wait-for-start " |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 286 | else: |
| 287 | startCliCommand = "onos " |
| 288 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 289 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 290 | self.karafPrompt, |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 291 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 292 | |
| 293 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 294 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 295 | if karafTimeout: # FIXME: This doesn't look right |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 296 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 297 | "config:property-set -p org.apache.karaf.shell\ |
| 298 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 299 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 300 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 301 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 302 | self.handle.expect( self.karafPrompt ) |
| 303 | main.log.debug( self.handle.before ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 304 | return main.TRUE |
| 305 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 306 | # If failed, send ctrl+c to process and try again |
| 307 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 308 | self.handle.send( "\x03" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 309 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 310 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 311 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 312 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 313 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 314 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 315 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 316 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 317 | "config:property-set -p org.apache.karaf.shell\ |
| 318 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 319 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 320 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 321 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 322 | self.handle.expect( self.karafPrompt ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 323 | return main.TRUE |
| 324 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 325 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 326 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 327 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 328 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 329 | except TypeError: |
| 330 | main.log.exception( self.name + ": Object not as expected" ) |
| 331 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 332 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 333 | main.log.error( self.name + ": EOF exception found" ) |
| 334 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 335 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 336 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 337 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 338 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 339 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 340 | def startCellCli( self, karafTimeout="", |
| 341 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 342 | """ |
| 343 | Start CLI on onos ecll handle. |
| 344 | |
| 345 | karafTimeout is an optional argument. karafTimeout value passed |
| 346 | by user would be used to set the current karaf shell idle timeout. |
| 347 | Note that when ever this property is modified the shell will exit and |
| 348 | the subsequent login would reflect new idle timeout. |
| 349 | Below is an example to start a session with 60 seconds idle timeout |
| 350 | ( input value is in milliseconds ): |
| 351 | |
| 352 | tValue = "60000" |
| 353 | |
| 354 | Note: karafTimeout is left as str so that this could be read |
| 355 | and passed to startOnosCli from PARAMS file as str. |
| 356 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 357 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 358 | try: |
| 359 | self.handle.sendline( "" ) |
| 360 | x = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 361 | self.prompt, self.karafPrompt ], commandlineTimeout ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 362 | |
| 363 | if x == 1: |
| 364 | main.log.info( "ONOS cli is already running" ) |
| 365 | return main.TRUE |
| 366 | |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 367 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 368 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 369 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 370 | self.karafPrompt, |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 371 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 372 | |
| 373 | if i == 0: |
| 374 | main.log.info( self.name + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 375 | if karafTimeout: # FIXME: This doesn't look right |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 376 | self.handle.sendline( |
| 377 | "config:property-set -p org.apache.karaf.shell\ |
| 378 | sshIdleTimeout " + |
| 379 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 380 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 381 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 382 | self.handle.expect( self.karafPrompt ) |
| 383 | main.log.debug( self.handle.before ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 384 | return main.TRUE |
| 385 | else: |
| 386 | # If failed, send ctrl+c to process and try again |
| 387 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 388 | self.handle.send( "\x03" ) |
| 389 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 390 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 391 | timeout=30 ) |
| 392 | if i == 0: |
| 393 | main.log.info( self.name + " CLI Started " + |
| 394 | "successfully after retry attempt" ) |
| 395 | if karafTimeout: |
| 396 | self.handle.sendline( |
| 397 | "config:property-set -p org.apache.karaf.shell\ |
| 398 | sshIdleTimeout " + |
| 399 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 400 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 401 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 402 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 403 | return main.TRUE |
| 404 | else: |
| 405 | main.log.error( "Connection to CLI " + |
| 406 | self.name + " timeout" ) |
| 407 | return main.FALSE |
| 408 | |
| 409 | except TypeError: |
| 410 | main.log.exception( self.name + ": Object not as expected" ) |
| 411 | return None |
| 412 | except pexpect.EOF: |
| 413 | main.log.error( self.name + ": EOF exception found" ) |
| 414 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 415 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 416 | except Exception: |
| 417 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 418 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 419 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 420 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 421 | """ |
| 422 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 423 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 424 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 425 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 426 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 427 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 428 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 429 | """ |
| 430 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 431 | lvlStr = "" |
| 432 | if level: |
| 433 | lvlStr = "--level=" + level |
| 434 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 435 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 436 | self.handle.expect( "log:log" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 437 | self.handle.expect( self.karafPrompt ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 438 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 439 | response = self.handle.before |
| 440 | if re.search( "Error", response ): |
| 441 | return main.FALSE |
| 442 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 443 | except pexpect.TIMEOUT: |
| 444 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 445 | if noExit: |
| 446 | main.cleanup() |
| 447 | return None |
| 448 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 449 | main.cleanAndExit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 450 | except pexpect.EOF: |
| 451 | main.log.error( self.name + ": EOF exception found" ) |
| 452 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 453 | if noExit: |
| 454 | main.cleanup() |
| 455 | return None |
| 456 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 457 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 458 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 459 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 460 | if noExit: |
| 461 | main.cleanup() |
| 462 | return None |
| 463 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 464 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 465 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 466 | def clearBuffer( self, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 467 | """ |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 468 | Test cli connection and clear any left over output in the buffer |
| 469 | Optional Arguments: |
| 470 | debug - Defaults to False. If True, will enable debug logging. |
| 471 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 472 | before a timeout. |
| 473 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 474 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 475 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 476 | # Try to reconnect if disconnected from cli |
| 477 | self.handle.sendline( "" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 478 | i = self.handle.expect( [ self.karafPrompt, self.prompt, pexpect.TIMEOUT ] ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 479 | response = self.handle.before |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 480 | if i == 1: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 481 | main.log.error( self.name + ": onos cli session closed. " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 482 | if self.onosIp: |
| 483 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 484 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 485 | if reconnectResult: |
| 486 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 487 | else: |
| 488 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 489 | if noExit: |
| 490 | return None |
| 491 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 492 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 493 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 494 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 495 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 496 | main.log.warn( "Timeout when testing cli responsiveness" ) |
| 497 | main.log.debug( self.handle.before ) |
| 498 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 499 | self.handle.expect( self.karafPrompt ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 500 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 501 | response += self.handle.before |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 502 | if debug: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 503 | main.log.debug( self.name + ": Raw output from sending ''" ) |
| 504 | main.log.debug( self.name + ": " + repr( response ) ) |
| 505 | except pexpect.TIMEOUT: |
| 506 | main.log.error( self.name + ": ONOS timeout" ) |
| 507 | main.log.debug( self.handle.before ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 508 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 509 | self.handle.expect( self.karafPrompt ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 510 | return None |
| 511 | except pexpect.EOF: |
| 512 | main.log.error( self.name + ": EOF exception found" ) |
| 513 | main.log.error( self.name + ": " + self.handle.before ) |
| 514 | if noExit: |
| 515 | return None |
| 516 | else: |
| 517 | main.cleanAndExit() |
| 518 | except Exception: |
| 519 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 520 | if noExit: |
| 521 | return None |
| 522 | else: |
| 523 | main.cleanAndExit() |
| 524 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 525 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 526 | """ |
| 527 | A wrapper around pexpect's sendline/expect. Will return all the output from a given command |
| 528 | |
| 529 | Required Arguments: |
| 530 | cmdStr - String to send to the pexpect session |
| 531 | |
| 532 | Optional Arguments: |
| 533 | showResponse - Defaults to False. If True will log the response. |
| 534 | debug - Defaults to False. If True, will enable debug logging. |
| 535 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 536 | before a timeout. |
| 537 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 538 | closed channel, but instead return None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 539 | relaxedRegex - Defaults to True. If there is a pipe in the command send, will only try to match the last part of the piped command. |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 540 | |
| 541 | Warning: There are no sanity checking to commands sent using this method. |
| 542 | |
| 543 | """ |
| 544 | try: |
| 545 | # Try to reconnect if disconnected from cli |
| 546 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
| 547 | if debug: |
| 548 | # NOTE: This adds an average of .4 seconds per call |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 549 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 550 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 551 | self.handle.sendline( cmdStr ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 552 | self.handle.expect( self.karafPrompt, timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 553 | response = self.handle.before |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 554 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 555 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 556 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 557 | main.log.debug( self.name + ": Raw output" ) |
| 558 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 559 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 560 | # Remove control codes from karaf 4.2.1 |
| 561 | karafEscape = re.compile( r"('(0|1)~\'|\r\r\r\n\x1b\[A\x1b\[79Cx|\x1b(>|=)|\x1b\[90m~)" ) |
| 562 | response = karafEscape.sub( '', response ) |
| 563 | if debug: |
| 564 | main.log.debug( self.name + ": karafEscape output" ) |
| 565 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 566 | # Remove ANSI color control strings from output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 567 | ansiEscape = re.compile( r'((\x9b|\x1b\[)[0-?]*[ -/]*[@-~])' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 568 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 569 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 570 | main.log.debug( self.name + ": ansiEscape output" ) |
| 571 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 572 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 573 | # Remove ANSI color control strings from output |
| 574 | backspaceEscape = re.compile( r'((..\x08\x08)|(.|\s)\x08)' ) |
| 575 | response = backspaceEscape.sub( '', response ) |
| 576 | if debug: |
| 577 | main.log.debug( self.name + ": backspaceEscape output" ) |
| 578 | main.log.debug( self.name + ": " + repr( response ) ) |
| 579 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 580 | # Remove extra return chars that get added |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 581 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 582 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 583 | main.log.debug( self.name + ": Removed extra returns " + |
| 584 | "from output" ) |
| 585 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 586 | |
| 587 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 588 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 589 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 590 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 591 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 592 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 593 | # parse for just the output, remove the cmd from response |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 594 | if relaxedRegex: |
| 595 | # This was added because karaf 4.2 is stripping some characters from the command echo |
| 596 | endStr = cmdStr.split( '|' )[-1] |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 597 | output = response.split( endStr.strip(), 1 ) |
| 598 | else: |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 599 | output = response.split( endStr.strip(), 1 ) |
| 600 | if len( output ) < 2: |
| 601 | main.log.warn( "Relaxing regex match to last 5 characters of the sent command" ) |
Jon Hall | 0623dd4 | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 602 | output = response.split( endStr.strip()[-5:], 1 ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 603 | if output: |
| 604 | if debug: |
| 605 | main.log.debug( self.name + ": split output" ) |
| 606 | for r in output: |
| 607 | main.log.debug( self.name + ": " + repr( r ) ) |
| 608 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 609 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 610 | main.log.info( "Response from ONOS: {}".format( output ) ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 611 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 612 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 613 | except pexpect.TIMEOUT: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 614 | main.log.error( self.name + ": ONOS timeout" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 615 | if debug: |
| 616 | main.log.debug( self.handle.before ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 617 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 618 | self.handle.expect( self.karafPrompt ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 619 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 620 | except IndexError: |
| 621 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 622 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 623 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 624 | except TypeError: |
| 625 | main.log.exception( self.name + ": Object not as expected" ) |
| 626 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 627 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 628 | main.log.error( self.name + ": EOF exception found" ) |
| 629 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 630 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 631 | return None |
| 632 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 633 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 634 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 635 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 636 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 637 | return None |
| 638 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 639 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 640 | |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 641 | def lineCount( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
| 642 | """ |
| 643 | A wrapper around sendline(). Will return the number of lines returned or None on error |
| 644 | |
| 645 | Required Arguments: |
| 646 | cmdStr - String to send to the pexpect session |
| 647 | |
| 648 | Optional Arguments: |
| 649 | showResponse - Defaults to False. If True will log the response. |
| 650 | debug - Defaults to False. If True, will enable debug logging. |
| 651 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 652 | before a timeout. |
| 653 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 654 | closed channel, but instead return None |
| 655 | 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. |
| 656 | |
| 657 | Warning: There are no sanity checking to commands sent using this method. |
| 658 | |
| 659 | """ |
| 660 | try: |
| 661 | numLines = self.sendline( cmdStr, showResponse, debug, timeout, noExit, relaxedRegex ) |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 662 | parsed = re.search( "(\d+)\s+(\d+)", numLines ) |
| 663 | if not parsed: |
| 664 | main.log.error( "Warning, output of karaf's wc may have changed" ) |
| 665 | return None |
| 666 | return parsed.group( 1 ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 667 | except IndexError: |
| 668 | main.log.exception( self.name + ": Object not as expected" ) |
| 669 | main.log.debug( "response: {}".format( repr( response ) ) ) |
| 670 | return None |
| 671 | except TypeError: |
| 672 | main.log.exception( self.name + ": Object not as expected" ) |
| 673 | return None |
| 674 | except Exception: |
| 675 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 676 | if noExit: |
| 677 | return None |
| 678 | else: |
| 679 | main.cleanAndExit() |
| 680 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 681 | # IMPORTANT NOTE: |
| 682 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 683 | # the cli command changing 'a:b' with 'aB'. |
| 684 | # Ex ) onos:topology > onosTopology |
| 685 | # onos:links > onosLinks |
| 686 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 687 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 688 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 689 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 690 | Adds a new cluster node by ID and address information. |
| 691 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 692 | * nodeId |
| 693 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 694 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 695 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 696 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 697 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 698 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 699 | str( ONOSIp ) + " " + str( tcpPort ) |
| 700 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 701 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 702 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 703 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 704 | main.log.error( self.name + ": Error in adding node" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 705 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 706 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 707 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 708 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 709 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 710 | except AssertionError: |
| 711 | main.log.exception( "" ) |
| 712 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 713 | except TypeError: |
| 714 | main.log.exception( self.name + ": Object not as expected" ) |
| 715 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 716 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 717 | main.log.error( self.name + ": EOF exception found" ) |
| 718 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 719 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 720 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 721 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 722 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 723 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 724 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 725 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 726 | Removes a cluster by ID |
| 727 | Issues command: 'remove-node [<node-id>]' |
| 728 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 729 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 730 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 731 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 732 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 733 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 734 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 735 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 736 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 737 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 738 | main.log.error( self.name + ": Error in removing node" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 739 | main.log.error( handle ) |
| 740 | return main.FALSE |
| 741 | else: |
| 742 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 743 | except AssertionError: |
| 744 | main.log.exception( "" ) |
| 745 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 746 | except TypeError: |
| 747 | main.log.exception( self.name + ": Object not as expected" ) |
| 748 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 749 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 750 | main.log.error( self.name + ": EOF exception found" ) |
| 751 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 752 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 753 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 754 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 755 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 756 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 757 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 758 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 759 | List the nodes currently visible |
| 760 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 761 | Optional argument: |
| 762 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 763 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 764 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 765 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 766 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 767 | cmdStr += " -j" |
| 768 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 769 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 770 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 771 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 772 | except AssertionError: |
| 773 | main.log.exception( "" ) |
| 774 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 775 | except TypeError: |
| 776 | main.log.exception( self.name + ": Object not as expected" ) |
| 777 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 778 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 779 | main.log.error( self.name + ": EOF exception found" ) |
| 780 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 781 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 782 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 783 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 784 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 785 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 786 | def topology( self ): |
| 787 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 788 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 789 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 790 | Return: |
| 791 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 792 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 793 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 794 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 795 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 796 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 797 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 798 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 799 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 800 | except AssertionError: |
| 801 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 802 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 803 | except TypeError: |
| 804 | main.log.exception( self.name + ": Object not as expected" ) |
| 805 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 806 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 807 | main.log.error( self.name + ": EOF exception found" ) |
| 808 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 809 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 810 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 811 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 812 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 813 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 814 | def deviceRemove( self, deviceId ): |
| 815 | """ |
| 816 | Removes particular device from storage |
| 817 | |
| 818 | TODO: refactor this function |
| 819 | """ |
| 820 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 821 | cmdStr = "device-remove " + str( deviceId ) |
| 822 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 823 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 824 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 825 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 826 | main.log.error( self.name + ": Error in removing device" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 827 | main.log.error( handle ) |
| 828 | return main.FALSE |
| 829 | else: |
| 830 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 831 | except AssertionError: |
| 832 | main.log.exception( "" ) |
| 833 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 834 | except TypeError: |
| 835 | main.log.exception( self.name + ": Object not as expected" ) |
| 836 | return None |
| 837 | except pexpect.EOF: |
| 838 | main.log.error( self.name + ": EOF exception found" ) |
| 839 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 840 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 841 | except Exception: |
| 842 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 843 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 844 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 845 | def devices( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 846 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 847 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 848 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 849 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 850 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 851 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 852 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 853 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 854 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 855 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 856 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 857 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 858 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 859 | except AssertionError: |
| 860 | main.log.exception( "" ) |
| 861 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 862 | except TypeError: |
| 863 | main.log.exception( self.name + ": Object not as expected" ) |
| 864 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 865 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 866 | main.log.error( self.name + ": EOF exception found" ) |
| 867 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 868 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 869 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 870 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 871 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 872 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 873 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 874 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 875 | This balances the devices across all controllers |
| 876 | by issuing command: 'onos> onos:balance-masters' |
| 877 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 878 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 879 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 880 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 881 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 882 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 883 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 884 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 885 | main.log.error( self.name + ": Error in balancing masters" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 886 | main.log.error( handle ) |
| 887 | return main.FALSE |
| 888 | else: |
| 889 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 890 | except AssertionError: |
| 891 | main.log.exception( "" ) |
| 892 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 893 | except TypeError: |
| 894 | main.log.exception( self.name + ": Object not as expected" ) |
| 895 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 896 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 897 | main.log.error( self.name + ": EOF exception found" ) |
| 898 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 899 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 900 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 901 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 902 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 903 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 904 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 905 | """ |
| 906 | Returns the output of the masters command. |
| 907 | Optional argument: |
| 908 | * jsonFormat - boolean indicating if you want output in json |
| 909 | """ |
| 910 | try: |
| 911 | cmdStr = "onos:masters" |
| 912 | if jsonFormat: |
| 913 | cmdStr += " -j" |
| 914 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 915 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 916 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 917 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 918 | except AssertionError: |
| 919 | main.log.exception( "" ) |
| 920 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 921 | except TypeError: |
| 922 | main.log.exception( self.name + ": Object not as expected" ) |
| 923 | return None |
| 924 | except pexpect.EOF: |
| 925 | main.log.error( self.name + ": EOF exception found" ) |
| 926 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 927 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 928 | except Exception: |
| 929 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 930 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 931 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 932 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 933 | """ |
| 934 | Uses the master command to check that the devices' leadership |
| 935 | is evenly divided |
| 936 | |
| 937 | Dependencies: checkMasters() and summary() |
| 938 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 939 | Returns main.TRUE if the devices are balanced |
| 940 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 941 | Exits on Exception |
| 942 | Returns None on TypeError |
| 943 | """ |
| 944 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 945 | summaryOutput = self.summary() |
| 946 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 947 | except ( TypeError, ValueError ): |
| 948 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 949 | return None |
| 950 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 951 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 952 | mastersOutput = self.checkMasters() |
| 953 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 954 | first = masters[ 0 ][ "size" ] |
| 955 | for master in masters: |
| 956 | totalOwnedDevices += master[ "size" ] |
| 957 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 958 | main.log.error( "Mastership not balanced" ) |
| 959 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 960 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 961 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 962 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 963 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 964 | except ( TypeError, ValueError ): |
| 965 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 966 | return None |
| 967 | except pexpect.EOF: |
| 968 | main.log.error( self.name + ": EOF exception found" ) |
| 969 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 970 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 971 | except Exception: |
| 972 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 973 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 974 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 975 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 976 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 977 | Lists all core links |
| 978 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 979 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 980 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 981 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 982 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 983 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 984 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 985 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 986 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 987 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 988 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 989 | except AssertionError: |
| 990 | main.log.exception( "" ) |
| 991 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 992 | except TypeError: |
| 993 | main.log.exception( self.name + ": Object not as expected" ) |
| 994 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 995 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 996 | main.log.error( self.name + ": EOF exception found" ) |
| 997 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 998 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 999 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1000 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1001 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1002 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1003 | def ports( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1004 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1005 | Lists all ports |
| 1006 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1007 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1008 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1009 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1010 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1011 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1012 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1013 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1014 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1015 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1016 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1017 | except AssertionError: |
| 1018 | main.log.exception( "" ) |
| 1019 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1020 | except TypeError: |
| 1021 | main.log.exception( self.name + ": Object not as expected" ) |
| 1022 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1023 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1024 | main.log.error( self.name + ": EOF exception found" ) |
| 1025 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1026 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1027 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1028 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1029 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1030 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1031 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1032 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1033 | Lists all devices and the controllers with roles assigned to them |
| 1034 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1035 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1036 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1037 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1038 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1039 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1040 | cmdStr += " -j" |
| 1041 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1042 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1043 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1044 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1045 | except AssertionError: |
| 1046 | main.log.exception( "" ) |
| 1047 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1048 | except TypeError: |
| 1049 | main.log.exception( self.name + ": Object not as expected" ) |
| 1050 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1051 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1052 | main.log.error( self.name + ": EOF exception found" ) |
| 1053 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1054 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1055 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1056 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1057 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1058 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1059 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1060 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1061 | Given the a string containing the json representation of the "roles" |
| 1062 | cli command and a partial or whole device id, returns a json object |
| 1063 | containing the roles output for the first device whose id contains |
| 1064 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1065 | |
| 1066 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1067 | A dict of the role assignments for the given device or |
| 1068 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1069 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1070 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1071 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1072 | return None |
| 1073 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1074 | rawRoles = self.roles() |
| 1075 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1076 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1077 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1078 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1079 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1080 | return device |
| 1081 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1082 | except ( TypeError, ValueError ): |
| 1083 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1084 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 1085 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1086 | main.log.error( self.name + ": EOF exception found" ) |
| 1087 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1088 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1089 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1090 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1091 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1092 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1093 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1094 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1095 | Iterates through each device and checks if there is a master assigned |
| 1096 | Returns: main.TRUE if each device has a master |
| 1097 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1098 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1099 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1100 | rawRoles = self.roles() |
| 1101 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1102 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1103 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1104 | # print device |
| 1105 | if device[ 'master' ] == "none": |
| 1106 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1107 | return main.FALSE |
| 1108 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1109 | except ( TypeError, ValueError ): |
| 1110 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1111 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1112 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1113 | main.log.error( self.name + ": EOF exception found" ) |
| 1114 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1115 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1116 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1117 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1118 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1119 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1120 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1121 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1122 | Returns string of paths, and the cost. |
| 1123 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1124 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1125 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1126 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1127 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1128 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1129 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1130 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1131 | main.log.error( self.name + ": Error in getting paths" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1132 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1133 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1134 | path = handle.split( ";" )[ 0 ] |
| 1135 | cost = handle.split( ";" )[ 1 ] |
| 1136 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1137 | except AssertionError: |
| 1138 | main.log.exception( "" ) |
| 1139 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1140 | except TypeError: |
| 1141 | main.log.exception( self.name + ": Object not as expected" ) |
| 1142 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1143 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1144 | main.log.error( self.name + ": EOF exception found" ) |
| 1145 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1146 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1147 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1148 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1149 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1150 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1151 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1152 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1153 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1154 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1155 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1156 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1157 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1158 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1159 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1160 | cmdStr += " -j" |
| 1161 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1162 | if handle: |
| 1163 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1164 | # TODO: Maybe make this less hardcoded |
| 1165 | # ConsistentMap Exceptions |
| 1166 | assert "org.onosproject.store.service" not in handle |
| 1167 | # Node not leader |
| 1168 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1169 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1170 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1171 | main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1172 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1173 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1174 | except TypeError: |
| 1175 | main.log.exception( self.name + ": Object not as expected" ) |
| 1176 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1177 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1178 | main.log.error( self.name + ": EOF exception found" ) |
| 1179 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1180 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1181 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1182 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1183 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1184 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1185 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1186 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1187 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1188 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1189 | 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] | 1190 | partial mac address |
| 1191 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1192 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1193 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1194 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1195 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1196 | return None |
| 1197 | else: |
| 1198 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1199 | rawHosts = self.hosts() |
| 1200 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1201 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1202 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1203 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1204 | if not host: |
| 1205 | pass |
| 1206 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1207 | return host |
| 1208 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1209 | except ( TypeError, ValueError ): |
| 1210 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1211 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1212 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1213 | main.log.error( self.name + ": EOF exception found" ) |
| 1214 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1215 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1216 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1217 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1218 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1219 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1220 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1221 | """ |
| 1222 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1223 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1224 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1225 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1226 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1227 | IMPORTANT: |
| 1228 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1229 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1230 | Furthermore, it assumes that value of VLAN is '-1' |
| 1231 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1232 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1233 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1234 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1235 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1236 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1237 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1238 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1239 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1240 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1241 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1242 | i = iter( str( hostHex ) ) |
| 1243 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1244 | hostHex = hostHex + "/-1" |
| 1245 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1246 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1247 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1248 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1249 | except TypeError: |
| 1250 | main.log.exception( self.name + ": Object not as expected" ) |
| 1251 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1252 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1253 | main.log.error( self.name + ": EOF exception found" ) |
| 1254 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1255 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1256 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1257 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1258 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1259 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1260 | def verifyHostLocation( self, hostIp, location ): |
| 1261 | """ |
| 1262 | Description: |
| 1263 | Verify the host given is discovered in all locations expected |
| 1264 | Required: |
| 1265 | hostIp: IP address of the host |
| 1266 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1267 | Could be a string or list |
| 1268 | Returns: |
| 1269 | main.TRUE if host is discovered on all locations provided |
| 1270 | main.FALSE otherwise |
| 1271 | """ |
| 1272 | import json |
| 1273 | locations = [ location ] if isinstance( location, str ) else location |
| 1274 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1275 | try: |
| 1276 | hosts = self.hosts() |
| 1277 | hosts = json.loads( hosts ) |
| 1278 | targetHost = None |
| 1279 | for host in hosts: |
| 1280 | if hostIp in host[ "ipAddresses" ]: |
| 1281 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1282 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1283 | result = main.TRUE |
| 1284 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1285 | for loc in locations: |
| 1286 | discovered = False |
| 1287 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1288 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1289 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1290 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1291 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1292 | break |
| 1293 | if discovered: |
| 1294 | locationsDiscovered.remove( locDiscovered ) |
| 1295 | else: |
| 1296 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1297 | result = main.FALSE |
| 1298 | if locationsDiscovered: |
| 1299 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1300 | result = main.FALSE |
| 1301 | return result |
| 1302 | except KeyError: |
| 1303 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1304 | return None |
| 1305 | except pexpect.EOF: |
| 1306 | main.log.error( self.name + ": EOF exception found" ) |
| 1307 | main.log.error( self.name + ": " + self.handle.before ) |
| 1308 | main.cleanAndExit() |
| 1309 | except Exception: |
| 1310 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1311 | return None |
| 1312 | |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1313 | def verifyHostIp( self, hostList=[], prefix="" ): |
| 1314 | """ |
| 1315 | Description: |
| 1316 | Verify that all hosts have IP address assigned to them |
| 1317 | Optional: |
| 1318 | hostList: If specified, verifications only happen to the hosts |
| 1319 | in hostList |
| 1320 | prefix: at least one of the ip address assigned to the host |
| 1321 | needs to have the specified prefix |
| 1322 | Returns: |
| 1323 | main.TRUE if all hosts have specific IP address assigned; |
| 1324 | main.FALSE otherwise |
| 1325 | """ |
| 1326 | import json |
| 1327 | try: |
| 1328 | hosts = self.hosts() |
| 1329 | hosts = json.loads( hosts ) |
| 1330 | if not hostList: |
| 1331 | hostList = [ host[ "id" ] for host in hosts ] |
| 1332 | for host in hosts: |
| 1333 | hostId = host[ "id" ] |
| 1334 | if hostId not in hostList: |
| 1335 | continue |
| 1336 | ipList = host[ "ipAddresses" ] |
| 1337 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1338 | if not ipList: |
| 1339 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1340 | else: |
| 1341 | if not any( ip.startswith( str( prefix ) ) for ip in ipList ): |
| 1342 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1343 | else: |
| 1344 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1345 | hostList.remove( hostId ) |
| 1346 | if hostList: |
| 1347 | main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) ) |
| 1348 | return main.FALSE |
| 1349 | else: |
| 1350 | return main.TRUE |
| 1351 | except KeyError: |
| 1352 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1353 | return None |
| 1354 | except pexpect.EOF: |
| 1355 | main.log.error( self.name + ": EOF exception found" ) |
| 1356 | main.log.error( self.name + ": " + self.handle.before ) |
| 1357 | main.cleanAndExit() |
| 1358 | except Exception: |
| 1359 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1360 | return None |
| 1361 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1362 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1363 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1364 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1365 | * hostIdOne: ONOS host id for host1 |
| 1366 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1367 | Optional: |
| 1368 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1369 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1370 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1371 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1372 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1373 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1374 | Returns: |
| 1375 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1376 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1377 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1378 | cmdStr = "add-host-intent " |
| 1379 | if vlanId: |
| 1380 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1381 | if setVlan: |
| 1382 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1383 | if encap: |
| 1384 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1385 | if bandwidth: |
| 1386 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1387 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1388 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1389 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1390 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1391 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1392 | main.log.error( self.name + ": Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1393 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1394 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1395 | else: |
| 1396 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1397 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1398 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1399 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1400 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1401 | else: |
| 1402 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1403 | main.log.debug( "Response from ONOS was: " + |
| 1404 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1405 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1406 | except AssertionError: |
| 1407 | main.log.exception( "" ) |
| 1408 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1409 | except TypeError: |
| 1410 | main.log.exception( self.name + ": Object not as expected" ) |
| 1411 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1412 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1413 | main.log.error( self.name + ": EOF exception found" ) |
| 1414 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1415 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1416 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1417 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1418 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1419 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1420 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1421 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1422 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1423 | * ingressDevice: device id of ingress device |
| 1424 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1425 | Optional: |
| 1426 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1427 | Description: |
| 1428 | Adds an optical intent by specifying an ingress and egress device |
| 1429 | Returns: |
| 1430 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1431 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1432 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1433 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1434 | " " + str( egressDevice ) |
| 1435 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1436 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1437 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1438 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1439 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1440 | main.log.error( self.name + ": Error in adding Optical intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1441 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1442 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1443 | main.log.info( "Optical intent installed between " + |
| 1444 | str( ingressDevice ) + " and " + |
| 1445 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1446 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1447 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1448 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1449 | else: |
| 1450 | main.log.error( "Error, intent ID not found" ) |
| 1451 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1452 | except AssertionError: |
| 1453 | main.log.exception( "" ) |
| 1454 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1455 | except TypeError: |
| 1456 | main.log.exception( self.name + ": Object not as expected" ) |
| 1457 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1458 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1459 | main.log.error( self.name + ": EOF exception found" ) |
| 1460 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1461 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1462 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1463 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1464 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1465 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1466 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1467 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1468 | ingressDevice, |
| 1469 | egressDevice, |
| 1470 | portIngress="", |
| 1471 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1472 | ethType="", |
| 1473 | ethSrc="", |
| 1474 | ethDst="", |
| 1475 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1476 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1477 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1478 | ipProto="", |
| 1479 | ipSrc="", |
| 1480 | ipDst="", |
| 1481 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1482 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1483 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1484 | setVlan="", |
| 1485 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1486 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1487 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1488 | * ingressDevice: device id of ingress device |
| 1489 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1490 | Optional: |
| 1491 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1492 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1493 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1494 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1495 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1496 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1497 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1498 | * ipSrc: specify ip source address |
| 1499 | * ipDst: specify ip destination address |
| 1500 | * tcpSrc: specify tcp source port |
| 1501 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1502 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1503 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1504 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1505 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1506 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1507 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1508 | Returns: |
| 1509 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1510 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1511 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1512 | options developers provide for point-to-point |
| 1513 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1514 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1515 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1516 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1517 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1518 | if ethType: |
| 1519 | cmd += " --ethType " + str( ethType ) |
| 1520 | if ethSrc: |
| 1521 | cmd += " --ethSrc " + str( ethSrc ) |
| 1522 | if ethDst: |
| 1523 | cmd += " --ethDst " + str( ethDst ) |
| 1524 | if bandwidth: |
| 1525 | cmd += " --bandwidth " + str( bandwidth ) |
| 1526 | if lambdaAlloc: |
| 1527 | cmd += " --lambda " |
| 1528 | if ipProto: |
| 1529 | cmd += " --ipProto " + str( ipProto ) |
| 1530 | if ipSrc: |
| 1531 | cmd += " --ipSrc " + str( ipSrc ) |
| 1532 | if ipDst: |
| 1533 | cmd += " --ipDst " + str( ipDst ) |
| 1534 | if tcpSrc: |
| 1535 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1536 | if tcpDst: |
| 1537 | cmd += " --tcpDst " + str( tcpDst ) |
| 1538 | if vlanId: |
| 1539 | cmd += " -v " + str( vlanId ) |
| 1540 | if setVlan: |
| 1541 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1542 | if encap: |
| 1543 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1544 | if protected: |
| 1545 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1546 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1547 | # Check whether the user appended the port |
| 1548 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1549 | if "/" in ingressDevice: |
| 1550 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1551 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1552 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1553 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1554 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1555 | # Would it make sense to throw an exception and exit |
| 1556 | # the test? |
| 1557 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1558 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1559 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1560 | str( ingressDevice ) + "/" +\ |
| 1561 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1562 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1563 | if "/" in egressDevice: |
| 1564 | cmd += " " + str( egressDevice ) |
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 portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1567 | main.log.error( "You must specify the egress port" ) |
| 1568 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1569 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1570 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1571 | str( egressDevice ) + "/" +\ |
| 1572 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1573 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1574 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1575 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1576 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1577 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1578 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1579 | main.log.error( self.name + ": Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1580 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1581 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1582 | # TODO: print out all the options in this message? |
| 1583 | main.log.info( "Point-to-point intent installed between " + |
| 1584 | str( ingressDevice ) + " and " + |
| 1585 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1586 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1587 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1588 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1589 | else: |
| 1590 | main.log.error( "Error, intent ID not found" ) |
| 1591 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1592 | except AssertionError: |
| 1593 | main.log.exception( "" ) |
| 1594 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1595 | except TypeError: |
| 1596 | main.log.exception( self.name + ": Object not as expected" ) |
| 1597 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1598 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1599 | main.log.error( self.name + ": EOF exception found" ) |
| 1600 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1601 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1602 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1603 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1604 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1605 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1606 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1607 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1608 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1609 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1610 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1611 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1612 | ethType="", |
| 1613 | ethSrc="", |
| 1614 | ethDst="", |
| 1615 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1616 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1617 | ipProto="", |
| 1618 | ipSrc="", |
| 1619 | ipDst="", |
| 1620 | tcpSrc="", |
| 1621 | tcpDst="", |
| 1622 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1623 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1624 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1625 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1626 | partial=False, |
| 1627 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1628 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1629 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1630 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1631 | is same. That is, all ingress devices include port numbers |
| 1632 | with a "/" or all ingress devices could specify device |
| 1633 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1634 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1635 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1636 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1637 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1638 | Optional: |
| 1639 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1640 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1641 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1642 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1643 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1644 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1645 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1646 | * ipSrc: specify ip source address |
| 1647 | * ipDst: specify ip destination address |
| 1648 | * tcpSrc: specify tcp source port |
| 1649 | * tcpDst: specify tcp destination port |
| 1650 | * setEthSrc: action to Rewrite Source MAC Address |
| 1651 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1652 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1653 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1654 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1655 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1656 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1657 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1658 | Returns: |
| 1659 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1660 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1661 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1662 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1663 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1664 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1665 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1666 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1667 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1668 | if ethType: |
| 1669 | cmd += " --ethType " + str( ethType ) |
| 1670 | if ethSrc: |
| 1671 | cmd += " --ethSrc " + str( ethSrc ) |
| 1672 | if ethDst: |
| 1673 | cmd += " --ethDst " + str( ethDst ) |
| 1674 | if bandwidth: |
| 1675 | cmd += " --bandwidth " + str( bandwidth ) |
| 1676 | if lambdaAlloc: |
| 1677 | cmd += " --lambda " |
| 1678 | if ipProto: |
| 1679 | cmd += " --ipProto " + str( ipProto ) |
| 1680 | if ipSrc: |
| 1681 | cmd += " --ipSrc " + str( ipSrc ) |
| 1682 | if ipDst: |
| 1683 | cmd += " --ipDst " + str( ipDst ) |
| 1684 | if tcpSrc: |
| 1685 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1686 | if tcpDst: |
| 1687 | cmd += " --tcpDst " + str( tcpDst ) |
| 1688 | if setEthSrc: |
| 1689 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1690 | if setEthDst: |
| 1691 | cmd += " --setEthDst " + str( setEthDst ) |
| 1692 | if vlanId: |
| 1693 | cmd += " -v " + str( vlanId ) |
| 1694 | if setVlan: |
| 1695 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1696 | if partial: |
| 1697 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1698 | if encap: |
| 1699 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1700 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1701 | # Check whether the user appended the port |
| 1702 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1703 | |
| 1704 | if portIngressList is None: |
| 1705 | for ingressDevice in ingressDeviceList: |
| 1706 | if "/" in ingressDevice: |
| 1707 | cmd += " " + str( ingressDevice ) |
| 1708 | else: |
| 1709 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1710 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1711 | # TODO: perhaps more meaningful return |
| 1712 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1713 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1714 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1715 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1716 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1717 | cmd += " " + \ |
| 1718 | str( ingressDevice ) + "/" +\ |
| 1719 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1720 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1721 | main.log.error( "Device list and port list does not " + |
| 1722 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1723 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1724 | if "/" in egressDevice: |
| 1725 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1726 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1727 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1728 | main.log.error( "You must specify " + |
| 1729 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1730 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1731 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1732 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1733 | str( egressDevice ) + "/" +\ |
| 1734 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1735 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1736 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1737 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1738 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1739 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1740 | main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1741 | "intent" ) |
| 1742 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1743 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1744 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1745 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1746 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1747 | else: |
| 1748 | main.log.error( "Error, intent ID not found" ) |
| 1749 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1750 | except AssertionError: |
| 1751 | main.log.exception( "" ) |
| 1752 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1753 | except TypeError: |
| 1754 | main.log.exception( self.name + ": Object not as expected" ) |
| 1755 | return None |
| 1756 | except pexpect.EOF: |
| 1757 | main.log.error( self.name + ": EOF exception found" ) |
| 1758 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1759 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1760 | except Exception: |
| 1761 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1762 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1763 | |
| 1764 | def addSinglepointToMultipointIntent( |
| 1765 | self, |
| 1766 | ingressDevice, |
| 1767 | egressDeviceList, |
| 1768 | portIngress="", |
| 1769 | portEgressList=None, |
| 1770 | ethType="", |
| 1771 | ethSrc="", |
| 1772 | ethDst="", |
| 1773 | bandwidth="", |
| 1774 | lambdaAlloc=False, |
| 1775 | ipProto="", |
| 1776 | ipSrc="", |
| 1777 | ipDst="", |
| 1778 | tcpSrc="", |
| 1779 | tcpDst="", |
| 1780 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1781 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1782 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1783 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1784 | partial=False, |
| 1785 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1786 | """ |
| 1787 | Note: |
| 1788 | This function assumes the format of all egress devices |
| 1789 | is same. That is, all egress devices include port numbers |
| 1790 | with a "/" or all egress devices could specify device |
| 1791 | ids and port numbers seperately. |
| 1792 | Required: |
| 1793 | * EgressDeviceList: List of device ids of egress device |
| 1794 | ( Atleast 2 eress devices required in the list ) |
| 1795 | * ingressDevice: device id of ingress device |
| 1796 | Optional: |
| 1797 | * ethType: specify ethType |
| 1798 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1799 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1800 | * bandwidth: specify bandwidth capacity of link |
| 1801 | * lambdaAlloc: if True, intent will allocate lambda |
| 1802 | for the specified intent |
| 1803 | * ipProto: specify ip protocol |
| 1804 | * ipSrc: specify ip source address |
| 1805 | * ipDst: specify ip destination address |
| 1806 | * tcpSrc: specify tcp source port |
| 1807 | * tcpDst: specify tcp destination port |
| 1808 | * setEthSrc: action to Rewrite Source MAC Address |
| 1809 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1810 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1811 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1812 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1813 | Description: |
| 1814 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1815 | specifying device id's and optional fields |
| 1816 | Returns: |
| 1817 | A string of the intent id or None on error |
| 1818 | |
| 1819 | NOTE: This function may change depending on the |
| 1820 | options developers provide for singlepoint-to-multipoint |
| 1821 | intent via cli |
| 1822 | """ |
| 1823 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1824 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1825 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1826 | if ethType: |
| 1827 | cmd += " --ethType " + str( ethType ) |
| 1828 | if ethSrc: |
| 1829 | cmd += " --ethSrc " + str( ethSrc ) |
| 1830 | if ethDst: |
| 1831 | cmd += " --ethDst " + str( ethDst ) |
| 1832 | if bandwidth: |
| 1833 | cmd += " --bandwidth " + str( bandwidth ) |
| 1834 | if lambdaAlloc: |
| 1835 | cmd += " --lambda " |
| 1836 | if ipProto: |
| 1837 | cmd += " --ipProto " + str( ipProto ) |
| 1838 | if ipSrc: |
| 1839 | cmd += " --ipSrc " + str( ipSrc ) |
| 1840 | if ipDst: |
| 1841 | cmd += " --ipDst " + str( ipDst ) |
| 1842 | if tcpSrc: |
| 1843 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1844 | if tcpDst: |
| 1845 | cmd += " --tcpDst " + str( tcpDst ) |
| 1846 | if setEthSrc: |
| 1847 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1848 | if setEthDst: |
| 1849 | cmd += " --setEthDst " + str( setEthDst ) |
| 1850 | if vlanId: |
| 1851 | cmd += " -v " + str( vlanId ) |
| 1852 | if setVlan: |
| 1853 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1854 | if partial: |
| 1855 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1856 | if encap: |
| 1857 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1858 | |
| 1859 | # Check whether the user appended the port |
| 1860 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1861 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1862 | if "/" in ingressDevice: |
| 1863 | cmd += " " + str( ingressDevice ) |
| 1864 | else: |
| 1865 | if not portIngress: |
| 1866 | main.log.error( "You must specify " + |
| 1867 | "the Ingress port" ) |
| 1868 | return main.FALSE |
| 1869 | |
| 1870 | cmd += " " +\ |
| 1871 | str( ingressDevice ) + "/" +\ |
| 1872 | str( portIngress ) |
| 1873 | |
| 1874 | if portEgressList is None: |
| 1875 | for egressDevice in egressDeviceList: |
| 1876 | if "/" in egressDevice: |
| 1877 | cmd += " " + str( egressDevice ) |
| 1878 | else: |
| 1879 | main.log.error( "You must specify " + |
| 1880 | "the egress port" ) |
| 1881 | # TODO: perhaps more meaningful return |
| 1882 | return main.FALSE |
| 1883 | else: |
| 1884 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1885 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1886 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1887 | cmd += " " + \ |
| 1888 | str( egressDevice ) + "/" +\ |
| 1889 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1890 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1891 | main.log.error( "Device list and port list does not " + |
| 1892 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1893 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1894 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1895 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1896 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1897 | # If error, return error message |
| 1898 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1899 | main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " + |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1900 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1901 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1902 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1903 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1904 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1905 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1906 | else: |
| 1907 | main.log.error( "Error, intent ID not found" ) |
| 1908 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1909 | except AssertionError: |
| 1910 | main.log.exception( "" ) |
| 1911 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1912 | except TypeError: |
| 1913 | main.log.exception( self.name + ": Object not as expected" ) |
| 1914 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1915 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1916 | main.log.error( self.name + ": EOF exception found" ) |
| 1917 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1918 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1919 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1920 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1921 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1922 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1923 | def addMplsIntent( |
| 1924 | self, |
| 1925 | ingressDevice, |
| 1926 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1927 | ingressPort="", |
| 1928 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1929 | ethType="", |
| 1930 | ethSrc="", |
| 1931 | ethDst="", |
| 1932 | bandwidth="", |
| 1933 | lambdaAlloc=False, |
| 1934 | ipProto="", |
| 1935 | ipSrc="", |
| 1936 | ipDst="", |
| 1937 | tcpSrc="", |
| 1938 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1939 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1940 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1941 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1942 | """ |
| 1943 | Required: |
| 1944 | * ingressDevice: device id of ingress device |
| 1945 | * egressDevice: device id of egress device |
| 1946 | Optional: |
| 1947 | * ethType: specify ethType |
| 1948 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1949 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1950 | * bandwidth: specify bandwidth capacity of link |
| 1951 | * lambdaAlloc: if True, intent will allocate lambda |
| 1952 | for the specified intent |
| 1953 | * ipProto: specify ip protocol |
| 1954 | * ipSrc: specify ip source address |
| 1955 | * ipDst: specify ip destination address |
| 1956 | * tcpSrc: specify tcp source port |
| 1957 | * tcpDst: specify tcp destination port |
| 1958 | * ingressLabel: Ingress MPLS label |
| 1959 | * egressLabel: Egress MPLS label |
| 1960 | Description: |
| 1961 | Adds MPLS intent by |
| 1962 | specifying device id's and optional fields |
| 1963 | Returns: |
| 1964 | A string of the intent id or None on error |
| 1965 | |
| 1966 | NOTE: This function may change depending on the |
| 1967 | options developers provide for MPLS |
| 1968 | intent via cli |
| 1969 | """ |
| 1970 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1971 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1972 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1973 | if ethType: |
| 1974 | cmd += " --ethType " + str( ethType ) |
| 1975 | if ethSrc: |
| 1976 | cmd += " --ethSrc " + str( ethSrc ) |
| 1977 | if ethDst: |
| 1978 | cmd += " --ethDst " + str( ethDst ) |
| 1979 | if bandwidth: |
| 1980 | cmd += " --bandwidth " + str( bandwidth ) |
| 1981 | if lambdaAlloc: |
| 1982 | cmd += " --lambda " |
| 1983 | if ipProto: |
| 1984 | cmd += " --ipProto " + str( ipProto ) |
| 1985 | if ipSrc: |
| 1986 | cmd += " --ipSrc " + str( ipSrc ) |
| 1987 | if ipDst: |
| 1988 | cmd += " --ipDst " + str( ipDst ) |
| 1989 | if tcpSrc: |
| 1990 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1991 | if tcpDst: |
| 1992 | cmd += " --tcpDst " + str( tcpDst ) |
| 1993 | if ingressLabel: |
| 1994 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1995 | if egressLabel: |
| 1996 | cmd += " --egressLabel " + str( egressLabel ) |
| 1997 | if priority: |
| 1998 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1999 | |
| 2000 | # Check whether the user appended the port |
| 2001 | # or provided it as an input |
| 2002 | if "/" in ingressDevice: |
| 2003 | cmd += " " + str( ingressDevice ) |
| 2004 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2005 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2006 | main.log.error( "You must specify the ingress port" ) |
| 2007 | return None |
| 2008 | |
| 2009 | cmd += " " + \ |
| 2010 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2011 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2012 | |
| 2013 | if "/" in egressDevice: |
| 2014 | cmd += " " + str( egressDevice ) |
| 2015 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2016 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2017 | main.log.error( "You must specify the egress port" ) |
| 2018 | return None |
| 2019 | |
| 2020 | cmd += " " +\ |
| 2021 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2022 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2023 | |
| 2024 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2025 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2026 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2027 | # If error, return error message |
| 2028 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2029 | main.log.error( self.name + ": Error in adding mpls intent" ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2030 | return None |
| 2031 | else: |
| 2032 | # TODO: print out all the options in this message? |
| 2033 | main.log.info( "MPLS intent installed between " + |
| 2034 | str( ingressDevice ) + " and " + |
| 2035 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2036 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2037 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2038 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2039 | else: |
| 2040 | main.log.error( "Error, intent ID not found" ) |
| 2041 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2042 | except AssertionError: |
| 2043 | main.log.exception( "" ) |
| 2044 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2045 | except TypeError: |
| 2046 | main.log.exception( self.name + ": Object not as expected" ) |
| 2047 | return None |
| 2048 | except pexpect.EOF: |
| 2049 | main.log.error( self.name + ": EOF exception found" ) |
| 2050 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2051 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2052 | except Exception: |
| 2053 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2054 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2055 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2056 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 2057 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2058 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2059 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2060 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2061 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2062 | -p or --purge: Purge the intent from the store after removal |
| 2063 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2064 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 2065 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2066 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2067 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2068 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2069 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2070 | if purge: |
| 2071 | cmdStr += " -p" |
| 2072 | if sync: |
| 2073 | cmdStr += " -s" |
| 2074 | |
| 2075 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2076 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2077 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2078 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2079 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2080 | main.log.error( self.name + ": Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2081 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2082 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2083 | # TODO: Should this be main.TRUE |
| 2084 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2085 | except AssertionError: |
| 2086 | main.log.exception( "" ) |
| 2087 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2088 | except TypeError: |
| 2089 | main.log.exception( self.name + ": Object not as expected" ) |
| 2090 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2091 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2092 | main.log.error( self.name + ": EOF exception found" ) |
| 2093 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2094 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2095 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2096 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2097 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2098 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2099 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2100 | """ |
| 2101 | Description: |
| 2102 | Remove all the intents |
| 2103 | Optional args:- |
| 2104 | -s or --sync: Waits for the removal before returning |
| 2105 | -p or --purge: Purge the intent from the store after removal |
| 2106 | Returns: |
| 2107 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2108 | main.FALSE; Returns None for exception |
| 2109 | """ |
| 2110 | try: |
| 2111 | cmdStr = "remove-intent" |
| 2112 | if purge: |
| 2113 | cmdStr += " -p" |
| 2114 | if sync: |
| 2115 | cmdStr += " -s" |
| 2116 | |
| 2117 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2118 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2119 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2120 | assert "Command not found:" not in handle, handle |
| 2121 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2122 | main.log.error( self.name + ": Error in removing intent" ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2123 | return main.FALSE |
| 2124 | else: |
| 2125 | return main.TRUE |
| 2126 | except AssertionError: |
| 2127 | main.log.exception( "" ) |
| 2128 | return None |
| 2129 | except TypeError: |
| 2130 | main.log.exception( self.name + ": Object not as expected" ) |
| 2131 | return None |
| 2132 | except pexpect.EOF: |
| 2133 | main.log.error( self.name + ": EOF exception found" ) |
| 2134 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2135 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2136 | except Exception: |
| 2137 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2138 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2139 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2140 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2141 | """ |
| 2142 | Purges all WITHDRAWN Intents |
| 2143 | """ |
| 2144 | try: |
| 2145 | cmdStr = "purge-intents" |
| 2146 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2147 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2148 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2149 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2150 | main.log.error( self.name + ": Error in purging intents" ) |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2151 | return main.FALSE |
| 2152 | else: |
| 2153 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2154 | except AssertionError: |
| 2155 | main.log.exception( "" ) |
| 2156 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2157 | except TypeError: |
| 2158 | main.log.exception( self.name + ": Object not as expected" ) |
| 2159 | return None |
| 2160 | except pexpect.EOF: |
| 2161 | main.log.error( self.name + ": EOF exception found" ) |
| 2162 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2163 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2164 | except Exception: |
| 2165 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2166 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2167 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2168 | def wipeout( self ): |
| 2169 | """ |
| 2170 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2171 | """ |
| 2172 | try: |
| 2173 | cmdStr = "wipe-out please" |
| 2174 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2175 | assert handle is not None, "Error in sendline" |
| 2176 | assert "Command not found:" not in handle, handle |
| 2177 | return main.TRUE |
| 2178 | except AssertionError: |
| 2179 | main.log.exception( "" ) |
| 2180 | return None |
| 2181 | except TypeError: |
| 2182 | main.log.exception( self.name + ": Object not as expected" ) |
| 2183 | return None |
| 2184 | except pexpect.EOF: |
| 2185 | main.log.error( self.name + ": EOF exception found" ) |
| 2186 | main.log.error( self.name + ": " + self.handle.before ) |
| 2187 | main.cleanAndExit() |
| 2188 | except Exception: |
| 2189 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2190 | main.cleanAndExit() |
| 2191 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2192 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2193 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2194 | NOTE: This method should be used after installing application: |
| 2195 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2196 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2197 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2198 | Description: |
| 2199 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2200 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2201 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2202 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2203 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2204 | cmdStr += " -j" |
| 2205 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2206 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2207 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2208 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2209 | except AssertionError: |
| 2210 | main.log.exception( "" ) |
| 2211 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2212 | except TypeError: |
| 2213 | main.log.exception( self.name + ": Object not as expected" ) |
| 2214 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2215 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2216 | main.log.error( self.name + ": EOF exception found" ) |
| 2217 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2218 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2219 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2220 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2221 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2222 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2223 | def ipv4RouteNumber( self ): |
| 2224 | """ |
| 2225 | NOTE: This method should be used after installing application: |
| 2226 | onos-app-sdnip |
| 2227 | Description: |
| 2228 | Obtain the total IPv4 routes number in the system |
| 2229 | """ |
| 2230 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2231 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2232 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2233 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2234 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2235 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2236 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2237 | except AssertionError: |
| 2238 | main.log.exception( "" ) |
| 2239 | return None |
| 2240 | except ( TypeError, ValueError ): |
| 2241 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2242 | return None |
| 2243 | except pexpect.EOF: |
| 2244 | main.log.error( self.name + ": EOF exception found" ) |
| 2245 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2246 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2247 | except Exception: |
| 2248 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2249 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2250 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2251 | # =============Function to check Bandwidth allocation======== |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2252 | def allocations( self, jsonFormat = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2253 | """ |
| 2254 | Description: |
| 2255 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2256 | """ |
| 2257 | try: |
| 2258 | cmdStr = "allocations" |
| 2259 | if jsonFormat: |
| 2260 | cmdStr += " -j" |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2261 | handle = self.sendline( cmdStr, timeout=300 ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2262 | assert handle is not None, "Error in sendline" |
| 2263 | assert "Command not found:" not in handle, handle |
| 2264 | return handle |
| 2265 | except AssertionError: |
| 2266 | main.log.exception( "" ) |
| 2267 | return None |
| 2268 | except ( TypeError, ValueError ): |
| 2269 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2270 | return None |
| 2271 | except pexpect.EOF: |
| 2272 | main.log.error( self.name + ": EOF exception found" ) |
| 2273 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2274 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2275 | except Exception: |
| 2276 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2277 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2278 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2279 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2280 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2281 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2282 | Obtain intents from the ONOS cli. |
| 2283 | Optional: |
| 2284 | * jsonFormat: Enable output formatting in json, default to True |
| 2285 | * summary: Whether only output the intent summary, defaults to False |
| 2286 | * type: Only output a certain type of intent. This options is valid |
| 2287 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2288 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2289 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2290 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2291 | if summary: |
| 2292 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2293 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2294 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2295 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2296 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2297 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2298 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2299 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2300 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2301 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2302 | intentType = "" |
| 2303 | # IF we want the summary of a specific intent type |
| 2304 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2305 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2306 | if intentType in jsonResult.keys(): |
| 2307 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2308 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2309 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2310 | return handle |
| 2311 | else: |
| 2312 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2313 | except AssertionError: |
| 2314 | main.log.exception( "" ) |
| 2315 | return None |
| 2316 | except ( TypeError, ValueError ): |
| 2317 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2318 | return None |
| 2319 | except pexpect.EOF: |
| 2320 | main.log.error( self.name + ": EOF exception found" ) |
| 2321 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2322 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2323 | except Exception: |
| 2324 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2325 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2326 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2327 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2328 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2329 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2330 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2331 | list of intent IDs. |
| 2332 | Parameters: |
| 2333 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2334 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2335 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2336 | 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] | 2337 | accepted. |
| 2338 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2339 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2340 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2341 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2342 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2343 | try: |
| 2344 | state = "State is Undefined" |
| 2345 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2346 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2347 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2348 | rawJson = intentsJson |
| 2349 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2350 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2351 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2352 | if intentsId == intent[ 'id' ]: |
| 2353 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2354 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2355 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2356 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2357 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2358 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2359 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2360 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2361 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2362 | for intent in parsedIntentsJson: |
| 2363 | if intentsId[ i ] == intent[ 'id' ]: |
| 2364 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2365 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2366 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2367 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2368 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2369 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2370 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2371 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2372 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2373 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2374 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2375 | except ( TypeError, ValueError ): |
| 2376 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2377 | return None |
| 2378 | except pexpect.EOF: |
| 2379 | main.log.error( self.name + ": EOF exception found" ) |
| 2380 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2381 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2382 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2383 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2384 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2385 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2386 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2387 | """ |
| 2388 | Description: |
| 2389 | Check intents state |
| 2390 | Required: |
| 2391 | intentsId - List of intents ID to be checked |
| 2392 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2393 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2394 | state in the list. |
| 2395 | *NOTE: You can pass in a list of expected state, |
| 2396 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2397 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2398 | Returns main.TRUE only if all intent are the same as expected states, |
| 2399 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2400 | """ |
| 2401 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2402 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2403 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2404 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2405 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2406 | intentsDict = [] |
| 2407 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2408 | if isinstance( intentsId, types.StringType ) \ |
| 2409 | and intent.get( 'id' ) == intentsId: |
| 2410 | intentsDict.append( intent ) |
| 2411 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2412 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2413 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2414 | |
| 2415 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2416 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2417 | "getting intents state" ) |
| 2418 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2419 | |
| 2420 | if isinstance( expectedState, types.StringType ): |
| 2421 | for intents in intentsDict: |
| 2422 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2423 | main.log.debug( self.name + " : Intent ID - " + |
| 2424 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2425 | " actual state = " + |
| 2426 | intents.get( 'state' ) |
| 2427 | + " does not equal expected state = " |
| 2428 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2429 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2430 | elif isinstance( expectedState, types.ListType ): |
| 2431 | for intents in intentsDict: |
| 2432 | if not any( state == intents.get( 'state' ) for state in |
| 2433 | expectedState ): |
| 2434 | main.log.debug( self.name + " : Intent ID - " + |
| 2435 | intents.get( 'id' ) + |
| 2436 | " actual state = " + |
| 2437 | intents.get( 'state' ) + |
| 2438 | " does not equal expected states = " |
| 2439 | + str( expectedState ) ) |
| 2440 | returnValue = main.FALSE |
| 2441 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2442 | if returnValue == main.TRUE: |
| 2443 | main.log.info( self.name + ": All " + |
| 2444 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2445 | " intents are in " + str( expectedState ) + |
| 2446 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2447 | return returnValue |
| 2448 | except TypeError: |
| 2449 | main.log.exception( self.name + ": Object not as expected" ) |
| 2450 | return None |
| 2451 | except pexpect.EOF: |
| 2452 | main.log.error( self.name + ": EOF exception found" ) |
| 2453 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2454 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2455 | except Exception: |
| 2456 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2457 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2458 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2459 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2460 | """ |
| 2461 | Description: |
| 2462 | Compare the allocated bandwidth with the given allocations |
| 2463 | Required: |
| 2464 | expectedAllocations - The expected ONOS output of the allocations command |
| 2465 | Return: |
| 2466 | Returns main.TRUE only if all intent are the same as expected states, |
| 2467 | otherwise returns main.FALSE. |
| 2468 | """ |
| 2469 | # FIXME: Convert these string comparisons to object comparisons |
| 2470 | try: |
| 2471 | returnValue = main.TRUE |
| 2472 | bandwidthFailed = False |
| 2473 | rawAlloc = self.allocations() |
| 2474 | expectedFormat = StringIO( expectedAllocations ) |
| 2475 | ONOSOutput = StringIO( rawAlloc ) |
| 2476 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2477 | str( expectedFormat ) ) ) |
| 2478 | |
| 2479 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2480 | actual = actual.rstrip() |
| 2481 | expected = expected.rstrip() |
| 2482 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2483 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2484 | marker1 = actual.find( 'allocated' ) |
| 2485 | m1 = actual[ :marker1 ] |
| 2486 | marker2 = expected.find( 'allocated' ) |
| 2487 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2488 | if m1 != m2: |
| 2489 | bandwidthFailed = True |
| 2490 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2491 | bandwidthFailed = True |
| 2492 | expectedFormat.close() |
| 2493 | ONOSOutput.close() |
| 2494 | |
| 2495 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2496 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2497 | returnValue = main.FALSE |
| 2498 | return returnValue |
| 2499 | except TypeError: |
| 2500 | main.log.exception( self.name + ": Object not as expected" ) |
| 2501 | return None |
| 2502 | except pexpect.EOF: |
| 2503 | main.log.error( self.name + ": EOF exception found" ) |
| 2504 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2505 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2506 | except Exception: |
| 2507 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2508 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2509 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2510 | def compareIntent( self, intentDict ): |
| 2511 | """ |
| 2512 | Description: |
| 2513 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2514 | Return: |
| 2515 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2516 | Arguments: |
| 2517 | intentDict: a dictionary which maps intent ids to intent states |
| 2518 | """ |
| 2519 | try: |
| 2520 | intentsRaw = self.intents() |
| 2521 | intentsJson = json.loads( intentsRaw ) |
| 2522 | intentDictONOS = {} |
| 2523 | for intent in intentsJson: |
| 2524 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2525 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2526 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2527 | 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] | 2528 | str( len( intentDict ) ) + " expected and " + |
| 2529 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2530 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2531 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2532 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2533 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2534 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2535 | else: |
| 2536 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2537 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2538 | " expected state is " + intentDict[ intentID ] + |
| 2539 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2540 | returnValue = main.FALSE |
| 2541 | intentDictONOS.pop( intentID ) |
| 2542 | if len( intentDictONOS ) > 0: |
| 2543 | returnValue = main.FALSE |
| 2544 | for intentID in intentDictONOS.keys(): |
| 2545 | 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] | 2546 | if returnValue == main.TRUE: |
| 2547 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2548 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2549 | except KeyError: |
| 2550 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2551 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2552 | except ( TypeError, ValueError ): |
| 2553 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2554 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2555 | except pexpect.EOF: |
| 2556 | main.log.error( self.name + ": EOF exception found" ) |
| 2557 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2558 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2559 | except Exception: |
| 2560 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2561 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2562 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2563 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2564 | """ |
| 2565 | Description: |
| 2566 | Check the number of installed intents. |
| 2567 | Optional: |
| 2568 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2569 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2570 | Return: |
| 2571 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2572 | , otherwise, returns main.FALSE. |
| 2573 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2574 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2575 | try: |
| 2576 | cmd = "intents -s -j" |
| 2577 | |
| 2578 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2579 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2580 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2581 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2582 | response = json.loads( response ) |
| 2583 | |
| 2584 | # get total and installed number, see if they are match |
| 2585 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2586 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2587 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2588 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2589 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2590 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2591 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2592 | return main.FALSE |
| 2593 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2594 | except ( TypeError, ValueError ): |
| 2595 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2596 | return None |
| 2597 | except pexpect.EOF: |
| 2598 | main.log.error( self.name + ": EOF exception found" ) |
| 2599 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2600 | if noExit: |
| 2601 | return main.FALSE |
| 2602 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2603 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2604 | except pexpect.TIMEOUT: |
| 2605 | main.log.error( self.name + ": ONOS timeout" ) |
| 2606 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2607 | except Exception: |
| 2608 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2609 | if noExit: |
| 2610 | return main.FALSE |
| 2611 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2612 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2613 | |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2614 | 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] | 2615 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2616 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2617 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2618 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2619 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2620 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2621 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2622 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2623 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2624 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2625 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2626 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2627 | cmdStr += " -n" |
| 2628 | cmdStr += " " + state |
| 2629 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2630 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2631 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2632 | assert "Command not found:" not in handle, handle |
| 2633 | if re.search( "Error:", handle ): |
| 2634 | main.log.error( self.name + ": flows() response: " + |
| 2635 | str( handle ) ) |
| 2636 | return handle |
| 2637 | except AssertionError: |
| 2638 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2639 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2640 | except TypeError: |
| 2641 | main.log.exception( self.name + ": Object not as expected" ) |
| 2642 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2643 | except pexpect.TIMEOUT: |
| 2644 | main.log.error( self.name + ": ONOS timeout" ) |
| 2645 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2646 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2647 | main.log.error( self.name + ": EOF exception found" ) |
| 2648 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2649 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2650 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2651 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2652 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2653 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2654 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2655 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2656 | count = int( count ) if count else 0 |
| 2657 | return count if ( count > min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2658 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2659 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2660 | """ |
| 2661 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2662 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2663 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2664 | if the count of those states is 0, which means all current flows |
| 2665 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2666 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2667 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2668 | Return: |
| 2669 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2670 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2671 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2672 | """ |
| 2673 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2674 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2675 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2676 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2677 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2678 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2679 | if rawFlows: |
| 2680 | # if we didn't get flows or flows function return None, we should return |
| 2681 | # main.Flase |
| 2682 | checkedStates.append( json.loads( rawFlows ) ) |
| 2683 | else: |
| 2684 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2685 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2686 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2687 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2688 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2689 | except TypeError: |
| 2690 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2691 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2692 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2693 | # We want to count PENDING_ADD if isPENDING is true |
| 2694 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2695 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2696 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2697 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2698 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2699 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2700 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2701 | except ( TypeError, ValueError ): |
| 2702 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2703 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2704 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2705 | except AssertionError: |
| 2706 | main.log.exception( "" ) |
| 2707 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2708 | except pexpect.TIMEOUT: |
| 2709 | main.log.error( self.name + ": ONOS timeout" ) |
| 2710 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2711 | except pexpect.EOF: |
| 2712 | main.log.error( self.name + ": EOF exception found" ) |
| 2713 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2714 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2715 | except Exception: |
| 2716 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2717 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2718 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2719 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2720 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2721 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2722 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2723 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2724 | a specific point-to-point intent definition |
| 2725 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2726 | * ingress: specify source dpid |
| 2727 | * egress: specify destination dpid |
| 2728 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2729 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2730 | * offset: the keyOffset is where the next batch of intents |
| 2731 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2732 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2733 | * getResponse: If set to True, function will return ONOS response. |
| 2734 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2735 | Returns: If failed to push test intents, it will returen None, |
| 2736 | if successful, return true. |
| 2737 | Timeout expection will return None, |
| 2738 | TypeError will return false |
| 2739 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2740 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2741 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2742 | if background: |
| 2743 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2744 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2745 | back = "" |
| 2746 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2747 | ingress, |
| 2748 | egress, |
| 2749 | batchSize, |
| 2750 | offset, |
| 2751 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2752 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2753 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2754 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2755 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2756 | if getResponse: |
| 2757 | return response |
| 2758 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2759 | # TODO: We should handle if there is failure in installation |
| 2760 | return main.TRUE |
| 2761 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2762 | except AssertionError: |
| 2763 | main.log.exception( "" ) |
| 2764 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2765 | except pexpect.TIMEOUT: |
| 2766 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2767 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2768 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2769 | main.log.error( self.name + ": EOF exception found" ) |
| 2770 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2771 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2772 | except TypeError: |
| 2773 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2774 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2775 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2776 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2777 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2778 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2779 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2780 | """ |
| 2781 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2782 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2783 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2784 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2785 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2786 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2787 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2788 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2789 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2790 | cmd = "flows -c added" |
| 2791 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2792 | if rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2793 | rawFlows = rawFlows.split( "\n" ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2794 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2795 | for l in rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2796 | totalFlows += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2797 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2798 | main.log.error( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2799 | return None |
| 2800 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2801 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2802 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2803 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2804 | return None |
| 2805 | except pexpect.EOF: |
| 2806 | main.log.error( self.name + ": EOF exception found" ) |
| 2807 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2808 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2809 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2810 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2811 | except pexpect.TIMEOUT: |
| 2812 | main.log.error( self.name + ": ONOS timeout" ) |
| 2813 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2814 | except Exception: |
| 2815 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2816 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2817 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2818 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2819 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2820 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2821 | """ |
| 2822 | Description: |
| 2823 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2824 | Optional: |
| 2825 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2826 | Return: |
| 2827 | The number of intents |
| 2828 | """ |
| 2829 | try: |
| 2830 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2831 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2832 | if response is None: |
| 2833 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2834 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2835 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2836 | except ( TypeError, ValueError ): |
| 2837 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2838 | return None |
| 2839 | except pexpect.EOF: |
| 2840 | main.log.error( self.name + ": EOF exception found" ) |
| 2841 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2842 | if noExit: |
| 2843 | return -1 |
| 2844 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2845 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2846 | except Exception: |
| 2847 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2848 | if noExit: |
| 2849 | return -1 |
| 2850 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2851 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2852 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2853 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2854 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2855 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2856 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2857 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2858 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2859 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2860 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2861 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2862 | cmdStr += " -j" |
| 2863 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2864 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2865 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2866 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2867 | except AssertionError: |
| 2868 | main.log.exception( "" ) |
| 2869 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2870 | except TypeError: |
| 2871 | main.log.exception( self.name + ": Object not as expected" ) |
| 2872 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2873 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2874 | main.log.error( self.name + ": EOF exception found" ) |
| 2875 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2876 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2877 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2878 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2879 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2880 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2881 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2882 | """ |
| 2883 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2884 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2885 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2886 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2887 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2888 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2889 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2890 | cmdStr += " -j" |
| 2891 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2892 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2893 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2894 | if handle: |
| 2895 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2896 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2897 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2898 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2899 | else: |
| 2900 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2901 | except AssertionError: |
| 2902 | main.log.exception( "" ) |
| 2903 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2904 | except TypeError: |
| 2905 | main.log.exception( self.name + ": Object not as expected" ) |
| 2906 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2907 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2908 | main.log.error( self.name + ": EOF exception found" ) |
| 2909 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2910 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2911 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2912 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2913 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2914 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2915 | # Wrapper functions **************** |
| 2916 | # Wrapper functions use existing driver |
| 2917 | # functions and extends their use case. |
| 2918 | # For example, we may use the output of |
| 2919 | # a normal driver function, and parse it |
| 2920 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2921 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2922 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2923 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2924 | Description: |
| 2925 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2926 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2927 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2928 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2929 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 2930 | if intentsStr is None: |
| 2931 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2932 | # Convert to a dictionary |
| 2933 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2934 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2935 | for intent in intents: |
| 2936 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2937 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2938 | except TypeError: |
| 2939 | main.log.exception( self.name + ": Object not as expected" ) |
| 2940 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2941 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2942 | main.log.error( self.name + ": EOF exception found" ) |
| 2943 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2944 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2945 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2946 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2947 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2948 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2949 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2950 | """ |
| 2951 | Determine the number of flow rules for the given device id that are |
| 2952 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2953 | Params: |
| 2954 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2955 | """ |
| 2956 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2957 | if core: |
| 2958 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2959 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2960 | else: |
| 2961 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2962 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 2963 | handle = self.lineCount( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2964 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2965 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2966 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2967 | except AssertionError: |
| 2968 | main.log.exception( "" ) |
| 2969 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2970 | except pexpect.EOF: |
| 2971 | main.log.error( self.name + ": EOF exception found" ) |
| 2972 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2973 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2974 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2975 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2976 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2977 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2978 | def groupAddedCount( self, deviceId, core=False ): |
| 2979 | """ |
| 2980 | Determine the number of group rules for the given device id that are |
| 2981 | in the added state |
| 2982 | Params: |
| 2983 | core: if True, only return the number of core groups added |
| 2984 | """ |
| 2985 | try: |
| 2986 | if core: |
| 2987 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 2988 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2989 | else: |
| 2990 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 2991 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 2992 | handle = self.lineCount( cmdStr ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2993 | assert handle is not None, "Error in sendline" |
| 2994 | assert "Command not found:" not in handle, handle |
| 2995 | return handle |
| 2996 | except AssertionError: |
| 2997 | main.log.exception( "" ) |
| 2998 | return None |
| 2999 | except pexpect.EOF: |
| 3000 | main.log.error( self.name + ": EOF exception found" ) |
| 3001 | main.log.error( self.name + ": " + self.handle.before ) |
| 3002 | main.cleanAndExit() |
| 3003 | except Exception: |
| 3004 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3005 | main.cleanAndExit() |
| 3006 | |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3007 | def addStaticRoute( self, subnet, intf): |
| 3008 | """ |
| 3009 | Adds a static route to onos. |
| 3010 | Params: |
| 3011 | subnet: The subnet reaching through this route |
| 3012 | intf: The interface this route is reachable through |
| 3013 | """ |
| 3014 | try: |
| 3015 | cmdStr = "route-add " + subnet + " " + intf |
| 3016 | handle = self.sendline( cmdStr ) |
| 3017 | assert handle is not None, "Error in sendline" |
| 3018 | assert "Command not found:" not in handle, handle |
| 3019 | return handle |
| 3020 | except AssertionError: |
| 3021 | main.log.exception( "" ) |
| 3022 | return None |
| 3023 | except pexpect.EOF: |
| 3024 | main.log.error( self.name + ": EOF exception found" ) |
| 3025 | main.log.error( self.name + ": " + self.handle.before ) |
| 3026 | main.cleanAndExit() |
| 3027 | except Exception: |
| 3028 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3029 | main.cleanAndExit() |
| 3030 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3031 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 3032 | """ |
| 3033 | Description: |
| 3034 | Check whether the number of groups for the given device id that |
| 3035 | are in ADDED state is bigger than minGroupCount. |
| 3036 | Required: |
| 3037 | * deviceId: device id to check the number of added group rules |
| 3038 | Optional: |
| 3039 | * minGroupCount: the number of groups to compare |
| 3040 | * core: if True, only check the number of core groups added |
| 3041 | * comparison: if 0, compare with greater than minFlowCount |
| 3042 | * if 1, compare with equal to minFlowCount |
| 3043 | Return: |
| 3044 | Returns the number of groups if it is bigger than minGroupCount, |
| 3045 | returns main.FALSE otherwise. |
| 3046 | """ |
| 3047 | count = self.groupAddedCount( deviceId, core ) |
| 3048 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3049 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3050 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 3051 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3052 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3053 | """ |
| 3054 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3055 | deviceId: Id of the device from which we retrieve groups |
| 3056 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3057 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3058 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3059 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 3060 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3061 | assert handle is not None, "Error in sendline" |
| 3062 | assert "Command not found:" not in handle, handle |
| 3063 | return handle |
| 3064 | except AssertionError: |
| 3065 | main.log.exception( "" ) |
| 3066 | return None |
| 3067 | except TypeError: |
| 3068 | main.log.exception( self.name + ": Object not as expected" ) |
| 3069 | return None |
| 3070 | except pexpect.EOF: |
| 3071 | main.log.error( self.name + ": EOF exception found" ) |
| 3072 | main.log.error( self.name + ": " + self.handle.before ) |
| 3073 | main.cleanAndExit() |
| 3074 | except Exception: |
| 3075 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3076 | main.cleanAndExit() |
| 3077 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3078 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3079 | """ |
| 3080 | Description: |
| 3081 | Check whether the number of flow rules for the given device id that |
| 3082 | are in ADDED state is bigger than minFlowCount. |
| 3083 | Required: |
| 3084 | * deviceId: device id to check the number of added flow rules |
| 3085 | Optional: |
| 3086 | * minFlowCount: the number of flow rules to compare |
| 3087 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3088 | * comparison: if 0, compare with greater than minFlowCount |
| 3089 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3090 | Return: |
| 3091 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 3092 | returns main.FALSE otherwise. |
| 3093 | """ |
| 3094 | count = self.flowAddedCount( deviceId, core ) |
| 3095 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3096 | main.log.debug( "found {} flows".format( count ) ) |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3097 | 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] | 3098 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3099 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3100 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3101 | Use 'devices' function to obtain list of all devices |
| 3102 | and parse the result to obtain a list of all device |
| 3103 | id's. Returns this list. Returns empty list if no |
| 3104 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3105 | List is ordered sequentially |
| 3106 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3107 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3108 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3109 | the ids. By obtaining the list of device ids on the fly, |
| 3110 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3111 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3112 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3113 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3114 | devicesStr = self.devices( jsonFormat=False ) |
| 3115 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3116 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3117 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3118 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3119 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3120 | |
| 3121 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3122 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3123 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3124 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3125 | # Split list further into arguments before and after string |
| 3126 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3127 | # append to idList |
| 3128 | for arg in tempList: |
| 3129 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3130 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3131 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3132 | except TypeError: |
| 3133 | main.log.exception( self.name + ": Object not as expected" ) |
| 3134 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3135 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3136 | main.log.error( self.name + ": EOF exception found" ) |
| 3137 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3138 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3139 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3140 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3141 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3142 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3143 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3144 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3145 | Uses 'nodes' function to obtain list of all nodes |
| 3146 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3147 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3148 | Returns: |
| 3149 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3150 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3151 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3152 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3153 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3154 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3155 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3156 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3157 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3158 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3159 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3160 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3161 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3162 | except ( TypeError, ValueError ): |
| 3163 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3164 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3165 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3166 | main.log.error( self.name + ": EOF exception found" ) |
| 3167 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3168 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3169 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3170 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3171 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3172 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3173 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3174 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3175 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3176 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3177 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3178 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3179 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3180 | return None |
| 3181 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3182 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3183 | rawDevices = self.devices() |
| 3184 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3185 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3186 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3187 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3188 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3189 | return device |
| 3190 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3191 | except ( TypeError, ValueError ): |
| 3192 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3193 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3194 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3195 | main.log.error( self.name + ": EOF exception found" ) |
| 3196 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3197 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3198 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3199 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3200 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3201 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3202 | def getTopology( self, topologyOutput ): |
| 3203 | """ |
| 3204 | Definition: |
| 3205 | Loads a json topology output |
| 3206 | Return: |
| 3207 | topology = current ONOS topology |
| 3208 | """ |
| 3209 | import json |
| 3210 | try: |
| 3211 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3212 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3213 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3214 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3215 | except ( TypeError, ValueError ): |
| 3216 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3217 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3218 | except pexpect.EOF: |
| 3219 | main.log.error( self.name + ": EOF exception found" ) |
| 3220 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3221 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3222 | except Exception: |
| 3223 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3224 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3225 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3226 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3227 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3228 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3229 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3230 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3231 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3232 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3233 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3234 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3235 | logLevel = level to log to. |
| 3236 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3237 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3238 | Returns: main.TRUE if the number of switches and links are correct, |
| 3239 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3240 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3241 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3242 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3243 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3244 | summary = self.summary() |
| 3245 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3246 | except ( TypeError, ValueError ): |
| 3247 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3248 | return main.ERROR |
| 3249 | try: |
| 3250 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3251 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3252 | return main.ERROR |
| 3253 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3254 | # Is the number of switches is what we expected |
| 3255 | devices = topology.get( 'devices', False ) |
| 3256 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3257 | nodes = summary.get( 'nodes', False ) |
| 3258 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3259 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3260 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3261 | # Is the number of links is what we expected |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3262 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3263 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 3264 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3265 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3266 | output = output + "The number of links and switches match "\ |
| 3267 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3268 | result = main.TRUE |
| 3269 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3270 | output = output + \ |
| 3271 | "The number of links and switches does not match " + \ |
| 3272 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3273 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3274 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 3275 | output = output + " (%i expected) " % int( numoswitch ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3276 | if int( numolink ) > 0: |
| 3277 | output = output + "and %i links " % int( links ) |
| 3278 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 3279 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3280 | output = output + "and %i controllers " % int( nodes ) |
| 3281 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3282 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3283 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3284 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3285 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3286 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3287 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3288 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3289 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3290 | main.log.error( self.name + ": EOF exception found" ) |
| 3291 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3292 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3293 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3294 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3295 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3296 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3297 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3298 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3299 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3300 | deviceId must be the id of a device as seen in the onos devices command |
| 3301 | 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] | 3302 | role must be either master, standby, or none |
| 3303 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3304 | Returns: |
| 3305 | main.TRUE or main.FALSE based on argument verification and |
| 3306 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3307 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3308 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3309 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3310 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3311 | cmdStr = "device-role " +\ |
| 3312 | str( deviceId ) + " " +\ |
| 3313 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3314 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3315 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3316 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3317 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3318 | if re.search( "Error", handle ): |
| 3319 | # end color output to escape any colours |
| 3320 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3321 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3322 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3323 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3324 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3325 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3326 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3327 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3328 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3329 | except AssertionError: |
| 3330 | main.log.exception( "" ) |
| 3331 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3332 | except TypeError: |
| 3333 | main.log.exception( self.name + ": Object not as expected" ) |
| 3334 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3335 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3336 | main.log.error( self.name + ": EOF exception found" ) |
| 3337 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3338 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3339 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3340 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3341 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3342 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3343 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3344 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3345 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3346 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3347 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3348 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3349 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3350 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3351 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3352 | cmdStr += " -j" |
| 3353 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3354 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3355 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3356 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3357 | except AssertionError: |
| 3358 | main.log.exception( "" ) |
| 3359 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3360 | except TypeError: |
| 3361 | main.log.exception( self.name + ": Object not as expected" ) |
| 3362 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3363 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3364 | main.log.error( self.name + ": EOF exception found" ) |
| 3365 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3366 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3367 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3368 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3369 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3370 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3371 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3372 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3373 | CLI command to get the current leader for the Election test application |
| 3374 | NOTE: Requires installation of the onos-app-election feature |
| 3375 | Returns: Node IP of the leader if one exists |
| 3376 | None if none exists |
| 3377 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3378 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3379 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3380 | cmdStr = "election-test-leader" |
| 3381 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3382 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3383 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3384 | # Leader |
| 3385 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3386 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3387 | nodeSearch = re.search( leaderPattern, response ) |
| 3388 | if nodeSearch: |
| 3389 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3390 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3391 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3392 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3393 | # no leader |
| 3394 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3395 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3396 | nullSearch = re.search( nullPattern, response ) |
| 3397 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3398 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3399 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3400 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3401 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3402 | main.log.error( self.name + ": Error in electionTestLeader on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3403 | ": " + "unexpected response" ) |
| 3404 | main.log.error( repr( response ) ) |
| 3405 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3406 | except AssertionError: |
| 3407 | main.log.exception( "" ) |
| 3408 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3409 | except TypeError: |
| 3410 | main.log.exception( self.name + ": Object not as expected" ) |
| 3411 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3412 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3413 | main.log.error( self.name + ": EOF exception found" ) |
| 3414 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3415 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3416 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3417 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3418 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3419 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3420 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3421 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3422 | CLI command to run for leadership of the Election test application. |
| 3423 | NOTE: Requires installation of the onos-app-election feature |
| 3424 | Returns: Main.TRUE on success |
| 3425 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3426 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3427 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3428 | cmdStr = "election-test-run" |
| 3429 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3430 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3431 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3432 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3433 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3434 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3435 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3436 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3437 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3438 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3439 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3440 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3441 | main.log.error( self.name + ": Error in electionTestRun on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3442 | ": " + "unexpected response" ) |
| 3443 | main.log.error( repr( response ) ) |
| 3444 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3445 | except AssertionError: |
| 3446 | main.log.exception( "" ) |
| 3447 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3448 | except TypeError: |
| 3449 | main.log.exception( self.name + ": Object not as expected" ) |
| 3450 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3451 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3452 | main.log.error( self.name + ": EOF exception found" ) |
| 3453 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3454 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3455 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3456 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3457 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3458 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3459 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3460 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3461 | * CLI command to withdraw the local node from leadership election for |
| 3462 | * the Election test application. |
| 3463 | #NOTE: Requires installation of the onos-app-election feature |
| 3464 | Returns: Main.TRUE on success |
| 3465 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3466 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3467 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3468 | cmdStr = "election-test-withdraw" |
| 3469 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3470 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3471 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3472 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3473 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3474 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3475 | if re.search( successPattern, response ): |
| 3476 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3477 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3478 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3479 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3480 | main.log.error( self.name + ": Error in electionTestWithdraw on " + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3481 | self.name + ": " + "unexpected response" ) |
| 3482 | main.log.error( repr( response ) ) |
| 3483 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3484 | except AssertionError: |
| 3485 | main.log.exception( "" ) |
| 3486 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3487 | except TypeError: |
| 3488 | main.log.exception( self.name + ": Object not as expected" ) |
| 3489 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3490 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3491 | main.log.error( self.name + ": EOF exception found" ) |
| 3492 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3493 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3494 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3495 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3496 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3497 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3498 | def getDevicePortsEnabledCount( self, dpid ): |
| 3499 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3500 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3501 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3502 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3503 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3504 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3505 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3506 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3507 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3508 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3509 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3510 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3511 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3512 | except AssertionError: |
| 3513 | main.log.exception( "" ) |
| 3514 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3515 | except TypeError: |
| 3516 | main.log.exception( self.name + ": Object not as expected" ) |
| 3517 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3518 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3519 | main.log.error( self.name + ": EOF exception found" ) |
| 3520 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3521 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3522 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3523 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3524 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3525 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3526 | def getDeviceLinksActiveCount( self, dpid ): |
| 3527 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3528 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3529 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3530 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3531 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3532 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3533 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3534 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3535 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3536 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3537 | main.log.error( self.name + ": Error in getting ports " ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3538 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3539 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3540 | except AssertionError: |
| 3541 | main.log.exception( "" ) |
| 3542 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3543 | except TypeError: |
| 3544 | main.log.exception( self.name + ": Object not as expected" ) |
| 3545 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3546 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3547 | main.log.error( self.name + ": EOF exception found" ) |
| 3548 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3549 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3550 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3551 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3552 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3553 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3554 | def getAllIntentIds( self ): |
| 3555 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3556 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3557 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3558 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3559 | cmdStr = "onos:intents | grep id=" |
| 3560 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3561 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3562 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3563 | if re.search( "Error", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3564 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3565 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3566 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3567 | except AssertionError: |
| 3568 | main.log.exception( "" ) |
| 3569 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3570 | except TypeError: |
| 3571 | main.log.exception( self.name + ": Object not as expected" ) |
| 3572 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3573 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3574 | main.log.error( self.name + ": EOF exception found" ) |
| 3575 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3576 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3577 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3578 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3579 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3580 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3581 | def intentSummary( self ): |
| 3582 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3583 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3584 | """ |
| 3585 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3586 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3587 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3588 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3589 | states.append( intent.get( 'state', None ) ) |
| 3590 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3591 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3592 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3593 | except ( TypeError, ValueError ): |
| 3594 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3595 | return None |
| 3596 | except pexpect.EOF: |
| 3597 | main.log.error( self.name + ": EOF exception found" ) |
| 3598 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3599 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3600 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3601 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3602 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3603 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3604 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3605 | """ |
| 3606 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3607 | Optional argument: |
| 3608 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3609 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3610 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3611 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3612 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3613 | cmdStr += " -j" |
| 3614 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3615 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3616 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3617 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3618 | except AssertionError: |
| 3619 | main.log.exception( "" ) |
| 3620 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3621 | except TypeError: |
| 3622 | main.log.exception( self.name + ": Object not as expected" ) |
| 3623 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3624 | except pexpect.EOF: |
| 3625 | main.log.error( self.name + ": EOF exception found" ) |
| 3626 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3627 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3628 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3629 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3630 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3631 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3632 | def leaderCandidates( self, jsonFormat=True ): |
| 3633 | """ |
| 3634 | Returns the output of the leaders -c command. |
| 3635 | Optional argument: |
| 3636 | * jsonFormat - boolean indicating if you want output in json |
| 3637 | """ |
| 3638 | try: |
| 3639 | cmdStr = "onos:leaders -c" |
| 3640 | if jsonFormat: |
| 3641 | cmdStr += " -j" |
| 3642 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3643 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3644 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3645 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3646 | except AssertionError: |
| 3647 | main.log.exception( "" ) |
| 3648 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3649 | except TypeError: |
| 3650 | main.log.exception( self.name + ": Object not as expected" ) |
| 3651 | return None |
| 3652 | except pexpect.EOF: |
| 3653 | main.log.error( self.name + ": EOF exception found" ) |
| 3654 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3655 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3656 | except Exception: |
| 3657 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3658 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3659 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3660 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3661 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3662 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3663 | topic parameter and an empty list if the topic doesn't exist |
| 3664 | If no leader is elected leader in the returned list will be "none" |
| 3665 | Returns None if there is a type error processing the json object |
| 3666 | """ |
| 3667 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3668 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3669 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3670 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3671 | assert "Command not found:" not in rawOutput, rawOutput |
| 3672 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3673 | results = [] |
| 3674 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3675 | if dict[ "topic" ] == topic: |
| 3676 | leader = dict[ "leader" ] |
| 3677 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3678 | results.append( leader ) |
| 3679 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3680 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3681 | except AssertionError: |
| 3682 | main.log.exception( "" ) |
| 3683 | return None |
| 3684 | except ( TypeError, ValueError ): |
| 3685 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3686 | return None |
| 3687 | except pexpect.EOF: |
| 3688 | main.log.error( self.name + ": EOF exception found" ) |
| 3689 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3690 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3691 | except Exception: |
| 3692 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3693 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3694 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3695 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3696 | """ |
| 3697 | Returns the output of the intent Pending map. |
| 3698 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3699 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3700 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3701 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3702 | cmdStr += " -j" |
| 3703 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3704 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3705 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3706 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3707 | except AssertionError: |
| 3708 | main.log.exception( "" ) |
| 3709 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3710 | except TypeError: |
| 3711 | main.log.exception( self.name + ": Object not as expected" ) |
| 3712 | return None |
| 3713 | except pexpect.EOF: |
| 3714 | main.log.error( self.name + ": EOF exception found" ) |
| 3715 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3716 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3717 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3718 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3719 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3720 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3721 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3722 | """ |
| 3723 | Returns the output of the raft partitions command for ONOS. |
| 3724 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3725 | # Sample JSON |
| 3726 | # { |
| 3727 | # "leader": "tcp://10.128.30.11:7238", |
| 3728 | # "members": [ |
| 3729 | # "tcp://10.128.30.11:7238", |
| 3730 | # "tcp://10.128.30.17:7238", |
| 3731 | # "tcp://10.128.30.13:7238", |
| 3732 | # ], |
| 3733 | # "name": "p1", |
| 3734 | # "term": 3 |
| 3735 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3736 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3737 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3738 | if candidates: |
| 3739 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3740 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3741 | cmdStr += " -j" |
| 3742 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3743 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3744 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3745 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3746 | except AssertionError: |
| 3747 | main.log.exception( "" ) |
| 3748 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3749 | except TypeError: |
| 3750 | main.log.exception( self.name + ": Object not as expected" ) |
| 3751 | return None |
| 3752 | except pexpect.EOF: |
| 3753 | main.log.error( self.name + ": EOF exception found" ) |
| 3754 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3755 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3756 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3757 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3758 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3759 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3760 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3761 | """ |
| 3762 | Returns the output of the apps command for ONOS. This command lists |
| 3763 | information about installed ONOS applications |
| 3764 | """ |
| 3765 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3766 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3767 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3768 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3769 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3770 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3771 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3772 | if summary: |
| 3773 | cmdStr += " -s" |
| 3774 | if active: |
| 3775 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3776 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3777 | cmdStr += " -j" |
| 3778 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3779 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3780 | assert "Command not found:" not in output, output |
| 3781 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3782 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3783 | # FIXME: look at specific exceptions/Errors |
| 3784 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3785 | main.log.exception( self.name + ": Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3786 | return None |
| 3787 | except TypeError: |
| 3788 | main.log.exception( self.name + ": Object not as expected" ) |
| 3789 | return None |
| 3790 | except pexpect.EOF: |
| 3791 | main.log.error( self.name + ": EOF exception found" ) |
| 3792 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3793 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3794 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3795 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3796 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3797 | |
You Wang | cdc51fe | 2018-08-12 17:14:56 -0700 | [diff] [blame] | 3798 | def appStatus( self, appName ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3799 | """ |
| 3800 | Uses the onos:apps cli command to return the status of an application. |
| 3801 | Returns: |
| 3802 | "ACTIVE" - If app is installed and activated |
| 3803 | "INSTALLED" - If app is installed and deactivated |
| 3804 | "UNINSTALLED" - If app is not installed |
| 3805 | None - on error |
| 3806 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3807 | try: |
| 3808 | if not isinstance( appName, types.StringType ): |
| 3809 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3810 | " a string" ) |
| 3811 | return None |
| 3812 | output = self.apps( jsonFormat=True ) |
| 3813 | appsJson = json.loads( output ) |
| 3814 | state = None |
| 3815 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3816 | if appName == app.get( 'name' ): |
| 3817 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3818 | break |
| 3819 | if state == "ACTIVE" or state == "INSTALLED": |
| 3820 | return state |
| 3821 | elif state is None: |
You Wang | 0d9f2c0 | 2018-08-10 14:56:32 -0700 | [diff] [blame] | 3822 | main.log.warn( "{} app not found".format( appName ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3823 | return "UNINSTALLED" |
| 3824 | elif state: |
| 3825 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3826 | str( state ) ) |
| 3827 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3828 | except ( TypeError, ValueError ): |
| 3829 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3830 | return None |
| 3831 | except pexpect.EOF: |
| 3832 | main.log.error( self.name + ": EOF exception found" ) |
| 3833 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3834 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3835 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3836 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3837 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3838 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3839 | def app( self, appName, option ): |
| 3840 | """ |
| 3841 | Interacts with the app command for ONOS. This command manages |
| 3842 | application inventory. |
| 3843 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3844 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3845 | # Validate argument types |
| 3846 | valid = True |
| 3847 | if not isinstance( appName, types.StringType ): |
| 3848 | main.log.error( self.name + ".app(): appName must be a " + |
| 3849 | "string" ) |
| 3850 | valid = False |
| 3851 | if not isinstance( option, types.StringType ): |
| 3852 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3853 | valid = False |
| 3854 | if not valid: |
| 3855 | return main.FALSE |
| 3856 | # Validate Option |
| 3857 | option = option.lower() |
| 3858 | # NOTE: Install may become a valid option |
| 3859 | if option == "activate": |
| 3860 | pass |
| 3861 | elif option == "deactivate": |
| 3862 | pass |
| 3863 | elif option == "uninstall": |
| 3864 | pass |
| 3865 | else: |
| 3866 | # Invalid option |
| 3867 | main.log.error( "The ONOS app command argument only takes " + |
| 3868 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3869 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3870 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3871 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3872 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3873 | assert output is not None, "Error in sendline" |
| 3874 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3875 | if "Error executing command" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3876 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3877 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3878 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3879 | elif "No such application" in output: |
| 3880 | main.log.error( "The application '" + appName + |
| 3881 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3882 | return main.FALSE |
| 3883 | elif "Command not found:" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3884 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3885 | str( output ) ) |
| 3886 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3887 | elif "Unsupported command:" in output: |
| 3888 | main.log.error( "Incorrect command given to 'app': " + |
| 3889 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3890 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3891 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3892 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3893 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3894 | except AssertionError: |
| 3895 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3896 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3897 | except TypeError: |
| 3898 | main.log.exception( self.name + ": Object not as expected" ) |
| 3899 | return main.ERROR |
| 3900 | except pexpect.EOF: |
| 3901 | main.log.error( self.name + ": EOF exception found" ) |
| 3902 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3903 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3904 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3905 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3906 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3907 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3908 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3909 | """ |
| 3910 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3911 | appName is the hierarchical app name, not the feature name |
| 3912 | If check is True, method will check the status of the app after the |
| 3913 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3914 | Returns main.TRUE if the command was successfully sent |
| 3915 | main.FALSE if the cli responded with an error or given |
| 3916 | incorrect input |
| 3917 | """ |
| 3918 | try: |
| 3919 | if not isinstance( appName, types.StringType ): |
| 3920 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3921 | " a string" ) |
| 3922 | return main.FALSE |
| 3923 | status = self.appStatus( appName ) |
| 3924 | if status == "INSTALLED": |
| 3925 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3926 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3927 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3928 | status = self.appStatus( appName ) |
| 3929 | if status == "ACTIVE": |
| 3930 | return main.TRUE |
| 3931 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3932 | main.log.debug( "The state of application " + |
| 3933 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3934 | time.sleep( 1 ) |
| 3935 | return main.FALSE |
| 3936 | else: # not 'check' or command didn't succeed |
| 3937 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3938 | elif status == "ACTIVE": |
| 3939 | return main.TRUE |
| 3940 | elif status == "UNINSTALLED": |
| 3941 | main.log.error( self.name + ": Tried to activate the " + |
| 3942 | "application '" + appName + "' which is not " + |
| 3943 | "installed." ) |
| 3944 | else: |
| 3945 | main.log.error( "Unexpected return value from appStatus: " + |
| 3946 | str( status ) ) |
| 3947 | return main.ERROR |
| 3948 | except TypeError: |
| 3949 | main.log.exception( self.name + ": Object not as expected" ) |
| 3950 | return main.ERROR |
| 3951 | except pexpect.EOF: |
| 3952 | main.log.error( self.name + ": EOF exception found" ) |
| 3953 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3954 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3955 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3956 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3957 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3958 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3959 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3960 | """ |
| 3961 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3962 | appName is the hierarchical app name, not the feature name |
| 3963 | If check is True, method will check the status of the app after the |
| 3964 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3965 | Returns main.TRUE if the command was successfully sent |
| 3966 | main.FALSE if the cli responded with an error or given |
| 3967 | incorrect input |
| 3968 | """ |
| 3969 | try: |
| 3970 | if not isinstance( appName, types.StringType ): |
| 3971 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3972 | "be a string" ) |
| 3973 | return main.FALSE |
| 3974 | status = self.appStatus( appName ) |
| 3975 | if status == "INSTALLED": |
| 3976 | return main.TRUE |
| 3977 | elif status == "ACTIVE": |
| 3978 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3979 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3980 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3981 | status = self.appStatus( appName ) |
| 3982 | if status == "INSTALLED": |
| 3983 | return main.TRUE |
| 3984 | else: |
| 3985 | time.sleep( 1 ) |
| 3986 | return main.FALSE |
| 3987 | else: # not check or command didn't succeed |
| 3988 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3989 | elif status == "UNINSTALLED": |
| 3990 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3991 | "application '" + appName + "' which is not " + |
| 3992 | "installed." ) |
| 3993 | return main.TRUE |
| 3994 | else: |
| 3995 | main.log.error( "Unexpected return value from appStatus: " + |
| 3996 | str( status ) ) |
| 3997 | return main.ERROR |
| 3998 | except TypeError: |
| 3999 | main.log.exception( self.name + ": Object not as expected" ) |
| 4000 | return main.ERROR |
| 4001 | except pexpect.EOF: |
| 4002 | main.log.error( self.name + ": EOF exception found" ) |
| 4003 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4004 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4005 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4006 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4007 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4008 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4009 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4010 | """ |
| 4011 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4012 | appName is the hierarchical app name, not the feature name |
| 4013 | If check is True, method will check the status of the app after the |
| 4014 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4015 | Returns main.TRUE if the command was successfully sent |
| 4016 | main.FALSE if the cli responded with an error or given |
| 4017 | incorrect input |
| 4018 | """ |
| 4019 | # TODO: check with Thomas about the state machine for apps |
| 4020 | try: |
| 4021 | if not isinstance( appName, types.StringType ): |
| 4022 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 4023 | "be a string" ) |
| 4024 | return main.FALSE |
| 4025 | status = self.appStatus( appName ) |
| 4026 | if status == "INSTALLED": |
| 4027 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4028 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4029 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4030 | status = self.appStatus( appName ) |
| 4031 | if status == "UNINSTALLED": |
| 4032 | return main.TRUE |
| 4033 | else: |
| 4034 | time.sleep( 1 ) |
| 4035 | return main.FALSE |
| 4036 | else: # not check or command didn't succeed |
| 4037 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4038 | elif status == "ACTIVE": |
| 4039 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 4040 | "application '" + appName + "' which is " + |
| 4041 | "currently active." ) |
| 4042 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4043 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4044 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4045 | status = self.appStatus( appName ) |
| 4046 | if status == "UNINSTALLED": |
| 4047 | return main.TRUE |
| 4048 | else: |
| 4049 | time.sleep( 1 ) |
| 4050 | return main.FALSE |
| 4051 | else: # not check or command didn't succeed |
| 4052 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4053 | elif status == "UNINSTALLED": |
| 4054 | return main.TRUE |
| 4055 | else: |
| 4056 | main.log.error( "Unexpected return value from appStatus: " + |
| 4057 | str( status ) ) |
| 4058 | return main.ERROR |
| 4059 | except TypeError: |
| 4060 | main.log.exception( self.name + ": Object not as expected" ) |
| 4061 | return main.ERROR |
| 4062 | except pexpect.EOF: |
| 4063 | main.log.error( self.name + ": EOF exception found" ) |
| 4064 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4065 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4066 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4067 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4068 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4069 | |
| 4070 | def appIDs( self, jsonFormat=True ): |
| 4071 | """ |
| 4072 | Show the mappings between app id and app names given by the 'app-ids' |
| 4073 | cli command |
| 4074 | """ |
| 4075 | try: |
| 4076 | cmdStr = "app-ids" |
| 4077 | if jsonFormat: |
| 4078 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4079 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4080 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4081 | assert "Command not found:" not in output, output |
| 4082 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4083 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4084 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4085 | main.log.exception( self.name + ": Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4086 | return None |
| 4087 | except TypeError: |
| 4088 | main.log.exception( self.name + ": Object not as expected" ) |
| 4089 | return None |
| 4090 | except pexpect.EOF: |
| 4091 | main.log.error( self.name + ": EOF exception found" ) |
| 4092 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4093 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4094 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4095 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4096 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4097 | |
| 4098 | def appToIDCheck( self ): |
| 4099 | """ |
| 4100 | This method will check that each application's ID listed in 'apps' is |
| 4101 | the same as the ID listed in 'app-ids'. The check will also check that |
| 4102 | there are no duplicate IDs issued. Note that an app ID should be |
| 4103 | a globaly unique numerical identifier for app/app-like features. Once |
| 4104 | an ID is registered, the ID is never freed up so that if an app is |
| 4105 | reinstalled it will have the same ID. |
| 4106 | |
| 4107 | Returns: main.TRUE if the check passes and |
| 4108 | main.FALSE if the check fails or |
| 4109 | main.ERROR if there is some error in processing the test |
| 4110 | """ |
| 4111 | try: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4112 | # Grab IDs |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4113 | rawJson = self.appIDs( jsonFormat=True ) |
| 4114 | if rawJson: |
| 4115 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4116 | else: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4117 | main.log.error( "app-ids returned nothing: " + repr( rawJson ) ) |
| 4118 | return main.FALSE |
| 4119 | |
| 4120 | # Grab Apps |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4121 | rawJson = self.apps( jsonFormat=True ) |
| 4122 | if rawJson: |
| 4123 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4124 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4125 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4126 | return main.FALSE |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4127 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4128 | result = main.TRUE |
| 4129 | for app in apps: |
| 4130 | appID = app.get( 'id' ) |
| 4131 | if appID is None: |
| 4132 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4133 | result = main.FALSE |
| 4134 | appName = app.get( 'name' ) |
| 4135 | if appName is None: |
| 4136 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4137 | result = main.FALSE |
| 4138 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4139 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4140 | if not current: # if ids doesn't have this id |
| 4141 | result = main.FALSE |
| 4142 | main.log.error( "'app-ids' does not have the ID for " + |
| 4143 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4144 | main.log.debug( "apps command returned: " + str( app ) + |
| 4145 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4146 | elif len( current ) > 1: |
| 4147 | # there is more than one app with this ID |
| 4148 | result = main.FALSE |
| 4149 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4150 | elif not current[ 0 ][ 'name' ] == appName: |
| 4151 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4152 | result = main.FALSE |
| 4153 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4154 | " registered under id:" + str( appID ) + |
| 4155 | " but 'apps' has " + str( appName ) ) |
| 4156 | else: |
| 4157 | pass # id and name match! |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4158 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4159 | # now make sure that app-ids has no duplicates |
| 4160 | idsList = [] |
| 4161 | namesList = [] |
| 4162 | for item in ids: |
| 4163 | idsList.append( item[ 'id' ] ) |
| 4164 | namesList.append( item[ 'name' ] ) |
| 4165 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4166 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4167 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4168 | + json.dumps( ids, |
| 4169 | sort_keys=True, |
| 4170 | indent=4, |
| 4171 | separators=( ',', ': ' ) ) ) |
| 4172 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4173 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4174 | except ( TypeError, ValueError ): |
| 4175 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4176 | return main.ERROR |
| 4177 | except pexpect.EOF: |
| 4178 | main.log.error( self.name + ": EOF exception found" ) |
| 4179 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4180 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4181 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4182 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4183 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4184 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4185 | def getCfg( self, component=None, propName=None, short=False, |
| 4186 | jsonFormat=True ): |
| 4187 | """ |
| 4188 | Get configuration settings from onos cli |
| 4189 | Optional arguments: |
| 4190 | component - Optionally only list configurations for a specific |
| 4191 | component. If None, all components with configurations |
| 4192 | are displayed. Case Sensitive string. |
| 4193 | propName - If component is specified, propName option will show |
| 4194 | only this specific configuration from that component. |
| 4195 | Case Sensitive string. |
| 4196 | jsonFormat - Returns output as json. Note that this will override |
| 4197 | the short option |
| 4198 | short - Short, less verbose, version of configurations. |
| 4199 | This is overridden by the json option |
| 4200 | returns: |
| 4201 | Output from cli as a string or None on error |
| 4202 | """ |
| 4203 | try: |
| 4204 | baseStr = "cfg" |
| 4205 | cmdStr = " get" |
| 4206 | componentStr = "" |
| 4207 | if component: |
| 4208 | componentStr += " " + component |
| 4209 | if propName: |
| 4210 | componentStr += " " + propName |
| 4211 | if jsonFormat: |
| 4212 | baseStr += " -j" |
| 4213 | elif short: |
| 4214 | baseStr += " -s" |
| 4215 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4216 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4217 | assert "Command not found:" not in output, output |
| 4218 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4219 | return output |
| 4220 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4221 | main.log.exception( self.name + ": Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4222 | return None |
| 4223 | except TypeError: |
| 4224 | main.log.exception( self.name + ": Object not as expected" ) |
| 4225 | return None |
| 4226 | except pexpect.EOF: |
| 4227 | main.log.error( self.name + ": EOF exception found" ) |
| 4228 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4229 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4230 | except Exception: |
| 4231 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4232 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4233 | |
| 4234 | def setCfg( self, component, propName, value=None, check=True ): |
| 4235 | """ |
| 4236 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4237 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4238 | component - The case sensitive name of the component whose |
| 4239 | property is to be set |
| 4240 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4241 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4242 | 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] | 4243 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4244 | check - Boolean, Check whether the option was successfully set this |
| 4245 | only applies when a value is given. |
| 4246 | returns: |
| 4247 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4248 | will return main.TRUE unless there is an error |
| 4249 | """ |
| 4250 | try: |
| 4251 | baseStr = "cfg" |
| 4252 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4253 | if value is not None: |
| 4254 | cmdStr += " " + str( value ) |
| 4255 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4256 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4257 | assert "Command not found:" not in output, output |
| 4258 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4259 | if value and check: |
| 4260 | results = self.getCfg( component=str( component ), |
| 4261 | propName=str( propName ), |
| 4262 | jsonFormat=True ) |
| 4263 | # Check if current value is what we just set |
| 4264 | try: |
| 4265 | jsonOutput = json.loads( results ) |
| 4266 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4267 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4268 | main.log.exception( "Error parsing cfg output" ) |
| 4269 | main.log.error( "output:" + repr( results ) ) |
| 4270 | return main.FALSE |
| 4271 | if current == str( value ): |
| 4272 | return main.TRUE |
| 4273 | return main.FALSE |
| 4274 | return main.TRUE |
| 4275 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4276 | main.log.exception( self.name + ": Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4277 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4278 | except ( TypeError, ValueError ): |
| 4279 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4280 | return main.FALSE |
| 4281 | except pexpect.EOF: |
| 4282 | main.log.error( self.name + ": EOF exception found" ) |
| 4283 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4284 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4285 | except Exception: |
| 4286 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4287 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4288 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4289 | def distPrimitivesSend( self, cmd ): |
| 4290 | """ |
| 4291 | Function to handle sending cli commands for the distributed primitives test app |
| 4292 | |
| 4293 | This command will catch some exceptions and retry the command on some |
| 4294 | specific store exceptions. |
| 4295 | |
| 4296 | Required arguments: |
| 4297 | cmd - The command to send to the cli |
| 4298 | returns: |
| 4299 | string containing the cli output |
| 4300 | None on Error |
| 4301 | """ |
| 4302 | try: |
| 4303 | output = self.sendline( cmd ) |
| 4304 | try: |
| 4305 | assert output is not None, "Error in sendline" |
| 4306 | # TODO: Maybe make this less hardcoded |
| 4307 | # ConsistentMap Exceptions |
| 4308 | assert "org.onosproject.store.service" not in output |
| 4309 | # Node not leader |
| 4310 | assert "java.lang.IllegalStateException" not in output |
| 4311 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4312 | main.log.error( self.name + ": Error in processing '" + cmd + "' " + |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4313 | "command: " + str( output ) ) |
| 4314 | retryTime = 30 # Conservative time, given by Madan |
| 4315 | main.log.info( "Waiting " + str( retryTime ) + |
| 4316 | "seconds before retrying." ) |
| 4317 | time.sleep( retryTime ) # Due to change in mastership |
| 4318 | output = self.sendline( cmd ) |
| 4319 | assert output is not None, "Error in sendline" |
| 4320 | assert "Command not found:" not in output, output |
| 4321 | assert "Error executing command" not in output, output |
| 4322 | main.log.info( self.name + ": " + output ) |
| 4323 | return output |
| 4324 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4325 | main.log.exception( self.name + ": Error in processing '" + cmd + "' command." ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4326 | return None |
| 4327 | except TypeError: |
| 4328 | main.log.exception( self.name + ": Object not as expected" ) |
| 4329 | return None |
| 4330 | except pexpect.EOF: |
| 4331 | main.log.error( self.name + ": EOF exception found" ) |
| 4332 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4333 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4334 | except Exception: |
| 4335 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4336 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4337 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4338 | def setTestAdd( self, setName, values ): |
| 4339 | """ |
| 4340 | CLI command to add elements to a distributed set. |
| 4341 | Arguments: |
| 4342 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4343 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4344 | Example usages: |
| 4345 | setTestAdd( "set1", "a b c" ) |
| 4346 | setTestAdd( "set2", "1" ) |
| 4347 | returns: |
| 4348 | main.TRUE on success OR |
| 4349 | main.FALSE if elements were already in the set OR |
| 4350 | main.ERROR on error |
| 4351 | """ |
| 4352 | try: |
| 4353 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4354 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4355 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4356 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4357 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4358 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4359 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4360 | return main.FALSE |
| 4361 | else: |
| 4362 | main.log.error( self.name + ": setTestAdd did not" + |
| 4363 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4364 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4365 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4366 | except TypeError: |
| 4367 | main.log.exception( self.name + ": Object not as expected" ) |
| 4368 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4369 | except Exception: |
| 4370 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4371 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4372 | |
| 4373 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4374 | """ |
| 4375 | CLI command to remove elements from a distributed set. |
| 4376 | Required arguments: |
| 4377 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4378 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4379 | Optional arguments: |
| 4380 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4381 | retain - Retain only the given values. (intersection of the |
| 4382 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4383 | returns: |
| 4384 | main.TRUE on success OR |
| 4385 | main.FALSE if the set was not changed OR |
| 4386 | main.ERROR on error |
| 4387 | """ |
| 4388 | try: |
| 4389 | cmdStr = "set-test-remove " |
| 4390 | if clear: |
| 4391 | cmdStr += "-c " + str( setName ) |
| 4392 | elif retain: |
| 4393 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4394 | else: |
| 4395 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4396 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4397 | if clear: |
| 4398 | pattern = "Set " + str( setName ) + " cleared" |
| 4399 | if re.search( pattern, output ): |
| 4400 | return main.TRUE |
| 4401 | elif retain: |
| 4402 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4403 | "only elements of set \[(.*)\]" |
| 4404 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4405 | "retaining only elements of the set " +\ |
| 4406 | "\[(.*)\]" |
| 4407 | if re.search( positivePattern, output ): |
| 4408 | return main.TRUE |
| 4409 | elif re.search( negativePattern, output ): |
| 4410 | return main.FALSE |
| 4411 | else: |
| 4412 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4413 | str( setName ) |
| 4414 | if ( len( values.split() ) == 1 ): |
| 4415 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4416 | str( setName ) |
| 4417 | else: |
| 4418 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4419 | str( setName ) |
| 4420 | if re.search( positivePattern, output ): |
| 4421 | return main.TRUE |
| 4422 | elif re.search( negativePattern, output ): |
| 4423 | return main.FALSE |
| 4424 | main.log.error( self.name + ": setTestRemove did not" + |
| 4425 | " match expected output" ) |
| 4426 | main.log.debug( self.name + " expected: " + pattern ) |
| 4427 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4428 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4429 | except TypeError: |
| 4430 | main.log.exception( self.name + ": Object not as expected" ) |
| 4431 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4432 | except Exception: |
| 4433 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4434 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4435 | |
| 4436 | def setTestGet( self, setName, values="" ): |
| 4437 | """ |
| 4438 | CLI command to get the elements in a distributed set. |
| 4439 | Required arguments: |
| 4440 | setName - The name of the set to remove from. |
| 4441 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4442 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4443 | returns: |
| 4444 | main.ERROR on error OR |
| 4445 | A list of elements in the set if no optional arguments are |
| 4446 | supplied OR |
| 4447 | A tuple containing the list then: |
| 4448 | main.FALSE if the given values are not in the set OR |
| 4449 | main.TRUE if the given values are in the set OR |
| 4450 | """ |
| 4451 | try: |
| 4452 | values = str( values ).strip() |
| 4453 | setName = str( setName ).strip() |
| 4454 | length = len( values.split() ) |
| 4455 | containsCheck = None |
| 4456 | # Patterns to match |
| 4457 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4458 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4459 | containsTrue = "Set " + setName + " contains the value " + values |
| 4460 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4461 | values |
| 4462 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4463 | setPattern |
| 4464 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4465 | " subset " + setPattern |
| 4466 | |
| 4467 | cmdStr = "set-test-get " |
| 4468 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4469 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4470 | if length == 0: |
| 4471 | match = re.search( pattern, output ) |
| 4472 | else: # if given values |
| 4473 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4474 | patternTrue = pattern + "\r\n" + containsTrue |
| 4475 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4476 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4477 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4478 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4479 | matchTrue = re.search( patternTrue, output ) |
| 4480 | matchFalse = re.search( patternFalse, output ) |
| 4481 | if matchTrue: |
| 4482 | containsCheck = main.TRUE |
| 4483 | match = matchTrue |
| 4484 | elif matchFalse: |
| 4485 | containsCheck = main.FALSE |
| 4486 | match = matchFalse |
| 4487 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4488 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4489 | "expected output" ) |
| 4490 | main.log.debug( self.name + " expected: " + pattern ) |
| 4491 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4492 | match = None |
| 4493 | if match: |
| 4494 | setMatch = match.group( 1 ) |
| 4495 | if setMatch == '': |
| 4496 | setList = [] |
| 4497 | else: |
| 4498 | setList = setMatch.split( ", " ) |
| 4499 | if length > 0: |
| 4500 | return ( setList, containsCheck ) |
| 4501 | else: |
| 4502 | return setList |
| 4503 | else: # no match |
| 4504 | main.log.error( self.name + ": setTestGet did not" + |
| 4505 | " match expected output" ) |
| 4506 | main.log.debug( self.name + " expected: " + pattern ) |
| 4507 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4508 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4509 | except TypeError: |
| 4510 | main.log.exception( self.name + ": Object not as expected" ) |
| 4511 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4512 | except Exception: |
| 4513 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4514 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4515 | |
| 4516 | def setTestSize( self, setName ): |
| 4517 | """ |
| 4518 | CLI command to get the elements in a distributed set. |
| 4519 | Required arguments: |
| 4520 | setName - The name of the set to remove from. |
| 4521 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4522 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4523 | None on error |
| 4524 | """ |
| 4525 | try: |
| 4526 | # TODO: Should this check against the number of elements returned |
| 4527 | # and then return true/false based on that? |
| 4528 | setName = str( setName ).strip() |
| 4529 | # Patterns to match |
| 4530 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4531 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4532 | setPattern |
| 4533 | cmdStr = "set-test-get -s " |
| 4534 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4535 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4536 | if output: |
| 4537 | match = re.search( pattern, output ) |
| 4538 | if match: |
| 4539 | setSize = int( match.group( 1 ) ) |
| 4540 | setMatch = match.group( 2 ) |
| 4541 | if len( setMatch.split() ) == setSize: |
| 4542 | main.log.info( "The size returned by " + self.name + |
| 4543 | " matches the number of elements in " + |
| 4544 | "the returned set" ) |
| 4545 | else: |
| 4546 | main.log.error( "The size returned by " + self.name + |
| 4547 | " does not match the number of " + |
| 4548 | "elements in the returned set." ) |
| 4549 | return setSize |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4550 | else: # no match |
| 4551 | main.log.error( self.name + ": setTestGet did not" + |
| 4552 | " match expected output" ) |
| 4553 | main.log.debug( self.name + " expected: " + pattern ) |
| 4554 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4555 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4556 | except TypeError: |
| 4557 | main.log.exception( self.name + ": Object not as expected" ) |
| 4558 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4559 | except Exception: |
| 4560 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4561 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4562 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4563 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4564 | """ |
| 4565 | Command to list the various counters in the system. |
| 4566 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4567 | if jsonFormat, a string of the json object returned by the cli |
| 4568 | command |
| 4569 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4570 | None on error |
| 4571 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4572 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4573 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4574 | if jsonFormat: |
| 4575 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4576 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4577 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4578 | assert "Command not found:" not in output, output |
| 4579 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4580 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4581 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4582 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4583 | main.log.exception( self.name + ": Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4584 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4585 | except TypeError: |
| 4586 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4587 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4588 | except pexpect.EOF: |
| 4589 | main.log.error( self.name + ": EOF exception found" ) |
| 4590 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4591 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4592 | except Exception: |
| 4593 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4594 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4595 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4596 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4597 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4598 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4599 | Required arguments: |
| 4600 | counter - The name of the counter to increment. |
| 4601 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4602 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4603 | returns: |
| 4604 | integer value of the counter or |
| 4605 | None on Error |
| 4606 | """ |
| 4607 | try: |
| 4608 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4609 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4610 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4611 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4612 | if delta != 1: |
| 4613 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4614 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4615 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4616 | match = re.search( pattern, output ) |
| 4617 | if match: |
| 4618 | return int( match.group( 1 ) ) |
| 4619 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4620 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4621 | " match expected output." ) |
| 4622 | main.log.debug( self.name + " expected: " + pattern ) |
| 4623 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4624 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4625 | except TypeError: |
| 4626 | main.log.exception( self.name + ": Object not as expected" ) |
| 4627 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4628 | except Exception: |
| 4629 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4630 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4631 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4632 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4633 | """ |
| 4634 | CLI command to get a distributed counter then add a delta to it. |
| 4635 | Required arguments: |
| 4636 | counter - The name of the counter to increment. |
| 4637 | Optional arguments: |
| 4638 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4639 | returns: |
| 4640 | integer value of the counter or |
| 4641 | None on Error |
| 4642 | """ |
| 4643 | try: |
| 4644 | counter = str( counter ) |
| 4645 | delta = int( delta ) |
| 4646 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4647 | cmdStr += counter |
| 4648 | if delta != 1: |
| 4649 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4650 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4651 | pattern = counter + " was updated to (-?\d+)" |
| 4652 | match = re.search( pattern, output ) |
| 4653 | if match: |
| 4654 | return int( match.group( 1 ) ) |
| 4655 | else: |
| 4656 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4657 | " match expected output." ) |
| 4658 | main.log.debug( self.name + " expected: " + pattern ) |
| 4659 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4660 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4661 | except TypeError: |
| 4662 | main.log.exception( self.name + ": Object not as expected" ) |
| 4663 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4664 | except Exception: |
| 4665 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4666 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4667 | |
| 4668 | def valueTestGet( self, valueName ): |
| 4669 | """ |
| 4670 | CLI command to get the value of an atomic value. |
| 4671 | Required arguments: |
| 4672 | valueName - The name of the value to get. |
| 4673 | returns: |
| 4674 | string value of the value or |
| 4675 | None on Error |
| 4676 | """ |
| 4677 | try: |
| 4678 | valueName = str( valueName ) |
| 4679 | cmdStr = "value-test " |
| 4680 | operation = "get" |
| 4681 | cmdStr = "value-test {} {}".format( valueName, |
| 4682 | operation ) |
| 4683 | output = self.distPrimitivesSend( cmdStr ) |
| 4684 | pattern = "(\w+)" |
| 4685 | match = re.search( pattern, output ) |
| 4686 | if match: |
| 4687 | return match.group( 1 ) |
| 4688 | else: |
| 4689 | main.log.error( self.name + ": valueTestGet did not" + |
| 4690 | " match expected output." ) |
| 4691 | main.log.debug( self.name + " expected: " + pattern ) |
| 4692 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4693 | return None |
| 4694 | except TypeError: |
| 4695 | main.log.exception( self.name + ": Object not as expected" ) |
| 4696 | return None |
| 4697 | except Exception: |
| 4698 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4699 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4700 | |
| 4701 | def valueTestSet( self, valueName, newValue ): |
| 4702 | """ |
| 4703 | CLI command to set the value of an atomic value. |
| 4704 | Required arguments: |
| 4705 | valueName - The name of the value to set. |
| 4706 | newValue - The value to assign to the given value. |
| 4707 | returns: |
| 4708 | main.TRUE on success or |
| 4709 | main.ERROR on Error |
| 4710 | """ |
| 4711 | try: |
| 4712 | valueName = str( valueName ) |
| 4713 | newValue = str( newValue ) |
| 4714 | operation = "set" |
| 4715 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4716 | operation, |
| 4717 | newValue ) |
| 4718 | output = self.distPrimitivesSend( cmdStr ) |
| 4719 | if output is not None: |
| 4720 | return main.TRUE |
| 4721 | else: |
| 4722 | return main.ERROR |
| 4723 | except TypeError: |
| 4724 | main.log.exception( self.name + ": Object not as expected" ) |
| 4725 | return main.ERROR |
| 4726 | except Exception: |
| 4727 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4728 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4729 | |
| 4730 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4731 | """ |
| 4732 | CLI command to compareAndSet the value of an atomic value. |
| 4733 | Required arguments: |
| 4734 | valueName - The name of the value. |
| 4735 | oldValue - Compare the current value of the atomic value to this |
| 4736 | newValue - If the value equals oldValue, set the value to newValue |
| 4737 | returns: |
| 4738 | main.TRUE on success or |
| 4739 | main.FALSE on failure or |
| 4740 | main.ERROR on Error |
| 4741 | """ |
| 4742 | try: |
| 4743 | valueName = str( valueName ) |
| 4744 | oldValue = str( oldValue ) |
| 4745 | newValue = str( newValue ) |
| 4746 | operation = "compareAndSet" |
| 4747 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4748 | operation, |
| 4749 | oldValue, |
| 4750 | newValue ) |
| 4751 | output = self.distPrimitivesSend( cmdStr ) |
| 4752 | pattern = "(\w+)" |
| 4753 | match = re.search( pattern, output ) |
| 4754 | if match: |
| 4755 | result = match.group( 1 ) |
| 4756 | if result == "true": |
| 4757 | return main.TRUE |
| 4758 | elif result == "false": |
| 4759 | return main.FALSE |
| 4760 | else: |
| 4761 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4762 | " match expected output." ) |
| 4763 | main.log.debug( self.name + " expected: " + pattern ) |
| 4764 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4765 | return main.ERROR |
| 4766 | except TypeError: |
| 4767 | main.log.exception( self.name + ": Object not as expected" ) |
| 4768 | return main.ERROR |
| 4769 | except Exception: |
| 4770 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4771 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4772 | |
| 4773 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4774 | """ |
| 4775 | CLI command to getAndSet the value of an atomic value. |
| 4776 | Required arguments: |
| 4777 | valueName - The name of the value to get. |
| 4778 | newValue - The value to assign to the given value |
| 4779 | returns: |
| 4780 | string value of the value or |
| 4781 | None on Error |
| 4782 | """ |
| 4783 | try: |
| 4784 | valueName = str( valueName ) |
| 4785 | cmdStr = "value-test " |
| 4786 | operation = "getAndSet" |
| 4787 | cmdStr += valueName + " " + operation |
| 4788 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4789 | operation, |
| 4790 | newValue ) |
| 4791 | output = self.distPrimitivesSend( cmdStr ) |
| 4792 | pattern = "(\w+)" |
| 4793 | match = re.search( pattern, output ) |
| 4794 | if match: |
| 4795 | return match.group( 1 ) |
| 4796 | else: |
| 4797 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4798 | " match expected output." ) |
| 4799 | main.log.debug( self.name + " expected: " + pattern ) |
| 4800 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4801 | return None |
| 4802 | except TypeError: |
| 4803 | main.log.exception( self.name + ": Object not as expected" ) |
| 4804 | return None |
| 4805 | except Exception: |
| 4806 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4807 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4808 | |
| 4809 | def valueTestDestroy( self, valueName ): |
| 4810 | """ |
| 4811 | CLI command to destroy an atomic value. |
| 4812 | Required arguments: |
| 4813 | valueName - The name of the value to destroy. |
| 4814 | returns: |
| 4815 | main.TRUE on success or |
| 4816 | main.ERROR on Error |
| 4817 | """ |
| 4818 | try: |
| 4819 | valueName = str( valueName ) |
| 4820 | cmdStr = "value-test " |
| 4821 | operation = "destroy" |
| 4822 | cmdStr += valueName + " " + operation |
| 4823 | output = self.distPrimitivesSend( cmdStr ) |
| 4824 | if output is not None: |
| 4825 | return main.TRUE |
| 4826 | else: |
| 4827 | return main.ERROR |
| 4828 | except TypeError: |
| 4829 | main.log.exception( self.name + ": Object not as expected" ) |
| 4830 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4831 | except Exception: |
| 4832 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4833 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4834 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4835 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4836 | """ |
| 4837 | Description: Execute summary command in onos |
| 4838 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4839 | no output |
| 4840 | |
| 4841 | """ |
| 4842 | try: |
| 4843 | cmdStr = "summary" |
| 4844 | if jsonFormat: |
| 4845 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4846 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4847 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4848 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4849 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 4850 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4851 | if not handle: |
| 4852 | main.log.error( self.name + ": There is no output in " + |
| 4853 | "summary command" ) |
| 4854 | return main.FALSE |
| 4855 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4856 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4857 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4858 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4859 | except TypeError: |
| 4860 | main.log.exception( self.name + ": Object not as expected" ) |
| 4861 | return None |
| 4862 | except pexpect.EOF: |
| 4863 | main.log.error( self.name + ": EOF exception found" ) |
| 4864 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4865 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4866 | except Exception: |
| 4867 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4868 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4869 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4870 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4871 | """ |
| 4872 | CLI command to get the value of a key in a consistent map using |
| 4873 | transactions. This a test function and can only get keys from the |
| 4874 | test map hard coded into the cli command |
| 4875 | Required arguments: |
| 4876 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4877 | returns: |
| 4878 | The string value of the key or |
| 4879 | None on Error |
| 4880 | """ |
| 4881 | try: |
| 4882 | keyName = str( keyName ) |
| 4883 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4884 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4885 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4886 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4887 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4888 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4889 | return None |
| 4890 | else: |
| 4891 | match = re.search( pattern, output ) |
| 4892 | if match: |
| 4893 | return match.groupdict()[ 'value' ] |
| 4894 | else: |
| 4895 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4896 | " match expected output." ) |
| 4897 | main.log.debug( self.name + " expected: " + pattern ) |
| 4898 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4899 | return None |
| 4900 | except TypeError: |
| 4901 | main.log.exception( self.name + ": Object not as expected" ) |
| 4902 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4903 | except Exception: |
| 4904 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4905 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4906 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4907 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4908 | """ |
| 4909 | CLI command to put a value into 'numKeys' number of keys in a |
| 4910 | consistent map using transactions. This a test function and can only |
| 4911 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4912 | Required arguments: |
| 4913 | numKeys - Number of keys to add the value to |
| 4914 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4915 | returns: |
| 4916 | A dictionary whose keys are the name of the keys put into the map |
| 4917 | and the values of the keys are dictionaries whose key-values are |
| 4918 | 'value': value put into map and optionaly |
| 4919 | 'oldValue': Previous value in the key or |
| 4920 | None on Error |
| 4921 | |
| 4922 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4923 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4924 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4925 | """ |
| 4926 | try: |
| 4927 | numKeys = str( numKeys ) |
| 4928 | value = str( value ) |
| 4929 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4930 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4931 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4932 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4933 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4934 | results = {} |
| 4935 | for line in output.splitlines(): |
| 4936 | new = re.search( newPattern, line ) |
| 4937 | updated = re.search( updatedPattern, line ) |
| 4938 | if new: |
| 4939 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4940 | elif updated: |
| 4941 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4942 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4943 | else: |
| 4944 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4945 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4946 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4947 | newPattern, |
| 4948 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4949 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4950 | return results |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4951 | except ( TypeError, AttributeError ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4952 | main.log.exception( self.name + ": Object not as expected" ) |
| 4953 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4954 | except Exception: |
| 4955 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4956 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4957 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4958 | def maps( self, jsonFormat=True ): |
| 4959 | """ |
| 4960 | Description: Returns result of onos:maps |
| 4961 | Optional: |
| 4962 | * jsonFormat: enable json formatting of output |
| 4963 | """ |
| 4964 | try: |
| 4965 | cmdStr = "maps" |
| 4966 | if jsonFormat: |
| 4967 | cmdStr += " -j" |
| 4968 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4969 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4970 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4971 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4972 | except AssertionError: |
| 4973 | main.log.exception( "" ) |
| 4974 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4975 | except TypeError: |
| 4976 | main.log.exception( self.name + ": Object not as expected" ) |
| 4977 | return None |
| 4978 | except pexpect.EOF: |
| 4979 | main.log.error( self.name + ": EOF exception found" ) |
| 4980 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4981 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4982 | except Exception: |
| 4983 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4984 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4985 | |
| 4986 | def getSwController( self, uri, jsonFormat=True ): |
| 4987 | """ |
| 4988 | Descrition: Gets the controller information from the device |
| 4989 | """ |
| 4990 | try: |
| 4991 | cmd = "device-controllers " |
| 4992 | if jsonFormat: |
| 4993 | cmd += "-j " |
| 4994 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4995 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4996 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4997 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4998 | except AssertionError: |
| 4999 | main.log.exception( "" ) |
| 5000 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5001 | except TypeError: |
| 5002 | main.log.exception( self.name + ": Object not as expected" ) |
| 5003 | return None |
| 5004 | except pexpect.EOF: |
| 5005 | main.log.error( self.name + ": EOF exception found" ) |
| 5006 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5007 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5008 | except Exception: |
| 5009 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5010 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5011 | |
| 5012 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 5013 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5014 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5015 | |
| 5016 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5017 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5018 | ip - String or List: The ip address of the controller. |
| 5019 | This parameter can be formed in a couple of different ways. |
| 5020 | VALID: |
| 5021 | 10.0.0.1 - just the ip address |
| 5022 | tcp:10.0.0.1 - the protocol and the ip address |
| 5023 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 5024 | so that you can add controllers with different |
| 5025 | protocols and ports |
| 5026 | INVALID: |
| 5027 | 10.0.0.1:6653 - this is not supported by ONOS |
| 5028 | |
| 5029 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 5030 | port - The port number. |
| 5031 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 5032 | |
| 5033 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 5034 | """ |
| 5035 | try: |
| 5036 | cmd = "device-setcontrollers" |
| 5037 | |
| 5038 | if jsonFormat: |
| 5039 | cmd += " -j" |
| 5040 | cmd += " " + uri |
| 5041 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5042 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5043 | for item in ip: |
| 5044 | if ":" in item: |
| 5045 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5046 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5047 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5048 | elif "." in sitem[ 1 ]: |
| 5049 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5050 | else: |
| 5051 | main.log.error( "Malformed entry: " + item ) |
| 5052 | raise TypeError |
| 5053 | else: |
| 5054 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5055 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5056 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5057 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5058 | if "Error" in response: |
| 5059 | main.log.error( response ) |
| 5060 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5061 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5062 | except AssertionError: |
| 5063 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5064 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5065 | except TypeError: |
| 5066 | main.log.exception( self.name + ": Object not as expected" ) |
| 5067 | return main.FALSE |
| 5068 | except pexpect.EOF: |
| 5069 | main.log.error( self.name + ": EOF exception found" ) |
| 5070 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5071 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5072 | except Exception: |
| 5073 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5074 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5075 | |
| 5076 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5077 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5078 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5079 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5080 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5081 | 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] | 5082 | Returns: |
| 5083 | Returns main.FALSE if an exception is thrown or an error is present |
| 5084 | in the response. Otherwise, returns main.TRUE. |
| 5085 | NOTE: |
| 5086 | 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] | 5087 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5088 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5089 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 5090 | deviceStr = device |
| 5091 | device = [] |
| 5092 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5093 | |
| 5094 | for d in device: |
| 5095 | time.sleep( 1 ) |
| 5096 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5097 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5098 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5099 | if "Error" in response: |
| 5100 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 5101 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5102 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5103 | except AssertionError: |
| 5104 | main.log.exception( "" ) |
| 5105 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5106 | except TypeError: |
| 5107 | main.log.exception( self.name + ": Object not as expected" ) |
| 5108 | return main.FALSE |
| 5109 | except pexpect.EOF: |
| 5110 | main.log.error( self.name + ": EOF exception found" ) |
| 5111 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5112 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5113 | except Exception: |
| 5114 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5115 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5116 | |
| 5117 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5118 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5119 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5120 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5121 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5122 | 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] | 5123 | Returns: |
| 5124 | Returns main.FALSE if an exception is thrown or an error is present |
| 5125 | in the response. Otherwise, returns main.TRUE. |
| 5126 | NOTE: |
| 5127 | 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] | 5128 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5129 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5130 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5131 | host = list( host ) |
| 5132 | |
| 5133 | for h in host: |
| 5134 | time.sleep( 1 ) |
| 5135 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5136 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5137 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5138 | if "Error" in response: |
| 5139 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5140 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5141 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5142 | except AssertionError: |
| 5143 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5144 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5145 | except TypeError: |
| 5146 | main.log.exception( self.name + ": Object not as expected" ) |
| 5147 | return main.FALSE |
| 5148 | except pexpect.EOF: |
| 5149 | main.log.error( self.name + ": EOF exception found" ) |
| 5150 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5151 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5152 | except Exception: |
| 5153 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5154 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5155 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5156 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5157 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5158 | Description: |
| 5159 | Bring link down or up in the null-provider. |
| 5160 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5161 | begin - (string) One end of a device or switch. |
| 5162 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5163 | returns: |
| 5164 | main.TRUE if no exceptions were thrown and no Errors are |
| 5165 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5166 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5167 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5168 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5169 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5170 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5171 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5172 | if "Error" in response or "Failure" in response: |
| 5173 | main.log.error( response ) |
| 5174 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5175 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5176 | except AssertionError: |
| 5177 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5178 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5179 | except TypeError: |
| 5180 | main.log.exception( self.name + ": Object not as expected" ) |
| 5181 | return main.FALSE |
| 5182 | except pexpect.EOF: |
| 5183 | main.log.error( self.name + ": EOF exception found" ) |
| 5184 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5185 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5186 | except Exception: |
| 5187 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5188 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5189 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5190 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5191 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5192 | Description: |
| 5193 | Changes the state of port in an OF switch by means of the |
| 5194 | PORTSTATUS OF messages. |
| 5195 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5196 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5197 | port - (string) target port in the device. Ex: '2' |
| 5198 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5199 | returns: |
| 5200 | main.TRUE if no exceptions were thrown and no Errors are |
| 5201 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5202 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5203 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5204 | state = state.lower() |
| 5205 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5206 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5207 | response = self.sendline( cmd, showResponse=True ) |
| 5208 | assert response is not None, "Error in sendline" |
| 5209 | assert "Command not found:" not in response, response |
| 5210 | if "Error" in response or "Failure" in response: |
| 5211 | main.log.error( response ) |
| 5212 | return main.FALSE |
| 5213 | return main.TRUE |
| 5214 | except AssertionError: |
| 5215 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5216 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5217 | except TypeError: |
| 5218 | main.log.exception( self.name + ": Object not as expected" ) |
| 5219 | return main.FALSE |
| 5220 | except pexpect.EOF: |
| 5221 | main.log.error( self.name + ": EOF exception found" ) |
| 5222 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5223 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5224 | except Exception: |
| 5225 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5226 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5227 | |
| 5228 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5229 | """ |
| 5230 | Set the logging level to lvl for a specific app |
| 5231 | returns main.TRUE on success |
| 5232 | returns main.FALSE if Error occurred |
| 5233 | if noExit is True, TestON will not exit, but clean up |
| 5234 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5235 | Level defaults to INFO |
| 5236 | """ |
| 5237 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5238 | self.handle.sendline( "log:set %s %s" % ( level, app ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 5239 | self.handle.expect( self.karafPrompt ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5240 | |
| 5241 | response = self.handle.before |
| 5242 | if re.search( "Error", response ): |
| 5243 | return main.FALSE |
| 5244 | return main.TRUE |
| 5245 | except pexpect.TIMEOUT: |
| 5246 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5247 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5248 | except pexpect.EOF: |
| 5249 | main.log.error( self.name + ": EOF exception found" ) |
| 5250 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5251 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5252 | except Exception: |
| 5253 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5254 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5255 | |
| 5256 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5257 | """ |
| 5258 | Return a dictionary which describes the latest network topology data as a |
| 5259 | graph. |
| 5260 | An example of the dictionary: |
| 5261 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5262 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5263 | Each vertex should at least have an 'edges' attribute which describes the |
| 5264 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5265 | 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] | 5266 | list of attributes. |
| 5267 | An example of the edges dictionary: |
| 5268 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5269 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5270 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5271 | in topology data. |
| 5272 | """ |
| 5273 | graphDict = {} |
| 5274 | try: |
| 5275 | links = self.links() |
| 5276 | links = json.loads( links ) |
| 5277 | devices = self.devices() |
| 5278 | devices = json.loads( devices ) |
| 5279 | idToDevice = {} |
| 5280 | for device in devices: |
| 5281 | idToDevice[ device[ 'id' ] ] = device |
| 5282 | if includeHost: |
| 5283 | hosts = self.hosts() |
| 5284 | # FIXME: support 'includeHost' argument |
| 5285 | for link in links: |
| 5286 | nodeA = link[ 'src' ][ 'device' ] |
| 5287 | nodeB = link[ 'dst' ][ 'device' ] |
| 5288 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5289 | if nodeA not in graphDict.keys(): |
| 5290 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5291 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5292 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5293 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5294 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5295 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5296 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5297 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5298 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5299 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5300 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5301 | else: |
| 5302 | # Assert nodeB is not connected to any current links of nodeA |
| 5303 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5304 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5305 | 'type': link[ 'type' ], |
| 5306 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5307 | return graphDict |
| 5308 | except ( TypeError, ValueError ): |
| 5309 | main.log.exception( self.name + ": Object not as expected" ) |
| 5310 | return None |
| 5311 | except KeyError: |
| 5312 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5313 | return None |
| 5314 | except AssertionError: |
| 5315 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5316 | return None |
| 5317 | except pexpect.EOF: |
| 5318 | main.log.error( self.name + ": EOF exception found" ) |
| 5319 | main.log.error( self.name + ": " + self.handle.before ) |
| 5320 | return None |
| 5321 | except Exception: |
| 5322 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5323 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5324 | |
| 5325 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5326 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5327 | Send command to check intent-perf summary |
| 5328 | Returns: dictionary for intent-perf summary |
| 5329 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5330 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5331 | cmd = "intent-perf -s" |
| 5332 | respDic = {} |
| 5333 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5334 | assert resp is not None, "Error in sendline" |
| 5335 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5336 | try: |
| 5337 | # Generate the dictionary to return |
| 5338 | for l in resp.split( "\n" ): |
| 5339 | # Delete any white space in line |
| 5340 | temp = re.sub( r'\s+', '', l ) |
| 5341 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5342 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5343 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5344 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5345 | main.log.exception( self.name + ": Object not as expected" ) |
| 5346 | return None |
| 5347 | except KeyError: |
| 5348 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5349 | return None |
| 5350 | except AssertionError: |
| 5351 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5352 | return None |
| 5353 | except pexpect.EOF: |
| 5354 | main.log.error( self.name + ": EOF exception found" ) |
| 5355 | main.log.error( self.name + ": " + self.handle.before ) |
| 5356 | return None |
| 5357 | except Exception: |
| 5358 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5359 | return None |
| 5360 | return respDic |
| 5361 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5362 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5363 | """ |
| 5364 | Searches the latest ONOS log file for the given search term and |
| 5365 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5366 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5367 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5368 | searchTerm: |
| 5369 | The string to grep from the ONOS log. |
| 5370 | startLine: |
| 5371 | The term that decides which line is the start to search the searchTerm in |
| 5372 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5373 | logNum: |
| 5374 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5375 | information.Because of this, search mutiply logs is necessary to capture |
| 5376 | the right result. logNum is the number of karaf logs that we need to search |
| 5377 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5378 | mode: |
| 5379 | all: return all the strings that contain the search term |
| 5380 | last: return the last string that contains the search term |
| 5381 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5382 | num: return the number of times that the searchTerm appears in the log |
| 5383 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5384 | """ |
| 5385 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5386 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5387 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5388 | logPath = '/opt/onos/log/karaf.log.' |
| 5389 | logPaths = '/opt/onos/log/karaf.log' |
| 5390 | for i in range( 1, logNum ): |
| 5391 | logPaths = logPath + str( i ) + " " + logPaths |
| 5392 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5393 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5394 | # 100000000 is just a extreme large number to make sure this function can |
| 5395 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5396 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5397 | if mode == 'all': |
| 5398 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5399 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5400 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5401 | elif mode == 'first': |
| 5402 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5403 | elif mode == 'num': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5404 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
You Wang | 118ba58 | 2017-01-02 17:14:43 -0800 | [diff] [blame] | 5405 | num = self.sendline( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5406 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5407 | elif mode == 'total': |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 5408 | totalLines = self.lineCount( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5409 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5410 | else: |
| 5411 | main.log.error( self.name + " unsupported mode" ) |
| 5412 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5413 | before = self.sendline( cmd ) |
| 5414 | before = before.splitlines() |
| 5415 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5416 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5417 | return returnLines |
| 5418 | except AssertionError: |
| 5419 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5420 | return None |
| 5421 | except pexpect.EOF: |
| 5422 | main.log.error( self.name + ": EOF exception found" ) |
| 5423 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5424 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5425 | except pexpect.TIMEOUT: |
| 5426 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5427 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5428 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5429 | except Exception: |
| 5430 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5431 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5432 | |
| 5433 | def vplsShow( self, jsonFormat=True ): |
| 5434 | """ |
| 5435 | Description: Returns result of onos:vpls show, which should list the |
| 5436 | configured VPLS networks and the assigned interfaces. |
| 5437 | Optional: |
| 5438 | * jsonFormat: enable json formatting of output |
| 5439 | Returns: |
| 5440 | The output of the command or None on error. |
| 5441 | """ |
| 5442 | try: |
| 5443 | cmdStr = "vpls show" |
| 5444 | if jsonFormat: |
| 5445 | raise NotImplementedError |
| 5446 | cmdStr += " -j" |
| 5447 | handle = self.sendline( cmdStr ) |
| 5448 | assert handle is not None, "Error in sendline" |
| 5449 | assert "Command not found:" not in handle, handle |
| 5450 | return handle |
| 5451 | except AssertionError: |
| 5452 | main.log.exception( "" ) |
| 5453 | return None |
| 5454 | except TypeError: |
| 5455 | main.log.exception( self.name + ": Object not as expected" ) |
| 5456 | return None |
| 5457 | except pexpect.EOF: |
| 5458 | main.log.error( self.name + ": EOF exception found" ) |
| 5459 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5460 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5461 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5462 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5463 | return None |
| 5464 | except Exception: |
| 5465 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5466 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5467 | |
| 5468 | def parseVplsShow( self ): |
| 5469 | """ |
| 5470 | Parse the cli output of 'vpls show' into json output. This is required |
| 5471 | as there is currently no json output available. |
| 5472 | """ |
| 5473 | try: |
| 5474 | output = [] |
| 5475 | raw = self.vplsShow( jsonFormat=False ) |
| 5476 | namePat = "VPLS name: (?P<name>\w+)" |
| 5477 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5478 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5479 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5480 | mIter = re.finditer( pattern, raw ) |
| 5481 | for match in mIter: |
| 5482 | item = {} |
| 5483 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5484 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5485 | if ifaces == [ "" ]: |
| 5486 | ifaces = [] |
| 5487 | item[ 'interfaces' ] = ifaces |
| 5488 | encap = match.group( 'encap' ) |
| 5489 | if encap != 'NONE': |
| 5490 | item[ 'encapsulation' ] = encap.lower() |
| 5491 | output.append( item ) |
| 5492 | return output |
| 5493 | except Exception: |
| 5494 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5495 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5496 | |
| 5497 | def vplsList( self, jsonFormat=True ): |
| 5498 | """ |
| 5499 | Description: Returns result of onos:vpls list, which should list the |
| 5500 | configured VPLS networks. |
| 5501 | Optional: |
| 5502 | * jsonFormat: enable json formatting of output |
| 5503 | """ |
| 5504 | try: |
| 5505 | cmdStr = "vpls list" |
| 5506 | if jsonFormat: |
| 5507 | raise NotImplementedError |
| 5508 | cmdStr += " -j" |
| 5509 | handle = self.sendline( cmdStr ) |
| 5510 | assert handle is not None, "Error in sendline" |
| 5511 | assert "Command not found:" not in handle, handle |
| 5512 | return handle |
| 5513 | except AssertionError: |
| 5514 | main.log.exception( "" ) |
| 5515 | return None |
| 5516 | except TypeError: |
| 5517 | main.log.exception( self.name + ": Object not as expected" ) |
| 5518 | return None |
| 5519 | except pexpect.EOF: |
| 5520 | main.log.error( self.name + ": EOF exception found" ) |
| 5521 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5522 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5523 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5524 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5525 | return None |
| 5526 | except Exception: |
| 5527 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5528 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5529 | |
| 5530 | def vplsCreate( self, network ): |
| 5531 | """ |
| 5532 | CLI command to create a new VPLS network. |
| 5533 | Required arguments: |
| 5534 | network - String name of the network to create. |
| 5535 | returns: |
| 5536 | main.TRUE on success and main.FALSE on failure |
| 5537 | """ |
| 5538 | try: |
| 5539 | network = str( network ) |
| 5540 | cmdStr = "vpls create " |
| 5541 | cmdStr += network |
| 5542 | output = self.sendline( cmdStr ) |
| 5543 | assert output is not None, "Error in sendline" |
| 5544 | assert "Command not found:" not in output, output |
| 5545 | assert "Error executing command" not in output, output |
| 5546 | assert "VPLS already exists:" not in output, output |
| 5547 | return main.TRUE |
| 5548 | except AssertionError: |
| 5549 | main.log.exception( "" ) |
| 5550 | return main.FALSE |
| 5551 | except TypeError: |
| 5552 | main.log.exception( self.name + ": Object not as expected" ) |
| 5553 | return main.FALSE |
| 5554 | except pexpect.EOF: |
| 5555 | main.log.error( self.name + ": EOF exception found" ) |
| 5556 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5557 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5558 | except Exception: |
| 5559 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5560 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5561 | |
| 5562 | def vplsDelete( self, network ): |
| 5563 | """ |
| 5564 | CLI command to delete a VPLS network. |
| 5565 | Required arguments: |
| 5566 | network - Name of the network to delete. |
| 5567 | returns: |
| 5568 | main.TRUE on success and main.FALSE on failure |
| 5569 | """ |
| 5570 | try: |
| 5571 | network = str( network ) |
| 5572 | cmdStr = "vpls delete " |
| 5573 | cmdStr += network |
| 5574 | output = self.sendline( cmdStr ) |
| 5575 | assert output is not None, "Error in sendline" |
| 5576 | assert "Command not found:" not in output, output |
| 5577 | assert "Error executing command" not in output, output |
| 5578 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5579 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5580 | return main.TRUE |
| 5581 | except AssertionError: |
| 5582 | main.log.exception( "" ) |
| 5583 | return main.FALSE |
| 5584 | except TypeError: |
| 5585 | main.log.exception( self.name + ": Object not as expected" ) |
| 5586 | return main.FALSE |
| 5587 | except pexpect.EOF: |
| 5588 | main.log.error( self.name + ": EOF exception found" ) |
| 5589 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5590 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5591 | except Exception: |
| 5592 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5593 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5594 | |
| 5595 | def vplsAddIface( self, network, iface ): |
| 5596 | """ |
| 5597 | CLI command to add an interface to a VPLS network. |
| 5598 | Required arguments: |
| 5599 | network - Name of the network to add the interface to. |
| 5600 | iface - The ONOS name for an interface. |
| 5601 | returns: |
| 5602 | main.TRUE on success and main.FALSE on failure |
| 5603 | """ |
| 5604 | try: |
| 5605 | network = str( network ) |
| 5606 | iface = str( iface ) |
| 5607 | cmdStr = "vpls add-if " |
| 5608 | cmdStr += network + " " + iface |
| 5609 | output = self.sendline( cmdStr ) |
| 5610 | assert output is not None, "Error in sendline" |
| 5611 | assert "Command not found:" not in output, output |
| 5612 | assert "Error executing command" not in output, output |
| 5613 | assert "already associated to network" not in output, output |
| 5614 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5615 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5616 | return main.TRUE |
| 5617 | except AssertionError: |
| 5618 | main.log.exception( "" ) |
| 5619 | return main.FALSE |
| 5620 | except TypeError: |
| 5621 | main.log.exception( self.name + ": Object not as expected" ) |
| 5622 | return main.FALSE |
| 5623 | except pexpect.EOF: |
| 5624 | main.log.error( self.name + ": EOF exception found" ) |
| 5625 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5626 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5627 | except Exception: |
| 5628 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5629 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5630 | |
| 5631 | def vplsRemIface( self, network, iface ): |
| 5632 | """ |
| 5633 | CLI command to remove an interface from a VPLS network. |
| 5634 | Required arguments: |
| 5635 | network - Name of the network to remove the interface from. |
| 5636 | iface - Name of the interface to remove. |
| 5637 | returns: |
| 5638 | main.TRUE on success and main.FALSE on failure |
| 5639 | """ |
| 5640 | try: |
| 5641 | iface = str( iface ) |
| 5642 | cmdStr = "vpls rem-if " |
| 5643 | cmdStr += network + " " + iface |
| 5644 | output = self.sendline( cmdStr ) |
| 5645 | assert output is not None, "Error in sendline" |
| 5646 | assert "Command not found:" not in output, output |
| 5647 | assert "Error executing command" not in output, output |
| 5648 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5649 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5650 | return main.TRUE |
| 5651 | except AssertionError: |
| 5652 | main.log.exception( "" ) |
| 5653 | return main.FALSE |
| 5654 | except TypeError: |
| 5655 | main.log.exception( self.name + ": Object not as expected" ) |
| 5656 | return main.FALSE |
| 5657 | except pexpect.EOF: |
| 5658 | main.log.error( self.name + ": EOF exception found" ) |
| 5659 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5660 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5661 | except Exception: |
| 5662 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5663 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5664 | |
| 5665 | def vplsClean( self ): |
| 5666 | """ |
| 5667 | Description: Clears the VPLS app configuration. |
| 5668 | Returns: main.TRUE on success and main.FALSE on failure |
| 5669 | """ |
| 5670 | try: |
| 5671 | cmdStr = "vpls clean" |
| 5672 | handle = self.sendline( cmdStr ) |
| 5673 | assert handle is not None, "Error in sendline" |
| 5674 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5675 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5676 | return handle |
| 5677 | except AssertionError: |
| 5678 | main.log.exception( "" ) |
| 5679 | return main.FALSE |
| 5680 | except TypeError: |
| 5681 | main.log.exception( self.name + ": Object not as expected" ) |
| 5682 | return main.FALSE |
| 5683 | except pexpect.EOF: |
| 5684 | main.log.error( self.name + ": EOF exception found" ) |
| 5685 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5686 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5687 | except Exception: |
| 5688 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5689 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5690 | |
| 5691 | def vplsSetEncap( self, network, encapType ): |
| 5692 | """ |
| 5693 | CLI command to add an interface to a VPLS network. |
| 5694 | Required arguments: |
| 5695 | network - Name of the network to create. |
| 5696 | encapType - Type of encapsulation. |
| 5697 | returns: |
| 5698 | main.TRUE on success and main.FALSE on failure |
| 5699 | """ |
| 5700 | try: |
| 5701 | network = str( network ) |
| 5702 | encapType = str( encapType ).upper() |
| 5703 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5704 | cmdStr = "vpls set-encap " |
| 5705 | cmdStr += network + " " + encapType |
| 5706 | output = self.sendline( cmdStr ) |
| 5707 | assert output is not None, "Error in sendline" |
| 5708 | assert "Command not found:" not in output, output |
| 5709 | assert "Error executing command" not in output, output |
| 5710 | assert "already associated to network" not in output, output |
| 5711 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5712 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5713 | return main.TRUE |
| 5714 | except AssertionError: |
| 5715 | main.log.exception( "" ) |
| 5716 | return main.FALSE |
| 5717 | except TypeError: |
| 5718 | main.log.exception( self.name + ": Object not as expected" ) |
| 5719 | return main.FALSE |
| 5720 | except pexpect.EOF: |
| 5721 | main.log.error( self.name + ": EOF exception found" ) |
| 5722 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5723 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5724 | except Exception: |
| 5725 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5726 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5727 | |
| 5728 | def interfaces( self, jsonFormat=True ): |
| 5729 | """ |
| 5730 | Description: Returns result of interfaces command. |
| 5731 | Optional: |
| 5732 | * jsonFormat: enable json formatting of output |
| 5733 | Returns: |
| 5734 | The output of the command or None on error. |
| 5735 | """ |
| 5736 | try: |
| 5737 | cmdStr = "interfaces" |
| 5738 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5739 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5740 | cmdStr += " -j" |
| 5741 | handle = self.sendline( cmdStr ) |
| 5742 | assert handle is not None, "Error in sendline" |
| 5743 | assert "Command not found:" not in handle, handle |
| 5744 | return handle |
| 5745 | except AssertionError: |
| 5746 | main.log.exception( "" ) |
| 5747 | return None |
| 5748 | except TypeError: |
| 5749 | main.log.exception( self.name + ": Object not as expected" ) |
| 5750 | return None |
| 5751 | except pexpect.EOF: |
| 5752 | main.log.error( self.name + ": EOF exception found" ) |
| 5753 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5754 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5755 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5756 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5757 | return None |
| 5758 | except Exception: |
| 5759 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5760 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5761 | |
| 5762 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5763 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5764 | Get the timestamp of searchTerm from karaf log. |
| 5765 | |
| 5766 | Arguments: |
| 5767 | splitTerm_before and splitTerm_after: |
| 5768 | |
| 5769 | The terms that split the string that contains the timeStamp of |
| 5770 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5771 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5772 | and the splitTerm_after is "x" |
| 5773 | |
| 5774 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5775 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5776 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5777 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5778 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5779 | return main.ERROR |
| 5780 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5781 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5782 | main.log.warn( "Captured timestamp string is empty" ) |
| 5783 | return main.ERROR |
| 5784 | lines = lines[ 0 ] |
| 5785 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5786 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5787 | # get the target value |
| 5788 | line = lines.split( splitTerm_before ) |
| 5789 | key = line[ 1 ].split( splitTerm_after ) |
| 5790 | return int( key[ 0 ] ) |
| 5791 | except IndexError: |
| 5792 | main.log.warn( "Index Error!" ) |
| 5793 | return main.ERROR |
| 5794 | except AssertionError: |
| 5795 | main.log.warn( "Search Term Not Found " ) |
| 5796 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5797 | |
| 5798 | def workQueueAdd( self, queueName, value ): |
| 5799 | """ |
| 5800 | CLI command to add a string to the specified Work Queue. |
| 5801 | This function uses the distributed primitives test app, which |
| 5802 | gives some cli access to distributed primitives for testing |
| 5803 | purposes only. |
| 5804 | |
| 5805 | Required arguments: |
| 5806 | queueName - The name of the queue to add to |
| 5807 | value - The value to add to the queue |
| 5808 | returns: |
| 5809 | main.TRUE on success, main.FALSE on failure and |
| 5810 | main.ERROR on error. |
| 5811 | """ |
| 5812 | try: |
| 5813 | queueName = str( queueName ) |
| 5814 | value = str( value ) |
| 5815 | prefix = "work-queue-test" |
| 5816 | operation = "add" |
| 5817 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5818 | output = self.distPrimitivesSend( cmdStr ) |
| 5819 | if "Invalid operation name" in output: |
| 5820 | main.log.warn( output ) |
| 5821 | return main.ERROR |
| 5822 | elif "Done" in output: |
| 5823 | return main.TRUE |
| 5824 | except TypeError: |
| 5825 | main.log.exception( self.name + ": Object not as expected" ) |
| 5826 | return main.ERROR |
| 5827 | except Exception: |
| 5828 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5829 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5830 | |
| 5831 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5832 | """ |
| 5833 | CLI command to add two strings to the specified Work Queue. |
| 5834 | This function uses the distributed primitives test app, which |
| 5835 | gives some cli access to distributed primitives for testing |
| 5836 | purposes only. |
| 5837 | |
| 5838 | Required arguments: |
| 5839 | queueName - The name of the queue to add to |
| 5840 | value1 - The first value to add to the queue |
| 5841 | value2 - The second value to add to the queue |
| 5842 | returns: |
| 5843 | main.TRUE on success, main.FALSE on failure and |
| 5844 | main.ERROR on error. |
| 5845 | """ |
| 5846 | try: |
| 5847 | queueName = str( queueName ) |
| 5848 | value1 = str( value1 ) |
| 5849 | value2 = str( value2 ) |
| 5850 | prefix = "work-queue-test" |
| 5851 | operation = "addMultiple" |
| 5852 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 5853 | output = self.distPrimitivesSend( cmdStr ) |
| 5854 | if "Invalid operation name" in output: |
| 5855 | main.log.warn( output ) |
| 5856 | return main.ERROR |
| 5857 | elif "Done" in output: |
| 5858 | return main.TRUE |
| 5859 | except TypeError: |
| 5860 | main.log.exception( self.name + ": Object not as expected" ) |
| 5861 | return main.ERROR |
| 5862 | except Exception: |
| 5863 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5864 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5865 | |
| 5866 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 5867 | """ |
| 5868 | CLI command to take a value from the specified Work Queue and compelte it. |
| 5869 | This function uses the distributed primitives test app, which |
| 5870 | gives some cli access to distributed primitives for testing |
| 5871 | purposes only. |
| 5872 | |
| 5873 | Required arguments: |
| 5874 | queueName - The name of the queue to add to |
| 5875 | number - The number of items to take and complete |
| 5876 | returns: |
| 5877 | main.TRUE on success, main.FALSE on failure and |
| 5878 | main.ERROR on error. |
| 5879 | """ |
| 5880 | try: |
| 5881 | queueName = str( queueName ) |
| 5882 | number = str( int( number ) ) |
| 5883 | prefix = "work-queue-test" |
| 5884 | operation = "takeAndComplete" |
| 5885 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 5886 | output = self.distPrimitivesSend( cmdStr ) |
| 5887 | if "Invalid operation name" in output: |
| 5888 | main.log.warn( output ) |
| 5889 | return main.ERROR |
| 5890 | elif "Done" in output: |
| 5891 | return main.TRUE |
| 5892 | except TypeError: |
| 5893 | main.log.exception( self.name + ": Object not as expected" ) |
| 5894 | return main.ERROR |
| 5895 | except Exception: |
| 5896 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5897 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5898 | |
| 5899 | def workQueueDestroy( self, queueName ): |
| 5900 | """ |
| 5901 | CLI command to destroy the specified Work Queue. |
| 5902 | This function uses the distributed primitives test app, which |
| 5903 | gives some cli access to distributed primitives for testing |
| 5904 | purposes only. |
| 5905 | |
| 5906 | Required arguments: |
| 5907 | queueName - The name of the queue to add to |
| 5908 | returns: |
| 5909 | main.TRUE on success, main.FALSE on failure and |
| 5910 | main.ERROR on error. |
| 5911 | """ |
| 5912 | try: |
| 5913 | queueName = str( queueName ) |
| 5914 | prefix = "work-queue-test" |
| 5915 | operation = "destroy" |
| 5916 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5917 | output = self.distPrimitivesSend( cmdStr ) |
| 5918 | if "Invalid operation name" in output: |
| 5919 | main.log.warn( output ) |
| 5920 | return main.ERROR |
| 5921 | return main.TRUE |
| 5922 | except TypeError: |
| 5923 | main.log.exception( self.name + ": Object not as expected" ) |
| 5924 | return main.ERROR |
| 5925 | except Exception: |
| 5926 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5927 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5928 | |
| 5929 | def workQueueTotalPending( self, queueName ): |
| 5930 | """ |
| 5931 | CLI command to get the Total Pending items of the specified Work Queue. |
| 5932 | This function uses the distributed primitives test app, which |
| 5933 | gives some cli access to distributed primitives for testing |
| 5934 | purposes only. |
| 5935 | |
| 5936 | Required arguments: |
| 5937 | queueName - The name of the queue to add to |
| 5938 | returns: |
| 5939 | The number of Pending items in the specified work queue or |
| 5940 | None on error |
| 5941 | """ |
| 5942 | try: |
| 5943 | queueName = str( queueName ) |
| 5944 | prefix = "work-queue-test" |
| 5945 | operation = "totalPending" |
| 5946 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5947 | output = self.distPrimitivesSend( cmdStr ) |
| 5948 | pattern = r'\d+' |
| 5949 | if "Invalid operation name" in output: |
| 5950 | main.log.warn( output ) |
| 5951 | return None |
| 5952 | else: |
| 5953 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5954 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5955 | except ( AttributeError, TypeError ): |
| 5956 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5957 | return None |
| 5958 | except Exception: |
| 5959 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5960 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5961 | |
| 5962 | def workQueueTotalCompleted( self, queueName ): |
| 5963 | """ |
| 5964 | CLI command to get the Total Completed items of the specified Work Queue. |
| 5965 | This function uses the distributed primitives test app, which |
| 5966 | gives some cli access to distributed primitives for testing |
| 5967 | purposes only. |
| 5968 | |
| 5969 | Required arguments: |
| 5970 | queueName - The name of the queue to add to |
| 5971 | returns: |
| 5972 | The number of complete items in the specified work queue or |
| 5973 | None on error |
| 5974 | """ |
| 5975 | try: |
| 5976 | queueName = str( queueName ) |
| 5977 | prefix = "work-queue-test" |
| 5978 | operation = "totalCompleted" |
| 5979 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5980 | output = self.distPrimitivesSend( cmdStr ) |
| 5981 | pattern = r'\d+' |
| 5982 | if "Invalid operation name" in output: |
| 5983 | main.log.warn( output ) |
| 5984 | return None |
| 5985 | else: |
| 5986 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5987 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5988 | except ( AttributeError, TypeError ): |
| 5989 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5990 | return None |
| 5991 | except Exception: |
| 5992 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5993 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5994 | |
| 5995 | def workQueueTotalInProgress( self, queueName ): |
| 5996 | """ |
| 5997 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 5998 | This function uses the distributed primitives test app, which |
| 5999 | gives some cli access to distributed primitives for testing |
| 6000 | purposes only. |
| 6001 | |
| 6002 | Required arguments: |
| 6003 | queueName - The name of the queue to add to |
| 6004 | returns: |
| 6005 | The number of In Progress items in the specified work queue or |
| 6006 | None on error |
| 6007 | """ |
| 6008 | try: |
| 6009 | queueName = str( queueName ) |
| 6010 | prefix = "work-queue-test" |
| 6011 | operation = "totalInProgress" |
| 6012 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6013 | output = self.distPrimitivesSend( cmdStr ) |
| 6014 | pattern = r'\d+' |
| 6015 | if "Invalid operation name" in output: |
| 6016 | main.log.warn( output ) |
| 6017 | return None |
| 6018 | else: |
| 6019 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6020 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6021 | except ( AttributeError, TypeError ): |
| 6022 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6023 | return None |
| 6024 | except Exception: |
| 6025 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6026 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 6027 | |
| 6028 | def events( self, args='-a' ): |
| 6029 | """ |
| 6030 | Description: Returns events -a command output |
| 6031 | Optional: |
| 6032 | add other arguments |
| 6033 | """ |
| 6034 | try: |
| 6035 | cmdStr = "events" |
| 6036 | if args: |
| 6037 | cmdStr += " " + args |
| 6038 | handle = self.sendline( cmdStr ) |
| 6039 | assert handle is not None, "Error in sendline" |
| 6040 | assert "Command not found:" not in handle, handle |
| 6041 | return handle |
| 6042 | except AssertionError: |
| 6043 | main.log.exception( "" ) |
| 6044 | return None |
| 6045 | except TypeError: |
| 6046 | main.log.exception( self.name + ": Object not as expected" ) |
| 6047 | return None |
| 6048 | except pexpect.EOF: |
| 6049 | main.log.error( self.name + ": EOF exception found" ) |
| 6050 | main.log.error( self.name + ": " + self.handle.before ) |
| 6051 | main.cleanAndExit() |
| 6052 | except Exception: |
| 6053 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6054 | main.cleanAndExit() |
| 6055 | |
| 6056 | def getMaster( self, deviceID ): |
| 6057 | """ |
| 6058 | Description: Obtains current master using "roles" command for a specific deviceID |
| 6059 | """ |
| 6060 | try: |
| 6061 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 6062 | except AssertionError: |
| 6063 | main.log.exception( "" ) |
| 6064 | return None |
| 6065 | except TypeError: |
| 6066 | main.log.exception( self.name + ": Object not as expected" ) |
| 6067 | return None |
| 6068 | except pexpect.EOF: |
| 6069 | main.log.error( self.name + ": EOF exception found" ) |
| 6070 | main.log.error( self.name + ": " + self.handle.before ) |
| 6071 | main.cleanAndExit() |
| 6072 | except Exception: |
| 6073 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 6074 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 6075 | |
| 6076 | def issu( self ): |
| 6077 | """ |
| 6078 | Short summary of In-Service Software Upgrade status |
| 6079 | |
| 6080 | Returns the output of the cli command or None on Error |
| 6081 | """ |
| 6082 | try: |
| 6083 | cmdStr = "issu" |
| 6084 | handle = self.sendline( cmdStr ) |
| 6085 | assert handle is not None, "Error in sendline" |
| 6086 | assert "Command not found:" not in handle, handle |
| 6087 | assert "Unsupported command:" not in handle, handle |
| 6088 | return handle |
| 6089 | except AssertionError: |
| 6090 | main.log.exception( "" ) |
| 6091 | return None |
| 6092 | except TypeError: |
| 6093 | main.log.exception( self.name + ": Object not as expected" ) |
| 6094 | return None |
| 6095 | except pexpect.EOF: |
| 6096 | main.log.error( self.name + ": EOF exception found" ) |
| 6097 | main.log.error( self.name + ": " + self.handle.before ) |
| 6098 | main.cleanAndExit() |
| 6099 | except Exception: |
| 6100 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6101 | main.cleanAndExit() |
| 6102 | |
| 6103 | def issuInit( self ): |
| 6104 | """ |
| 6105 | Initiates an In-Service Software Upgrade |
| 6106 | |
| 6107 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6108 | """ |
| 6109 | try: |
| 6110 | cmdStr = "issu init" |
| 6111 | handle = self.sendline( cmdStr ) |
| 6112 | assert handle is not None, "Error in sendline" |
| 6113 | assert "Command not found:" not in handle, handle |
| 6114 | assert "Unsupported command:" not in handle, handle |
| 6115 | if "Initialized" in handle: |
| 6116 | return main.TRUE |
| 6117 | else: |
| 6118 | return main.FALSE |
| 6119 | except AssertionError: |
| 6120 | main.log.exception( "" ) |
| 6121 | return main.ERROR |
| 6122 | except TypeError: |
| 6123 | main.log.exception( self.name + ": Object not as expected" ) |
| 6124 | return main.ERROR |
| 6125 | except pexpect.EOF: |
| 6126 | main.log.error( self.name + ": EOF exception found" ) |
| 6127 | main.log.error( self.name + ": " + self.handle.before ) |
| 6128 | main.cleanAndExit() |
| 6129 | except Exception: |
| 6130 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6131 | main.cleanAndExit() |
| 6132 | |
| 6133 | def issuUpgrade( self ): |
| 6134 | """ |
| 6135 | Transitions stores to upgraded nodes |
| 6136 | |
| 6137 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6138 | """ |
| 6139 | try: |
| 6140 | cmdStr = "issu upgrade" |
| 6141 | handle = self.sendline( cmdStr ) |
| 6142 | assert handle is not None, "Error in sendline" |
| 6143 | assert "Command not found:" not in handle, handle |
| 6144 | assert "Unsupported command:" not in handle, handle |
| 6145 | if "Upgraded" in handle: |
| 6146 | return main.TRUE |
| 6147 | else: |
| 6148 | return main.FALSE |
| 6149 | except AssertionError: |
| 6150 | main.log.exception( "" ) |
| 6151 | return main.ERROR |
| 6152 | except TypeError: |
| 6153 | main.log.exception( self.name + ": Object not as expected" ) |
| 6154 | return main.ERROR |
| 6155 | except pexpect.EOF: |
| 6156 | main.log.error( self.name + ": EOF exception found" ) |
| 6157 | main.log.error( self.name + ": " + self.handle.before ) |
| 6158 | main.cleanAndExit() |
| 6159 | except Exception: |
| 6160 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6161 | main.cleanAndExit() |
| 6162 | |
| 6163 | def issuCommit( self ): |
| 6164 | """ |
| 6165 | Finalizes an In-Service Software Upgrade |
| 6166 | |
| 6167 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6168 | """ |
| 6169 | try: |
| 6170 | cmdStr = "issu commit" |
| 6171 | handle = self.sendline( cmdStr ) |
| 6172 | assert handle is not None, "Error in sendline" |
| 6173 | assert "Command not found:" not in handle, handle |
| 6174 | assert "Unsupported command:" not in handle, handle |
| 6175 | # TODO: Check the version returned by this command |
| 6176 | if "Committed version" in handle: |
| 6177 | return main.TRUE |
| 6178 | else: |
| 6179 | return main.FALSE |
| 6180 | except AssertionError: |
| 6181 | main.log.exception( "" ) |
| 6182 | return main.ERROR |
| 6183 | except TypeError: |
| 6184 | main.log.exception( self.name + ": Object not as expected" ) |
| 6185 | return main.ERROR |
| 6186 | except pexpect.EOF: |
| 6187 | main.log.error( self.name + ": EOF exception found" ) |
| 6188 | main.log.error( self.name + ": " + self.handle.before ) |
| 6189 | main.cleanAndExit() |
| 6190 | except Exception: |
| 6191 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6192 | main.cleanAndExit() |
| 6193 | |
| 6194 | def issuRollback( self ): |
| 6195 | """ |
| 6196 | Rolls back an In-Service Software Upgrade |
| 6197 | |
| 6198 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6199 | """ |
| 6200 | try: |
| 6201 | cmdStr = "issu rollback" |
| 6202 | handle = self.sendline( cmdStr ) |
| 6203 | assert handle is not None, "Error in sendline" |
| 6204 | assert "Command not found:" not in handle, handle |
| 6205 | assert "Unsupported command:" not in handle, handle |
| 6206 | # TODO: Check the version returned by this command |
| 6207 | if "Rolled back to version" in handle: |
| 6208 | return main.TRUE |
| 6209 | else: |
| 6210 | return main.FALSE |
| 6211 | except AssertionError: |
| 6212 | main.log.exception( "" ) |
| 6213 | return main.ERROR |
| 6214 | except TypeError: |
| 6215 | main.log.exception( self.name + ": Object not as expected" ) |
| 6216 | return main.ERROR |
| 6217 | except pexpect.EOF: |
| 6218 | main.log.error( self.name + ": EOF exception found" ) |
| 6219 | main.log.error( self.name + ": " + self.handle.before ) |
| 6220 | main.cleanAndExit() |
| 6221 | except Exception: |
| 6222 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6223 | main.cleanAndExit() |
| 6224 | |
| 6225 | def issuReset( self ): |
| 6226 | """ |
| 6227 | Resets the In-Service Software Upgrade status after a rollback |
| 6228 | |
| 6229 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6230 | """ |
| 6231 | try: |
| 6232 | cmdStr = "issu reset" |
| 6233 | handle = self.sendline( cmdStr ) |
| 6234 | assert handle is not None, "Error in sendline" |
| 6235 | assert "Command not found:" not in handle, handle |
| 6236 | assert "Unsupported command:" not in handle, handle |
| 6237 | # TODO: Check the version returned by this command |
| 6238 | if "Reset version" in handle: |
| 6239 | return main.TRUE |
| 6240 | else: |
| 6241 | return main.FALSE |
| 6242 | except AssertionError: |
| 6243 | main.log.exception( "" ) |
| 6244 | return main.ERROR |
| 6245 | except TypeError: |
| 6246 | main.log.exception( self.name + ": Object not as expected" ) |
| 6247 | return main.ERROR |
| 6248 | except pexpect.EOF: |
| 6249 | main.log.error( self.name + ": EOF exception found" ) |
| 6250 | main.log.error( self.name + ": " + self.handle.before ) |
| 6251 | main.cleanAndExit() |
| 6252 | except Exception: |
| 6253 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6254 | main.cleanAndExit() |
| 6255 | |
| 6256 | def issuStatus( self ): |
| 6257 | """ |
| 6258 | Status of an In-Service Software Upgrade |
| 6259 | |
| 6260 | Returns the output of the cli command or None on Error |
| 6261 | """ |
| 6262 | try: |
| 6263 | cmdStr = "issu status" |
| 6264 | handle = self.sendline( cmdStr ) |
| 6265 | assert handle is not None, "Error in sendline" |
| 6266 | assert "Command not found:" not in handle, handle |
| 6267 | assert "Unsupported command:" not in handle, handle |
| 6268 | return handle |
| 6269 | except AssertionError: |
| 6270 | main.log.exception( "" ) |
| 6271 | return None |
| 6272 | except TypeError: |
| 6273 | main.log.exception( self.name + ": Object not as expected" ) |
| 6274 | return None |
| 6275 | except pexpect.EOF: |
| 6276 | main.log.error( self.name + ": EOF exception found" ) |
| 6277 | main.log.error( self.name + ": " + self.handle.before ) |
| 6278 | main.cleanAndExit() |
| 6279 | except Exception: |
| 6280 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6281 | main.cleanAndExit() |
| 6282 | |
| 6283 | def issuVersion( self ): |
| 6284 | """ |
| 6285 | Get the version of an In-Service Software Upgrade |
| 6286 | |
| 6287 | Returns the output of the cli command or None on Error |
| 6288 | """ |
| 6289 | try: |
| 6290 | cmdStr = "issu version" |
| 6291 | handle = self.sendline( cmdStr ) |
| 6292 | assert handle is not None, "Error in sendline" |
| 6293 | assert "Command not found:" not in handle, handle |
| 6294 | assert "Unsupported command:" not in handle, handle |
| 6295 | return handle |
| 6296 | except AssertionError: |
| 6297 | main.log.exception( "" ) |
| 6298 | return None |
| 6299 | except TypeError: |
| 6300 | main.log.exception( self.name + ": Object not as expected" ) |
| 6301 | return None |
| 6302 | except pexpect.EOF: |
| 6303 | main.log.error( self.name + ": EOF exception found" ) |
| 6304 | main.log.error( self.name + ": " + self.handle.before ) |
| 6305 | main.cleanAndExit() |
| 6306 | except Exception: |
| 6307 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6308 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6309 | |
| 6310 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6311 | """ |
| 6312 | Create a multicast route by calling 'mcast-join' command |
| 6313 | sIP: source IP of the multicast route |
| 6314 | groupIP: group IP of the multicast route |
| 6315 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6316 | dPorts: a list of destination ports of the multicast route |
| 6317 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6318 | """ |
| 6319 | try: |
| 6320 | cmdStr = "mcast-join" |
| 6321 | cmdStr += " " + str( sIP ) |
| 6322 | cmdStr += " " + str( groupIP ) |
| 6323 | cmdStr += " " + str( sPort ) |
| 6324 | assert isinstance( dPorts, list ) |
| 6325 | for dPort in dPorts: |
| 6326 | cmdStr += " " + str( dPort ) |
| 6327 | handle = self.sendline( cmdStr ) |
| 6328 | assert handle is not None, "Error in sendline" |
| 6329 | assert "Command not found:" not in handle, handle |
| 6330 | assert "Unsupported command:" not in handle, handle |
| 6331 | assert "Error executing command" not in handle, handle |
| 6332 | if "Added the mcast route" in handle: |
| 6333 | return main.TRUE |
| 6334 | else: |
| 6335 | return main.FALSE |
| 6336 | except AssertionError: |
| 6337 | main.log.exception( "" ) |
| 6338 | return None |
| 6339 | except TypeError: |
| 6340 | main.log.exception( self.name + ": Object not as expected" ) |
| 6341 | return None |
| 6342 | except pexpect.EOF: |
| 6343 | main.log.error( self.name + ": EOF exception found" ) |
| 6344 | main.log.error( self.name + ": " + self.handle.before ) |
| 6345 | main.cleanAndExit() |
| 6346 | except Exception: |
| 6347 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6348 | main.cleanAndExit() |
| 6349 | |
| 6350 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6351 | """ |
| 6352 | Delete a multicast route by calling 'mcast-delete' command |
| 6353 | sIP: source IP of the multicast route |
| 6354 | groupIP: group IP of the multicast route |
| 6355 | dPorts: a list of destination ports of the multicast route |
| 6356 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6357 | """ |
| 6358 | try: |
| 6359 | cmdStr = "mcast-delete" |
| 6360 | cmdStr += " " + str( sIP ) |
| 6361 | cmdStr += " " + str( groupIP ) |
| 6362 | assert isinstance( dPorts, list ) |
| 6363 | for dPort in dPorts: |
| 6364 | cmdStr += " " + str( dPort ) |
| 6365 | handle = self.sendline( cmdStr ) |
| 6366 | assert handle is not None, "Error in sendline" |
| 6367 | assert "Command not found:" not in handle, handle |
| 6368 | assert "Unsupported command:" not in handle, handle |
| 6369 | assert "Error executing command" not in handle, handle |
| 6370 | if "Updated the mcast route" in handle: |
| 6371 | return main.TRUE |
| 6372 | else: |
| 6373 | return main.FALSE |
| 6374 | except AssertionError: |
| 6375 | main.log.exception( "" ) |
| 6376 | return None |
| 6377 | except TypeError: |
| 6378 | main.log.exception( self.name + ": Object not as expected" ) |
| 6379 | return None |
| 6380 | except pexpect.EOF: |
| 6381 | main.log.error( self.name + ": EOF exception found" ) |
| 6382 | main.log.error( self.name + ": " + self.handle.before ) |
| 6383 | main.cleanAndExit() |
| 6384 | except Exception: |
| 6385 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6386 | main.cleanAndExit() |
| 6387 | |
| 6388 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6389 | """ |
| 6390 | Create a multicast route by calling 'mcast-host-join' command |
| 6391 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6392 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6393 | 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] | 6394 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6395 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6396 | """ |
| 6397 | try: |
| 6398 | cmdStr = "mcast-host-join" |
| 6399 | cmdStr += " -sAddr " + str( sAddr ) |
| 6400 | cmdStr += " -gAddr " + str( gAddr ) |
| 6401 | assert isinstance( srcs, list ) |
| 6402 | for src in srcs: |
| 6403 | cmdStr += " -srcs " + str( src ) |
| 6404 | assert isinstance( sinks, list ) |
| 6405 | for sink in sinks: |
| 6406 | cmdStr += " -sinks " + str( sink ) |
| 6407 | handle = self.sendline( cmdStr ) |
| 6408 | assert handle is not None, "Error in sendline" |
| 6409 | assert "Command not found:" not in handle, handle |
| 6410 | assert "Unsupported command:" not in handle, handle |
| 6411 | assert "Error executing command" not in handle, handle |
| 6412 | if "Added the mcast route" in handle: |
| 6413 | return main.TRUE |
| 6414 | else: |
| 6415 | return main.FALSE |
| 6416 | except AssertionError: |
| 6417 | main.log.exception( "" ) |
| 6418 | return None |
| 6419 | except TypeError: |
| 6420 | main.log.exception( self.name + ": Object not as expected" ) |
| 6421 | return None |
| 6422 | except pexpect.EOF: |
| 6423 | main.log.error( self.name + ": EOF exception found" ) |
| 6424 | main.log.error( self.name + ": " + self.handle.before ) |
| 6425 | main.cleanAndExit() |
| 6426 | except Exception: |
| 6427 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6428 | main.cleanAndExit() |
| 6429 | |
| 6430 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6431 | """ |
| 6432 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6433 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6434 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6435 | 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] | 6436 | will delete the route if not specified |
| 6437 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6438 | """ |
| 6439 | try: |
| 6440 | cmdStr = "mcast-host-delete" |
| 6441 | cmdStr += " -sAddr " + str( sAddr ) |
| 6442 | cmdStr += " -gAddr " + str( gAddr ) |
| 6443 | if host: |
| 6444 | cmdStr += " -h " + str( host ) |
| 6445 | handle = self.sendline( cmdStr ) |
| 6446 | assert handle is not None, "Error in sendline" |
| 6447 | assert "Command not found:" not in handle, handle |
| 6448 | assert "Unsupported command:" not in handle, handle |
| 6449 | assert "Error executing command" not in handle, handle |
| 6450 | if "Updated the mcast route" in handle: |
| 6451 | return main.TRUE |
| 6452 | elif "Deleted the mcast route" in handle: |
| 6453 | return main.TRUE |
| 6454 | else: |
| 6455 | return main.FALSE |
| 6456 | except AssertionError: |
| 6457 | main.log.exception( "" ) |
| 6458 | return None |
| 6459 | except TypeError: |
| 6460 | main.log.exception( self.name + ": Object not as expected" ) |
| 6461 | return None |
| 6462 | except pexpect.EOF: |
| 6463 | main.log.error( self.name + ": EOF exception found" ) |
| 6464 | main.log.error( self.name + ": " + self.handle.before ) |
| 6465 | main.cleanAndExit() |
| 6466 | except Exception: |
| 6467 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6468 | main.cleanAndExit() |
| 6469 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6470 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6471 | """ |
| 6472 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6473 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6474 | gAddr: specifies multicast group address |
| 6475 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6476 | will delete the route if not specified |
| 6477 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6478 | """ |
| 6479 | try: |
| 6480 | cmdStr = "mcast-sink-delete" |
| 6481 | cmdStr += " -sAddr " + str( sAddr ) |
| 6482 | cmdStr += " -gAddr " + str( gAddr ) |
| 6483 | if sink: |
| 6484 | cmdStr += " -s " + str( sink ) |
| 6485 | handle = self.sendline( cmdStr ) |
| 6486 | assert handle is not None, "Error in sendline" |
| 6487 | assert "Command not found:" not in handle, handle |
| 6488 | assert "Unsupported command:" not in handle, handle |
| 6489 | assert "Error executing command" not in handle, handle |
| 6490 | if "Updated the mcast route" in handle: |
| 6491 | return main.TRUE |
| 6492 | elif "Deleted the mcast route" in handle: |
| 6493 | return main.TRUE |
| 6494 | else: |
| 6495 | return main.FALSE |
| 6496 | except AssertionError: |
| 6497 | main.log.exception( "" ) |
| 6498 | return None |
| 6499 | except TypeError: |
| 6500 | main.log.exception( self.name + ": Object not as expected" ) |
| 6501 | return None |
| 6502 | except pexpect.EOF: |
| 6503 | main.log.error( self.name + ": EOF exception found" ) |
| 6504 | main.log.error( self.name + ": " + self.handle.before ) |
| 6505 | main.cleanAndExit() |
| 6506 | except Exception: |
| 6507 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6508 | main.cleanAndExit() |
| 6509 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6510 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6511 | """ |
| 6512 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6513 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6514 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6515 | 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] | 6516 | will delete the route if not specified |
| 6517 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6518 | """ |
| 6519 | try: |
| 6520 | cmdStr = "mcast-source-delete" |
| 6521 | cmdStr += " -sAddr " + str( sAddr ) |
| 6522 | cmdStr += " -gAddr " + str( gAddr ) |
| 6523 | if srcs: |
| 6524 | assert isinstance( srcs, list ) |
| 6525 | for src in srcs: |
| 6526 | cmdStr += " -src " + str( src ) |
| 6527 | handle = self.sendline( cmdStr ) |
| 6528 | assert handle is not None, "Error in sendline" |
| 6529 | assert "Command not found:" not in handle, handle |
| 6530 | assert "Unsupported command:" not in handle, handle |
| 6531 | assert "Error executing command" not in handle, handle |
| 6532 | if "Updated the mcast route" in handle: |
| 6533 | return main.TRUE |
| 6534 | elif "Deleted the mcast route" in handle: |
| 6535 | return main.TRUE |
| 6536 | else: |
| 6537 | return main.FALSE |
| 6538 | except AssertionError: |
| 6539 | main.log.exception( "" ) |
| 6540 | return None |
| 6541 | except TypeError: |
| 6542 | main.log.exception( self.name + ": Object not as expected" ) |
| 6543 | return None |
| 6544 | except pexpect.EOF: |
| 6545 | main.log.error( self.name + ": EOF exception found" ) |
| 6546 | main.log.error( self.name + ": " + self.handle.before ) |
| 6547 | main.cleanAndExit() |
| 6548 | except Exception: |
| 6549 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6550 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6551 | |
| 6552 | def netcfg( self, jsonFormat=True, args="" ): |
| 6553 | """ |
| 6554 | Run netcfg cli command with given args |
| 6555 | """ |
| 6556 | try: |
| 6557 | cmdStr = "netcfg" |
| 6558 | if jsonFormat: |
| 6559 | cmdStr = cmdStr + " -j" |
| 6560 | if args: |
| 6561 | cmdStr = cmdStr + " " + str( args ) |
| 6562 | handle = self.sendline( cmdStr ) |
| 6563 | assert handle is not None, "Error in sendline" |
| 6564 | assert "Command not found:" not in handle, handle |
| 6565 | assert "Unsupported command:" not in handle, handle |
| 6566 | assert "Error executing command" not in handle, handle |
| 6567 | return handle |
| 6568 | except AssertionError: |
| 6569 | main.log.exception( "" ) |
| 6570 | return None |
| 6571 | except TypeError: |
| 6572 | main.log.exception( self.name + ": Object not as expected" ) |
| 6573 | return None |
| 6574 | except pexpect.EOF: |
| 6575 | main.log.error( self.name + ": EOF exception found" ) |
| 6576 | main.log.error( self.name + ": " + self.handle.before ) |
| 6577 | main.cleanAndExit() |
| 6578 | except Exception: |
| 6579 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6580 | main.cleanAndExit() |
| 6581 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6582 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6583 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6584 | 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] | 6585 | Options: |
| 6586 | sAddr: IP address of the source host |
| 6587 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6588 | ipv6: True if hosts are IPv6 |
| 6589 | verbose: return verbose t3 output if True |
| 6590 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6591 | """ |
| 6592 | try: |
| 6593 | # Collect information of both hosts from onos |
| 6594 | hosts = self.hosts() |
| 6595 | hosts = json.loads( hosts ) |
| 6596 | sHost = None |
| 6597 | dHost = None |
| 6598 | for host in hosts: |
| 6599 | if sAddr in host[ "ipAddresses" ]: |
| 6600 | sHost = host |
| 6601 | elif dAddr in host[ "ipAddresses" ]: |
| 6602 | dHost = host |
| 6603 | if sHost and dHost: |
| 6604 | break |
| 6605 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6606 | cmdList = [] |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6607 | if simple: |
| 6608 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6609 | cmdStr = "t3-troubleshoot-simple" |
| 6610 | if verbose: |
| 6611 | cmdStr += " -vv" |
| 6612 | if ipv6: |
| 6613 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6614 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6615 | cmdList.append( cmdStr ) |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6616 | else: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6617 | for location in sHost[ "locations" ]: |
| 6618 | cmdStr = "t3-troubleshoot" |
| 6619 | if verbose: |
| 6620 | cmdStr += " -vv" |
| 6621 | if ipv6: |
| 6622 | cmdStr += " -et ipv6" |
| 6623 | cmdStr += " -s " + str( sAddr ) |
| 6624 | cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] ) |
| 6625 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6626 | if sHost[ "vlan" ] != "None": |
| 6627 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6628 | cmdStr += " -d " + str( dAddr ) |
| 6629 | netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) ) |
| 6630 | netcfg = json.loads( netcfg ) |
| 6631 | assert netcfg, "Failed to get netcfg" |
| 6632 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
| 6633 | cmdList.append( cmdStr ) |
| 6634 | return cmdList |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6635 | except AssertionError: |
| 6636 | main.log.exception( "" ) |
| 6637 | return None |
| 6638 | except ( KeyError, TypeError ): |
| 6639 | main.log.exception( self.name + ": Object not as expected" ) |
| 6640 | return None |
| 6641 | except Exception: |
| 6642 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6643 | main.cleanAndExit() |
| 6644 | |
| 6645 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6646 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6647 | 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] | 6648 | Options: |
| 6649 | sAddr: IP address of the source host |
| 6650 | dAddr: IP address of the destination host |
| 6651 | """ |
| 6652 | try: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6653 | cmdList = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6654 | assert cmdList is not None, "composeT3Command returned None" |
| 6655 | t3Output = "" |
| 6656 | for cmdStr in cmdList: |
| 6657 | handle = self.sendline( cmdStr ) |
| 6658 | assert handle is not None, "Error in sendline" |
| 6659 | assert "Command not found:" not in handle, handle |
| 6660 | assert "Unsupported command:" not in handle, handle |
| 6661 | assert "Error executing command" not in handle, handle |
| 6662 | assert "Tracing packet" in handle |
| 6663 | t3Output += handle |
| 6664 | return t3Output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6665 | except AssertionError: |
| 6666 | main.log.exception( "" ) |
| 6667 | return None |
| 6668 | except pexpect.EOF: |
| 6669 | main.log.error( self.name + ": EOF exception found" ) |
| 6670 | main.log.error( self.name + ": " + self.handle.before ) |
| 6671 | main.cleanAndExit() |
| 6672 | except Exception: |
| 6673 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6674 | main.cleanAndExit() |