andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | OCT 13 2014 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5 | Copyright 2014 Open Networking Foundation (ONF) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 6 | |
| 7 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 8 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 9 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 10 | |
| 11 | TestON is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 14 | (at your option) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 15 | |
| 16 | TestON is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 23 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 25 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 26 | This driver enters the onos> prompt to issue commands. |
| 27 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 28 | Please follow the coding style demonstrated by existing |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 29 | functions and document properly. |
| 30 | |
| 31 | If you are a contributor to the driver, please |
| 32 | list your email here for future contact: |
| 33 | |
| 34 | jhall@onlab.us |
| 35 | andrew@onlab.us |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 36 | shreya@onlab.us |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 37 | jeremyr@opennetworking.org |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 38 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 39 | import pexpect |
| 40 | import re |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 41 | import json |
| 42 | import types |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 43 | import time |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 44 | import os |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 45 | from drivers.common.clidriver import CLI |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 46 | from core.graph import Graph |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 47 | from cStringIO import StringIO |
| 48 | from itertools import izip |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 49 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 50 | class OnosCliDriver( CLI ): |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 51 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 52 | def __init__( self ): |
| 53 | """ |
| 54 | Initialize client |
| 55 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 56 | self.name = None |
| 57 | self.home = None |
| 58 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 59 | self.karafUser = None |
| 60 | self.karafPass = None |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 61 | self.graph = Graph() |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 62 | super( OnosCliDriver, self ).__init__() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 63 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 64 | def checkOptions( self, var, defaultVar ): |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 65 | if var is None or var == "": |
| 66 | return defaultVar |
| 67 | return var |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 68 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 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 ] |
| 84 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 85 | self.home = self.checkOptions( self.home, "~/onos" ) |
| 86 | self.karafUser = self.checkOptions( self.karafUser, self.user_name ) |
| 87 | self.karafPass = self.checkOptions( self.karafPass, self.pwd ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 88 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 89 | for key in self.options: |
| 90 | if key == 'onosIp': |
| 91 | self.onosIp = self.options[ 'onosIp' ] |
| 92 | break |
| 93 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 94 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 95 | |
| 96 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 97 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 98 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 99 | else: |
| 100 | main.log.info( self.name + |
| 101 | ": Trying to connect to " + |
| 102 | self.ip_address ) |
| 103 | |
| 104 | except KeyError: |
| 105 | main.log.info( "Invalid host name," + |
| 106 | " connecting to local host instead" ) |
| 107 | self.ip_address = 'localhost' |
| 108 | except Exception as inst: |
| 109 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 110 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 111 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 112 | user_name=self.user_name, |
| 113 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 114 | port=self.port, |
| 115 | pwd=self.pwd, |
| 116 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 117 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 118 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 119 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 120 | if self.handle: |
| 121 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 122 | else: |
| 123 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 124 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 125 | except TypeError: |
| 126 | main.log.exception( self.name + ": Object not as expected" ) |
| 127 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 128 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 129 | main.log.error( self.name + ": EOF exception found" ) |
| 130 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 131 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 132 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 133 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 134 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 135 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 136 | def disconnect( self ): |
| 137 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 138 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 139 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 140 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 141 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 142 | if self.handle: |
| 143 | i = self.logout() |
| 144 | if i == main.TRUE: |
| 145 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 146 | self.handle.expect( self.prompt ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 147 | self.handle.sendline( "exit" ) |
| 148 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 149 | except TypeError: |
| 150 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 151 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 152 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 153 | main.log.error( self.name + ": EOF exception found" ) |
| 154 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 155 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 156 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 157 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 158 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 159 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 160 | response = main.FALSE |
| 161 | return response |
| 162 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 163 | def logout( self ): |
| 164 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 165 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 166 | Returns main.TRUE if exited CLI and |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 167 | main.FALSE on timeout (not guranteed you are disconnected) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 168 | None on TypeError |
| 169 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 170 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 171 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 172 | if self.handle: |
| 173 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 174 | i = self.handle.expect( [ "onos>", self.prompt, pexpect.TIMEOUT ], |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 175 | timeout=10 ) |
| 176 | if i == 0: # In ONOS CLI |
| 177 | self.handle.sendline( "logout" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 178 | j = self.handle.expect( [ self.prompt, |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 179 | "Command not found:", |
| 180 | pexpect.TIMEOUT ] ) |
| 181 | if j == 0: # Successfully logged out |
| 182 | return main.TRUE |
| 183 | elif j == 1 or j == 2: |
| 184 | # ONOS didn't fully load, and logout command isn't working |
| 185 | # or the command timed out |
| 186 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 187 | try: |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 188 | self.handle.expect( self.prompt ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 189 | except pexpect.TIMEOUT: |
| 190 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 191 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 192 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 193 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 194 | repr( self.handle.before ) ) |
| 195 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 196 | elif i == 1: # not in CLI |
| 197 | return main.TRUE |
| 198 | elif i == 3: # Timeout |
| 199 | return main.FALSE |
| 200 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 201 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 202 | except TypeError: |
| 203 | main.log.exception( self.name + ": Object not as expected" ) |
| 204 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 205 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 206 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 207 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 208 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 209 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 210 | main.log.error( self.name + |
| 211 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 212 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 213 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 214 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 215 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 216 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 217 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 218 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 219 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 220 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 221 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 222 | try: |
| 223 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 224 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 225 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 226 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 227 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 228 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 229 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 230 | # and that this driver will have to change accordingly |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 231 | self.handle.expect( str( cellname ) ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 232 | handleBefore = self.handle.before |
| 233 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 234 | # Get the rest of the handle |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 235 | self.handle.sendline( "" ) |
| 236 | self.handle.expect( self.prompt ) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 237 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 238 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 239 | main.log.info( "Cell call returned: " + handleBefore + |
| 240 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 241 | |
| 242 | return main.TRUE |
| 243 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 244 | except TypeError: |
| 245 | main.log.exception( self.name + ": Object not as expected" ) |
| 246 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 247 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 248 | main.log.error( self.name + ": eof exception found" ) |
| 249 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 250 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 251 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 252 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 253 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 254 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 255 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 256 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 257 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 258 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 259 | by user would be used to set the current karaf shell idle timeout. |
| 260 | 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] | 261 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 262 | Below is an example to start a session with 60 seconds idle timeout |
| 263 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 264 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 265 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 266 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 267 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 268 | Note: karafTimeout is left as str so that this could be read |
| 269 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 270 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 271 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 272 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 273 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 274 | self.handle.sendline( "" ) |
| 275 | x = self.handle.expect( [ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 276 | self.prompt, "onos>" ], commandlineTimeout ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 277 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 278 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 279 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 280 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 281 | # Not in CLI so login |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 282 | if waitForStart: |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 283 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
| 284 | startCliCommand = "onos-wait-for-start " |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 285 | else: |
| 286 | startCliCommand = "onos " |
| 287 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 288 | i = self.handle.expect( [ |
| 289 | "onos>", |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 290 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 291 | |
| 292 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 293 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 294 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 295 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 296 | "config:property-set -p org.apache.karaf.shell\ |
| 297 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 298 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 299 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 300 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 301 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 302 | return main.TRUE |
| 303 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 304 | # If failed, send ctrl+c to process and try again |
| 305 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 306 | self.handle.send( "\x03" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 307 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 308 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 309 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 310 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 311 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 312 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 313 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 314 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 315 | "config:property-set -p org.apache.karaf.shell\ |
| 316 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 317 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 318 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 319 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 320 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 321 | return main.TRUE |
| 322 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 323 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 324 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 325 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 326 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 327 | except TypeError: |
| 328 | main.log.exception( self.name + ": Object not as expected" ) |
| 329 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 330 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 331 | main.log.error( self.name + ": EOF exception found" ) |
| 332 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 333 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 334 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 335 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 336 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 337 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 338 | def startCellCli( self, karafTimeout="", |
| 339 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 340 | """ |
| 341 | Start CLI on onos ecll handle. |
| 342 | |
| 343 | karafTimeout is an optional argument. karafTimeout value passed |
| 344 | by user would be used to set the current karaf shell idle timeout. |
| 345 | Note that when ever this property is modified the shell will exit and |
| 346 | the subsequent login would reflect new idle timeout. |
| 347 | Below is an example to start a session with 60 seconds idle timeout |
| 348 | ( input value is in milliseconds ): |
| 349 | |
| 350 | tValue = "60000" |
| 351 | |
| 352 | Note: karafTimeout is left as str so that this could be read |
| 353 | and passed to startOnosCli from PARAMS file as str. |
| 354 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 355 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 356 | try: |
| 357 | self.handle.sendline( "" ) |
| 358 | x = self.handle.expect( [ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 359 | self.prompt, "onos>" ], commandlineTimeout ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 360 | |
| 361 | if x == 1: |
| 362 | main.log.info( "ONOS cli is already running" ) |
| 363 | return main.TRUE |
| 364 | |
Jeremy Ronquillo | ec916a4 | 2018-02-02 13:05:57 -0800 | [diff] [blame] | 365 | # Wait for onos start ( onos-wait-for-start ) and enter onos cli |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 366 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 367 | i = self.handle.expect( [ |
| 368 | "onos>", |
| 369 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 370 | |
| 371 | if i == 0: |
| 372 | main.log.info( self.name + " CLI Started successfully" ) |
| 373 | if karafTimeout: |
| 374 | self.handle.sendline( |
| 375 | "config:property-set -p org.apache.karaf.shell\ |
| 376 | sshIdleTimeout " + |
| 377 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 378 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 379 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 380 | self.handle.expect( "onos>" ) |
| 381 | return main.TRUE |
| 382 | else: |
| 383 | # If failed, send ctrl+c to process and try again |
| 384 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 385 | self.handle.send( "\x03" ) |
| 386 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 387 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 388 | timeout=30 ) |
| 389 | if i == 0: |
| 390 | main.log.info( self.name + " CLI Started " + |
| 391 | "successfully after retry attempt" ) |
| 392 | if karafTimeout: |
| 393 | self.handle.sendline( |
| 394 | "config:property-set -p org.apache.karaf.shell\ |
| 395 | sshIdleTimeout " + |
| 396 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 397 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 398 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 399 | self.handle.expect( "onos>" ) |
| 400 | return main.TRUE |
| 401 | else: |
| 402 | main.log.error( "Connection to CLI " + |
| 403 | self.name + " timeout" ) |
| 404 | return main.FALSE |
| 405 | |
| 406 | except TypeError: |
| 407 | main.log.exception( self.name + ": Object not as expected" ) |
| 408 | return None |
| 409 | except pexpect.EOF: |
| 410 | main.log.error( self.name + ": EOF exception found" ) |
| 411 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 412 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 413 | except Exception: |
| 414 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 415 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 416 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 417 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 418 | """ |
| 419 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 420 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 421 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 422 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 423 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 424 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 425 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 426 | """ |
| 427 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 428 | lvlStr = "" |
| 429 | if level: |
| 430 | lvlStr = "--level=" + level |
| 431 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 432 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 433 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 434 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 435 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 436 | response = self.handle.before |
| 437 | if re.search( "Error", response ): |
| 438 | return main.FALSE |
| 439 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 440 | except pexpect.TIMEOUT: |
| 441 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 442 | if noExit: |
| 443 | main.cleanup() |
| 444 | return None |
| 445 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 446 | main.cleanAndExit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 447 | except pexpect.EOF: |
| 448 | main.log.error( self.name + ": EOF exception found" ) |
| 449 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 450 | if noExit: |
| 451 | main.cleanup() |
| 452 | return None |
| 453 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 454 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 455 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 456 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 457 | if noExit: |
| 458 | main.cleanup() |
| 459 | return None |
| 460 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 461 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 462 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 463 | def sendline( self, cmdStr, showResponse=False, debug=False, timeout=10, noExit=False, dollarSign=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 464 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 465 | Send a completely user specified string to |
| 466 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 467 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 468 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 469 | if noExit is True, TestON will not exit, and return None |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 470 | if dollarSign is True, TestON will not expect for '$' as a new CLI or onos> prompt |
| 471 | since '$' can be in the output. |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 472 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 473 | Warning: There are no sanity checking to commands |
| 474 | sent using this method. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 475 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 476 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 477 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 478 | # Try to reconnect if disconnected from cli |
| 479 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 480 | i = self.handle.expect( [ "onos>", self.prompt, pexpect.TIMEOUT ] ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 481 | if i == 1: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 482 | main.log.error( self.name + ": onos cli session closed. " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 483 | if self.onosIp: |
| 484 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 485 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 486 | if reconnectResult: |
| 487 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 488 | else: |
| 489 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 490 | if noExit: |
| 491 | return None |
| 492 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 493 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 494 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 495 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 496 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 497 | main.log.warn( "Timeout when testing cli responsiveness" ) |
| 498 | main.log.debug( self.handle.before ) |
| 499 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 500 | self.handle.expect( "onos>" ) |
| 501 | |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 502 | if debug: |
| 503 | # NOTE: This adds and average of .4 seconds per call |
| 504 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 505 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 506 | self.handle.sendline( cmdStr ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 507 | if dollarSign: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 508 | i = self.handle.expect( [ "onos>" ], timeout ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 509 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 510 | i = self.handle.expect( [ "onos>", self.prompt ], timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 511 | response = self.handle.before |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 512 | # TODO: do something with i |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 513 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 514 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 515 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 516 | main.log.debug( self.name + ": Raw output" ) |
| 517 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 518 | |
| 519 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 520 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 521 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 522 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 523 | main.log.debug( self.name + ": ansiEscape output" ) |
| 524 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 525 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 526 | # Remove extra return chars that get added |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 527 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 528 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 529 | main.log.debug( self.name + ": Removed extra returns " + |
| 530 | "from output" ) |
| 531 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 532 | |
| 533 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 534 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 535 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 536 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 537 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 538 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 539 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 540 | output = response.split( cmdStr.strip(), 1 ) |
| 541 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 542 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 543 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 544 | main.log.debug( self.name + ": " + repr( r ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 545 | output = output[ 1 ].strip() |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 546 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 547 | main.log.info( "Response from ONOS: {}".format( output ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 548 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 549 | except pexpect.TIMEOUT: |
| 550 | main.log.error( self.name + ":ONOS timeout" ) |
| 551 | if debug: |
| 552 | main.log.debug( self.handle.before ) |
| 553 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 554 | except IndexError: |
| 555 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 556 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 557 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 558 | except TypeError: |
| 559 | main.log.exception( self.name + ": Object not as expected" ) |
| 560 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 561 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 562 | main.log.error( self.name + ": EOF exception found" ) |
| 563 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 564 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 565 | return None |
| 566 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 567 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 568 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 569 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 570 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 571 | return None |
| 572 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 573 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 574 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 575 | # IMPORTANT NOTE: |
| 576 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 577 | # the cli command changing 'a:b' with 'aB'. |
| 578 | # Ex ) onos:topology > onosTopology |
| 579 | # onos:links > onosLinks |
| 580 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 581 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 582 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 583 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 584 | Adds a new cluster node by ID and address information. |
| 585 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 586 | * nodeId |
| 587 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 588 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 589 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 590 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 591 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 592 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 593 | str( ONOSIp ) + " " + str( tcpPort ) |
| 594 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 595 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 596 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 597 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 598 | main.log.error( "Error in adding node" ) |
| 599 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 600 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 601 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 602 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 603 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 604 | except AssertionError: |
| 605 | main.log.exception( "" ) |
| 606 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 607 | except TypeError: |
| 608 | main.log.exception( self.name + ": Object not as expected" ) |
| 609 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 610 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 611 | main.log.error( self.name + ": EOF exception found" ) |
| 612 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 613 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 614 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 615 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 616 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 617 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 618 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 619 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 620 | Removes a cluster by ID |
| 621 | Issues command: 'remove-node [<node-id>]' |
| 622 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 623 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 624 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 625 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 626 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 627 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 628 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 629 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 630 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 631 | if re.search( "Error", handle ): |
| 632 | main.log.error( "Error in removing node" ) |
| 633 | main.log.error( handle ) |
| 634 | return main.FALSE |
| 635 | else: |
| 636 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 637 | except AssertionError: |
| 638 | main.log.exception( "" ) |
| 639 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 640 | except TypeError: |
| 641 | main.log.exception( self.name + ": Object not as expected" ) |
| 642 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 643 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 644 | main.log.error( self.name + ": EOF exception found" ) |
| 645 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 646 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 647 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 648 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 649 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 650 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 651 | def nodes( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 652 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 653 | List the nodes currently visible |
| 654 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 655 | Optional argument: |
| 656 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 657 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 658 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 659 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 660 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 661 | cmdStr += " -j" |
| 662 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 663 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 664 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 665 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 666 | except AssertionError: |
| 667 | main.log.exception( "" ) |
| 668 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 669 | except TypeError: |
| 670 | main.log.exception( self.name + ": Object not as expected" ) |
| 671 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 672 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 673 | main.log.error( self.name + ": EOF exception found" ) |
| 674 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 675 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 676 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 677 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 678 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 679 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 680 | def topology( self ): |
| 681 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 682 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 683 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 684 | Return: |
| 685 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 686 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 687 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 688 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 689 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 690 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 691 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 692 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 693 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 694 | except AssertionError: |
| 695 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 696 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 697 | except TypeError: |
| 698 | main.log.exception( self.name + ": Object not as expected" ) |
| 699 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 700 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 701 | main.log.error( self.name + ": EOF exception found" ) |
| 702 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 703 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 704 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 705 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 706 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 707 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 708 | def deviceRemove( self, deviceId ): |
| 709 | """ |
| 710 | Removes particular device from storage |
| 711 | |
| 712 | TODO: refactor this function |
| 713 | """ |
| 714 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 715 | cmdStr = "device-remove " + str( deviceId ) |
| 716 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 717 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 718 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 719 | if re.search( "Error", handle ): |
| 720 | main.log.error( "Error in removing device" ) |
| 721 | main.log.error( handle ) |
| 722 | return main.FALSE |
| 723 | else: |
| 724 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 725 | except AssertionError: |
| 726 | main.log.exception( "" ) |
| 727 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 728 | except TypeError: |
| 729 | main.log.exception( self.name + ": Object not as expected" ) |
| 730 | return None |
| 731 | except pexpect.EOF: |
| 732 | main.log.error( self.name + ": EOF exception found" ) |
| 733 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 734 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 735 | except Exception: |
| 736 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 737 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 738 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 739 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 740 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 741 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 742 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 743 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 744 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 745 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 746 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 747 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 748 | cmdStr += " -j" |
| 749 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 750 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 751 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 752 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 753 | except AssertionError: |
| 754 | main.log.exception( "" ) |
| 755 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 756 | except TypeError: |
| 757 | main.log.exception( self.name + ": Object not as expected" ) |
| 758 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 759 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 760 | main.log.error( self.name + ": EOF exception found" ) |
| 761 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 762 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 763 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 764 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 765 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 766 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 767 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 768 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 769 | This balances the devices across all controllers |
| 770 | by issuing command: 'onos> onos:balance-masters' |
| 771 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 772 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 773 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 774 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 775 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 776 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 777 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 778 | if re.search( "Error", handle ): |
| 779 | main.log.error( "Error in balancing masters" ) |
| 780 | main.log.error( handle ) |
| 781 | return main.FALSE |
| 782 | else: |
| 783 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 784 | except AssertionError: |
| 785 | main.log.exception( "" ) |
| 786 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 787 | except TypeError: |
| 788 | main.log.exception( self.name + ": Object not as expected" ) |
| 789 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 790 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 791 | main.log.error( self.name + ": EOF exception found" ) |
| 792 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 793 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 794 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 795 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 796 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 797 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 798 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 799 | """ |
| 800 | Returns the output of the masters command. |
| 801 | Optional argument: |
| 802 | * jsonFormat - boolean indicating if you want output in json |
| 803 | """ |
| 804 | try: |
| 805 | cmdStr = "onos:masters" |
| 806 | if jsonFormat: |
| 807 | cmdStr += " -j" |
| 808 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 809 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 810 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 811 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 812 | except AssertionError: |
| 813 | main.log.exception( "" ) |
| 814 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 815 | except TypeError: |
| 816 | main.log.exception( self.name + ": Object not as expected" ) |
| 817 | return None |
| 818 | except pexpect.EOF: |
| 819 | main.log.error( self.name + ": EOF exception found" ) |
| 820 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 821 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 822 | except Exception: |
| 823 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 824 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 825 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 826 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 827 | """ |
| 828 | Uses the master command to check that the devices' leadership |
| 829 | is evenly divided |
| 830 | |
| 831 | Dependencies: checkMasters() and summary() |
| 832 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 833 | Returns main.TRUE if the devices are balanced |
| 834 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 835 | Exits on Exception |
| 836 | Returns None on TypeError |
| 837 | """ |
| 838 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 839 | summaryOutput = self.summary() |
| 840 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 841 | except ( TypeError, ValueError ): |
| 842 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 843 | return None |
| 844 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 845 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 846 | mastersOutput = self.checkMasters() |
| 847 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 848 | first = masters[ 0 ][ "size" ] |
| 849 | for master in masters: |
| 850 | totalOwnedDevices += master[ "size" ] |
| 851 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 852 | main.log.error( "Mastership not balanced" ) |
| 853 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 854 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 855 | main.log.info( "Mastership balanced between " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 856 | str( len( masters ) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 857 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 858 | except ( TypeError, ValueError ): |
| 859 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 860 | return None |
| 861 | except pexpect.EOF: |
| 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() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 865 | except Exception: |
| 866 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 867 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 868 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 869 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 870 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 871 | Lists all core links |
| 872 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 873 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 874 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 875 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 876 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 877 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 878 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 879 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 880 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 881 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 882 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 883 | except AssertionError: |
| 884 | main.log.exception( "" ) |
| 885 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 886 | except TypeError: |
| 887 | main.log.exception( self.name + ": Object not as expected" ) |
| 888 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 889 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 890 | main.log.error( self.name + ": EOF exception found" ) |
| 891 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 892 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 893 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 894 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 895 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 896 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 897 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 898 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 899 | Lists all ports |
| 900 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 901 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 902 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 903 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 904 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 905 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 906 | cmdStr += " -j" |
| 907 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 908 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 909 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 910 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 911 | except AssertionError: |
| 912 | main.log.exception( "" ) |
| 913 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 914 | except TypeError: |
| 915 | main.log.exception( self.name + ": Object not as expected" ) |
| 916 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 917 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 918 | main.log.error( self.name + ": EOF exception found" ) |
| 919 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 920 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 921 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 922 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 923 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 924 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 925 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 926 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 927 | Lists all devices and the controllers with roles assigned to them |
| 928 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 929 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 930 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 931 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 932 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 933 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 934 | cmdStr += " -j" |
| 935 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 936 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 937 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 938 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 939 | except AssertionError: |
| 940 | main.log.exception( "" ) |
| 941 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 942 | except TypeError: |
| 943 | main.log.exception( self.name + ": Object not as expected" ) |
| 944 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 945 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 946 | main.log.error( self.name + ": EOF exception found" ) |
| 947 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 948 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 949 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 950 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 951 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 952 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 953 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 954 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 955 | Given the a string containing the json representation of the "roles" |
| 956 | cli command and a partial or whole device id, returns a json object |
| 957 | containing the roles output for the first device whose id contains |
| 958 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 959 | |
| 960 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 961 | A dict of the role assignments for the given device or |
| 962 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 963 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 964 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 965 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 966 | return None |
| 967 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 968 | rawRoles = self.roles() |
| 969 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 970 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 971 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 972 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 973 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 974 | return device |
| 975 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 976 | except ( TypeError, ValueError ): |
| 977 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 978 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 979 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 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() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 983 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 984 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 985 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 986 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 987 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 988 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 989 | Iterates through each device and checks if there is a master assigned |
| 990 | Returns: main.TRUE if each device has a master |
| 991 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 992 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 993 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 994 | rawRoles = self.roles() |
| 995 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 996 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 997 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 998 | # print device |
| 999 | if device[ 'master' ] == "none": |
| 1000 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1001 | return main.FALSE |
| 1002 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1003 | except ( TypeError, ValueError ): |
| 1004 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1005 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1006 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1007 | main.log.error( self.name + ": EOF exception found" ) |
| 1008 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1009 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1010 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1011 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1012 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1013 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1014 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1015 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1016 | Returns string of paths, and the cost. |
| 1017 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1018 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1019 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1020 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1021 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1022 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1023 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1024 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1025 | main.log.error( "Error in getting paths" ) |
| 1026 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1027 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1028 | path = handle.split( ";" )[ 0 ] |
| 1029 | cost = handle.split( ";" )[ 1 ] |
| 1030 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1031 | except AssertionError: |
| 1032 | main.log.exception( "" ) |
| 1033 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1034 | except TypeError: |
| 1035 | main.log.exception( self.name + ": Object not as expected" ) |
| 1036 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1037 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1038 | main.log.error( self.name + ": EOF exception found" ) |
| 1039 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1040 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1041 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1042 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1043 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1044 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1045 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1046 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1047 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1048 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1049 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1050 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1051 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1052 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1053 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1054 | cmdStr += " -j" |
| 1055 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1056 | if handle: |
| 1057 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1058 | # TODO: Maybe make this less hardcoded |
| 1059 | # ConsistentMap Exceptions |
| 1060 | assert "org.onosproject.store.service" not in handle |
| 1061 | # Node not leader |
| 1062 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1063 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1064 | except AssertionError: |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1065 | main.log.exception( "Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1066 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1067 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1068 | except TypeError: |
| 1069 | main.log.exception( self.name + ": Object not as expected" ) |
| 1070 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1071 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1072 | main.log.error( self.name + ": EOF exception found" ) |
| 1073 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1074 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1075 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1076 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1077 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1078 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1079 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1080 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1081 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1082 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1083 | 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] | 1084 | partial mac address |
| 1085 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1086 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1087 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1088 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1089 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1090 | return None |
| 1091 | else: |
| 1092 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1093 | rawHosts = self.hosts() |
| 1094 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1095 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1096 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1097 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1098 | if not host: |
| 1099 | pass |
| 1100 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1101 | return host |
| 1102 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1103 | except ( TypeError, ValueError ): |
| 1104 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1105 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -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 | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1113 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1114 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1115 | """ |
| 1116 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1117 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1118 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1119 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1120 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1121 | IMPORTANT: |
| 1122 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1123 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1124 | Furthermore, it assumes that value of VLAN is '-1' |
| 1125 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1126 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1127 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1128 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1129 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1130 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1131 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1132 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1133 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1134 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1135 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1136 | i = iter( str( hostHex ) ) |
| 1137 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1138 | hostHex = hostHex + "/-1" |
| 1139 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1140 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1141 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1142 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1143 | except TypeError: |
| 1144 | main.log.exception( self.name + ": Object not as expected" ) |
| 1145 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1146 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1147 | main.log.error( self.name + ": EOF exception found" ) |
| 1148 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1149 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1150 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1151 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1152 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1153 | |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1154 | def verifyHostLocation( self, hostIp, location ): |
| 1155 | """ |
| 1156 | Description: |
| 1157 | Verify the host given is discovered in all locations expected |
| 1158 | Required: |
| 1159 | hostIp: IP address of the host |
| 1160 | location: expected location(s) of the given host. ex. "of:0000000000000005/8" |
| 1161 | Could be a string or list |
| 1162 | Returns: |
| 1163 | main.TRUE if host is discovered on all locations provided |
| 1164 | main.FALSE otherwise |
| 1165 | """ |
| 1166 | import json |
| 1167 | locations = [ location ] if isinstance( location, str ) else location |
| 1168 | assert isinstance( locations, list ), "Wrong type of location: {}".format( type( location ) ) |
| 1169 | try: |
| 1170 | hosts = self.hosts() |
| 1171 | hosts = json.loads( hosts ) |
| 1172 | targetHost = None |
| 1173 | for host in hosts: |
| 1174 | if hostIp in host[ "ipAddresses" ]: |
| 1175 | targetHost = host |
You Wang | fd80ab4 | 2018-05-10 17:21:53 -0700 | [diff] [blame] | 1176 | assert targetHost, "Not able to find host with IP {}".format( hostIp ) |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1177 | result = main.TRUE |
| 1178 | locationsDiscovered = [ loc[ "elementId" ] + "/" + loc[ "port" ] for loc in targetHost[ "locations" ] ] |
| 1179 | for loc in locations: |
| 1180 | discovered = False |
| 1181 | for locDiscovered in locationsDiscovered: |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1182 | locToMatch = locDiscovered if "/" in loc else locDiscovered.split( "/" )[0] |
| 1183 | if loc == locToMatch: |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1184 | main.log.debug( "Host {} discovered with location {}".format( hostIp, loc ) ) |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 1185 | discovered = True |
You Wang | bc898b8 | 2018-05-03 16:22:34 -0700 | [diff] [blame] | 1186 | break |
| 1187 | if discovered: |
| 1188 | locationsDiscovered.remove( locDiscovered ) |
| 1189 | else: |
| 1190 | main.log.warn( "Host {} not discovered with location {}".format( hostIp, loc ) ) |
| 1191 | result = main.FALSE |
| 1192 | if locationsDiscovered: |
| 1193 | main.log.warn( "Host {} is also discovered with location {}".format( hostIp, locationsDiscovered ) ) |
| 1194 | result = main.FALSE |
| 1195 | return result |
| 1196 | except KeyError: |
| 1197 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1198 | return None |
| 1199 | except pexpect.EOF: |
| 1200 | main.log.error( self.name + ": EOF exception found" ) |
| 1201 | main.log.error( self.name + ": " + self.handle.before ) |
| 1202 | main.cleanAndExit() |
| 1203 | except Exception: |
| 1204 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1205 | return None |
| 1206 | |
You Wang | 53dba1e | 2018-02-02 17:45:44 -0800 | [diff] [blame] | 1207 | def verifyHostIp( self, hostList=[], prefix="" ): |
| 1208 | """ |
| 1209 | Description: |
| 1210 | Verify that all hosts have IP address assigned to them |
| 1211 | Optional: |
| 1212 | hostList: If specified, verifications only happen to the hosts |
| 1213 | in hostList |
| 1214 | prefix: at least one of the ip address assigned to the host |
| 1215 | needs to have the specified prefix |
| 1216 | Returns: |
| 1217 | main.TRUE if all hosts have specific IP address assigned; |
| 1218 | main.FALSE otherwise |
| 1219 | """ |
| 1220 | import json |
| 1221 | try: |
| 1222 | hosts = self.hosts() |
| 1223 | hosts = json.loads( hosts ) |
| 1224 | if not hostList: |
| 1225 | hostList = [ host[ "id" ] for host in hosts ] |
| 1226 | for host in hosts: |
| 1227 | hostId = host[ "id" ] |
| 1228 | if hostId not in hostList: |
| 1229 | continue |
| 1230 | ipList = host[ "ipAddresses" ] |
| 1231 | main.log.debug( self.name + ": IP list on host " + str( hostId ) + ": " + str( ipList ) ) |
| 1232 | if not ipList: |
| 1233 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostId ) ) |
| 1234 | else: |
| 1235 | if not any( ip.startswith( str( prefix ) ) for ip in ipList ): |
| 1236 | main.log.warn( self.name + ": None of the IPs on host " + str( hostId ) + " has prefix " + str( prefix ) ) |
| 1237 | else: |
| 1238 | main.log.debug( self.name + ": Found matching IP on host " + str( hostId ) ) |
| 1239 | hostList.remove( hostId ) |
| 1240 | if hostList: |
| 1241 | main.log.warn( self.name + ": failed to verify IP on following hosts: " + str( hostList) ) |
| 1242 | return main.FALSE |
| 1243 | else: |
| 1244 | return main.TRUE |
| 1245 | except KeyError: |
| 1246 | main.log.exception( self.name + ": host data not as expected: " + hosts ) |
| 1247 | return None |
| 1248 | except pexpect.EOF: |
| 1249 | main.log.error( self.name + ": EOF exception found" ) |
| 1250 | main.log.error( self.name + ": " + self.handle.before ) |
| 1251 | main.cleanAndExit() |
| 1252 | except Exception: |
| 1253 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1254 | return None |
| 1255 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1256 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1257 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1258 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1259 | * hostIdOne: ONOS host id for host1 |
| 1260 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1261 | Optional: |
| 1262 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1263 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1264 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1265 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1266 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1267 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1268 | Returns: |
| 1269 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1270 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1271 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1272 | cmdStr = "add-host-intent " |
| 1273 | if vlanId: |
| 1274 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1275 | if setVlan: |
| 1276 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1277 | if encap: |
| 1278 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1279 | if bandwidth: |
| 1280 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1281 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1282 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1283 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1284 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1285 | if re.search( "Error", handle ): |
| 1286 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1287 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1288 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1289 | else: |
| 1290 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1291 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1292 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1293 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1294 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1295 | else: |
| 1296 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1297 | main.log.debug( "Response from ONOS was: " + |
| 1298 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1299 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1300 | except AssertionError: |
| 1301 | main.log.exception( "" ) |
| 1302 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1303 | except TypeError: |
| 1304 | main.log.exception( self.name + ": Object not as expected" ) |
| 1305 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1306 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1307 | main.log.error( self.name + ": EOF exception found" ) |
| 1308 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1309 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1310 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1311 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1312 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1313 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1314 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1315 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1316 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1317 | * ingressDevice: device id of ingress device |
| 1318 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1319 | Optional: |
| 1320 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1321 | Description: |
| 1322 | Adds an optical intent by specifying an ingress and egress device |
| 1323 | Returns: |
| 1324 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1325 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1326 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1327 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1328 | " " + str( egressDevice ) |
| 1329 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1330 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1331 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1332 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1333 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1334 | main.log.error( "Error in adding Optical intent" ) |
| 1335 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1336 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1337 | main.log.info( "Optical intent installed between " + |
| 1338 | str( ingressDevice ) + " and " + |
| 1339 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1340 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1341 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1342 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1343 | else: |
| 1344 | main.log.error( "Error, intent ID not found" ) |
| 1345 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1346 | except AssertionError: |
| 1347 | main.log.exception( "" ) |
| 1348 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1349 | except TypeError: |
| 1350 | main.log.exception( self.name + ": Object not as expected" ) |
| 1351 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1352 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1353 | main.log.error( self.name + ": EOF exception found" ) |
| 1354 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1355 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1356 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1357 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1358 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1359 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1360 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1361 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1362 | ingressDevice, |
| 1363 | egressDevice, |
| 1364 | portIngress="", |
| 1365 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1366 | ethType="", |
| 1367 | ethSrc="", |
| 1368 | ethDst="", |
| 1369 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1370 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1371 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1372 | ipProto="", |
| 1373 | ipSrc="", |
| 1374 | ipDst="", |
| 1375 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1376 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1377 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1378 | setVlan="", |
| 1379 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1380 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1381 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1382 | * ingressDevice: device id of ingress device |
| 1383 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1384 | Optional: |
| 1385 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1386 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1387 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1388 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1389 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1390 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1391 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1392 | * ipSrc: specify ip source address |
| 1393 | * ipDst: specify ip destination address |
| 1394 | * tcpSrc: specify tcp source port |
| 1395 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1396 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1397 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1398 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1399 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1400 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1401 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1402 | Returns: |
| 1403 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1404 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1405 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1406 | options developers provide for point-to-point |
| 1407 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1408 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1409 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1410 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1411 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1412 | if ethType: |
| 1413 | cmd += " --ethType " + str( ethType ) |
| 1414 | if ethSrc: |
| 1415 | cmd += " --ethSrc " + str( ethSrc ) |
| 1416 | if ethDst: |
| 1417 | cmd += " --ethDst " + str( ethDst ) |
| 1418 | if bandwidth: |
| 1419 | cmd += " --bandwidth " + str( bandwidth ) |
| 1420 | if lambdaAlloc: |
| 1421 | cmd += " --lambda " |
| 1422 | if ipProto: |
| 1423 | cmd += " --ipProto " + str( ipProto ) |
| 1424 | if ipSrc: |
| 1425 | cmd += " --ipSrc " + str( ipSrc ) |
| 1426 | if ipDst: |
| 1427 | cmd += " --ipDst " + str( ipDst ) |
| 1428 | if tcpSrc: |
| 1429 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1430 | if tcpDst: |
| 1431 | cmd += " --tcpDst " + str( tcpDst ) |
| 1432 | if vlanId: |
| 1433 | cmd += " -v " + str( vlanId ) |
| 1434 | if setVlan: |
| 1435 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1436 | if encap: |
| 1437 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1438 | if protected: |
| 1439 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1440 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1441 | # Check whether the user appended the port |
| 1442 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1443 | if "/" in ingressDevice: |
| 1444 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1445 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1446 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1447 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1448 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1449 | # Would it make sense to throw an exception and exit |
| 1450 | # the test? |
| 1451 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1452 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1453 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1454 | str( ingressDevice ) + "/" +\ |
| 1455 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1456 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1457 | if "/" in egressDevice: |
| 1458 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1459 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1460 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1461 | main.log.error( "You must specify the egress port" ) |
| 1462 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1463 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1464 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1465 | str( egressDevice ) + "/" +\ |
| 1466 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1467 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1468 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1469 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1470 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1471 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1472 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1473 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1474 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1475 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1476 | # TODO: print out all the options in this message? |
| 1477 | main.log.info( "Point-to-point intent installed between " + |
| 1478 | str( ingressDevice ) + " and " + |
| 1479 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1480 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1481 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1482 | return match.group()[ 3:-1 ] |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1483 | else: |
| 1484 | main.log.error( "Error, intent ID not found" ) |
| 1485 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1486 | except AssertionError: |
| 1487 | main.log.exception( "" ) |
| 1488 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1489 | except TypeError: |
| 1490 | main.log.exception( self.name + ": Object not as expected" ) |
| 1491 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1492 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1493 | main.log.error( self.name + ": EOF exception found" ) |
| 1494 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1495 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1496 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1497 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1498 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1499 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1500 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1501 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1502 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1503 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1504 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1505 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1506 | ethType="", |
| 1507 | ethSrc="", |
| 1508 | ethDst="", |
| 1509 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1510 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1511 | ipProto="", |
| 1512 | ipSrc="", |
| 1513 | ipDst="", |
| 1514 | tcpSrc="", |
| 1515 | tcpDst="", |
| 1516 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1517 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1518 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1519 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1520 | partial=False, |
| 1521 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1522 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1523 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1524 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1525 | is same. That is, all ingress devices include port numbers |
| 1526 | with a "/" or all ingress devices could specify device |
| 1527 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1528 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1529 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1530 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1531 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1532 | Optional: |
| 1533 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1534 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1535 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1536 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1537 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1538 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1539 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1540 | * ipSrc: specify ip source address |
| 1541 | * ipDst: specify ip destination address |
| 1542 | * tcpSrc: specify tcp source port |
| 1543 | * tcpDst: specify tcp destination port |
| 1544 | * setEthSrc: action to Rewrite Source MAC Address |
| 1545 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1546 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1547 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1548 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1549 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1550 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1551 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1552 | Returns: |
| 1553 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1554 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1555 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1556 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1557 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1558 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1559 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1560 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1561 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1562 | if ethType: |
| 1563 | cmd += " --ethType " + str( ethType ) |
| 1564 | if ethSrc: |
| 1565 | cmd += " --ethSrc " + str( ethSrc ) |
| 1566 | if ethDst: |
| 1567 | cmd += " --ethDst " + str( ethDst ) |
| 1568 | if bandwidth: |
| 1569 | cmd += " --bandwidth " + str( bandwidth ) |
| 1570 | if lambdaAlloc: |
| 1571 | cmd += " --lambda " |
| 1572 | if ipProto: |
| 1573 | cmd += " --ipProto " + str( ipProto ) |
| 1574 | if ipSrc: |
| 1575 | cmd += " --ipSrc " + str( ipSrc ) |
| 1576 | if ipDst: |
| 1577 | cmd += " --ipDst " + str( ipDst ) |
| 1578 | if tcpSrc: |
| 1579 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1580 | if tcpDst: |
| 1581 | cmd += " --tcpDst " + str( tcpDst ) |
| 1582 | if setEthSrc: |
| 1583 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1584 | if setEthDst: |
| 1585 | cmd += " --setEthDst " + str( setEthDst ) |
| 1586 | if vlanId: |
| 1587 | cmd += " -v " + str( vlanId ) |
| 1588 | if setVlan: |
| 1589 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1590 | if partial: |
| 1591 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1592 | if encap: |
| 1593 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1594 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1595 | # Check whether the user appended the port |
| 1596 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1597 | |
| 1598 | if portIngressList is None: |
| 1599 | for ingressDevice in ingressDeviceList: |
| 1600 | if "/" in ingressDevice: |
| 1601 | cmd += " " + str( ingressDevice ) |
| 1602 | else: |
| 1603 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1604 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1605 | # TODO: perhaps more meaningful return |
| 1606 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1607 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1608 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1609 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1610 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1611 | cmd += " " + \ |
| 1612 | str( ingressDevice ) + "/" +\ |
| 1613 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1614 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1615 | main.log.error( "Device list and port list does not " + |
| 1616 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1617 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1618 | if "/" in egressDevice: |
| 1619 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1620 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1621 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1622 | main.log.error( "You must specify " + |
| 1623 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1624 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1625 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1626 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1627 | str( egressDevice ) + "/" +\ |
| 1628 | str( portEgress ) |
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 ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1634 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1635 | "intent" ) |
| 1636 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1637 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1638 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1639 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1640 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1641 | else: |
| 1642 | main.log.error( "Error, intent ID not found" ) |
| 1643 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1644 | except AssertionError: |
| 1645 | main.log.exception( "" ) |
| 1646 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1647 | except TypeError: |
| 1648 | main.log.exception( self.name + ": Object not as expected" ) |
| 1649 | return None |
| 1650 | except pexpect.EOF: |
| 1651 | main.log.error( self.name + ": EOF exception found" ) |
| 1652 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1653 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1654 | except Exception: |
| 1655 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1656 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1657 | |
| 1658 | def addSinglepointToMultipointIntent( |
| 1659 | self, |
| 1660 | ingressDevice, |
| 1661 | egressDeviceList, |
| 1662 | portIngress="", |
| 1663 | portEgressList=None, |
| 1664 | ethType="", |
| 1665 | ethSrc="", |
| 1666 | ethDst="", |
| 1667 | bandwidth="", |
| 1668 | lambdaAlloc=False, |
| 1669 | ipProto="", |
| 1670 | ipSrc="", |
| 1671 | ipDst="", |
| 1672 | tcpSrc="", |
| 1673 | tcpDst="", |
| 1674 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1675 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1676 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1677 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1678 | partial=False, |
| 1679 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1680 | """ |
| 1681 | Note: |
| 1682 | This function assumes the format of all egress devices |
| 1683 | is same. That is, all egress devices include port numbers |
| 1684 | with a "/" or all egress devices could specify device |
| 1685 | ids and port numbers seperately. |
| 1686 | Required: |
| 1687 | * EgressDeviceList: List of device ids of egress device |
| 1688 | ( Atleast 2 eress devices required in the list ) |
| 1689 | * ingressDevice: device id of ingress device |
| 1690 | Optional: |
| 1691 | * ethType: specify ethType |
| 1692 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1693 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1694 | * bandwidth: specify bandwidth capacity of link |
| 1695 | * lambdaAlloc: if True, intent will allocate lambda |
| 1696 | for the specified intent |
| 1697 | * ipProto: specify ip protocol |
| 1698 | * ipSrc: specify ip source address |
| 1699 | * ipDst: specify ip destination address |
| 1700 | * tcpSrc: specify tcp source port |
| 1701 | * tcpDst: specify tcp destination port |
| 1702 | * setEthSrc: action to Rewrite Source MAC Address |
| 1703 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1704 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1705 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1706 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1707 | Description: |
| 1708 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1709 | specifying device id's and optional fields |
| 1710 | Returns: |
| 1711 | A string of the intent id or None on error |
| 1712 | |
| 1713 | NOTE: This function may change depending on the |
| 1714 | options developers provide for singlepoint-to-multipoint |
| 1715 | intent via cli |
| 1716 | """ |
| 1717 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1718 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1719 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1720 | if ethType: |
| 1721 | cmd += " --ethType " + str( ethType ) |
| 1722 | if ethSrc: |
| 1723 | cmd += " --ethSrc " + str( ethSrc ) |
| 1724 | if ethDst: |
| 1725 | cmd += " --ethDst " + str( ethDst ) |
| 1726 | if bandwidth: |
| 1727 | cmd += " --bandwidth " + str( bandwidth ) |
| 1728 | if lambdaAlloc: |
| 1729 | cmd += " --lambda " |
| 1730 | if ipProto: |
| 1731 | cmd += " --ipProto " + str( ipProto ) |
| 1732 | if ipSrc: |
| 1733 | cmd += " --ipSrc " + str( ipSrc ) |
| 1734 | if ipDst: |
| 1735 | cmd += " --ipDst " + str( ipDst ) |
| 1736 | if tcpSrc: |
| 1737 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1738 | if tcpDst: |
| 1739 | cmd += " --tcpDst " + str( tcpDst ) |
| 1740 | if setEthSrc: |
| 1741 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1742 | if setEthDst: |
| 1743 | cmd += " --setEthDst " + str( setEthDst ) |
| 1744 | if vlanId: |
| 1745 | cmd += " -v " + str( vlanId ) |
| 1746 | if setVlan: |
| 1747 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1748 | if partial: |
| 1749 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1750 | if encap: |
| 1751 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1752 | |
| 1753 | # Check whether the user appended the port |
| 1754 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1755 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1756 | if "/" in ingressDevice: |
| 1757 | cmd += " " + str( ingressDevice ) |
| 1758 | else: |
| 1759 | if not portIngress: |
| 1760 | main.log.error( "You must specify " + |
| 1761 | "the Ingress port" ) |
| 1762 | return main.FALSE |
| 1763 | |
| 1764 | cmd += " " +\ |
| 1765 | str( ingressDevice ) + "/" +\ |
| 1766 | str( portIngress ) |
| 1767 | |
| 1768 | if portEgressList is None: |
| 1769 | for egressDevice in egressDeviceList: |
| 1770 | if "/" in egressDevice: |
| 1771 | cmd += " " + str( egressDevice ) |
| 1772 | else: |
| 1773 | main.log.error( "You must specify " + |
| 1774 | "the egress port" ) |
| 1775 | # TODO: perhaps more meaningful return |
| 1776 | return main.FALSE |
| 1777 | else: |
| 1778 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1779 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1780 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1781 | cmd += " " + \ |
| 1782 | str( egressDevice ) + "/" +\ |
| 1783 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1784 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1785 | main.log.error( "Device list and port list does not " + |
| 1786 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1787 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1788 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1789 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1790 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1791 | # If error, return error message |
| 1792 | if re.search( "Error", handle ): |
| 1793 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1794 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1795 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1796 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1797 | match = re.search( 'id=0x([\da-f]+),', handle ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1798 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1799 | return match.group()[ 3:-1 ] |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1800 | else: |
| 1801 | main.log.error( "Error, intent ID not found" ) |
| 1802 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1803 | except AssertionError: |
| 1804 | main.log.exception( "" ) |
| 1805 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1806 | except TypeError: |
| 1807 | main.log.exception( self.name + ": Object not as expected" ) |
| 1808 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1809 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1810 | main.log.error( self.name + ": EOF exception found" ) |
| 1811 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1812 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1813 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1814 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1815 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1816 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1817 | def addMplsIntent( |
| 1818 | self, |
| 1819 | ingressDevice, |
| 1820 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1821 | ingressPort="", |
| 1822 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1823 | ethType="", |
| 1824 | ethSrc="", |
| 1825 | ethDst="", |
| 1826 | bandwidth="", |
| 1827 | lambdaAlloc=False, |
| 1828 | ipProto="", |
| 1829 | ipSrc="", |
| 1830 | ipDst="", |
| 1831 | tcpSrc="", |
| 1832 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1833 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1834 | egressLabel="", |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1835 | priority="" ): |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1836 | """ |
| 1837 | Required: |
| 1838 | * ingressDevice: device id of ingress device |
| 1839 | * egressDevice: device id of egress device |
| 1840 | Optional: |
| 1841 | * ethType: specify ethType |
| 1842 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1843 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1844 | * bandwidth: specify bandwidth capacity of link |
| 1845 | * lambdaAlloc: if True, intent will allocate lambda |
| 1846 | for the specified intent |
| 1847 | * ipProto: specify ip protocol |
| 1848 | * ipSrc: specify ip source address |
| 1849 | * ipDst: specify ip destination address |
| 1850 | * tcpSrc: specify tcp source port |
| 1851 | * tcpDst: specify tcp destination port |
| 1852 | * ingressLabel: Ingress MPLS label |
| 1853 | * egressLabel: Egress MPLS label |
| 1854 | Description: |
| 1855 | Adds MPLS intent by |
| 1856 | specifying device id's and optional fields |
| 1857 | Returns: |
| 1858 | A string of the intent id or None on error |
| 1859 | |
| 1860 | NOTE: This function may change depending on the |
| 1861 | options developers provide for MPLS |
| 1862 | intent via cli |
| 1863 | """ |
| 1864 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1865 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1866 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1867 | if ethType: |
| 1868 | cmd += " --ethType " + str( ethType ) |
| 1869 | if ethSrc: |
| 1870 | cmd += " --ethSrc " + str( ethSrc ) |
| 1871 | if ethDst: |
| 1872 | cmd += " --ethDst " + str( ethDst ) |
| 1873 | if bandwidth: |
| 1874 | cmd += " --bandwidth " + str( bandwidth ) |
| 1875 | if lambdaAlloc: |
| 1876 | cmd += " --lambda " |
| 1877 | if ipProto: |
| 1878 | cmd += " --ipProto " + str( ipProto ) |
| 1879 | if ipSrc: |
| 1880 | cmd += " --ipSrc " + str( ipSrc ) |
| 1881 | if ipDst: |
| 1882 | cmd += " --ipDst " + str( ipDst ) |
| 1883 | if tcpSrc: |
| 1884 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1885 | if tcpDst: |
| 1886 | cmd += " --tcpDst " + str( tcpDst ) |
| 1887 | if ingressLabel: |
| 1888 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1889 | if egressLabel: |
| 1890 | cmd += " --egressLabel " + str( egressLabel ) |
| 1891 | if priority: |
| 1892 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1893 | |
| 1894 | # Check whether the user appended the port |
| 1895 | # or provided it as an input |
| 1896 | if "/" in ingressDevice: |
| 1897 | cmd += " " + str( ingressDevice ) |
| 1898 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1899 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1900 | main.log.error( "You must specify the ingress port" ) |
| 1901 | return None |
| 1902 | |
| 1903 | cmd += " " + \ |
| 1904 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1905 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1906 | |
| 1907 | if "/" in egressDevice: |
| 1908 | cmd += " " + str( egressDevice ) |
| 1909 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1910 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1911 | main.log.error( "You must specify the egress port" ) |
| 1912 | return None |
| 1913 | |
| 1914 | cmd += " " +\ |
| 1915 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1916 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1917 | |
| 1918 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1919 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1920 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1921 | # If error, return error message |
| 1922 | if re.search( "Error", handle ): |
| 1923 | main.log.error( "Error in adding mpls intent" ) |
| 1924 | return None |
| 1925 | else: |
| 1926 | # TODO: print out all the options in this message? |
| 1927 | main.log.info( "MPLS intent installed between " + |
| 1928 | str( ingressDevice ) + " and " + |
| 1929 | str( egressDevice ) ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1930 | match = re.search( 'id=0x([\da-f]+),', handle ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1931 | if match: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 1932 | return match.group()[ 3:-1 ] |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1933 | else: |
| 1934 | main.log.error( "Error, intent ID not found" ) |
| 1935 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1936 | except AssertionError: |
| 1937 | main.log.exception( "" ) |
| 1938 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1939 | except TypeError: |
| 1940 | main.log.exception( self.name + ": Object not as expected" ) |
| 1941 | return None |
| 1942 | except pexpect.EOF: |
| 1943 | main.log.error( self.name + ": EOF exception found" ) |
| 1944 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1945 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1946 | except Exception: |
| 1947 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1948 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1949 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1950 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1951 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1952 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1953 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1954 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1955 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1956 | -p or --purge: Purge the intent from the store after removal |
| 1957 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1958 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 1959 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1960 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1961 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1962 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1963 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1964 | if purge: |
| 1965 | cmdStr += " -p" |
| 1966 | if sync: |
| 1967 | cmdStr += " -s" |
| 1968 | |
| 1969 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1970 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1971 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1972 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1973 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1974 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1975 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1976 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1977 | # TODO: Should this be main.TRUE |
| 1978 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1979 | except AssertionError: |
| 1980 | main.log.exception( "" ) |
| 1981 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1982 | except TypeError: |
| 1983 | main.log.exception( self.name + ": Object not as expected" ) |
| 1984 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1985 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1986 | main.log.error( self.name + ": EOF exception found" ) |
| 1987 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1988 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1989 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1990 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1991 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1992 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1993 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1994 | """ |
| 1995 | Description: |
| 1996 | Remove all the intents |
| 1997 | Optional args:- |
| 1998 | -s or --sync: Waits for the removal before returning |
| 1999 | -p or --purge: Purge the intent from the store after removal |
| 2000 | Returns: |
| 2001 | Returns main.TRUE if all intents are removed, otherwise returns |
| 2002 | main.FALSE; Returns None for exception |
| 2003 | """ |
| 2004 | try: |
| 2005 | cmdStr = "remove-intent" |
| 2006 | if purge: |
| 2007 | cmdStr += " -p" |
| 2008 | if sync: |
| 2009 | cmdStr += " -s" |
| 2010 | |
| 2011 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 2012 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2013 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2014 | assert "Command not found:" not in handle, handle |
| 2015 | if re.search( "Error", handle ): |
| 2016 | main.log.error( "Error in removing intent" ) |
| 2017 | return main.FALSE |
| 2018 | else: |
| 2019 | return main.TRUE |
| 2020 | except AssertionError: |
| 2021 | main.log.exception( "" ) |
| 2022 | return None |
| 2023 | except TypeError: |
| 2024 | main.log.exception( self.name + ": Object not as expected" ) |
| 2025 | return None |
| 2026 | except pexpect.EOF: |
| 2027 | main.log.error( self.name + ": EOF exception found" ) |
| 2028 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2029 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2030 | except Exception: |
| 2031 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2032 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 2033 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 2034 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2035 | """ |
| 2036 | Purges all WITHDRAWN Intents |
| 2037 | """ |
| 2038 | try: |
| 2039 | cmdStr = "purge-intents" |
| 2040 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2041 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2042 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2043 | if re.search( "Error", handle ): |
| 2044 | main.log.error( "Error in purging intents" ) |
| 2045 | return main.FALSE |
| 2046 | else: |
| 2047 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2048 | except AssertionError: |
| 2049 | main.log.exception( "" ) |
| 2050 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2051 | except TypeError: |
| 2052 | main.log.exception( self.name + ": Object not as expected" ) |
| 2053 | return None |
| 2054 | except pexpect.EOF: |
| 2055 | main.log.error( self.name + ": EOF exception found" ) |
| 2056 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2057 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2058 | except Exception: |
| 2059 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2060 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 2061 | |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 2062 | def wipeout( self ): |
| 2063 | """ |
| 2064 | Wipe out the flows,intents,links,devices,hosts, and groups from the ONOS. |
| 2065 | """ |
| 2066 | try: |
| 2067 | cmdStr = "wipe-out please" |
| 2068 | handle = self.sendline( cmdStr, timeout=60 ) |
| 2069 | assert handle is not None, "Error in sendline" |
| 2070 | assert "Command not found:" not in handle, handle |
| 2071 | return main.TRUE |
| 2072 | except AssertionError: |
| 2073 | main.log.exception( "" ) |
| 2074 | return None |
| 2075 | except TypeError: |
| 2076 | main.log.exception( self.name + ": Object not as expected" ) |
| 2077 | return None |
| 2078 | except pexpect.EOF: |
| 2079 | main.log.error( self.name + ": EOF exception found" ) |
| 2080 | main.log.error( self.name + ": " + self.handle.before ) |
| 2081 | main.cleanAndExit() |
| 2082 | except Exception: |
| 2083 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2084 | main.cleanAndExit() |
| 2085 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2086 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2087 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2088 | NOTE: This method should be used after installing application: |
| 2089 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2090 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2091 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2092 | Description: |
| 2093 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2094 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2095 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2096 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2097 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2098 | cmdStr += " -j" |
| 2099 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2100 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2101 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2102 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2103 | except AssertionError: |
| 2104 | main.log.exception( "" ) |
| 2105 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2106 | except TypeError: |
| 2107 | main.log.exception( self.name + ": Object not as expected" ) |
| 2108 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2109 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2110 | main.log.error( self.name + ": EOF exception found" ) |
| 2111 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2112 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2113 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2114 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2115 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 2116 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2117 | def ipv4RouteNumber( self ): |
| 2118 | """ |
| 2119 | NOTE: This method should be used after installing application: |
| 2120 | onos-app-sdnip |
| 2121 | Description: |
| 2122 | Obtain the total IPv4 routes number in the system |
| 2123 | """ |
| 2124 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2125 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2126 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2127 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2128 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2129 | jsonResult = json.loads( handle ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2130 | return len( jsonResult[ 'routes4' ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2131 | except AssertionError: |
| 2132 | main.log.exception( "" ) |
| 2133 | return None |
| 2134 | except ( TypeError, ValueError ): |
| 2135 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2136 | return None |
| 2137 | except pexpect.EOF: |
| 2138 | main.log.error( self.name + ": EOF exception found" ) |
| 2139 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2140 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2141 | except Exception: |
| 2142 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2143 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2144 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2145 | # =============Function to check Bandwidth allocation======== |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2146 | def allocations( self, jsonFormat = True, dollarSign = True ): |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2147 | """ |
| 2148 | Description: |
| 2149 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2150 | """ |
| 2151 | try: |
| 2152 | cmdStr = "allocations" |
| 2153 | if jsonFormat: |
| 2154 | cmdStr += " -j" |
| 2155 | handle = self.sendline( cmdStr, timeout=300, dollarSign=True ) |
| 2156 | assert handle is not None, "Error in sendline" |
| 2157 | assert "Command not found:" not in handle, handle |
| 2158 | return handle |
| 2159 | except AssertionError: |
| 2160 | main.log.exception( "" ) |
| 2161 | return None |
| 2162 | except ( TypeError, ValueError ): |
| 2163 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2164 | return None |
| 2165 | except pexpect.EOF: |
| 2166 | main.log.error( self.name + ": EOF exception found" ) |
| 2167 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2168 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2169 | except Exception: |
| 2170 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2171 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2172 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2173 | def intents( self, jsonFormat = True, summary = False, **intentargs ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2174 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2175 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2176 | Obtain intents from the ONOS cli. |
| 2177 | Optional: |
| 2178 | * jsonFormat: Enable output formatting in json, default to True |
| 2179 | * summary: Whether only output the intent summary, defaults to False |
| 2180 | * type: Only output a certain type of intent. This options is valid |
| 2181 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2182 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2183 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2184 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2185 | if summary: |
| 2186 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2187 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2188 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2189 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2190 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2191 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2192 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2193 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2194 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2195 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2196 | intentType = "" |
| 2197 | # IF we want the summary of a specific intent type |
| 2198 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2199 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2200 | if intentType in jsonResult.keys(): |
| 2201 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2202 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2203 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2204 | return handle |
| 2205 | else: |
| 2206 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2207 | except AssertionError: |
| 2208 | main.log.exception( "" ) |
| 2209 | return None |
| 2210 | except ( TypeError, ValueError ): |
| 2211 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2212 | return None |
| 2213 | except pexpect.EOF: |
| 2214 | main.log.error( self.name + ": EOF exception found" ) |
| 2215 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2216 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2217 | except Exception: |
| 2218 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2219 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2220 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2221 | def getIntentState( self, intentsId, intentsJson=None ): |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2222 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2223 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2224 | Gets intent state. Accepts a single intent ID (string type) or a |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2225 | list of intent IDs. |
| 2226 | Parameters: |
| 2227 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2228 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2229 | Returns: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2230 | 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] | 2231 | accepted. |
| 2232 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2233 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2234 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2235 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2236 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2237 | try: |
| 2238 | state = "State is Undefined" |
| 2239 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2240 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2241 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2242 | rawJson = intentsJson |
| 2243 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2244 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2245 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2246 | if intentsId == intent[ 'id' ]: |
| 2247 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2248 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2249 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2250 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2251 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2252 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2253 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2254 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2255 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2256 | for intent in parsedIntentsJson: |
| 2257 | if intentsId[ i ] == intent[ 'id' ]: |
| 2258 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2259 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2260 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2261 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2262 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2263 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2264 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2265 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2266 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2267 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2268 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2269 | except ( TypeError, ValueError ): |
| 2270 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2271 | return None |
| 2272 | except pexpect.EOF: |
| 2273 | main.log.error( self.name + ": EOF exception found" ) |
| 2274 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2275 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2276 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2277 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2278 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2279 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2280 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2281 | """ |
| 2282 | Description: |
| 2283 | Check intents state |
| 2284 | Required: |
| 2285 | intentsId - List of intents ID to be checked |
| 2286 | Optional: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2287 | expectedState - Check the expected state(s) of each intents |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2288 | state in the list. |
| 2289 | *NOTE: You can pass in a list of expected state, |
| 2290 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2291 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2292 | Returns main.TRUE only if all intent are the same as expected states, |
| 2293 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2294 | """ |
| 2295 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2296 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2297 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2298 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2299 | # intentsDict = self.getIntentState( intentsId ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2300 | intentsDict = [] |
| 2301 | for intent in json.loads( self.intents() ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2302 | if isinstance( intentsId, types.StringType ) \ |
| 2303 | and intent.get( 'id' ) == intentsId: |
| 2304 | intentsDict.append( intent ) |
| 2305 | elif isinstance( intentsId, types.ListType ) \ |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2306 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2307 | intentsDict.append( intent ) |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2308 | |
| 2309 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2310 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2311 | "getting intents state" ) |
| 2312 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2313 | |
| 2314 | if isinstance( expectedState, types.StringType ): |
| 2315 | for intents in intentsDict: |
| 2316 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2317 | main.log.debug( self.name + " : Intent ID - " + |
| 2318 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2319 | " actual state = " + |
| 2320 | intents.get( 'state' ) |
| 2321 | + " does not equal expected state = " |
| 2322 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2323 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2324 | elif isinstance( expectedState, types.ListType ): |
| 2325 | for intents in intentsDict: |
| 2326 | if not any( state == intents.get( 'state' ) for state in |
| 2327 | expectedState ): |
| 2328 | main.log.debug( self.name + " : Intent ID - " + |
| 2329 | intents.get( 'id' ) + |
| 2330 | " actual state = " + |
| 2331 | intents.get( 'state' ) + |
| 2332 | " does not equal expected states = " |
| 2333 | + str( expectedState ) ) |
| 2334 | returnValue = main.FALSE |
| 2335 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2336 | if returnValue == main.TRUE: |
| 2337 | main.log.info( self.name + ": All " + |
| 2338 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2339 | " intents are in " + str( expectedState ) + |
| 2340 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2341 | return returnValue |
| 2342 | except TypeError: |
| 2343 | main.log.exception( self.name + ": Object not as expected" ) |
| 2344 | return None |
| 2345 | except pexpect.EOF: |
| 2346 | main.log.error( self.name + ": EOF exception found" ) |
| 2347 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2348 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2349 | except Exception: |
| 2350 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2351 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2352 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2353 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2354 | """ |
| 2355 | Description: |
| 2356 | Compare the allocated bandwidth with the given allocations |
| 2357 | Required: |
| 2358 | expectedAllocations - The expected ONOS output of the allocations command |
| 2359 | Return: |
| 2360 | Returns main.TRUE only if all intent are the same as expected states, |
| 2361 | otherwise returns main.FALSE. |
| 2362 | """ |
| 2363 | # FIXME: Convert these string comparisons to object comparisons |
| 2364 | try: |
| 2365 | returnValue = main.TRUE |
| 2366 | bandwidthFailed = False |
| 2367 | rawAlloc = self.allocations() |
| 2368 | expectedFormat = StringIO( expectedAllocations ) |
| 2369 | ONOSOutput = StringIO( rawAlloc ) |
| 2370 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2371 | str( expectedFormat ) ) ) |
| 2372 | |
| 2373 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2374 | actual = actual.rstrip() |
| 2375 | expected = expected.rstrip() |
| 2376 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2377 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2378 | marker1 = actual.find( 'allocated' ) |
| 2379 | m1 = actual[ :marker1 ] |
| 2380 | marker2 = expected.find( 'allocated' ) |
| 2381 | m2 = expected[ :marker2 ] |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2382 | if m1 != m2: |
| 2383 | bandwidthFailed = True |
| 2384 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2385 | bandwidthFailed = True |
| 2386 | expectedFormat.close() |
| 2387 | ONOSOutput.close() |
| 2388 | |
| 2389 | if bandwidthFailed: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2390 | main.log.error( "Bandwidth not allocated correctly using Intents!!" ) |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2391 | returnValue = main.FALSE |
| 2392 | return returnValue |
| 2393 | except TypeError: |
| 2394 | main.log.exception( self.name + ": Object not as expected" ) |
| 2395 | return None |
| 2396 | except pexpect.EOF: |
| 2397 | main.log.error( self.name + ": EOF exception found" ) |
| 2398 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2399 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2400 | except Exception: |
| 2401 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2402 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2403 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2404 | def compareIntent( self, intentDict ): |
| 2405 | """ |
| 2406 | Description: |
| 2407 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2408 | Return: |
| 2409 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2410 | Arguments: |
| 2411 | intentDict: a dictionary which maps intent ids to intent states |
| 2412 | """ |
| 2413 | try: |
| 2414 | intentsRaw = self.intents() |
| 2415 | intentsJson = json.loads( intentsRaw ) |
| 2416 | intentDictONOS = {} |
| 2417 | for intent in intentsJson: |
| 2418 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2419 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2420 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2421 | 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] | 2422 | str( len( intentDict ) ) + " expected and " + |
| 2423 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2424 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2425 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2426 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2427 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2428 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2429 | else: |
| 2430 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2431 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2432 | " expected state is " + intentDict[ intentID ] + |
| 2433 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2434 | returnValue = main.FALSE |
| 2435 | intentDictONOS.pop( intentID ) |
| 2436 | if len( intentDictONOS ) > 0: |
| 2437 | returnValue = main.FALSE |
| 2438 | for intentID in intentDictONOS.keys(): |
| 2439 | 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] | 2440 | if returnValue == main.TRUE: |
| 2441 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2442 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2443 | except KeyError: |
| 2444 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2445 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2446 | except ( TypeError, ValueError ): |
| 2447 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2448 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2449 | except pexpect.EOF: |
| 2450 | main.log.error( self.name + ": EOF exception found" ) |
| 2451 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2452 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2453 | except Exception: |
| 2454 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2455 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2456 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2457 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2458 | """ |
| 2459 | Description: |
| 2460 | Check the number of installed intents. |
| 2461 | Optional: |
| 2462 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2463 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2464 | Return: |
| 2465 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2466 | , otherwise, returns main.FALSE. |
| 2467 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2468 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2469 | try: |
| 2470 | cmd = "intents -s -j" |
| 2471 | |
| 2472 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2473 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2474 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2475 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2476 | response = json.loads( response ) |
| 2477 | |
| 2478 | # get total and installed number, see if they are match |
| 2479 | allState = response.get( 'all' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2480 | if allState.get( 'total' ) == allState.get( 'installed' ): |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2481 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( |
| 2482 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2483 | return main.TRUE |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 2484 | main.log.info( 'Verified Intents failed Expected intents: {} installed intents: {}'.format( |
| 2485 | allState.get( 'total' ), allState.get( 'installed' ) ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2486 | return main.FALSE |
| 2487 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2488 | except ( TypeError, ValueError ): |
| 2489 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2490 | return None |
| 2491 | except pexpect.EOF: |
| 2492 | main.log.error( self.name + ": EOF exception found" ) |
| 2493 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2494 | if noExit: |
| 2495 | return main.FALSE |
| 2496 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2497 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2498 | except pexpect.TIMEOUT: |
| 2499 | main.log.error( self.name + ": ONOS timeout" ) |
| 2500 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2501 | except Exception: |
| 2502 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2503 | if noExit: |
| 2504 | return main.FALSE |
| 2505 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2506 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2507 | |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2508 | 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] | 2509 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2510 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2511 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2512 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2513 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2514 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2515 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2516 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2517 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2518 | if jsonFormat: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2519 | cmdStr += " -j" |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2520 | if noCore: |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2521 | cmdStr += " -n" |
| 2522 | cmdStr += " " + state |
| 2523 | cmdStr += " " + device |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2524 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2525 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2526 | assert "Command not found:" not in handle, handle |
| 2527 | if re.search( "Error:", handle ): |
| 2528 | main.log.error( self.name + ": flows() response: " + |
| 2529 | str( handle ) ) |
| 2530 | return handle |
| 2531 | except AssertionError: |
| 2532 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2533 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2534 | except TypeError: |
| 2535 | main.log.exception( self.name + ": Object not as expected" ) |
| 2536 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2537 | except pexpect.TIMEOUT: |
| 2538 | main.log.error( self.name + ": ONOS timeout" ) |
| 2539 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2540 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2541 | main.log.error( self.name + ": EOF exception found" ) |
| 2542 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2543 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2544 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2545 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2546 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2547 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2548 | def checkFlowCount( self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2549 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2550 | count = int( count ) if count else 0 |
| 2551 | return count if ( count > min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2552 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2553 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2554 | """ |
| 2555 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2556 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2557 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2558 | if the count of those states is 0, which means all current flows |
| 2559 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2560 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2561 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2562 | Return: |
| 2563 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2564 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2565 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2566 | """ |
| 2567 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2568 | states = [ "PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED" ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2569 | checkedStates = [] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2570 | statesCount = [ 0, 0, 0, 0 ] |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2571 | for s in states: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2572 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2573 | if rawFlows: |
| 2574 | # if we didn't get flows or flows function return None, we should return |
| 2575 | # main.Flase |
| 2576 | checkedStates.append( json.loads( rawFlows ) ) |
| 2577 | else: |
| 2578 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2579 | for i in range( len( states ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2580 | for c in checkedStates[ i ]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2581 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2582 | statesCount[ i ] += int( c.get( "flowCount" ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2583 | except TypeError: |
| 2584 | main.log.exception( "Json object not as expected" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2585 | main.log.info( states[ i ] + " flows: " + str( statesCount[ i ] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2586 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2587 | # We want to count PENDING_ADD if isPENDING is true |
| 2588 | if isPENDING: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2589 | if statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2590 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2591 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2592 | if statesCount[ 0 ] + statesCount[ 1 ] + statesCount[ 2 ] + statesCount[ 3 ] > 0: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2593 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2594 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2595 | except ( TypeError, ValueError ): |
| 2596 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2597 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2598 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2599 | except AssertionError: |
| 2600 | main.log.exception( "" ) |
| 2601 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2602 | except pexpect.TIMEOUT: |
| 2603 | main.log.error( self.name + ": ONOS timeout" ) |
| 2604 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2605 | except pexpect.EOF: |
| 2606 | main.log.error( self.name + ": EOF exception found" ) |
| 2607 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2608 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2609 | except Exception: |
| 2610 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2611 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2612 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2613 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2614 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2615 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2616 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2617 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2618 | a specific point-to-point intent definition |
| 2619 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2620 | * ingress: specify source dpid |
| 2621 | * egress: specify destination dpid |
| 2622 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2623 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2624 | * offset: the keyOffset is where the next batch of intents |
| 2625 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2626 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2627 | * getResponse: If set to True, function will return ONOS response. |
| 2628 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2629 | Returns: If failed to push test intents, it will returen None, |
| 2630 | if successful, return true. |
| 2631 | Timeout expection will return None, |
| 2632 | TypeError will return false |
| 2633 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2634 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2635 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2636 | if background: |
| 2637 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2638 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2639 | back = "" |
| 2640 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2641 | ingress, |
| 2642 | egress, |
| 2643 | batchSize, |
| 2644 | offset, |
| 2645 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2646 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2647 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2648 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2649 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2650 | if getResponse: |
| 2651 | return response |
| 2652 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2653 | # TODO: We should handle if there is failure in installation |
| 2654 | return main.TRUE |
| 2655 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2656 | except AssertionError: |
| 2657 | main.log.exception( "" ) |
| 2658 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2659 | except pexpect.TIMEOUT: |
| 2660 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2661 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2662 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2663 | main.log.error( self.name + ": EOF exception found" ) |
| 2664 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2665 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2666 | except TypeError: |
| 2667 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2668 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2669 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2670 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2671 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2672 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2673 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2674 | """ |
| 2675 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2676 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2677 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2678 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2679 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2680 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2681 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2682 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2683 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2684 | cmd = "flows -c added" |
| 2685 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2686 | if rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2687 | rawFlows = rawFlows.split( "\n" ) |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2688 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2689 | for l in rawFlows: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2690 | totalFlows += int( l.split( "Count=" )[ 1 ] ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2691 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2692 | main.log.error( "Response not as expected!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2693 | return None |
| 2694 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2695 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2696 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2697 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2698 | return None |
| 2699 | except pexpect.EOF: |
| 2700 | main.log.error( self.name + ": EOF exception found" ) |
| 2701 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2702 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2703 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2704 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2705 | except pexpect.TIMEOUT: |
| 2706 | main.log.error( self.name + ": ONOS timeout" ) |
| 2707 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2708 | except Exception: |
| 2709 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2710 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2711 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2712 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2713 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 2714 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2715 | """ |
| 2716 | Description: |
| 2717 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2718 | Optional: |
| 2719 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2720 | Return: |
| 2721 | The number of intents |
| 2722 | """ |
| 2723 | try: |
| 2724 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2725 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2726 | if response is None: |
| 2727 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2728 | response = json.loads( response ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2729 | return int( response.get( "intents" ) ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2730 | except ( TypeError, ValueError ): |
| 2731 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2732 | return None |
| 2733 | except pexpect.EOF: |
| 2734 | main.log.error( self.name + ": EOF exception found" ) |
| 2735 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2736 | if noExit: |
| 2737 | return -1 |
| 2738 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2739 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2740 | except Exception: |
| 2741 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2742 | if noExit: |
| 2743 | return -1 |
| 2744 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2745 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2746 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2747 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2748 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2749 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2750 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2751 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2752 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2753 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2754 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2755 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2756 | cmdStr += " -j" |
| 2757 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2758 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2759 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2760 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2761 | except AssertionError: |
| 2762 | main.log.exception( "" ) |
| 2763 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2764 | except TypeError: |
| 2765 | main.log.exception( self.name + ": Object not as expected" ) |
| 2766 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2767 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 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() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2771 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2772 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2773 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2774 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2775 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2776 | """ |
| 2777 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2778 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2779 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2780 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2781 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2782 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2783 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2784 | cmdStr += " -j" |
| 2785 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2786 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2787 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2788 | if handle: |
| 2789 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2790 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2791 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2792 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2793 | else: |
| 2794 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2795 | except AssertionError: |
| 2796 | main.log.exception( "" ) |
| 2797 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2798 | except TypeError: |
| 2799 | main.log.exception( self.name + ": Object not as expected" ) |
| 2800 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2801 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2802 | main.log.error( self.name + ": EOF exception found" ) |
| 2803 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2804 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2805 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2806 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2807 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2808 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2809 | # Wrapper functions **************** |
| 2810 | # Wrapper functions use existing driver |
| 2811 | # functions and extends their use case. |
| 2812 | # For example, we may use the output of |
| 2813 | # a normal driver function, and parse it |
| 2814 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2815 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2816 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2817 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2818 | Description: |
| 2819 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2820 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2821 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2822 | # Obtain output of intents function |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 2823 | intentsStr = self.intents( jsonFormat=True ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 2824 | if intentsStr is None: |
| 2825 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2826 | # Convert to a dictionary |
| 2827 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2828 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2829 | for intent in intents: |
| 2830 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2831 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2832 | except TypeError: |
| 2833 | main.log.exception( self.name + ": Object not as expected" ) |
| 2834 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2835 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2836 | main.log.error( self.name + ": EOF exception found" ) |
| 2837 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2838 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2839 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2840 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2841 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2842 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2843 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2844 | """ |
| 2845 | Determine the number of flow rules for the given device id that are |
| 2846 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2847 | Params: |
| 2848 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2849 | """ |
| 2850 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2851 | if core: |
| 2852 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2853 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2854 | else: |
| 2855 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2856 | "grep 'state=ADDED' | wc -l" |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2857 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2858 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2859 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2860 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2861 | except AssertionError: |
| 2862 | main.log.exception( "" ) |
| 2863 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2864 | except pexpect.EOF: |
| 2865 | main.log.error( self.name + ": EOF exception found" ) |
| 2866 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2867 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2868 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2869 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2870 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2871 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2872 | def groupAddedCount( self, deviceId, core=False ): |
| 2873 | """ |
| 2874 | Determine the number of group rules for the given device id that are |
| 2875 | in the added state |
| 2876 | Params: |
| 2877 | core: if True, only return the number of core groups added |
| 2878 | """ |
| 2879 | try: |
| 2880 | if core: |
| 2881 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 2882 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2883 | else: |
| 2884 | cmdStr = "groups any " + str( deviceId ) + " | " +\ |
| 2885 | "grep 'state=ADDED' | wc -l" |
| 2886 | handle = self.sendline( cmdStr ) |
| 2887 | assert handle is not None, "Error in sendline" |
| 2888 | assert "Command not found:" not in handle, handle |
| 2889 | return handle |
| 2890 | except AssertionError: |
| 2891 | main.log.exception( "" ) |
| 2892 | return None |
| 2893 | except pexpect.EOF: |
| 2894 | main.log.error( self.name + ": EOF exception found" ) |
| 2895 | main.log.error( self.name + ": " + self.handle.before ) |
| 2896 | main.cleanAndExit() |
| 2897 | except Exception: |
| 2898 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2899 | main.cleanAndExit() |
| 2900 | |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 2901 | def addStaticRoute( self, subnet, intf): |
| 2902 | """ |
| 2903 | Adds a static route to onos. |
| 2904 | Params: |
| 2905 | subnet: The subnet reaching through this route |
| 2906 | intf: The interface this route is reachable through |
| 2907 | """ |
| 2908 | try: |
| 2909 | cmdStr = "route-add " + subnet + " " + intf |
| 2910 | handle = self.sendline( cmdStr ) |
| 2911 | assert handle is not None, "Error in sendline" |
| 2912 | assert "Command not found:" not in handle, handle |
| 2913 | return handle |
| 2914 | except AssertionError: |
| 2915 | main.log.exception( "" ) |
| 2916 | return None |
| 2917 | except pexpect.EOF: |
| 2918 | main.log.error( self.name + ": EOF exception found" ) |
| 2919 | main.log.error( self.name + ": " + self.handle.before ) |
| 2920 | main.cleanAndExit() |
| 2921 | except Exception: |
| 2922 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2923 | main.cleanAndExit() |
| 2924 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2925 | def checkGroupAddedCount( self, deviceId, expectedGroupCount=0, core=False, comparison=0): |
| 2926 | """ |
| 2927 | Description: |
| 2928 | Check whether the number of groups for the given device id that |
| 2929 | are in ADDED state is bigger than minGroupCount. |
| 2930 | Required: |
| 2931 | * deviceId: device id to check the number of added group rules |
| 2932 | Optional: |
| 2933 | * minGroupCount: the number of groups to compare |
| 2934 | * core: if True, only check the number of core groups added |
| 2935 | * comparison: if 0, compare with greater than minFlowCount |
| 2936 | * if 1, compare with equal to minFlowCount |
| 2937 | Return: |
| 2938 | Returns the number of groups if it is bigger than minGroupCount, |
| 2939 | returns main.FALSE otherwise. |
| 2940 | """ |
| 2941 | count = self.groupAddedCount( deviceId, core ) |
| 2942 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 2943 | main.log.debug( "found {} groups".format( count ) ) |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2944 | return count if ((count > expectedGroupCount) if (comparison == 0) else (count == expectedGroupCount)) else main.FALSE |
| 2945 | |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 2946 | def getGroups( self, deviceId, groupType="any" ): |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2947 | """ |
| 2948 | Retrieve groups from a specific device. |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 2949 | deviceId: Id of the device from which we retrieve groups |
| 2950 | groupType: Type of group |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2951 | """ |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2952 | try: |
You Wang | c02f3be | 2018-05-18 12:14:23 -0700 | [diff] [blame] | 2953 | groupCmd = "groups -t {0} any {1}".format( groupType, deviceId ) |
| 2954 | handle = self.sendline( groupCmd ) |
Andreas Pantelopoulos | df5061f | 2018-05-15 11:46:59 -0700 | [diff] [blame] | 2955 | assert handle is not None, "Error in sendline" |
| 2956 | assert "Command not found:" not in handle, handle |
| 2957 | return handle |
| 2958 | except AssertionError: |
| 2959 | main.log.exception( "" ) |
| 2960 | return None |
| 2961 | except TypeError: |
| 2962 | main.log.exception( self.name + ": Object not as expected" ) |
| 2963 | return None |
| 2964 | except pexpect.EOF: |
| 2965 | main.log.error( self.name + ": EOF exception found" ) |
| 2966 | main.log.error( self.name + ": " + self.handle.before ) |
| 2967 | main.cleanAndExit() |
| 2968 | except Exception: |
| 2969 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2970 | main.cleanAndExit() |
| 2971 | |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2972 | def checkFlowAddedCount( self, deviceId, expectedFlowCount=0, core=False, comparison=0): |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 2973 | """ |
| 2974 | Description: |
| 2975 | Check whether the number of flow rules for the given device id that |
| 2976 | are in ADDED state is bigger than minFlowCount. |
| 2977 | Required: |
| 2978 | * deviceId: device id to check the number of added flow rules |
| 2979 | Optional: |
| 2980 | * minFlowCount: the number of flow rules to compare |
| 2981 | * core: if True, only check the number of core flows added |
Andreas Pantelopoulos | 9173d44 | 2018-03-01 17:07:37 -0800 | [diff] [blame] | 2982 | * comparison: if 0, compare with greater than minFlowCount |
| 2983 | * if 1, compare with equal to minFlowCount |
Jonghwan Hyun | cf2345c | 2018-02-26 11:07:54 -0800 | [diff] [blame] | 2984 | Return: |
| 2985 | Returns the number of flow rules if it is bigger than minFlowCount, |
| 2986 | returns main.FALSE otherwise. |
| 2987 | """ |
| 2988 | count = self.flowAddedCount( deviceId, core ) |
| 2989 | count = int( count ) if count else 0 |
Jon Hall | 9677ed3 | 2018-04-24 11:16:23 -0700 | [diff] [blame] | 2990 | main.log.debug( "found {} flows".format( count ) ) |
Andreas Pantelopoulos | 2eae324 | 2018-03-06 13:47:20 -0800 | [diff] [blame] | 2991 | 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] | 2992 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2993 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2994 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2995 | Use 'devices' function to obtain list of all devices |
| 2996 | and parse the result to obtain a list of all device |
| 2997 | id's. Returns this list. Returns empty list if no |
| 2998 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2999 | List is ordered sequentially |
| 3000 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3001 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3002 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 3003 | the ids. By obtaining the list of device ids on the fly, |
| 3004 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3005 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3006 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3007 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3008 | devicesStr = self.devices( jsonFormat=False ) |
| 3009 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3010 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3011 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3012 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3013 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3014 | |
| 3015 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3016 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3017 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3018 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3019 | # Split list further into arguments before and after string |
| 3020 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3021 | # append to idList |
| 3022 | for arg in tempList: |
| 3023 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 3024 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3025 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3026 | except TypeError: |
| 3027 | main.log.exception( self.name + ": Object not as expected" ) |
| 3028 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3029 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3030 | main.log.error( self.name + ": EOF exception found" ) |
| 3031 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3032 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3033 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3034 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3035 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3036 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3037 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3038 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3039 | Uses 'nodes' function to obtain list of all nodes |
| 3040 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3041 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3042 | Returns: |
| 3043 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3044 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3045 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3046 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3047 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3048 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 3049 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3050 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3051 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3052 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3053 | nodesJson = json.loads( nodesStr ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3054 | idList = [ node.get( 'id' ) for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3055 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3056 | except ( TypeError, ValueError ): |
| 3057 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3058 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 3059 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3060 | main.log.error( self.name + ": EOF exception found" ) |
| 3061 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3062 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3063 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3064 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3065 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 3066 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3067 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3068 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3069 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 3070 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3071 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3072 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3073 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3074 | return None |
| 3075 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3076 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3077 | rawDevices = self.devices() |
| 3078 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3079 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3080 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3081 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 3082 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3083 | return device |
| 3084 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3085 | except ( TypeError, ValueError ): |
| 3086 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3087 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3088 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3089 | main.log.error( self.name + ": EOF exception found" ) |
| 3090 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3091 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3092 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3093 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3094 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 3095 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3096 | def getTopology( self, topologyOutput ): |
| 3097 | """ |
| 3098 | Definition: |
| 3099 | Loads a json topology output |
| 3100 | Return: |
| 3101 | topology = current ONOS topology |
| 3102 | """ |
| 3103 | import json |
| 3104 | try: |
| 3105 | # either onos:topology or 'topology' will work in CLI |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3106 | topology = json.loads( topologyOutput ) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 3107 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3108 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 3109 | except ( TypeError, ValueError ): |
| 3110 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 3111 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3112 | except pexpect.EOF: |
| 3113 | main.log.error( self.name + ": EOF exception found" ) |
| 3114 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3115 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3116 | except Exception: |
| 3117 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3118 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3119 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3120 | def checkStatus( self, numoswitch, numolink = -1, numoctrl = -1, logLevel="info" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3121 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3122 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3123 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3124 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3125 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3126 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3127 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3128 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3129 | logLevel = level to log to. |
| 3130 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3131 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3132 | Returns: main.TRUE if the number of switches and links are correct, |
| 3133 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3134 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3135 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3136 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3137 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 3138 | summary = self.summary() |
| 3139 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 3140 | except ( TypeError, ValueError ): |
| 3141 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 3142 | return main.ERROR |
| 3143 | try: |
| 3144 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 3145 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3146 | return main.ERROR |
| 3147 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3148 | # Is the number of switches is what we expected |
| 3149 | devices = topology.get( 'devices', False ) |
| 3150 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3151 | nodes = summary.get( 'nodes', False ) |
| 3152 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3153 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3154 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3155 | # Is the number of links is what we expected |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3156 | linkCheck = ( int( links ) == int( numolink ) ) or int( numolink ) == -1 |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3157 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 3158 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3159 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3160 | output = output + "The number of links and switches match "\ |
| 3161 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3162 | result = main.TRUE |
| 3163 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3164 | output = output + \ |
| 3165 | "The number of links and switches does not match " + \ |
| 3166 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3167 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3168 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 3169 | output = output + " (%i expected) " % int( numoswitch ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 3170 | if int( numolink ) > 0: |
| 3171 | output = output + "and %i links " % int( links ) |
| 3172 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 3173 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 3174 | output = output + "and %i controllers " % int( nodes ) |
| 3175 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3176 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3177 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3178 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3179 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3180 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 3181 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3182 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 3183 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3184 | main.log.error( self.name + ": EOF exception found" ) |
| 3185 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3186 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3187 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3188 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3189 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3190 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3191 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3192 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3193 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3194 | deviceId must be the id of a device as seen in the onos devices command |
| 3195 | 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] | 3196 | role must be either master, standby, or none |
| 3197 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3198 | Returns: |
| 3199 | main.TRUE or main.FALSE based on argument verification and |
| 3200 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3201 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3202 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3203 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3204 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3205 | cmdStr = "device-role " +\ |
| 3206 | str( deviceId ) + " " +\ |
| 3207 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3208 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3209 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3210 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3211 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3212 | if re.search( "Error", handle ): |
| 3213 | # end color output to escape any colours |
| 3214 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3215 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3216 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3217 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3218 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3219 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3220 | main.log.error( "Invalid 'role' given to device_role(). " + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3221 | "Value was '" + str( role ) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3222 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3223 | except AssertionError: |
| 3224 | main.log.exception( "" ) |
| 3225 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3226 | except TypeError: |
| 3227 | main.log.exception( self.name + ": Object not as expected" ) |
| 3228 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3229 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3230 | main.log.error( self.name + ": EOF exception found" ) |
| 3231 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3232 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3233 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3234 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3235 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3236 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3237 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3238 | """ |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3239 | Lists all topology clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 3240 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3241 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3242 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3243 | try: |
Jon Hall | 0dd0995 | 2018-04-19 09:59:11 -0700 | [diff] [blame] | 3244 | cmdStr = "topo-clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3245 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3246 | cmdStr += " -j" |
| 3247 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3248 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3249 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3250 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3251 | except AssertionError: |
| 3252 | main.log.exception( "" ) |
| 3253 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3254 | except TypeError: |
| 3255 | main.log.exception( self.name + ": Object not as expected" ) |
| 3256 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3257 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3258 | main.log.error( self.name + ": EOF exception found" ) |
| 3259 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3260 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3261 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3262 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3263 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3264 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3265 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3266 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3267 | CLI command to get the current leader for the Election test application |
| 3268 | NOTE: Requires installation of the onos-app-election feature |
| 3269 | Returns: Node IP of the leader if one exists |
| 3270 | None if none exists |
| 3271 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3272 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3273 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3274 | cmdStr = "election-test-leader" |
| 3275 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3276 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3277 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3278 | # Leader |
| 3279 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3280 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3281 | nodeSearch = re.search( leaderPattern, response ) |
| 3282 | if nodeSearch: |
| 3283 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3284 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3285 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3286 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3287 | # no leader |
| 3288 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3289 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3290 | nullSearch = re.search( nullPattern, response ) |
| 3291 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3292 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3293 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3294 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3295 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3296 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 3297 | ": " + "unexpected response" ) |
| 3298 | main.log.error( repr( response ) ) |
| 3299 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3300 | except AssertionError: |
| 3301 | main.log.exception( "" ) |
| 3302 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3303 | except TypeError: |
| 3304 | main.log.exception( self.name + ": Object not as expected" ) |
| 3305 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3306 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3307 | main.log.error( self.name + ": EOF exception found" ) |
| 3308 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3309 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3310 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3311 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3312 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3313 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3314 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3315 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3316 | CLI command to run for leadership of the Election test application. |
| 3317 | NOTE: Requires installation of the onos-app-election feature |
| 3318 | Returns: Main.TRUE on success |
| 3319 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3320 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3321 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3322 | cmdStr = "election-test-run" |
| 3323 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3324 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3325 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3326 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3327 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3328 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3329 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3330 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3331 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3332 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3333 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3334 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3335 | main.log.error( "Error in electionTestRun on " + self.name + |
| 3336 | ": " + "unexpected response" ) |
| 3337 | main.log.error( repr( response ) ) |
| 3338 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3339 | except AssertionError: |
| 3340 | main.log.exception( "" ) |
| 3341 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3342 | except TypeError: |
| 3343 | main.log.exception( self.name + ": Object not as expected" ) |
| 3344 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3345 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3346 | main.log.error( self.name + ": EOF exception found" ) |
| 3347 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3348 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3349 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3350 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3351 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3352 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3353 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3354 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3355 | * CLI command to withdraw the local node from leadership election for |
| 3356 | * the Election test application. |
| 3357 | #NOTE: Requires installation of the onos-app-election feature |
| 3358 | Returns: Main.TRUE on success |
| 3359 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3360 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3361 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3362 | cmdStr = "election-test-withdraw" |
| 3363 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3364 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3365 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3366 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3367 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3368 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3369 | if re.search( successPattern, response ): |
| 3370 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3371 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3372 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3373 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3374 | main.log.error( "Error in electionTestWithdraw on " + |
| 3375 | self.name + ": " + "unexpected response" ) |
| 3376 | main.log.error( repr( response ) ) |
| 3377 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3378 | except AssertionError: |
| 3379 | main.log.exception( "" ) |
| 3380 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3381 | except TypeError: |
| 3382 | main.log.exception( self.name + ": Object not as expected" ) |
| 3383 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3384 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3385 | main.log.error( self.name + ": EOF exception found" ) |
| 3386 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3387 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3388 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3389 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3390 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3391 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3392 | def getDevicePortsEnabledCount( self, dpid ): |
| 3393 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3394 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3395 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3396 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3397 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3398 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 3399 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3400 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3401 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3402 | if re.search( "No such device", output ): |
| 3403 | main.log.error( "Error in getting ports" ) |
| 3404 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3405 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3406 | except AssertionError: |
| 3407 | main.log.exception( "" ) |
| 3408 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3409 | except TypeError: |
| 3410 | main.log.exception( self.name + ": Object not as expected" ) |
| 3411 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3412 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3413 | main.log.error( self.name + ": EOF exception found" ) |
| 3414 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3415 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3416 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3417 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3418 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3419 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3420 | def getDeviceLinksActiveCount( self, dpid ): |
| 3421 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3422 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3423 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3424 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3425 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3426 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3427 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3428 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3429 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3430 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3431 | main.log.error( "Error in getting ports " ) |
| 3432 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3433 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3434 | except AssertionError: |
| 3435 | main.log.exception( "" ) |
| 3436 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3437 | except TypeError: |
| 3438 | main.log.exception( self.name + ": Object not as expected" ) |
| 3439 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3440 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3441 | main.log.error( self.name + ": EOF exception found" ) |
| 3442 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3443 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3444 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3445 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3446 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3447 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3448 | def getAllIntentIds( self ): |
| 3449 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3450 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3451 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3452 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3453 | cmdStr = "onos:intents | grep id=" |
| 3454 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3455 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3456 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3457 | if re.search( "Error", output ): |
| 3458 | main.log.error( "Error in getting ports" ) |
| 3459 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3460 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3461 | except AssertionError: |
| 3462 | main.log.exception( "" ) |
| 3463 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3464 | except TypeError: |
| 3465 | main.log.exception( self.name + ": Object not as expected" ) |
| 3466 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3467 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3468 | main.log.error( self.name + ": EOF exception found" ) |
| 3469 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3470 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3471 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3472 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3473 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3474 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3475 | def intentSummary( self ): |
| 3476 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3477 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3478 | """ |
| 3479 | try: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3480 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3481 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3482 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3483 | states.append( intent.get( 'state', None ) ) |
| 3484 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3485 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3486 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3487 | except ( TypeError, ValueError ): |
| 3488 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3489 | return None |
| 3490 | except pexpect.EOF: |
| 3491 | main.log.error( self.name + ": EOF exception found" ) |
| 3492 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3493 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3494 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3495 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3496 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3497 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3498 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3499 | """ |
| 3500 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3501 | Optional argument: |
| 3502 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3503 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3504 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3505 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3506 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3507 | cmdStr += " -j" |
| 3508 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3509 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3510 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3511 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3512 | except AssertionError: |
| 3513 | main.log.exception( "" ) |
| 3514 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3515 | except TypeError: |
| 3516 | main.log.exception( self.name + ": Object not as expected" ) |
| 3517 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3518 | except pexpect.EOF: |
| 3519 | main.log.error( self.name + ": EOF exception found" ) |
| 3520 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3521 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3522 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3523 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3524 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3525 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3526 | def leaderCandidates( self, jsonFormat=True ): |
| 3527 | """ |
| 3528 | Returns the output of the leaders -c command. |
| 3529 | Optional argument: |
| 3530 | * jsonFormat - boolean indicating if you want output in json |
| 3531 | """ |
| 3532 | try: |
| 3533 | cmdStr = "onos:leaders -c" |
| 3534 | if jsonFormat: |
| 3535 | cmdStr += " -j" |
| 3536 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3537 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3538 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3539 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3540 | except AssertionError: |
| 3541 | main.log.exception( "" ) |
| 3542 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3543 | except TypeError: |
| 3544 | main.log.exception( self.name + ": Object not as expected" ) |
| 3545 | return None |
| 3546 | except pexpect.EOF: |
| 3547 | main.log.error( self.name + ": EOF exception found" ) |
| 3548 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3549 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3550 | except Exception: |
| 3551 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3552 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3553 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3554 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3555 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3556 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3557 | topic parameter and an empty list if the topic doesn't exist |
| 3558 | If no leader is elected leader in the returned list will be "none" |
| 3559 | Returns None if there is a type error processing the json object |
| 3560 | """ |
| 3561 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3562 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3563 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3564 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3565 | assert "Command not found:" not in rawOutput, rawOutput |
| 3566 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3567 | results = [] |
| 3568 | for dict in output: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3569 | if dict[ "topic" ] == topic: |
| 3570 | leader = dict[ "leader" ] |
| 3571 | candidates = re.split( ", ", dict[ "candidates" ][ 1:-1 ] ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3572 | results.append( leader ) |
| 3573 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3574 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3575 | except AssertionError: |
| 3576 | main.log.exception( "" ) |
| 3577 | return None |
| 3578 | except ( TypeError, ValueError ): |
| 3579 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3580 | return None |
| 3581 | except pexpect.EOF: |
| 3582 | main.log.error( self.name + ": EOF exception found" ) |
| 3583 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3584 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3585 | except Exception: |
| 3586 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3587 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3588 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3589 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3590 | """ |
| 3591 | Returns the output of the intent Pending map. |
| 3592 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3593 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3594 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3595 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3596 | cmdStr += " -j" |
| 3597 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3598 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3599 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3600 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3601 | except AssertionError: |
| 3602 | main.log.exception( "" ) |
| 3603 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3604 | except TypeError: |
| 3605 | main.log.exception( self.name + ": Object not as expected" ) |
| 3606 | return None |
| 3607 | except pexpect.EOF: |
| 3608 | main.log.error( self.name + ": EOF exception found" ) |
| 3609 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3610 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3611 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3612 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3613 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3614 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3615 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3616 | """ |
| 3617 | Returns the output of the raft partitions command for ONOS. |
| 3618 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3619 | # Sample JSON |
| 3620 | # { |
| 3621 | # "leader": "tcp://10.128.30.11:7238", |
| 3622 | # "members": [ |
| 3623 | # "tcp://10.128.30.11:7238", |
| 3624 | # "tcp://10.128.30.17:7238", |
| 3625 | # "tcp://10.128.30.13:7238", |
| 3626 | # ], |
| 3627 | # "name": "p1", |
| 3628 | # "term": 3 |
| 3629 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3630 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3631 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3632 | if candidates: |
| 3633 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3634 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3635 | cmdStr += " -j" |
| 3636 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3637 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3638 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3639 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3640 | except AssertionError: |
| 3641 | main.log.exception( "" ) |
| 3642 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3643 | except TypeError: |
| 3644 | main.log.exception( self.name + ": Object not as expected" ) |
| 3645 | return None |
| 3646 | except pexpect.EOF: |
| 3647 | main.log.error( self.name + ": EOF exception found" ) |
| 3648 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3649 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3650 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3651 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3652 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3653 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3654 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3655 | """ |
| 3656 | Returns the output of the apps command for ONOS. This command lists |
| 3657 | information about installed ONOS applications |
| 3658 | """ |
| 3659 | # Sample JSON object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3660 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3661 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3662 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 3663 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3664 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3665 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3666 | if summary: |
| 3667 | cmdStr += " -s" |
| 3668 | if active: |
| 3669 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3670 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3671 | cmdStr += " -j" |
| 3672 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3673 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3674 | assert "Command not found:" not in output, output |
| 3675 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3676 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3677 | # FIXME: look at specific exceptions/Errors |
| 3678 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3679 | main.log.exception( "Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3680 | return None |
| 3681 | except TypeError: |
| 3682 | main.log.exception( self.name + ": Object not as expected" ) |
| 3683 | return None |
| 3684 | except pexpect.EOF: |
| 3685 | main.log.error( self.name + ": EOF exception found" ) |
| 3686 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3687 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3688 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3689 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3690 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3691 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3692 | def appStatus( self, appName ): |
| 3693 | """ |
| 3694 | Uses the onos:apps cli command to return the status of an application. |
| 3695 | Returns: |
| 3696 | "ACTIVE" - If app is installed and activated |
| 3697 | "INSTALLED" - If app is installed and deactivated |
| 3698 | "UNINSTALLED" - If app is not installed |
| 3699 | None - on error |
| 3700 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3701 | try: |
| 3702 | if not isinstance( appName, types.StringType ): |
| 3703 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3704 | " a string" ) |
| 3705 | return None |
| 3706 | output = self.apps( jsonFormat=True ) |
| 3707 | appsJson = json.loads( output ) |
| 3708 | state = None |
| 3709 | for app in appsJson: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3710 | if appName == app.get( 'name' ): |
| 3711 | state = app.get( 'state' ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3712 | break |
| 3713 | if state == "ACTIVE" or state == "INSTALLED": |
| 3714 | return state |
| 3715 | elif state is None: |
Jon Hall | 8bafdc0 | 2017-09-05 11:36:26 -0700 | [diff] [blame] | 3716 | main.log.warn( "{} app not found", appName ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3717 | return "UNINSTALLED" |
| 3718 | elif state: |
| 3719 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3720 | str( state ) ) |
| 3721 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3722 | except ( TypeError, ValueError ): |
| 3723 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3724 | return None |
| 3725 | except pexpect.EOF: |
| 3726 | main.log.error( self.name + ": EOF exception found" ) |
| 3727 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3728 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3729 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3730 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3731 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3732 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3733 | def app( self, appName, option ): |
| 3734 | """ |
| 3735 | Interacts with the app command for ONOS. This command manages |
| 3736 | application inventory. |
| 3737 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3738 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3739 | # Validate argument types |
| 3740 | valid = True |
| 3741 | if not isinstance( appName, types.StringType ): |
| 3742 | main.log.error( self.name + ".app(): appName must be a " + |
| 3743 | "string" ) |
| 3744 | valid = False |
| 3745 | if not isinstance( option, types.StringType ): |
| 3746 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3747 | valid = False |
| 3748 | if not valid: |
| 3749 | return main.FALSE |
| 3750 | # Validate Option |
| 3751 | option = option.lower() |
| 3752 | # NOTE: Install may become a valid option |
| 3753 | if option == "activate": |
| 3754 | pass |
| 3755 | elif option == "deactivate": |
| 3756 | pass |
| 3757 | elif option == "uninstall": |
| 3758 | pass |
| 3759 | else: |
| 3760 | # Invalid option |
| 3761 | main.log.error( "The ONOS app command argument only takes " + |
| 3762 | "the values: (activate|deactivate|uninstall)" + |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3763 | "; was given '" + option + "'" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3764 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3765 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3766 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3767 | assert output is not None, "Error in sendline" |
| 3768 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3769 | if "Error executing command" in output: |
| 3770 | main.log.error( "Error in processing onos:app command: " + |
| 3771 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3772 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3773 | elif "No such application" in output: |
| 3774 | main.log.error( "The application '" + appName + |
| 3775 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3776 | return main.FALSE |
| 3777 | elif "Command not found:" in output: |
| 3778 | main.log.error( "Error in processing onos:app command: " + |
| 3779 | str( output ) ) |
| 3780 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3781 | elif "Unsupported command:" in output: |
| 3782 | main.log.error( "Incorrect command given to 'app': " + |
| 3783 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3784 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3785 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3786 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3787 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3788 | except AssertionError: |
| 3789 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3790 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3791 | except TypeError: |
| 3792 | main.log.exception( self.name + ": Object not as expected" ) |
| 3793 | return main.ERROR |
| 3794 | except pexpect.EOF: |
| 3795 | main.log.error( self.name + ": EOF exception found" ) |
| 3796 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3797 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3798 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3799 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3800 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3801 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3802 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3803 | """ |
| 3804 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3805 | appName is the hierarchical app name, not the feature name |
| 3806 | If check is True, method will check the status of the app after the |
| 3807 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3808 | Returns main.TRUE if the command was successfully sent |
| 3809 | main.FALSE if the cli responded with an error or given |
| 3810 | incorrect input |
| 3811 | """ |
| 3812 | try: |
| 3813 | if not isinstance( appName, types.StringType ): |
| 3814 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3815 | " a string" ) |
| 3816 | return main.FALSE |
| 3817 | status = self.appStatus( appName ) |
| 3818 | if status == "INSTALLED": |
| 3819 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3820 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3821 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3822 | status = self.appStatus( appName ) |
| 3823 | if status == "ACTIVE": |
| 3824 | return main.TRUE |
| 3825 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3826 | main.log.debug( "The state of application " + |
| 3827 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3828 | time.sleep( 1 ) |
| 3829 | return main.FALSE |
| 3830 | else: # not 'check' or command didn't succeed |
| 3831 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3832 | elif status == "ACTIVE": |
| 3833 | return main.TRUE |
| 3834 | elif status == "UNINSTALLED": |
| 3835 | main.log.error( self.name + ": Tried to activate the " + |
| 3836 | "application '" + appName + "' which is not " + |
| 3837 | "installed." ) |
| 3838 | else: |
| 3839 | main.log.error( "Unexpected return value from appStatus: " + |
| 3840 | str( status ) ) |
| 3841 | return main.ERROR |
| 3842 | except TypeError: |
| 3843 | main.log.exception( self.name + ": Object not as expected" ) |
| 3844 | return main.ERROR |
| 3845 | except pexpect.EOF: |
| 3846 | main.log.error( self.name + ": EOF exception found" ) |
| 3847 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3848 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3849 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3850 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3851 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3852 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3853 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3854 | """ |
| 3855 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3856 | appName is the hierarchical app name, not the feature name |
| 3857 | If check is True, method will check the status of the app after the |
| 3858 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3859 | Returns main.TRUE if the command was successfully sent |
| 3860 | main.FALSE if the cli responded with an error or given |
| 3861 | incorrect input |
| 3862 | """ |
| 3863 | try: |
| 3864 | if not isinstance( appName, types.StringType ): |
| 3865 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3866 | "be a string" ) |
| 3867 | return main.FALSE |
| 3868 | status = self.appStatus( appName ) |
| 3869 | if status == "INSTALLED": |
| 3870 | return main.TRUE |
| 3871 | elif status == "ACTIVE": |
| 3872 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3873 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3874 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3875 | status = self.appStatus( appName ) |
| 3876 | if status == "INSTALLED": |
| 3877 | return main.TRUE |
| 3878 | else: |
| 3879 | time.sleep( 1 ) |
| 3880 | return main.FALSE |
| 3881 | else: # not check or command didn't succeed |
| 3882 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3883 | elif status == "UNINSTALLED": |
| 3884 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3885 | "application '" + appName + "' which is not " + |
| 3886 | "installed." ) |
| 3887 | return main.TRUE |
| 3888 | else: |
| 3889 | main.log.error( "Unexpected return value from appStatus: " + |
| 3890 | str( status ) ) |
| 3891 | return main.ERROR |
| 3892 | except TypeError: |
| 3893 | main.log.exception( self.name + ": Object not as expected" ) |
| 3894 | return main.ERROR |
| 3895 | except pexpect.EOF: |
| 3896 | main.log.error( self.name + ": EOF exception found" ) |
| 3897 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3898 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3899 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3900 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3901 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3902 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3903 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3904 | """ |
| 3905 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3906 | appName is the hierarchical app name, not the feature name |
| 3907 | If check is True, method will check the status of the app after the |
| 3908 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3909 | Returns main.TRUE if the command was successfully sent |
| 3910 | main.FALSE if the cli responded with an error or given |
| 3911 | incorrect input |
| 3912 | """ |
| 3913 | # TODO: check with Thomas about the state machine for apps |
| 3914 | try: |
| 3915 | if not isinstance( appName, types.StringType ): |
| 3916 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3917 | "be a string" ) |
| 3918 | return main.FALSE |
| 3919 | status = self.appStatus( appName ) |
| 3920 | if status == "INSTALLED": |
| 3921 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3922 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3923 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3924 | status = self.appStatus( appName ) |
| 3925 | if status == "UNINSTALLED": |
| 3926 | return main.TRUE |
| 3927 | else: |
| 3928 | time.sleep( 1 ) |
| 3929 | return main.FALSE |
| 3930 | else: # not check or command didn't succeed |
| 3931 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3932 | elif status == "ACTIVE": |
| 3933 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3934 | "application '" + appName + "' which is " + |
| 3935 | "currently active." ) |
| 3936 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3937 | if check and response == main.TRUE: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 3938 | for i in range( 10 ): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3939 | status = self.appStatus( appName ) |
| 3940 | if status == "UNINSTALLED": |
| 3941 | return main.TRUE |
| 3942 | else: |
| 3943 | time.sleep( 1 ) |
| 3944 | return main.FALSE |
| 3945 | else: # not check or command didn't succeed |
| 3946 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3947 | elif status == "UNINSTALLED": |
| 3948 | return main.TRUE |
| 3949 | else: |
| 3950 | main.log.error( "Unexpected return value from appStatus: " + |
| 3951 | str( status ) ) |
| 3952 | return main.ERROR |
| 3953 | except TypeError: |
| 3954 | main.log.exception( self.name + ": Object not as expected" ) |
| 3955 | return main.ERROR |
| 3956 | except pexpect.EOF: |
| 3957 | main.log.error( self.name + ": EOF exception found" ) |
| 3958 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3959 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3960 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3961 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3962 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3963 | |
| 3964 | def appIDs( self, jsonFormat=True ): |
| 3965 | """ |
| 3966 | Show the mappings between app id and app names given by the 'app-ids' |
| 3967 | cli command |
| 3968 | """ |
| 3969 | try: |
| 3970 | cmdStr = "app-ids" |
| 3971 | if jsonFormat: |
| 3972 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3973 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3974 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3975 | assert "Command not found:" not in output, output |
| 3976 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3977 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3978 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3979 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3980 | return None |
| 3981 | except TypeError: |
| 3982 | main.log.exception( self.name + ": Object not as expected" ) |
| 3983 | return None |
| 3984 | except pexpect.EOF: |
| 3985 | main.log.error( self.name + ": EOF exception found" ) |
| 3986 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3987 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3988 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3989 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3990 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3991 | |
| 3992 | def appToIDCheck( self ): |
| 3993 | """ |
| 3994 | This method will check that each application's ID listed in 'apps' is |
| 3995 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3996 | there are no duplicate IDs issued. Note that an app ID should be |
| 3997 | a globaly unique numerical identifier for app/app-like features. Once |
| 3998 | an ID is registered, the ID is never freed up so that if an app is |
| 3999 | reinstalled it will have the same ID. |
| 4000 | |
| 4001 | Returns: main.TRUE if the check passes and |
| 4002 | main.FALSE if the check fails or |
| 4003 | main.ERROR if there is some error in processing the test |
| 4004 | """ |
| 4005 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4006 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4007 | rawJson = self.appIDs( jsonFormat=True ) |
| 4008 | if rawJson: |
| 4009 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4010 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4011 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4012 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4013 | rawJson = self.apps( jsonFormat=True ) |
| 4014 | if rawJson: |
| 4015 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4016 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4017 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4018 | bail = True |
| 4019 | if bail: |
| 4020 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4021 | result = main.TRUE |
| 4022 | for app in apps: |
| 4023 | appID = app.get( 'id' ) |
| 4024 | if appID is None: |
| 4025 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4026 | result = main.FALSE |
| 4027 | appName = app.get( 'name' ) |
| 4028 | if appName is None: |
| 4029 | main.log.error( "Error parsing app: " + str( app ) ) |
| 4030 | result = main.FALSE |
| 4031 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4032 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4033 | if not current: # if ids doesn't have this id |
| 4034 | result = main.FALSE |
| 4035 | main.log.error( "'app-ids' does not have the ID for " + |
| 4036 | str( appName ) + " that apps does." ) |
Jon Hall | b9d381e | 2018-02-05 12:02:10 -0800 | [diff] [blame] | 4037 | main.log.debug( "apps command returned: " + str( app ) + |
| 4038 | "; app-ids has: " + str( ids ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4039 | elif len( current ) > 1: |
| 4040 | # there is more than one app with this ID |
| 4041 | result = main.FALSE |
| 4042 | # We will log this later in the method |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4043 | elif not current[ 0 ][ 'name' ] == appName: |
| 4044 | currentName = current[ 0 ][ 'name' ] |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4045 | result = main.FALSE |
| 4046 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 4047 | " registered under id:" + str( appID ) + |
| 4048 | " but 'apps' has " + str( appName ) ) |
| 4049 | else: |
| 4050 | pass # id and name match! |
| 4051 | # now make sure that app-ids has no duplicates |
| 4052 | idsList = [] |
| 4053 | namesList = [] |
| 4054 | for item in ids: |
| 4055 | idsList.append( item[ 'id' ] ) |
| 4056 | namesList.append( item[ 'name' ] ) |
| 4057 | if len( idsList ) != len( set( idsList ) ) or\ |
| 4058 | len( namesList ) != len( set( namesList ) ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4059 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 4060 | + json.dumps( ids, |
| 4061 | sort_keys=True, |
| 4062 | indent=4, |
| 4063 | separators=( ',', ': ' ) ) ) |
| 4064 | result = main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4065 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4066 | except ( TypeError, ValueError ): |
| 4067 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4068 | return main.ERROR |
| 4069 | except pexpect.EOF: |
| 4070 | main.log.error( self.name + ": EOF exception found" ) |
| 4071 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4072 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 4073 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4074 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4075 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 4076 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4077 | def getCfg( self, component=None, propName=None, short=False, |
| 4078 | jsonFormat=True ): |
| 4079 | """ |
| 4080 | Get configuration settings from onos cli |
| 4081 | Optional arguments: |
| 4082 | component - Optionally only list configurations for a specific |
| 4083 | component. If None, all components with configurations |
| 4084 | are displayed. Case Sensitive string. |
| 4085 | propName - If component is specified, propName option will show |
| 4086 | only this specific configuration from that component. |
| 4087 | Case Sensitive string. |
| 4088 | jsonFormat - Returns output as json. Note that this will override |
| 4089 | the short option |
| 4090 | short - Short, less verbose, version of configurations. |
| 4091 | This is overridden by the json option |
| 4092 | returns: |
| 4093 | Output from cli as a string or None on error |
| 4094 | """ |
| 4095 | try: |
| 4096 | baseStr = "cfg" |
| 4097 | cmdStr = " get" |
| 4098 | componentStr = "" |
| 4099 | if component: |
| 4100 | componentStr += " " + component |
| 4101 | if propName: |
| 4102 | componentStr += " " + propName |
| 4103 | if jsonFormat: |
| 4104 | baseStr += " -j" |
| 4105 | elif short: |
| 4106 | baseStr += " -s" |
| 4107 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4108 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4109 | assert "Command not found:" not in output, output |
| 4110 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4111 | return output |
| 4112 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4113 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4114 | return None |
| 4115 | except TypeError: |
| 4116 | main.log.exception( self.name + ": Object not as expected" ) |
| 4117 | return None |
| 4118 | except pexpect.EOF: |
| 4119 | main.log.error( self.name + ": EOF exception found" ) |
| 4120 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4121 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4122 | except Exception: |
| 4123 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4124 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4125 | |
| 4126 | def setCfg( self, component, propName, value=None, check=True ): |
| 4127 | """ |
| 4128 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4129 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4130 | component - The case sensitive name of the component whose |
| 4131 | property is to be set |
| 4132 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4133 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4134 | 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] | 4135 | property and revert it to it's default value(if applicable) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4136 | check - Boolean, Check whether the option was successfully set this |
| 4137 | only applies when a value is given. |
| 4138 | returns: |
| 4139 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 4140 | will return main.TRUE unless there is an error |
| 4141 | """ |
| 4142 | try: |
| 4143 | baseStr = "cfg" |
| 4144 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 4145 | if value is not None: |
| 4146 | cmdStr += " " + str( value ) |
| 4147 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4148 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4149 | assert "Command not found:" not in output, output |
| 4150 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4151 | if value and check: |
| 4152 | results = self.getCfg( component=str( component ), |
| 4153 | propName=str( propName ), |
| 4154 | jsonFormat=True ) |
| 4155 | # Check if current value is what we just set |
| 4156 | try: |
| 4157 | jsonOutput = json.loads( results ) |
| 4158 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4159 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4160 | main.log.exception( "Error parsing cfg output" ) |
| 4161 | main.log.error( "output:" + repr( results ) ) |
| 4162 | return main.FALSE |
| 4163 | if current == str( value ): |
| 4164 | return main.TRUE |
| 4165 | return main.FALSE |
| 4166 | return main.TRUE |
| 4167 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4168 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4169 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4170 | except ( TypeError, ValueError ): |
| 4171 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4172 | return main.FALSE |
| 4173 | except pexpect.EOF: |
| 4174 | main.log.error( self.name + ": EOF exception found" ) |
| 4175 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4176 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4177 | except Exception: |
| 4178 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4179 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 4180 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4181 | def distPrimitivesSend( self, cmd ): |
| 4182 | """ |
| 4183 | Function to handle sending cli commands for the distributed primitives test app |
| 4184 | |
| 4185 | This command will catch some exceptions and retry the command on some |
| 4186 | specific store exceptions. |
| 4187 | |
| 4188 | Required arguments: |
| 4189 | cmd - The command to send to the cli |
| 4190 | returns: |
| 4191 | string containing the cli output |
| 4192 | None on Error |
| 4193 | """ |
| 4194 | try: |
| 4195 | output = self.sendline( cmd ) |
| 4196 | try: |
| 4197 | assert output is not None, "Error in sendline" |
| 4198 | # TODO: Maybe make this less hardcoded |
| 4199 | # ConsistentMap Exceptions |
| 4200 | assert "org.onosproject.store.service" not in output |
| 4201 | # Node not leader |
| 4202 | assert "java.lang.IllegalStateException" not in output |
| 4203 | except AssertionError: |
| 4204 | main.log.error( "Error in processing '" + cmd + "' " + |
| 4205 | "command: " + str( output ) ) |
| 4206 | retryTime = 30 # Conservative time, given by Madan |
| 4207 | main.log.info( "Waiting " + str( retryTime ) + |
| 4208 | "seconds before retrying." ) |
| 4209 | time.sleep( retryTime ) # Due to change in mastership |
| 4210 | output = self.sendline( cmd ) |
| 4211 | assert output is not None, "Error in sendline" |
| 4212 | assert "Command not found:" not in output, output |
| 4213 | assert "Error executing command" not in output, output |
| 4214 | main.log.info( self.name + ": " + output ) |
| 4215 | return output |
| 4216 | except AssertionError: |
| 4217 | main.log.exception( "Error in processing '" + cmd + "' command." ) |
| 4218 | return None |
| 4219 | except TypeError: |
| 4220 | main.log.exception( self.name + ": Object not as expected" ) |
| 4221 | return None |
| 4222 | except pexpect.EOF: |
| 4223 | main.log.error( self.name + ": EOF exception found" ) |
| 4224 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4225 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4226 | except Exception: |
| 4227 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4228 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4229 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4230 | def setTestAdd( self, setName, values ): |
| 4231 | """ |
| 4232 | CLI command to add elements to a distributed set. |
| 4233 | Arguments: |
| 4234 | setName - The name of the set to add to. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4235 | values - The value(s) to add to the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4236 | Example usages: |
| 4237 | setTestAdd( "set1", "a b c" ) |
| 4238 | setTestAdd( "set2", "1" ) |
| 4239 | returns: |
| 4240 | main.TRUE on success OR |
| 4241 | main.FALSE if elements were already in the set OR |
| 4242 | main.ERROR on error |
| 4243 | """ |
| 4244 | try: |
| 4245 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4246 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4247 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 4248 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4249 | if re.search( positiveMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4250 | return main.TRUE |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4251 | elif re.search( negativeMatch, output ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4252 | return main.FALSE |
| 4253 | else: |
| 4254 | main.log.error( self.name + ": setTestAdd did not" + |
| 4255 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4256 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4257 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4258 | except TypeError: |
| 4259 | main.log.exception( self.name + ": Object not as expected" ) |
| 4260 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4261 | except Exception: |
| 4262 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4263 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4264 | |
| 4265 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4266 | """ |
| 4267 | CLI command to remove elements from a distributed set. |
| 4268 | Required arguments: |
| 4269 | setName - The name of the set to remove from. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4270 | values - The value(s) to remove from the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4271 | Optional arguments: |
| 4272 | clear - Clear all elements from the set |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4273 | retain - Retain only the given values. (intersection of the |
| 4274 | original set and the given set) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4275 | returns: |
| 4276 | main.TRUE on success OR |
| 4277 | main.FALSE if the set was not changed OR |
| 4278 | main.ERROR on error |
| 4279 | """ |
| 4280 | try: |
| 4281 | cmdStr = "set-test-remove " |
| 4282 | if clear: |
| 4283 | cmdStr += "-c " + str( setName ) |
| 4284 | elif retain: |
| 4285 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4286 | else: |
| 4287 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4288 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4289 | if clear: |
| 4290 | pattern = "Set " + str( setName ) + " cleared" |
| 4291 | if re.search( pattern, output ): |
| 4292 | return main.TRUE |
| 4293 | elif retain: |
| 4294 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4295 | "only elements of set \[(.*)\]" |
| 4296 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4297 | "retaining only elements of the set " +\ |
| 4298 | "\[(.*)\]" |
| 4299 | if re.search( positivePattern, output ): |
| 4300 | return main.TRUE |
| 4301 | elif re.search( negativePattern, output ): |
| 4302 | return main.FALSE |
| 4303 | else: |
| 4304 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4305 | str( setName ) |
| 4306 | if ( len( values.split() ) == 1 ): |
| 4307 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4308 | str( setName ) |
| 4309 | else: |
| 4310 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4311 | str( setName ) |
| 4312 | if re.search( positivePattern, output ): |
| 4313 | return main.TRUE |
| 4314 | elif re.search( negativePattern, output ): |
| 4315 | return main.FALSE |
| 4316 | main.log.error( self.name + ": setTestRemove did not" + |
| 4317 | " match expected output" ) |
| 4318 | main.log.debug( self.name + " expected: " + pattern ) |
| 4319 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4320 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4321 | except TypeError: |
| 4322 | main.log.exception( self.name + ": Object not as expected" ) |
| 4323 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4324 | except Exception: |
| 4325 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4326 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4327 | |
| 4328 | def setTestGet( self, setName, values="" ): |
| 4329 | """ |
| 4330 | CLI command to get the elements in a distributed set. |
| 4331 | Required arguments: |
| 4332 | setName - The name of the set to remove from. |
| 4333 | Optional arguments: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4334 | values - The value(s) to check if in the set, space seperated. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4335 | returns: |
| 4336 | main.ERROR on error OR |
| 4337 | A list of elements in the set if no optional arguments are |
| 4338 | supplied OR |
| 4339 | A tuple containing the list then: |
| 4340 | main.FALSE if the given values are not in the set OR |
| 4341 | main.TRUE if the given values are in the set OR |
| 4342 | """ |
| 4343 | try: |
| 4344 | values = str( values ).strip() |
| 4345 | setName = str( setName ).strip() |
| 4346 | length = len( values.split() ) |
| 4347 | containsCheck = None |
| 4348 | # Patterns to match |
| 4349 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4350 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4351 | containsTrue = "Set " + setName + " contains the value " + values |
| 4352 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4353 | values |
| 4354 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4355 | setPattern |
| 4356 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4357 | " subset " + setPattern |
| 4358 | |
| 4359 | cmdStr = "set-test-get " |
| 4360 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4361 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4362 | if length == 0: |
| 4363 | match = re.search( pattern, output ) |
| 4364 | else: # if given values |
| 4365 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4366 | patternTrue = pattern + "\r\n" + containsTrue |
| 4367 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4368 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4369 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4370 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4371 | matchTrue = re.search( patternTrue, output ) |
| 4372 | matchFalse = re.search( patternFalse, output ) |
| 4373 | if matchTrue: |
| 4374 | containsCheck = main.TRUE |
| 4375 | match = matchTrue |
| 4376 | elif matchFalse: |
| 4377 | containsCheck = main.FALSE |
| 4378 | match = matchFalse |
| 4379 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4380 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4381 | "expected output" ) |
| 4382 | main.log.debug( self.name + " expected: " + pattern ) |
| 4383 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4384 | match = None |
| 4385 | if match: |
| 4386 | setMatch = match.group( 1 ) |
| 4387 | if setMatch == '': |
| 4388 | setList = [] |
| 4389 | else: |
| 4390 | setList = setMatch.split( ", " ) |
| 4391 | if length > 0: |
| 4392 | return ( setList, containsCheck ) |
| 4393 | else: |
| 4394 | return setList |
| 4395 | else: # no match |
| 4396 | main.log.error( self.name + ": setTestGet did not" + |
| 4397 | " match expected output" ) |
| 4398 | main.log.debug( self.name + " expected: " + pattern ) |
| 4399 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4400 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4401 | except TypeError: |
| 4402 | main.log.exception( self.name + ": Object not as expected" ) |
| 4403 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4404 | except Exception: |
| 4405 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4406 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4407 | |
| 4408 | def setTestSize( self, setName ): |
| 4409 | """ |
| 4410 | CLI command to get the elements in a distributed set. |
| 4411 | Required arguments: |
| 4412 | setName - The name of the set to remove from. |
| 4413 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4414 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4415 | None on error |
| 4416 | """ |
| 4417 | try: |
| 4418 | # TODO: Should this check against the number of elements returned |
| 4419 | # and then return true/false based on that? |
| 4420 | setName = str( setName ).strip() |
| 4421 | # Patterns to match |
| 4422 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4423 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4424 | setPattern |
| 4425 | cmdStr = "set-test-get -s " |
| 4426 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4427 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4428 | match = re.search( pattern, output ) |
| 4429 | if match: |
| 4430 | setSize = int( match.group( 1 ) ) |
| 4431 | setMatch = match.group( 2 ) |
| 4432 | if len( setMatch.split() ) == setSize: |
| 4433 | main.log.info( "The size returned by " + self.name + |
| 4434 | " matches the number of elements in " + |
| 4435 | "the returned set" ) |
| 4436 | else: |
| 4437 | main.log.error( "The size returned by " + self.name + |
| 4438 | " does not match the number of " + |
| 4439 | "elements in the returned set." ) |
| 4440 | return setSize |
| 4441 | else: # no match |
| 4442 | main.log.error( self.name + ": setTestGet did not" + |
| 4443 | " match expected output" ) |
| 4444 | main.log.debug( self.name + " expected: " + pattern ) |
| 4445 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4446 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4447 | except TypeError: |
| 4448 | main.log.exception( self.name + ": Object not as expected" ) |
| 4449 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4450 | except Exception: |
| 4451 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4452 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4453 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4454 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4455 | """ |
| 4456 | Command to list the various counters in the system. |
| 4457 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4458 | if jsonFormat, a string of the json object returned by the cli |
| 4459 | command |
| 4460 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4461 | None on error |
| 4462 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4463 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4464 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4465 | if jsonFormat: |
| 4466 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4467 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4468 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4469 | assert "Command not found:" not in output, output |
| 4470 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4471 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4472 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4473 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4474 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4475 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4476 | except TypeError: |
| 4477 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4478 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4479 | except pexpect.EOF: |
| 4480 | main.log.error( self.name + ": EOF exception found" ) |
| 4481 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4482 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4483 | except Exception: |
| 4484 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4485 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4486 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4487 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4488 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4489 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4490 | Required arguments: |
| 4491 | counter - The name of the counter to increment. |
| 4492 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4493 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4494 | returns: |
| 4495 | integer value of the counter or |
| 4496 | None on Error |
| 4497 | """ |
| 4498 | try: |
| 4499 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4500 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4501 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4502 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4503 | if delta != 1: |
| 4504 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4505 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4506 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4507 | match = re.search( pattern, output ) |
| 4508 | if match: |
| 4509 | return int( match.group( 1 ) ) |
| 4510 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4511 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4512 | " match expected output." ) |
| 4513 | main.log.debug( self.name + " expected: " + pattern ) |
| 4514 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4515 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4516 | except TypeError: |
| 4517 | main.log.exception( self.name + ": Object not as expected" ) |
| 4518 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4519 | except Exception: |
| 4520 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4521 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4522 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4523 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4524 | """ |
| 4525 | CLI command to get a distributed counter then add a delta to it. |
| 4526 | Required arguments: |
| 4527 | counter - The name of the counter to increment. |
| 4528 | Optional arguments: |
| 4529 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4530 | returns: |
| 4531 | integer value of the counter or |
| 4532 | None on Error |
| 4533 | """ |
| 4534 | try: |
| 4535 | counter = str( counter ) |
| 4536 | delta = int( delta ) |
| 4537 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4538 | cmdStr += counter |
| 4539 | if delta != 1: |
| 4540 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4541 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4542 | pattern = counter + " was updated to (-?\d+)" |
| 4543 | match = re.search( pattern, output ) |
| 4544 | if match: |
| 4545 | return int( match.group( 1 ) ) |
| 4546 | else: |
| 4547 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4548 | " match expected output." ) |
| 4549 | main.log.debug( self.name + " expected: " + pattern ) |
| 4550 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4551 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4552 | except TypeError: |
| 4553 | main.log.exception( self.name + ": Object not as expected" ) |
| 4554 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4555 | except Exception: |
| 4556 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4557 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4558 | |
| 4559 | def valueTestGet( self, valueName ): |
| 4560 | """ |
| 4561 | CLI command to get the value of an atomic value. |
| 4562 | Required arguments: |
| 4563 | valueName - The name of the value to get. |
| 4564 | returns: |
| 4565 | string value of the value or |
| 4566 | None on Error |
| 4567 | """ |
| 4568 | try: |
| 4569 | valueName = str( valueName ) |
| 4570 | cmdStr = "value-test " |
| 4571 | operation = "get" |
| 4572 | cmdStr = "value-test {} {}".format( valueName, |
| 4573 | operation ) |
| 4574 | output = self.distPrimitivesSend( cmdStr ) |
| 4575 | pattern = "(\w+)" |
| 4576 | match = re.search( pattern, output ) |
| 4577 | if match: |
| 4578 | return match.group( 1 ) |
| 4579 | else: |
| 4580 | main.log.error( self.name + ": valueTestGet did not" + |
| 4581 | " match expected output." ) |
| 4582 | main.log.debug( self.name + " expected: " + pattern ) |
| 4583 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4584 | return None |
| 4585 | except TypeError: |
| 4586 | main.log.exception( self.name + ": Object not as expected" ) |
| 4587 | return None |
| 4588 | except Exception: |
| 4589 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4590 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4591 | |
| 4592 | def valueTestSet( self, valueName, newValue ): |
| 4593 | """ |
| 4594 | CLI command to set the value of an atomic value. |
| 4595 | Required arguments: |
| 4596 | valueName - The name of the value to set. |
| 4597 | newValue - The value to assign to the given value. |
| 4598 | returns: |
| 4599 | main.TRUE on success or |
| 4600 | main.ERROR on Error |
| 4601 | """ |
| 4602 | try: |
| 4603 | valueName = str( valueName ) |
| 4604 | newValue = str( newValue ) |
| 4605 | operation = "set" |
| 4606 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4607 | operation, |
| 4608 | newValue ) |
| 4609 | output = self.distPrimitivesSend( cmdStr ) |
| 4610 | if output is not None: |
| 4611 | return main.TRUE |
| 4612 | else: |
| 4613 | return main.ERROR |
| 4614 | except TypeError: |
| 4615 | main.log.exception( self.name + ": Object not as expected" ) |
| 4616 | return main.ERROR |
| 4617 | except Exception: |
| 4618 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4619 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4620 | |
| 4621 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4622 | """ |
| 4623 | CLI command to compareAndSet the value of an atomic value. |
| 4624 | Required arguments: |
| 4625 | valueName - The name of the value. |
| 4626 | oldValue - Compare the current value of the atomic value to this |
| 4627 | newValue - If the value equals oldValue, set the value to newValue |
| 4628 | returns: |
| 4629 | main.TRUE on success or |
| 4630 | main.FALSE on failure or |
| 4631 | main.ERROR on Error |
| 4632 | """ |
| 4633 | try: |
| 4634 | valueName = str( valueName ) |
| 4635 | oldValue = str( oldValue ) |
| 4636 | newValue = str( newValue ) |
| 4637 | operation = "compareAndSet" |
| 4638 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4639 | operation, |
| 4640 | oldValue, |
| 4641 | newValue ) |
| 4642 | output = self.distPrimitivesSend( cmdStr ) |
| 4643 | pattern = "(\w+)" |
| 4644 | match = re.search( pattern, output ) |
| 4645 | if match: |
| 4646 | result = match.group( 1 ) |
| 4647 | if result == "true": |
| 4648 | return main.TRUE |
| 4649 | elif result == "false": |
| 4650 | return main.FALSE |
| 4651 | else: |
| 4652 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4653 | " match expected output." ) |
| 4654 | main.log.debug( self.name + " expected: " + pattern ) |
| 4655 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4656 | return main.ERROR |
| 4657 | except TypeError: |
| 4658 | main.log.exception( self.name + ": Object not as expected" ) |
| 4659 | return main.ERROR |
| 4660 | except Exception: |
| 4661 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4662 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4663 | |
| 4664 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4665 | """ |
| 4666 | CLI command to getAndSet the value of an atomic value. |
| 4667 | Required arguments: |
| 4668 | valueName - The name of the value to get. |
| 4669 | newValue - The value to assign to the given value |
| 4670 | returns: |
| 4671 | string value of the value or |
| 4672 | None on Error |
| 4673 | """ |
| 4674 | try: |
| 4675 | valueName = str( valueName ) |
| 4676 | cmdStr = "value-test " |
| 4677 | operation = "getAndSet" |
| 4678 | cmdStr += valueName + " " + operation |
| 4679 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4680 | operation, |
| 4681 | newValue ) |
| 4682 | output = self.distPrimitivesSend( cmdStr ) |
| 4683 | pattern = "(\w+)" |
| 4684 | match = re.search( pattern, output ) |
| 4685 | if match: |
| 4686 | return match.group( 1 ) |
| 4687 | else: |
| 4688 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4689 | " match expected output." ) |
| 4690 | main.log.debug( self.name + " expected: " + pattern ) |
| 4691 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4692 | return None |
| 4693 | except TypeError: |
| 4694 | main.log.exception( self.name + ": Object not as expected" ) |
| 4695 | return None |
| 4696 | except Exception: |
| 4697 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4698 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4699 | |
| 4700 | def valueTestDestroy( self, valueName ): |
| 4701 | """ |
| 4702 | CLI command to destroy an atomic value. |
| 4703 | Required arguments: |
| 4704 | valueName - The name of the value to destroy. |
| 4705 | returns: |
| 4706 | main.TRUE on success or |
| 4707 | main.ERROR on Error |
| 4708 | """ |
| 4709 | try: |
| 4710 | valueName = str( valueName ) |
| 4711 | cmdStr = "value-test " |
| 4712 | operation = "destroy" |
| 4713 | cmdStr += valueName + " " + operation |
| 4714 | output = self.distPrimitivesSend( cmdStr ) |
| 4715 | if output is not None: |
| 4716 | return main.TRUE |
| 4717 | else: |
| 4718 | return main.ERROR |
| 4719 | except TypeError: |
| 4720 | main.log.exception( self.name + ": Object not as expected" ) |
| 4721 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4722 | except Exception: |
| 4723 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4724 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4725 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4726 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4727 | """ |
| 4728 | Description: Execute summary command in onos |
| 4729 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4730 | no output |
| 4731 | |
| 4732 | """ |
| 4733 | try: |
| 4734 | cmdStr = "summary" |
| 4735 | if jsonFormat: |
| 4736 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4737 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4738 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4739 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4740 | assert "Error:" not in handle, handle |
Devin Lim | a7cfdbd | 2017-09-29 15:02:22 -0700 | [diff] [blame] | 4741 | assert "Error executing" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4742 | if not handle: |
| 4743 | main.log.error( self.name + ": There is no output in " + |
| 4744 | "summary command" ) |
| 4745 | return main.FALSE |
| 4746 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4747 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4748 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4749 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4750 | except TypeError: |
| 4751 | main.log.exception( self.name + ": Object not as expected" ) |
| 4752 | return None |
| 4753 | except pexpect.EOF: |
| 4754 | main.log.error( self.name + ": EOF exception found" ) |
| 4755 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4756 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4757 | except Exception: |
| 4758 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4759 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4760 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4761 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4762 | """ |
| 4763 | CLI command to get the value of a key in a consistent map using |
| 4764 | transactions. This a test function and can only get keys from the |
| 4765 | test map hard coded into the cli command |
| 4766 | Required arguments: |
| 4767 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4768 | returns: |
| 4769 | The string value of the key or |
| 4770 | None on Error |
| 4771 | """ |
| 4772 | try: |
| 4773 | keyName = str( keyName ) |
| 4774 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4775 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4776 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4777 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4778 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4779 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4780 | return None |
| 4781 | else: |
| 4782 | match = re.search( pattern, output ) |
| 4783 | if match: |
| 4784 | return match.groupdict()[ 'value' ] |
| 4785 | else: |
| 4786 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4787 | " match expected output." ) |
| 4788 | main.log.debug( self.name + " expected: " + pattern ) |
| 4789 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4790 | return None |
| 4791 | except TypeError: |
| 4792 | main.log.exception( self.name + ": Object not as expected" ) |
| 4793 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4794 | except Exception: |
| 4795 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4796 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4797 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4798 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4799 | """ |
| 4800 | CLI command to put a value into 'numKeys' number of keys in a |
| 4801 | consistent map using transactions. This a test function and can only |
| 4802 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4803 | Required arguments: |
| 4804 | numKeys - Number of keys to add the value to |
| 4805 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4806 | returns: |
| 4807 | A dictionary whose keys are the name of the keys put into the map |
| 4808 | and the values of the keys are dictionaries whose key-values are |
| 4809 | 'value': value put into map and optionaly |
| 4810 | 'oldValue': Previous value in the key or |
| 4811 | None on Error |
| 4812 | |
| 4813 | Example output |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4814 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4815 | 'Key2': {'value': 'Testing'} } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4816 | """ |
| 4817 | try: |
| 4818 | numKeys = str( numKeys ) |
| 4819 | value = str( value ) |
| 4820 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4821 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4822 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4823 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4824 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4825 | results = {} |
| 4826 | for line in output.splitlines(): |
| 4827 | new = re.search( newPattern, line ) |
| 4828 | updated = re.search( updatedPattern, line ) |
| 4829 | if new: |
| 4830 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4831 | elif updated: |
| 4832 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4833 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4834 | else: |
| 4835 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4836 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4837 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4838 | newPattern, |
| 4839 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4840 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4841 | return results |
| 4842 | except TypeError: |
| 4843 | main.log.exception( self.name + ": Object not as expected" ) |
| 4844 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4845 | except Exception: |
| 4846 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4847 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4848 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4849 | def maps( self, jsonFormat=True ): |
| 4850 | """ |
| 4851 | Description: Returns result of onos:maps |
| 4852 | Optional: |
| 4853 | * jsonFormat: enable json formatting of output |
| 4854 | """ |
| 4855 | try: |
| 4856 | cmdStr = "maps" |
| 4857 | if jsonFormat: |
| 4858 | cmdStr += " -j" |
| 4859 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4860 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4861 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4862 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4863 | except AssertionError: |
| 4864 | main.log.exception( "" ) |
| 4865 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4866 | except TypeError: |
| 4867 | main.log.exception( self.name + ": Object not as expected" ) |
| 4868 | return None |
| 4869 | except pexpect.EOF: |
| 4870 | main.log.error( self.name + ": EOF exception found" ) |
| 4871 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4872 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4873 | except Exception: |
| 4874 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4875 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4876 | |
| 4877 | def getSwController( self, uri, jsonFormat=True ): |
| 4878 | """ |
| 4879 | Descrition: Gets the controller information from the device |
| 4880 | """ |
| 4881 | try: |
| 4882 | cmd = "device-controllers " |
| 4883 | if jsonFormat: |
| 4884 | cmd += "-j " |
| 4885 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4886 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4887 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4888 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4889 | except AssertionError: |
| 4890 | main.log.exception( "" ) |
| 4891 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4892 | except TypeError: |
| 4893 | main.log.exception( self.name + ": Object not as expected" ) |
| 4894 | return None |
| 4895 | except pexpect.EOF: |
| 4896 | main.log.error( self.name + ": EOF exception found" ) |
| 4897 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4898 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4899 | except Exception: |
| 4900 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4901 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4902 | |
| 4903 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4904 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4905 | Descrition: sets the controller(s) for the specified device |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4906 | |
| 4907 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4908 | Required: uri - String: The uri of the device(switch). |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4909 | ip - String or List: The ip address of the controller. |
| 4910 | This parameter can be formed in a couple of different ways. |
| 4911 | VALID: |
| 4912 | 10.0.0.1 - just the ip address |
| 4913 | tcp:10.0.0.1 - the protocol and the ip address |
| 4914 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4915 | so that you can add controllers with different |
| 4916 | protocols and ports |
| 4917 | INVALID: |
| 4918 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4919 | |
| 4920 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4921 | port - The port number. |
| 4922 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4923 | |
| 4924 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4925 | """ |
| 4926 | try: |
| 4927 | cmd = "device-setcontrollers" |
| 4928 | |
| 4929 | if jsonFormat: |
| 4930 | cmd += " -j" |
| 4931 | cmd += " " + uri |
| 4932 | if isinstance( ip, str ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4933 | ip = [ ip ] |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4934 | for item in ip: |
| 4935 | if ":" in item: |
| 4936 | sitem = item.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4937 | if len( sitem ) == 3: |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4938 | cmd += " " + item |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4939 | elif "." in sitem[ 1 ]: |
| 4940 | cmd += " {}:{}".format( item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4941 | else: |
| 4942 | main.log.error( "Malformed entry: " + item ) |
| 4943 | raise TypeError |
| 4944 | else: |
| 4945 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4946 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4947 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4948 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4949 | if "Error" in response: |
| 4950 | main.log.error( response ) |
| 4951 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4952 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4953 | except AssertionError: |
| 4954 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4955 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4956 | except TypeError: |
| 4957 | main.log.exception( self.name + ": Object not as expected" ) |
| 4958 | return main.FALSE |
| 4959 | except pexpect.EOF: |
| 4960 | main.log.error( self.name + ": EOF exception found" ) |
| 4961 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4962 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4963 | except Exception: |
| 4964 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4965 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4966 | |
| 4967 | def removeDevice( self, device ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4968 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4969 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4970 | Remove a device from ONOS by passing the uri of the device(s). |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4971 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4972 | 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] | 4973 | Returns: |
| 4974 | Returns main.FALSE if an exception is thrown or an error is present |
| 4975 | in the response. Otherwise, returns main.TRUE. |
| 4976 | NOTE: |
| 4977 | 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] | 4978 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4979 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 4980 | if isinstance( device, str ): |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 4981 | deviceStr = device |
| 4982 | device = [] |
| 4983 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4984 | |
| 4985 | for d in device: |
| 4986 | time.sleep( 1 ) |
| 4987 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4988 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4989 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4990 | if "Error" in response: |
| 4991 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4992 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4993 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4994 | except AssertionError: |
| 4995 | main.log.exception( "" ) |
| 4996 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4997 | except TypeError: |
| 4998 | main.log.exception( self.name + ": Object not as expected" ) |
| 4999 | return main.FALSE |
| 5000 | except pexpect.EOF: |
| 5001 | main.log.error( self.name + ": EOF exception found" ) |
| 5002 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5003 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5004 | except Exception: |
| 5005 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5006 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5007 | |
| 5008 | def removeHost( self, host ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5009 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5010 | Description: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5011 | Remove a host from ONOS by passing the id of the host(s) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5012 | Parameters: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5013 | 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] | 5014 | Returns: |
| 5015 | Returns main.FALSE if an exception is thrown or an error is present |
| 5016 | in the response. Otherwise, returns main.TRUE. |
| 5017 | NOTE: |
| 5018 | 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] | 5019 | ''' |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5020 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5021 | if isinstance( host, str ): |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5022 | host = list( host ) |
| 5023 | |
| 5024 | for h in host: |
| 5025 | time.sleep( 1 ) |
| 5026 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5027 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5028 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5029 | if "Error" in response: |
| 5030 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 5031 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5032 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5033 | except AssertionError: |
| 5034 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5035 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5036 | except TypeError: |
| 5037 | main.log.exception( self.name + ": Object not as expected" ) |
| 5038 | return main.FALSE |
| 5039 | except pexpect.EOF: |
| 5040 | main.log.error( self.name + ": EOF exception found" ) |
| 5041 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5042 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 5043 | except Exception: |
| 5044 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5045 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5046 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5047 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5048 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5049 | Description: |
| 5050 | Bring link down or up in the null-provider. |
| 5051 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5052 | begin - (string) One end of a device or switch. |
| 5053 | end - (string) the other end of the device or switch |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5054 | returns: |
| 5055 | main.TRUE if no exceptions were thrown and no Errors are |
| 5056 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5057 | ''' |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5058 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5059 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 5060 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 5061 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5062 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5063 | if "Error" in response or "Failure" in response: |
| 5064 | main.log.error( response ) |
| 5065 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5066 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 5067 | except AssertionError: |
| 5068 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5069 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5070 | except TypeError: |
| 5071 | main.log.exception( self.name + ": Object not as expected" ) |
| 5072 | return main.FALSE |
| 5073 | except pexpect.EOF: |
| 5074 | main.log.error( self.name + ": EOF exception found" ) |
| 5075 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5076 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5077 | except Exception: |
| 5078 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5079 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 5080 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5081 | def portstate( self, dpid, port, state ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5082 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5083 | Description: |
| 5084 | Changes the state of port in an OF switch by means of the |
| 5085 | PORTSTATUS OF messages. |
| 5086 | params: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5087 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 5088 | port - (string) target port in the device. Ex: '2' |
| 5089 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5090 | returns: |
| 5091 | main.TRUE if no exceptions were thrown and no Errors are |
| 5092 | present in the resoponse. Otherwise, returns main.FALSE |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5093 | ''' |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5094 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5095 | state = state.lower() |
| 5096 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5097 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5098 | response = self.sendline( cmd, showResponse=True ) |
| 5099 | assert response is not None, "Error in sendline" |
| 5100 | assert "Command not found:" not in response, response |
| 5101 | if "Error" in response or "Failure" in response: |
| 5102 | main.log.error( response ) |
| 5103 | return main.FALSE |
| 5104 | return main.TRUE |
| 5105 | except AssertionError: |
| 5106 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5107 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5108 | except TypeError: |
| 5109 | main.log.exception( self.name + ": Object not as expected" ) |
| 5110 | return main.FALSE |
| 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() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [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() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5118 | |
| 5119 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 5120 | """ |
| 5121 | Set the logging level to lvl for a specific app |
| 5122 | returns main.TRUE on success |
| 5123 | returns main.FALSE if Error occurred |
| 5124 | if noExit is True, TestON will not exit, but clean up |
| 5125 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 5126 | Level defaults to INFO |
| 5127 | """ |
| 5128 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5129 | self.handle.sendline( "log:set %s %s" % ( level, app ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5130 | self.handle.expect( "onos>" ) |
| 5131 | |
| 5132 | response = self.handle.before |
| 5133 | if re.search( "Error", response ): |
| 5134 | return main.FALSE |
| 5135 | return main.TRUE |
| 5136 | except pexpect.TIMEOUT: |
| 5137 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5138 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5139 | except pexpect.EOF: |
| 5140 | main.log.error( self.name + ": EOF exception found" ) |
| 5141 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5142 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 5143 | except Exception: |
| 5144 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5145 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5146 | |
| 5147 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 5148 | """ |
| 5149 | Return a dictionary which describes the latest network topology data as a |
| 5150 | graph. |
| 5151 | An example of the dictionary: |
| 5152 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 5153 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 5154 | Each vertex should at least have an 'edges' attribute which describes the |
| 5155 | adjacency information. The value of 'edges' attribute is also represented by |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5156 | 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] | 5157 | list of attributes. |
| 5158 | An example of the edges dictionary: |
| 5159 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 5160 | vertex3: { 'port': ..., 'weight': ... } } |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5161 | If includeHost == True, all hosts (and host-switch links) will be included |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5162 | in topology data. |
| 5163 | """ |
| 5164 | graphDict = {} |
| 5165 | try: |
| 5166 | links = self.links() |
| 5167 | links = json.loads( links ) |
| 5168 | devices = self.devices() |
| 5169 | devices = json.loads( devices ) |
| 5170 | idToDevice = {} |
| 5171 | for device in devices: |
| 5172 | idToDevice[ device[ 'id' ] ] = device |
| 5173 | if includeHost: |
| 5174 | hosts = self.hosts() |
| 5175 | # FIXME: support 'includeHost' argument |
| 5176 | for link in links: |
| 5177 | nodeA = link[ 'src' ][ 'device' ] |
| 5178 | nodeB = link[ 'dst' ][ 'device' ] |
| 5179 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5180 | if nodeA not in graphDict.keys(): |
| 5181 | graphDict[ nodeA ] = { 'edges': {}, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5182 | 'dpid': idToDevice[ nodeA ][ 'id' ][ 3: ], |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5183 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 5184 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 5185 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 5186 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 5187 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 5188 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 5189 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 5190 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5191 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5192 | else: |
| 5193 | # Assert nodeB is not connected to any current links of nodeA |
| 5194 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5195 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 5196 | 'type': link[ 'type' ], |
| 5197 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 5198 | return graphDict |
| 5199 | except ( TypeError, ValueError ): |
| 5200 | main.log.exception( self.name + ": Object not as expected" ) |
| 5201 | return None |
| 5202 | except KeyError: |
| 5203 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5204 | return None |
| 5205 | except AssertionError: |
| 5206 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5207 | return None |
| 5208 | except pexpect.EOF: |
| 5209 | main.log.error( self.name + ": EOF exception found" ) |
| 5210 | main.log.error( self.name + ": " + self.handle.before ) |
| 5211 | return None |
| 5212 | except Exception: |
| 5213 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5214 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5215 | |
| 5216 | def getIntentPerfSummary( self ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5217 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5218 | Send command to check intent-perf summary |
| 5219 | Returns: dictionary for intent-perf summary |
| 5220 | if something wrong, function will return None |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5221 | ''' |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5222 | cmd = "intent-perf -s" |
| 5223 | respDic = {} |
| 5224 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 5225 | assert resp is not None, "Error in sendline" |
| 5226 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5227 | try: |
| 5228 | # Generate the dictionary to return |
| 5229 | for l in resp.split( "\n" ): |
| 5230 | # Delete any white space in line |
| 5231 | temp = re.sub( r'\s+', '', l ) |
| 5232 | temp = temp.split( ":" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5233 | respDic[ temp[ 0 ] ] = temp[ 1 ] |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5234 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5235 | except ( TypeError, ValueError ): |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5236 | main.log.exception( self.name + ": Object not as expected" ) |
| 5237 | return None |
| 5238 | except KeyError: |
| 5239 | main.log.exception( self.name + ": KeyError exception found" ) |
| 5240 | return None |
| 5241 | except AssertionError: |
| 5242 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 5243 | return None |
| 5244 | except pexpect.EOF: |
| 5245 | main.log.error( self.name + ": EOF exception found" ) |
| 5246 | main.log.error( self.name + ": " + self.handle.before ) |
| 5247 | return None |
| 5248 | except Exception: |
| 5249 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5250 | return None |
| 5251 | return respDic |
| 5252 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5253 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5254 | """ |
| 5255 | Searches the latest ONOS log file for the given search term and |
| 5256 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5257 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5258 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5259 | searchTerm: |
| 5260 | The string to grep from the ONOS log. |
| 5261 | startLine: |
| 5262 | The term that decides which line is the start to search the searchTerm in |
| 5263 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5264 | logNum: |
| 5265 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5266 | information.Because of this, search mutiply logs is necessary to capture |
| 5267 | the right result. logNum is the number of karaf logs that we need to search |
| 5268 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5269 | mode: |
| 5270 | all: return all the strings that contain the search term |
| 5271 | last: return the last string that contains the search term |
| 5272 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5273 | num: return the number of times that the searchTerm appears in the log |
| 5274 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5275 | """ |
| 5276 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5277 | assert isinstance( searchTerm, str ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5278 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5279 | logPath = '/opt/onos/log/karaf.log.' |
| 5280 | logPaths = '/opt/onos/log/karaf.log' |
| 5281 | for i in range( 1, logNum ): |
| 5282 | logPaths = logPath + str( i ) + " " + logPaths |
| 5283 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5284 | if startLine: |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5285 | # 100000000 is just a extreme large number to make sure this function can |
| 5286 | # grep all the lines after startLine |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5287 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5288 | if mode == 'all': |
| 5289 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5290 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5291 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5292 | elif mode == 'first': |
| 5293 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5294 | elif mode == 'num': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5295 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
You Wang | 118ba58 | 2017-01-02 17:14:43 -0800 | [diff] [blame] | 5296 | num = self.sendline( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5297 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5298 | elif mode == 'total': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5299 | totalLines = self.sendline( "cat /opt/onos/log/karaf.log | wc -l" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5300 | return int( totalLines ) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5301 | else: |
| 5302 | main.log.error( self.name + " unsupported mode" ) |
| 5303 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5304 | before = self.sendline( cmd ) |
| 5305 | before = before.splitlines() |
| 5306 | # make sure the returned list only contains the search term |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5307 | returnLines = [ line for line in before if searchTerm in line ] |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5308 | return returnLines |
| 5309 | except AssertionError: |
| 5310 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5311 | return None |
| 5312 | except pexpect.EOF: |
| 5313 | main.log.error( self.name + ": EOF exception found" ) |
| 5314 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5315 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5316 | except pexpect.TIMEOUT: |
| 5317 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5318 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5319 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5320 | except Exception: |
| 5321 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5322 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5323 | |
| 5324 | def vplsShow( self, jsonFormat=True ): |
| 5325 | """ |
| 5326 | Description: Returns result of onos:vpls show, which should list the |
| 5327 | configured VPLS networks and the assigned interfaces. |
| 5328 | Optional: |
| 5329 | * jsonFormat: enable json formatting of output |
| 5330 | Returns: |
| 5331 | The output of the command or None on error. |
| 5332 | """ |
| 5333 | try: |
| 5334 | cmdStr = "vpls show" |
| 5335 | if jsonFormat: |
| 5336 | raise NotImplementedError |
| 5337 | cmdStr += " -j" |
| 5338 | handle = self.sendline( cmdStr ) |
| 5339 | assert handle is not None, "Error in sendline" |
| 5340 | assert "Command not found:" not in handle, handle |
| 5341 | return handle |
| 5342 | except AssertionError: |
| 5343 | main.log.exception( "" ) |
| 5344 | return None |
| 5345 | except TypeError: |
| 5346 | main.log.exception( self.name + ": Object not as expected" ) |
| 5347 | return None |
| 5348 | except pexpect.EOF: |
| 5349 | main.log.error( self.name + ": EOF exception found" ) |
| 5350 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5351 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5352 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5353 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5354 | return None |
| 5355 | except Exception: |
| 5356 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5357 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5358 | |
| 5359 | def parseVplsShow( self ): |
| 5360 | """ |
| 5361 | Parse the cli output of 'vpls show' into json output. This is required |
| 5362 | as there is currently no json output available. |
| 5363 | """ |
| 5364 | try: |
| 5365 | output = [] |
| 5366 | raw = self.vplsShow( jsonFormat=False ) |
| 5367 | namePat = "VPLS name: (?P<name>\w+)" |
| 5368 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5369 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5370 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5371 | mIter = re.finditer( pattern, raw ) |
| 5372 | for match in mIter: |
| 5373 | item = {} |
| 5374 | item[ 'name' ] = match.group( 'name' ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5375 | ifaces = match.group( 'interfaces' ).split( ', ' ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5376 | if ifaces == [ "" ]: |
| 5377 | ifaces = [] |
| 5378 | item[ 'interfaces' ] = ifaces |
| 5379 | encap = match.group( 'encap' ) |
| 5380 | if encap != 'NONE': |
| 5381 | item[ 'encapsulation' ] = encap.lower() |
| 5382 | output.append( item ) |
| 5383 | return output |
| 5384 | except Exception: |
| 5385 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5386 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5387 | |
| 5388 | def vplsList( self, jsonFormat=True ): |
| 5389 | """ |
| 5390 | Description: Returns result of onos:vpls list, which should list the |
| 5391 | configured VPLS networks. |
| 5392 | Optional: |
| 5393 | * jsonFormat: enable json formatting of output |
| 5394 | """ |
| 5395 | try: |
| 5396 | cmdStr = "vpls list" |
| 5397 | if jsonFormat: |
| 5398 | raise NotImplementedError |
| 5399 | cmdStr += " -j" |
| 5400 | handle = self.sendline( cmdStr ) |
| 5401 | assert handle is not None, "Error in sendline" |
| 5402 | assert "Command not found:" not in handle, handle |
| 5403 | return handle |
| 5404 | except AssertionError: |
| 5405 | main.log.exception( "" ) |
| 5406 | return None |
| 5407 | except TypeError: |
| 5408 | main.log.exception( self.name + ": Object not as expected" ) |
| 5409 | return None |
| 5410 | except pexpect.EOF: |
| 5411 | main.log.error( self.name + ": EOF exception found" ) |
| 5412 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5413 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5414 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5415 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5416 | return None |
| 5417 | except Exception: |
| 5418 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5419 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5420 | |
| 5421 | def vplsCreate( self, network ): |
| 5422 | """ |
| 5423 | CLI command to create a new VPLS network. |
| 5424 | Required arguments: |
| 5425 | network - String name of the network to create. |
| 5426 | returns: |
| 5427 | main.TRUE on success and main.FALSE on failure |
| 5428 | """ |
| 5429 | try: |
| 5430 | network = str( network ) |
| 5431 | cmdStr = "vpls create " |
| 5432 | cmdStr += network |
| 5433 | output = self.sendline( cmdStr ) |
| 5434 | assert output is not None, "Error in sendline" |
| 5435 | assert "Command not found:" not in output, output |
| 5436 | assert "Error executing command" not in output, output |
| 5437 | assert "VPLS already exists:" not in output, output |
| 5438 | return main.TRUE |
| 5439 | except AssertionError: |
| 5440 | main.log.exception( "" ) |
| 5441 | return main.FALSE |
| 5442 | except TypeError: |
| 5443 | main.log.exception( self.name + ": Object not as expected" ) |
| 5444 | return main.FALSE |
| 5445 | except pexpect.EOF: |
| 5446 | main.log.error( self.name + ": EOF exception found" ) |
| 5447 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5448 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5449 | except Exception: |
| 5450 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5451 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5452 | |
| 5453 | def vplsDelete( self, network ): |
| 5454 | """ |
| 5455 | CLI command to delete a VPLS network. |
| 5456 | Required arguments: |
| 5457 | network - Name of the network to delete. |
| 5458 | returns: |
| 5459 | main.TRUE on success and main.FALSE on failure |
| 5460 | """ |
| 5461 | try: |
| 5462 | network = str( network ) |
| 5463 | cmdStr = "vpls delete " |
| 5464 | cmdStr += network |
| 5465 | output = self.sendline( cmdStr ) |
| 5466 | assert output is not None, "Error in sendline" |
| 5467 | assert "Command not found:" not in output, output |
| 5468 | assert "Error executing command" not in output, output |
| 5469 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5470 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5471 | return main.TRUE |
| 5472 | except AssertionError: |
| 5473 | main.log.exception( "" ) |
| 5474 | return main.FALSE |
| 5475 | except TypeError: |
| 5476 | main.log.exception( self.name + ": Object not as expected" ) |
| 5477 | return main.FALSE |
| 5478 | except pexpect.EOF: |
| 5479 | main.log.error( self.name + ": EOF exception found" ) |
| 5480 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5481 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5482 | except Exception: |
| 5483 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5484 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5485 | |
| 5486 | def vplsAddIface( self, network, iface ): |
| 5487 | """ |
| 5488 | CLI command to add an interface to a VPLS network. |
| 5489 | Required arguments: |
| 5490 | network - Name of the network to add the interface to. |
| 5491 | iface - The ONOS name for an interface. |
| 5492 | returns: |
| 5493 | main.TRUE on success and main.FALSE on failure |
| 5494 | """ |
| 5495 | try: |
| 5496 | network = str( network ) |
| 5497 | iface = str( iface ) |
| 5498 | cmdStr = "vpls add-if " |
| 5499 | cmdStr += network + " " + iface |
| 5500 | output = self.sendline( cmdStr ) |
| 5501 | assert output is not None, "Error in sendline" |
| 5502 | assert "Command not found:" not in output, output |
| 5503 | assert "Error executing command" not in output, output |
| 5504 | assert "already associated to network" not in output, output |
| 5505 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5506 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5507 | return main.TRUE |
| 5508 | except AssertionError: |
| 5509 | main.log.exception( "" ) |
| 5510 | return main.FALSE |
| 5511 | except TypeError: |
| 5512 | main.log.exception( self.name + ": Object not as expected" ) |
| 5513 | return main.FALSE |
| 5514 | except pexpect.EOF: |
| 5515 | main.log.error( self.name + ": EOF exception found" ) |
| 5516 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5517 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5518 | except Exception: |
| 5519 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5520 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5521 | |
| 5522 | def vplsRemIface( self, network, iface ): |
| 5523 | """ |
| 5524 | CLI command to remove an interface from a VPLS network. |
| 5525 | Required arguments: |
| 5526 | network - Name of the network to remove the interface from. |
| 5527 | iface - Name of the interface to remove. |
| 5528 | returns: |
| 5529 | main.TRUE on success and main.FALSE on failure |
| 5530 | """ |
| 5531 | try: |
| 5532 | iface = str( iface ) |
| 5533 | cmdStr = "vpls rem-if " |
| 5534 | cmdStr += network + " " + iface |
| 5535 | output = self.sendline( cmdStr ) |
| 5536 | assert output is not None, "Error in sendline" |
| 5537 | assert "Command not found:" not in output, output |
| 5538 | assert "Error executing command" not in output, output |
| 5539 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5540 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5541 | return main.TRUE |
| 5542 | except AssertionError: |
| 5543 | main.log.exception( "" ) |
| 5544 | return main.FALSE |
| 5545 | except TypeError: |
| 5546 | main.log.exception( self.name + ": Object not as expected" ) |
| 5547 | return main.FALSE |
| 5548 | except pexpect.EOF: |
| 5549 | main.log.error( self.name + ": EOF exception found" ) |
| 5550 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5551 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5552 | except Exception: |
| 5553 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5554 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5555 | |
| 5556 | def vplsClean( self ): |
| 5557 | """ |
| 5558 | Description: Clears the VPLS app configuration. |
| 5559 | Returns: main.TRUE on success and main.FALSE on failure |
| 5560 | """ |
| 5561 | try: |
| 5562 | cmdStr = "vpls clean" |
| 5563 | handle = self.sendline( cmdStr ) |
| 5564 | assert handle is not None, "Error in sendline" |
| 5565 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5566 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5567 | return handle |
| 5568 | except AssertionError: |
| 5569 | main.log.exception( "" ) |
| 5570 | return main.FALSE |
| 5571 | except TypeError: |
| 5572 | main.log.exception( self.name + ": Object not as expected" ) |
| 5573 | return main.FALSE |
| 5574 | except pexpect.EOF: |
| 5575 | main.log.error( self.name + ": EOF exception found" ) |
| 5576 | main.log.error( self.name + ": " + self.handle.before ) |
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 | except Exception: |
| 5579 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5580 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5581 | |
| 5582 | def vplsSetEncap( self, network, encapType ): |
| 5583 | """ |
| 5584 | CLI command to add an interface to a VPLS network. |
| 5585 | Required arguments: |
| 5586 | network - Name of the network to create. |
| 5587 | encapType - Type of encapsulation. |
| 5588 | returns: |
| 5589 | main.TRUE on success and main.FALSE on failure |
| 5590 | """ |
| 5591 | try: |
| 5592 | network = str( network ) |
| 5593 | encapType = str( encapType ).upper() |
| 5594 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5595 | cmdStr = "vpls set-encap " |
| 5596 | cmdStr += network + " " + encapType |
| 5597 | output = self.sendline( cmdStr ) |
| 5598 | assert output is not None, "Error in sendline" |
| 5599 | assert "Command not found:" not in output, output |
| 5600 | assert "Error executing command" not in output, output |
| 5601 | assert "already associated to network" not in output, output |
| 5602 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5603 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5604 | return main.TRUE |
| 5605 | except AssertionError: |
| 5606 | main.log.exception( "" ) |
| 5607 | return main.FALSE |
| 5608 | except TypeError: |
| 5609 | main.log.exception( self.name + ": Object not as expected" ) |
| 5610 | return main.FALSE |
| 5611 | except pexpect.EOF: |
| 5612 | main.log.error( self.name + ": EOF exception found" ) |
| 5613 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5614 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5615 | except Exception: |
| 5616 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5617 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5618 | |
| 5619 | def interfaces( self, jsonFormat=True ): |
| 5620 | """ |
| 5621 | Description: Returns result of interfaces command. |
| 5622 | Optional: |
| 5623 | * jsonFormat: enable json formatting of output |
| 5624 | Returns: |
| 5625 | The output of the command or None on error. |
| 5626 | """ |
| 5627 | try: |
| 5628 | cmdStr = "interfaces" |
| 5629 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5630 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5631 | cmdStr += " -j" |
| 5632 | handle = self.sendline( cmdStr ) |
| 5633 | assert handle is not None, "Error in sendline" |
| 5634 | assert "Command not found:" not in handle, handle |
| 5635 | return handle |
| 5636 | except AssertionError: |
| 5637 | main.log.exception( "" ) |
| 5638 | return None |
| 5639 | except TypeError: |
| 5640 | main.log.exception( self.name + ": Object not as expected" ) |
| 5641 | return None |
| 5642 | except pexpect.EOF: |
| 5643 | main.log.error( self.name + ": EOF exception found" ) |
| 5644 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5645 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5646 | except NotImplementedError: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5647 | main.log.exception( self.name + ": Json output not supported" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5648 | return None |
| 5649 | except Exception: |
| 5650 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5651 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5652 | |
| 5653 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5654 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5655 | Get the timestamp of searchTerm from karaf log. |
| 5656 | |
| 5657 | Arguments: |
| 5658 | splitTerm_before and splitTerm_after: |
| 5659 | |
| 5660 | The terms that split the string that contains the timeStamp of |
| 5661 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5662 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5663 | and the splitTerm_after is "x" |
| 5664 | |
| 5665 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5666 | Please look at the "logsearch" Function in onosclidriver.py |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 5667 | ''' |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5668 | if logNum < 0: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5669 | main.log.error( "Get wrong log number ") |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5670 | return main.ERROR |
| 5671 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5672 | if len( lines ) == 0: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5673 | main.log.warn( "Captured timestamp string is empty" ) |
| 5674 | return main.ERROR |
| 5675 | lines = lines[ 0 ] |
| 5676 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5677 | assert isinstance( lines, str ) |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5678 | # get the target value |
| 5679 | line = lines.split( splitTerm_before ) |
| 5680 | key = line[ 1 ].split( splitTerm_after ) |
| 5681 | return int( key[ 0 ] ) |
| 5682 | except IndexError: |
| 5683 | main.log.warn( "Index Error!" ) |
| 5684 | return main.ERROR |
| 5685 | except AssertionError: |
| 5686 | main.log.warn( "Search Term Not Found " ) |
| 5687 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5688 | |
| 5689 | def workQueueAdd( self, queueName, value ): |
| 5690 | """ |
| 5691 | CLI command to add a string to the specified Work Queue. |
| 5692 | This function uses the distributed primitives test app, which |
| 5693 | gives some cli access to distributed primitives for testing |
| 5694 | purposes only. |
| 5695 | |
| 5696 | Required arguments: |
| 5697 | queueName - The name of the queue to add to |
| 5698 | value - The value to add to the queue |
| 5699 | returns: |
| 5700 | main.TRUE on success, main.FALSE on failure and |
| 5701 | main.ERROR on error. |
| 5702 | """ |
| 5703 | try: |
| 5704 | queueName = str( queueName ) |
| 5705 | value = str( value ) |
| 5706 | prefix = "work-queue-test" |
| 5707 | operation = "add" |
| 5708 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5709 | output = self.distPrimitivesSend( cmdStr ) |
| 5710 | if "Invalid operation name" in output: |
| 5711 | main.log.warn( output ) |
| 5712 | return main.ERROR |
| 5713 | elif "Done" in output: |
| 5714 | return main.TRUE |
| 5715 | except TypeError: |
| 5716 | main.log.exception( self.name + ": Object not as expected" ) |
| 5717 | return main.ERROR |
| 5718 | except Exception: |
| 5719 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5720 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5721 | |
| 5722 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5723 | """ |
| 5724 | CLI command to add two strings to the specified Work Queue. |
| 5725 | This function uses the distributed primitives test app, which |
| 5726 | gives some cli access to distributed primitives for testing |
| 5727 | purposes only. |
| 5728 | |
| 5729 | Required arguments: |
| 5730 | queueName - The name of the queue to add to |
| 5731 | value1 - The first value to add to the queue |
| 5732 | value2 - The second value to add to the queue |
| 5733 | returns: |
| 5734 | main.TRUE on success, main.FALSE on failure and |
| 5735 | main.ERROR on error. |
| 5736 | """ |
| 5737 | try: |
| 5738 | queueName = str( queueName ) |
| 5739 | value1 = str( value1 ) |
| 5740 | value2 = str( value2 ) |
| 5741 | prefix = "work-queue-test" |
| 5742 | operation = "addMultiple" |
| 5743 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 5744 | output = self.distPrimitivesSend( cmdStr ) |
| 5745 | if "Invalid operation name" in output: |
| 5746 | main.log.warn( output ) |
| 5747 | return main.ERROR |
| 5748 | elif "Done" in output: |
| 5749 | return main.TRUE |
| 5750 | except TypeError: |
| 5751 | main.log.exception( self.name + ": Object not as expected" ) |
| 5752 | return main.ERROR |
| 5753 | except Exception: |
| 5754 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5755 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5756 | |
| 5757 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 5758 | """ |
| 5759 | CLI command to take a value from the specified Work Queue and compelte it. |
| 5760 | This function uses the distributed primitives test app, which |
| 5761 | gives some cli access to distributed primitives for testing |
| 5762 | purposes only. |
| 5763 | |
| 5764 | Required arguments: |
| 5765 | queueName - The name of the queue to add to |
| 5766 | number - The number of items to take and complete |
| 5767 | returns: |
| 5768 | main.TRUE on success, main.FALSE on failure and |
| 5769 | main.ERROR on error. |
| 5770 | """ |
| 5771 | try: |
| 5772 | queueName = str( queueName ) |
| 5773 | number = str( int( number ) ) |
| 5774 | prefix = "work-queue-test" |
| 5775 | operation = "takeAndComplete" |
| 5776 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 5777 | output = self.distPrimitivesSend( cmdStr ) |
| 5778 | if "Invalid operation name" in output: |
| 5779 | main.log.warn( output ) |
| 5780 | return main.ERROR |
| 5781 | elif "Done" in output: |
| 5782 | return main.TRUE |
| 5783 | except TypeError: |
| 5784 | main.log.exception( self.name + ": Object not as expected" ) |
| 5785 | return main.ERROR |
| 5786 | except Exception: |
| 5787 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5788 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5789 | |
| 5790 | def workQueueDestroy( self, queueName ): |
| 5791 | """ |
| 5792 | CLI command to destroy the specified Work Queue. |
| 5793 | This function uses the distributed primitives test app, which |
| 5794 | gives some cli access to distributed primitives for testing |
| 5795 | purposes only. |
| 5796 | |
| 5797 | Required arguments: |
| 5798 | queueName - The name of the queue to add to |
| 5799 | returns: |
| 5800 | main.TRUE on success, main.FALSE on failure and |
| 5801 | main.ERROR on error. |
| 5802 | """ |
| 5803 | try: |
| 5804 | queueName = str( queueName ) |
| 5805 | prefix = "work-queue-test" |
| 5806 | operation = "destroy" |
| 5807 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5808 | output = self.distPrimitivesSend( cmdStr ) |
| 5809 | if "Invalid operation name" in output: |
| 5810 | main.log.warn( output ) |
| 5811 | return main.ERROR |
| 5812 | return main.TRUE |
| 5813 | except TypeError: |
| 5814 | main.log.exception( self.name + ": Object not as expected" ) |
| 5815 | return main.ERROR |
| 5816 | except Exception: |
| 5817 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5818 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5819 | |
| 5820 | def workQueueTotalPending( self, queueName ): |
| 5821 | """ |
| 5822 | CLI command to get the Total Pending items of the specified Work Queue. |
| 5823 | This function uses the distributed primitives test app, which |
| 5824 | gives some cli access to distributed primitives for testing |
| 5825 | purposes only. |
| 5826 | |
| 5827 | Required arguments: |
| 5828 | queueName - The name of the queue to add to |
| 5829 | returns: |
| 5830 | The number of Pending items in the specified work queue or |
| 5831 | None on error |
| 5832 | """ |
| 5833 | try: |
| 5834 | queueName = str( queueName ) |
| 5835 | prefix = "work-queue-test" |
| 5836 | operation = "totalPending" |
| 5837 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5838 | output = self.distPrimitivesSend( cmdStr ) |
| 5839 | pattern = r'\d+' |
| 5840 | if "Invalid operation name" in output: |
| 5841 | main.log.warn( output ) |
| 5842 | return None |
| 5843 | else: |
| 5844 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5845 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5846 | except ( AttributeError, TypeError ): |
| 5847 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5848 | return None |
| 5849 | except Exception: |
| 5850 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5851 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5852 | |
| 5853 | def workQueueTotalCompleted( self, queueName ): |
| 5854 | """ |
| 5855 | CLI command to get the Total Completed items of the specified Work Queue. |
| 5856 | This function uses the distributed primitives test app, which |
| 5857 | gives some cli access to distributed primitives for testing |
| 5858 | purposes only. |
| 5859 | |
| 5860 | Required arguments: |
| 5861 | queueName - The name of the queue to add to |
| 5862 | returns: |
| 5863 | The number of complete items in the specified work queue or |
| 5864 | None on error |
| 5865 | """ |
| 5866 | try: |
| 5867 | queueName = str( queueName ) |
| 5868 | prefix = "work-queue-test" |
| 5869 | operation = "totalCompleted" |
| 5870 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5871 | output = self.distPrimitivesSend( cmdStr ) |
| 5872 | pattern = r'\d+' |
| 5873 | if "Invalid operation name" in output: |
| 5874 | main.log.warn( output ) |
| 5875 | return None |
| 5876 | else: |
| 5877 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5878 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5879 | except ( AttributeError, TypeError ): |
| 5880 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5881 | return None |
| 5882 | except Exception: |
| 5883 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5884 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5885 | |
| 5886 | def workQueueTotalInProgress( self, queueName ): |
| 5887 | """ |
| 5888 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 5889 | This function uses the distributed primitives test app, which |
| 5890 | gives some cli access to distributed primitives for testing |
| 5891 | purposes only. |
| 5892 | |
| 5893 | Required arguments: |
| 5894 | queueName - The name of the queue to add to |
| 5895 | returns: |
| 5896 | The number of In Progress items in the specified work queue or |
| 5897 | None on error |
| 5898 | """ |
| 5899 | try: |
| 5900 | queueName = str( queueName ) |
| 5901 | prefix = "work-queue-test" |
| 5902 | operation = "totalInProgress" |
| 5903 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5904 | output = self.distPrimitivesSend( cmdStr ) |
| 5905 | pattern = r'\d+' |
| 5906 | if "Invalid operation name" in output: |
| 5907 | main.log.warn( output ) |
| 5908 | return None |
| 5909 | else: |
| 5910 | match = re.search( pattern, output ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 5911 | return match.group( 0 ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5912 | except ( AttributeError, TypeError ): |
| 5913 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5914 | return None |
| 5915 | except Exception: |
| 5916 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5917 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 5918 | |
| 5919 | def events( self, args='-a' ): |
| 5920 | """ |
| 5921 | Description: Returns events -a command output |
| 5922 | Optional: |
| 5923 | add other arguments |
| 5924 | """ |
| 5925 | try: |
| 5926 | cmdStr = "events" |
| 5927 | if args: |
| 5928 | cmdStr += " " + args |
| 5929 | handle = self.sendline( cmdStr ) |
| 5930 | assert handle is not None, "Error in sendline" |
| 5931 | assert "Command not found:" not in handle, handle |
| 5932 | return handle |
| 5933 | except AssertionError: |
| 5934 | main.log.exception( "" ) |
| 5935 | return None |
| 5936 | except TypeError: |
| 5937 | main.log.exception( self.name + ": Object not as expected" ) |
| 5938 | return None |
| 5939 | except pexpect.EOF: |
| 5940 | main.log.error( self.name + ": EOF exception found" ) |
| 5941 | main.log.error( self.name + ": " + self.handle.before ) |
| 5942 | main.cleanAndExit() |
| 5943 | except Exception: |
| 5944 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5945 | main.cleanAndExit() |
| 5946 | |
| 5947 | def getMaster( self, deviceID ): |
| 5948 | """ |
| 5949 | Description: Obtains current master using "roles" command for a specific deviceID |
| 5950 | """ |
| 5951 | try: |
| 5952 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 5953 | except AssertionError: |
| 5954 | main.log.exception( "" ) |
| 5955 | return None |
| 5956 | except TypeError: |
| 5957 | main.log.exception( self.name + ": Object not as expected" ) |
| 5958 | return None |
| 5959 | except pexpect.EOF: |
| 5960 | main.log.error( self.name + ": EOF exception found" ) |
| 5961 | main.log.error( self.name + ": " + self.handle.before ) |
| 5962 | main.cleanAndExit() |
| 5963 | except Exception: |
| 5964 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | e6fe3c4 | 2017-10-18 16:28:40 -0700 | [diff] [blame] | 5965 | main.cleanAndExit() |
Jon Hall | a478b85 | 2017-12-04 15:00:15 -0800 | [diff] [blame] | 5966 | |
| 5967 | def issu( self ): |
| 5968 | """ |
| 5969 | Short summary of In-Service Software Upgrade status |
| 5970 | |
| 5971 | Returns the output of the cli command or None on Error |
| 5972 | """ |
| 5973 | try: |
| 5974 | cmdStr = "issu" |
| 5975 | handle = self.sendline( cmdStr ) |
| 5976 | assert handle is not None, "Error in sendline" |
| 5977 | assert "Command not found:" not in handle, handle |
| 5978 | assert "Unsupported command:" not in handle, handle |
| 5979 | return handle |
| 5980 | except AssertionError: |
| 5981 | main.log.exception( "" ) |
| 5982 | return None |
| 5983 | except TypeError: |
| 5984 | main.log.exception( self.name + ": Object not as expected" ) |
| 5985 | return None |
| 5986 | except pexpect.EOF: |
| 5987 | main.log.error( self.name + ": EOF exception found" ) |
| 5988 | main.log.error( self.name + ": " + self.handle.before ) |
| 5989 | main.cleanAndExit() |
| 5990 | except Exception: |
| 5991 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5992 | main.cleanAndExit() |
| 5993 | |
| 5994 | def issuInit( self ): |
| 5995 | """ |
| 5996 | Initiates an In-Service Software Upgrade |
| 5997 | |
| 5998 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 5999 | """ |
| 6000 | try: |
| 6001 | cmdStr = "issu init" |
| 6002 | handle = self.sendline( cmdStr ) |
| 6003 | assert handle is not None, "Error in sendline" |
| 6004 | assert "Command not found:" not in handle, handle |
| 6005 | assert "Unsupported command:" not in handle, handle |
| 6006 | if "Initialized" in handle: |
| 6007 | return main.TRUE |
| 6008 | else: |
| 6009 | return main.FALSE |
| 6010 | except AssertionError: |
| 6011 | main.log.exception( "" ) |
| 6012 | return main.ERROR |
| 6013 | except TypeError: |
| 6014 | main.log.exception( self.name + ": Object not as expected" ) |
| 6015 | return main.ERROR |
| 6016 | except pexpect.EOF: |
| 6017 | main.log.error( self.name + ": EOF exception found" ) |
| 6018 | main.log.error( self.name + ": " + self.handle.before ) |
| 6019 | main.cleanAndExit() |
| 6020 | except Exception: |
| 6021 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6022 | main.cleanAndExit() |
| 6023 | |
| 6024 | def issuUpgrade( self ): |
| 6025 | """ |
| 6026 | Transitions stores to upgraded nodes |
| 6027 | |
| 6028 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6029 | """ |
| 6030 | try: |
| 6031 | cmdStr = "issu upgrade" |
| 6032 | handle = self.sendline( cmdStr ) |
| 6033 | assert handle is not None, "Error in sendline" |
| 6034 | assert "Command not found:" not in handle, handle |
| 6035 | assert "Unsupported command:" not in handle, handle |
| 6036 | if "Upgraded" in handle: |
| 6037 | return main.TRUE |
| 6038 | else: |
| 6039 | return main.FALSE |
| 6040 | except AssertionError: |
| 6041 | main.log.exception( "" ) |
| 6042 | return main.ERROR |
| 6043 | except TypeError: |
| 6044 | main.log.exception( self.name + ": Object not as expected" ) |
| 6045 | return main.ERROR |
| 6046 | except pexpect.EOF: |
| 6047 | main.log.error( self.name + ": EOF exception found" ) |
| 6048 | main.log.error( self.name + ": " + self.handle.before ) |
| 6049 | main.cleanAndExit() |
| 6050 | except Exception: |
| 6051 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6052 | main.cleanAndExit() |
| 6053 | |
| 6054 | def issuCommit( self ): |
| 6055 | """ |
| 6056 | Finalizes an In-Service Software Upgrade |
| 6057 | |
| 6058 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6059 | """ |
| 6060 | try: |
| 6061 | cmdStr = "issu commit" |
| 6062 | handle = self.sendline( cmdStr ) |
| 6063 | assert handle is not None, "Error in sendline" |
| 6064 | assert "Command not found:" not in handle, handle |
| 6065 | assert "Unsupported command:" not in handle, handle |
| 6066 | # TODO: Check the version returned by this command |
| 6067 | if "Committed version" in handle: |
| 6068 | return main.TRUE |
| 6069 | else: |
| 6070 | return main.FALSE |
| 6071 | except AssertionError: |
| 6072 | main.log.exception( "" ) |
| 6073 | return main.ERROR |
| 6074 | except TypeError: |
| 6075 | main.log.exception( self.name + ": Object not as expected" ) |
| 6076 | return main.ERROR |
| 6077 | except pexpect.EOF: |
| 6078 | main.log.error( self.name + ": EOF exception found" ) |
| 6079 | main.log.error( self.name + ": " + self.handle.before ) |
| 6080 | main.cleanAndExit() |
| 6081 | except Exception: |
| 6082 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6083 | main.cleanAndExit() |
| 6084 | |
| 6085 | def issuRollback( self ): |
| 6086 | """ |
| 6087 | Rolls back an In-Service Software Upgrade |
| 6088 | |
| 6089 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6090 | """ |
| 6091 | try: |
| 6092 | cmdStr = "issu rollback" |
| 6093 | handle = self.sendline( cmdStr ) |
| 6094 | assert handle is not None, "Error in sendline" |
| 6095 | assert "Command not found:" not in handle, handle |
| 6096 | assert "Unsupported command:" not in handle, handle |
| 6097 | # TODO: Check the version returned by this command |
| 6098 | if "Rolled back to version" in handle: |
| 6099 | return main.TRUE |
| 6100 | else: |
| 6101 | return main.FALSE |
| 6102 | except AssertionError: |
| 6103 | main.log.exception( "" ) |
| 6104 | return main.ERROR |
| 6105 | except TypeError: |
| 6106 | main.log.exception( self.name + ": Object not as expected" ) |
| 6107 | return main.ERROR |
| 6108 | except pexpect.EOF: |
| 6109 | main.log.error( self.name + ": EOF exception found" ) |
| 6110 | main.log.error( self.name + ": " + self.handle.before ) |
| 6111 | main.cleanAndExit() |
| 6112 | except Exception: |
| 6113 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6114 | main.cleanAndExit() |
| 6115 | |
| 6116 | def issuReset( self ): |
| 6117 | """ |
| 6118 | Resets the In-Service Software Upgrade status after a rollback |
| 6119 | |
| 6120 | Returns main.TRUE on success, main.ERROR on error, else main.FALSE |
| 6121 | """ |
| 6122 | try: |
| 6123 | cmdStr = "issu reset" |
| 6124 | handle = self.sendline( cmdStr ) |
| 6125 | assert handle is not None, "Error in sendline" |
| 6126 | assert "Command not found:" not in handle, handle |
| 6127 | assert "Unsupported command:" not in handle, handle |
| 6128 | # TODO: Check the version returned by this command |
| 6129 | if "Reset version" in handle: |
| 6130 | return main.TRUE |
| 6131 | else: |
| 6132 | return main.FALSE |
| 6133 | except AssertionError: |
| 6134 | main.log.exception( "" ) |
| 6135 | return main.ERROR |
| 6136 | except TypeError: |
| 6137 | main.log.exception( self.name + ": Object not as expected" ) |
| 6138 | return main.ERROR |
| 6139 | except pexpect.EOF: |
| 6140 | main.log.error( self.name + ": EOF exception found" ) |
| 6141 | main.log.error( self.name + ": " + self.handle.before ) |
| 6142 | main.cleanAndExit() |
| 6143 | except Exception: |
| 6144 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6145 | main.cleanAndExit() |
| 6146 | |
| 6147 | def issuStatus( self ): |
| 6148 | """ |
| 6149 | Status of an In-Service Software Upgrade |
| 6150 | |
| 6151 | Returns the output of the cli command or None on Error |
| 6152 | """ |
| 6153 | try: |
| 6154 | cmdStr = "issu status" |
| 6155 | handle = self.sendline( cmdStr ) |
| 6156 | assert handle is not None, "Error in sendline" |
| 6157 | assert "Command not found:" not in handle, handle |
| 6158 | assert "Unsupported command:" not in handle, handle |
| 6159 | return handle |
| 6160 | except AssertionError: |
| 6161 | main.log.exception( "" ) |
| 6162 | return None |
| 6163 | except TypeError: |
| 6164 | main.log.exception( self.name + ": Object not as expected" ) |
| 6165 | return None |
| 6166 | except pexpect.EOF: |
| 6167 | main.log.error( self.name + ": EOF exception found" ) |
| 6168 | main.log.error( self.name + ": " + self.handle.before ) |
| 6169 | main.cleanAndExit() |
| 6170 | except Exception: |
| 6171 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6172 | main.cleanAndExit() |
| 6173 | |
| 6174 | def issuVersion( self ): |
| 6175 | """ |
| 6176 | Get the version of an In-Service Software Upgrade |
| 6177 | |
| 6178 | Returns the output of the cli command or None on Error |
| 6179 | """ |
| 6180 | try: |
| 6181 | cmdStr = "issu version" |
| 6182 | handle = self.sendline( cmdStr ) |
| 6183 | assert handle is not None, "Error in sendline" |
| 6184 | assert "Command not found:" not in handle, handle |
| 6185 | assert "Unsupported command:" not in handle, handle |
| 6186 | return handle |
| 6187 | except AssertionError: |
| 6188 | main.log.exception( "" ) |
| 6189 | return None |
| 6190 | except TypeError: |
| 6191 | main.log.exception( self.name + ": Object not as expected" ) |
| 6192 | return None |
| 6193 | except pexpect.EOF: |
| 6194 | main.log.error( self.name + ": EOF exception found" ) |
| 6195 | main.log.error( self.name + ": " + self.handle.before ) |
| 6196 | main.cleanAndExit() |
| 6197 | except Exception: |
| 6198 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6199 | main.cleanAndExit() |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6200 | |
| 6201 | def mcastJoin( self, sIP, groupIP, sPort, dPorts ): |
| 6202 | """ |
| 6203 | Create a multicast route by calling 'mcast-join' command |
| 6204 | sIP: source IP of the multicast route |
| 6205 | groupIP: group IP of the multicast route |
| 6206 | sPort: source port (e.g. of:0000000000000001/3 ) of the multicast route |
| 6207 | dPorts: a list of destination ports of the multicast route |
| 6208 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6209 | """ |
| 6210 | try: |
| 6211 | cmdStr = "mcast-join" |
| 6212 | cmdStr += " " + str( sIP ) |
| 6213 | cmdStr += " " + str( groupIP ) |
| 6214 | cmdStr += " " + str( sPort ) |
| 6215 | assert isinstance( dPorts, list ) |
| 6216 | for dPort in dPorts: |
| 6217 | cmdStr += " " + str( dPort ) |
| 6218 | handle = self.sendline( cmdStr ) |
| 6219 | assert handle is not None, "Error in sendline" |
| 6220 | assert "Command not found:" not in handle, handle |
| 6221 | assert "Unsupported command:" not in handle, handle |
| 6222 | assert "Error executing command" not in handle, handle |
| 6223 | if "Added the mcast route" in handle: |
| 6224 | return main.TRUE |
| 6225 | else: |
| 6226 | return main.FALSE |
| 6227 | except AssertionError: |
| 6228 | main.log.exception( "" ) |
| 6229 | return None |
| 6230 | except TypeError: |
| 6231 | main.log.exception( self.name + ": Object not as expected" ) |
| 6232 | return None |
| 6233 | except pexpect.EOF: |
| 6234 | main.log.error( self.name + ": EOF exception found" ) |
| 6235 | main.log.error( self.name + ": " + self.handle.before ) |
| 6236 | main.cleanAndExit() |
| 6237 | except Exception: |
| 6238 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6239 | main.cleanAndExit() |
| 6240 | |
| 6241 | def mcastDelete( self, sIP, groupIP, dPorts ): |
| 6242 | """ |
| 6243 | Delete a multicast route by calling 'mcast-delete' command |
| 6244 | sIP: source IP of the multicast route |
| 6245 | groupIP: group IP of the multicast route |
| 6246 | dPorts: a list of destination ports of the multicast route |
| 6247 | Returns main.TRUE if mcast route is deleted; Otherwise main.FALSE |
| 6248 | """ |
| 6249 | try: |
| 6250 | cmdStr = "mcast-delete" |
| 6251 | cmdStr += " " + str( sIP ) |
| 6252 | cmdStr += " " + str( groupIP ) |
| 6253 | assert isinstance( dPorts, list ) |
| 6254 | for dPort in dPorts: |
| 6255 | cmdStr += " " + str( dPort ) |
| 6256 | handle = self.sendline( cmdStr ) |
| 6257 | assert handle is not None, "Error in sendline" |
| 6258 | assert "Command not found:" not in handle, handle |
| 6259 | assert "Unsupported command:" not in handle, handle |
| 6260 | assert "Error executing command" not in handle, handle |
| 6261 | if "Updated the mcast route" in handle: |
| 6262 | return main.TRUE |
| 6263 | else: |
| 6264 | return main.FALSE |
| 6265 | except AssertionError: |
| 6266 | main.log.exception( "" ) |
| 6267 | return None |
| 6268 | except TypeError: |
| 6269 | main.log.exception( self.name + ": Object not as expected" ) |
| 6270 | return None |
| 6271 | except pexpect.EOF: |
| 6272 | main.log.error( self.name + ": EOF exception found" ) |
| 6273 | main.log.error( self.name + ": " + self.handle.before ) |
| 6274 | main.cleanAndExit() |
| 6275 | except Exception: |
| 6276 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6277 | main.cleanAndExit() |
| 6278 | |
| 6279 | def mcastHostJoin( self, sAddr, gAddr, srcs, sinks ): |
| 6280 | """ |
| 6281 | Create a multicast route by calling 'mcast-host-join' command |
| 6282 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6283 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6284 | 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] | 6285 | sinks: a list of HostId of the sinks e.g. ["00:AA:00:00:01:05/40"] |
| 6286 | Returns main.TRUE if mcast route is added; Otherwise main.FALSE |
| 6287 | """ |
| 6288 | try: |
| 6289 | cmdStr = "mcast-host-join" |
| 6290 | cmdStr += " -sAddr " + str( sAddr ) |
| 6291 | cmdStr += " -gAddr " + str( gAddr ) |
| 6292 | assert isinstance( srcs, list ) |
| 6293 | for src in srcs: |
| 6294 | cmdStr += " -srcs " + str( src ) |
| 6295 | assert isinstance( sinks, list ) |
| 6296 | for sink in sinks: |
| 6297 | cmdStr += " -sinks " + str( sink ) |
| 6298 | handle = self.sendline( cmdStr ) |
| 6299 | assert handle is not None, "Error in sendline" |
| 6300 | assert "Command not found:" not in handle, handle |
| 6301 | assert "Unsupported command:" not in handle, handle |
| 6302 | assert "Error executing command" not in handle, handle |
| 6303 | if "Added the mcast route" in handle: |
| 6304 | return main.TRUE |
| 6305 | else: |
| 6306 | return main.FALSE |
| 6307 | except AssertionError: |
| 6308 | main.log.exception( "" ) |
| 6309 | return None |
| 6310 | except TypeError: |
| 6311 | main.log.exception( self.name + ": Object not as expected" ) |
| 6312 | return None |
| 6313 | except pexpect.EOF: |
| 6314 | main.log.error( self.name + ": EOF exception found" ) |
| 6315 | main.log.error( self.name + ": " + self.handle.before ) |
| 6316 | main.cleanAndExit() |
| 6317 | except Exception: |
| 6318 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6319 | main.cleanAndExit() |
| 6320 | |
| 6321 | def mcastHostDelete( self, sAddr, gAddr, host=None ): |
| 6322 | """ |
| 6323 | Delete multicast sink(s) by calling 'mcast-host-delete' command |
| 6324 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6325 | gAddr: specifies multicast group address |
You Wang | c02d835 | 2018-04-17 16:42:10 -0700 | [diff] [blame] | 6326 | 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] | 6327 | will delete the route if not specified |
| 6328 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6329 | """ |
| 6330 | try: |
| 6331 | cmdStr = "mcast-host-delete" |
| 6332 | cmdStr += " -sAddr " + str( sAddr ) |
| 6333 | cmdStr += " -gAddr " + str( gAddr ) |
| 6334 | if host: |
| 6335 | cmdStr += " -h " + str( host ) |
| 6336 | handle = self.sendline( cmdStr ) |
| 6337 | assert handle is not None, "Error in sendline" |
| 6338 | assert "Command not found:" not in handle, handle |
| 6339 | assert "Unsupported command:" not in handle, handle |
| 6340 | assert "Error executing command" not in handle, handle |
| 6341 | if "Updated the mcast route" in handle: |
| 6342 | return main.TRUE |
| 6343 | elif "Deleted the mcast route" in handle: |
| 6344 | return main.TRUE |
| 6345 | else: |
| 6346 | return main.FALSE |
| 6347 | except AssertionError: |
| 6348 | main.log.exception( "" ) |
| 6349 | return None |
| 6350 | except TypeError: |
| 6351 | main.log.exception( self.name + ": Object not as expected" ) |
| 6352 | return None |
| 6353 | except pexpect.EOF: |
| 6354 | main.log.error( self.name + ": EOF exception found" ) |
| 6355 | main.log.error( self.name + ": " + self.handle.before ) |
| 6356 | main.cleanAndExit() |
| 6357 | except Exception: |
| 6358 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6359 | main.cleanAndExit() |
| 6360 | |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6361 | def mcastSinkDelete( self, sAddr, gAddr, sink=None ): |
| 6362 | """ |
| 6363 | Delete multicast sink(s) by calling 'mcast-sink-delete' command |
| 6364 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6365 | gAddr: specifies multicast group address |
| 6366 | host: HostId of the sink e.g. "00:AA:00:00:01:05/40", |
| 6367 | will delete the route if not specified |
| 6368 | Returns main.TRUE if the mcast sink is deleted; Otherwise main.FALSE |
| 6369 | """ |
| 6370 | try: |
| 6371 | cmdStr = "mcast-sink-delete" |
| 6372 | cmdStr += " -sAddr " + str( sAddr ) |
| 6373 | cmdStr += " -gAddr " + str( gAddr ) |
| 6374 | if sink: |
| 6375 | cmdStr += " -s " + str( sink ) |
| 6376 | handle = self.sendline( cmdStr ) |
| 6377 | assert handle is not None, "Error in sendline" |
| 6378 | assert "Command not found:" not in handle, handle |
| 6379 | assert "Unsupported command:" not in handle, handle |
| 6380 | assert "Error executing command" not in handle, handle |
| 6381 | if "Updated the mcast route" in handle: |
| 6382 | return main.TRUE |
| 6383 | elif "Deleted the mcast route" in handle: |
| 6384 | return main.TRUE |
| 6385 | else: |
| 6386 | return main.FALSE |
| 6387 | except AssertionError: |
| 6388 | main.log.exception( "" ) |
| 6389 | return None |
| 6390 | except TypeError: |
| 6391 | main.log.exception( self.name + ": Object not as expected" ) |
| 6392 | return None |
| 6393 | except pexpect.EOF: |
| 6394 | main.log.error( self.name + ": EOF exception found" ) |
| 6395 | main.log.error( self.name + ": " + self.handle.before ) |
| 6396 | main.cleanAndExit() |
| 6397 | except Exception: |
| 6398 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6399 | main.cleanAndExit() |
| 6400 | |
You Wang | e24d627 | 2018-03-27 21:18:50 -0700 | [diff] [blame] | 6401 | def mcastSourceDelete( self, sAddr, gAddr, srcs=None ): |
| 6402 | """ |
| 6403 | Delete multicast src(s) by calling 'mcast-source-delete' command |
| 6404 | sAddr: we can provide * for ASM or a specific address for SSM |
| 6405 | gAddr: specifies multicast group address |
You Wang | 547893e | 2018-05-08 13:34:59 -0700 | [diff] [blame] | 6406 | 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] | 6407 | will delete the route if not specified |
| 6408 | Returns main.TRUE if mcast sink is deleted; Otherwise main.FALSE |
| 6409 | """ |
| 6410 | try: |
| 6411 | cmdStr = "mcast-source-delete" |
| 6412 | cmdStr += " -sAddr " + str( sAddr ) |
| 6413 | cmdStr += " -gAddr " + str( gAddr ) |
| 6414 | if srcs: |
| 6415 | assert isinstance( srcs, list ) |
| 6416 | for src in srcs: |
| 6417 | cmdStr += " -src " + str( src ) |
| 6418 | handle = self.sendline( cmdStr ) |
| 6419 | assert handle is not None, "Error in sendline" |
| 6420 | assert "Command not found:" not in handle, handle |
| 6421 | assert "Unsupported command:" not in handle, handle |
| 6422 | assert "Error executing command" not in handle, handle |
| 6423 | if "Updated the mcast route" in handle: |
| 6424 | return main.TRUE |
| 6425 | elif "Deleted the mcast route" in handle: |
| 6426 | return main.TRUE |
| 6427 | else: |
| 6428 | return main.FALSE |
| 6429 | except AssertionError: |
| 6430 | main.log.exception( "" ) |
| 6431 | return None |
| 6432 | except TypeError: |
| 6433 | main.log.exception( self.name + ": Object not as expected" ) |
| 6434 | return None |
| 6435 | except pexpect.EOF: |
| 6436 | main.log.error( self.name + ": EOF exception found" ) |
| 6437 | main.log.error( self.name + ": " + self.handle.before ) |
| 6438 | main.cleanAndExit() |
| 6439 | except Exception: |
| 6440 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6441 | main.cleanAndExit() |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6442 | |
| 6443 | def netcfg( self, jsonFormat=True, args="" ): |
| 6444 | """ |
| 6445 | Run netcfg cli command with given args |
| 6446 | """ |
| 6447 | try: |
| 6448 | cmdStr = "netcfg" |
| 6449 | if jsonFormat: |
| 6450 | cmdStr = cmdStr + " -j" |
| 6451 | if args: |
| 6452 | cmdStr = cmdStr + " " + str( args ) |
| 6453 | handle = self.sendline( cmdStr ) |
| 6454 | assert handle is not None, "Error in sendline" |
| 6455 | assert "Command not found:" not in handle, handle |
| 6456 | assert "Unsupported command:" not in handle, handle |
| 6457 | assert "Error executing command" not in handle, handle |
| 6458 | return handle |
| 6459 | except AssertionError: |
| 6460 | main.log.exception( "" ) |
| 6461 | return None |
| 6462 | except TypeError: |
| 6463 | main.log.exception( self.name + ": Object not as expected" ) |
| 6464 | return None |
| 6465 | except pexpect.EOF: |
| 6466 | main.log.error( self.name + ": EOF exception found" ) |
| 6467 | main.log.error( self.name + ": " + self.handle.before ) |
| 6468 | main.cleanAndExit() |
| 6469 | except Exception: |
| 6470 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6471 | main.cleanAndExit() |
| 6472 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6473 | def composeT3Command( self, sAddr, dAddr, ipv6=False, verbose=True, simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6474 | """ |
| 6475 | Compose and return t3-troubleshoot cli command for given source and destination addresses |
| 6476 | Options: |
| 6477 | sAddr: IP address of the source host |
| 6478 | dAddr: IP address of the destination host |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6479 | ipv6: True if hosts are IPv6 |
| 6480 | verbose: return verbose t3 output if True |
| 6481 | simple: compose command for t3-troubleshoot-simple if True |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6482 | """ |
| 6483 | try: |
| 6484 | # Collect information of both hosts from onos |
| 6485 | hosts = self.hosts() |
| 6486 | hosts = json.loads( hosts ) |
| 6487 | sHost = None |
| 6488 | dHost = None |
| 6489 | for host in hosts: |
| 6490 | if sAddr in host[ "ipAddresses" ]: |
| 6491 | sHost = host |
| 6492 | elif dAddr in host[ "ipAddresses" ]: |
| 6493 | dHost = host |
| 6494 | if sHost and dHost: |
| 6495 | break |
| 6496 | assert sHost, "Not able to find host with IP {}".format( sAddr ) |
You Wang | 5d9527b | 2018-05-29 17:08:54 -0700 | [diff] [blame] | 6497 | if simple: |
| 6498 | assert dHost, "Not able to find host with IP {}".format( dAddr ) |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6499 | cmdStr = "t3-troubleshoot" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6500 | if simple: |
| 6501 | cmdStr += "-simple" |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6502 | if verbose: |
| 6503 | cmdStr += " -vv" |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6504 | if ipv6: |
| 6505 | cmdStr += " -et ipv6" |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 6506 | if simple: |
| 6507 | cmdStr += " {}/{} {}/{}".format( sHost[ "mac" ], sHost[ "vlan" ], dHost[ "mac" ], dHost[ "vlan" ] ) |
| 6508 | else: |
| 6509 | cmdStr += " -s " + str( sAddr ) |
| 6510 | cmdStr += " -sp " + str( sHost[ "locations" ][ 0 ][ "elementId" ] ) + "/" + str( sHost[ "locations" ][ 0 ][ "port" ] ) |
| 6511 | cmdStr += " -sm " + str( sHost[ "mac" ] ) |
| 6512 | if sHost[ "vlan" ] != "None": |
| 6513 | cmdStr += " -vid " + sHost[ "vlan" ] |
| 6514 | cmdStr += " -d " + str( dAddr ) |
| 6515 | netcfg = self.netcfg( args="devices {}".format( sHost[ "locations" ][ 0 ][ "elementId" ] ) ) |
| 6516 | netcfg = json.loads( netcfg ) |
| 6517 | assert netcfg, "Failed to get netcfg" |
| 6518 | cmdStr += " -dm " + str( netcfg[ "segmentrouting" ][ "routerMac" ] ) |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 6519 | return cmdStr |
| 6520 | except AssertionError: |
| 6521 | main.log.exception( "" ) |
| 6522 | return None |
| 6523 | except ( KeyError, TypeError ): |
| 6524 | main.log.exception( self.name + ": Object not as expected" ) |
| 6525 | return None |
| 6526 | except Exception: |
| 6527 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6528 | main.cleanAndExit() |
| 6529 | |
| 6530 | def t3( self, sAddr, dAddr, ipv6=False ): |
| 6531 | """ |
| 6532 | Run t3-troubleshoot cli command for given source and destination addresses |
| 6533 | Options: |
| 6534 | sAddr: IP address of the source host |
| 6535 | dAddr: IP address of the destination host |
| 6536 | """ |
| 6537 | try: |
| 6538 | cmdStr = self.composeT3Command( sAddr, dAddr, ipv6 ) |
| 6539 | handle = self.sendline( cmdStr ) |
| 6540 | assert handle is not None, "Error in sendline" |
| 6541 | assert "Command not found:" not in handle, handle |
| 6542 | assert "Unsupported command:" not in handle, handle |
| 6543 | assert "Error executing command" not in handle, handle |
| 6544 | assert "Tracing packet" in handle |
| 6545 | return handle |
| 6546 | except AssertionError: |
| 6547 | main.log.exception( "" ) |
| 6548 | return None |
| 6549 | except pexpect.EOF: |
| 6550 | main.log.error( self.name + ": EOF exception found" ) |
| 6551 | main.log.error( self.name + ": " + self.handle.before ) |
| 6552 | main.cleanAndExit() |
| 6553 | except Exception: |
| 6554 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 6555 | main.cleanAndExit() |