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 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 39 | import pexpect |
| 40 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 41 | import json |
| 42 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 43 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 44 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 45 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 46 | from core.graph import Graph |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 47 | from cStringIO import StringIO |
| 48 | from itertools import izip |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 49 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 51 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | def __init__( self ): |
| 53 | """ |
| 54 | Initialize client |
| 55 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 56 | self.name = None |
| 57 | self.home = None |
| 58 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 59 | self.karafUser = None |
| 60 | self.karafPass = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 61 | self.karafPort = None |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 62 | self.karafTimeout = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 63 | self.address = None |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 64 | self.karafPromptUser = None |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 65 | self.dockerPrompt = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 66 | self.graph = Graph() |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 67 | super( OnosCliDriver, self ).__init__() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 68 | |
| 69 | def connect( self, **connectargs ): |
| 70 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 71 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 72 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 73 | try: |
| 74 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 75 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 76 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 77 | for key in self.options: |
| 78 | if key == "home": |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 79 | self.home = self.options[ key ] |
| 80 | elif key == "karaf_username": |
| 81 | self.karafUser = self.options[ key ] |
| 82 | elif key == "karaf_password": |
| 83 | self.karafPass = self.options[ key ] |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 84 | elif key == "docker_prompt": |
| 85 | self.dockerPrompt = self.options[ key ] |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 86 | elif key == "karaf_timeout": |
| 87 | self.karafTimeout = self.options[ key ] |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 88 | elif key == "karaf_port": |
| 89 | self.karafPort = self.options[ key ] |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 90 | elif key == "karafPromptUser": |
| 91 | self.karafPromptUser = self.options[ key ] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 92 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 93 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 94 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 95 | self.karafPort = self.checkOptions( self.karafPort, 8101 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 96 | self.dockerPrompt = self.checkOptions( self.dockerPrompt, "~/onos#" ) |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 97 | self.karafTimeout = self.checkOptions( self.karafTimeout, 7200000 ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 98 | |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 99 | self.karafPrompt = self.karafPromptUser + "@root >" |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 100 | for key in self.options: |
| 101 | if key == 'onosIp': |
| 102 | self.onosIp = self.options[ 'onosIp' ] |
| 103 | break |
| 104 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 105 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 106 | |
| 107 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 108 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 109 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 110 | else: |
| 111 | main.log.info( self.name + |
| 112 | ": Trying to connect to " + |
| 113 | self.ip_address ) |
| 114 | |
| 115 | except KeyError: |
| 116 | main.log.info( "Invalid host name," + |
| 117 | " connecting to local host instead" ) |
| 118 | self.ip_address = 'localhost' |
| 119 | except Exception as inst: |
| 120 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 121 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 122 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 123 | user_name=self.user_name, |
| 124 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 125 | port=self.port, |
| 126 | pwd=self.pwd, |
| 127 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 128 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 129 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 130 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 131 | if self.handle: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 132 | self.address = self.ip_address |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 133 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 134 | else: |
| 135 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 136 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 137 | except TypeError: |
| 138 | main.log.exception( self.name + ": Object not as expected" ) |
| 139 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 140 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 141 | main.log.error( self.name + ": EOF exception found" ) |
| 142 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 143 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 144 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 145 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 146 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 147 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 148 | def disconnect( self ): |
| 149 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 150 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 151 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 152 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 153 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 154 | if self.handle: |
| 155 | i = self.logout() |
| 156 | if i == main.TRUE: |
| 157 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 158 | for l in range( 3 ): |
| 159 | p = self.handle.expect( [ self.prompt, self.dockerPrompt ] ) |
| 160 | if p == 1: |
| 161 | self.inDocker = False |
| 162 | self.handle.sendline( "exit" ) |
| 163 | j = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=3 ) |
| 164 | if j == 0: |
| 165 | return response |
| 166 | response = main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 167 | except TypeError: |
| 168 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 169 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 170 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 171 | main.log.error( self.name + ": EOF exception found" ) |
| 172 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 173 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 174 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 175 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 176 | except Exception: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 177 | main.log.exception( self.name + ": disconnection failed from the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 178 | response = main.FALSE |
| 179 | return response |
| 180 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 181 | def logout( self ): |
| 182 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 183 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 184 | Returns main.TRUE if exited CLI and |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 185 | main.FALSE on timeout (not guranteed you are disconnected) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 186 | None on TypeError |
| 187 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 188 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 189 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 190 | if self.handle: |
| 191 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 192 | i = self.handle.expect( [ self.karafPrompt, |
| 193 | self.Prompt(), |
| 194 | pexpect.TIMEOUT ], |
| 195 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 196 | timeout=10 ) |
| 197 | if i == 0: # In ONOS CLI |
| 198 | self.handle.sendline( "logout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 199 | j = self.handle.expect( [ self.Prompt(), |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 200 | "Command not found:", |
| 201 | pexpect.TIMEOUT ] ) |
| 202 | if j == 0: # Successfully logged out |
| 203 | return main.TRUE |
| 204 | elif j == 1 or j == 2: |
| 205 | # ONOS didn't fully load, and logout command isn't working |
| 206 | # or the command timed out |
| 207 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 208 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 209 | self.handle.expect( self.Prompt() ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 210 | except pexpect.TIMEOUT: |
| 211 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 212 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 213 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 214 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 215 | repr( self.handle.before ) ) |
| 216 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 217 | elif i == 1: # not in CLI |
| 218 | return main.TRUE |
steven30801 | e42f1fb | 2019-01-17 11:31:45 +0800 | [diff] [blame] | 219 | elif i == 2: # Timeout |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 220 | return main.FALSE |
| 221 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 222 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 223 | except TypeError: |
| 224 | main.log.exception( self.name + ": Object not as expected" ) |
| 225 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 226 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 227 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 228 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 229 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 230 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 231 | main.log.error( self.name + |
| 232 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 233 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 234 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 235 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 236 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 237 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 238 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 239 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 240 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 241 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 242 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 243 | try: |
| 244 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 245 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 246 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 247 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 248 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 249 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 250 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 251 | # and that this driver will have to change accordingly |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 252 | self.handle.expect( str( cellname ) ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 253 | handleBefore = self.handle.before |
| 254 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 255 | # Get the rest of the handle |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 256 | self.handle.sendline( "" ) |
| 257 | self.handle.expect( self.prompt ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 258 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 259 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 260 | main.log.info( "Cell call returned: " + handleBefore + |
| 261 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 262 | |
| 263 | return main.TRUE |
| 264 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 265 | except TypeError: |
| 266 | main.log.exception( self.name + ": Object not as expected" ) |
| 267 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 268 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 269 | main.log.error( self.name + ": eof exception found" ) |
| 270 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 271 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 272 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 273 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 274 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 275 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 276 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 277 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 278 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 279 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 280 | by user would be used to set the current karaf shell idle timeout. |
| 281 | 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] | 282 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 283 | Below is an example to start a session with 60 seconds idle timeout |
| 284 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 285 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 286 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 287 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 288 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 289 | Note: karafTimeout is left as str so that this could be read |
| 290 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 291 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 292 | self.onosIp = ONOSIp |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 293 | self.address = self.onosIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 294 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 295 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 296 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 297 | x = self.handle.expect( [ self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 298 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 299 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 300 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 301 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 302 | # Not in CLI so login |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 303 | if self.inDocker: |
| 304 | # The Docker does not have all the wrapper scripts |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 305 | 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] | 306 | elif waitForStart: |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 307 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 308 | startCliCommand = "onos-wait-for-start " + str( ONOSIp ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 309 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 310 | startCliCommand = "onos " + str( ONOSIp ) |
| 311 | self.handle.sendline( startCliCommand ) |
| 312 | tries = 0 |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 313 | timeoutSet = False |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 314 | while tries < 5: |
| 315 | i = self.handle.expect( [ |
| 316 | self.karafPrompt, |
| 317 | "Password:", |
| 318 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 319 | |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 320 | if i == 0: |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 321 | if not karafTimeout or timeoutSet: |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 322 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
| 323 | return main.TRUE |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 324 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 325 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 326 | "config:property-set -p org.apache.karaf.shell\ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 327 | sshIdleTimeout " + |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 328 | str( karafTimeout ) ) |
| 329 | self.handle.expect( "closed by remote host" ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 330 | self.handle.expect( self.Prompt() ) |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 331 | self.handle.sendline( startCliCommand ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 332 | timeoutSet = True |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 333 | elif i == 1: |
| 334 | main.log.info( str( ONOSIp ) + " CLI asking for password" ) |
| 335 | main.log.debug( "Sending %s" % self.karafPass ) |
| 336 | self.handle.sendline( self.karafPass ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 337 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 338 | # If failed, send ctrl+c to process and try again |
| 339 | main.log.info( "Starting CLI failed. Retrying..." ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 340 | time.sleep( 5 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 341 | self.handle.send( "\x03" ) |
| 342 | self.handle.sendline( startCliCommand ) |
| 343 | tries += 1 |
| 344 | main.log.error( "Connection to CLI " + str( ONOSIp ) + " timeout" ) |
| 345 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 346 | except TypeError: |
| 347 | main.log.exception( self.name + ": Object not as expected" ) |
| 348 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 349 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 350 | main.log.error( self.name + ": EOF exception found" ) |
| 351 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 352 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 353 | except Exception: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 354 | main.log.debug( self.handle.before + str( self.handle.after ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 355 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 356 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 357 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 358 | def startCellCli( self, karafTimeout="", |
| 359 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 360 | """ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 361 | Start CLI on onos cell handle. |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 362 | |
| 363 | karafTimeout is an optional argument. karafTimeout value passed |
| 364 | by user would be used to set the current karaf shell idle timeout. |
| 365 | Note that when ever this property is modified the shell will exit and |
| 366 | the subsequent login would reflect new idle timeout. |
| 367 | Below is an example to start a session with 60 seconds idle timeout |
| 368 | ( input value is in milliseconds ): |
| 369 | |
| 370 | tValue = "60000" |
| 371 | |
| 372 | Note: karafTimeout is left as str so that this could be read |
| 373 | and passed to startOnosCli from PARAMS file as str. |
| 374 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 375 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 376 | try: |
| 377 | self.handle.sendline( "" ) |
| 378 | x = self.handle.expect( [ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 379 | self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 380 | |
| 381 | if x == 1: |
| 382 | main.log.info( "ONOS cli is already running" ) |
| 383 | return main.TRUE |
| 384 | |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 385 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 386 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 387 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 388 | self.karafPrompt, |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 389 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 390 | |
| 391 | if i == 0: |
| 392 | main.log.info( self.name + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 393 | if karafTimeout: # FIXME: This doesn't look right |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 394 | self.handle.sendline( |
| 395 | "config:property-set -p org.apache.karaf.shell\ |
| 396 | sshIdleTimeout " + |
| 397 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 398 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 399 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 400 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 401 | return main.TRUE |
| 402 | else: |
| 403 | # If failed, send ctrl+c to process and try again |
| 404 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 405 | self.handle.send( "\x03" ) |
| 406 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 407 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 408 | timeout=30 ) |
| 409 | if i == 0: |
| 410 | main.log.info( self.name + " CLI Started " + |
| 411 | "successfully after retry attempt" ) |
| 412 | if karafTimeout: |
| 413 | self.handle.sendline( |
| 414 | "config:property-set -p org.apache.karaf.shell\ |
| 415 | sshIdleTimeout " + |
| 416 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 417 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 418 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 419 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 420 | return main.TRUE |
| 421 | else: |
| 422 | main.log.error( "Connection to CLI " + |
| 423 | self.name + " timeout" ) |
| 424 | return main.FALSE |
| 425 | |
| 426 | except TypeError: |
| 427 | main.log.exception( self.name + ": Object not as expected" ) |
| 428 | return None |
| 429 | except pexpect.EOF: |
| 430 | main.log.error( self.name + ": EOF exception found" ) |
| 431 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 432 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 433 | except Exception: |
| 434 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 435 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 436 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 437 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 438 | """ |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 439 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 440 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 441 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 442 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 443 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 444 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 445 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 446 | """ |
| 447 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 448 | lvlStr = "" |
| 449 | if level: |
| 450 | lvlStr = "--level=" + level |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 451 | handle = self.sendline( "log:log " + lvlStr + " " + cmdStr, noExit=noExit ) |
| 452 | assert handle is not None, "Error in sendline" |
| 453 | assert "Command not found:" not in handle, handle |
| 454 | if re.search( "Error", handle ): |
| 455 | main.log.error( self.name + ": Error in logging message" ) |
| 456 | main.log.error( handle ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 457 | return main.FALSE |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 458 | else: |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 459 | return main.TRUE |
| 460 | except AssertionError: |
| 461 | main.log.exception( "" ) |
| 462 | return None |
| 463 | except TypeError: |
| 464 | main.log.exception( self.name + ": Object not as expected" ) |
| 465 | return None |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 466 | except pexpect.EOF: |
| 467 | main.log.error( self.name + ": EOF exception found" ) |
| 468 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 469 | if noExit: |
| 470 | main.cleanup() |
| 471 | return None |
| 472 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 473 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 474 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 475 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 476 | if noExit: |
| 477 | main.cleanup() |
| 478 | return None |
| 479 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 480 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 481 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 482 | def clearBuffer( self, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 483 | """ |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 484 | Test cli connection and clear any left over output in the buffer |
| 485 | Optional Arguments: |
| 486 | debug - Defaults to False. If True, will enable debug logging. |
| 487 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 488 | before a timeout. |
| 489 | 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] | 490 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 491 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 492 | # Try to reconnect if disconnected from cli |
| 493 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 494 | i = self.handle.expect( [ self.karafPrompt, |
| 495 | self.Prompt(), |
| 496 | pexpect.TIMEOUT ] ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 497 | response = self.handle.before |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 498 | if i == 1: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 499 | main.log.error( self.name + ": onos cli session closed. " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 500 | if self.onosIp: |
| 501 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 502 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 503 | if reconnectResult: |
| 504 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 505 | else: |
| 506 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 507 | if noExit: |
| 508 | return None |
| 509 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 510 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 511 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 512 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 513 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 514 | main.log.warn( "Timeout when testing cli responsiveness" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 515 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 516 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 517 | self.handle.expect( self.karafPrompt ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 518 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 519 | response += self.handle.before |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 520 | if debug: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 521 | main.log.debug( self.name + ": Raw output from sending ''" ) |
| 522 | main.log.debug( self.name + ": " + repr( response ) ) |
| 523 | except pexpect.TIMEOUT: |
| 524 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 525 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 526 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 527 | self.handle.expect( self.karafPrompt ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 528 | return None |
| 529 | except pexpect.EOF: |
| 530 | main.log.error( self.name + ": EOF exception found" ) |
| 531 | main.log.error( self.name + ": " + self.handle.before ) |
| 532 | if noExit: |
| 533 | return None |
| 534 | else: |
| 535 | main.cleanAndExit() |
| 536 | except Exception: |
| 537 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 538 | if noExit: |
| 539 | return None |
| 540 | else: |
| 541 | main.cleanAndExit() |
| 542 | |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 543 | 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] | 544 | """ |
| 545 | A wrapper around pexpect's sendline/expect. Will return all the output from a given command |
| 546 | |
| 547 | Required Arguments: |
| 548 | cmdStr - String to send to the pexpect session |
| 549 | |
| 550 | Optional Arguments: |
| 551 | showResponse - Defaults to False. If True will log the response. |
| 552 | debug - Defaults to False. If True, will enable debug logging. |
| 553 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 554 | before a timeout. |
| 555 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 556 | closed channel, but instead return None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 557 | 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] | 558 | |
| 559 | Warning: There are no sanity checking to commands sent using this method. |
| 560 | |
| 561 | """ |
| 562 | try: |
| 563 | # Try to reconnect if disconnected from cli |
| 564 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
| 565 | if debug: |
| 566 | # NOTE: This adds an average of .4 seconds per call |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 567 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 568 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 569 | self.handle.sendline( cmdStr ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 570 | self.handle.expect( self.karafPrompt, timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 571 | response = self.handle.before |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 572 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 573 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 574 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 575 | main.log.debug( self.name + ": Raw output" ) |
| 576 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 577 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 578 | response = self.cleanOutput( response, debug=debug ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 579 | # Remove control codes from karaf 4.2.1 |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 580 | 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] | 581 | response = karafEscape.sub( '', response ) |
| 582 | if debug: |
| 583 | main.log.debug( self.name + ": karafEscape output" ) |
| 584 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 585 | # Remove ANSI color control strings from output |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 586 | response = self.cleanOutput( response, debug ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 587 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 588 | # Remove ANSI color control strings from output |
Jon Hall | cf31d0f | 2018-12-13 11:18:48 -0800 | [diff] [blame] | 589 | # NOTE: karaf is sometimes adding a single character then two |
| 590 | # backspaces and sometimes adding 2 characters with 2 backspaces?? |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 591 | backspaceEscape = re.compile( r'((.|\s)\x08)' ) |
| 592 | unchanged = False |
| 593 | while not unchanged: |
| 594 | old = response |
| 595 | response = backspaceEscape.sub( '', response, count=1 ) |
| 596 | if debug: |
| 597 | main.log.debug( self.name + ": backspaceEscape output" ) |
| 598 | main.log.debug( self.name + ": " + repr( response ) ) |
| 599 | unchanged = old == response |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 600 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 601 | # Remove extra return chars that get added |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 602 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 603 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 604 | main.log.debug( self.name + ": Removed extra returns " + |
| 605 | "from output" ) |
| 606 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 607 | |
| 608 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 609 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 610 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 611 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 612 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 613 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 614 | # parse for just the output, remove the cmd from response |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 615 | cmdPattern = cmdStr.strip() |
| 616 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 617 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 618 | if relaxedRegex: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 619 | cmdPattern = cmdPattern.split( '|' )[ -1 ].strip() |
| 620 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 621 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 622 | # 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] | 623 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 624 | if expectJson: |
| 625 | main.log.warn( "Relaxed Regex: Searching for a json string amongst the output" ) |
| 626 | jsonPattern = r'\{.*\}' |
| 627 | match = re.search( jsonPattern, output[ 0 ] ) |
| 628 | if match: |
| 629 | 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] | 630 | if len( output ) < 2: |
| 631 | 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] | 632 | cmdPattern = cmdPattern[ -5: ] |
| 633 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 634 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
| 635 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 636 | else: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 637 | output = response.split( cmdPattern, 1 ) |
| 638 | 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] | 639 | 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] | 640 | output = response.split( cmdPattern[ -5: ], 1 ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 641 | if output: |
| 642 | if debug: |
| 643 | main.log.debug( self.name + ": split output" ) |
| 644 | for r in output: |
| 645 | main.log.debug( self.name + ": " + repr( r ) ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 646 | if len( output ) == 1: |
| 647 | main.log.error( "Could not remove sent command echo from output" ) |
| 648 | return output |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 649 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 650 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 651 | main.log.info( "Response from ONOS: {}".format( output ) ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 652 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 653 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 654 | except pexpect.TIMEOUT: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 655 | main.log.error( self.name + ": ONOS timeout" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 656 | if debug: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 657 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 6b5e5ba | 2019-01-31 15:32:40 -0800 | [diff] [blame] | 658 | self.exitFromCmd( self.karafPrompt, 100 ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 659 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 660 | except IndexError: |
| 661 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 662 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 663 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 664 | except TypeError: |
| 665 | main.log.exception( self.name + ": Object not as expected" ) |
| 666 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 667 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 668 | main.log.error( self.name + ": EOF exception found" ) |
| 669 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 670 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 671 | return None |
| 672 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 673 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 674 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 675 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 676 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 677 | return None |
| 678 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 679 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 680 | |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 681 | def lineCount( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
| 682 | """ |
| 683 | A wrapper around sendline(). Will return the number of lines returned or None on error |
| 684 | |
| 685 | Required Arguments: |
| 686 | cmdStr - String to send to the pexpect session |
| 687 | |
| 688 | Optional Arguments: |
| 689 | showResponse - Defaults to False. If True will log the response. |
| 690 | debug - Defaults to False. If True, will enable debug logging. |
| 691 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 692 | before a timeout. |
| 693 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 694 | closed channel, but instead return None |
| 695 | 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. |
| 696 | |
| 697 | Warning: There are no sanity checking to commands sent using this method. |
| 698 | |
| 699 | """ |
| 700 | try: |
| 701 | numLines = self.sendline( cmdStr, showResponse, debug, timeout, noExit, relaxedRegex ) |
You Wang | 0ce8e0c | 2019-02-22 12:22:26 -0800 | [diff] [blame] | 702 | parsed = re.search( "(\d+)", numLines ) |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 703 | if not parsed: |
| 704 | main.log.error( "Warning, output of karaf's wc may have changed" ) |
| 705 | return None |
| 706 | return parsed.group( 1 ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 707 | except IndexError: |
| 708 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 709 | main.log.debug( "response: {}".format( repr( numLines ) ) ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 710 | return None |
| 711 | except TypeError: |
| 712 | main.log.exception( self.name + ": Object not as expected" ) |
| 713 | return None |
| 714 | except Exception: |
| 715 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 716 | if noExit: |
| 717 | return None |
| 718 | else: |
| 719 | main.cleanAndExit() |
| 720 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 721 | # IMPORTANT NOTE: |
| 722 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 723 | # the cli command changing 'a:b' with 'aB'. |
| 724 | # Ex ) onos:topology > onosTopology |
| 725 | # onos:links > onosLinks |
| 726 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 727 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 728 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 729 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 730 | Adds a new cluster node by ID and address information. |
| 731 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 732 | * nodeId |
| 733 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 734 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 735 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 736 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 737 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 738 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 739 | str( ONOSIp ) + " " + str( tcpPort ) |
| 740 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 741 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 742 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 743 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 744 | main.log.error( self.name + ": Error in adding node" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 745 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 746 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 747 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 748 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 749 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 750 | except AssertionError: |
| 751 | main.log.exception( "" ) |
| 752 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 753 | except TypeError: |
| 754 | main.log.exception( self.name + ": Object not as expected" ) |
| 755 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 756 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 757 | main.log.error( self.name + ": EOF exception found" ) |
| 758 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 759 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 760 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 761 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 762 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 763 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 764 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 765 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 766 | Removes a cluster by ID |
| 767 | Issues command: 'remove-node [<node-id>]' |
| 768 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 769 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 770 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 771 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 772 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 773 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 774 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 775 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 776 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 777 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 778 | main.log.error( self.name + ": Error in removing node" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 779 | main.log.error( handle ) |
| 780 | return main.FALSE |
| 781 | else: |
| 782 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 783 | except AssertionError: |
| 784 | main.log.exception( "" ) |
| 785 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 786 | except TypeError: |
| 787 | main.log.exception( self.name + ": Object not as expected" ) |
| 788 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 789 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 790 | main.log.error( self.name + ": EOF exception found" ) |
| 791 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 792 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 793 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 794 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 795 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 796 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 797 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 798 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 799 | List the nodes currently visible |
| 800 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 801 | Optional argument: |
| 802 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 803 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 804 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 805 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 806 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 807 | cmdStr += " -j" |
| 808 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 809 | assert output is not None, "Error in sendline" |
Jon Hall | e37bd1f | 2020-09-10 12:16:41 -0700 | [diff] [blame] | 810 | # "Command not found" or "Service org.onosproject.security.AuditService not found" |
| 811 | assert "not found" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 812 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 813 | except AssertionError: |
| 814 | main.log.exception( "" ) |
| 815 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 816 | except TypeError: |
| 817 | main.log.exception( self.name + ": Object not as expected" ) |
| 818 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 819 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 820 | main.log.error( self.name + ": EOF exception found" ) |
| 821 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 822 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 823 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 824 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 825 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 826 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 827 | def topology( self ): |
| 828 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 829 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 830 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 831 | Return: |
| 832 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 833 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 834 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 835 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 836 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 837 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 838 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 839 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 840 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 841 | except AssertionError: |
| 842 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 843 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 844 | except TypeError: |
| 845 | main.log.exception( self.name + ": Object not as expected" ) |
| 846 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 847 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 848 | main.log.error( self.name + ": EOF exception found" ) |
| 849 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 850 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 851 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 852 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 853 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 854 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 855 | def deviceRemove( self, deviceId ): |
| 856 | """ |
| 857 | Removes particular device from storage |
| 858 | |
| 859 | TODO: refactor this function |
| 860 | """ |
| 861 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 862 | cmdStr = "device-remove " + str( deviceId ) |
| 863 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 864 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 865 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 866 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 867 | main.log.error( self.name + ": Error in removing device" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 868 | main.log.error( handle ) |
| 869 | return main.FALSE |
| 870 | else: |
| 871 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 872 | except AssertionError: |
| 873 | main.log.exception( "" ) |
| 874 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 875 | except TypeError: |
| 876 | main.log.exception( self.name + ": Object not as expected" ) |
| 877 | return None |
| 878 | except pexpect.EOF: |
| 879 | main.log.error( self.name + ": EOF exception found" ) |
| 880 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 881 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 882 | except Exception: |
| 883 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 884 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 885 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 886 | def devices( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 887 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 888 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 889 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 890 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 891 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 892 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 893 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 894 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 895 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 896 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 897 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 898 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 899 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 900 | except AssertionError: |
| 901 | main.log.exception( "" ) |
| 902 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 903 | except TypeError: |
| 904 | main.log.exception( self.name + ": Object not as expected" ) |
| 905 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 906 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 907 | main.log.error( self.name + ": EOF exception found" ) |
| 908 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 909 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 910 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 911 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 912 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 913 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 914 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 915 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 916 | This balances the devices across all controllers |
| 917 | by issuing command: 'onos> onos:balance-masters' |
| 918 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 919 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 920 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 921 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 922 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 923 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 924 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 925 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 926 | main.log.error( self.name + ": Error in balancing masters" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 927 | main.log.error( handle ) |
| 928 | return main.FALSE |
| 929 | else: |
| 930 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 931 | except AssertionError: |
| 932 | main.log.exception( "" ) |
| 933 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 934 | except TypeError: |
| 935 | main.log.exception( self.name + ": Object not as expected" ) |
| 936 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 937 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 938 | main.log.error( self.name + ": EOF exception found" ) |
| 939 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 940 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 941 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 942 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 943 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 944 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 945 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 946 | """ |
| 947 | Returns the output of the masters command. |
| 948 | Optional argument: |
| 949 | * jsonFormat - boolean indicating if you want output in json |
| 950 | """ |
| 951 | try: |
| 952 | cmdStr = "onos:masters" |
| 953 | if jsonFormat: |
| 954 | cmdStr += " -j" |
| 955 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 956 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 957 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 958 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 959 | except AssertionError: |
| 960 | main.log.exception( "" ) |
| 961 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 962 | except TypeError: |
| 963 | main.log.exception( self.name + ": Object not as expected" ) |
| 964 | return None |
| 965 | except pexpect.EOF: |
| 966 | main.log.error( self.name + ": EOF exception found" ) |
| 967 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 968 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 969 | except Exception: |
| 970 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 971 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 972 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 973 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 974 | """ |
| 975 | Uses the master command to check that the devices' leadership |
| 976 | is evenly divided |
| 977 | |
| 978 | Dependencies: checkMasters() and summary() |
| 979 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 980 | Returns main.TRUE if the devices are balanced |
| 981 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 982 | Exits on Exception |
| 983 | Returns None on TypeError |
| 984 | """ |
| 985 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 986 | summaryOutput = self.summary() |
| 987 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 988 | except ( TypeError, ValueError ): |
| 989 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 990 | return None |
| 991 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 992 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 993 | mastersOutput = self.checkMasters() |
| 994 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 995 | first = masters[ 0 ][ "size" ] |
| 996 | for master in masters: |
| 997 | totalOwnedDevices += master[ "size" ] |
| 998 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 999 | main.log.error( "Mastership not balanced" ) |
| 1000 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 1001 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 1002 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1003 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1004 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1005 | except ( TypeError, ValueError ): |
| 1006 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1007 | return None |
| 1008 | except pexpect.EOF: |
| 1009 | main.log.error( self.name + ": EOF exception found" ) |
| 1010 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1011 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1012 | except Exception: |
| 1013 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1014 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1015 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1016 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1017 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1018 | Lists all core links |
| 1019 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1020 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1021 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1022 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1023 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1024 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1025 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1026 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1027 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1028 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1029 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1030 | except AssertionError: |
| 1031 | main.log.exception( "" ) |
| 1032 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1033 | except TypeError: |
| 1034 | main.log.exception( self.name + ": Object not as expected" ) |
| 1035 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1036 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1037 | main.log.error( self.name + ": EOF exception found" ) |
| 1038 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1039 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1040 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1041 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1042 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1043 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1044 | def ports( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1045 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1046 | Lists all ports |
| 1047 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1048 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1049 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1050 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1051 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1052 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1053 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1054 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1055 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1056 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1057 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1058 | except AssertionError: |
| 1059 | main.log.exception( "" ) |
| 1060 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1061 | except TypeError: |
| 1062 | main.log.exception( self.name + ": Object not as expected" ) |
| 1063 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1064 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1065 | main.log.error( self.name + ": EOF exception found" ) |
| 1066 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1067 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1068 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1069 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1070 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1071 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1072 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1073 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1074 | Lists all devices and the controllers with roles assigned to them |
| 1075 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1076 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1077 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1078 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1079 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1080 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1081 | cmdStr += " -j" |
| 1082 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1083 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1084 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1085 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1086 | except AssertionError: |
| 1087 | main.log.exception( "" ) |
| 1088 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1089 | except TypeError: |
| 1090 | main.log.exception( self.name + ": Object not as expected" ) |
| 1091 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1092 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1093 | main.log.error( self.name + ": EOF exception found" ) |
| 1094 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1095 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1096 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1097 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1098 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1099 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1100 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1101 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1102 | Given the a string containing the json representation of the "roles" |
| 1103 | cli command and a partial or whole device id, returns a json object |
| 1104 | containing the roles output for the first device whose id contains |
| 1105 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1106 | |
| 1107 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1108 | A dict of the role assignments for the given device or |
| 1109 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1110 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1111 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1112 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1113 | return None |
| 1114 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1115 | rawRoles = self.roles() |
| 1116 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1117 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1118 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1119 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1120 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1121 | return device |
| 1122 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1123 | except ( TypeError, ValueError ): |
| 1124 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1125 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 1126 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1127 | main.log.error( self.name + ": EOF exception found" ) |
| 1128 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1129 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1130 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1131 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1132 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1133 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1134 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1135 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1136 | Iterates through each device and checks if there is a master assigned |
| 1137 | Returns: main.TRUE if each device has a master |
| 1138 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1139 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1140 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1141 | rawRoles = self.roles() |
| 1142 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1143 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1144 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1145 | # print device |
| 1146 | if device[ 'master' ] == "none": |
| 1147 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1148 | return main.FALSE |
| 1149 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1150 | except ( TypeError, ValueError ): |
| 1151 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1152 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1153 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1154 | main.log.error( self.name + ": EOF exception found" ) |
| 1155 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1156 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1157 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1158 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1159 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1160 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1161 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1162 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1163 | Returns string of paths, and the cost. |
| 1164 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1165 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1166 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1167 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1168 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1169 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1170 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1171 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1172 | main.log.error( self.name + ": Error in getting paths" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1173 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1174 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1175 | path = handle.split( ";" )[ 0 ] |
| 1176 | cost = handle.split( ";" )[ 1 ] |
| 1177 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1178 | except AssertionError: |
| 1179 | main.log.exception( "" ) |
| 1180 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1181 | except TypeError: |
| 1182 | main.log.exception( self.name + ": Object not as expected" ) |
| 1183 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1184 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1185 | main.log.error( self.name + ": EOF exception found" ) |
| 1186 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1187 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1188 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1189 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1190 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1191 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1192 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1193 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1194 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1195 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1196 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1197 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1198 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1199 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1200 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1201 | cmdStr += " -j" |
| 1202 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1203 | if handle: |
| 1204 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1205 | # TODO: Maybe make this less hardcoded |
| 1206 | # ConsistentMap Exceptions |
| 1207 | assert "org.onosproject.store.service" not in handle |
| 1208 | # Node not leader |
| 1209 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1210 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1211 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1212 | main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1213 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1214 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1215 | except TypeError: |
| 1216 | main.log.exception( self.name + ": Object not as expected" ) |
| 1217 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1218 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1219 | main.log.error( self.name + ": EOF exception found" ) |
| 1220 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1221 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1222 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1223 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1224 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1225 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1226 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1227 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1228 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1229 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1230 | 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] | 1231 | partial mac address |
| 1232 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1233 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1234 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1235 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1236 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1237 | return None |
| 1238 | else: |
| 1239 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1240 | rawHosts = self.hosts() |
| 1241 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1242 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1243 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1244 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1245 | if not host: |
| 1246 | pass |
| 1247 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1248 | return host |
| 1249 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1250 | except ( TypeError, ValueError ): |
| 1251 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1252 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1253 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1254 | main.log.error( self.name + ": EOF exception found" ) |
| 1255 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1256 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1257 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1258 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1259 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1260 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1261 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1262 | """ |
| 1263 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1264 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1265 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1266 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1267 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1268 | IMPORTANT: |
| 1269 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1270 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1271 | Furthermore, it assumes that value of VLAN is '-1' |
| 1272 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1273 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1274 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1275 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1276 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1277 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1278 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1279 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1280 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1281 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1282 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1283 | i = iter( str( hostHex ) ) |
| 1284 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1285 | hostHex = hostHex + "/-1" |
| 1286 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1287 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1288 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1289 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1290 | except TypeError: |
| 1291 | main.log.exception( self.name + ": Object not as expected" ) |
| 1292 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1293 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1294 | main.log.error( self.name + ": EOF exception found" ) |
| 1295 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1296 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1297 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1298 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1299 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1300 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1301 | def verifyHostLocation( self, hostIp, location ): |
| 1302 | """ |
| 1303 | Description: |
| 1304 | Verify the host given is discovered in all locations expected |
| 1305 | Required: |
| 1306 | hostIp: IP address of the host |
| 1307 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1308 | Could be a string or list |
| 1309 | Returns: |
| 1310 | main.TRUE if host is discovered on all locations provided |
| 1311 | main.FALSE otherwise |
| 1312 | """ |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1313 | locations = [ location ] if isinstance( location, str ) else location |
| 1314 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1315 | try: |
| 1316 | hosts = self.hosts() |
| 1317 | hosts = json.loads( hosts ) |
| 1318 | targetHost = None |
| 1319 | for host in hosts: |
| 1320 | if hostIp in host[ "ipAddresses" ]: |
| 1321 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1322 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1323 | result = main.TRUE |
| 1324 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1325 | for loc in locations: |
| 1326 | discovered = False |
| 1327 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1328 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1329 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1330 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1331 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1332 | break |
| 1333 | if discovered: |
| 1334 | locationsDiscovered.remove( locDiscovered ) |
| 1335 | else: |
| 1336 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1337 | result = main.FALSE |
| 1338 | if locationsDiscovered: |
| 1339 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1340 | result = main.FALSE |
| 1341 | return result |
| 1342 | except KeyError: |
| 1343 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1344 | return None |
| 1345 | except pexpect.EOF: |
| 1346 | main.log.error( self.name + ": EOF exception found" ) |
| 1347 | main.log.error( self.name + ": " + self.handle.before ) |
| 1348 | main.cleanAndExit() |
| 1349 | except Exception: |
| 1350 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1351 | return None |
| 1352 | |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1353 | def verifyHostIp( self, hostList=[], prefix="" ): |
| 1354 | """ |
| 1355 | Description: |
| 1356 | Verify that all hosts have IP address assigned to them |
| 1357 | Optional: |
| 1358 | hostList: If specified, verifications only happen to the hosts |
| 1359 | in hostList |
| 1360 | prefix: at least one of the ip address assigned to the host |
| 1361 | needs to have the specified prefix |
| 1362 | Returns: |
| 1363 | main.TRUE if all hosts have specific IP address assigned; |
| 1364 | main.FALSE otherwise |
| 1365 | """ |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1366 | try: |
| 1367 | hosts = self.hosts() |
| 1368 | hosts = json.loads( hosts ) |
| 1369 | if not hostList: |
| 1370 | hostList = [ host[ "id" ] for host in hosts ] |
| 1371 | for host in hosts: |
| 1372 | hostId = host[ "id" ] |
| 1373 | if hostId not in hostList: |
| 1374 | continue |
| 1375 | ipList = host[ "ipAddresses" ] |
| 1376 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1377 | if not ipList: |
| 1378 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1379 | else: |
| 1380 | if not any( ip.startswith( str( prefix ) ) for ip in ipList ): |
| 1381 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1382 | else: |
| 1383 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1384 | hostList.remove( hostId ) |
| 1385 | if hostList: |
| 1386 | 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] | 1387 | # Print info for debugging |
| 1388 | main.log.debug( self.name + ": hosts output: " + str( hosts ) ) |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1389 | return main.FALSE |
| 1390 | else: |
| 1391 | return main.TRUE |
| 1392 | except KeyError: |
| 1393 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1394 | return None |
| 1395 | except pexpect.EOF: |
| 1396 | main.log.error( self.name + ": EOF exception found" ) |
| 1397 | main.log.error( self.name + ": " + self.handle.before ) |
| 1398 | main.cleanAndExit() |
| 1399 | except Exception: |
| 1400 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1401 | return None |
| 1402 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1403 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1404 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1405 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1406 | * hostIdOne: ONOS host id for host1 |
| 1407 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1408 | Optional: |
| 1409 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1410 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1411 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1412 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1413 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1414 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1415 | Returns: |
| 1416 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1417 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1418 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1419 | cmdStr = "add-host-intent " |
| 1420 | if vlanId: |
| 1421 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1422 | if setVlan: |
| 1423 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1424 | if encap: |
| 1425 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1426 | if bandwidth: |
| 1427 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1428 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1429 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1430 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1431 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1432 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1433 | main.log.error( self.name + ": Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1434 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1435 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1436 | else: |
| 1437 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1438 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1439 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1440 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1441 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1442 | else: |
| 1443 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1444 | main.log.debug( "Response from ONOS was: " + |
| 1445 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1446 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1447 | except AssertionError: |
| 1448 | main.log.exception( "" ) |
| 1449 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1450 | except TypeError: |
| 1451 | main.log.exception( self.name + ": Object not as expected" ) |
| 1452 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1453 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1454 | main.log.error( self.name + ": EOF exception found" ) |
| 1455 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1456 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1457 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1458 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1459 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1460 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1461 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1462 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1463 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1464 | * ingressDevice: device id of ingress device |
| 1465 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1466 | Optional: |
| 1467 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1468 | Description: |
| 1469 | Adds an optical intent by specifying an ingress and egress device |
| 1470 | Returns: |
| 1471 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1472 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1473 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1474 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1475 | " " + str( egressDevice ) |
| 1476 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1477 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1478 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1479 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1480 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1481 | main.log.error( self.name + ": Error in adding Optical intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1482 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1483 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1484 | main.log.info( "Optical intent installed between " + |
| 1485 | str( ingressDevice ) + " and " + |
| 1486 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1487 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1488 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1489 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1490 | else: |
| 1491 | main.log.error( "Error, intent ID not found" ) |
| 1492 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1493 | except AssertionError: |
| 1494 | main.log.exception( "" ) |
| 1495 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1496 | except TypeError: |
| 1497 | main.log.exception( self.name + ": Object not as expected" ) |
| 1498 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1499 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1500 | main.log.error( self.name + ": EOF exception found" ) |
| 1501 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1502 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1503 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1504 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1505 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1506 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1507 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1508 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1509 | ingressDevice, |
| 1510 | egressDevice, |
| 1511 | portIngress="", |
| 1512 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1513 | ethType="", |
| 1514 | ethSrc="", |
| 1515 | ethDst="", |
| 1516 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1517 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1518 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1519 | ipProto="", |
| 1520 | ipSrc="", |
| 1521 | ipDst="", |
| 1522 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1523 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1524 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1525 | setVlan="", |
| 1526 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1527 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1528 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1529 | * ingressDevice: device id of ingress device |
| 1530 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1531 | Optional: |
| 1532 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1533 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1534 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1535 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1536 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1537 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1538 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1539 | * ipSrc: specify ip source address |
| 1540 | * ipDst: specify ip destination address |
| 1541 | * tcpSrc: specify tcp source port |
| 1542 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1543 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1544 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1545 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1546 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1547 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1548 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1549 | Returns: |
| 1550 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1551 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1552 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1553 | options developers provide for point-to-point |
| 1554 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1555 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1556 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1557 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1558 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1559 | if ethType: |
| 1560 | cmd += " --ethType " + str( ethType ) |
| 1561 | if ethSrc: |
| 1562 | cmd += " --ethSrc " + str( ethSrc ) |
| 1563 | if ethDst: |
| 1564 | cmd += " --ethDst " + str( ethDst ) |
| 1565 | if bandwidth: |
| 1566 | cmd += " --bandwidth " + str( bandwidth ) |
| 1567 | if lambdaAlloc: |
| 1568 | cmd += " --lambda " |
| 1569 | if ipProto: |
| 1570 | cmd += " --ipProto " + str( ipProto ) |
| 1571 | if ipSrc: |
| 1572 | cmd += " --ipSrc " + str( ipSrc ) |
| 1573 | if ipDst: |
| 1574 | cmd += " --ipDst " + str( ipDst ) |
| 1575 | if tcpSrc: |
| 1576 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1577 | if tcpDst: |
| 1578 | cmd += " --tcpDst " + str( tcpDst ) |
| 1579 | if vlanId: |
| 1580 | cmd += " -v " + str( vlanId ) |
| 1581 | if setVlan: |
| 1582 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1583 | if encap: |
| 1584 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1585 | if protected: |
| 1586 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1587 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1588 | # Check whether the user appended the port |
| 1589 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1590 | if "/" in ingressDevice: |
| 1591 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1592 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1593 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1594 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1595 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1596 | # Would it make sense to throw an exception and exit |
| 1597 | # the test? |
| 1598 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1599 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1600 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1601 | str( ingressDevice ) + "/" +\ |
| 1602 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1603 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1604 | if "/" in egressDevice: |
| 1605 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1606 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1607 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1608 | main.log.error( "You must specify the egress port" ) |
| 1609 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1610 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1611 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1612 | str( egressDevice ) + "/" +\ |
| 1613 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1614 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1615 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1616 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1617 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1618 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1619 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1620 | main.log.error( self.name + ": Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1621 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1622 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1623 | # TODO: print out all the options in this message? |
| 1624 | main.log.info( "Point-to-point intent installed between " + |
| 1625 | str( ingressDevice ) + " and " + |
| 1626 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1627 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1628 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1629 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1630 | else: |
| 1631 | main.log.error( "Error, intent ID not found" ) |
| 1632 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1633 | except AssertionError: |
| 1634 | main.log.exception( "" ) |
| 1635 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1636 | except TypeError: |
| 1637 | main.log.exception( self.name + ": Object not as expected" ) |
| 1638 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1639 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1640 | main.log.error( self.name + ": EOF exception found" ) |
| 1641 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1642 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1643 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1644 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1645 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1646 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1647 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1648 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1649 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1650 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1651 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1652 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1653 | ethType="", |
| 1654 | ethSrc="", |
| 1655 | ethDst="", |
| 1656 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1657 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1658 | ipProto="", |
| 1659 | ipSrc="", |
| 1660 | ipDst="", |
| 1661 | tcpSrc="", |
| 1662 | tcpDst="", |
| 1663 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1664 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1665 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1666 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1667 | partial=False, |
| 1668 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1669 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1670 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1671 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1672 | is same. That is, all ingress devices include port numbers |
| 1673 | with a "/" or all ingress devices could specify device |
| 1674 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1675 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1676 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1677 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1678 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1679 | Optional: |
| 1680 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1681 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1682 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1683 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1684 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1685 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1686 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1687 | * ipSrc: specify ip source address |
| 1688 | * ipDst: specify ip destination address |
| 1689 | * tcpSrc: specify tcp source port |
| 1690 | * tcpDst: specify tcp destination port |
| 1691 | * setEthSrc: action to Rewrite Source MAC Address |
| 1692 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1693 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1694 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1695 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1696 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1697 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1698 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1699 | Returns: |
| 1700 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1701 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1702 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1703 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1704 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1705 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1706 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1707 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1708 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1709 | if ethType: |
| 1710 | cmd += " --ethType " + str( ethType ) |
| 1711 | if ethSrc: |
| 1712 | cmd += " --ethSrc " + str( ethSrc ) |
| 1713 | if ethDst: |
| 1714 | cmd += " --ethDst " + str( ethDst ) |
| 1715 | if bandwidth: |
| 1716 | cmd += " --bandwidth " + str( bandwidth ) |
| 1717 | if lambdaAlloc: |
| 1718 | cmd += " --lambda " |
| 1719 | if ipProto: |
| 1720 | cmd += " --ipProto " + str( ipProto ) |
| 1721 | if ipSrc: |
| 1722 | cmd += " --ipSrc " + str( ipSrc ) |
| 1723 | if ipDst: |
| 1724 | cmd += " --ipDst " + str( ipDst ) |
| 1725 | if tcpSrc: |
| 1726 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1727 | if tcpDst: |
| 1728 | cmd += " --tcpDst " + str( tcpDst ) |
| 1729 | if setEthSrc: |
| 1730 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1731 | if setEthDst: |
| 1732 | cmd += " --setEthDst " + str( setEthDst ) |
| 1733 | if vlanId: |
| 1734 | cmd += " -v " + str( vlanId ) |
| 1735 | if setVlan: |
| 1736 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1737 | if partial: |
| 1738 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1739 | if encap: |
| 1740 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1741 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1742 | # Check whether the user appended the port |
| 1743 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1744 | |
| 1745 | if portIngressList is None: |
| 1746 | for ingressDevice in ingressDeviceList: |
| 1747 | if "/" in ingressDevice: |
| 1748 | cmd += " " + str( ingressDevice ) |
| 1749 | else: |
| 1750 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1751 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1752 | # TODO: perhaps more meaningful return |
| 1753 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1754 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1755 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1756 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1757 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1758 | cmd += " " + \ |
| 1759 | str( ingressDevice ) + "/" +\ |
| 1760 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1761 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1762 | main.log.error( "Device list and port list does not " + |
| 1763 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1764 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1765 | if "/" in egressDevice: |
| 1766 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1767 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1768 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1769 | main.log.error( "You must specify " + |
| 1770 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1771 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1772 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1773 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1774 | str( egressDevice ) + "/" +\ |
| 1775 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1776 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1777 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1778 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1779 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1780 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1781 | main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1782 | "intent" ) |
| 1783 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1784 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1785 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1786 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1787 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1788 | else: |
| 1789 | main.log.error( "Error, intent ID not found" ) |
| 1790 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1791 | except AssertionError: |
| 1792 | main.log.exception( "" ) |
| 1793 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1794 | except TypeError: |
| 1795 | main.log.exception( self.name + ": Object not as expected" ) |
| 1796 | return None |
| 1797 | except pexpect.EOF: |
| 1798 | main.log.error( self.name + ": EOF exception found" ) |
| 1799 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1800 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1801 | except Exception: |
| 1802 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1803 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1804 | |
| 1805 | def addSinglepointToMultipointIntent( |
| 1806 | self, |
| 1807 | ingressDevice, |
| 1808 | egressDeviceList, |
| 1809 | portIngress="", |
| 1810 | portEgressList=None, |
| 1811 | ethType="", |
| 1812 | ethSrc="", |
| 1813 | ethDst="", |
| 1814 | bandwidth="", |
| 1815 | lambdaAlloc=False, |
| 1816 | ipProto="", |
| 1817 | ipSrc="", |
| 1818 | ipDst="", |
| 1819 | tcpSrc="", |
| 1820 | tcpDst="", |
| 1821 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1822 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1823 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1824 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1825 | partial=False, |
| 1826 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1827 | """ |
| 1828 | Note: |
| 1829 | This function assumes the format of all egress devices |
| 1830 | is same. That is, all egress devices include port numbers |
| 1831 | with a "/" or all egress devices could specify device |
| 1832 | ids and port numbers seperately. |
| 1833 | Required: |
| 1834 | * EgressDeviceList: List of device ids of egress device |
| 1835 | ( Atleast 2 eress devices required in the list ) |
| 1836 | * ingressDevice: device id of ingress device |
| 1837 | Optional: |
| 1838 | * ethType: specify ethType |
| 1839 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1840 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1841 | * bandwidth: specify bandwidth capacity of link |
| 1842 | * lambdaAlloc: if True, intent will allocate lambda |
| 1843 | for the specified intent |
| 1844 | * ipProto: specify ip protocol |
| 1845 | * ipSrc: specify ip source address |
| 1846 | * ipDst: specify ip destination address |
| 1847 | * tcpSrc: specify tcp source port |
| 1848 | * tcpDst: specify tcp destination port |
| 1849 | * setEthSrc: action to Rewrite Source MAC Address |
| 1850 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1851 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1852 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1853 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1854 | Description: |
| 1855 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1856 | specifying device id's and optional fields |
| 1857 | Returns: |
| 1858 | A string of the intent id or None on error |
| 1859 | |
| 1860 | NOTE: This function may change depending on the |
| 1861 | options developers provide for singlepoint-to-multipoint |
| 1862 | intent via cli |
| 1863 | """ |
| 1864 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1865 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1866 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1867 | if ethType: |
| 1868 | cmd += " --ethType " + str( ethType ) |
| 1869 | if ethSrc: |
| 1870 | cmd += " --ethSrc " + str( ethSrc ) |
| 1871 | if ethDst: |
| 1872 | cmd += " --ethDst " + str( ethDst ) |
| 1873 | if bandwidth: |
| 1874 | cmd += " --bandwidth " + str( bandwidth ) |
| 1875 | if lambdaAlloc: |
| 1876 | cmd += " --lambda " |
| 1877 | if ipProto: |
| 1878 | cmd += " --ipProto " + str( ipProto ) |
| 1879 | if ipSrc: |
| 1880 | cmd += " --ipSrc " + str( ipSrc ) |
| 1881 | if ipDst: |
| 1882 | cmd += " --ipDst " + str( ipDst ) |
| 1883 | if tcpSrc: |
| 1884 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1885 | if tcpDst: |
| 1886 | cmd += " --tcpDst " + str( tcpDst ) |
| 1887 | if setEthSrc: |
| 1888 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1889 | if setEthDst: |
| 1890 | cmd += " --setEthDst " + str( setEthDst ) |
| 1891 | if vlanId: |
| 1892 | cmd += " -v " + str( vlanId ) |
| 1893 | if setVlan: |
| 1894 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1895 | if partial: |
| 1896 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1897 | if encap: |
| 1898 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1899 | |
| 1900 | # Check whether the user appended the port |
| 1901 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1902 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1903 | if "/" in ingressDevice: |
| 1904 | cmd += " " + str( ingressDevice ) |
| 1905 | else: |
| 1906 | if not portIngress: |
| 1907 | main.log.error( "You must specify " + |
| 1908 | "the Ingress port" ) |
| 1909 | return main.FALSE |
| 1910 | |
| 1911 | cmd += " " +\ |
| 1912 | str( ingressDevice ) + "/" +\ |
| 1913 | str( portIngress ) |
| 1914 | |
| 1915 | if portEgressList is None: |
| 1916 | for egressDevice in egressDeviceList: |
| 1917 | if "/" in egressDevice: |
| 1918 | cmd += " " + str( egressDevice ) |
| 1919 | else: |
| 1920 | main.log.error( "You must specify " + |
| 1921 | "the egress port" ) |
| 1922 | # TODO: perhaps more meaningful return |
| 1923 | return main.FALSE |
| 1924 | else: |
| 1925 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1926 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1927 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1928 | cmd += " " + \ |
| 1929 | str( egressDevice ) + "/" +\ |
| 1930 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1931 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1932 | main.log.error( "Device list and port list does not " + |
| 1933 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1934 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1935 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1936 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1937 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1938 | # If error, return error message |
| 1939 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1940 | main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " + |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1941 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1942 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1943 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1944 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1945 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1946 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1947 | else: |
| 1948 | main.log.error( "Error, intent ID not found" ) |
| 1949 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1950 | except AssertionError: |
| 1951 | main.log.exception( "" ) |
| 1952 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1953 | except TypeError: |
| 1954 | main.log.exception( self.name + ": Object not as expected" ) |
| 1955 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1956 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1957 | main.log.error( self.name + ": EOF exception found" ) |
| 1958 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1959 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1960 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1961 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1962 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1963 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1964 | def addMplsIntent( |
| 1965 | self, |
| 1966 | ingressDevice, |
| 1967 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1968 | ingressPort="", |
| 1969 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1970 | ethType="", |
| 1971 | ethSrc="", |
| 1972 | ethDst="", |
| 1973 | bandwidth="", |
| 1974 | lambdaAlloc=False, |
| 1975 | ipProto="", |
| 1976 | ipSrc="", |
| 1977 | ipDst="", |
| 1978 | tcpSrc="", |
| 1979 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1980 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1981 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1982 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1983 | """ |
| 1984 | Required: |
| 1985 | * ingressDevice: device id of ingress device |
| 1986 | * egressDevice: device id of egress device |
| 1987 | Optional: |
| 1988 | * ethType: specify ethType |
| 1989 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1990 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1991 | * bandwidth: specify bandwidth capacity of link |
| 1992 | * lambdaAlloc: if True, intent will allocate lambda |
| 1993 | for the specified intent |
| 1994 | * ipProto: specify ip protocol |
| 1995 | * ipSrc: specify ip source address |
| 1996 | * ipDst: specify ip destination address |
| 1997 | * tcpSrc: specify tcp source port |
| 1998 | * tcpDst: specify tcp destination port |
| 1999 | * ingressLabel: Ingress MPLS label |
| 2000 | * egressLabel: Egress MPLS label |
| 2001 | Description: |
| 2002 | Adds MPLS intent by |
| 2003 | specifying device id's and optional fields |
| 2004 | Returns: |
| 2005 | A string of the intent id or None on error |
| 2006 | |
| 2007 | NOTE: This function may change depending on the |
| 2008 | options developers provide for MPLS |
| 2009 | intent via cli |
| 2010 | """ |
| 2011 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 2012 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2013 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 2014 | if ethType: |
| 2015 | cmd += " --ethType " + str( ethType ) |
| 2016 | if ethSrc: |
| 2017 | cmd += " --ethSrc " + str( ethSrc ) |
| 2018 | if ethDst: |
| 2019 | cmd += " --ethDst " + str( ethDst ) |
| 2020 | if bandwidth: |
| 2021 | cmd += " --bandwidth " + str( bandwidth ) |
| 2022 | if lambdaAlloc: |
| 2023 | cmd += " --lambda " |
| 2024 | if ipProto: |
| 2025 | cmd += " --ipProto " + str( ipProto ) |
| 2026 | if ipSrc: |
| 2027 | cmd += " --ipSrc " + str( ipSrc ) |
| 2028 | if ipDst: |
| 2029 | cmd += " --ipDst " + str( ipDst ) |
| 2030 | if tcpSrc: |
| 2031 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 2032 | if tcpDst: |
| 2033 | cmd += " --tcpDst " + str( tcpDst ) |
| 2034 | if ingressLabel: |
| 2035 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 2036 | if egressLabel: |
| 2037 | cmd += " --egressLabel " + str( egressLabel ) |
| 2038 | if priority: |
| 2039 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2040 | |
| 2041 | # Check whether the user appended the port |
| 2042 | # or provided it as an input |
| 2043 | if "/" in ingressDevice: |
| 2044 | cmd += " " + str( ingressDevice ) |
| 2045 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2046 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2047 | main.log.error( "You must specify the ingress port" ) |
| 2048 | return None |
| 2049 | |
| 2050 | cmd += " " + \ |
| 2051 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2052 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2053 | |
| 2054 | if "/" in egressDevice: |
| 2055 | cmd += " " + str( egressDevice ) |
| 2056 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2057 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2058 | main.log.error( "You must specify the egress port" ) |
| 2059 | return None |
| 2060 | |
| 2061 | cmd += " " +\ |
| 2062 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2063 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2064 | |
| 2065 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2066 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2067 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2068 | # If error, return error message |
| 2069 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2070 | main.log.error( self.name + ": Error in adding mpls intent" ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2071 | return None |
| 2072 | else: |
| 2073 | # TODO: print out all the options in this message? |
| 2074 | main.log.info( "MPLS intent installed between " + |
| 2075 | str( ingressDevice ) + " and " + |
| 2076 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2077 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2078 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2079 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2080 | else: |
| 2081 | main.log.error( "Error, intent ID not found" ) |
| 2082 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2083 | except AssertionError: |
| 2084 | main.log.exception( "" ) |
| 2085 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2086 | except TypeError: |
| 2087 | main.log.exception( self.name + ": Object not as expected" ) |
| 2088 | return None |
| 2089 | except pexpect.EOF: |
| 2090 | main.log.error( self.name + ": EOF exception found" ) |
| 2091 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2092 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2093 | except Exception: |
| 2094 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2095 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2096 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2097 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 2098 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2099 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2100 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2101 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2102 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2103 | -p or --purge: Purge the intent from the store after removal |
| 2104 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2105 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 2106 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2107 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2108 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2109 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2110 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2111 | if purge: |
| 2112 | cmdStr += " -p" |
| 2113 | if sync: |
| 2114 | cmdStr += " -s" |
| 2115 | |
| 2116 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2117 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2118 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2119 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2120 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2121 | main.log.error( self.name + ": Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2122 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2123 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2124 | # TODO: Should this be main.TRUE |
| 2125 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2126 | except AssertionError: |
| 2127 | main.log.exception( "" ) |
| 2128 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2129 | except TypeError: |
| 2130 | main.log.exception( self.name + ": Object not as expected" ) |
| 2131 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2132 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2133 | main.log.error( self.name + ": EOF exception found" ) |
| 2134 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2135 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2136 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2137 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2138 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2139 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2140 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2141 | """ |
| 2142 | Description: |
| 2143 | Remove all the intents |
| 2144 | Optional args:- |
| 2145 | -s or --sync: Waits for the removal before returning |
| 2146 | -p or --purge: Purge the intent from the store after removal |
| 2147 | Returns: |
| 2148 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2149 | main.FALSE; Returns None for exception |
| 2150 | """ |
| 2151 | try: |
| 2152 | cmdStr = "remove-intent" |
| 2153 | if purge: |
| 2154 | cmdStr += " -p" |
| 2155 | if sync: |
| 2156 | cmdStr += " -s" |
| 2157 | |
| 2158 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2159 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2160 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2161 | assert "Command not found:" not in handle, handle |
| 2162 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2163 | main.log.error( self.name + ": Error in removing intent" ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2164 | return main.FALSE |
| 2165 | else: |
| 2166 | return main.TRUE |
| 2167 | except AssertionError: |
| 2168 | main.log.exception( "" ) |
| 2169 | return None |
| 2170 | except TypeError: |
| 2171 | main.log.exception( self.name + ": Object not as expected" ) |
| 2172 | return None |
| 2173 | except pexpect.EOF: |
| 2174 | main.log.error( self.name + ": EOF exception found" ) |
| 2175 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2176 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2177 | except Exception: |
| 2178 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2179 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2180 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2181 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2182 | """ |
| 2183 | Purges all WITHDRAWN Intents |
| 2184 | """ |
| 2185 | try: |
| 2186 | cmdStr = "purge-intents" |
| 2187 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2188 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2189 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2190 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2191 | main.log.error( self.name + ": Error in purging intents" ) |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2192 | return main.FALSE |
| 2193 | else: |
| 2194 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2195 | except AssertionError: |
| 2196 | main.log.exception( "" ) |
| 2197 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2198 | except TypeError: |
| 2199 | main.log.exception( self.name + ": Object not as expected" ) |
| 2200 | return None |
| 2201 | except pexpect.EOF: |
| 2202 | main.log.error( self.name + ": EOF exception found" ) |
| 2203 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2204 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2205 | except Exception: |
| 2206 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2207 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2208 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2209 | def wipeout( self ): |
| 2210 | """ |
| 2211 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2212 | """ |
| 2213 | try: |
| 2214 | cmdStr = "wipe-out please" |
| 2215 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2216 | assert handle is not None, "Error in sendline" |
| 2217 | assert "Command not found:" not in handle, handle |
| 2218 | return main.TRUE |
| 2219 | except AssertionError: |
| 2220 | main.log.exception( "" ) |
| 2221 | return None |
| 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 ) |
| 2228 | main.cleanAndExit() |
| 2229 | except Exception: |
| 2230 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2231 | main.cleanAndExit() |
| 2232 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2233 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2234 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2235 | NOTE: This method should be used after installing application: |
| 2236 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2237 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2238 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2239 | Description: |
| 2240 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2241 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2242 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2243 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2244 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2245 | cmdStr += " -j" |
| 2246 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2247 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2248 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2249 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2250 | except AssertionError: |
| 2251 | main.log.exception( "" ) |
| 2252 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2253 | except TypeError: |
| 2254 | main.log.exception( self.name + ": Object not as expected" ) |
| 2255 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2256 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2257 | main.log.error( self.name + ": EOF exception found" ) |
| 2258 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2259 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2260 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2261 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2262 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2263 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2264 | def ipv4RouteNumber( self ): |
| 2265 | """ |
| 2266 | NOTE: This method should be used after installing application: |
| 2267 | onos-app-sdnip |
| 2268 | Description: |
| 2269 | Obtain the total IPv4 routes number in the system |
| 2270 | """ |
| 2271 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2272 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2273 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2274 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2275 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2276 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2277 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2278 | except AssertionError: |
| 2279 | main.log.exception( "" ) |
| 2280 | return None |
| 2281 | except ( TypeError, ValueError ): |
| 2282 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2283 | return None |
| 2284 | except pexpect.EOF: |
| 2285 | main.log.error( self.name + ": EOF exception found" ) |
| 2286 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2287 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2288 | except Exception: |
| 2289 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2290 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2291 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2292 | # =============Function to check Bandwidth allocation======== |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2293 | def allocations( self, jsonFormat = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2294 | """ |
| 2295 | Description: |
| 2296 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2297 | """ |
| 2298 | try: |
| 2299 | cmdStr = "allocations" |
| 2300 | if jsonFormat: |
| 2301 | cmdStr += " -j" |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2302 | handle = self.sendline( cmdStr, timeout=300 ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2303 | assert handle is not None, "Error in sendline" |
| 2304 | assert "Command not found:" not in handle, handle |
| 2305 | return handle |
| 2306 | except AssertionError: |
| 2307 | main.log.exception( "" ) |
| 2308 | return None |
| 2309 | except ( TypeError, ValueError ): |
| 2310 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2311 | return None |
| 2312 | except pexpect.EOF: |
| 2313 | main.log.error( self.name + ": EOF exception found" ) |
| 2314 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2315 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2316 | except Exception: |
| 2317 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2318 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2319 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2320 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2321 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2322 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2323 | Obtain intents from the ONOS cli. |
| 2324 | Optional: |
| 2325 | * jsonFormat: Enable output formatting in json, default to True |
| 2326 | * summary: Whether only output the intent summary, defaults to False |
| 2327 | * type: Only output a certain type of intent. This options is valid |
| 2328 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2329 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2330 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2331 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2332 | if summary: |
| 2333 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2334 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2335 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2336 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2337 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2338 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2339 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2340 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2341 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2342 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2343 | intentType = "" |
| 2344 | # IF we want the summary of a specific intent type |
| 2345 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2346 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2347 | if intentType in jsonResult.keys(): |
| 2348 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2349 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2350 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2351 | return handle |
| 2352 | else: |
| 2353 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2354 | except AssertionError: |
| 2355 | main.log.exception( "" ) |
| 2356 | return None |
| 2357 | except ( TypeError, ValueError ): |
| 2358 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2359 | return None |
| 2360 | except pexpect.EOF: |
| 2361 | main.log.error( self.name + ": EOF exception found" ) |
| 2362 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2363 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2364 | except Exception: |
| 2365 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2366 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2367 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2368 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2369 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2370 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2371 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2372 | list of intent IDs. |
| 2373 | Parameters: |
| 2374 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2375 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2376 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2377 | 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] | 2378 | accepted. |
| 2379 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2380 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2381 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2382 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2383 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2384 | try: |
| 2385 | state = "State is Undefined" |
| 2386 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2387 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2388 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2389 | rawJson = intentsJson |
| 2390 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2391 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2392 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2393 | if intentsId == intent[ 'id' ]: |
| 2394 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2395 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2396 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2397 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2398 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2399 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2400 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2401 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2402 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2403 | for intent in parsedIntentsJson: |
| 2404 | if intentsId[ i ] == intent[ 'id' ]: |
| 2405 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2406 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2407 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2408 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2409 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2410 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2411 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2412 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2413 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2414 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2415 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2416 | except ( TypeError, ValueError ): |
| 2417 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2418 | return None |
| 2419 | except pexpect.EOF: |
| 2420 | main.log.error( self.name + ": EOF exception found" ) |
| 2421 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2422 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2423 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2424 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2425 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2426 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2427 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2428 | """ |
| 2429 | Description: |
| 2430 | Check intents state |
| 2431 | Required: |
| 2432 | intentsId - List of intents ID to be checked |
| 2433 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2434 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2435 | state in the list. |
| 2436 | *NOTE: You can pass in a list of expected state, |
| 2437 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2438 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2439 | Returns main.TRUE only if all intent are the same as expected states, |
| 2440 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2441 | """ |
| 2442 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2443 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2444 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2445 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2446 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2447 | intentsDict = [] |
| 2448 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2449 | if isinstance( intentsId, types.StringType ) \ |
| 2450 | and intent.get( 'id' ) == intentsId: |
| 2451 | intentsDict.append( intent ) |
| 2452 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2453 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2454 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2455 | |
| 2456 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2457 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2458 | "getting intents state" ) |
| 2459 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2460 | |
| 2461 | if isinstance( expectedState, types.StringType ): |
| 2462 | for intents in intentsDict: |
| 2463 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2464 | main.log.debug( self.name + " : Intent ID - " + |
| 2465 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2466 | " actual state = " + |
| 2467 | intents.get( 'state' ) |
| 2468 | + " does not equal expected state = " |
| 2469 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2470 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2471 | elif isinstance( expectedState, types.ListType ): |
| 2472 | for intents in intentsDict: |
| 2473 | if not any( state == intents.get( 'state' ) for state in |
| 2474 | expectedState ): |
| 2475 | main.log.debug( self.name + " : Intent ID - " + |
| 2476 | intents.get( 'id' ) + |
| 2477 | " actual state = " + |
| 2478 | intents.get( 'state' ) + |
| 2479 | " does not equal expected states = " |
| 2480 | + str( expectedState ) ) |
| 2481 | returnValue = main.FALSE |
| 2482 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2483 | if returnValue == main.TRUE: |
| 2484 | main.log.info( self.name + ": All " + |
| 2485 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2486 | " intents are in " + str( expectedState ) + |
| 2487 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2488 | return returnValue |
| 2489 | except TypeError: |
| 2490 | main.log.exception( self.name + ": Object not as expected" ) |
| 2491 | return None |
| 2492 | except pexpect.EOF: |
| 2493 | main.log.error( self.name + ": EOF exception found" ) |
| 2494 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2495 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2496 | except Exception: |
| 2497 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2498 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2499 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2500 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2501 | """ |
| 2502 | Description: |
| 2503 | Compare the allocated bandwidth with the given allocations |
| 2504 | Required: |
| 2505 | expectedAllocations - The expected ONOS output of the allocations command |
| 2506 | Return: |
| 2507 | Returns main.TRUE only if all intent are the same as expected states, |
| 2508 | otherwise returns main.FALSE. |
| 2509 | """ |
| 2510 | # FIXME: Convert these string comparisons to object comparisons |
| 2511 | try: |
| 2512 | returnValue = main.TRUE |
| 2513 | bandwidthFailed = False |
| 2514 | rawAlloc = self.allocations() |
| 2515 | expectedFormat = StringIO( expectedAllocations ) |
| 2516 | ONOSOutput = StringIO( rawAlloc ) |
| 2517 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2518 | str( expectedFormat ) ) ) |
| 2519 | |
| 2520 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2521 | actual = actual.rstrip() |
| 2522 | expected = expected.rstrip() |
| 2523 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2524 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2525 | marker1 = actual.find( 'allocated' ) |
| 2526 | m1 = actual[ :marker1 ] |
| 2527 | marker2 = expected.find( 'allocated' ) |
| 2528 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2529 | if m1 != m2: |
| 2530 | bandwidthFailed = True |
| 2531 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2532 | bandwidthFailed = True |
| 2533 | expectedFormat.close() |
| 2534 | ONOSOutput.close() |
| 2535 | |
| 2536 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2537 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2538 | returnValue = main.FALSE |
| 2539 | return returnValue |
| 2540 | except TypeError: |
| 2541 | main.log.exception( self.name + ": Object not as expected" ) |
| 2542 | return None |
| 2543 | except pexpect.EOF: |
| 2544 | main.log.error( self.name + ": EOF exception found" ) |
| 2545 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2546 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2547 | except Exception: |
| 2548 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2549 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2550 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2551 | def compareIntent( self, intentDict ): |
| 2552 | """ |
| 2553 | Description: |
| 2554 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2555 | Return: |
| 2556 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2557 | Arguments: |
| 2558 | intentDict: a dictionary which maps intent ids to intent states |
| 2559 | """ |
| 2560 | try: |
| 2561 | intentsRaw = self.intents() |
| 2562 | intentsJson = json.loads( intentsRaw ) |
| 2563 | intentDictONOS = {} |
| 2564 | for intent in intentsJson: |
| 2565 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2566 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2567 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2568 | 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] | 2569 | str( len( intentDict ) ) + " expected and " + |
| 2570 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2571 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2572 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2573 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2574 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2575 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2576 | else: |
| 2577 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2578 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2579 | " expected state is " + intentDict[ intentID ] + |
| 2580 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2581 | returnValue = main.FALSE |
| 2582 | intentDictONOS.pop( intentID ) |
| 2583 | if len( intentDictONOS ) > 0: |
| 2584 | returnValue = main.FALSE |
| 2585 | for intentID in intentDictONOS.keys(): |
| 2586 | 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] | 2587 | if returnValue == main.TRUE: |
| 2588 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2589 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2590 | except KeyError: |
| 2591 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2592 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2593 | except ( TypeError, ValueError ): |
| 2594 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2595 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2596 | except pexpect.EOF: |
| 2597 | main.log.error( self.name + ": EOF exception found" ) |
| 2598 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2599 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2600 | except Exception: |
| 2601 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2602 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2603 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2604 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2605 | """ |
| 2606 | Description: |
| 2607 | Check the number of installed intents. |
| 2608 | Optional: |
| 2609 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2610 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2611 | Return: |
| 2612 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2613 | , otherwise, returns main.FALSE. |
| 2614 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2615 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2616 | try: |
| 2617 | cmd = "intents -s -j" |
| 2618 | |
| 2619 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2620 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2621 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2622 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2623 | response = json.loads( response ) |
| 2624 | |
| 2625 | # get total and installed number, see if they are match |
| 2626 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2627 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2628 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2629 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2630 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2631 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2632 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2633 | return main.FALSE |
| 2634 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2635 | except ( TypeError, ValueError ): |
| 2636 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2637 | return None |
| 2638 | except pexpect.EOF: |
| 2639 | main.log.error( self.name + ": EOF exception found" ) |
| 2640 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2641 | if noExit: |
| 2642 | return main.FALSE |
| 2643 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2644 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2645 | except pexpect.TIMEOUT: |
| 2646 | main.log.error( self.name + ": ONOS timeout" ) |
| 2647 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2648 | except Exception: |
| 2649 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2650 | if noExit: |
| 2651 | return main.FALSE |
| 2652 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2653 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2654 | |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2655 | def flows( self, state="any", jsonFormat=True, timeout=60, noExit=False, noCore=False, device=""): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2656 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2657 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2658 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2659 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2660 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2661 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2662 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2663 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2664 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2665 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2666 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2667 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2668 | cmdStr += " -n" |
| 2669 | cmdStr += " " + state |
| 2670 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2671 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2672 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2673 | assert "Command not found:" not in handle, handle |
| 2674 | if re.search( "Error:", handle ): |
| 2675 | main.log.error( self.name + ": flows() response: " + |
| 2676 | str( handle ) ) |
| 2677 | return handle |
| 2678 | except AssertionError: |
| 2679 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2680 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2681 | except TypeError: |
| 2682 | main.log.exception( self.name + ": Object not as expected" ) |
| 2683 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2684 | except pexpect.TIMEOUT: |
| 2685 | main.log.error( self.name + ": ONOS timeout" ) |
| 2686 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2687 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2688 | main.log.error( self.name + ": EOF exception found" ) |
| 2689 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2690 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2691 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2692 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2693 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2694 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2695 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2696 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2697 | count = int( count ) if count else 0 |
steven30801 | eccfe21 | 2019-01-24 13:00:42 +0800 | [diff] [blame] | 2698 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 2699 | return count if ( count >= min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2700 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2701 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2702 | """ |
| 2703 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2704 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2705 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2706 | if the count of those states is 0, which means all current flows |
| 2707 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2708 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2709 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2710 | Return: |
| 2711 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2712 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2713 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2714 | """ |
| 2715 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2716 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2717 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2718 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2719 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2720 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2721 | if rawFlows: |
| 2722 | # if we didn't get flows or flows function return None, we should return |
| 2723 | # main.Flase |
| 2724 | checkedStates.append( json.loads( rawFlows ) ) |
| 2725 | else: |
| 2726 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2727 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2728 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2729 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2730 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2731 | except TypeError: |
| 2732 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2733 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2734 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2735 | # We want to count PENDING_ADD if isPENDING is true |
| 2736 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2737 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2738 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2739 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2740 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2741 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2742 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2743 | except ( TypeError, ValueError ): |
| 2744 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2745 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2746 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2747 | except AssertionError: |
| 2748 | main.log.exception( "" ) |
| 2749 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2750 | except pexpect.TIMEOUT: |
| 2751 | main.log.error( self.name + ": ONOS timeout" ) |
| 2752 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2753 | except pexpect.EOF: |
| 2754 | main.log.error( self.name + ": EOF exception found" ) |
| 2755 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2756 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2757 | except Exception: |
| 2758 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2759 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2760 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2761 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2762 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2763 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2764 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2765 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2766 | a specific point-to-point intent definition |
| 2767 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2768 | * ingress: specify source dpid |
| 2769 | * egress: specify destination dpid |
| 2770 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2771 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2772 | * offset: the keyOffset is where the next batch of intents |
| 2773 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2774 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2775 | * getResponse: If set to True, function will return ONOS response. |
| 2776 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2777 | Returns: If failed to push test intents, it will returen None, |
| 2778 | if successful, return true. |
| 2779 | Timeout expection will return None, |
| 2780 | TypeError will return false |
| 2781 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2782 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2783 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2784 | if background: |
| 2785 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2786 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2787 | back = "" |
| 2788 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2789 | ingress, |
| 2790 | egress, |
| 2791 | batchSize, |
| 2792 | offset, |
| 2793 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2794 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2795 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2796 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2797 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2798 | if getResponse: |
| 2799 | return response |
| 2800 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2801 | # TODO: We should handle if there is failure in installation |
| 2802 | return main.TRUE |
| 2803 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2804 | except AssertionError: |
| 2805 | main.log.exception( "" ) |
| 2806 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2807 | except pexpect.TIMEOUT: |
| 2808 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2809 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2810 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2811 | main.log.error( self.name + ": EOF exception found" ) |
| 2812 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2813 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2814 | except TypeError: |
| 2815 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2816 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2817 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2818 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2819 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2820 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2821 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2822 | """ |
| 2823 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2824 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2825 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2826 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2827 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2828 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2829 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2830 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2831 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2832 | cmd = "flows -c added" |
| 2833 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2834 | if rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2835 | rawFlows = rawFlows.split( "\n" ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2836 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2837 | for l in rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2838 | totalFlows += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2839 | else: |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 2840 | main.log.warn( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2841 | return None |
| 2842 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2843 | |
You Wang | d3097f7 | 2018-12-12 11:56:03 -0800 | [diff] [blame] | 2844 | except IndexError: |
| 2845 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
| 2846 | main.log.debug( "rawFlows: {}".format( rawFlows ) ) |
| 2847 | return None |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2848 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2849 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2850 | return None |
| 2851 | except pexpect.EOF: |
| 2852 | main.log.error( self.name + ": EOF exception found" ) |
| 2853 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2854 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2855 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2856 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2857 | except pexpect.TIMEOUT: |
| 2858 | main.log.error( self.name + ": ONOS timeout" ) |
| 2859 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2860 | except Exception: |
| 2861 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2862 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2863 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2864 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2865 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2866 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2867 | """ |
| 2868 | Description: |
| 2869 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2870 | Optional: |
| 2871 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2872 | Return: |
| 2873 | The number of intents |
| 2874 | """ |
| 2875 | try: |
| 2876 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2877 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2878 | if response is None: |
| 2879 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2880 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2881 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2882 | except ( TypeError, ValueError ): |
| 2883 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2884 | return None |
| 2885 | except pexpect.EOF: |
| 2886 | main.log.error( self.name + ": EOF exception found" ) |
| 2887 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2888 | if noExit: |
| 2889 | return -1 |
| 2890 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2891 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2892 | except Exception: |
| 2893 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2894 | if noExit: |
| 2895 | return -1 |
| 2896 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2897 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2898 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2899 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2900 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2901 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2902 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2903 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2904 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2905 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2906 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2907 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2908 | cmdStr += " -j" |
| 2909 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2910 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2911 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2912 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2913 | except AssertionError: |
| 2914 | main.log.exception( "" ) |
| 2915 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2916 | except TypeError: |
| 2917 | main.log.exception( self.name + ": Object not as expected" ) |
| 2918 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2919 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2920 | main.log.error( self.name + ": EOF exception found" ) |
| 2921 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2922 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2923 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2924 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2925 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2926 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2927 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2928 | """ |
| 2929 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2930 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2931 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2932 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2933 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2934 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2935 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2936 | cmdStr += " -j" |
| 2937 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2938 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2939 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2940 | if handle: |
| 2941 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2942 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2943 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2944 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2945 | else: |
| 2946 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2947 | except AssertionError: |
| 2948 | main.log.exception( "" ) |
| 2949 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2950 | except TypeError: |
| 2951 | main.log.exception( self.name + ": Object not as expected" ) |
| 2952 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2953 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2954 | main.log.error( self.name + ": EOF exception found" ) |
| 2955 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2956 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2957 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2958 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2959 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2960 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2961 | # Wrapper functions **************** |
| 2962 | # Wrapper functions use existing driver |
| 2963 | # functions and extends their use case. |
| 2964 | # For example, we may use the output of |
| 2965 | # a normal driver function, and parse it |
| 2966 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2967 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2968 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2969 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2970 | Description: |
| 2971 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2972 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2973 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2974 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2975 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 2976 | if intentsStr is None: |
| 2977 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2978 | # Convert to a dictionary |
| 2979 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2980 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2981 | for intent in intents: |
| 2982 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2983 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2984 | except TypeError: |
| 2985 | main.log.exception( self.name + ": Object not as expected" ) |
| 2986 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2987 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2988 | main.log.error( self.name + ": EOF exception found" ) |
| 2989 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2990 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2991 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2992 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2993 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2994 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2995 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2996 | """ |
| 2997 | Determine the number of flow rules for the given device id that are |
| 2998 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2999 | Params: |
| 3000 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3001 | """ |
| 3002 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3003 | if core: |
| 3004 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 3005 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3006 | else: |
| 3007 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 3008 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3009 | handle = self.lineCount( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3010 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3011 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3012 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3013 | except AssertionError: |
| 3014 | main.log.exception( "" ) |
| 3015 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3016 | except pexpect.EOF: |
| 3017 | main.log.error( self.name + ": EOF exception found" ) |
| 3018 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3019 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3020 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3021 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3022 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 3023 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3024 | def groupAddedCount( self, deviceId, core=False ): |
| 3025 | """ |
| 3026 | Determine the number of group rules for the given device id that are |
| 3027 | in the added state |
| 3028 | Params: |
| 3029 | core: if True, only return the number of core groups added |
| 3030 | """ |
| 3031 | try: |
| 3032 | if core: |
| 3033 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3034 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3035 | else: |
| 3036 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3037 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3038 | handle = self.lineCount( cmdStr ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3039 | assert handle is not None, "Error in sendline" |
| 3040 | assert "Command not found:" not in handle, handle |
| 3041 | return handle |
| 3042 | except AssertionError: |
| 3043 | main.log.exception( "" ) |
| 3044 | return None |
| 3045 | except pexpect.EOF: |
| 3046 | main.log.error( self.name + ": EOF exception found" ) |
| 3047 | main.log.error( self.name + ": " + self.handle.before ) |
| 3048 | main.cleanAndExit() |
| 3049 | except Exception: |
| 3050 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3051 | main.cleanAndExit() |
| 3052 | |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3053 | def addStaticRoute( self, subnet, intf): |
| 3054 | """ |
| 3055 | Adds a static route to onos. |
| 3056 | Params: |
| 3057 | subnet: The subnet reaching through this route |
| 3058 | intf: The interface this route is reachable through |
| 3059 | """ |
| 3060 | try: |
| 3061 | cmdStr = "route-add " + subnet + " " + intf |
| 3062 | handle = self.sendline( cmdStr ) |
| 3063 | assert handle is not None, "Error in sendline" |
| 3064 | assert "Command not found:" not in handle, handle |
| 3065 | return handle |
| 3066 | except AssertionError: |
| 3067 | main.log.exception( "" ) |
| 3068 | return None |
| 3069 | except pexpect.EOF: |
| 3070 | main.log.error( self.name + ": EOF exception found" ) |
| 3071 | main.log.error( self.name + ": " + self.handle.before ) |
| 3072 | main.cleanAndExit() |
| 3073 | except Exception: |
| 3074 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3075 | main.cleanAndExit() |
| 3076 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3077 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 3078 | """ |
| 3079 | Description: |
| 3080 | Check whether the number of groups for the given device id that |
| 3081 | are in ADDED state is bigger than minGroupCount. |
| 3082 | Required: |
| 3083 | * deviceId: device id to check the number of added group rules |
| 3084 | Optional: |
| 3085 | * minGroupCount: the number of groups to compare |
| 3086 | * core: if True, only check the number of core groups added |
| 3087 | * comparison: if 0, compare with greater than minFlowCount |
| 3088 | * if 1, compare with equal to minFlowCount |
| 3089 | Return: |
| 3090 | Returns the number of groups if it is bigger than minGroupCount, |
| 3091 | returns main.FALSE otherwise. |
| 3092 | """ |
| 3093 | count = self.groupAddedCount( deviceId, core ) |
| 3094 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3095 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3096 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 3097 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3098 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3099 | """ |
| 3100 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3101 | deviceId: Id of the device from which we retrieve groups |
| 3102 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3103 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3104 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3105 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 3106 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3107 | assert handle is not None, "Error in sendline" |
| 3108 | assert "Command not found:" not in handle, handle |
| 3109 | return handle |
| 3110 | except AssertionError: |
| 3111 | main.log.exception( "" ) |
| 3112 | return None |
| 3113 | except TypeError: |
| 3114 | main.log.exception( self.name + ": Object not as expected" ) |
| 3115 | return None |
| 3116 | except pexpect.EOF: |
| 3117 | main.log.error( self.name + ": EOF exception found" ) |
| 3118 | main.log.error( self.name + ": " + self.handle.before ) |
| 3119 | main.cleanAndExit() |
| 3120 | except Exception: |
| 3121 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3122 | main.cleanAndExit() |
| 3123 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3124 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3125 | """ |
| 3126 | Description: |
| 3127 | Check whether the number of flow rules for the given device id that |
| 3128 | are in ADDED state is bigger than minFlowCount. |
| 3129 | Required: |
| 3130 | * deviceId: device id to check the number of added flow rules |
| 3131 | Optional: |
| 3132 | * minFlowCount: the number of flow rules to compare |
| 3133 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3134 | * comparison: if 0, compare with greater than minFlowCount |
| 3135 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3136 | Return: |
| 3137 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 3138 | returns main.FALSE otherwise. |
| 3139 | """ |
| 3140 | count = self.flowAddedCount( deviceId, core ) |
| 3141 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3142 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3143 | 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] | 3144 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3145 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3146 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3147 | Use 'devices' function to obtain list of all devices |
| 3148 | and parse the result to obtain a list of all device |
| 3149 | id's. Returns this list. Returns empty list if no |
| 3150 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3151 | List is ordered sequentially |
| 3152 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3153 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3154 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3155 | the ids. By obtaining the list of device ids on the fly, |
| 3156 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3157 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3158 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3159 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3160 | devicesStr = self.devices( jsonFormat=False ) |
| 3161 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3162 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3163 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3164 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3165 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3166 | |
| 3167 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3168 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3169 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3170 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3171 | # Split list further into arguments before and after string |
| 3172 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3173 | # append to idList |
| 3174 | for arg in tempList: |
| 3175 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3176 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3177 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3178 | except TypeError: |
| 3179 | main.log.exception( self.name + ": Object not as expected" ) |
| 3180 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3181 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3182 | main.log.error( self.name + ": EOF exception found" ) |
| 3183 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3184 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3185 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3186 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3187 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3188 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3189 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3190 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3191 | Uses 'nodes' function to obtain list of all nodes |
| 3192 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3193 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3194 | Returns: |
| 3195 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3196 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3197 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3198 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3199 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3200 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3201 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3202 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3203 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3204 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3205 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3206 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3207 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3208 | except ( TypeError, ValueError ): |
| 3209 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3210 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3211 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3212 | main.log.error( self.name + ": EOF exception found" ) |
| 3213 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3214 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3215 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3216 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3217 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3218 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3219 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3220 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3221 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3222 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3223 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3224 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3225 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3226 | return None |
| 3227 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3228 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3229 | rawDevices = self.devices() |
| 3230 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3231 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3232 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3233 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3234 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3235 | return device |
| 3236 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3237 | except ( TypeError, ValueError ): |
| 3238 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3239 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3240 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3241 | main.log.error( self.name + ": EOF exception found" ) |
| 3242 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3243 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3244 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3245 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3246 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3247 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3248 | def getTopology( self, topologyOutput ): |
| 3249 | """ |
| 3250 | Definition: |
| 3251 | Loads a json topology output |
| 3252 | Return: |
| 3253 | topology = current ONOS topology |
| 3254 | """ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3255 | try: |
| 3256 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3257 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3258 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3259 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3260 | except ( TypeError, ValueError ): |
| 3261 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3262 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3263 | except pexpect.EOF: |
| 3264 | main.log.error( self.name + ": EOF exception found" ) |
| 3265 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3266 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3267 | except Exception: |
| 3268 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3269 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3270 | |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3271 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, numoSCCs=1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3272 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3273 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3274 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3275 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3276 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3277 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3278 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3279 | numoctrl = expected number of controllers |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3280 | numoSCCs = Number of expected SCCs |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3281 | logLevel = level to log to. |
| 3282 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3283 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3284 | Returns: main.TRUE if the number of switches and links are correct, |
| 3285 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3286 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3287 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3288 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3289 | summary = self.summary() |
| 3290 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3291 | except ( TypeError, ValueError ): |
| 3292 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3293 | return main.ERROR |
| 3294 | try: |
| 3295 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3296 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3297 | return main.ERROR |
| 3298 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3299 | # Is the number of switches is what we expected |
| 3300 | devices = topology.get( 'devices', False ) |
| 3301 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3302 | nodes = summary.get( 'nodes', False ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3303 | SCCs = summary.get( 'SCC(s)', False ) |
| 3304 | if devices is False or links is False or nodes is False or SCCs is False: |
| 3305 | main.log.warn( "Issues parsing topology and summary output" ) |
| 3306 | main.log.debug( topology ) |
| 3307 | main.log.debug( summary ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3308 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3309 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3310 | if not switchCheck: |
| 3311 | main.log.debug( "switch Check Failed" ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3312 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3313 | if not linkCheck: |
| 3314 | main.log.debug( "link Check Failed" ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3315 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3316 | if not nodeCheck: |
| 3317 | main.log.debug( "node Check Failed" ) |
| 3318 | SCCsCheck = ( int( SCCs ) == int( numoSCCs ) ) or int( numoSCCs ) == -1 |
| 3319 | if not SCCsCheck: |
| 3320 | main.log.debug( "SCCs Check Failed" ) |
| 3321 | if switchCheck and linkCheck and nodeCheck and SCCsCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3322 | # We expected the correct numbers |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3323 | output = output + "The number of links, switches, nodes, and SCCs match "\ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3324 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3325 | result = main.TRUE |
| 3326 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3327 | output = output + \ |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 3328 | "The number of links, switches, nodes, and SCCs does not match " + \ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3329 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3330 | result = main.FALSE |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3331 | output = output + "\n ONOS sees %i devices " % int( devices ) |
| 3332 | output = output + "(%i expected) " % int( numoswitch ) |
| 3333 | if int( numolink ) >= 0: |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3334 | output = output + "and %i links " % int( links ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3335 | output = output + "(%i expected) " % int( numolink ) |
| 3336 | if int( numoctrl ) >= 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3337 | output = output + "and %i controllers " % int( nodes ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3338 | output = output + "(%i expected) " % int( numoctrl ) |
| 3339 | if int( numoSCCs ) >= 0: |
| 3340 | output = output + "and %i SCCs " % int( SCCs ) |
| 3341 | output = output + "(%i expected)" % int( numoSCCs ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3342 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3343 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3344 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3345 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3346 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3347 | main.log.info( output ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3348 | main.TOPOOUTPUT = output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3349 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3350 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3351 | main.log.error( self.name + ": EOF exception found" ) |
| 3352 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3353 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3354 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3355 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3356 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3357 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3358 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3359 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3360 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3361 | deviceId must be the id of a device as seen in the onos devices command |
| 3362 | 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] | 3363 | role must be either master, standby, or none |
| 3364 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3365 | Returns: |
| 3366 | main.TRUE or main.FALSE based on argument verification and |
| 3367 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3368 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3369 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3370 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3371 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3372 | cmdStr = "device-role " +\ |
| 3373 | str( deviceId ) + " " +\ |
| 3374 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3375 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3376 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3377 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3378 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3379 | if re.search( "Error", handle ): |
| 3380 | # end color output to escape any colours |
| 3381 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3382 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3383 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3384 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3385 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3386 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3387 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3388 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3389 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3390 | except AssertionError: |
| 3391 | main.log.exception( "" ) |
| 3392 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3393 | except TypeError: |
| 3394 | main.log.exception( self.name + ": Object not as expected" ) |
| 3395 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3396 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3397 | main.log.error( self.name + ": EOF exception found" ) |
| 3398 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3399 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3400 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3401 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3402 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3403 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3404 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3405 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3406 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3407 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3408 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3409 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3410 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3411 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3412 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3413 | cmdStr += " -j" |
| 3414 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3415 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3416 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3417 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3418 | except AssertionError: |
| 3419 | main.log.exception( "" ) |
| 3420 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3421 | except TypeError: |
| 3422 | main.log.exception( self.name + ": Object not as expected" ) |
| 3423 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3424 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3425 | main.log.error( self.name + ": EOF exception found" ) |
| 3426 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3427 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3428 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3429 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3430 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3431 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3432 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3433 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3434 | CLI command to get the current leader for the Election test application |
| 3435 | NOTE: Requires installation of the onos-app-election feature |
| 3436 | Returns: Node IP of the leader if one exists |
| 3437 | None if none exists |
| 3438 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3439 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3440 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3441 | cmdStr = "election-test-leader" |
| 3442 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3443 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3444 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3445 | # Leader |
| 3446 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3447 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3448 | nodeSearch = re.search( leaderPattern, response ) |
| 3449 | if nodeSearch: |
| 3450 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3451 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3452 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3453 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3454 | # no leader |
| 3455 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3456 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3457 | nullSearch = re.search( nullPattern, response ) |
| 3458 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3459 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3460 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3461 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3462 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3463 | main.log.error( self.name + ": Error in electionTestLeader on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3464 | ": " + "unexpected response" ) |
| 3465 | main.log.error( repr( response ) ) |
| 3466 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3467 | except AssertionError: |
| 3468 | main.log.exception( "" ) |
| 3469 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3470 | except TypeError: |
| 3471 | main.log.exception( self.name + ": Object not as expected" ) |
| 3472 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3473 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3474 | main.log.error( self.name + ": EOF exception found" ) |
| 3475 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3476 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3477 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3478 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3479 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3480 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3481 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3482 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3483 | CLI command to run for leadership of the Election test application. |
| 3484 | NOTE: Requires installation of the onos-app-election feature |
| 3485 | Returns: Main.TRUE on success |
| 3486 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3487 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3488 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3489 | cmdStr = "election-test-run" |
| 3490 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3491 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3492 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3493 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3494 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3495 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3496 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3497 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3498 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3499 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3500 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3501 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3502 | main.log.error( self.name + ": Error in electionTestRun on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3503 | ": " + "unexpected response" ) |
| 3504 | main.log.error( repr( response ) ) |
| 3505 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3506 | except AssertionError: |
| 3507 | main.log.exception( "" ) |
| 3508 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3509 | except TypeError: |
| 3510 | main.log.exception( self.name + ": Object not as expected" ) |
| 3511 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3512 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3513 | main.log.error( self.name + ": EOF exception found" ) |
| 3514 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3515 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3516 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3517 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3518 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3519 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3520 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3521 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3522 | * CLI command to withdraw the local node from leadership election for |
| 3523 | * the Election test application. |
| 3524 | #NOTE: Requires installation of the onos-app-election feature |
| 3525 | Returns: Main.TRUE on success |
| 3526 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3527 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3528 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3529 | cmdStr = "election-test-withdraw" |
| 3530 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3531 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3532 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3533 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3534 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3535 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3536 | if re.search( successPattern, response ): |
| 3537 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3538 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3539 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3540 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3541 | main.log.error( self.name + ": Error in electionTestWithdraw on " + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3542 | self.name + ": " + "unexpected response" ) |
| 3543 | main.log.error( repr( response ) ) |
| 3544 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3545 | except AssertionError: |
| 3546 | main.log.exception( "" ) |
| 3547 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3548 | except TypeError: |
| 3549 | main.log.exception( self.name + ": Object not as expected" ) |
| 3550 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3551 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3552 | main.log.error( self.name + ": EOF exception found" ) |
| 3553 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3554 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3555 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3556 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3557 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3558 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3559 | def getDevicePortsEnabledCount( self, dpid ): |
| 3560 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3561 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3562 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3563 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3564 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3565 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3566 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3567 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3568 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3569 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3570 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3571 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3572 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3573 | except AssertionError: |
| 3574 | main.log.exception( "" ) |
| 3575 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3576 | except TypeError: |
| 3577 | main.log.exception( self.name + ": Object not as expected" ) |
| 3578 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3579 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3580 | main.log.error( self.name + ": EOF exception found" ) |
| 3581 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3582 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3583 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3584 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3585 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3586 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3587 | def getDeviceLinksActiveCount( self, dpid ): |
| 3588 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3589 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3590 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3591 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3592 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3593 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3594 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3595 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3596 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3597 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3598 | main.log.error( self.name + ": Error in getting ports " ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3599 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3600 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3601 | except AssertionError: |
| 3602 | main.log.exception( "" ) |
| 3603 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3604 | except TypeError: |
| 3605 | main.log.exception( self.name + ": Object not as expected" ) |
| 3606 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3607 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3608 | main.log.error( self.name + ": EOF exception found" ) |
| 3609 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3610 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3611 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3612 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3613 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3614 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3615 | def getAllIntentIds( self ): |
| 3616 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3617 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3618 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3619 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3620 | cmdStr = "onos:intents | grep id=" |
| 3621 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3622 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3623 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3624 | if re.search( "Error", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3625 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3626 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3627 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3628 | except AssertionError: |
| 3629 | main.log.exception( "" ) |
| 3630 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3631 | except TypeError: |
| 3632 | main.log.exception( self.name + ": Object not as expected" ) |
| 3633 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3634 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3635 | main.log.error( self.name + ": EOF exception found" ) |
| 3636 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3637 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3638 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3639 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3640 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3641 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3642 | def intentSummary( self ): |
| 3643 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3644 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3645 | """ |
| 3646 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3647 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3648 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3649 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3650 | states.append( intent.get( 'state', None ) ) |
| 3651 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3652 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3653 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3654 | except ( TypeError, ValueError ): |
| 3655 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3656 | return None |
| 3657 | except pexpect.EOF: |
| 3658 | main.log.error( self.name + ": EOF exception found" ) |
| 3659 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3660 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3661 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3662 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3663 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3664 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3665 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3666 | """ |
| 3667 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3668 | Optional argument: |
| 3669 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3670 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3671 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3672 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3673 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3674 | cmdStr += " -j" |
| 3675 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3676 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3677 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3678 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3679 | except AssertionError: |
| 3680 | main.log.exception( "" ) |
| 3681 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3682 | except TypeError: |
| 3683 | main.log.exception( self.name + ": Object not as expected" ) |
| 3684 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3685 | except pexpect.EOF: |
| 3686 | main.log.error( self.name + ": EOF exception found" ) |
| 3687 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3688 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3689 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3690 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3691 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3692 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3693 | def leaderCandidates( self, jsonFormat=True ): |
| 3694 | """ |
| 3695 | Returns the output of the leaders -c command. |
| 3696 | Optional argument: |
| 3697 | * jsonFormat - boolean indicating if you want output in json |
| 3698 | """ |
| 3699 | try: |
| 3700 | cmdStr = "onos:leaders -c" |
| 3701 | if jsonFormat: |
| 3702 | cmdStr += " -j" |
| 3703 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3704 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3705 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3706 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3707 | except AssertionError: |
| 3708 | main.log.exception( "" ) |
| 3709 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3710 | except TypeError: |
| 3711 | main.log.exception( self.name + ": Object not as expected" ) |
| 3712 | return None |
| 3713 | except pexpect.EOF: |
| 3714 | main.log.error( self.name + ": EOF exception found" ) |
| 3715 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3716 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3717 | except Exception: |
| 3718 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3719 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3720 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3721 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3722 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3723 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3724 | topic parameter and an empty list if the topic doesn't exist |
| 3725 | If no leader is elected leader in the returned list will be "none" |
| 3726 | Returns None if there is a type error processing the json object |
| 3727 | """ |
| 3728 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3729 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3730 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3731 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3732 | assert "Command not found:" not in rawOutput, rawOutput |
| 3733 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3734 | results = [] |
| 3735 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3736 | if dict[ "topic" ] == topic: |
| 3737 | leader = dict[ "leader" ] |
| 3738 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3739 | results.append( leader ) |
| 3740 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3741 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3742 | except AssertionError: |
| 3743 | main.log.exception( "" ) |
| 3744 | return None |
| 3745 | except ( TypeError, ValueError ): |
| 3746 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3747 | return None |
| 3748 | except pexpect.EOF: |
| 3749 | main.log.error( self.name + ": EOF exception found" ) |
| 3750 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3751 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3752 | except Exception: |
| 3753 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3754 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3755 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3756 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3757 | """ |
| 3758 | Returns the output of the intent Pending map. |
| 3759 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3760 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3761 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3762 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3763 | cmdStr += " -j" |
| 3764 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3765 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3766 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3767 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3768 | except AssertionError: |
| 3769 | main.log.exception( "" ) |
| 3770 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3771 | except TypeError: |
| 3772 | main.log.exception( self.name + ": Object not as expected" ) |
| 3773 | return None |
| 3774 | except pexpect.EOF: |
| 3775 | main.log.error( self.name + ": EOF exception found" ) |
| 3776 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3777 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3778 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3779 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3780 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3781 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3782 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3783 | """ |
| 3784 | Returns the output of the raft partitions command for ONOS. |
| 3785 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3786 | # Sample JSON |
| 3787 | # { |
| 3788 | # "leader": "tcp://10.128.30.11:7238", |
| 3789 | # "members": [ |
| 3790 | # "tcp://10.128.30.11:7238", |
| 3791 | # "tcp://10.128.30.17:7238", |
| 3792 | # "tcp://10.128.30.13:7238", |
| 3793 | # ], |
| 3794 | # "name": "p1", |
| 3795 | # "term": 3 |
| 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:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3799 | if candidates: |
| 3800 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3801 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3802 | cmdStr += " -j" |
| 3803 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3804 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3805 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3806 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3807 | except AssertionError: |
| 3808 | main.log.exception( "" ) |
| 3809 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3810 | except TypeError: |
| 3811 | main.log.exception( self.name + ": Object not as expected" ) |
| 3812 | return None |
| 3813 | except pexpect.EOF: |
| 3814 | main.log.error( self.name + ": EOF exception found" ) |
| 3815 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3816 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3817 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3818 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3819 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3820 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3821 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3822 | """ |
| 3823 | Returns the output of the apps command for ONOS. This command lists |
| 3824 | information about installed ONOS applications |
| 3825 | """ |
| 3826 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3827 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3828 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3829 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3830 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3831 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3832 | cmdStr = "onos:apps" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3833 | expectJson = False |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3834 | if summary: |
| 3835 | cmdStr += " -s" |
| 3836 | if active: |
| 3837 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3838 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3839 | cmdStr += " -j" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3840 | expectJson = True |
| 3841 | output = self.sendline( cmdStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3842 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3843 | assert "Command not found:" not in output, output |
| 3844 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3845 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3846 | # FIXME: look at specific exceptions/Errors |
| 3847 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3848 | main.log.exception( self.name + ": Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3849 | return None |
| 3850 | except TypeError: |
| 3851 | main.log.exception( self.name + ": Object not as expected" ) |
| 3852 | return None |
| 3853 | except pexpect.EOF: |
| 3854 | main.log.error( self.name + ": EOF exception found" ) |
| 3855 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3856 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3857 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3858 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3859 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3860 | |
You Wang | cdc51fe | 2018-08-12 17:14:56 -0700 | [diff] [blame] | 3861 | def appStatus( self, appName ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3862 | """ |
| 3863 | Uses the onos:apps cli command to return the status of an application. |
| 3864 | Returns: |
| 3865 | "ACTIVE" - If app is installed and activated |
| 3866 | "INSTALLED" - If app is installed and deactivated |
| 3867 | "UNINSTALLED" - If app is not installed |
| 3868 | None - on error |
| 3869 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3870 | try: |
| 3871 | if not isinstance( appName, types.StringType ): |
| 3872 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3873 | " a string" ) |
| 3874 | return None |
| 3875 | output = self.apps( jsonFormat=True ) |
| 3876 | appsJson = json.loads( output ) |
| 3877 | state = None |
| 3878 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3879 | if appName == app.get( 'name' ): |
| 3880 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3881 | break |
| 3882 | if state == "ACTIVE" or state == "INSTALLED": |
| 3883 | return state |
| 3884 | elif state is None: |
You Wang | 0d9f2c0 | 2018-08-10 14:56:32 -0700 | [diff] [blame] | 3885 | main.log.warn( "{} app not found".format( appName ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3886 | return "UNINSTALLED" |
| 3887 | elif state: |
| 3888 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3889 | str( state ) ) |
| 3890 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3891 | except ( TypeError, ValueError ): |
| 3892 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3893 | return None |
| 3894 | except pexpect.EOF: |
| 3895 | main.log.error( self.name + ": EOF exception found" ) |
| 3896 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3897 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3898 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3899 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3900 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3901 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3902 | def app( self, appName, option ): |
| 3903 | """ |
| 3904 | Interacts with the app command for ONOS. This command manages |
| 3905 | application inventory. |
| 3906 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3907 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3908 | # Validate argument types |
| 3909 | valid = True |
| 3910 | if not isinstance( appName, types.StringType ): |
| 3911 | main.log.error( self.name + ".app(): appName must be a " + |
| 3912 | "string" ) |
| 3913 | valid = False |
| 3914 | if not isinstance( option, types.StringType ): |
| 3915 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3916 | valid = False |
| 3917 | if not valid: |
| 3918 | return main.FALSE |
| 3919 | # Validate Option |
| 3920 | option = option.lower() |
| 3921 | # NOTE: Install may become a valid option |
| 3922 | if option == "activate": |
| 3923 | pass |
| 3924 | elif option == "deactivate": |
| 3925 | pass |
| 3926 | elif option == "uninstall": |
| 3927 | pass |
| 3928 | else: |
| 3929 | # Invalid option |
| 3930 | main.log.error( "The ONOS app command argument only takes " + |
| 3931 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3932 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3933 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3934 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3935 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3936 | assert output is not None, "Error in sendline" |
| 3937 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3938 | if "Error executing command" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3939 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3940 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3941 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3942 | elif "No such application" in output: |
| 3943 | main.log.error( "The application '" + appName + |
| 3944 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3945 | return main.FALSE |
| 3946 | elif "Command not found:" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3947 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3948 | str( output ) ) |
| 3949 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3950 | elif "Unsupported command:" in output: |
| 3951 | main.log.error( "Incorrect command given to 'app': " + |
| 3952 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3953 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3954 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3955 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3956 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3957 | except AssertionError: |
| 3958 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3959 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3960 | except TypeError: |
| 3961 | main.log.exception( self.name + ": Object not as expected" ) |
| 3962 | return main.ERROR |
| 3963 | except pexpect.EOF: |
| 3964 | main.log.error( self.name + ": EOF exception found" ) |
| 3965 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3966 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3967 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3968 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3969 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3970 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3971 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3972 | """ |
| 3973 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3974 | appName is the hierarchical app name, not the feature name |
| 3975 | If check is True, method will check the status of the app after the |
| 3976 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3977 | Returns main.TRUE if the command was successfully sent |
| 3978 | main.FALSE if the cli responded with an error or given |
| 3979 | incorrect input |
| 3980 | """ |
| 3981 | try: |
| 3982 | if not isinstance( appName, types.StringType ): |
| 3983 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3984 | " a string" ) |
| 3985 | return main.FALSE |
| 3986 | status = self.appStatus( appName ) |
| 3987 | if status == "INSTALLED": |
| 3988 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3989 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3990 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3991 | status = self.appStatus( appName ) |
| 3992 | if status == "ACTIVE": |
| 3993 | return main.TRUE |
| 3994 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3995 | main.log.debug( "The state of application " + |
| 3996 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3997 | time.sleep( 1 ) |
| 3998 | return main.FALSE |
| 3999 | else: # not 'check' or command didn't succeed |
| 4000 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4001 | elif status == "ACTIVE": |
| 4002 | return main.TRUE |
| 4003 | elif status == "UNINSTALLED": |
| 4004 | main.log.error( self.name + ": Tried to activate the " + |
| 4005 | "application '" + appName + "' which is not " + |
| 4006 | "installed." ) |
| 4007 | else: |
| 4008 | main.log.error( "Unexpected return value from appStatus: " + |
| 4009 | str( status ) ) |
| 4010 | return main.ERROR |
| 4011 | except TypeError: |
| 4012 | main.log.exception( self.name + ": Object not as expected" ) |
| 4013 | return main.ERROR |
| 4014 | except pexpect.EOF: |
| 4015 | main.log.error( self.name + ": EOF exception found" ) |
| 4016 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4017 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4018 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4019 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4020 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4021 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4022 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4023 | """ |
| 4024 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4025 | appName is the hierarchical app name, not the feature name |
| 4026 | If check is True, method will check the status of the app after the |
| 4027 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4028 | Returns main.TRUE if the command was successfully sent |
| 4029 | main.FALSE if the cli responded with an error or given |
| 4030 | incorrect input |
| 4031 | """ |
| 4032 | try: |
| 4033 | if not isinstance( appName, types.StringType ): |
| 4034 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 4035 | "be a string" ) |
| 4036 | return main.FALSE |
| 4037 | status = self.appStatus( appName ) |
| 4038 | if status == "INSTALLED": |
| 4039 | return main.TRUE |
| 4040 | elif status == "ACTIVE": |
| 4041 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4042 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4043 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4044 | status = self.appStatus( appName ) |
| 4045 | if status == "INSTALLED": |
| 4046 | return main.TRUE |
| 4047 | else: |
| 4048 | time.sleep( 1 ) |
| 4049 | return main.FALSE |
| 4050 | else: # not check or command didn't succeed |
| 4051 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4052 | elif status == "UNINSTALLED": |
| 4053 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 4054 | "application '" + appName + "' which is not " + |
| 4055 | "installed." ) |
| 4056 | return main.TRUE |
| 4057 | else: |
| 4058 | main.log.error( "Unexpected return value from appStatus: " + |
| 4059 | str( status ) ) |
| 4060 | return main.ERROR |
| 4061 | except TypeError: |
| 4062 | main.log.exception( self.name + ": Object not as expected" ) |
| 4063 | return main.ERROR |
| 4064 | except pexpect.EOF: |
| 4065 | main.log.error( self.name + ": EOF exception found" ) |
| 4066 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4067 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4068 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4069 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4070 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4071 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4072 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4073 | """ |
| 4074 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4075 | appName is the hierarchical app name, not the feature name |
| 4076 | If check is True, method will check the status of the app after the |
| 4077 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4078 | Returns main.TRUE if the command was successfully sent |
| 4079 | main.FALSE if the cli responded with an error or given |
| 4080 | incorrect input |
| 4081 | """ |
| 4082 | # TODO: check with Thomas about the state machine for apps |
| 4083 | try: |
| 4084 | if not isinstance( appName, types.StringType ): |
| 4085 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 4086 | "be a string" ) |
| 4087 | return main.FALSE |
| 4088 | status = self.appStatus( appName ) |
| 4089 | if status == "INSTALLED": |
| 4090 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4091 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4092 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4093 | status = self.appStatus( appName ) |
| 4094 | if status == "UNINSTALLED": |
| 4095 | return main.TRUE |
| 4096 | else: |
| 4097 | time.sleep( 1 ) |
| 4098 | return main.FALSE |
| 4099 | else: # not check or command didn't succeed |
| 4100 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4101 | elif status == "ACTIVE": |
| 4102 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 4103 | "application '" + appName + "' which is " + |
| 4104 | "currently active." ) |
| 4105 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4106 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4107 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4108 | status = self.appStatus( appName ) |
| 4109 | if status == "UNINSTALLED": |
| 4110 | return main.TRUE |
| 4111 | else: |
| 4112 | time.sleep( 1 ) |
| 4113 | return main.FALSE |
| 4114 | else: # not check or command didn't succeed |
| 4115 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4116 | elif status == "UNINSTALLED": |
| 4117 | return main.TRUE |
| 4118 | else: |
| 4119 | main.log.error( "Unexpected return value from appStatus: " + |
| 4120 | str( status ) ) |
| 4121 | return main.ERROR |
| 4122 | except TypeError: |
| 4123 | main.log.exception( self.name + ": Object not as expected" ) |
| 4124 | return main.ERROR |
| 4125 | except pexpect.EOF: |
| 4126 | main.log.error( self.name + ": EOF exception found" ) |
| 4127 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4128 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4129 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4130 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4131 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4132 | |
| 4133 | def appIDs( self, jsonFormat=True ): |
| 4134 | """ |
| 4135 | Show the mappings between app id and app names given by the 'app-ids' |
| 4136 | cli command |
| 4137 | """ |
| 4138 | try: |
| 4139 | cmdStr = "app-ids" |
| 4140 | if jsonFormat: |
| 4141 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4142 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4143 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4144 | assert "Command not found:" not in output, output |
| 4145 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4146 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4147 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4148 | main.log.exception( self.name + ": Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4149 | return None |
| 4150 | except TypeError: |
| 4151 | main.log.exception( self.name + ": Object not as expected" ) |
| 4152 | return None |
| 4153 | except pexpect.EOF: |
| 4154 | main.log.error( self.name + ": EOF exception found" ) |
| 4155 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4156 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4157 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4158 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4159 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4160 | |
| 4161 | def appToIDCheck( self ): |
| 4162 | """ |
| 4163 | This method will check that each application's ID listed in 'apps' is |
| 4164 | the same as the ID listed in 'app-ids'. The check will also check that |
| 4165 | there are no duplicate IDs issued. Note that an app ID should be |
| 4166 | a globaly unique numerical identifier for app/app-like features. Once |
| 4167 | an ID is registered, the ID is never freed up so that if an app is |
| 4168 | reinstalled it will have the same ID. |
| 4169 | |
| 4170 | Returns: main.TRUE if the check passes and |
| 4171 | main.FALSE if the check fails or |
| 4172 | main.ERROR if there is some error in processing the test |
| 4173 | """ |
| 4174 | try: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4175 | # Grab IDs |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4176 | rawJson = self.appIDs( jsonFormat=True ) |
| 4177 | if rawJson: |
| 4178 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4179 | else: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4180 | main.log.error( "app-ids returned nothing: " + repr( rawJson ) ) |
| 4181 | return main.FALSE |
| 4182 | |
| 4183 | # Grab Apps |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4184 | rawJson = self.apps( jsonFormat=True ) |
| 4185 | if rawJson: |
| 4186 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4187 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4188 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4189 | return main.FALSE |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4190 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4191 | result = main.TRUE |
| 4192 | for app in apps: |
| 4193 | appID = app.get( 'id' ) |
| 4194 | if appID is None: |
| 4195 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4196 | result = main.FALSE |
| 4197 | appName = app.get( 'name' ) |
| 4198 | if appName is None: |
| 4199 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4200 | result = main.FALSE |
| 4201 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4202 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4203 | if not current: # if ids doesn't have this id |
| 4204 | result = main.FALSE |
| 4205 | main.log.error( "'app-ids' does not have the ID for " + |
| 4206 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4207 | main.log.debug( "apps command returned: " + str( app ) + |
| 4208 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4209 | elif len( current ) > 1: |
| 4210 | # there is more than one app with this ID |
| 4211 | result = main.FALSE |
| 4212 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4213 | elif not current[ 0 ][ 'name' ] == appName: |
| 4214 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4215 | result = main.FALSE |
| 4216 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4217 | " registered under id:" + str( appID ) + |
| 4218 | " but 'apps' has " + str( appName ) ) |
| 4219 | else: |
| 4220 | pass # id and name match! |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4221 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4222 | # now make sure that app-ids has no duplicates |
| 4223 | idsList = [] |
| 4224 | namesList = [] |
| 4225 | for item in ids: |
| 4226 | idsList.append( item[ 'id' ] ) |
| 4227 | namesList.append( item[ 'name' ] ) |
| 4228 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4229 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4230 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4231 | + json.dumps( ids, |
| 4232 | sort_keys=True, |
| 4233 | indent=4, |
| 4234 | separators=( ',', ': ' ) ) ) |
| 4235 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4236 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4237 | except ( TypeError, ValueError ): |
| 4238 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4239 | return main.ERROR |
| 4240 | except pexpect.EOF: |
| 4241 | main.log.error( self.name + ": EOF exception found" ) |
| 4242 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4243 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4244 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4245 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4246 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4247 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4248 | def getCfg( self, component=None, propName=None, short=False, |
| 4249 | jsonFormat=True ): |
| 4250 | """ |
| 4251 | Get configuration settings from onos cli |
| 4252 | Optional arguments: |
| 4253 | component - Optionally only list configurations for a specific |
| 4254 | component. If None, all components with configurations |
| 4255 | are displayed. Case Sensitive string. |
| 4256 | propName - If component is specified, propName option will show |
| 4257 | only this specific configuration from that component. |
| 4258 | Case Sensitive string. |
| 4259 | jsonFormat - Returns output as json. Note that this will override |
| 4260 | the short option |
| 4261 | short - Short, less verbose, version of configurations. |
| 4262 | This is overridden by the json option |
| 4263 | returns: |
| 4264 | Output from cli as a string or None on error |
| 4265 | """ |
| 4266 | try: |
| 4267 | baseStr = "cfg" |
| 4268 | cmdStr = " get" |
| 4269 | componentStr = "" |
| 4270 | if component: |
| 4271 | componentStr += " " + component |
| 4272 | if propName: |
| 4273 | componentStr += " " + propName |
| 4274 | if jsonFormat: |
| 4275 | baseStr += " -j" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4276 | expectJson = True |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4277 | elif short: |
| 4278 | baseStr += " -s" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4279 | expectJson = False |
| 4280 | output = self.sendline( baseStr + cmdStr + componentStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4281 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4282 | assert "Command not found:" not in output, output |
| 4283 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4284 | return output |
| 4285 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4286 | main.log.exception( self.name + ": Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4287 | return None |
| 4288 | except TypeError: |
| 4289 | main.log.exception( self.name + ": Object not as expected" ) |
| 4290 | return None |
| 4291 | except pexpect.EOF: |
| 4292 | main.log.error( self.name + ": EOF exception found" ) |
| 4293 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4294 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4295 | except Exception: |
| 4296 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4297 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4298 | |
| 4299 | def setCfg( self, component, propName, value=None, check=True ): |
| 4300 | """ |
| 4301 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4302 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4303 | component - The case sensitive name of the component whose |
| 4304 | property is to be set |
| 4305 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4306 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4307 | 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] | 4308 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4309 | check - Boolean, Check whether the option was successfully set this |
| 4310 | only applies when a value is given. |
| 4311 | returns: |
| 4312 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4313 | will return main.TRUE unless there is an error |
| 4314 | """ |
| 4315 | try: |
| 4316 | baseStr = "cfg" |
| 4317 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4318 | if value is not None: |
| 4319 | cmdStr += " " + str( value ) |
| 4320 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4321 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4322 | assert "Command not found:" not in output, output |
| 4323 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4324 | if value and check: |
| 4325 | results = self.getCfg( component=str( component ), |
| 4326 | propName=str( propName ), |
| 4327 | jsonFormat=True ) |
| 4328 | # Check if current value is what we just set |
| 4329 | try: |
| 4330 | jsonOutput = json.loads( results ) |
| 4331 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4332 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4333 | main.log.exception( "Error parsing cfg output" ) |
| 4334 | main.log.error( "output:" + repr( results ) ) |
| 4335 | return main.FALSE |
| 4336 | if current == str( value ): |
| 4337 | return main.TRUE |
| 4338 | return main.FALSE |
| 4339 | return main.TRUE |
| 4340 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4341 | main.log.exception( self.name + ": Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4342 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4343 | except ( TypeError, ValueError ): |
| 4344 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4345 | return main.FALSE |
| 4346 | except pexpect.EOF: |
| 4347 | main.log.error( self.name + ": EOF exception found" ) |
| 4348 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4349 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4350 | except Exception: |
| 4351 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4352 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4353 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4354 | def distPrimitivesSend( self, cmd ): |
| 4355 | """ |
| 4356 | Function to handle sending cli commands for the distributed primitives test app |
| 4357 | |
| 4358 | This command will catch some exceptions and retry the command on some |
| 4359 | specific store exceptions. |
| 4360 | |
| 4361 | Required arguments: |
| 4362 | cmd - The command to send to the cli |
| 4363 | returns: |
| 4364 | string containing the cli output |
| 4365 | None on Error |
| 4366 | """ |
| 4367 | try: |
| 4368 | output = self.sendline( cmd ) |
| 4369 | try: |
| 4370 | assert output is not None, "Error in sendline" |
| 4371 | # TODO: Maybe make this less hardcoded |
| 4372 | # ConsistentMap Exceptions |
| 4373 | assert "org.onosproject.store.service" not in output |
| 4374 | # Node not leader |
| 4375 | assert "java.lang.IllegalStateException" not in output |
| 4376 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4377 | main.log.error( self.name + ": Error in processing '" + cmd + "' " + |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4378 | "command: " + str( output ) ) |
| 4379 | retryTime = 30 # Conservative time, given by Madan |
| 4380 | main.log.info( "Waiting " + str( retryTime ) + |
| 4381 | "seconds before retrying." ) |
| 4382 | time.sleep( retryTime ) # Due to change in mastership |
| 4383 | output = self.sendline( cmd ) |
| 4384 | assert output is not None, "Error in sendline" |
| 4385 | assert "Command not found:" not in output, output |
| 4386 | assert "Error executing command" not in output, output |
| 4387 | main.log.info( self.name + ": " + output ) |
| 4388 | return output |
| 4389 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4390 | main.log.exception( self.name + ": Error in processing '" + cmd + "' command." ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4391 | return None |
| 4392 | except TypeError: |
| 4393 | main.log.exception( self.name + ": Object not as expected" ) |
| 4394 | return None |
| 4395 | except pexpect.EOF: |
| 4396 | main.log.error( self.name + ": EOF exception found" ) |
| 4397 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4398 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4399 | except Exception: |
| 4400 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4401 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4402 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4403 | def setTestAdd( self, setName, values ): |
| 4404 | """ |
| 4405 | CLI command to add elements to a distributed set. |
| 4406 | Arguments: |
| 4407 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4408 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4409 | Example usages: |
| 4410 | setTestAdd( "set1", "a b c" ) |
| 4411 | setTestAdd( "set2", "1" ) |
| 4412 | returns: |
| 4413 | main.TRUE on success OR |
| 4414 | main.FALSE if elements were already in the set OR |
| 4415 | main.ERROR on error |
| 4416 | """ |
| 4417 | try: |
| 4418 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4419 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4420 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4421 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4422 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4423 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4424 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4425 | return main.FALSE |
| 4426 | else: |
| 4427 | main.log.error( self.name + ": setTestAdd did not" + |
| 4428 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4429 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4430 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4431 | except TypeError: |
| 4432 | main.log.exception( self.name + ": Object not as expected" ) |
| 4433 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4434 | except Exception: |
| 4435 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4436 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4437 | |
| 4438 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4439 | """ |
| 4440 | CLI command to remove elements from a distributed set. |
| 4441 | Required arguments: |
| 4442 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4443 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4444 | Optional arguments: |
| 4445 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4446 | retain - Retain only the given values. (intersection of the |
| 4447 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4448 | returns: |
| 4449 | main.TRUE on success OR |
| 4450 | main.FALSE if the set was not changed OR |
| 4451 | main.ERROR on error |
| 4452 | """ |
| 4453 | try: |
| 4454 | cmdStr = "set-test-remove " |
| 4455 | if clear: |
| 4456 | cmdStr += "-c " + str( setName ) |
| 4457 | elif retain: |
| 4458 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4459 | else: |
| 4460 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4461 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4462 | if clear: |
| 4463 | pattern = "Set " + str( setName ) + " cleared" |
| 4464 | if re.search( pattern, output ): |
| 4465 | return main.TRUE |
| 4466 | elif retain: |
| 4467 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4468 | "only elements of set \[(.*)\]" |
| 4469 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4470 | "retaining only elements of the set " +\ |
| 4471 | "\[(.*)\]" |
| 4472 | if re.search( positivePattern, output ): |
| 4473 | return main.TRUE |
| 4474 | elif re.search( negativePattern, output ): |
| 4475 | return main.FALSE |
| 4476 | else: |
| 4477 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4478 | str( setName ) |
| 4479 | if ( len( values.split() ) == 1 ): |
| 4480 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4481 | str( setName ) |
| 4482 | else: |
| 4483 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4484 | str( setName ) |
| 4485 | if re.search( positivePattern, output ): |
| 4486 | return main.TRUE |
| 4487 | elif re.search( negativePattern, output ): |
| 4488 | return main.FALSE |
| 4489 | main.log.error( self.name + ": setTestRemove did not" + |
| 4490 | " match expected output" ) |
| 4491 | main.log.debug( self.name + " expected: " + pattern ) |
| 4492 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4493 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4494 | except TypeError: |
| 4495 | main.log.exception( self.name + ": Object not as expected" ) |
| 4496 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4497 | except Exception: |
| 4498 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4499 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4500 | |
| 4501 | def setTestGet( self, setName, values="" ): |
| 4502 | """ |
| 4503 | CLI command to get the elements in a distributed set. |
| 4504 | Required arguments: |
| 4505 | setName - The name of the set to remove from. |
| 4506 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4507 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4508 | returns: |
| 4509 | main.ERROR on error OR |
| 4510 | A list of elements in the set if no optional arguments are |
| 4511 | supplied OR |
| 4512 | A tuple containing the list then: |
| 4513 | main.FALSE if the given values are not in the set OR |
| 4514 | main.TRUE if the given values are in the set OR |
| 4515 | """ |
| 4516 | try: |
| 4517 | values = str( values ).strip() |
| 4518 | setName = str( setName ).strip() |
| 4519 | length = len( values.split() ) |
| 4520 | containsCheck = None |
| 4521 | # Patterns to match |
| 4522 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4523 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4524 | containsTrue = "Set " + setName + " contains the value " + values |
| 4525 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4526 | values |
| 4527 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4528 | setPattern |
| 4529 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4530 | " subset " + setPattern |
| 4531 | |
| 4532 | cmdStr = "set-test-get " |
| 4533 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4534 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4535 | if length == 0: |
| 4536 | match = re.search( pattern, output ) |
| 4537 | else: # if given values |
| 4538 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4539 | patternTrue = pattern + "\r\n" + containsTrue |
| 4540 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4541 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4542 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4543 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4544 | matchTrue = re.search( patternTrue, output ) |
| 4545 | matchFalse = re.search( patternFalse, output ) |
| 4546 | if matchTrue: |
| 4547 | containsCheck = main.TRUE |
| 4548 | match = matchTrue |
| 4549 | elif matchFalse: |
| 4550 | containsCheck = main.FALSE |
| 4551 | match = matchFalse |
| 4552 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4553 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4554 | "expected output" ) |
| 4555 | main.log.debug( self.name + " expected: " + pattern ) |
| 4556 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4557 | match = None |
| 4558 | if match: |
| 4559 | setMatch = match.group( 1 ) |
| 4560 | if setMatch == '': |
| 4561 | setList = [] |
| 4562 | else: |
| 4563 | setList = setMatch.split( ", " ) |
| 4564 | if length > 0: |
| 4565 | return ( setList, containsCheck ) |
| 4566 | else: |
| 4567 | return setList |
| 4568 | else: # no match |
| 4569 | main.log.error( self.name + ": setTestGet did not" + |
| 4570 | " match expected output" ) |
| 4571 | main.log.debug( self.name + " expected: " + pattern ) |
| 4572 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4573 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4574 | except TypeError: |
| 4575 | main.log.exception( self.name + ": Object not as expected" ) |
| 4576 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4577 | except Exception: |
| 4578 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4579 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4580 | |
| 4581 | def setTestSize( self, setName ): |
| 4582 | """ |
| 4583 | CLI command to get the elements in a distributed set. |
| 4584 | Required arguments: |
| 4585 | setName - The name of the set to remove from. |
| 4586 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4587 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4588 | None on error |
| 4589 | """ |
| 4590 | try: |
| 4591 | # TODO: Should this check against the number of elements returned |
| 4592 | # and then return true/false based on that? |
| 4593 | setName = str( setName ).strip() |
| 4594 | # Patterns to match |
| 4595 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4596 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4597 | setPattern |
| 4598 | cmdStr = "set-test-get -s " |
| 4599 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4600 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4601 | if output: |
| 4602 | match = re.search( pattern, output ) |
| 4603 | if match: |
| 4604 | setSize = int( match.group( 1 ) ) |
| 4605 | setMatch = match.group( 2 ) |
| 4606 | if len( setMatch.split() ) == setSize: |
| 4607 | main.log.info( "The size returned by " + self.name + |
| 4608 | " matches the number of elements in " + |
| 4609 | "the returned set" ) |
| 4610 | else: |
| 4611 | main.log.error( "The size returned by " + self.name + |
| 4612 | " does not match the number of " + |
| 4613 | "elements in the returned set." ) |
| 4614 | return setSize |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4615 | else: # no match |
| 4616 | main.log.error( self.name + ": setTestGet did not" + |
| 4617 | " match expected output" ) |
| 4618 | main.log.debug( self.name + " expected: " + pattern ) |
| 4619 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4620 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4621 | except TypeError: |
| 4622 | main.log.exception( self.name + ": Object not as expected" ) |
| 4623 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4624 | except Exception: |
| 4625 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4626 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4627 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4628 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4629 | """ |
| 4630 | Command to list the various counters in the system. |
| 4631 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4632 | if jsonFormat, a string of the json object returned by the cli |
| 4633 | command |
| 4634 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4635 | None on error |
| 4636 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4637 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4638 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4639 | if jsonFormat: |
| 4640 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4641 | output = self.sendline( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4642 | main.log.debug( self.name + ": Counters unparsed: " + output ) |
| 4643 | output = output.split( "\r\n" )[ -1 ] |
| 4644 | main.log.debug( self.name + ": Counters parsed: " + output ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4645 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4646 | assert "Command not found:" not in output, output |
| 4647 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4648 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4649 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4650 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4651 | main.log.exception( self.name + ": Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4652 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4653 | except TypeError: |
| 4654 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4655 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4656 | except pexpect.EOF: |
| 4657 | main.log.error( self.name + ": EOF exception found" ) |
| 4658 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4659 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4660 | except Exception: |
| 4661 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4662 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4663 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4664 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4665 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4666 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4667 | Required arguments: |
| 4668 | counter - The name of the counter to increment. |
| 4669 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4670 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4671 | returns: |
| 4672 | integer value of the counter or |
| 4673 | None on Error |
| 4674 | """ |
| 4675 | try: |
| 4676 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4677 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4678 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4679 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4680 | if delta != 1: |
| 4681 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4682 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4683 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4684 | match = re.search( pattern, output ) |
| 4685 | if match: |
| 4686 | return int( match.group( 1 ) ) |
| 4687 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4688 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4689 | " match expected output." ) |
| 4690 | main.log.debug( self.name + " expected: " + pattern ) |
| 4691 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4692 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4693 | except TypeError: |
| 4694 | main.log.exception( self.name + ": Object not as expected" ) |
| 4695 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4696 | except Exception: |
| 4697 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4698 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4699 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4700 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4701 | """ |
| 4702 | CLI command to get a distributed counter then add a delta to it. |
| 4703 | Required arguments: |
| 4704 | counter - The name of the counter to increment. |
| 4705 | Optional arguments: |
| 4706 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4707 | returns: |
| 4708 | integer value of the counter or |
| 4709 | None on Error |
| 4710 | """ |
| 4711 | try: |
| 4712 | counter = str( counter ) |
| 4713 | delta = int( delta ) |
| 4714 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4715 | cmdStr += counter |
| 4716 | if delta != 1: |
| 4717 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4718 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4719 | pattern = counter + " was updated to (-?\d+)" |
| 4720 | match = re.search( pattern, output ) |
| 4721 | if match: |
| 4722 | return int( match.group( 1 ) ) |
| 4723 | else: |
| 4724 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4725 | " match expected output." ) |
| 4726 | main.log.debug( self.name + " expected: " + pattern ) |
| 4727 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4728 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4729 | except TypeError: |
| 4730 | main.log.exception( self.name + ": Object not as expected" ) |
| 4731 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4732 | except Exception: |
| 4733 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4734 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4735 | |
| 4736 | def valueTestGet( self, valueName ): |
| 4737 | """ |
| 4738 | CLI command to get the value of an atomic value. |
| 4739 | Required arguments: |
| 4740 | valueName - The name of the value to get. |
| 4741 | returns: |
| 4742 | string value of the value or |
| 4743 | None on Error |
| 4744 | """ |
| 4745 | try: |
| 4746 | valueName = str( valueName ) |
| 4747 | cmdStr = "value-test " |
| 4748 | operation = "get" |
| 4749 | cmdStr = "value-test {} {}".format( valueName, |
| 4750 | operation ) |
| 4751 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4752 | main.log.debug( self.name + ": value test unparsed: " + output ) |
| 4753 | output = output.split( "\r\n" )[ -1 ] |
| 4754 | main.log.debug( self.name + ": value test parsed: " + output ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4755 | pattern = "(\w+)" |
| 4756 | match = re.search( pattern, output ) |
| 4757 | if match: |
| 4758 | return match.group( 1 ) |
| 4759 | else: |
| 4760 | main.log.error( self.name + ": valueTestGet did not" + |
| 4761 | " match expected output." ) |
| 4762 | main.log.debug( self.name + " expected: " + pattern ) |
| 4763 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4764 | return None |
| 4765 | except TypeError: |
| 4766 | main.log.exception( self.name + ": Object not as expected" ) |
| 4767 | return None |
| 4768 | except Exception: |
| 4769 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4770 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4771 | |
| 4772 | def valueTestSet( self, valueName, newValue ): |
| 4773 | """ |
| 4774 | CLI command to set the value of an atomic value. |
| 4775 | Required arguments: |
| 4776 | valueName - The name of the value to set. |
| 4777 | newValue - The value to assign to the given value. |
| 4778 | returns: |
| 4779 | main.TRUE on success or |
| 4780 | main.ERROR on Error |
| 4781 | """ |
| 4782 | try: |
| 4783 | valueName = str( valueName ) |
| 4784 | newValue = str( newValue ) |
| 4785 | operation = "set" |
| 4786 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4787 | operation, |
| 4788 | newValue ) |
| 4789 | output = self.distPrimitivesSend( cmdStr ) |
| 4790 | if output is not None: |
| 4791 | return main.TRUE |
| 4792 | else: |
| 4793 | return main.ERROR |
| 4794 | except TypeError: |
| 4795 | main.log.exception( self.name + ": Object not as expected" ) |
| 4796 | return main.ERROR |
| 4797 | except Exception: |
| 4798 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4799 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4800 | |
| 4801 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4802 | """ |
| 4803 | CLI command to compareAndSet the value of an atomic value. |
| 4804 | Required arguments: |
| 4805 | valueName - The name of the value. |
| 4806 | oldValue - Compare the current value of the atomic value to this |
| 4807 | newValue - If the value equals oldValue, set the value to newValue |
| 4808 | returns: |
| 4809 | main.TRUE on success or |
| 4810 | main.FALSE on failure or |
| 4811 | main.ERROR on Error |
| 4812 | """ |
| 4813 | try: |
| 4814 | valueName = str( valueName ) |
| 4815 | oldValue = str( oldValue ) |
| 4816 | newValue = str( newValue ) |
| 4817 | operation = "compareAndSet" |
| 4818 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4819 | operation, |
| 4820 | oldValue, |
| 4821 | newValue ) |
| 4822 | output = self.distPrimitivesSend( cmdStr ) |
| 4823 | pattern = "(\w+)" |
| 4824 | match = re.search( pattern, output ) |
| 4825 | if match: |
| 4826 | result = match.group( 1 ) |
| 4827 | if result == "true": |
| 4828 | return main.TRUE |
| 4829 | elif result == "false": |
| 4830 | return main.FALSE |
| 4831 | else: |
| 4832 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4833 | " match expected output." ) |
| 4834 | main.log.debug( self.name + " expected: " + pattern ) |
| 4835 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4836 | return main.ERROR |
| 4837 | except TypeError: |
| 4838 | main.log.exception( self.name + ": Object not as expected" ) |
| 4839 | return main.ERROR |
| 4840 | except Exception: |
| 4841 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4842 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4843 | |
| 4844 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4845 | """ |
| 4846 | CLI command to getAndSet the value of an atomic value. |
| 4847 | Required arguments: |
| 4848 | valueName - The name of the value to get. |
| 4849 | newValue - The value to assign to the given value |
| 4850 | returns: |
| 4851 | string value of the value or |
| 4852 | None on Error |
| 4853 | """ |
| 4854 | try: |
| 4855 | valueName = str( valueName ) |
| 4856 | cmdStr = "value-test " |
| 4857 | operation = "getAndSet" |
| 4858 | cmdStr += valueName + " " + operation |
| 4859 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4860 | operation, |
| 4861 | newValue ) |
| 4862 | output = self.distPrimitivesSend( cmdStr ) |
| 4863 | pattern = "(\w+)" |
| 4864 | match = re.search( pattern, output ) |
| 4865 | if match: |
| 4866 | return match.group( 1 ) |
| 4867 | else: |
| 4868 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4869 | " match expected output." ) |
| 4870 | main.log.debug( self.name + " expected: " + pattern ) |
| 4871 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4872 | return None |
| 4873 | except TypeError: |
| 4874 | main.log.exception( self.name + ": Object not as expected" ) |
| 4875 | return None |
| 4876 | except Exception: |
| 4877 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4878 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4879 | |
| 4880 | def valueTestDestroy( self, valueName ): |
| 4881 | """ |
| 4882 | CLI command to destroy an atomic value. |
| 4883 | Required arguments: |
| 4884 | valueName - The name of the value to destroy. |
| 4885 | returns: |
| 4886 | main.TRUE on success or |
| 4887 | main.ERROR on Error |
| 4888 | """ |
| 4889 | try: |
| 4890 | valueName = str( valueName ) |
| 4891 | cmdStr = "value-test " |
| 4892 | operation = "destroy" |
| 4893 | cmdStr += valueName + " " + operation |
| 4894 | output = self.distPrimitivesSend( cmdStr ) |
| 4895 | if output is not None: |
| 4896 | return main.TRUE |
| 4897 | else: |
| 4898 | return main.ERROR |
| 4899 | except TypeError: |
| 4900 | main.log.exception( self.name + ": Object not as expected" ) |
| 4901 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4902 | except Exception: |
| 4903 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4904 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4905 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4906 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4907 | """ |
| 4908 | Description: Execute summary command in onos |
| 4909 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4910 | no output |
| 4911 | |
| 4912 | """ |
| 4913 | try: |
| 4914 | cmdStr = "summary" |
| 4915 | if jsonFormat: |
| 4916 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4917 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4918 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4919 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4920 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 4921 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4922 | if not handle: |
| 4923 | main.log.error( self.name + ": There is no output in " + |
| 4924 | "summary command" ) |
| 4925 | return main.FALSE |
| 4926 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4927 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4928 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4929 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4930 | except TypeError: |
| 4931 | main.log.exception( self.name + ": Object not as expected" ) |
| 4932 | return None |
| 4933 | except pexpect.EOF: |
| 4934 | main.log.error( self.name + ": EOF exception found" ) |
| 4935 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4936 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4937 | except Exception: |
| 4938 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4939 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4940 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 4941 | def getAddress( self): |
| 4942 | """ |
| 4943 | Get the onos ip address from the cli. This is usefull when connecting using |
| 4944 | a container manager such as kubernetes. This function also sets self.address |
| 4945 | the value from ONOS. |
| 4946 | |
| 4947 | Returns: |
| 4948 | The string value of the key or |
| 4949 | None on Error |
| 4950 | """ |
| 4951 | try: |
| 4952 | output = self.summary() |
| 4953 | address = json.loads( output ).get( 'node' ) |
| 4954 | self.address = address |
| 4955 | return address |
You Wang | 327bad4 | 2021-03-24 14:19:58 -0700 | [diff] [blame] | 4956 | except ( TypeError, ValueError ): |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 4957 | main.log.exception( self.name + ": Object not as expected" ) |
| 4958 | return None |
| 4959 | except Exception: |
| 4960 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4961 | main.cleanAndExit() |
| 4962 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4963 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4964 | """ |
| 4965 | CLI command to get the value of a key in a consistent map using |
| 4966 | transactions. This a test function and can only get keys from the |
| 4967 | test map hard coded into the cli command |
| 4968 | Required arguments: |
| 4969 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4970 | returns: |
| 4971 | The string value of the key or |
| 4972 | None on Error |
| 4973 | """ |
| 4974 | try: |
| 4975 | keyName = str( keyName ) |
| 4976 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4977 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4978 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4979 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4980 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4981 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4982 | return None |
| 4983 | else: |
| 4984 | match = re.search( pattern, output ) |
| 4985 | if match: |
| 4986 | return match.groupdict()[ 'value' ] |
| 4987 | else: |
| 4988 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4989 | " match expected output." ) |
| 4990 | main.log.debug( self.name + " expected: " + pattern ) |
| 4991 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4992 | return None |
| 4993 | except TypeError: |
| 4994 | main.log.exception( self.name + ": Object not as expected" ) |
| 4995 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4996 | except Exception: |
| 4997 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4998 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4999 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 5000 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5001 | """ |
| 5002 | CLI command to put a value into 'numKeys' number of keys in a |
| 5003 | consistent map using transactions. This a test function and can only |
| 5004 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 5005 | Required arguments: |
| 5006 | numKeys - Number of keys to add the value to |
| 5007 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5008 | returns: |
| 5009 | A dictionary whose keys are the name of the keys put into the map |
| 5010 | and the values of the keys are dictionaries whose key-values are |
| 5011 | 'value': value put into map and optionaly |
| 5012 | 'oldValue': Previous value in the key or |
| 5013 | None on Error |
| 5014 | |
| 5015 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5016 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 5017 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5018 | """ |
| 5019 | try: |
| 5020 | numKeys = str( numKeys ) |
| 5021 | value = str( value ) |
| 5022 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5023 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 5024 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5025 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 5026 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 5027 | results = {} |
| 5028 | for line in output.splitlines(): |
| 5029 | new = re.search( newPattern, line ) |
| 5030 | updated = re.search( updatedPattern, line ) |
| 5031 | if new: |
| 5032 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 5033 | elif updated: |
| 5034 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5035 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5036 | else: |
| 5037 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 5038 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5039 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 5040 | newPattern, |
| 5041 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5042 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 5043 | return results |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 5044 | except ( TypeError, AttributeError ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5045 | main.log.exception( self.name + ": Object not as expected" ) |
| 5046 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5047 | except Exception: |
| 5048 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5049 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5050 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5051 | def maps( self, jsonFormat=True ): |
| 5052 | """ |
| 5053 | Description: Returns result of onos:maps |
| 5054 | Optional: |
| 5055 | * jsonFormat: enable json formatting of output |
| 5056 | """ |
| 5057 | try: |
| 5058 | cmdStr = "maps" |
| 5059 | if jsonFormat: |
| 5060 | cmdStr += " -j" |
| 5061 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5062 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5063 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5064 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5065 | except AssertionError: |
| 5066 | main.log.exception( "" ) |
| 5067 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5068 | except TypeError: |
| 5069 | main.log.exception( self.name + ": Object not as expected" ) |
| 5070 | return None |
| 5071 | except pexpect.EOF: |
| 5072 | main.log.error( self.name + ": EOF exception found" ) |
| 5073 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5074 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5075 | except Exception: |
| 5076 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5077 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5078 | |
| 5079 | def getSwController( self, uri, jsonFormat=True ): |
| 5080 | """ |
| 5081 | Descrition: Gets the controller information from the device |
| 5082 | """ |
| 5083 | try: |
| 5084 | cmd = "device-controllers " |
| 5085 | if jsonFormat: |
| 5086 | cmd += "-j " |
| 5087 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5088 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5089 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5090 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5091 | except AssertionError: |
| 5092 | main.log.exception( "" ) |
| 5093 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5094 | except TypeError: |
| 5095 | main.log.exception( self.name + ": Object not as expected" ) |
| 5096 | return None |
| 5097 | except pexpect.EOF: |
| 5098 | main.log.error( self.name + ": EOF exception found" ) |
| 5099 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5100 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5101 | except Exception: |
| 5102 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5103 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5104 | |
| 5105 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 5106 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5107 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5108 | |
| 5109 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5110 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5111 | ip - String or List: The ip address of the controller. |
| 5112 | This parameter can be formed in a couple of different ways. |
| 5113 | VALID: |
| 5114 | 10.0.0.1 - just the ip address |
| 5115 | tcp:10.0.0.1 - the protocol and the ip address |
| 5116 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 5117 | so that you can add controllers with different |
| 5118 | protocols and ports |
| 5119 | INVALID: |
| 5120 | 10.0.0.1:6653 - this is not supported by ONOS |
| 5121 | |
| 5122 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 5123 | port - The port number. |
| 5124 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 5125 | |
| 5126 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 5127 | """ |
| 5128 | try: |
| 5129 | cmd = "device-setcontrollers" |
| 5130 | |
| 5131 | if jsonFormat: |
| 5132 | cmd += " -j" |
| 5133 | cmd += " " + uri |
| 5134 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5135 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5136 | for item in ip: |
| 5137 | if ":" in item: |
| 5138 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5139 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5140 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5141 | elif "." in sitem[ 1 ]: |
| 5142 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5143 | else: |
| 5144 | main.log.error( "Malformed entry: " + item ) |
| 5145 | raise TypeError |
| 5146 | else: |
| 5147 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5148 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5149 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5150 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5151 | if "Error" in response: |
| 5152 | main.log.error( response ) |
| 5153 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5154 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5155 | except AssertionError: |
| 5156 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5157 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5158 | except TypeError: |
| 5159 | main.log.exception( self.name + ": Object not as expected" ) |
| 5160 | return main.FALSE |
| 5161 | except pexpect.EOF: |
| 5162 | main.log.error( self.name + ": EOF exception found" ) |
| 5163 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5164 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5165 | except Exception: |
| 5166 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5167 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5168 | |
| 5169 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5170 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5171 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5172 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5173 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5174 | 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] | 5175 | Returns: |
| 5176 | Returns main.FALSE if an exception is thrown or an error is present |
| 5177 | in the response. Otherwise, returns main.TRUE. |
| 5178 | NOTE: |
| 5179 | 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] | 5180 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5181 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5182 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 5183 | deviceStr = device |
| 5184 | device = [] |
| 5185 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5186 | |
| 5187 | for d in device: |
| 5188 | time.sleep( 1 ) |
| 5189 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5190 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5191 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5192 | if "Error" in response: |
| 5193 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 5194 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5195 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5196 | except AssertionError: |
| 5197 | main.log.exception( "" ) |
| 5198 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5199 | except TypeError: |
| 5200 | main.log.exception( self.name + ": Object not as expected" ) |
| 5201 | return main.FALSE |
| 5202 | except pexpect.EOF: |
| 5203 | main.log.error( self.name + ": EOF exception found" ) |
| 5204 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5205 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5206 | except Exception: |
| 5207 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5208 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5209 | |
| 5210 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5211 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5212 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5213 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5214 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5215 | 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] | 5216 | Returns: |
| 5217 | Returns main.FALSE if an exception is thrown or an error is present |
| 5218 | in the response. Otherwise, returns main.TRUE. |
| 5219 | NOTE: |
| 5220 | 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] | 5221 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5222 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5223 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5224 | host = list( host ) |
| 5225 | |
| 5226 | for h in host: |
| 5227 | time.sleep( 1 ) |
| 5228 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5229 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5230 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5231 | if "Error" in response: |
| 5232 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5233 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5234 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5235 | except AssertionError: |
| 5236 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5237 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5238 | except TypeError: |
| 5239 | main.log.exception( self.name + ": Object not as expected" ) |
| 5240 | return main.FALSE |
| 5241 | except pexpect.EOF: |
| 5242 | main.log.error( self.name + ": EOF exception found" ) |
| 5243 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5244 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5245 | except Exception: |
| 5246 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5247 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5248 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5249 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5250 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5251 | Description: |
| 5252 | Bring link down or up in the null-provider. |
| 5253 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5254 | begin - (string) One end of a device or switch. |
| 5255 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5256 | returns: |
| 5257 | main.TRUE if no exceptions were thrown and no Errors are |
| 5258 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5259 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5260 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5261 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5262 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5263 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5264 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5265 | if "Error" in response or "Failure" in response: |
| 5266 | main.log.error( response ) |
| 5267 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5268 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5269 | except AssertionError: |
| 5270 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5271 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5272 | except TypeError: |
| 5273 | main.log.exception( self.name + ": Object not as expected" ) |
| 5274 | return main.FALSE |
| 5275 | except pexpect.EOF: |
| 5276 | main.log.error( self.name + ": EOF exception found" ) |
| 5277 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5278 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5279 | except Exception: |
| 5280 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5281 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5282 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5283 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5284 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5285 | Description: |
| 5286 | Changes the state of port in an OF switch by means of the |
| 5287 | PORTSTATUS OF messages. |
| 5288 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5289 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5290 | port - (string) target port in the device. Ex: '2' |
| 5291 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5292 | returns: |
| 5293 | main.TRUE if no exceptions were thrown and no Errors are |
| 5294 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5295 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5296 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5297 | state = state.lower() |
| 5298 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5299 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5300 | response = self.sendline( cmd, showResponse=True ) |
| 5301 | assert response is not None, "Error in sendline" |
| 5302 | assert "Command not found:" not in response, response |
| 5303 | if "Error" in response or "Failure" in response: |
| 5304 | main.log.error( response ) |
| 5305 | return main.FALSE |
| 5306 | return main.TRUE |
| 5307 | except AssertionError: |
| 5308 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5309 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5310 | except TypeError: |
| 5311 | main.log.exception( self.name + ": Object not as expected" ) |
| 5312 | return main.FALSE |
| 5313 | except pexpect.EOF: |
| 5314 | main.log.error( self.name + ": EOF exception found" ) |
| 5315 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5316 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5317 | except Exception: |
| 5318 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5319 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5320 | |
| 5321 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5322 | """ |
| 5323 | Set the logging level to lvl for a specific app |
| 5324 | returns main.TRUE on success |
| 5325 | returns main.FALSE if Error occurred |
| 5326 | if noExit is True, TestON will not exit, but clean up |
| 5327 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5328 | Level defaults to INFO |
| 5329 | """ |
| 5330 | try: |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 5331 | handle = self.sendline( "log:set %s %s" % ( level, app ) ) |
| 5332 | assert handle is not None, "Error in sendline" |
| 5333 | assert "Command not found:" not in handle, handle |
| 5334 | if re.search( "Error", handle ): |
| 5335 | main.log.error( self.name + ": Error in setting log level" ) |
| 5336 | main.log.error( handle ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5337 | return main.FALSE |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 5338 | else: |
| 5339 | return main.TRUE |
| 5340 | except AssertionError: |
| 5341 | main.log.exception( "" ) |
| 5342 | return None |
| 5343 | except TypeError: |
| 5344 | main.log.exception( self.name + ": Object not as expected" ) |
| 5345 | return None |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5346 | except pexpect.EOF: |
| 5347 | main.log.error( self.name + ": EOF exception found" ) |
| 5348 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5349 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5350 | except Exception: |
| 5351 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5352 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5353 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 5354 | def logList( self, saveValues=True ): |
| 5355 | """ |
| 5356 | Gets the current log levels and optionally saves them |
| 5357 | returns a dict of the log levels or |
| 5358 | returns main.FALSE if Error occurred |
| 5359 | """ |
| 5360 | try: |
| 5361 | self.handle.sendline( "log:list" ) |
| 5362 | self.handle.expect( self.karafPrompt ) |
| 5363 | |
| 5364 | response = self.handle.before |
| 5365 | logLevels = {} |
| 5366 | for line in response.splitlines(): |
| 5367 | parsed = line.split('│') |
| 5368 | logger = parsed[0].strip() |
| 5369 | if len( parsed ) != 2 or 'Level' in parsed[1] or logger[0] == '─': |
| 5370 | continue |
| 5371 | level = parsed[1].strip() |
| 5372 | logLevels[ logger ] = level |
| 5373 | if saveValues: |
| 5374 | self.logLevels = logLevels |
| 5375 | return logLevels |
| 5376 | except pexpect.TIMEOUT: |
| 5377 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 5378 | main.cleanAndExit() |
| 5379 | except pexpect.EOF: |
| 5380 | main.log.error( self.name + ": EOF exception found" ) |
| 5381 | main.log.error( self.name + ": " + self.handle.before ) |
| 5382 | main.cleanAndExit() |
| 5383 | except Exception: |
| 5384 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5385 | main.cleanAndExit() |
| 5386 | |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5387 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5388 | """ |
| 5389 | Return a dictionary which describes the latest network topology data as a |
| 5390 | graph. |
| 5391 | An example of the dictionary: |
| 5392 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5393 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5394 | Each vertex should at least have an 'edges' attribute which describes the |
| 5395 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5396 | 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] | 5397 | list of attributes. |
| 5398 | An example of the edges dictionary: |
| 5399 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5400 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5401 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5402 | in topology data. |
| 5403 | """ |
| 5404 | graphDict = {} |
| 5405 | try: |
| 5406 | links = self.links() |
| 5407 | links = json.loads( links ) |
| 5408 | devices = self.devices() |
| 5409 | devices = json.loads( devices ) |
| 5410 | idToDevice = {} |
| 5411 | for device in devices: |
| 5412 | idToDevice[ device[ 'id' ] ] = device |
| 5413 | if includeHost: |
| 5414 | hosts = self.hosts() |
| 5415 | # FIXME: support 'includeHost' argument |
| 5416 | for link in links: |
| 5417 | nodeA = link[ 'src' ][ 'device' ] |
| 5418 | nodeB = link[ 'dst' ][ 'device' ] |
| 5419 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5420 | if nodeA not in graphDict.keys(): |
| 5421 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5422 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5423 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5424 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5425 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5426 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5427 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5428 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5429 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5430 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5431 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5432 | else: |
| 5433 | # Assert nodeB is not connected to any current links of nodeA |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 5434 | # assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 5435 | pass |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5436 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5437 | 'type': link[ 'type' ], |
| 5438 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5439 | return graphDict |
| 5440 | except ( TypeError, ValueError ): |
| 5441 | main.log.exception( self.name + ": Object not as expected" ) |
| 5442 | return None |
| 5443 | except KeyError: |
| 5444 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5445 | return None |
| 5446 | except AssertionError: |
| 5447 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5448 | return None |
| 5449 | except pexpect.EOF: |
| 5450 | main.log.error( self.name + ": EOF exception found" ) |
| 5451 | main.log.error( self.name + ": " + self.handle.before ) |
| 5452 | return None |
| 5453 | except Exception: |
| 5454 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5455 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5456 | |
| 5457 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5458 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5459 | Send command to check intent-perf summary |
| 5460 | Returns: dictionary for intent-perf summary |
| 5461 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5462 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5463 | cmd = "intent-perf -s" |
| 5464 | respDic = {} |
| 5465 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5466 | assert resp is not None, "Error in sendline" |
| 5467 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5468 | try: |
| 5469 | # Generate the dictionary to return |
| 5470 | for l in resp.split( "\n" ): |
| 5471 | # Delete any white space in line |
| 5472 | temp = re.sub( r'\s+', '', l ) |
| 5473 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5474 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5475 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5476 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5477 | main.log.exception( self.name + ": Object not as expected" ) |
| 5478 | return None |
| 5479 | except KeyError: |
| 5480 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5481 | return None |
| 5482 | except AssertionError: |
| 5483 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5484 | return None |
| 5485 | except pexpect.EOF: |
| 5486 | main.log.error( self.name + ": EOF exception found" ) |
| 5487 | main.log.error( self.name + ": " + self.handle.before ) |
| 5488 | return None |
| 5489 | except Exception: |
| 5490 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5491 | return None |
| 5492 | return respDic |
| 5493 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5494 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5495 | """ |
| 5496 | Searches the latest ONOS log file for the given search term and |
| 5497 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5498 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5499 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5500 | searchTerm: |
| 5501 | The string to grep from the ONOS log. |
| 5502 | startLine: |
| 5503 | The term that decides which line is the start to search the searchTerm in |
| 5504 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5505 | logNum: |
| 5506 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5507 | information.Because of this, search mutiply logs is necessary to capture |
| 5508 | the right result. logNum is the number of karaf logs that we need to search |
| 5509 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5510 | mode: |
| 5511 | all: return all the strings that contain the search term |
| 5512 | last: return the last string that contains the search term |
| 5513 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5514 | num: return the number of times that the searchTerm appears in the log |
| 5515 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5516 | """ |
| 5517 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5518 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5519 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5520 | logPath = '/opt/onos/log/karaf.log.' |
| 5521 | logPaths = '/opt/onos/log/karaf.log' |
| 5522 | for i in range( 1, logNum ): |
| 5523 | logPaths = logPath + str( i ) + " " + logPaths |
| 5524 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5525 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5526 | # 100000000 is just a extreme large number to make sure this function can |
| 5527 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5528 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5529 | if mode == 'all': |
| 5530 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5531 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5532 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5533 | elif mode == 'first': |
| 5534 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5535 | elif mode == 'num': |
You Wang | d91a70f | 2019-01-03 15:28:10 -0800 | [diff] [blame] | 5536 | cmd = cmd + " | grep \'" + searchTerm + "\' | wc -l" |
| 5537 | num = self.lineCount( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5538 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5539 | elif mode == 'total': |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 5540 | totalLines = self.lineCount( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5541 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5542 | else: |
| 5543 | main.log.error( self.name + " unsupported mode" ) |
| 5544 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5545 | before = self.sendline( cmd ) |
| 5546 | before = before.splitlines() |
| 5547 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5548 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5549 | return returnLines |
| 5550 | except AssertionError: |
| 5551 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5552 | return None |
| 5553 | except pexpect.EOF: |
| 5554 | main.log.error( self.name + ": EOF exception found" ) |
| 5555 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5556 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5557 | except pexpect.TIMEOUT: |
| 5558 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5559 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5560 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5561 | except Exception: |
| 5562 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5563 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5564 | |
| 5565 | def vplsShow( self, jsonFormat=True ): |
| 5566 | """ |
| 5567 | Description: Returns result of onos:vpls show, which should list the |
| 5568 | configured VPLS networks and the assigned interfaces. |
| 5569 | Optional: |
| 5570 | * jsonFormat: enable json formatting of output |
| 5571 | Returns: |
| 5572 | The output of the command or None on error. |
| 5573 | """ |
| 5574 | try: |
| 5575 | cmdStr = "vpls show" |
| 5576 | if jsonFormat: |
| 5577 | raise NotImplementedError |
| 5578 | cmdStr += " -j" |
| 5579 | handle = self.sendline( cmdStr ) |
| 5580 | assert handle is not None, "Error in sendline" |
| 5581 | assert "Command not found:" not in handle, handle |
| 5582 | return handle |
| 5583 | except AssertionError: |
| 5584 | main.log.exception( "" ) |
| 5585 | return None |
| 5586 | except TypeError: |
| 5587 | main.log.exception( self.name + ": Object not as expected" ) |
| 5588 | return None |
| 5589 | except pexpect.EOF: |
| 5590 | main.log.error( self.name + ": EOF exception found" ) |
| 5591 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5592 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5593 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5594 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5595 | return None |
| 5596 | except Exception: |
| 5597 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5598 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5599 | |
| 5600 | def parseVplsShow( self ): |
| 5601 | """ |
| 5602 | Parse the cli output of 'vpls show' into json output. This is required |
| 5603 | as there is currently no json output available. |
| 5604 | """ |
| 5605 | try: |
| 5606 | output = [] |
| 5607 | raw = self.vplsShow( jsonFormat=False ) |
| 5608 | namePat = "VPLS name: (?P<name>\w+)" |
| 5609 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5610 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5611 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5612 | mIter = re.finditer( pattern, raw ) |
| 5613 | for match in mIter: |
| 5614 | item = {} |
| 5615 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5616 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5617 | if ifaces == [ "" ]: |
| 5618 | ifaces = [] |
| 5619 | item[ 'interfaces' ] = ifaces |
| 5620 | encap = match.group( 'encap' ) |
| 5621 | if encap != 'NONE': |
| 5622 | item[ 'encapsulation' ] = encap.lower() |
| 5623 | output.append( item ) |
| 5624 | return output |
| 5625 | except Exception: |
| 5626 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5627 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5628 | |
| 5629 | def vplsList( self, jsonFormat=True ): |
| 5630 | """ |
| 5631 | Description: Returns result of onos:vpls list, which should list the |
| 5632 | configured VPLS networks. |
| 5633 | Optional: |
| 5634 | * jsonFormat: enable json formatting of output |
| 5635 | """ |
| 5636 | try: |
| 5637 | cmdStr = "vpls list" |
| 5638 | if jsonFormat: |
| 5639 | raise NotImplementedError |
| 5640 | cmdStr += " -j" |
| 5641 | handle = self.sendline( cmdStr ) |
| 5642 | assert handle is not None, "Error in sendline" |
| 5643 | assert "Command not found:" not in handle, handle |
| 5644 | return handle |
| 5645 | except AssertionError: |
| 5646 | main.log.exception( "" ) |
| 5647 | return None |
| 5648 | except TypeError: |
| 5649 | main.log.exception( self.name + ": Object not as expected" ) |
| 5650 | return None |
| 5651 | except pexpect.EOF: |
| 5652 | main.log.error( self.name + ": EOF exception found" ) |
| 5653 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5654 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5655 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5656 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5657 | return None |
| 5658 | except Exception: |
| 5659 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5660 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5661 | |
| 5662 | def vplsCreate( self, network ): |
| 5663 | """ |
| 5664 | CLI command to create a new VPLS network. |
| 5665 | Required arguments: |
| 5666 | network - String name of the network to create. |
| 5667 | returns: |
| 5668 | main.TRUE on success and main.FALSE on failure |
| 5669 | """ |
| 5670 | try: |
| 5671 | network = str( network ) |
| 5672 | cmdStr = "vpls create " |
| 5673 | cmdStr += network |
| 5674 | output = self.sendline( cmdStr ) |
| 5675 | assert output is not None, "Error in sendline" |
| 5676 | assert "Command not found:" not in output, output |
| 5677 | assert "Error executing command" not in output, output |
| 5678 | assert "VPLS already exists:" not in output, output |
| 5679 | return main.TRUE |
| 5680 | except AssertionError: |
| 5681 | main.log.exception( "" ) |
| 5682 | return main.FALSE |
| 5683 | except TypeError: |
| 5684 | main.log.exception( self.name + ": Object not as expected" ) |
| 5685 | return main.FALSE |
| 5686 | except pexpect.EOF: |
| 5687 | main.log.error( self.name + ": EOF exception found" ) |
| 5688 | main.log.error( self.name + ": " + self.handle.before ) |
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 | except Exception: |
| 5691 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5692 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5693 | |
| 5694 | def vplsDelete( self, network ): |
| 5695 | """ |
| 5696 | CLI command to delete a VPLS network. |
| 5697 | Required arguments: |
| 5698 | network - Name of the network to delete. |
| 5699 | returns: |
| 5700 | main.TRUE on success and main.FALSE on failure |
| 5701 | """ |
| 5702 | try: |
| 5703 | network = str( network ) |
| 5704 | cmdStr = "vpls delete " |
| 5705 | cmdStr += network |
| 5706 | output = self.sendline( cmdStr ) |
| 5707 | assert output is not None, "Error in sendline" |
| 5708 | assert "Command not found:" not in output, output |
| 5709 | assert "Error executing command" not in output, output |
| 5710 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5711 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5712 | return main.TRUE |
| 5713 | except AssertionError: |
| 5714 | main.log.exception( "" ) |
| 5715 | return main.FALSE |
| 5716 | except TypeError: |
| 5717 | main.log.exception( self.name + ": Object not as expected" ) |
| 5718 | return main.FALSE |
| 5719 | except pexpect.EOF: |
| 5720 | main.log.error( self.name + ": EOF exception found" ) |
| 5721 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5722 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5723 | except Exception: |
| 5724 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5725 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5726 | |
| 5727 | def vplsAddIface( self, network, iface ): |
| 5728 | """ |
| 5729 | CLI command to add an interface to a VPLS network. |
| 5730 | Required arguments: |
| 5731 | network - Name of the network to add the interface to. |
| 5732 | iface - The ONOS name for an interface. |
| 5733 | returns: |
| 5734 | main.TRUE on success and main.FALSE on failure |
| 5735 | """ |
| 5736 | try: |
| 5737 | network = str( network ) |
| 5738 | iface = str( iface ) |
| 5739 | cmdStr = "vpls add-if " |
| 5740 | cmdStr += network + " " + iface |
| 5741 | output = self.sendline( cmdStr ) |
| 5742 | assert output is not None, "Error in sendline" |
| 5743 | assert "Command not found:" not in output, output |
| 5744 | assert "Error executing command" not in output, output |
| 5745 | assert "already associated to network" not in output, output |
| 5746 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5747 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5748 | return main.TRUE |
| 5749 | except AssertionError: |
| 5750 | main.log.exception( "" ) |
| 5751 | return main.FALSE |
| 5752 | except TypeError: |
| 5753 | main.log.exception( self.name + ": Object not as expected" ) |
| 5754 | return main.FALSE |
| 5755 | except pexpect.EOF: |
| 5756 | main.log.error( self.name + ": EOF exception found" ) |
| 5757 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5758 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5759 | except Exception: |
| 5760 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5761 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5762 | |
| 5763 | def vplsRemIface( self, network, iface ): |
| 5764 | """ |
| 5765 | CLI command to remove an interface from a VPLS network. |
| 5766 | Required arguments: |
| 5767 | network - Name of the network to remove the interface from. |
| 5768 | iface - Name of the interface to remove. |
| 5769 | returns: |
| 5770 | main.TRUE on success and main.FALSE on failure |
| 5771 | """ |
| 5772 | try: |
| 5773 | iface = str( iface ) |
| 5774 | cmdStr = "vpls rem-if " |
| 5775 | cmdStr += network + " " + iface |
| 5776 | output = self.sendline( cmdStr ) |
| 5777 | assert output is not None, "Error in sendline" |
| 5778 | assert "Command not found:" not in output, output |
| 5779 | assert "Error executing command" not in output, output |
| 5780 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5781 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5782 | return main.TRUE |
| 5783 | except AssertionError: |
| 5784 | main.log.exception( "" ) |
| 5785 | return main.FALSE |
| 5786 | except TypeError: |
| 5787 | main.log.exception( self.name + ": Object not as expected" ) |
| 5788 | return main.FALSE |
| 5789 | except pexpect.EOF: |
| 5790 | main.log.error( self.name + ": EOF exception found" ) |
| 5791 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5792 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5793 | except Exception: |
| 5794 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5795 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5796 | |
| 5797 | def vplsClean( self ): |
| 5798 | """ |
| 5799 | Description: Clears the VPLS app configuration. |
| 5800 | Returns: main.TRUE on success and main.FALSE on failure |
| 5801 | """ |
| 5802 | try: |
| 5803 | cmdStr = "vpls clean" |
| 5804 | handle = self.sendline( cmdStr ) |
| 5805 | assert handle is not None, "Error in sendline" |
| 5806 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5807 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5808 | return handle |
| 5809 | except AssertionError: |
| 5810 | main.log.exception( "" ) |
| 5811 | return main.FALSE |
| 5812 | except TypeError: |
| 5813 | main.log.exception( self.name + ": Object not as expected" ) |
| 5814 | return main.FALSE |
| 5815 | except pexpect.EOF: |
| 5816 | main.log.error( self.name + ": EOF exception found" ) |
| 5817 | main.log.error( self.name + ": " + self.handle.before ) |
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 | except Exception: |
| 5820 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5821 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5822 | |
| 5823 | def vplsSetEncap( self, network, encapType ): |
| 5824 | """ |
| 5825 | CLI command to add an interface to a VPLS network. |
| 5826 | Required arguments: |
| 5827 | network - Name of the network to create. |
| 5828 | encapType - Type of encapsulation. |
| 5829 | returns: |
| 5830 | main.TRUE on success and main.FALSE on failure |
| 5831 | """ |
| 5832 | try: |
| 5833 | network = str( network ) |
| 5834 | encapType = str( encapType ).upper() |
| 5835 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5836 | cmdStr = "vpls set-encap " |
| 5837 | cmdStr += network + " " + encapType |
| 5838 | output = self.sendline( cmdStr ) |
| 5839 | assert output is not None, "Error in sendline" |
| 5840 | assert "Command not found:" not in output, output |
| 5841 | assert "Error executing command" not in output, output |
| 5842 | assert "already associated to network" not in output, output |
| 5843 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5844 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5845 | return main.TRUE |
| 5846 | except AssertionError: |
| 5847 | main.log.exception( "" ) |
| 5848 | return main.FALSE |
| 5849 | except TypeError: |
| 5850 | main.log.exception( self.name + ": Object not as expected" ) |
| 5851 | return main.FALSE |
| 5852 | except pexpect.EOF: |
| 5853 | main.log.error( self.name + ": EOF exception found" ) |
| 5854 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5855 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5856 | except Exception: |
| 5857 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5858 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5859 | |
| 5860 | def interfaces( self, jsonFormat=True ): |
| 5861 | """ |
| 5862 | Description: Returns result of interfaces command. |
| 5863 | Optional: |
| 5864 | * jsonFormat: enable json formatting of output |
| 5865 | Returns: |
| 5866 | The output of the command or None on error. |
| 5867 | """ |
| 5868 | try: |
| 5869 | cmdStr = "interfaces" |
| 5870 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5871 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5872 | cmdStr += " -j" |
| 5873 | handle = self.sendline( cmdStr ) |
| 5874 | assert handle is not None, "Error in sendline" |
| 5875 | assert "Command not found:" not in handle, handle |
| 5876 | return handle |
| 5877 | except AssertionError: |
| 5878 | main.log.exception( "" ) |
| 5879 | return None |
| 5880 | except TypeError: |
| 5881 | main.log.exception( self.name + ": Object not as expected" ) |
| 5882 | return None |
| 5883 | except pexpect.EOF: |
| 5884 | main.log.error( self.name + ": EOF exception found" ) |
| 5885 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5886 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5887 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5888 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5889 | return None |
| 5890 | except Exception: |
| 5891 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5892 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5893 | |
| 5894 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5895 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5896 | Get the timestamp of searchTerm from karaf log. |
| 5897 | |
| 5898 | Arguments: |
| 5899 | splitTerm_before and splitTerm_after: |
| 5900 | |
| 5901 | The terms that split the string that contains the timeStamp of |
| 5902 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5903 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5904 | and the splitTerm_after is "x" |
| 5905 | |
| 5906 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5907 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5908 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5909 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5910 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5911 | return main.ERROR |
| 5912 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5913 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5914 | main.log.warn( "Captured timestamp string is empty" ) |
| 5915 | return main.ERROR |
| 5916 | lines = lines[ 0 ] |
| 5917 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5918 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5919 | # get the target value |
| 5920 | line = lines.split( splitTerm_before ) |
| 5921 | key = line[ 1 ].split( splitTerm_after ) |
| 5922 | return int( key[ 0 ] ) |
| 5923 | except IndexError: |
| 5924 | main.log.warn( "Index Error!" ) |
| 5925 | return main.ERROR |
| 5926 | except AssertionError: |
| 5927 | main.log.warn( "Search Term Not Found " ) |
| 5928 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5929 | |
| 5930 | def workQueueAdd( self, queueName, value ): |
| 5931 | """ |
| 5932 | CLI command to add a string to the specified Work Queue. |
| 5933 | This function uses the distributed primitives test app, which |
| 5934 | gives some cli access to distributed primitives for testing |
| 5935 | purposes only. |
| 5936 | |
| 5937 | Required arguments: |
| 5938 | queueName - The name of the queue to add to |
| 5939 | value - The value to add to the queue |
| 5940 | returns: |
| 5941 | main.TRUE on success, main.FALSE on failure and |
| 5942 | main.ERROR on error. |
| 5943 | """ |
| 5944 | try: |
| 5945 | queueName = str( queueName ) |
| 5946 | value = str( value ) |
| 5947 | prefix = "work-queue-test" |
| 5948 | operation = "add" |
| 5949 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5950 | output = self.distPrimitivesSend( cmdStr ) |
| 5951 | if "Invalid operation name" in output: |
| 5952 | main.log.warn( output ) |
| 5953 | return main.ERROR |
| 5954 | elif "Done" in output: |
| 5955 | return main.TRUE |
| 5956 | except TypeError: |
| 5957 | main.log.exception( self.name + ": Object not as expected" ) |
| 5958 | return main.ERROR |
| 5959 | except Exception: |
| 5960 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5961 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5962 | |
| 5963 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5964 | """ |
| 5965 | CLI command to add two strings to the specified Work Queue. |
| 5966 | This function uses the distributed primitives test app, which |
| 5967 | gives some cli access to distributed primitives for testing |
| 5968 | purposes only. |
| 5969 | |
| 5970 | Required arguments: |
| 5971 | queueName - The name of the queue to add to |
| 5972 | value1 - The first value to add to the queue |
| 5973 | value2 - The second value to add to the queue |
| 5974 | returns: |
| 5975 | main.TRUE on success, main.FALSE on failure and |
| 5976 | main.ERROR on error. |
| 5977 | """ |
| 5978 | try: |
| 5979 | queueName = str( queueName ) |
| 5980 | value1 = str( value1 ) |
| 5981 | value2 = str( value2 ) |
| 5982 | prefix = "work-queue-test" |
| 5983 | operation = "addMultiple" |
| 5984 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 5985 | output = self.distPrimitivesSend( cmdStr ) |
| 5986 | if "Invalid operation name" in output: |
| 5987 | main.log.warn( output ) |
| 5988 | return main.ERROR |
| 5989 | elif "Done" in output: |
| 5990 | return main.TRUE |
| 5991 | except TypeError: |
| 5992 | main.log.exception( self.name + ": Object not as expected" ) |
| 5993 | return main.ERROR |
| 5994 | except Exception: |
| 5995 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5996 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5997 | |
| 5998 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 5999 | """ |
| 6000 | CLI command to take a value from the specified Work Queue and compelte it. |
| 6001 | This function uses the distributed primitives test app, which |
| 6002 | gives some cli access to distributed primitives for testing |
| 6003 | purposes only. |
| 6004 | |
| 6005 | Required arguments: |
| 6006 | queueName - The name of the queue to add to |
| 6007 | number - The number of items to take and complete |
| 6008 | returns: |
| 6009 | main.TRUE on success, main.FALSE on failure and |
| 6010 | main.ERROR on error. |
| 6011 | """ |
| 6012 | try: |
| 6013 | queueName = str( queueName ) |
| 6014 | number = str( int( number ) ) |
| 6015 | prefix = "work-queue-test" |
| 6016 | operation = "takeAndComplete" |
| 6017 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 6018 | output = self.distPrimitivesSend( cmdStr ) |
| 6019 | if "Invalid operation name" in output: |
| 6020 | main.log.warn( output ) |
| 6021 | return main.ERROR |
| 6022 | elif "Done" in output: |
| 6023 | return main.TRUE |
| 6024 | except TypeError: |
| 6025 | main.log.exception( self.name + ": Object not as expected" ) |
| 6026 | return main.ERROR |
| 6027 | except Exception: |
| 6028 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6029 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6030 | |
| 6031 | def workQueueDestroy( self, queueName ): |
| 6032 | """ |
| 6033 | CLI command to destroy the specified Work Queue. |
| 6034 | This function uses the distributed primitives test app, which |
| 6035 | gives some cli access to distributed primitives for testing |
| 6036 | purposes only. |
| 6037 | |
| 6038 | Required arguments: |
| 6039 | queueName - The name of the queue to add to |
| 6040 | returns: |
| 6041 | main.TRUE on success, main.FALSE on failure and |
| 6042 | main.ERROR on error. |
| 6043 | """ |
| 6044 | try: |
| 6045 | queueName = str( queueName ) |
| 6046 | prefix = "work-queue-test" |
| 6047 | operation = "destroy" |
| 6048 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6049 | output = self.distPrimitivesSend( cmdStr ) |
| 6050 | if "Invalid operation name" in output: |
| 6051 | main.log.warn( output ) |
| 6052 | return main.ERROR |
| 6053 | return main.TRUE |
| 6054 | except TypeError: |
| 6055 | main.log.exception( self.name + ": Object not as expected" ) |
| 6056 | return main.ERROR |
| 6057 | except Exception: |
| 6058 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6059 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6060 | |
| 6061 | def workQueueTotalPending( self, queueName ): |
| 6062 | """ |
| 6063 | CLI command to get the Total Pending items of the specified Work Queue. |
| 6064 | This function uses the distributed primitives test app, which |
| 6065 | gives some cli access to distributed primitives for testing |
| 6066 | purposes only. |
| 6067 | |
| 6068 | Required arguments: |
| 6069 | queueName - The name of the queue to add to |
| 6070 | returns: |
| 6071 | The number of Pending items in the specified work queue or |
| 6072 | None on error |
| 6073 | """ |
| 6074 | try: |
| 6075 | queueName = str( queueName ) |
| 6076 | prefix = "work-queue-test" |
| 6077 | operation = "totalPending" |
| 6078 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6079 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6080 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6081 | output = output.split( "\r\n" )[ -1 ] |
| 6082 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6083 | pattern = r'\d+' |
| 6084 | if "Invalid operation name" in output: |
| 6085 | main.log.warn( output ) |
| 6086 | return None |
| 6087 | else: |
| 6088 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6089 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6090 | except ( AttributeError, TypeError ): |
| 6091 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6092 | return None |
| 6093 | except Exception: |
| 6094 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6095 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6096 | |
| 6097 | def workQueueTotalCompleted( self, queueName ): |
| 6098 | """ |
| 6099 | CLI command to get the Total Completed items of the specified Work Queue. |
| 6100 | This function uses the distributed primitives test app, which |
| 6101 | gives some cli access to distributed primitives for testing |
| 6102 | purposes only. |
| 6103 | |
| 6104 | Required arguments: |
| 6105 | queueName - The name of the queue to add to |
| 6106 | returns: |
| 6107 | The number of complete items in the specified work queue or |
| 6108 | None on error |
| 6109 | """ |
| 6110 | try: |
| 6111 | queueName = str( queueName ) |
| 6112 | prefix = "work-queue-test" |
| 6113 | operation = "totalCompleted" |
| 6114 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6115 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6116 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6117 | output = output.split( "\r\n" )[ -1 ] |
| 6118 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6119 | pattern = r'\d+' |
| 6120 | if "Invalid operation name" in output: |
| 6121 | main.log.warn( output ) |
| 6122 | return None |
| 6123 | else: |
| 6124 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6125 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6126 | except ( AttributeError, TypeError ): |
| 6127 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6128 | return None |
| 6129 | except Exception: |
| 6130 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6131 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6132 | |
| 6133 | def workQueueTotalInProgress( self, queueName ): |
| 6134 | """ |
| 6135 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 6136 | This function uses the distributed primitives test app, which |
| 6137 | gives some cli access to distributed primitives for testing |
| 6138 | purposes only. |
| 6139 | |
| 6140 | Required arguments: |
| 6141 | queueName - The name of the queue to add to |
| 6142 | returns: |
| 6143 | The number of In Progress items in the specified work queue or |
| 6144 | None on error |
| 6145 | """ |
| 6146 | try: |
| 6147 | queueName = str( queueName ) |
| 6148 | prefix = "work-queue-test" |
| 6149 | operation = "totalInProgress" |
| 6150 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6151 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6152 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6153 | output = output.split( "\r\n" )[ -1 ] |
| 6154 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6155 | pattern = r'\d+' |
| 6156 | if "Invalid operation name" in output: |
| 6157 | main.log.warn( output ) |
| 6158 | return None |
| 6159 | else: |
| 6160 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6161 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6162 | except ( AttributeError, TypeError ): |
| 6163 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6164 | return None |
| 6165 | except Exception: |
| 6166 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6167 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 6168 | |
| 6169 | def events( self, args='-a' ): |
| 6170 | """ |
| 6171 | Description: Returns events -a command output |
| 6172 | Optional: |
| 6173 | add other arguments |
| 6174 | """ |
| 6175 | try: |
| 6176 | cmdStr = "events" |
| 6177 | if args: |
| 6178 | cmdStr += " " + args |
| 6179 | handle = self.sendline( cmdStr ) |
| 6180 | assert handle is not None, "Error in sendline" |
| 6181 | assert "Command not found:" not in handle, handle |
| 6182 | return handle |
| 6183 | except AssertionError: |
| 6184 | main.log.exception( "" ) |
| 6185 | return None |
| 6186 | except TypeError: |
| 6187 | main.log.exception( self.name + ": Object not as expected" ) |
| 6188 | return None |
| 6189 | except pexpect.EOF: |
| 6190 | main.log.error( self.name + ": EOF exception found" ) |
| 6191 | main.log.error( self.name + ": " + self.handle.before ) |
| 6192 | main.cleanAndExit() |
| 6193 | except Exception: |
| 6194 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6195 | main.cleanAndExit() |
| 6196 | |
| 6197 | def getMaster( self, deviceID ): |
| 6198 | """ |
| 6199 | Description: Obtains current master using "roles" command for a specific deviceID |
| 6200 | """ |
| 6201 | try: |
| 6202 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 6203 | except AssertionError: |
| 6204 | main.log.exception( "" ) |
| 6205 | return None |
| 6206 | except TypeError: |
| 6207 | main.log.exception( self.name + ": Object not as expected" ) |
| 6208 | return None |
| 6209 | except pexpect.EOF: |
| 6210 | main.log.error( self.name + ": EOF exception found" ) |
| 6211 | main.log.error( self.name + ": " + self.handle.before ) |
| 6212 | main.cleanAndExit() |
| 6213 | except Exception: |
| 6214 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 6215 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 6216 | |
| 6217 | def issu( self ): |
| 6218 | """ |
| 6219 | Short summary of In-Service Software Upgrade status |
| 6220 | |
| 6221 | Returns the output of the cli command or None on Error |
| 6222 | """ |
| 6223 | try: |
| 6224 | cmdStr = "issu" |
| 6225 | handle = self.sendline( cmdStr ) |
| 6226 | assert handle is not None, "Error in sendline" |
| 6227 | assert "Command not found:" not in handle, handle |
| 6228 | assert "Unsupported command:" not in handle, handle |
| 6229 | return handle |
| 6230 | except AssertionError: |
| 6231 | main.log.exception( "" ) |
| 6232 | return None |
| 6233 | except TypeError: |
| 6234 | main.log.exception( self.name + ": Object not as expected" ) |
| 6235 | return None |
| 6236 | except pexpect.EOF: |
| 6237 | main.log.error( self.name + ": EOF exception found" ) |
| 6238 | main.log.error( self.name + ": " + self.handle.before ) |
| 6239 | main.cleanAndExit() |
| 6240 | except Exception: |
| 6241 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6242 | main.cleanAndExit() |
| 6243 | |
| 6244 | def issuInit( self ): |
| 6245 | """ |
| 6246 | Initiates an In-Service Software Upgrade |
| 6247 | |
| 6248 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6249 | """ |
| 6250 | try: |
| 6251 | cmdStr = "issu init" |
| 6252 | handle = self.sendline( cmdStr ) |
| 6253 | assert handle is not None, "Error in sendline" |
| 6254 | assert "Command not found:" not in handle, handle |
| 6255 | assert "Unsupported command:" not in handle, handle |
| 6256 | if "Initialized" in handle: |
| 6257 | return main.TRUE |
| 6258 | else: |
| 6259 | return main.FALSE |
| 6260 | except AssertionError: |
| 6261 | main.log.exception( "" ) |
| 6262 | return main.ERROR |
| 6263 | except TypeError: |
| 6264 | main.log.exception( self.name + ": Object not as expected" ) |
| 6265 | return main.ERROR |
| 6266 | except pexpect.EOF: |
| 6267 | main.log.error( self.name + ": EOF exception found" ) |
| 6268 | main.log.error( self.name + ": " + self.handle.before ) |
| 6269 | main.cleanAndExit() |
| 6270 | except Exception: |
| 6271 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6272 | main.cleanAndExit() |
| 6273 | |
| 6274 | def issuUpgrade( self ): |
| 6275 | """ |
| 6276 | Transitions stores to upgraded nodes |
| 6277 | |
| 6278 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6279 | """ |
| 6280 | try: |
| 6281 | cmdStr = "issu upgrade" |
| 6282 | handle = self.sendline( cmdStr ) |
| 6283 | assert handle is not None, "Error in sendline" |
| 6284 | assert "Command not found:" not in handle, handle |
| 6285 | assert "Unsupported command:" not in handle, handle |
| 6286 | if "Upgraded" in handle: |
| 6287 | return main.TRUE |
| 6288 | else: |
| 6289 | return main.FALSE |
| 6290 | except AssertionError: |
| 6291 | main.log.exception( "" ) |
| 6292 | return main.ERROR |
| 6293 | except TypeError: |
| 6294 | main.log.exception( self.name + ": Object not as expected" ) |
| 6295 | return main.ERROR |
| 6296 | except pexpect.EOF: |
| 6297 | main.log.error( self.name + ": EOF exception found" ) |
| 6298 | main.log.error( self.name + ": " + self.handle.before ) |
| 6299 | main.cleanAndExit() |
| 6300 | except Exception: |
| 6301 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6302 | main.cleanAndExit() |
| 6303 | |
| 6304 | def issuCommit( self ): |
| 6305 | """ |
| 6306 | Finalizes an In-Service Software Upgrade |
| 6307 | |
| 6308 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6309 | """ |
| 6310 | try: |
| 6311 | cmdStr = "issu commit" |
| 6312 | handle = self.sendline( cmdStr ) |
| 6313 | assert handle is not None, "Error in sendline" |
| 6314 | assert "Command not found:" not in handle, handle |
| 6315 | assert "Unsupported command:" not in handle, handle |
| 6316 | # TODO: Check the version returned by this command |
| 6317 | if "Committed version" in handle: |
| 6318 | return main.TRUE |
| 6319 | else: |
| 6320 | return main.FALSE |
| 6321 | except AssertionError: |
| 6322 | main.log.exception( "" ) |
| 6323 | return main.ERROR |
| 6324 | except TypeError: |
| 6325 | main.log.exception( self.name + ": Object not as expected" ) |
| 6326 | return main.ERROR |
| 6327 | except pexpect.EOF: |
| 6328 | main.log.error( self.name + ": EOF exception found" ) |
| 6329 | main.log.error( self.name + ": " + self.handle.before ) |
| 6330 | main.cleanAndExit() |
| 6331 | except Exception: |
| 6332 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6333 | main.cleanAndExit() |
| 6334 | |
| 6335 | def issuRollback( self ): |
| 6336 | """ |
| 6337 | Rolls back an In-Service Software Upgrade |
| 6338 | |
| 6339 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6340 | """ |
| 6341 | try: |
| 6342 | cmdStr = "issu rollback" |
| 6343 | handle = self.sendline( cmdStr ) |
| 6344 | assert handle is not None, "Error in sendline" |
| 6345 | assert "Command not found:" not in handle, handle |
| 6346 | assert "Unsupported command:" not in handle, handle |
| 6347 | # TODO: Check the version returned by this command |
| 6348 | if "Rolled back to version" in handle: |
| 6349 | return main.TRUE |
| 6350 | else: |
| 6351 | return main.FALSE |
| 6352 | except AssertionError: |
| 6353 | main.log.exception( "" ) |
| 6354 | return main.ERROR |
| 6355 | except TypeError: |
| 6356 | main.log.exception( self.name + ": Object not as expected" ) |
| 6357 | return main.ERROR |
| 6358 | except pexpect.EOF: |
| 6359 | main.log.error( self.name + ": EOF exception found" ) |
| 6360 | main.log.error( self.name + ": " + self.handle.before ) |
| 6361 | main.cleanAndExit() |
| 6362 | except Exception: |
| 6363 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6364 | main.cleanAndExit() |
| 6365 | |
| 6366 | def issuReset( self ): |
| 6367 | """ |
| 6368 | Resets the In-Service Software Upgrade status after a rollback |
| 6369 | |
| 6370 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6371 | """ |
| 6372 | try: |
| 6373 | cmdStr = "issu reset" |
| 6374 | handle = self.sendline( cmdStr ) |
| 6375 | assert handle is not None, "Error in sendline" |
| 6376 | assert "Command not found:" not in handle, handle |
| 6377 | assert "Unsupported command:" not in handle, handle |
| 6378 | # TODO: Check the version returned by this command |
| 6379 | if "Reset version" in handle: |
| 6380 | return main.TRUE |
| 6381 | else: |
| 6382 | return main.FALSE |
| 6383 | except AssertionError: |
| 6384 | main.log.exception( "" ) |
| 6385 | return main.ERROR |
| 6386 | except TypeError: |
| 6387 | main.log.exception( self.name + ": Object not as expected" ) |
| 6388 | return main.ERROR |
| 6389 | except pexpect.EOF: |
| 6390 | main.log.error( self.name + ": EOF exception found" ) |
| 6391 | main.log.error( self.name + ": " + self.handle.before ) |
| 6392 | main.cleanAndExit() |
| 6393 | except Exception: |
| 6394 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6395 | main.cleanAndExit() |
| 6396 | |
| 6397 | def issuStatus( self ): |
| 6398 | """ |
| 6399 | Status of an In-Service Software Upgrade |
| 6400 | |
| 6401 | Returns the output of the cli command or None on Error |
| 6402 | """ |
| 6403 | try: |
| 6404 | cmdStr = "issu status" |
| 6405 | handle = self.sendline( cmdStr ) |
| 6406 | assert handle is not None, "Error in sendline" |
| 6407 | assert "Command not found:" not in handle, handle |
| 6408 | assert "Unsupported command:" not in handle, handle |
| 6409 | return handle |
| 6410 | except AssertionError: |
| 6411 | main.log.exception( "" ) |
| 6412 | return None |
| 6413 | except TypeError: |
| 6414 | main.log.exception( self.name + ": Object not as expected" ) |
| 6415 | return None |
| 6416 | except pexpect.EOF: |
| 6417 | main.log.error( self.name + ": EOF exception found" ) |
| 6418 | main.log.error( self.name + ": " + self.handle.before ) |
| 6419 | main.cleanAndExit() |
| 6420 | except Exception: |
| 6421 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6422 | main.cleanAndExit() |
| 6423 | |
| 6424 | def issuVersion( self ): |
| 6425 | """ |
| 6426 | Get the version of an In-Service Software Upgrade |
| 6427 | |
| 6428 | Returns the output of the cli command or None on Error |
| 6429 | """ |
| 6430 | try: |
| 6431 | cmdStr = "issu version" |
| 6432 | handle = self.sendline( cmdStr ) |
| 6433 | assert handle is not None, "Error in sendline" |
| 6434 | assert "Command not found:" not in handle, handle |
| 6435 | assert "Unsupported command:" not in handle, handle |
| 6436 | return handle |
| 6437 | except AssertionError: |
| 6438 | main.log.exception( "" ) |
| 6439 | return None |
| 6440 | except TypeError: |
| 6441 | main.log.exception( self.name + ": Object not as expected" ) |
| 6442 | return None |
| 6443 | except pexpect.EOF: |
| 6444 | main.log.error( self.name + ": EOF exception found" ) |
| 6445 | main.log.error( self.name + ": " + self.handle.before ) |
| 6446 | main.cleanAndExit() |
| 6447 | except Exception: |
| 6448 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6449 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6450 | |
| 6451 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6452 | """ |
| 6453 | Create a multicast route by calling 'mcast-join' command |
| 6454 | sIP: source IP of the multicast route |
| 6455 | groupIP: group IP of the multicast route |
| 6456 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6457 | dPorts: a list of destination ports of the multicast route |
| 6458 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6459 | """ |
| 6460 | try: |
| 6461 | cmdStr = "mcast-join" |
| 6462 | cmdStr += " " + str( sIP ) |
| 6463 | cmdStr += " " + str( groupIP ) |
| 6464 | cmdStr += " " + str( sPort ) |
| 6465 | assert isinstance( dPorts, list ) |
| 6466 | for dPort in dPorts: |
| 6467 | cmdStr += " " + str( dPort ) |
| 6468 | handle = self.sendline( cmdStr ) |
| 6469 | assert handle is not None, "Error in sendline" |
| 6470 | assert "Command not found:" not in handle, handle |
| 6471 | assert "Unsupported command:" not in handle, handle |
| 6472 | assert "Error executing command" not in handle, handle |
| 6473 | if "Added the mcast route" in handle: |
| 6474 | return main.TRUE |
| 6475 | else: |
| 6476 | return main.FALSE |
| 6477 | except AssertionError: |
| 6478 | main.log.exception( "" ) |
| 6479 | return None |
| 6480 | except TypeError: |
| 6481 | main.log.exception( self.name + ": Object not as expected" ) |
| 6482 | return None |
| 6483 | except pexpect.EOF: |
| 6484 | main.log.error( self.name + ": EOF exception found" ) |
| 6485 | main.log.error( self.name + ": " + self.handle.before ) |
| 6486 | main.cleanAndExit() |
| 6487 | except Exception: |
| 6488 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6489 | main.cleanAndExit() |
| 6490 | |
| 6491 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6492 | """ |
| 6493 | Delete a multicast route by calling 'mcast-delete' command |
| 6494 | sIP: source IP of the multicast route |
| 6495 | groupIP: group IP of the multicast route |
| 6496 | dPorts: a list of destination ports of the multicast route |
| 6497 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6498 | """ |
| 6499 | try: |
| 6500 | cmdStr = "mcast-delete" |
| 6501 | cmdStr += " " + str( sIP ) |
| 6502 | cmdStr += " " + str( groupIP ) |
| 6503 | assert isinstance( dPorts, list ) |
| 6504 | for dPort in dPorts: |
| 6505 | cmdStr += " " + str( dPort ) |
| 6506 | handle = self.sendline( cmdStr ) |
| 6507 | assert handle is not None, "Error in sendline" |
| 6508 | assert "Command not found:" not in handle, handle |
| 6509 | assert "Unsupported command:" not in handle, handle |
| 6510 | assert "Error executing command" not in handle, handle |
| 6511 | if "Updated the mcast route" in handle: |
| 6512 | return main.TRUE |
| 6513 | else: |
| 6514 | return main.FALSE |
| 6515 | except AssertionError: |
| 6516 | main.log.exception( "" ) |
| 6517 | return None |
| 6518 | except TypeError: |
| 6519 | main.log.exception( self.name + ": Object not as expected" ) |
| 6520 | return None |
| 6521 | except pexpect.EOF: |
| 6522 | main.log.error( self.name + ": EOF exception found" ) |
| 6523 | main.log.error( self.name + ": " + self.handle.before ) |
| 6524 | main.cleanAndExit() |
| 6525 | except Exception: |
| 6526 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6527 | main.cleanAndExit() |
| 6528 | |
| 6529 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6530 | """ |
| 6531 | Create a multicast route by calling 'mcast-host-join' command |
| 6532 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6533 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6534 | 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] | 6535 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6536 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6537 | """ |
| 6538 | try: |
| 6539 | cmdStr = "mcast-host-join" |
| 6540 | cmdStr += " -sAddr " + str( sAddr ) |
| 6541 | cmdStr += " -gAddr " + str( gAddr ) |
| 6542 | assert isinstance( srcs, list ) |
| 6543 | for src in srcs: |
| 6544 | cmdStr += " -srcs " + str( src ) |
| 6545 | assert isinstance( sinks, list ) |
| 6546 | for sink in sinks: |
| 6547 | cmdStr += " -sinks " + str( sink ) |
| 6548 | handle = self.sendline( cmdStr ) |
| 6549 | assert handle is not None, "Error in sendline" |
| 6550 | assert "Command not found:" not in handle, handle |
| 6551 | assert "Unsupported command:" not in handle, handle |
| 6552 | assert "Error executing command" not in handle, handle |
| 6553 | if "Added the mcast route" in handle: |
| 6554 | return main.TRUE |
| 6555 | else: |
| 6556 | return main.FALSE |
| 6557 | except AssertionError: |
| 6558 | main.log.exception( "" ) |
| 6559 | return None |
| 6560 | except TypeError: |
| 6561 | main.log.exception( self.name + ": Object not as expected" ) |
| 6562 | return None |
| 6563 | except pexpect.EOF: |
| 6564 | main.log.error( self.name + ": EOF exception found" ) |
| 6565 | main.log.error( self.name + ": " + self.handle.before ) |
| 6566 | main.cleanAndExit() |
| 6567 | except Exception: |
| 6568 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6569 | main.cleanAndExit() |
| 6570 | |
| 6571 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6572 | """ |
| 6573 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6574 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6575 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6576 | 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] | 6577 | will delete the route if not specified |
| 6578 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6579 | """ |
| 6580 | try: |
| 6581 | cmdStr = "mcast-host-delete" |
| 6582 | cmdStr += " -sAddr " + str( sAddr ) |
| 6583 | cmdStr += " -gAddr " + str( gAddr ) |
| 6584 | if host: |
| 6585 | cmdStr += " -h " + str( host ) |
| 6586 | handle = self.sendline( cmdStr ) |
| 6587 | assert handle is not None, "Error in sendline" |
| 6588 | assert "Command not found:" not in handle, handle |
| 6589 | assert "Unsupported command:" not in handle, handle |
| 6590 | assert "Error executing command" not in handle, handle |
| 6591 | if "Updated the mcast route" in handle: |
| 6592 | return main.TRUE |
| 6593 | elif "Deleted the mcast route" in handle: |
| 6594 | return main.TRUE |
| 6595 | else: |
| 6596 | return main.FALSE |
| 6597 | except AssertionError: |
| 6598 | main.log.exception( "" ) |
| 6599 | return None |
| 6600 | except TypeError: |
| 6601 | main.log.exception( self.name + ": Object not as expected" ) |
| 6602 | return None |
| 6603 | except pexpect.EOF: |
| 6604 | main.log.error( self.name + ": EOF exception found" ) |
| 6605 | main.log.error( self.name + ": " + self.handle.before ) |
| 6606 | main.cleanAndExit() |
| 6607 | except Exception: |
| 6608 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6609 | main.cleanAndExit() |
| 6610 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6611 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6612 | """ |
| 6613 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6614 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6615 | gAddr: specifies multicast group address |
| 6616 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6617 | will delete the route if not specified |
| 6618 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6619 | """ |
| 6620 | try: |
| 6621 | cmdStr = "mcast-sink-delete" |
| 6622 | cmdStr += " -sAddr " + str( sAddr ) |
| 6623 | cmdStr += " -gAddr " + str( gAddr ) |
| 6624 | if sink: |
| 6625 | cmdStr += " -s " + str( sink ) |
| 6626 | handle = self.sendline( cmdStr ) |
| 6627 | assert handle is not None, "Error in sendline" |
| 6628 | assert "Command not found:" not in handle, handle |
| 6629 | assert "Unsupported command:" not in handle, handle |
| 6630 | assert "Error executing command" not in handle, handle |
| 6631 | if "Updated the mcast route" in handle: |
| 6632 | return main.TRUE |
| 6633 | elif "Deleted the mcast route" in handle: |
| 6634 | return main.TRUE |
| 6635 | else: |
| 6636 | return main.FALSE |
| 6637 | except AssertionError: |
| 6638 | main.log.exception( "" ) |
| 6639 | return None |
| 6640 | except TypeError: |
| 6641 | main.log.exception( self.name + ": Object not as expected" ) |
| 6642 | return None |
| 6643 | except pexpect.EOF: |
| 6644 | main.log.error( self.name + ": EOF exception found" ) |
| 6645 | main.log.error( self.name + ": " + self.handle.before ) |
| 6646 | main.cleanAndExit() |
| 6647 | except Exception: |
| 6648 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6649 | main.cleanAndExit() |
| 6650 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6651 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6652 | """ |
| 6653 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6654 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6655 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6656 | 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] | 6657 | will delete the route if not specified |
| 6658 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6659 | """ |
| 6660 | try: |
| 6661 | cmdStr = "mcast-source-delete" |
| 6662 | cmdStr += " -sAddr " + str( sAddr ) |
| 6663 | cmdStr += " -gAddr " + str( gAddr ) |
| 6664 | if srcs: |
| 6665 | assert isinstance( srcs, list ) |
| 6666 | for src in srcs: |
| 6667 | cmdStr += " -src " + str( src ) |
| 6668 | handle = self.sendline( cmdStr ) |
| 6669 | assert handle is not None, "Error in sendline" |
| 6670 | assert "Command not found:" not in handle, handle |
| 6671 | assert "Unsupported command:" not in handle, handle |
| 6672 | assert "Error executing command" not in handle, handle |
| 6673 | if "Updated the mcast route" in handle: |
| 6674 | return main.TRUE |
| 6675 | elif "Deleted the mcast route" in handle: |
| 6676 | return main.TRUE |
| 6677 | else: |
| 6678 | return main.FALSE |
| 6679 | except AssertionError: |
| 6680 | main.log.exception( "" ) |
| 6681 | return None |
| 6682 | except TypeError: |
| 6683 | main.log.exception( self.name + ": Object not as expected" ) |
| 6684 | return None |
| 6685 | except pexpect.EOF: |
| 6686 | main.log.error( self.name + ": EOF exception found" ) |
| 6687 | main.log.error( self.name + ": " + self.handle.before ) |
| 6688 | main.cleanAndExit() |
| 6689 | except Exception: |
| 6690 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6691 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6692 | |
| 6693 | def netcfg( self, jsonFormat=True, args="" ): |
| 6694 | """ |
| 6695 | Run netcfg cli command with given args |
| 6696 | """ |
| 6697 | try: |
| 6698 | cmdStr = "netcfg" |
| 6699 | if jsonFormat: |
| 6700 | cmdStr = cmdStr + " -j" |
| 6701 | if args: |
| 6702 | cmdStr = cmdStr + " " + str( args ) |
| 6703 | handle = self.sendline( cmdStr ) |
| 6704 | assert handle is not None, "Error in sendline" |
| 6705 | assert "Command not found:" not in handle, handle |
| 6706 | assert "Unsupported command:" not in handle, handle |
| 6707 | assert "Error executing command" not in handle, handle |
| 6708 | return handle |
| 6709 | except AssertionError: |
| 6710 | main.log.exception( "" ) |
| 6711 | return None |
| 6712 | except TypeError: |
| 6713 | main.log.exception( self.name + ": Object not as expected" ) |
| 6714 | return None |
| 6715 | except pexpect.EOF: |
| 6716 | main.log.error( self.name + ": EOF exception found" ) |
| 6717 | main.log.error( self.name + ": " + self.handle.before ) |
| 6718 | main.cleanAndExit() |
| 6719 | except Exception: |
| 6720 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6721 | main.cleanAndExit() |
| 6722 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6723 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6724 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6725 | 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] | 6726 | Options: |
| 6727 | sAddr: IP address of the source host |
| 6728 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6729 | ipv6: True if hosts are IPv6 |
| 6730 | verbose: return verbose t3 output if True |
| 6731 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6732 | """ |
| 6733 | try: |
| 6734 | # Collect information of both hosts from onos |
| 6735 | hosts = self.hosts() |
| 6736 | hosts = json.loads( hosts ) |
| 6737 | sHost = None |
| 6738 | dHost = None |
| 6739 | for host in hosts: |
| 6740 | if sAddr in host[ "ipAddresses" ]: |
| 6741 | sHost = host |
| 6742 | elif dAddr in host[ "ipAddresses" ]: |
| 6743 | dHost = host |
| 6744 | if sHost and dHost: |
| 6745 | break |
| 6746 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6747 | cmdList = [] |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6748 | if simple: |
| 6749 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6750 | cmdStr = "t3-troubleshoot-simple" |
| 6751 | if verbose: |
| 6752 | cmdStr += " -vv" |
| 6753 | if ipv6: |
| 6754 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6755 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6756 | cmdList.append( cmdStr ) |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6757 | else: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6758 | for location in sHost[ "locations" ]: |
| 6759 | cmdStr = "t3-troubleshoot" |
| 6760 | if verbose: |
| 6761 | cmdStr += " -vv" |
| 6762 | if ipv6: |
| 6763 | cmdStr += " -et ipv6" |
| 6764 | cmdStr += " -s " + str( sAddr ) |
| 6765 | cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] ) |
| 6766 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6767 | if sHost[ "vlan" ] != "None": |
| 6768 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6769 | cmdStr += " -d " + str( dAddr ) |
| 6770 | netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) ) |
| 6771 | netcfg = json.loads( netcfg ) |
| 6772 | assert netcfg, "Failed to get netcfg" |
| 6773 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
| 6774 | cmdList.append( cmdStr ) |
| 6775 | return cmdList |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6776 | except AssertionError: |
| 6777 | main.log.exception( "" ) |
| 6778 | return None |
| 6779 | except ( KeyError, TypeError ): |
| 6780 | main.log.exception( self.name + ": Object not as expected" ) |
| 6781 | return None |
| 6782 | except Exception: |
| 6783 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6784 | main.cleanAndExit() |
| 6785 | |
| 6786 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6787 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6788 | 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] | 6789 | Options: |
| 6790 | sAddr: IP address of the source host |
| 6791 | dAddr: IP address of the destination host |
| 6792 | """ |
| 6793 | try: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6794 | cmdList = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6795 | assert cmdList is not None, "composeT3Command returned None" |
| 6796 | t3Output = "" |
| 6797 | for cmdStr in cmdList: |
| 6798 | handle = self.sendline( cmdStr ) |
| 6799 | assert handle is not None, "Error in sendline" |
| 6800 | assert "Command not found:" not in handle, handle |
| 6801 | assert "Unsupported command:" not in handle, handle |
| 6802 | assert "Error executing command" not in handle, handle |
| 6803 | assert "Tracing packet" in handle |
| 6804 | t3Output += handle |
| 6805 | return t3Output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6806 | except AssertionError: |
| 6807 | main.log.exception( "" ) |
| 6808 | return None |
| 6809 | except pexpect.EOF: |
| 6810 | main.log.error( self.name + ": EOF exception found" ) |
| 6811 | main.log.error( self.name + ": " + self.handle.before ) |
| 6812 | main.cleanAndExit() |
| 6813 | except Exception: |
| 6814 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6815 | main.cleanAndExit() |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6816 | |
| 6817 | def prepareForCLI( self, debug=True, maxRetries=120 ): |
| 6818 | """ |
| 6819 | Prepare docker container to connect to onos cli |
| 6820 | """ |
| 6821 | try: |
| 6822 | # Wait for log files to be created |
| 6823 | ready = 0 |
| 6824 | retries = 0 |
| 6825 | while not ready and retries < maxRetries: |
| 6826 | retries += 1 |
| 6827 | self.handle.sendline( "ls -al /root/onos/apache-karaf-*/data/log" ) |
| 6828 | ready = self.handle.expect( [ "No such file or directory", self.dockerPrompt ] ) |
| 6829 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6830 | if not ready: |
| 6831 | self.handle.expect( self.dockerPrompt ) |
| 6832 | time.sleep( 1 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6833 | |
| 6834 | cmdList = [] |
| 6835 | cmdList.append( "apt-get update" ) |
| 6836 | cmdList.append( "apt-get install -y openssh-server" ) |
| 6837 | # Some built in scripts are hardcoded |
| 6838 | cmdList.append( "ln -s /root/onos /opt/onos" ) |
| 6839 | cmdList.append( "ln -s /root/onos/apache-karaf-*/data/log /opt/onos/log" ) |
| 6840 | cmdList.append( "ls -al /opt/onos" ) |
| 6841 | output = "" |
| 6842 | for cmdStr in cmdList: |
| 6843 | self.handle.sendline( cmdStr ) |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 6844 | self.handle.expect( self.dockerPrompt, timeout=420 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6845 | self.handle.sendline( "" ) |
| 6846 | self.handle.expect( self.dockerPrompt ) |
| 6847 | handle = self.handle.before |
| 6848 | assert "command not found" not in handle, handle |
| 6849 | assert "No such file or directory" not in handle, handle |
| 6850 | output += handle |
| 6851 | if debug: |
| 6852 | main.log.debug( "%s: %s" % ( self.name, output ) ) |
| 6853 | return output |
| 6854 | except AssertionError: |
| 6855 | main.log.exception( "" ) |
| 6856 | return None |
| 6857 | except pexpect.EOF: |
| 6858 | main.log.error( self.name + ": EOF exception found" ) |
| 6859 | main.log.error( self.name + ": " + self.handle.before ) |
| 6860 | main.cleanAndExit() |
| 6861 | except Exception: |
| 6862 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6863 | main.cleanAndExit() |
| 6864 | |
| 6865 | def onosSecureSSH( self, userName="onos", userPWD="rocks" ): |
| 6866 | """ |
| 6867 | Enables secure access to ONOS console |
| 6868 | by removing default users & keys. |
| 6869 | |
| 6870 | bin/onos-user-password onos rocks |
| 6871 | |
| 6872 | Returns: main.TRUE on success and main.FALSE on failure |
| 6873 | """ |
| 6874 | |
| 6875 | try: |
| 6876 | self.handle.sendline( "" ) |
| 6877 | self.handle.expect( self.dockerPrompt ) |
| 6878 | |
| 6879 | self.handle.sendline( "[ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q" ) |
| 6880 | self.handle.expect( self.dockerPrompt ) |
| 6881 | main.log.debug( "%s: %s%s" % ( self.name, self.handle.before, self.handle.after ) ) |
| 6882 | |
| 6883 | self.handle.sendline( "bin/onos-user-key $(id -un) $(cut -d\\\\ -f2 ~/.ssh/id_rsa.pub)" ) |
| 6884 | self.handle.expect( pexpect.TIMEOUT, timeout=10 ) |
| 6885 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6886 | |
| 6887 | self.handle.sendline( "bin/onos-user-password %s %s" % ( userName, userPWD ) ) |
| 6888 | i = self.handle.expect( [ "usage", |
| 6889 | self.dockerPrompt, |
| 6890 | pexpect.TIMEOUT ] ) |
| 6891 | if i == 0: |
| 6892 | # malformed command |
| 6893 | main.log.warn( self.name + ": Could not parse onos-user-password command" ) |
| 6894 | self.handle.expect( self.dockerPrompt ) |
| 6895 | return main.FALSE |
| 6896 | elif i == 1: |
| 6897 | # Process started |
| 6898 | main.log.info( self.name + ": SSH password added for user " + userName ) |
| 6899 | return main.TRUE |
| 6900 | elif i == 2: |
| 6901 | # timeout |
| 6902 | main.log.error( self.name + ": Failed to secure onos ssh " ) |
| 6903 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6904 | except pexpect.EOF: |
| 6905 | main.log.error( self.name + ": EOF exception found" ) |
| 6906 | main.log.error( self.name + ": " + self.handle.before ) |
| 6907 | main.cleanAndExit() |
| 6908 | except Exception: |
| 6909 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6910 | main.cleanAndExit() |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 6911 | |
| 6912 | def routeAdd( self, prefix, nextHop, jsonFormat=True ): |
| 6913 | """ |
| 6914 | Add a route with the given prefix and nextHop |
| 6915 | """ |
| 6916 | try: |
| 6917 | cmdStr = "route-add %s %s %s" % ( "-j" if jsonFormat else "", prefix, nextHop ) |
| 6918 | handle = self.sendline( cmdStr ) |
| 6919 | assert handle is not None, "Error in sendline" |
| 6920 | assert "Command not found:" not in handle, handle |
| 6921 | assert "Unsupported command:" not in handle, handle |
| 6922 | assert "Error executing command" not in handle, handle |
| 6923 | return handle |
| 6924 | except AssertionError: |
| 6925 | main.log.exception( "" ) |
| 6926 | return None |
| 6927 | except TypeError: |
| 6928 | main.log.exception( self.name + ": Object not as expected" ) |
| 6929 | return None |
| 6930 | except pexpect.EOF: |
| 6931 | main.log.error( self.name + ": EOF exception found" ) |
| 6932 | main.log.error( self.name + ": " + self.handle.before ) |
| 6933 | main.cleanAndExit() |
| 6934 | except Exception: |
| 6935 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6936 | main.cleanAndExit() |
| 6937 | |
| 6938 | def routeRemove( self, prefix, nextHop, source=None, jsonFormat=True ): |
| 6939 | """ |
| 6940 | Remove a route with the given prefix and nextHop |
| 6941 | """ |
| 6942 | try: |
| 6943 | if source: |
| 6944 | raise NotImplemented |
| 6945 | cmdStr = "route-remove %s %s %s" % ( "-j" if jsonFormat else "", prefix, nextHop ) |
| 6946 | handle = self.sendline( cmdStr ) |
| 6947 | assert handle is not None, "Error in sendline" |
| 6948 | assert "Command not found:" not in handle, handle |
| 6949 | assert "Unsupported command:" not in handle, handle |
| 6950 | assert "Error executing command" not in handle, handle |
| 6951 | return handle |
| 6952 | except AssertionError: |
| 6953 | main.log.exception( "" ) |
| 6954 | return None |
| 6955 | except TypeError: |
| 6956 | main.log.exception( self.name + ": Object not as expected" ) |
| 6957 | return None |
| 6958 | except pexpect.EOF: |
| 6959 | main.log.error( self.name + ": EOF exception found" ) |
| 6960 | main.log.error( self.name + ": " + self.handle.before ) |
| 6961 | main.cleanAndExit() |
| 6962 | except Exception: |
| 6963 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6964 | main.cleanAndExit() |
| 6965 | |
| 6966 | def routes( self, jsonFormat=True, args="" ): |
| 6967 | """ |
| 6968 | Get routes from ONOS |
| 6969 | """ |
| 6970 | try: |
| 6971 | cmdStr = "routes" |
| 6972 | if jsonFormat: |
| 6973 | cmdStr = cmdStr + " -j" |
| 6974 | handle = self.sendline( cmdStr ) |
| 6975 | assert handle is not None, "Error in sendline" |
| 6976 | assert "Command not found:" not in handle, handle |
| 6977 | assert "Unsupported command:" not in handle, handle |
| 6978 | assert "Error executing command" not in handle, handle |
| 6979 | return handle |
| 6980 | except AssertionError: |
| 6981 | main.log.exception( "" ) |
| 6982 | return None |
| 6983 | except TypeError: |
| 6984 | main.log.exception( self.name + ": Object not as expected" ) |
| 6985 | return None |
| 6986 | except pexpect.EOF: |
| 6987 | main.log.error( self.name + ": EOF exception found" ) |
| 6988 | main.log.error( self.name + ": " + self.handle.before ) |
| 6989 | main.cleanAndExit() |
| 6990 | except Exception: |
| 6991 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6992 | main.cleanAndExit() |