andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | OCT 13 2014 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5 | Copyright 2014 Open Networking Foundation (ONF) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 6 | |
| 7 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 8 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 9 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 10 | |
| 11 | TestON is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 14 | (at your option) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 15 | |
| 16 | TestON is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 23 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 25 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 26 | This driver enters the onos> prompt to issue commands. |
| 27 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 28 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 29 | functions and document properly. |
| 30 | |
| 31 | If you are a contributor to the driver, please |
| 32 | list your email here for future contact: |
| 33 | |
| 34 | jhall@onlab.us |
| 35 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 36 | shreya@onlab.us |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 37 | jeremyr@opennetworking.org |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 38 | """ |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 39 | import ipaddress |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 40 | import pexpect |
| 41 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 42 | import json |
| 43 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 44 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 45 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 46 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 47 | from core.graph import Graph |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 48 | from cStringIO import StringIO |
| 49 | from itertools import izip |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 50 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 51 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 52 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 53 | def __init__( self ): |
| 54 | """ |
| 55 | Initialize client |
| 56 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 57 | self.name = None |
| 58 | self.home = None |
| 59 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 60 | self.karafUser = None |
| 61 | self.karafPass = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 62 | self.karafPort = None |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 63 | self.karafTimeout = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 64 | self.address = None |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 65 | self.karafPromptUser = None |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 66 | self.dockerPrompt = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 67 | self.graph = Graph() |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 68 | super( OnosCliDriver, self ).__init__() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 69 | |
| 70 | def connect( self, **connectargs ): |
| 71 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 72 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 73 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 74 | try: |
| 75 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 76 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 77 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 78 | for key in self.options: |
| 79 | if key == "home": |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 80 | self.home = self.options[ key ] |
| 81 | elif key == "karaf_username": |
| 82 | self.karafUser = self.options[ key ] |
| 83 | elif key == "karaf_password": |
| 84 | self.karafPass = self.options[ key ] |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 85 | elif key == "docker_prompt": |
| 86 | self.dockerPrompt = self.options[ key ] |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 87 | elif key == "karaf_timeout": |
| 88 | self.karafTimeout = self.options[ key ] |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 89 | elif key == "karaf_port": |
| 90 | self.karafPort = self.options[ key ] |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 91 | elif key == "karafPromptUser": |
| 92 | self.karafPromptUser = self.options[ key ] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 93 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 94 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 95 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 96 | self.karafPort = self.checkOptions( self.karafPort, 8101 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 97 | self.dockerPrompt = self.checkOptions( self.dockerPrompt, "~/onos#" ) |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 98 | self.karafTimeout = self.checkOptions( self.karafTimeout, 7200000 ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 99 | |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 100 | self.karafPrompt = self.karafPromptUser + "@root >" |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 101 | for key in self.options: |
| 102 | if key == 'onosIp': |
| 103 | self.onosIp = self.options[ 'onosIp' ] |
| 104 | break |
| 105 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 106 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 107 | |
| 108 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 109 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 110 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 111 | else: |
| 112 | main.log.info( self.name + |
| 113 | ": Trying to connect to " + |
| 114 | self.ip_address ) |
| 115 | |
| 116 | except KeyError: |
| 117 | main.log.info( "Invalid host name," + |
| 118 | " connecting to local host instead" ) |
| 119 | self.ip_address = 'localhost' |
| 120 | except Exception as inst: |
| 121 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 122 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 123 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 124 | user_name=self.user_name, |
| 125 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 126 | port=self.port, |
| 127 | pwd=self.pwd, |
| 128 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 129 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 130 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 131 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 132 | if self.handle: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 133 | self.address = self.ip_address |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 135 | else: |
| 136 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 137 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 138 | except TypeError: |
| 139 | main.log.exception( self.name + ": Object not as expected" ) |
| 140 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 141 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 142 | main.log.error( self.name + ": EOF exception found" ) |
| 143 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 144 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 145 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 146 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 147 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 148 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 149 | def disconnect( self ): |
| 150 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 151 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 152 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 153 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 154 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 155 | if self.handle: |
| 156 | i = self.logout() |
| 157 | if i == main.TRUE: |
| 158 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 159 | for l in range( 3 ): |
| 160 | p = self.handle.expect( [ self.prompt, self.dockerPrompt ] ) |
| 161 | if p == 1: |
| 162 | self.inDocker = False |
| 163 | self.handle.sendline( "exit" ) |
| 164 | j = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=3 ) |
| 165 | if j == 0: |
| 166 | return response |
| 167 | response = main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 168 | except TypeError: |
| 169 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 170 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 171 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 172 | main.log.error( self.name + ": EOF exception found" ) |
| 173 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 174 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 175 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 176 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 177 | except Exception: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 178 | main.log.exception( self.name + ": disconnection failed from the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 179 | response = main.FALSE |
| 180 | return response |
| 181 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 182 | def logout( self ): |
| 183 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 184 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 185 | Returns main.TRUE if exited CLI and |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 186 | main.FALSE on timeout (not guranteed you are disconnected) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 187 | None on TypeError |
| 188 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 189 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 190 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 191 | if self.handle: |
| 192 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 193 | i = self.handle.expect( [ self.karafPrompt, |
| 194 | self.Prompt(), |
| 195 | pexpect.TIMEOUT ], |
| 196 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 197 | timeout=10 ) |
| 198 | if i == 0: # In ONOS CLI |
| 199 | self.handle.sendline( "logout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 200 | j = self.handle.expect( [ self.Prompt(), |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 201 | "Command not found:", |
| 202 | pexpect.TIMEOUT ] ) |
| 203 | if j == 0: # Successfully logged out |
| 204 | return main.TRUE |
| 205 | elif j == 1 or j == 2: |
| 206 | # ONOS didn't fully load, and logout command isn't working |
| 207 | # or the command timed out |
| 208 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 209 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 210 | self.handle.expect( self.Prompt() ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 211 | except pexpect.TIMEOUT: |
| 212 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 213 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 214 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 215 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 216 | repr( self.handle.before ) ) |
| 217 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 218 | elif i == 1: # not in CLI |
| 219 | return main.TRUE |
steven30801 | e42f1fb | 2019-01-17 11:31:45 +0800 | [diff] [blame] | 220 | elif i == 2: # Timeout |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 221 | return main.FALSE |
| 222 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 223 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 224 | except TypeError: |
| 225 | main.log.exception( self.name + ": Object not as expected" ) |
| 226 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 227 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 228 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 229 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 230 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 231 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 232 | main.log.error( self.name + |
| 233 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 234 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 235 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 236 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 237 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 238 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 239 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 240 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 241 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 242 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 243 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 244 | try: |
| 245 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 246 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 247 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 248 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 249 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 250 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 251 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 252 | # and that this driver will have to change accordingly |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 253 | self.handle.expect( str( cellname ) ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 254 | handleBefore = self.handle.before |
| 255 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 256 | # Get the rest of the handle |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 257 | self.handle.sendline( "" ) |
| 258 | self.handle.expect( self.prompt ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 259 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 260 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 261 | main.log.info( "Cell call returned: " + handleBefore + |
| 262 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 263 | |
| 264 | return main.TRUE |
| 265 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 266 | except TypeError: |
| 267 | main.log.exception( self.name + ": Object not as expected" ) |
| 268 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 269 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 270 | main.log.error( self.name + ": eof exception found" ) |
| 271 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 272 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 273 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 274 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 275 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 276 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 277 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 278 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 279 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 280 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 281 | by user would be used to set the current karaf shell idle timeout. |
| 282 | Note that when ever this property is modified the shell will exit and |
Hari Krishna | d7b9c20 | 2015-01-05 10:38:14 -0800 | [diff] [blame] | 283 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 284 | Below is an example to start a session with 60 seconds idle timeout |
| 285 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 286 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 287 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 288 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 289 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 290 | Note: karafTimeout is left as str so that this could be read |
| 291 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 292 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 293 | self.onosIp = ONOSIp |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 294 | self.address = self.onosIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 295 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 296 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 297 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 298 | x = self.handle.expect( [ self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 299 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 300 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 301 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 302 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 303 | # Not in CLI so login |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 304 | if self.inDocker: |
| 305 | # The Docker does not have all the wrapper scripts |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 306 | startCliCommand = "ssh -p %s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null %s@localhost" % ( self.karafPort, self.karafUser ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 307 | elif waitForStart: |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 308 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 309 | startCliCommand = "onos-wait-for-start " + str( ONOSIp ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 310 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 311 | startCliCommand = "onos " + str( ONOSIp ) |
| 312 | self.handle.sendline( startCliCommand ) |
| 313 | tries = 0 |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 314 | timeoutSet = False |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 315 | while tries < 5: |
| 316 | i = self.handle.expect( [ |
| 317 | self.karafPrompt, |
| 318 | "Password:", |
| 319 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 320 | |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 321 | if i == 0: |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 322 | if not karafTimeout or timeoutSet: |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 323 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
| 324 | return main.TRUE |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 325 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 326 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 327 | "config:property-set -p org.apache.karaf.shell\ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 328 | sshIdleTimeout " + |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 329 | str( karafTimeout ) ) |
| 330 | self.handle.expect( "closed by remote host" ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 331 | self.handle.expect( self.Prompt() ) |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 332 | self.handle.sendline( startCliCommand ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 333 | timeoutSet = True |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 334 | elif i == 1: |
| 335 | main.log.info( str( ONOSIp ) + " CLI asking for password" ) |
| 336 | main.log.debug( "Sending %s" % self.karafPass ) |
| 337 | self.handle.sendline( self.karafPass ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 338 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 339 | # If failed, send ctrl+c to process and try again |
| 340 | main.log.info( "Starting CLI failed. Retrying..." ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 341 | time.sleep( 5 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 342 | self.handle.send( "\x03" ) |
| 343 | self.handle.sendline( startCliCommand ) |
| 344 | tries += 1 |
| 345 | main.log.error( "Connection to CLI " + str( ONOSIp ) + " timeout" ) |
| 346 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 347 | except TypeError: |
| 348 | main.log.exception( self.name + ": Object not as expected" ) |
| 349 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 350 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 351 | main.log.error( self.name + ": EOF exception found" ) |
| 352 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 353 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 354 | except Exception: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 355 | main.log.debug( self.handle.before + str( self.handle.after ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 356 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 357 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 358 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 359 | def startCellCli( self, karafTimeout="", |
| 360 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 361 | """ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 362 | Start CLI on onos cell handle. |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 363 | |
| 364 | karafTimeout is an optional argument. karafTimeout value passed |
| 365 | by user would be used to set the current karaf shell idle timeout. |
| 366 | Note that when ever this property is modified the shell will exit and |
| 367 | the subsequent login would reflect new idle timeout. |
| 368 | Below is an example to start a session with 60 seconds idle timeout |
| 369 | ( input value is in milliseconds ): |
| 370 | |
| 371 | tValue = "60000" |
| 372 | |
| 373 | Note: karafTimeout is left as str so that this could be read |
| 374 | and passed to startOnosCli from PARAMS file as str. |
| 375 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 376 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 377 | try: |
| 378 | self.handle.sendline( "" ) |
| 379 | x = self.handle.expect( [ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 380 | self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 381 | |
| 382 | if x == 1: |
| 383 | main.log.info( "ONOS cli is already running" ) |
| 384 | return main.TRUE |
| 385 | |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 386 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 387 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 388 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 389 | self.karafPrompt, |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 390 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 391 | |
| 392 | if i == 0: |
| 393 | main.log.info( self.name + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 394 | if karafTimeout: # FIXME: This doesn't look right |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 395 | self.handle.sendline( |
| 396 | "config:property-set -p org.apache.karaf.shell\ |
| 397 | sshIdleTimeout " + |
| 398 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 399 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 400 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 401 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 402 | return main.TRUE |
| 403 | else: |
| 404 | # If failed, send ctrl+c to process and try again |
| 405 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 406 | self.handle.send( "\x03" ) |
| 407 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 408 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 409 | timeout=30 ) |
| 410 | if i == 0: |
| 411 | main.log.info( self.name + " CLI Started " + |
| 412 | "successfully after retry attempt" ) |
| 413 | if karafTimeout: |
| 414 | self.handle.sendline( |
| 415 | "config:property-set -p org.apache.karaf.shell\ |
| 416 | sshIdleTimeout " + |
| 417 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 418 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 419 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 420 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 421 | return main.TRUE |
| 422 | else: |
| 423 | main.log.error( "Connection to CLI " + |
| 424 | self.name + " timeout" ) |
| 425 | return main.FALSE |
| 426 | |
| 427 | except TypeError: |
| 428 | main.log.exception( self.name + ": Object not as expected" ) |
| 429 | return None |
| 430 | except pexpect.EOF: |
| 431 | main.log.error( self.name + ": EOF exception found" ) |
| 432 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 433 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 434 | except Exception: |
| 435 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 436 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 437 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 438 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 439 | """ |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 440 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 441 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 442 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 443 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 444 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 445 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 446 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 447 | """ |
| 448 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 449 | lvlStr = "" |
| 450 | if level: |
| 451 | lvlStr = "--level=" + level |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 452 | handle = self.sendline( "log:log " + lvlStr + " " + cmdStr, noExit=noExit ) |
| 453 | assert handle is not None, "Error in sendline" |
| 454 | assert "Command not found:" not in handle, handle |
| 455 | if re.search( "Error", handle ): |
| 456 | main.log.error( self.name + ": Error in logging message" ) |
| 457 | main.log.error( handle ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 458 | return main.FALSE |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 459 | else: |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 460 | return main.TRUE |
| 461 | except AssertionError: |
| 462 | main.log.exception( "" ) |
| 463 | return None |
| 464 | except TypeError: |
| 465 | main.log.exception( self.name + ": Object not as expected" ) |
| 466 | return None |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 467 | except pexpect.EOF: |
| 468 | main.log.error( self.name + ": EOF exception found" ) |
| 469 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 470 | if noExit: |
| 471 | main.cleanup() |
| 472 | return None |
| 473 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 474 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 475 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 476 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 477 | if noExit: |
| 478 | main.cleanup() |
| 479 | return None |
| 480 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 481 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 482 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 483 | def clearBuffer( self, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 484 | """ |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 485 | Test cli connection and clear any left over output in the buffer |
| 486 | Optional Arguments: |
| 487 | debug - Defaults to False. If True, will enable debug logging. |
| 488 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 489 | before a timeout. |
| 490 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 491 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 492 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 493 | # Try to reconnect if disconnected from cli |
| 494 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 495 | i = self.handle.expect( [ self.karafPrompt, |
| 496 | self.Prompt(), |
| 497 | pexpect.TIMEOUT ] ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 498 | response = self.handle.before |
Jon Hall | a16b4db | 2021-10-20 14:11:59 -0700 | [diff] [blame] | 499 | if i == 1: # Not in ONOS CLI |
| 500 | # FIXME: This isn't really the correct place for this, but it works for now |
| 501 | # Check if port-forward session is still up first |
| 502 | if hasattr( main, "Cluster"): |
| 503 | ctrl = None |
| 504 | for c in main.Cluster.controllers: |
| 505 | if c.CLI is self: |
| 506 | ctrl = c |
| 507 | break |
| 508 | if not ctrl: |
| 509 | main.log.warn( self.name + ": Could not find this node in Cluster. Can't check port-forward status" ) |
| 510 | elif ctrl.k8s: |
| 511 | ctrl.k8s.checkPortForward( ctrl.k8s.podName, |
| 512 | kubeconfig=ctrl.k8s.kubeConfig, |
| 513 | namespace=main.params[ 'kubernetes' ][ 'namespace' ] ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 514 | main.log.error( self.name + ": onos cli session closed. " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 515 | if self.onosIp: |
| 516 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 517 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 518 | if reconnectResult: |
| 519 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 520 | else: |
| 521 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 522 | if noExit: |
| 523 | return None |
| 524 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 525 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 526 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 527 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 528 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 529 | main.log.warn( "Timeout when testing cli responsiveness" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 530 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 531 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 532 | self.handle.expect( self.karafPrompt ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 533 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 534 | response += self.handle.before |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 535 | if debug: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 536 | main.log.debug( self.name + ": Raw output from sending ''" ) |
| 537 | main.log.debug( self.name + ": " + repr( response ) ) |
| 538 | except pexpect.TIMEOUT: |
| 539 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 540 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 541 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 542 | self.handle.expect( self.karafPrompt ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 543 | return None |
| 544 | except pexpect.EOF: |
| 545 | main.log.error( self.name + ": EOF exception found" ) |
| 546 | main.log.error( self.name + ": " + self.handle.before ) |
| 547 | if noExit: |
| 548 | return None |
| 549 | else: |
| 550 | main.cleanAndExit() |
| 551 | except Exception: |
| 552 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 553 | if noExit: |
| 554 | return None |
| 555 | else: |
| 556 | main.cleanAndExit() |
| 557 | |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 558 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True, expectJson=False ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 559 | """ |
| 560 | A wrapper around pexpect's sendline/expect. Will return all the output from a given command |
| 561 | |
| 562 | Required Arguments: |
| 563 | cmdStr - String to send to the pexpect session |
| 564 | |
| 565 | Optional Arguments: |
| 566 | showResponse - Defaults to False. If True will log the response. |
| 567 | debug - Defaults to False. If True, will enable debug logging. |
| 568 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 569 | before a timeout. |
| 570 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 571 | closed channel, but instead return None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 572 | relaxedRegex - Defaults to True. If there is a pipe in the command send, will only try to match the last part of the piped command. |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 573 | |
| 574 | Warning: There are no sanity checking to commands sent using this method. |
| 575 | |
| 576 | """ |
| 577 | try: |
| 578 | # Try to reconnect if disconnected from cli |
| 579 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
| 580 | if debug: |
| 581 | # NOTE: This adds an average of .4 seconds per call |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 582 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 583 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 584 | self.handle.sendline( cmdStr ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 585 | self.handle.expect( self.karafPrompt, timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 586 | response = self.handle.before |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 587 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 588 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 589 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 590 | main.log.debug( self.name + ": Raw output" ) |
| 591 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 592 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 593 | response = self.cleanOutput( response, debug=debug ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 594 | # Remove control codes from karaf 4.2.1 |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 595 | karafEscape = re.compile( r"('(0|1)~\'|\r\r\r\n\x1b\[A\x1b\[79C(x|\s)?|\x1b(>|=~?)|\x1b\[90m~)" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 596 | response = karafEscape.sub( '', response ) |
| 597 | if debug: |
| 598 | main.log.debug( self.name + ": karafEscape output" ) |
| 599 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 600 | # Remove ANSI color control strings from output |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 601 | response = self.cleanOutput( response, debug ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 602 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 603 | # Remove ANSI color control strings from output |
Jon Hall | cf31d0f | 2018-12-13 11:18:48 -0800 | [diff] [blame] | 604 | # NOTE: karaf is sometimes adding a single character then two |
| 605 | # backspaces and sometimes adding 2 characters with 2 backspaces?? |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 606 | backspaceEscape = re.compile( r'((.|\s)\x08)' ) |
| 607 | unchanged = False |
| 608 | while not unchanged: |
| 609 | old = response |
| 610 | response = backspaceEscape.sub( '', response, count=1 ) |
| 611 | if debug: |
| 612 | main.log.debug( self.name + ": backspaceEscape output" ) |
| 613 | main.log.debug( self.name + ": " + repr( response ) ) |
| 614 | unchanged = old == response |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 615 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 616 | # Remove extra return chars that get added |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 617 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 618 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 619 | main.log.debug( self.name + ": Removed extra returns " + |
| 620 | "from output" ) |
| 621 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 622 | |
| 623 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 624 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 625 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 626 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 627 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 628 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 629 | # parse for just the output, remove the cmd from response |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 630 | cmdPattern = cmdStr.strip() |
| 631 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 632 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 633 | if relaxedRegex: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 634 | cmdPattern = cmdPattern.split( '|' )[ -1 ].strip() |
| 635 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 636 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 637 | # This was added because karaf 4.2 is stripping some characters from the command echo |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 638 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 639 | if expectJson: |
| 640 | main.log.warn( "Relaxed Regex: Searching for a json string amongst the output" ) |
| 641 | jsonPattern = r'\{.*\}' |
| 642 | match = re.search( jsonPattern, output[ 0 ] ) |
| 643 | if match: |
| 644 | output = [ '' , match.group( 0 ) ] # We expect a list with the second element to be the output |
Jon Hall | 39e3ffe | 2018-12-05 11:40:29 -0800 | [diff] [blame] | 645 | if len( output ) < 2: |
| 646 | main.log.warn( "Relaxing regex match to last 5 characters of the sent command" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 647 | cmdPattern = cmdPattern[ -5: ] |
| 648 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 649 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
| 650 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 651 | else: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 652 | output = response.split( cmdPattern, 1 ) |
| 653 | if len( output ) < 2: # TODO: Should we do this without the relaxedRegex flag? |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 654 | main.log.warn( "Relaxing regex match to last 5 characters of the sent command" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 655 | output = response.split( cmdPattern[ -5: ], 1 ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 656 | if output: |
| 657 | if debug: |
| 658 | main.log.debug( self.name + ": split output" ) |
| 659 | for r in output: |
| 660 | main.log.debug( self.name + ": " + repr( r ) ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 661 | if len( output ) == 1: |
| 662 | main.log.error( "Could not remove sent command echo from output" ) |
| 663 | return output |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 664 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 665 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 666 | main.log.info( "Response from ONOS: {}".format( output ) ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 667 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 668 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 669 | except pexpect.TIMEOUT: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 670 | main.log.error( self.name + ": ONOS timeout" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 671 | if debug: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 672 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 6b5e5ba | 2019-01-31 15:32:40 -0800 | [diff] [blame] | 673 | self.exitFromCmd( self.karafPrompt, 100 ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 674 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 675 | except IndexError: |
| 676 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 677 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 678 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 679 | except TypeError: |
| 680 | main.log.exception( self.name + ": Object not as expected" ) |
| 681 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 682 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 683 | main.log.error( self.name + ": EOF exception found" ) |
| 684 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 685 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 686 | return None |
| 687 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 688 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 689 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 690 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 691 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 692 | return None |
| 693 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 694 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 695 | |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 696 | def lineCount( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
| 697 | """ |
| 698 | A wrapper around sendline(). Will return the number of lines returned or None on error |
| 699 | |
| 700 | Required Arguments: |
| 701 | cmdStr - String to send to the pexpect session |
| 702 | |
| 703 | Optional Arguments: |
| 704 | showResponse - Defaults to False. If True will log the response. |
| 705 | debug - Defaults to False. If True, will enable debug logging. |
| 706 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 707 | before a timeout. |
| 708 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 709 | closed channel, but instead return None |
| 710 | relaxedRegex - Defaults to True. If there is a pipe in the command send, will only try to match the last part of the piped command. |
| 711 | |
| 712 | Warning: There are no sanity checking to commands sent using this method. |
| 713 | |
| 714 | """ |
| 715 | try: |
| 716 | numLines = self.sendline( cmdStr, showResponse, debug, timeout, noExit, relaxedRegex ) |
You Wang | 0ce8e0c | 2019-02-22 12:22:26 -0800 | [diff] [blame] | 717 | parsed = re.search( "(\d+)", numLines ) |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 718 | if not parsed: |
| 719 | main.log.error( "Warning, output of karaf's wc may have changed" ) |
| 720 | return None |
| 721 | return parsed.group( 1 ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 722 | except IndexError: |
| 723 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 724 | main.log.debug( "response: {}".format( repr( numLines ) ) ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 725 | return None |
| 726 | except TypeError: |
| 727 | main.log.exception( self.name + ": Object not as expected" ) |
| 728 | return None |
| 729 | except Exception: |
| 730 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 731 | if noExit: |
| 732 | return None |
| 733 | else: |
| 734 | main.cleanAndExit() |
| 735 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 736 | # IMPORTANT NOTE: |
| 737 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 738 | # the cli command changing 'a:b' with 'aB'. |
| 739 | # Ex ) onos:topology > onosTopology |
| 740 | # onos:links > onosLinks |
| 741 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 742 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 743 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 744 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 745 | Adds a new cluster node by ID and address information. |
| 746 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 747 | * nodeId |
| 748 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 749 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 750 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 751 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 752 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 753 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 754 | str( ONOSIp ) + " " + str( tcpPort ) |
| 755 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 756 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 757 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 758 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 759 | main.log.error( self.name + ": Error in adding node" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 760 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 761 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 762 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 763 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 764 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 765 | except AssertionError: |
| 766 | main.log.exception( "" ) |
| 767 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 768 | except TypeError: |
| 769 | main.log.exception( self.name + ": Object not as expected" ) |
| 770 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 771 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 772 | main.log.error( self.name + ": EOF exception found" ) |
| 773 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 774 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 775 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 776 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 777 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 778 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 779 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 780 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 781 | Removes a cluster by ID |
| 782 | Issues command: 'remove-node [<node-id>]' |
| 783 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 784 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 785 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 786 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 787 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 788 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 789 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 790 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 791 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 792 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 793 | main.log.error( self.name + ": Error in removing node" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 794 | main.log.error( handle ) |
| 795 | return main.FALSE |
| 796 | else: |
| 797 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 798 | except AssertionError: |
| 799 | main.log.exception( "" ) |
| 800 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 801 | except TypeError: |
| 802 | main.log.exception( self.name + ": Object not as expected" ) |
| 803 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 804 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 805 | main.log.error( self.name + ": EOF exception found" ) |
| 806 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 807 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 808 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 809 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 810 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 811 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 812 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 813 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 814 | List the nodes currently visible |
| 815 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 816 | Optional argument: |
| 817 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 818 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 819 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 820 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 821 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 822 | cmdStr += " -j" |
| 823 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 824 | assert output is not None, "Error in sendline" |
Jon Hall | e37bd1f | 2020-09-10 12:16:41 -0700 | [diff] [blame] | 825 | # "Command not found" or "Service org.onosproject.security.AuditService not found" |
| 826 | assert "not found" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 827 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 828 | except AssertionError: |
| 829 | main.log.exception( "" ) |
| 830 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 831 | except TypeError: |
| 832 | main.log.exception( self.name + ": Object not as expected" ) |
| 833 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 834 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 835 | main.log.error( self.name + ": EOF exception found" ) |
| 836 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 837 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 838 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 839 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 840 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 841 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 842 | def topology( self ): |
| 843 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 844 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 845 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 846 | Return: |
| 847 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 848 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 849 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 850 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 851 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 852 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 853 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 854 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 855 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 856 | except AssertionError: |
| 857 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 858 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 859 | except TypeError: |
| 860 | main.log.exception( self.name + ": Object not as expected" ) |
| 861 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 862 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 863 | main.log.error( self.name + ": EOF exception found" ) |
| 864 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 865 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 866 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 867 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 868 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 869 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 870 | def deviceRemove( self, deviceId ): |
| 871 | """ |
| 872 | Removes particular device from storage |
| 873 | |
| 874 | TODO: refactor this function |
| 875 | """ |
| 876 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 877 | cmdStr = "device-remove " + str( deviceId ) |
| 878 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 879 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 880 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 881 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 882 | main.log.error( self.name + ": Error in removing device" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 883 | main.log.error( handle ) |
| 884 | return main.FALSE |
| 885 | else: |
| 886 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 887 | except AssertionError: |
| 888 | main.log.exception( "" ) |
| 889 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 890 | except TypeError: |
| 891 | main.log.exception( self.name + ": Object not as expected" ) |
| 892 | return None |
| 893 | except pexpect.EOF: |
| 894 | main.log.error( self.name + ": EOF exception found" ) |
| 895 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 896 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 897 | except Exception: |
| 898 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 899 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 900 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 901 | def devices( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 902 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 903 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 904 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 905 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 906 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 907 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 908 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 909 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 910 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 911 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 912 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 913 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 914 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 915 | except AssertionError: |
| 916 | main.log.exception( "" ) |
| 917 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 918 | except TypeError: |
| 919 | main.log.exception( self.name + ": Object not as expected" ) |
| 920 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 921 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 922 | main.log.error( self.name + ": EOF exception found" ) |
| 923 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 924 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 925 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 926 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 927 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 928 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 929 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 930 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 931 | This balances the devices across all controllers |
| 932 | by issuing command: 'onos> onos:balance-masters' |
| 933 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 934 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 935 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 936 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 937 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 938 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 939 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 940 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 941 | main.log.error( self.name + ": Error in balancing masters" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 942 | main.log.error( handle ) |
| 943 | return main.FALSE |
| 944 | else: |
| 945 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 946 | except AssertionError: |
| 947 | main.log.exception( "" ) |
| 948 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 949 | except TypeError: |
| 950 | main.log.exception( self.name + ": Object not as expected" ) |
| 951 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 952 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 953 | main.log.error( self.name + ": EOF exception found" ) |
| 954 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 955 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 956 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 957 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 958 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 959 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 960 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 961 | """ |
| 962 | Returns the output of the masters command. |
| 963 | Optional argument: |
| 964 | * jsonFormat - boolean indicating if you want output in json |
| 965 | """ |
| 966 | try: |
| 967 | cmdStr = "onos:masters" |
| 968 | if jsonFormat: |
| 969 | cmdStr += " -j" |
| 970 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 971 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 972 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 973 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 974 | except AssertionError: |
| 975 | main.log.exception( "" ) |
| 976 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 977 | except TypeError: |
| 978 | main.log.exception( self.name + ": Object not as expected" ) |
| 979 | return None |
| 980 | except pexpect.EOF: |
| 981 | main.log.error( self.name + ": EOF exception found" ) |
| 982 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 983 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 984 | except Exception: |
| 985 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 986 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 987 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 988 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 989 | """ |
| 990 | Uses the master command to check that the devices' leadership |
| 991 | is evenly divided |
| 992 | |
| 993 | Dependencies: checkMasters() and summary() |
| 994 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 995 | Returns main.TRUE if the devices are balanced |
| 996 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 997 | Exits on Exception |
| 998 | Returns None on TypeError |
| 999 | """ |
| 1000 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1001 | summaryOutput = self.summary() |
| 1002 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 1003 | except ( TypeError, ValueError ): |
| 1004 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 1005 | return None |
| 1006 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1007 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1008 | mastersOutput = self.checkMasters() |
| 1009 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1010 | first = masters[ 0 ][ "size" ] |
| 1011 | for master in masters: |
| 1012 | totalOwnedDevices += master[ "size" ] |
| 1013 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 1014 | main.log.error( "Mastership not balanced" ) |
| 1015 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 1016 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 1017 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1018 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1019 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1020 | except ( TypeError, ValueError ): |
| 1021 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1022 | return None |
| 1023 | except pexpect.EOF: |
| 1024 | main.log.error( self.name + ": EOF exception found" ) |
| 1025 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1026 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1027 | except Exception: |
| 1028 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1029 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1030 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1031 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1032 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1033 | Lists all core links |
| 1034 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1035 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1036 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1037 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1038 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1039 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1040 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1041 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1042 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1043 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1044 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1045 | except AssertionError: |
| 1046 | main.log.exception( "" ) |
| 1047 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1048 | except TypeError: |
| 1049 | main.log.exception( self.name + ": Object not as expected" ) |
| 1050 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1051 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1052 | main.log.error( self.name + ": EOF exception found" ) |
| 1053 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1054 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1055 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1056 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1057 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1058 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1059 | def ports( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1060 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1061 | Lists all ports |
| 1062 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1063 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1064 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1065 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1066 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1067 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1068 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1069 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1070 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1071 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1072 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1073 | except AssertionError: |
| 1074 | main.log.exception( "" ) |
| 1075 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1076 | except TypeError: |
| 1077 | main.log.exception( self.name + ": Object not as expected" ) |
| 1078 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1079 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1080 | main.log.error( self.name + ": EOF exception found" ) |
| 1081 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1082 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1083 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1084 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1085 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1086 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1087 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1088 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1089 | Lists all devices and the controllers with roles assigned to them |
| 1090 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1091 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1092 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1093 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1094 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1095 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1096 | cmdStr += " -j" |
| 1097 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1098 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1099 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1100 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1101 | except AssertionError: |
| 1102 | main.log.exception( "" ) |
| 1103 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1104 | except TypeError: |
| 1105 | main.log.exception( self.name + ": Object not as expected" ) |
| 1106 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1107 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1108 | main.log.error( self.name + ": EOF exception found" ) |
| 1109 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1110 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1111 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1112 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1113 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1114 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1115 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1116 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1117 | Given the a string containing the json representation of the "roles" |
| 1118 | cli command and a partial or whole device id, returns a json object |
| 1119 | containing the roles output for the first device whose id contains |
| 1120 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1121 | |
| 1122 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1123 | A dict of the role assignments for the given device or |
| 1124 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1125 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1126 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1127 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1128 | return None |
| 1129 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1130 | rawRoles = self.roles() |
| 1131 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1132 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1133 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1134 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1135 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1136 | return device |
| 1137 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1138 | except ( TypeError, ValueError ): |
| 1139 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1140 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 1141 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1142 | main.log.error( self.name + ": EOF exception found" ) |
| 1143 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1144 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1145 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1146 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1147 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1148 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1149 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1150 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1151 | Iterates through each device and checks if there is a master assigned |
| 1152 | Returns: main.TRUE if each device has a master |
| 1153 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1154 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1155 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1156 | rawRoles = self.roles() |
| 1157 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1158 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1159 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1160 | # print device |
| 1161 | if device[ 'master' ] == "none": |
| 1162 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1163 | return main.FALSE |
| 1164 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1165 | except ( TypeError, ValueError ): |
| 1166 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1167 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1168 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1169 | main.log.error( self.name + ": EOF exception found" ) |
| 1170 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1171 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1172 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1173 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1174 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1175 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1176 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1177 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1178 | Returns string of paths, and the cost. |
| 1179 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1180 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1181 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1182 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1183 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1184 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1185 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1186 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1187 | main.log.error( self.name + ": Error in getting paths" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1188 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1189 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1190 | path = handle.split( ";" )[ 0 ] |
| 1191 | cost = handle.split( ";" )[ 1 ] |
| 1192 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1193 | except AssertionError: |
| 1194 | main.log.exception( "" ) |
| 1195 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1196 | except TypeError: |
| 1197 | main.log.exception( self.name + ": Object not as expected" ) |
| 1198 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1199 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1200 | main.log.error( self.name + ": EOF exception found" ) |
| 1201 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1202 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1203 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1204 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1205 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1206 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1207 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1208 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1209 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1210 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1211 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1212 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1213 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1214 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1215 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1216 | cmdStr += " -j" |
| 1217 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1218 | if handle: |
| 1219 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1220 | # TODO: Maybe make this less hardcoded |
| 1221 | # ConsistentMap Exceptions |
| 1222 | assert "org.onosproject.store.service" not in handle |
| 1223 | # Node not leader |
| 1224 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1225 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1226 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1227 | main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1228 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1229 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1230 | except TypeError: |
| 1231 | main.log.exception( self.name + ": Object not as expected" ) |
| 1232 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1233 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1234 | main.log.error( self.name + ": EOF exception found" ) |
| 1235 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1236 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1237 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1238 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1239 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1240 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1241 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1242 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1243 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1244 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1245 | Note: mac must be a colon separated mac address, but could be a |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1246 | partial mac address |
| 1247 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1248 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1249 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1250 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1251 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1252 | return None |
| 1253 | else: |
| 1254 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1255 | rawHosts = self.hosts() |
| 1256 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1257 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1258 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1259 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1260 | if not host: |
| 1261 | pass |
| 1262 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1263 | return host |
| 1264 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1265 | except ( TypeError, ValueError ): |
| 1266 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1267 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1268 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1269 | main.log.error( self.name + ": EOF exception found" ) |
| 1270 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1271 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1272 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1273 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1274 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1275 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1276 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1277 | """ |
| 1278 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1279 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1280 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1281 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1282 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1283 | IMPORTANT: |
| 1284 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1285 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1286 | Furthermore, it assumes that value of VLAN is '-1' |
| 1287 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1288 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1289 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1290 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1291 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1292 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1293 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1294 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1295 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1296 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1297 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1298 | i = iter( str( hostHex ) ) |
| 1299 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1300 | hostHex = hostHex + "/-1" |
| 1301 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1302 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1303 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1304 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1305 | except TypeError: |
| 1306 | main.log.exception( self.name + ": Object not as expected" ) |
| 1307 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1308 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1309 | main.log.error( self.name + ": EOF exception found" ) |
| 1310 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1311 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1312 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1313 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1314 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1315 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1316 | def verifyHostLocation( self, hostIp, location ): |
| 1317 | """ |
| 1318 | Description: |
| 1319 | Verify the host given is discovered in all locations expected |
| 1320 | Required: |
| 1321 | hostIp: IP address of the host |
| 1322 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1323 | Could be a string or list |
| 1324 | Returns: |
| 1325 | main.TRUE if host is discovered on all locations provided |
| 1326 | main.FALSE otherwise |
| 1327 | """ |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1328 | locations = [ location ] if isinstance( location, str ) else location |
| 1329 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1330 | try: |
| 1331 | hosts = self.hosts() |
| 1332 | hosts = json.loads( hosts ) |
| 1333 | targetHost = None |
| 1334 | for host in hosts: |
| 1335 | if hostIp in host[ "ipAddresses" ]: |
| 1336 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1337 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1338 | result = main.TRUE |
| 1339 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1340 | for loc in locations: |
| 1341 | discovered = False |
| 1342 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1343 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1344 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1345 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1346 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1347 | break |
| 1348 | if discovered: |
| 1349 | locationsDiscovered.remove( locDiscovered ) |
| 1350 | else: |
| 1351 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1352 | result = main.FALSE |
| 1353 | if locationsDiscovered: |
| 1354 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1355 | result = main.FALSE |
| 1356 | return result |
| 1357 | except KeyError: |
| 1358 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1359 | return None |
| 1360 | except pexpect.EOF: |
| 1361 | main.log.error( self.name + ": EOF exception found" ) |
| 1362 | main.log.error( self.name + ": " + self.handle.before ) |
| 1363 | main.cleanAndExit() |
| 1364 | except Exception: |
| 1365 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1366 | return None |
| 1367 | |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1368 | def verifyHostIp( self, hostList=[], prefix="", prefixLength=24 ): |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1369 | """ |
| 1370 | Description: |
| 1371 | Verify that all hosts have IP address assigned to them |
| 1372 | Optional: |
| 1373 | hostList: If specified, verifications only happen to the hosts |
| 1374 | in hostList |
| 1375 | prefix: at least one of the ip address assigned to the host |
| 1376 | needs to have the specified prefix |
| 1377 | Returns: |
| 1378 | main.TRUE if all hosts have specific IP address assigned; |
| 1379 | main.FALSE otherwise |
| 1380 | """ |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1381 | try: |
| 1382 | hosts = self.hosts() |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1383 | hosts = json.loads( hosts, "utf-8" ) |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1384 | if not hostList: |
| 1385 | hostList = [ host[ "id" ] for host in hosts ] |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1386 | hostList = [ str( h.lower() ) for h in hostList ] |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1387 | for host in hosts: |
| 1388 | hostId = host[ "id" ] |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1389 | hostId = str( hostId.lower() ) |
| 1390 | match = False |
| 1391 | for onosHostId in hostList: |
| 1392 | if onosHostId == hostId: |
| 1393 | match = True |
| 1394 | if not match: |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1395 | continue |
| 1396 | ipList = host[ "ipAddresses" ] |
| 1397 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1398 | if not ipList: |
| 1399 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1400 | else: |
Siddesh | 715a919 | 2021-11-03 19:51:20 +0000 | [diff] [blame] | 1401 | # if no hostIp in subnet: Get warning otherwise remove hostId from hostList |
| 1402 | subnetString = u"%s/%s" % (prefix, prefixLength) |
| 1403 | subnet = ipaddress.ip_network( subnetString, strict=False ) |
| 1404 | if not any( ipaddress.ip_address( ip ) in subnet for ip in ipList ): |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1405 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1406 | else: |
| 1407 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1408 | hostList.remove( hostId ) |
| 1409 | if hostList: |
| 1410 | main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 1411 | # Print info for debugging |
| 1412 | main.log.debug( self.name + ": hosts output: " + str( hosts ) ) |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1413 | return main.FALSE |
| 1414 | else: |
| 1415 | return main.TRUE |
| 1416 | except KeyError: |
| 1417 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1418 | return None |
| 1419 | except pexpect.EOF: |
| 1420 | main.log.error( self.name + ": EOF exception found" ) |
| 1421 | main.log.error( self.name + ": " + self.handle.before ) |
| 1422 | main.cleanAndExit() |
| 1423 | except Exception: |
| 1424 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1425 | return None |
| 1426 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1427 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1428 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1429 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1430 | * hostIdOne: ONOS host id for host1 |
| 1431 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1432 | Optional: |
| 1433 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1434 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1435 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1436 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1437 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1438 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1439 | Returns: |
| 1440 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1441 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1442 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1443 | cmdStr = "add-host-intent " |
| 1444 | if vlanId: |
| 1445 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1446 | if setVlan: |
| 1447 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1448 | if encap: |
| 1449 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1450 | if bandwidth: |
| 1451 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1452 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1453 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1454 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1455 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1456 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1457 | main.log.error( self.name + ": Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1458 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1459 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1460 | else: |
| 1461 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1462 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1463 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1464 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1465 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1466 | else: |
| 1467 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1468 | main.log.debug( "Response from ONOS was: " + |
| 1469 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1470 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1471 | except AssertionError: |
| 1472 | main.log.exception( "" ) |
| 1473 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1474 | except TypeError: |
| 1475 | main.log.exception( self.name + ": Object not as expected" ) |
| 1476 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1477 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1478 | main.log.error( self.name + ": EOF exception found" ) |
| 1479 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1480 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1481 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1482 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1483 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1484 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1485 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1486 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1487 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1488 | * ingressDevice: device id of ingress device |
| 1489 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1490 | Optional: |
| 1491 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1492 | Description: |
| 1493 | Adds an optical intent by specifying an ingress and egress device |
| 1494 | Returns: |
| 1495 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1496 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1497 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1498 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1499 | " " + str( egressDevice ) |
| 1500 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1501 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1502 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1503 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1504 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1505 | main.log.error( self.name + ": Error in adding Optical intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1506 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1507 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1508 | main.log.info( "Optical intent installed between " + |
| 1509 | str( ingressDevice ) + " and " + |
| 1510 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1511 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1512 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1513 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1514 | else: |
| 1515 | main.log.error( "Error, intent ID not found" ) |
| 1516 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1517 | except AssertionError: |
| 1518 | main.log.exception( "" ) |
| 1519 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1520 | except TypeError: |
| 1521 | main.log.exception( self.name + ": Object not as expected" ) |
| 1522 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1523 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1524 | main.log.error( self.name + ": EOF exception found" ) |
| 1525 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1526 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1527 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1528 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1529 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1530 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1531 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1532 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1533 | ingressDevice, |
| 1534 | egressDevice, |
| 1535 | portIngress="", |
| 1536 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1537 | ethType="", |
| 1538 | ethSrc="", |
| 1539 | ethDst="", |
| 1540 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1541 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1542 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1543 | ipProto="", |
| 1544 | ipSrc="", |
| 1545 | ipDst="", |
| 1546 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1547 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1548 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1549 | setVlan="", |
| 1550 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1551 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1552 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1553 | * ingressDevice: device id of ingress device |
| 1554 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1555 | Optional: |
| 1556 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1557 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1558 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1559 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1560 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1561 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1562 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1563 | * ipSrc: specify ip source address |
| 1564 | * ipDst: specify ip destination address |
| 1565 | * tcpSrc: specify tcp source port |
| 1566 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1567 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1568 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1569 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1570 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1571 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1572 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1573 | Returns: |
| 1574 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1575 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1576 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1577 | options developers provide for point-to-point |
| 1578 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1579 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1580 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1581 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1582 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1583 | if ethType: |
| 1584 | cmd += " --ethType " + str( ethType ) |
| 1585 | if ethSrc: |
| 1586 | cmd += " --ethSrc " + str( ethSrc ) |
| 1587 | if ethDst: |
| 1588 | cmd += " --ethDst " + str( ethDst ) |
| 1589 | if bandwidth: |
| 1590 | cmd += " --bandwidth " + str( bandwidth ) |
| 1591 | if lambdaAlloc: |
| 1592 | cmd += " --lambda " |
| 1593 | if ipProto: |
| 1594 | cmd += " --ipProto " + str( ipProto ) |
| 1595 | if ipSrc: |
| 1596 | cmd += " --ipSrc " + str( ipSrc ) |
| 1597 | if ipDst: |
| 1598 | cmd += " --ipDst " + str( ipDst ) |
| 1599 | if tcpSrc: |
| 1600 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1601 | if tcpDst: |
| 1602 | cmd += " --tcpDst " + str( tcpDst ) |
| 1603 | if vlanId: |
| 1604 | cmd += " -v " + str( vlanId ) |
| 1605 | if setVlan: |
| 1606 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1607 | if encap: |
| 1608 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1609 | if protected: |
| 1610 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1611 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1612 | # Check whether the user appended the port |
| 1613 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1614 | if "/" in ingressDevice: |
| 1615 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1616 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1617 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1618 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1619 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1620 | # Would it make sense to throw an exception and exit |
| 1621 | # the test? |
| 1622 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1623 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1624 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1625 | str( ingressDevice ) + "/" +\ |
| 1626 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1627 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1628 | if "/" in egressDevice: |
| 1629 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1630 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1631 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1632 | main.log.error( "You must specify the egress port" ) |
| 1633 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1634 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1635 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1636 | str( egressDevice ) + "/" +\ |
| 1637 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1638 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1639 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1640 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1641 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1642 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1643 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1644 | main.log.error( self.name + ": Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1645 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1646 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1647 | # TODO: print out all the options in this message? |
| 1648 | main.log.info( "Point-to-point intent installed between " + |
| 1649 | str( ingressDevice ) + " and " + |
| 1650 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1651 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1652 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1653 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1654 | else: |
| 1655 | main.log.error( "Error, intent ID not found" ) |
| 1656 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1657 | except AssertionError: |
| 1658 | main.log.exception( "" ) |
| 1659 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1660 | except TypeError: |
| 1661 | main.log.exception( self.name + ": Object not as expected" ) |
| 1662 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1663 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1664 | main.log.error( self.name + ": EOF exception found" ) |
| 1665 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1666 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1667 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1668 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1669 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1670 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1671 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1672 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1673 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1674 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1675 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1676 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1677 | ethType="", |
| 1678 | ethSrc="", |
| 1679 | ethDst="", |
| 1680 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1681 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1682 | ipProto="", |
| 1683 | ipSrc="", |
| 1684 | ipDst="", |
| 1685 | tcpSrc="", |
| 1686 | tcpDst="", |
| 1687 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1688 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1689 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1690 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1691 | partial=False, |
| 1692 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1693 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1694 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1695 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1696 | is same. That is, all ingress devices include port numbers |
| 1697 | with a "/" or all ingress devices could specify device |
| 1698 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1699 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1700 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1701 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1702 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1703 | Optional: |
| 1704 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1705 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1706 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1707 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1708 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1709 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1710 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1711 | * ipSrc: specify ip source address |
| 1712 | * ipDst: specify ip destination address |
| 1713 | * tcpSrc: specify tcp source port |
| 1714 | * tcpDst: specify tcp destination port |
| 1715 | * setEthSrc: action to Rewrite Source MAC Address |
| 1716 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1717 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1718 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1719 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1720 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1721 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1722 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1723 | Returns: |
| 1724 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1725 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1726 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1727 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1728 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1729 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1730 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1731 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1732 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1733 | if ethType: |
| 1734 | cmd += " --ethType " + str( ethType ) |
| 1735 | if ethSrc: |
| 1736 | cmd += " --ethSrc " + str( ethSrc ) |
| 1737 | if ethDst: |
| 1738 | cmd += " --ethDst " + str( ethDst ) |
| 1739 | if bandwidth: |
| 1740 | cmd += " --bandwidth " + str( bandwidth ) |
| 1741 | if lambdaAlloc: |
| 1742 | cmd += " --lambda " |
| 1743 | if ipProto: |
| 1744 | cmd += " --ipProto " + str( ipProto ) |
| 1745 | if ipSrc: |
| 1746 | cmd += " --ipSrc " + str( ipSrc ) |
| 1747 | if ipDst: |
| 1748 | cmd += " --ipDst " + str( ipDst ) |
| 1749 | if tcpSrc: |
| 1750 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1751 | if tcpDst: |
| 1752 | cmd += " --tcpDst " + str( tcpDst ) |
| 1753 | if setEthSrc: |
| 1754 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1755 | if setEthDst: |
| 1756 | cmd += " --setEthDst " + str( setEthDst ) |
| 1757 | if vlanId: |
| 1758 | cmd += " -v " + str( vlanId ) |
| 1759 | if setVlan: |
| 1760 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1761 | if partial: |
| 1762 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1763 | if encap: |
| 1764 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1765 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1766 | # Check whether the user appended the port |
| 1767 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1768 | |
| 1769 | if portIngressList is None: |
| 1770 | for ingressDevice in ingressDeviceList: |
| 1771 | if "/" in ingressDevice: |
| 1772 | cmd += " " + str( ingressDevice ) |
| 1773 | else: |
| 1774 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1775 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1776 | # TODO: perhaps more meaningful return |
| 1777 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1778 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1779 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1780 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1781 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1782 | cmd += " " + \ |
| 1783 | str( ingressDevice ) + "/" +\ |
| 1784 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1785 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1786 | main.log.error( "Device list and port list does not " + |
| 1787 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1788 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1789 | if "/" in egressDevice: |
| 1790 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1791 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1792 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1793 | main.log.error( "You must specify " + |
| 1794 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1795 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1796 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1797 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1798 | str( egressDevice ) + "/" +\ |
| 1799 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1800 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1801 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1802 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1803 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1804 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1805 | main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1806 | "intent" ) |
| 1807 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1808 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1809 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1810 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1811 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1812 | else: |
| 1813 | main.log.error( "Error, intent ID not found" ) |
| 1814 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1815 | except AssertionError: |
| 1816 | main.log.exception( "" ) |
| 1817 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1818 | except TypeError: |
| 1819 | main.log.exception( self.name + ": Object not as expected" ) |
| 1820 | return None |
| 1821 | except pexpect.EOF: |
| 1822 | main.log.error( self.name + ": EOF exception found" ) |
| 1823 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1824 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1825 | except Exception: |
| 1826 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1827 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1828 | |
| 1829 | def addSinglepointToMultipointIntent( |
| 1830 | self, |
| 1831 | ingressDevice, |
| 1832 | egressDeviceList, |
| 1833 | portIngress="", |
| 1834 | portEgressList=None, |
| 1835 | ethType="", |
| 1836 | ethSrc="", |
| 1837 | ethDst="", |
| 1838 | bandwidth="", |
| 1839 | lambdaAlloc=False, |
| 1840 | ipProto="", |
| 1841 | ipSrc="", |
| 1842 | ipDst="", |
| 1843 | tcpSrc="", |
| 1844 | tcpDst="", |
| 1845 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1846 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1847 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1848 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1849 | partial=False, |
| 1850 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1851 | """ |
| 1852 | Note: |
| 1853 | This function assumes the format of all egress devices |
| 1854 | is same. That is, all egress devices include port numbers |
| 1855 | with a "/" or all egress devices could specify device |
| 1856 | ids and port numbers seperately. |
| 1857 | Required: |
| 1858 | * EgressDeviceList: List of device ids of egress device |
| 1859 | ( Atleast 2 eress devices required in the list ) |
| 1860 | * ingressDevice: device id of ingress device |
| 1861 | Optional: |
| 1862 | * ethType: specify ethType |
| 1863 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1864 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1865 | * bandwidth: specify bandwidth capacity of link |
| 1866 | * lambdaAlloc: if True, intent will allocate lambda |
| 1867 | for the specified intent |
| 1868 | * ipProto: specify ip protocol |
| 1869 | * ipSrc: specify ip source address |
| 1870 | * ipDst: specify ip destination address |
| 1871 | * tcpSrc: specify tcp source port |
| 1872 | * tcpDst: specify tcp destination port |
| 1873 | * setEthSrc: action to Rewrite Source MAC Address |
| 1874 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1875 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1876 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1877 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1878 | Description: |
| 1879 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1880 | specifying device id's and optional fields |
| 1881 | Returns: |
| 1882 | A string of the intent id or None on error |
| 1883 | |
| 1884 | NOTE: This function may change depending on the |
| 1885 | options developers provide for singlepoint-to-multipoint |
| 1886 | intent via cli |
| 1887 | """ |
| 1888 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1889 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1890 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1891 | if ethType: |
| 1892 | cmd += " --ethType " + str( ethType ) |
| 1893 | if ethSrc: |
| 1894 | cmd += " --ethSrc " + str( ethSrc ) |
| 1895 | if ethDst: |
| 1896 | cmd += " --ethDst " + str( ethDst ) |
| 1897 | if bandwidth: |
| 1898 | cmd += " --bandwidth " + str( bandwidth ) |
| 1899 | if lambdaAlloc: |
| 1900 | cmd += " --lambda " |
| 1901 | if ipProto: |
| 1902 | cmd += " --ipProto " + str( ipProto ) |
| 1903 | if ipSrc: |
| 1904 | cmd += " --ipSrc " + str( ipSrc ) |
| 1905 | if ipDst: |
| 1906 | cmd += " --ipDst " + str( ipDst ) |
| 1907 | if tcpSrc: |
| 1908 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1909 | if tcpDst: |
| 1910 | cmd += " --tcpDst " + str( tcpDst ) |
| 1911 | if setEthSrc: |
| 1912 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1913 | if setEthDst: |
| 1914 | cmd += " --setEthDst " + str( setEthDst ) |
| 1915 | if vlanId: |
| 1916 | cmd += " -v " + str( vlanId ) |
| 1917 | if setVlan: |
| 1918 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1919 | if partial: |
| 1920 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1921 | if encap: |
| 1922 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1923 | |
| 1924 | # Check whether the user appended the port |
| 1925 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1926 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1927 | if "/" in ingressDevice: |
| 1928 | cmd += " " + str( ingressDevice ) |
| 1929 | else: |
| 1930 | if not portIngress: |
| 1931 | main.log.error( "You must specify " + |
| 1932 | "the Ingress port" ) |
| 1933 | return main.FALSE |
| 1934 | |
| 1935 | cmd += " " +\ |
| 1936 | str( ingressDevice ) + "/" +\ |
| 1937 | str( portIngress ) |
| 1938 | |
| 1939 | if portEgressList is None: |
| 1940 | for egressDevice in egressDeviceList: |
| 1941 | if "/" in egressDevice: |
| 1942 | cmd += " " + str( egressDevice ) |
| 1943 | else: |
| 1944 | main.log.error( "You must specify " + |
| 1945 | "the egress port" ) |
| 1946 | # TODO: perhaps more meaningful return |
| 1947 | return main.FALSE |
| 1948 | else: |
| 1949 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1950 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1951 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1952 | cmd += " " + \ |
| 1953 | str( egressDevice ) + "/" +\ |
| 1954 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1955 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1956 | main.log.error( "Device list and port list does not " + |
| 1957 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1958 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1959 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1960 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1961 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1962 | # If error, return error message |
| 1963 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1964 | main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " + |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1965 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1966 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1967 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1968 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1969 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1970 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1971 | else: |
| 1972 | main.log.error( "Error, intent ID not found" ) |
| 1973 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1974 | except AssertionError: |
| 1975 | main.log.exception( "" ) |
| 1976 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1977 | except TypeError: |
| 1978 | main.log.exception( self.name + ": Object not as expected" ) |
| 1979 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1980 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1981 | main.log.error( self.name + ": EOF exception found" ) |
| 1982 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1983 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1984 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1985 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1986 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1987 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1988 | def addMplsIntent( |
| 1989 | self, |
| 1990 | ingressDevice, |
| 1991 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1992 | ingressPort="", |
| 1993 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1994 | ethType="", |
| 1995 | ethSrc="", |
| 1996 | ethDst="", |
| 1997 | bandwidth="", |
| 1998 | lambdaAlloc=False, |
| 1999 | ipProto="", |
| 2000 | ipSrc="", |
| 2001 | ipDst="", |
| 2002 | tcpSrc="", |
| 2003 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2004 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 2005 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2006 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2007 | """ |
| 2008 | Required: |
| 2009 | * ingressDevice: device id of ingress device |
| 2010 | * egressDevice: device id of egress device |
| 2011 | Optional: |
| 2012 | * ethType: specify ethType |
| 2013 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 2014 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 2015 | * bandwidth: specify bandwidth capacity of link |
| 2016 | * lambdaAlloc: if True, intent will allocate lambda |
| 2017 | for the specified intent |
| 2018 | * ipProto: specify ip protocol |
| 2019 | * ipSrc: specify ip source address |
| 2020 | * ipDst: specify ip destination address |
| 2021 | * tcpSrc: specify tcp source port |
| 2022 | * tcpDst: specify tcp destination port |
| 2023 | * ingressLabel: Ingress MPLS label |
| 2024 | * egressLabel: Egress MPLS label |
| 2025 | Description: |
| 2026 | Adds MPLS intent by |
| 2027 | specifying device id's and optional fields |
| 2028 | Returns: |
| 2029 | A string of the intent id or None on error |
| 2030 | |
| 2031 | NOTE: This function may change depending on the |
| 2032 | options developers provide for MPLS |
| 2033 | intent via cli |
| 2034 | """ |
| 2035 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 2036 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2037 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 2038 | if ethType: |
| 2039 | cmd += " --ethType " + str( ethType ) |
| 2040 | if ethSrc: |
| 2041 | cmd += " --ethSrc " + str( ethSrc ) |
| 2042 | if ethDst: |
| 2043 | cmd += " --ethDst " + str( ethDst ) |
| 2044 | if bandwidth: |
| 2045 | cmd += " --bandwidth " + str( bandwidth ) |
| 2046 | if lambdaAlloc: |
| 2047 | cmd += " --lambda " |
| 2048 | if ipProto: |
| 2049 | cmd += " --ipProto " + str( ipProto ) |
| 2050 | if ipSrc: |
| 2051 | cmd += " --ipSrc " + str( ipSrc ) |
| 2052 | if ipDst: |
| 2053 | cmd += " --ipDst " + str( ipDst ) |
| 2054 | if tcpSrc: |
| 2055 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 2056 | if tcpDst: |
| 2057 | cmd += " --tcpDst " + str( tcpDst ) |
| 2058 | if ingressLabel: |
| 2059 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 2060 | if egressLabel: |
| 2061 | cmd += " --egressLabel " + str( egressLabel ) |
| 2062 | if priority: |
| 2063 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2064 | |
| 2065 | # Check whether the user appended the port |
| 2066 | # or provided it as an input |
| 2067 | if "/" in ingressDevice: |
| 2068 | cmd += " " + str( ingressDevice ) |
| 2069 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2070 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2071 | main.log.error( "You must specify the ingress port" ) |
| 2072 | return None |
| 2073 | |
| 2074 | cmd += " " + \ |
| 2075 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2076 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2077 | |
| 2078 | if "/" in egressDevice: |
| 2079 | cmd += " " + str( egressDevice ) |
| 2080 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2081 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2082 | main.log.error( "You must specify the egress port" ) |
| 2083 | return None |
| 2084 | |
| 2085 | cmd += " " +\ |
| 2086 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2087 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2088 | |
| 2089 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2090 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2091 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2092 | # If error, return error message |
| 2093 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2094 | main.log.error( self.name + ": Error in adding mpls intent" ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2095 | return None |
| 2096 | else: |
| 2097 | # TODO: print out all the options in this message? |
| 2098 | main.log.info( "MPLS intent installed between " + |
| 2099 | str( ingressDevice ) + " and " + |
| 2100 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2101 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2102 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2103 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2104 | else: |
| 2105 | main.log.error( "Error, intent ID not found" ) |
| 2106 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2107 | except AssertionError: |
| 2108 | main.log.exception( "" ) |
| 2109 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2110 | except TypeError: |
| 2111 | main.log.exception( self.name + ": Object not as expected" ) |
| 2112 | return None |
| 2113 | except pexpect.EOF: |
| 2114 | main.log.error( self.name + ": EOF exception found" ) |
| 2115 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2116 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2117 | except Exception: |
| 2118 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2119 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2120 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2121 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 2122 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2123 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2124 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2125 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2126 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2127 | -p or --purge: Purge the intent from the store after removal |
| 2128 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2129 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 2130 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2131 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2132 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2133 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2134 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2135 | if purge: |
| 2136 | cmdStr += " -p" |
| 2137 | if sync: |
| 2138 | cmdStr += " -s" |
| 2139 | |
| 2140 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2141 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2142 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2143 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2144 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2145 | main.log.error( self.name + ": Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2146 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2147 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2148 | # TODO: Should this be main.TRUE |
| 2149 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2150 | except AssertionError: |
| 2151 | main.log.exception( "" ) |
| 2152 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2153 | except TypeError: |
| 2154 | main.log.exception( self.name + ": Object not as expected" ) |
| 2155 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2156 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2157 | main.log.error( self.name + ": EOF exception found" ) |
| 2158 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2159 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2160 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2161 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2162 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2163 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2164 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2165 | """ |
| 2166 | Description: |
| 2167 | Remove all the intents |
| 2168 | Optional args:- |
| 2169 | -s or --sync: Waits for the removal before returning |
| 2170 | -p or --purge: Purge the intent from the store after removal |
| 2171 | Returns: |
| 2172 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2173 | main.FALSE; Returns None for exception |
| 2174 | """ |
| 2175 | try: |
| 2176 | cmdStr = "remove-intent" |
| 2177 | if purge: |
| 2178 | cmdStr += " -p" |
| 2179 | if sync: |
| 2180 | cmdStr += " -s" |
| 2181 | |
| 2182 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2183 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2184 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2185 | assert "Command not found:" not in handle, handle |
| 2186 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2187 | main.log.error( self.name + ": Error in removing intent" ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2188 | return main.FALSE |
| 2189 | else: |
| 2190 | return main.TRUE |
| 2191 | except AssertionError: |
| 2192 | main.log.exception( "" ) |
| 2193 | return None |
| 2194 | except TypeError: |
| 2195 | main.log.exception( self.name + ": Object not as expected" ) |
| 2196 | return None |
| 2197 | except pexpect.EOF: |
| 2198 | main.log.error( self.name + ": EOF exception found" ) |
| 2199 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2200 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2201 | except Exception: |
| 2202 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2203 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2204 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2205 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2206 | """ |
| 2207 | Purges all WITHDRAWN Intents |
| 2208 | """ |
| 2209 | try: |
| 2210 | cmdStr = "purge-intents" |
| 2211 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2212 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2213 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2214 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2215 | main.log.error( self.name + ": Error in purging intents" ) |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2216 | return main.FALSE |
| 2217 | else: |
| 2218 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2219 | except AssertionError: |
| 2220 | main.log.exception( "" ) |
| 2221 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2222 | except TypeError: |
| 2223 | main.log.exception( self.name + ": Object not as expected" ) |
| 2224 | return None |
| 2225 | except pexpect.EOF: |
| 2226 | main.log.error( self.name + ": EOF exception found" ) |
| 2227 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2228 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2229 | except Exception: |
| 2230 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2231 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2232 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2233 | def wipeout( self ): |
| 2234 | """ |
| 2235 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2236 | """ |
| 2237 | try: |
| 2238 | cmdStr = "wipe-out please" |
| 2239 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2240 | assert handle is not None, "Error in sendline" |
| 2241 | assert "Command not found:" not in handle, handle |
| 2242 | return main.TRUE |
| 2243 | except AssertionError: |
| 2244 | main.log.exception( "" ) |
| 2245 | return None |
| 2246 | except TypeError: |
| 2247 | main.log.exception( self.name + ": Object not as expected" ) |
| 2248 | return None |
| 2249 | except pexpect.EOF: |
| 2250 | main.log.error( self.name + ": EOF exception found" ) |
| 2251 | main.log.error( self.name + ": " + self.handle.before ) |
| 2252 | main.cleanAndExit() |
| 2253 | except Exception: |
| 2254 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2255 | main.cleanAndExit() |
| 2256 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2257 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2258 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2259 | NOTE: This method should be used after installing application: |
| 2260 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2261 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2262 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2263 | Description: |
| 2264 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2265 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2266 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2267 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2268 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2269 | cmdStr += " -j" |
| 2270 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2271 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2272 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2273 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2274 | except AssertionError: |
| 2275 | main.log.exception( "" ) |
| 2276 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2277 | except TypeError: |
| 2278 | main.log.exception( self.name + ": Object not as expected" ) |
| 2279 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2280 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2281 | main.log.error( self.name + ": EOF exception found" ) |
| 2282 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2283 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2284 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2285 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2286 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2287 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2288 | def ipv4RouteNumber( self ): |
| 2289 | """ |
| 2290 | NOTE: This method should be used after installing application: |
| 2291 | onos-app-sdnip |
| 2292 | Description: |
| 2293 | Obtain the total IPv4 routes number in the system |
| 2294 | """ |
| 2295 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2296 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2297 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2298 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2299 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2300 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2301 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2302 | except AssertionError: |
| 2303 | main.log.exception( "" ) |
| 2304 | return None |
| 2305 | except ( TypeError, ValueError ): |
| 2306 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2307 | return None |
| 2308 | except pexpect.EOF: |
| 2309 | main.log.error( self.name + ": EOF exception found" ) |
| 2310 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2311 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2312 | except Exception: |
| 2313 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2314 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2315 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2316 | # =============Function to check Bandwidth allocation======== |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2317 | def allocations( self, jsonFormat = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2318 | """ |
| 2319 | Description: |
| 2320 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2321 | """ |
| 2322 | try: |
| 2323 | cmdStr = "allocations" |
| 2324 | if jsonFormat: |
| 2325 | cmdStr += " -j" |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2326 | handle = self.sendline( cmdStr, timeout=300 ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2327 | assert handle is not None, "Error in sendline" |
| 2328 | assert "Command not found:" not in handle, handle |
| 2329 | return handle |
| 2330 | except AssertionError: |
| 2331 | main.log.exception( "" ) |
| 2332 | return None |
| 2333 | except ( TypeError, ValueError ): |
| 2334 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2335 | return None |
| 2336 | except pexpect.EOF: |
| 2337 | main.log.error( self.name + ": EOF exception found" ) |
| 2338 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2339 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2340 | except Exception: |
| 2341 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2342 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2343 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2344 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2345 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2346 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2347 | Obtain intents from the ONOS cli. |
| 2348 | Optional: |
| 2349 | * jsonFormat: Enable output formatting in json, default to True |
| 2350 | * summary: Whether only output the intent summary, defaults to False |
| 2351 | * type: Only output a certain type of intent. This options is valid |
| 2352 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2353 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2354 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2355 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2356 | if summary: |
| 2357 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2358 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2359 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2360 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2361 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2362 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2363 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2364 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2365 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2366 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2367 | intentType = "" |
| 2368 | # IF we want the summary of a specific intent type |
| 2369 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2370 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2371 | if intentType in jsonResult.keys(): |
| 2372 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2373 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2374 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2375 | return handle |
| 2376 | else: |
| 2377 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2378 | except AssertionError: |
| 2379 | main.log.exception( "" ) |
| 2380 | return None |
| 2381 | except ( TypeError, ValueError ): |
| 2382 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2383 | return None |
| 2384 | except pexpect.EOF: |
| 2385 | main.log.error( self.name + ": EOF exception found" ) |
| 2386 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2387 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2388 | except Exception: |
| 2389 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2390 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2391 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2392 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2393 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2394 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2395 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2396 | list of intent IDs. |
| 2397 | Parameters: |
| 2398 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2399 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2400 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2401 | Returns the state (string type) of the ID if a single intent ID is |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2402 | accepted. |
| 2403 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2404 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2405 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2406 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2407 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2408 | try: |
| 2409 | state = "State is Undefined" |
| 2410 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2411 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2412 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2413 | rawJson = intentsJson |
| 2414 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2415 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2416 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2417 | if intentsId == intent[ 'id' ]: |
| 2418 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2419 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2420 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2421 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2422 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2423 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2424 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2425 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2426 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2427 | for intent in parsedIntentsJson: |
| 2428 | if intentsId[ i ] == intent[ 'id' ]: |
| 2429 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2430 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2431 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2432 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2433 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2434 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2435 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2436 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2437 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2438 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2439 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2440 | except ( TypeError, ValueError ): |
| 2441 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2442 | return None |
| 2443 | except pexpect.EOF: |
| 2444 | main.log.error( self.name + ": EOF exception found" ) |
| 2445 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2446 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2447 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2448 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2449 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2450 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2451 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2452 | """ |
| 2453 | Description: |
| 2454 | Check intents state |
| 2455 | Required: |
| 2456 | intentsId - List of intents ID to be checked |
| 2457 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2458 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2459 | state in the list. |
| 2460 | *NOTE: You can pass in a list of expected state, |
| 2461 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2462 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2463 | Returns main.TRUE only if all intent are the same as expected states, |
| 2464 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2465 | """ |
| 2466 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2467 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2468 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2469 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2470 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2471 | intentsDict = [] |
| 2472 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2473 | if isinstance( intentsId, types.StringType ) \ |
| 2474 | and intent.get( 'id' ) == intentsId: |
| 2475 | intentsDict.append( intent ) |
| 2476 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2477 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2478 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2479 | |
| 2480 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2481 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2482 | "getting intents state" ) |
| 2483 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2484 | |
| 2485 | if isinstance( expectedState, types.StringType ): |
| 2486 | for intents in intentsDict: |
| 2487 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2488 | main.log.debug( self.name + " : Intent ID - " + |
| 2489 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2490 | " actual state = " + |
| 2491 | intents.get( 'state' ) |
| 2492 | + " does not equal expected state = " |
| 2493 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2494 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2495 | elif isinstance( expectedState, types.ListType ): |
| 2496 | for intents in intentsDict: |
| 2497 | if not any( state == intents.get( 'state' ) for state in |
| 2498 | expectedState ): |
| 2499 | main.log.debug( self.name + " : Intent ID - " + |
| 2500 | intents.get( 'id' ) + |
| 2501 | " actual state = " + |
| 2502 | intents.get( 'state' ) + |
| 2503 | " does not equal expected states = " |
| 2504 | + str( expectedState ) ) |
| 2505 | returnValue = main.FALSE |
| 2506 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2507 | if returnValue == main.TRUE: |
| 2508 | main.log.info( self.name + ": All " + |
| 2509 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2510 | " intents are in " + str( expectedState ) + |
| 2511 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2512 | return returnValue |
| 2513 | except TypeError: |
| 2514 | main.log.exception( self.name + ": Object not as expected" ) |
| 2515 | return None |
| 2516 | except pexpect.EOF: |
| 2517 | main.log.error( self.name + ": EOF exception found" ) |
| 2518 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2519 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2520 | except Exception: |
| 2521 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2522 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2523 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2524 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2525 | """ |
| 2526 | Description: |
| 2527 | Compare the allocated bandwidth with the given allocations |
| 2528 | Required: |
| 2529 | expectedAllocations - The expected ONOS output of the allocations command |
| 2530 | Return: |
| 2531 | Returns main.TRUE only if all intent are the same as expected states, |
| 2532 | otherwise returns main.FALSE. |
| 2533 | """ |
| 2534 | # FIXME: Convert these string comparisons to object comparisons |
| 2535 | try: |
| 2536 | returnValue = main.TRUE |
| 2537 | bandwidthFailed = False |
| 2538 | rawAlloc = self.allocations() |
| 2539 | expectedFormat = StringIO( expectedAllocations ) |
| 2540 | ONOSOutput = StringIO( rawAlloc ) |
| 2541 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2542 | str( expectedFormat ) ) ) |
| 2543 | |
| 2544 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2545 | actual = actual.rstrip() |
| 2546 | expected = expected.rstrip() |
| 2547 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2548 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2549 | marker1 = actual.find( 'allocated' ) |
| 2550 | m1 = actual[ :marker1 ] |
| 2551 | marker2 = expected.find( 'allocated' ) |
| 2552 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2553 | if m1 != m2: |
| 2554 | bandwidthFailed = True |
| 2555 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2556 | bandwidthFailed = True |
| 2557 | expectedFormat.close() |
| 2558 | ONOSOutput.close() |
| 2559 | |
| 2560 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2561 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2562 | returnValue = main.FALSE |
| 2563 | return returnValue |
| 2564 | except TypeError: |
| 2565 | main.log.exception( self.name + ": Object not as expected" ) |
| 2566 | return None |
| 2567 | except pexpect.EOF: |
| 2568 | main.log.error( self.name + ": EOF exception found" ) |
| 2569 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2570 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2571 | except Exception: |
| 2572 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2573 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2574 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2575 | def compareIntent( self, intentDict ): |
| 2576 | """ |
| 2577 | Description: |
| 2578 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2579 | Return: |
| 2580 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2581 | Arguments: |
| 2582 | intentDict: a dictionary which maps intent ids to intent states |
| 2583 | """ |
| 2584 | try: |
| 2585 | intentsRaw = self.intents() |
| 2586 | intentsJson = json.loads( intentsRaw ) |
| 2587 | intentDictONOS = {} |
| 2588 | for intent in intentsJson: |
| 2589 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2590 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2591 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2592 | main.log.warn( self.name + ": expected intent count does not match that in ONOS, " + |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2593 | str( len( intentDict ) ) + " expected and " + |
| 2594 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2595 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2596 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2597 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2598 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2599 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2600 | else: |
| 2601 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2602 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2603 | " expected state is " + intentDict[ intentID ] + |
| 2604 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2605 | returnValue = main.FALSE |
| 2606 | intentDictONOS.pop( intentID ) |
| 2607 | if len( intentDictONOS ) > 0: |
| 2608 | returnValue = main.FALSE |
| 2609 | for intentID in intentDictONOS.keys(): |
| 2610 | main.log.debug( self.name + ": find extra intent in ONOS: intent ID " + intentID ) |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2611 | if returnValue == main.TRUE: |
| 2612 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2613 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2614 | except KeyError: |
| 2615 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2616 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2617 | except ( TypeError, ValueError ): |
| 2618 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2619 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2620 | except pexpect.EOF: |
| 2621 | main.log.error( self.name + ": EOF exception found" ) |
| 2622 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2623 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2624 | except Exception: |
| 2625 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2626 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2627 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2628 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2629 | """ |
| 2630 | Description: |
| 2631 | Check the number of installed intents. |
| 2632 | Optional: |
| 2633 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2634 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2635 | Return: |
| 2636 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2637 | , otherwise, returns main.FALSE. |
| 2638 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2639 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2640 | try: |
| 2641 | cmd = "intents -s -j" |
| 2642 | |
| 2643 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2644 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2645 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2646 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2647 | response = json.loads( response ) |
| 2648 | |
| 2649 | # get total and installed number, see if they are match |
| 2650 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2651 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2652 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2653 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2654 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2655 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2656 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2657 | return main.FALSE |
| 2658 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2659 | except ( TypeError, ValueError ): |
| 2660 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2661 | return None |
| 2662 | except pexpect.EOF: |
| 2663 | main.log.error( self.name + ": EOF exception found" ) |
| 2664 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2665 | if noExit: |
| 2666 | return main.FALSE |
| 2667 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2668 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2669 | except pexpect.TIMEOUT: |
| 2670 | main.log.error( self.name + ": ONOS timeout" ) |
| 2671 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2672 | except Exception: |
| 2673 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2674 | if noExit: |
| 2675 | return main.FALSE |
| 2676 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2677 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2678 | |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2679 | 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] | 2680 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2681 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2682 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2683 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2684 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2685 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2686 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2687 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2688 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2689 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2690 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2691 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2692 | cmdStr += " -n" |
| 2693 | cmdStr += " " + state |
| 2694 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2695 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2696 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2697 | assert "Command not found:" not in handle, handle |
| 2698 | if re.search( "Error:", handle ): |
| 2699 | main.log.error( self.name + ": flows() response: " + |
| 2700 | str( handle ) ) |
| 2701 | return handle |
| 2702 | except AssertionError: |
| 2703 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2704 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2705 | except TypeError: |
| 2706 | main.log.exception( self.name + ": Object not as expected" ) |
| 2707 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2708 | except pexpect.TIMEOUT: |
| 2709 | main.log.error( self.name + ": ONOS timeout" ) |
| 2710 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2711 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2712 | main.log.error( self.name + ": EOF exception found" ) |
| 2713 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2714 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2715 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2716 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2717 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2718 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2719 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2720 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2721 | count = int( count ) if count else 0 |
steven30801 | eccfe21 | 2019-01-24 13:00:42 +0800 | [diff] [blame] | 2722 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 2723 | return count if ( count >= min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2724 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2725 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2726 | """ |
| 2727 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2728 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2729 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2730 | if the count of those states is 0, which means all current flows |
| 2731 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2732 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2733 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2734 | Return: |
| 2735 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2736 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2737 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2738 | """ |
| 2739 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2740 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2741 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2742 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2743 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2744 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2745 | if rawFlows: |
| 2746 | # if we didn't get flows or flows function return None, we should return |
| 2747 | # main.Flase |
| 2748 | checkedStates.append( json.loads( rawFlows ) ) |
| 2749 | else: |
| 2750 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2751 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2752 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2753 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2754 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2755 | except TypeError: |
| 2756 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2757 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2758 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2759 | # We want to count PENDING_ADD if isPENDING is true |
| 2760 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2761 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2762 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2763 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2764 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2765 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2766 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2767 | except ( TypeError, ValueError ): |
| 2768 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2769 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2770 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2771 | except AssertionError: |
| 2772 | main.log.exception( "" ) |
| 2773 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2774 | except pexpect.TIMEOUT: |
| 2775 | main.log.error( self.name + ": ONOS timeout" ) |
| 2776 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2777 | except pexpect.EOF: |
| 2778 | main.log.error( self.name + ": EOF exception found" ) |
| 2779 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2780 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2781 | except Exception: |
| 2782 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2783 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2784 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2785 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2786 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2787 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2788 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2789 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2790 | a specific point-to-point intent definition |
| 2791 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2792 | * ingress: specify source dpid |
| 2793 | * egress: specify destination dpid |
| 2794 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2795 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2796 | * offset: the keyOffset is where the next batch of intents |
| 2797 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2798 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2799 | * getResponse: If set to True, function will return ONOS response. |
| 2800 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2801 | Returns: If failed to push test intents, it will returen None, |
| 2802 | if successful, return true. |
| 2803 | Timeout expection will return None, |
| 2804 | TypeError will return false |
| 2805 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2806 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2807 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2808 | if background: |
| 2809 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2810 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2811 | back = "" |
| 2812 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2813 | ingress, |
| 2814 | egress, |
| 2815 | batchSize, |
| 2816 | offset, |
| 2817 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2818 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2819 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2820 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2821 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2822 | if getResponse: |
| 2823 | return response |
| 2824 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2825 | # TODO: We should handle if there is failure in installation |
| 2826 | return main.TRUE |
| 2827 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2828 | except AssertionError: |
| 2829 | main.log.exception( "" ) |
| 2830 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2831 | except pexpect.TIMEOUT: |
| 2832 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2833 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2834 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2835 | main.log.error( self.name + ": EOF exception found" ) |
| 2836 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2837 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2838 | except TypeError: |
| 2839 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2840 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2841 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2842 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2843 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2844 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2845 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2846 | """ |
| 2847 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2848 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2849 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2850 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2851 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2852 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2853 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2854 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2855 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2856 | cmd = "flows -c added" |
| 2857 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2858 | if rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2859 | rawFlows = rawFlows.split( "\n" ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2860 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2861 | for l in rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2862 | totalFlows += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2863 | else: |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 2864 | main.log.warn( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2865 | return None |
| 2866 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2867 | |
You Wang | d3097f7 | 2018-12-12 11:56:03 -0800 | [diff] [blame] | 2868 | except IndexError: |
| 2869 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
| 2870 | main.log.debug( "rawFlows: {}".format( rawFlows ) ) |
| 2871 | return None |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2872 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2873 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2874 | return None |
| 2875 | except pexpect.EOF: |
| 2876 | main.log.error( self.name + ": EOF exception found" ) |
| 2877 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2878 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2879 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2880 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2881 | except pexpect.TIMEOUT: |
| 2882 | main.log.error( self.name + ": ONOS timeout" ) |
| 2883 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2884 | except Exception: |
| 2885 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2886 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2887 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2888 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2889 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2890 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2891 | """ |
| 2892 | Description: |
| 2893 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2894 | Optional: |
| 2895 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2896 | Return: |
| 2897 | The number of intents |
| 2898 | """ |
| 2899 | try: |
| 2900 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2901 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2902 | if response is None: |
| 2903 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2904 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2905 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2906 | except ( TypeError, ValueError ): |
| 2907 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2908 | return None |
| 2909 | except pexpect.EOF: |
| 2910 | main.log.error( self.name + ": EOF exception found" ) |
| 2911 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2912 | if noExit: |
| 2913 | return -1 |
| 2914 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2915 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2916 | except Exception: |
| 2917 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2918 | if noExit: |
| 2919 | return -1 |
| 2920 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2921 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2922 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2923 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2924 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2925 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2926 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2927 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2928 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2929 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2930 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2931 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2932 | cmdStr += " -j" |
| 2933 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2934 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2935 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2936 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2937 | except AssertionError: |
| 2938 | main.log.exception( "" ) |
| 2939 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2940 | except TypeError: |
| 2941 | main.log.exception( self.name + ": Object not as expected" ) |
| 2942 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2943 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2944 | main.log.error( self.name + ": EOF exception found" ) |
| 2945 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2946 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2947 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2948 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2949 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2950 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2951 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2952 | """ |
| 2953 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2954 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2955 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2956 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2957 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2958 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2959 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2960 | cmdStr += " -j" |
| 2961 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2962 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2963 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2964 | if handle: |
| 2965 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2966 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2967 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2968 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2969 | else: |
| 2970 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2971 | except AssertionError: |
| 2972 | main.log.exception( "" ) |
| 2973 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2974 | except TypeError: |
| 2975 | main.log.exception( self.name + ": Object not as expected" ) |
| 2976 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2977 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2978 | main.log.error( self.name + ": EOF exception found" ) |
| 2979 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2980 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2981 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2982 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2983 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2984 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2985 | # Wrapper functions **************** |
| 2986 | # Wrapper functions use existing driver |
| 2987 | # functions and extends their use case. |
| 2988 | # For example, we may use the output of |
| 2989 | # a normal driver function, and parse it |
| 2990 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2991 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2992 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2993 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2994 | Description: |
| 2995 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2996 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2997 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2998 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2999 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3000 | if intentsStr is None: |
| 3001 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 3002 | # Convert to a dictionary |
| 3003 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3004 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 3005 | for intent in intents: |
| 3006 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3007 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3008 | except TypeError: |
| 3009 | main.log.exception( self.name + ": Object not as expected" ) |
| 3010 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 3011 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3012 | main.log.error( self.name + ": EOF exception found" ) |
| 3013 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3014 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3015 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3016 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3017 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 3018 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3019 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3020 | """ |
| 3021 | Determine the number of flow rules for the given device id that are |
| 3022 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3023 | Params: |
| 3024 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3025 | """ |
| 3026 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3027 | if core: |
| 3028 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 3029 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3030 | else: |
| 3031 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 3032 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3033 | handle = self.lineCount( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3034 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3035 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3036 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3037 | except AssertionError: |
| 3038 | main.log.exception( "" ) |
| 3039 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3040 | except pexpect.EOF: |
| 3041 | main.log.error( self.name + ": EOF exception found" ) |
| 3042 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3043 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3044 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3045 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3046 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 3047 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3048 | def groupAddedCount( self, deviceId, core=False ): |
| 3049 | """ |
| 3050 | Determine the number of group rules for the given device id that are |
| 3051 | in the added state |
| 3052 | Params: |
| 3053 | core: if True, only return the number of core groups added |
| 3054 | """ |
| 3055 | try: |
| 3056 | if core: |
| 3057 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3058 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3059 | else: |
| 3060 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3061 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3062 | handle = self.lineCount( cmdStr ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 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 | |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame^] | 3077 | def sendOnosCliCommand( self, cmd, params=[] ): |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3078 | """ |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame^] | 3079 | Handles sending an arbitrary command to the ONOS cli and parsing standard errors |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3080 | Params: |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame^] | 3081 | cmd: The command to be sent in the onos cli |
| 3082 | params: Sending the parameters within the command we send in the cli |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3083 | """ |
| 3084 | try: |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame^] | 3085 | if not isinstance(params, list): |
| 3086 | params=[params] |
| 3087 | cmdStr = cmd + " " + ' '.join(params) |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3088 | handle = self.sendline( cmdStr ) |
| 3089 | assert handle is not None, "Error in sendline" |
| 3090 | assert "Command not found:" not in handle, handle |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame^] | 3091 | # Check for error in cli response |
| 3092 | assert "Error executing command" not in handle, handle |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3093 | return handle |
| 3094 | except AssertionError: |
| 3095 | main.log.exception( "" ) |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame^] | 3096 | return main.FALSE |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3097 | except pexpect.EOF: |
| 3098 | main.log.error( self.name + ": EOF exception found" ) |
| 3099 | main.log.error( self.name + ": " + self.handle.before ) |
| 3100 | main.cleanAndExit() |
| 3101 | except Exception: |
| 3102 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3103 | main.cleanAndExit() |
| 3104 | |
Siddesh | 713c4c5 | 2022-01-14 22:08:05 +0000 | [diff] [blame^] | 3105 | def blackholeStaticRoute( self, subnet, addBlackhole=True ): |
| 3106 | """ |
| 3107 | Manage black hole routes in onos. |
| 3108 | Params: |
| 3109 | subnet: The subnet to be blocked through this route |
| 3110 | addBlackhole: Boolean set to declare if we are adding or removing the blackhole route |
| 3111 | """ |
| 3112 | if addBlackhole: |
| 3113 | addedBlackholeIP = self.sendOnosCliCommand('sr-blackhole add', subnet) |
| 3114 | else: |
| 3115 | removedBlackholeIP = self.sendOnosCliCommand('sr-blackhole remove', subnet) |
| 3116 | blackHoleList = self.sendOnosCliCommand('sr-blackhole list') |
| 3117 | if subnet in blackHoleList: |
| 3118 | exists = True |
| 3119 | else: |
| 3120 | exists = False |
| 3121 | if (addBlackhole and exists) or (not addBlackhole and not exists): |
| 3122 | return main.TRUE |
| 3123 | else: |
| 3124 | return main.FALSE |
| 3125 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3126 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 3127 | """ |
| 3128 | Description: |
| 3129 | Check whether the number of groups for the given device id that |
| 3130 | are in ADDED state is bigger than minGroupCount. |
| 3131 | Required: |
| 3132 | * deviceId: device id to check the number of added group rules |
| 3133 | Optional: |
| 3134 | * minGroupCount: the number of groups to compare |
| 3135 | * core: if True, only check the number of core groups added |
| 3136 | * comparison: if 0, compare with greater than minFlowCount |
| 3137 | * if 1, compare with equal to minFlowCount |
| 3138 | Return: |
| 3139 | Returns the number of groups if it is bigger than minGroupCount, |
| 3140 | returns main.FALSE otherwise. |
| 3141 | """ |
| 3142 | count = self.groupAddedCount( deviceId, core ) |
| 3143 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3144 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3145 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 3146 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3147 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3148 | """ |
| 3149 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3150 | deviceId: Id of the device from which we retrieve groups |
| 3151 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3152 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3153 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3154 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 3155 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3156 | assert handle is not None, "Error in sendline" |
| 3157 | assert "Command not found:" not in handle, handle |
| 3158 | return handle |
| 3159 | except AssertionError: |
| 3160 | main.log.exception( "" ) |
| 3161 | return None |
| 3162 | except TypeError: |
| 3163 | main.log.exception( self.name + ": Object not as expected" ) |
| 3164 | return None |
| 3165 | except pexpect.EOF: |
| 3166 | main.log.error( self.name + ": EOF exception found" ) |
| 3167 | main.log.error( self.name + ": " + self.handle.before ) |
| 3168 | main.cleanAndExit() |
| 3169 | except Exception: |
| 3170 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3171 | main.cleanAndExit() |
| 3172 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3173 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3174 | """ |
| 3175 | Description: |
| 3176 | Check whether the number of flow rules for the given device id that |
| 3177 | are in ADDED state is bigger than minFlowCount. |
| 3178 | Required: |
| 3179 | * deviceId: device id to check the number of added flow rules |
| 3180 | Optional: |
| 3181 | * minFlowCount: the number of flow rules to compare |
| 3182 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3183 | * comparison: if 0, compare with greater than minFlowCount |
| 3184 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3185 | Return: |
| 3186 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 3187 | returns main.FALSE otherwise. |
| 3188 | """ |
| 3189 | count = self.flowAddedCount( deviceId, core ) |
| 3190 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3191 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3192 | 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] | 3193 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3194 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3195 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3196 | Use 'devices' function to obtain list of all devices |
| 3197 | and parse the result to obtain a list of all device |
| 3198 | id's. Returns this list. Returns empty list if no |
| 3199 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3200 | List is ordered sequentially |
| 3201 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3202 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3203 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3204 | the ids. By obtaining the list of device ids on the fly, |
| 3205 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3206 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3207 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3208 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3209 | devicesStr = self.devices( jsonFormat=False ) |
| 3210 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3211 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3212 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3213 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3214 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3215 | |
| 3216 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3217 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3218 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3219 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3220 | # Split list further into arguments before and after string |
| 3221 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3222 | # append to idList |
| 3223 | for arg in tempList: |
| 3224 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3225 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3226 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3227 | except TypeError: |
| 3228 | main.log.exception( self.name + ": Object not as expected" ) |
| 3229 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3230 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3231 | main.log.error( self.name + ": EOF exception found" ) |
| 3232 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3233 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3234 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3235 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3236 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3237 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3238 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3239 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3240 | Uses 'nodes' function to obtain list of all nodes |
| 3241 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3242 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3243 | Returns: |
| 3244 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3245 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3246 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3247 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3248 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3249 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3250 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3251 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3252 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3253 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3254 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3255 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3256 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3257 | except ( TypeError, ValueError ): |
| 3258 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3259 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3260 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3261 | main.log.error( self.name + ": EOF exception found" ) |
| 3262 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3263 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3264 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3265 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3266 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3267 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3268 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3269 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3270 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3271 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3272 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3273 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3274 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3275 | return None |
| 3276 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3277 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3278 | rawDevices = self.devices() |
| 3279 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3280 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3281 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3282 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3283 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3284 | return device |
| 3285 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3286 | except ( TypeError, ValueError ): |
| 3287 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3288 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3289 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3290 | main.log.error( self.name + ": EOF exception found" ) |
| 3291 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3292 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3293 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3294 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3295 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3296 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3297 | def getTopology( self, topologyOutput ): |
| 3298 | """ |
| 3299 | Definition: |
| 3300 | Loads a json topology output |
| 3301 | Return: |
| 3302 | topology = current ONOS topology |
| 3303 | """ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3304 | try: |
| 3305 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3306 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3307 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3308 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3309 | except ( TypeError, ValueError ): |
| 3310 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3311 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3312 | except pexpect.EOF: |
| 3313 | main.log.error( self.name + ": EOF exception found" ) |
| 3314 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3315 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3316 | except Exception: |
| 3317 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3318 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3319 | |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3320 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, numoSCCs=1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3321 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3322 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3323 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3324 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3325 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3326 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3327 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3328 | numoctrl = expected number of controllers |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3329 | numoSCCs = Number of expected SCCs |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3330 | logLevel = level to log to. |
| 3331 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3332 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3333 | Returns: main.TRUE if the number of switches and links are correct, |
| 3334 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3335 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3336 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3337 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3338 | summary = self.summary() |
| 3339 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3340 | except ( TypeError, ValueError ): |
| 3341 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3342 | return main.ERROR |
| 3343 | try: |
| 3344 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3345 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3346 | return main.ERROR |
| 3347 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3348 | # Is the number of switches is what we expected |
| 3349 | devices = topology.get( 'devices', False ) |
| 3350 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3351 | nodes = summary.get( 'nodes', False ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3352 | SCCs = summary.get( 'SCC(s)', False ) |
| 3353 | if devices is False or links is False or nodes is False or SCCs is False: |
| 3354 | main.log.warn( "Issues parsing topology and summary output" ) |
| 3355 | main.log.debug( topology ) |
| 3356 | main.log.debug( summary ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3357 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3358 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3359 | if not switchCheck: |
| 3360 | main.log.debug( "switch Check Failed" ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3361 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3362 | if not linkCheck: |
| 3363 | main.log.debug( "link Check Failed" ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3364 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3365 | if not nodeCheck: |
| 3366 | main.log.debug( "node Check Failed" ) |
| 3367 | SCCsCheck = ( int( SCCs ) == int( numoSCCs ) ) or int( numoSCCs ) == -1 |
| 3368 | if not SCCsCheck: |
| 3369 | main.log.debug( "SCCs Check Failed" ) |
| 3370 | if switchCheck and linkCheck and nodeCheck and SCCsCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3371 | # We expected the correct numbers |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3372 | output = output + "The number of links, switches, nodes, and SCCs match "\ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3373 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3374 | result = main.TRUE |
| 3375 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3376 | output = output + \ |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 3377 | "The number of links, switches, nodes, and SCCs does not match " + \ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3378 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3379 | result = main.FALSE |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3380 | output = output + "\n ONOS sees %i devices " % int( devices ) |
| 3381 | output = output + "(%i expected) " % int( numoswitch ) |
| 3382 | if int( numolink ) >= 0: |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3383 | output = output + "and %i links " % int( links ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3384 | output = output + "(%i expected) " % int( numolink ) |
| 3385 | if int( numoctrl ) >= 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3386 | output = output + "and %i controllers " % int( nodes ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3387 | output = output + "(%i expected) " % int( numoctrl ) |
| 3388 | if int( numoSCCs ) >= 0: |
| 3389 | output = output + "and %i SCCs " % int( SCCs ) |
| 3390 | output = output + "(%i expected)" % int( numoSCCs ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3391 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3392 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3393 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3394 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3395 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3396 | main.log.info( output ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3397 | main.TOPOOUTPUT = output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3398 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3399 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3400 | main.log.error( self.name + ": EOF exception found" ) |
| 3401 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3402 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3403 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3404 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3405 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3406 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3407 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3408 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3409 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3410 | deviceId must be the id of a device as seen in the onos devices command |
| 3411 | 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] | 3412 | role must be either master, standby, or none |
| 3413 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3414 | Returns: |
| 3415 | main.TRUE or main.FALSE based on argument verification and |
| 3416 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3417 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3418 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3419 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3420 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3421 | cmdStr = "device-role " +\ |
| 3422 | str( deviceId ) + " " +\ |
| 3423 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3424 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3425 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3426 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3427 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3428 | if re.search( "Error", handle ): |
| 3429 | # end color output to escape any colours |
| 3430 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3431 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3432 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3433 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3434 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3435 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3436 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3437 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3438 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3439 | except AssertionError: |
| 3440 | main.log.exception( "" ) |
| 3441 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3442 | except TypeError: |
| 3443 | main.log.exception( self.name + ": Object not as expected" ) |
| 3444 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3445 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3446 | main.log.error( self.name + ": EOF exception found" ) |
| 3447 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3448 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3449 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3450 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3451 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3452 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3453 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3454 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3455 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3456 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3457 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3458 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3459 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3460 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3461 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3462 | cmdStr += " -j" |
| 3463 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3464 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3465 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3466 | return handle |
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 None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -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 | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3480 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3481 | def electionTestLeader( 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 get the current leader for the Election test application |
| 3484 | NOTE: Requires installation of the onos-app-election feature |
| 3485 | Returns: Node IP of the leader if one exists |
| 3486 | None if none exists |
| 3487 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3488 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3489 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3490 | cmdStr = "election-test-leader" |
| 3491 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3492 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3493 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3494 | # Leader |
| 3495 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3496 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3497 | nodeSearch = re.search( leaderPattern, response ) |
| 3498 | if nodeSearch: |
| 3499 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3500 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3501 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3502 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3503 | # no leader |
| 3504 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3505 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3506 | nullSearch = re.search( nullPattern, response ) |
| 3507 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3508 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3509 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3510 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3511 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3512 | main.log.error( self.name + ": Error in electionTestLeader on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3513 | ": " + "unexpected response" ) |
| 3514 | main.log.error( repr( response ) ) |
| 3515 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3516 | except AssertionError: |
| 3517 | main.log.exception( "" ) |
| 3518 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3519 | except TypeError: |
| 3520 | main.log.exception( self.name + ": Object not as expected" ) |
| 3521 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3522 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3523 | main.log.error( self.name + ": EOF exception found" ) |
| 3524 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3525 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3526 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3527 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3528 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3529 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3530 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3531 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3532 | CLI command to run for leadership of the Election test application. |
| 3533 | NOTE: Requires installation of the onos-app-election feature |
| 3534 | Returns: Main.TRUE on success |
| 3535 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3536 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3537 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3538 | cmdStr = "election-test-run" |
| 3539 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3540 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3541 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3542 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3543 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3544 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3545 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3546 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3547 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3548 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3549 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3550 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3551 | main.log.error( self.name + ": Error in electionTestRun on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3552 | ": " + "unexpected response" ) |
| 3553 | main.log.error( repr( response ) ) |
| 3554 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3555 | except AssertionError: |
| 3556 | main.log.exception( "" ) |
| 3557 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3558 | except TypeError: |
| 3559 | main.log.exception( self.name + ": Object not as expected" ) |
| 3560 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3561 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3562 | main.log.error( self.name + ": EOF exception found" ) |
| 3563 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3564 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3565 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3566 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3567 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3568 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3569 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3570 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3571 | * CLI command to withdraw the local node from leadership election for |
| 3572 | * the Election test application. |
| 3573 | #NOTE: Requires installation of the onos-app-election feature |
| 3574 | Returns: Main.TRUE on success |
| 3575 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3576 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3577 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3578 | cmdStr = "election-test-withdraw" |
| 3579 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3580 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3581 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3582 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3583 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3584 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3585 | if re.search( successPattern, response ): |
| 3586 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3587 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3588 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3589 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3590 | main.log.error( self.name + ": Error in electionTestWithdraw on " + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3591 | self.name + ": " + "unexpected response" ) |
| 3592 | main.log.error( repr( response ) ) |
| 3593 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3594 | except AssertionError: |
| 3595 | main.log.exception( "" ) |
| 3596 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3597 | except TypeError: |
| 3598 | main.log.exception( self.name + ": Object not as expected" ) |
| 3599 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3600 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3601 | main.log.error( self.name + ": EOF exception found" ) |
| 3602 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3603 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3604 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3605 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3606 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3607 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3608 | def getDevicePortsEnabledCount( self, dpid ): |
| 3609 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3610 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3611 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3612 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3613 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3614 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3615 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3616 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3617 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3618 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3619 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3620 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3621 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3622 | except AssertionError: |
| 3623 | main.log.exception( "" ) |
| 3624 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3625 | except TypeError: |
| 3626 | main.log.exception( self.name + ": Object not as expected" ) |
| 3627 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3628 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3629 | main.log.error( self.name + ": EOF exception found" ) |
| 3630 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3631 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3632 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3633 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3634 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3635 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3636 | def getDeviceLinksActiveCount( self, dpid ): |
| 3637 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3638 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3639 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3640 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3641 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3642 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3643 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3644 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3645 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3646 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3647 | main.log.error( self.name + ": Error in getting ports " ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3648 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3649 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3650 | except AssertionError: |
| 3651 | main.log.exception( "" ) |
| 3652 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3653 | except TypeError: |
| 3654 | main.log.exception( self.name + ": Object not as expected" ) |
| 3655 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3656 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3657 | main.log.error( self.name + ": EOF exception found" ) |
| 3658 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3659 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3660 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3661 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3662 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3663 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3664 | def getAllIntentIds( self ): |
| 3665 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3666 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3667 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3668 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3669 | cmdStr = "onos:intents | grep id=" |
| 3670 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3671 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3672 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3673 | if re.search( "Error", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3674 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3675 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3676 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3677 | except AssertionError: |
| 3678 | main.log.exception( "" ) |
| 3679 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3680 | except TypeError: |
| 3681 | main.log.exception( self.name + ": Object not as expected" ) |
| 3682 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3683 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3684 | main.log.error( self.name + ": EOF exception found" ) |
| 3685 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3686 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3687 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3688 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3689 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3690 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3691 | def intentSummary( self ): |
| 3692 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3693 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3694 | """ |
| 3695 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3696 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3697 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3698 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3699 | states.append( intent.get( 'state', None ) ) |
| 3700 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3701 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3702 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3703 | except ( TypeError, ValueError ): |
| 3704 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3705 | return None |
| 3706 | except pexpect.EOF: |
| 3707 | main.log.error( self.name + ": EOF exception found" ) |
| 3708 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3709 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3710 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3711 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3712 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3713 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3714 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3715 | """ |
| 3716 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3717 | Optional argument: |
| 3718 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3719 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3720 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3721 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3722 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3723 | cmdStr += " -j" |
| 3724 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3725 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3726 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3727 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3728 | except AssertionError: |
| 3729 | main.log.exception( "" ) |
| 3730 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3731 | except TypeError: |
| 3732 | main.log.exception( self.name + ": Object not as expected" ) |
| 3733 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3734 | except pexpect.EOF: |
| 3735 | main.log.error( self.name + ": EOF exception found" ) |
| 3736 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3737 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3738 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3739 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3740 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3741 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3742 | def leaderCandidates( self, jsonFormat=True ): |
| 3743 | """ |
| 3744 | Returns the output of the leaders -c command. |
| 3745 | Optional argument: |
| 3746 | * jsonFormat - boolean indicating if you want output in json |
| 3747 | """ |
| 3748 | try: |
| 3749 | cmdStr = "onos:leaders -c" |
| 3750 | if jsonFormat: |
| 3751 | cmdStr += " -j" |
| 3752 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3753 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3754 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3755 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3756 | except AssertionError: |
| 3757 | main.log.exception( "" ) |
| 3758 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3759 | except TypeError: |
| 3760 | main.log.exception( self.name + ": Object not as expected" ) |
| 3761 | return None |
| 3762 | except pexpect.EOF: |
| 3763 | main.log.error( self.name + ": EOF exception found" ) |
| 3764 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3765 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3766 | except Exception: |
| 3767 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3768 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3769 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3770 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3771 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3772 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3773 | topic parameter and an empty list if the topic doesn't exist |
| 3774 | If no leader is elected leader in the returned list will be "none" |
| 3775 | Returns None if there is a type error processing the json object |
| 3776 | """ |
| 3777 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3778 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3779 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3780 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3781 | assert "Command not found:" not in rawOutput, rawOutput |
| 3782 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3783 | results = [] |
| 3784 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3785 | if dict[ "topic" ] == topic: |
| 3786 | leader = dict[ "leader" ] |
| 3787 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3788 | results.append( leader ) |
| 3789 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3790 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3791 | except AssertionError: |
| 3792 | main.log.exception( "" ) |
| 3793 | return None |
| 3794 | except ( TypeError, ValueError ): |
| 3795 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3796 | return None |
| 3797 | except pexpect.EOF: |
| 3798 | main.log.error( self.name + ": EOF exception found" ) |
| 3799 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3800 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3801 | except Exception: |
| 3802 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3803 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3804 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3805 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3806 | """ |
| 3807 | Returns the output of the intent Pending map. |
| 3808 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3809 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3810 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3811 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3812 | cmdStr += " -j" |
| 3813 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3814 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3815 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3816 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3817 | except AssertionError: |
| 3818 | main.log.exception( "" ) |
| 3819 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3820 | except TypeError: |
| 3821 | main.log.exception( self.name + ": Object not as expected" ) |
| 3822 | return None |
| 3823 | except pexpect.EOF: |
| 3824 | main.log.error( self.name + ": EOF exception found" ) |
| 3825 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3826 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3827 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3828 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3829 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3830 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3831 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3832 | """ |
| 3833 | Returns the output of the raft partitions command for ONOS. |
| 3834 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3835 | # Sample JSON |
| 3836 | # { |
| 3837 | # "leader": "tcp://10.128.30.11:7238", |
| 3838 | # "members": [ |
| 3839 | # "tcp://10.128.30.11:7238", |
| 3840 | # "tcp://10.128.30.17:7238", |
| 3841 | # "tcp://10.128.30.13:7238", |
| 3842 | # ], |
| 3843 | # "name": "p1", |
| 3844 | # "term": 3 |
| 3845 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3846 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3847 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3848 | if candidates: |
| 3849 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3850 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3851 | cmdStr += " -j" |
| 3852 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3853 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3854 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3855 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3856 | except AssertionError: |
| 3857 | main.log.exception( "" ) |
| 3858 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3859 | except TypeError: |
| 3860 | main.log.exception( self.name + ": Object not as expected" ) |
| 3861 | return None |
| 3862 | except pexpect.EOF: |
| 3863 | main.log.error( self.name + ": EOF exception found" ) |
| 3864 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3865 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3866 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3867 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3868 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3869 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3870 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3871 | """ |
| 3872 | Returns the output of the apps command for ONOS. This command lists |
| 3873 | information about installed ONOS applications |
| 3874 | """ |
| 3875 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3876 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3877 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3878 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3879 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3880 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3881 | cmdStr = "onos:apps" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3882 | expectJson = False |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3883 | if summary: |
| 3884 | cmdStr += " -s" |
| 3885 | if active: |
| 3886 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3887 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3888 | cmdStr += " -j" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3889 | expectJson = True |
| 3890 | output = self.sendline( cmdStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3891 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3892 | assert "Command not found:" not in output, output |
| 3893 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3894 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3895 | # FIXME: look at specific exceptions/Errors |
| 3896 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3897 | main.log.exception( self.name + ": Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3898 | return None |
| 3899 | except TypeError: |
| 3900 | main.log.exception( self.name + ": Object not as expected" ) |
| 3901 | return None |
| 3902 | except pexpect.EOF: |
| 3903 | main.log.error( self.name + ": EOF exception found" ) |
| 3904 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3905 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3906 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3907 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3908 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3909 | |
You Wang | cdc51fe | 2018-08-12 17:14:56 -0700 | [diff] [blame] | 3910 | def appStatus( self, appName ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3911 | """ |
| 3912 | Uses the onos:apps cli command to return the status of an application. |
| 3913 | Returns: |
| 3914 | "ACTIVE" - If app is installed and activated |
| 3915 | "INSTALLED" - If app is installed and deactivated |
| 3916 | "UNINSTALLED" - If app is not installed |
| 3917 | None - on error |
| 3918 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3919 | try: |
| 3920 | if not isinstance( appName, types.StringType ): |
| 3921 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3922 | " a string" ) |
| 3923 | return None |
| 3924 | output = self.apps( jsonFormat=True ) |
| 3925 | appsJson = json.loads( output ) |
| 3926 | state = None |
| 3927 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3928 | if appName == app.get( 'name' ): |
| 3929 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3930 | break |
| 3931 | if state == "ACTIVE" or state == "INSTALLED": |
| 3932 | return state |
| 3933 | elif state is None: |
You Wang | 0d9f2c0 | 2018-08-10 14:56:32 -0700 | [diff] [blame] | 3934 | main.log.warn( "{} app not found".format( appName ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3935 | return "UNINSTALLED" |
| 3936 | elif state: |
| 3937 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3938 | str( state ) ) |
| 3939 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3940 | except ( TypeError, ValueError ): |
| 3941 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3942 | return None |
| 3943 | except pexpect.EOF: |
| 3944 | main.log.error( self.name + ": EOF exception found" ) |
| 3945 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3946 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3947 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3948 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3949 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3950 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3951 | def app( self, appName, option ): |
| 3952 | """ |
| 3953 | Interacts with the app command for ONOS. This command manages |
| 3954 | application inventory. |
| 3955 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3956 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3957 | # Validate argument types |
| 3958 | valid = True |
| 3959 | if not isinstance( appName, types.StringType ): |
| 3960 | main.log.error( self.name + ".app(): appName must be a " + |
| 3961 | "string" ) |
| 3962 | valid = False |
| 3963 | if not isinstance( option, types.StringType ): |
| 3964 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3965 | valid = False |
| 3966 | if not valid: |
| 3967 | return main.FALSE |
| 3968 | # Validate Option |
| 3969 | option = option.lower() |
| 3970 | # NOTE: Install may become a valid option |
| 3971 | if option == "activate": |
| 3972 | pass |
| 3973 | elif option == "deactivate": |
| 3974 | pass |
| 3975 | elif option == "uninstall": |
| 3976 | pass |
| 3977 | else: |
| 3978 | # Invalid option |
| 3979 | main.log.error( "The ONOS app command argument only takes " + |
| 3980 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3981 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3982 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3983 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3984 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3985 | assert output is not None, "Error in sendline" |
| 3986 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3987 | if "Error executing command" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3988 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3989 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3990 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3991 | elif "No such application" in output: |
| 3992 | main.log.error( "The application '" + appName + |
| 3993 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3994 | return main.FALSE |
| 3995 | elif "Command not found:" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3996 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3997 | str( output ) ) |
| 3998 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3999 | elif "Unsupported command:" in output: |
| 4000 | main.log.error( "Incorrect command given to 'app': " + |
| 4001 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4002 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4003 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 4004 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4005 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 4006 | except AssertionError: |
| 4007 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4008 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4009 | except TypeError: |
| 4010 | main.log.exception( self.name + ": Object not as expected" ) |
| 4011 | return main.ERROR |
| 4012 | except pexpect.EOF: |
| 4013 | main.log.error( self.name + ": EOF exception found" ) |
| 4014 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4015 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4016 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 4017 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4018 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4019 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4020 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4021 | """ |
| 4022 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4023 | appName is the hierarchical app name, not the feature name |
| 4024 | If check is True, method will check the status of the app after the |
| 4025 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4026 | Returns main.TRUE if the command was successfully sent |
| 4027 | main.FALSE if the cli responded with an error or given |
| 4028 | incorrect input |
| 4029 | """ |
| 4030 | try: |
| 4031 | if not isinstance( appName, types.StringType ): |
| 4032 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 4033 | " a string" ) |
| 4034 | return main.FALSE |
| 4035 | status = self.appStatus( appName ) |
| 4036 | if status == "INSTALLED": |
| 4037 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4038 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4039 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4040 | status = self.appStatus( appName ) |
| 4041 | if status == "ACTIVE": |
| 4042 | return main.TRUE |
| 4043 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 4044 | main.log.debug( "The state of application " + |
| 4045 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4046 | time.sleep( 1 ) |
| 4047 | return main.FALSE |
| 4048 | else: # not 'check' or command didn't succeed |
| 4049 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4050 | elif status == "ACTIVE": |
| 4051 | return main.TRUE |
| 4052 | elif status == "UNINSTALLED": |
| 4053 | main.log.error( self.name + ": Tried to activate the " + |
| 4054 | "application '" + appName + "' which is not " + |
| 4055 | "installed." ) |
| 4056 | else: |
| 4057 | main.log.error( "Unexpected return value from appStatus: " + |
| 4058 | str( status ) ) |
| 4059 | return main.ERROR |
| 4060 | except TypeError: |
| 4061 | main.log.exception( self.name + ": Object not as expected" ) |
| 4062 | return main.ERROR |
| 4063 | except pexpect.EOF: |
| 4064 | main.log.error( self.name + ": EOF exception found" ) |
| 4065 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4066 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4067 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4068 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4069 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4070 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4071 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4072 | """ |
| 4073 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4074 | appName is the hierarchical app name, not the feature name |
| 4075 | If check is True, method will check the status of the app after the |
| 4076 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4077 | Returns main.TRUE if the command was successfully sent |
| 4078 | main.FALSE if the cli responded with an error or given |
| 4079 | incorrect input |
| 4080 | """ |
| 4081 | try: |
| 4082 | if not isinstance( appName, types.StringType ): |
| 4083 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 4084 | "be a string" ) |
| 4085 | return main.FALSE |
| 4086 | status = self.appStatus( appName ) |
| 4087 | if status == "INSTALLED": |
| 4088 | return main.TRUE |
| 4089 | elif status == "ACTIVE": |
| 4090 | response = self.app( appName, "deactivate" ) |
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 == "INSTALLED": |
| 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 == "UNINSTALLED": |
| 4102 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 4103 | "application '" + appName + "' which is not " + |
| 4104 | "installed." ) |
| 4105 | return main.TRUE |
| 4106 | else: |
| 4107 | main.log.error( "Unexpected return value from appStatus: " + |
| 4108 | str( status ) ) |
| 4109 | return main.ERROR |
| 4110 | except TypeError: |
| 4111 | main.log.exception( self.name + ": Object not as expected" ) |
| 4112 | return main.ERROR |
| 4113 | except pexpect.EOF: |
| 4114 | main.log.error( self.name + ": EOF exception found" ) |
| 4115 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4116 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4117 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4118 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4119 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4120 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4121 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4122 | """ |
| 4123 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4124 | appName is the hierarchical app name, not the feature name |
| 4125 | If check is True, method will check the status of the app after the |
| 4126 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4127 | Returns main.TRUE if the command was successfully sent |
| 4128 | main.FALSE if the cli responded with an error or given |
| 4129 | incorrect input |
| 4130 | """ |
| 4131 | # TODO: check with Thomas about the state machine for apps |
| 4132 | try: |
| 4133 | if not isinstance( appName, types.StringType ): |
| 4134 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 4135 | "be a string" ) |
| 4136 | return main.FALSE |
| 4137 | status = self.appStatus( appName ) |
| 4138 | if status == "INSTALLED": |
| 4139 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4140 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4141 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4142 | status = self.appStatus( appName ) |
| 4143 | if status == "UNINSTALLED": |
| 4144 | return main.TRUE |
| 4145 | else: |
| 4146 | time.sleep( 1 ) |
| 4147 | return main.FALSE |
| 4148 | else: # not check or command didn't succeed |
| 4149 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4150 | elif status == "ACTIVE": |
| 4151 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 4152 | "application '" + appName + "' which is " + |
| 4153 | "currently active." ) |
| 4154 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4155 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4156 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4157 | status = self.appStatus( appName ) |
| 4158 | if status == "UNINSTALLED": |
| 4159 | return main.TRUE |
| 4160 | else: |
| 4161 | time.sleep( 1 ) |
| 4162 | return main.FALSE |
| 4163 | else: # not check or command didn't succeed |
| 4164 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4165 | elif status == "UNINSTALLED": |
| 4166 | return main.TRUE |
| 4167 | else: |
| 4168 | main.log.error( "Unexpected return value from appStatus: " + |
| 4169 | str( status ) ) |
| 4170 | return main.ERROR |
| 4171 | except TypeError: |
| 4172 | main.log.exception( self.name + ": Object not as expected" ) |
| 4173 | return main.ERROR |
| 4174 | except pexpect.EOF: |
| 4175 | main.log.error( self.name + ": EOF exception found" ) |
| 4176 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4177 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4178 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4179 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4180 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4181 | |
| 4182 | def appIDs( self, jsonFormat=True ): |
| 4183 | """ |
| 4184 | Show the mappings between app id and app names given by the 'app-ids' |
| 4185 | cli command |
| 4186 | """ |
| 4187 | try: |
| 4188 | cmdStr = "app-ids" |
| 4189 | if jsonFormat: |
| 4190 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4191 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4192 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4193 | assert "Command not found:" not in output, output |
| 4194 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4195 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4196 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4197 | main.log.exception( self.name + ": Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4198 | return None |
| 4199 | except TypeError: |
| 4200 | main.log.exception( self.name + ": Object not as expected" ) |
| 4201 | return None |
| 4202 | except pexpect.EOF: |
| 4203 | main.log.error( self.name + ": EOF exception found" ) |
| 4204 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4205 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4206 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4207 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4208 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4209 | |
| 4210 | def appToIDCheck( self ): |
| 4211 | """ |
| 4212 | This method will check that each application's ID listed in 'apps' is |
| 4213 | the same as the ID listed in 'app-ids'. The check will also check that |
| 4214 | there are no duplicate IDs issued. Note that an app ID should be |
| 4215 | a globaly unique numerical identifier for app/app-like features. Once |
| 4216 | an ID is registered, the ID is never freed up so that if an app is |
| 4217 | reinstalled it will have the same ID. |
| 4218 | |
| 4219 | Returns: main.TRUE if the check passes and |
| 4220 | main.FALSE if the check fails or |
| 4221 | main.ERROR if there is some error in processing the test |
| 4222 | """ |
| 4223 | try: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4224 | # Grab IDs |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4225 | rawJson = self.appIDs( jsonFormat=True ) |
| 4226 | if rawJson: |
| 4227 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4228 | else: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4229 | main.log.error( "app-ids returned nothing: " + repr( rawJson ) ) |
| 4230 | return main.FALSE |
| 4231 | |
| 4232 | # Grab Apps |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4233 | rawJson = self.apps( jsonFormat=True ) |
| 4234 | if rawJson: |
| 4235 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4236 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4237 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4238 | return main.FALSE |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4239 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4240 | result = main.TRUE |
| 4241 | for app in apps: |
| 4242 | appID = app.get( 'id' ) |
| 4243 | if appID is None: |
| 4244 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4245 | result = main.FALSE |
| 4246 | appName = app.get( 'name' ) |
| 4247 | if appName is None: |
| 4248 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4249 | result = main.FALSE |
| 4250 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4251 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4252 | if not current: # if ids doesn't have this id |
| 4253 | result = main.FALSE |
| 4254 | main.log.error( "'app-ids' does not have the ID for " + |
| 4255 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4256 | main.log.debug( "apps command returned: " + str( app ) + |
| 4257 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4258 | elif len( current ) > 1: |
| 4259 | # there is more than one app with this ID |
| 4260 | result = main.FALSE |
| 4261 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4262 | elif not current[ 0 ][ 'name' ] == appName: |
| 4263 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4264 | result = main.FALSE |
| 4265 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4266 | " registered under id:" + str( appID ) + |
| 4267 | " but 'apps' has " + str( appName ) ) |
| 4268 | else: |
| 4269 | pass # id and name match! |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4270 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4271 | # now make sure that app-ids has no duplicates |
| 4272 | idsList = [] |
| 4273 | namesList = [] |
| 4274 | for item in ids: |
| 4275 | idsList.append( item[ 'id' ] ) |
| 4276 | namesList.append( item[ 'name' ] ) |
| 4277 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4278 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4279 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4280 | + json.dumps( ids, |
| 4281 | sort_keys=True, |
| 4282 | indent=4, |
| 4283 | separators=( ',', ': ' ) ) ) |
| 4284 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4285 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4286 | except ( TypeError, ValueError ): |
| 4287 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4288 | return main.ERROR |
| 4289 | except pexpect.EOF: |
| 4290 | main.log.error( self.name + ": EOF exception found" ) |
| 4291 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4292 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4293 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4294 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4295 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4296 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4297 | def getCfg( self, component=None, propName=None, short=False, |
| 4298 | jsonFormat=True ): |
| 4299 | """ |
| 4300 | Get configuration settings from onos cli |
| 4301 | Optional arguments: |
| 4302 | component - Optionally only list configurations for a specific |
| 4303 | component. If None, all components with configurations |
| 4304 | are displayed. Case Sensitive string. |
| 4305 | propName - If component is specified, propName option will show |
| 4306 | only this specific configuration from that component. |
| 4307 | Case Sensitive string. |
| 4308 | jsonFormat - Returns output as json. Note that this will override |
| 4309 | the short option |
| 4310 | short - Short, less verbose, version of configurations. |
| 4311 | This is overridden by the json option |
| 4312 | returns: |
| 4313 | Output from cli as a string or None on error |
| 4314 | """ |
| 4315 | try: |
| 4316 | baseStr = "cfg" |
| 4317 | cmdStr = " get" |
| 4318 | componentStr = "" |
| 4319 | if component: |
| 4320 | componentStr += " " + component |
| 4321 | if propName: |
| 4322 | componentStr += " " + propName |
| 4323 | if jsonFormat: |
| 4324 | baseStr += " -j" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4325 | expectJson = True |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4326 | elif short: |
| 4327 | baseStr += " -s" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4328 | expectJson = False |
| 4329 | output = self.sendline( baseStr + cmdStr + componentStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4330 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4331 | assert "Command not found:" not in output, output |
| 4332 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4333 | return output |
| 4334 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4335 | main.log.exception( self.name + ": Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4336 | return None |
| 4337 | except TypeError: |
| 4338 | main.log.exception( self.name + ": Object not as expected" ) |
| 4339 | return None |
| 4340 | except pexpect.EOF: |
| 4341 | main.log.error( self.name + ": EOF exception found" ) |
| 4342 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4343 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4344 | except Exception: |
| 4345 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4346 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4347 | |
| 4348 | def setCfg( self, component, propName, value=None, check=True ): |
| 4349 | """ |
| 4350 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4351 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4352 | component - The case sensitive name of the component whose |
| 4353 | property is to be set |
| 4354 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4355 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4356 | 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] | 4357 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4358 | check - Boolean, Check whether the option was successfully set this |
| 4359 | only applies when a value is given. |
| 4360 | returns: |
| 4361 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4362 | will return main.TRUE unless there is an error |
| 4363 | """ |
| 4364 | try: |
| 4365 | baseStr = "cfg" |
| 4366 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4367 | if value is not None: |
| 4368 | cmdStr += " " + str( value ) |
| 4369 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4370 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4371 | assert "Command not found:" not in output, output |
| 4372 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4373 | if value and check: |
| 4374 | results = self.getCfg( component=str( component ), |
| 4375 | propName=str( propName ), |
| 4376 | jsonFormat=True ) |
| 4377 | # Check if current value is what we just set |
| 4378 | try: |
| 4379 | jsonOutput = json.loads( results ) |
| 4380 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4381 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4382 | main.log.exception( "Error parsing cfg output" ) |
| 4383 | main.log.error( "output:" + repr( results ) ) |
| 4384 | return main.FALSE |
| 4385 | if current == str( value ): |
| 4386 | return main.TRUE |
| 4387 | return main.FALSE |
| 4388 | return main.TRUE |
| 4389 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4390 | main.log.exception( self.name + ": Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4391 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4392 | except ( TypeError, ValueError ): |
| 4393 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4394 | return main.FALSE |
| 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 | fb760a0 | 2015-04-13 15:35:03 -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 | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4402 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4403 | def distPrimitivesSend( self, cmd ): |
| 4404 | """ |
| 4405 | Function to handle sending cli commands for the distributed primitives test app |
| 4406 | |
| 4407 | This command will catch some exceptions and retry the command on some |
| 4408 | specific store exceptions. |
| 4409 | |
| 4410 | Required arguments: |
| 4411 | cmd - The command to send to the cli |
| 4412 | returns: |
| 4413 | string containing the cli output |
| 4414 | None on Error |
| 4415 | """ |
| 4416 | try: |
| 4417 | output = self.sendline( cmd ) |
| 4418 | try: |
| 4419 | assert output is not None, "Error in sendline" |
| 4420 | # TODO: Maybe make this less hardcoded |
| 4421 | # ConsistentMap Exceptions |
| 4422 | assert "org.onosproject.store.service" not in output |
| 4423 | # Node not leader |
| 4424 | assert "java.lang.IllegalStateException" not in output |
| 4425 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4426 | main.log.error( self.name + ": Error in processing '" + cmd + "' " + |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4427 | "command: " + str( output ) ) |
| 4428 | retryTime = 30 # Conservative time, given by Madan |
| 4429 | main.log.info( "Waiting " + str( retryTime ) + |
| 4430 | "seconds before retrying." ) |
| 4431 | time.sleep( retryTime ) # Due to change in mastership |
| 4432 | output = self.sendline( cmd ) |
| 4433 | assert output is not None, "Error in sendline" |
| 4434 | assert "Command not found:" not in output, output |
| 4435 | assert "Error executing command" not in output, output |
| 4436 | main.log.info( self.name + ": " + output ) |
| 4437 | return output |
| 4438 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4439 | main.log.exception( self.name + ": Error in processing '" + cmd + "' command." ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4440 | return None |
| 4441 | except TypeError: |
| 4442 | main.log.exception( self.name + ": Object not as expected" ) |
| 4443 | return None |
| 4444 | except pexpect.EOF: |
| 4445 | main.log.error( self.name + ": EOF exception found" ) |
| 4446 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4447 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4448 | except Exception: |
| 4449 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4450 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4451 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4452 | def setTestAdd( self, setName, values ): |
| 4453 | """ |
| 4454 | CLI command to add elements to a distributed set. |
| 4455 | Arguments: |
| 4456 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4457 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4458 | Example usages: |
| 4459 | setTestAdd( "set1", "a b c" ) |
| 4460 | setTestAdd( "set2", "1" ) |
| 4461 | returns: |
| 4462 | main.TRUE on success OR |
| 4463 | main.FALSE if elements were already in the set OR |
| 4464 | main.ERROR on error |
| 4465 | """ |
| 4466 | try: |
| 4467 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4468 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4469 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4470 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4471 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4472 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4473 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4474 | return main.FALSE |
| 4475 | else: |
| 4476 | main.log.error( self.name + ": setTestAdd did not" + |
| 4477 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4478 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4479 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4480 | except TypeError: |
| 4481 | main.log.exception( self.name + ": Object not as expected" ) |
| 4482 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4483 | except Exception: |
| 4484 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4485 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4486 | |
| 4487 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4488 | """ |
| 4489 | CLI command to remove elements from a distributed set. |
| 4490 | Required arguments: |
| 4491 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4492 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4493 | Optional arguments: |
| 4494 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4495 | retain - Retain only the given values. (intersection of the |
| 4496 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4497 | returns: |
| 4498 | main.TRUE on success OR |
| 4499 | main.FALSE if the set was not changed OR |
| 4500 | main.ERROR on error |
| 4501 | """ |
| 4502 | try: |
| 4503 | cmdStr = "set-test-remove " |
| 4504 | if clear: |
| 4505 | cmdStr += "-c " + str( setName ) |
| 4506 | elif retain: |
| 4507 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4508 | else: |
| 4509 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4510 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4511 | if clear: |
| 4512 | pattern = "Set " + str( setName ) + " cleared" |
| 4513 | if re.search( pattern, output ): |
| 4514 | return main.TRUE |
| 4515 | elif retain: |
| 4516 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4517 | "only elements of set \[(.*)\]" |
| 4518 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4519 | "retaining only elements of the set " +\ |
| 4520 | "\[(.*)\]" |
| 4521 | if re.search( positivePattern, output ): |
| 4522 | return main.TRUE |
| 4523 | elif re.search( negativePattern, output ): |
| 4524 | return main.FALSE |
| 4525 | else: |
| 4526 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4527 | str( setName ) |
| 4528 | if ( len( values.split() ) == 1 ): |
| 4529 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4530 | str( setName ) |
| 4531 | else: |
| 4532 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4533 | str( setName ) |
| 4534 | if re.search( positivePattern, output ): |
| 4535 | return main.TRUE |
| 4536 | elif re.search( negativePattern, output ): |
| 4537 | return main.FALSE |
| 4538 | main.log.error( self.name + ": setTestRemove did not" + |
| 4539 | " match expected output" ) |
| 4540 | main.log.debug( self.name + " expected: " + pattern ) |
| 4541 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4542 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4543 | except TypeError: |
| 4544 | main.log.exception( self.name + ": Object not as expected" ) |
| 4545 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4546 | except Exception: |
| 4547 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4548 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4549 | |
| 4550 | def setTestGet( self, setName, values="" ): |
| 4551 | """ |
| 4552 | CLI command to get the elements in a distributed set. |
| 4553 | Required arguments: |
| 4554 | setName - The name of the set to remove from. |
| 4555 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4556 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4557 | returns: |
| 4558 | main.ERROR on error OR |
| 4559 | A list of elements in the set if no optional arguments are |
| 4560 | supplied OR |
| 4561 | A tuple containing the list then: |
| 4562 | main.FALSE if the given values are not in the set OR |
| 4563 | main.TRUE if the given values are in the set OR |
| 4564 | """ |
| 4565 | try: |
| 4566 | values = str( values ).strip() |
| 4567 | setName = str( setName ).strip() |
| 4568 | length = len( values.split() ) |
| 4569 | containsCheck = None |
| 4570 | # Patterns to match |
| 4571 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4572 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4573 | containsTrue = "Set " + setName + " contains the value " + values |
| 4574 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4575 | values |
| 4576 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4577 | setPattern |
| 4578 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4579 | " subset " + setPattern |
| 4580 | |
| 4581 | cmdStr = "set-test-get " |
| 4582 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4583 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4584 | if length == 0: |
| 4585 | match = re.search( pattern, output ) |
| 4586 | else: # if given values |
| 4587 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4588 | patternTrue = pattern + "\r\n" + containsTrue |
| 4589 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4590 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4591 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4592 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4593 | matchTrue = re.search( patternTrue, output ) |
| 4594 | matchFalse = re.search( patternFalse, output ) |
| 4595 | if matchTrue: |
| 4596 | containsCheck = main.TRUE |
| 4597 | match = matchTrue |
| 4598 | elif matchFalse: |
| 4599 | containsCheck = main.FALSE |
| 4600 | match = matchFalse |
| 4601 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4602 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4603 | "expected output" ) |
| 4604 | main.log.debug( self.name + " expected: " + pattern ) |
| 4605 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4606 | match = None |
| 4607 | if match: |
| 4608 | setMatch = match.group( 1 ) |
| 4609 | if setMatch == '': |
| 4610 | setList = [] |
| 4611 | else: |
| 4612 | setList = setMatch.split( ", " ) |
| 4613 | if length > 0: |
| 4614 | return ( setList, containsCheck ) |
| 4615 | else: |
| 4616 | return setList |
| 4617 | else: # no match |
| 4618 | main.log.error( self.name + ": setTestGet did not" + |
| 4619 | " match expected output" ) |
| 4620 | main.log.debug( self.name + " expected: " + pattern ) |
| 4621 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4622 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4623 | except TypeError: |
| 4624 | main.log.exception( self.name + ": Object not as expected" ) |
| 4625 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4626 | except Exception: |
| 4627 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4628 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4629 | |
| 4630 | def setTestSize( self, setName ): |
| 4631 | """ |
| 4632 | CLI command to get the elements in a distributed set. |
| 4633 | Required arguments: |
| 4634 | setName - The name of the set to remove from. |
| 4635 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4636 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4637 | None on error |
| 4638 | """ |
| 4639 | try: |
| 4640 | # TODO: Should this check against the number of elements returned |
| 4641 | # and then return true/false based on that? |
| 4642 | setName = str( setName ).strip() |
| 4643 | # Patterns to match |
| 4644 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4645 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4646 | setPattern |
| 4647 | cmdStr = "set-test-get -s " |
| 4648 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4649 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4650 | if output: |
| 4651 | match = re.search( pattern, output ) |
| 4652 | if match: |
| 4653 | setSize = int( match.group( 1 ) ) |
| 4654 | setMatch = match.group( 2 ) |
| 4655 | if len( setMatch.split() ) == setSize: |
| 4656 | main.log.info( "The size returned by " + self.name + |
| 4657 | " matches the number of elements in " + |
| 4658 | "the returned set" ) |
| 4659 | else: |
| 4660 | main.log.error( "The size returned by " + self.name + |
| 4661 | " does not match the number of " + |
| 4662 | "elements in the returned set." ) |
| 4663 | return setSize |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4664 | else: # no match |
| 4665 | main.log.error( self.name + ": setTestGet did not" + |
| 4666 | " match expected output" ) |
| 4667 | main.log.debug( self.name + " expected: " + pattern ) |
| 4668 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4669 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4670 | except TypeError: |
| 4671 | main.log.exception( self.name + ": Object not as expected" ) |
| 4672 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4673 | except Exception: |
| 4674 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4675 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4676 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4677 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4678 | """ |
| 4679 | Command to list the various counters in the system. |
| 4680 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4681 | if jsonFormat, a string of the json object returned by the cli |
| 4682 | command |
| 4683 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4684 | None on error |
| 4685 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4686 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4687 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4688 | if jsonFormat: |
| 4689 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4690 | output = self.sendline( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4691 | main.log.debug( self.name + ": Counters unparsed: " + output ) |
| 4692 | output = output.split( "\r\n" )[ -1 ] |
| 4693 | main.log.debug( self.name + ": Counters parsed: " + output ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4694 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4695 | assert "Command not found:" not in output, output |
| 4696 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4697 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4698 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4699 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4700 | main.log.exception( self.name + ": Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4701 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4702 | except TypeError: |
| 4703 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4704 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4705 | except pexpect.EOF: |
| 4706 | main.log.error( self.name + ": EOF exception found" ) |
| 4707 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4708 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4709 | except Exception: |
| 4710 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4711 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4712 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4713 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4714 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4715 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4716 | Required arguments: |
| 4717 | counter - The name of the counter to increment. |
| 4718 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4719 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4720 | returns: |
| 4721 | integer value of the counter or |
| 4722 | None on Error |
| 4723 | """ |
| 4724 | try: |
| 4725 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4726 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4727 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4728 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4729 | if delta != 1: |
| 4730 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4731 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4732 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4733 | match = re.search( pattern, output ) |
| 4734 | if match: |
| 4735 | return int( match.group( 1 ) ) |
| 4736 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4737 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4738 | " match expected output." ) |
| 4739 | main.log.debug( self.name + " expected: " + pattern ) |
| 4740 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4741 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4742 | except TypeError: |
| 4743 | main.log.exception( self.name + ": Object not as expected" ) |
| 4744 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4745 | except Exception: |
| 4746 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4747 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4748 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4749 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4750 | """ |
| 4751 | CLI command to get a distributed counter then add a delta to it. |
| 4752 | Required arguments: |
| 4753 | counter - The name of the counter to increment. |
| 4754 | Optional arguments: |
| 4755 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4756 | returns: |
| 4757 | integer value of the counter or |
| 4758 | None on Error |
| 4759 | """ |
| 4760 | try: |
| 4761 | counter = str( counter ) |
| 4762 | delta = int( delta ) |
| 4763 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4764 | cmdStr += counter |
| 4765 | if delta != 1: |
| 4766 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4767 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4768 | pattern = counter + " was updated to (-?\d+)" |
| 4769 | match = re.search( pattern, output ) |
| 4770 | if match: |
| 4771 | return int( match.group( 1 ) ) |
| 4772 | else: |
| 4773 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4774 | " match expected output." ) |
| 4775 | main.log.debug( self.name + " expected: " + pattern ) |
| 4776 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4777 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4778 | except TypeError: |
| 4779 | main.log.exception( self.name + ": Object not as expected" ) |
| 4780 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4781 | except Exception: |
| 4782 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4783 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4784 | |
| 4785 | def valueTestGet( self, valueName ): |
| 4786 | """ |
| 4787 | CLI command to get the value of an atomic value. |
| 4788 | Required arguments: |
| 4789 | valueName - The name of the value to get. |
| 4790 | returns: |
| 4791 | string value of the value or |
| 4792 | None on Error |
| 4793 | """ |
| 4794 | try: |
| 4795 | valueName = str( valueName ) |
| 4796 | cmdStr = "value-test " |
| 4797 | operation = "get" |
| 4798 | cmdStr = "value-test {} {}".format( valueName, |
| 4799 | operation ) |
| 4800 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4801 | main.log.debug( self.name + ": value test unparsed: " + output ) |
| 4802 | output = output.split( "\r\n" )[ -1 ] |
| 4803 | main.log.debug( self.name + ": value test parsed: " + output ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4804 | pattern = "(\w+)" |
| 4805 | match = re.search( pattern, output ) |
| 4806 | if match: |
| 4807 | return match.group( 1 ) |
| 4808 | else: |
| 4809 | main.log.error( self.name + ": valueTestGet did not" + |
| 4810 | " match expected output." ) |
| 4811 | main.log.debug( self.name + " expected: " + pattern ) |
| 4812 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4813 | return None |
| 4814 | except TypeError: |
| 4815 | main.log.exception( self.name + ": Object not as expected" ) |
| 4816 | return None |
| 4817 | except Exception: |
| 4818 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4819 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4820 | |
| 4821 | def valueTestSet( self, valueName, newValue ): |
| 4822 | """ |
| 4823 | CLI command to set the value of an atomic value. |
| 4824 | Required arguments: |
| 4825 | valueName - The name of the value to set. |
| 4826 | newValue - The value to assign to the given value. |
| 4827 | returns: |
| 4828 | main.TRUE on success or |
| 4829 | main.ERROR on Error |
| 4830 | """ |
| 4831 | try: |
| 4832 | valueName = str( valueName ) |
| 4833 | newValue = str( newValue ) |
| 4834 | operation = "set" |
| 4835 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4836 | operation, |
| 4837 | newValue ) |
| 4838 | output = self.distPrimitivesSend( cmdStr ) |
| 4839 | if output is not None: |
| 4840 | return main.TRUE |
| 4841 | else: |
| 4842 | return main.ERROR |
| 4843 | except TypeError: |
| 4844 | main.log.exception( self.name + ": Object not as expected" ) |
| 4845 | return main.ERROR |
| 4846 | except Exception: |
| 4847 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4848 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4849 | |
| 4850 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4851 | """ |
| 4852 | CLI command to compareAndSet the value of an atomic value. |
| 4853 | Required arguments: |
| 4854 | valueName - The name of the value. |
| 4855 | oldValue - Compare the current value of the atomic value to this |
| 4856 | newValue - If the value equals oldValue, set the value to newValue |
| 4857 | returns: |
| 4858 | main.TRUE on success or |
| 4859 | main.FALSE on failure or |
| 4860 | main.ERROR on Error |
| 4861 | """ |
| 4862 | try: |
| 4863 | valueName = str( valueName ) |
| 4864 | oldValue = str( oldValue ) |
| 4865 | newValue = str( newValue ) |
| 4866 | operation = "compareAndSet" |
| 4867 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4868 | operation, |
| 4869 | oldValue, |
| 4870 | newValue ) |
| 4871 | output = self.distPrimitivesSend( cmdStr ) |
| 4872 | pattern = "(\w+)" |
| 4873 | match = re.search( pattern, output ) |
| 4874 | if match: |
| 4875 | result = match.group( 1 ) |
| 4876 | if result == "true": |
| 4877 | return main.TRUE |
| 4878 | elif result == "false": |
| 4879 | return main.FALSE |
| 4880 | else: |
| 4881 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4882 | " match expected output." ) |
| 4883 | main.log.debug( self.name + " expected: " + pattern ) |
| 4884 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4885 | return main.ERROR |
| 4886 | except TypeError: |
| 4887 | main.log.exception( self.name + ": Object not as expected" ) |
| 4888 | return main.ERROR |
| 4889 | except Exception: |
| 4890 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4891 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4892 | |
| 4893 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4894 | """ |
| 4895 | CLI command to getAndSet the value of an atomic value. |
| 4896 | Required arguments: |
| 4897 | valueName - The name of the value to get. |
| 4898 | newValue - The value to assign to the given value |
| 4899 | returns: |
| 4900 | string value of the value or |
| 4901 | None on Error |
| 4902 | """ |
| 4903 | try: |
| 4904 | valueName = str( valueName ) |
| 4905 | cmdStr = "value-test " |
| 4906 | operation = "getAndSet" |
| 4907 | cmdStr += valueName + " " + operation |
| 4908 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4909 | operation, |
| 4910 | newValue ) |
| 4911 | output = self.distPrimitivesSend( cmdStr ) |
| 4912 | pattern = "(\w+)" |
| 4913 | match = re.search( pattern, output ) |
| 4914 | if match: |
| 4915 | return match.group( 1 ) |
| 4916 | else: |
| 4917 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4918 | " match expected output." ) |
| 4919 | main.log.debug( self.name + " expected: " + pattern ) |
| 4920 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4921 | return None |
| 4922 | except TypeError: |
| 4923 | main.log.exception( self.name + ": Object not as expected" ) |
| 4924 | return None |
| 4925 | except Exception: |
| 4926 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4927 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4928 | |
| 4929 | def valueTestDestroy( self, valueName ): |
| 4930 | """ |
| 4931 | CLI command to destroy an atomic value. |
| 4932 | Required arguments: |
| 4933 | valueName - The name of the value to destroy. |
| 4934 | returns: |
| 4935 | main.TRUE on success or |
| 4936 | main.ERROR on Error |
| 4937 | """ |
| 4938 | try: |
| 4939 | valueName = str( valueName ) |
| 4940 | cmdStr = "value-test " |
| 4941 | operation = "destroy" |
| 4942 | cmdStr += valueName + " " + operation |
| 4943 | output = self.distPrimitivesSend( cmdStr ) |
| 4944 | if output is not None: |
| 4945 | return main.TRUE |
| 4946 | else: |
| 4947 | return main.ERROR |
| 4948 | except TypeError: |
| 4949 | main.log.exception( self.name + ": Object not as expected" ) |
| 4950 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4951 | except Exception: |
| 4952 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4953 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4954 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4955 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4956 | """ |
| 4957 | Description: Execute summary command in onos |
| 4958 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4959 | no output |
| 4960 | |
| 4961 | """ |
| 4962 | try: |
| 4963 | cmdStr = "summary" |
| 4964 | if jsonFormat: |
| 4965 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4966 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4967 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4968 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4969 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 4970 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4971 | if not handle: |
| 4972 | main.log.error( self.name + ": There is no output in " + |
| 4973 | "summary command" ) |
| 4974 | return main.FALSE |
| 4975 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4976 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4977 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4978 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4979 | except TypeError: |
| 4980 | main.log.exception( self.name + ": Object not as expected" ) |
| 4981 | return None |
| 4982 | except pexpect.EOF: |
| 4983 | main.log.error( self.name + ": EOF exception found" ) |
| 4984 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4985 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4986 | except Exception: |
| 4987 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4988 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4989 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 4990 | def getAddress( self): |
| 4991 | """ |
| 4992 | Get the onos ip address from the cli. This is usefull when connecting using |
| 4993 | a container manager such as kubernetes. This function also sets self.address |
| 4994 | the value from ONOS. |
| 4995 | |
| 4996 | Returns: |
| 4997 | The string value of the key or |
| 4998 | None on Error |
| 4999 | """ |
| 5000 | try: |
| 5001 | output = self.summary() |
| 5002 | address = json.loads( output ).get( 'node' ) |
| 5003 | self.address = address |
| 5004 | return address |
You Wang | 327bad4 | 2021-03-24 14:19:58 -0700 | [diff] [blame] | 5005 | except ( TypeError, ValueError ): |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 5006 | main.log.exception( self.name + ": Object not as expected" ) |
| 5007 | return None |
| 5008 | except Exception: |
| 5009 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5010 | main.cleanAndExit() |
| 5011 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 5012 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5013 | """ |
| 5014 | CLI command to get the value of a key in a consistent map using |
| 5015 | transactions. This a test function and can only get keys from the |
| 5016 | test map hard coded into the cli command |
| 5017 | Required arguments: |
| 5018 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5019 | returns: |
| 5020 | The string value of the key or |
| 5021 | None on Error |
| 5022 | """ |
| 5023 | try: |
| 5024 | keyName = str( keyName ) |
| 5025 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5026 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 5027 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5028 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 5029 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 5030 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5031 | return None |
| 5032 | else: |
| 5033 | match = re.search( pattern, output ) |
| 5034 | if match: |
| 5035 | return match.groupdict()[ 'value' ] |
| 5036 | else: |
| 5037 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 5038 | " match expected output." ) |
| 5039 | main.log.debug( self.name + " expected: " + pattern ) |
| 5040 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 5041 | return None |
| 5042 | except TypeError: |
| 5043 | main.log.exception( self.name + ": Object not as expected" ) |
| 5044 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5045 | except Exception: |
| 5046 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5047 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5048 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 5049 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5050 | """ |
| 5051 | CLI command to put a value into 'numKeys' number of keys in a |
| 5052 | consistent map using transactions. This a test function and can only |
| 5053 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 5054 | Required arguments: |
| 5055 | numKeys - Number of keys to add the value to |
| 5056 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5057 | returns: |
| 5058 | A dictionary whose keys are the name of the keys put into the map |
| 5059 | and the values of the keys are dictionaries whose key-values are |
| 5060 | 'value': value put into map and optionaly |
| 5061 | 'oldValue': Previous value in the key or |
| 5062 | None on Error |
| 5063 | |
| 5064 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5065 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 5066 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5067 | """ |
| 5068 | try: |
| 5069 | numKeys = str( numKeys ) |
| 5070 | value = str( value ) |
| 5071 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5072 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 5073 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5074 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 5075 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 5076 | results = {} |
| 5077 | for line in output.splitlines(): |
| 5078 | new = re.search( newPattern, line ) |
| 5079 | updated = re.search( updatedPattern, line ) |
| 5080 | if new: |
| 5081 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 5082 | elif updated: |
| 5083 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5084 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5085 | else: |
| 5086 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 5087 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5088 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 5089 | newPattern, |
| 5090 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5091 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 5092 | return results |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 5093 | except ( TypeError, AttributeError ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5094 | main.log.exception( self.name + ": Object not as expected" ) |
| 5095 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5096 | except Exception: |
| 5097 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5098 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5099 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5100 | def maps( self, jsonFormat=True ): |
| 5101 | """ |
| 5102 | Description: Returns result of onos:maps |
| 5103 | Optional: |
| 5104 | * jsonFormat: enable json formatting of output |
| 5105 | """ |
| 5106 | try: |
| 5107 | cmdStr = "maps" |
| 5108 | if jsonFormat: |
| 5109 | cmdStr += " -j" |
| 5110 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5111 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5112 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5113 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5114 | except AssertionError: |
| 5115 | main.log.exception( "" ) |
| 5116 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5117 | except TypeError: |
| 5118 | main.log.exception( self.name + ": Object not as expected" ) |
| 5119 | return None |
| 5120 | except pexpect.EOF: |
| 5121 | main.log.error( self.name + ": EOF exception found" ) |
| 5122 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5123 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5124 | except Exception: |
| 5125 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5126 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5127 | |
| 5128 | def getSwController( self, uri, jsonFormat=True ): |
| 5129 | """ |
| 5130 | Descrition: Gets the controller information from the device |
| 5131 | """ |
| 5132 | try: |
| 5133 | cmd = "device-controllers " |
| 5134 | if jsonFormat: |
| 5135 | cmd += "-j " |
| 5136 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5137 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5138 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5139 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5140 | except AssertionError: |
| 5141 | main.log.exception( "" ) |
| 5142 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5143 | except TypeError: |
| 5144 | main.log.exception( self.name + ": Object not as expected" ) |
| 5145 | return None |
| 5146 | except pexpect.EOF: |
| 5147 | main.log.error( self.name + ": EOF exception found" ) |
| 5148 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5149 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5150 | except Exception: |
| 5151 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5152 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5153 | |
| 5154 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 5155 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5156 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5157 | |
| 5158 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5159 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5160 | ip - String or List: The ip address of the controller. |
| 5161 | This parameter can be formed in a couple of different ways. |
| 5162 | VALID: |
| 5163 | 10.0.0.1 - just the ip address |
| 5164 | tcp:10.0.0.1 - the protocol and the ip address |
| 5165 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 5166 | so that you can add controllers with different |
| 5167 | protocols and ports |
| 5168 | INVALID: |
| 5169 | 10.0.0.1:6653 - this is not supported by ONOS |
| 5170 | |
| 5171 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 5172 | port - The port number. |
| 5173 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 5174 | |
| 5175 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 5176 | """ |
| 5177 | try: |
| 5178 | cmd = "device-setcontrollers" |
| 5179 | |
| 5180 | if jsonFormat: |
| 5181 | cmd += " -j" |
| 5182 | cmd += " " + uri |
| 5183 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5184 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5185 | for item in ip: |
| 5186 | if ":" in item: |
| 5187 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5188 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5189 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5190 | elif "." in sitem[ 1 ]: |
| 5191 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5192 | else: |
| 5193 | main.log.error( "Malformed entry: " + item ) |
| 5194 | raise TypeError |
| 5195 | else: |
| 5196 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5197 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5198 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5199 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5200 | if "Error" in response: |
| 5201 | main.log.error( response ) |
| 5202 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5203 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5204 | except AssertionError: |
| 5205 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5206 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5207 | except TypeError: |
| 5208 | main.log.exception( self.name + ": Object not as expected" ) |
| 5209 | return main.FALSE |
| 5210 | except pexpect.EOF: |
| 5211 | main.log.error( self.name + ": EOF exception found" ) |
| 5212 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5213 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5214 | except Exception: |
| 5215 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5216 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5217 | |
| 5218 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5219 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5220 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5221 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5222 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5223 | 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] | 5224 | Returns: |
| 5225 | Returns main.FALSE if an exception is thrown or an error is present |
| 5226 | in the response. Otherwise, returns main.TRUE. |
| 5227 | NOTE: |
| 5228 | 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] | 5229 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5230 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5231 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 5232 | deviceStr = device |
| 5233 | device = [] |
| 5234 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5235 | |
| 5236 | for d in device: |
| 5237 | time.sleep( 1 ) |
| 5238 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5239 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5240 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5241 | if "Error" in response: |
| 5242 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 5243 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5244 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5245 | except AssertionError: |
| 5246 | main.log.exception( "" ) |
| 5247 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5248 | except TypeError: |
| 5249 | main.log.exception( self.name + ": Object not as expected" ) |
| 5250 | return main.FALSE |
| 5251 | except pexpect.EOF: |
| 5252 | main.log.error( self.name + ": EOF exception found" ) |
| 5253 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5254 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5255 | except Exception: |
| 5256 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5257 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5258 | |
| 5259 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5260 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5261 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5262 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5263 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5264 | 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] | 5265 | Returns: |
| 5266 | Returns main.FALSE if an exception is thrown or an error is present |
| 5267 | in the response. Otherwise, returns main.TRUE. |
| 5268 | NOTE: |
| 5269 | 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] | 5270 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5271 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5272 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5273 | host = list( host ) |
| 5274 | |
| 5275 | for h in host: |
| 5276 | time.sleep( 1 ) |
| 5277 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5278 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5279 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5280 | if "Error" in response: |
| 5281 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5282 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5283 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5284 | except AssertionError: |
| 5285 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5286 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5287 | except TypeError: |
| 5288 | main.log.exception( self.name + ": Object not as expected" ) |
| 5289 | return main.FALSE |
| 5290 | except pexpect.EOF: |
| 5291 | main.log.error( self.name + ": EOF exception found" ) |
| 5292 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5293 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5294 | except Exception: |
| 5295 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5296 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5297 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5298 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5299 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5300 | Description: |
| 5301 | Bring link down or up in the null-provider. |
| 5302 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5303 | begin - (string) One end of a device or switch. |
| 5304 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5305 | returns: |
| 5306 | main.TRUE if no exceptions were thrown and no Errors are |
| 5307 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5308 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5309 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5310 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5311 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5312 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5313 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5314 | if "Error" in response or "Failure" in response: |
| 5315 | main.log.error( response ) |
| 5316 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5317 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5318 | except AssertionError: |
| 5319 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5320 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5321 | except TypeError: |
| 5322 | main.log.exception( self.name + ": Object not as expected" ) |
| 5323 | return main.FALSE |
| 5324 | except pexpect.EOF: |
| 5325 | main.log.error( self.name + ": EOF exception found" ) |
| 5326 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5327 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5328 | except Exception: |
| 5329 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5330 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5331 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5332 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5333 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5334 | Description: |
| 5335 | Changes the state of port in an OF switch by means of the |
| 5336 | PORTSTATUS OF messages. |
| 5337 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5338 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5339 | port - (string) target port in the device. Ex: '2' |
| 5340 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5341 | returns: |
| 5342 | main.TRUE if no exceptions were thrown and no Errors are |
| 5343 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5344 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5345 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5346 | state = state.lower() |
| 5347 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5348 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5349 | response = self.sendline( cmd, showResponse=True ) |
| 5350 | assert response is not None, "Error in sendline" |
| 5351 | assert "Command not found:" not in response, response |
| 5352 | if "Error" in response or "Failure" in response: |
| 5353 | main.log.error( response ) |
| 5354 | return main.FALSE |
| 5355 | return main.TRUE |
| 5356 | except AssertionError: |
| 5357 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5358 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5359 | except TypeError: |
| 5360 | main.log.exception( self.name + ": Object not as expected" ) |
| 5361 | return main.FALSE |
| 5362 | except pexpect.EOF: |
| 5363 | main.log.error( self.name + ": EOF exception found" ) |
| 5364 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5365 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5366 | except Exception: |
| 5367 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5368 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5369 | |
| 5370 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5371 | """ |
| 5372 | Set the logging level to lvl for a specific app |
| 5373 | returns main.TRUE on success |
| 5374 | returns main.FALSE if Error occurred |
| 5375 | if noExit is True, TestON will not exit, but clean up |
| 5376 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5377 | Level defaults to INFO |
| 5378 | """ |
| 5379 | try: |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 5380 | handle = self.sendline( "log:set %s %s" % ( level, app ) ) |
| 5381 | assert handle is not None, "Error in sendline" |
| 5382 | assert "Command not found:" not in handle, handle |
| 5383 | if re.search( "Error", handle ): |
| 5384 | main.log.error( self.name + ": Error in setting log level" ) |
| 5385 | main.log.error( handle ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5386 | return main.FALSE |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 5387 | else: |
| 5388 | return main.TRUE |
| 5389 | except AssertionError: |
| 5390 | main.log.exception( "" ) |
| 5391 | return None |
| 5392 | except TypeError: |
| 5393 | main.log.exception( self.name + ": Object not as expected" ) |
| 5394 | return None |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5395 | except pexpect.EOF: |
| 5396 | main.log.error( self.name + ": EOF exception found" ) |
| 5397 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5398 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5399 | except Exception: |
| 5400 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5401 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5402 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 5403 | def logList( self, saveValues=True ): |
| 5404 | """ |
| 5405 | Gets the current log levels and optionally saves them |
| 5406 | returns a dict of the log levels or |
| 5407 | returns main.FALSE if Error occurred |
| 5408 | """ |
| 5409 | try: |
| 5410 | self.handle.sendline( "log:list" ) |
| 5411 | self.handle.expect( self.karafPrompt ) |
| 5412 | |
| 5413 | response = self.handle.before |
| 5414 | logLevels = {} |
| 5415 | for line in response.splitlines(): |
| 5416 | parsed = line.split('│') |
| 5417 | logger = parsed[0].strip() |
| 5418 | if len( parsed ) != 2 or 'Level' in parsed[1] or logger[0] == '─': |
| 5419 | continue |
| 5420 | level = parsed[1].strip() |
| 5421 | logLevels[ logger ] = level |
| 5422 | if saveValues: |
| 5423 | self.logLevels = logLevels |
| 5424 | return logLevels |
| 5425 | except pexpect.TIMEOUT: |
| 5426 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 5427 | main.cleanAndExit() |
| 5428 | except pexpect.EOF: |
| 5429 | main.log.error( self.name + ": EOF exception found" ) |
| 5430 | main.log.error( self.name + ": " + self.handle.before ) |
| 5431 | main.cleanAndExit() |
| 5432 | except Exception: |
| 5433 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5434 | main.cleanAndExit() |
| 5435 | |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5436 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5437 | """ |
| 5438 | Return a dictionary which describes the latest network topology data as a |
| 5439 | graph. |
| 5440 | An example of the dictionary: |
| 5441 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5442 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5443 | Each vertex should at least have an 'edges' attribute which describes the |
| 5444 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5445 | 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] | 5446 | list of attributes. |
| 5447 | An example of the edges dictionary: |
| 5448 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5449 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5450 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5451 | in topology data. |
| 5452 | """ |
| 5453 | graphDict = {} |
| 5454 | try: |
| 5455 | links = self.links() |
| 5456 | links = json.loads( links ) |
| 5457 | devices = self.devices() |
| 5458 | devices = json.loads( devices ) |
| 5459 | idToDevice = {} |
| 5460 | for device in devices: |
| 5461 | idToDevice[ device[ 'id' ] ] = device |
| 5462 | if includeHost: |
| 5463 | hosts = self.hosts() |
| 5464 | # FIXME: support 'includeHost' argument |
| 5465 | for link in links: |
| 5466 | nodeA = link[ 'src' ][ 'device' ] |
| 5467 | nodeB = link[ 'dst' ][ 'device' ] |
| 5468 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5469 | if nodeA not in graphDict.keys(): |
| 5470 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5471 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5472 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5473 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5474 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5475 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5476 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5477 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5478 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5479 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5480 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5481 | else: |
| 5482 | # Assert nodeB is not connected to any current links of nodeA |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 5483 | # assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 5484 | pass |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5485 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5486 | 'type': link[ 'type' ], |
| 5487 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5488 | return graphDict |
| 5489 | except ( TypeError, ValueError ): |
| 5490 | main.log.exception( self.name + ": Object not as expected" ) |
| 5491 | return None |
| 5492 | except KeyError: |
| 5493 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5494 | return None |
| 5495 | except AssertionError: |
| 5496 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5497 | return None |
| 5498 | except pexpect.EOF: |
| 5499 | main.log.error( self.name + ": EOF exception found" ) |
| 5500 | main.log.error( self.name + ": " + self.handle.before ) |
| 5501 | return None |
| 5502 | except Exception: |
| 5503 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5504 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5505 | |
| 5506 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5507 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5508 | Send command to check intent-perf summary |
| 5509 | Returns: dictionary for intent-perf summary |
| 5510 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5511 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5512 | cmd = "intent-perf -s" |
| 5513 | respDic = {} |
| 5514 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5515 | assert resp is not None, "Error in sendline" |
| 5516 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5517 | try: |
| 5518 | # Generate the dictionary to return |
| 5519 | for l in resp.split( "\n" ): |
| 5520 | # Delete any white space in line |
| 5521 | temp = re.sub( r'\s+', '', l ) |
| 5522 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5523 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5524 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5525 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5526 | main.log.exception( self.name + ": Object not as expected" ) |
| 5527 | return None |
| 5528 | except KeyError: |
| 5529 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5530 | return None |
| 5531 | except AssertionError: |
| 5532 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5533 | return None |
| 5534 | except pexpect.EOF: |
| 5535 | main.log.error( self.name + ": EOF exception found" ) |
| 5536 | main.log.error( self.name + ": " + self.handle.before ) |
| 5537 | return None |
| 5538 | except Exception: |
| 5539 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5540 | return None |
| 5541 | return respDic |
| 5542 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5543 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5544 | """ |
| 5545 | Searches the latest ONOS log file for the given search term and |
| 5546 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5547 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5548 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5549 | searchTerm: |
| 5550 | The string to grep from the ONOS log. |
| 5551 | startLine: |
| 5552 | The term that decides which line is the start to search the searchTerm in |
| 5553 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5554 | logNum: |
| 5555 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5556 | information.Because of this, search mutiply logs is necessary to capture |
| 5557 | the right result. logNum is the number of karaf logs that we need to search |
| 5558 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5559 | mode: |
| 5560 | all: return all the strings that contain the search term |
| 5561 | last: return the last string that contains the search term |
| 5562 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5563 | num: return the number of times that the searchTerm appears in the log |
| 5564 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5565 | """ |
| 5566 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5567 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5568 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5569 | logPath = '/opt/onos/log/karaf.log.' |
| 5570 | logPaths = '/opt/onos/log/karaf.log' |
| 5571 | for i in range( 1, logNum ): |
| 5572 | logPaths = logPath + str( i ) + " " + logPaths |
| 5573 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5574 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5575 | # 100000000 is just a extreme large number to make sure this function can |
| 5576 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5577 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5578 | if mode == 'all': |
| 5579 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5580 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5581 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5582 | elif mode == 'first': |
| 5583 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5584 | elif mode == 'num': |
You Wang | d91a70f | 2019-01-03 15:28:10 -0800 | [diff] [blame] | 5585 | cmd = cmd + " | grep \'" + searchTerm + "\' | wc -l" |
| 5586 | num = self.lineCount( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5587 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5588 | elif mode == 'total': |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 5589 | totalLines = self.lineCount( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5590 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5591 | else: |
| 5592 | main.log.error( self.name + " unsupported mode" ) |
| 5593 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5594 | before = self.sendline( cmd ) |
| 5595 | before = before.splitlines() |
| 5596 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5597 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5598 | return returnLines |
| 5599 | except AssertionError: |
| 5600 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5601 | return None |
| 5602 | except pexpect.EOF: |
| 5603 | main.log.error( self.name + ": EOF exception found" ) |
| 5604 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5605 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5606 | except pexpect.TIMEOUT: |
| 5607 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5608 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5609 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5610 | except Exception: |
| 5611 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5612 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5613 | |
| 5614 | def vplsShow( self, jsonFormat=True ): |
| 5615 | """ |
| 5616 | Description: Returns result of onos:vpls show, which should list the |
| 5617 | configured VPLS networks and the assigned interfaces. |
| 5618 | Optional: |
| 5619 | * jsonFormat: enable json formatting of output |
| 5620 | Returns: |
| 5621 | The output of the command or None on error. |
| 5622 | """ |
| 5623 | try: |
| 5624 | cmdStr = "vpls show" |
| 5625 | if jsonFormat: |
| 5626 | raise NotImplementedError |
| 5627 | cmdStr += " -j" |
| 5628 | handle = self.sendline( cmdStr ) |
| 5629 | assert handle is not None, "Error in sendline" |
| 5630 | assert "Command not found:" not in handle, handle |
| 5631 | return handle |
| 5632 | except AssertionError: |
| 5633 | main.log.exception( "" ) |
| 5634 | return None |
| 5635 | except TypeError: |
| 5636 | main.log.exception( self.name + ": Object not as expected" ) |
| 5637 | return None |
| 5638 | except pexpect.EOF: |
| 5639 | main.log.error( self.name + ": EOF exception found" ) |
| 5640 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5641 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5642 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5643 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5644 | return None |
| 5645 | except Exception: |
| 5646 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5647 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5648 | |
| 5649 | def parseVplsShow( self ): |
| 5650 | """ |
| 5651 | Parse the cli output of 'vpls show' into json output. This is required |
| 5652 | as there is currently no json output available. |
| 5653 | """ |
| 5654 | try: |
| 5655 | output = [] |
| 5656 | raw = self.vplsShow( jsonFormat=False ) |
| 5657 | namePat = "VPLS name: (?P<name>\w+)" |
| 5658 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5659 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5660 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5661 | mIter = re.finditer( pattern, raw ) |
| 5662 | for match in mIter: |
| 5663 | item = {} |
| 5664 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5665 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5666 | if ifaces == [ "" ]: |
| 5667 | ifaces = [] |
| 5668 | item[ 'interfaces' ] = ifaces |
| 5669 | encap = match.group( 'encap' ) |
| 5670 | if encap != 'NONE': |
| 5671 | item[ 'encapsulation' ] = encap.lower() |
| 5672 | output.append( item ) |
| 5673 | return output |
| 5674 | except Exception: |
| 5675 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5676 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5677 | |
| 5678 | def vplsList( self, jsonFormat=True ): |
| 5679 | """ |
| 5680 | Description: Returns result of onos:vpls list, which should list the |
| 5681 | configured VPLS networks. |
| 5682 | Optional: |
| 5683 | * jsonFormat: enable json formatting of output |
| 5684 | """ |
| 5685 | try: |
| 5686 | cmdStr = "vpls list" |
| 5687 | if jsonFormat: |
| 5688 | raise NotImplementedError |
| 5689 | cmdStr += " -j" |
| 5690 | handle = self.sendline( cmdStr ) |
| 5691 | assert handle is not None, "Error in sendline" |
| 5692 | assert "Command not found:" not in handle, handle |
| 5693 | return handle |
| 5694 | except AssertionError: |
| 5695 | main.log.exception( "" ) |
| 5696 | return None |
| 5697 | except TypeError: |
| 5698 | main.log.exception( self.name + ": Object not as expected" ) |
| 5699 | return None |
| 5700 | except pexpect.EOF: |
| 5701 | main.log.error( self.name + ": EOF exception found" ) |
| 5702 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5703 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5704 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5705 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5706 | return None |
| 5707 | except Exception: |
| 5708 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5709 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5710 | |
| 5711 | def vplsCreate( self, network ): |
| 5712 | """ |
| 5713 | CLI command to create a new VPLS network. |
| 5714 | Required arguments: |
| 5715 | network - String name of the network to create. |
| 5716 | returns: |
| 5717 | main.TRUE on success and main.FALSE on failure |
| 5718 | """ |
| 5719 | try: |
| 5720 | network = str( network ) |
| 5721 | cmdStr = "vpls create " |
| 5722 | cmdStr += network |
| 5723 | output = self.sendline( cmdStr ) |
| 5724 | assert output is not None, "Error in sendline" |
| 5725 | assert "Command not found:" not in output, output |
| 5726 | assert "Error executing command" not in output, output |
| 5727 | assert "VPLS already exists:" not in output, output |
| 5728 | return main.TRUE |
| 5729 | except AssertionError: |
| 5730 | main.log.exception( "" ) |
| 5731 | return main.FALSE |
| 5732 | except TypeError: |
| 5733 | main.log.exception( self.name + ": Object not as expected" ) |
| 5734 | return main.FALSE |
| 5735 | except pexpect.EOF: |
| 5736 | main.log.error( self.name + ": EOF exception found" ) |
| 5737 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5738 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5739 | except Exception: |
| 5740 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5741 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5742 | |
| 5743 | def vplsDelete( self, network ): |
| 5744 | """ |
| 5745 | CLI command to delete a VPLS network. |
| 5746 | Required arguments: |
| 5747 | network - Name of the network to delete. |
| 5748 | returns: |
| 5749 | main.TRUE on success and main.FALSE on failure |
| 5750 | """ |
| 5751 | try: |
| 5752 | network = str( network ) |
| 5753 | cmdStr = "vpls delete " |
| 5754 | cmdStr += network |
| 5755 | output = self.sendline( cmdStr ) |
| 5756 | assert output is not None, "Error in sendline" |
| 5757 | assert "Command not found:" not in output, output |
| 5758 | assert "Error executing command" not in output, output |
| 5759 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5760 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5761 | return main.TRUE |
| 5762 | except AssertionError: |
| 5763 | main.log.exception( "" ) |
| 5764 | return main.FALSE |
| 5765 | except TypeError: |
| 5766 | main.log.exception( self.name + ": Object not as expected" ) |
| 5767 | return main.FALSE |
| 5768 | except pexpect.EOF: |
| 5769 | main.log.error( self.name + ": EOF exception found" ) |
| 5770 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5771 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5772 | except Exception: |
| 5773 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5774 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5775 | |
| 5776 | def vplsAddIface( self, network, iface ): |
| 5777 | """ |
| 5778 | CLI command to add an interface to a VPLS network. |
| 5779 | Required arguments: |
| 5780 | network - Name of the network to add the interface to. |
| 5781 | iface - The ONOS name for an interface. |
| 5782 | returns: |
| 5783 | main.TRUE on success and main.FALSE on failure |
| 5784 | """ |
| 5785 | try: |
| 5786 | network = str( network ) |
| 5787 | iface = str( iface ) |
| 5788 | cmdStr = "vpls add-if " |
| 5789 | cmdStr += network + " " + iface |
| 5790 | output = self.sendline( cmdStr ) |
| 5791 | assert output is not None, "Error in sendline" |
| 5792 | assert "Command not found:" not in output, output |
| 5793 | assert "Error executing command" not in output, output |
| 5794 | assert "already associated to network" not in output, output |
| 5795 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5796 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5797 | return main.TRUE |
| 5798 | except AssertionError: |
| 5799 | main.log.exception( "" ) |
| 5800 | return main.FALSE |
| 5801 | except TypeError: |
| 5802 | main.log.exception( self.name + ": Object not as expected" ) |
| 5803 | return main.FALSE |
| 5804 | except pexpect.EOF: |
| 5805 | main.log.error( self.name + ": EOF exception found" ) |
| 5806 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5807 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5808 | except Exception: |
| 5809 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5810 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5811 | |
| 5812 | def vplsRemIface( self, network, iface ): |
| 5813 | """ |
| 5814 | CLI command to remove an interface from a VPLS network. |
| 5815 | Required arguments: |
| 5816 | network - Name of the network to remove the interface from. |
| 5817 | iface - Name of the interface to remove. |
| 5818 | returns: |
| 5819 | main.TRUE on success and main.FALSE on failure |
| 5820 | """ |
| 5821 | try: |
| 5822 | iface = str( iface ) |
| 5823 | cmdStr = "vpls rem-if " |
| 5824 | cmdStr += network + " " + iface |
| 5825 | output = self.sendline( cmdStr ) |
| 5826 | assert output is not None, "Error in sendline" |
| 5827 | assert "Command not found:" not in output, output |
| 5828 | assert "Error executing command" not in output, output |
| 5829 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5830 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5831 | return main.TRUE |
| 5832 | except AssertionError: |
| 5833 | main.log.exception( "" ) |
| 5834 | return main.FALSE |
| 5835 | except TypeError: |
| 5836 | main.log.exception( self.name + ": Object not as expected" ) |
| 5837 | return main.FALSE |
| 5838 | except pexpect.EOF: |
| 5839 | main.log.error( self.name + ": EOF exception found" ) |
| 5840 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5841 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5842 | except Exception: |
| 5843 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5844 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5845 | |
| 5846 | def vplsClean( self ): |
| 5847 | """ |
| 5848 | Description: Clears the VPLS app configuration. |
| 5849 | Returns: main.TRUE on success and main.FALSE on failure |
| 5850 | """ |
| 5851 | try: |
| 5852 | cmdStr = "vpls clean" |
| 5853 | handle = self.sendline( cmdStr ) |
| 5854 | assert handle is not None, "Error in sendline" |
| 5855 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5856 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5857 | return handle |
| 5858 | except AssertionError: |
| 5859 | main.log.exception( "" ) |
| 5860 | return main.FALSE |
| 5861 | except TypeError: |
| 5862 | main.log.exception( self.name + ": Object not as expected" ) |
| 5863 | return main.FALSE |
| 5864 | except pexpect.EOF: |
| 5865 | main.log.error( self.name + ": EOF exception found" ) |
| 5866 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5867 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5868 | except Exception: |
| 5869 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5870 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5871 | |
| 5872 | def vplsSetEncap( self, network, encapType ): |
| 5873 | """ |
| 5874 | CLI command to add an interface to a VPLS network. |
| 5875 | Required arguments: |
| 5876 | network - Name of the network to create. |
| 5877 | encapType - Type of encapsulation. |
| 5878 | returns: |
| 5879 | main.TRUE on success and main.FALSE on failure |
| 5880 | """ |
| 5881 | try: |
| 5882 | network = str( network ) |
| 5883 | encapType = str( encapType ).upper() |
| 5884 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5885 | cmdStr = "vpls set-encap " |
| 5886 | cmdStr += network + " " + encapType |
| 5887 | output = self.sendline( cmdStr ) |
| 5888 | assert output is not None, "Error in sendline" |
| 5889 | assert "Command not found:" not in output, output |
| 5890 | assert "Error executing command" not in output, output |
| 5891 | assert "already associated to network" not in output, output |
| 5892 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5893 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5894 | return main.TRUE |
| 5895 | except AssertionError: |
| 5896 | main.log.exception( "" ) |
| 5897 | return main.FALSE |
| 5898 | except TypeError: |
| 5899 | main.log.exception( self.name + ": Object not as expected" ) |
| 5900 | return main.FALSE |
| 5901 | except pexpect.EOF: |
| 5902 | main.log.error( self.name + ": EOF exception found" ) |
| 5903 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5904 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5905 | except Exception: |
| 5906 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5907 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5908 | |
| 5909 | def interfaces( self, jsonFormat=True ): |
| 5910 | """ |
| 5911 | Description: Returns result of interfaces command. |
| 5912 | Optional: |
| 5913 | * jsonFormat: enable json formatting of output |
| 5914 | Returns: |
| 5915 | The output of the command or None on error. |
| 5916 | """ |
| 5917 | try: |
| 5918 | cmdStr = "interfaces" |
| 5919 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5920 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5921 | cmdStr += " -j" |
| 5922 | handle = self.sendline( cmdStr ) |
| 5923 | assert handle is not None, "Error in sendline" |
| 5924 | assert "Command not found:" not in handle, handle |
| 5925 | return handle |
| 5926 | except AssertionError: |
| 5927 | main.log.exception( "" ) |
| 5928 | return None |
| 5929 | except TypeError: |
| 5930 | main.log.exception( self.name + ": Object not as expected" ) |
| 5931 | return None |
| 5932 | except pexpect.EOF: |
| 5933 | main.log.error( self.name + ": EOF exception found" ) |
| 5934 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5935 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5936 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5937 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5938 | return None |
| 5939 | except Exception: |
| 5940 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5941 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5942 | |
| 5943 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5944 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5945 | Get the timestamp of searchTerm from karaf log. |
| 5946 | |
| 5947 | Arguments: |
| 5948 | splitTerm_before and splitTerm_after: |
| 5949 | |
| 5950 | The terms that split the string that contains the timeStamp of |
| 5951 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5952 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5953 | and the splitTerm_after is "x" |
| 5954 | |
| 5955 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5956 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5957 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5958 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5959 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5960 | return main.ERROR |
| 5961 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5962 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5963 | main.log.warn( "Captured timestamp string is empty" ) |
| 5964 | return main.ERROR |
| 5965 | lines = lines[ 0 ] |
| 5966 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5967 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5968 | # get the target value |
| 5969 | line = lines.split( splitTerm_before ) |
| 5970 | key = line[ 1 ].split( splitTerm_after ) |
| 5971 | return int( key[ 0 ] ) |
| 5972 | except IndexError: |
| 5973 | main.log.warn( "Index Error!" ) |
| 5974 | return main.ERROR |
| 5975 | except AssertionError: |
| 5976 | main.log.warn( "Search Term Not Found " ) |
| 5977 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5978 | |
| 5979 | def workQueueAdd( self, queueName, value ): |
| 5980 | """ |
| 5981 | CLI command to add a string to the specified Work Queue. |
| 5982 | This function uses the distributed primitives test app, which |
| 5983 | gives some cli access to distributed primitives for testing |
| 5984 | purposes only. |
| 5985 | |
| 5986 | Required arguments: |
| 5987 | queueName - The name of the queue to add to |
| 5988 | value - The value to add to the queue |
| 5989 | returns: |
| 5990 | main.TRUE on success, main.FALSE on failure and |
| 5991 | main.ERROR on error. |
| 5992 | """ |
| 5993 | try: |
| 5994 | queueName = str( queueName ) |
| 5995 | value = str( value ) |
| 5996 | prefix = "work-queue-test" |
| 5997 | operation = "add" |
| 5998 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5999 | output = self.distPrimitivesSend( cmdStr ) |
| 6000 | if "Invalid operation name" in output: |
| 6001 | main.log.warn( output ) |
| 6002 | return main.ERROR |
| 6003 | elif "Done" in output: |
| 6004 | return main.TRUE |
| 6005 | except TypeError: |
| 6006 | main.log.exception( self.name + ": Object not as expected" ) |
| 6007 | return main.ERROR |
| 6008 | except Exception: |
| 6009 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6010 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6011 | |
| 6012 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 6013 | """ |
| 6014 | CLI command to add two strings to the specified Work Queue. |
| 6015 | This function uses the distributed primitives test app, which |
| 6016 | gives some cli access to distributed primitives for testing |
| 6017 | purposes only. |
| 6018 | |
| 6019 | Required arguments: |
| 6020 | queueName - The name of the queue to add to |
| 6021 | value1 - The first value to add to the queue |
| 6022 | value2 - The second value to add to the queue |
| 6023 | returns: |
| 6024 | main.TRUE on success, main.FALSE on failure and |
| 6025 | main.ERROR on error. |
| 6026 | """ |
| 6027 | try: |
| 6028 | queueName = str( queueName ) |
| 6029 | value1 = str( value1 ) |
| 6030 | value2 = str( value2 ) |
| 6031 | prefix = "work-queue-test" |
| 6032 | operation = "addMultiple" |
| 6033 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 6034 | output = self.distPrimitivesSend( cmdStr ) |
| 6035 | if "Invalid operation name" in output: |
| 6036 | main.log.warn( output ) |
| 6037 | return main.ERROR |
| 6038 | elif "Done" in output: |
| 6039 | return main.TRUE |
| 6040 | except TypeError: |
| 6041 | main.log.exception( self.name + ": Object not as expected" ) |
| 6042 | return main.ERROR |
| 6043 | except Exception: |
| 6044 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6045 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6046 | |
| 6047 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 6048 | """ |
| 6049 | CLI command to take a value from the specified Work Queue and compelte it. |
| 6050 | This function uses the distributed primitives test app, which |
| 6051 | gives some cli access to distributed primitives for testing |
| 6052 | purposes only. |
| 6053 | |
| 6054 | Required arguments: |
| 6055 | queueName - The name of the queue to add to |
| 6056 | number - The number of items to take and complete |
| 6057 | returns: |
| 6058 | main.TRUE on success, main.FALSE on failure and |
| 6059 | main.ERROR on error. |
| 6060 | """ |
| 6061 | try: |
| 6062 | queueName = str( queueName ) |
| 6063 | number = str( int( number ) ) |
| 6064 | prefix = "work-queue-test" |
| 6065 | operation = "takeAndComplete" |
| 6066 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 6067 | output = self.distPrimitivesSend( cmdStr ) |
| 6068 | if "Invalid operation name" in output: |
| 6069 | main.log.warn( output ) |
| 6070 | return main.ERROR |
| 6071 | elif "Done" in output: |
| 6072 | return main.TRUE |
| 6073 | except TypeError: |
| 6074 | main.log.exception( self.name + ": Object not as expected" ) |
| 6075 | return main.ERROR |
| 6076 | except Exception: |
| 6077 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6078 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6079 | |
| 6080 | def workQueueDestroy( self, queueName ): |
| 6081 | """ |
| 6082 | CLI command to destroy the specified Work Queue. |
| 6083 | This function uses the distributed primitives test app, which |
| 6084 | gives some cli access to distributed primitives for testing |
| 6085 | purposes only. |
| 6086 | |
| 6087 | Required arguments: |
| 6088 | queueName - The name of the queue to add to |
| 6089 | returns: |
| 6090 | main.TRUE on success, main.FALSE on failure and |
| 6091 | main.ERROR on error. |
| 6092 | """ |
| 6093 | try: |
| 6094 | queueName = str( queueName ) |
| 6095 | prefix = "work-queue-test" |
| 6096 | operation = "destroy" |
| 6097 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6098 | output = self.distPrimitivesSend( cmdStr ) |
| 6099 | if "Invalid operation name" in output: |
| 6100 | main.log.warn( output ) |
| 6101 | return main.ERROR |
| 6102 | return main.TRUE |
| 6103 | except TypeError: |
| 6104 | main.log.exception( self.name + ": Object not as expected" ) |
| 6105 | return main.ERROR |
| 6106 | except Exception: |
| 6107 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6108 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6109 | |
| 6110 | def workQueueTotalPending( self, queueName ): |
| 6111 | """ |
| 6112 | CLI command to get the Total Pending items of the specified Work Queue. |
| 6113 | This function uses the distributed primitives test app, which |
| 6114 | gives some cli access to distributed primitives for testing |
| 6115 | purposes only. |
| 6116 | |
| 6117 | Required arguments: |
| 6118 | queueName - The name of the queue to add to |
| 6119 | returns: |
| 6120 | The number of Pending items in the specified work queue or |
| 6121 | None on error |
| 6122 | """ |
| 6123 | try: |
| 6124 | queueName = str( queueName ) |
| 6125 | prefix = "work-queue-test" |
| 6126 | operation = "totalPending" |
| 6127 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6128 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6129 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6130 | output = output.split( "\r\n" )[ -1 ] |
| 6131 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6132 | pattern = r'\d+' |
| 6133 | if "Invalid operation name" in output: |
| 6134 | main.log.warn( output ) |
| 6135 | return None |
| 6136 | else: |
| 6137 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6138 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6139 | except ( AttributeError, TypeError ): |
| 6140 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6141 | return None |
| 6142 | except Exception: |
| 6143 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6144 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6145 | |
| 6146 | def workQueueTotalCompleted( self, queueName ): |
| 6147 | """ |
| 6148 | CLI command to get the Total Completed items of the specified Work Queue. |
| 6149 | This function uses the distributed primitives test app, which |
| 6150 | gives some cli access to distributed primitives for testing |
| 6151 | purposes only. |
| 6152 | |
| 6153 | Required arguments: |
| 6154 | queueName - The name of the queue to add to |
| 6155 | returns: |
| 6156 | The number of complete items in the specified work queue or |
| 6157 | None on error |
| 6158 | """ |
| 6159 | try: |
| 6160 | queueName = str( queueName ) |
| 6161 | prefix = "work-queue-test" |
| 6162 | operation = "totalCompleted" |
| 6163 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6164 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6165 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6166 | output = output.split( "\r\n" )[ -1 ] |
| 6167 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6168 | pattern = r'\d+' |
| 6169 | if "Invalid operation name" in output: |
| 6170 | main.log.warn( output ) |
| 6171 | return None |
| 6172 | else: |
| 6173 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6174 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6175 | except ( AttributeError, TypeError ): |
| 6176 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6177 | return None |
| 6178 | except Exception: |
| 6179 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6180 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6181 | |
| 6182 | def workQueueTotalInProgress( self, queueName ): |
| 6183 | """ |
| 6184 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 6185 | This function uses the distributed primitives test app, which |
| 6186 | gives some cli access to distributed primitives for testing |
| 6187 | purposes only. |
| 6188 | |
| 6189 | Required arguments: |
| 6190 | queueName - The name of the queue to add to |
| 6191 | returns: |
| 6192 | The number of In Progress items in the specified work queue or |
| 6193 | None on error |
| 6194 | """ |
| 6195 | try: |
| 6196 | queueName = str( queueName ) |
| 6197 | prefix = "work-queue-test" |
| 6198 | operation = "totalInProgress" |
| 6199 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6200 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6201 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6202 | output = output.split( "\r\n" )[ -1 ] |
| 6203 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6204 | pattern = r'\d+' |
| 6205 | if "Invalid operation name" in output: |
| 6206 | main.log.warn( output ) |
| 6207 | return None |
| 6208 | else: |
| 6209 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6210 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6211 | except ( AttributeError, TypeError ): |
| 6212 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6213 | return None |
| 6214 | except Exception: |
| 6215 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6216 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 6217 | |
| 6218 | def events( self, args='-a' ): |
| 6219 | """ |
| 6220 | Description: Returns events -a command output |
| 6221 | Optional: |
| 6222 | add other arguments |
| 6223 | """ |
| 6224 | try: |
| 6225 | cmdStr = "events" |
| 6226 | if args: |
| 6227 | cmdStr += " " + args |
| 6228 | handle = self.sendline( cmdStr ) |
| 6229 | assert handle is not None, "Error in sendline" |
| 6230 | assert "Command not found:" not in handle, handle |
| 6231 | return handle |
| 6232 | except AssertionError: |
| 6233 | main.log.exception( "" ) |
| 6234 | return None |
| 6235 | except TypeError: |
| 6236 | main.log.exception( self.name + ": Object not as expected" ) |
| 6237 | return None |
| 6238 | except pexpect.EOF: |
| 6239 | main.log.error( self.name + ": EOF exception found" ) |
| 6240 | main.log.error( self.name + ": " + self.handle.before ) |
| 6241 | main.cleanAndExit() |
| 6242 | except Exception: |
| 6243 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6244 | main.cleanAndExit() |
| 6245 | |
| 6246 | def getMaster( self, deviceID ): |
| 6247 | """ |
| 6248 | Description: Obtains current master using "roles" command for a specific deviceID |
| 6249 | """ |
| 6250 | try: |
| 6251 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 6252 | except AssertionError: |
| 6253 | main.log.exception( "" ) |
| 6254 | return None |
| 6255 | except TypeError: |
| 6256 | main.log.exception( self.name + ": Object not as expected" ) |
| 6257 | return None |
| 6258 | except pexpect.EOF: |
| 6259 | main.log.error( self.name + ": EOF exception found" ) |
| 6260 | main.log.error( self.name + ": " + self.handle.before ) |
| 6261 | main.cleanAndExit() |
| 6262 | except Exception: |
| 6263 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 6264 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 6265 | |
| 6266 | def issu( self ): |
| 6267 | """ |
| 6268 | Short summary of In-Service Software Upgrade status |
| 6269 | |
| 6270 | Returns the output of the cli command or None on Error |
| 6271 | """ |
| 6272 | try: |
| 6273 | cmdStr = "issu" |
| 6274 | handle = self.sendline( cmdStr ) |
| 6275 | assert handle is not None, "Error in sendline" |
| 6276 | assert "Command not found:" not in handle, handle |
| 6277 | assert "Unsupported command:" not in handle, handle |
| 6278 | return handle |
| 6279 | except AssertionError: |
| 6280 | main.log.exception( "" ) |
| 6281 | return None |
| 6282 | except TypeError: |
| 6283 | main.log.exception( self.name + ": Object not as expected" ) |
| 6284 | return None |
| 6285 | except pexpect.EOF: |
| 6286 | main.log.error( self.name + ": EOF exception found" ) |
| 6287 | main.log.error( self.name + ": " + self.handle.before ) |
| 6288 | main.cleanAndExit() |
| 6289 | except Exception: |
| 6290 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6291 | main.cleanAndExit() |
| 6292 | |
| 6293 | def issuInit( self ): |
| 6294 | """ |
| 6295 | Initiates an In-Service Software Upgrade |
| 6296 | |
| 6297 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6298 | """ |
| 6299 | try: |
| 6300 | cmdStr = "issu init" |
| 6301 | handle = self.sendline( cmdStr ) |
| 6302 | assert handle is not None, "Error in sendline" |
| 6303 | assert "Command not found:" not in handle, handle |
| 6304 | assert "Unsupported command:" not in handle, handle |
| 6305 | if "Initialized" in handle: |
| 6306 | return main.TRUE |
| 6307 | else: |
| 6308 | return main.FALSE |
| 6309 | except AssertionError: |
| 6310 | main.log.exception( "" ) |
| 6311 | return main.ERROR |
| 6312 | except TypeError: |
| 6313 | main.log.exception( self.name + ": Object not as expected" ) |
| 6314 | return main.ERROR |
| 6315 | except pexpect.EOF: |
| 6316 | main.log.error( self.name + ": EOF exception found" ) |
| 6317 | main.log.error( self.name + ": " + self.handle.before ) |
| 6318 | main.cleanAndExit() |
| 6319 | except Exception: |
| 6320 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6321 | main.cleanAndExit() |
| 6322 | |
| 6323 | def issuUpgrade( self ): |
| 6324 | """ |
| 6325 | Transitions stores to upgraded nodes |
| 6326 | |
| 6327 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6328 | """ |
| 6329 | try: |
| 6330 | cmdStr = "issu upgrade" |
| 6331 | handle = self.sendline( cmdStr ) |
| 6332 | assert handle is not None, "Error in sendline" |
| 6333 | assert "Command not found:" not in handle, handle |
| 6334 | assert "Unsupported command:" not in handle, handle |
| 6335 | if "Upgraded" in handle: |
| 6336 | return main.TRUE |
| 6337 | else: |
| 6338 | return main.FALSE |
| 6339 | except AssertionError: |
| 6340 | main.log.exception( "" ) |
| 6341 | return main.ERROR |
| 6342 | except TypeError: |
| 6343 | main.log.exception( self.name + ": Object not as expected" ) |
| 6344 | return main.ERROR |
| 6345 | except pexpect.EOF: |
| 6346 | main.log.error( self.name + ": EOF exception found" ) |
| 6347 | main.log.error( self.name + ": " + self.handle.before ) |
| 6348 | main.cleanAndExit() |
| 6349 | except Exception: |
| 6350 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6351 | main.cleanAndExit() |
| 6352 | |
| 6353 | def issuCommit( self ): |
| 6354 | """ |
| 6355 | Finalizes an In-Service Software Upgrade |
| 6356 | |
| 6357 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6358 | """ |
| 6359 | try: |
| 6360 | cmdStr = "issu commit" |
| 6361 | handle = self.sendline( cmdStr ) |
| 6362 | assert handle is not None, "Error in sendline" |
| 6363 | assert "Command not found:" not in handle, handle |
| 6364 | assert "Unsupported command:" not in handle, handle |
| 6365 | # TODO: Check the version returned by this command |
| 6366 | if "Committed version" in handle: |
| 6367 | return main.TRUE |
| 6368 | else: |
| 6369 | return main.FALSE |
| 6370 | except AssertionError: |
| 6371 | main.log.exception( "" ) |
| 6372 | return main.ERROR |
| 6373 | except TypeError: |
| 6374 | main.log.exception( self.name + ": Object not as expected" ) |
| 6375 | return main.ERROR |
| 6376 | except pexpect.EOF: |
| 6377 | main.log.error( self.name + ": EOF exception found" ) |
| 6378 | main.log.error( self.name + ": " + self.handle.before ) |
| 6379 | main.cleanAndExit() |
| 6380 | except Exception: |
| 6381 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6382 | main.cleanAndExit() |
| 6383 | |
| 6384 | def issuRollback( self ): |
| 6385 | """ |
| 6386 | Rolls back an In-Service Software Upgrade |
| 6387 | |
| 6388 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6389 | """ |
| 6390 | try: |
| 6391 | cmdStr = "issu rollback" |
| 6392 | handle = self.sendline( cmdStr ) |
| 6393 | assert handle is not None, "Error in sendline" |
| 6394 | assert "Command not found:" not in handle, handle |
| 6395 | assert "Unsupported command:" not in handle, handle |
| 6396 | # TODO: Check the version returned by this command |
| 6397 | if "Rolled back to version" in handle: |
| 6398 | return main.TRUE |
| 6399 | else: |
| 6400 | return main.FALSE |
| 6401 | except AssertionError: |
| 6402 | main.log.exception( "" ) |
| 6403 | return main.ERROR |
| 6404 | except TypeError: |
| 6405 | main.log.exception( self.name + ": Object not as expected" ) |
| 6406 | return main.ERROR |
| 6407 | except pexpect.EOF: |
| 6408 | main.log.error( self.name + ": EOF exception found" ) |
| 6409 | main.log.error( self.name + ": " + self.handle.before ) |
| 6410 | main.cleanAndExit() |
| 6411 | except Exception: |
| 6412 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6413 | main.cleanAndExit() |
| 6414 | |
| 6415 | def issuReset( self ): |
| 6416 | """ |
| 6417 | Resets the In-Service Software Upgrade status after a rollback |
| 6418 | |
| 6419 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6420 | """ |
| 6421 | try: |
| 6422 | cmdStr = "issu reset" |
| 6423 | handle = self.sendline( cmdStr ) |
| 6424 | assert handle is not None, "Error in sendline" |
| 6425 | assert "Command not found:" not in handle, handle |
| 6426 | assert "Unsupported command:" not in handle, handle |
| 6427 | # TODO: Check the version returned by this command |
| 6428 | if "Reset version" in handle: |
| 6429 | return main.TRUE |
| 6430 | else: |
| 6431 | return main.FALSE |
| 6432 | except AssertionError: |
| 6433 | main.log.exception( "" ) |
| 6434 | return main.ERROR |
| 6435 | except TypeError: |
| 6436 | main.log.exception( self.name + ": Object not as expected" ) |
| 6437 | return main.ERROR |
| 6438 | except pexpect.EOF: |
| 6439 | main.log.error( self.name + ": EOF exception found" ) |
| 6440 | main.log.error( self.name + ": " + self.handle.before ) |
| 6441 | main.cleanAndExit() |
| 6442 | except Exception: |
| 6443 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6444 | main.cleanAndExit() |
| 6445 | |
| 6446 | def issuStatus( self ): |
| 6447 | """ |
| 6448 | Status of an In-Service Software Upgrade |
| 6449 | |
| 6450 | Returns the output of the cli command or None on Error |
| 6451 | """ |
| 6452 | try: |
| 6453 | cmdStr = "issu status" |
| 6454 | handle = self.sendline( cmdStr ) |
| 6455 | assert handle is not None, "Error in sendline" |
| 6456 | assert "Command not found:" not in handle, handle |
| 6457 | assert "Unsupported command:" not in handle, handle |
| 6458 | return handle |
| 6459 | except AssertionError: |
| 6460 | main.log.exception( "" ) |
| 6461 | return None |
| 6462 | except TypeError: |
| 6463 | main.log.exception( self.name + ": Object not as expected" ) |
| 6464 | return None |
| 6465 | except pexpect.EOF: |
| 6466 | main.log.error( self.name + ": EOF exception found" ) |
| 6467 | main.log.error( self.name + ": " + self.handle.before ) |
| 6468 | main.cleanAndExit() |
| 6469 | except Exception: |
| 6470 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6471 | main.cleanAndExit() |
| 6472 | |
| 6473 | def issuVersion( self ): |
| 6474 | """ |
| 6475 | Get the version of an In-Service Software Upgrade |
| 6476 | |
| 6477 | Returns the output of the cli command or None on Error |
| 6478 | """ |
| 6479 | try: |
| 6480 | cmdStr = "issu version" |
| 6481 | handle = self.sendline( cmdStr ) |
| 6482 | assert handle is not None, "Error in sendline" |
| 6483 | assert "Command not found:" not in handle, handle |
| 6484 | assert "Unsupported command:" not in handle, handle |
| 6485 | return handle |
| 6486 | except AssertionError: |
| 6487 | main.log.exception( "" ) |
| 6488 | return None |
| 6489 | except TypeError: |
| 6490 | main.log.exception( self.name + ": Object not as expected" ) |
| 6491 | return None |
| 6492 | except pexpect.EOF: |
| 6493 | main.log.error( self.name + ": EOF exception found" ) |
| 6494 | main.log.error( self.name + ": " + self.handle.before ) |
| 6495 | main.cleanAndExit() |
| 6496 | except Exception: |
| 6497 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6498 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6499 | |
| 6500 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6501 | """ |
| 6502 | Create a multicast route by calling 'mcast-join' command |
| 6503 | sIP: source IP of the multicast route |
| 6504 | groupIP: group IP of the multicast route |
| 6505 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6506 | dPorts: a list of destination ports of the multicast route |
| 6507 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6508 | """ |
| 6509 | try: |
| 6510 | cmdStr = "mcast-join" |
| 6511 | cmdStr += " " + str( sIP ) |
| 6512 | cmdStr += " " + str( groupIP ) |
| 6513 | cmdStr += " " + str( sPort ) |
| 6514 | assert isinstance( dPorts, list ) |
| 6515 | for dPort in dPorts: |
| 6516 | cmdStr += " " + str( dPort ) |
| 6517 | handle = self.sendline( cmdStr ) |
| 6518 | assert handle is not None, "Error in sendline" |
| 6519 | assert "Command not found:" not in handle, handle |
| 6520 | assert "Unsupported command:" not in handle, handle |
| 6521 | assert "Error executing command" not in handle, handle |
| 6522 | if "Added the mcast route" in handle: |
| 6523 | return main.TRUE |
| 6524 | else: |
| 6525 | return main.FALSE |
| 6526 | except AssertionError: |
| 6527 | main.log.exception( "" ) |
| 6528 | return None |
| 6529 | except TypeError: |
| 6530 | main.log.exception( self.name + ": Object not as expected" ) |
| 6531 | return None |
| 6532 | except pexpect.EOF: |
| 6533 | main.log.error( self.name + ": EOF exception found" ) |
| 6534 | main.log.error( self.name + ": " + self.handle.before ) |
| 6535 | main.cleanAndExit() |
| 6536 | except Exception: |
| 6537 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6538 | main.cleanAndExit() |
| 6539 | |
| 6540 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6541 | """ |
| 6542 | Delete a multicast route by calling 'mcast-delete' command |
| 6543 | sIP: source IP of the multicast route |
| 6544 | groupIP: group IP of the multicast route |
| 6545 | dPorts: a list of destination ports of the multicast route |
| 6546 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6547 | """ |
| 6548 | try: |
| 6549 | cmdStr = "mcast-delete" |
| 6550 | cmdStr += " " + str( sIP ) |
| 6551 | cmdStr += " " + str( groupIP ) |
| 6552 | assert isinstance( dPorts, list ) |
| 6553 | for dPort in dPorts: |
| 6554 | cmdStr += " " + str( dPort ) |
| 6555 | handle = self.sendline( cmdStr ) |
| 6556 | assert handle is not None, "Error in sendline" |
| 6557 | assert "Command not found:" not in handle, handle |
| 6558 | assert "Unsupported command:" not in handle, handle |
| 6559 | assert "Error executing command" not in handle, handle |
| 6560 | if "Updated the mcast route" in handle: |
| 6561 | return main.TRUE |
| 6562 | else: |
| 6563 | return main.FALSE |
| 6564 | except AssertionError: |
| 6565 | main.log.exception( "" ) |
| 6566 | return None |
| 6567 | except TypeError: |
| 6568 | main.log.exception( self.name + ": Object not as expected" ) |
| 6569 | return None |
| 6570 | except pexpect.EOF: |
| 6571 | main.log.error( self.name + ": EOF exception found" ) |
| 6572 | main.log.error( self.name + ": " + self.handle.before ) |
| 6573 | main.cleanAndExit() |
| 6574 | except Exception: |
| 6575 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6576 | main.cleanAndExit() |
| 6577 | |
| 6578 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6579 | """ |
| 6580 | Create a multicast route by calling 'mcast-host-join' command |
| 6581 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6582 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6583 | 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] | 6584 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6585 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6586 | """ |
| 6587 | try: |
| 6588 | cmdStr = "mcast-host-join" |
| 6589 | cmdStr += " -sAddr " + str( sAddr ) |
| 6590 | cmdStr += " -gAddr " + str( gAddr ) |
| 6591 | assert isinstance( srcs, list ) |
| 6592 | for src in srcs: |
| 6593 | cmdStr += " -srcs " + str( src ) |
| 6594 | assert isinstance( sinks, list ) |
| 6595 | for sink in sinks: |
| 6596 | cmdStr += " -sinks " + str( sink ) |
| 6597 | handle = self.sendline( cmdStr ) |
| 6598 | assert handle is not None, "Error in sendline" |
| 6599 | assert "Command not found:" not in handle, handle |
| 6600 | assert "Unsupported command:" not in handle, handle |
| 6601 | assert "Error executing command" not in handle, handle |
| 6602 | if "Added the mcast route" in handle: |
| 6603 | return main.TRUE |
| 6604 | else: |
| 6605 | return main.FALSE |
| 6606 | except AssertionError: |
| 6607 | main.log.exception( "" ) |
| 6608 | return None |
| 6609 | except TypeError: |
| 6610 | main.log.exception( self.name + ": Object not as expected" ) |
| 6611 | return None |
| 6612 | except pexpect.EOF: |
| 6613 | main.log.error( self.name + ": EOF exception found" ) |
| 6614 | main.log.error( self.name + ": " + self.handle.before ) |
| 6615 | main.cleanAndExit() |
| 6616 | except Exception: |
| 6617 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6618 | main.cleanAndExit() |
| 6619 | |
| 6620 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6621 | """ |
| 6622 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6623 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6624 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6625 | 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] | 6626 | will delete the route if not specified |
| 6627 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6628 | """ |
| 6629 | try: |
| 6630 | cmdStr = "mcast-host-delete" |
| 6631 | cmdStr += " -sAddr " + str( sAddr ) |
| 6632 | cmdStr += " -gAddr " + str( gAddr ) |
| 6633 | if host: |
| 6634 | cmdStr += " -h " + str( host ) |
| 6635 | handle = self.sendline( cmdStr ) |
| 6636 | assert handle is not None, "Error in sendline" |
| 6637 | assert "Command not found:" not in handle, handle |
| 6638 | assert "Unsupported command:" not in handle, handle |
| 6639 | assert "Error executing command" not in handle, handle |
| 6640 | if "Updated the mcast route" in handle: |
| 6641 | return main.TRUE |
| 6642 | elif "Deleted the mcast route" in handle: |
| 6643 | return main.TRUE |
| 6644 | else: |
| 6645 | return main.FALSE |
| 6646 | except AssertionError: |
| 6647 | main.log.exception( "" ) |
| 6648 | return None |
| 6649 | except TypeError: |
| 6650 | main.log.exception( self.name + ": Object not as expected" ) |
| 6651 | return None |
| 6652 | except pexpect.EOF: |
| 6653 | main.log.error( self.name + ": EOF exception found" ) |
| 6654 | main.log.error( self.name + ": " + self.handle.before ) |
| 6655 | main.cleanAndExit() |
| 6656 | except Exception: |
| 6657 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6658 | main.cleanAndExit() |
| 6659 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6660 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6661 | """ |
| 6662 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6663 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6664 | gAddr: specifies multicast group address |
| 6665 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6666 | will delete the route if not specified |
| 6667 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6668 | """ |
| 6669 | try: |
| 6670 | cmdStr = "mcast-sink-delete" |
| 6671 | cmdStr += " -sAddr " + str( sAddr ) |
| 6672 | cmdStr += " -gAddr " + str( gAddr ) |
| 6673 | if sink: |
| 6674 | cmdStr += " -s " + str( sink ) |
| 6675 | handle = self.sendline( cmdStr ) |
| 6676 | assert handle is not None, "Error in sendline" |
| 6677 | assert "Command not found:" not in handle, handle |
| 6678 | assert "Unsupported command:" not in handle, handle |
| 6679 | assert "Error executing command" not in handle, handle |
| 6680 | if "Updated the mcast route" in handle: |
| 6681 | return main.TRUE |
| 6682 | elif "Deleted the mcast route" in handle: |
| 6683 | return main.TRUE |
| 6684 | else: |
| 6685 | return main.FALSE |
| 6686 | except AssertionError: |
| 6687 | main.log.exception( "" ) |
| 6688 | return None |
| 6689 | except TypeError: |
| 6690 | main.log.exception( self.name + ": Object not as expected" ) |
| 6691 | return None |
| 6692 | except pexpect.EOF: |
| 6693 | main.log.error( self.name + ": EOF exception found" ) |
| 6694 | main.log.error( self.name + ": " + self.handle.before ) |
| 6695 | main.cleanAndExit() |
| 6696 | except Exception: |
| 6697 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6698 | main.cleanAndExit() |
| 6699 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6700 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6701 | """ |
| 6702 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6703 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6704 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6705 | 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] | 6706 | will delete the route if not specified |
| 6707 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6708 | """ |
| 6709 | try: |
| 6710 | cmdStr = "mcast-source-delete" |
| 6711 | cmdStr += " -sAddr " + str( sAddr ) |
| 6712 | cmdStr += " -gAddr " + str( gAddr ) |
| 6713 | if srcs: |
| 6714 | assert isinstance( srcs, list ) |
| 6715 | for src in srcs: |
| 6716 | cmdStr += " -src " + str( src ) |
| 6717 | handle = self.sendline( cmdStr ) |
| 6718 | assert handle is not None, "Error in sendline" |
| 6719 | assert "Command not found:" not in handle, handle |
| 6720 | assert "Unsupported command:" not in handle, handle |
| 6721 | assert "Error executing command" not in handle, handle |
| 6722 | if "Updated the mcast route" in handle: |
| 6723 | return main.TRUE |
| 6724 | elif "Deleted the mcast route" in handle: |
| 6725 | return main.TRUE |
| 6726 | else: |
| 6727 | return main.FALSE |
| 6728 | except AssertionError: |
| 6729 | main.log.exception( "" ) |
| 6730 | return None |
| 6731 | except TypeError: |
| 6732 | main.log.exception( self.name + ": Object not as expected" ) |
| 6733 | return None |
| 6734 | except pexpect.EOF: |
| 6735 | main.log.error( self.name + ": EOF exception found" ) |
| 6736 | main.log.error( self.name + ": " + self.handle.before ) |
| 6737 | main.cleanAndExit() |
| 6738 | except Exception: |
| 6739 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6740 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6741 | |
| 6742 | def netcfg( self, jsonFormat=True, args="" ): |
| 6743 | """ |
| 6744 | Run netcfg cli command with given args |
| 6745 | """ |
| 6746 | try: |
| 6747 | cmdStr = "netcfg" |
| 6748 | if jsonFormat: |
| 6749 | cmdStr = cmdStr + " -j" |
| 6750 | if args: |
| 6751 | cmdStr = cmdStr + " " + str( args ) |
| 6752 | handle = self.sendline( cmdStr ) |
| 6753 | assert handle is not None, "Error in sendline" |
| 6754 | assert "Command not found:" not in handle, handle |
| 6755 | assert "Unsupported command:" not in handle, handle |
| 6756 | assert "Error executing command" not in handle, handle |
| 6757 | return handle |
| 6758 | except AssertionError: |
| 6759 | main.log.exception( "" ) |
| 6760 | return None |
| 6761 | except TypeError: |
| 6762 | main.log.exception( self.name + ": Object not as expected" ) |
| 6763 | return None |
| 6764 | except pexpect.EOF: |
| 6765 | main.log.error( self.name + ": EOF exception found" ) |
| 6766 | main.log.error( self.name + ": " + self.handle.before ) |
| 6767 | main.cleanAndExit() |
| 6768 | except Exception: |
| 6769 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6770 | main.cleanAndExit() |
| 6771 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6772 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6773 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6774 | 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] | 6775 | Options: |
| 6776 | sAddr: IP address of the source host |
| 6777 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6778 | ipv6: True if hosts are IPv6 |
| 6779 | verbose: return verbose t3 output if True |
| 6780 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6781 | """ |
| 6782 | try: |
| 6783 | # Collect information of both hosts from onos |
| 6784 | hosts = self.hosts() |
| 6785 | hosts = json.loads( hosts ) |
| 6786 | sHost = None |
| 6787 | dHost = None |
| 6788 | for host in hosts: |
| 6789 | if sAddr in host[ "ipAddresses" ]: |
| 6790 | sHost = host |
| 6791 | elif dAddr in host[ "ipAddresses" ]: |
| 6792 | dHost = host |
| 6793 | if sHost and dHost: |
| 6794 | break |
| 6795 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6796 | cmdList = [] |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6797 | if simple: |
| 6798 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6799 | cmdStr = "t3-troubleshoot-simple" |
| 6800 | if verbose: |
| 6801 | cmdStr += " -vv" |
| 6802 | if ipv6: |
| 6803 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6804 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6805 | cmdList.append( cmdStr ) |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6806 | else: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6807 | for location in sHost[ "locations" ]: |
| 6808 | cmdStr = "t3-troubleshoot" |
| 6809 | if verbose: |
| 6810 | cmdStr += " -vv" |
| 6811 | if ipv6: |
| 6812 | cmdStr += " -et ipv6" |
| 6813 | cmdStr += " -s " + str( sAddr ) |
| 6814 | cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] ) |
| 6815 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6816 | if sHost[ "vlan" ] != "None": |
| 6817 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6818 | cmdStr += " -d " + str( dAddr ) |
| 6819 | netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) ) |
| 6820 | netcfg = json.loads( netcfg ) |
| 6821 | assert netcfg, "Failed to get netcfg" |
| 6822 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
| 6823 | cmdList.append( cmdStr ) |
| 6824 | return cmdList |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6825 | except AssertionError: |
| 6826 | main.log.exception( "" ) |
| 6827 | return None |
| 6828 | except ( KeyError, TypeError ): |
| 6829 | main.log.exception( self.name + ": Object not as expected" ) |
| 6830 | return None |
| 6831 | except Exception: |
| 6832 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6833 | main.cleanAndExit() |
| 6834 | |
| 6835 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6836 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6837 | 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] | 6838 | Options: |
| 6839 | sAddr: IP address of the source host |
| 6840 | dAddr: IP address of the destination host |
| 6841 | """ |
| 6842 | try: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6843 | cmdList = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6844 | assert cmdList is not None, "composeT3Command returned None" |
| 6845 | t3Output = "" |
| 6846 | for cmdStr in cmdList: |
| 6847 | handle = self.sendline( cmdStr ) |
| 6848 | assert handle is not None, "Error in sendline" |
| 6849 | assert "Command not found:" not in handle, handle |
| 6850 | assert "Unsupported command:" not in handle, handle |
| 6851 | assert "Error executing command" not in handle, handle |
| 6852 | assert "Tracing packet" in handle |
| 6853 | t3Output += handle |
| 6854 | return t3Output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6855 | except AssertionError: |
| 6856 | main.log.exception( "" ) |
| 6857 | return None |
| 6858 | except pexpect.EOF: |
| 6859 | main.log.error( self.name + ": EOF exception found" ) |
| 6860 | main.log.error( self.name + ": " + self.handle.before ) |
| 6861 | main.cleanAndExit() |
| 6862 | except Exception: |
| 6863 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6864 | main.cleanAndExit() |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6865 | |
| 6866 | def prepareForCLI( self, debug=True, maxRetries=120 ): |
| 6867 | """ |
| 6868 | Prepare docker container to connect to onos cli |
| 6869 | """ |
| 6870 | try: |
| 6871 | # Wait for log files to be created |
| 6872 | ready = 0 |
| 6873 | retries = 0 |
| 6874 | while not ready and retries < maxRetries: |
| 6875 | retries += 1 |
| 6876 | self.handle.sendline( "ls -al /root/onos/apache-karaf-*/data/log" ) |
| 6877 | ready = self.handle.expect( [ "No such file or directory", self.dockerPrompt ] ) |
| 6878 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6879 | if not ready: |
| 6880 | self.handle.expect( self.dockerPrompt ) |
| 6881 | time.sleep( 1 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6882 | |
| 6883 | cmdList = [] |
| 6884 | cmdList.append( "apt-get update" ) |
| 6885 | cmdList.append( "apt-get install -y openssh-server" ) |
| 6886 | # Some built in scripts are hardcoded |
| 6887 | cmdList.append( "ln -s /root/onos /opt/onos" ) |
| 6888 | cmdList.append( "ln -s /root/onos/apache-karaf-*/data/log /opt/onos/log" ) |
| 6889 | cmdList.append( "ls -al /opt/onos" ) |
| 6890 | output = "" |
| 6891 | for cmdStr in cmdList: |
| 6892 | self.handle.sendline( cmdStr ) |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 6893 | self.handle.expect( self.dockerPrompt, timeout=420 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6894 | self.handle.sendline( "" ) |
| 6895 | self.handle.expect( self.dockerPrompt ) |
| 6896 | handle = self.handle.before |
| 6897 | assert "command not found" not in handle, handle |
| 6898 | assert "No such file or directory" not in handle, handle |
| 6899 | output += handle |
| 6900 | if debug: |
| 6901 | main.log.debug( "%s: %s" % ( self.name, output ) ) |
| 6902 | return output |
| 6903 | except AssertionError: |
| 6904 | main.log.exception( "" ) |
| 6905 | return None |
| 6906 | except pexpect.EOF: |
| 6907 | main.log.error( self.name + ": EOF exception found" ) |
| 6908 | main.log.error( self.name + ": " + self.handle.before ) |
| 6909 | main.cleanAndExit() |
| 6910 | except Exception: |
| 6911 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6912 | main.cleanAndExit() |
| 6913 | |
| 6914 | def onosSecureSSH( self, userName="onos", userPWD="rocks" ): |
| 6915 | """ |
| 6916 | Enables secure access to ONOS console |
| 6917 | by removing default users & keys. |
| 6918 | |
| 6919 | bin/onos-user-password onos rocks |
| 6920 | |
| 6921 | Returns: main.TRUE on success and main.FALSE on failure |
| 6922 | """ |
| 6923 | |
| 6924 | try: |
| 6925 | self.handle.sendline( "" ) |
| 6926 | self.handle.expect( self.dockerPrompt ) |
| 6927 | |
| 6928 | self.handle.sendline( "[ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q" ) |
| 6929 | self.handle.expect( self.dockerPrompt ) |
| 6930 | main.log.debug( "%s: %s%s" % ( self.name, self.handle.before, self.handle.after ) ) |
| 6931 | |
| 6932 | self.handle.sendline( "bin/onos-user-key $(id -un) $(cut -d\\\\ -f2 ~/.ssh/id_rsa.pub)" ) |
| 6933 | self.handle.expect( pexpect.TIMEOUT, timeout=10 ) |
| 6934 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6935 | |
| 6936 | self.handle.sendline( "bin/onos-user-password %s %s" % ( userName, userPWD ) ) |
| 6937 | i = self.handle.expect( [ "usage", |
| 6938 | self.dockerPrompt, |
| 6939 | pexpect.TIMEOUT ] ) |
| 6940 | if i == 0: |
| 6941 | # malformed command |
| 6942 | main.log.warn( self.name + ": Could not parse onos-user-password command" ) |
| 6943 | self.handle.expect( self.dockerPrompt ) |
| 6944 | return main.FALSE |
| 6945 | elif i == 1: |
| 6946 | # Process started |
| 6947 | main.log.info( self.name + ": SSH password added for user " + userName ) |
| 6948 | return main.TRUE |
| 6949 | elif i == 2: |
| 6950 | # timeout |
| 6951 | main.log.error( self.name + ": Failed to secure onos ssh " ) |
| 6952 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6953 | except pexpect.EOF: |
| 6954 | main.log.error( self.name + ": EOF exception found" ) |
| 6955 | main.log.error( self.name + ": " + self.handle.before ) |
| 6956 | main.cleanAndExit() |
| 6957 | except Exception: |
| 6958 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6959 | main.cleanAndExit() |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 6960 | |
| 6961 | def routeAdd( self, prefix, nextHop, jsonFormat=True ): |
| 6962 | """ |
| 6963 | Add a route with the given prefix and nextHop |
| 6964 | """ |
| 6965 | try: |
| 6966 | cmdStr = "route-add %s %s %s" % ( "-j" if jsonFormat else "", prefix, nextHop ) |
| 6967 | handle = self.sendline( cmdStr ) |
| 6968 | assert handle is not None, "Error in sendline" |
| 6969 | assert "Command not found:" not in handle, handle |
| 6970 | assert "Unsupported command:" not in handle, handle |
| 6971 | assert "Error executing command" not in handle, handle |
| 6972 | return handle |
| 6973 | except AssertionError: |
| 6974 | main.log.exception( "" ) |
| 6975 | return None |
| 6976 | except TypeError: |
| 6977 | main.log.exception( self.name + ": Object not as expected" ) |
| 6978 | return None |
| 6979 | except pexpect.EOF: |
| 6980 | main.log.error( self.name + ": EOF exception found" ) |
| 6981 | main.log.error( self.name + ": " + self.handle.before ) |
| 6982 | main.cleanAndExit() |
| 6983 | except Exception: |
| 6984 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6985 | main.cleanAndExit() |
| 6986 | |
| 6987 | def routeRemove( self, prefix, nextHop, source=None, jsonFormat=True ): |
| 6988 | """ |
| 6989 | Remove a route with the given prefix and nextHop |
| 6990 | """ |
| 6991 | try: |
| 6992 | if source: |
| 6993 | raise NotImplemented |
| 6994 | cmdStr = "route-remove %s %s %s" % ( "-j" if jsonFormat else "", prefix, nextHop ) |
| 6995 | handle = self.sendline( cmdStr ) |
| 6996 | assert handle is not None, "Error in sendline" |
| 6997 | assert "Command not found:" not in handle, handle |
| 6998 | assert "Unsupported command:" not in handle, handle |
| 6999 | assert "Error executing command" not in handle, handle |
| 7000 | return handle |
| 7001 | except AssertionError: |
| 7002 | main.log.exception( "" ) |
| 7003 | return None |
| 7004 | except TypeError: |
| 7005 | main.log.exception( self.name + ": Object not as expected" ) |
| 7006 | return None |
| 7007 | except pexpect.EOF: |
| 7008 | main.log.error( self.name + ": EOF exception found" ) |
| 7009 | main.log.error( self.name + ": " + self.handle.before ) |
| 7010 | main.cleanAndExit() |
| 7011 | except Exception: |
| 7012 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 7013 | main.cleanAndExit() |
| 7014 | |
| 7015 | def routes( self, jsonFormat=True, args="" ): |
| 7016 | """ |
| 7017 | Get routes from ONOS |
| 7018 | """ |
| 7019 | try: |
| 7020 | cmdStr = "routes" |
| 7021 | if jsonFormat: |
| 7022 | cmdStr = cmdStr + " -j" |
| 7023 | handle = self.sendline( cmdStr ) |
| 7024 | assert handle is not None, "Error in sendline" |
| 7025 | assert "Command not found:" not in handle, handle |
| 7026 | assert "Unsupported command:" not in handle, handle |
| 7027 | assert "Error executing command" not in handle, handle |
| 7028 | return handle |
| 7029 | except AssertionError: |
| 7030 | main.log.exception( "" ) |
| 7031 | return None |
| 7032 | except TypeError: |
| 7033 | main.log.exception( self.name + ": Object not as expected" ) |
| 7034 | return None |
| 7035 | except pexpect.EOF: |
| 7036 | main.log.error( self.name + ": EOF exception found" ) |
| 7037 | main.log.error( self.name + ": " + self.handle.before ) |
| 7038 | main.cleanAndExit() |
| 7039 | except Exception: |
| 7040 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 7041 | main.cleanAndExit() |