andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
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 | """ |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 39 | import ipaddress |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 40 | import pexpect |
| 41 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 42 | import json |
| 43 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 44 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 45 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 46 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 47 | from core.graph import Graph |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 48 | from cStringIO import StringIO |
| 49 | from itertools import izip |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 50 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 51 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 52 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 53 | def __init__( self ): |
| 54 | """ |
| 55 | Initialize client |
| 56 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 57 | self.name = None |
| 58 | self.home = None |
| 59 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 60 | self.karafUser = None |
| 61 | self.karafPass = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 62 | self.karafPort = None |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 63 | self.karafTimeout = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 64 | self.address = None |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 65 | self.karafPromptUser = None |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 66 | self.dockerPrompt = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 67 | self.graph = Graph() |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 68 | super( OnosCliDriver, self ).__init__() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 69 | |
| 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 ] |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 85 | elif key == "docker_prompt": |
| 86 | self.dockerPrompt = self.options[ key ] |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 87 | elif key == "karaf_timeout": |
| 88 | self.karafTimeout = self.options[ key ] |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 89 | elif key == "karaf_port": |
| 90 | self.karafPort = self.options[ key ] |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 91 | elif key == "karafPromptUser": |
| 92 | self.karafPromptUser = self.options[ key ] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 93 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 94 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 95 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 96 | self.karafPort = self.checkOptions( self.karafPort, 8101 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 97 | self.dockerPrompt = self.checkOptions( self.dockerPrompt, "~/onos#" ) |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 98 | self.karafTimeout = self.checkOptions( self.karafTimeout, 7200000 ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 99 | |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 100 | self.karafPrompt = self.karafPromptUser + "@root >" |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 101 | for key in self.options: |
| 102 | if key == 'onosIp': |
| 103 | self.onosIp = self.options[ 'onosIp' ] |
| 104 | break |
| 105 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 106 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 107 | |
| 108 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 109 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 110 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 111 | else: |
| 112 | main.log.info( self.name + |
| 113 | ": Trying to connect to " + |
| 114 | self.ip_address ) |
| 115 | |
| 116 | except KeyError: |
| 117 | main.log.info( "Invalid host name," + |
| 118 | " connecting to local host instead" ) |
| 119 | self.ip_address = 'localhost' |
| 120 | except Exception as inst: |
| 121 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 122 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 123 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 124 | user_name=self.user_name, |
| 125 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 126 | port=self.port, |
| 127 | pwd=self.pwd, |
| 128 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 129 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 130 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 131 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 132 | if self.handle: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 133 | self.address = self.ip_address |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 135 | else: |
| 136 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 137 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 138 | except TypeError: |
| 139 | main.log.exception( self.name + ": Object not as expected" ) |
| 140 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 141 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 142 | main.log.error( self.name + ": EOF exception found" ) |
| 143 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 144 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 145 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 146 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 147 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 148 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 149 | def disconnect( self ): |
| 150 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 151 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 152 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 153 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 154 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 155 | if self.handle: |
| 156 | i = self.logout() |
| 157 | if i == main.TRUE: |
| 158 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 159 | for l in range( 3 ): |
| 160 | p = self.handle.expect( [ self.prompt, self.dockerPrompt ] ) |
| 161 | if p == 1: |
| 162 | self.inDocker = False |
| 163 | self.handle.sendline( "exit" ) |
| 164 | j = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=3 ) |
| 165 | if j == 0: |
| 166 | return response |
| 167 | response = main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 168 | except TypeError: |
| 169 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 170 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 171 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 172 | main.log.error( self.name + ": EOF exception found" ) |
| 173 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 174 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 175 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 176 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 177 | except Exception: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 178 | main.log.exception( self.name + ": disconnection failed from the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 179 | response = main.FALSE |
| 180 | return response |
| 181 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 182 | def logout( self ): |
| 183 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 184 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 185 | Returns main.TRUE if exited CLI and |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 186 | main.FALSE on timeout (not guranteed you are disconnected) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 187 | None on TypeError |
| 188 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 189 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 190 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 191 | if self.handle: |
| 192 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 193 | i = self.handle.expect( [ self.karafPrompt, |
| 194 | self.Prompt(), |
| 195 | pexpect.TIMEOUT ], |
| 196 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 197 | timeout=10 ) |
| 198 | if i == 0: # In ONOS CLI |
| 199 | self.handle.sendline( "logout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 200 | j = self.handle.expect( [ self.Prompt(), |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 201 | "Command not found:", |
| 202 | pexpect.TIMEOUT ] ) |
| 203 | if j == 0: # Successfully logged out |
| 204 | return main.TRUE |
| 205 | elif j == 1 or j == 2: |
| 206 | # ONOS didn't fully load, and logout command isn't working |
| 207 | # or the command timed out |
| 208 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 209 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 210 | self.handle.expect( self.Prompt() ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 211 | except pexpect.TIMEOUT: |
| 212 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 213 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 214 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 215 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 216 | repr( self.handle.before ) ) |
| 217 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 218 | elif i == 1: # not in CLI |
| 219 | return main.TRUE |
steven30801 | e42f1fb | 2019-01-17 11:31:45 +0800 | [diff] [blame] | 220 | elif i == 2: # Timeout |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 221 | return main.FALSE |
| 222 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 223 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 224 | except TypeError: |
| 225 | main.log.exception( self.name + ": Object not as expected" ) |
| 226 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 227 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 228 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 229 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 230 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 231 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 232 | main.log.error( self.name + |
| 233 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 234 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 235 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 236 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 237 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 238 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 239 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 240 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 241 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 242 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 243 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 244 | try: |
| 245 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 246 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 247 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 248 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 249 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 250 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 251 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 252 | # and that this driver will have to change accordingly |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 253 | self.handle.expect( str( cellname ) ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 254 | handleBefore = self.handle.before |
| 255 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 256 | # Get the rest of the handle |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 257 | self.handle.sendline( "" ) |
| 258 | self.handle.expect( self.prompt ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 259 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 260 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 261 | main.log.info( "Cell call returned: " + handleBefore + |
| 262 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 263 | |
| 264 | return main.TRUE |
| 265 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 266 | except TypeError: |
| 267 | main.log.exception( self.name + ": Object not as expected" ) |
| 268 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 269 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 270 | main.log.error( self.name + ": eof exception found" ) |
| 271 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 272 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 273 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 274 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 275 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 276 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 277 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 278 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 279 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 280 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 281 | by user would be used to set the current karaf shell idle timeout. |
| 282 | 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] | 283 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 284 | Below is an example to start a session with 60 seconds idle timeout |
| 285 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 286 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 287 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 288 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 289 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 290 | Note: karafTimeout is left as str so that this could be read |
| 291 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 292 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 293 | self.onosIp = ONOSIp |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 294 | self.address = self.onosIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 295 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 296 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 297 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 298 | x = self.handle.expect( [ self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 299 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 300 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 301 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 302 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 303 | # Not in CLI so login |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 304 | if self.inDocker: |
| 305 | # The Docker does not have all the wrapper scripts |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 306 | startCliCommand = "ssh -p %s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null %s@localhost" % ( self.karafPort, self.karafUser ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 307 | elif waitForStart: |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 308 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 309 | startCliCommand = "onos-wait-for-start " + str( ONOSIp ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 310 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 311 | startCliCommand = "onos " + str( ONOSIp ) |
| 312 | self.handle.sendline( startCliCommand ) |
| 313 | tries = 0 |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 314 | timeoutSet = False |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 315 | while tries < 5: |
| 316 | i = self.handle.expect( [ |
| 317 | self.karafPrompt, |
| 318 | "Password:", |
| 319 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 320 | |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 321 | if i == 0: |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 322 | if not karafTimeout or timeoutSet: |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 323 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
| 324 | return main.TRUE |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 325 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 326 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 327 | "config:property-set -p org.apache.karaf.shell\ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 328 | sshIdleTimeout " + |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 329 | str( karafTimeout ) ) |
| 330 | self.handle.expect( "closed by remote host" ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 331 | self.handle.expect( self.Prompt() ) |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 332 | self.handle.sendline( startCliCommand ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 333 | timeoutSet = True |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 334 | elif i == 1: |
| 335 | main.log.info( str( ONOSIp ) + " CLI asking for password" ) |
| 336 | main.log.debug( "Sending %s" % self.karafPass ) |
| 337 | self.handle.sendline( self.karafPass ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 338 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 339 | # If failed, send ctrl+c to process and try again |
| 340 | main.log.info( "Starting CLI failed. Retrying..." ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 341 | time.sleep( 5 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 342 | self.handle.send( "\x03" ) |
| 343 | self.handle.sendline( startCliCommand ) |
| 344 | tries += 1 |
| 345 | main.log.error( "Connection to CLI " + str( ONOSIp ) + " timeout" ) |
| 346 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 347 | except TypeError: |
| 348 | main.log.exception( self.name + ": Object not as expected" ) |
| 349 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 350 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 351 | main.log.error( self.name + ": EOF exception found" ) |
| 352 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 353 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 354 | except Exception: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 355 | main.log.debug( self.handle.before + str( self.handle.after ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 356 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 357 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 358 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 359 | def startCellCli( self, karafTimeout="", |
| 360 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 361 | """ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 362 | Start CLI on onos cell handle. |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 363 | |
| 364 | karafTimeout is an optional argument. karafTimeout value passed |
| 365 | by user would be used to set the current karaf shell idle timeout. |
| 366 | Note that when ever this property is modified the shell will exit and |
| 367 | the subsequent login would reflect new idle timeout. |
| 368 | Below is an example to start a session with 60 seconds idle timeout |
| 369 | ( input value is in milliseconds ): |
| 370 | |
| 371 | tValue = "60000" |
| 372 | |
| 373 | Note: karafTimeout is left as str so that this could be read |
| 374 | and passed to startOnosCli from PARAMS file as str. |
| 375 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 376 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 377 | try: |
| 378 | self.handle.sendline( "" ) |
| 379 | x = self.handle.expect( [ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 380 | self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 381 | |
| 382 | if x == 1: |
| 383 | main.log.info( "ONOS cli is already running" ) |
| 384 | return main.TRUE |
| 385 | |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 386 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 387 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 388 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 389 | self.karafPrompt, |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 390 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 391 | |
| 392 | if i == 0: |
| 393 | main.log.info( self.name + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 394 | if karafTimeout: # FIXME: This doesn't look right |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 395 | self.handle.sendline( |
| 396 | "config:property-set -p org.apache.karaf.shell\ |
| 397 | sshIdleTimeout " + |
| 398 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 399 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 400 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 401 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 402 | return main.TRUE |
| 403 | else: |
| 404 | # If failed, send ctrl+c to process and try again |
| 405 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 406 | self.handle.send( "\x03" ) |
| 407 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 408 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 409 | timeout=30 ) |
| 410 | if i == 0: |
| 411 | main.log.info( self.name + " CLI Started " + |
| 412 | "successfully after retry attempt" ) |
| 413 | if karafTimeout: |
| 414 | self.handle.sendline( |
| 415 | "config:property-set -p org.apache.karaf.shell\ |
| 416 | sshIdleTimeout " + |
| 417 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 418 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 419 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 420 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 421 | return main.TRUE |
| 422 | else: |
| 423 | main.log.error( "Connection to CLI " + |
| 424 | self.name + " timeout" ) |
| 425 | return main.FALSE |
| 426 | |
| 427 | except TypeError: |
| 428 | main.log.exception( self.name + ": Object not as expected" ) |
| 429 | return None |
| 430 | except pexpect.EOF: |
| 431 | main.log.error( self.name + ": EOF exception found" ) |
| 432 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 433 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 434 | except Exception: |
| 435 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 436 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 437 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 438 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 439 | """ |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 440 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 441 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 442 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 443 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 444 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 445 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 446 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 447 | """ |
| 448 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 449 | lvlStr = "" |
| 450 | if level: |
| 451 | lvlStr = "--level=" + level |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 452 | handle = self.sendline( "log:log " + lvlStr + " " + cmdStr, noExit=noExit ) |
| 453 | assert handle is not None, "Error in sendline" |
| 454 | assert "Command not found:" not in handle, handle |
| 455 | if re.search( "Error", handle ): |
| 456 | main.log.error( self.name + ": Error in logging message" ) |
| 457 | main.log.error( handle ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 458 | return main.FALSE |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 459 | else: |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 460 | return main.TRUE |
| 461 | except AssertionError: |
| 462 | main.log.exception( "" ) |
| 463 | return None |
| 464 | except TypeError: |
| 465 | main.log.exception( self.name + ": Object not as expected" ) |
| 466 | return None |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 467 | except pexpect.EOF: |
| 468 | main.log.error( self.name + ": EOF exception found" ) |
| 469 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 470 | if noExit: |
| 471 | main.cleanup() |
| 472 | return None |
| 473 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 474 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 475 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 476 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 477 | if noExit: |
| 478 | main.cleanup() |
| 479 | return None |
| 480 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 481 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 482 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 483 | def clearBuffer( self, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 484 | """ |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 485 | Test cli connection and clear any left over output in the buffer |
| 486 | Optional Arguments: |
| 487 | debug - Defaults to False. If True, will enable debug logging. |
| 488 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 489 | before a timeout. |
| 490 | 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] | 491 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 492 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 493 | # Try to reconnect if disconnected from cli |
| 494 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 495 | i = self.handle.expect( [ self.karafPrompt, |
| 496 | self.Prompt(), |
| 497 | pexpect.TIMEOUT ] ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 498 | response = self.handle.before |
Jon Hall | a16b4db | 2021-10-20 14:11:59 -0700 | [diff] [blame] | 499 | if i == 1: # Not in ONOS CLI |
| 500 | # FIXME: This isn't really the correct place for this, but it works for now |
| 501 | # Check if port-forward session is still up first |
| 502 | if hasattr( main, "Cluster"): |
| 503 | ctrl = None |
| 504 | for c in main.Cluster.controllers: |
| 505 | if c.CLI is self: |
| 506 | ctrl = c |
| 507 | break |
| 508 | if not ctrl: |
| 509 | main.log.warn( self.name + ": Could not find this node in Cluster. Can't check port-forward status" ) |
| 510 | elif ctrl.k8s: |
| 511 | ctrl.k8s.checkPortForward( ctrl.k8s.podName, |
| 512 | kubeconfig=ctrl.k8s.kubeConfig, |
| 513 | namespace=main.params[ 'kubernetes' ][ 'namespace' ] ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 514 | main.log.error( self.name + ": onos cli session closed. " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 515 | if self.onosIp: |
| 516 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 517 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 518 | if reconnectResult: |
| 519 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 520 | else: |
| 521 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 522 | if noExit: |
| 523 | return None |
| 524 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 525 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 526 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 527 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 528 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 529 | main.log.warn( "Timeout when testing cli responsiveness" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 530 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 531 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 532 | self.handle.expect( self.karafPrompt ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 533 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 534 | response += self.handle.before |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 535 | if debug: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 536 | main.log.debug( self.name + ": Raw output from sending ''" ) |
| 537 | main.log.debug( self.name + ": " + repr( response ) ) |
| 538 | except pexpect.TIMEOUT: |
| 539 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 540 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 541 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 542 | self.handle.expect( self.karafPrompt ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 543 | return None |
| 544 | except pexpect.EOF: |
| 545 | main.log.error( self.name + ": EOF exception found" ) |
| 546 | main.log.error( self.name + ": " + self.handle.before ) |
| 547 | if noExit: |
| 548 | return None |
| 549 | else: |
| 550 | main.cleanAndExit() |
| 551 | except Exception: |
| 552 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 553 | if noExit: |
| 554 | return None |
| 555 | else: |
| 556 | main.cleanAndExit() |
| 557 | |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 558 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True, expectJson=False ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 559 | """ |
| 560 | A wrapper around pexpect's sendline/expect. Will return all the output from a given command |
| 561 | |
| 562 | Required Arguments: |
| 563 | cmdStr - String to send to the pexpect session |
| 564 | |
| 565 | Optional Arguments: |
| 566 | showResponse - Defaults to False. If True will log the response. |
| 567 | debug - Defaults to False. If True, will enable debug logging. |
| 568 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 569 | before a timeout. |
| 570 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 571 | closed channel, but instead return None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 572 | 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] | 573 | |
| 574 | Warning: There are no sanity checking to commands sent using this method. |
| 575 | |
| 576 | """ |
| 577 | try: |
| 578 | # Try to reconnect if disconnected from cli |
| 579 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
| 580 | if debug: |
| 581 | # NOTE: This adds an average of .4 seconds per call |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 582 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 583 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 584 | self.handle.sendline( cmdStr ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 585 | self.handle.expect( self.karafPrompt, timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 586 | response = self.handle.before |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 587 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 588 | + self.name + "." ) |
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 + ": Raw output" ) |
| 591 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 592 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 593 | response = self.cleanOutput( response, debug=debug ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 594 | # Remove control codes from karaf 4.2.1 |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 595 | karafEscape = re.compile( r"('(0|1)~\'|\r\r\r\n\x1b\[A\x1b\[79C(x|\s)?|\x1b(>|=~?)|\x1b\[90m~)" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 596 | response = karafEscape.sub( '', response ) |
| 597 | if debug: |
| 598 | main.log.debug( self.name + ": karafEscape output" ) |
| 599 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 600 | # Remove ANSI color control strings from output |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 601 | response = self.cleanOutput( response, debug ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 602 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 603 | # Remove ANSI color control strings from output |
Jon Hall | cf31d0f | 2018-12-13 11:18:48 -0800 | [diff] [blame] | 604 | # NOTE: karaf is sometimes adding a single character then two |
| 605 | # backspaces and sometimes adding 2 characters with 2 backspaces?? |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 606 | backspaceEscape = re.compile( r'((.|\s)\x08)' ) |
| 607 | unchanged = False |
| 608 | while not unchanged: |
| 609 | old = response |
| 610 | response = backspaceEscape.sub( '', response, count=1 ) |
| 611 | if debug: |
| 612 | main.log.debug( self.name + ": backspaceEscape output" ) |
| 613 | main.log.debug( self.name + ": " + repr( response ) ) |
| 614 | unchanged = old == response |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 615 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 616 | # Remove extra return chars that get added |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 617 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 618 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 619 | main.log.debug( self.name + ": Removed extra returns " + |
| 620 | "from output" ) |
| 621 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 622 | |
| 623 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 624 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 625 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 626 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 627 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 628 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 629 | # parse for just the output, remove the cmd from response |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 630 | cmdPattern = cmdStr.strip() |
| 631 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 632 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 633 | if relaxedRegex: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 634 | cmdPattern = cmdPattern.split( '|' )[ -1 ].strip() |
| 635 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 636 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 637 | # This was added because karaf 4.2 is stripping some characters from the command echo |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 638 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 639 | if expectJson: |
| 640 | main.log.warn( "Relaxed Regex: Searching for a json string amongst the output" ) |
| 641 | jsonPattern = r'\{.*\}' |
| 642 | match = re.search( jsonPattern, output[ 0 ] ) |
| 643 | if match: |
| 644 | output = [ '' , match.group( 0 ) ] # We expect a list with the second element to be the output |
Jon Hall | 39e3ffe | 2018-12-05 11:40:29 -0800 | [diff] [blame] | 645 | if len( output ) < 2: |
| 646 | main.log.warn( "Relaxing regex match to last 5 characters of the sent command" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 647 | cmdPattern = cmdPattern[ -5: ] |
| 648 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 649 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
| 650 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 651 | else: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 652 | output = response.split( cmdPattern, 1 ) |
| 653 | if len( output ) < 2: # TODO: Should we do this without the relaxedRegex flag? |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 654 | main.log.warn( "Relaxing regex match to last 5 characters of the sent command" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 655 | output = response.split( cmdPattern[ -5: ], 1 ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 656 | if output: |
| 657 | if debug: |
| 658 | main.log.debug( self.name + ": split output" ) |
| 659 | for r in output: |
| 660 | main.log.debug( self.name + ": " + repr( r ) ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 661 | if len( output ) == 1: |
| 662 | main.log.error( "Could not remove sent command echo from output" ) |
| 663 | return output |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 664 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 665 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 666 | main.log.info( "Response from ONOS: {}".format( output ) ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 667 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 668 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 669 | except pexpect.TIMEOUT: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 670 | main.log.error( self.name + ": ONOS timeout" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 671 | if debug: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 672 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 6b5e5ba | 2019-01-31 15:32:40 -0800 | [diff] [blame] | 673 | self.exitFromCmd( self.karafPrompt, 100 ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 674 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 675 | except IndexError: |
| 676 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 677 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 678 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 679 | except TypeError: |
| 680 | main.log.exception( self.name + ": Object not as expected" ) |
| 681 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 682 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 683 | main.log.error( self.name + ": EOF exception found" ) |
| 684 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 685 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 686 | return None |
| 687 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 688 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 689 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 690 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 691 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 692 | return None |
| 693 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 694 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 695 | |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 696 | def lineCount( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
| 697 | """ |
| 698 | A wrapper around sendline(). Will return the number of lines returned or None on error |
| 699 | |
| 700 | Required Arguments: |
| 701 | cmdStr - String to send to the pexpect session |
| 702 | |
| 703 | Optional Arguments: |
| 704 | showResponse - Defaults to False. If True will log the response. |
| 705 | debug - Defaults to False. If True, will enable debug logging. |
| 706 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 707 | before a timeout. |
| 708 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 709 | closed channel, but instead return None |
| 710 | 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. |
| 711 | |
| 712 | Warning: There are no sanity checking to commands sent using this method. |
| 713 | |
| 714 | """ |
| 715 | try: |
| 716 | numLines = self.sendline( cmdStr, showResponse, debug, timeout, noExit, relaxedRegex ) |
You Wang | 0ce8e0c | 2019-02-22 12:22:26 -0800 | [diff] [blame] | 717 | parsed = re.search( "(\d+)", numLines ) |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 718 | if not parsed: |
| 719 | main.log.error( "Warning, output of karaf's wc may have changed" ) |
| 720 | return None |
| 721 | return parsed.group( 1 ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 722 | except IndexError: |
| 723 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 724 | main.log.debug( "response: {}".format( repr( numLines ) ) ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 725 | return None |
| 726 | except TypeError: |
| 727 | main.log.exception( self.name + ": Object not as expected" ) |
| 728 | return None |
| 729 | except Exception: |
| 730 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 731 | if noExit: |
| 732 | return None |
| 733 | else: |
| 734 | main.cleanAndExit() |
| 735 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 736 | # IMPORTANT NOTE: |
| 737 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 738 | # the cli command changing 'a:b' with 'aB'. |
| 739 | # Ex ) onos:topology > onosTopology |
| 740 | # onos:links > onosLinks |
| 741 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 742 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 743 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 744 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 745 | Adds a new cluster node by ID and address information. |
| 746 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 747 | * nodeId |
| 748 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 749 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 750 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 751 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 752 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 753 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 754 | str( ONOSIp ) + " " + str( tcpPort ) |
| 755 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 756 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 757 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 758 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 759 | main.log.error( self.name + ": Error in adding node" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 760 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 761 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 762 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 763 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 764 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 765 | except AssertionError: |
| 766 | main.log.exception( "" ) |
| 767 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 768 | except TypeError: |
| 769 | main.log.exception( self.name + ": Object not as expected" ) |
| 770 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 771 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 772 | main.log.error( self.name + ": EOF exception found" ) |
| 773 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 774 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 775 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 776 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 777 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 778 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 779 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 780 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 781 | Removes a cluster by ID |
| 782 | Issues command: 'remove-node [<node-id>]' |
| 783 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 784 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 785 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 786 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 787 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 788 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 789 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 790 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 791 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 792 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 793 | main.log.error( self.name + ": Error in removing node" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 794 | main.log.error( handle ) |
| 795 | return main.FALSE |
| 796 | else: |
| 797 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 798 | except AssertionError: |
| 799 | main.log.exception( "" ) |
| 800 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 801 | except TypeError: |
| 802 | main.log.exception( self.name + ": Object not as expected" ) |
| 803 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 804 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 805 | main.log.error( self.name + ": EOF exception found" ) |
| 806 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 807 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 808 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 809 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 810 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 811 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 812 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 813 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 814 | List the nodes currently visible |
| 815 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 816 | Optional argument: |
| 817 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 818 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 819 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 820 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 821 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 822 | cmdStr += " -j" |
| 823 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 824 | assert output is not None, "Error in sendline" |
Jon Hall | e37bd1f | 2020-09-10 12:16:41 -0700 | [diff] [blame] | 825 | # "Command not found" or "Service org.onosproject.security.AuditService not found" |
| 826 | assert "not found" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 827 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 828 | except AssertionError: |
| 829 | main.log.exception( "" ) |
| 830 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 831 | except TypeError: |
| 832 | main.log.exception( self.name + ": Object not as expected" ) |
| 833 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 834 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 835 | main.log.error( self.name + ": EOF exception found" ) |
| 836 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 837 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 838 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 839 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 840 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 841 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 842 | def topology( self ): |
| 843 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 844 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 845 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 846 | Return: |
| 847 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 848 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 849 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 850 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 851 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 852 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 853 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 854 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 855 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 856 | except AssertionError: |
| 857 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 858 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 859 | except TypeError: |
| 860 | main.log.exception( self.name + ": Object not as expected" ) |
| 861 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 862 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 863 | main.log.error( self.name + ": EOF exception found" ) |
| 864 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 865 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 866 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 867 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 868 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 869 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 870 | def deviceRemove( self, deviceId ): |
| 871 | """ |
| 872 | Removes particular device from storage |
| 873 | |
| 874 | TODO: refactor this function |
| 875 | """ |
| 876 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 877 | cmdStr = "device-remove " + str( deviceId ) |
| 878 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 879 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 880 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 881 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 882 | main.log.error( self.name + ": Error in removing device" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 883 | main.log.error( handle ) |
| 884 | return main.FALSE |
| 885 | else: |
| 886 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 887 | except AssertionError: |
| 888 | main.log.exception( "" ) |
| 889 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 890 | except TypeError: |
| 891 | main.log.exception( self.name + ": Object not as expected" ) |
| 892 | return None |
| 893 | except pexpect.EOF: |
| 894 | main.log.error( self.name + ": EOF exception found" ) |
| 895 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 896 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 897 | except Exception: |
| 898 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 899 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 900 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 901 | def devices( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 902 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 903 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 904 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 905 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 906 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 907 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 908 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 909 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 910 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 911 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 912 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 913 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 914 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 915 | except AssertionError: |
| 916 | main.log.exception( "" ) |
| 917 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 918 | except TypeError: |
| 919 | main.log.exception( self.name + ": Object not as expected" ) |
| 920 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 921 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 922 | main.log.error( self.name + ": EOF exception found" ) |
| 923 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 924 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 925 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 926 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 927 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 928 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 929 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 930 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 931 | This balances the devices across all controllers |
| 932 | by issuing command: 'onos> onos:balance-masters' |
| 933 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 934 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 935 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 936 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 937 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 938 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 939 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 940 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 941 | main.log.error( self.name + ": Error in balancing masters" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 942 | main.log.error( handle ) |
| 943 | return main.FALSE |
| 944 | else: |
| 945 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 946 | except AssertionError: |
| 947 | main.log.exception( "" ) |
| 948 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 949 | except TypeError: |
| 950 | main.log.exception( self.name + ": Object not as expected" ) |
| 951 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 952 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 953 | main.log.error( self.name + ": EOF exception found" ) |
| 954 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 955 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 956 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 957 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 958 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 959 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 960 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 961 | """ |
| 962 | Returns the output of the masters command. |
| 963 | Optional argument: |
| 964 | * jsonFormat - boolean indicating if you want output in json |
| 965 | """ |
| 966 | try: |
| 967 | cmdStr = "onos:masters" |
| 968 | if jsonFormat: |
| 969 | cmdStr += " -j" |
| 970 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 971 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 972 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 973 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 974 | except AssertionError: |
| 975 | main.log.exception( "" ) |
| 976 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 977 | except TypeError: |
| 978 | main.log.exception( self.name + ": Object not as expected" ) |
| 979 | return None |
| 980 | except pexpect.EOF: |
| 981 | main.log.error( self.name + ": EOF exception found" ) |
| 982 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 983 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 984 | except Exception: |
| 985 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 986 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 987 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 988 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 989 | """ |
| 990 | Uses the master command to check that the devices' leadership |
| 991 | is evenly divided |
| 992 | |
| 993 | Dependencies: checkMasters() and summary() |
| 994 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 995 | Returns main.TRUE if the devices are balanced |
| 996 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 997 | Exits on Exception |
| 998 | Returns None on TypeError |
| 999 | """ |
| 1000 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1001 | summaryOutput = self.summary() |
| 1002 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 1003 | except ( TypeError, ValueError ): |
| 1004 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 1005 | return None |
| 1006 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1007 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1008 | mastersOutput = self.checkMasters() |
| 1009 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1010 | first = masters[ 0 ][ "size" ] |
| 1011 | for master in masters: |
| 1012 | totalOwnedDevices += master[ "size" ] |
| 1013 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 1014 | main.log.error( "Mastership not balanced" ) |
| 1015 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 1016 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 1017 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1018 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1019 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1020 | except ( TypeError, ValueError ): |
| 1021 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1022 | return None |
| 1023 | except pexpect.EOF: |
| 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() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1027 | except Exception: |
| 1028 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1029 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1030 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1031 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1032 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1033 | Lists all core links |
| 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 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1037 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1038 | cmdStr = "links" |
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" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1041 | handle = self.sendline( cmdStr, timeout=timeout ) |
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 | e821748 | 2014-10-17 13:49:14 -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 | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1058 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1059 | def ports( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1060 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1061 | Lists all ports |
| 1062 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1063 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1064 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1065 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1066 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1067 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1068 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1069 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1070 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1071 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1072 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1073 | except AssertionError: |
| 1074 | main.log.exception( "" ) |
| 1075 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1076 | except TypeError: |
| 1077 | main.log.exception( self.name + ": Object not as expected" ) |
| 1078 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1079 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1080 | main.log.error( self.name + ": EOF exception found" ) |
| 1081 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1082 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1083 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1084 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1085 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1086 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1087 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1088 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1089 | Lists all devices and the controllers with roles assigned to them |
| 1090 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1091 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1092 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1093 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1094 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1095 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1096 | cmdStr += " -j" |
| 1097 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1098 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1099 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1100 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1101 | except AssertionError: |
| 1102 | main.log.exception( "" ) |
| 1103 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1104 | except TypeError: |
| 1105 | main.log.exception( self.name + ": Object not as expected" ) |
| 1106 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1107 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1108 | main.log.error( self.name + ": EOF exception found" ) |
| 1109 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1110 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1111 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1112 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1113 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1114 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1115 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1116 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1117 | Given the a string containing the json representation of the "roles" |
| 1118 | cli command and a partial or whole device id, returns a json object |
| 1119 | containing the roles output for the first device whose id contains |
| 1120 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1121 | |
| 1122 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1123 | A dict of the role assignments for the given device or |
| 1124 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1125 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1126 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1127 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1128 | return None |
| 1129 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1130 | rawRoles = self.roles() |
| 1131 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1132 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1133 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1134 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1135 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1136 | return device |
| 1137 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1138 | except ( TypeError, ValueError ): |
| 1139 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1140 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 1141 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1142 | main.log.error( self.name + ": EOF exception found" ) |
| 1143 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1144 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1145 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1146 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1147 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1148 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1149 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1150 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1151 | Iterates through each device and checks if there is a master assigned |
| 1152 | Returns: main.TRUE if each device has a master |
| 1153 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1154 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1155 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1156 | rawRoles = self.roles() |
| 1157 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1158 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1159 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1160 | # print device |
| 1161 | if device[ 'master' ] == "none": |
| 1162 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1163 | return main.FALSE |
| 1164 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1165 | except ( TypeError, ValueError ): |
| 1166 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1167 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1168 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1169 | main.log.error( self.name + ": EOF exception found" ) |
| 1170 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1171 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1172 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1173 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1174 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1175 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1176 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1177 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1178 | Returns string of paths, and the cost. |
| 1179 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1180 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1181 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1182 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1183 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1184 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1185 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1186 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1187 | main.log.error( self.name + ": Error in getting paths" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1188 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1189 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1190 | path = handle.split( ";" )[ 0 ] |
| 1191 | cost = handle.split( ";" )[ 1 ] |
| 1192 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1193 | except AssertionError: |
| 1194 | main.log.exception( "" ) |
| 1195 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1196 | except TypeError: |
| 1197 | main.log.exception( self.name + ": Object not as expected" ) |
| 1198 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1199 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1200 | main.log.error( self.name + ": EOF exception found" ) |
| 1201 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1202 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1203 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1204 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1205 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1206 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1207 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1208 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1209 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1210 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1211 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1212 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1213 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1214 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1215 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1216 | cmdStr += " -j" |
| 1217 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1218 | if handle: |
| 1219 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1220 | # TODO: Maybe make this less hardcoded |
| 1221 | # ConsistentMap Exceptions |
| 1222 | assert "org.onosproject.store.service" not in handle |
| 1223 | # Node not leader |
| 1224 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1225 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1226 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1227 | main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1228 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1229 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1230 | except TypeError: |
| 1231 | main.log.exception( self.name + ": Object not as expected" ) |
| 1232 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1233 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1234 | main.log.error( self.name + ": EOF exception found" ) |
| 1235 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1236 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1237 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1238 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1239 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1240 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1241 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1242 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1243 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1244 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1245 | 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] | 1246 | partial mac address |
| 1247 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1248 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1249 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1250 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1251 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1252 | return None |
| 1253 | else: |
| 1254 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1255 | rawHosts = self.hosts() |
| 1256 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1257 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1258 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1259 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1260 | if not host: |
| 1261 | pass |
| 1262 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1263 | return host |
| 1264 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1265 | except ( TypeError, ValueError ): |
| 1266 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1267 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1268 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1269 | main.log.error( self.name + ": EOF exception found" ) |
| 1270 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1271 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1272 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1273 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1274 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1275 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1276 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1277 | """ |
| 1278 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1279 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1280 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1281 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1282 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1283 | IMPORTANT: |
| 1284 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1285 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1286 | Furthermore, it assumes that value of VLAN is '-1' |
| 1287 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1288 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1289 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1290 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1291 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1292 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1293 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1294 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1295 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1296 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1297 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1298 | i = iter( str( hostHex ) ) |
| 1299 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1300 | hostHex = hostHex + "/-1" |
| 1301 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1302 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1303 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1304 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1305 | except TypeError: |
| 1306 | main.log.exception( self.name + ": Object not as expected" ) |
| 1307 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1308 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1309 | main.log.error( self.name + ": EOF exception found" ) |
| 1310 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1311 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1312 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1313 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1314 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1315 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1316 | def verifyHostLocation( self, hostIp, location ): |
| 1317 | """ |
| 1318 | Description: |
| 1319 | Verify the host given is discovered in all locations expected |
| 1320 | Required: |
| 1321 | hostIp: IP address of the host |
| 1322 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1323 | Could be a string or list |
| 1324 | Returns: |
| 1325 | main.TRUE if host is discovered on all locations provided |
| 1326 | main.FALSE otherwise |
| 1327 | """ |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1328 | locations = [ location ] if isinstance( location, str ) else location |
| 1329 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1330 | try: |
| 1331 | hosts = self.hosts() |
| 1332 | hosts = json.loads( hosts ) |
| 1333 | targetHost = None |
| 1334 | for host in hosts: |
| 1335 | if hostIp in host[ "ipAddresses" ]: |
| 1336 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1337 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1338 | result = main.TRUE |
| 1339 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1340 | for loc in locations: |
| 1341 | discovered = False |
| 1342 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1343 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1344 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1345 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1346 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1347 | break |
| 1348 | if discovered: |
| 1349 | locationsDiscovered.remove( locDiscovered ) |
| 1350 | else: |
| 1351 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1352 | result = main.FALSE |
| 1353 | if locationsDiscovered: |
| 1354 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1355 | result = main.FALSE |
| 1356 | return result |
| 1357 | except KeyError: |
| 1358 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1359 | return None |
| 1360 | except pexpect.EOF: |
| 1361 | main.log.error( self.name + ": EOF exception found" ) |
| 1362 | main.log.error( self.name + ": " + self.handle.before ) |
| 1363 | main.cleanAndExit() |
| 1364 | except Exception: |
| 1365 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1366 | return None |
| 1367 | |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1368 | def verifyHostIp( self, hostList=[], prefix="", prefixLength=24 ): |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1369 | """ |
| 1370 | Description: |
| 1371 | Verify that all hosts have IP address assigned to them |
| 1372 | Optional: |
| 1373 | hostList: If specified, verifications only happen to the hosts |
| 1374 | in hostList |
| 1375 | prefix: at least one of the ip address assigned to the host |
| 1376 | needs to have the specified prefix |
| 1377 | Returns: |
| 1378 | main.TRUE if all hosts have specific IP address assigned; |
| 1379 | main.FALSE otherwise |
| 1380 | """ |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1381 | try: |
| 1382 | hosts = self.hosts() |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1383 | hosts = json.loads( hosts, "utf-8" ) |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1384 | if not hostList: |
| 1385 | hostList = [ host[ "id" ] for host in hosts ] |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1386 | hostList = [ str( h.lower() ) for h in hostList ] |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1387 | for host in hosts: |
| 1388 | hostId = host[ "id" ] |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1389 | hostId = str( hostId.lower() ) |
| 1390 | match = False |
| 1391 | for onosHostId in hostList: |
| 1392 | if onosHostId == hostId: |
| 1393 | match = True |
| 1394 | if not match: |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1395 | continue |
| 1396 | ipList = host[ "ipAddresses" ] |
| 1397 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1398 | if not ipList: |
| 1399 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1400 | else: |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1401 | # if no hostIp in subnet: Get warning otherwise remove hostId from hostList |
| 1402 | subnetString = u"%s/%s" % (prefix, prefixLength) |
| 1403 | subnet = ipaddress.ip_network( subnetString, strict=False ) |
| 1404 | if not any( ipaddress.ip_address( ip ) in subnet for ip in ipList ): |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1405 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1406 | else: |
| 1407 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1408 | hostList.remove( hostId ) |
| 1409 | if hostList: |
| 1410 | main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 1411 | # Print info for debugging |
| 1412 | main.log.debug( self.name + ": hosts output: " + str( hosts ) ) |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1413 | return main.FALSE |
| 1414 | else: |
| 1415 | return main.TRUE |
| 1416 | except KeyError: |
| 1417 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1418 | return None |
| 1419 | except pexpect.EOF: |
| 1420 | main.log.error( self.name + ": EOF exception found" ) |
| 1421 | main.log.error( self.name + ": " + self.handle.before ) |
| 1422 | main.cleanAndExit() |
| 1423 | except Exception: |
| 1424 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1425 | return None |
| 1426 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1427 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1428 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1429 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1430 | * hostIdOne: ONOS host id for host1 |
| 1431 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1432 | Optional: |
| 1433 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1434 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1435 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1436 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1437 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1438 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1439 | Returns: |
| 1440 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1441 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1442 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1443 | cmdStr = "add-host-intent " |
| 1444 | if vlanId: |
| 1445 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1446 | if setVlan: |
| 1447 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1448 | if encap: |
| 1449 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1450 | if bandwidth: |
| 1451 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1452 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1453 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1454 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1455 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1456 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1457 | main.log.error( self.name + ": Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1458 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1459 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1460 | else: |
| 1461 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1462 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1463 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1464 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1465 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1466 | else: |
| 1467 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1468 | main.log.debug( "Response from ONOS was: " + |
| 1469 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1470 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1471 | except AssertionError: |
| 1472 | main.log.exception( "" ) |
| 1473 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1474 | except TypeError: |
| 1475 | main.log.exception( self.name + ": Object not as expected" ) |
| 1476 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1477 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1478 | main.log.error( self.name + ": EOF exception found" ) |
| 1479 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1480 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1481 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1482 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1483 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1484 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1485 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1486 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -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 | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1490 | Optional: |
| 1491 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1492 | Description: |
| 1493 | Adds an optical intent by specifying an ingress and egress device |
| 1494 | Returns: |
| 1495 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1496 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1497 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1498 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1499 | " " + str( egressDevice ) |
| 1500 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1501 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1502 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1503 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1504 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1505 | main.log.error( self.name + ": Error in adding Optical intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1506 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1507 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1508 | main.log.info( "Optical intent installed between " + |
| 1509 | str( ingressDevice ) + " and " + |
| 1510 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1511 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1512 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1513 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1514 | else: |
| 1515 | main.log.error( "Error, intent ID not found" ) |
| 1516 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1517 | except AssertionError: |
| 1518 | main.log.exception( "" ) |
| 1519 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1520 | except TypeError: |
| 1521 | main.log.exception( self.name + ": Object not as expected" ) |
| 1522 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1523 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1524 | main.log.error( self.name + ": EOF exception found" ) |
| 1525 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1526 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1527 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1528 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1529 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1530 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1531 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1532 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1533 | ingressDevice, |
| 1534 | egressDevice, |
| 1535 | portIngress="", |
| 1536 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1537 | ethType="", |
| 1538 | ethSrc="", |
| 1539 | ethDst="", |
| 1540 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1541 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1542 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1543 | ipProto="", |
| 1544 | ipSrc="", |
| 1545 | ipDst="", |
| 1546 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1547 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1548 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1549 | setVlan="", |
| 1550 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1551 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1552 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1553 | * ingressDevice: device id of ingress device |
| 1554 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1555 | Optional: |
| 1556 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1557 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1558 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1559 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1560 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1561 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1562 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1563 | * ipSrc: specify ip source address |
| 1564 | * ipDst: specify ip destination address |
| 1565 | * tcpSrc: specify tcp source port |
| 1566 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1567 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1568 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1569 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1570 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1571 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1572 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1573 | Returns: |
| 1574 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1575 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1576 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1577 | options developers provide for point-to-point |
| 1578 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1579 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1580 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1581 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1582 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1583 | if ethType: |
| 1584 | cmd += " --ethType " + str( ethType ) |
| 1585 | if ethSrc: |
| 1586 | cmd += " --ethSrc " + str( ethSrc ) |
| 1587 | if ethDst: |
| 1588 | cmd += " --ethDst " + str( ethDst ) |
| 1589 | if bandwidth: |
| 1590 | cmd += " --bandwidth " + str( bandwidth ) |
| 1591 | if lambdaAlloc: |
| 1592 | cmd += " --lambda " |
| 1593 | if ipProto: |
| 1594 | cmd += " --ipProto " + str( ipProto ) |
| 1595 | if ipSrc: |
| 1596 | cmd += " --ipSrc " + str( ipSrc ) |
| 1597 | if ipDst: |
| 1598 | cmd += " --ipDst " + str( ipDst ) |
| 1599 | if tcpSrc: |
| 1600 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1601 | if tcpDst: |
| 1602 | cmd += " --tcpDst " + str( tcpDst ) |
| 1603 | if vlanId: |
| 1604 | cmd += " -v " + str( vlanId ) |
| 1605 | if setVlan: |
| 1606 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1607 | if encap: |
| 1608 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1609 | if protected: |
| 1610 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1611 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1612 | # Check whether the user appended the port |
| 1613 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1614 | if "/" in ingressDevice: |
| 1615 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1616 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1617 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1618 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1619 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1620 | # Would it make sense to throw an exception and exit |
| 1621 | # the test? |
| 1622 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1623 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1624 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1625 | str( ingressDevice ) + "/" +\ |
| 1626 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1627 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1628 | if "/" in egressDevice: |
| 1629 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1630 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1631 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1632 | main.log.error( "You must specify the egress port" ) |
| 1633 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1634 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1635 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1636 | str( egressDevice ) + "/" +\ |
| 1637 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1638 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1639 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1640 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1641 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1642 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1643 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1644 | main.log.error( self.name + ": Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1645 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1646 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1647 | # TODO: print out all the options in this message? |
| 1648 | main.log.info( "Point-to-point intent installed between " + |
| 1649 | str( ingressDevice ) + " and " + |
| 1650 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1651 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1652 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1653 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1654 | else: |
| 1655 | main.log.error( "Error, intent ID not found" ) |
| 1656 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1657 | except AssertionError: |
| 1658 | main.log.exception( "" ) |
| 1659 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1660 | except TypeError: |
| 1661 | main.log.exception( self.name + ": Object not as expected" ) |
| 1662 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1663 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1664 | main.log.error( self.name + ": EOF exception found" ) |
| 1665 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1666 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1667 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1668 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1669 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1670 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1671 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1672 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1673 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1674 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1675 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1676 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1677 | ethType="", |
| 1678 | ethSrc="", |
| 1679 | ethDst="", |
| 1680 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1681 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1682 | ipProto="", |
| 1683 | ipSrc="", |
| 1684 | ipDst="", |
| 1685 | tcpSrc="", |
| 1686 | tcpDst="", |
| 1687 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1688 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1689 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1690 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1691 | partial=False, |
| 1692 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1693 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1694 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1695 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1696 | is same. That is, all ingress devices include port numbers |
| 1697 | with a "/" or all ingress devices could specify device |
| 1698 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1699 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1700 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1701 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1702 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1703 | Optional: |
| 1704 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1705 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1706 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1707 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1708 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1709 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1710 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1711 | * ipSrc: specify ip source address |
| 1712 | * ipDst: specify ip destination address |
| 1713 | * tcpSrc: specify tcp source port |
| 1714 | * tcpDst: specify tcp destination port |
| 1715 | * setEthSrc: action to Rewrite Source MAC Address |
| 1716 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1717 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1718 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1719 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1720 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1721 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1722 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1723 | Returns: |
| 1724 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1725 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1726 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1727 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1728 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1729 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1730 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1731 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1732 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1733 | if ethType: |
| 1734 | cmd += " --ethType " + str( ethType ) |
| 1735 | if ethSrc: |
| 1736 | cmd += " --ethSrc " + str( ethSrc ) |
| 1737 | if ethDst: |
| 1738 | cmd += " --ethDst " + str( ethDst ) |
| 1739 | if bandwidth: |
| 1740 | cmd += " --bandwidth " + str( bandwidth ) |
| 1741 | if lambdaAlloc: |
| 1742 | cmd += " --lambda " |
| 1743 | if ipProto: |
| 1744 | cmd += " --ipProto " + str( ipProto ) |
| 1745 | if ipSrc: |
| 1746 | cmd += " --ipSrc " + str( ipSrc ) |
| 1747 | if ipDst: |
| 1748 | cmd += " --ipDst " + str( ipDst ) |
| 1749 | if tcpSrc: |
| 1750 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1751 | if tcpDst: |
| 1752 | cmd += " --tcpDst " + str( tcpDst ) |
| 1753 | if setEthSrc: |
| 1754 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1755 | if setEthDst: |
| 1756 | cmd += " --setEthDst " + str( setEthDst ) |
| 1757 | if vlanId: |
| 1758 | cmd += " -v " + str( vlanId ) |
| 1759 | if setVlan: |
| 1760 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1761 | if partial: |
| 1762 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1763 | if encap: |
| 1764 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1765 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1766 | # Check whether the user appended the port |
| 1767 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1768 | |
| 1769 | if portIngressList is None: |
| 1770 | for ingressDevice in ingressDeviceList: |
| 1771 | if "/" in ingressDevice: |
| 1772 | cmd += " " + str( ingressDevice ) |
| 1773 | else: |
| 1774 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1775 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1776 | # TODO: perhaps more meaningful return |
| 1777 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1778 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1779 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1780 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1781 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1782 | cmd += " " + \ |
| 1783 | str( ingressDevice ) + "/" +\ |
| 1784 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1785 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1786 | main.log.error( "Device list and port list does not " + |
| 1787 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1788 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1789 | if "/" in egressDevice: |
| 1790 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1791 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1792 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1793 | main.log.error( "You must specify " + |
| 1794 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1795 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1796 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1797 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1798 | str( egressDevice ) + "/" +\ |
| 1799 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1800 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1801 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1802 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1803 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1804 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1805 | main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1806 | "intent" ) |
| 1807 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1808 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1809 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1810 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1811 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1812 | else: |
| 1813 | main.log.error( "Error, intent ID not found" ) |
| 1814 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1815 | except AssertionError: |
| 1816 | main.log.exception( "" ) |
| 1817 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1818 | except TypeError: |
| 1819 | main.log.exception( self.name + ": Object not as expected" ) |
| 1820 | return None |
| 1821 | except pexpect.EOF: |
| 1822 | main.log.error( self.name + ": EOF exception found" ) |
| 1823 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1824 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1825 | except Exception: |
| 1826 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1827 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1828 | |
| 1829 | def addSinglepointToMultipointIntent( |
| 1830 | self, |
| 1831 | ingressDevice, |
| 1832 | egressDeviceList, |
| 1833 | portIngress="", |
| 1834 | portEgressList=None, |
| 1835 | ethType="", |
| 1836 | ethSrc="", |
| 1837 | ethDst="", |
| 1838 | bandwidth="", |
| 1839 | lambdaAlloc=False, |
| 1840 | ipProto="", |
| 1841 | ipSrc="", |
| 1842 | ipDst="", |
| 1843 | tcpSrc="", |
| 1844 | tcpDst="", |
| 1845 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1846 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1847 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1848 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1849 | partial=False, |
| 1850 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1851 | """ |
| 1852 | Note: |
| 1853 | This function assumes the format of all egress devices |
| 1854 | is same. That is, all egress devices include port numbers |
| 1855 | with a "/" or all egress devices could specify device |
| 1856 | ids and port numbers seperately. |
| 1857 | Required: |
| 1858 | * EgressDeviceList: List of device ids of egress device |
| 1859 | ( Atleast 2 eress devices required in the list ) |
| 1860 | * ingressDevice: device id of ingress device |
| 1861 | Optional: |
| 1862 | * ethType: specify ethType |
| 1863 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1864 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1865 | * bandwidth: specify bandwidth capacity of link |
| 1866 | * lambdaAlloc: if True, intent will allocate lambda |
| 1867 | for the specified intent |
| 1868 | * ipProto: specify ip protocol |
| 1869 | * ipSrc: specify ip source address |
| 1870 | * ipDst: specify ip destination address |
| 1871 | * tcpSrc: specify tcp source port |
| 1872 | * tcpDst: specify tcp destination port |
| 1873 | * setEthSrc: action to Rewrite Source MAC Address |
| 1874 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1875 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1876 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1877 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1878 | Description: |
| 1879 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1880 | specifying device id's and optional fields |
| 1881 | Returns: |
| 1882 | A string of the intent id or None on error |
| 1883 | |
| 1884 | NOTE: This function may change depending on the |
| 1885 | options developers provide for singlepoint-to-multipoint |
| 1886 | intent via cli |
| 1887 | """ |
| 1888 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1889 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1890 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1891 | if ethType: |
| 1892 | cmd += " --ethType " + str( ethType ) |
| 1893 | if ethSrc: |
| 1894 | cmd += " --ethSrc " + str( ethSrc ) |
| 1895 | if ethDst: |
| 1896 | cmd += " --ethDst " + str( ethDst ) |
| 1897 | if bandwidth: |
| 1898 | cmd += " --bandwidth " + str( bandwidth ) |
| 1899 | if lambdaAlloc: |
| 1900 | cmd += " --lambda " |
| 1901 | if ipProto: |
| 1902 | cmd += " --ipProto " + str( ipProto ) |
| 1903 | if ipSrc: |
| 1904 | cmd += " --ipSrc " + str( ipSrc ) |
| 1905 | if ipDst: |
| 1906 | cmd += " --ipDst " + str( ipDst ) |
| 1907 | if tcpSrc: |
| 1908 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1909 | if tcpDst: |
| 1910 | cmd += " --tcpDst " + str( tcpDst ) |
| 1911 | if setEthSrc: |
| 1912 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1913 | if setEthDst: |
| 1914 | cmd += " --setEthDst " + str( setEthDst ) |
| 1915 | if vlanId: |
| 1916 | cmd += " -v " + str( vlanId ) |
| 1917 | if setVlan: |
| 1918 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1919 | if partial: |
| 1920 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1921 | if encap: |
| 1922 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1923 | |
| 1924 | # Check whether the user appended the port |
| 1925 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1926 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1927 | if "/" in ingressDevice: |
| 1928 | cmd += " " + str( ingressDevice ) |
| 1929 | else: |
| 1930 | if not portIngress: |
| 1931 | main.log.error( "You must specify " + |
| 1932 | "the Ingress port" ) |
| 1933 | return main.FALSE |
| 1934 | |
| 1935 | cmd += " " +\ |
| 1936 | str( ingressDevice ) + "/" +\ |
| 1937 | str( portIngress ) |
| 1938 | |
| 1939 | if portEgressList is None: |
| 1940 | for egressDevice in egressDeviceList: |
| 1941 | if "/" in egressDevice: |
| 1942 | cmd += " " + str( egressDevice ) |
| 1943 | else: |
| 1944 | main.log.error( "You must specify " + |
| 1945 | "the egress port" ) |
| 1946 | # TODO: perhaps more meaningful return |
| 1947 | return main.FALSE |
| 1948 | else: |
| 1949 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1950 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1951 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1952 | cmd += " " + \ |
| 1953 | str( egressDevice ) + "/" +\ |
| 1954 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1955 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1956 | main.log.error( "Device list and port list does not " + |
| 1957 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1958 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1959 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1960 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1961 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1962 | # If error, return error message |
| 1963 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1964 | main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " + |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1965 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1966 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1967 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1968 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1969 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1970 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1971 | else: |
| 1972 | main.log.error( "Error, intent ID not found" ) |
| 1973 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1974 | except AssertionError: |
| 1975 | main.log.exception( "" ) |
| 1976 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1977 | except TypeError: |
| 1978 | main.log.exception( self.name + ": Object not as expected" ) |
| 1979 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1980 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1981 | main.log.error( self.name + ": EOF exception found" ) |
| 1982 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1983 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1984 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1985 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1986 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1987 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1988 | def addMplsIntent( |
| 1989 | self, |
| 1990 | ingressDevice, |
| 1991 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1992 | ingressPort="", |
| 1993 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1994 | ethType="", |
| 1995 | ethSrc="", |
| 1996 | ethDst="", |
| 1997 | bandwidth="", |
| 1998 | lambdaAlloc=False, |
| 1999 | ipProto="", |
| 2000 | ipSrc="", |
| 2001 | ipDst="", |
| 2002 | tcpSrc="", |
| 2003 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2004 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 2005 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2006 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2007 | """ |
| 2008 | Required: |
| 2009 | * ingressDevice: device id of ingress device |
| 2010 | * egressDevice: device id of egress device |
| 2011 | Optional: |
| 2012 | * ethType: specify ethType |
| 2013 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 2014 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 2015 | * bandwidth: specify bandwidth capacity of link |
| 2016 | * lambdaAlloc: if True, intent will allocate lambda |
| 2017 | for the specified intent |
| 2018 | * ipProto: specify ip protocol |
| 2019 | * ipSrc: specify ip source address |
| 2020 | * ipDst: specify ip destination address |
| 2021 | * tcpSrc: specify tcp source port |
| 2022 | * tcpDst: specify tcp destination port |
| 2023 | * ingressLabel: Ingress MPLS label |
| 2024 | * egressLabel: Egress MPLS label |
| 2025 | Description: |
| 2026 | Adds MPLS intent by |
| 2027 | specifying device id's and optional fields |
| 2028 | Returns: |
| 2029 | A string of the intent id or None on error |
| 2030 | |
| 2031 | NOTE: This function may change depending on the |
| 2032 | options developers provide for MPLS |
| 2033 | intent via cli |
| 2034 | """ |
| 2035 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 2036 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2037 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 2038 | if ethType: |
| 2039 | cmd += " --ethType " + str( ethType ) |
| 2040 | if ethSrc: |
| 2041 | cmd += " --ethSrc " + str( ethSrc ) |
| 2042 | if ethDst: |
| 2043 | cmd += " --ethDst " + str( ethDst ) |
| 2044 | if bandwidth: |
| 2045 | cmd += " --bandwidth " + str( bandwidth ) |
| 2046 | if lambdaAlloc: |
| 2047 | cmd += " --lambda " |
| 2048 | if ipProto: |
| 2049 | cmd += " --ipProto " + str( ipProto ) |
| 2050 | if ipSrc: |
| 2051 | cmd += " --ipSrc " + str( ipSrc ) |
| 2052 | if ipDst: |
| 2053 | cmd += " --ipDst " + str( ipDst ) |
| 2054 | if tcpSrc: |
| 2055 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 2056 | if tcpDst: |
| 2057 | cmd += " --tcpDst " + str( tcpDst ) |
| 2058 | if ingressLabel: |
| 2059 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 2060 | if egressLabel: |
| 2061 | cmd += " --egressLabel " + str( egressLabel ) |
| 2062 | if priority: |
| 2063 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2064 | |
| 2065 | # Check whether the user appended the port |
| 2066 | # or provided it as an input |
| 2067 | if "/" in ingressDevice: |
| 2068 | cmd += " " + str( ingressDevice ) |
| 2069 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2070 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2071 | main.log.error( "You must specify the ingress port" ) |
| 2072 | return None |
| 2073 | |
| 2074 | cmd += " " + \ |
| 2075 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2076 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2077 | |
| 2078 | if "/" in egressDevice: |
| 2079 | cmd += " " + str( egressDevice ) |
| 2080 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2081 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2082 | main.log.error( "You must specify the egress port" ) |
| 2083 | return None |
| 2084 | |
| 2085 | cmd += " " +\ |
| 2086 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2087 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2088 | |
| 2089 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2090 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2091 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2092 | # If error, return error message |
| 2093 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2094 | main.log.error( self.name + ": Error in adding mpls intent" ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2095 | return None |
| 2096 | else: |
| 2097 | # TODO: print out all the options in this message? |
| 2098 | main.log.info( "MPLS intent installed between " + |
| 2099 | str( ingressDevice ) + " and " + |
| 2100 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2101 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2102 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2103 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2104 | else: |
| 2105 | main.log.error( "Error, intent ID not found" ) |
| 2106 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2107 | except AssertionError: |
| 2108 | main.log.exception( "" ) |
| 2109 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2110 | except TypeError: |
| 2111 | main.log.exception( self.name + ": Object not as expected" ) |
| 2112 | return None |
| 2113 | except pexpect.EOF: |
| 2114 | main.log.error( self.name + ": EOF exception found" ) |
| 2115 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2116 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2117 | except Exception: |
| 2118 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2119 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2120 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2121 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 2122 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2123 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2124 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2125 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2126 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2127 | -p or --purge: Purge the intent from the store after removal |
| 2128 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2129 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 2130 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2131 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2132 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2133 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2134 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2135 | if purge: |
| 2136 | cmdStr += " -p" |
| 2137 | if sync: |
| 2138 | cmdStr += " -s" |
| 2139 | |
| 2140 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2141 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2142 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2143 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2144 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2145 | main.log.error( self.name + ": Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2146 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2147 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2148 | # TODO: Should this be main.TRUE |
| 2149 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2150 | except AssertionError: |
| 2151 | main.log.exception( "" ) |
| 2152 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2153 | except TypeError: |
| 2154 | main.log.exception( self.name + ": Object not as expected" ) |
| 2155 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2156 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2157 | main.log.error( self.name + ": EOF exception found" ) |
| 2158 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2159 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2160 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2161 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2162 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2163 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2164 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2165 | """ |
| 2166 | Description: |
| 2167 | Remove all the intents |
| 2168 | Optional args:- |
| 2169 | -s or --sync: Waits for the removal before returning |
| 2170 | -p or --purge: Purge the intent from the store after removal |
| 2171 | Returns: |
| 2172 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2173 | main.FALSE; Returns None for exception |
| 2174 | """ |
| 2175 | try: |
| 2176 | cmdStr = "remove-intent" |
| 2177 | if purge: |
| 2178 | cmdStr += " -p" |
| 2179 | if sync: |
| 2180 | cmdStr += " -s" |
| 2181 | |
| 2182 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2183 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2184 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2185 | assert "Command not found:" not in handle, handle |
| 2186 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2187 | main.log.error( self.name + ": Error in removing intent" ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2188 | return main.FALSE |
| 2189 | else: |
| 2190 | return main.TRUE |
| 2191 | except AssertionError: |
| 2192 | main.log.exception( "" ) |
| 2193 | return None |
| 2194 | except TypeError: |
| 2195 | main.log.exception( self.name + ": Object not as expected" ) |
| 2196 | return None |
| 2197 | except pexpect.EOF: |
| 2198 | main.log.error( self.name + ": EOF exception found" ) |
| 2199 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2200 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2201 | except Exception: |
| 2202 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2203 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2204 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2205 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2206 | """ |
| 2207 | Purges all WITHDRAWN Intents |
| 2208 | """ |
| 2209 | try: |
| 2210 | cmdStr = "purge-intents" |
| 2211 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2212 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2213 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2214 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2215 | main.log.error( self.name + ": Error in purging intents" ) |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2216 | return main.FALSE |
| 2217 | else: |
| 2218 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2219 | except AssertionError: |
| 2220 | main.log.exception( "" ) |
| 2221 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2222 | except TypeError: |
| 2223 | main.log.exception( self.name + ": Object not as expected" ) |
| 2224 | return None |
| 2225 | except pexpect.EOF: |
| 2226 | main.log.error( self.name + ": EOF exception found" ) |
| 2227 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2228 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2229 | except Exception: |
| 2230 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2231 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2232 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2233 | def wipeout( self ): |
| 2234 | """ |
| 2235 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2236 | """ |
| 2237 | try: |
| 2238 | cmdStr = "wipe-out please" |
| 2239 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2240 | assert handle is not None, "Error in sendline" |
| 2241 | assert "Command not found:" not in handle, handle |
| 2242 | return main.TRUE |
| 2243 | except AssertionError: |
| 2244 | main.log.exception( "" ) |
| 2245 | return None |
| 2246 | except TypeError: |
| 2247 | main.log.exception( self.name + ": Object not as expected" ) |
| 2248 | return None |
| 2249 | except pexpect.EOF: |
| 2250 | main.log.error( self.name + ": EOF exception found" ) |
| 2251 | main.log.error( self.name + ": " + self.handle.before ) |
| 2252 | main.cleanAndExit() |
| 2253 | except Exception: |
| 2254 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2255 | main.cleanAndExit() |
| 2256 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2257 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2258 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2259 | NOTE: This method should be used after installing application: |
| 2260 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2261 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2262 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2263 | Description: |
| 2264 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2265 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2266 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2267 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2268 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2269 | cmdStr += " -j" |
| 2270 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2271 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2272 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2273 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2274 | except AssertionError: |
| 2275 | main.log.exception( "" ) |
| 2276 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2277 | except TypeError: |
| 2278 | main.log.exception( self.name + ": Object not as expected" ) |
| 2279 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2280 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2281 | main.log.error( self.name + ": EOF exception found" ) |
| 2282 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2283 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2284 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2285 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2286 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2287 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2288 | def ipv4RouteNumber( self ): |
| 2289 | """ |
| 2290 | NOTE: This method should be used after installing application: |
| 2291 | onos-app-sdnip |
| 2292 | Description: |
| 2293 | Obtain the total IPv4 routes number in the system |
| 2294 | """ |
| 2295 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2296 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2297 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2298 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2299 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2300 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2301 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2302 | except AssertionError: |
| 2303 | main.log.exception( "" ) |
| 2304 | return None |
| 2305 | except ( TypeError, ValueError ): |
| 2306 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2307 | return None |
| 2308 | except pexpect.EOF: |
| 2309 | main.log.error( self.name + ": EOF exception found" ) |
| 2310 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2311 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2312 | except Exception: |
| 2313 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2314 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2315 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2316 | # =============Function to check Bandwidth allocation======== |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2317 | def allocations( self, jsonFormat = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2318 | """ |
| 2319 | Description: |
| 2320 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2321 | """ |
| 2322 | try: |
| 2323 | cmdStr = "allocations" |
| 2324 | if jsonFormat: |
| 2325 | cmdStr += " -j" |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2326 | handle = self.sendline( cmdStr, timeout=300 ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2327 | assert handle is not None, "Error in sendline" |
| 2328 | assert "Command not found:" not in handle, handle |
| 2329 | return handle |
| 2330 | except AssertionError: |
| 2331 | main.log.exception( "" ) |
| 2332 | return None |
| 2333 | except ( TypeError, ValueError ): |
| 2334 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2335 | return None |
| 2336 | except pexpect.EOF: |
| 2337 | main.log.error( self.name + ": EOF exception found" ) |
| 2338 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2339 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2340 | except Exception: |
| 2341 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2342 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2343 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2344 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2345 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2346 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2347 | Obtain intents from the ONOS cli. |
| 2348 | Optional: |
| 2349 | * jsonFormat: Enable output formatting in json, default to True |
| 2350 | * summary: Whether only output the intent summary, defaults to False |
| 2351 | * type: Only output a certain type of intent. This options is valid |
| 2352 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2353 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2354 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2355 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2356 | if summary: |
| 2357 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2358 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2359 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2360 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2361 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2362 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2363 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2364 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2365 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2366 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2367 | intentType = "" |
| 2368 | # IF we want the summary of a specific intent type |
| 2369 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2370 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2371 | if intentType in jsonResult.keys(): |
| 2372 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2373 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2374 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2375 | return handle |
| 2376 | else: |
| 2377 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2378 | except AssertionError: |
| 2379 | main.log.exception( "" ) |
| 2380 | return None |
| 2381 | except ( TypeError, ValueError ): |
| 2382 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2383 | return None |
| 2384 | except pexpect.EOF: |
| 2385 | main.log.error( self.name + ": EOF exception found" ) |
| 2386 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2387 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2388 | except Exception: |
| 2389 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2390 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2391 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2392 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2393 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2394 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2395 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2396 | list of intent IDs. |
| 2397 | Parameters: |
| 2398 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2399 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2400 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2401 | 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] | 2402 | accepted. |
| 2403 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2404 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2405 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2406 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2407 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2408 | try: |
| 2409 | state = "State is Undefined" |
| 2410 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2411 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2412 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2413 | rawJson = intentsJson |
| 2414 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2415 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2416 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2417 | if intentsId == intent[ 'id' ]: |
| 2418 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2419 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2420 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2421 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2422 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2423 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2424 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2425 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2426 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2427 | for intent in parsedIntentsJson: |
| 2428 | if intentsId[ i ] == intent[ 'id' ]: |
| 2429 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2430 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2431 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2432 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2433 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2434 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2435 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2436 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2437 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2438 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2439 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2440 | except ( TypeError, ValueError ): |
| 2441 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2442 | return None |
| 2443 | except pexpect.EOF: |
| 2444 | main.log.error( self.name + ": EOF exception found" ) |
| 2445 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2446 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2447 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2448 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2449 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2450 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2451 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2452 | """ |
| 2453 | Description: |
| 2454 | Check intents state |
| 2455 | Required: |
| 2456 | intentsId - List of intents ID to be checked |
| 2457 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2458 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2459 | state in the list. |
| 2460 | *NOTE: You can pass in a list of expected state, |
| 2461 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2462 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2463 | Returns main.TRUE only if all intent are the same as expected states, |
| 2464 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2465 | """ |
| 2466 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2467 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2468 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2469 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2470 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2471 | intentsDict = [] |
| 2472 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2473 | if isinstance( intentsId, types.StringType ) \ |
| 2474 | and intent.get( 'id' ) == intentsId: |
| 2475 | intentsDict.append( intent ) |
| 2476 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2477 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2478 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2479 | |
| 2480 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2481 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2482 | "getting intents state" ) |
| 2483 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2484 | |
| 2485 | if isinstance( expectedState, types.StringType ): |
| 2486 | for intents in intentsDict: |
| 2487 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2488 | main.log.debug( self.name + " : Intent ID - " + |
| 2489 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2490 | " actual state = " + |
| 2491 | intents.get( 'state' ) |
| 2492 | + " does not equal expected state = " |
| 2493 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2494 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2495 | elif isinstance( expectedState, types.ListType ): |
| 2496 | for intents in intentsDict: |
| 2497 | if not any( state == intents.get( 'state' ) for state in |
| 2498 | expectedState ): |
| 2499 | main.log.debug( self.name + " : Intent ID - " + |
| 2500 | intents.get( 'id' ) + |
| 2501 | " actual state = " + |
| 2502 | intents.get( 'state' ) + |
| 2503 | " does not equal expected states = " |
| 2504 | + str( expectedState ) ) |
| 2505 | returnValue = main.FALSE |
| 2506 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2507 | if returnValue == main.TRUE: |
| 2508 | main.log.info( self.name + ": All " + |
| 2509 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2510 | " intents are in " + str( expectedState ) + |
| 2511 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2512 | return returnValue |
| 2513 | except TypeError: |
| 2514 | main.log.exception( self.name + ": Object not as expected" ) |
| 2515 | return None |
| 2516 | except pexpect.EOF: |
| 2517 | main.log.error( self.name + ": EOF exception found" ) |
| 2518 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2519 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2520 | except Exception: |
| 2521 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2522 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2523 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2524 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2525 | """ |
| 2526 | Description: |
| 2527 | Compare the allocated bandwidth with the given allocations |
| 2528 | Required: |
| 2529 | expectedAllocations - The expected ONOS output of the allocations command |
| 2530 | Return: |
| 2531 | Returns main.TRUE only if all intent are the same as expected states, |
| 2532 | otherwise returns main.FALSE. |
| 2533 | """ |
| 2534 | # FIXME: Convert these string comparisons to object comparisons |
| 2535 | try: |
| 2536 | returnValue = main.TRUE |
| 2537 | bandwidthFailed = False |
| 2538 | rawAlloc = self.allocations() |
| 2539 | expectedFormat = StringIO( expectedAllocations ) |
| 2540 | ONOSOutput = StringIO( rawAlloc ) |
| 2541 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2542 | str( expectedFormat ) ) ) |
| 2543 | |
| 2544 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2545 | actual = actual.rstrip() |
| 2546 | expected = expected.rstrip() |
| 2547 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2548 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2549 | marker1 = actual.find( 'allocated' ) |
| 2550 | m1 = actual[ :marker1 ] |
| 2551 | marker2 = expected.find( 'allocated' ) |
| 2552 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2553 | if m1 != m2: |
| 2554 | bandwidthFailed = True |
| 2555 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2556 | bandwidthFailed = True |
| 2557 | expectedFormat.close() |
| 2558 | ONOSOutput.close() |
| 2559 | |
| 2560 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2561 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2562 | returnValue = main.FALSE |
| 2563 | return returnValue |
| 2564 | except TypeError: |
| 2565 | main.log.exception( self.name + ": Object not as expected" ) |
| 2566 | return None |
| 2567 | except pexpect.EOF: |
| 2568 | main.log.error( self.name + ": EOF exception found" ) |
| 2569 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2570 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2571 | except Exception: |
| 2572 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2573 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2574 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2575 | def compareIntent( self, intentDict ): |
| 2576 | """ |
| 2577 | Description: |
| 2578 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2579 | Return: |
| 2580 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2581 | Arguments: |
| 2582 | intentDict: a dictionary which maps intent ids to intent states |
| 2583 | """ |
| 2584 | try: |
| 2585 | intentsRaw = self.intents() |
| 2586 | intentsJson = json.loads( intentsRaw ) |
| 2587 | intentDictONOS = {} |
| 2588 | for intent in intentsJson: |
| 2589 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2590 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2591 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2592 | 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] | 2593 | str( len( intentDict ) ) + " expected and " + |
| 2594 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2595 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2596 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2597 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2598 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2599 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2600 | else: |
| 2601 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2602 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2603 | " expected state is " + intentDict[ intentID ] + |
| 2604 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2605 | returnValue = main.FALSE |
| 2606 | intentDictONOS.pop( intentID ) |
| 2607 | if len( intentDictONOS ) > 0: |
| 2608 | returnValue = main.FALSE |
| 2609 | for intentID in intentDictONOS.keys(): |
| 2610 | 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] | 2611 | if returnValue == main.TRUE: |
| 2612 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2613 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2614 | except KeyError: |
| 2615 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2616 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2617 | except ( TypeError, ValueError ): |
| 2618 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2619 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2620 | except pexpect.EOF: |
| 2621 | main.log.error( self.name + ": EOF exception found" ) |
| 2622 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2623 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2624 | except Exception: |
| 2625 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2626 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2627 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2628 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2629 | """ |
| 2630 | Description: |
| 2631 | Check the number of installed intents. |
| 2632 | Optional: |
| 2633 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2634 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2635 | Return: |
| 2636 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2637 | , otherwise, returns main.FALSE. |
| 2638 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2639 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2640 | try: |
| 2641 | cmd = "intents -s -j" |
| 2642 | |
| 2643 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2644 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2645 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2646 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2647 | response = json.loads( response ) |
| 2648 | |
| 2649 | # get total and installed number, see if they are match |
| 2650 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2651 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2652 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2653 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2654 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2655 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2656 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2657 | return main.FALSE |
| 2658 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2659 | except ( TypeError, ValueError ): |
| 2660 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2661 | return None |
| 2662 | except pexpect.EOF: |
| 2663 | main.log.error( self.name + ": EOF exception found" ) |
| 2664 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2665 | if noExit: |
| 2666 | return main.FALSE |
| 2667 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2668 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2669 | except pexpect.TIMEOUT: |
| 2670 | main.log.error( self.name + ": ONOS timeout" ) |
| 2671 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2672 | except Exception: |
| 2673 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2674 | if noExit: |
| 2675 | return main.FALSE |
| 2676 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2677 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2678 | |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2679 | def flows( self, state="any", jsonFormat=True, timeout=60, noExit=False, noCore=False, device="" ): |
| 2680 | return self.dataPlaneEntities( entity="flows", state=state, jsonFormat=jsonFormat, |
| 2681 | timeout=timeout, noExit=noExit, noCore=noCore, device=device ) |
| 2682 | |
| 2683 | def groups( self, state="any", jsonFormat=True, timeout=60, noExit=False, noCore=False, device="" ): |
| 2684 | return self.dataPlaneEntities( entity="groups", state=state, jsonFormat=jsonFormat, |
| 2685 | timeout=timeout, noExit=noExit, noCore=noCore, device=device ) |
| 2686 | |
| 2687 | def dataPlaneEntities( self, entity="flows", state="any", jsonFormat=True, |
| 2688 | timeout=60, noExit=False, noCore=False, device="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2689 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2690 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2691 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2692 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2693 | Description: |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2694 | Obtain dataplaneEntities currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2695 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2696 | try: |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2697 | cmdStr = entity |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2698 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2699 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2700 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2701 | cmdStr += " -n" |
| 2702 | cmdStr += " " + state |
| 2703 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2704 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2705 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2706 | assert "Command not found:" not in handle, handle |
| 2707 | if re.search( "Error:", handle ): |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2708 | main.log.error( self.name + ": " + entity + "() response: " + |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2709 | str( handle ) ) |
| 2710 | return handle |
| 2711 | except AssertionError: |
| 2712 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2713 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2714 | except TypeError: |
| 2715 | main.log.exception( self.name + ": Object not as expected" ) |
| 2716 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2717 | except pexpect.TIMEOUT: |
| 2718 | main.log.error( self.name + ": ONOS timeout" ) |
| 2719 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2720 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2721 | main.log.error( self.name + ": EOF exception found" ) |
| 2722 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2723 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2724 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2725 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2726 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2727 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2728 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2729 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2730 | count = int( count ) if count else 0 |
steven30801 | eccfe21 | 2019-01-24 13:00:42 +0800 | [diff] [blame] | 2731 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 2732 | return count if ( count >= min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2733 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2734 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2735 | """ |
| 2736 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2737 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2738 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2739 | if the count of those states is 0, which means all current flows |
| 2740 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2741 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2742 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2743 | Return: |
| 2744 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2745 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2746 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2747 | """ |
| 2748 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2749 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2750 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2751 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2752 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2753 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2754 | if rawFlows: |
| 2755 | # if we didn't get flows or flows function return None, we should return |
| 2756 | # main.Flase |
| 2757 | checkedStates.append( json.loads( rawFlows ) ) |
| 2758 | else: |
| 2759 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2760 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2761 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2762 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2763 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2764 | except TypeError: |
| 2765 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2766 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2767 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2768 | # We want to count PENDING_ADD if isPENDING is true |
| 2769 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2770 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2771 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2772 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2773 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2774 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2775 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2776 | except ( TypeError, ValueError ): |
| 2777 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2778 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2779 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2780 | except AssertionError: |
| 2781 | main.log.exception( "" ) |
| 2782 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2783 | except pexpect.TIMEOUT: |
| 2784 | main.log.error( self.name + ": ONOS timeout" ) |
| 2785 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2786 | except pexpect.EOF: |
| 2787 | main.log.error( self.name + ": EOF exception found" ) |
| 2788 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2789 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2790 | except Exception: |
| 2791 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2792 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2793 | |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2794 | def checkGroupCount( self, min=0, timeout=60 ): |
| 2795 | count = self.getTotalGroupsNum( timeout=timeout ) |
| 2796 | count = int( count ) if count else 0 |
| 2797 | main.log.debug( "found {} groups".format( count ) ) |
| 2798 | return count if ( count >= min ) else False |
| 2799 | |
| 2800 | def checkGroupsState( self, isPENDING=True, timeout=60, noExit=False ): |
| 2801 | """ |
| 2802 | Description: |
| 2803 | Check the if all the current groups are in ADDED state |
| 2804 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED groups, |
| 2805 | if the count of those states is 0, which means all current groups |
| 2806 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
| 2807 | Optional: |
| 2808 | * isPENDING: whether the PENDING_ADD is also a correct status |
| 2809 | Return: |
| 2810 | returnValue - Returns main.TRUE only if all groups are in |
| 2811 | ADDED state or PENDING_ADD if the isPENDING |
| 2812 | parameter is set true, return main.FALSE otherwise. |
| 2813 | """ |
| 2814 | try: |
| 2815 | states = [ "PENDING_ADD", "PENDING_ADD_RETRY", "PENDING_DELETE", "PENDING_UPDATE", "WAITING_AUDIT_COMPLETE" ] |
| 2816 | checkedStates = [] |
| 2817 | statesCount = [ 0, 0, 0, 0, 0 ] |
| 2818 | for s in states: |
| 2819 | rawGroups = self.groups( state=s, timeout = timeout ) |
| 2820 | if rawGroups: |
| 2821 | # if we didn't get groups or groups function return None, we should return |
| 2822 | # main.Flase |
| 2823 | checkedStates.append( json.loads( rawGroups ) ) |
| 2824 | else: |
| 2825 | return main.FALSE |
| 2826 | for i in range( len( states ) ): |
| 2827 | statesCount[ i ] += int( len(checkedStates[i]) ) |
| 2828 | main.log.info( states[ i ] + " groups: " + str( statesCount[ i ] ) ) |
| 2829 | |
| 2830 | # We want to count PENDING_ADD if isPENDING is true |
| 2831 | if isPENDING: |
| 2832 | if statesCount[ 2 ] + statesCount[ 3 ] + statesCount[ 4 ] > 0: |
| 2833 | return main.FALSE |
| 2834 | else: |
| 2835 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] + statesCount[ 4 ] > 0: |
| 2836 | return main.FALSE |
| 2837 | return main.TRUE |
| 2838 | except ( TypeError, ValueError ): |
| 2839 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawGroups ) ) |
| 2840 | return None |
| 2841 | |
| 2842 | except AssertionError: |
| 2843 | main.log.exception( "" ) |
| 2844 | return None |
| 2845 | except pexpect.TIMEOUT: |
| 2846 | main.log.error( self.name + ": ONOS timeout" ) |
| 2847 | return None |
| 2848 | except pexpect.EOF: |
| 2849 | main.log.error( self.name + ": EOF exception found" ) |
| 2850 | main.log.error( self.name + ": " + self.handle.before ) |
| 2851 | main.cleanAndExit() |
| 2852 | except Exception: |
| 2853 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2854 | main.cleanAndExit() |
| 2855 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2856 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2857 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2858 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2859 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2860 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2861 | a specific point-to-point intent definition |
| 2862 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2863 | * ingress: specify source dpid |
| 2864 | * egress: specify destination dpid |
| 2865 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2866 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2867 | * offset: the keyOffset is where the next batch of intents |
| 2868 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2869 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2870 | * getResponse: If set to True, function will return ONOS response. |
| 2871 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2872 | Returns: If failed to push test intents, it will returen None, |
| 2873 | if successful, return true. |
| 2874 | Timeout expection will return None, |
| 2875 | TypeError will return false |
| 2876 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2877 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2878 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2879 | if background: |
| 2880 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2881 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2882 | back = "" |
| 2883 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2884 | ingress, |
| 2885 | egress, |
| 2886 | batchSize, |
| 2887 | offset, |
| 2888 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2889 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2890 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2891 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2892 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2893 | if getResponse: |
| 2894 | return response |
| 2895 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2896 | # TODO: We should handle if there is failure in installation |
| 2897 | return main.TRUE |
| 2898 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2899 | except AssertionError: |
| 2900 | main.log.exception( "" ) |
| 2901 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2902 | except pexpect.TIMEOUT: |
| 2903 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2904 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2905 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2906 | main.log.error( self.name + ": EOF exception found" ) |
| 2907 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2908 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2909 | except TypeError: |
| 2910 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2911 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2912 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2913 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2914 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2915 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2916 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2917 | return self.getTotalEntitiesNum( entity="flows", timeout=60, noExit=False ) |
| 2918 | |
| 2919 | def getTotalGroupsNum( self, timeout=60, noExit=False ): |
| 2920 | return self.getTotalEntitiesNum( entity="groups", timeout=60, noExit=False ) |
| 2921 | |
| 2922 | def getTotalEntitiesNum( self, entity="flows", timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2923 | """ |
| 2924 | Description: |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2925 | Get the number of ADDED entities. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2926 | Return: |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2927 | The number of ADDED entities |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2928 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2929 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2930 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2931 | try: |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2932 | # get total added entities number |
| 2933 | cmd = entity + " -c added" |
| 2934 | rawEntities = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2935 | if rawEntities: |
| 2936 | rawEntities = rawEntities.split( "\n" ) |
| 2937 | totalEntities = 0 |
| 2938 | for l in rawEntities: |
| 2939 | totalEntities += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2940 | else: |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 2941 | main.log.warn( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2942 | return None |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2943 | return totalEntities |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2944 | |
You Wang | d3097f7 | 2018-12-12 11:56:03 -0800 | [diff] [blame] | 2945 | except IndexError: |
| 2946 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
pierventre | 3ee58d3 | 2022-02-19 18:30:19 -0800 | [diff] [blame] | 2947 | main.log.debug( "rawEntities: {}".format( rawEntities ) ) |
You Wang | d3097f7 | 2018-12-12 11:56:03 -0800 | [diff] [blame] | 2948 | return None |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2949 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2950 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2951 | return None |
| 2952 | except pexpect.EOF: |
| 2953 | main.log.error( self.name + ": EOF exception found" ) |
| 2954 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2955 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2956 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2957 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2958 | except pexpect.TIMEOUT: |
| 2959 | main.log.error( self.name + ": ONOS timeout" ) |
| 2960 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2961 | except Exception: |
| 2962 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2963 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2964 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2965 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2966 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2967 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2968 | """ |
| 2969 | Description: |
| 2970 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2971 | Optional: |
| 2972 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2973 | Return: |
| 2974 | The number of intents |
| 2975 | """ |
| 2976 | try: |
| 2977 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2978 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2979 | if response is None: |
| 2980 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2981 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2982 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2983 | except ( TypeError, ValueError ): |
| 2984 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2985 | return None |
| 2986 | except pexpect.EOF: |
| 2987 | main.log.error( self.name + ": EOF exception found" ) |
| 2988 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2989 | if noExit: |
| 2990 | return -1 |
| 2991 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2992 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2993 | except Exception: |
| 2994 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2995 | if noExit: |
| 2996 | return -1 |
| 2997 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2998 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2999 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3000 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3001 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3002 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 3003 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3004 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3005 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 3006 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3007 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3008 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3009 | cmdStr += " -j" |
| 3010 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3011 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3012 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 3013 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3014 | except AssertionError: |
| 3015 | main.log.exception( "" ) |
| 3016 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3017 | except TypeError: |
| 3018 | main.log.exception( self.name + ": Object not as expected" ) |
| 3019 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 3020 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3021 | main.log.error( self.name + ": EOF exception found" ) |
| 3022 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3023 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3024 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3025 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3026 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 3027 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3028 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3029 | """ |
| 3030 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 3031 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3032 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3033 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 3034 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3035 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3036 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3037 | cmdStr += " -j" |
| 3038 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3039 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3040 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 3041 | if handle: |
| 3042 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3043 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3044 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 3045 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3046 | else: |
| 3047 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3048 | except AssertionError: |
| 3049 | main.log.exception( "" ) |
| 3050 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3051 | except TypeError: |
| 3052 | main.log.exception( self.name + ": Object not as expected" ) |
| 3053 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 3054 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3055 | main.log.error( self.name + ": EOF exception found" ) |
| 3056 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3057 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3058 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3059 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3060 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 3061 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3062 | # Wrapper functions **************** |
| 3063 | # Wrapper functions use existing driver |
| 3064 | # functions and extends their use case. |
| 3065 | # For example, we may use the output of |
| 3066 | # a normal driver function, and parse it |
| 3067 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 3068 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3069 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3070 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 3071 | Description: |
| 3072 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3073 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 3074 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3075 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3076 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3077 | if intentsStr is None: |
| 3078 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 3079 | # Convert to a dictionary |
| 3080 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3081 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 3082 | for intent in intents: |
| 3083 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3084 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3085 | except TypeError: |
| 3086 | main.log.exception( self.name + ": Object not as expected" ) |
| 3087 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 3088 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3089 | main.log.error( self.name + ": EOF exception found" ) |
| 3090 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3091 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3092 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3093 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3094 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 3095 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3096 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3097 | """ |
| 3098 | Determine the number of flow rules for the given device id that are |
| 3099 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3100 | Params: |
| 3101 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3102 | """ |
| 3103 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3104 | if core: |
| 3105 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 3106 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3107 | else: |
| 3108 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 3109 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3110 | handle = self.lineCount( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3111 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3112 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3113 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3114 | except AssertionError: |
| 3115 | main.log.exception( "" ) |
| 3116 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3117 | except pexpect.EOF: |
| 3118 | main.log.error( self.name + ": EOF exception found" ) |
| 3119 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3120 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3121 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3122 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3123 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 3124 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3125 | def groupAddedCount( self, deviceId, core=False ): |
| 3126 | """ |
| 3127 | Determine the number of group rules for the given device id that are |
| 3128 | in the added state |
| 3129 | Params: |
| 3130 | core: if True, only return the number of core groups added |
| 3131 | """ |
| 3132 | try: |
| 3133 | if core: |
| 3134 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3135 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3136 | else: |
| 3137 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3138 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3139 | handle = self.lineCount( cmdStr ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3140 | assert handle is not None, "Error in sendline" |
| 3141 | assert "Command not found:" not in handle, handle |
| 3142 | return handle |
| 3143 | except AssertionError: |
| 3144 | main.log.exception( "" ) |
| 3145 | return None |
| 3146 | except pexpect.EOF: |
| 3147 | main.log.error( self.name + ": EOF exception found" ) |
| 3148 | main.log.error( self.name + ": " + self.handle.before ) |
| 3149 | main.cleanAndExit() |
| 3150 | except Exception: |
| 3151 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3152 | main.cleanAndExit() |
| 3153 | |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame] | 3154 | def sendOnosCliCommand( self, cmd, params=[] ): |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3155 | """ |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame] | 3156 | Handles sending an arbitrary command to the ONOS cli and parsing standard errors |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3157 | Params: |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame] | 3158 | cmd: The command to be sent in the onos cli |
| 3159 | params: Sending the parameters within the command we send in the cli |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3160 | """ |
| 3161 | try: |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame] | 3162 | if not isinstance(params, list): |
| 3163 | params=[params] |
| 3164 | cmdStr = cmd + " " + ' '.join(params) |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3165 | handle = self.sendline( cmdStr ) |
| 3166 | assert handle is not None, "Error in sendline" |
| 3167 | assert "Command not found:" not in handle, handle |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame] | 3168 | # Check for error in cli response |
| 3169 | assert "Error executing command" not in handle, handle |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3170 | return handle |
| 3171 | except AssertionError: |
| 3172 | main.log.exception( "" ) |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame] | 3173 | return main.FALSE |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3174 | except pexpect.EOF: |
| 3175 | main.log.error( self.name + ": EOF exception found" ) |
| 3176 | main.log.error( self.name + ": " + self.handle.before ) |
| 3177 | main.cleanAndExit() |
| 3178 | except Exception: |
| 3179 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3180 | main.cleanAndExit() |
| 3181 | |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame] | 3182 | def blackholeStaticRoute( self, subnet, addBlackhole=True ): |
| 3183 | """ |
| 3184 | Manage black hole routes in onos. |
| 3185 | Params: |
| 3186 | subnet: The subnet to be blocked through this route |
| 3187 | addBlackhole: Boolean set to declare if we are adding or removing the blackhole route |
| 3188 | """ |
| 3189 | if addBlackhole: |
| 3190 | addedBlackholeIP = self.sendOnosCliCommand('sr-blackhole add', subnet) |
| 3191 | else: |
| 3192 | removedBlackholeIP = self.sendOnosCliCommand('sr-blackhole remove', subnet) |
| 3193 | blackHoleList = self.sendOnosCliCommand('sr-blackhole list') |
| 3194 | if subnet in blackHoleList: |
| 3195 | exists = True |
| 3196 | else: |
| 3197 | exists = False |
| 3198 | if (addBlackhole and exists) or (not addBlackhole and not exists): |
| 3199 | return main.TRUE |
| 3200 | else: |
| 3201 | return main.FALSE |
| 3202 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3203 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 3204 | """ |
| 3205 | Description: |
| 3206 | Check whether the number of groups for the given device id that |
| 3207 | are in ADDED state is bigger than minGroupCount. |
| 3208 | Required: |
| 3209 | * deviceId: device id to check the number of added group rules |
| 3210 | Optional: |
| 3211 | * minGroupCount: the number of groups to compare |
| 3212 | * core: if True, only check the number of core groups added |
| 3213 | * comparison: if 0, compare with greater than minFlowCount |
| 3214 | * if 1, compare with equal to minFlowCount |
| 3215 | Return: |
| 3216 | Returns the number of groups if it is bigger than minGroupCount, |
| 3217 | returns main.FALSE otherwise. |
| 3218 | """ |
| 3219 | count = self.groupAddedCount( deviceId, core ) |
| 3220 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3221 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3222 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 3223 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3224 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3225 | """ |
| 3226 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3227 | deviceId: Id of the device from which we retrieve groups |
| 3228 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3229 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3230 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3231 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 3232 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3233 | assert handle is not None, "Error in sendline" |
| 3234 | assert "Command not found:" not in handle, handle |
| 3235 | return handle |
| 3236 | except AssertionError: |
| 3237 | main.log.exception( "" ) |
| 3238 | return None |
| 3239 | except TypeError: |
| 3240 | main.log.exception( self.name + ": Object not as expected" ) |
| 3241 | return None |
| 3242 | except pexpect.EOF: |
| 3243 | main.log.error( self.name + ": EOF exception found" ) |
| 3244 | main.log.error( self.name + ": " + self.handle.before ) |
| 3245 | main.cleanAndExit() |
| 3246 | except Exception: |
| 3247 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3248 | main.cleanAndExit() |
| 3249 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3250 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3251 | """ |
| 3252 | Description: |
| 3253 | Check whether the number of flow rules for the given device id that |
| 3254 | are in ADDED state is bigger than minFlowCount. |
| 3255 | Required: |
| 3256 | * deviceId: device id to check the number of added flow rules |
| 3257 | Optional: |
| 3258 | * minFlowCount: the number of flow rules to compare |
| 3259 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3260 | * comparison: if 0, compare with greater than minFlowCount |
| 3261 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3262 | Return: |
| 3263 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 3264 | returns main.FALSE otherwise. |
| 3265 | """ |
| 3266 | count = self.flowAddedCount( deviceId, core ) |
| 3267 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3268 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3269 | 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] | 3270 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3271 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3272 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3273 | Use 'devices' function to obtain list of all devices |
| 3274 | and parse the result to obtain a list of all device |
| 3275 | id's. Returns this list. Returns empty list if no |
| 3276 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3277 | List is ordered sequentially |
| 3278 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3279 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3280 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3281 | the ids. By obtaining the list of device ids on the fly, |
| 3282 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3283 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3284 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3285 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3286 | devicesStr = self.devices( jsonFormat=False ) |
| 3287 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3288 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3289 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3290 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3291 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3292 | |
| 3293 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3294 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3295 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3296 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3297 | # Split list further into arguments before and after string |
| 3298 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3299 | # append to idList |
| 3300 | for arg in tempList: |
| 3301 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3302 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3303 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3304 | except TypeError: |
| 3305 | main.log.exception( self.name + ": Object not as expected" ) |
| 3306 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3307 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3308 | main.log.error( self.name + ": EOF exception found" ) |
| 3309 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3310 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3311 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3312 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3313 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3314 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3315 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3316 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3317 | Uses 'nodes' function to obtain list of all nodes |
| 3318 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3319 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3320 | Returns: |
| 3321 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3322 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3323 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3324 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3325 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3326 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3327 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3328 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3329 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3330 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3331 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3332 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3333 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3334 | except ( TypeError, ValueError ): |
| 3335 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3336 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3337 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3338 | main.log.error( self.name + ": EOF exception found" ) |
| 3339 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3340 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3341 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3342 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3343 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3344 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3345 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3346 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3347 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3348 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3349 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3350 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3351 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3352 | return None |
| 3353 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3354 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3355 | rawDevices = self.devices() |
| 3356 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3357 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3358 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3359 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3360 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3361 | return device |
| 3362 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3363 | except ( TypeError, ValueError ): |
| 3364 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3365 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3366 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3367 | main.log.error( self.name + ": EOF exception found" ) |
| 3368 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3369 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3370 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3371 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3372 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3373 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3374 | def getTopology( self, topologyOutput ): |
| 3375 | """ |
| 3376 | Definition: |
| 3377 | Loads a json topology output |
| 3378 | Return: |
| 3379 | topology = current ONOS topology |
| 3380 | """ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3381 | try: |
| 3382 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3383 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3384 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3385 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3386 | except ( TypeError, ValueError ): |
| 3387 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3388 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3389 | except pexpect.EOF: |
| 3390 | main.log.error( self.name + ": EOF exception found" ) |
| 3391 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3392 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3393 | except Exception: |
| 3394 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3395 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3396 | |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3397 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, numoSCCs=1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3398 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3399 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3400 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3401 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3402 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3403 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3404 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3405 | numoctrl = expected number of controllers |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3406 | numoSCCs = Number of expected SCCs |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3407 | logLevel = level to log to. |
| 3408 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3409 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3410 | Returns: main.TRUE if the number of switches and links are correct, |
| 3411 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3412 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3413 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3414 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3415 | summary = self.summary() |
| 3416 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3417 | except ( TypeError, ValueError ): |
| 3418 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3419 | return main.ERROR |
| 3420 | try: |
| 3421 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3422 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3423 | return main.ERROR |
| 3424 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3425 | # Is the number of switches is what we expected |
| 3426 | devices = topology.get( 'devices', False ) |
| 3427 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3428 | nodes = summary.get( 'nodes', False ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3429 | SCCs = summary.get( 'SCC(s)', False ) |
| 3430 | if devices is False or links is False or nodes is False or SCCs is False: |
| 3431 | main.log.warn( "Issues parsing topology and summary output" ) |
| 3432 | main.log.debug( topology ) |
| 3433 | main.log.debug( summary ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3434 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3435 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3436 | if not switchCheck: |
| 3437 | main.log.debug( "switch Check Failed" ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3438 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3439 | if not linkCheck: |
| 3440 | main.log.debug( "link Check Failed" ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3441 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3442 | if not nodeCheck: |
| 3443 | main.log.debug( "node Check Failed" ) |
| 3444 | SCCsCheck = ( int( SCCs ) == int( numoSCCs ) ) or int( numoSCCs ) == -1 |
| 3445 | if not SCCsCheck: |
| 3446 | main.log.debug( "SCCs Check Failed" ) |
| 3447 | if switchCheck and linkCheck and nodeCheck and SCCsCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3448 | # We expected the correct numbers |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3449 | output = output + "The number of links, switches, nodes, and SCCs match "\ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3450 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3451 | result = main.TRUE |
| 3452 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3453 | output = output + \ |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 3454 | "The number of links, switches, nodes, and SCCs does not match " + \ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3455 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3456 | result = main.FALSE |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3457 | output = output + "\n ONOS sees %i devices " % int( devices ) |
| 3458 | output = output + "(%i expected) " % int( numoswitch ) |
| 3459 | if int( numolink ) >= 0: |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3460 | output = output + "and %i links " % int( links ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3461 | output = output + "(%i expected) " % int( numolink ) |
| 3462 | if int( numoctrl ) >= 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3463 | output = output + "and %i controllers " % int( nodes ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3464 | output = output + "(%i expected) " % int( numoctrl ) |
| 3465 | if int( numoSCCs ) >= 0: |
| 3466 | output = output + "and %i SCCs " % int( SCCs ) |
| 3467 | output = output + "(%i expected)" % int( numoSCCs ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3468 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3469 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3470 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3471 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3472 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3473 | main.log.info( output ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3474 | main.TOPOOUTPUT = output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3475 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3476 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3477 | main.log.error( self.name + ": EOF exception found" ) |
| 3478 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3479 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3480 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3481 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3482 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3483 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3484 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3485 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3486 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3487 | deviceId must be the id of a device as seen in the onos devices command |
| 3488 | 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] | 3489 | role must be either master, standby, or none |
| 3490 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3491 | Returns: |
| 3492 | main.TRUE or main.FALSE based on argument verification and |
| 3493 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3494 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3495 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3496 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3497 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3498 | cmdStr = "device-role " +\ |
| 3499 | str( deviceId ) + " " +\ |
| 3500 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3501 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3502 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3503 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3504 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3505 | if re.search( "Error", handle ): |
| 3506 | # end color output to escape any colours |
| 3507 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3508 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3509 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3510 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3511 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3512 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3513 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3514 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3515 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3516 | except AssertionError: |
| 3517 | main.log.exception( "" ) |
| 3518 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3519 | except TypeError: |
| 3520 | main.log.exception( self.name + ": Object not as expected" ) |
| 3521 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3522 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3523 | main.log.error( self.name + ": EOF exception found" ) |
| 3524 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3525 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3526 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3527 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3528 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3529 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3530 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3531 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3532 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3533 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3534 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3535 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3536 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3537 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3538 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3539 | cmdStr += " -j" |
| 3540 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3541 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3542 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3543 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3544 | except AssertionError: |
| 3545 | main.log.exception( "" ) |
| 3546 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3547 | except TypeError: |
| 3548 | main.log.exception( self.name + ": Object not as expected" ) |
| 3549 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3550 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3551 | main.log.error( self.name + ": EOF exception found" ) |
| 3552 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3553 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3554 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3555 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3556 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3557 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3558 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3559 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3560 | CLI command to get the current leader for the Election test application |
| 3561 | NOTE: Requires installation of the onos-app-election feature |
| 3562 | Returns: Node IP of the leader if one exists |
| 3563 | None if none exists |
| 3564 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3565 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3566 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3567 | cmdStr = "election-test-leader" |
| 3568 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3569 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3570 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3571 | # Leader |
| 3572 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3573 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3574 | nodeSearch = re.search( leaderPattern, response ) |
| 3575 | if nodeSearch: |
| 3576 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3577 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3578 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3579 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3580 | # no leader |
| 3581 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3582 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3583 | nullSearch = re.search( nullPattern, response ) |
| 3584 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3585 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3586 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3587 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3588 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3589 | main.log.error( self.name + ": Error in electionTestLeader on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3590 | ": " + "unexpected response" ) |
| 3591 | main.log.error( repr( response ) ) |
| 3592 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3593 | except AssertionError: |
| 3594 | main.log.exception( "" ) |
| 3595 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3596 | except TypeError: |
| 3597 | main.log.exception( self.name + ": Object not as expected" ) |
| 3598 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3599 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3600 | main.log.error( self.name + ": EOF exception found" ) |
| 3601 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3602 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3603 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3604 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3605 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3606 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3607 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3608 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3609 | CLI command to run for leadership of the Election test application. |
| 3610 | NOTE: Requires installation of the onos-app-election feature |
| 3611 | Returns: Main.TRUE on success |
| 3612 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3613 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3614 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3615 | cmdStr = "election-test-run" |
| 3616 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3617 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3618 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3619 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3620 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3621 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3622 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3623 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3624 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3625 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3626 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3627 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3628 | main.log.error( self.name + ": Error in electionTestRun on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3629 | ": " + "unexpected response" ) |
| 3630 | main.log.error( repr( response ) ) |
| 3631 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3632 | except AssertionError: |
| 3633 | main.log.exception( "" ) |
| 3634 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3635 | except TypeError: |
| 3636 | main.log.exception( self.name + ": Object not as expected" ) |
| 3637 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3638 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3639 | main.log.error( self.name + ": EOF exception found" ) |
| 3640 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3641 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3642 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3643 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3644 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3645 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3646 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3647 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3648 | * CLI command to withdraw the local node from leadership election for |
| 3649 | * the Election test application. |
| 3650 | #NOTE: Requires installation of the onos-app-election feature |
| 3651 | Returns: Main.TRUE on success |
| 3652 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3653 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3654 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3655 | cmdStr = "election-test-withdraw" |
| 3656 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3657 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3658 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3659 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3660 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3661 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3662 | if re.search( successPattern, response ): |
| 3663 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3664 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3665 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3666 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3667 | main.log.error( self.name + ": Error in electionTestWithdraw on " + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3668 | self.name + ": " + "unexpected response" ) |
| 3669 | main.log.error( repr( response ) ) |
| 3670 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3671 | except AssertionError: |
| 3672 | main.log.exception( "" ) |
| 3673 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3674 | except TypeError: |
| 3675 | main.log.exception( self.name + ": Object not as expected" ) |
| 3676 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3677 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3678 | main.log.error( self.name + ": EOF exception found" ) |
| 3679 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3680 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3681 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3682 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3683 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3684 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3685 | def getDevicePortsEnabledCount( self, dpid ): |
| 3686 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3687 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3688 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3689 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3690 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3691 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3692 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3693 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3694 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3695 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3696 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3697 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3698 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3699 | except AssertionError: |
| 3700 | main.log.exception( "" ) |
| 3701 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3702 | except TypeError: |
| 3703 | main.log.exception( self.name + ": Object not as expected" ) |
| 3704 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3705 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3706 | main.log.error( self.name + ": EOF exception found" ) |
| 3707 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3708 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3709 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3710 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3711 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3712 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3713 | def getDeviceLinksActiveCount( self, dpid ): |
| 3714 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3715 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3716 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3717 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3718 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3719 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3720 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3721 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3722 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3723 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3724 | main.log.error( self.name + ": Error in getting ports " ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3725 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3726 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3727 | except AssertionError: |
| 3728 | main.log.exception( "" ) |
| 3729 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3730 | except TypeError: |
| 3731 | main.log.exception( self.name + ": Object not as expected" ) |
| 3732 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3733 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3734 | main.log.error( self.name + ": EOF exception found" ) |
| 3735 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3736 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3737 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3738 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3739 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3740 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3741 | def getAllIntentIds( self ): |
| 3742 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3743 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3744 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3745 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3746 | cmdStr = "onos:intents | grep id=" |
| 3747 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3748 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3749 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3750 | if re.search( "Error", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3751 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3752 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3753 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3754 | except AssertionError: |
| 3755 | main.log.exception( "" ) |
| 3756 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3757 | except TypeError: |
| 3758 | main.log.exception( self.name + ": Object not as expected" ) |
| 3759 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3760 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3761 | main.log.error( self.name + ": EOF exception found" ) |
| 3762 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3763 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3764 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3765 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3766 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3767 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3768 | def intentSummary( self ): |
| 3769 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3770 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3771 | """ |
| 3772 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3773 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3774 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3775 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3776 | states.append( intent.get( 'state', None ) ) |
| 3777 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3778 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3779 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3780 | except ( TypeError, ValueError ): |
| 3781 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3782 | return None |
| 3783 | except pexpect.EOF: |
| 3784 | main.log.error( self.name + ": EOF exception found" ) |
| 3785 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3786 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3787 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3788 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3789 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3790 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3791 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3792 | """ |
| 3793 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3794 | Optional argument: |
| 3795 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3796 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3797 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3798 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3799 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3800 | cmdStr += " -j" |
| 3801 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3802 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3803 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3804 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3805 | except AssertionError: |
| 3806 | main.log.exception( "" ) |
| 3807 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3808 | except TypeError: |
| 3809 | main.log.exception( self.name + ": Object not as expected" ) |
| 3810 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3811 | except pexpect.EOF: |
| 3812 | main.log.error( self.name + ": EOF exception found" ) |
| 3813 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3814 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3815 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3816 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3817 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3818 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3819 | def leaderCandidates( self, jsonFormat=True ): |
| 3820 | """ |
| 3821 | Returns the output of the leaders -c command. |
| 3822 | Optional argument: |
| 3823 | * jsonFormat - boolean indicating if you want output in json |
| 3824 | """ |
| 3825 | try: |
| 3826 | cmdStr = "onos:leaders -c" |
| 3827 | if jsonFormat: |
| 3828 | cmdStr += " -j" |
| 3829 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3830 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3831 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3832 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3833 | except AssertionError: |
| 3834 | main.log.exception( "" ) |
| 3835 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3836 | except TypeError: |
| 3837 | main.log.exception( self.name + ": Object not as expected" ) |
| 3838 | return None |
| 3839 | except pexpect.EOF: |
| 3840 | main.log.error( self.name + ": EOF exception found" ) |
| 3841 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3842 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3843 | except Exception: |
| 3844 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3845 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3846 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3847 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3848 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3849 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3850 | topic parameter and an empty list if the topic doesn't exist |
| 3851 | If no leader is elected leader in the returned list will be "none" |
| 3852 | Returns None if there is a type error processing the json object |
| 3853 | """ |
| 3854 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3855 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3856 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3857 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3858 | assert "Command not found:" not in rawOutput, rawOutput |
| 3859 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3860 | results = [] |
| 3861 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3862 | if dict[ "topic" ] == topic: |
| 3863 | leader = dict[ "leader" ] |
| 3864 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3865 | results.append( leader ) |
| 3866 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3867 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3868 | except AssertionError: |
| 3869 | main.log.exception( "" ) |
| 3870 | return None |
| 3871 | except ( TypeError, ValueError ): |
| 3872 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3873 | return None |
| 3874 | except pexpect.EOF: |
| 3875 | main.log.error( self.name + ": EOF exception found" ) |
| 3876 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3877 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3878 | except Exception: |
| 3879 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3880 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3881 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3882 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3883 | """ |
| 3884 | Returns the output of the intent Pending map. |
| 3885 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3886 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3887 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3888 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3889 | cmdStr += " -j" |
| 3890 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3891 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3892 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3893 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3894 | except AssertionError: |
| 3895 | main.log.exception( "" ) |
| 3896 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3897 | except TypeError: |
| 3898 | main.log.exception( self.name + ": Object not as expected" ) |
| 3899 | return None |
| 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 | 6360493 | 2015-02-26 17:09:50 -0800 | [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 | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3907 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3908 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3909 | """ |
| 3910 | Returns the output of the raft partitions command for ONOS. |
| 3911 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3912 | # Sample JSON |
| 3913 | # { |
| 3914 | # "leader": "tcp://10.128.30.11:7238", |
| 3915 | # "members": [ |
| 3916 | # "tcp://10.128.30.11:7238", |
| 3917 | # "tcp://10.128.30.17:7238", |
| 3918 | # "tcp://10.128.30.13:7238", |
| 3919 | # ], |
| 3920 | # "name": "p1", |
| 3921 | # "term": 3 |
| 3922 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3923 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3924 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3925 | if candidates: |
| 3926 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3927 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3928 | cmdStr += " -j" |
| 3929 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3930 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3931 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3932 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3933 | except AssertionError: |
| 3934 | main.log.exception( "" ) |
| 3935 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3936 | except TypeError: |
| 3937 | main.log.exception( self.name + ": Object not as expected" ) |
| 3938 | return None |
| 3939 | except pexpect.EOF: |
| 3940 | main.log.error( self.name + ": EOF exception found" ) |
| 3941 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3942 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3943 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3944 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3945 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3946 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3947 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3948 | """ |
| 3949 | Returns the output of the apps command for ONOS. This command lists |
| 3950 | information about installed ONOS applications |
| 3951 | """ |
| 3952 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3953 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3954 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3955 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3956 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3957 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3958 | cmdStr = "onos:apps" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3959 | expectJson = False |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3960 | if summary: |
| 3961 | cmdStr += " -s" |
| 3962 | if active: |
| 3963 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3964 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3965 | cmdStr += " -j" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3966 | expectJson = True |
| 3967 | output = self.sendline( cmdStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3968 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3969 | assert "Command not found:" not in output, output |
| 3970 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3971 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3972 | # FIXME: look at specific exceptions/Errors |
| 3973 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3974 | main.log.exception( self.name + ": Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3975 | return None |
| 3976 | except TypeError: |
| 3977 | main.log.exception( self.name + ": Object not as expected" ) |
| 3978 | return None |
| 3979 | except pexpect.EOF: |
| 3980 | main.log.error( self.name + ": EOF exception found" ) |
| 3981 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3982 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3983 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3984 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3985 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3986 | |
You Wang | cdc51fe | 2018-08-12 17:14:56 -0700 | [diff] [blame] | 3987 | def appStatus( self, appName ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3988 | """ |
| 3989 | Uses the onos:apps cli command to return the status of an application. |
| 3990 | Returns: |
| 3991 | "ACTIVE" - If app is installed and activated |
| 3992 | "INSTALLED" - If app is installed and deactivated |
| 3993 | "UNINSTALLED" - If app is not installed |
| 3994 | None - on error |
| 3995 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3996 | try: |
| 3997 | if not isinstance( appName, types.StringType ): |
| 3998 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3999 | " a string" ) |
| 4000 | return None |
| 4001 | output = self.apps( jsonFormat=True ) |
| 4002 | appsJson = json.loads( output ) |
| 4003 | state = None |
| 4004 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4005 | if appName == app.get( 'name' ): |
| 4006 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4007 | break |
| 4008 | if state == "ACTIVE" or state == "INSTALLED": |
| 4009 | return state |
| 4010 | elif state is None: |
You Wang | 0d9f2c0 | 2018-08-10 14:56:32 -0700 | [diff] [blame] | 4011 | main.log.warn( "{} app not found".format( appName ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4012 | return "UNINSTALLED" |
| 4013 | elif state: |
| 4014 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 4015 | str( state ) ) |
| 4016 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4017 | except ( TypeError, ValueError ): |
| 4018 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4019 | return None |
| 4020 | except pexpect.EOF: |
| 4021 | main.log.error( self.name + ": EOF exception found" ) |
| 4022 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4023 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4024 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4025 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4026 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4027 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4028 | def app( self, appName, option ): |
| 4029 | """ |
| 4030 | Interacts with the app command for ONOS. This command manages |
| 4031 | application inventory. |
| 4032 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4033 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4034 | # Validate argument types |
| 4035 | valid = True |
| 4036 | if not isinstance( appName, types.StringType ): |
| 4037 | main.log.error( self.name + ".app(): appName must be a " + |
| 4038 | "string" ) |
| 4039 | valid = False |
| 4040 | if not isinstance( option, types.StringType ): |
| 4041 | main.log.error( self.name + ".app(): option must be a string" ) |
| 4042 | valid = False |
| 4043 | if not valid: |
| 4044 | return main.FALSE |
| 4045 | # Validate Option |
| 4046 | option = option.lower() |
| 4047 | # NOTE: Install may become a valid option |
| 4048 | if option == "activate": |
| 4049 | pass |
| 4050 | elif option == "deactivate": |
| 4051 | pass |
| 4052 | elif option == "uninstall": |
| 4053 | pass |
| 4054 | else: |
| 4055 | # Invalid option |
| 4056 | main.log.error( "The ONOS app command argument only takes " + |
| 4057 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4058 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4059 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4060 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4061 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 4062 | assert output is not None, "Error in sendline" |
| 4063 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4064 | if "Error executing command" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4065 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4066 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4067 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4068 | elif "No such application" in output: |
| 4069 | main.log.error( "The application '" + appName + |
| 4070 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4071 | return main.FALSE |
| 4072 | elif "Command not found:" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4073 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4074 | str( output ) ) |
| 4075 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4076 | elif "Unsupported command:" in output: |
| 4077 | main.log.error( "Incorrect command given to 'app': " + |
| 4078 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4079 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4080 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 4081 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4082 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 4083 | except AssertionError: |
| 4084 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4085 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4086 | except TypeError: |
| 4087 | main.log.exception( self.name + ": Object not as expected" ) |
| 4088 | return main.ERROR |
| 4089 | except pexpect.EOF: |
| 4090 | main.log.error( self.name + ": EOF exception found" ) |
| 4091 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4092 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4093 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4094 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4095 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4096 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4097 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4098 | """ |
| 4099 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4100 | appName is the hierarchical app name, not the feature name |
| 4101 | If check is True, method will check the status of the app after the |
| 4102 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4103 | Returns main.TRUE if the command was successfully sent |
| 4104 | main.FALSE if the cli responded with an error or given |
| 4105 | incorrect input |
| 4106 | """ |
| 4107 | try: |
| 4108 | if not isinstance( appName, types.StringType ): |
| 4109 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 4110 | " a string" ) |
| 4111 | return main.FALSE |
| 4112 | status = self.appStatus( appName ) |
| 4113 | if status == "INSTALLED": |
| 4114 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4115 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4116 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4117 | status = self.appStatus( appName ) |
| 4118 | if status == "ACTIVE": |
| 4119 | return main.TRUE |
| 4120 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 4121 | main.log.debug( "The state of application " + |
| 4122 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4123 | time.sleep( 1 ) |
| 4124 | return main.FALSE |
| 4125 | else: # not 'check' or command didn't succeed |
| 4126 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4127 | elif status == "ACTIVE": |
| 4128 | return main.TRUE |
| 4129 | elif status == "UNINSTALLED": |
| 4130 | main.log.error( self.name + ": Tried to activate the " + |
| 4131 | "application '" + appName + "' which is not " + |
| 4132 | "installed." ) |
| 4133 | else: |
| 4134 | main.log.error( "Unexpected return value from appStatus: " + |
| 4135 | str( status ) ) |
| 4136 | return main.ERROR |
| 4137 | except TypeError: |
| 4138 | main.log.exception( self.name + ": Object not as expected" ) |
| 4139 | return main.ERROR |
| 4140 | except pexpect.EOF: |
| 4141 | main.log.error( self.name + ": EOF exception found" ) |
| 4142 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4143 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4144 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4145 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4146 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4147 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4148 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4149 | """ |
| 4150 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4151 | appName is the hierarchical app name, not the feature name |
| 4152 | If check is True, method will check the status of the app after the |
| 4153 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4154 | Returns main.TRUE if the command was successfully sent |
| 4155 | main.FALSE if the cli responded with an error or given |
| 4156 | incorrect input |
| 4157 | """ |
| 4158 | try: |
| 4159 | if not isinstance( appName, types.StringType ): |
| 4160 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 4161 | "be a string" ) |
| 4162 | return main.FALSE |
| 4163 | status = self.appStatus( appName ) |
| 4164 | if status == "INSTALLED": |
| 4165 | return main.TRUE |
| 4166 | elif status == "ACTIVE": |
| 4167 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4168 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4169 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4170 | status = self.appStatus( appName ) |
| 4171 | if status == "INSTALLED": |
| 4172 | return main.TRUE |
| 4173 | else: |
| 4174 | time.sleep( 1 ) |
| 4175 | return main.FALSE |
| 4176 | else: # not check or command didn't succeed |
| 4177 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4178 | elif status == "UNINSTALLED": |
| 4179 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 4180 | "application '" + appName + "' which is not " + |
| 4181 | "installed." ) |
| 4182 | return main.TRUE |
| 4183 | else: |
| 4184 | main.log.error( "Unexpected return value from appStatus: " + |
| 4185 | str( status ) ) |
| 4186 | return main.ERROR |
| 4187 | except TypeError: |
| 4188 | main.log.exception( self.name + ": Object not as expected" ) |
| 4189 | return main.ERROR |
| 4190 | except pexpect.EOF: |
| 4191 | main.log.error( self.name + ": EOF exception found" ) |
| 4192 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4193 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4194 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4195 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4196 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4197 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4198 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4199 | """ |
| 4200 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4201 | appName is the hierarchical app name, not the feature name |
| 4202 | If check is True, method will check the status of the app after the |
| 4203 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4204 | Returns main.TRUE if the command was successfully sent |
| 4205 | main.FALSE if the cli responded with an error or given |
| 4206 | incorrect input |
| 4207 | """ |
| 4208 | # TODO: check with Thomas about the state machine for apps |
| 4209 | try: |
| 4210 | if not isinstance( appName, types.StringType ): |
| 4211 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 4212 | "be a string" ) |
| 4213 | return main.FALSE |
| 4214 | status = self.appStatus( appName ) |
| 4215 | if status == "INSTALLED": |
| 4216 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4217 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4218 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4219 | status = self.appStatus( appName ) |
| 4220 | if status == "UNINSTALLED": |
| 4221 | return main.TRUE |
| 4222 | else: |
| 4223 | time.sleep( 1 ) |
| 4224 | return main.FALSE |
| 4225 | else: # not check or command didn't succeed |
| 4226 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4227 | elif status == "ACTIVE": |
| 4228 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 4229 | "application '" + appName + "' which is " + |
| 4230 | "currently active." ) |
| 4231 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4232 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4233 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4234 | status = self.appStatus( appName ) |
| 4235 | if status == "UNINSTALLED": |
| 4236 | return main.TRUE |
| 4237 | else: |
| 4238 | time.sleep( 1 ) |
| 4239 | return main.FALSE |
| 4240 | else: # not check or command didn't succeed |
| 4241 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4242 | elif status == "UNINSTALLED": |
| 4243 | return main.TRUE |
| 4244 | else: |
| 4245 | main.log.error( "Unexpected return value from appStatus: " + |
| 4246 | str( status ) ) |
| 4247 | return main.ERROR |
| 4248 | except TypeError: |
| 4249 | main.log.exception( self.name + ": Object not as expected" ) |
| 4250 | return main.ERROR |
| 4251 | except pexpect.EOF: |
| 4252 | main.log.error( self.name + ": EOF exception found" ) |
| 4253 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4254 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4255 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4256 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4257 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4258 | |
| 4259 | def appIDs( self, jsonFormat=True ): |
| 4260 | """ |
| 4261 | Show the mappings between app id and app names given by the 'app-ids' |
| 4262 | cli command |
| 4263 | """ |
| 4264 | try: |
| 4265 | cmdStr = "app-ids" |
| 4266 | if jsonFormat: |
| 4267 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4268 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4269 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4270 | assert "Command not found:" not in output, output |
| 4271 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4272 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4273 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4274 | main.log.exception( self.name + ": Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4275 | return None |
| 4276 | except TypeError: |
| 4277 | main.log.exception( self.name + ": Object not as expected" ) |
| 4278 | return None |
| 4279 | except pexpect.EOF: |
| 4280 | main.log.error( self.name + ": EOF exception found" ) |
| 4281 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4282 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4283 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4284 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4285 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4286 | |
| 4287 | def appToIDCheck( self ): |
| 4288 | """ |
| 4289 | This method will check that each application's ID listed in 'apps' is |
| 4290 | the same as the ID listed in 'app-ids'. The check will also check that |
| 4291 | there are no duplicate IDs issued. Note that an app ID should be |
| 4292 | a globaly unique numerical identifier for app/app-like features. Once |
| 4293 | an ID is registered, the ID is never freed up so that if an app is |
| 4294 | reinstalled it will have the same ID. |
| 4295 | |
| 4296 | Returns: main.TRUE if the check passes and |
| 4297 | main.FALSE if the check fails or |
| 4298 | main.ERROR if there is some error in processing the test |
| 4299 | """ |
| 4300 | try: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4301 | # Grab IDs |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4302 | rawJson = self.appIDs( jsonFormat=True ) |
| 4303 | if rawJson: |
| 4304 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4305 | else: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4306 | main.log.error( "app-ids returned nothing: " + repr( rawJson ) ) |
| 4307 | return main.FALSE |
| 4308 | |
| 4309 | # Grab Apps |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4310 | rawJson = self.apps( jsonFormat=True ) |
| 4311 | if rawJson: |
| 4312 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4313 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4314 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4315 | return main.FALSE |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4316 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4317 | result = main.TRUE |
| 4318 | for app in apps: |
| 4319 | appID = app.get( 'id' ) |
| 4320 | if appID is None: |
| 4321 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4322 | result = main.FALSE |
| 4323 | appName = app.get( 'name' ) |
| 4324 | if appName is None: |
| 4325 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4326 | result = main.FALSE |
| 4327 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4328 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4329 | if not current: # if ids doesn't have this id |
| 4330 | result = main.FALSE |
| 4331 | main.log.error( "'app-ids' does not have the ID for " + |
| 4332 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4333 | main.log.debug( "apps command returned: " + str( app ) + |
| 4334 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4335 | elif len( current ) > 1: |
| 4336 | # there is more than one app with this ID |
| 4337 | result = main.FALSE |
| 4338 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4339 | elif not current[ 0 ][ 'name' ] == appName: |
| 4340 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4341 | result = main.FALSE |
| 4342 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4343 | " registered under id:" + str( appID ) + |
| 4344 | " but 'apps' has " + str( appName ) ) |
| 4345 | else: |
| 4346 | pass # id and name match! |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4347 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4348 | # now make sure that app-ids has no duplicates |
| 4349 | idsList = [] |
| 4350 | namesList = [] |
| 4351 | for item in ids: |
| 4352 | idsList.append( item[ 'id' ] ) |
| 4353 | namesList.append( item[ 'name' ] ) |
| 4354 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4355 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4356 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4357 | + json.dumps( ids, |
| 4358 | sort_keys=True, |
| 4359 | indent=4, |
| 4360 | separators=( ',', ': ' ) ) ) |
| 4361 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4362 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4363 | except ( TypeError, ValueError ): |
| 4364 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4365 | return main.ERROR |
| 4366 | except pexpect.EOF: |
| 4367 | main.log.error( self.name + ": EOF exception found" ) |
| 4368 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4369 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4370 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4371 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4372 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4373 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4374 | def getCfg( self, component=None, propName=None, short=False, |
| 4375 | jsonFormat=True ): |
| 4376 | """ |
| 4377 | Get configuration settings from onos cli |
| 4378 | Optional arguments: |
| 4379 | component - Optionally only list configurations for a specific |
| 4380 | component. If None, all components with configurations |
| 4381 | are displayed. Case Sensitive string. |
| 4382 | propName - If component is specified, propName option will show |
| 4383 | only this specific configuration from that component. |
| 4384 | Case Sensitive string. |
| 4385 | jsonFormat - Returns output as json. Note that this will override |
| 4386 | the short option |
| 4387 | short - Short, less verbose, version of configurations. |
| 4388 | This is overridden by the json option |
| 4389 | returns: |
| 4390 | Output from cli as a string or None on error |
| 4391 | """ |
| 4392 | try: |
| 4393 | baseStr = "cfg" |
| 4394 | cmdStr = " get" |
| 4395 | componentStr = "" |
| 4396 | if component: |
| 4397 | componentStr += " " + component |
| 4398 | if propName: |
| 4399 | componentStr += " " + propName |
| 4400 | if jsonFormat: |
| 4401 | baseStr += " -j" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4402 | expectJson = True |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4403 | elif short: |
| 4404 | baseStr += " -s" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4405 | expectJson = False |
| 4406 | output = self.sendline( baseStr + cmdStr + componentStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4407 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4408 | assert "Command not found:" not in output, output |
| 4409 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4410 | return output |
| 4411 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4412 | main.log.exception( self.name + ": Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4413 | return None |
| 4414 | except TypeError: |
| 4415 | main.log.exception( self.name + ": Object not as expected" ) |
| 4416 | return None |
| 4417 | except pexpect.EOF: |
| 4418 | main.log.error( self.name + ": EOF exception found" ) |
| 4419 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4420 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4421 | except Exception: |
| 4422 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4423 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4424 | |
| 4425 | def setCfg( self, component, propName, value=None, check=True ): |
| 4426 | """ |
| 4427 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4428 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4429 | component - The case sensitive name of the component whose |
| 4430 | property is to be set |
| 4431 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4432 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4433 | 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] | 4434 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4435 | check - Boolean, Check whether the option was successfully set this |
| 4436 | only applies when a value is given. |
| 4437 | returns: |
| 4438 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4439 | will return main.TRUE unless there is an error |
| 4440 | """ |
| 4441 | try: |
| 4442 | baseStr = "cfg" |
| 4443 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4444 | if value is not None: |
| 4445 | cmdStr += " " + str( value ) |
| 4446 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4447 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4448 | assert "Command not found:" not in output, output |
| 4449 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4450 | if value and check: |
| 4451 | results = self.getCfg( component=str( component ), |
| 4452 | propName=str( propName ), |
| 4453 | jsonFormat=True ) |
| 4454 | # Check if current value is what we just set |
| 4455 | try: |
| 4456 | jsonOutput = json.loads( results ) |
| 4457 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4458 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4459 | main.log.exception( "Error parsing cfg output" ) |
| 4460 | main.log.error( "output:" + repr( results ) ) |
| 4461 | return main.FALSE |
| 4462 | if current == str( value ): |
| 4463 | return main.TRUE |
| 4464 | return main.FALSE |
| 4465 | return main.TRUE |
| 4466 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4467 | main.log.exception( self.name + ": Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4468 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4469 | except ( TypeError, ValueError ): |
| 4470 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4471 | return main.FALSE |
| 4472 | except pexpect.EOF: |
| 4473 | main.log.error( self.name + ": EOF exception found" ) |
| 4474 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4475 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4476 | except Exception: |
| 4477 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4478 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4479 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4480 | def distPrimitivesSend( self, cmd ): |
| 4481 | """ |
| 4482 | Function to handle sending cli commands for the distributed primitives test app |
| 4483 | |
| 4484 | This command will catch some exceptions and retry the command on some |
| 4485 | specific store exceptions. |
| 4486 | |
| 4487 | Required arguments: |
| 4488 | cmd - The command to send to the cli |
| 4489 | returns: |
| 4490 | string containing the cli output |
| 4491 | None on Error |
| 4492 | """ |
| 4493 | try: |
| 4494 | output = self.sendline( cmd ) |
| 4495 | try: |
| 4496 | assert output is not None, "Error in sendline" |
| 4497 | # TODO: Maybe make this less hardcoded |
| 4498 | # ConsistentMap Exceptions |
| 4499 | assert "org.onosproject.store.service" not in output |
| 4500 | # Node not leader |
| 4501 | assert "java.lang.IllegalStateException" not in output |
| 4502 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4503 | main.log.error( self.name + ": Error in processing '" + cmd + "' " + |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4504 | "command: " + str( output ) ) |
| 4505 | retryTime = 30 # Conservative time, given by Madan |
| 4506 | main.log.info( "Waiting " + str( retryTime ) + |
| 4507 | "seconds before retrying." ) |
| 4508 | time.sleep( retryTime ) # Due to change in mastership |
| 4509 | output = self.sendline( cmd ) |
| 4510 | assert output is not None, "Error in sendline" |
| 4511 | assert "Command not found:" not in output, output |
| 4512 | assert "Error executing command" not in output, output |
| 4513 | main.log.info( self.name + ": " + output ) |
| 4514 | return output |
| 4515 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4516 | main.log.exception( self.name + ": Error in processing '" + cmd + "' command." ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4517 | return None |
| 4518 | except TypeError: |
| 4519 | main.log.exception( self.name + ": Object not as expected" ) |
| 4520 | return None |
| 4521 | except pexpect.EOF: |
| 4522 | main.log.error( self.name + ": EOF exception found" ) |
| 4523 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4524 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4525 | except Exception: |
| 4526 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4527 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4528 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4529 | def setTestAdd( self, setName, values ): |
| 4530 | """ |
| 4531 | CLI command to add elements to a distributed set. |
| 4532 | Arguments: |
| 4533 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4534 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4535 | Example usages: |
| 4536 | setTestAdd( "set1", "a b c" ) |
| 4537 | setTestAdd( "set2", "1" ) |
| 4538 | returns: |
| 4539 | main.TRUE on success OR |
| 4540 | main.FALSE if elements were already in the set OR |
| 4541 | main.ERROR on error |
| 4542 | """ |
| 4543 | try: |
| 4544 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4545 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4546 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4547 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4548 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4549 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4550 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4551 | return main.FALSE |
| 4552 | else: |
| 4553 | main.log.error( self.name + ": setTestAdd did not" + |
| 4554 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4555 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4556 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4557 | except TypeError: |
| 4558 | main.log.exception( self.name + ": Object not as expected" ) |
| 4559 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4560 | except Exception: |
| 4561 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4562 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4563 | |
| 4564 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4565 | """ |
| 4566 | CLI command to remove elements from a distributed set. |
| 4567 | Required arguments: |
| 4568 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4569 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4570 | Optional arguments: |
| 4571 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4572 | retain - Retain only the given values. (intersection of the |
| 4573 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4574 | returns: |
| 4575 | main.TRUE on success OR |
| 4576 | main.FALSE if the set was not changed OR |
| 4577 | main.ERROR on error |
| 4578 | """ |
| 4579 | try: |
| 4580 | cmdStr = "set-test-remove " |
| 4581 | if clear: |
| 4582 | cmdStr += "-c " + str( setName ) |
| 4583 | elif retain: |
| 4584 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4585 | else: |
| 4586 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4587 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4588 | if clear: |
| 4589 | pattern = "Set " + str( setName ) + " cleared" |
| 4590 | if re.search( pattern, output ): |
| 4591 | return main.TRUE |
| 4592 | elif retain: |
| 4593 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4594 | "only elements of set \[(.*)\]" |
| 4595 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4596 | "retaining only elements of the set " +\ |
| 4597 | "\[(.*)\]" |
| 4598 | if re.search( positivePattern, output ): |
| 4599 | return main.TRUE |
| 4600 | elif re.search( negativePattern, output ): |
| 4601 | return main.FALSE |
| 4602 | else: |
| 4603 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4604 | str( setName ) |
| 4605 | if ( len( values.split() ) == 1 ): |
| 4606 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4607 | str( setName ) |
| 4608 | else: |
| 4609 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4610 | str( setName ) |
| 4611 | if re.search( positivePattern, output ): |
| 4612 | return main.TRUE |
| 4613 | elif re.search( negativePattern, output ): |
| 4614 | return main.FALSE |
| 4615 | main.log.error( self.name + ": setTestRemove did not" + |
| 4616 | " match expected output" ) |
| 4617 | main.log.debug( self.name + " expected: " + pattern ) |
| 4618 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4619 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4620 | except TypeError: |
| 4621 | main.log.exception( self.name + ": Object not as expected" ) |
| 4622 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4623 | except Exception: |
| 4624 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4625 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4626 | |
| 4627 | def setTestGet( self, setName, values="" ): |
| 4628 | """ |
| 4629 | CLI command to get the elements in a distributed set. |
| 4630 | Required arguments: |
| 4631 | setName - The name of the set to remove from. |
| 4632 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4633 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4634 | returns: |
| 4635 | main.ERROR on error OR |
| 4636 | A list of elements in the set if no optional arguments are |
| 4637 | supplied OR |
| 4638 | A tuple containing the list then: |
| 4639 | main.FALSE if the given values are not in the set OR |
| 4640 | main.TRUE if the given values are in the set OR |
| 4641 | """ |
| 4642 | try: |
| 4643 | values = str( values ).strip() |
| 4644 | setName = str( setName ).strip() |
| 4645 | length = len( values.split() ) |
| 4646 | containsCheck = None |
| 4647 | # Patterns to match |
| 4648 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4649 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4650 | containsTrue = "Set " + setName + " contains the value " + values |
| 4651 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4652 | values |
| 4653 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4654 | setPattern |
| 4655 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4656 | " subset " + setPattern |
| 4657 | |
| 4658 | cmdStr = "set-test-get " |
| 4659 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4660 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4661 | if length == 0: |
| 4662 | match = re.search( pattern, output ) |
| 4663 | else: # if given values |
| 4664 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4665 | patternTrue = pattern + "\r\n" + containsTrue |
| 4666 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4667 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4668 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4669 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4670 | matchTrue = re.search( patternTrue, output ) |
| 4671 | matchFalse = re.search( patternFalse, output ) |
| 4672 | if matchTrue: |
| 4673 | containsCheck = main.TRUE |
| 4674 | match = matchTrue |
| 4675 | elif matchFalse: |
| 4676 | containsCheck = main.FALSE |
| 4677 | match = matchFalse |
| 4678 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4679 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4680 | "expected output" ) |
| 4681 | main.log.debug( self.name + " expected: " + pattern ) |
| 4682 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4683 | match = None |
| 4684 | if match: |
| 4685 | setMatch = match.group( 1 ) |
| 4686 | if setMatch == '': |
| 4687 | setList = [] |
| 4688 | else: |
| 4689 | setList = setMatch.split( ", " ) |
| 4690 | if length > 0: |
| 4691 | return ( setList, containsCheck ) |
| 4692 | else: |
| 4693 | return setList |
| 4694 | else: # no match |
| 4695 | main.log.error( self.name + ": setTestGet did not" + |
| 4696 | " match expected output" ) |
| 4697 | main.log.debug( self.name + " expected: " + pattern ) |
| 4698 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4699 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4700 | except TypeError: |
| 4701 | main.log.exception( self.name + ": Object not as expected" ) |
| 4702 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4703 | except Exception: |
| 4704 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4705 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4706 | |
| 4707 | def setTestSize( self, setName ): |
| 4708 | """ |
| 4709 | CLI command to get the elements in a distributed set. |
| 4710 | Required arguments: |
| 4711 | setName - The name of the set to remove from. |
| 4712 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4713 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4714 | None on error |
| 4715 | """ |
| 4716 | try: |
| 4717 | # TODO: Should this check against the number of elements returned |
| 4718 | # and then return true/false based on that? |
| 4719 | setName = str( setName ).strip() |
| 4720 | # Patterns to match |
| 4721 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4722 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4723 | setPattern |
| 4724 | cmdStr = "set-test-get -s " |
| 4725 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4726 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4727 | if output: |
| 4728 | match = re.search( pattern, output ) |
| 4729 | if match: |
| 4730 | setSize = int( match.group( 1 ) ) |
| 4731 | setMatch = match.group( 2 ) |
| 4732 | if len( setMatch.split() ) == setSize: |
| 4733 | main.log.info( "The size returned by " + self.name + |
| 4734 | " matches the number of elements in " + |
| 4735 | "the returned set" ) |
| 4736 | else: |
| 4737 | main.log.error( "The size returned by " + self.name + |
| 4738 | " does not match the number of " + |
| 4739 | "elements in the returned set." ) |
| 4740 | return setSize |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4741 | else: # no match |
| 4742 | main.log.error( self.name + ": setTestGet did not" + |
| 4743 | " match expected output" ) |
| 4744 | main.log.debug( self.name + " expected: " + pattern ) |
| 4745 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4746 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4747 | except TypeError: |
| 4748 | main.log.exception( self.name + ": Object not as expected" ) |
| 4749 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4750 | except Exception: |
| 4751 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4752 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4753 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4754 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4755 | """ |
| 4756 | Command to list the various counters in the system. |
| 4757 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4758 | if jsonFormat, a string of the json object returned by the cli |
| 4759 | command |
| 4760 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4761 | None on error |
| 4762 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4763 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4764 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4765 | if jsonFormat: |
| 4766 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4767 | output = self.sendline( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4768 | main.log.debug( self.name + ": Counters unparsed: " + output ) |
| 4769 | output = output.split( "\r\n" )[ -1 ] |
| 4770 | main.log.debug( self.name + ": Counters parsed: " + output ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4771 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4772 | assert "Command not found:" not in output, output |
| 4773 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4774 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4775 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4776 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4777 | main.log.exception( self.name + ": Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4778 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4779 | except TypeError: |
| 4780 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4781 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4782 | except pexpect.EOF: |
| 4783 | main.log.error( self.name + ": EOF exception found" ) |
| 4784 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4785 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4786 | except Exception: |
| 4787 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4788 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4789 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4790 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4791 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4792 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4793 | Required arguments: |
| 4794 | counter - The name of the counter to increment. |
| 4795 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4796 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4797 | returns: |
| 4798 | integer value of the counter or |
| 4799 | None on Error |
| 4800 | """ |
| 4801 | try: |
| 4802 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4803 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4804 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4805 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4806 | if delta != 1: |
| 4807 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4808 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4809 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4810 | match = re.search( pattern, output ) |
| 4811 | if match: |
| 4812 | return int( match.group( 1 ) ) |
| 4813 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4814 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4815 | " match expected output." ) |
| 4816 | main.log.debug( self.name + " expected: " + pattern ) |
| 4817 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4818 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4819 | except TypeError: |
| 4820 | main.log.exception( self.name + ": Object not as expected" ) |
| 4821 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4822 | except Exception: |
| 4823 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4824 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4825 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4826 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4827 | """ |
| 4828 | CLI command to get a distributed counter then add a delta to it. |
| 4829 | Required arguments: |
| 4830 | counter - The name of the counter to increment. |
| 4831 | Optional arguments: |
| 4832 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4833 | returns: |
| 4834 | integer value of the counter or |
| 4835 | None on Error |
| 4836 | """ |
| 4837 | try: |
| 4838 | counter = str( counter ) |
| 4839 | delta = int( delta ) |
| 4840 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4841 | cmdStr += counter |
| 4842 | if delta != 1: |
| 4843 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4844 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4845 | pattern = counter + " was updated to (-?\d+)" |
| 4846 | match = re.search( pattern, output ) |
| 4847 | if match: |
| 4848 | return int( match.group( 1 ) ) |
| 4849 | else: |
| 4850 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4851 | " match expected output." ) |
| 4852 | main.log.debug( self.name + " expected: " + pattern ) |
| 4853 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4854 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4855 | except TypeError: |
| 4856 | main.log.exception( self.name + ": Object not as expected" ) |
| 4857 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4858 | except Exception: |
| 4859 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4860 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4861 | |
| 4862 | def valueTestGet( self, valueName ): |
| 4863 | """ |
| 4864 | CLI command to get the value of an atomic value. |
| 4865 | Required arguments: |
| 4866 | valueName - The name of the value to get. |
| 4867 | returns: |
| 4868 | string value of the value or |
| 4869 | None on Error |
| 4870 | """ |
| 4871 | try: |
| 4872 | valueName = str( valueName ) |
| 4873 | cmdStr = "value-test " |
| 4874 | operation = "get" |
| 4875 | cmdStr = "value-test {} {}".format( valueName, |
| 4876 | operation ) |
| 4877 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4878 | main.log.debug( self.name + ": value test unparsed: " + output ) |
| 4879 | output = output.split( "\r\n" )[ -1 ] |
| 4880 | main.log.debug( self.name + ": value test parsed: " + output ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4881 | pattern = "(\w+)" |
| 4882 | match = re.search( pattern, output ) |
| 4883 | if match: |
| 4884 | return match.group( 1 ) |
| 4885 | else: |
| 4886 | main.log.error( self.name + ": valueTestGet did not" + |
| 4887 | " match expected output." ) |
| 4888 | main.log.debug( self.name + " expected: " + pattern ) |
| 4889 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4890 | return None |
| 4891 | except TypeError: |
| 4892 | main.log.exception( self.name + ": Object not as expected" ) |
| 4893 | return None |
| 4894 | except Exception: |
| 4895 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4896 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4897 | |
| 4898 | def valueTestSet( self, valueName, newValue ): |
| 4899 | """ |
| 4900 | CLI command to set the value of an atomic value. |
| 4901 | Required arguments: |
| 4902 | valueName - The name of the value to set. |
| 4903 | newValue - The value to assign to the given value. |
| 4904 | returns: |
| 4905 | main.TRUE on success or |
| 4906 | main.ERROR on Error |
| 4907 | """ |
| 4908 | try: |
| 4909 | valueName = str( valueName ) |
| 4910 | newValue = str( newValue ) |
| 4911 | operation = "set" |
| 4912 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4913 | operation, |
| 4914 | newValue ) |
| 4915 | output = self.distPrimitivesSend( cmdStr ) |
| 4916 | if output is not None: |
| 4917 | return main.TRUE |
| 4918 | else: |
| 4919 | return main.ERROR |
| 4920 | except TypeError: |
| 4921 | main.log.exception( self.name + ": Object not as expected" ) |
| 4922 | return main.ERROR |
| 4923 | except Exception: |
| 4924 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4925 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4926 | |
| 4927 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4928 | """ |
| 4929 | CLI command to compareAndSet the value of an atomic value. |
| 4930 | Required arguments: |
| 4931 | valueName - The name of the value. |
| 4932 | oldValue - Compare the current value of the atomic value to this |
| 4933 | newValue - If the value equals oldValue, set the value to newValue |
| 4934 | returns: |
| 4935 | main.TRUE on success or |
| 4936 | main.FALSE on failure or |
| 4937 | main.ERROR on Error |
| 4938 | """ |
| 4939 | try: |
| 4940 | valueName = str( valueName ) |
| 4941 | oldValue = str( oldValue ) |
| 4942 | newValue = str( newValue ) |
| 4943 | operation = "compareAndSet" |
| 4944 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4945 | operation, |
| 4946 | oldValue, |
| 4947 | newValue ) |
| 4948 | output = self.distPrimitivesSend( cmdStr ) |
| 4949 | pattern = "(\w+)" |
| 4950 | match = re.search( pattern, output ) |
| 4951 | if match: |
| 4952 | result = match.group( 1 ) |
| 4953 | if result == "true": |
| 4954 | return main.TRUE |
| 4955 | elif result == "false": |
| 4956 | return main.FALSE |
| 4957 | else: |
| 4958 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4959 | " match expected output." ) |
| 4960 | main.log.debug( self.name + " expected: " + pattern ) |
| 4961 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4962 | return main.ERROR |
| 4963 | except TypeError: |
| 4964 | main.log.exception( self.name + ": Object not as expected" ) |
| 4965 | return main.ERROR |
| 4966 | except Exception: |
| 4967 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4968 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4969 | |
| 4970 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4971 | """ |
| 4972 | CLI command to getAndSet the value of an atomic value. |
| 4973 | Required arguments: |
| 4974 | valueName - The name of the value to get. |
| 4975 | newValue - The value to assign to the given value |
| 4976 | returns: |
| 4977 | string value of the value or |
| 4978 | None on Error |
| 4979 | """ |
| 4980 | try: |
| 4981 | valueName = str( valueName ) |
| 4982 | cmdStr = "value-test " |
| 4983 | operation = "getAndSet" |
| 4984 | cmdStr += valueName + " " + operation |
| 4985 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4986 | operation, |
| 4987 | newValue ) |
| 4988 | output = self.distPrimitivesSend( cmdStr ) |
| 4989 | pattern = "(\w+)" |
| 4990 | match = re.search( pattern, output ) |
| 4991 | if match: |
| 4992 | return match.group( 1 ) |
| 4993 | else: |
| 4994 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4995 | " match expected output." ) |
| 4996 | main.log.debug( self.name + " expected: " + pattern ) |
| 4997 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4998 | return None |
| 4999 | except TypeError: |
| 5000 | main.log.exception( self.name + ": Object not as expected" ) |
| 5001 | return None |
| 5002 | except Exception: |
| 5003 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5004 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 5005 | |
| 5006 | def valueTestDestroy( self, valueName ): |
| 5007 | """ |
| 5008 | CLI command to destroy an atomic value. |
| 5009 | Required arguments: |
| 5010 | valueName - The name of the value to destroy. |
| 5011 | returns: |
| 5012 | main.TRUE on success or |
| 5013 | main.ERROR on Error |
| 5014 | """ |
| 5015 | try: |
| 5016 | valueName = str( valueName ) |
| 5017 | cmdStr = "value-test " |
| 5018 | operation = "destroy" |
| 5019 | cmdStr += valueName + " " + operation |
| 5020 | output = self.distPrimitivesSend( cmdStr ) |
| 5021 | if output is not None: |
| 5022 | return main.TRUE |
| 5023 | else: |
| 5024 | return main.ERROR |
| 5025 | except TypeError: |
| 5026 | main.log.exception( self.name + ": Object not as expected" ) |
| 5027 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 5028 | except Exception: |
| 5029 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5030 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 5031 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5032 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 5033 | """ |
| 5034 | Description: Execute summary command in onos |
| 5035 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 5036 | no output |
| 5037 | |
| 5038 | """ |
| 5039 | try: |
| 5040 | cmdStr = "summary" |
| 5041 | if jsonFormat: |
| 5042 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5043 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5044 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5045 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 5046 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 5047 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 5048 | if not handle: |
| 5049 | main.log.error( self.name + ": There is no output in " + |
| 5050 | "summary command" ) |
| 5051 | return main.FALSE |
| 5052 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5053 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 5054 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5055 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 5056 | except TypeError: |
| 5057 | main.log.exception( self.name + ": Object not as expected" ) |
| 5058 | return None |
| 5059 | except pexpect.EOF: |
| 5060 | main.log.error( self.name + ": EOF exception found" ) |
| 5061 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5062 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 5063 | except Exception: |
| 5064 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5065 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5066 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 5067 | def getAddress( self): |
| 5068 | """ |
| 5069 | Get the onos ip address from the cli. This is usefull when connecting using |
| 5070 | a container manager such as kubernetes. This function also sets self.address |
| 5071 | the value from ONOS. |
| 5072 | |
| 5073 | Returns: |
| 5074 | The string value of the key or |
| 5075 | None on Error |
| 5076 | """ |
| 5077 | try: |
| 5078 | output = self.summary() |
| 5079 | address = json.loads( output ).get( 'node' ) |
| 5080 | self.address = address |
| 5081 | return address |
You Wang | 327bad4 | 2021-03-24 14:19:58 -0700 | [diff] [blame] | 5082 | except ( TypeError, ValueError ): |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 5083 | main.log.exception( self.name + ": Object not as expected" ) |
| 5084 | return None |
| 5085 | except Exception: |
| 5086 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5087 | main.cleanAndExit() |
| 5088 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 5089 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5090 | """ |
| 5091 | CLI command to get the value of a key in a consistent map using |
| 5092 | transactions. This a test function and can only get keys from the |
| 5093 | test map hard coded into the cli command |
| 5094 | Required arguments: |
| 5095 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5096 | returns: |
| 5097 | The string value of the key or |
| 5098 | None on Error |
| 5099 | """ |
| 5100 | try: |
| 5101 | keyName = str( keyName ) |
| 5102 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5103 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 5104 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5105 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 5106 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 5107 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5108 | return None |
| 5109 | else: |
| 5110 | match = re.search( pattern, output ) |
| 5111 | if match: |
| 5112 | return match.groupdict()[ 'value' ] |
| 5113 | else: |
| 5114 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 5115 | " match expected output." ) |
| 5116 | main.log.debug( self.name + " expected: " + pattern ) |
| 5117 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 5118 | return None |
| 5119 | except TypeError: |
| 5120 | main.log.exception( self.name + ": Object not as expected" ) |
| 5121 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5122 | except Exception: |
| 5123 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5124 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5125 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 5126 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5127 | """ |
| 5128 | CLI command to put a value into 'numKeys' number of keys in a |
| 5129 | consistent map using transactions. This a test function and can only |
| 5130 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 5131 | Required arguments: |
| 5132 | numKeys - Number of keys to add the value to |
| 5133 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5134 | returns: |
| 5135 | A dictionary whose keys are the name of the keys put into the map |
| 5136 | and the values of the keys are dictionaries whose key-values are |
| 5137 | 'value': value put into map and optionaly |
| 5138 | 'oldValue': Previous value in the key or |
| 5139 | None on Error |
| 5140 | |
| 5141 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5142 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 5143 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5144 | """ |
| 5145 | try: |
| 5146 | numKeys = str( numKeys ) |
| 5147 | value = str( value ) |
| 5148 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5149 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 5150 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5151 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 5152 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 5153 | results = {} |
| 5154 | for line in output.splitlines(): |
| 5155 | new = re.search( newPattern, line ) |
| 5156 | updated = re.search( updatedPattern, line ) |
| 5157 | if new: |
| 5158 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 5159 | elif updated: |
| 5160 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5161 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5162 | else: |
| 5163 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 5164 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5165 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 5166 | newPattern, |
| 5167 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5168 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 5169 | return results |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 5170 | except ( TypeError, AttributeError ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5171 | main.log.exception( self.name + ": Object not as expected" ) |
| 5172 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5173 | except Exception: |
| 5174 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5175 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5176 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5177 | def maps( self, jsonFormat=True ): |
| 5178 | """ |
| 5179 | Description: Returns result of onos:maps |
| 5180 | Optional: |
| 5181 | * jsonFormat: enable json formatting of output |
| 5182 | """ |
| 5183 | try: |
| 5184 | cmdStr = "maps" |
| 5185 | if jsonFormat: |
| 5186 | cmdStr += " -j" |
| 5187 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5188 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5189 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5190 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5191 | except AssertionError: |
| 5192 | main.log.exception( "" ) |
| 5193 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5194 | except TypeError: |
| 5195 | main.log.exception( self.name + ": Object not as expected" ) |
| 5196 | return None |
| 5197 | except pexpect.EOF: |
| 5198 | main.log.error( self.name + ": EOF exception found" ) |
| 5199 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5200 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5201 | except Exception: |
| 5202 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5203 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5204 | |
| 5205 | def getSwController( self, uri, jsonFormat=True ): |
| 5206 | """ |
| 5207 | Descrition: Gets the controller information from the device |
| 5208 | """ |
| 5209 | try: |
| 5210 | cmd = "device-controllers " |
| 5211 | if jsonFormat: |
| 5212 | cmd += "-j " |
| 5213 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5214 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5215 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5216 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5217 | except AssertionError: |
| 5218 | main.log.exception( "" ) |
| 5219 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5220 | except TypeError: |
| 5221 | main.log.exception( self.name + ": Object not as expected" ) |
| 5222 | return None |
| 5223 | except pexpect.EOF: |
| 5224 | main.log.error( self.name + ": EOF exception found" ) |
| 5225 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5226 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5227 | except Exception: |
| 5228 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5229 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5230 | |
| 5231 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 5232 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5233 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5234 | |
| 5235 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5236 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5237 | ip - String or List: The ip address of the controller. |
| 5238 | This parameter can be formed in a couple of different ways. |
| 5239 | VALID: |
| 5240 | 10.0.0.1 - just the ip address |
| 5241 | tcp:10.0.0.1 - the protocol and the ip address |
| 5242 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 5243 | so that you can add controllers with different |
| 5244 | protocols and ports |
| 5245 | INVALID: |
| 5246 | 10.0.0.1:6653 - this is not supported by ONOS |
| 5247 | |
| 5248 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 5249 | port - The port number. |
| 5250 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 5251 | |
| 5252 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 5253 | """ |
| 5254 | try: |
| 5255 | cmd = "device-setcontrollers" |
| 5256 | |
| 5257 | if jsonFormat: |
| 5258 | cmd += " -j" |
| 5259 | cmd += " " + uri |
| 5260 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5261 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5262 | for item in ip: |
| 5263 | if ":" in item: |
| 5264 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5265 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5266 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5267 | elif "." in sitem[ 1 ]: |
| 5268 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5269 | else: |
| 5270 | main.log.error( "Malformed entry: " + item ) |
| 5271 | raise TypeError |
| 5272 | else: |
| 5273 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5274 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5275 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5276 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5277 | if "Error" in response: |
| 5278 | main.log.error( response ) |
| 5279 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5280 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5281 | except AssertionError: |
| 5282 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5283 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5284 | except TypeError: |
| 5285 | main.log.exception( self.name + ": Object not as expected" ) |
| 5286 | return main.FALSE |
| 5287 | except pexpect.EOF: |
| 5288 | main.log.error( self.name + ": EOF exception found" ) |
| 5289 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5290 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5291 | except Exception: |
| 5292 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5293 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5294 | |
| 5295 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5296 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5297 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5298 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5299 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5300 | 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] | 5301 | Returns: |
| 5302 | Returns main.FALSE if an exception is thrown or an error is present |
| 5303 | in the response. Otherwise, returns main.TRUE. |
| 5304 | NOTE: |
| 5305 | 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] | 5306 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5307 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5308 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 5309 | deviceStr = device |
| 5310 | device = [] |
| 5311 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5312 | |
| 5313 | for d in device: |
| 5314 | time.sleep( 1 ) |
| 5315 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5316 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5317 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5318 | if "Error" in response: |
| 5319 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 5320 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5321 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5322 | except AssertionError: |
| 5323 | main.log.exception( "" ) |
| 5324 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5325 | except TypeError: |
| 5326 | main.log.exception( self.name + ": Object not as expected" ) |
| 5327 | return main.FALSE |
| 5328 | except pexpect.EOF: |
| 5329 | main.log.error( self.name + ": EOF exception found" ) |
| 5330 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5331 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5332 | except Exception: |
| 5333 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5334 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5335 | |
| 5336 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5337 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5338 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5339 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5340 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5341 | 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] | 5342 | Returns: |
| 5343 | Returns main.FALSE if an exception is thrown or an error is present |
| 5344 | in the response. Otherwise, returns main.TRUE. |
| 5345 | NOTE: |
| 5346 | 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] | 5347 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5348 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5349 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5350 | host = list( host ) |
| 5351 | |
| 5352 | for h in host: |
| 5353 | time.sleep( 1 ) |
| 5354 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5355 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5356 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5357 | if "Error" in response: |
| 5358 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5359 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5360 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5361 | except AssertionError: |
| 5362 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5363 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5364 | except TypeError: |
| 5365 | main.log.exception( self.name + ": Object not as expected" ) |
| 5366 | return main.FALSE |
| 5367 | except pexpect.EOF: |
| 5368 | main.log.error( self.name + ": EOF exception found" ) |
| 5369 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5370 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5371 | except Exception: |
| 5372 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5373 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5374 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5375 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5376 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5377 | Description: |
| 5378 | Bring link down or up in the null-provider. |
| 5379 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5380 | begin - (string) One end of a device or switch. |
| 5381 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5382 | returns: |
| 5383 | main.TRUE if no exceptions were thrown and no Errors are |
| 5384 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5385 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5386 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5387 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5388 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5389 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5390 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5391 | if "Error" in response or "Failure" in response: |
| 5392 | main.log.error( response ) |
| 5393 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5394 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5395 | except AssertionError: |
| 5396 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5397 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5398 | except TypeError: |
| 5399 | main.log.exception( self.name + ": Object not as expected" ) |
| 5400 | return main.FALSE |
| 5401 | except pexpect.EOF: |
| 5402 | main.log.error( self.name + ": EOF exception found" ) |
| 5403 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5404 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5405 | except Exception: |
| 5406 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5407 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5408 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5409 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5410 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5411 | Description: |
| 5412 | Changes the state of port in an OF switch by means of the |
| 5413 | PORTSTATUS OF messages. |
| 5414 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5415 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5416 | port - (string) target port in the device. Ex: '2' |
| 5417 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5418 | returns: |
| 5419 | main.TRUE if no exceptions were thrown and no Errors are |
| 5420 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5421 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5422 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5423 | state = state.lower() |
| 5424 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5425 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5426 | response = self.sendline( cmd, showResponse=True ) |
| 5427 | assert response is not None, "Error in sendline" |
| 5428 | assert "Command not found:" not in response, response |
| 5429 | if "Error" in response or "Failure" in response: |
| 5430 | main.log.error( response ) |
| 5431 | return main.FALSE |
| 5432 | return main.TRUE |
| 5433 | except AssertionError: |
| 5434 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5435 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5436 | except TypeError: |
| 5437 | main.log.exception( self.name + ": Object not as expected" ) |
| 5438 | return main.FALSE |
| 5439 | except pexpect.EOF: |
| 5440 | main.log.error( self.name + ": EOF exception found" ) |
| 5441 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5442 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5443 | except Exception: |
| 5444 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5445 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5446 | |
| 5447 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5448 | """ |
| 5449 | Set the logging level to lvl for a specific app |
| 5450 | returns main.TRUE on success |
| 5451 | returns main.FALSE if Error occurred |
| 5452 | if noExit is True, TestON will not exit, but clean up |
| 5453 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5454 | Level defaults to INFO |
| 5455 | """ |
| 5456 | try: |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 5457 | handle = self.sendline( "log:set %s %s" % ( level, app ) ) |
| 5458 | assert handle is not None, "Error in sendline" |
| 5459 | assert "Command not found:" not in handle, handle |
| 5460 | if re.search( "Error", handle ): |
| 5461 | main.log.error( self.name + ": Error in setting log level" ) |
| 5462 | main.log.error( handle ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5463 | return main.FALSE |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 5464 | else: |
| 5465 | return main.TRUE |
| 5466 | except AssertionError: |
| 5467 | main.log.exception( "" ) |
| 5468 | return None |
| 5469 | except TypeError: |
| 5470 | main.log.exception( self.name + ": Object not as expected" ) |
| 5471 | return None |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5472 | except pexpect.EOF: |
| 5473 | main.log.error( self.name + ": EOF exception found" ) |
| 5474 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5475 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5476 | except Exception: |
| 5477 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5478 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5479 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 5480 | def logList( self, saveValues=True ): |
| 5481 | """ |
| 5482 | Gets the current log levels and optionally saves them |
| 5483 | returns a dict of the log levels or |
| 5484 | returns main.FALSE if Error occurred |
| 5485 | """ |
| 5486 | try: |
| 5487 | self.handle.sendline( "log:list" ) |
| 5488 | self.handle.expect( self.karafPrompt ) |
| 5489 | |
| 5490 | response = self.handle.before |
| 5491 | logLevels = {} |
| 5492 | for line in response.splitlines(): |
| 5493 | parsed = line.split('│') |
| 5494 | logger = parsed[0].strip() |
| 5495 | if len( parsed ) != 2 or 'Level' in parsed[1] or logger[0] == '─': |
| 5496 | continue |
| 5497 | level = parsed[1].strip() |
| 5498 | logLevels[ logger ] = level |
| 5499 | if saveValues: |
| 5500 | self.logLevels = logLevels |
| 5501 | return logLevels |
| 5502 | except pexpect.TIMEOUT: |
| 5503 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 5504 | main.cleanAndExit() |
| 5505 | except pexpect.EOF: |
| 5506 | main.log.error( self.name + ": EOF exception found" ) |
| 5507 | main.log.error( self.name + ": " + self.handle.before ) |
| 5508 | main.cleanAndExit() |
| 5509 | except Exception: |
| 5510 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5511 | main.cleanAndExit() |
| 5512 | |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5513 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5514 | """ |
| 5515 | Return a dictionary which describes the latest network topology data as a |
| 5516 | graph. |
| 5517 | An example of the dictionary: |
| 5518 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5519 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5520 | Each vertex should at least have an 'edges' attribute which describes the |
| 5521 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5522 | 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] | 5523 | list of attributes. |
| 5524 | An example of the edges dictionary: |
| 5525 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5526 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5527 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5528 | in topology data. |
| 5529 | """ |
| 5530 | graphDict = {} |
| 5531 | try: |
| 5532 | links = self.links() |
| 5533 | links = json.loads( links ) |
| 5534 | devices = self.devices() |
| 5535 | devices = json.loads( devices ) |
| 5536 | idToDevice = {} |
| 5537 | for device in devices: |
| 5538 | idToDevice[ device[ 'id' ] ] = device |
| 5539 | if includeHost: |
| 5540 | hosts = self.hosts() |
| 5541 | # FIXME: support 'includeHost' argument |
| 5542 | for link in links: |
| 5543 | nodeA = link[ 'src' ][ 'device' ] |
| 5544 | nodeB = link[ 'dst' ][ 'device' ] |
| 5545 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5546 | if nodeA not in graphDict.keys(): |
| 5547 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5548 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5549 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5550 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5551 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5552 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5553 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5554 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5555 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5556 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5557 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5558 | else: |
| 5559 | # Assert nodeB is not connected to any current links of nodeA |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 5560 | # assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 5561 | pass |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5562 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5563 | 'type': link[ 'type' ], |
| 5564 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5565 | return graphDict |
| 5566 | except ( TypeError, ValueError ): |
| 5567 | main.log.exception( self.name + ": Object not as expected" ) |
| 5568 | return None |
| 5569 | except KeyError: |
| 5570 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5571 | return None |
| 5572 | except AssertionError: |
| 5573 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5574 | return None |
| 5575 | except pexpect.EOF: |
| 5576 | main.log.error( self.name + ": EOF exception found" ) |
| 5577 | main.log.error( self.name + ": " + self.handle.before ) |
| 5578 | return None |
| 5579 | except Exception: |
| 5580 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5581 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5582 | |
| 5583 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5584 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5585 | Send command to check intent-perf summary |
| 5586 | Returns: dictionary for intent-perf summary |
| 5587 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5588 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5589 | cmd = "intent-perf -s" |
| 5590 | respDic = {} |
| 5591 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5592 | assert resp is not None, "Error in sendline" |
| 5593 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5594 | try: |
| 5595 | # Generate the dictionary to return |
| 5596 | for l in resp.split( "\n" ): |
| 5597 | # Delete any white space in line |
| 5598 | temp = re.sub( r'\s+', '', l ) |
| 5599 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5600 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5601 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5602 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5603 | main.log.exception( self.name + ": Object not as expected" ) |
| 5604 | return None |
| 5605 | except KeyError: |
| 5606 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5607 | return None |
| 5608 | except AssertionError: |
| 5609 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5610 | return None |
| 5611 | except pexpect.EOF: |
| 5612 | main.log.error( self.name + ": EOF exception found" ) |
| 5613 | main.log.error( self.name + ": " + self.handle.before ) |
| 5614 | return None |
| 5615 | except Exception: |
| 5616 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5617 | return None |
| 5618 | return respDic |
| 5619 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5620 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5621 | """ |
| 5622 | Searches the latest ONOS log file for the given search term and |
| 5623 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5624 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5625 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5626 | searchTerm: |
| 5627 | The string to grep from the ONOS log. |
| 5628 | startLine: |
| 5629 | The term that decides which line is the start to search the searchTerm in |
| 5630 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5631 | logNum: |
| 5632 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5633 | information.Because of this, search mutiply logs is necessary to capture |
| 5634 | the right result. logNum is the number of karaf logs that we need to search |
| 5635 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5636 | mode: |
| 5637 | all: return all the strings that contain the search term |
| 5638 | last: return the last string that contains the search term |
| 5639 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5640 | num: return the number of times that the searchTerm appears in the log |
| 5641 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5642 | """ |
| 5643 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5644 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5645 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5646 | logPath = '/opt/onos/log/karaf.log.' |
| 5647 | logPaths = '/opt/onos/log/karaf.log' |
| 5648 | for i in range( 1, logNum ): |
| 5649 | logPaths = logPath + str( i ) + " " + logPaths |
| 5650 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5651 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5652 | # 100000000 is just a extreme large number to make sure this function can |
| 5653 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5654 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5655 | if mode == 'all': |
| 5656 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5657 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5658 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5659 | elif mode == 'first': |
| 5660 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5661 | elif mode == 'num': |
You Wang | d91a70f | 2019-01-03 15:28:10 -0800 | [diff] [blame] | 5662 | cmd = cmd + " | grep \'" + searchTerm + "\' | wc -l" |
| 5663 | num = self.lineCount( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5664 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5665 | elif mode == 'total': |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 5666 | totalLines = self.lineCount( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5667 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5668 | else: |
| 5669 | main.log.error( self.name + " unsupported mode" ) |
| 5670 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5671 | before = self.sendline( cmd ) |
| 5672 | before = before.splitlines() |
| 5673 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5674 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5675 | return returnLines |
| 5676 | except AssertionError: |
| 5677 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5678 | return None |
| 5679 | except pexpect.EOF: |
| 5680 | main.log.error( self.name + ": EOF exception found" ) |
| 5681 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5682 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5683 | except pexpect.TIMEOUT: |
| 5684 | main.log.error( self.name + ": TIMEOUT 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() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [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 vplsShow( self, jsonFormat=True ): |
| 5692 | """ |
| 5693 | Description: Returns result of onos:vpls show, which should list the |
| 5694 | configured VPLS networks and the assigned interfaces. |
| 5695 | Optional: |
| 5696 | * jsonFormat: enable json formatting of output |
| 5697 | Returns: |
| 5698 | The output of the command or None on error. |
| 5699 | """ |
| 5700 | try: |
| 5701 | cmdStr = "vpls show" |
| 5702 | if jsonFormat: |
| 5703 | raise NotImplementedError |
| 5704 | cmdStr += " -j" |
| 5705 | handle = self.sendline( cmdStr ) |
| 5706 | assert handle is not None, "Error in sendline" |
| 5707 | assert "Command not found:" not in handle, handle |
| 5708 | return handle |
| 5709 | except AssertionError: |
| 5710 | main.log.exception( "" ) |
| 5711 | return None |
| 5712 | except TypeError: |
| 5713 | main.log.exception( self.name + ": Object not as expected" ) |
| 5714 | return None |
| 5715 | except pexpect.EOF: |
| 5716 | main.log.error( self.name + ": EOF exception found" ) |
| 5717 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5718 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5719 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5720 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5721 | return None |
| 5722 | except Exception: |
| 5723 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5724 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5725 | |
| 5726 | def parseVplsShow( self ): |
| 5727 | """ |
| 5728 | Parse the cli output of 'vpls show' into json output. This is required |
| 5729 | as there is currently no json output available. |
| 5730 | """ |
| 5731 | try: |
| 5732 | output = [] |
| 5733 | raw = self.vplsShow( jsonFormat=False ) |
| 5734 | namePat = "VPLS name: (?P<name>\w+)" |
| 5735 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5736 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5737 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5738 | mIter = re.finditer( pattern, raw ) |
| 5739 | for match in mIter: |
| 5740 | item = {} |
| 5741 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5742 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5743 | if ifaces == [ "" ]: |
| 5744 | ifaces = [] |
| 5745 | item[ 'interfaces' ] = ifaces |
| 5746 | encap = match.group( 'encap' ) |
| 5747 | if encap != 'NONE': |
| 5748 | item[ 'encapsulation' ] = encap.lower() |
| 5749 | output.append( item ) |
| 5750 | return output |
| 5751 | except Exception: |
| 5752 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5753 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5754 | |
| 5755 | def vplsList( self, jsonFormat=True ): |
| 5756 | """ |
| 5757 | Description: Returns result of onos:vpls list, which should list the |
| 5758 | configured VPLS networks. |
| 5759 | Optional: |
| 5760 | * jsonFormat: enable json formatting of output |
| 5761 | """ |
| 5762 | try: |
| 5763 | cmdStr = "vpls list" |
| 5764 | if jsonFormat: |
| 5765 | raise NotImplementedError |
| 5766 | cmdStr += " -j" |
| 5767 | handle = self.sendline( cmdStr ) |
| 5768 | assert handle is not None, "Error in sendline" |
| 5769 | assert "Command not found:" not in handle, handle |
| 5770 | return handle |
| 5771 | except AssertionError: |
| 5772 | main.log.exception( "" ) |
| 5773 | return None |
| 5774 | except TypeError: |
| 5775 | main.log.exception( self.name + ": Object not as expected" ) |
| 5776 | return None |
| 5777 | except pexpect.EOF: |
| 5778 | main.log.error( self.name + ": EOF exception found" ) |
| 5779 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5780 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5781 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5782 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5783 | return None |
| 5784 | except Exception: |
| 5785 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5786 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5787 | |
| 5788 | def vplsCreate( self, network ): |
| 5789 | """ |
| 5790 | CLI command to create a new VPLS network. |
| 5791 | Required arguments: |
| 5792 | network - String name of the network to create. |
| 5793 | returns: |
| 5794 | main.TRUE on success and main.FALSE on failure |
| 5795 | """ |
| 5796 | try: |
| 5797 | network = str( network ) |
| 5798 | cmdStr = "vpls create " |
| 5799 | cmdStr += network |
| 5800 | output = self.sendline( cmdStr ) |
| 5801 | assert output is not None, "Error in sendline" |
| 5802 | assert "Command not found:" not in output, output |
| 5803 | assert "Error executing command" not in output, output |
| 5804 | assert "VPLS already exists:" not in output, output |
| 5805 | return main.TRUE |
| 5806 | except AssertionError: |
| 5807 | main.log.exception( "" ) |
| 5808 | return main.FALSE |
| 5809 | except TypeError: |
| 5810 | main.log.exception( self.name + ": Object not as expected" ) |
| 5811 | return main.FALSE |
| 5812 | except pexpect.EOF: |
| 5813 | main.log.error( self.name + ": EOF exception found" ) |
| 5814 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5815 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5816 | except Exception: |
| 5817 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5818 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5819 | |
| 5820 | def vplsDelete( self, network ): |
| 5821 | """ |
| 5822 | CLI command to delete a VPLS network. |
| 5823 | Required arguments: |
| 5824 | network - Name of the network to delete. |
| 5825 | returns: |
| 5826 | main.TRUE on success and main.FALSE on failure |
| 5827 | """ |
| 5828 | try: |
| 5829 | network = str( network ) |
| 5830 | cmdStr = "vpls delete " |
| 5831 | cmdStr += network |
| 5832 | output = self.sendline( cmdStr ) |
| 5833 | assert output is not None, "Error in sendline" |
| 5834 | assert "Command not found:" not in output, output |
| 5835 | assert "Error executing command" not in output, output |
| 5836 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5837 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5838 | return main.TRUE |
| 5839 | except AssertionError: |
| 5840 | main.log.exception( "" ) |
| 5841 | return main.FALSE |
| 5842 | except TypeError: |
| 5843 | main.log.exception( self.name + ": Object not as expected" ) |
| 5844 | return main.FALSE |
| 5845 | except pexpect.EOF: |
| 5846 | main.log.error( self.name + ": EOF exception found" ) |
| 5847 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5848 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5849 | except Exception: |
| 5850 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5851 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5852 | |
| 5853 | def vplsAddIface( self, network, iface ): |
| 5854 | """ |
| 5855 | CLI command to add an interface to a VPLS network. |
| 5856 | Required arguments: |
| 5857 | network - Name of the network to add the interface to. |
| 5858 | iface - The ONOS name for an interface. |
| 5859 | returns: |
| 5860 | main.TRUE on success and main.FALSE on failure |
| 5861 | """ |
| 5862 | try: |
| 5863 | network = str( network ) |
| 5864 | iface = str( iface ) |
| 5865 | cmdStr = "vpls add-if " |
| 5866 | cmdStr += network + " " + iface |
| 5867 | output = self.sendline( cmdStr ) |
| 5868 | assert output is not None, "Error in sendline" |
| 5869 | assert "Command not found:" not in output, output |
| 5870 | assert "Error executing command" not in output, output |
| 5871 | assert "already associated to network" not in output, output |
| 5872 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5873 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5874 | return main.TRUE |
| 5875 | except AssertionError: |
| 5876 | main.log.exception( "" ) |
| 5877 | return main.FALSE |
| 5878 | except TypeError: |
| 5879 | main.log.exception( self.name + ": Object not as expected" ) |
| 5880 | return main.FALSE |
| 5881 | except pexpect.EOF: |
| 5882 | main.log.error( self.name + ": EOF exception found" ) |
| 5883 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5884 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5885 | except Exception: |
| 5886 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5887 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5888 | |
| 5889 | def vplsRemIface( self, network, iface ): |
| 5890 | """ |
| 5891 | CLI command to remove an interface from a VPLS network. |
| 5892 | Required arguments: |
| 5893 | network - Name of the network to remove the interface from. |
| 5894 | iface - Name of the interface to remove. |
| 5895 | returns: |
| 5896 | main.TRUE on success and main.FALSE on failure |
| 5897 | """ |
| 5898 | try: |
| 5899 | iface = str( iface ) |
| 5900 | cmdStr = "vpls rem-if " |
| 5901 | cmdStr += network + " " + iface |
| 5902 | output = self.sendline( cmdStr ) |
| 5903 | assert output is not None, "Error in sendline" |
| 5904 | assert "Command not found:" not in output, output |
| 5905 | assert "Error executing command" not in output, output |
| 5906 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5907 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5908 | return main.TRUE |
| 5909 | except AssertionError: |
| 5910 | main.log.exception( "" ) |
| 5911 | return main.FALSE |
| 5912 | except TypeError: |
| 5913 | main.log.exception( self.name + ": Object not as expected" ) |
| 5914 | return main.FALSE |
| 5915 | except pexpect.EOF: |
| 5916 | main.log.error( self.name + ": EOF exception found" ) |
| 5917 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5918 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5919 | except Exception: |
| 5920 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5921 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5922 | |
| 5923 | def vplsClean( self ): |
| 5924 | """ |
| 5925 | Description: Clears the VPLS app configuration. |
| 5926 | Returns: main.TRUE on success and main.FALSE on failure |
| 5927 | """ |
| 5928 | try: |
| 5929 | cmdStr = "vpls clean" |
| 5930 | handle = self.sendline( cmdStr ) |
| 5931 | assert handle is not None, "Error in sendline" |
| 5932 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5933 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5934 | return handle |
| 5935 | except AssertionError: |
| 5936 | main.log.exception( "" ) |
| 5937 | return main.FALSE |
| 5938 | except TypeError: |
| 5939 | main.log.exception( self.name + ": Object not as expected" ) |
| 5940 | return main.FALSE |
| 5941 | except pexpect.EOF: |
| 5942 | main.log.error( self.name + ": EOF exception found" ) |
| 5943 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5944 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5945 | except Exception: |
| 5946 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5947 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5948 | |
| 5949 | def vplsSetEncap( self, network, encapType ): |
| 5950 | """ |
| 5951 | CLI command to add an interface to a VPLS network. |
| 5952 | Required arguments: |
| 5953 | network - Name of the network to create. |
| 5954 | encapType - Type of encapsulation. |
| 5955 | returns: |
| 5956 | main.TRUE on success and main.FALSE on failure |
| 5957 | """ |
| 5958 | try: |
| 5959 | network = str( network ) |
| 5960 | encapType = str( encapType ).upper() |
| 5961 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5962 | cmdStr = "vpls set-encap " |
| 5963 | cmdStr += network + " " + encapType |
| 5964 | output = self.sendline( cmdStr ) |
| 5965 | assert output is not None, "Error in sendline" |
| 5966 | assert "Command not found:" not in output, output |
| 5967 | assert "Error executing command" not in output, output |
| 5968 | assert "already associated to network" not in output, output |
| 5969 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5970 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5971 | return main.TRUE |
| 5972 | except AssertionError: |
| 5973 | main.log.exception( "" ) |
| 5974 | return main.FALSE |
| 5975 | except TypeError: |
| 5976 | main.log.exception( self.name + ": Object not as expected" ) |
| 5977 | return main.FALSE |
| 5978 | except pexpect.EOF: |
| 5979 | main.log.error( self.name + ": EOF exception found" ) |
| 5980 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5981 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5982 | except Exception: |
| 5983 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5984 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5985 | |
| 5986 | def interfaces( self, jsonFormat=True ): |
| 5987 | """ |
| 5988 | Description: Returns result of interfaces command. |
| 5989 | Optional: |
| 5990 | * jsonFormat: enable json formatting of output |
| 5991 | Returns: |
| 5992 | The output of the command or None on error. |
| 5993 | """ |
| 5994 | try: |
| 5995 | cmdStr = "interfaces" |
| 5996 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5997 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5998 | cmdStr += " -j" |
| 5999 | handle = self.sendline( cmdStr ) |
| 6000 | assert handle is not None, "Error in sendline" |
| 6001 | assert "Command not found:" not in handle, handle |
| 6002 | return handle |
| 6003 | except AssertionError: |
| 6004 | main.log.exception( "" ) |
| 6005 | return None |
| 6006 | except TypeError: |
| 6007 | main.log.exception( self.name + ": Object not as expected" ) |
| 6008 | return None |
| 6009 | except pexpect.EOF: |
| 6010 | main.log.error( self.name + ": EOF exception found" ) |
| 6011 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6012 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 6013 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6014 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 6015 | return None |
| 6016 | except Exception: |
| 6017 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6018 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 6019 | |
| 6020 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 6021 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 6022 | Get the timestamp of searchTerm from karaf log. |
| 6023 | |
| 6024 | Arguments: |
| 6025 | splitTerm_before and splitTerm_after: |
| 6026 | |
| 6027 | The terms that split the string that contains the timeStamp of |
| 6028 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 6029 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 6030 | and the splitTerm_after is "x" |
| 6031 | |
| 6032 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6033 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 6034 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 6035 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6036 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 6037 | return main.ERROR |
| 6038 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6039 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 6040 | main.log.warn( "Captured timestamp string is empty" ) |
| 6041 | return main.ERROR |
| 6042 | lines = lines[ 0 ] |
| 6043 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6044 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 6045 | # get the target value |
| 6046 | line = lines.split( splitTerm_before ) |
| 6047 | key = line[ 1 ].split( splitTerm_after ) |
| 6048 | return int( key[ 0 ] ) |
| 6049 | except IndexError: |
| 6050 | main.log.warn( "Index Error!" ) |
| 6051 | return main.ERROR |
| 6052 | except AssertionError: |
| 6053 | main.log.warn( "Search Term Not Found " ) |
| 6054 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6055 | |
| 6056 | def workQueueAdd( self, queueName, value ): |
| 6057 | """ |
| 6058 | CLI command to add a string to the specified Work Queue. |
| 6059 | This function uses the distributed primitives test app, which |
| 6060 | gives some cli access to distributed primitives for testing |
| 6061 | purposes only. |
| 6062 | |
| 6063 | Required arguments: |
| 6064 | queueName - The name of the queue to add to |
| 6065 | value - The value to add to the queue |
| 6066 | returns: |
| 6067 | main.TRUE on success, main.FALSE on failure and |
| 6068 | main.ERROR on error. |
| 6069 | """ |
| 6070 | try: |
| 6071 | queueName = str( queueName ) |
| 6072 | value = str( value ) |
| 6073 | prefix = "work-queue-test" |
| 6074 | operation = "add" |
| 6075 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 6076 | output = self.distPrimitivesSend( cmdStr ) |
| 6077 | if "Invalid operation name" in output: |
| 6078 | main.log.warn( output ) |
| 6079 | return main.ERROR |
| 6080 | elif "Done" in output: |
| 6081 | return main.TRUE |
| 6082 | except TypeError: |
| 6083 | main.log.exception( self.name + ": Object not as expected" ) |
| 6084 | return main.ERROR |
| 6085 | except Exception: |
| 6086 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6087 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6088 | |
| 6089 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 6090 | """ |
| 6091 | CLI command to add two strings to the specified Work Queue. |
| 6092 | This function uses the distributed primitives test app, which |
| 6093 | gives some cli access to distributed primitives for testing |
| 6094 | purposes only. |
| 6095 | |
| 6096 | Required arguments: |
| 6097 | queueName - The name of the queue to add to |
| 6098 | value1 - The first value to add to the queue |
| 6099 | value2 - The second value to add to the queue |
| 6100 | returns: |
| 6101 | main.TRUE on success, main.FALSE on failure and |
| 6102 | main.ERROR on error. |
| 6103 | """ |
| 6104 | try: |
| 6105 | queueName = str( queueName ) |
| 6106 | value1 = str( value1 ) |
| 6107 | value2 = str( value2 ) |
| 6108 | prefix = "work-queue-test" |
| 6109 | operation = "addMultiple" |
| 6110 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 6111 | output = self.distPrimitivesSend( cmdStr ) |
| 6112 | if "Invalid operation name" in output: |
| 6113 | main.log.warn( output ) |
| 6114 | return main.ERROR |
| 6115 | elif "Done" in output: |
| 6116 | return main.TRUE |
| 6117 | except TypeError: |
| 6118 | main.log.exception( self.name + ": Object not as expected" ) |
| 6119 | return main.ERROR |
| 6120 | except Exception: |
| 6121 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6122 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6123 | |
| 6124 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 6125 | """ |
| 6126 | CLI command to take a value from the specified Work Queue and compelte it. |
| 6127 | This function uses the distributed primitives test app, which |
| 6128 | gives some cli access to distributed primitives for testing |
| 6129 | purposes only. |
| 6130 | |
| 6131 | Required arguments: |
| 6132 | queueName - The name of the queue to add to |
| 6133 | number - The number of items to take and complete |
| 6134 | returns: |
| 6135 | main.TRUE on success, main.FALSE on failure and |
| 6136 | main.ERROR on error. |
| 6137 | """ |
| 6138 | try: |
| 6139 | queueName = str( queueName ) |
| 6140 | number = str( int( number ) ) |
| 6141 | prefix = "work-queue-test" |
| 6142 | operation = "takeAndComplete" |
| 6143 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 6144 | output = self.distPrimitivesSend( cmdStr ) |
| 6145 | if "Invalid operation name" in output: |
| 6146 | main.log.warn( output ) |
| 6147 | return main.ERROR |
| 6148 | elif "Done" in output: |
| 6149 | return main.TRUE |
| 6150 | except TypeError: |
| 6151 | main.log.exception( self.name + ": Object not as expected" ) |
| 6152 | return main.ERROR |
| 6153 | except Exception: |
| 6154 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6155 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6156 | |
| 6157 | def workQueueDestroy( self, queueName ): |
| 6158 | """ |
| 6159 | CLI command to destroy the specified Work Queue. |
| 6160 | This function uses the distributed primitives test app, which |
| 6161 | gives some cli access to distributed primitives for testing |
| 6162 | purposes only. |
| 6163 | |
| 6164 | Required arguments: |
| 6165 | queueName - The name of the queue to add to |
| 6166 | returns: |
| 6167 | main.TRUE on success, main.FALSE on failure and |
| 6168 | main.ERROR on error. |
| 6169 | """ |
| 6170 | try: |
| 6171 | queueName = str( queueName ) |
| 6172 | prefix = "work-queue-test" |
| 6173 | operation = "destroy" |
| 6174 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6175 | output = self.distPrimitivesSend( cmdStr ) |
| 6176 | if "Invalid operation name" in output: |
| 6177 | main.log.warn( output ) |
| 6178 | return main.ERROR |
| 6179 | return main.TRUE |
| 6180 | except TypeError: |
| 6181 | main.log.exception( self.name + ": Object not as expected" ) |
| 6182 | return main.ERROR |
| 6183 | except Exception: |
| 6184 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6185 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6186 | |
| 6187 | def workQueueTotalPending( self, queueName ): |
| 6188 | """ |
| 6189 | CLI command to get the Total Pending items of the specified Work Queue. |
| 6190 | This function uses the distributed primitives test app, which |
| 6191 | gives some cli access to distributed primitives for testing |
| 6192 | purposes only. |
| 6193 | |
| 6194 | Required arguments: |
| 6195 | queueName - The name of the queue to add to |
| 6196 | returns: |
| 6197 | The number of Pending items in the specified work queue or |
| 6198 | None on error |
| 6199 | """ |
| 6200 | try: |
| 6201 | queueName = str( queueName ) |
| 6202 | prefix = "work-queue-test" |
| 6203 | operation = "totalPending" |
| 6204 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6205 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6206 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6207 | output = output.split( "\r\n" )[ -1 ] |
| 6208 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6209 | pattern = r'\d+' |
| 6210 | if "Invalid operation name" in output: |
| 6211 | main.log.warn( output ) |
| 6212 | return None |
| 6213 | else: |
| 6214 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6215 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6216 | except ( AttributeError, TypeError ): |
| 6217 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6218 | return None |
| 6219 | except Exception: |
| 6220 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6221 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6222 | |
| 6223 | def workQueueTotalCompleted( self, queueName ): |
| 6224 | """ |
| 6225 | CLI command to get the Total Completed items of the specified Work Queue. |
| 6226 | This function uses the distributed primitives test app, which |
| 6227 | gives some cli access to distributed primitives for testing |
| 6228 | purposes only. |
| 6229 | |
| 6230 | Required arguments: |
| 6231 | queueName - The name of the queue to add to |
| 6232 | returns: |
| 6233 | The number of complete items in the specified work queue or |
| 6234 | None on error |
| 6235 | """ |
| 6236 | try: |
| 6237 | queueName = str( queueName ) |
| 6238 | prefix = "work-queue-test" |
| 6239 | operation = "totalCompleted" |
| 6240 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6241 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6242 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6243 | output = output.split( "\r\n" )[ -1 ] |
| 6244 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6245 | pattern = r'\d+' |
| 6246 | if "Invalid operation name" in output: |
| 6247 | main.log.warn( output ) |
| 6248 | return None |
| 6249 | else: |
| 6250 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6251 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6252 | except ( AttributeError, TypeError ): |
| 6253 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6254 | return None |
| 6255 | except Exception: |
| 6256 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6257 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6258 | |
| 6259 | def workQueueTotalInProgress( self, queueName ): |
| 6260 | """ |
| 6261 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 6262 | This function uses the distributed primitives test app, which |
| 6263 | gives some cli access to distributed primitives for testing |
| 6264 | purposes only. |
| 6265 | |
| 6266 | Required arguments: |
| 6267 | queueName - The name of the queue to add to |
| 6268 | returns: |
| 6269 | The number of In Progress items in the specified work queue or |
| 6270 | None on error |
| 6271 | """ |
| 6272 | try: |
| 6273 | queueName = str( queueName ) |
| 6274 | prefix = "work-queue-test" |
| 6275 | operation = "totalInProgress" |
| 6276 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6277 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6278 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6279 | output = output.split( "\r\n" )[ -1 ] |
| 6280 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6281 | pattern = r'\d+' |
| 6282 | if "Invalid operation name" in output: |
| 6283 | main.log.warn( output ) |
| 6284 | return None |
| 6285 | else: |
| 6286 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6287 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6288 | except ( AttributeError, TypeError ): |
| 6289 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6290 | return None |
| 6291 | except Exception: |
| 6292 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6293 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 6294 | |
| 6295 | def events( self, args='-a' ): |
| 6296 | """ |
| 6297 | Description: Returns events -a command output |
| 6298 | Optional: |
| 6299 | add other arguments |
| 6300 | """ |
| 6301 | try: |
| 6302 | cmdStr = "events" |
| 6303 | if args: |
| 6304 | cmdStr += " " + args |
| 6305 | handle = self.sendline( cmdStr ) |
| 6306 | assert handle is not None, "Error in sendline" |
| 6307 | assert "Command not found:" not in handle, handle |
| 6308 | return handle |
| 6309 | except AssertionError: |
| 6310 | main.log.exception( "" ) |
| 6311 | return None |
| 6312 | except TypeError: |
| 6313 | main.log.exception( self.name + ": Object not as expected" ) |
| 6314 | return None |
| 6315 | except pexpect.EOF: |
| 6316 | main.log.error( self.name + ": EOF exception found" ) |
| 6317 | main.log.error( self.name + ": " + self.handle.before ) |
| 6318 | main.cleanAndExit() |
| 6319 | except Exception: |
| 6320 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6321 | main.cleanAndExit() |
| 6322 | |
| 6323 | def getMaster( self, deviceID ): |
| 6324 | """ |
| 6325 | Description: Obtains current master using "roles" command for a specific deviceID |
| 6326 | """ |
| 6327 | try: |
| 6328 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 6329 | except AssertionError: |
| 6330 | main.log.exception( "" ) |
| 6331 | return None |
| 6332 | except TypeError: |
| 6333 | main.log.exception( self.name + ": Object not as expected" ) |
| 6334 | return None |
| 6335 | except pexpect.EOF: |
| 6336 | main.log.error( self.name + ": EOF exception found" ) |
| 6337 | main.log.error( self.name + ": " + self.handle.before ) |
| 6338 | main.cleanAndExit() |
| 6339 | except Exception: |
| 6340 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 6341 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 6342 | |
| 6343 | def issu( self ): |
| 6344 | """ |
| 6345 | Short summary of In-Service Software Upgrade status |
| 6346 | |
| 6347 | Returns the output of the cli command or None on Error |
| 6348 | """ |
| 6349 | try: |
| 6350 | cmdStr = "issu" |
| 6351 | handle = self.sendline( cmdStr ) |
| 6352 | assert handle is not None, "Error in sendline" |
| 6353 | assert "Command not found:" not in handle, handle |
| 6354 | assert "Unsupported command:" not in handle, handle |
| 6355 | return handle |
| 6356 | except AssertionError: |
| 6357 | main.log.exception( "" ) |
| 6358 | return None |
| 6359 | except TypeError: |
| 6360 | main.log.exception( self.name + ": Object not as expected" ) |
| 6361 | return None |
| 6362 | except pexpect.EOF: |
| 6363 | main.log.error( self.name + ": EOF exception found" ) |
| 6364 | main.log.error( self.name + ": " + self.handle.before ) |
| 6365 | main.cleanAndExit() |
| 6366 | except Exception: |
| 6367 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6368 | main.cleanAndExit() |
| 6369 | |
| 6370 | def issuInit( self ): |
| 6371 | """ |
| 6372 | Initiates an In-Service Software Upgrade |
| 6373 | |
| 6374 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6375 | """ |
| 6376 | try: |
| 6377 | cmdStr = "issu init" |
| 6378 | handle = self.sendline( cmdStr ) |
| 6379 | assert handle is not None, "Error in sendline" |
| 6380 | assert "Command not found:" not in handle, handle |
| 6381 | assert "Unsupported command:" not in handle, handle |
| 6382 | if "Initialized" in handle: |
| 6383 | return main.TRUE |
| 6384 | else: |
| 6385 | return main.FALSE |
| 6386 | except AssertionError: |
| 6387 | main.log.exception( "" ) |
| 6388 | return main.ERROR |
| 6389 | except TypeError: |
| 6390 | main.log.exception( self.name + ": Object not as expected" ) |
| 6391 | return main.ERROR |
| 6392 | except pexpect.EOF: |
| 6393 | main.log.error( self.name + ": EOF exception found" ) |
| 6394 | main.log.error( self.name + ": " + self.handle.before ) |
| 6395 | main.cleanAndExit() |
| 6396 | except Exception: |
| 6397 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6398 | main.cleanAndExit() |
| 6399 | |
| 6400 | def issuUpgrade( self ): |
| 6401 | """ |
| 6402 | Transitions stores to upgraded nodes |
| 6403 | |
| 6404 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6405 | """ |
| 6406 | try: |
| 6407 | cmdStr = "issu upgrade" |
| 6408 | handle = self.sendline( cmdStr ) |
| 6409 | assert handle is not None, "Error in sendline" |
| 6410 | assert "Command not found:" not in handle, handle |
| 6411 | assert "Unsupported command:" not in handle, handle |
| 6412 | if "Upgraded" in handle: |
| 6413 | return main.TRUE |
| 6414 | else: |
| 6415 | return main.FALSE |
| 6416 | except AssertionError: |
| 6417 | main.log.exception( "" ) |
| 6418 | return main.ERROR |
| 6419 | except TypeError: |
| 6420 | main.log.exception( self.name + ": Object not as expected" ) |
| 6421 | return main.ERROR |
| 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 issuCommit( self ): |
| 6431 | """ |
| 6432 | Finalizes an In-Service Software Upgrade |
| 6433 | |
| 6434 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6435 | """ |
| 6436 | try: |
| 6437 | cmdStr = "issu commit" |
| 6438 | handle = self.sendline( cmdStr ) |
| 6439 | assert handle is not None, "Error in sendline" |
| 6440 | assert "Command not found:" not in handle, handle |
| 6441 | assert "Unsupported command:" not in handle, handle |
| 6442 | # TODO: Check the version returned by this command |
| 6443 | if "Committed version" in handle: |
| 6444 | return main.TRUE |
| 6445 | else: |
| 6446 | return main.FALSE |
| 6447 | except AssertionError: |
| 6448 | main.log.exception( "" ) |
| 6449 | return main.ERROR |
| 6450 | except TypeError: |
| 6451 | main.log.exception( self.name + ": Object not as expected" ) |
| 6452 | return main.ERROR |
| 6453 | except pexpect.EOF: |
| 6454 | main.log.error( self.name + ": EOF exception found" ) |
| 6455 | main.log.error( self.name + ": " + self.handle.before ) |
| 6456 | main.cleanAndExit() |
| 6457 | except Exception: |
| 6458 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6459 | main.cleanAndExit() |
| 6460 | |
| 6461 | def issuRollback( self ): |
| 6462 | """ |
| 6463 | Rolls back an In-Service Software Upgrade |
| 6464 | |
| 6465 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6466 | """ |
| 6467 | try: |
| 6468 | cmdStr = "issu rollback" |
| 6469 | handle = self.sendline( cmdStr ) |
| 6470 | assert handle is not None, "Error in sendline" |
| 6471 | assert "Command not found:" not in handle, handle |
| 6472 | assert "Unsupported command:" not in handle, handle |
| 6473 | # TODO: Check the version returned by this command |
| 6474 | if "Rolled back to version" in handle: |
| 6475 | return main.TRUE |
| 6476 | else: |
| 6477 | return main.FALSE |
| 6478 | except AssertionError: |
| 6479 | main.log.exception( "" ) |
| 6480 | return main.ERROR |
| 6481 | except TypeError: |
| 6482 | main.log.exception( self.name + ": Object not as expected" ) |
| 6483 | return main.ERROR |
| 6484 | except pexpect.EOF: |
| 6485 | main.log.error( self.name + ": EOF exception found" ) |
| 6486 | main.log.error( self.name + ": " + self.handle.before ) |
| 6487 | main.cleanAndExit() |
| 6488 | except Exception: |
| 6489 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6490 | main.cleanAndExit() |
| 6491 | |
| 6492 | def issuReset( self ): |
| 6493 | """ |
| 6494 | Resets the In-Service Software Upgrade status after a rollback |
| 6495 | |
| 6496 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6497 | """ |
| 6498 | try: |
| 6499 | cmdStr = "issu reset" |
| 6500 | handle = self.sendline( cmdStr ) |
| 6501 | assert handle is not None, "Error in sendline" |
| 6502 | assert "Command not found:" not in handle, handle |
| 6503 | assert "Unsupported command:" not in handle, handle |
| 6504 | # TODO: Check the version returned by this command |
| 6505 | if "Reset version" in handle: |
| 6506 | return main.TRUE |
| 6507 | else: |
| 6508 | return main.FALSE |
| 6509 | except AssertionError: |
| 6510 | main.log.exception( "" ) |
| 6511 | return main.ERROR |
| 6512 | except TypeError: |
| 6513 | main.log.exception( self.name + ": Object not as expected" ) |
| 6514 | return main.ERROR |
| 6515 | except pexpect.EOF: |
| 6516 | main.log.error( self.name + ": EOF exception found" ) |
| 6517 | main.log.error( self.name + ": " + self.handle.before ) |
| 6518 | main.cleanAndExit() |
| 6519 | except Exception: |
| 6520 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6521 | main.cleanAndExit() |
| 6522 | |
| 6523 | def issuStatus( self ): |
| 6524 | """ |
| 6525 | Status of an In-Service Software Upgrade |
| 6526 | |
| 6527 | Returns the output of the cli command or None on Error |
| 6528 | """ |
| 6529 | try: |
| 6530 | cmdStr = "issu status" |
| 6531 | handle = self.sendline( cmdStr ) |
| 6532 | assert handle is not None, "Error in sendline" |
| 6533 | assert "Command not found:" not in handle, handle |
| 6534 | assert "Unsupported command:" not in handle, handle |
| 6535 | return handle |
| 6536 | except AssertionError: |
| 6537 | main.log.exception( "" ) |
| 6538 | return None |
| 6539 | except TypeError: |
| 6540 | main.log.exception( self.name + ": Object not as expected" ) |
| 6541 | return None |
| 6542 | except pexpect.EOF: |
| 6543 | main.log.error( self.name + ": EOF exception found" ) |
| 6544 | main.log.error( self.name + ": " + self.handle.before ) |
| 6545 | main.cleanAndExit() |
| 6546 | except Exception: |
| 6547 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6548 | main.cleanAndExit() |
| 6549 | |
| 6550 | def issuVersion( self ): |
| 6551 | """ |
| 6552 | Get the version of an In-Service Software Upgrade |
| 6553 | |
| 6554 | Returns the output of the cli command or None on Error |
| 6555 | """ |
| 6556 | try: |
| 6557 | cmdStr = "issu version" |
| 6558 | handle = self.sendline( cmdStr ) |
| 6559 | assert handle is not None, "Error in sendline" |
| 6560 | assert "Command not found:" not in handle, handle |
| 6561 | assert "Unsupported command:" not in handle, handle |
| 6562 | return handle |
| 6563 | except AssertionError: |
| 6564 | main.log.exception( "" ) |
| 6565 | return None |
| 6566 | except TypeError: |
| 6567 | main.log.exception( self.name + ": Object not as expected" ) |
| 6568 | return None |
| 6569 | except pexpect.EOF: |
| 6570 | main.log.error( self.name + ": EOF exception found" ) |
| 6571 | main.log.error( self.name + ": " + self.handle.before ) |
| 6572 | main.cleanAndExit() |
| 6573 | except Exception: |
| 6574 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6575 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6576 | |
| 6577 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6578 | """ |
| 6579 | Create a multicast route by calling 'mcast-join' command |
| 6580 | sIP: source IP of the multicast route |
| 6581 | groupIP: group IP of the multicast route |
| 6582 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6583 | dPorts: a list of destination ports of the multicast route |
| 6584 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6585 | """ |
| 6586 | try: |
| 6587 | cmdStr = "mcast-join" |
| 6588 | cmdStr += " " + str( sIP ) |
| 6589 | cmdStr += " " + str( groupIP ) |
| 6590 | cmdStr += " " + str( sPort ) |
| 6591 | assert isinstance( dPorts, list ) |
| 6592 | for dPort in dPorts: |
| 6593 | cmdStr += " " + str( dPort ) |
| 6594 | handle = self.sendline( cmdStr ) |
| 6595 | assert handle is not None, "Error in sendline" |
| 6596 | assert "Command not found:" not in handle, handle |
| 6597 | assert "Unsupported command:" not in handle, handle |
| 6598 | assert "Error executing command" not in handle, handle |
| 6599 | if "Added the mcast route" in handle: |
| 6600 | return main.TRUE |
| 6601 | else: |
| 6602 | return main.FALSE |
| 6603 | except AssertionError: |
| 6604 | main.log.exception( "" ) |
| 6605 | return None |
| 6606 | except TypeError: |
| 6607 | main.log.exception( self.name + ": Object not as expected" ) |
| 6608 | return None |
| 6609 | except pexpect.EOF: |
| 6610 | main.log.error( self.name + ": EOF exception found" ) |
| 6611 | main.log.error( self.name + ": " + self.handle.before ) |
| 6612 | main.cleanAndExit() |
| 6613 | except Exception: |
| 6614 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6615 | main.cleanAndExit() |
| 6616 | |
| 6617 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6618 | """ |
| 6619 | Delete a multicast route by calling 'mcast-delete' command |
| 6620 | sIP: source IP of the multicast route |
| 6621 | groupIP: group IP of the multicast route |
| 6622 | dPorts: a list of destination ports of the multicast route |
| 6623 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6624 | """ |
| 6625 | try: |
| 6626 | cmdStr = "mcast-delete" |
| 6627 | cmdStr += " " + str( sIP ) |
| 6628 | cmdStr += " " + str( groupIP ) |
| 6629 | assert isinstance( dPorts, list ) |
| 6630 | for dPort in dPorts: |
| 6631 | cmdStr += " " + str( dPort ) |
| 6632 | handle = self.sendline( cmdStr ) |
| 6633 | assert handle is not None, "Error in sendline" |
| 6634 | assert "Command not found:" not in handle, handle |
| 6635 | assert "Unsupported command:" not in handle, handle |
| 6636 | assert "Error executing command" not in handle, handle |
| 6637 | if "Updated the mcast route" in handle: |
| 6638 | return main.TRUE |
| 6639 | else: |
| 6640 | return main.FALSE |
| 6641 | except AssertionError: |
| 6642 | main.log.exception( "" ) |
| 6643 | return None |
| 6644 | except TypeError: |
| 6645 | main.log.exception( self.name + ": Object not as expected" ) |
| 6646 | return None |
| 6647 | except pexpect.EOF: |
| 6648 | main.log.error( self.name + ": EOF exception found" ) |
| 6649 | main.log.error( self.name + ": " + self.handle.before ) |
| 6650 | main.cleanAndExit() |
| 6651 | except Exception: |
| 6652 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6653 | main.cleanAndExit() |
| 6654 | |
| 6655 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6656 | """ |
| 6657 | Create a multicast route by calling 'mcast-host-join' command |
| 6658 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6659 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6660 | 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] | 6661 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6662 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6663 | """ |
| 6664 | try: |
| 6665 | cmdStr = "mcast-host-join" |
| 6666 | cmdStr += " -sAddr " + str( sAddr ) |
| 6667 | cmdStr += " -gAddr " + str( gAddr ) |
| 6668 | assert isinstance( srcs, list ) |
| 6669 | for src in srcs: |
| 6670 | cmdStr += " -srcs " + str( src ) |
| 6671 | assert isinstance( sinks, list ) |
| 6672 | for sink in sinks: |
| 6673 | cmdStr += " -sinks " + str( sink ) |
| 6674 | handle = self.sendline( cmdStr ) |
| 6675 | assert handle is not None, "Error in sendline" |
| 6676 | assert "Command not found:" not in handle, handle |
| 6677 | assert "Unsupported command:" not in handle, handle |
| 6678 | assert "Error executing command" not in handle, handle |
| 6679 | if "Added the mcast route" in handle: |
| 6680 | return main.TRUE |
| 6681 | else: |
| 6682 | return main.FALSE |
| 6683 | except AssertionError: |
| 6684 | main.log.exception( "" ) |
| 6685 | return None |
| 6686 | except TypeError: |
| 6687 | main.log.exception( self.name + ": Object not as expected" ) |
| 6688 | return None |
| 6689 | except pexpect.EOF: |
| 6690 | main.log.error( self.name + ": EOF exception found" ) |
| 6691 | main.log.error( self.name + ": " + self.handle.before ) |
| 6692 | main.cleanAndExit() |
| 6693 | except Exception: |
| 6694 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6695 | main.cleanAndExit() |
| 6696 | |
| 6697 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6698 | """ |
| 6699 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6700 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6701 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6702 | 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] | 6703 | will delete the route if not specified |
| 6704 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6705 | """ |
| 6706 | try: |
| 6707 | cmdStr = "mcast-host-delete" |
| 6708 | cmdStr += " -sAddr " + str( sAddr ) |
| 6709 | cmdStr += " -gAddr " + str( gAddr ) |
| 6710 | if host: |
| 6711 | cmdStr += " -h " + str( host ) |
| 6712 | handle = self.sendline( cmdStr ) |
| 6713 | assert handle is not None, "Error in sendline" |
| 6714 | assert "Command not found:" not in handle, handle |
| 6715 | assert "Unsupported command:" not in handle, handle |
| 6716 | assert "Error executing command" not in handle, handle |
| 6717 | if "Updated the mcast route" in handle: |
| 6718 | return main.TRUE |
| 6719 | elif "Deleted the mcast route" in handle: |
| 6720 | return main.TRUE |
| 6721 | else: |
| 6722 | return main.FALSE |
| 6723 | except AssertionError: |
| 6724 | main.log.exception( "" ) |
| 6725 | return None |
| 6726 | except TypeError: |
| 6727 | main.log.exception( self.name + ": Object not as expected" ) |
| 6728 | return None |
| 6729 | except pexpect.EOF: |
| 6730 | main.log.error( self.name + ": EOF exception found" ) |
| 6731 | main.log.error( self.name + ": " + self.handle.before ) |
| 6732 | main.cleanAndExit() |
| 6733 | except Exception: |
| 6734 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6735 | main.cleanAndExit() |
| 6736 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6737 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6738 | """ |
| 6739 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6740 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6741 | gAddr: specifies multicast group address |
| 6742 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6743 | will delete the route if not specified |
| 6744 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6745 | """ |
| 6746 | try: |
| 6747 | cmdStr = "mcast-sink-delete" |
| 6748 | cmdStr += " -sAddr " + str( sAddr ) |
| 6749 | cmdStr += " -gAddr " + str( gAddr ) |
| 6750 | if sink: |
| 6751 | cmdStr += " -s " + str( sink ) |
| 6752 | handle = self.sendline( cmdStr ) |
| 6753 | assert handle is not None, "Error in sendline" |
| 6754 | assert "Command not found:" not in handle, handle |
| 6755 | assert "Unsupported command:" not in handle, handle |
| 6756 | assert "Error executing command" not in handle, handle |
| 6757 | if "Updated the mcast route" in handle: |
| 6758 | return main.TRUE |
| 6759 | elif "Deleted the mcast route" in handle: |
| 6760 | return main.TRUE |
| 6761 | else: |
| 6762 | return main.FALSE |
| 6763 | except AssertionError: |
| 6764 | main.log.exception( "" ) |
| 6765 | return None |
| 6766 | except TypeError: |
| 6767 | main.log.exception( self.name + ": Object not as expected" ) |
| 6768 | return None |
| 6769 | except pexpect.EOF: |
| 6770 | main.log.error( self.name + ": EOF exception found" ) |
| 6771 | main.log.error( self.name + ": " + self.handle.before ) |
| 6772 | main.cleanAndExit() |
| 6773 | except Exception: |
| 6774 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6775 | main.cleanAndExit() |
| 6776 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6777 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6778 | """ |
| 6779 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6780 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6781 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6782 | 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] | 6783 | will delete the route if not specified |
| 6784 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6785 | """ |
| 6786 | try: |
| 6787 | cmdStr = "mcast-source-delete" |
| 6788 | cmdStr += " -sAddr " + str( sAddr ) |
| 6789 | cmdStr += " -gAddr " + str( gAddr ) |
| 6790 | if srcs: |
| 6791 | assert isinstance( srcs, list ) |
| 6792 | for src in srcs: |
| 6793 | cmdStr += " -src " + str( src ) |
| 6794 | handle = self.sendline( cmdStr ) |
| 6795 | assert handle is not None, "Error in sendline" |
| 6796 | assert "Command not found:" not in handle, handle |
| 6797 | assert "Unsupported command:" not in handle, handle |
| 6798 | assert "Error executing command" not in handle, handle |
| 6799 | if "Updated the mcast route" in handle: |
| 6800 | return main.TRUE |
| 6801 | elif "Deleted the mcast route" in handle: |
| 6802 | return main.TRUE |
| 6803 | else: |
| 6804 | return main.FALSE |
| 6805 | except AssertionError: |
| 6806 | main.log.exception( "" ) |
| 6807 | return None |
| 6808 | except TypeError: |
| 6809 | main.log.exception( self.name + ": Object not as expected" ) |
| 6810 | return None |
| 6811 | except pexpect.EOF: |
| 6812 | main.log.error( self.name + ": EOF exception found" ) |
| 6813 | main.log.error( self.name + ": " + self.handle.before ) |
| 6814 | main.cleanAndExit() |
| 6815 | except Exception: |
| 6816 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6817 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6818 | |
| 6819 | def netcfg( self, jsonFormat=True, args="" ): |
| 6820 | """ |
| 6821 | Run netcfg cli command with given args |
| 6822 | """ |
| 6823 | try: |
| 6824 | cmdStr = "netcfg" |
| 6825 | if jsonFormat: |
| 6826 | cmdStr = cmdStr + " -j" |
| 6827 | if args: |
| 6828 | cmdStr = cmdStr + " " + str( args ) |
| 6829 | handle = self.sendline( cmdStr ) |
| 6830 | assert handle is not None, "Error in sendline" |
| 6831 | assert "Command not found:" not in handle, handle |
| 6832 | assert "Unsupported command:" not in handle, handle |
| 6833 | assert "Error executing command" not in handle, handle |
| 6834 | return handle |
| 6835 | except AssertionError: |
| 6836 | main.log.exception( "" ) |
| 6837 | return None |
| 6838 | except TypeError: |
| 6839 | main.log.exception( self.name + ": Object not as expected" ) |
| 6840 | return None |
| 6841 | except pexpect.EOF: |
| 6842 | main.log.error( self.name + ": EOF exception found" ) |
| 6843 | main.log.error( self.name + ": " + self.handle.before ) |
| 6844 | main.cleanAndExit() |
| 6845 | except Exception: |
| 6846 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6847 | main.cleanAndExit() |
| 6848 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6849 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6850 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6851 | 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] | 6852 | Options: |
| 6853 | sAddr: IP address of the source host |
| 6854 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6855 | ipv6: True if hosts are IPv6 |
| 6856 | verbose: return verbose t3 output if True |
| 6857 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6858 | """ |
| 6859 | try: |
| 6860 | # Collect information of both hosts from onos |
| 6861 | hosts = self.hosts() |
| 6862 | hosts = json.loads( hosts ) |
| 6863 | sHost = None |
| 6864 | dHost = None |
| 6865 | for host in hosts: |
| 6866 | if sAddr in host[ "ipAddresses" ]: |
| 6867 | sHost = host |
| 6868 | elif dAddr in host[ "ipAddresses" ]: |
| 6869 | dHost = host |
| 6870 | if sHost and dHost: |
| 6871 | break |
| 6872 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6873 | cmdList = [] |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6874 | if simple: |
| 6875 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6876 | cmdStr = "t3-troubleshoot-simple" |
| 6877 | if verbose: |
| 6878 | cmdStr += " -vv" |
| 6879 | if ipv6: |
| 6880 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6881 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6882 | cmdList.append( cmdStr ) |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6883 | else: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6884 | for location in sHost[ "locations" ]: |
| 6885 | cmdStr = "t3-troubleshoot" |
| 6886 | if verbose: |
| 6887 | cmdStr += " -vv" |
| 6888 | if ipv6: |
| 6889 | cmdStr += " -et ipv6" |
| 6890 | cmdStr += " -s " + str( sAddr ) |
| 6891 | cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] ) |
| 6892 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6893 | if sHost[ "vlan" ] != "None": |
| 6894 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6895 | cmdStr += " -d " + str( dAddr ) |
| 6896 | netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) ) |
| 6897 | netcfg = json.loads( netcfg ) |
| 6898 | assert netcfg, "Failed to get netcfg" |
| 6899 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
| 6900 | cmdList.append( cmdStr ) |
| 6901 | return cmdList |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6902 | except AssertionError: |
| 6903 | main.log.exception( "" ) |
| 6904 | return None |
| 6905 | except ( KeyError, TypeError ): |
| 6906 | main.log.exception( self.name + ": Object not as expected" ) |
| 6907 | return None |
| 6908 | except Exception: |
| 6909 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6910 | main.cleanAndExit() |
| 6911 | |
| 6912 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6913 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6914 | 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] | 6915 | Options: |
| 6916 | sAddr: IP address of the source host |
| 6917 | dAddr: IP address of the destination host |
| 6918 | """ |
| 6919 | try: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6920 | cmdList = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6921 | assert cmdList is not None, "composeT3Command returned None" |
| 6922 | t3Output = "" |
| 6923 | for cmdStr in cmdList: |
| 6924 | handle = self.sendline( cmdStr ) |
| 6925 | assert handle is not None, "Error in sendline" |
| 6926 | assert "Command not found:" not in handle, handle |
| 6927 | assert "Unsupported command:" not in handle, handle |
| 6928 | assert "Error executing command" not in handle, handle |
| 6929 | assert "Tracing packet" in handle |
| 6930 | t3Output += handle |
| 6931 | return t3Output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6932 | except AssertionError: |
| 6933 | main.log.exception( "" ) |
| 6934 | return None |
| 6935 | except pexpect.EOF: |
| 6936 | main.log.error( self.name + ": EOF exception found" ) |
| 6937 | main.log.error( self.name + ": " + self.handle.before ) |
| 6938 | main.cleanAndExit() |
| 6939 | except Exception: |
| 6940 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6941 | main.cleanAndExit() |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6942 | |
| 6943 | def prepareForCLI( self, debug=True, maxRetries=120 ): |
| 6944 | """ |
| 6945 | Prepare docker container to connect to onos cli |
| 6946 | """ |
| 6947 | try: |
| 6948 | # Wait for log files to be created |
| 6949 | ready = 0 |
| 6950 | retries = 0 |
| 6951 | while not ready and retries < maxRetries: |
| 6952 | retries += 1 |
| 6953 | self.handle.sendline( "ls -al /root/onos/apache-karaf-*/data/log" ) |
| 6954 | ready = self.handle.expect( [ "No such file or directory", self.dockerPrompt ] ) |
| 6955 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6956 | if not ready: |
| 6957 | self.handle.expect( self.dockerPrompt ) |
| 6958 | time.sleep( 1 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6959 | |
| 6960 | cmdList = [] |
| 6961 | cmdList.append( "apt-get update" ) |
| 6962 | cmdList.append( "apt-get install -y openssh-server" ) |
| 6963 | # Some built in scripts are hardcoded |
| 6964 | cmdList.append( "ln -s /root/onos /opt/onos" ) |
| 6965 | cmdList.append( "ln -s /root/onos/apache-karaf-*/data/log /opt/onos/log" ) |
| 6966 | cmdList.append( "ls -al /opt/onos" ) |
| 6967 | output = "" |
| 6968 | for cmdStr in cmdList: |
| 6969 | self.handle.sendline( cmdStr ) |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 6970 | self.handle.expect( self.dockerPrompt, timeout=420 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6971 | self.handle.sendline( "" ) |
| 6972 | self.handle.expect( self.dockerPrompt ) |
| 6973 | handle = self.handle.before |
| 6974 | assert "command not found" not in handle, handle |
| 6975 | assert "No such file or directory" not in handle, handle |
| 6976 | output += handle |
| 6977 | if debug: |
| 6978 | main.log.debug( "%s: %s" % ( self.name, output ) ) |
| 6979 | return output |
| 6980 | except AssertionError: |
| 6981 | main.log.exception( "" ) |
| 6982 | return None |
| 6983 | except pexpect.EOF: |
| 6984 | main.log.error( self.name + ": EOF exception found" ) |
| 6985 | main.log.error( self.name + ": " + self.handle.before ) |
| 6986 | main.cleanAndExit() |
| 6987 | except Exception: |
| 6988 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6989 | main.cleanAndExit() |
| 6990 | |
| 6991 | def onosSecureSSH( self, userName="onos", userPWD="rocks" ): |
| 6992 | """ |
| 6993 | Enables secure access to ONOS console |
| 6994 | by removing default users & keys. |
| 6995 | |
| 6996 | bin/onos-user-password onos rocks |
| 6997 | |
| 6998 | Returns: main.TRUE on success and main.FALSE on failure |
| 6999 | """ |
| 7000 | |
| 7001 | try: |
| 7002 | self.handle.sendline( "" ) |
| 7003 | self.handle.expect( self.dockerPrompt ) |
| 7004 | |
| 7005 | self.handle.sendline( "[ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q" ) |
| 7006 | self.handle.expect( self.dockerPrompt ) |
| 7007 | main.log.debug( "%s: %s%s" % ( self.name, self.handle.before, self.handle.after ) ) |
| 7008 | |
| 7009 | self.handle.sendline( "bin/onos-user-key $(id -un) $(cut -d\\\\ -f2 ~/.ssh/id_rsa.pub)" ) |
| 7010 | self.handle.expect( pexpect.TIMEOUT, timeout=10 ) |
| 7011 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 7012 | |
| 7013 | self.handle.sendline( "bin/onos-user-password %s %s" % ( userName, userPWD ) ) |
| 7014 | i = self.handle.expect( [ "usage", |
| 7015 | self.dockerPrompt, |
| 7016 | pexpect.TIMEOUT ] ) |
| 7017 | if i == 0: |
| 7018 | # malformed command |
| 7019 | main.log.warn( self.name + ": Could not parse onos-user-password command" ) |
| 7020 | self.handle.expect( self.dockerPrompt ) |
| 7021 | return main.FALSE |
| 7022 | elif i == 1: |
| 7023 | # Process started |
| 7024 | main.log.info( self.name + ": SSH password added for user " + userName ) |
| 7025 | return main.TRUE |
| 7026 | elif i == 2: |
| 7027 | # timeout |
| 7028 | main.log.error( self.name + ": Failed to secure onos ssh " ) |
| 7029 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 7030 | except pexpect.EOF: |
| 7031 | main.log.error( self.name + ": EOF exception found" ) |
| 7032 | main.log.error( self.name + ": " + self.handle.before ) |
| 7033 | main.cleanAndExit() |
| 7034 | except Exception: |
| 7035 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 7036 | main.cleanAndExit() |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 7037 | |
| 7038 | def routeAdd( self, prefix, nextHop, jsonFormat=True ): |
| 7039 | """ |
| 7040 | Add a route with the given prefix and nextHop |
| 7041 | """ |
| 7042 | try: |
| 7043 | cmdStr = "route-add %s %s %s" % ( "-j" if jsonFormat else "", prefix, nextHop ) |
| 7044 | handle = self.sendline( cmdStr ) |
| 7045 | assert handle is not None, "Error in sendline" |
| 7046 | assert "Command not found:" not in handle, handle |
| 7047 | assert "Unsupported command:" not in handle, handle |
| 7048 | assert "Error executing command" not in handle, handle |
| 7049 | return handle |
| 7050 | except AssertionError: |
| 7051 | main.log.exception( "" ) |
| 7052 | return None |
| 7053 | except TypeError: |
| 7054 | main.log.exception( self.name + ": Object not as expected" ) |
| 7055 | return None |
| 7056 | except pexpect.EOF: |
| 7057 | main.log.error( self.name + ": EOF exception found" ) |
| 7058 | main.log.error( self.name + ": " + self.handle.before ) |
| 7059 | main.cleanAndExit() |
| 7060 | except Exception: |
| 7061 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 7062 | main.cleanAndExit() |
| 7063 | |
| 7064 | def routeRemove( self, prefix, nextHop, source=None, jsonFormat=True ): |
| 7065 | """ |
| 7066 | Remove a route with the given prefix and nextHop |
| 7067 | """ |
| 7068 | try: |
| 7069 | if source: |
| 7070 | raise NotImplemented |
| 7071 | cmdStr = "route-remove %s %s %s" % ( "-j" if jsonFormat else "", prefix, nextHop ) |
| 7072 | handle = self.sendline( cmdStr ) |
| 7073 | assert handle is not None, "Error in sendline" |
| 7074 | assert "Command not found:" not in handle, handle |
| 7075 | assert "Unsupported command:" not in handle, handle |
| 7076 | assert "Error executing command" not in handle, handle |
| 7077 | return handle |
| 7078 | except AssertionError: |
| 7079 | main.log.exception( "" ) |
| 7080 | return None |
| 7081 | except TypeError: |
| 7082 | main.log.exception( self.name + ": Object not as expected" ) |
| 7083 | return None |
| 7084 | except pexpect.EOF: |
| 7085 | main.log.error( self.name + ": EOF exception found" ) |
| 7086 | main.log.error( self.name + ": " + self.handle.before ) |
| 7087 | main.cleanAndExit() |
| 7088 | except Exception: |
| 7089 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 7090 | main.cleanAndExit() |
| 7091 | |
| 7092 | def routes( self, jsonFormat=True, args="" ): |
| 7093 | """ |
| 7094 | Get routes from ONOS |
| 7095 | """ |
| 7096 | try: |
| 7097 | cmdStr = "routes" |
| 7098 | if jsonFormat: |
| 7099 | cmdStr = cmdStr + " -j" |
| 7100 | handle = self.sendline( cmdStr ) |
| 7101 | assert handle is not None, "Error in sendline" |
| 7102 | assert "Command not found:" not in handle, handle |
| 7103 | assert "Unsupported command:" not in handle, handle |
| 7104 | assert "Error executing command" not in handle, handle |
| 7105 | return handle |
| 7106 | except AssertionError: |
| 7107 | main.log.exception( "" ) |
| 7108 | return None |
| 7109 | except TypeError: |
| 7110 | main.log.exception( self.name + ": Object not as expected" ) |
| 7111 | return None |
| 7112 | except pexpect.EOF: |
| 7113 | main.log.error( self.name + ": EOF exception found" ) |
| 7114 | main.log.error( self.name + ": " + self.handle.before ) |
| 7115 | main.cleanAndExit() |
| 7116 | except Exception: |
| 7117 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 7118 | main.cleanAndExit() |