andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | OCT 13 2014 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5 | Copyright 2014 Open Networking Foundation (ONF) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 6 | |
| 7 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 8 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 9 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 10 | |
| 11 | TestON is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 14 | (at your option) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 15 | |
| 16 | TestON is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 23 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 25 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 26 | This driver enters the onos> prompt to issue commands. |
| 27 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 28 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 29 | functions and document properly. |
| 30 | |
| 31 | If you are a contributor to the driver, please |
| 32 | list your email here for future contact: |
| 33 | |
| 34 | jhall@onlab.us |
| 35 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 36 | shreya@onlab.us |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 37 | jeremyr@opennetworking.org |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 38 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 39 | import pexpect |
| 40 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 41 | import json |
| 42 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 43 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 44 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 45 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 46 | from core.graph import Graph |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 47 | from cStringIO import StringIO |
| 48 | from itertools import izip |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 49 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 51 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | def __init__( self ): |
| 53 | """ |
| 54 | Initialize client |
| 55 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 56 | self.name = None |
| 57 | self.home = None |
| 58 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 59 | self.karafUser = None |
| 60 | self.karafPass = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 61 | self.karafPort = None |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 62 | self.karafTimeout = None |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 63 | self.address = None |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 64 | self.karafPromptUser = None |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 65 | self.dockerPrompt = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 66 | self.graph = Graph() |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 67 | super( OnosCliDriver, self ).__init__() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 68 | |
| 69 | def connect( self, **connectargs ): |
| 70 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 71 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 72 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 73 | try: |
| 74 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 75 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 76 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 77 | for key in self.options: |
| 78 | if key == "home": |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 79 | self.home = self.options[ key ] |
| 80 | elif key == "karaf_username": |
| 81 | self.karafUser = self.options[ key ] |
| 82 | elif key == "karaf_password": |
| 83 | self.karafPass = self.options[ key ] |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 84 | elif key == "docker_prompt": |
| 85 | self.dockerPrompt = self.options[ key ] |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 86 | elif key == "karaf_timeout": |
| 87 | self.karafTimeout = self.options[ key ] |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 88 | elif key == "karaf_port": |
| 89 | self.karafPort = self.options[ key ] |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 90 | elif key == "karafPromptUser": |
| 91 | self.karafPromptUser = self.options[ key ] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 92 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 93 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 94 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 95 | self.karafPort = self.checkOptions( self.karafPort, 8101 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 96 | self.dockerPrompt = self.checkOptions( self.dockerPrompt, "~/onos#" ) |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 97 | self.karafTimeout = self.checkOptions( self.karafTimeout, 7200000 ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 98 | |
Siddesh | 5275062 | 2021-03-05 19:52:03 +0000 | [diff] [blame] | 99 | self.karafPrompt = self.karafPromptUser + "@root >" |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 100 | for key in self.options: |
| 101 | if key == 'onosIp': |
| 102 | self.onosIp = self.options[ 'onosIp' ] |
| 103 | break |
| 104 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 105 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 106 | |
| 107 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 108 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 109 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 110 | else: |
| 111 | main.log.info( self.name + |
| 112 | ": Trying to connect to " + |
| 113 | self.ip_address ) |
| 114 | |
| 115 | except KeyError: |
| 116 | main.log.info( "Invalid host name," + |
| 117 | " connecting to local host instead" ) |
| 118 | self.ip_address = 'localhost' |
| 119 | except Exception as inst: |
| 120 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 121 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 122 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 123 | user_name=self.user_name, |
| 124 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 125 | port=self.port, |
| 126 | pwd=self.pwd, |
| 127 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 128 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 129 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 130 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 131 | if self.handle: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 132 | self.address = self.ip_address |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 133 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 134 | else: |
| 135 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 136 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 137 | except TypeError: |
| 138 | main.log.exception( self.name + ": Object not as expected" ) |
| 139 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 140 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 141 | main.log.error( self.name + ": EOF exception found" ) |
| 142 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 143 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 144 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 145 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 146 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 147 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 148 | def disconnect( self ): |
| 149 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 150 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 151 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 152 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 153 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 154 | if self.handle: |
| 155 | i = self.logout() |
| 156 | if i == main.TRUE: |
| 157 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 158 | for l in range( 3 ): |
| 159 | p = self.handle.expect( [ self.prompt, self.dockerPrompt ] ) |
| 160 | if p == 1: |
| 161 | self.inDocker = False |
| 162 | self.handle.sendline( "exit" ) |
| 163 | j = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=3 ) |
| 164 | if j == 0: |
| 165 | return response |
| 166 | response = main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 167 | except TypeError: |
| 168 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 169 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 170 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 171 | main.log.error( self.name + ": EOF exception found" ) |
| 172 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 173 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 174 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 175 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 176 | except Exception: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 177 | main.log.exception( self.name + ": disconnection failed from the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 178 | response = main.FALSE |
| 179 | return response |
| 180 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 181 | def logout( self ): |
| 182 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 183 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 184 | Returns main.TRUE if exited CLI and |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 185 | main.FALSE on timeout (not guranteed you are disconnected) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 186 | None on TypeError |
| 187 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 188 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 189 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 190 | if self.handle: |
| 191 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 192 | i = self.handle.expect( [ self.karafPrompt, |
| 193 | self.Prompt(), |
| 194 | pexpect.TIMEOUT ], |
| 195 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 196 | timeout=10 ) |
| 197 | if i == 0: # In ONOS CLI |
| 198 | self.handle.sendline( "logout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 199 | j = self.handle.expect( [ self.Prompt(), |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 200 | "Command not found:", |
| 201 | pexpect.TIMEOUT ] ) |
| 202 | if j == 0: # Successfully logged out |
| 203 | return main.TRUE |
| 204 | elif j == 1 or j == 2: |
| 205 | # ONOS didn't fully load, and logout command isn't working |
| 206 | # or the command timed out |
| 207 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 208 | try: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 209 | self.handle.expect( self.Prompt() ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 210 | except pexpect.TIMEOUT: |
| 211 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 212 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 213 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 214 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 215 | repr( self.handle.before ) ) |
| 216 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 217 | elif i == 1: # not in CLI |
| 218 | return main.TRUE |
steven30801 | e42f1fb | 2019-01-17 11:31:45 +0800 | [diff] [blame] | 219 | elif i == 2: # Timeout |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 220 | return main.FALSE |
| 221 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 222 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 223 | except TypeError: |
| 224 | main.log.exception( self.name + ": Object not as expected" ) |
| 225 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 226 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 227 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 228 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 229 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 230 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 231 | main.log.error( self.name + |
| 232 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 233 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 234 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 235 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 236 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 237 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 238 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 239 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 240 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 241 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 242 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 243 | try: |
| 244 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 245 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 246 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 247 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 248 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 249 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 250 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 251 | # and that this driver will have to change accordingly |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 252 | self.handle.expect( str( cellname ) ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 253 | handleBefore = self.handle.before |
| 254 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 255 | # Get the rest of the handle |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 256 | self.handle.sendline( "" ) |
| 257 | self.handle.expect( self.prompt ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 258 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 259 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 260 | main.log.info( "Cell call returned: " + handleBefore + |
| 261 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 262 | |
| 263 | return main.TRUE |
| 264 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 265 | except TypeError: |
| 266 | main.log.exception( self.name + ": Object not as expected" ) |
| 267 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 268 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 269 | main.log.error( self.name + ": eof exception found" ) |
| 270 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 271 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 272 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 273 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 274 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 275 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 276 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 277 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 278 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 279 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 280 | by user would be used to set the current karaf shell idle timeout. |
| 281 | Note that when ever this property is modified the shell will exit and |
Hari Krishna | d7b9c20 | 2015-01-05 10:38:14 -0800 | [diff] [blame] | 282 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 283 | Below is an example to start a session with 60 seconds idle timeout |
| 284 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 285 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 286 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 287 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 288 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 289 | Note: karafTimeout is left as str so that this could be read |
| 290 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 291 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 292 | self.onosIp = ONOSIp |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 293 | self.address = self.onosIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 294 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 295 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 296 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 297 | x = self.handle.expect( [ self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 298 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 299 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 300 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 301 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 302 | # Not in CLI so login |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 303 | if self.inDocker: |
| 304 | # The Docker does not have all the wrapper scripts |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 305 | startCliCommand = "ssh -p %s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null %s@localhost" % ( self.karafPort, self.karafUser ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 306 | elif waitForStart: |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 307 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 308 | startCliCommand = "onos-wait-for-start " + str( ONOSIp ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 309 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 310 | startCliCommand = "onos " + str( ONOSIp ) |
| 311 | self.handle.sendline( startCliCommand ) |
| 312 | tries = 0 |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 313 | timeoutSet = False |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 314 | while tries < 5: |
| 315 | i = self.handle.expect( [ |
| 316 | self.karafPrompt, |
| 317 | "Password:", |
| 318 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 319 | |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 320 | if i == 0: |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 321 | if not karafTimeout or timeoutSet: |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 322 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
| 323 | return main.TRUE |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 324 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 325 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 326 | "config:property-set -p org.apache.karaf.shell\ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 327 | sshIdleTimeout " + |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 328 | str( karafTimeout ) ) |
| 329 | self.handle.expect( "closed by remote host" ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 330 | self.handle.expect( self.Prompt() ) |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 331 | self.handle.sendline( startCliCommand ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 332 | timeoutSet = True |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 333 | elif i == 1: |
| 334 | main.log.info( str( ONOSIp ) + " CLI asking for password" ) |
| 335 | main.log.debug( "Sending %s" % self.karafPass ) |
| 336 | self.handle.sendline( self.karafPass ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 337 | else: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 338 | # If failed, send ctrl+c to process and try again |
| 339 | main.log.info( "Starting CLI failed. Retrying..." ) |
Jon Hall | f69e316 | 2020-09-01 09:08:44 -0700 | [diff] [blame] | 340 | time.sleep( 5 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 341 | self.handle.send( "\x03" ) |
| 342 | self.handle.sendline( startCliCommand ) |
| 343 | tries += 1 |
| 344 | main.log.error( "Connection to CLI " + str( ONOSIp ) + " timeout" ) |
| 345 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 346 | except TypeError: |
| 347 | main.log.exception( self.name + ": Object not as expected" ) |
| 348 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 349 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 350 | main.log.error( self.name + ": EOF exception found" ) |
| 351 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 352 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 353 | except Exception: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 354 | main.log.debug( self.handle.before + str( self.handle.after ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 355 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 356 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 357 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 358 | def startCellCli( self, karafTimeout="", |
| 359 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 360 | """ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 361 | Start CLI on onos cell handle. |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 362 | |
| 363 | karafTimeout is an optional argument. karafTimeout value passed |
| 364 | by user would be used to set the current karaf shell idle timeout. |
| 365 | Note that when ever this property is modified the shell will exit and |
| 366 | the subsequent login would reflect new idle timeout. |
| 367 | Below is an example to start a session with 60 seconds idle timeout |
| 368 | ( input value is in milliseconds ): |
| 369 | |
| 370 | tValue = "60000" |
| 371 | |
| 372 | Note: karafTimeout is left as str so that this could be read |
| 373 | and passed to startOnosCli from PARAMS file as str. |
| 374 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 375 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 376 | try: |
| 377 | self.handle.sendline( "" ) |
| 378 | x = self.handle.expect( [ |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 379 | self.Prompt(), self.karafPrompt ], commandlineTimeout ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 380 | |
| 381 | if x == 1: |
| 382 | main.log.info( "ONOS cli is already running" ) |
| 383 | return main.TRUE |
| 384 | |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 385 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 386 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 387 | i = self.handle.expect( [ |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 388 | self.karafPrompt, |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 389 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 390 | |
| 391 | if i == 0: |
| 392 | main.log.info( self.name + " CLI Started successfully" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 393 | if karafTimeout: # FIXME: This doesn't look right |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 394 | self.handle.sendline( |
| 395 | "config:property-set -p org.apache.karaf.shell\ |
| 396 | sshIdleTimeout " + |
| 397 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 398 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 399 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 400 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 401 | return main.TRUE |
| 402 | else: |
| 403 | # If failed, send ctrl+c to process and try again |
| 404 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 405 | self.handle.send( "\x03" ) |
| 406 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 407 | i = self.handle.expect( [ self.karafPrompt, pexpect.TIMEOUT ], |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 408 | timeout=30 ) |
| 409 | if i == 0: |
| 410 | main.log.info( self.name + " CLI Started " + |
| 411 | "successfully after retry attempt" ) |
| 412 | if karafTimeout: |
| 413 | self.handle.sendline( |
| 414 | "config:property-set -p org.apache.karaf.shell\ |
| 415 | sshIdleTimeout " + |
| 416 | karafTimeout ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 417 | self.handle.expect( self.Prompt() ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 418 | self.handle.sendline( "/opt/onos/bin/onos" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 419 | self.handle.expect( self.karafPrompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 420 | return main.TRUE |
| 421 | else: |
| 422 | main.log.error( "Connection to CLI " + |
| 423 | self.name + " timeout" ) |
| 424 | return main.FALSE |
| 425 | |
| 426 | except TypeError: |
| 427 | main.log.exception( self.name + ": Object not as expected" ) |
| 428 | return None |
| 429 | except pexpect.EOF: |
| 430 | main.log.error( self.name + ": EOF exception found" ) |
| 431 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 432 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 433 | except Exception: |
| 434 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 435 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 436 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 437 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 438 | """ |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 439 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 440 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 441 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 442 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 443 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 444 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 445 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 446 | """ |
| 447 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 448 | lvlStr = "" |
| 449 | if level: |
| 450 | lvlStr = "--level=" + level |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 451 | handle = self.sendline( "log:log " + lvlStr + " " + cmdStr, noExit=noExit ) |
| 452 | assert handle is not None, "Error in sendline" |
| 453 | assert "Command not found:" not in handle, handle |
| 454 | if re.search( "Error", handle ): |
| 455 | main.log.error( self.name + ": Error in logging message" ) |
| 456 | main.log.error( handle ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 457 | return main.FALSE |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 458 | else: |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 459 | return main.TRUE |
| 460 | except AssertionError: |
| 461 | main.log.exception( "" ) |
| 462 | return None |
| 463 | except TypeError: |
| 464 | main.log.exception( self.name + ": Object not as expected" ) |
| 465 | return None |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 466 | except pexpect.EOF: |
| 467 | main.log.error( self.name + ": EOF exception found" ) |
| 468 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 469 | if noExit: |
| 470 | main.cleanup() |
| 471 | return None |
| 472 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 473 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 474 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 475 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 476 | if noExit: |
| 477 | main.cleanup() |
| 478 | return None |
| 479 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 480 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 481 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 482 | def clearBuffer( self, debug=False, timeout=10, noExit=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 483 | """ |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 484 | Test cli connection and clear any left over output in the buffer |
| 485 | Optional Arguments: |
| 486 | debug - Defaults to False. If True, will enable debug logging. |
| 487 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 488 | before a timeout. |
| 489 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 490 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 491 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 492 | # Try to reconnect if disconnected from cli |
| 493 | self.handle.sendline( "" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 494 | i = self.handle.expect( [ self.karafPrompt, |
| 495 | self.Prompt(), |
| 496 | pexpect.TIMEOUT ] ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 497 | response = self.handle.before |
Jon Hall | a16b4db | 2021-10-20 14:11:59 -0700 | [diff] [blame^] | 498 | if i == 1: # Not in ONOS CLI |
| 499 | # FIXME: This isn't really the correct place for this, but it works for now |
| 500 | # Check if port-forward session is still up first |
| 501 | if hasattr( main, "Cluster"): |
| 502 | ctrl = None |
| 503 | for c in main.Cluster.controllers: |
| 504 | if c.CLI is self: |
| 505 | ctrl = c |
| 506 | break |
| 507 | if not ctrl: |
| 508 | main.log.warn( self.name + ": Could not find this node in Cluster. Can't check port-forward status" ) |
| 509 | elif ctrl.k8s: |
| 510 | ctrl.k8s.checkPortForward( ctrl.k8s.podName, |
| 511 | kubeconfig=ctrl.k8s.kubeConfig, |
| 512 | namespace=main.params[ 'kubernetes' ][ 'namespace' ] ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 513 | main.log.error( self.name + ": onos cli session closed. " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 514 | if self.onosIp: |
| 515 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 516 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 517 | if reconnectResult: |
| 518 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 519 | else: |
| 520 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 521 | if noExit: |
| 522 | return None |
| 523 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 524 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 525 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 526 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 527 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 528 | main.log.warn( "Timeout when testing cli responsiveness" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 529 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 530 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 531 | self.handle.expect( self.karafPrompt ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 532 | |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 533 | response += self.handle.before |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 534 | if debug: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 535 | main.log.debug( self.name + ": Raw output from sending ''" ) |
| 536 | main.log.debug( self.name + ": " + repr( response ) ) |
| 537 | except pexpect.TIMEOUT: |
| 538 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 539 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 141b43b | 2018-06-26 16:50:18 -0700 | [diff] [blame] | 540 | self.handle.send( "\x03" ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 541 | self.handle.expect( self.karafPrompt ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 542 | return None |
| 543 | except pexpect.EOF: |
| 544 | main.log.error( self.name + ": EOF exception found" ) |
| 545 | main.log.error( self.name + ": " + self.handle.before ) |
| 546 | if noExit: |
| 547 | return None |
| 548 | else: |
| 549 | main.cleanAndExit() |
| 550 | except Exception: |
| 551 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 552 | if noExit: |
| 553 | return None |
| 554 | else: |
| 555 | main.cleanAndExit() |
| 556 | |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 557 | 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] | 558 | """ |
| 559 | A wrapper around pexpect's sendline/expect. Will return all the output from a given command |
| 560 | |
| 561 | Required Arguments: |
| 562 | cmdStr - String to send to the pexpect session |
| 563 | |
| 564 | Optional Arguments: |
| 565 | showResponse - Defaults to False. If True will log the response. |
| 566 | debug - Defaults to False. If True, will enable debug logging. |
| 567 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 568 | before a timeout. |
| 569 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 570 | closed channel, but instead return None |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 571 | 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] | 572 | |
| 573 | Warning: There are no sanity checking to commands sent using this method. |
| 574 | |
| 575 | """ |
| 576 | try: |
| 577 | # Try to reconnect if disconnected from cli |
| 578 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
| 579 | if debug: |
| 580 | # NOTE: This adds an average of .4 seconds per call |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 581 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 582 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 583 | self.handle.sendline( cmdStr ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 584 | self.handle.expect( self.karafPrompt, timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 585 | response = self.handle.before |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 586 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 587 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 588 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 589 | main.log.debug( self.name + ": Raw output" ) |
| 590 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 591 | |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 592 | response = self.cleanOutput( response, debug=debug ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 593 | # Remove control codes from karaf 4.2.1 |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 594 | 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] | 595 | response = karafEscape.sub( '', response ) |
| 596 | if debug: |
| 597 | main.log.debug( self.name + ": karafEscape output" ) |
| 598 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 599 | # Remove ANSI color control strings from output |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 600 | response = self.cleanOutput( response, debug ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 601 | |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 602 | # Remove ANSI color control strings from output |
Jon Hall | cf31d0f | 2018-12-13 11:18:48 -0800 | [diff] [blame] | 603 | # NOTE: karaf is sometimes adding a single character then two |
| 604 | # backspaces and sometimes adding 2 characters with 2 backspaces?? |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 605 | backspaceEscape = re.compile( r'((.|\s)\x08)' ) |
| 606 | unchanged = False |
| 607 | while not unchanged: |
| 608 | old = response |
| 609 | response = backspaceEscape.sub( '', response, count=1 ) |
| 610 | if debug: |
| 611 | main.log.debug( self.name + ": backspaceEscape output" ) |
| 612 | main.log.debug( self.name + ": " + repr( response ) ) |
| 613 | unchanged = old == response |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 614 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 615 | # Remove extra return chars that get added |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 616 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 617 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 618 | main.log.debug( self.name + ": Removed extra returns " + |
| 619 | "from output" ) |
| 620 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 621 | |
| 622 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 623 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 624 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 625 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 626 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 627 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 628 | # parse for just the output, remove the cmd from response |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 629 | cmdPattern = cmdStr.strip() |
| 630 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 631 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 632 | if relaxedRegex: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 633 | cmdPattern = cmdPattern.split( '|' )[ -1 ].strip() |
| 634 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 635 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 636 | # 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] | 637 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 638 | if expectJson: |
| 639 | main.log.warn( "Relaxed Regex: Searching for a json string amongst the output" ) |
| 640 | jsonPattern = r'\{.*\}' |
| 641 | match = re.search( jsonPattern, output[ 0 ] ) |
| 642 | if match: |
| 643 | 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] | 644 | if len( output ) < 2: |
| 645 | 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] | 646 | cmdPattern = cmdPattern[ -5: ] |
| 647 | main.log.debug( "REGEX PATTERN SET TO: " + repr( cmdPattern ) + |
| 648 | "\nSENT COMMAND STRING WAS: " + repr( cmdStr ) ) |
| 649 | output = response.split( cmdPattern, 1 ) |
Jon Hall | 6c9e2da | 2018-11-06 12:01:23 -0800 | [diff] [blame] | 650 | else: |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 651 | output = response.split( cmdPattern, 1 ) |
| 652 | 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] | 653 | 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] | 654 | output = response.split( cmdPattern[ -5: ], 1 ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 655 | if output: |
| 656 | if debug: |
| 657 | main.log.debug( self.name + ": split output" ) |
| 658 | for r in output: |
| 659 | main.log.debug( self.name + ": " + repr( r ) ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 660 | if len( output ) == 1: |
| 661 | main.log.error( "Could not remove sent command echo from output" ) |
| 662 | return output |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 663 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 664 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 665 | main.log.info( "Response from ONOS: {}".format( output ) ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 666 | self.clearBuffer( debug=debug, timeout=timeout, noExit=noExit ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 667 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 668 | except pexpect.TIMEOUT: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 669 | main.log.error( self.name + ": ONOS timeout" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 670 | if debug: |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 671 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
You Wang | 6b5e5ba | 2019-01-31 15:32:40 -0800 | [diff] [blame] | 672 | self.exitFromCmd( self.karafPrompt, 100 ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 673 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 674 | except IndexError: |
| 675 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 676 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 677 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 678 | except TypeError: |
| 679 | main.log.exception( self.name + ": Object not as expected" ) |
| 680 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 681 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 682 | main.log.error( self.name + ": EOF exception found" ) |
| 683 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 684 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 685 | return None |
| 686 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 687 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 688 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 689 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 690 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 691 | return None |
| 692 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 693 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 694 | |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 695 | def lineCount( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, relaxedRegex=True ): |
| 696 | """ |
| 697 | A wrapper around sendline(). Will return the number of lines returned or None on error |
| 698 | |
| 699 | Required Arguments: |
| 700 | cmdStr - String to send to the pexpect session |
| 701 | |
| 702 | Optional Arguments: |
| 703 | showResponse - Defaults to False. If True will log the response. |
| 704 | debug - Defaults to False. If True, will enable debug logging. |
| 705 | timeout - Defaults to 10. Amount of time in seconds for a command to return |
| 706 | before a timeout. |
| 707 | noExit - Defaults to False. If True, will not exit TestON in the event of a |
| 708 | closed channel, but instead return None |
| 709 | 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. |
| 710 | |
| 711 | Warning: There are no sanity checking to commands sent using this method. |
| 712 | |
| 713 | """ |
| 714 | try: |
| 715 | numLines = self.sendline( cmdStr, showResponse, debug, timeout, noExit, relaxedRegex ) |
You Wang | 0ce8e0c | 2019-02-22 12:22:26 -0800 | [diff] [blame] | 716 | parsed = re.search( "(\d+)", numLines ) |
Jon Hall | 8c9dd1c | 2018-11-14 15:40:39 -0800 | [diff] [blame] | 717 | if not parsed: |
| 718 | main.log.error( "Warning, output of karaf's wc may have changed" ) |
| 719 | return None |
| 720 | return parsed.group( 1 ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 721 | except IndexError: |
| 722 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | ce0d70b | 2018-12-11 11:01:32 -0800 | [diff] [blame] | 723 | main.log.debug( "response: {}".format( repr( numLines ) ) ) |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 724 | return None |
| 725 | except TypeError: |
| 726 | main.log.exception( self.name + ": Object not as expected" ) |
| 727 | return None |
| 728 | except Exception: |
| 729 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 730 | if noExit: |
| 731 | return None |
| 732 | else: |
| 733 | main.cleanAndExit() |
| 734 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 735 | # IMPORTANT NOTE: |
| 736 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 737 | # the cli command changing 'a:b' with 'aB'. |
| 738 | # Ex ) onos:topology > onosTopology |
| 739 | # onos:links > onosLinks |
| 740 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 741 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 742 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 743 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 744 | Adds a new cluster node by ID and address information. |
| 745 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 746 | * nodeId |
| 747 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 748 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 749 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 750 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 751 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 752 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 753 | str( ONOSIp ) + " " + str( tcpPort ) |
| 754 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 755 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 756 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 757 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 758 | main.log.error( self.name + ": Error in adding node" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 759 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 760 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 761 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 762 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 763 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 764 | except AssertionError: |
| 765 | main.log.exception( "" ) |
| 766 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 767 | except TypeError: |
| 768 | main.log.exception( self.name + ": Object not as expected" ) |
| 769 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 770 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 771 | main.log.error( self.name + ": EOF exception found" ) |
| 772 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 773 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 774 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 775 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 776 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 777 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 778 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 779 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 780 | Removes a cluster by ID |
| 781 | Issues command: 'remove-node [<node-id>]' |
| 782 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 783 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 784 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 785 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 786 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 787 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 788 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 789 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 790 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 791 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 792 | main.log.error( self.name + ": Error in removing node" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 793 | main.log.error( handle ) |
| 794 | return main.FALSE |
| 795 | else: |
| 796 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 797 | except AssertionError: |
| 798 | main.log.exception( "" ) |
| 799 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 800 | except TypeError: |
| 801 | main.log.exception( self.name + ": Object not as expected" ) |
| 802 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 803 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 804 | main.log.error( self.name + ": EOF exception found" ) |
| 805 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 806 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 807 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 808 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 809 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 810 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 811 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 812 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 813 | List the nodes currently visible |
| 814 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 815 | Optional argument: |
| 816 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 817 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 818 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 819 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 820 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 821 | cmdStr += " -j" |
| 822 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 823 | assert output is not None, "Error in sendline" |
Jon Hall | e37bd1f | 2020-09-10 12:16:41 -0700 | [diff] [blame] | 824 | # "Command not found" or "Service org.onosproject.security.AuditService not found" |
| 825 | assert "not found" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 826 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 827 | except AssertionError: |
| 828 | main.log.exception( "" ) |
| 829 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 830 | except TypeError: |
| 831 | main.log.exception( self.name + ": Object not as expected" ) |
| 832 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 833 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 834 | main.log.error( self.name + ": EOF exception found" ) |
| 835 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 836 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 837 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 838 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 839 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 840 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 841 | def topology( self ): |
| 842 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 843 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 844 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 845 | Return: |
| 846 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 847 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 848 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 849 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 850 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 851 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 852 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 853 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 854 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 855 | except AssertionError: |
| 856 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 857 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 858 | except TypeError: |
| 859 | main.log.exception( self.name + ": Object not as expected" ) |
| 860 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 861 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 862 | main.log.error( self.name + ": EOF exception found" ) |
| 863 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 864 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 865 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 866 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 867 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 868 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 869 | def deviceRemove( self, deviceId ): |
| 870 | """ |
| 871 | Removes particular device from storage |
| 872 | |
| 873 | TODO: refactor this function |
| 874 | """ |
| 875 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 876 | cmdStr = "device-remove " + str( deviceId ) |
| 877 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 878 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 879 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 880 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 881 | main.log.error( self.name + ": Error in removing device" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 882 | main.log.error( handle ) |
| 883 | return main.FALSE |
| 884 | else: |
| 885 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 886 | except AssertionError: |
| 887 | main.log.exception( "" ) |
| 888 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 889 | except TypeError: |
| 890 | main.log.exception( self.name + ": Object not as expected" ) |
| 891 | return None |
| 892 | except pexpect.EOF: |
| 893 | main.log.error( self.name + ": EOF exception found" ) |
| 894 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 895 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 896 | except Exception: |
| 897 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 898 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 899 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 900 | def devices( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 901 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 902 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 903 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 904 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 905 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 906 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 907 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 908 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 909 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 910 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 911 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 912 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 913 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 914 | except AssertionError: |
| 915 | main.log.exception( "" ) |
| 916 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 917 | except TypeError: |
| 918 | main.log.exception( self.name + ": Object not as expected" ) |
| 919 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 920 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 921 | main.log.error( self.name + ": EOF exception found" ) |
| 922 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 923 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 924 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 925 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 926 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 927 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 928 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 929 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 930 | This balances the devices across all controllers |
| 931 | by issuing command: 'onos> onos:balance-masters' |
| 932 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 933 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 934 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 935 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 936 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 937 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 938 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 939 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 940 | main.log.error( self.name + ": Error in balancing masters" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 941 | main.log.error( handle ) |
| 942 | return main.FALSE |
| 943 | else: |
| 944 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 945 | except AssertionError: |
| 946 | main.log.exception( "" ) |
| 947 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 948 | except TypeError: |
| 949 | main.log.exception( self.name + ": Object not as expected" ) |
| 950 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 951 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 952 | main.log.error( self.name + ": EOF exception found" ) |
| 953 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 954 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 955 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 956 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 957 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 958 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 959 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 960 | """ |
| 961 | Returns the output of the masters command. |
| 962 | Optional argument: |
| 963 | * jsonFormat - boolean indicating if you want output in json |
| 964 | """ |
| 965 | try: |
| 966 | cmdStr = "onos:masters" |
| 967 | if jsonFormat: |
| 968 | cmdStr += " -j" |
| 969 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 970 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 971 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 972 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 973 | except AssertionError: |
| 974 | main.log.exception( "" ) |
| 975 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 976 | except TypeError: |
| 977 | main.log.exception( self.name + ": Object not as expected" ) |
| 978 | return None |
| 979 | except pexpect.EOF: |
| 980 | main.log.error( self.name + ": EOF exception found" ) |
| 981 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 982 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 983 | except Exception: |
| 984 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 985 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 986 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 987 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 988 | """ |
| 989 | Uses the master command to check that the devices' leadership |
| 990 | is evenly divided |
| 991 | |
| 992 | Dependencies: checkMasters() and summary() |
| 993 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 994 | Returns main.TRUE if the devices are balanced |
| 995 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 996 | Exits on Exception |
| 997 | Returns None on TypeError |
| 998 | """ |
| 999 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1000 | summaryOutput = self.summary() |
| 1001 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 1002 | except ( TypeError, ValueError ): |
| 1003 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 1004 | return None |
| 1005 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1006 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1007 | mastersOutput = self.checkMasters() |
| 1008 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1009 | first = masters[ 0 ][ "size" ] |
| 1010 | for master in masters: |
| 1011 | totalOwnedDevices += master[ "size" ] |
| 1012 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 1013 | main.log.error( "Mastership not balanced" ) |
| 1014 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 1015 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 1016 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1017 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1018 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1019 | except ( TypeError, ValueError ): |
| 1020 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1021 | return None |
| 1022 | except pexpect.EOF: |
| 1023 | main.log.error( self.name + ": EOF exception found" ) |
| 1024 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1025 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1026 | except Exception: |
| 1027 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1028 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 1029 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1030 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1031 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1032 | Lists all core links |
| 1033 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1034 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1035 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1036 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1037 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1038 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1039 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1040 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1041 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1042 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1043 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1044 | except AssertionError: |
| 1045 | main.log.exception( "" ) |
| 1046 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1047 | except TypeError: |
| 1048 | main.log.exception( self.name + ": Object not as expected" ) |
| 1049 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1050 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1051 | main.log.error( self.name + ": EOF exception found" ) |
| 1052 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1053 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1054 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1055 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1056 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1057 | |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1058 | def ports( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1059 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1060 | Lists all ports |
| 1061 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1062 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1063 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1064 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1065 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1066 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1067 | cmdStr += " -j" |
You Wang | 3b9689a | 2018-08-30 12:24:00 -0700 | [diff] [blame] | 1068 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1069 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1070 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1071 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1072 | except AssertionError: |
| 1073 | main.log.exception( "" ) |
| 1074 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1075 | except TypeError: |
| 1076 | main.log.exception( self.name + ": Object not as expected" ) |
| 1077 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1078 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1079 | main.log.error( self.name + ": EOF exception found" ) |
| 1080 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1081 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1082 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1083 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1084 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 1085 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1086 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1087 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1088 | Lists all devices and the controllers with roles assigned to them |
| 1089 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1090 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1091 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 1092 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1093 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1094 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1095 | cmdStr += " -j" |
| 1096 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1097 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1098 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1099 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1100 | except AssertionError: |
| 1101 | main.log.exception( "" ) |
| 1102 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1103 | except TypeError: |
| 1104 | main.log.exception( self.name + ": Object not as expected" ) |
| 1105 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1106 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1107 | main.log.error( self.name + ": EOF exception found" ) |
| 1108 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1109 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1110 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1111 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1112 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1113 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1114 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1115 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1116 | Given the a string containing the json representation of the "roles" |
| 1117 | cli command and a partial or whole device id, returns a json object |
| 1118 | containing the roles output for the first device whose id contains |
| 1119 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1120 | |
| 1121 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1122 | A dict of the role assignments for the given device or |
| 1123 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1124 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1125 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1126 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1127 | return None |
| 1128 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1129 | rawRoles = self.roles() |
| 1130 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1131 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1132 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1133 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1134 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 1135 | return device |
| 1136 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1137 | except ( TypeError, ValueError ): |
| 1138 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1139 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 1140 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1141 | main.log.error( self.name + ": EOF exception found" ) |
| 1142 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1143 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1144 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1145 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1146 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1147 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1148 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1149 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1150 | Iterates through each device and checks if there is a master assigned |
| 1151 | Returns: main.TRUE if each device has a master |
| 1152 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1153 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1154 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1155 | rawRoles = self.roles() |
| 1156 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1157 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1158 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1159 | # print device |
| 1160 | if device[ 'master' ] == "none": |
| 1161 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1162 | return main.FALSE |
| 1163 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1164 | except ( TypeError, ValueError ): |
| 1165 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1166 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1167 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1168 | main.log.error( self.name + ": EOF exception found" ) |
| 1169 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1170 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1171 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1172 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1173 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1174 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1175 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1176 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1177 | Returns string of paths, and the cost. |
| 1178 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1179 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1180 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1181 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1182 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1183 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1184 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1185 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1186 | main.log.error( self.name + ": Error in getting paths" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1187 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1188 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1189 | path = handle.split( ";" )[ 0 ] |
| 1190 | cost = handle.split( ";" )[ 1 ] |
| 1191 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1192 | except AssertionError: |
| 1193 | main.log.exception( "" ) |
| 1194 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1195 | except TypeError: |
| 1196 | main.log.exception( self.name + ": Object not as expected" ) |
| 1197 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1198 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1199 | main.log.error( self.name + ": EOF exception found" ) |
| 1200 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1201 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1202 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1203 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1204 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1205 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1206 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1207 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1208 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1209 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1210 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1211 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1212 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1213 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1214 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1215 | cmdStr += " -j" |
| 1216 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1217 | if handle: |
| 1218 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1219 | # TODO: Maybe make this less hardcoded |
| 1220 | # ConsistentMap Exceptions |
| 1221 | assert "org.onosproject.store.service" not in handle |
| 1222 | # Node not leader |
| 1223 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1224 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1225 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1226 | main.log.exception( self.name + ": Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1227 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1228 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1229 | except TypeError: |
| 1230 | main.log.exception( self.name + ": Object not as expected" ) |
| 1231 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1232 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1233 | main.log.error( self.name + ": EOF exception found" ) |
| 1234 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1235 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1236 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1237 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1238 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1239 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1240 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1241 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1242 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1243 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1244 | 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] | 1245 | partial mac address |
| 1246 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1247 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1248 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1249 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1250 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1251 | return None |
| 1252 | else: |
| 1253 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1254 | rawHosts = self.hosts() |
| 1255 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1256 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1257 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1258 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1259 | if not host: |
| 1260 | pass |
| 1261 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1262 | return host |
| 1263 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1264 | except ( TypeError, ValueError ): |
| 1265 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1266 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1267 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1268 | main.log.error( self.name + ": EOF exception found" ) |
| 1269 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1270 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1271 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1272 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1273 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1274 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1275 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1276 | """ |
| 1277 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1278 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1279 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1280 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1281 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1282 | IMPORTANT: |
| 1283 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1284 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1285 | Furthermore, it assumes that value of VLAN is '-1' |
| 1286 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1287 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1288 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1289 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1290 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1291 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1292 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1293 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1294 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1295 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1296 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1297 | i = iter( str( hostHex ) ) |
| 1298 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1299 | hostHex = hostHex + "/-1" |
| 1300 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1301 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1302 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1303 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1304 | except TypeError: |
| 1305 | main.log.exception( self.name + ": Object not as expected" ) |
| 1306 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1307 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1308 | main.log.error( self.name + ": EOF exception found" ) |
| 1309 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1310 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1311 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1312 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1313 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1314 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1315 | def verifyHostLocation( self, hostIp, location ): |
| 1316 | """ |
| 1317 | Description: |
| 1318 | Verify the host given is discovered in all locations expected |
| 1319 | Required: |
| 1320 | hostIp: IP address of the host |
| 1321 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1322 | Could be a string or list |
| 1323 | Returns: |
| 1324 | main.TRUE if host is discovered on all locations provided |
| 1325 | main.FALSE otherwise |
| 1326 | """ |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1327 | locations = [ location ] if isinstance( location, str ) else location |
| 1328 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1329 | try: |
| 1330 | hosts = self.hosts() |
| 1331 | hosts = json.loads( hosts ) |
| 1332 | targetHost = None |
| 1333 | for host in hosts: |
| 1334 | if hostIp in host[ "ipAddresses" ]: |
| 1335 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1336 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1337 | result = main.TRUE |
| 1338 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1339 | for loc in locations: |
| 1340 | discovered = False |
| 1341 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1342 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1343 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1344 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1345 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1346 | break |
| 1347 | if discovered: |
| 1348 | locationsDiscovered.remove( locDiscovered ) |
| 1349 | else: |
| 1350 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1351 | result = main.FALSE |
| 1352 | if locationsDiscovered: |
| 1353 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1354 | result = main.FALSE |
| 1355 | return result |
| 1356 | except KeyError: |
| 1357 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1358 | return None |
| 1359 | except pexpect.EOF: |
| 1360 | main.log.error( self.name + ": EOF exception found" ) |
| 1361 | main.log.error( self.name + ": " + self.handle.before ) |
| 1362 | main.cleanAndExit() |
| 1363 | except Exception: |
| 1364 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1365 | return None |
| 1366 | |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1367 | def verifyHostIp( self, hostList=[], prefix="" ): |
| 1368 | """ |
| 1369 | Description: |
| 1370 | Verify that all hosts have IP address assigned to them |
| 1371 | Optional: |
| 1372 | hostList: If specified, verifications only happen to the hosts |
| 1373 | in hostList |
| 1374 | prefix: at least one of the ip address assigned to the host |
| 1375 | needs to have the specified prefix |
| 1376 | Returns: |
| 1377 | main.TRUE if all hosts have specific IP address assigned; |
| 1378 | main.FALSE otherwise |
| 1379 | """ |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1380 | try: |
| 1381 | hosts = self.hosts() |
| 1382 | hosts = json.loads( hosts ) |
| 1383 | if not hostList: |
| 1384 | hostList = [ host[ "id" ] for host in hosts ] |
| 1385 | for host in hosts: |
| 1386 | hostId = host[ "id" ] |
| 1387 | if hostId not in hostList: |
| 1388 | continue |
| 1389 | ipList = host[ "ipAddresses" ] |
| 1390 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1391 | if not ipList: |
| 1392 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1393 | else: |
| 1394 | if not any( ip.startswith( str( prefix ) ) for ip in ipList ): |
| 1395 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1396 | else: |
| 1397 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1398 | hostList.remove( hostId ) |
| 1399 | if hostList: |
| 1400 | 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] | 1401 | # Print info for debugging |
| 1402 | main.log.debug( self.name + ": hosts output: " + str( hosts ) ) |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1403 | return main.FALSE |
| 1404 | else: |
| 1405 | return main.TRUE |
| 1406 | except KeyError: |
| 1407 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1408 | return None |
| 1409 | except pexpect.EOF: |
| 1410 | main.log.error( self.name + ": EOF exception found" ) |
| 1411 | main.log.error( self.name + ": " + self.handle.before ) |
| 1412 | main.cleanAndExit() |
| 1413 | except Exception: |
| 1414 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1415 | return None |
| 1416 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1417 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1418 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1419 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1420 | * hostIdOne: ONOS host id for host1 |
| 1421 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1422 | Optional: |
| 1423 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1424 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1425 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1426 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1427 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1428 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1429 | Returns: |
| 1430 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1431 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1432 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1433 | cmdStr = "add-host-intent " |
| 1434 | if vlanId: |
| 1435 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1436 | if setVlan: |
| 1437 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1438 | if encap: |
| 1439 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1440 | if bandwidth: |
| 1441 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1442 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1443 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1444 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1445 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1446 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1447 | main.log.error( self.name + ": Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1448 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1449 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1450 | else: |
| 1451 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1452 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1453 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1454 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1455 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1456 | else: |
| 1457 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1458 | main.log.debug( "Response from ONOS was: " + |
| 1459 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1460 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1461 | except AssertionError: |
| 1462 | main.log.exception( "" ) |
| 1463 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1464 | except TypeError: |
| 1465 | main.log.exception( self.name + ": Object not as expected" ) |
| 1466 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1467 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1468 | main.log.error( self.name + ": EOF exception found" ) |
| 1469 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1470 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1471 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1472 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1473 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1474 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1475 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1476 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1477 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1478 | * ingressDevice: device id of ingress device |
| 1479 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1480 | Optional: |
| 1481 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1482 | Description: |
| 1483 | Adds an optical intent by specifying an ingress and egress device |
| 1484 | Returns: |
| 1485 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1486 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1487 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1488 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1489 | " " + str( egressDevice ) |
| 1490 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1491 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1492 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1493 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1494 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1495 | main.log.error( self.name + ": Error in adding Optical intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1496 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1497 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1498 | main.log.info( "Optical intent installed between " + |
| 1499 | str( ingressDevice ) + " and " + |
| 1500 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1501 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1502 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1503 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1504 | else: |
| 1505 | main.log.error( "Error, intent ID not found" ) |
| 1506 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1507 | except AssertionError: |
| 1508 | main.log.exception( "" ) |
| 1509 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1510 | except TypeError: |
| 1511 | main.log.exception( self.name + ": Object not as expected" ) |
| 1512 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1513 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1514 | main.log.error( self.name + ": EOF exception found" ) |
| 1515 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1516 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1517 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1518 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1519 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1520 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1521 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1522 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1523 | ingressDevice, |
| 1524 | egressDevice, |
| 1525 | portIngress="", |
| 1526 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1527 | ethType="", |
| 1528 | ethSrc="", |
| 1529 | ethDst="", |
| 1530 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1531 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1532 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1533 | ipProto="", |
| 1534 | ipSrc="", |
| 1535 | ipDst="", |
| 1536 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1537 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1538 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1539 | setVlan="", |
| 1540 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1541 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1542 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1543 | * ingressDevice: device id of ingress device |
| 1544 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1545 | Optional: |
| 1546 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1547 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1548 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1549 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1550 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1551 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1552 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1553 | * ipSrc: specify ip source address |
| 1554 | * ipDst: specify ip destination address |
| 1555 | * tcpSrc: specify tcp source port |
| 1556 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1557 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1558 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1559 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1560 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1561 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1562 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1563 | Returns: |
| 1564 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1565 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1566 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1567 | options developers provide for point-to-point |
| 1568 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1569 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1570 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1571 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1572 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1573 | if ethType: |
| 1574 | cmd += " --ethType " + str( ethType ) |
| 1575 | if ethSrc: |
| 1576 | cmd += " --ethSrc " + str( ethSrc ) |
| 1577 | if ethDst: |
| 1578 | cmd += " --ethDst " + str( ethDst ) |
| 1579 | if bandwidth: |
| 1580 | cmd += " --bandwidth " + str( bandwidth ) |
| 1581 | if lambdaAlloc: |
| 1582 | cmd += " --lambda " |
| 1583 | if ipProto: |
| 1584 | cmd += " --ipProto " + str( ipProto ) |
| 1585 | if ipSrc: |
| 1586 | cmd += " --ipSrc " + str( ipSrc ) |
| 1587 | if ipDst: |
| 1588 | cmd += " --ipDst " + str( ipDst ) |
| 1589 | if tcpSrc: |
| 1590 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1591 | if tcpDst: |
| 1592 | cmd += " --tcpDst " + str( tcpDst ) |
| 1593 | if vlanId: |
| 1594 | cmd += " -v " + str( vlanId ) |
| 1595 | if setVlan: |
| 1596 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1597 | if encap: |
| 1598 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1599 | if protected: |
| 1600 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1601 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1602 | # Check whether the user appended the port |
| 1603 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1604 | if "/" in ingressDevice: |
| 1605 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1606 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1607 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1608 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1609 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1610 | # Would it make sense to throw an exception and exit |
| 1611 | # the test? |
| 1612 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1613 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1614 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1615 | str( ingressDevice ) + "/" +\ |
| 1616 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1617 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1618 | if "/" in egressDevice: |
| 1619 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1620 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1621 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1622 | main.log.error( "You must specify the egress port" ) |
| 1623 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1624 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1625 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1626 | str( egressDevice ) + "/" +\ |
| 1627 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1628 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1629 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1630 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1631 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1632 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1633 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1634 | main.log.error( self.name + ": Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1635 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1636 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1637 | # TODO: print out all the options in this message? |
| 1638 | main.log.info( "Point-to-point intent installed between " + |
| 1639 | str( ingressDevice ) + " and " + |
| 1640 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1641 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1642 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1643 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1644 | else: |
| 1645 | main.log.error( "Error, intent ID not found" ) |
| 1646 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1647 | except AssertionError: |
| 1648 | main.log.exception( "" ) |
| 1649 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1650 | except TypeError: |
| 1651 | main.log.exception( self.name + ": Object not as expected" ) |
| 1652 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1653 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1654 | main.log.error( self.name + ": EOF exception found" ) |
| 1655 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1656 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1657 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1658 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1659 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1660 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1661 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1662 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1663 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1664 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1665 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1666 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1667 | ethType="", |
| 1668 | ethSrc="", |
| 1669 | ethDst="", |
| 1670 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1671 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1672 | ipProto="", |
| 1673 | ipSrc="", |
| 1674 | ipDst="", |
| 1675 | tcpSrc="", |
| 1676 | tcpDst="", |
| 1677 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1678 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1679 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1680 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1681 | partial=False, |
| 1682 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1683 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1684 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1685 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1686 | is same. That is, all ingress devices include port numbers |
| 1687 | with a "/" or all ingress devices could specify device |
| 1688 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1689 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1690 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1691 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1692 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1693 | Optional: |
| 1694 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1695 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1696 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1697 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1698 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1699 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1700 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1701 | * ipSrc: specify ip source address |
| 1702 | * ipDst: specify ip destination address |
| 1703 | * tcpSrc: specify tcp source port |
| 1704 | * tcpDst: specify tcp destination port |
| 1705 | * setEthSrc: action to Rewrite Source MAC Address |
| 1706 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1707 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1708 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1709 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1710 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1711 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1712 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1713 | Returns: |
| 1714 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1715 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1716 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1717 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1718 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1719 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1720 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1721 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1722 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1723 | if ethType: |
| 1724 | cmd += " --ethType " + str( ethType ) |
| 1725 | if ethSrc: |
| 1726 | cmd += " --ethSrc " + str( ethSrc ) |
| 1727 | if ethDst: |
| 1728 | cmd += " --ethDst " + str( ethDst ) |
| 1729 | if bandwidth: |
| 1730 | cmd += " --bandwidth " + str( bandwidth ) |
| 1731 | if lambdaAlloc: |
| 1732 | cmd += " --lambda " |
| 1733 | if ipProto: |
| 1734 | cmd += " --ipProto " + str( ipProto ) |
| 1735 | if ipSrc: |
| 1736 | cmd += " --ipSrc " + str( ipSrc ) |
| 1737 | if ipDst: |
| 1738 | cmd += " --ipDst " + str( ipDst ) |
| 1739 | if tcpSrc: |
| 1740 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1741 | if tcpDst: |
| 1742 | cmd += " --tcpDst " + str( tcpDst ) |
| 1743 | if setEthSrc: |
| 1744 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1745 | if setEthDst: |
| 1746 | cmd += " --setEthDst " + str( setEthDst ) |
| 1747 | if vlanId: |
| 1748 | cmd += " -v " + str( vlanId ) |
| 1749 | if setVlan: |
| 1750 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1751 | if partial: |
| 1752 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1753 | if encap: |
| 1754 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1755 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1756 | # Check whether the user appended the port |
| 1757 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1758 | |
| 1759 | if portIngressList is None: |
| 1760 | for ingressDevice in ingressDeviceList: |
| 1761 | if "/" in ingressDevice: |
| 1762 | cmd += " " + str( ingressDevice ) |
| 1763 | else: |
| 1764 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1765 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1766 | # TODO: perhaps more meaningful return |
| 1767 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1768 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1769 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1770 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1771 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1772 | cmd += " " + \ |
| 1773 | str( ingressDevice ) + "/" +\ |
| 1774 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1775 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1776 | main.log.error( "Device list and port list does not " + |
| 1777 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1778 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1779 | if "/" in egressDevice: |
| 1780 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1781 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1782 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1783 | main.log.error( "You must specify " + |
| 1784 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1785 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1786 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1787 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1788 | str( egressDevice ) + "/" +\ |
| 1789 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1790 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1791 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1792 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1793 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1794 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1795 | main.log.error( self.name + ": Error in adding multipoint-to-singlepoint " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1796 | "intent" ) |
| 1797 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1798 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1799 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1800 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1801 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1802 | else: |
| 1803 | main.log.error( "Error, intent ID not found" ) |
| 1804 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1805 | except AssertionError: |
| 1806 | main.log.exception( "" ) |
| 1807 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1808 | except TypeError: |
| 1809 | main.log.exception( self.name + ": Object not as expected" ) |
| 1810 | return None |
| 1811 | except pexpect.EOF: |
| 1812 | main.log.error( self.name + ": EOF exception found" ) |
| 1813 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1814 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1815 | except Exception: |
| 1816 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1817 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1818 | |
| 1819 | def addSinglepointToMultipointIntent( |
| 1820 | self, |
| 1821 | ingressDevice, |
| 1822 | egressDeviceList, |
| 1823 | portIngress="", |
| 1824 | portEgressList=None, |
| 1825 | ethType="", |
| 1826 | ethSrc="", |
| 1827 | ethDst="", |
| 1828 | bandwidth="", |
| 1829 | lambdaAlloc=False, |
| 1830 | ipProto="", |
| 1831 | ipSrc="", |
| 1832 | ipDst="", |
| 1833 | tcpSrc="", |
| 1834 | tcpDst="", |
| 1835 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1836 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1837 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1838 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1839 | partial=False, |
| 1840 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1841 | """ |
| 1842 | Note: |
| 1843 | This function assumes the format of all egress devices |
| 1844 | is same. That is, all egress devices include port numbers |
| 1845 | with a "/" or all egress devices could specify device |
| 1846 | ids and port numbers seperately. |
| 1847 | Required: |
| 1848 | * EgressDeviceList: List of device ids of egress device |
| 1849 | ( Atleast 2 eress devices required in the list ) |
| 1850 | * ingressDevice: device id of ingress device |
| 1851 | Optional: |
| 1852 | * ethType: specify ethType |
| 1853 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1854 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1855 | * bandwidth: specify bandwidth capacity of link |
| 1856 | * lambdaAlloc: if True, intent will allocate lambda |
| 1857 | for the specified intent |
| 1858 | * ipProto: specify ip protocol |
| 1859 | * ipSrc: specify ip source address |
| 1860 | * ipDst: specify ip destination address |
| 1861 | * tcpSrc: specify tcp source port |
| 1862 | * tcpDst: specify tcp destination port |
| 1863 | * setEthSrc: action to Rewrite Source MAC Address |
| 1864 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1865 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1866 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1867 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1868 | Description: |
| 1869 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1870 | specifying device id's and optional fields |
| 1871 | Returns: |
| 1872 | A string of the intent id or None on error |
| 1873 | |
| 1874 | NOTE: This function may change depending on the |
| 1875 | options developers provide for singlepoint-to-multipoint |
| 1876 | intent via cli |
| 1877 | """ |
| 1878 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1879 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1880 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1881 | if ethType: |
| 1882 | cmd += " --ethType " + str( ethType ) |
| 1883 | if ethSrc: |
| 1884 | cmd += " --ethSrc " + str( ethSrc ) |
| 1885 | if ethDst: |
| 1886 | cmd += " --ethDst " + str( ethDst ) |
| 1887 | if bandwidth: |
| 1888 | cmd += " --bandwidth " + str( bandwidth ) |
| 1889 | if lambdaAlloc: |
| 1890 | cmd += " --lambda " |
| 1891 | if ipProto: |
| 1892 | cmd += " --ipProto " + str( ipProto ) |
| 1893 | if ipSrc: |
| 1894 | cmd += " --ipSrc " + str( ipSrc ) |
| 1895 | if ipDst: |
| 1896 | cmd += " --ipDst " + str( ipDst ) |
| 1897 | if tcpSrc: |
| 1898 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1899 | if tcpDst: |
| 1900 | cmd += " --tcpDst " + str( tcpDst ) |
| 1901 | if setEthSrc: |
| 1902 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1903 | if setEthDst: |
| 1904 | cmd += " --setEthDst " + str( setEthDst ) |
| 1905 | if vlanId: |
| 1906 | cmd += " -v " + str( vlanId ) |
| 1907 | if setVlan: |
| 1908 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1909 | if partial: |
| 1910 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1911 | if encap: |
| 1912 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1913 | |
| 1914 | # Check whether the user appended the port |
| 1915 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1916 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1917 | if "/" in ingressDevice: |
| 1918 | cmd += " " + str( ingressDevice ) |
| 1919 | else: |
| 1920 | if not portIngress: |
| 1921 | main.log.error( "You must specify " + |
| 1922 | "the Ingress port" ) |
| 1923 | return main.FALSE |
| 1924 | |
| 1925 | cmd += " " +\ |
| 1926 | str( ingressDevice ) + "/" +\ |
| 1927 | str( portIngress ) |
| 1928 | |
| 1929 | if portEgressList is None: |
| 1930 | for egressDevice in egressDeviceList: |
| 1931 | if "/" in egressDevice: |
| 1932 | cmd += " " + str( egressDevice ) |
| 1933 | else: |
| 1934 | main.log.error( "You must specify " + |
| 1935 | "the egress port" ) |
| 1936 | # TODO: perhaps more meaningful return |
| 1937 | return main.FALSE |
| 1938 | else: |
| 1939 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1940 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1941 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1942 | cmd += " " + \ |
| 1943 | str( egressDevice ) + "/" +\ |
| 1944 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1945 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1946 | main.log.error( "Device list and port list does not " + |
| 1947 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1948 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1949 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1950 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1951 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1952 | # If error, return error message |
| 1953 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 1954 | main.log.error( self.name + ": Error in adding singlepoint-to-multipoint " + |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1955 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1956 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1957 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1958 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1959 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1960 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1961 | else: |
| 1962 | main.log.error( "Error, intent ID not found" ) |
| 1963 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1964 | except AssertionError: |
| 1965 | main.log.exception( "" ) |
| 1966 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1967 | except TypeError: |
| 1968 | main.log.exception( self.name + ": Object not as expected" ) |
| 1969 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1970 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1971 | main.log.error( self.name + ": EOF exception found" ) |
| 1972 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1973 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1974 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1975 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1976 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1977 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1978 | def addMplsIntent( |
| 1979 | self, |
| 1980 | ingressDevice, |
| 1981 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1982 | ingressPort="", |
| 1983 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1984 | ethType="", |
| 1985 | ethSrc="", |
| 1986 | ethDst="", |
| 1987 | bandwidth="", |
| 1988 | lambdaAlloc=False, |
| 1989 | ipProto="", |
| 1990 | ipSrc="", |
| 1991 | ipDst="", |
| 1992 | tcpSrc="", |
| 1993 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1994 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1995 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1996 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1997 | """ |
| 1998 | Required: |
| 1999 | * ingressDevice: device id of ingress device |
| 2000 | * egressDevice: device id of egress device |
| 2001 | Optional: |
| 2002 | * ethType: specify ethType |
| 2003 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 2004 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 2005 | * bandwidth: specify bandwidth capacity of link |
| 2006 | * lambdaAlloc: if True, intent will allocate lambda |
| 2007 | for the specified intent |
| 2008 | * ipProto: specify ip protocol |
| 2009 | * ipSrc: specify ip source address |
| 2010 | * ipDst: specify ip destination address |
| 2011 | * tcpSrc: specify tcp source port |
| 2012 | * tcpDst: specify tcp destination port |
| 2013 | * ingressLabel: Ingress MPLS label |
| 2014 | * egressLabel: Egress MPLS label |
| 2015 | Description: |
| 2016 | Adds MPLS intent by |
| 2017 | specifying device id's and optional fields |
| 2018 | Returns: |
| 2019 | A string of the intent id or None on error |
| 2020 | |
| 2021 | NOTE: This function may change depending on the |
| 2022 | options developers provide for MPLS |
| 2023 | intent via cli |
| 2024 | """ |
| 2025 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 2026 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2027 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 2028 | if ethType: |
| 2029 | cmd += " --ethType " + str( ethType ) |
| 2030 | if ethSrc: |
| 2031 | cmd += " --ethSrc " + str( ethSrc ) |
| 2032 | if ethDst: |
| 2033 | cmd += " --ethDst " + str( ethDst ) |
| 2034 | if bandwidth: |
| 2035 | cmd += " --bandwidth " + str( bandwidth ) |
| 2036 | if lambdaAlloc: |
| 2037 | cmd += " --lambda " |
| 2038 | if ipProto: |
| 2039 | cmd += " --ipProto " + str( ipProto ) |
| 2040 | if ipSrc: |
| 2041 | cmd += " --ipSrc " + str( ipSrc ) |
| 2042 | if ipDst: |
| 2043 | cmd += " --ipDst " + str( ipDst ) |
| 2044 | if tcpSrc: |
| 2045 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 2046 | if tcpDst: |
| 2047 | cmd += " --tcpDst " + str( tcpDst ) |
| 2048 | if ingressLabel: |
| 2049 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 2050 | if egressLabel: |
| 2051 | cmd += " --egressLabel " + str( egressLabel ) |
| 2052 | if priority: |
| 2053 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2054 | |
| 2055 | # Check whether the user appended the port |
| 2056 | # or provided it as an input |
| 2057 | if "/" in ingressDevice: |
| 2058 | cmd += " " + str( ingressDevice ) |
| 2059 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2060 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2061 | main.log.error( "You must specify the ingress port" ) |
| 2062 | return None |
| 2063 | |
| 2064 | cmd += " " + \ |
| 2065 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2066 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2067 | |
| 2068 | if "/" in egressDevice: |
| 2069 | cmd += " " + str( egressDevice ) |
| 2070 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2071 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2072 | main.log.error( "You must specify the egress port" ) |
| 2073 | return None |
| 2074 | |
| 2075 | cmd += " " +\ |
| 2076 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 2077 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2078 | |
| 2079 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2080 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2081 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2082 | # If error, return error message |
| 2083 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2084 | main.log.error( self.name + ": Error in adding mpls intent" ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2085 | return None |
| 2086 | else: |
| 2087 | # TODO: print out all the options in this message? |
| 2088 | main.log.info( "MPLS intent installed between " + |
| 2089 | str( ingressDevice ) + " and " + |
| 2090 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2091 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2092 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2093 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2094 | else: |
| 2095 | main.log.error( "Error, intent ID not found" ) |
| 2096 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2097 | except AssertionError: |
| 2098 | main.log.exception( "" ) |
| 2099 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2100 | except TypeError: |
| 2101 | main.log.exception( self.name + ": Object not as expected" ) |
| 2102 | return None |
| 2103 | except pexpect.EOF: |
| 2104 | main.log.error( self.name + ": EOF exception found" ) |
| 2105 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2106 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2107 | except Exception: |
| 2108 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2109 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 2110 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2111 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 2112 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2113 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2114 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2115 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2116 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 2117 | -p or --purge: Purge the intent from the store after removal |
| 2118 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2119 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 2120 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2121 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2122 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2123 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2124 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 2125 | if purge: |
| 2126 | cmdStr += " -p" |
| 2127 | if sync: |
| 2128 | cmdStr += " -s" |
| 2129 | |
| 2130 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2131 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2132 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2133 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2134 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2135 | main.log.error( self.name + ": Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2136 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2137 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2138 | # TODO: Should this be main.TRUE |
| 2139 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2140 | except AssertionError: |
| 2141 | main.log.exception( "" ) |
| 2142 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2143 | except TypeError: |
| 2144 | main.log.exception( self.name + ": Object not as expected" ) |
| 2145 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2146 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2147 | main.log.error( self.name + ": EOF exception found" ) |
| 2148 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2149 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2150 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2151 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2152 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2153 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2154 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2155 | """ |
| 2156 | Description: |
| 2157 | Remove all the intents |
| 2158 | Optional args:- |
| 2159 | -s or --sync: Waits for the removal before returning |
| 2160 | -p or --purge: Purge the intent from the store after removal |
| 2161 | Returns: |
| 2162 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2163 | main.FALSE; Returns None for exception |
| 2164 | """ |
| 2165 | try: |
| 2166 | cmdStr = "remove-intent" |
| 2167 | if purge: |
| 2168 | cmdStr += " -p" |
| 2169 | if sync: |
| 2170 | cmdStr += " -s" |
| 2171 | |
| 2172 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2173 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2174 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2175 | assert "Command not found:" not in handle, handle |
| 2176 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2177 | main.log.error( self.name + ": Error in removing intent" ) |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2178 | return main.FALSE |
| 2179 | else: |
| 2180 | return main.TRUE |
| 2181 | except AssertionError: |
| 2182 | main.log.exception( "" ) |
| 2183 | return None |
| 2184 | except TypeError: |
| 2185 | main.log.exception( self.name + ": Object not as expected" ) |
| 2186 | return None |
| 2187 | except pexpect.EOF: |
| 2188 | main.log.error( self.name + ": EOF exception found" ) |
| 2189 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2190 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2191 | except Exception: |
| 2192 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2193 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2194 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2195 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2196 | """ |
| 2197 | Purges all WITHDRAWN Intents |
| 2198 | """ |
| 2199 | try: |
| 2200 | cmdStr = "purge-intents" |
| 2201 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2202 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2203 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2204 | if re.search( "Error", handle ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2205 | main.log.error( self.name + ": Error in purging intents" ) |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2206 | return main.FALSE |
| 2207 | else: |
| 2208 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2209 | except AssertionError: |
| 2210 | main.log.exception( "" ) |
| 2211 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2212 | except TypeError: |
| 2213 | main.log.exception( self.name + ": Object not as expected" ) |
| 2214 | return None |
| 2215 | except pexpect.EOF: |
| 2216 | main.log.error( self.name + ": EOF exception found" ) |
| 2217 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2218 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2219 | except Exception: |
| 2220 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2221 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2222 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2223 | def wipeout( self ): |
| 2224 | """ |
| 2225 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2226 | """ |
| 2227 | try: |
| 2228 | cmdStr = "wipe-out please" |
| 2229 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2230 | assert handle is not None, "Error in sendline" |
| 2231 | assert "Command not found:" not in handle, handle |
| 2232 | return main.TRUE |
| 2233 | except AssertionError: |
| 2234 | main.log.exception( "" ) |
| 2235 | return None |
| 2236 | except TypeError: |
| 2237 | main.log.exception( self.name + ": Object not as expected" ) |
| 2238 | return None |
| 2239 | except pexpect.EOF: |
| 2240 | main.log.error( self.name + ": EOF exception found" ) |
| 2241 | main.log.error( self.name + ": " + self.handle.before ) |
| 2242 | main.cleanAndExit() |
| 2243 | except Exception: |
| 2244 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2245 | main.cleanAndExit() |
| 2246 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2247 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2248 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2249 | NOTE: This method should be used after installing application: |
| 2250 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2251 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2252 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2253 | Description: |
| 2254 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2255 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2256 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2257 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2258 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2259 | cmdStr += " -j" |
| 2260 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2261 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2262 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2263 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2264 | except AssertionError: |
| 2265 | main.log.exception( "" ) |
| 2266 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2267 | except TypeError: |
| 2268 | main.log.exception( self.name + ": Object not as expected" ) |
| 2269 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2270 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2271 | main.log.error( self.name + ": EOF exception found" ) |
| 2272 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2273 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2274 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2275 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2276 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2277 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2278 | def ipv4RouteNumber( self ): |
| 2279 | """ |
| 2280 | NOTE: This method should be used after installing application: |
| 2281 | onos-app-sdnip |
| 2282 | Description: |
| 2283 | Obtain the total IPv4 routes number in the system |
| 2284 | """ |
| 2285 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2286 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2287 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2288 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2289 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2290 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2291 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2292 | except AssertionError: |
| 2293 | main.log.exception( "" ) |
| 2294 | return None |
| 2295 | except ( TypeError, ValueError ): |
| 2296 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2297 | return None |
| 2298 | except pexpect.EOF: |
| 2299 | main.log.error( self.name + ": EOF exception found" ) |
| 2300 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2301 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2302 | except Exception: |
| 2303 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2304 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2305 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2306 | # =============Function to check Bandwidth allocation======== |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2307 | def allocations( self, jsonFormat = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2308 | """ |
| 2309 | Description: |
| 2310 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2311 | """ |
| 2312 | try: |
| 2313 | cmdStr = "allocations" |
| 2314 | if jsonFormat: |
| 2315 | cmdStr += " -j" |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 2316 | handle = self.sendline( cmdStr, timeout=300 ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2317 | assert handle is not None, "Error in sendline" |
| 2318 | assert "Command not found:" not in handle, handle |
| 2319 | return handle |
| 2320 | except AssertionError: |
| 2321 | main.log.exception( "" ) |
| 2322 | return None |
| 2323 | except ( TypeError, ValueError ): |
| 2324 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2325 | return None |
| 2326 | except pexpect.EOF: |
| 2327 | main.log.error( self.name + ": EOF exception found" ) |
| 2328 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2329 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2330 | except Exception: |
| 2331 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2332 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2333 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2334 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2335 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2336 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2337 | Obtain intents from the ONOS cli. |
| 2338 | Optional: |
| 2339 | * jsonFormat: Enable output formatting in json, default to True |
| 2340 | * summary: Whether only output the intent summary, defaults to False |
| 2341 | * type: Only output a certain type of intent. This options is valid |
| 2342 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2343 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2344 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2345 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2346 | if summary: |
| 2347 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2348 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2349 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2350 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2351 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2352 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2353 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2354 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2355 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2356 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2357 | intentType = "" |
| 2358 | # IF we want the summary of a specific intent type |
| 2359 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2360 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2361 | if intentType in jsonResult.keys(): |
| 2362 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2363 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2364 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2365 | return handle |
| 2366 | else: |
| 2367 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2368 | except AssertionError: |
| 2369 | main.log.exception( "" ) |
| 2370 | return None |
| 2371 | except ( TypeError, ValueError ): |
| 2372 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2373 | return None |
| 2374 | except pexpect.EOF: |
| 2375 | main.log.error( self.name + ": EOF exception found" ) |
| 2376 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2377 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2378 | except Exception: |
| 2379 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2380 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2381 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2382 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2383 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2384 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2385 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2386 | list of intent IDs. |
| 2387 | Parameters: |
| 2388 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2389 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2390 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2391 | 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] | 2392 | accepted. |
| 2393 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2394 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2395 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2396 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2397 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2398 | try: |
| 2399 | state = "State is Undefined" |
| 2400 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2401 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2402 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2403 | rawJson = intentsJson |
| 2404 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2405 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2406 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2407 | if intentsId == intent[ 'id' ]: |
| 2408 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2409 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2410 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2411 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2412 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2413 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2414 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2415 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2416 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2417 | for intent in parsedIntentsJson: |
| 2418 | if intentsId[ i ] == intent[ 'id' ]: |
| 2419 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2420 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2421 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2422 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2423 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2424 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2425 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2426 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2427 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2428 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2429 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2430 | except ( TypeError, ValueError ): |
| 2431 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2432 | return None |
| 2433 | except pexpect.EOF: |
| 2434 | main.log.error( self.name + ": EOF exception found" ) |
| 2435 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2436 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2437 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2438 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2439 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2440 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2441 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2442 | """ |
| 2443 | Description: |
| 2444 | Check intents state |
| 2445 | Required: |
| 2446 | intentsId - List of intents ID to be checked |
| 2447 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2448 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2449 | state in the list. |
| 2450 | *NOTE: You can pass in a list of expected state, |
| 2451 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2452 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2453 | Returns main.TRUE only if all intent are the same as expected states, |
| 2454 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2455 | """ |
| 2456 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2457 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2458 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2459 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2460 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2461 | intentsDict = [] |
| 2462 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2463 | if isinstance( intentsId, types.StringType ) \ |
| 2464 | and intent.get( 'id' ) == intentsId: |
| 2465 | intentsDict.append( intent ) |
| 2466 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2467 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2468 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2469 | |
| 2470 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2471 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2472 | "getting intents state" ) |
| 2473 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2474 | |
| 2475 | if isinstance( expectedState, types.StringType ): |
| 2476 | for intents in intentsDict: |
| 2477 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2478 | main.log.debug( self.name + " : Intent ID - " + |
| 2479 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2480 | " actual state = " + |
| 2481 | intents.get( 'state' ) |
| 2482 | + " does not equal expected state = " |
| 2483 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2484 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2485 | elif isinstance( expectedState, types.ListType ): |
| 2486 | for intents in intentsDict: |
| 2487 | if not any( state == intents.get( 'state' ) for state in |
| 2488 | expectedState ): |
| 2489 | main.log.debug( self.name + " : Intent ID - " + |
| 2490 | intents.get( 'id' ) + |
| 2491 | " actual state = " + |
| 2492 | intents.get( 'state' ) + |
| 2493 | " does not equal expected states = " |
| 2494 | + str( expectedState ) ) |
| 2495 | returnValue = main.FALSE |
| 2496 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2497 | if returnValue == main.TRUE: |
| 2498 | main.log.info( self.name + ": All " + |
| 2499 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2500 | " intents are in " + str( expectedState ) + |
| 2501 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2502 | return returnValue |
| 2503 | except TypeError: |
| 2504 | main.log.exception( self.name + ": Object not as expected" ) |
| 2505 | return None |
| 2506 | except pexpect.EOF: |
| 2507 | main.log.error( self.name + ": EOF exception found" ) |
| 2508 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2509 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2510 | except Exception: |
| 2511 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2512 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2513 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2514 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2515 | """ |
| 2516 | Description: |
| 2517 | Compare the allocated bandwidth with the given allocations |
| 2518 | Required: |
| 2519 | expectedAllocations - The expected ONOS output of the allocations command |
| 2520 | Return: |
| 2521 | Returns main.TRUE only if all intent are the same as expected states, |
| 2522 | otherwise returns main.FALSE. |
| 2523 | """ |
| 2524 | # FIXME: Convert these string comparisons to object comparisons |
| 2525 | try: |
| 2526 | returnValue = main.TRUE |
| 2527 | bandwidthFailed = False |
| 2528 | rawAlloc = self.allocations() |
| 2529 | expectedFormat = StringIO( expectedAllocations ) |
| 2530 | ONOSOutput = StringIO( rawAlloc ) |
| 2531 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2532 | str( expectedFormat ) ) ) |
| 2533 | |
| 2534 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2535 | actual = actual.rstrip() |
| 2536 | expected = expected.rstrip() |
| 2537 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2538 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2539 | marker1 = actual.find( 'allocated' ) |
| 2540 | m1 = actual[ :marker1 ] |
| 2541 | marker2 = expected.find( 'allocated' ) |
| 2542 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2543 | if m1 != m2: |
| 2544 | bandwidthFailed = True |
| 2545 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2546 | bandwidthFailed = True |
| 2547 | expectedFormat.close() |
| 2548 | ONOSOutput.close() |
| 2549 | |
| 2550 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2551 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2552 | returnValue = main.FALSE |
| 2553 | return returnValue |
| 2554 | except TypeError: |
| 2555 | main.log.exception( self.name + ": Object not as expected" ) |
| 2556 | return None |
| 2557 | except pexpect.EOF: |
| 2558 | main.log.error( self.name + ": EOF exception found" ) |
| 2559 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2560 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2561 | except Exception: |
| 2562 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2563 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2564 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2565 | def compareIntent( self, intentDict ): |
| 2566 | """ |
| 2567 | Description: |
| 2568 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2569 | Return: |
| 2570 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2571 | Arguments: |
| 2572 | intentDict: a dictionary which maps intent ids to intent states |
| 2573 | """ |
| 2574 | try: |
| 2575 | intentsRaw = self.intents() |
| 2576 | intentsJson = json.loads( intentsRaw ) |
| 2577 | intentDictONOS = {} |
| 2578 | for intent in intentsJson: |
| 2579 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2580 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2581 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2582 | 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] | 2583 | str( len( intentDict ) ) + " expected and " + |
| 2584 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2585 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2586 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2587 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2588 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2589 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2590 | else: |
| 2591 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2592 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2593 | " expected state is " + intentDict[ intentID ] + |
| 2594 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2595 | returnValue = main.FALSE |
| 2596 | intentDictONOS.pop( intentID ) |
| 2597 | if len( intentDictONOS ) > 0: |
| 2598 | returnValue = main.FALSE |
| 2599 | for intentID in intentDictONOS.keys(): |
| 2600 | 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] | 2601 | if returnValue == main.TRUE: |
| 2602 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2603 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2604 | except KeyError: |
| 2605 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2606 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2607 | except ( TypeError, ValueError ): |
| 2608 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2609 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2610 | except pexpect.EOF: |
| 2611 | main.log.error( self.name + ": EOF exception found" ) |
| 2612 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2613 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2614 | except Exception: |
| 2615 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2616 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2617 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2618 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2619 | """ |
| 2620 | Description: |
| 2621 | Check the number of installed intents. |
| 2622 | Optional: |
| 2623 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2624 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2625 | Return: |
| 2626 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2627 | , otherwise, returns main.FALSE. |
| 2628 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2629 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2630 | try: |
| 2631 | cmd = "intents -s -j" |
| 2632 | |
| 2633 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2634 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2635 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2636 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2637 | response = json.loads( response ) |
| 2638 | |
| 2639 | # get total and installed number, see if they are match |
| 2640 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2641 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2642 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2643 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2644 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2645 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2646 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2647 | return main.FALSE |
| 2648 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2649 | except ( TypeError, ValueError ): |
| 2650 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2651 | return None |
| 2652 | except pexpect.EOF: |
| 2653 | main.log.error( self.name + ": EOF exception found" ) |
| 2654 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2655 | if noExit: |
| 2656 | return main.FALSE |
| 2657 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2658 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2659 | except pexpect.TIMEOUT: |
| 2660 | main.log.error( self.name + ": ONOS timeout" ) |
| 2661 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2662 | except Exception: |
| 2663 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2664 | if noExit: |
| 2665 | return main.FALSE |
| 2666 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2667 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2668 | |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2669 | 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] | 2670 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2671 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2672 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2673 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2674 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2675 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2676 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2677 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2678 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2679 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2680 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2681 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2682 | cmdStr += " -n" |
| 2683 | cmdStr += " " + state |
| 2684 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2685 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2686 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2687 | assert "Command not found:" not in handle, handle |
| 2688 | if re.search( "Error:", handle ): |
| 2689 | main.log.error( self.name + ": flows() response: " + |
| 2690 | str( handle ) ) |
| 2691 | return handle |
| 2692 | except AssertionError: |
| 2693 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2694 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2695 | except TypeError: |
| 2696 | main.log.exception( self.name + ": Object not as expected" ) |
| 2697 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2698 | except pexpect.TIMEOUT: |
| 2699 | main.log.error( self.name + ": ONOS timeout" ) |
| 2700 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2701 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2702 | main.log.error( self.name + ": EOF exception found" ) |
| 2703 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2704 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2705 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2706 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2707 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2708 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2709 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2710 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2711 | count = int( count ) if count else 0 |
steven30801 | eccfe21 | 2019-01-24 13:00:42 +0800 | [diff] [blame] | 2712 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 2713 | return count if ( count >= min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2714 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2715 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2716 | """ |
| 2717 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2718 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2719 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2720 | if the count of those states is 0, which means all current flows |
| 2721 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2722 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2723 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2724 | Return: |
| 2725 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2726 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2727 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2728 | """ |
| 2729 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2730 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2731 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2732 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2733 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2734 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2735 | if rawFlows: |
| 2736 | # if we didn't get flows or flows function return None, we should return |
| 2737 | # main.Flase |
| 2738 | checkedStates.append( json.loads( rawFlows ) ) |
| 2739 | else: |
| 2740 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2741 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2742 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2743 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2744 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2745 | except TypeError: |
| 2746 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2747 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2748 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2749 | # We want to count PENDING_ADD if isPENDING is true |
| 2750 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2751 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2752 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2753 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2754 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2755 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2756 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2757 | except ( TypeError, ValueError ): |
| 2758 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2759 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2760 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2761 | except AssertionError: |
| 2762 | main.log.exception( "" ) |
| 2763 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2764 | except pexpect.TIMEOUT: |
| 2765 | main.log.error( self.name + ": ONOS timeout" ) |
| 2766 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2767 | except pexpect.EOF: |
| 2768 | main.log.error( self.name + ": EOF exception found" ) |
| 2769 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2770 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2771 | except Exception: |
| 2772 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2773 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2774 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2775 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2776 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2777 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2778 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2779 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2780 | a specific point-to-point intent definition |
| 2781 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2782 | * ingress: specify source dpid |
| 2783 | * egress: specify destination dpid |
| 2784 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2785 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2786 | * offset: the keyOffset is where the next batch of intents |
| 2787 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2788 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2789 | * getResponse: If set to True, function will return ONOS response. |
| 2790 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2791 | Returns: If failed to push test intents, it will returen None, |
| 2792 | if successful, return true. |
| 2793 | Timeout expection will return None, |
| 2794 | TypeError will return false |
| 2795 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2796 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2797 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2798 | if background: |
| 2799 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2800 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2801 | back = "" |
| 2802 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2803 | ingress, |
| 2804 | egress, |
| 2805 | batchSize, |
| 2806 | offset, |
| 2807 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2808 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2809 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2810 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2811 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2812 | if getResponse: |
| 2813 | return response |
| 2814 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2815 | # TODO: We should handle if there is failure in installation |
| 2816 | return main.TRUE |
| 2817 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2818 | except AssertionError: |
| 2819 | main.log.exception( "" ) |
| 2820 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2821 | except pexpect.TIMEOUT: |
| 2822 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2823 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2824 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2825 | main.log.error( self.name + ": EOF exception found" ) |
| 2826 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2827 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2828 | except TypeError: |
| 2829 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2830 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2831 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2832 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2833 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2834 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2835 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2836 | """ |
| 2837 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2838 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2839 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2840 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2841 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2842 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2843 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2844 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2845 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2846 | cmd = "flows -c added" |
| 2847 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2848 | if rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2849 | rawFlows = rawFlows.split( "\n" ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2850 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2851 | for l in rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2852 | totalFlows += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2853 | else: |
You Wang | 68568b1 | 2019-03-04 11:49:57 -0800 | [diff] [blame] | 2854 | main.log.warn( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2855 | return None |
| 2856 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2857 | |
You Wang | d3097f7 | 2018-12-12 11:56:03 -0800 | [diff] [blame] | 2858 | except IndexError: |
| 2859 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
| 2860 | main.log.debug( "rawFlows: {}".format( rawFlows ) ) |
| 2861 | return None |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2862 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2863 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2864 | return None |
| 2865 | except pexpect.EOF: |
| 2866 | main.log.error( self.name + ": EOF exception found" ) |
| 2867 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2868 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2869 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2870 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2871 | except pexpect.TIMEOUT: |
| 2872 | main.log.error( self.name + ": ONOS timeout" ) |
| 2873 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2874 | except Exception: |
| 2875 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2876 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2877 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2878 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2879 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2880 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2881 | """ |
| 2882 | Description: |
| 2883 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2884 | Optional: |
| 2885 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2886 | Return: |
| 2887 | The number of intents |
| 2888 | """ |
| 2889 | try: |
| 2890 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2891 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2892 | if response is None: |
| 2893 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2894 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2895 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2896 | except ( TypeError, ValueError ): |
| 2897 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2898 | return None |
| 2899 | except pexpect.EOF: |
| 2900 | main.log.error( self.name + ": EOF exception found" ) |
| 2901 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2902 | if noExit: |
| 2903 | return -1 |
| 2904 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2905 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2906 | except Exception: |
| 2907 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2908 | if noExit: |
| 2909 | return -1 |
| 2910 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2911 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2912 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2913 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2914 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2915 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2916 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2917 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2918 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2919 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2920 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2921 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2922 | cmdStr += " -j" |
| 2923 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2924 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2925 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2926 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2927 | except AssertionError: |
| 2928 | main.log.exception( "" ) |
| 2929 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2930 | except TypeError: |
| 2931 | main.log.exception( self.name + ": Object not as expected" ) |
| 2932 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2933 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2934 | main.log.error( self.name + ": EOF exception found" ) |
| 2935 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2936 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2937 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2938 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2939 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2940 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2941 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2942 | """ |
| 2943 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2944 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2945 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2946 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2947 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2948 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2949 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2950 | cmdStr += " -j" |
| 2951 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2952 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2953 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2954 | if handle: |
| 2955 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2956 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2957 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2958 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2959 | else: |
| 2960 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2961 | except AssertionError: |
| 2962 | main.log.exception( "" ) |
| 2963 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2964 | except TypeError: |
| 2965 | main.log.exception( self.name + ": Object not as expected" ) |
| 2966 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2967 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2968 | main.log.error( self.name + ": EOF exception found" ) |
| 2969 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2970 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2971 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2972 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2973 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2974 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2975 | # Wrapper functions **************** |
| 2976 | # Wrapper functions use existing driver |
| 2977 | # functions and extends their use case. |
| 2978 | # For example, we may use the output of |
| 2979 | # a normal driver function, and parse it |
| 2980 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2981 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2982 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2983 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2984 | Description: |
| 2985 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2986 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2987 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2988 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2989 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 2990 | if intentsStr is None: |
| 2991 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2992 | # Convert to a dictionary |
| 2993 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2994 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2995 | for intent in intents: |
| 2996 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2997 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2998 | except TypeError: |
| 2999 | main.log.exception( self.name + ": Object not as expected" ) |
| 3000 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 3001 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3002 | main.log.error( self.name + ": EOF exception found" ) |
| 3003 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3004 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3005 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3006 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3007 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 3008 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3009 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3010 | """ |
| 3011 | Determine the number of flow rules for the given device id that are |
| 3012 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3013 | Params: |
| 3014 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3015 | """ |
| 3016 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 3017 | if core: |
| 3018 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 3019 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3020 | else: |
| 3021 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 3022 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3023 | handle = self.lineCount( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3024 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3025 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3026 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3027 | except AssertionError: |
| 3028 | main.log.exception( "" ) |
| 3029 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3030 | except pexpect.EOF: |
| 3031 | main.log.error( self.name + ": EOF exception found" ) |
| 3032 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3033 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3034 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 3035 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3036 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 3037 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3038 | def groupAddedCount( self, deviceId, core=False ): |
| 3039 | """ |
| 3040 | Determine the number of group rules for the given device id that are |
| 3041 | in the added state |
| 3042 | Params: |
| 3043 | core: if True, only return the number of core groups added |
| 3044 | """ |
| 3045 | try: |
| 3046 | if core: |
| 3047 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3048 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 3049 | else: |
| 3050 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 3051 | "grep 'state=ADDED' | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3052 | handle = self.lineCount( cmdStr ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3053 | assert handle is not None, "Error in sendline" |
| 3054 | assert "Command not found:" not in handle, handle |
| 3055 | return handle |
| 3056 | except AssertionError: |
| 3057 | main.log.exception( "" ) |
| 3058 | return None |
| 3059 | except pexpect.EOF: |
| 3060 | main.log.error( self.name + ": EOF exception found" ) |
| 3061 | main.log.error( self.name + ": " + self.handle.before ) |
| 3062 | main.cleanAndExit() |
| 3063 | except Exception: |
| 3064 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3065 | main.cleanAndExit() |
| 3066 | |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 3067 | def addStaticRoute( self, subnet, intf): |
| 3068 | """ |
| 3069 | Adds a static route to onos. |
| 3070 | Params: |
| 3071 | subnet: The subnet reaching through this route |
| 3072 | intf: The interface this route is reachable through |
| 3073 | """ |
| 3074 | try: |
| 3075 | cmdStr = "route-add " + subnet + " " + intf |
| 3076 | handle = self.sendline( cmdStr ) |
| 3077 | assert handle is not None, "Error in sendline" |
| 3078 | assert "Command not found:" not in handle, handle |
| 3079 | return handle |
| 3080 | except AssertionError: |
| 3081 | main.log.exception( "" ) |
| 3082 | return None |
| 3083 | except pexpect.EOF: |
| 3084 | main.log.error( self.name + ": EOF exception found" ) |
| 3085 | main.log.error( self.name + ": " + self.handle.before ) |
| 3086 | main.cleanAndExit() |
| 3087 | except Exception: |
| 3088 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3089 | main.cleanAndExit() |
| 3090 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3091 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 3092 | """ |
| 3093 | Description: |
| 3094 | Check whether the number of groups for the given device id that |
| 3095 | are in ADDED state is bigger than minGroupCount. |
| 3096 | Required: |
| 3097 | * deviceId: device id to check the number of added group rules |
| 3098 | Optional: |
| 3099 | * minGroupCount: the number of groups to compare |
| 3100 | * core: if True, only check the number of core groups added |
| 3101 | * comparison: if 0, compare with greater than minFlowCount |
| 3102 | * if 1, compare with equal to minFlowCount |
| 3103 | Return: |
| 3104 | Returns the number of groups if it is bigger than minGroupCount, |
| 3105 | returns main.FALSE otherwise. |
| 3106 | """ |
| 3107 | count = self.groupAddedCount( deviceId, core ) |
| 3108 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3109 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3110 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 3111 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3112 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3113 | """ |
| 3114 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3115 | deviceId: Id of the device from which we retrieve groups |
| 3116 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3117 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3118 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 3119 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 3120 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 3121 | assert handle is not None, "Error in sendline" |
| 3122 | assert "Command not found:" not in handle, handle |
| 3123 | return handle |
| 3124 | except AssertionError: |
| 3125 | main.log.exception( "" ) |
| 3126 | return None |
| 3127 | except TypeError: |
| 3128 | main.log.exception( self.name + ": Object not as expected" ) |
| 3129 | return None |
| 3130 | except pexpect.EOF: |
| 3131 | main.log.error( self.name + ": EOF exception found" ) |
| 3132 | main.log.error( self.name + ": " + self.handle.before ) |
| 3133 | main.cleanAndExit() |
| 3134 | except Exception: |
| 3135 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3136 | main.cleanAndExit() |
| 3137 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3138 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3139 | """ |
| 3140 | Description: |
| 3141 | Check whether the number of flow rules for the given device id that |
| 3142 | are in ADDED state is bigger than minFlowCount. |
| 3143 | Required: |
| 3144 | * deviceId: device id to check the number of added flow rules |
| 3145 | Optional: |
| 3146 | * minFlowCount: the number of flow rules to compare |
| 3147 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 3148 | * comparison: if 0, compare with greater than minFlowCount |
| 3149 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 3150 | Return: |
| 3151 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 3152 | returns main.FALSE otherwise. |
| 3153 | """ |
| 3154 | count = self.flowAddedCount( deviceId, core ) |
| 3155 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 3156 | main.log.debug( "found {} flows".format( count ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3157 | 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] | 3158 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3159 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3160 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3161 | Use 'devices' function to obtain list of all devices |
| 3162 | and parse the result to obtain a list of all device |
| 3163 | id's. Returns this list. Returns empty list if no |
| 3164 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3165 | List is ordered sequentially |
| 3166 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3167 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3168 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3169 | the ids. By obtaining the list of device ids on the fly, |
| 3170 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3171 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3172 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3173 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3174 | devicesStr = self.devices( jsonFormat=False ) |
| 3175 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3176 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3177 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3178 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3179 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3180 | |
| 3181 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3182 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3183 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3184 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3185 | # Split list further into arguments before and after string |
| 3186 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3187 | # append to idList |
| 3188 | for arg in tempList: |
| 3189 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3190 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3191 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3192 | except TypeError: |
| 3193 | main.log.exception( self.name + ": Object not as expected" ) |
| 3194 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3195 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3196 | main.log.error( self.name + ": EOF exception found" ) |
| 3197 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3198 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3199 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3200 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3201 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3202 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3203 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3204 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3205 | Uses 'nodes' function to obtain list of all nodes |
| 3206 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3207 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3208 | Returns: |
| 3209 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3210 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3211 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3212 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3213 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3214 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3215 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3216 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3217 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3218 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3219 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3220 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3221 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3222 | except ( TypeError, ValueError ): |
| 3223 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3224 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3225 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3226 | main.log.error( self.name + ": EOF exception found" ) |
| 3227 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3228 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3229 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3230 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3231 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3232 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3233 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3234 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3235 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3236 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3237 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3238 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3239 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3240 | return None |
| 3241 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3242 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3243 | rawDevices = self.devices() |
| 3244 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3245 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3246 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3247 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3248 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3249 | return device |
| 3250 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3251 | except ( TypeError, ValueError ): |
| 3252 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3253 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3254 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3255 | main.log.error( self.name + ": EOF exception found" ) |
| 3256 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3257 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3258 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3259 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3260 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3261 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3262 | def getTopology( self, topologyOutput ): |
| 3263 | """ |
| 3264 | Definition: |
| 3265 | Loads a json topology output |
| 3266 | Return: |
| 3267 | topology = current ONOS topology |
| 3268 | """ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3269 | try: |
| 3270 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3271 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3272 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3273 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3274 | except ( TypeError, ValueError ): |
| 3275 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3276 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3277 | except pexpect.EOF: |
| 3278 | main.log.error( self.name + ": EOF exception found" ) |
| 3279 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3280 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3281 | except Exception: |
| 3282 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3283 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3284 | |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3285 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, numoSCCs=1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3286 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3287 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3288 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3289 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3290 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3291 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3292 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3293 | numoctrl = expected number of controllers |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3294 | numoSCCs = Number of expected SCCs |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3295 | logLevel = level to log to. |
| 3296 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3297 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3298 | Returns: main.TRUE if the number of switches and links are correct, |
| 3299 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3300 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3301 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3302 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3303 | summary = self.summary() |
| 3304 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3305 | except ( TypeError, ValueError ): |
| 3306 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3307 | return main.ERROR |
| 3308 | try: |
| 3309 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3310 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3311 | return main.ERROR |
| 3312 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3313 | # Is the number of switches is what we expected |
| 3314 | devices = topology.get( 'devices', False ) |
| 3315 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3316 | nodes = summary.get( 'nodes', False ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3317 | SCCs = summary.get( 'SCC(s)', False ) |
| 3318 | if devices is False or links is False or nodes is False or SCCs is False: |
| 3319 | main.log.warn( "Issues parsing topology and summary output" ) |
| 3320 | main.log.debug( topology ) |
| 3321 | main.log.debug( summary ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3322 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3323 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3324 | if not switchCheck: |
| 3325 | main.log.debug( "switch Check Failed" ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3326 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3327 | if not linkCheck: |
| 3328 | main.log.debug( "link Check Failed" ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3329 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3330 | if not nodeCheck: |
| 3331 | main.log.debug( "node Check Failed" ) |
| 3332 | SCCsCheck = ( int( SCCs ) == int( numoSCCs ) ) or int( numoSCCs ) == -1 |
| 3333 | if not SCCsCheck: |
| 3334 | main.log.debug( "SCCs Check Failed" ) |
| 3335 | if switchCheck and linkCheck and nodeCheck and SCCsCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3336 | # We expected the correct numbers |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3337 | output = output + "The number of links, switches, nodes, and SCCs match "\ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3338 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3339 | result = main.TRUE |
| 3340 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3341 | output = output + \ |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 3342 | "The number of links, switches, nodes, and SCCs does not match " + \ |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3343 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3344 | result = main.FALSE |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3345 | output = output + "\n ONOS sees %i devices " % int( devices ) |
| 3346 | output = output + "(%i expected) " % int( numoswitch ) |
| 3347 | if int( numolink ) >= 0: |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3348 | output = output + "and %i links " % int( links ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3349 | output = output + "(%i expected) " % int( numolink ) |
| 3350 | if int( numoctrl ) >= 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3351 | output = output + "and %i controllers " % int( nodes ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3352 | output = output + "(%i expected) " % int( numoctrl ) |
| 3353 | if int( numoSCCs ) >= 0: |
| 3354 | output = output + "and %i SCCs " % int( SCCs ) |
| 3355 | output = output + "(%i expected)" % int( numoSCCs ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3356 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3357 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3358 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3359 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3360 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3361 | main.log.info( output ) |
Jon Hall | 3957026 | 2020-11-17 12:18:19 -0800 | [diff] [blame] | 3362 | main.TOPOOUTPUT = output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3363 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3364 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3365 | main.log.error( self.name + ": EOF exception found" ) |
| 3366 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3367 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3368 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3369 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3370 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3371 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3372 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3373 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3374 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3375 | deviceId must be the id of a device as seen in the onos devices command |
| 3376 | 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] | 3377 | role must be either master, standby, or none |
| 3378 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3379 | Returns: |
| 3380 | main.TRUE or main.FALSE based on argument verification and |
| 3381 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3382 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3383 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3384 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3385 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3386 | cmdStr = "device-role " +\ |
| 3387 | str( deviceId ) + " " +\ |
| 3388 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3389 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3390 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3391 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3392 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3393 | if re.search( "Error", handle ): |
| 3394 | # end color output to escape any colours |
| 3395 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3396 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3397 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3398 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3399 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3400 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3401 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3402 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3403 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3404 | except AssertionError: |
| 3405 | main.log.exception( "" ) |
| 3406 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3407 | except TypeError: |
| 3408 | main.log.exception( self.name + ": Object not as expected" ) |
| 3409 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3410 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3411 | main.log.error( self.name + ": EOF exception found" ) |
| 3412 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3413 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3414 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3415 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3416 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3417 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3418 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3419 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3420 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3421 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3422 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3423 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3424 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3425 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3426 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3427 | cmdStr += " -j" |
| 3428 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3429 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3430 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3431 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3432 | except AssertionError: |
| 3433 | main.log.exception( "" ) |
| 3434 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3435 | except TypeError: |
| 3436 | main.log.exception( self.name + ": Object not as expected" ) |
| 3437 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3438 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3439 | main.log.error( self.name + ": EOF exception found" ) |
| 3440 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3441 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3442 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3443 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3444 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3445 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3446 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3447 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3448 | CLI command to get the current leader for the Election test application |
| 3449 | NOTE: Requires installation of the onos-app-election feature |
| 3450 | Returns: Node IP of the leader if one exists |
| 3451 | None if none exists |
| 3452 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3453 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3454 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3455 | cmdStr = "election-test-leader" |
| 3456 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3457 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3458 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3459 | # Leader |
| 3460 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3461 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3462 | nodeSearch = re.search( leaderPattern, response ) |
| 3463 | if nodeSearch: |
| 3464 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3465 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3466 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3467 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3468 | # no leader |
| 3469 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3470 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3471 | nullSearch = re.search( nullPattern, response ) |
| 3472 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3473 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3474 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3475 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3476 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3477 | main.log.error( self.name + ": Error in electionTestLeader on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3478 | ": " + "unexpected response" ) |
| 3479 | main.log.error( repr( response ) ) |
| 3480 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3481 | except AssertionError: |
| 3482 | main.log.exception( "" ) |
| 3483 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3484 | except TypeError: |
| 3485 | main.log.exception( self.name + ": Object not as expected" ) |
| 3486 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3487 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3488 | main.log.error( self.name + ": EOF exception found" ) |
| 3489 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3490 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3491 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3492 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3493 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3494 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3495 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3496 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3497 | CLI command to run for leadership of the Election test application. |
| 3498 | NOTE: Requires installation of the onos-app-election feature |
| 3499 | Returns: Main.TRUE on success |
| 3500 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3501 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3502 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3503 | cmdStr = "election-test-run" |
| 3504 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3505 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3506 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3507 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3508 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3509 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3510 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3511 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3512 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3513 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3514 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3515 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3516 | main.log.error( self.name + ": Error in electionTestRun on " + self.name + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3517 | ": " + "unexpected response" ) |
| 3518 | main.log.error( repr( response ) ) |
| 3519 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3520 | except AssertionError: |
| 3521 | main.log.exception( "" ) |
| 3522 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3523 | except TypeError: |
| 3524 | main.log.exception( self.name + ": Object not as expected" ) |
| 3525 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3526 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3527 | main.log.error( self.name + ": EOF exception found" ) |
| 3528 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3529 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3530 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3531 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3532 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3533 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3534 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3535 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3536 | * CLI command to withdraw the local node from leadership election for |
| 3537 | * the Election test application. |
| 3538 | #NOTE: Requires installation of the onos-app-election feature |
| 3539 | Returns: Main.TRUE on success |
| 3540 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3541 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3542 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3543 | cmdStr = "election-test-withdraw" |
| 3544 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3545 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3546 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3547 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3548 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3549 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3550 | if re.search( successPattern, response ): |
| 3551 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3552 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3553 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3554 | # error |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3555 | main.log.error( self.name + ": Error in electionTestWithdraw on " + |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3556 | self.name + ": " + "unexpected response" ) |
| 3557 | main.log.error( repr( response ) ) |
| 3558 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3559 | except AssertionError: |
| 3560 | main.log.exception( "" ) |
| 3561 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3562 | except TypeError: |
| 3563 | main.log.exception( self.name + ": Object not as expected" ) |
| 3564 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3565 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3566 | main.log.error( self.name + ": EOF exception found" ) |
| 3567 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3568 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3569 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3570 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3571 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3572 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3573 | def getDevicePortsEnabledCount( self, dpid ): |
| 3574 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3575 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3576 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3577 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3578 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3579 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3580 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3581 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3582 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3583 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3584 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3585 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3586 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3587 | except AssertionError: |
| 3588 | main.log.exception( "" ) |
| 3589 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3590 | except TypeError: |
| 3591 | main.log.exception( self.name + ": Object not as expected" ) |
| 3592 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3593 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3594 | main.log.error( self.name + ": EOF exception found" ) |
| 3595 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3596 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3597 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3598 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3599 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3600 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3601 | def getDeviceLinksActiveCount( self, dpid ): |
| 3602 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3603 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3604 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3605 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3606 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3607 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 3608 | output = self.lineCount( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3609 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3610 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3611 | if re.search( "No such device", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3612 | main.log.error( self.name + ": Error in getting ports " ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3613 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3614 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3615 | except AssertionError: |
| 3616 | main.log.exception( "" ) |
| 3617 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3618 | except TypeError: |
| 3619 | main.log.exception( self.name + ": Object not as expected" ) |
| 3620 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3621 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3622 | main.log.error( self.name + ": EOF exception found" ) |
| 3623 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3624 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3625 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3626 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3627 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3628 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3629 | def getAllIntentIds( self ): |
| 3630 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3631 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3632 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3633 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3634 | cmdStr = "onos:intents | grep id=" |
| 3635 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3636 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3637 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3638 | if re.search( "Error", output ): |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3639 | main.log.error( self.name + ": Error in getting ports" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3640 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3641 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3642 | except AssertionError: |
| 3643 | main.log.exception( "" ) |
| 3644 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3645 | except TypeError: |
| 3646 | main.log.exception( self.name + ": Object not as expected" ) |
| 3647 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3648 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3649 | main.log.error( self.name + ": EOF exception found" ) |
| 3650 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3651 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3652 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3653 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3654 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3655 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3656 | def intentSummary( self ): |
| 3657 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3658 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3659 | """ |
| 3660 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3661 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3662 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3663 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3664 | states.append( intent.get( 'state', None ) ) |
| 3665 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3666 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3667 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3668 | except ( TypeError, ValueError ): |
| 3669 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3670 | return None |
| 3671 | except pexpect.EOF: |
| 3672 | main.log.error( self.name + ": EOF exception found" ) |
| 3673 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3674 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3675 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3676 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3677 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3678 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3679 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3680 | """ |
| 3681 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3682 | Optional argument: |
| 3683 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3684 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3685 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3686 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3687 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3688 | cmdStr += " -j" |
| 3689 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3690 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3691 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3692 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3693 | except AssertionError: |
| 3694 | main.log.exception( "" ) |
| 3695 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3696 | except TypeError: |
| 3697 | main.log.exception( self.name + ": Object not as expected" ) |
| 3698 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3699 | except pexpect.EOF: |
| 3700 | main.log.error( self.name + ": EOF exception found" ) |
| 3701 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3702 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3703 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3704 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3705 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3706 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3707 | def leaderCandidates( self, jsonFormat=True ): |
| 3708 | """ |
| 3709 | Returns the output of the leaders -c command. |
| 3710 | Optional argument: |
| 3711 | * jsonFormat - boolean indicating if you want output in json |
| 3712 | """ |
| 3713 | try: |
| 3714 | cmdStr = "onos:leaders -c" |
| 3715 | if jsonFormat: |
| 3716 | cmdStr += " -j" |
| 3717 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3718 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3719 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3720 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3721 | except AssertionError: |
| 3722 | main.log.exception( "" ) |
| 3723 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3724 | except TypeError: |
| 3725 | main.log.exception( self.name + ": Object not as expected" ) |
| 3726 | return None |
| 3727 | except pexpect.EOF: |
| 3728 | main.log.error( self.name + ": EOF exception found" ) |
| 3729 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3730 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3731 | except Exception: |
| 3732 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3733 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3734 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3735 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3736 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3737 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3738 | topic parameter and an empty list if the topic doesn't exist |
| 3739 | If no leader is elected leader in the returned list will be "none" |
| 3740 | Returns None if there is a type error processing the json object |
| 3741 | """ |
| 3742 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3743 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3744 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3745 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3746 | assert "Command not found:" not in rawOutput, rawOutput |
| 3747 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3748 | results = [] |
| 3749 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3750 | if dict[ "topic" ] == topic: |
| 3751 | leader = dict[ "leader" ] |
| 3752 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3753 | results.append( leader ) |
| 3754 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3755 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3756 | except AssertionError: |
| 3757 | main.log.exception( "" ) |
| 3758 | return None |
| 3759 | except ( TypeError, ValueError ): |
| 3760 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 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 | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3770 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3771 | """ |
| 3772 | Returns the output of the intent Pending map. |
| 3773 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3774 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3775 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3776 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3777 | cmdStr += " -j" |
| 3778 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3779 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3780 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3781 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3782 | except AssertionError: |
| 3783 | main.log.exception( "" ) |
| 3784 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3785 | except TypeError: |
| 3786 | main.log.exception( self.name + ": Object not as expected" ) |
| 3787 | return None |
| 3788 | except pexpect.EOF: |
| 3789 | main.log.error( self.name + ": EOF exception found" ) |
| 3790 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3791 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3792 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3793 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3794 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3795 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3796 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3797 | """ |
| 3798 | Returns the output of the raft partitions command for ONOS. |
| 3799 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3800 | # Sample JSON |
| 3801 | # { |
| 3802 | # "leader": "tcp://10.128.30.11:7238", |
| 3803 | # "members": [ |
| 3804 | # "tcp://10.128.30.11:7238", |
| 3805 | # "tcp://10.128.30.17:7238", |
| 3806 | # "tcp://10.128.30.13:7238", |
| 3807 | # ], |
| 3808 | # "name": "p1", |
| 3809 | # "term": 3 |
| 3810 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3811 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3812 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3813 | if candidates: |
| 3814 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3815 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3816 | cmdStr += " -j" |
| 3817 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3818 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3819 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3820 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3821 | except AssertionError: |
| 3822 | main.log.exception( "" ) |
| 3823 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3824 | except TypeError: |
| 3825 | main.log.exception( self.name + ": Object not as expected" ) |
| 3826 | return None |
| 3827 | except pexpect.EOF: |
| 3828 | main.log.error( self.name + ": EOF exception found" ) |
| 3829 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3830 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3831 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3832 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3833 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3834 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3835 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3836 | """ |
| 3837 | Returns the output of the apps command for ONOS. This command lists |
| 3838 | information about installed ONOS applications |
| 3839 | """ |
| 3840 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3841 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3842 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3843 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3844 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3845 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3846 | cmdStr = "onos:apps" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3847 | expectJson = False |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3848 | if summary: |
| 3849 | cmdStr += " -s" |
| 3850 | if active: |
| 3851 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3852 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3853 | cmdStr += " -j" |
Jon Hall | f03ae76 | 2019-01-22 13:25:27 -0800 | [diff] [blame] | 3854 | expectJson = True |
| 3855 | output = self.sendline( cmdStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3856 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3857 | assert "Command not found:" not in output, output |
| 3858 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3859 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3860 | # FIXME: look at specific exceptions/Errors |
| 3861 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3862 | main.log.exception( self.name + ": Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3863 | return None |
| 3864 | except TypeError: |
| 3865 | main.log.exception( self.name + ": Object not as expected" ) |
| 3866 | return None |
| 3867 | except pexpect.EOF: |
| 3868 | main.log.error( self.name + ": EOF exception found" ) |
| 3869 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3870 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3871 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3872 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3873 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3874 | |
You Wang | cdc51fe | 2018-08-12 17:14:56 -0700 | [diff] [blame] | 3875 | def appStatus( self, appName ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3876 | """ |
| 3877 | Uses the onos:apps cli command to return the status of an application. |
| 3878 | Returns: |
| 3879 | "ACTIVE" - If app is installed and activated |
| 3880 | "INSTALLED" - If app is installed and deactivated |
| 3881 | "UNINSTALLED" - If app is not installed |
| 3882 | None - on error |
| 3883 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3884 | try: |
| 3885 | if not isinstance( appName, types.StringType ): |
| 3886 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3887 | " a string" ) |
| 3888 | return None |
| 3889 | output = self.apps( jsonFormat=True ) |
| 3890 | appsJson = json.loads( output ) |
| 3891 | state = None |
| 3892 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3893 | if appName == app.get( 'name' ): |
| 3894 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3895 | break |
| 3896 | if state == "ACTIVE" or state == "INSTALLED": |
| 3897 | return state |
| 3898 | elif state is None: |
You Wang | 0d9f2c0 | 2018-08-10 14:56:32 -0700 | [diff] [blame] | 3899 | main.log.warn( "{} app not found".format( appName ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3900 | return "UNINSTALLED" |
| 3901 | elif state: |
| 3902 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3903 | str( state ) ) |
| 3904 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3905 | except ( TypeError, ValueError ): |
| 3906 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3907 | return None |
| 3908 | except pexpect.EOF: |
| 3909 | main.log.error( self.name + ": EOF exception found" ) |
| 3910 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3911 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3912 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3913 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3914 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3915 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3916 | def app( self, appName, option ): |
| 3917 | """ |
| 3918 | Interacts with the app command for ONOS. This command manages |
| 3919 | application inventory. |
| 3920 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3921 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3922 | # Validate argument types |
| 3923 | valid = True |
| 3924 | if not isinstance( appName, types.StringType ): |
| 3925 | main.log.error( self.name + ".app(): appName must be a " + |
| 3926 | "string" ) |
| 3927 | valid = False |
| 3928 | if not isinstance( option, types.StringType ): |
| 3929 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3930 | valid = False |
| 3931 | if not valid: |
| 3932 | return main.FALSE |
| 3933 | # Validate Option |
| 3934 | option = option.lower() |
| 3935 | # NOTE: Install may become a valid option |
| 3936 | if option == "activate": |
| 3937 | pass |
| 3938 | elif option == "deactivate": |
| 3939 | pass |
| 3940 | elif option == "uninstall": |
| 3941 | pass |
| 3942 | else: |
| 3943 | # Invalid option |
| 3944 | main.log.error( "The ONOS app command argument only takes " + |
| 3945 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3946 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3947 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3948 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3949 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3950 | assert output is not None, "Error in sendline" |
| 3951 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3952 | if "Error executing command" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3953 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3954 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3955 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3956 | elif "No such application" in output: |
| 3957 | main.log.error( "The application '" + appName + |
| 3958 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3959 | return main.FALSE |
| 3960 | elif "Command not found:" in output: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 3961 | main.log.error( self.name + ": Error in processing onos:app command: " + |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3962 | str( output ) ) |
| 3963 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3964 | elif "Unsupported command:" in output: |
| 3965 | main.log.error( "Incorrect command given to 'app': " + |
| 3966 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3967 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3968 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3969 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3970 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3971 | except AssertionError: |
| 3972 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3973 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3974 | except TypeError: |
| 3975 | main.log.exception( self.name + ": Object not as expected" ) |
| 3976 | return main.ERROR |
| 3977 | except pexpect.EOF: |
| 3978 | main.log.error( self.name + ": EOF exception found" ) |
| 3979 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3980 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3981 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3982 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3983 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3984 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3985 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3986 | """ |
| 3987 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3988 | appName is the hierarchical app name, not the feature name |
| 3989 | If check is True, method will check the status of the app after the |
| 3990 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3991 | Returns main.TRUE if the command was successfully sent |
| 3992 | main.FALSE if the cli responded with an error or given |
| 3993 | incorrect input |
| 3994 | """ |
| 3995 | try: |
| 3996 | if not isinstance( appName, types.StringType ): |
| 3997 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3998 | " a string" ) |
| 3999 | return main.FALSE |
| 4000 | status = self.appStatus( appName ) |
| 4001 | if status == "INSTALLED": |
| 4002 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4003 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4004 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4005 | status = self.appStatus( appName ) |
| 4006 | if status == "ACTIVE": |
| 4007 | return main.TRUE |
| 4008 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 4009 | main.log.debug( "The state of application " + |
| 4010 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4011 | time.sleep( 1 ) |
| 4012 | return main.FALSE |
| 4013 | else: # not 'check' or command didn't succeed |
| 4014 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4015 | elif status == "ACTIVE": |
| 4016 | return main.TRUE |
| 4017 | elif status == "UNINSTALLED": |
| 4018 | main.log.error( self.name + ": Tried to activate the " + |
| 4019 | "application '" + appName + "' which is not " + |
| 4020 | "installed." ) |
| 4021 | else: |
| 4022 | main.log.error( "Unexpected return value from appStatus: " + |
| 4023 | str( status ) ) |
| 4024 | return main.ERROR |
| 4025 | except TypeError: |
| 4026 | main.log.exception( self.name + ": Object not as expected" ) |
| 4027 | return main.ERROR |
| 4028 | except pexpect.EOF: |
| 4029 | main.log.error( self.name + ": EOF exception found" ) |
| 4030 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4031 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4032 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4033 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4034 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4035 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4036 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4037 | """ |
| 4038 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4039 | appName is the hierarchical app name, not the feature name |
| 4040 | If check is True, method will check the status of the app after the |
| 4041 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4042 | Returns main.TRUE if the command was successfully sent |
| 4043 | main.FALSE if the cli responded with an error or given |
| 4044 | incorrect input |
| 4045 | """ |
| 4046 | try: |
| 4047 | if not isinstance( appName, types.StringType ): |
| 4048 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 4049 | "be a string" ) |
| 4050 | return main.FALSE |
| 4051 | status = self.appStatus( appName ) |
| 4052 | if status == "INSTALLED": |
| 4053 | return main.TRUE |
| 4054 | elif status == "ACTIVE": |
| 4055 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4056 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4057 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4058 | status = self.appStatus( appName ) |
| 4059 | if status == "INSTALLED": |
| 4060 | return main.TRUE |
| 4061 | else: |
| 4062 | time.sleep( 1 ) |
| 4063 | return main.FALSE |
| 4064 | else: # not check or command didn't succeed |
| 4065 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4066 | elif status == "UNINSTALLED": |
| 4067 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 4068 | "application '" + appName + "' which is not " + |
| 4069 | "installed." ) |
| 4070 | return main.TRUE |
| 4071 | else: |
| 4072 | main.log.error( "Unexpected return value from appStatus: " + |
| 4073 | str( status ) ) |
| 4074 | return main.ERROR |
| 4075 | except TypeError: |
| 4076 | main.log.exception( self.name + ": Object not as expected" ) |
| 4077 | return main.ERROR |
| 4078 | except pexpect.EOF: |
| 4079 | main.log.error( self.name + ": EOF exception found" ) |
| 4080 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4081 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4082 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4083 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4084 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4085 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4086 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4087 | """ |
| 4088 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4089 | appName is the hierarchical app name, not the feature name |
| 4090 | If check is True, method will check the status of the app after the |
| 4091 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4092 | Returns main.TRUE if the command was successfully sent |
| 4093 | main.FALSE if the cli responded with an error or given |
| 4094 | incorrect input |
| 4095 | """ |
| 4096 | # TODO: check with Thomas about the state machine for apps |
| 4097 | try: |
| 4098 | if not isinstance( appName, types.StringType ): |
| 4099 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 4100 | "be a string" ) |
| 4101 | return main.FALSE |
| 4102 | status = self.appStatus( appName ) |
| 4103 | if status == "INSTALLED": |
| 4104 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4105 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4106 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4107 | status = self.appStatus( appName ) |
| 4108 | if status == "UNINSTALLED": |
| 4109 | return main.TRUE |
| 4110 | else: |
| 4111 | time.sleep( 1 ) |
| 4112 | return main.FALSE |
| 4113 | else: # not check or command didn't succeed |
| 4114 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4115 | elif status == "ACTIVE": |
| 4116 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 4117 | "application '" + appName + "' which is " + |
| 4118 | "currently active." ) |
| 4119 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4120 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4121 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4122 | status = self.appStatus( appName ) |
| 4123 | if status == "UNINSTALLED": |
| 4124 | return main.TRUE |
| 4125 | else: |
| 4126 | time.sleep( 1 ) |
| 4127 | return main.FALSE |
| 4128 | else: # not check or command didn't succeed |
| 4129 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4130 | elif status == "UNINSTALLED": |
| 4131 | return main.TRUE |
| 4132 | else: |
| 4133 | main.log.error( "Unexpected return value from appStatus: " + |
| 4134 | str( status ) ) |
| 4135 | return main.ERROR |
| 4136 | except TypeError: |
| 4137 | main.log.exception( self.name + ": Object not as expected" ) |
| 4138 | return main.ERROR |
| 4139 | except pexpect.EOF: |
| 4140 | main.log.error( self.name + ": EOF exception found" ) |
| 4141 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4142 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4143 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 4144 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4145 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4146 | |
| 4147 | def appIDs( self, jsonFormat=True ): |
| 4148 | """ |
| 4149 | Show the mappings between app id and app names given by the 'app-ids' |
| 4150 | cli command |
| 4151 | """ |
| 4152 | try: |
| 4153 | cmdStr = "app-ids" |
| 4154 | if jsonFormat: |
| 4155 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4156 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4157 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4158 | assert "Command not found:" not in output, output |
| 4159 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 4160 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4161 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4162 | main.log.exception( self.name + ": Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4163 | return None |
| 4164 | except TypeError: |
| 4165 | main.log.exception( self.name + ": Object not as expected" ) |
| 4166 | return None |
| 4167 | except pexpect.EOF: |
| 4168 | main.log.error( self.name + ": EOF exception found" ) |
| 4169 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4170 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4171 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4172 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4173 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4174 | |
| 4175 | def appToIDCheck( self ): |
| 4176 | """ |
| 4177 | This method will check that each application's ID listed in 'apps' is |
| 4178 | the same as the ID listed in 'app-ids'. The check will also check that |
| 4179 | there are no duplicate IDs issued. Note that an app ID should be |
| 4180 | a globaly unique numerical identifier for app/app-like features. Once |
| 4181 | an ID is registered, the ID is never freed up so that if an app is |
| 4182 | reinstalled it will have the same ID. |
| 4183 | |
| 4184 | Returns: main.TRUE if the check passes and |
| 4185 | main.FALSE if the check fails or |
| 4186 | main.ERROR if there is some error in processing the test |
| 4187 | """ |
| 4188 | try: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4189 | # Grab IDs |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4190 | rawJson = self.appIDs( jsonFormat=True ) |
| 4191 | if rawJson: |
| 4192 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4193 | else: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4194 | main.log.error( "app-ids returned nothing: " + repr( rawJson ) ) |
| 4195 | return main.FALSE |
| 4196 | |
| 4197 | # Grab Apps |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4198 | rawJson = self.apps( jsonFormat=True ) |
| 4199 | if rawJson: |
| 4200 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4201 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4202 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4203 | return main.FALSE |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4204 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4205 | result = main.TRUE |
| 4206 | for app in apps: |
| 4207 | appID = app.get( 'id' ) |
| 4208 | if appID is None: |
| 4209 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4210 | result = main.FALSE |
| 4211 | appName = app.get( 'name' ) |
| 4212 | if appName is None: |
| 4213 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4214 | result = main.FALSE |
| 4215 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4216 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4217 | if not current: # if ids doesn't have this id |
| 4218 | result = main.FALSE |
| 4219 | main.log.error( "'app-ids' does not have the ID for " + |
| 4220 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4221 | main.log.debug( "apps command returned: " + str( app ) + |
| 4222 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4223 | elif len( current ) > 1: |
| 4224 | # there is more than one app with this ID |
| 4225 | result = main.FALSE |
| 4226 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4227 | elif not current[ 0 ][ 'name' ] == appName: |
| 4228 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4229 | result = main.FALSE |
| 4230 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4231 | " registered under id:" + str( appID ) + |
| 4232 | " but 'apps' has " + str( appName ) ) |
| 4233 | else: |
| 4234 | pass # id and name match! |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4235 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4236 | # now make sure that app-ids has no duplicates |
| 4237 | idsList = [] |
| 4238 | namesList = [] |
| 4239 | for item in ids: |
| 4240 | idsList.append( item[ 'id' ] ) |
| 4241 | namesList.append( item[ 'name' ] ) |
| 4242 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4243 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4244 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4245 | + json.dumps( ids, |
| 4246 | sort_keys=True, |
| 4247 | indent=4, |
| 4248 | separators=( ',', ': ' ) ) ) |
| 4249 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4250 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4251 | except ( TypeError, ValueError ): |
| 4252 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4253 | return main.ERROR |
| 4254 | except pexpect.EOF: |
| 4255 | main.log.error( self.name + ": EOF exception found" ) |
| 4256 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4257 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4258 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4259 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4260 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4261 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4262 | def getCfg( self, component=None, propName=None, short=False, |
| 4263 | jsonFormat=True ): |
| 4264 | """ |
| 4265 | Get configuration settings from onos cli |
| 4266 | Optional arguments: |
| 4267 | component - Optionally only list configurations for a specific |
| 4268 | component. If None, all components with configurations |
| 4269 | are displayed. Case Sensitive string. |
| 4270 | propName - If component is specified, propName option will show |
| 4271 | only this specific configuration from that component. |
| 4272 | Case Sensitive string. |
| 4273 | jsonFormat - Returns output as json. Note that this will override |
| 4274 | the short option |
| 4275 | short - Short, less verbose, version of configurations. |
| 4276 | This is overridden by the json option |
| 4277 | returns: |
| 4278 | Output from cli as a string or None on error |
| 4279 | """ |
| 4280 | try: |
| 4281 | baseStr = "cfg" |
| 4282 | cmdStr = " get" |
| 4283 | componentStr = "" |
| 4284 | if component: |
| 4285 | componentStr += " " + component |
| 4286 | if propName: |
| 4287 | componentStr += " " + propName |
| 4288 | if jsonFormat: |
| 4289 | baseStr += " -j" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4290 | expectJson = True |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4291 | elif short: |
| 4292 | baseStr += " -s" |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4293 | expectJson = False |
| 4294 | output = self.sendline( baseStr + cmdStr + componentStr, expectJson=expectJson ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4295 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4296 | assert "Command not found:" not in output, output |
| 4297 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4298 | return output |
| 4299 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4300 | main.log.exception( self.name + ": Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4301 | return None |
| 4302 | except TypeError: |
| 4303 | main.log.exception( self.name + ": Object not as expected" ) |
| 4304 | return None |
| 4305 | except pexpect.EOF: |
| 4306 | main.log.error( self.name + ": EOF exception found" ) |
| 4307 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4308 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4309 | except Exception: |
| 4310 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4311 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4312 | |
| 4313 | def setCfg( self, component, propName, value=None, check=True ): |
| 4314 | """ |
| 4315 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4316 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4317 | component - The case sensitive name of the component whose |
| 4318 | property is to be set |
| 4319 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4320 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4321 | 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] | 4322 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4323 | check - Boolean, Check whether the option was successfully set this |
| 4324 | only applies when a value is given. |
| 4325 | returns: |
| 4326 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4327 | will return main.TRUE unless there is an error |
| 4328 | """ |
| 4329 | try: |
| 4330 | baseStr = "cfg" |
| 4331 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4332 | if value is not None: |
| 4333 | cmdStr += " " + str( value ) |
| 4334 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4335 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4336 | assert "Command not found:" not in output, output |
| 4337 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4338 | if value and check: |
| 4339 | results = self.getCfg( component=str( component ), |
| 4340 | propName=str( propName ), |
| 4341 | jsonFormat=True ) |
| 4342 | # Check if current value is what we just set |
| 4343 | try: |
| 4344 | jsonOutput = json.loads( results ) |
| 4345 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4346 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4347 | main.log.exception( "Error parsing cfg output" ) |
| 4348 | main.log.error( "output:" + repr( results ) ) |
| 4349 | return main.FALSE |
| 4350 | if current == str( value ): |
| 4351 | return main.TRUE |
| 4352 | return main.FALSE |
| 4353 | return main.TRUE |
| 4354 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4355 | main.log.exception( self.name + ": Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4356 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4357 | except ( TypeError, ValueError ): |
| 4358 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4359 | return main.FALSE |
| 4360 | except pexpect.EOF: |
| 4361 | main.log.error( self.name + ": EOF exception found" ) |
| 4362 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4363 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4364 | except Exception: |
| 4365 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4366 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4367 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4368 | def distPrimitivesSend( self, cmd ): |
| 4369 | """ |
| 4370 | Function to handle sending cli commands for the distributed primitives test app |
| 4371 | |
| 4372 | This command will catch some exceptions and retry the command on some |
| 4373 | specific store exceptions. |
| 4374 | |
| 4375 | Required arguments: |
| 4376 | cmd - The command to send to the cli |
| 4377 | returns: |
| 4378 | string containing the cli output |
| 4379 | None on Error |
| 4380 | """ |
| 4381 | try: |
| 4382 | output = self.sendline( cmd ) |
| 4383 | try: |
| 4384 | assert output is not None, "Error in sendline" |
| 4385 | # TODO: Maybe make this less hardcoded |
| 4386 | # ConsistentMap Exceptions |
| 4387 | assert "org.onosproject.store.service" not in output |
| 4388 | # Node not leader |
| 4389 | assert "java.lang.IllegalStateException" not in output |
| 4390 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4391 | main.log.error( self.name + ": Error in processing '" + cmd + "' " + |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4392 | "command: " + str( output ) ) |
| 4393 | retryTime = 30 # Conservative time, given by Madan |
| 4394 | main.log.info( "Waiting " + str( retryTime ) + |
| 4395 | "seconds before retrying." ) |
| 4396 | time.sleep( retryTime ) # Due to change in mastership |
| 4397 | output = self.sendline( cmd ) |
| 4398 | assert output is not None, "Error in sendline" |
| 4399 | assert "Command not found:" not in output, output |
| 4400 | assert "Error executing command" not in output, output |
| 4401 | main.log.info( self.name + ": " + output ) |
| 4402 | return output |
| 4403 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4404 | main.log.exception( self.name + ": Error in processing '" + cmd + "' command." ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4405 | return None |
| 4406 | except TypeError: |
| 4407 | main.log.exception( self.name + ": Object not as expected" ) |
| 4408 | return None |
| 4409 | except pexpect.EOF: |
| 4410 | main.log.error( self.name + ": EOF exception found" ) |
| 4411 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4412 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4413 | except Exception: |
| 4414 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4415 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4416 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4417 | def setTestAdd( self, setName, values ): |
| 4418 | """ |
| 4419 | CLI command to add elements to a distributed set. |
| 4420 | Arguments: |
| 4421 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4422 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4423 | Example usages: |
| 4424 | setTestAdd( "set1", "a b c" ) |
| 4425 | setTestAdd( "set2", "1" ) |
| 4426 | returns: |
| 4427 | main.TRUE on success OR |
| 4428 | main.FALSE if elements were already in the set OR |
| 4429 | main.ERROR on error |
| 4430 | """ |
| 4431 | try: |
| 4432 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4433 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4434 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4435 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4436 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4437 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4438 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4439 | return main.FALSE |
| 4440 | else: |
| 4441 | main.log.error( self.name + ": setTestAdd did not" + |
| 4442 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4443 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4444 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4445 | except TypeError: |
| 4446 | main.log.exception( self.name + ": Object not as expected" ) |
| 4447 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -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 | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4451 | |
| 4452 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4453 | """ |
| 4454 | CLI command to remove elements from a distributed set. |
| 4455 | Required arguments: |
| 4456 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4457 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4458 | Optional arguments: |
| 4459 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4460 | retain - Retain only the given values. (intersection of the |
| 4461 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4462 | returns: |
| 4463 | main.TRUE on success OR |
| 4464 | main.FALSE if the set was not changed OR |
| 4465 | main.ERROR on error |
| 4466 | """ |
| 4467 | try: |
| 4468 | cmdStr = "set-test-remove " |
| 4469 | if clear: |
| 4470 | cmdStr += "-c " + str( setName ) |
| 4471 | elif retain: |
| 4472 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4473 | else: |
| 4474 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4475 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4476 | if clear: |
| 4477 | pattern = "Set " + str( setName ) + " cleared" |
| 4478 | if re.search( pattern, output ): |
| 4479 | return main.TRUE |
| 4480 | elif retain: |
| 4481 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4482 | "only elements of set \[(.*)\]" |
| 4483 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4484 | "retaining only elements of the set " +\ |
| 4485 | "\[(.*)\]" |
| 4486 | if re.search( positivePattern, output ): |
| 4487 | return main.TRUE |
| 4488 | elif re.search( negativePattern, output ): |
| 4489 | return main.FALSE |
| 4490 | else: |
| 4491 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4492 | str( setName ) |
| 4493 | if ( len( values.split() ) == 1 ): |
| 4494 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4495 | str( setName ) |
| 4496 | else: |
| 4497 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4498 | str( setName ) |
| 4499 | if re.search( positivePattern, output ): |
| 4500 | return main.TRUE |
| 4501 | elif re.search( negativePattern, output ): |
| 4502 | return main.FALSE |
| 4503 | main.log.error( self.name + ": setTestRemove did not" + |
| 4504 | " match expected output" ) |
| 4505 | main.log.debug( self.name + " expected: " + pattern ) |
| 4506 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4507 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4508 | except TypeError: |
| 4509 | main.log.exception( self.name + ": Object not as expected" ) |
| 4510 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4511 | except Exception: |
| 4512 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4513 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4514 | |
| 4515 | def setTestGet( self, setName, values="" ): |
| 4516 | """ |
| 4517 | CLI command to get the elements in a distributed set. |
| 4518 | Required arguments: |
| 4519 | setName - The name of the set to remove from. |
| 4520 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4521 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4522 | returns: |
| 4523 | main.ERROR on error OR |
| 4524 | A list of elements in the set if no optional arguments are |
| 4525 | supplied OR |
| 4526 | A tuple containing the list then: |
| 4527 | main.FALSE if the given values are not in the set OR |
| 4528 | main.TRUE if the given values are in the set OR |
| 4529 | """ |
| 4530 | try: |
| 4531 | values = str( values ).strip() |
| 4532 | setName = str( setName ).strip() |
| 4533 | length = len( values.split() ) |
| 4534 | containsCheck = None |
| 4535 | # Patterns to match |
| 4536 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4537 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4538 | containsTrue = "Set " + setName + " contains the value " + values |
| 4539 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4540 | values |
| 4541 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4542 | setPattern |
| 4543 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4544 | " subset " + setPattern |
| 4545 | |
| 4546 | cmdStr = "set-test-get " |
| 4547 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4548 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4549 | if length == 0: |
| 4550 | match = re.search( pattern, output ) |
| 4551 | else: # if given values |
| 4552 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4553 | patternTrue = pattern + "\r\n" + containsTrue |
| 4554 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4555 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4556 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4557 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4558 | matchTrue = re.search( patternTrue, output ) |
| 4559 | matchFalse = re.search( patternFalse, output ) |
| 4560 | if matchTrue: |
| 4561 | containsCheck = main.TRUE |
| 4562 | match = matchTrue |
| 4563 | elif matchFalse: |
| 4564 | containsCheck = main.FALSE |
| 4565 | match = matchFalse |
| 4566 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4567 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4568 | "expected output" ) |
| 4569 | main.log.debug( self.name + " expected: " + pattern ) |
| 4570 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4571 | match = None |
| 4572 | if match: |
| 4573 | setMatch = match.group( 1 ) |
| 4574 | if setMatch == '': |
| 4575 | setList = [] |
| 4576 | else: |
| 4577 | setList = setMatch.split( ", " ) |
| 4578 | if length > 0: |
| 4579 | return ( setList, containsCheck ) |
| 4580 | else: |
| 4581 | return setList |
| 4582 | else: # no match |
| 4583 | main.log.error( self.name + ": setTestGet did not" + |
| 4584 | " match expected output" ) |
| 4585 | main.log.debug( self.name + " expected: " + pattern ) |
| 4586 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4587 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4588 | except TypeError: |
| 4589 | main.log.exception( self.name + ": Object not as expected" ) |
| 4590 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4591 | except Exception: |
| 4592 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4593 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4594 | |
| 4595 | def setTestSize( self, setName ): |
| 4596 | """ |
| 4597 | CLI command to get the elements in a distributed set. |
| 4598 | Required arguments: |
| 4599 | setName - The name of the set to remove from. |
| 4600 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4601 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4602 | None on error |
| 4603 | """ |
| 4604 | try: |
| 4605 | # TODO: Should this check against the number of elements returned |
| 4606 | # and then return true/false based on that? |
| 4607 | setName = str( setName ).strip() |
| 4608 | # Patterns to match |
| 4609 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4610 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4611 | setPattern |
| 4612 | cmdStr = "set-test-get -s " |
| 4613 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4614 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4615 | if output: |
| 4616 | match = re.search( pattern, output ) |
| 4617 | if match: |
| 4618 | setSize = int( match.group( 1 ) ) |
| 4619 | setMatch = match.group( 2 ) |
| 4620 | if len( setMatch.split() ) == setSize: |
| 4621 | main.log.info( "The size returned by " + self.name + |
| 4622 | " matches the number of elements in " + |
| 4623 | "the returned set" ) |
| 4624 | else: |
| 4625 | main.log.error( "The size returned by " + self.name + |
| 4626 | " does not match the number of " + |
| 4627 | "elements in the returned set." ) |
| 4628 | return setSize |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4629 | else: # no match |
| 4630 | main.log.error( self.name + ": setTestGet did not" + |
| 4631 | " match expected output" ) |
| 4632 | main.log.debug( self.name + " expected: " + pattern ) |
| 4633 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4634 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4635 | except TypeError: |
| 4636 | main.log.exception( self.name + ": Object not as expected" ) |
| 4637 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4638 | except Exception: |
| 4639 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4640 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4641 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4642 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4643 | """ |
| 4644 | Command to list the various counters in the system. |
| 4645 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4646 | if jsonFormat, a string of the json object returned by the cli |
| 4647 | command |
| 4648 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4649 | None on error |
| 4650 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4651 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4652 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4653 | if jsonFormat: |
| 4654 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4655 | output = self.sendline( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4656 | main.log.debug( self.name + ": Counters unparsed: " + output ) |
| 4657 | output = output.split( "\r\n" )[ -1 ] |
| 4658 | main.log.debug( self.name + ": Counters parsed: " + output ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4659 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4660 | assert "Command not found:" not in output, output |
| 4661 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4662 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4663 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4664 | except AssertionError: |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 4665 | main.log.exception( self.name + ": Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4666 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4667 | except TypeError: |
| 4668 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4669 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4670 | except pexpect.EOF: |
| 4671 | main.log.error( self.name + ": EOF exception found" ) |
| 4672 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4673 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4674 | except Exception: |
| 4675 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4676 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4677 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4678 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4679 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4680 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4681 | Required arguments: |
| 4682 | counter - The name of the counter to increment. |
| 4683 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4684 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4685 | returns: |
| 4686 | integer value of the counter or |
| 4687 | None on Error |
| 4688 | """ |
| 4689 | try: |
| 4690 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4691 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4692 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4693 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4694 | if delta != 1: |
| 4695 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4696 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4697 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4698 | match = re.search( pattern, output ) |
| 4699 | if match: |
| 4700 | return int( match.group( 1 ) ) |
| 4701 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4702 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4703 | " match expected output." ) |
| 4704 | main.log.debug( self.name + " expected: " + pattern ) |
| 4705 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4706 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4707 | except TypeError: |
| 4708 | main.log.exception( self.name + ": Object not as expected" ) |
| 4709 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4710 | except Exception: |
| 4711 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4712 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4713 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4714 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4715 | """ |
| 4716 | CLI command to get a distributed counter then add a delta to it. |
| 4717 | Required arguments: |
| 4718 | counter - The name of the counter to increment. |
| 4719 | Optional arguments: |
| 4720 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4721 | returns: |
| 4722 | integer value of the counter or |
| 4723 | None on Error |
| 4724 | """ |
| 4725 | try: |
| 4726 | counter = str( counter ) |
| 4727 | delta = int( delta ) |
| 4728 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4729 | cmdStr += counter |
| 4730 | if delta != 1: |
| 4731 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4732 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4733 | pattern = counter + " was updated to (-?\d+)" |
| 4734 | match = re.search( pattern, output ) |
| 4735 | if match: |
| 4736 | return int( match.group( 1 ) ) |
| 4737 | else: |
| 4738 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4739 | " match expected output." ) |
| 4740 | main.log.debug( self.name + " expected: " + pattern ) |
| 4741 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4742 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4743 | except TypeError: |
| 4744 | main.log.exception( self.name + ": Object not as expected" ) |
| 4745 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4746 | except Exception: |
| 4747 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4748 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4749 | |
| 4750 | def valueTestGet( self, valueName ): |
| 4751 | """ |
| 4752 | CLI command to get the value of an atomic value. |
| 4753 | Required arguments: |
| 4754 | valueName - The name of the value to get. |
| 4755 | returns: |
| 4756 | string value of the value or |
| 4757 | None on Error |
| 4758 | """ |
| 4759 | try: |
| 4760 | valueName = str( valueName ) |
| 4761 | cmdStr = "value-test " |
| 4762 | operation = "get" |
| 4763 | cmdStr = "value-test {} {}".format( valueName, |
| 4764 | operation ) |
| 4765 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 4766 | main.log.debug( self.name + ": value test unparsed: " + output ) |
| 4767 | output = output.split( "\r\n" )[ -1 ] |
| 4768 | main.log.debug( self.name + ": value test parsed: " + output ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4769 | pattern = "(\w+)" |
| 4770 | match = re.search( pattern, output ) |
| 4771 | if match: |
| 4772 | return match.group( 1 ) |
| 4773 | else: |
| 4774 | main.log.error( self.name + ": valueTestGet did not" + |
| 4775 | " match expected output." ) |
| 4776 | main.log.debug( self.name + " expected: " + pattern ) |
| 4777 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4778 | return None |
| 4779 | except TypeError: |
| 4780 | main.log.exception( self.name + ": Object not as expected" ) |
| 4781 | return None |
| 4782 | except Exception: |
| 4783 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4784 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4785 | |
| 4786 | def valueTestSet( self, valueName, newValue ): |
| 4787 | """ |
| 4788 | CLI command to set the value of an atomic value. |
| 4789 | Required arguments: |
| 4790 | valueName - The name of the value to set. |
| 4791 | newValue - The value to assign to the given value. |
| 4792 | returns: |
| 4793 | main.TRUE on success or |
| 4794 | main.ERROR on Error |
| 4795 | """ |
| 4796 | try: |
| 4797 | valueName = str( valueName ) |
| 4798 | newValue = str( newValue ) |
| 4799 | operation = "set" |
| 4800 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4801 | operation, |
| 4802 | newValue ) |
| 4803 | output = self.distPrimitivesSend( cmdStr ) |
| 4804 | if output is not None: |
| 4805 | return main.TRUE |
| 4806 | else: |
| 4807 | return main.ERROR |
| 4808 | except TypeError: |
| 4809 | main.log.exception( self.name + ": Object not as expected" ) |
| 4810 | return main.ERROR |
| 4811 | except Exception: |
| 4812 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4813 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4814 | |
| 4815 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4816 | """ |
| 4817 | CLI command to compareAndSet the value of an atomic value. |
| 4818 | Required arguments: |
| 4819 | valueName - The name of the value. |
| 4820 | oldValue - Compare the current value of the atomic value to this |
| 4821 | newValue - If the value equals oldValue, set the value to newValue |
| 4822 | returns: |
| 4823 | main.TRUE on success or |
| 4824 | main.FALSE on failure or |
| 4825 | main.ERROR on Error |
| 4826 | """ |
| 4827 | try: |
| 4828 | valueName = str( valueName ) |
| 4829 | oldValue = str( oldValue ) |
| 4830 | newValue = str( newValue ) |
| 4831 | operation = "compareAndSet" |
| 4832 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4833 | operation, |
| 4834 | oldValue, |
| 4835 | newValue ) |
| 4836 | output = self.distPrimitivesSend( cmdStr ) |
| 4837 | pattern = "(\w+)" |
| 4838 | match = re.search( pattern, output ) |
| 4839 | if match: |
| 4840 | result = match.group( 1 ) |
| 4841 | if result == "true": |
| 4842 | return main.TRUE |
| 4843 | elif result == "false": |
| 4844 | return main.FALSE |
| 4845 | else: |
| 4846 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4847 | " match expected output." ) |
| 4848 | main.log.debug( self.name + " expected: " + pattern ) |
| 4849 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4850 | return main.ERROR |
| 4851 | except TypeError: |
| 4852 | main.log.exception( self.name + ": Object not as expected" ) |
| 4853 | return main.ERROR |
| 4854 | except Exception: |
| 4855 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4856 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4857 | |
| 4858 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4859 | """ |
| 4860 | CLI command to getAndSet the value of an atomic value. |
| 4861 | Required arguments: |
| 4862 | valueName - The name of the value to get. |
| 4863 | newValue - The value to assign to the given value |
| 4864 | returns: |
| 4865 | string value of the value or |
| 4866 | None on Error |
| 4867 | """ |
| 4868 | try: |
| 4869 | valueName = str( valueName ) |
| 4870 | cmdStr = "value-test " |
| 4871 | operation = "getAndSet" |
| 4872 | cmdStr += valueName + " " + operation |
| 4873 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4874 | operation, |
| 4875 | newValue ) |
| 4876 | output = self.distPrimitivesSend( cmdStr ) |
| 4877 | pattern = "(\w+)" |
| 4878 | match = re.search( pattern, output ) |
| 4879 | if match: |
| 4880 | return match.group( 1 ) |
| 4881 | else: |
| 4882 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4883 | " match expected output." ) |
| 4884 | main.log.debug( self.name + " expected: " + pattern ) |
| 4885 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4886 | return None |
| 4887 | except TypeError: |
| 4888 | main.log.exception( self.name + ": Object not as expected" ) |
| 4889 | return None |
| 4890 | except Exception: |
| 4891 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4892 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4893 | |
| 4894 | def valueTestDestroy( self, valueName ): |
| 4895 | """ |
| 4896 | CLI command to destroy an atomic value. |
| 4897 | Required arguments: |
| 4898 | valueName - The name of the value to destroy. |
| 4899 | returns: |
| 4900 | main.TRUE on success or |
| 4901 | main.ERROR on Error |
| 4902 | """ |
| 4903 | try: |
| 4904 | valueName = str( valueName ) |
| 4905 | cmdStr = "value-test " |
| 4906 | operation = "destroy" |
| 4907 | cmdStr += valueName + " " + operation |
| 4908 | output = self.distPrimitivesSend( cmdStr ) |
| 4909 | if output is not None: |
| 4910 | return main.TRUE |
| 4911 | else: |
| 4912 | return main.ERROR |
| 4913 | except TypeError: |
| 4914 | main.log.exception( self.name + ": Object not as expected" ) |
| 4915 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4916 | except Exception: |
| 4917 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4918 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4919 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4920 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4921 | """ |
| 4922 | Description: Execute summary command in onos |
| 4923 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4924 | no output |
| 4925 | |
| 4926 | """ |
| 4927 | try: |
| 4928 | cmdStr = "summary" |
| 4929 | if jsonFormat: |
| 4930 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4931 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4932 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4933 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4934 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 4935 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4936 | if not handle: |
| 4937 | main.log.error( self.name + ": There is no output in " + |
| 4938 | "summary command" ) |
| 4939 | return main.FALSE |
| 4940 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4941 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4942 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4943 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4944 | except TypeError: |
| 4945 | main.log.exception( self.name + ": Object not as expected" ) |
| 4946 | return None |
| 4947 | except pexpect.EOF: |
| 4948 | main.log.error( self.name + ": EOF exception found" ) |
| 4949 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4950 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -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 | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4954 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 4955 | def getAddress( self): |
| 4956 | """ |
| 4957 | Get the onos ip address from the cli. This is usefull when connecting using |
| 4958 | a container manager such as kubernetes. This function also sets self.address |
| 4959 | the value from ONOS. |
| 4960 | |
| 4961 | Returns: |
| 4962 | The string value of the key or |
| 4963 | None on Error |
| 4964 | """ |
| 4965 | try: |
| 4966 | output = self.summary() |
| 4967 | address = json.loads( output ).get( 'node' ) |
| 4968 | self.address = address |
| 4969 | return address |
You Wang | 327bad4 | 2021-03-24 14:19:58 -0700 | [diff] [blame] | 4970 | except ( TypeError, ValueError ): |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 4971 | main.log.exception( self.name + ": Object not as expected" ) |
| 4972 | return None |
| 4973 | except Exception: |
| 4974 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4975 | main.cleanAndExit() |
| 4976 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4977 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4978 | """ |
| 4979 | CLI command to get the value of a key in a consistent map using |
| 4980 | transactions. This a test function and can only get keys from the |
| 4981 | test map hard coded into the cli command |
| 4982 | Required arguments: |
| 4983 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4984 | returns: |
| 4985 | The string value of the key or |
| 4986 | None on Error |
| 4987 | """ |
| 4988 | try: |
| 4989 | keyName = str( keyName ) |
| 4990 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4991 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4992 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4993 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4994 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4995 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4996 | return None |
| 4997 | else: |
| 4998 | match = re.search( pattern, output ) |
| 4999 | if match: |
| 5000 | return match.groupdict()[ 'value' ] |
| 5001 | else: |
| 5002 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 5003 | " match expected output." ) |
| 5004 | main.log.debug( self.name + " expected: " + pattern ) |
| 5005 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 5006 | return None |
| 5007 | except TypeError: |
| 5008 | main.log.exception( self.name + ": Object not as expected" ) |
| 5009 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5010 | except Exception: |
| 5011 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5012 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5013 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 5014 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5015 | """ |
| 5016 | CLI command to put a value into 'numKeys' number of keys in a |
| 5017 | consistent map using transactions. This a test function and can only |
| 5018 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 5019 | Required arguments: |
| 5020 | numKeys - Number of keys to add the value to |
| 5021 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5022 | returns: |
| 5023 | A dictionary whose keys are the name of the keys put into the map |
| 5024 | and the values of the keys are dictionaries whose key-values are |
| 5025 | 'value': value put into map and optionaly |
| 5026 | 'oldValue': Previous value in the key or |
| 5027 | None on Error |
| 5028 | |
| 5029 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5030 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 5031 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5032 | """ |
| 5033 | try: |
| 5034 | numKeys = str( numKeys ) |
| 5035 | value = str( value ) |
| 5036 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5037 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 5038 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5039 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 5040 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 5041 | results = {} |
| 5042 | for line in output.splitlines(): |
| 5043 | new = re.search( newPattern, line ) |
| 5044 | updated = re.search( updatedPattern, line ) |
| 5045 | if new: |
| 5046 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 5047 | elif updated: |
| 5048 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5049 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5050 | else: |
| 5051 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 5052 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5053 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 5054 | newPattern, |
| 5055 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5056 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 5057 | return results |
Jon Hall | 0e24037 | 2018-05-02 11:21:57 -0700 | [diff] [blame] | 5058 | except ( TypeError, AttributeError ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5059 | main.log.exception( self.name + ": Object not as expected" ) |
| 5060 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 5061 | except Exception: |
| 5062 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5063 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5064 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5065 | def maps( self, jsonFormat=True ): |
| 5066 | """ |
| 5067 | Description: Returns result of onos:maps |
| 5068 | Optional: |
| 5069 | * jsonFormat: enable json formatting of output |
| 5070 | """ |
| 5071 | try: |
| 5072 | cmdStr = "maps" |
| 5073 | if jsonFormat: |
| 5074 | cmdStr += " -j" |
| 5075 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5076 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5077 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5078 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5079 | except AssertionError: |
| 5080 | main.log.exception( "" ) |
| 5081 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5082 | except TypeError: |
| 5083 | main.log.exception( self.name + ": Object not as expected" ) |
| 5084 | return None |
| 5085 | except pexpect.EOF: |
| 5086 | main.log.error( self.name + ": EOF exception found" ) |
| 5087 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5088 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 5089 | except Exception: |
| 5090 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5091 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5092 | |
| 5093 | def getSwController( self, uri, jsonFormat=True ): |
| 5094 | """ |
| 5095 | Descrition: Gets the controller information from the device |
| 5096 | """ |
| 5097 | try: |
| 5098 | cmd = "device-controllers " |
| 5099 | if jsonFormat: |
| 5100 | cmd += "-j " |
| 5101 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5102 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5103 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5104 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5105 | except AssertionError: |
| 5106 | main.log.exception( "" ) |
| 5107 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5108 | except TypeError: |
| 5109 | main.log.exception( self.name + ": Object not as expected" ) |
| 5110 | return None |
| 5111 | except pexpect.EOF: |
| 5112 | main.log.error( self.name + ": EOF exception found" ) |
| 5113 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5114 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5115 | except Exception: |
| 5116 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5117 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5118 | |
| 5119 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 5120 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5121 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5122 | |
| 5123 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5124 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5125 | ip - String or List: The ip address of the controller. |
| 5126 | This parameter can be formed in a couple of different ways. |
| 5127 | VALID: |
| 5128 | 10.0.0.1 - just the ip address |
| 5129 | tcp:10.0.0.1 - the protocol and the ip address |
| 5130 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 5131 | so that you can add controllers with different |
| 5132 | protocols and ports |
| 5133 | INVALID: |
| 5134 | 10.0.0.1:6653 - this is not supported by ONOS |
| 5135 | |
| 5136 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 5137 | port - The port number. |
| 5138 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 5139 | |
| 5140 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 5141 | """ |
| 5142 | try: |
| 5143 | cmd = "device-setcontrollers" |
| 5144 | |
| 5145 | if jsonFormat: |
| 5146 | cmd += " -j" |
| 5147 | cmd += " " + uri |
| 5148 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5149 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5150 | for item in ip: |
| 5151 | if ":" in item: |
| 5152 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5153 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5154 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5155 | elif "." in sitem[ 1 ]: |
| 5156 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5157 | else: |
| 5158 | main.log.error( "Malformed entry: " + item ) |
| 5159 | raise TypeError |
| 5160 | else: |
| 5161 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5162 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5163 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5164 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5165 | if "Error" in response: |
| 5166 | main.log.error( response ) |
| 5167 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5168 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5169 | except AssertionError: |
| 5170 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5171 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5172 | except TypeError: |
| 5173 | main.log.exception( self.name + ": Object not as expected" ) |
| 5174 | return main.FALSE |
| 5175 | except pexpect.EOF: |
| 5176 | main.log.error( self.name + ": EOF exception found" ) |
| 5177 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5178 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 5179 | except Exception: |
| 5180 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5181 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5182 | |
| 5183 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5184 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5185 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5186 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5187 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5188 | 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] | 5189 | Returns: |
| 5190 | Returns main.FALSE if an exception is thrown or an error is present |
| 5191 | in the response. Otherwise, returns main.TRUE. |
| 5192 | NOTE: |
| 5193 | 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] | 5194 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5195 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5196 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 5197 | deviceStr = device |
| 5198 | device = [] |
| 5199 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5200 | |
| 5201 | for d in device: |
| 5202 | time.sleep( 1 ) |
| 5203 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5204 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5205 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5206 | if "Error" in response: |
| 5207 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 5208 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5209 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5210 | except AssertionError: |
| 5211 | main.log.exception( "" ) |
| 5212 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5213 | except TypeError: |
| 5214 | main.log.exception( self.name + ": Object not as expected" ) |
| 5215 | return main.FALSE |
| 5216 | except pexpect.EOF: |
| 5217 | main.log.error( self.name + ": EOF exception found" ) |
| 5218 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5219 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5220 | except Exception: |
| 5221 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5222 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5223 | |
| 5224 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5225 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5226 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5227 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5228 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5229 | 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] | 5230 | Returns: |
| 5231 | Returns main.FALSE if an exception is thrown or an error is present |
| 5232 | in the response. Otherwise, returns main.TRUE. |
| 5233 | NOTE: |
| 5234 | 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] | 5235 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5236 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5237 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5238 | host = list( host ) |
| 5239 | |
| 5240 | for h in host: |
| 5241 | time.sleep( 1 ) |
| 5242 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5243 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5244 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5245 | if "Error" in response: |
| 5246 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5247 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5248 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5249 | except AssertionError: |
| 5250 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5251 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5252 | except TypeError: |
| 5253 | main.log.exception( self.name + ": Object not as expected" ) |
| 5254 | return main.FALSE |
| 5255 | except pexpect.EOF: |
| 5256 | main.log.error( self.name + ": EOF exception found" ) |
| 5257 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5258 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5259 | except Exception: |
| 5260 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5261 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5262 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5263 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5264 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5265 | Description: |
| 5266 | Bring link down or up in the null-provider. |
| 5267 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5268 | begin - (string) One end of a device or switch. |
| 5269 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5270 | returns: |
| 5271 | main.TRUE if no exceptions were thrown and no Errors are |
| 5272 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5273 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5274 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5275 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5276 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5277 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5278 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5279 | if "Error" in response or "Failure" in response: |
| 5280 | main.log.error( response ) |
| 5281 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5282 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5283 | except AssertionError: |
| 5284 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5285 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5286 | except TypeError: |
| 5287 | main.log.exception( self.name + ": Object not as expected" ) |
| 5288 | return main.FALSE |
| 5289 | except pexpect.EOF: |
| 5290 | main.log.error( self.name + ": EOF exception found" ) |
| 5291 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5292 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5293 | except Exception: |
| 5294 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5295 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5296 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5297 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5298 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5299 | Description: |
| 5300 | Changes the state of port in an OF switch by means of the |
| 5301 | PORTSTATUS OF messages. |
| 5302 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5303 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5304 | port - (string) target port in the device. Ex: '2' |
| 5305 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5306 | returns: |
| 5307 | main.TRUE if no exceptions were thrown and no Errors are |
| 5308 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5309 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5310 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5311 | state = state.lower() |
| 5312 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5313 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5314 | response = self.sendline( cmd, showResponse=True ) |
| 5315 | assert response is not None, "Error in sendline" |
| 5316 | assert "Command not found:" not in response, response |
| 5317 | if "Error" in response or "Failure" in response: |
| 5318 | main.log.error( response ) |
| 5319 | return main.FALSE |
| 5320 | return main.TRUE |
| 5321 | except AssertionError: |
| 5322 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5323 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5324 | except TypeError: |
| 5325 | main.log.exception( self.name + ": Object not as expected" ) |
| 5326 | return main.FALSE |
| 5327 | except pexpect.EOF: |
| 5328 | main.log.error( self.name + ": EOF exception found" ) |
| 5329 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5330 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5331 | except Exception: |
| 5332 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5333 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5334 | |
| 5335 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5336 | """ |
| 5337 | Set the logging level to lvl for a specific app |
| 5338 | returns main.TRUE on success |
| 5339 | returns main.FALSE if Error occurred |
| 5340 | if noExit is True, TestON will not exit, but clean up |
| 5341 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5342 | Level defaults to INFO |
| 5343 | """ |
| 5344 | try: |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 5345 | handle = self.sendline( "log:set %s %s" % ( level, app ) ) |
| 5346 | assert handle is not None, "Error in sendline" |
| 5347 | assert "Command not found:" not in handle, handle |
| 5348 | if re.search( "Error", handle ): |
| 5349 | main.log.error( self.name + ": Error in setting log level" ) |
| 5350 | main.log.error( handle ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5351 | return main.FALSE |
You Wang | 22e807e | 2021-03-29 10:53:38 -0700 | [diff] [blame] | 5352 | else: |
| 5353 | return main.TRUE |
| 5354 | except AssertionError: |
| 5355 | main.log.exception( "" ) |
| 5356 | return None |
| 5357 | except TypeError: |
| 5358 | main.log.exception( self.name + ": Object not as expected" ) |
| 5359 | return None |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5360 | except pexpect.EOF: |
| 5361 | main.log.error( self.name + ": EOF exception found" ) |
| 5362 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5363 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5364 | except Exception: |
| 5365 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5366 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5367 | |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 5368 | def logList( self, saveValues=True ): |
| 5369 | """ |
| 5370 | Gets the current log levels and optionally saves them |
| 5371 | returns a dict of the log levels or |
| 5372 | returns main.FALSE if Error occurred |
| 5373 | """ |
| 5374 | try: |
| 5375 | self.handle.sendline( "log:list" ) |
| 5376 | self.handle.expect( self.karafPrompt ) |
| 5377 | |
| 5378 | response = self.handle.before |
| 5379 | logLevels = {} |
| 5380 | for line in response.splitlines(): |
| 5381 | parsed = line.split('│') |
| 5382 | logger = parsed[0].strip() |
| 5383 | if len( parsed ) != 2 or 'Level' in parsed[1] or logger[0] == '─': |
| 5384 | continue |
| 5385 | level = parsed[1].strip() |
| 5386 | logLevels[ logger ] = level |
| 5387 | if saveValues: |
| 5388 | self.logLevels = logLevels |
| 5389 | return logLevels |
| 5390 | except pexpect.TIMEOUT: |
| 5391 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
| 5392 | main.cleanAndExit() |
| 5393 | except pexpect.EOF: |
| 5394 | main.log.error( self.name + ": EOF exception found" ) |
| 5395 | main.log.error( self.name + ": " + self.handle.before ) |
| 5396 | main.cleanAndExit() |
| 5397 | except Exception: |
| 5398 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5399 | main.cleanAndExit() |
| 5400 | |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5401 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5402 | """ |
| 5403 | Return a dictionary which describes the latest network topology data as a |
| 5404 | graph. |
| 5405 | An example of the dictionary: |
| 5406 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5407 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5408 | Each vertex should at least have an 'edges' attribute which describes the |
| 5409 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5410 | 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] | 5411 | list of attributes. |
| 5412 | An example of the edges dictionary: |
| 5413 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5414 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5415 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5416 | in topology data. |
| 5417 | """ |
| 5418 | graphDict = {} |
| 5419 | try: |
| 5420 | links = self.links() |
| 5421 | links = json.loads( links ) |
| 5422 | devices = self.devices() |
| 5423 | devices = json.loads( devices ) |
| 5424 | idToDevice = {} |
| 5425 | for device in devices: |
| 5426 | idToDevice[ device[ 'id' ] ] = device |
| 5427 | if includeHost: |
| 5428 | hosts = self.hosts() |
| 5429 | # FIXME: support 'includeHost' argument |
| 5430 | for link in links: |
| 5431 | nodeA = link[ 'src' ][ 'device' ] |
| 5432 | nodeB = link[ 'dst' ][ 'device' ] |
| 5433 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5434 | if nodeA not in graphDict.keys(): |
| 5435 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5436 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5437 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5438 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5439 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5440 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5441 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5442 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5443 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5444 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5445 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5446 | else: |
| 5447 | # Assert nodeB is not connected to any current links of nodeA |
You Wang | 7d14d64 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 5448 | # assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
| 5449 | pass |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5450 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5451 | 'type': link[ 'type' ], |
| 5452 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5453 | return graphDict |
| 5454 | except ( TypeError, ValueError ): |
| 5455 | main.log.exception( self.name + ": Object not as expected" ) |
| 5456 | return None |
| 5457 | except KeyError: |
| 5458 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5459 | return None |
| 5460 | except AssertionError: |
| 5461 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5462 | return None |
| 5463 | except pexpect.EOF: |
| 5464 | main.log.error( self.name + ": EOF exception found" ) |
| 5465 | main.log.error( self.name + ": " + self.handle.before ) |
| 5466 | return None |
| 5467 | except Exception: |
| 5468 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5469 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5470 | |
| 5471 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5472 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5473 | Send command to check intent-perf summary |
| 5474 | Returns: dictionary for intent-perf summary |
| 5475 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5476 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5477 | cmd = "intent-perf -s" |
| 5478 | respDic = {} |
| 5479 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5480 | assert resp is not None, "Error in sendline" |
| 5481 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5482 | try: |
| 5483 | # Generate the dictionary to return |
| 5484 | for l in resp.split( "\n" ): |
| 5485 | # Delete any white space in line |
| 5486 | temp = re.sub( r'\s+', '', l ) |
| 5487 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5488 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5489 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5490 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5491 | main.log.exception( self.name + ": Object not as expected" ) |
| 5492 | return None |
| 5493 | except KeyError: |
| 5494 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5495 | return None |
| 5496 | except AssertionError: |
| 5497 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5498 | return None |
| 5499 | except pexpect.EOF: |
| 5500 | main.log.error( self.name + ": EOF exception found" ) |
| 5501 | main.log.error( self.name + ": " + self.handle.before ) |
| 5502 | return None |
| 5503 | except Exception: |
| 5504 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5505 | return None |
| 5506 | return respDic |
| 5507 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5508 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5509 | """ |
| 5510 | Searches the latest ONOS log file for the given search term and |
| 5511 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5512 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5513 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5514 | searchTerm: |
| 5515 | The string to grep from the ONOS log. |
| 5516 | startLine: |
| 5517 | The term that decides which line is the start to search the searchTerm in |
| 5518 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5519 | logNum: |
| 5520 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5521 | information.Because of this, search mutiply logs is necessary to capture |
| 5522 | the right result. logNum is the number of karaf logs that we need to search |
| 5523 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5524 | mode: |
| 5525 | all: return all the strings that contain the search term |
| 5526 | last: return the last string that contains the search term |
| 5527 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5528 | num: return the number of times that the searchTerm appears in the log |
| 5529 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5530 | """ |
| 5531 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5532 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5533 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5534 | logPath = '/opt/onos/log/karaf.log.' |
| 5535 | logPaths = '/opt/onos/log/karaf.log' |
| 5536 | for i in range( 1, logNum ): |
| 5537 | logPaths = logPath + str( i ) + " " + logPaths |
| 5538 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5539 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5540 | # 100000000 is just a extreme large number to make sure this function can |
| 5541 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5542 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5543 | if mode == 'all': |
| 5544 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5545 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5546 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5547 | elif mode == 'first': |
| 5548 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5549 | elif mode == 'num': |
You Wang | d91a70f | 2019-01-03 15:28:10 -0800 | [diff] [blame] | 5550 | cmd = cmd + " | grep \'" + searchTerm + "\' | wc -l" |
| 5551 | num = self.lineCount( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5552 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5553 | elif mode == 'total': |
Jon Hall | d5a94fb | 2018-11-13 14:32:23 -0800 | [diff] [blame] | 5554 | totalLines = self.lineCount( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5555 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5556 | else: |
| 5557 | main.log.error( self.name + " unsupported mode" ) |
| 5558 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5559 | before = self.sendline( cmd ) |
| 5560 | before = before.splitlines() |
| 5561 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5562 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5563 | return returnLines |
| 5564 | except AssertionError: |
| 5565 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5566 | return None |
| 5567 | except pexpect.EOF: |
| 5568 | main.log.error( self.name + ": EOF exception found" ) |
| 5569 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5570 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5571 | except pexpect.TIMEOUT: |
| 5572 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5573 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5574 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5575 | except Exception: |
| 5576 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5577 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5578 | |
| 5579 | def vplsShow( self, jsonFormat=True ): |
| 5580 | """ |
| 5581 | Description: Returns result of onos:vpls show, which should list the |
| 5582 | configured VPLS networks and the assigned interfaces. |
| 5583 | Optional: |
| 5584 | * jsonFormat: enable json formatting of output |
| 5585 | Returns: |
| 5586 | The output of the command or None on error. |
| 5587 | """ |
| 5588 | try: |
| 5589 | cmdStr = "vpls show" |
| 5590 | if jsonFormat: |
| 5591 | raise NotImplementedError |
| 5592 | cmdStr += " -j" |
| 5593 | handle = self.sendline( cmdStr ) |
| 5594 | assert handle is not None, "Error in sendline" |
| 5595 | assert "Command not found:" not in handle, handle |
| 5596 | return handle |
| 5597 | except AssertionError: |
| 5598 | main.log.exception( "" ) |
| 5599 | return None |
| 5600 | except TypeError: |
| 5601 | main.log.exception( self.name + ": Object not as expected" ) |
| 5602 | return None |
| 5603 | except pexpect.EOF: |
| 5604 | main.log.error( self.name + ": EOF exception found" ) |
| 5605 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5606 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5607 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5608 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5609 | return None |
| 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 parseVplsShow( self ): |
| 5615 | """ |
| 5616 | Parse the cli output of 'vpls show' into json output. This is required |
| 5617 | as there is currently no json output available. |
| 5618 | """ |
| 5619 | try: |
| 5620 | output = [] |
| 5621 | raw = self.vplsShow( jsonFormat=False ) |
| 5622 | namePat = "VPLS name: (?P<name>\w+)" |
| 5623 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5624 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5625 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5626 | mIter = re.finditer( pattern, raw ) |
| 5627 | for match in mIter: |
| 5628 | item = {} |
| 5629 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5630 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5631 | if ifaces == [ "" ]: |
| 5632 | ifaces = [] |
| 5633 | item[ 'interfaces' ] = ifaces |
| 5634 | encap = match.group( 'encap' ) |
| 5635 | if encap != 'NONE': |
| 5636 | item[ 'encapsulation' ] = encap.lower() |
| 5637 | output.append( item ) |
| 5638 | return output |
| 5639 | except Exception: |
| 5640 | main.log.exception( self.name + ": Uncaught exception!" ) |
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 | |
| 5643 | def vplsList( self, jsonFormat=True ): |
| 5644 | """ |
| 5645 | Description: Returns result of onos:vpls list, which should list the |
| 5646 | configured VPLS networks. |
| 5647 | Optional: |
| 5648 | * jsonFormat: enable json formatting of output |
| 5649 | """ |
| 5650 | try: |
| 5651 | cmdStr = "vpls list" |
| 5652 | if jsonFormat: |
| 5653 | raise NotImplementedError |
| 5654 | cmdStr += " -j" |
| 5655 | handle = self.sendline( cmdStr ) |
| 5656 | assert handle is not None, "Error in sendline" |
| 5657 | assert "Command not found:" not in handle, handle |
| 5658 | return handle |
| 5659 | except AssertionError: |
| 5660 | main.log.exception( "" ) |
| 5661 | return None |
| 5662 | except TypeError: |
| 5663 | main.log.exception( self.name + ": Object not as expected" ) |
| 5664 | return None |
| 5665 | except pexpect.EOF: |
| 5666 | main.log.error( self.name + ": EOF exception found" ) |
| 5667 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5668 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5669 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5670 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5671 | return None |
| 5672 | except Exception: |
| 5673 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5674 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5675 | |
| 5676 | def vplsCreate( self, network ): |
| 5677 | """ |
| 5678 | CLI command to create a new VPLS network. |
| 5679 | Required arguments: |
| 5680 | network - String name of the network to create. |
| 5681 | returns: |
| 5682 | main.TRUE on success and main.FALSE on failure |
| 5683 | """ |
| 5684 | try: |
| 5685 | network = str( network ) |
| 5686 | cmdStr = "vpls create " |
| 5687 | cmdStr += network |
| 5688 | output = self.sendline( cmdStr ) |
| 5689 | assert output is not None, "Error in sendline" |
| 5690 | assert "Command not found:" not in output, output |
| 5691 | assert "Error executing command" not in output, output |
| 5692 | assert "VPLS already exists:" not in output, output |
| 5693 | return main.TRUE |
| 5694 | except AssertionError: |
| 5695 | main.log.exception( "" ) |
| 5696 | return main.FALSE |
| 5697 | except TypeError: |
| 5698 | main.log.exception( self.name + ": Object not as expected" ) |
| 5699 | return main.FALSE |
| 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 Exception: |
| 5705 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5706 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5707 | |
| 5708 | def vplsDelete( self, network ): |
| 5709 | """ |
| 5710 | CLI command to delete a VPLS network. |
| 5711 | Required arguments: |
| 5712 | network - Name of the network to delete. |
| 5713 | returns: |
| 5714 | main.TRUE on success and main.FALSE on failure |
| 5715 | """ |
| 5716 | try: |
| 5717 | network = str( network ) |
| 5718 | cmdStr = "vpls delete " |
| 5719 | cmdStr += network |
| 5720 | output = self.sendline( cmdStr ) |
| 5721 | assert output is not None, "Error in sendline" |
| 5722 | assert "Command not found:" not in output, output |
| 5723 | assert "Error executing command" not in output, output |
| 5724 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5725 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5726 | return main.TRUE |
| 5727 | except AssertionError: |
| 5728 | main.log.exception( "" ) |
| 5729 | return main.FALSE |
| 5730 | except TypeError: |
| 5731 | main.log.exception( self.name + ": Object not as expected" ) |
| 5732 | return main.FALSE |
| 5733 | except pexpect.EOF: |
| 5734 | main.log.error( self.name + ": EOF exception found" ) |
| 5735 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5736 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5737 | except Exception: |
| 5738 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5739 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5740 | |
| 5741 | def vplsAddIface( self, network, iface ): |
| 5742 | """ |
| 5743 | CLI command to add an interface to a VPLS network. |
| 5744 | Required arguments: |
| 5745 | network - Name of the network to add the interface to. |
| 5746 | iface - The ONOS name for an interface. |
| 5747 | returns: |
| 5748 | main.TRUE on success and main.FALSE on failure |
| 5749 | """ |
| 5750 | try: |
| 5751 | network = str( network ) |
| 5752 | iface = str( iface ) |
| 5753 | cmdStr = "vpls add-if " |
| 5754 | cmdStr += network + " " + iface |
| 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 "already associated to network" not in output, output |
| 5760 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5761 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5762 | return main.TRUE |
| 5763 | except AssertionError: |
| 5764 | main.log.exception( "" ) |
| 5765 | return main.FALSE |
| 5766 | except TypeError: |
| 5767 | main.log.exception( self.name + ": Object not as expected" ) |
| 5768 | return main.FALSE |
| 5769 | except pexpect.EOF: |
| 5770 | main.log.error( self.name + ": EOF exception found" ) |
| 5771 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5772 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5773 | except Exception: |
| 5774 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5775 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5776 | |
| 5777 | def vplsRemIface( self, network, iface ): |
| 5778 | """ |
| 5779 | CLI command to remove an interface from a VPLS network. |
| 5780 | Required arguments: |
| 5781 | network - Name of the network to remove the interface from. |
| 5782 | iface - Name of the interface to remove. |
| 5783 | returns: |
| 5784 | main.TRUE on success and main.FALSE on failure |
| 5785 | """ |
| 5786 | try: |
| 5787 | iface = str( iface ) |
| 5788 | cmdStr = "vpls rem-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 "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5795 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5796 | return main.TRUE |
| 5797 | except AssertionError: |
| 5798 | main.log.exception( "" ) |
| 5799 | return main.FALSE |
| 5800 | except TypeError: |
| 5801 | main.log.exception( self.name + ": Object not as expected" ) |
| 5802 | return main.FALSE |
| 5803 | except pexpect.EOF: |
| 5804 | main.log.error( self.name + ": EOF exception found" ) |
| 5805 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5806 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5807 | except Exception: |
| 5808 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5809 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5810 | |
| 5811 | def vplsClean( self ): |
| 5812 | """ |
| 5813 | Description: Clears the VPLS app configuration. |
| 5814 | Returns: main.TRUE on success and main.FALSE on failure |
| 5815 | """ |
| 5816 | try: |
| 5817 | cmdStr = "vpls clean" |
| 5818 | handle = self.sendline( cmdStr ) |
| 5819 | assert handle is not None, "Error in sendline" |
| 5820 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5821 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5822 | return handle |
| 5823 | except AssertionError: |
| 5824 | main.log.exception( "" ) |
| 5825 | return main.FALSE |
| 5826 | except TypeError: |
| 5827 | main.log.exception( self.name + ": Object not as expected" ) |
| 5828 | return main.FALSE |
| 5829 | except pexpect.EOF: |
| 5830 | main.log.error( self.name + ": EOF exception found" ) |
| 5831 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5832 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5833 | except Exception: |
| 5834 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5835 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5836 | |
| 5837 | def vplsSetEncap( self, network, encapType ): |
| 5838 | """ |
| 5839 | CLI command to add an interface to a VPLS network. |
| 5840 | Required arguments: |
| 5841 | network - Name of the network to create. |
| 5842 | encapType - Type of encapsulation. |
| 5843 | returns: |
| 5844 | main.TRUE on success and main.FALSE on failure |
| 5845 | """ |
| 5846 | try: |
| 5847 | network = str( network ) |
| 5848 | encapType = str( encapType ).upper() |
| 5849 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5850 | cmdStr = "vpls set-encap " |
| 5851 | cmdStr += network + " " + encapType |
| 5852 | output = self.sendline( cmdStr ) |
| 5853 | assert output is not None, "Error in sendline" |
| 5854 | assert "Command not found:" not in output, output |
| 5855 | assert "Error executing command" not in output, output |
| 5856 | assert "already associated to network" not in output, output |
| 5857 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5858 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5859 | return main.TRUE |
| 5860 | except AssertionError: |
| 5861 | main.log.exception( "" ) |
| 5862 | return main.FALSE |
| 5863 | except TypeError: |
| 5864 | main.log.exception( self.name + ": Object not as expected" ) |
| 5865 | return main.FALSE |
| 5866 | except pexpect.EOF: |
| 5867 | main.log.error( self.name + ": EOF exception found" ) |
| 5868 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5869 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5870 | except Exception: |
| 5871 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5872 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5873 | |
| 5874 | def interfaces( self, jsonFormat=True ): |
| 5875 | """ |
| 5876 | Description: Returns result of interfaces command. |
| 5877 | Optional: |
| 5878 | * jsonFormat: enable json formatting of output |
| 5879 | Returns: |
| 5880 | The output of the command or None on error. |
| 5881 | """ |
| 5882 | try: |
| 5883 | cmdStr = "interfaces" |
| 5884 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5885 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5886 | cmdStr += " -j" |
| 5887 | handle = self.sendline( cmdStr ) |
| 5888 | assert handle is not None, "Error in sendline" |
| 5889 | assert "Command not found:" not in handle, handle |
| 5890 | return handle |
| 5891 | except AssertionError: |
| 5892 | main.log.exception( "" ) |
| 5893 | return None |
| 5894 | except TypeError: |
| 5895 | main.log.exception( self.name + ": Object not as expected" ) |
| 5896 | return None |
| 5897 | except pexpect.EOF: |
| 5898 | main.log.error( self.name + ": EOF exception found" ) |
| 5899 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5900 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5901 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5902 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5903 | return None |
| 5904 | except Exception: |
| 5905 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5906 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5907 | |
| 5908 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5909 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5910 | Get the timestamp of searchTerm from karaf log. |
| 5911 | |
| 5912 | Arguments: |
| 5913 | splitTerm_before and splitTerm_after: |
| 5914 | |
| 5915 | The terms that split the string that contains the timeStamp of |
| 5916 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5917 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5918 | and the splitTerm_after is "x" |
| 5919 | |
| 5920 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5921 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5922 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5923 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5924 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5925 | return main.ERROR |
| 5926 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5927 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5928 | main.log.warn( "Captured timestamp string is empty" ) |
| 5929 | return main.ERROR |
| 5930 | lines = lines[ 0 ] |
| 5931 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5932 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5933 | # get the target value |
| 5934 | line = lines.split( splitTerm_before ) |
| 5935 | key = line[ 1 ].split( splitTerm_after ) |
| 5936 | return int( key[ 0 ] ) |
| 5937 | except IndexError: |
| 5938 | main.log.warn( "Index Error!" ) |
| 5939 | return main.ERROR |
| 5940 | except AssertionError: |
| 5941 | main.log.warn( "Search Term Not Found " ) |
| 5942 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5943 | |
| 5944 | def workQueueAdd( self, queueName, value ): |
| 5945 | """ |
| 5946 | CLI command to add a string to the specified Work Queue. |
| 5947 | This function uses the distributed primitives test app, which |
| 5948 | gives some cli access to distributed primitives for testing |
| 5949 | purposes only. |
| 5950 | |
| 5951 | Required arguments: |
| 5952 | queueName - The name of the queue to add to |
| 5953 | value - The value to add to the queue |
| 5954 | returns: |
| 5955 | main.TRUE on success, main.FALSE on failure and |
| 5956 | main.ERROR on error. |
| 5957 | """ |
| 5958 | try: |
| 5959 | queueName = str( queueName ) |
| 5960 | value = str( value ) |
| 5961 | prefix = "work-queue-test" |
| 5962 | operation = "add" |
| 5963 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5964 | output = self.distPrimitivesSend( cmdStr ) |
| 5965 | if "Invalid operation name" in output: |
| 5966 | main.log.warn( output ) |
| 5967 | return main.ERROR |
| 5968 | elif "Done" in output: |
| 5969 | return main.TRUE |
| 5970 | except TypeError: |
| 5971 | main.log.exception( self.name + ": Object not as expected" ) |
| 5972 | return main.ERROR |
| 5973 | except Exception: |
| 5974 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5975 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5976 | |
| 5977 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5978 | """ |
| 5979 | CLI command to add two strings to the specified Work Queue. |
| 5980 | This function uses the distributed primitives test app, which |
| 5981 | gives some cli access to distributed primitives for testing |
| 5982 | purposes only. |
| 5983 | |
| 5984 | Required arguments: |
| 5985 | queueName - The name of the queue to add to |
| 5986 | value1 - The first value to add to the queue |
| 5987 | value2 - The second value to add to the queue |
| 5988 | returns: |
| 5989 | main.TRUE on success, main.FALSE on failure and |
| 5990 | main.ERROR on error. |
| 5991 | """ |
| 5992 | try: |
| 5993 | queueName = str( queueName ) |
| 5994 | value1 = str( value1 ) |
| 5995 | value2 = str( value2 ) |
| 5996 | prefix = "work-queue-test" |
| 5997 | operation = "addMultiple" |
| 5998 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 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 workQueueTakeAndComplete( self, queueName, number=1 ): |
| 6013 | """ |
| 6014 | CLI command to take a value from the specified Work Queue and compelte it. |
| 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 | number - The number of items to take and complete |
| 6022 | returns: |
| 6023 | main.TRUE on success, main.FALSE on failure and |
| 6024 | main.ERROR on error. |
| 6025 | """ |
| 6026 | try: |
| 6027 | queueName = str( queueName ) |
| 6028 | number = str( int( number ) ) |
| 6029 | prefix = "work-queue-test" |
| 6030 | operation = "takeAndComplete" |
| 6031 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 6032 | output = self.distPrimitivesSend( cmdStr ) |
| 6033 | if "Invalid operation name" in output: |
| 6034 | main.log.warn( output ) |
| 6035 | return main.ERROR |
| 6036 | elif "Done" in output: |
| 6037 | return main.TRUE |
| 6038 | except TypeError: |
| 6039 | main.log.exception( self.name + ": Object not as expected" ) |
| 6040 | return main.ERROR |
| 6041 | except Exception: |
| 6042 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6043 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6044 | |
| 6045 | def workQueueDestroy( self, queueName ): |
| 6046 | """ |
| 6047 | CLI command to destroy the specified Work Queue. |
| 6048 | This function uses the distributed primitives test app, which |
| 6049 | gives some cli access to distributed primitives for testing |
| 6050 | purposes only. |
| 6051 | |
| 6052 | Required arguments: |
| 6053 | queueName - The name of the queue to add to |
| 6054 | returns: |
| 6055 | main.TRUE on success, main.FALSE on failure and |
| 6056 | main.ERROR on error. |
| 6057 | """ |
| 6058 | try: |
| 6059 | queueName = str( queueName ) |
| 6060 | prefix = "work-queue-test" |
| 6061 | operation = "destroy" |
| 6062 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6063 | output = self.distPrimitivesSend( cmdStr ) |
| 6064 | if "Invalid operation name" in output: |
| 6065 | main.log.warn( output ) |
| 6066 | return main.ERROR |
| 6067 | return main.TRUE |
| 6068 | except TypeError: |
| 6069 | main.log.exception( self.name + ": Object not as expected" ) |
| 6070 | return main.ERROR |
| 6071 | except Exception: |
| 6072 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6073 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6074 | |
| 6075 | def workQueueTotalPending( self, queueName ): |
| 6076 | """ |
| 6077 | CLI command to get the Total Pending items of the specified Work Queue. |
| 6078 | This function uses the distributed primitives test app, which |
| 6079 | gives some cli access to distributed primitives for testing |
| 6080 | purposes only. |
| 6081 | |
| 6082 | Required arguments: |
| 6083 | queueName - The name of the queue to add to |
| 6084 | returns: |
| 6085 | The number of Pending items in the specified work queue or |
| 6086 | None on error |
| 6087 | """ |
| 6088 | try: |
| 6089 | queueName = str( queueName ) |
| 6090 | prefix = "work-queue-test" |
| 6091 | operation = "totalPending" |
| 6092 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6093 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6094 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6095 | output = output.split( "\r\n" )[ -1 ] |
| 6096 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6097 | pattern = r'\d+' |
| 6098 | if "Invalid operation name" in output: |
| 6099 | main.log.warn( output ) |
| 6100 | return None |
| 6101 | else: |
| 6102 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6103 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6104 | except ( AttributeError, TypeError ): |
| 6105 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6106 | return None |
| 6107 | except Exception: |
| 6108 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6109 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6110 | |
| 6111 | def workQueueTotalCompleted( self, queueName ): |
| 6112 | """ |
| 6113 | CLI command to get the Total Completed items of the specified Work Queue. |
| 6114 | This function uses the distributed primitives test app, which |
| 6115 | gives some cli access to distributed primitives for testing |
| 6116 | purposes only. |
| 6117 | |
| 6118 | Required arguments: |
| 6119 | queueName - The name of the queue to add to |
| 6120 | returns: |
| 6121 | The number of complete items in the specified work queue or |
| 6122 | None on error |
| 6123 | """ |
| 6124 | try: |
| 6125 | queueName = str( queueName ) |
| 6126 | prefix = "work-queue-test" |
| 6127 | operation = "totalCompleted" |
| 6128 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6129 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6130 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6131 | output = output.split( "\r\n" )[ -1 ] |
| 6132 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6133 | pattern = r'\d+' |
| 6134 | if "Invalid operation name" in output: |
| 6135 | main.log.warn( output ) |
| 6136 | return None |
| 6137 | else: |
| 6138 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6139 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6140 | except ( AttributeError, TypeError ): |
| 6141 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6142 | return None |
| 6143 | except Exception: |
| 6144 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6145 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6146 | |
| 6147 | def workQueueTotalInProgress( self, queueName ): |
| 6148 | """ |
| 6149 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 6150 | This function uses the distributed primitives test app, which |
| 6151 | gives some cli access to distributed primitives for testing |
| 6152 | purposes only. |
| 6153 | |
| 6154 | Required arguments: |
| 6155 | queueName - The name of the queue to add to |
| 6156 | returns: |
| 6157 | The number of In Progress items in the specified work queue or |
| 6158 | None on error |
| 6159 | """ |
| 6160 | try: |
| 6161 | queueName = str( queueName ) |
| 6162 | prefix = "work-queue-test" |
| 6163 | operation = "totalInProgress" |
| 6164 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 6165 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 22e94ce | 2019-01-15 14:52:17 -0800 | [diff] [blame] | 6166 | main.log.debug( self.name + ": work queue unparsed: " + output ) |
| 6167 | output = output.split( "\r\n" )[ -1 ] |
| 6168 | main.log.debug( self.name + ": work queue parsed: " + output ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6169 | pattern = r'\d+' |
| 6170 | if "Invalid operation name" in output: |
| 6171 | main.log.warn( output ) |
| 6172 | return None |
| 6173 | else: |
| 6174 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 6175 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 6176 | except ( AttributeError, TypeError ): |
| 6177 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 6178 | return None |
| 6179 | except Exception: |
| 6180 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 6181 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 6182 | |
| 6183 | def events( self, args='-a' ): |
| 6184 | """ |
| 6185 | Description: Returns events -a command output |
| 6186 | Optional: |
| 6187 | add other arguments |
| 6188 | """ |
| 6189 | try: |
| 6190 | cmdStr = "events" |
| 6191 | if args: |
| 6192 | cmdStr += " " + args |
| 6193 | handle = self.sendline( cmdStr ) |
| 6194 | assert handle is not None, "Error in sendline" |
| 6195 | assert "Command not found:" not in handle, handle |
| 6196 | return handle |
| 6197 | except AssertionError: |
| 6198 | main.log.exception( "" ) |
| 6199 | return None |
| 6200 | except TypeError: |
| 6201 | main.log.exception( self.name + ": Object not as expected" ) |
| 6202 | return None |
| 6203 | except pexpect.EOF: |
| 6204 | main.log.error( self.name + ": EOF exception found" ) |
| 6205 | main.log.error( self.name + ": " + self.handle.before ) |
| 6206 | main.cleanAndExit() |
| 6207 | except Exception: |
| 6208 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6209 | main.cleanAndExit() |
| 6210 | |
| 6211 | def getMaster( self, deviceID ): |
| 6212 | """ |
| 6213 | Description: Obtains current master using "roles" command for a specific deviceID |
| 6214 | """ |
| 6215 | try: |
| 6216 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 6217 | except AssertionError: |
| 6218 | main.log.exception( "" ) |
| 6219 | return None |
| 6220 | except TypeError: |
| 6221 | main.log.exception( self.name + ": Object not as expected" ) |
| 6222 | return None |
| 6223 | except pexpect.EOF: |
| 6224 | main.log.error( self.name + ": EOF exception found" ) |
| 6225 | main.log.error( self.name + ": " + self.handle.before ) |
| 6226 | main.cleanAndExit() |
| 6227 | except Exception: |
| 6228 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 6229 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 6230 | |
| 6231 | def issu( self ): |
| 6232 | """ |
| 6233 | Short summary of In-Service Software Upgrade status |
| 6234 | |
| 6235 | Returns the output of the cli command or None on Error |
| 6236 | """ |
| 6237 | try: |
| 6238 | cmdStr = "issu" |
| 6239 | handle = self.sendline( cmdStr ) |
| 6240 | assert handle is not None, "Error in sendline" |
| 6241 | assert "Command not found:" not in handle, handle |
| 6242 | assert "Unsupported command:" not in handle, handle |
| 6243 | return handle |
| 6244 | except AssertionError: |
| 6245 | main.log.exception( "" ) |
| 6246 | return None |
| 6247 | except TypeError: |
| 6248 | main.log.exception( self.name + ": Object not as expected" ) |
| 6249 | return None |
| 6250 | except pexpect.EOF: |
| 6251 | main.log.error( self.name + ": EOF exception found" ) |
| 6252 | main.log.error( self.name + ": " + self.handle.before ) |
| 6253 | main.cleanAndExit() |
| 6254 | except Exception: |
| 6255 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6256 | main.cleanAndExit() |
| 6257 | |
| 6258 | def issuInit( self ): |
| 6259 | """ |
| 6260 | Initiates an In-Service Software Upgrade |
| 6261 | |
| 6262 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6263 | """ |
| 6264 | try: |
| 6265 | cmdStr = "issu init" |
| 6266 | handle = self.sendline( cmdStr ) |
| 6267 | assert handle is not None, "Error in sendline" |
| 6268 | assert "Command not found:" not in handle, handle |
| 6269 | assert "Unsupported command:" not in handle, handle |
| 6270 | if "Initialized" in handle: |
| 6271 | return main.TRUE |
| 6272 | else: |
| 6273 | return main.FALSE |
| 6274 | except AssertionError: |
| 6275 | main.log.exception( "" ) |
| 6276 | return main.ERROR |
| 6277 | except TypeError: |
| 6278 | main.log.exception( self.name + ": Object not as expected" ) |
| 6279 | return main.ERROR |
| 6280 | except pexpect.EOF: |
| 6281 | main.log.error( self.name + ": EOF exception found" ) |
| 6282 | main.log.error( self.name + ": " + self.handle.before ) |
| 6283 | main.cleanAndExit() |
| 6284 | except Exception: |
| 6285 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6286 | main.cleanAndExit() |
| 6287 | |
| 6288 | def issuUpgrade( self ): |
| 6289 | """ |
| 6290 | Transitions stores to upgraded nodes |
| 6291 | |
| 6292 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6293 | """ |
| 6294 | try: |
| 6295 | cmdStr = "issu upgrade" |
| 6296 | handle = self.sendline( cmdStr ) |
| 6297 | assert handle is not None, "Error in sendline" |
| 6298 | assert "Command not found:" not in handle, handle |
| 6299 | assert "Unsupported command:" not in handle, handle |
| 6300 | if "Upgraded" in handle: |
| 6301 | return main.TRUE |
| 6302 | else: |
| 6303 | return main.FALSE |
| 6304 | except AssertionError: |
| 6305 | main.log.exception( "" ) |
| 6306 | return main.ERROR |
| 6307 | except TypeError: |
| 6308 | main.log.exception( self.name + ": Object not as expected" ) |
| 6309 | return main.ERROR |
| 6310 | except pexpect.EOF: |
| 6311 | main.log.error( self.name + ": EOF exception found" ) |
| 6312 | main.log.error( self.name + ": " + self.handle.before ) |
| 6313 | main.cleanAndExit() |
| 6314 | except Exception: |
| 6315 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6316 | main.cleanAndExit() |
| 6317 | |
| 6318 | def issuCommit( self ): |
| 6319 | """ |
| 6320 | Finalizes an In-Service Software Upgrade |
| 6321 | |
| 6322 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6323 | """ |
| 6324 | try: |
| 6325 | cmdStr = "issu commit" |
| 6326 | handle = self.sendline( cmdStr ) |
| 6327 | assert handle is not None, "Error in sendline" |
| 6328 | assert "Command not found:" not in handle, handle |
| 6329 | assert "Unsupported command:" not in handle, handle |
| 6330 | # TODO: Check the version returned by this command |
| 6331 | if "Committed version" in handle: |
| 6332 | return main.TRUE |
| 6333 | else: |
| 6334 | return main.FALSE |
| 6335 | except AssertionError: |
| 6336 | main.log.exception( "" ) |
| 6337 | return main.ERROR |
| 6338 | except TypeError: |
| 6339 | main.log.exception( self.name + ": Object not as expected" ) |
| 6340 | return main.ERROR |
| 6341 | except pexpect.EOF: |
| 6342 | main.log.error( self.name + ": EOF exception found" ) |
| 6343 | main.log.error( self.name + ": " + self.handle.before ) |
| 6344 | main.cleanAndExit() |
| 6345 | except Exception: |
| 6346 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6347 | main.cleanAndExit() |
| 6348 | |
| 6349 | def issuRollback( self ): |
| 6350 | """ |
| 6351 | Rolls back an In-Service Software Upgrade |
| 6352 | |
| 6353 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6354 | """ |
| 6355 | try: |
| 6356 | cmdStr = "issu rollback" |
| 6357 | handle = self.sendline( cmdStr ) |
| 6358 | assert handle is not None, "Error in sendline" |
| 6359 | assert "Command not found:" not in handle, handle |
| 6360 | assert "Unsupported command:" not in handle, handle |
| 6361 | # TODO: Check the version returned by this command |
| 6362 | if "Rolled back to version" in handle: |
| 6363 | return main.TRUE |
| 6364 | else: |
| 6365 | return main.FALSE |
| 6366 | except AssertionError: |
| 6367 | main.log.exception( "" ) |
| 6368 | return main.ERROR |
| 6369 | except TypeError: |
| 6370 | main.log.exception( self.name + ": Object not as expected" ) |
| 6371 | return main.ERROR |
| 6372 | except pexpect.EOF: |
| 6373 | main.log.error( self.name + ": EOF exception found" ) |
| 6374 | main.log.error( self.name + ": " + self.handle.before ) |
| 6375 | main.cleanAndExit() |
| 6376 | except Exception: |
| 6377 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6378 | main.cleanAndExit() |
| 6379 | |
| 6380 | def issuReset( self ): |
| 6381 | """ |
| 6382 | Resets the In-Service Software Upgrade status after a rollback |
| 6383 | |
| 6384 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6385 | """ |
| 6386 | try: |
| 6387 | cmdStr = "issu reset" |
| 6388 | handle = self.sendline( cmdStr ) |
| 6389 | assert handle is not None, "Error in sendline" |
| 6390 | assert "Command not found:" not in handle, handle |
| 6391 | assert "Unsupported command:" not in handle, handle |
| 6392 | # TODO: Check the version returned by this command |
| 6393 | if "Reset version" in handle: |
| 6394 | return main.TRUE |
| 6395 | else: |
| 6396 | return main.FALSE |
| 6397 | except AssertionError: |
| 6398 | main.log.exception( "" ) |
| 6399 | return main.ERROR |
| 6400 | except TypeError: |
| 6401 | main.log.exception( self.name + ": Object not as expected" ) |
| 6402 | return main.ERROR |
| 6403 | except pexpect.EOF: |
| 6404 | main.log.error( self.name + ": EOF exception found" ) |
| 6405 | main.log.error( self.name + ": " + self.handle.before ) |
| 6406 | main.cleanAndExit() |
| 6407 | except Exception: |
| 6408 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6409 | main.cleanAndExit() |
| 6410 | |
| 6411 | def issuStatus( self ): |
| 6412 | """ |
| 6413 | Status of an In-Service Software Upgrade |
| 6414 | |
| 6415 | Returns the output of the cli command or None on Error |
| 6416 | """ |
| 6417 | try: |
| 6418 | cmdStr = "issu status" |
| 6419 | handle = self.sendline( cmdStr ) |
| 6420 | assert handle is not None, "Error in sendline" |
| 6421 | assert "Command not found:" not in handle, handle |
| 6422 | assert "Unsupported command:" not in handle, handle |
| 6423 | return handle |
| 6424 | except AssertionError: |
| 6425 | main.log.exception( "" ) |
| 6426 | return None |
| 6427 | except TypeError: |
| 6428 | main.log.exception( self.name + ": Object not as expected" ) |
| 6429 | return None |
| 6430 | except pexpect.EOF: |
| 6431 | main.log.error( self.name + ": EOF exception found" ) |
| 6432 | main.log.error( self.name + ": " + self.handle.before ) |
| 6433 | main.cleanAndExit() |
| 6434 | except Exception: |
| 6435 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6436 | main.cleanAndExit() |
| 6437 | |
| 6438 | def issuVersion( self ): |
| 6439 | """ |
| 6440 | Get the version of an In-Service Software Upgrade |
| 6441 | |
| 6442 | Returns the output of the cli command or None on Error |
| 6443 | """ |
| 6444 | try: |
| 6445 | cmdStr = "issu version" |
| 6446 | handle = self.sendline( cmdStr ) |
| 6447 | assert handle is not None, "Error in sendline" |
| 6448 | assert "Command not found:" not in handle, handle |
| 6449 | assert "Unsupported command:" not in handle, handle |
| 6450 | return handle |
| 6451 | except AssertionError: |
| 6452 | main.log.exception( "" ) |
| 6453 | return None |
| 6454 | except TypeError: |
| 6455 | main.log.exception( self.name + ": Object not as expected" ) |
| 6456 | return None |
| 6457 | except pexpect.EOF: |
| 6458 | main.log.error( self.name + ": EOF exception found" ) |
| 6459 | main.log.error( self.name + ": " + self.handle.before ) |
| 6460 | main.cleanAndExit() |
| 6461 | except Exception: |
| 6462 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6463 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6464 | |
| 6465 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6466 | """ |
| 6467 | Create a multicast route by calling 'mcast-join' command |
| 6468 | sIP: source IP of the multicast route |
| 6469 | groupIP: group IP of the multicast route |
| 6470 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6471 | dPorts: a list of destination ports of the multicast route |
| 6472 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6473 | """ |
| 6474 | try: |
| 6475 | cmdStr = "mcast-join" |
| 6476 | cmdStr += " " + str( sIP ) |
| 6477 | cmdStr += " " + str( groupIP ) |
| 6478 | cmdStr += " " + str( sPort ) |
| 6479 | assert isinstance( dPorts, list ) |
| 6480 | for dPort in dPorts: |
| 6481 | cmdStr += " " + str( dPort ) |
| 6482 | handle = self.sendline( cmdStr ) |
| 6483 | assert handle is not None, "Error in sendline" |
| 6484 | assert "Command not found:" not in handle, handle |
| 6485 | assert "Unsupported command:" not in handle, handle |
| 6486 | assert "Error executing command" not in handle, handle |
| 6487 | if "Added the mcast route" in handle: |
| 6488 | return main.TRUE |
| 6489 | else: |
| 6490 | return main.FALSE |
| 6491 | except AssertionError: |
| 6492 | main.log.exception( "" ) |
| 6493 | return None |
| 6494 | except TypeError: |
| 6495 | main.log.exception( self.name + ": Object not as expected" ) |
| 6496 | return None |
| 6497 | except pexpect.EOF: |
| 6498 | main.log.error( self.name + ": EOF exception found" ) |
| 6499 | main.log.error( self.name + ": " + self.handle.before ) |
| 6500 | main.cleanAndExit() |
| 6501 | except Exception: |
| 6502 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6503 | main.cleanAndExit() |
| 6504 | |
| 6505 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6506 | """ |
| 6507 | Delete a multicast route by calling 'mcast-delete' command |
| 6508 | sIP: source IP of the multicast route |
| 6509 | groupIP: group IP of the multicast route |
| 6510 | dPorts: a list of destination ports of the multicast route |
| 6511 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6512 | """ |
| 6513 | try: |
| 6514 | cmdStr = "mcast-delete" |
| 6515 | cmdStr += " " + str( sIP ) |
| 6516 | cmdStr += " " + str( groupIP ) |
| 6517 | assert isinstance( dPorts, list ) |
| 6518 | for dPort in dPorts: |
| 6519 | cmdStr += " " + str( dPort ) |
| 6520 | handle = self.sendline( cmdStr ) |
| 6521 | assert handle is not None, "Error in sendline" |
| 6522 | assert "Command not found:" not in handle, handle |
| 6523 | assert "Unsupported command:" not in handle, handle |
| 6524 | assert "Error executing command" not in handle, handle |
| 6525 | if "Updated the mcast route" in handle: |
| 6526 | return main.TRUE |
| 6527 | else: |
| 6528 | return main.FALSE |
| 6529 | except AssertionError: |
| 6530 | main.log.exception( "" ) |
| 6531 | return None |
| 6532 | except TypeError: |
| 6533 | main.log.exception( self.name + ": Object not as expected" ) |
| 6534 | return None |
| 6535 | except pexpect.EOF: |
| 6536 | main.log.error( self.name + ": EOF exception found" ) |
| 6537 | main.log.error( self.name + ": " + self.handle.before ) |
| 6538 | main.cleanAndExit() |
| 6539 | except Exception: |
| 6540 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6541 | main.cleanAndExit() |
| 6542 | |
| 6543 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6544 | """ |
| 6545 | Create a multicast route by calling 'mcast-host-join' command |
| 6546 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6547 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6548 | 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] | 6549 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6550 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6551 | """ |
| 6552 | try: |
| 6553 | cmdStr = "mcast-host-join" |
| 6554 | cmdStr += " -sAddr " + str( sAddr ) |
| 6555 | cmdStr += " -gAddr " + str( gAddr ) |
| 6556 | assert isinstance( srcs, list ) |
| 6557 | for src in srcs: |
| 6558 | cmdStr += " -srcs " + str( src ) |
| 6559 | assert isinstance( sinks, list ) |
| 6560 | for sink in sinks: |
| 6561 | cmdStr += " -sinks " + str( sink ) |
| 6562 | handle = self.sendline( cmdStr ) |
| 6563 | assert handle is not None, "Error in sendline" |
| 6564 | assert "Command not found:" not in handle, handle |
| 6565 | assert "Unsupported command:" not in handle, handle |
| 6566 | assert "Error executing command" not in handle, handle |
| 6567 | if "Added the mcast route" in handle: |
| 6568 | return main.TRUE |
| 6569 | else: |
| 6570 | return main.FALSE |
| 6571 | except AssertionError: |
| 6572 | main.log.exception( "" ) |
| 6573 | return None |
| 6574 | except TypeError: |
| 6575 | main.log.exception( self.name + ": Object not as expected" ) |
| 6576 | return None |
| 6577 | except pexpect.EOF: |
| 6578 | main.log.error( self.name + ": EOF exception found" ) |
| 6579 | main.log.error( self.name + ": " + self.handle.before ) |
| 6580 | main.cleanAndExit() |
| 6581 | except Exception: |
| 6582 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6583 | main.cleanAndExit() |
| 6584 | |
| 6585 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6586 | """ |
| 6587 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6588 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6589 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6590 | 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] | 6591 | will delete the route if not specified |
| 6592 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6593 | """ |
| 6594 | try: |
| 6595 | cmdStr = "mcast-host-delete" |
| 6596 | cmdStr += " -sAddr " + str( sAddr ) |
| 6597 | cmdStr += " -gAddr " + str( gAddr ) |
| 6598 | if host: |
| 6599 | cmdStr += " -h " + str( host ) |
| 6600 | handle = self.sendline( cmdStr ) |
| 6601 | assert handle is not None, "Error in sendline" |
| 6602 | assert "Command not found:" not in handle, handle |
| 6603 | assert "Unsupported command:" not in handle, handle |
| 6604 | assert "Error executing command" not in handle, handle |
| 6605 | if "Updated the mcast route" in handle: |
| 6606 | return main.TRUE |
| 6607 | elif "Deleted the mcast route" in handle: |
| 6608 | return main.TRUE |
| 6609 | else: |
| 6610 | return main.FALSE |
| 6611 | except AssertionError: |
| 6612 | main.log.exception( "" ) |
| 6613 | return None |
| 6614 | except TypeError: |
| 6615 | main.log.exception( self.name + ": Object not as expected" ) |
| 6616 | return None |
| 6617 | except pexpect.EOF: |
| 6618 | main.log.error( self.name + ": EOF exception found" ) |
| 6619 | main.log.error( self.name + ": " + self.handle.before ) |
| 6620 | main.cleanAndExit() |
| 6621 | except Exception: |
| 6622 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6623 | main.cleanAndExit() |
| 6624 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6625 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6626 | """ |
| 6627 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6628 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6629 | gAddr: specifies multicast group address |
| 6630 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6631 | will delete the route if not specified |
| 6632 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6633 | """ |
| 6634 | try: |
| 6635 | cmdStr = "mcast-sink-delete" |
| 6636 | cmdStr += " -sAddr " + str( sAddr ) |
| 6637 | cmdStr += " -gAddr " + str( gAddr ) |
| 6638 | if sink: |
| 6639 | cmdStr += " -s " + str( sink ) |
| 6640 | handle = self.sendline( cmdStr ) |
| 6641 | assert handle is not None, "Error in sendline" |
| 6642 | assert "Command not found:" not in handle, handle |
| 6643 | assert "Unsupported command:" not in handle, handle |
| 6644 | assert "Error executing command" not in handle, handle |
| 6645 | if "Updated the mcast route" in handle: |
| 6646 | return main.TRUE |
| 6647 | elif "Deleted the mcast route" in handle: |
| 6648 | return main.TRUE |
| 6649 | else: |
| 6650 | return main.FALSE |
| 6651 | except AssertionError: |
| 6652 | main.log.exception( "" ) |
| 6653 | return None |
| 6654 | except TypeError: |
| 6655 | main.log.exception( self.name + ": Object not as expected" ) |
| 6656 | return None |
| 6657 | except pexpect.EOF: |
| 6658 | main.log.error( self.name + ": EOF exception found" ) |
| 6659 | main.log.error( self.name + ": " + self.handle.before ) |
| 6660 | main.cleanAndExit() |
| 6661 | except Exception: |
| 6662 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6663 | main.cleanAndExit() |
| 6664 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6665 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6666 | """ |
| 6667 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6668 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6669 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6670 | 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] | 6671 | will delete the route if not specified |
| 6672 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6673 | """ |
| 6674 | try: |
| 6675 | cmdStr = "mcast-source-delete" |
| 6676 | cmdStr += " -sAddr " + str( sAddr ) |
| 6677 | cmdStr += " -gAddr " + str( gAddr ) |
| 6678 | if srcs: |
| 6679 | assert isinstance( srcs, list ) |
| 6680 | for src in srcs: |
| 6681 | cmdStr += " -src " + str( src ) |
| 6682 | handle = self.sendline( cmdStr ) |
| 6683 | assert handle is not None, "Error in sendline" |
| 6684 | assert "Command not found:" not in handle, handle |
| 6685 | assert "Unsupported command:" not in handle, handle |
| 6686 | assert "Error executing command" not in handle, handle |
| 6687 | if "Updated the mcast route" in handle: |
| 6688 | return main.TRUE |
| 6689 | elif "Deleted the mcast route" in handle: |
| 6690 | return main.TRUE |
| 6691 | else: |
| 6692 | return main.FALSE |
| 6693 | except AssertionError: |
| 6694 | main.log.exception( "" ) |
| 6695 | return None |
| 6696 | except TypeError: |
| 6697 | main.log.exception( self.name + ": Object not as expected" ) |
| 6698 | return None |
| 6699 | except pexpect.EOF: |
| 6700 | main.log.error( self.name + ": EOF exception found" ) |
| 6701 | main.log.error( self.name + ": " + self.handle.before ) |
| 6702 | main.cleanAndExit() |
| 6703 | except Exception: |
| 6704 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6705 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6706 | |
| 6707 | def netcfg( self, jsonFormat=True, args="" ): |
| 6708 | """ |
| 6709 | Run netcfg cli command with given args |
| 6710 | """ |
| 6711 | try: |
| 6712 | cmdStr = "netcfg" |
| 6713 | if jsonFormat: |
| 6714 | cmdStr = cmdStr + " -j" |
| 6715 | if args: |
| 6716 | cmdStr = cmdStr + " " + str( args ) |
| 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 | return handle |
| 6723 | except AssertionError: |
| 6724 | main.log.exception( "" ) |
| 6725 | return None |
| 6726 | except TypeError: |
| 6727 | main.log.exception( self.name + ": Object not as expected" ) |
| 6728 | return None |
| 6729 | except pexpect.EOF: |
| 6730 | main.log.error( self.name + ": EOF exception found" ) |
| 6731 | main.log.error( self.name + ": " + self.handle.before ) |
| 6732 | main.cleanAndExit() |
| 6733 | except Exception: |
| 6734 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6735 | main.cleanAndExit() |
| 6736 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6737 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6738 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6739 | 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] | 6740 | Options: |
| 6741 | sAddr: IP address of the source host |
| 6742 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6743 | ipv6: True if hosts are IPv6 |
| 6744 | verbose: return verbose t3 output if True |
| 6745 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6746 | """ |
| 6747 | try: |
| 6748 | # Collect information of both hosts from onos |
| 6749 | hosts = self.hosts() |
| 6750 | hosts = json.loads( hosts ) |
| 6751 | sHost = None |
| 6752 | dHost = None |
| 6753 | for host in hosts: |
| 6754 | if sAddr in host[ "ipAddresses" ]: |
| 6755 | sHost = host |
| 6756 | elif dAddr in host[ "ipAddresses" ]: |
| 6757 | dHost = host |
| 6758 | if sHost and dHost: |
| 6759 | break |
| 6760 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6761 | cmdList = [] |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6762 | if simple: |
| 6763 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6764 | cmdStr = "t3-troubleshoot-simple" |
| 6765 | if verbose: |
| 6766 | cmdStr += " -vv" |
| 6767 | if ipv6: |
| 6768 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6769 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6770 | cmdList.append( cmdStr ) |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6771 | else: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6772 | for location in sHost[ "locations" ]: |
| 6773 | cmdStr = "t3-troubleshoot" |
| 6774 | if verbose: |
| 6775 | cmdStr += " -vv" |
| 6776 | if ipv6: |
| 6777 | cmdStr += " -et ipv6" |
| 6778 | cmdStr += " -s " + str( sAddr ) |
| 6779 | cmdStr += " -sp " + str( location[ "elementId" ] ) + "/" + str( location[ "port" ] ) |
| 6780 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6781 | if sHost[ "vlan" ] != "None": |
| 6782 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6783 | cmdStr += " -d " + str( dAddr ) |
| 6784 | netcfg = self.netcfg( args="devices {}".format( location[ "elementId" ] ) ) |
| 6785 | netcfg = json.loads( netcfg ) |
| 6786 | assert netcfg, "Failed to get netcfg" |
| 6787 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
| 6788 | cmdList.append( cmdStr ) |
| 6789 | return cmdList |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6790 | except AssertionError: |
| 6791 | main.log.exception( "" ) |
| 6792 | return None |
| 6793 | except ( KeyError, TypeError ): |
| 6794 | main.log.exception( self.name + ": Object not as expected" ) |
| 6795 | return None |
| 6796 | except Exception: |
| 6797 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6798 | main.cleanAndExit() |
| 6799 | |
| 6800 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6801 | """ |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6802 | 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] | 6803 | Options: |
| 6804 | sAddr: IP address of the source host |
| 6805 | dAddr: IP address of the destination host |
| 6806 | """ |
| 6807 | try: |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 6808 | cmdList = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6809 | assert cmdList is not None, "composeT3Command returned None" |
| 6810 | t3Output = "" |
| 6811 | for cmdStr in cmdList: |
| 6812 | handle = self.sendline( cmdStr ) |
| 6813 | assert handle is not None, "Error in sendline" |
| 6814 | assert "Command not found:" not in handle, handle |
| 6815 | assert "Unsupported command:" not in handle, handle |
| 6816 | assert "Error executing command" not in handle, handle |
| 6817 | assert "Tracing packet" in handle |
| 6818 | t3Output += handle |
| 6819 | return t3Output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6820 | except AssertionError: |
| 6821 | main.log.exception( "" ) |
| 6822 | return None |
| 6823 | except pexpect.EOF: |
| 6824 | main.log.error( self.name + ": EOF exception found" ) |
| 6825 | main.log.error( self.name + ": " + self.handle.before ) |
| 6826 | main.cleanAndExit() |
| 6827 | except Exception: |
| 6828 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6829 | main.cleanAndExit() |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6830 | |
| 6831 | def prepareForCLI( self, debug=True, maxRetries=120 ): |
| 6832 | """ |
| 6833 | Prepare docker container to connect to onos cli |
| 6834 | """ |
| 6835 | try: |
| 6836 | # Wait for log files to be created |
| 6837 | ready = 0 |
| 6838 | retries = 0 |
| 6839 | while not ready and retries < maxRetries: |
| 6840 | retries += 1 |
| 6841 | self.handle.sendline( "ls -al /root/onos/apache-karaf-*/data/log" ) |
| 6842 | ready = self.handle.expect( [ "No such file or directory", self.dockerPrompt ] ) |
| 6843 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6844 | if not ready: |
| 6845 | self.handle.expect( self.dockerPrompt ) |
| 6846 | time.sleep( 1 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6847 | |
| 6848 | cmdList = [] |
| 6849 | cmdList.append( "apt-get update" ) |
| 6850 | cmdList.append( "apt-get install -y openssh-server" ) |
| 6851 | # Some built in scripts are hardcoded |
| 6852 | cmdList.append( "ln -s /root/onos /opt/onos" ) |
| 6853 | cmdList.append( "ln -s /root/onos/apache-karaf-*/data/log /opt/onos/log" ) |
| 6854 | cmdList.append( "ls -al /opt/onos" ) |
| 6855 | output = "" |
| 6856 | for cmdStr in cmdList: |
| 6857 | self.handle.sendline( cmdStr ) |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 6858 | self.handle.expect( self.dockerPrompt, timeout=420 ) |
Jon Hall | 3c0114c | 2020-08-11 15:07:42 -0700 | [diff] [blame] | 6859 | self.handle.sendline( "" ) |
| 6860 | self.handle.expect( self.dockerPrompt ) |
| 6861 | handle = self.handle.before |
| 6862 | assert "command not found" not in handle, handle |
| 6863 | assert "No such file or directory" not in handle, handle |
| 6864 | output += handle |
| 6865 | if debug: |
| 6866 | main.log.debug( "%s: %s" % ( self.name, output ) ) |
| 6867 | return output |
| 6868 | except AssertionError: |
| 6869 | main.log.exception( "" ) |
| 6870 | return None |
| 6871 | except pexpect.EOF: |
| 6872 | main.log.error( self.name + ": EOF exception found" ) |
| 6873 | main.log.error( self.name + ": " + self.handle.before ) |
| 6874 | main.cleanAndExit() |
| 6875 | except Exception: |
| 6876 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6877 | main.cleanAndExit() |
| 6878 | |
| 6879 | def onosSecureSSH( self, userName="onos", userPWD="rocks" ): |
| 6880 | """ |
| 6881 | Enables secure access to ONOS console |
| 6882 | by removing default users & keys. |
| 6883 | |
| 6884 | bin/onos-user-password onos rocks |
| 6885 | |
| 6886 | Returns: main.TRUE on success and main.FALSE on failure |
| 6887 | """ |
| 6888 | |
| 6889 | try: |
| 6890 | self.handle.sendline( "" ) |
| 6891 | self.handle.expect( self.dockerPrompt ) |
| 6892 | |
| 6893 | self.handle.sendline( "[ ! -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -q" ) |
| 6894 | self.handle.expect( self.dockerPrompt ) |
| 6895 | main.log.debug( "%s: %s%s" % ( self.name, self.handle.before, self.handle.after ) ) |
| 6896 | |
| 6897 | self.handle.sendline( "bin/onos-user-key $(id -un) $(cut -d\\\\ -f2 ~/.ssh/id_rsa.pub)" ) |
| 6898 | self.handle.expect( pexpect.TIMEOUT, timeout=10 ) |
| 6899 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6900 | |
| 6901 | self.handle.sendline( "bin/onos-user-password %s %s" % ( userName, userPWD ) ) |
| 6902 | i = self.handle.expect( [ "usage", |
| 6903 | self.dockerPrompt, |
| 6904 | pexpect.TIMEOUT ] ) |
| 6905 | if i == 0: |
| 6906 | # malformed command |
| 6907 | main.log.warn( self.name + ": Could not parse onos-user-password command" ) |
| 6908 | self.handle.expect( self.dockerPrompt ) |
| 6909 | return main.FALSE |
| 6910 | elif i == 1: |
| 6911 | # Process started |
| 6912 | main.log.info( self.name + ": SSH password added for user " + userName ) |
| 6913 | return main.TRUE |
| 6914 | elif i == 2: |
| 6915 | # timeout |
| 6916 | main.log.error( self.name + ": Failed to secure onos ssh " ) |
| 6917 | main.log.debug( "%s: %s" % ( self.name, self.handle.before ) ) |
| 6918 | except pexpect.EOF: |
| 6919 | main.log.error( self.name + ": EOF exception found" ) |
| 6920 | main.log.error( self.name + ": " + self.handle.before ) |
| 6921 | main.cleanAndExit() |
| 6922 | except Exception: |
| 6923 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6924 | main.cleanAndExit() |
Jon Hall | 62ab675 | 2021-08-22 16:47:43 -0700 | [diff] [blame] | 6925 | |
| 6926 | def routeAdd( self, prefix, nextHop, jsonFormat=True ): |
| 6927 | """ |
| 6928 | Add a route with the given prefix and nextHop |
| 6929 | """ |
| 6930 | try: |
| 6931 | cmdStr = "route-add %s %s %s" % ( "-j" if jsonFormat else "", prefix, nextHop ) |
| 6932 | handle = self.sendline( cmdStr ) |
| 6933 | assert handle is not None, "Error in sendline" |
| 6934 | assert "Command not found:" not in handle, handle |
| 6935 | assert "Unsupported command:" not in handle, handle |
| 6936 | assert "Error executing command" not in handle, handle |
| 6937 | return handle |
| 6938 | except AssertionError: |
| 6939 | main.log.exception( "" ) |
| 6940 | return None |
| 6941 | except TypeError: |
| 6942 | main.log.exception( self.name + ": Object not as expected" ) |
| 6943 | return None |
| 6944 | except pexpect.EOF: |
| 6945 | main.log.error( self.name + ": EOF exception found" ) |
| 6946 | main.log.error( self.name + ": " + self.handle.before ) |
| 6947 | main.cleanAndExit() |
| 6948 | except Exception: |
| 6949 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6950 | main.cleanAndExit() |
| 6951 | |
| 6952 | def routeRemove( self, prefix, nextHop, source=None, jsonFormat=True ): |
| 6953 | """ |
| 6954 | Remove a route with the given prefix and nextHop |
| 6955 | """ |
| 6956 | try: |
| 6957 | if source: |
| 6958 | raise NotImplemented |
| 6959 | cmdStr = "route-remove %s %s %s" % ( "-j" if jsonFormat else "", prefix, nextHop ) |
| 6960 | handle = self.sendline( cmdStr ) |
| 6961 | assert handle is not None, "Error in sendline" |
| 6962 | assert "Command not found:" not in handle, handle |
| 6963 | assert "Unsupported command:" not in handle, handle |
| 6964 | assert "Error executing command" not in handle, handle |
| 6965 | return handle |
| 6966 | except AssertionError: |
| 6967 | main.log.exception( "" ) |
| 6968 | return None |
| 6969 | except TypeError: |
| 6970 | main.log.exception( self.name + ": Object not as expected" ) |
| 6971 | return None |
| 6972 | except pexpect.EOF: |
| 6973 | main.log.error( self.name + ": EOF exception found" ) |
| 6974 | main.log.error( self.name + ": " + self.handle.before ) |
| 6975 | main.cleanAndExit() |
| 6976 | except Exception: |
| 6977 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6978 | main.cleanAndExit() |
| 6979 | |
| 6980 | def routes( self, jsonFormat=True, args="" ): |
| 6981 | """ |
| 6982 | Get routes from ONOS |
| 6983 | """ |
| 6984 | try: |
| 6985 | cmdStr = "routes" |
| 6986 | if jsonFormat: |
| 6987 | cmdStr = cmdStr + " -j" |
| 6988 | handle = self.sendline( cmdStr ) |
| 6989 | assert handle is not None, "Error in sendline" |
| 6990 | assert "Command not found:" not in handle, handle |
| 6991 | assert "Unsupported command:" not in handle, handle |
| 6992 | assert "Error executing command" not in handle, handle |
| 6993 | return handle |
| 6994 | except AssertionError: |
| 6995 | main.log.exception( "" ) |
| 6996 | return None |
| 6997 | except TypeError: |
| 6998 | main.log.exception( self.name + ": Object not as expected" ) |
| 6999 | return None |
| 7000 | except pexpect.EOF: |
| 7001 | main.log.error( self.name + ": EOF exception found" ) |
| 7002 | main.log.error( self.name + ": " + self.handle.before ) |
| 7003 | main.cleanAndExit() |
| 7004 | except Exception: |
| 7005 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 7006 | main.cleanAndExit() |