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 |
| 5 | Copyright 2014 Open Networking Foundation (ONF) |
| 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 |
| 14 | (at your option) any later version. |
| 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 | """ |
| 24 | |
| 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 | |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 64 | def checkOptions(self, var, defaultVar): |
| 65 | if var is None or var == "": |
| 66 | return defaultVar |
| 67 | return var |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 68 | def connect( self, **connectargs ): |
| 69 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 70 | Creates ssh handle for ONOS cli. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 71 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 72 | try: |
| 73 | for key in connectargs: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 74 | vars( self )[ key ] = connectargs[ key ] |
andrew@onlab.us | 658ec01 | 2015-03-11 15:13:09 -0700 | [diff] [blame] | 75 | self.home = "~/onos" |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 76 | for key in self.options: |
| 77 | if key == "home": |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 78 | self.home = self.options[ key ] |
| 79 | elif key == "karaf_username": |
| 80 | self.karafUser = self.options[ key ] |
| 81 | elif key == "karaf_password": |
| 82 | self.karafPass = self.options[ key ] |
| 83 | |
| 84 | self.home = self.checkOptions(self.home, "~/onos") |
| 85 | self.karafUser = self.checkOptions(self.karafUser, self.user_name) |
| 86 | self.karafPass = self.checkOptions(self.karafPass, self.pwd ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 87 | |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 88 | for key in self.options: |
| 89 | if key == 'onosIp': |
| 90 | self.onosIp = self.options[ 'onosIp' ] |
| 91 | break |
| 92 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 93 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 94 | |
| 95 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 96 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 97 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 98 | else: |
| 99 | main.log.info( self.name + |
| 100 | ": Trying to connect to " + |
| 101 | self.ip_address ) |
| 102 | |
| 103 | except KeyError: |
| 104 | main.log.info( "Invalid host name," + |
| 105 | " connecting to local host instead" ) |
| 106 | self.ip_address = 'localhost' |
| 107 | except Exception as inst: |
| 108 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 109 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 110 | self.handle = super( OnosCliDriver, self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 111 | user_name=self.user_name, |
| 112 | ip_address=self.ip_address, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 113 | port=self.port, |
| 114 | pwd=self.pwd, |
| 115 | home=self.home ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 116 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 117 | self.handle.sendline( "cd " + self.home ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 118 | self.handle.expect( self.prompt ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 119 | if self.handle: |
| 120 | return self.handle |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 121 | else: |
| 122 | main.log.info( "NO ONOS HANDLE" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 123 | return main.FALSE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 124 | except TypeError: |
| 125 | main.log.exception( self.name + ": Object not as expected" ) |
| 126 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 127 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 128 | main.log.error( self.name + ": EOF exception found" ) |
| 129 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 130 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 131 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 132 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 133 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 134 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 135 | def disconnect( self ): |
| 136 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 137 | Called when Test is complete to disconnect the ONOS handle. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 138 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 139 | response = main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 140 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 141 | if self.handle: |
| 142 | i = self.logout() |
| 143 | if i == main.TRUE: |
| 144 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 145 | self.handle.expect( self.prompt ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 146 | self.handle.sendline( "exit" ) |
| 147 | self.handle.expect( "closed" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 148 | except TypeError: |
| 149 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 150 | response = main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 151 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 152 | main.log.error( self.name + ": EOF exception found" ) |
| 153 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 154 | except ValueError: |
Jon Hall | 1a77a1e | 2015-04-06 10:41:13 -0700 | [diff] [blame] | 155 | main.log.exception( "Exception in disconnect of " + self.name ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 156 | response = main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 157 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 158 | main.log.exception( self.name + ": Connection failed to the host" ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 159 | response = main.FALSE |
| 160 | return response |
| 161 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 162 | def logout( self ): |
| 163 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 164 | Sends 'logout' command to ONOS cli |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 165 | Returns main.TRUE if exited CLI and |
| 166 | main.FALSE on timeout (not guranteed you are disconnected) |
| 167 | None on TypeError |
| 168 | Exits test on unknown error or pexpect exits unexpectedly |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 169 | """ |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 170 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 171 | if self.handle: |
| 172 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 173 | i = self.handle.expect( [ "onos>", self.prompt, pexpect.TIMEOUT ], |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 174 | timeout=10 ) |
| 175 | if i == 0: # In ONOS CLI |
| 176 | self.handle.sendline( "logout" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 177 | j = self.handle.expect( [ self.prompt, |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 178 | "Command not found:", |
| 179 | pexpect.TIMEOUT ] ) |
| 180 | if j == 0: # Successfully logged out |
| 181 | return main.TRUE |
| 182 | elif j == 1 or j == 2: |
| 183 | # ONOS didn't fully load, and logout command isn't working |
| 184 | # or the command timed out |
| 185 | self.handle.send( "\x04" ) # send ctrl-d |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 186 | try: |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 187 | self.handle.expect( self.prompt ) |
Jon Hall | 64ab3bd | 2016-05-13 11:29:44 -0700 | [diff] [blame] | 188 | except pexpect.TIMEOUT: |
| 189 | main.log.error( "ONOS did not respond to 'logout' or CTRL-d" ) |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 190 | return main.TRUE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 191 | else: # some other output |
Jon Hall | bfe0000 | 2016-04-05 10:23:54 -0700 | [diff] [blame] | 192 | main.log.warn( "Unknown repsonse to logout command: '{}'", |
| 193 | repr( self.handle.before ) ) |
| 194 | return main.FALSE |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 195 | elif i == 1: # not in CLI |
| 196 | return main.TRUE |
| 197 | elif i == 3: # Timeout |
| 198 | return main.FALSE |
| 199 | else: |
andrewonlab | 9627f43 | 2014-11-14 12:45:10 -0500 | [diff] [blame] | 200 | return main.TRUE |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 201 | except TypeError: |
| 202 | main.log.exception( self.name + ": Object not as expected" ) |
| 203 | return None |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 204 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 205 | main.log.error( self.name + ": eof exception found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 206 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 207 | main.cleanAndExit() |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 208 | except ValueError: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 209 | main.log.error( self.name + |
| 210 | "ValueError exception in logout method" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 211 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 212 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 213 | main.cleanAndExit() |
andrewonlab | 38d2b4a | 2014-11-13 16:28:47 -0500 | [diff] [blame] | 214 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 215 | def setCell( self, cellname ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 216 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 217 | Calls 'cell <name>' to set the environment variables on ONOSbench |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 218 | |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 219 | Before issuing any cli commands, set the environment variable first. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 220 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 221 | try: |
| 222 | if not cellname: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 223 | main.log.error( "Must define cellname" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 224 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 225 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 226 | self.handle.sendline( "cell " + str( cellname ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 227 | # Expect the cellname in the ONOSCELL variable. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 228 | # Note that this variable name is subject to change |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 229 | # and that this driver will have to change accordingly |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 230 | self.handle.expect(str(cellname)) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 231 | handleBefore = self.handle.before |
| 232 | handleAfter = self.handle.after |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 233 | # Get the rest of the handle |
Cameron Franke | 9c94fb0 | 2015-01-21 10:20:20 -0800 | [diff] [blame] | 234 | self.handle.sendline("") |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 235 | self.handle.expect(self.prompt) |
andrew@onlab.us | c400b11 | 2015-01-21 15:33:19 -0800 | [diff] [blame] | 236 | handleMore = self.handle.before |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 237 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 238 | main.log.info( "Cell call returned: " + handleBefore + |
| 239 | handleAfter + handleMore ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 240 | |
| 241 | return main.TRUE |
| 242 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 243 | except TypeError: |
| 244 | main.log.exception( self.name + ": Object not as expected" ) |
| 245 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 246 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 247 | main.log.error( self.name + ": eof exception found" ) |
| 248 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 249 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 250 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 251 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 252 | main.cleanAndExit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 253 | |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 254 | def startOnosCli( self, ONOSIp, karafTimeout="", |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 255 | commandlineTimeout=10, onosStartTimeout=60, waitForStart=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 256 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 257 | karafTimeout is an optional argument. karafTimeout value passed |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 258 | by user would be used to set the current karaf shell idle timeout. |
| 259 | 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] | 260 | the subsequent login would reflect new idle timeout. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 261 | Below is an example to start a session with 60 seconds idle timeout |
| 262 | ( input value is in milliseconds ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 263 | |
Hari Krishna | 25d42f7 | 2015-01-05 15:08:28 -0800 | [diff] [blame] | 264 | tValue = "60000" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 265 | main.ONOScli1.startOnosCli( ONOSIp, karafTimeout=tValue ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 266 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 267 | Note: karafTimeout is left as str so that this could be read |
| 268 | and passed to startOnosCli from PARAMS file as str. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 269 | """ |
You Wang | f69ab39 | 2016-01-26 16:34:38 -0800 | [diff] [blame] | 270 | self.onosIp = ONOSIp |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 271 | try: |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 272 | # Check if we are already in the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 273 | self.handle.sendline( "" ) |
| 274 | x = self.handle.expect( [ |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 275 | self.prompt, "onos>" ], commandlineTimeout) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 276 | if x == 1: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 277 | main.log.info( "ONOS cli is already running" ) |
andrewonlab | 48829f6 | 2014-11-17 13:49:01 -0500 | [diff] [blame] | 278 | return main.TRUE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 279 | |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 280 | # Not in CLI so login |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 281 | if waitForStart: |
| 282 | # Wait for onos start ( -w ) and enter onos cli |
| 283 | startCliCommand = "onos -w " |
| 284 | else: |
| 285 | startCliCommand = "onos " |
| 286 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 287 | i = self.handle.expect( [ |
| 288 | "onos>", |
pingping-lin | 57a56ce | 2015-05-20 16:43:48 -0700 | [diff] [blame] | 289 | pexpect.TIMEOUT ], onosStartTimeout ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 290 | |
| 291 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 292 | main.log.info( str( ONOSIp ) + " CLI Started successfully" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 293 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 294 | self.handle.sendline( |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 295 | "config:property-set -p org.apache.karaf.shell\ |
| 296 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 297 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 298 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 299 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 300 | self.handle.expect( "onos>" ) |
andrewonlab | 2a7ea9b | 2014-10-24 12:21:05 -0400 | [diff] [blame] | 301 | return main.TRUE |
| 302 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 303 | # If failed, send ctrl+c to process and try again |
| 304 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 305 | self.handle.send( "\x03" ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 306 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 307 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 308 | timeout=30 ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 309 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 310 | main.log.info( str( ONOSIp ) + " CLI Started " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 311 | "successfully after retry attempt" ) |
Hari Krishna | e36ef21 | 2015-01-04 14:09:13 -0800 | [diff] [blame] | 312 | if karafTimeout: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 313 | self.handle.sendline( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 314 | "config:property-set -p org.apache.karaf.shell\ |
| 315 | sshIdleTimeout " + |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 316 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 317 | self.handle.expect( self.prompt ) |
Chiyu Cheng | ef10950 | 2016-11-21 15:51:38 -0800 | [diff] [blame] | 318 | self.handle.sendline( startCliCommand + str( ONOSIp ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 319 | self.handle.expect( "onos>" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 320 | return main.TRUE |
| 321 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 322 | main.log.error( "Connection to CLI " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 323 | str( ONOSIp ) + " timeout" ) |
andrewonlab | 3a7c3c7 | 2014-10-24 17:21:03 -0400 | [diff] [blame] | 324 | return main.FALSE |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 325 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 326 | except TypeError: |
| 327 | main.log.exception( self.name + ": Object not as expected" ) |
| 328 | return None |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 329 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 330 | main.log.error( self.name + ": EOF exception found" ) |
| 331 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 332 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 333 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 334 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 335 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 336 | |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 337 | def startCellCli( self, karafTimeout="", |
| 338 | commandlineTimeout=10, onosStartTimeout=60 ): |
| 339 | """ |
| 340 | Start CLI on onos ecll handle. |
| 341 | |
| 342 | karafTimeout is an optional argument. karafTimeout value passed |
| 343 | by user would be used to set the current karaf shell idle timeout. |
| 344 | Note that when ever this property is modified the shell will exit and |
| 345 | the subsequent login would reflect new idle timeout. |
| 346 | Below is an example to start a session with 60 seconds idle timeout |
| 347 | ( input value is in milliseconds ): |
| 348 | |
| 349 | tValue = "60000" |
| 350 | |
| 351 | Note: karafTimeout is left as str so that this could be read |
| 352 | and passed to startOnosCli from PARAMS file as str. |
| 353 | """ |
| 354 | |
| 355 | try: |
| 356 | self.handle.sendline( "" ) |
| 357 | x = self.handle.expect( [ |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 358 | self.prompt, "onos>" ], commandlineTimeout) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 359 | |
| 360 | if x == 1: |
| 361 | main.log.info( "ONOS cli is already running" ) |
| 362 | return main.TRUE |
| 363 | |
| 364 | # Wait for onos start ( -w ) and enter onos cli |
| 365 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 366 | i = self.handle.expect( [ |
| 367 | "onos>", |
| 368 | pexpect.TIMEOUT ], onosStartTimeout ) |
| 369 | |
| 370 | if i == 0: |
| 371 | main.log.info( self.name + " CLI Started successfully" ) |
| 372 | if karafTimeout: |
| 373 | self.handle.sendline( |
| 374 | "config:property-set -p org.apache.karaf.shell\ |
| 375 | sshIdleTimeout " + |
| 376 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 377 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 378 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 379 | self.handle.expect( "onos>" ) |
| 380 | return main.TRUE |
| 381 | else: |
| 382 | # If failed, send ctrl+c to process and try again |
| 383 | main.log.info( "Starting CLI failed. Retrying..." ) |
| 384 | self.handle.send( "\x03" ) |
| 385 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 386 | i = self.handle.expect( [ "onos>", pexpect.TIMEOUT ], |
| 387 | timeout=30 ) |
| 388 | if i == 0: |
| 389 | main.log.info( self.name + " CLI Started " + |
| 390 | "successfully after retry attempt" ) |
| 391 | if karafTimeout: |
| 392 | self.handle.sendline( |
| 393 | "config:property-set -p org.apache.karaf.shell\ |
| 394 | sshIdleTimeout " + |
| 395 | karafTimeout ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 396 | self.handle.expect( self.prompt ) |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 397 | self.handle.sendline( "/opt/onos/bin/onos" ) |
| 398 | self.handle.expect( "onos>" ) |
| 399 | return main.TRUE |
| 400 | else: |
| 401 | main.log.error( "Connection to CLI " + |
| 402 | self.name + " timeout" ) |
| 403 | return main.FALSE |
| 404 | |
| 405 | except TypeError: |
| 406 | main.log.exception( self.name + ": Object not as expected" ) |
| 407 | return None |
| 408 | except pexpect.EOF: |
| 409 | main.log.error( self.name + ": EOF exception found" ) |
| 410 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 411 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 412 | except Exception: |
| 413 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 414 | main.cleanAndExit() |
suibin zhang | 116647a | 2016-05-06 16:30:09 -0700 | [diff] [blame] | 415 | |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 416 | def log( self, cmdStr, level="", noExit=False ): |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 417 | """ |
| 418 | log the commands in the onos CLI. |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 419 | returns main.TRUE on success |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 420 | returns main.FALSE if Error occurred |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 421 | if noExit is True, TestON will not exit, but clean up |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 422 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 423 | Level defaults to INFO |
Pratik Parab | 3b2ab5a | 2017-02-14 13:15:14 -0800 | [diff] [blame] | 424 | if cmdStr has spaces then put quotes in the passed string |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 425 | """ |
| 426 | try: |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 427 | lvlStr = "" |
| 428 | if level: |
| 429 | lvlStr = "--level=" + level |
| 430 | |
kelvin-onlab | 338f551 | 2015-02-06 10:53:16 -0800 | [diff] [blame] | 431 | self.handle.sendline( "log:log " + lvlStr + " " + cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 432 | self.handle.expect( "log:log" ) |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 433 | self.handle.expect( "onos>" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 434 | |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 435 | response = self.handle.before |
| 436 | if re.search( "Error", response ): |
| 437 | return main.FALSE |
| 438 | return main.TRUE |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 439 | except pexpect.TIMEOUT: |
| 440 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 441 | if noExit: |
| 442 | main.cleanup() |
| 443 | return None |
| 444 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 445 | main.cleanAndExit() |
kelvin-onlab | 9f54103 | 2015-02-04 16:19:53 -0800 | [diff] [blame] | 446 | except pexpect.EOF: |
| 447 | main.log.error( self.name + ": EOF exception found" ) |
| 448 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 449 | if noExit: |
| 450 | main.cleanup() |
| 451 | return None |
| 452 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 453 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 454 | except Exception: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 455 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 456 | if noExit: |
| 457 | main.cleanup() |
| 458 | return None |
| 459 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 460 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 461 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 462 | 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] | 463 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 464 | Send a completely user specified string to |
| 465 | the onos> prompt. Use this function if you have |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 466 | a very specific command to send. |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 467 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 468 | if noExit is True, TestON will not exit, and return None |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 469 | if dollarSign is True, TestON will not expect for '$' as a new CLI or onos> prompt |
| 470 | since '$' can be in the output. |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 471 | |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 472 | Warning: There are no sanity checking to commands |
| 473 | sent using this method. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 474 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 475 | """ |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 476 | try: |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 477 | # Try to reconnect if disconnected from cli |
| 478 | self.handle.sendline( "" ) |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 479 | i = self.handle.expect( [ "onos>", self.prompt, pexpect.TIMEOUT ] ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 480 | if i == 1: |
| 481 | main.log.error( self.name + ": onos cli session closed. ") |
| 482 | if self.onosIp: |
| 483 | main.log.warn( "Trying to reconnect " + self.onosIp ) |
| 484 | reconnectResult = self.startOnosCli( self.onosIp ) |
| 485 | if reconnectResult: |
| 486 | main.log.info( self.name + ": onos cli session reconnected." ) |
| 487 | else: |
| 488 | main.log.error( self.name + ": reconnection failed." ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 489 | if noExit: |
| 490 | return None |
| 491 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 492 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 493 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 494 | main.cleanAndExit() |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 495 | if i == 2: |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 496 | main.log.warn( "Timeout when testing cli responsiveness" ) |
| 497 | main.log.debug( self.handle.before ) |
| 498 | self.handle.send( "\x03" ) # Send ctrl-c to clear previous output |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 499 | self.handle.expect( "onos>" ) |
| 500 | |
Jon Hall | 14a03b5 | 2016-05-11 12:07:30 -0700 | [diff] [blame] | 501 | if debug: |
| 502 | # NOTE: This adds and average of .4 seconds per call |
| 503 | logStr = "\"Sending CLI command: '" + cmdStr + "'\"" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 504 | self.log( logStr, noExit=noExit ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 505 | self.handle.sendline( cmdStr ) |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 506 | if dollarSign: |
| 507 | i = self.handle.expect( ["onos>"], timeout ) |
| 508 | else: |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 509 | i = self.handle.expect( ["onos>", self.prompt], timeout ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 510 | response = self.handle.before |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 511 | # TODO: do something with i |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 512 | main.log.info( "Command '" + str( cmdStr ) + "' sent to " |
| 513 | + self.name + "." ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 514 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 515 | main.log.debug( self.name + ": Raw output" ) |
| 516 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 517 | |
| 518 | # Remove ANSI color control strings from output |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 519 | ansiEscape = re.compile( r'\x1b[^m]*m' ) |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 520 | response = ansiEscape.sub( '', response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 521 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 522 | main.log.debug( self.name + ": ansiEscape output" ) |
| 523 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 524 | |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 525 | # Remove extra return chars that get added |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 526 | response = re.sub( r"\s\r", "", response ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 527 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 528 | main.log.debug( self.name + ": Removed extra returns " + |
| 529 | "from output" ) |
| 530 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 531 | |
| 532 | # Strip excess whitespace |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 533 | response = response.strip() |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 534 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 535 | main.log.debug( self.name + ": parsed and stripped output" ) |
| 536 | main.log.debug( self.name + ": " + repr( response ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 537 | |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 538 | # parse for just the output, remove the cmd from response |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 539 | output = response.split( cmdStr.strip(), 1 ) |
| 540 | if debug: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 541 | main.log.debug( self.name + ": split output" ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 542 | for r in output: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 543 | main.log.debug( self.name + ": " + repr( r ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 544 | output = output[1].strip() |
| 545 | if showResponse: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 546 | main.log.info( "Response from ONOS: {}".format( output ) ) |
GlennRC | 8587043 | 2015-11-23 11:45:51 -0800 | [diff] [blame] | 547 | return output |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 548 | except pexpect.TIMEOUT: |
| 549 | main.log.error( self.name + ":ONOS timeout" ) |
| 550 | if debug: |
| 551 | main.log.debug( self.handle.before ) |
| 552 | return None |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 553 | except IndexError: |
| 554 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 555 | main.log.debug( "response: {}".format( repr( response ) ) ) |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 556 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 557 | except TypeError: |
| 558 | main.log.exception( self.name + ": Object not as expected" ) |
| 559 | return None |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 560 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 561 | main.log.error( self.name + ": EOF exception found" ) |
| 562 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 563 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 564 | return None |
| 565 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 566 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 567 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 568 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 569 | if noExit: |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 570 | return None |
| 571 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 572 | main.cleanAndExit() |
andrewonlab | a18f6bf | 2014-10-13 19:31:54 -0400 | [diff] [blame] | 573 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 574 | # IMPORTANT NOTE: |
| 575 | # For all cli commands, naming convention should match |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 576 | # the cli command changing 'a:b' with 'aB'. |
| 577 | # Ex ) onos:topology > onosTopology |
| 578 | # onos:links > onosLinks |
| 579 | # feature:list > featureList |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 580 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 581 | def addNode( self, nodeId, ONOSIp, tcpPort="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 582 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 583 | Adds a new cluster node by ID and address information. |
| 584 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 585 | * nodeId |
| 586 | * ONOSIp |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 587 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 588 | * tcpPort |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 589 | """ |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 590 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 591 | cmdStr = "add-node " + str( nodeId ) + " " +\ |
| 592 | str( ONOSIp ) + " " + str( tcpPort ) |
| 593 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 594 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 595 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 596 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 597 | main.log.error( "Error in adding node" ) |
| 598 | main.log.error( handle ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 599 | return main.FALSE |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 600 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 601 | main.log.info( "Node " + str( ONOSIp ) + " added" ) |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 602 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 603 | except AssertionError: |
| 604 | main.log.exception( "" ) |
| 605 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 606 | except TypeError: |
| 607 | main.log.exception( self.name + ": Object not as expected" ) |
| 608 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 609 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 610 | main.log.error( self.name + ": EOF exception found" ) |
| 611 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 612 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 613 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 614 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 615 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 616 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 617 | def removeNode( self, nodeId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 618 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 619 | Removes a cluster by ID |
| 620 | Issues command: 'remove-node [<node-id>]' |
| 621 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 622 | * nodeId |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 623 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 624 | try: |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 625 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 626 | cmdStr = "remove-node " + str( nodeId ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 627 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 628 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 629 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 630 | if re.search( "Error", handle ): |
| 631 | main.log.error( "Error in removing node" ) |
| 632 | main.log.error( handle ) |
| 633 | return main.FALSE |
| 634 | else: |
| 635 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 636 | except AssertionError: |
| 637 | main.log.exception( "" ) |
| 638 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 639 | except TypeError: |
| 640 | main.log.exception( self.name + ": Object not as expected" ) |
| 641 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 642 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 643 | main.log.error( self.name + ": EOF exception found" ) |
| 644 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 645 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 646 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 647 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 648 | main.cleanAndExit() |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 649 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 650 | def nodes( self, jsonFormat=True): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 651 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 652 | List the nodes currently visible |
| 653 | Issues command: 'nodes' |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 654 | Optional argument: |
| 655 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 656 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 657 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 658 | cmdStr = "nodes" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 659 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 660 | cmdStr += " -j" |
| 661 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 662 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 663 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 664 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 665 | except AssertionError: |
| 666 | main.log.exception( "" ) |
| 667 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 668 | except TypeError: |
| 669 | main.log.exception( self.name + ": Object not as expected" ) |
| 670 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 671 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 672 | main.log.error( self.name + ": EOF exception found" ) |
| 673 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 674 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 675 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 676 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 677 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 678 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 679 | def topology( self ): |
| 680 | """ |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 681 | Definition: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 682 | Returns the output of topology command. |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 683 | Return: |
| 684 | topology = current ONOS topology |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 685 | """ |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 686 | try: |
Hari Krishna | ef1bd4e | 2015-03-12 16:55:30 -0700 | [diff] [blame] | 687 | cmdStr = "topology -j" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 688 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 689 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 690 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 691 | main.log.info( cmdStr + " returned: " + str( handle ) ) |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 692 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 693 | except AssertionError: |
| 694 | main.log.exception( "" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 695 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 696 | except TypeError: |
| 697 | main.log.exception( self.name + ": Object not as expected" ) |
| 698 | return None |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 699 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 700 | main.log.error( self.name + ": EOF exception found" ) |
| 701 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 702 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 703 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 704 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 705 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 706 | |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 707 | def deviceRemove( self, deviceId ): |
| 708 | """ |
| 709 | Removes particular device from storage |
| 710 | |
| 711 | TODO: refactor this function |
| 712 | """ |
| 713 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 714 | cmdStr = "device-remove " + str( deviceId ) |
| 715 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 716 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 717 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 718 | if re.search( "Error", handle ): |
| 719 | main.log.error( "Error in removing device" ) |
| 720 | main.log.error( handle ) |
| 721 | return main.FALSE |
| 722 | else: |
| 723 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 724 | except AssertionError: |
| 725 | main.log.exception( "" ) |
| 726 | return None |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 727 | except TypeError: |
| 728 | main.log.exception( self.name + ": Object not as expected" ) |
| 729 | return None |
| 730 | except pexpect.EOF: |
| 731 | main.log.error( self.name + ": EOF exception found" ) |
| 732 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 733 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 734 | except Exception: |
| 735 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 736 | main.cleanAndExit() |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 737 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 738 | def devices( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 739 | """ |
Jon Hall | 7b02d95 | 2014-10-17 20:14:54 -0400 | [diff] [blame] | 740 | Lists all infrastructure devices or switches |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 741 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 742 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 743 | """ |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 744 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 745 | cmdStr = "devices" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 746 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 747 | cmdStr += " -j" |
| 748 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 749 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 750 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 751 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 752 | except AssertionError: |
| 753 | main.log.exception( "" ) |
| 754 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 755 | except TypeError: |
| 756 | main.log.exception( self.name + ": Object not as expected" ) |
| 757 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 758 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 759 | main.log.error( self.name + ": EOF exception found" ) |
| 760 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 761 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 762 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 763 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 764 | main.cleanAndExit() |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 765 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 766 | def balanceMasters( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 767 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 768 | This balances the devices across all controllers |
| 769 | by issuing command: 'onos> onos:balance-masters' |
| 770 | If required this could be extended to return devices balanced output. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 771 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 772 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 773 | cmdStr = "onos:balance-masters" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 774 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 775 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 776 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 777 | if re.search( "Error", handle ): |
| 778 | main.log.error( "Error in balancing masters" ) |
| 779 | main.log.error( handle ) |
| 780 | return main.FALSE |
| 781 | else: |
| 782 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 783 | except AssertionError: |
| 784 | main.log.exception( "" ) |
| 785 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 786 | except TypeError: |
| 787 | main.log.exception( self.name + ": Object not as expected" ) |
| 788 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 789 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 790 | main.log.error( self.name + ": EOF exception found" ) |
| 791 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 792 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 793 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 794 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 795 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 796 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 797 | def checkMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 798 | """ |
| 799 | Returns the output of the masters command. |
| 800 | Optional argument: |
| 801 | * jsonFormat - boolean indicating if you want output in json |
| 802 | """ |
| 803 | try: |
| 804 | cmdStr = "onos:masters" |
| 805 | if jsonFormat: |
| 806 | cmdStr += " -j" |
| 807 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 808 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 809 | assert "Command not found:" not in output, output |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 810 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 811 | except AssertionError: |
| 812 | main.log.exception( "" ) |
| 813 | return None |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 814 | except TypeError: |
| 815 | main.log.exception( self.name + ": Object not as expected" ) |
| 816 | return None |
| 817 | except pexpect.EOF: |
| 818 | main.log.error( self.name + ": EOF exception found" ) |
| 819 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 820 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 821 | except Exception: |
| 822 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 823 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 824 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 825 | def checkBalanceMasters( self, jsonFormat=True ): |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 826 | """ |
| 827 | Uses the master command to check that the devices' leadership |
| 828 | is evenly divided |
| 829 | |
| 830 | Dependencies: checkMasters() and summary() |
| 831 | |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 832 | Returns main.TRUE if the devices are balanced |
| 833 | Returns main.FALSE if the devices are unbalanced |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 834 | Exits on Exception |
| 835 | Returns None on TypeError |
| 836 | """ |
| 837 | try: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 838 | summaryOutput = self.summary() |
| 839 | totalDevices = json.loads( summaryOutput )[ "devices" ] |
| 840 | except ( TypeError, ValueError ): |
| 841 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summaryOutput ) ) |
| 842 | return None |
| 843 | try: |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 844 | totalOwnedDevices = 0 |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 845 | mastersOutput = self.checkMasters() |
| 846 | masters = json.loads( mastersOutput ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 847 | first = masters[ 0 ][ "size" ] |
| 848 | for master in masters: |
| 849 | totalOwnedDevices += master[ "size" ] |
| 850 | if master[ "size" ] > first + 1 or master[ "size" ] < first - 1: |
| 851 | main.log.error( "Mastership not balanced" ) |
| 852 | main.log.info( "\n" + self.checkMasters( False ) ) |
| 853 | return main.FALSE |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 854 | main.log.info( "Mastership balanced between " + |
| 855 | str( len(masters) ) + " masters" ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 856 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 857 | except ( TypeError, ValueError ): |
| 858 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, mastersOutput ) ) |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 859 | return None |
| 860 | except pexpect.EOF: |
| 861 | main.log.error( self.name + ": EOF exception found" ) |
| 862 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 863 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 864 | except Exception: |
| 865 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 866 | main.cleanAndExit() |
acsmars | 2495002 | 2015-07-30 18:00:43 -0700 | [diff] [blame] | 867 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 868 | def links( self, jsonFormat=True, timeout=30 ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 869 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 870 | Lists all core links |
| 871 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 872 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 873 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 874 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 875 | cmdStr = "links" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 876 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 877 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 878 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 879 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 880 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 881 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 882 | except AssertionError: |
| 883 | main.log.exception( "" ) |
| 884 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 885 | except TypeError: |
| 886 | main.log.exception( self.name + ": Object not as expected" ) |
| 887 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 888 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 889 | main.log.error( self.name + ": EOF exception found" ) |
| 890 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 891 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 892 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 893 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 894 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 895 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 896 | def ports( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 897 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 898 | Lists all ports |
| 899 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 900 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 901 | """ |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 902 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 903 | cmdStr = "ports" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 904 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 905 | cmdStr += " -j" |
| 906 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 907 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 908 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 909 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 910 | except AssertionError: |
| 911 | main.log.exception( "" ) |
| 912 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 913 | except TypeError: |
| 914 | main.log.exception( self.name + ": Object not as expected" ) |
| 915 | return None |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 916 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 917 | main.log.error( self.name + ": EOF exception found" ) |
| 918 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 919 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 920 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 921 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 922 | main.cleanAndExit() |
Jon Hall | e821748 | 2014-10-17 13:49:14 -0400 | [diff] [blame] | 923 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 924 | def roles( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 925 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 926 | Lists all devices and the controllers with roles assigned to them |
| 927 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 928 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 929 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 930 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 931 | cmdStr = "roles" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 932 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 933 | cmdStr += " -j" |
| 934 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 935 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 936 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 937 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 938 | except AssertionError: |
| 939 | main.log.exception( "" ) |
| 940 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 941 | except TypeError: |
| 942 | main.log.exception( self.name + ": Object not as expected" ) |
| 943 | return None |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 944 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 945 | main.log.error( self.name + ": EOF exception found" ) |
| 946 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 947 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 948 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 949 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 950 | main.cleanAndExit() |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 951 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 952 | def getRole( self, deviceId ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 953 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 954 | Given the a string containing the json representation of the "roles" |
| 955 | cli command and a partial or whole device id, returns a json object |
| 956 | containing the roles output for the first device whose id contains |
| 957 | "device_id" |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 958 | |
| 959 | Returns: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 960 | A dict of the role assignments for the given device or |
| 961 | None if no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 962 | """ |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 963 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 964 | if deviceId is None: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 965 | return None |
| 966 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 967 | rawRoles = self.roles() |
| 968 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 969 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 970 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 971 | # print device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 972 | if str( deviceId ) in device[ 'id' ]: |
Jon Hall | 983a170 | 2014-10-28 18:44:22 -0400 | [diff] [blame] | 973 | return device |
| 974 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 975 | except ( TypeError, ValueError ): |
| 976 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 977 | return None |
andrewonlab | 86dc308 | 2014-10-13 18:18:38 -0400 | [diff] [blame] | 978 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 979 | main.log.error( self.name + ": EOF exception found" ) |
| 980 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 981 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 982 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 983 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 984 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 985 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 986 | def rolesNotNull( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 987 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 988 | Iterates through each device and checks if there is a master assigned |
| 989 | Returns: main.TRUE if each device has a master |
| 990 | main.FALSE any device has no master |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 991 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 992 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 993 | rawRoles = self.roles() |
| 994 | rolesJson = json.loads( rawRoles ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 995 | # search json for the device with id then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 996 | for device in rolesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 997 | # print device |
| 998 | if device[ 'master' ] == "none": |
| 999 | main.log.warn( "Device has no master: " + str( device ) ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1000 | return main.FALSE |
| 1001 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1002 | except ( TypeError, ValueError ): |
| 1003 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawRoles ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1004 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1005 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1006 | main.log.error( self.name + ": EOF exception found" ) |
| 1007 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1008 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1009 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1010 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1011 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1012 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1013 | def paths( self, srcId, dstId ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1014 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1015 | Returns string of paths, and the cost. |
| 1016 | Issues command: onos:paths <src> <dst> |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1017 | """ |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1018 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1019 | cmdStr = "onos:paths " + str( srcId ) + " " + str( dstId ) |
| 1020 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1021 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1022 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1023 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1024 | main.log.error( "Error in getting paths" ) |
| 1025 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1026 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1027 | path = handle.split( ";" )[ 0 ] |
| 1028 | cost = handle.split( ";" )[ 1 ] |
| 1029 | return ( path, cost ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1030 | except AssertionError: |
| 1031 | main.log.exception( "" ) |
| 1032 | return ( handle, "Error" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1033 | except TypeError: |
| 1034 | main.log.exception( self.name + ": Object not as expected" ) |
| 1035 | return ( handle, "Error" ) |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1036 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1037 | main.log.error( self.name + ": EOF exception found" ) |
| 1038 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1039 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1040 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1041 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1042 | main.cleanAndExit() |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1043 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1044 | def hosts( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1045 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1046 | Lists all discovered hosts |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1047 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1048 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1049 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1050 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1051 | cmdStr = "hosts" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1052 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1053 | cmdStr += " -j" |
| 1054 | handle = self.sendline( cmdStr ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1055 | if handle: |
| 1056 | assert "Command not found:" not in handle, handle |
Jon Hall | baf5316 | 2015-12-17 17:04:34 -0800 | [diff] [blame] | 1057 | # TODO: Maybe make this less hardcoded |
| 1058 | # ConsistentMap Exceptions |
| 1059 | assert "org.onosproject.store.service" not in handle |
| 1060 | # Node not leader |
| 1061 | assert "java.lang.IllegalStateException" not in handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1062 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1063 | except AssertionError: |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 1064 | main.log.exception( "Error in processing '" + cmdStr + "' " + |
Jeremy Songster | 6949cea | 2016-04-19 18:13:18 -0700 | [diff] [blame] | 1065 | "command: " + str( handle ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1066 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1067 | except TypeError: |
| 1068 | main.log.exception( self.name + ": Object not as expected" ) |
| 1069 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1070 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1071 | main.log.error( self.name + ": EOF exception found" ) |
| 1072 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1073 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1074 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1075 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1076 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1077 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1078 | def getHost( self, mac ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1079 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1080 | Return the first host from the hosts api whose 'id' contains 'mac' |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1081 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1082 | 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] | 1083 | partial mac address |
| 1084 | |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1085 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1086 | """ |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1087 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1088 | if mac is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1089 | return None |
| 1090 | else: |
| 1091 | mac = mac |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1092 | rawHosts = self.hosts() |
| 1093 | hostsJson = json.loads( rawHosts ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1094 | # search json for the host with mac then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1095 | for host in hostsJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1096 | # print "%s in %s?" % ( mac, host[ 'id' ] ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1097 | if not host: |
| 1098 | pass |
| 1099 | elif mac in host[ 'id' ]: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1100 | return host |
| 1101 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1102 | except ( TypeError, ValueError ): |
| 1103 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawHosts ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1104 | return None |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1105 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1106 | main.log.error( self.name + ": EOF exception found" ) |
| 1107 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1108 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1109 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1110 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1111 | main.cleanAndExit() |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 1112 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1113 | def getHostsId( self, hostList ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1114 | """ |
| 1115 | Obtain list of hosts |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1116 | Issues command: 'onos> hosts' |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1117 | |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1118 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1119 | * hostList: List of hosts obtained by Mininet |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1120 | IMPORTANT: |
| 1121 | This function assumes that you started your |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1122 | topology with the option '--mac'. |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1123 | Furthermore, it assumes that value of VLAN is '-1' |
| 1124 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1125 | Converts mininet hosts ( h1, h2, h3... ) into |
| 1126 | ONOS format ( 00:00:00:00:00:01/-1 , ... ) |
| 1127 | """ |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1128 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1129 | onosHostList = [] |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1130 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1131 | for host in hostList: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1132 | host = host.replace( "h", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1133 | hostHex = hex( int( host ) ).zfill( 12 ) |
| 1134 | hostHex = str( hostHex ).replace( 'x', '0' ) |
| 1135 | i = iter( str( hostHex ) ) |
| 1136 | hostHex = ":".join( a + b for a, b in zip( i, i ) ) |
| 1137 | hostHex = hostHex + "/-1" |
| 1138 | onosHostList.append( hostHex ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1139 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1140 | return onosHostList |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1141 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1142 | except TypeError: |
| 1143 | main.log.exception( self.name + ": Object not as expected" ) |
| 1144 | return None |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1145 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1146 | main.log.error( self.name + ": EOF exception found" ) |
| 1147 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1148 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1149 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1150 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1151 | main.cleanAndExit() |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 1152 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1153 | def addHostIntent( self, hostIdOne, hostIdTwo, vlanId="", setVlan="", encap="", bandwidth="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1154 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1155 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1156 | * hostIdOne: ONOS host id for host1 |
| 1157 | * hostIdTwo: ONOS host id for host2 |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1158 | Optional: |
| 1159 | * vlanId: specify a VLAN id for the intent |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1160 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1161 | * encap: specify an encapsulation type |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1162 | Description: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1163 | Adds a host-to-host intent ( bidirectional ) by |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1164 | specifying the two hosts. |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1165 | Returns: |
| 1166 | A string of the intent id or None on Error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1167 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1168 | try: |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1169 | cmdStr = "add-host-intent " |
| 1170 | if vlanId: |
| 1171 | cmdStr += "-v " + str( vlanId ) + " " |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1172 | if setVlan: |
| 1173 | cmdStr += "--setVlan " + str( vlanId ) + " " |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1174 | if encap: |
| 1175 | cmdStr += "--encapsulation " + str( encap ) + " " |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 1176 | if bandwidth: |
| 1177 | cmdStr += "-b " + str( bandwidth ) + " " |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1178 | cmdStr += str( hostIdOne ) + " " + str( hostIdTwo ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1179 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1180 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1181 | assert "Command not found:" not in handle, handle |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1182 | if re.search( "Error", handle ): |
| 1183 | main.log.error( "Error in adding Host intent" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1184 | main.log.debug( "Response from ONOS was: " + repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1185 | return None |
Hari Krishna | ac4e178 | 2015-01-26 12:09:12 -0800 | [diff] [blame] | 1186 | else: |
| 1187 | main.log.info( "Host intent installed between " + |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1188 | str( hostIdOne ) + " and " + str( hostIdTwo ) ) |
| 1189 | match = re.search('id=0x([\da-f]+),', handle) |
| 1190 | if match: |
| 1191 | return match.group()[3:-1] |
| 1192 | else: |
| 1193 | main.log.error( "Error, intent ID not found" ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1194 | main.log.debug( "Response from ONOS was: " + |
| 1195 | repr( handle ) ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1196 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1197 | except AssertionError: |
| 1198 | main.log.exception( "" ) |
| 1199 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1200 | except TypeError: |
| 1201 | main.log.exception( self.name + ": Object not as expected" ) |
| 1202 | return None |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1203 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1204 | main.log.error( self.name + ": EOF exception found" ) |
| 1205 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1206 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1207 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1208 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1209 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 1210 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1211 | def addOpticalIntent( self, ingressDevice, egressDevice ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1212 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1213 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1214 | * ingressDevice: device id of ingress device |
| 1215 | * egressDevice: device id of egress device |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1216 | Optional: |
| 1217 | TODO: Still needs to be implemented via dev side |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1218 | Description: |
| 1219 | Adds an optical intent by specifying an ingress and egress device |
| 1220 | Returns: |
| 1221 | A string of the intent id or None on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1222 | """ |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1223 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1224 | cmdStr = "add-optical-intent " + str( ingressDevice ) +\ |
| 1225 | " " + str( egressDevice ) |
| 1226 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1227 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1228 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1229 | # If error, return error message |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1230 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1231 | main.log.error( "Error in adding Optical intent" ) |
| 1232 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1233 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1234 | main.log.info( "Optical intent installed between " + |
| 1235 | str( ingressDevice ) + " and " + |
| 1236 | str( egressDevice ) ) |
| 1237 | match = re.search('id=0x([\da-f]+),', handle) |
| 1238 | if match: |
| 1239 | return match.group()[3:-1] |
| 1240 | else: |
| 1241 | main.log.error( "Error, intent ID not found" ) |
| 1242 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1243 | except AssertionError: |
| 1244 | main.log.exception( "" ) |
| 1245 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1246 | except TypeError: |
| 1247 | main.log.exception( self.name + ": Object not as expected" ) |
| 1248 | return None |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1249 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1250 | main.log.error( self.name + ": EOF exception found" ) |
| 1251 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1252 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1253 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1254 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1255 | main.cleanAndExit() |
andrewonlab | 7b31d23 | 2014-10-24 13:31:47 -0400 | [diff] [blame] | 1256 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1257 | def addPointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1258 | self, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1259 | ingressDevice, |
| 1260 | egressDevice, |
| 1261 | portIngress="", |
| 1262 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1263 | ethType="", |
| 1264 | ethSrc="", |
| 1265 | ethDst="", |
| 1266 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1267 | lambdaAlloc=False, |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1268 | protected=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1269 | ipProto="", |
| 1270 | ipSrc="", |
| 1271 | ipDst="", |
| 1272 | tcpSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1273 | tcpDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1274 | vlanId="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1275 | setVlan="", |
| 1276 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1277 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1278 | Required: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1279 | * ingressDevice: device id of ingress device |
| 1280 | * egressDevice: device id of egress device |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1281 | Optional: |
| 1282 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1283 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1284 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 1285 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1286 | * lambdaAlloc: if True, intent will allocate lambda |
andrewonlab | 40ccd8b | 2014-11-06 16:23:34 -0500 | [diff] [blame] | 1287 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1288 | * ipProto: specify ip protocol |
andrewonlab | f77e0cb | 2014-11-11 17:17:59 -0500 | [diff] [blame] | 1289 | * ipSrc: specify ip source address |
| 1290 | * ipDst: specify ip destination address |
| 1291 | * tcpSrc: specify tcp source port |
| 1292 | * tcpDst: specify tcp destination port |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1293 | * vlanId: specify vlan ID |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1294 | * setVlan: specify a VLAN id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1295 | * encap: specify an Encapsulation type to use |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1296 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1297 | Adds a point-to-point intent ( uni-directional ) by |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1298 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1299 | Returns: |
| 1300 | A string of the intent id or None on error |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1301 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1302 | NOTE: This function may change depending on the |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1303 | options developers provide for point-to-point |
| 1304 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1305 | """ |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1306 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1307 | cmd = "add-point-intent" |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1308 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1309 | if ethType: |
| 1310 | cmd += " --ethType " + str( ethType ) |
| 1311 | if ethSrc: |
| 1312 | cmd += " --ethSrc " + str( ethSrc ) |
| 1313 | if ethDst: |
| 1314 | cmd += " --ethDst " + str( ethDst ) |
| 1315 | if bandwidth: |
| 1316 | cmd += " --bandwidth " + str( bandwidth ) |
| 1317 | if lambdaAlloc: |
| 1318 | cmd += " --lambda " |
| 1319 | if ipProto: |
| 1320 | cmd += " --ipProto " + str( ipProto ) |
| 1321 | if ipSrc: |
| 1322 | cmd += " --ipSrc " + str( ipSrc ) |
| 1323 | if ipDst: |
| 1324 | cmd += " --ipDst " + str( ipDst ) |
| 1325 | if tcpSrc: |
| 1326 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1327 | if tcpDst: |
| 1328 | cmd += " --tcpDst " + str( tcpDst ) |
| 1329 | if vlanId: |
| 1330 | cmd += " -v " + str( vlanId ) |
| 1331 | if setVlan: |
| 1332 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1333 | if encap: |
| 1334 | cmd += " --encapsulation " + str( encap ) |
alison | da15727 | 2016-12-22 01:13:21 -0800 | [diff] [blame] | 1335 | if protected: |
| 1336 | cmd += " --protect " |
andrewonlab | 289e4b7 | 2014-10-21 21:24:18 -0400 | [diff] [blame] | 1337 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1338 | # Check whether the user appended the port |
| 1339 | # or provided it as an input |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1340 | if "/" in ingressDevice: |
| 1341 | cmd += " " + str( ingressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1342 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1343 | if not portIngress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1344 | main.log.error( "You must specify the ingress port" ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1345 | # TODO: perhaps more meaningful return |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1346 | # Would it make sense to throw an exception and exit |
| 1347 | # the test? |
| 1348 | return None |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1349 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1350 | cmd += " " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1351 | str( ingressDevice ) + "/" +\ |
| 1352 | str( portIngress ) + " " |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1353 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1354 | if "/" in egressDevice: |
| 1355 | cmd += " " + str( egressDevice ) |
andrewonlab | 36af382 | 2014-11-18 17:48:18 -0500 | [diff] [blame] | 1356 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1357 | if not portEgress: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1358 | main.log.error( "You must specify the egress port" ) |
| 1359 | return None |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1360 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1361 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1362 | str( egressDevice ) + "/" +\ |
| 1363 | str( portEgress ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1364 | |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1365 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1366 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1367 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1368 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1369 | if re.search( "Error", handle ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1370 | main.log.error( "Error in adding point-to-point intent" ) |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1371 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1372 | else: |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1373 | # TODO: print out all the options in this message? |
| 1374 | main.log.info( "Point-to-point intent installed between " + |
| 1375 | str( ingressDevice ) + " and " + |
| 1376 | str( egressDevice ) ) |
| 1377 | match = re.search('id=0x([\da-f]+),', handle) |
| 1378 | if match: |
| 1379 | return match.group()[3:-1] |
| 1380 | else: |
| 1381 | main.log.error( "Error, intent ID not found" ) |
| 1382 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1383 | except AssertionError: |
| 1384 | main.log.exception( "" ) |
| 1385 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1386 | except TypeError: |
| 1387 | main.log.exception( self.name + ": Object not as expected" ) |
| 1388 | return None |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1389 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1390 | main.log.error( self.name + ": EOF exception found" ) |
| 1391 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1392 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1393 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1394 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1395 | main.cleanAndExit() |
andrewonlab | 4dbb4d8 | 2014-10-17 18:22:31 -0400 | [diff] [blame] | 1396 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1397 | def addMultipointToSinglepointIntent( |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1398 | self, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1399 | ingressDeviceList, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1400 | egressDevice, |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1401 | portIngressList=None, |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1402 | portEgress="", |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1403 | ethType="", |
| 1404 | ethSrc="", |
| 1405 | ethDst="", |
| 1406 | bandwidth="", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1407 | lambdaAlloc=False, |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1408 | ipProto="", |
| 1409 | ipSrc="", |
| 1410 | ipDst="", |
| 1411 | tcpSrc="", |
| 1412 | tcpDst="", |
| 1413 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1414 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1415 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1416 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1417 | partial=False, |
| 1418 | encap="" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1419 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1420 | Note: |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1421 | This function assumes the format of all ingress devices |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1422 | is same. That is, all ingress devices include port numbers |
| 1423 | with a "/" or all ingress devices could specify device |
| 1424 | ids and port numbers seperately. |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1425 | Required: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1426 | * ingressDeviceList: List of device ids of ingress device |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1427 | ( Atleast 2 ingress devices required in the list ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1428 | * egressDevice: device id of egress device |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1429 | Optional: |
| 1430 | * ethType: specify ethType |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1431 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1432 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1433 | * bandwidth: specify bandwidth capacity of link |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1434 | * lambdaAlloc: if True, intent will allocate lambda |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1435 | for the specified intent |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1436 | * ipProto: specify ip protocol |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1437 | * ipSrc: specify ip source address |
| 1438 | * ipDst: specify ip destination address |
| 1439 | * tcpSrc: specify tcp source port |
| 1440 | * tcpDst: specify tcp destination port |
| 1441 | * setEthSrc: action to Rewrite Source MAC Address |
| 1442 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1443 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1444 | * setVlan: specify VLAN Id treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1445 | * encap: specify a type of encapsulation |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1446 | Description: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1447 | Adds a multipoint-to-singlepoint intent ( uni-directional ) by |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1448 | specifying device id's and optional fields |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1449 | Returns: |
| 1450 | A string of the intent id or None on error |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1451 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1452 | NOTE: This function may change depending on the |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1453 | options developers provide for multipoint-to-singlepoint |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1454 | intent via cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1455 | """ |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1456 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1457 | cmd = "add-multi-to-single-intent" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1458 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1459 | if ethType: |
| 1460 | cmd += " --ethType " + str( ethType ) |
| 1461 | if ethSrc: |
| 1462 | cmd += " --ethSrc " + str( ethSrc ) |
| 1463 | if ethDst: |
| 1464 | cmd += " --ethDst " + str( ethDst ) |
| 1465 | if bandwidth: |
| 1466 | cmd += " --bandwidth " + str( bandwidth ) |
| 1467 | if lambdaAlloc: |
| 1468 | cmd += " --lambda " |
| 1469 | if ipProto: |
| 1470 | cmd += " --ipProto " + str( ipProto ) |
| 1471 | if ipSrc: |
| 1472 | cmd += " --ipSrc " + str( ipSrc ) |
| 1473 | if ipDst: |
| 1474 | cmd += " --ipDst " + str( ipDst ) |
| 1475 | if tcpSrc: |
| 1476 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1477 | if tcpDst: |
| 1478 | cmd += " --tcpDst " + str( tcpDst ) |
| 1479 | if setEthSrc: |
| 1480 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1481 | if setEthDst: |
| 1482 | cmd += " --setEthDst " + str( setEthDst ) |
| 1483 | if vlanId: |
| 1484 | cmd += " -v " + str( vlanId ) |
| 1485 | if setVlan: |
| 1486 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1487 | if partial: |
| 1488 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1489 | if encap: |
| 1490 | cmd += " --encapsulation " + str( encap ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1491 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1492 | # Check whether the user appended the port |
| 1493 | # or provided it as an input |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1494 | |
| 1495 | if portIngressList is None: |
| 1496 | for ingressDevice in ingressDeviceList: |
| 1497 | if "/" in ingressDevice: |
| 1498 | cmd += " " + str( ingressDevice ) |
| 1499 | else: |
| 1500 | main.log.error( "You must specify " + |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 1501 | "the ingress port" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1502 | # TODO: perhaps more meaningful return |
| 1503 | return main.FALSE |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1504 | else: |
Jon Hall | 71ce4e7 | 2015-03-23 14:05:58 -0700 | [diff] [blame] | 1505 | if len( ingressDeviceList ) == len( portIngressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1506 | for ingressDevice, portIngress in zip( ingressDeviceList, |
| 1507 | portIngressList ): |
shahshreya | 70622b1 | 2015-03-19 17:19:00 -0700 | [diff] [blame] | 1508 | cmd += " " + \ |
| 1509 | str( ingressDevice ) + "/" +\ |
| 1510 | str( portIngress ) + " " |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1511 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1512 | main.log.error( "Device list and port list does not " + |
| 1513 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1514 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1515 | if "/" in egressDevice: |
| 1516 | cmd += " " + str( egressDevice ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1517 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1518 | if not portEgress: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1519 | main.log.error( "You must specify " + |
| 1520 | "the egress port" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1521 | return main.FALSE |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1522 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1523 | cmd += " " +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1524 | str( egressDevice ) + "/" +\ |
| 1525 | str( portEgress ) |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1526 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1527 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1528 | assert "Command not found:" not in handle, handle |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1529 | # If error, return error message |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1530 | if re.search( "Error", handle ): |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 1531 | main.log.error( "Error in adding multipoint-to-singlepoint " + |
| 1532 | "intent" ) |
| 1533 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1534 | else: |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1535 | match = re.search('id=0x([\da-f]+),', handle) |
| 1536 | if match: |
| 1537 | return match.group()[3:-1] |
| 1538 | else: |
| 1539 | main.log.error( "Error, intent ID not found" ) |
| 1540 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1541 | except AssertionError: |
| 1542 | main.log.exception( "" ) |
| 1543 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1544 | except TypeError: |
| 1545 | main.log.exception( self.name + ": Object not as expected" ) |
| 1546 | return None |
| 1547 | except pexpect.EOF: |
| 1548 | main.log.error( self.name + ": EOF exception found" ) |
| 1549 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1550 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1551 | except Exception: |
| 1552 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1553 | main.cleanAndExit() |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1554 | |
| 1555 | def addSinglepointToMultipointIntent( |
| 1556 | self, |
| 1557 | ingressDevice, |
| 1558 | egressDeviceList, |
| 1559 | portIngress="", |
| 1560 | portEgressList=None, |
| 1561 | ethType="", |
| 1562 | ethSrc="", |
| 1563 | ethDst="", |
| 1564 | bandwidth="", |
| 1565 | lambdaAlloc=False, |
| 1566 | ipProto="", |
| 1567 | ipSrc="", |
| 1568 | ipDst="", |
| 1569 | tcpSrc="", |
| 1570 | tcpDst="", |
| 1571 | setEthSrc="", |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1572 | setEthDst="", |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1573 | vlanId="", |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1574 | setVlan="", |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1575 | partial=False, |
| 1576 | encap="" ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1577 | """ |
| 1578 | Note: |
| 1579 | This function assumes the format of all egress devices |
| 1580 | is same. That is, all egress devices include port numbers |
| 1581 | with a "/" or all egress devices could specify device |
| 1582 | ids and port numbers seperately. |
| 1583 | Required: |
| 1584 | * EgressDeviceList: List of device ids of egress device |
| 1585 | ( Atleast 2 eress devices required in the list ) |
| 1586 | * ingressDevice: device id of ingress device |
| 1587 | Optional: |
| 1588 | * ethType: specify ethType |
| 1589 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1590 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1591 | * bandwidth: specify bandwidth capacity of link |
| 1592 | * lambdaAlloc: if True, intent will allocate lambda |
| 1593 | for the specified intent |
| 1594 | * ipProto: specify ip protocol |
| 1595 | * ipSrc: specify ip source address |
| 1596 | * ipDst: specify ip destination address |
| 1597 | * tcpSrc: specify tcp source port |
| 1598 | * tcpDst: specify tcp destination port |
| 1599 | * setEthSrc: action to Rewrite Source MAC Address |
| 1600 | * setEthDst: action to Rewrite Destination MAC Address |
Jeremy Songster | 832f9e9 | 2016-05-05 14:30:49 -0700 | [diff] [blame] | 1601 | * vlanId: specify vlan Id |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1602 | * setVlan: specify VLAN ID treatment |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1603 | * encap: specify an encapsulation type |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1604 | Description: |
| 1605 | Adds a singlepoint-to-multipoint intent ( uni-directional ) by |
| 1606 | specifying device id's and optional fields |
| 1607 | Returns: |
| 1608 | A string of the intent id or None on error |
| 1609 | |
| 1610 | NOTE: This function may change depending on the |
| 1611 | options developers provide for singlepoint-to-multipoint |
| 1612 | intent via cli |
| 1613 | """ |
| 1614 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1615 | cmd = "add-single-to-multi-intent" |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1616 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1617 | if ethType: |
| 1618 | cmd += " --ethType " + str( ethType ) |
| 1619 | if ethSrc: |
| 1620 | cmd += " --ethSrc " + str( ethSrc ) |
| 1621 | if ethDst: |
| 1622 | cmd += " --ethDst " + str( ethDst ) |
| 1623 | if bandwidth: |
| 1624 | cmd += " --bandwidth " + str( bandwidth ) |
| 1625 | if lambdaAlloc: |
| 1626 | cmd += " --lambda " |
| 1627 | if ipProto: |
| 1628 | cmd += " --ipProto " + str( ipProto ) |
| 1629 | if ipSrc: |
| 1630 | cmd += " --ipSrc " + str( ipSrc ) |
| 1631 | if ipDst: |
| 1632 | cmd += " --ipDst " + str( ipDst ) |
| 1633 | if tcpSrc: |
| 1634 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1635 | if tcpDst: |
| 1636 | cmd += " --tcpDst " + str( tcpDst ) |
| 1637 | if setEthSrc: |
| 1638 | cmd += " --setEthSrc " + str( setEthSrc ) |
| 1639 | if setEthDst: |
| 1640 | cmd += " --setEthDst " + str( setEthDst ) |
| 1641 | if vlanId: |
| 1642 | cmd += " -v " + str( vlanId ) |
| 1643 | if setVlan: |
| 1644 | cmd += " --setVlan " + str( setVlan ) |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 1645 | if partial: |
| 1646 | cmd += " --partial" |
Jeremy Songster | c032f16 | 2016-08-04 17:14:49 -0700 | [diff] [blame] | 1647 | if encap: |
| 1648 | cmd += " --encapsulation " + str( encap ) |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1649 | |
| 1650 | # Check whether the user appended the port |
| 1651 | # or provided it as an input |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1652 | |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1653 | if "/" in ingressDevice: |
| 1654 | cmd += " " + str( ingressDevice ) |
| 1655 | else: |
| 1656 | if not portIngress: |
| 1657 | main.log.error( "You must specify " + |
| 1658 | "the Ingress port" ) |
| 1659 | return main.FALSE |
| 1660 | |
| 1661 | cmd += " " +\ |
| 1662 | str( ingressDevice ) + "/" +\ |
| 1663 | str( portIngress ) |
| 1664 | |
| 1665 | if portEgressList is None: |
| 1666 | for egressDevice in egressDeviceList: |
| 1667 | if "/" in egressDevice: |
| 1668 | cmd += " " + str( egressDevice ) |
| 1669 | else: |
| 1670 | main.log.error( "You must specify " + |
| 1671 | "the egress port" ) |
| 1672 | # TODO: perhaps more meaningful return |
| 1673 | return main.FALSE |
| 1674 | else: |
| 1675 | if len( egressDeviceList ) == len( portEgressList ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1676 | for egressDevice, portEgress in zip( egressDeviceList, |
| 1677 | portEgressList ): |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1678 | cmd += " " + \ |
| 1679 | str( egressDevice ) + "/" +\ |
| 1680 | str( portEgress ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1681 | else: |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 1682 | main.log.error( "Device list and port list does not " + |
| 1683 | "have the same length" ) |
kelvin-onlab | 3814381 | 2015-04-01 15:03:01 -0700 | [diff] [blame] | 1684 | return main.FALSE |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1685 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1686 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1687 | assert "Command not found:" not in handle, handle |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1688 | # If error, return error message |
| 1689 | if re.search( "Error", handle ): |
| 1690 | main.log.error( "Error in adding singlepoint-to-multipoint " + |
| 1691 | "intent" ) |
shahshreya | c2f9707 | 2015-03-19 17:04:29 -0700 | [diff] [blame] | 1692 | return None |
kelvin-onlab | b940821 | 2015-04-01 13:34:04 -0700 | [diff] [blame] | 1693 | else: |
| 1694 | match = re.search('id=0x([\da-f]+),', handle) |
| 1695 | if match: |
| 1696 | return match.group()[3:-1] |
| 1697 | else: |
| 1698 | main.log.error( "Error, intent ID not found" ) |
| 1699 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1700 | except AssertionError: |
| 1701 | main.log.exception( "" ) |
| 1702 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1703 | except TypeError: |
| 1704 | main.log.exception( self.name + ": Object not as expected" ) |
| 1705 | return None |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1706 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1707 | main.log.error( self.name + ": EOF exception found" ) |
| 1708 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1709 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1710 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1711 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1712 | main.cleanAndExit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1713 | |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1714 | def addMplsIntent( |
| 1715 | self, |
| 1716 | ingressDevice, |
| 1717 | egressDevice, |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1718 | ingressPort="", |
| 1719 | egressPort="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1720 | ethType="", |
| 1721 | ethSrc="", |
| 1722 | ethDst="", |
| 1723 | bandwidth="", |
| 1724 | lambdaAlloc=False, |
| 1725 | ipProto="", |
| 1726 | ipSrc="", |
| 1727 | ipDst="", |
| 1728 | tcpSrc="", |
| 1729 | tcpDst="", |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1730 | ingressLabel="", |
Hari Krishna | dfff667 | 2015-04-13 17:53:27 -0700 | [diff] [blame] | 1731 | egressLabel="", |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1732 | priority=""): |
| 1733 | """ |
| 1734 | Required: |
| 1735 | * ingressDevice: device id of ingress device |
| 1736 | * egressDevice: device id of egress device |
| 1737 | Optional: |
| 1738 | * ethType: specify ethType |
| 1739 | * ethSrc: specify ethSrc ( i.e. src mac addr ) |
| 1740 | * ethDst: specify ethDst ( i.e. dst mac addr ) |
| 1741 | * bandwidth: specify bandwidth capacity of link |
| 1742 | * lambdaAlloc: if True, intent will allocate lambda |
| 1743 | for the specified intent |
| 1744 | * ipProto: specify ip protocol |
| 1745 | * ipSrc: specify ip source address |
| 1746 | * ipDst: specify ip destination address |
| 1747 | * tcpSrc: specify tcp source port |
| 1748 | * tcpDst: specify tcp destination port |
| 1749 | * ingressLabel: Ingress MPLS label |
| 1750 | * egressLabel: Egress MPLS label |
| 1751 | Description: |
| 1752 | Adds MPLS intent by |
| 1753 | specifying device id's and optional fields |
| 1754 | Returns: |
| 1755 | A string of the intent id or None on error |
| 1756 | |
| 1757 | NOTE: This function may change depending on the |
| 1758 | options developers provide for MPLS |
| 1759 | intent via cli |
| 1760 | """ |
| 1761 | try: |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1762 | cmd = "add-mpls-intent" |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1763 | |
Jeremy Songster | ff55367 | 2016-05-12 17:06:23 -0700 | [diff] [blame] | 1764 | if ethType: |
| 1765 | cmd += " --ethType " + str( ethType ) |
| 1766 | if ethSrc: |
| 1767 | cmd += " --ethSrc " + str( ethSrc ) |
| 1768 | if ethDst: |
| 1769 | cmd += " --ethDst " + str( ethDst ) |
| 1770 | if bandwidth: |
| 1771 | cmd += " --bandwidth " + str( bandwidth ) |
| 1772 | if lambdaAlloc: |
| 1773 | cmd += " --lambda " |
| 1774 | if ipProto: |
| 1775 | cmd += " --ipProto " + str( ipProto ) |
| 1776 | if ipSrc: |
| 1777 | cmd += " --ipSrc " + str( ipSrc ) |
| 1778 | if ipDst: |
| 1779 | cmd += " --ipDst " + str( ipDst ) |
| 1780 | if tcpSrc: |
| 1781 | cmd += " --tcpSrc " + str( tcpSrc ) |
| 1782 | if tcpDst: |
| 1783 | cmd += " --tcpDst " + str( tcpDst ) |
| 1784 | if ingressLabel: |
| 1785 | cmd += " --ingressLabel " + str( ingressLabel ) |
| 1786 | if egressLabel: |
| 1787 | cmd += " --egressLabel " + str( egressLabel ) |
| 1788 | if priority: |
| 1789 | cmd += " --priority " + str( priority ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1790 | |
| 1791 | # Check whether the user appended the port |
| 1792 | # or provided it as an input |
| 1793 | if "/" in ingressDevice: |
| 1794 | cmd += " " + str( ingressDevice ) |
| 1795 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1796 | if not ingressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1797 | main.log.error( "You must specify the ingress port" ) |
| 1798 | return None |
| 1799 | |
| 1800 | cmd += " " + \ |
| 1801 | str( ingressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1802 | str( ingressPort ) + " " |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1803 | |
| 1804 | if "/" in egressDevice: |
| 1805 | cmd += " " + str( egressDevice ) |
| 1806 | else: |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1807 | if not egressPort: |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1808 | main.log.error( "You must specify the egress port" ) |
| 1809 | return None |
| 1810 | |
| 1811 | cmd += " " +\ |
| 1812 | str( egressDevice ) + "/" +\ |
Hari Krishna | 87a17f1 | 2015-04-13 17:42:23 -0700 | [diff] [blame] | 1813 | str( egressPort ) |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1814 | |
| 1815 | handle = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1816 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1817 | assert "Command not found:" not in handle, handle |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1818 | # If error, return error message |
| 1819 | if re.search( "Error", handle ): |
| 1820 | main.log.error( "Error in adding mpls intent" ) |
| 1821 | return None |
| 1822 | else: |
| 1823 | # TODO: print out all the options in this message? |
| 1824 | main.log.info( "MPLS intent installed between " + |
| 1825 | str( ingressDevice ) + " and " + |
| 1826 | str( egressDevice ) ) |
| 1827 | match = re.search('id=0x([\da-f]+),', handle) |
| 1828 | if match: |
| 1829 | return match.group()[3:-1] |
| 1830 | else: |
| 1831 | main.log.error( "Error, intent ID not found" ) |
| 1832 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1833 | except AssertionError: |
| 1834 | main.log.exception( "" ) |
| 1835 | return None |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1836 | except TypeError: |
| 1837 | main.log.exception( self.name + ": Object not as expected" ) |
| 1838 | return None |
| 1839 | except pexpect.EOF: |
| 1840 | main.log.error( self.name + ": EOF exception found" ) |
| 1841 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1842 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1843 | except Exception: |
| 1844 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1845 | main.cleanAndExit() |
Hari Krishna | 9e23260 | 2015-04-13 17:29:08 -0700 | [diff] [blame] | 1846 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1847 | def removeIntent( self, intentId, app='org.onosproject.cli', |
| 1848 | purge=False, sync=False ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1849 | """ |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1850 | Remove intent for specified application id and intent id |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1851 | Optional args:- |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1852 | -s or --sync: Waits for the removal before returning |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 1853 | -p or --purge: Purge the intent from the store after removal |
| 1854 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1855 | Returns: |
Jon Hall | 6509dbf | 2016-06-21 17:01:17 -0700 | [diff] [blame] | 1856 | main.FALSE on error and |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1857 | cli output otherwise |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1858 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1859 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1860 | cmdStr = "remove-intent" |
shahshreya | 1c818fc | 2015-02-26 13:44:08 -0800 | [diff] [blame] | 1861 | if purge: |
| 1862 | cmdStr += " -p" |
| 1863 | if sync: |
| 1864 | cmdStr += " -s" |
| 1865 | |
| 1866 | cmdStr += " " + app + " " + str( intentId ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1867 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1868 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1869 | assert "Command not found:" not in handle, handle |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1870 | if re.search( "Error", handle ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1871 | main.log.error( "Error in removing intent" ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1872 | return main.FALSE |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1873 | else: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 1874 | # TODO: Should this be main.TRUE |
| 1875 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1876 | except AssertionError: |
| 1877 | main.log.exception( "" ) |
| 1878 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1879 | except TypeError: |
| 1880 | main.log.exception( self.name + ": Object not as expected" ) |
| 1881 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1882 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1883 | main.log.error( self.name + ": EOF exception found" ) |
| 1884 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1885 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1886 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1887 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1888 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 1889 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1890 | def removeAllIntents( self, purge=False, sync=False, app='org.onosproject.cli', timeout=30 ): |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1891 | """ |
| 1892 | Description: |
| 1893 | Remove all the intents |
| 1894 | Optional args:- |
| 1895 | -s or --sync: Waits for the removal before returning |
| 1896 | -p or --purge: Purge the intent from the store after removal |
| 1897 | Returns: |
| 1898 | Returns main.TRUE if all intents are removed, otherwise returns |
| 1899 | main.FALSE; Returns None for exception |
| 1900 | """ |
| 1901 | try: |
| 1902 | cmdStr = "remove-intent" |
| 1903 | if purge: |
| 1904 | cmdStr += " -p" |
| 1905 | if sync: |
| 1906 | cmdStr += " -s" |
| 1907 | |
| 1908 | cmdStr += " " + app |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 1909 | handle = self.sendline( cmdStr, timeout=timeout ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1910 | assert handle is not None, "Error in sendline" |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1911 | assert "Command not found:" not in handle, handle |
| 1912 | if re.search( "Error", handle ): |
| 1913 | main.log.error( "Error in removing intent" ) |
| 1914 | return main.FALSE |
| 1915 | else: |
| 1916 | return main.TRUE |
| 1917 | except AssertionError: |
| 1918 | main.log.exception( "" ) |
| 1919 | return None |
| 1920 | except TypeError: |
| 1921 | main.log.exception( self.name + ": Object not as expected" ) |
| 1922 | return None |
| 1923 | except pexpect.EOF: |
| 1924 | main.log.error( self.name + ": EOF exception found" ) |
| 1925 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1926 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1927 | except Exception: |
| 1928 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1929 | main.cleanAndExit() |
Jeremy | 42df2e7 | 2016-02-23 16:37:46 -0800 | [diff] [blame] | 1930 | |
Hari Krishna | acabd5a | 2015-07-01 17:10:19 -0700 | [diff] [blame] | 1931 | def purgeWithdrawnIntents( self ): |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1932 | """ |
| 1933 | Purges all WITHDRAWN Intents |
| 1934 | """ |
| 1935 | try: |
| 1936 | cmdStr = "purge-intents" |
| 1937 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1938 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1939 | assert "Command not found:" not in handle, handle |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1940 | if re.search( "Error", handle ): |
| 1941 | main.log.error( "Error in purging intents" ) |
| 1942 | return main.FALSE |
| 1943 | else: |
| 1944 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1945 | except AssertionError: |
| 1946 | main.log.exception( "" ) |
| 1947 | return None |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1948 | except TypeError: |
| 1949 | main.log.exception( self.name + ": Object not as expected" ) |
| 1950 | return None |
| 1951 | except pexpect.EOF: |
| 1952 | main.log.error( self.name + ": EOF exception found" ) |
| 1953 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1954 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1955 | except Exception: |
| 1956 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1957 | main.cleanAndExit() |
Hari Krishna | 0ce0e15 | 2015-06-23 09:55:29 -0700 | [diff] [blame] | 1958 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1959 | def routes( self, jsonFormat=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1960 | """ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 1961 | NOTE: This method should be used after installing application: |
| 1962 | onos-app-sdnip |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1963 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1964 | * jsonFormat: enable output formatting in json |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1965 | Description: |
| 1966 | Obtain all routes in the system |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1967 | """ |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1968 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1969 | cmdStr = "routes" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1970 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 1971 | cmdStr += " -j" |
| 1972 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 1973 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1974 | assert "Command not found:" not in handle, handle |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1975 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 1976 | except AssertionError: |
| 1977 | main.log.exception( "" ) |
| 1978 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1979 | except TypeError: |
| 1980 | main.log.exception( self.name + ": Object not as expected" ) |
| 1981 | return None |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1982 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 1983 | main.log.error( self.name + ": EOF exception found" ) |
| 1984 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1985 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1986 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 1987 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 1988 | main.cleanAndExit() |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 1989 | |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 1990 | def ipv4RouteNumber( self ): |
| 1991 | """ |
| 1992 | NOTE: This method should be used after installing application: |
| 1993 | onos-app-sdnip |
| 1994 | Description: |
| 1995 | Obtain the total IPv4 routes number in the system |
| 1996 | """ |
| 1997 | try: |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 1998 | cmdStr = "routes -j" |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 1999 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2000 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2001 | assert "Command not found:" not in handle, handle |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2002 | jsonResult = json.loads( handle ) |
Pratik Parab | 5796357 | 2017-05-09 11:37:54 -0700 | [diff] [blame] | 2003 | return len(jsonResult['routes4']) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2004 | except AssertionError: |
| 2005 | main.log.exception( "" ) |
| 2006 | return None |
| 2007 | except ( TypeError, ValueError ): |
| 2008 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2009 | return None |
| 2010 | except pexpect.EOF: |
| 2011 | main.log.error( self.name + ": EOF exception found" ) |
| 2012 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2013 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2014 | except Exception: |
| 2015 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2016 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2017 | |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2018 | #=============Function to check Bandwidth allocation======== |
| 2019 | def allocations( self, jsonFormat = True, dollarSign = True ): |
| 2020 | """ |
| 2021 | Description: |
| 2022 | Obtain Bandwidth Allocation Information from ONOS cli. |
| 2023 | """ |
| 2024 | try: |
| 2025 | cmdStr = "allocations" |
| 2026 | if jsonFormat: |
| 2027 | cmdStr += " -j" |
| 2028 | handle = self.sendline( cmdStr, timeout=300, dollarSign=True ) |
| 2029 | assert handle is not None, "Error in sendline" |
| 2030 | assert "Command not found:" not in handle, handle |
| 2031 | return handle |
| 2032 | except AssertionError: |
| 2033 | main.log.exception( "" ) |
| 2034 | return None |
| 2035 | except ( TypeError, ValueError ): |
| 2036 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
| 2037 | return None |
| 2038 | except pexpect.EOF: |
| 2039 | main.log.error( self.name + ": EOF exception found" ) |
| 2040 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2041 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2042 | except Exception: |
| 2043 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2044 | main.cleanAndExit() |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2045 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2046 | def intents( self, jsonFormat = True, summary = False, **intentargs): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2047 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2048 | Description: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2049 | Obtain intents from the ONOS cli. |
| 2050 | Optional: |
| 2051 | * jsonFormat: Enable output formatting in json, default to True |
| 2052 | * summary: Whether only output the intent summary, defaults to False |
| 2053 | * type: Only output a certain type of intent. This options is valid |
| 2054 | only when jsonFormat is True and summary is True. |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2055 | """ |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2056 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2057 | cmdStr = "intents" |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2058 | if summary: |
| 2059 | cmdStr += " -s" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2060 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2061 | cmdStr += " -j" |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2062 | handle = self.sendline( cmdStr, timeout=300 ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2063 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2064 | assert "Command not found:" not in handle, handle |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2065 | args = utilities.parse_args( [ "TYPE" ], **intentargs ) |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2066 | if "TYPE" in args.keys(): |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2067 | intentType = args[ "TYPE" ] |
acsmars | 5b5fbaf | 2015-09-18 10:38:20 -0700 | [diff] [blame] | 2068 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2069 | intentType = "" |
| 2070 | # IF we want the summary of a specific intent type |
| 2071 | if jsonFormat and summary and ( intentType != "" ): |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2072 | jsonResult = json.loads( handle ) |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2073 | if intentType in jsonResult.keys(): |
| 2074 | return jsonResult[ intentType ] |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2075 | else: |
Jon Hall | ff566d5 | 2016-01-15 14:45:36 -0800 | [diff] [blame] | 2076 | main.log.error( "unknown TYPE, returning all types of intents" ) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 2077 | return handle |
| 2078 | else: |
| 2079 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2080 | except AssertionError: |
| 2081 | main.log.exception( "" ) |
| 2082 | return None |
| 2083 | except ( TypeError, ValueError ): |
| 2084 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, handle ) ) |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2085 | return None |
| 2086 | except pexpect.EOF: |
| 2087 | main.log.error( self.name + ": EOF exception found" ) |
| 2088 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2089 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2090 | except Exception: |
| 2091 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2092 | main.cleanAndExit() |
pingping-lin | 54b0337 | 2015-08-13 14:43:10 -0700 | [diff] [blame] | 2093 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2094 | def getIntentState(self, intentsId, intentsJson=None): |
| 2095 | """ |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2096 | Description: |
| 2097 | Gets intent state. Accepts a single intent ID (string type) or a |
| 2098 | list of intent IDs. |
| 2099 | Parameters: |
| 2100 | intentsId: intent ID, both string type and list type are acceptable |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2101 | intentsJson: parsed json object from the onos:intents api |
You Wang | fdcbfc4 | 2016-05-16 12:16:53 -0700 | [diff] [blame] | 2102 | Returns: |
| 2103 | Returns the state (string type) of the ID if a single intent ID is |
| 2104 | accepted. |
| 2105 | Returns a list of dictionaries if a list of intent IDs is accepted, |
| 2106 | and each dictionary maps 'id' to the Intent ID and 'state' to |
| 2107 | corresponding intent state. |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2108 | """ |
Shreya Chowdhary | 6fbb96c | 2017-05-02 16:20:19 -0700 | [diff] [blame] | 2109 | |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2110 | try: |
| 2111 | state = "State is Undefined" |
| 2112 | if not intentsJson: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2113 | rawJson = self.intents() |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2114 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2115 | rawJson = intentsJson |
| 2116 | parsedIntentsJson = json.loads( rawJson ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2117 | if isinstance( intentsId, types.StringType ): |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2118 | for intent in parsedIntentsJson: |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2119 | if intentsId == intent[ 'id' ]: |
| 2120 | state = intent[ 'state' ] |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2121 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2122 | main.log.info( "Cannot find intent ID" + str( intentsId ) + |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2123 | " in the list" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2124 | return state |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2125 | elif isinstance( intentsId, types.ListType ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2126 | dictList = [] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2127 | for i in xrange( len( intentsId ) ): |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2128 | stateDict = {} |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2129 | for intent in parsedIntentsJson: |
| 2130 | if intentsId[ i ] == intent[ 'id' ]: |
| 2131 | stateDict[ 'state' ] = intent[ 'state' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2132 | stateDict[ 'id' ] = intentsId[ i ] |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2133 | dictList.append( stateDict ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2134 | break |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2135 | if len( intentsId ) != len( dictList ): |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2136 | main.log.warn( "Could not find all intents in ONOS output" ) |
| 2137 | main.log.debug( "expected ids: {} \n ONOS intents: {}".format( intentsId, parsedIntentsJson ) ) |
kelvin-onlab | 07dbd01 | 2015-03-04 16:29:39 -0800 | [diff] [blame] | 2138 | return dictList |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2139 | else: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2140 | main.log.info( "Invalid type for intentsId argument" ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2141 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2142 | except ( TypeError, ValueError ): |
| 2143 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2144 | return None |
| 2145 | except pexpect.EOF: |
| 2146 | main.log.error( self.name + ": EOF exception found" ) |
| 2147 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2148 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2149 | except Exception: |
kelvin-onlab | 54400a9 | 2015-02-26 18:05:51 -0800 | [diff] [blame] | 2150 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2151 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 2152 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2153 | def checkIntentState( self, intentsId, expectedState='INSTALLED' ): |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2154 | """ |
| 2155 | Description: |
| 2156 | Check intents state |
| 2157 | Required: |
| 2158 | intentsId - List of intents ID to be checked |
| 2159 | Optional: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2160 | expectedState - Check the expected state(s) of each intents |
| 2161 | state in the list. |
| 2162 | *NOTE: You can pass in a list of expected state, |
| 2163 | Eg: expectedState = [ 'INSTALLED' , 'INSTALLING' ] |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2164 | Return: |
Jon Hall | 5315808 | 2017-05-18 11:17:00 -0700 | [diff] [blame] | 2165 | Returns main.TRUE only if all intent are the same as expected states, |
| 2166 | otherwise returns main.FALSE. |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2167 | """ |
| 2168 | try: |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2169 | returnValue = main.TRUE |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2170 | # Generating a dictionary: intent id as a key and state as value |
Devin Lim | 752dd7b | 2017-06-27 14:40:03 -0700 | [diff] [blame] | 2171 | |
| 2172 | #intentsDict = self.getIntentState( intentsId ) |
| 2173 | intentsDict = [] |
| 2174 | for intent in json.loads( self.intents() ): |
| 2175 | if isinstance ( intentsId, types.StringType) \ |
| 2176 | and intent.get('id') == intentsId: |
| 2177 | intentsDict.append(intent) |
| 2178 | elif isinstance ( intentsId, types.ListType ) \ |
| 2179 | and any( intent.get( 'id' ) == ids for ids in intentsId ): |
| 2180 | intentsDict.append(intent) |
| 2181 | |
| 2182 | if not intentsDict: |
Jon Hall | ae04e62 | 2016-01-27 10:38:05 -0800 | [diff] [blame] | 2183 | main.log.info( self.name + ": There is something wrong " + |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2184 | "getting intents state" ) |
| 2185 | return main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2186 | |
| 2187 | if isinstance( expectedState, types.StringType ): |
| 2188 | for intents in intentsDict: |
| 2189 | if intents.get( 'state' ) != expectedState: |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2190 | main.log.debug( self.name + " : Intent ID - " + |
| 2191 | intents.get( 'id' ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2192 | " actual state = " + |
| 2193 | intents.get( 'state' ) |
| 2194 | + " does not equal expected state = " |
| 2195 | + expectedState ) |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 2196 | returnValue = main.FALSE |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2197 | elif isinstance( expectedState, types.ListType ): |
| 2198 | for intents in intentsDict: |
| 2199 | if not any( state == intents.get( 'state' ) for state in |
| 2200 | expectedState ): |
| 2201 | main.log.debug( self.name + " : Intent ID - " + |
| 2202 | intents.get( 'id' ) + |
| 2203 | " actual state = " + |
| 2204 | intents.get( 'state' ) + |
| 2205 | " does not equal expected states = " |
| 2206 | + str( expectedState ) ) |
| 2207 | returnValue = main.FALSE |
| 2208 | |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2209 | if returnValue == main.TRUE: |
| 2210 | main.log.info( self.name + ": All " + |
| 2211 | str( len( intentsDict ) ) + |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 2212 | " intents are in " + str( expectedState ) + |
| 2213 | " state" ) |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2214 | return returnValue |
| 2215 | except TypeError: |
| 2216 | main.log.exception( self.name + ": Object not as expected" ) |
| 2217 | return None |
| 2218 | except pexpect.EOF: |
| 2219 | main.log.error( self.name + ": EOF exception found" ) |
| 2220 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2221 | main.cleanAndExit() |
kelvin-onlab | c2dcd3f | 2015-04-09 16:40:02 -0700 | [diff] [blame] | 2222 | except Exception: |
| 2223 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2224 | main.cleanAndExit() |
andrewonlab | e674534 | 2014-10-17 14:29:13 -0400 | [diff] [blame] | 2225 | |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2226 | def compareBandwidthAllocations( self, expectedAllocations ): |
| 2227 | """ |
| 2228 | Description: |
| 2229 | Compare the allocated bandwidth with the given allocations |
| 2230 | Required: |
| 2231 | expectedAllocations - The expected ONOS output of the allocations command |
| 2232 | Return: |
| 2233 | Returns main.TRUE only if all intent are the same as expected states, |
| 2234 | otherwise returns main.FALSE. |
| 2235 | """ |
| 2236 | # FIXME: Convert these string comparisons to object comparisons |
| 2237 | try: |
| 2238 | returnValue = main.TRUE |
| 2239 | bandwidthFailed = False |
| 2240 | rawAlloc = self.allocations() |
| 2241 | expectedFormat = StringIO( expectedAllocations ) |
| 2242 | ONOSOutput = StringIO( rawAlloc ) |
| 2243 | main.log.debug( "ONOSOutput: {}\nexpected output: {}".format( str( ONOSOutput ), |
| 2244 | str( expectedFormat ) ) ) |
| 2245 | |
| 2246 | for actual, expected in izip( ONOSOutput, expectedFormat ): |
| 2247 | actual = actual.rstrip() |
| 2248 | expected = expected.rstrip() |
| 2249 | main.log.debug( "Expect: {}\nactual: {}".format( expected, actual ) ) |
| 2250 | if actual != expected and 'allocated' in actual and 'allocated' in expected: |
| 2251 | marker1 = actual.find('allocated') |
| 2252 | m1 = actual[:marker1] |
| 2253 | marker2 = expected.find('allocated') |
| 2254 | m2 = expected[:marker2] |
| 2255 | if m1 != m2: |
| 2256 | bandwidthFailed = True |
| 2257 | elif actual != expected and 'allocated' not in actual and 'allocated' not in expected: |
| 2258 | bandwidthFailed = True |
| 2259 | expectedFormat.close() |
| 2260 | ONOSOutput.close() |
| 2261 | |
| 2262 | if bandwidthFailed: |
| 2263 | main.log.error("Bandwidth not allocated correctly using Intents!!") |
| 2264 | returnValue = main.FALSE |
| 2265 | return returnValue |
| 2266 | except TypeError: |
| 2267 | main.log.exception( self.name + ": Object not as expected" ) |
| 2268 | return None |
| 2269 | except pexpect.EOF: |
| 2270 | main.log.error( self.name + ": EOF exception found" ) |
| 2271 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2272 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2273 | except Exception: |
| 2274 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2275 | main.cleanAndExit() |
Jon Hall | f539eb9 | 2017-05-22 17:18:42 -0700 | [diff] [blame] | 2276 | |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2277 | def compareIntent( self, intentDict ): |
| 2278 | """ |
| 2279 | Description: |
| 2280 | Compare the intent ids and states provided in the argument with all intents in ONOS |
| 2281 | Return: |
| 2282 | Returns main.TRUE if the two sets of intents match exactly, otherwise main.FALSE |
| 2283 | Arguments: |
| 2284 | intentDict: a dictionary which maps intent ids to intent states |
| 2285 | """ |
| 2286 | try: |
| 2287 | intentsRaw = self.intents() |
| 2288 | intentsJson = json.loads( intentsRaw ) |
| 2289 | intentDictONOS = {} |
| 2290 | for intent in intentsJson: |
| 2291 | intentDictONOS[ intent[ 'id' ] ] = intent[ 'state' ] |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2292 | returnValue = main.TRUE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2293 | if len( intentDict ) != len( intentDictONOS ): |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2294 | 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] | 2295 | str( len( intentDict ) ) + " expected and " + |
| 2296 | str( len( intentDictONOS ) ) + " actual" ) |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2297 | returnValue = main.FALSE |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2298 | for intentID in intentDict.keys(): |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2299 | if intentID not in intentDictONOS.keys(): |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2300 | main.log.debug( self.name + ": intent ID - " + intentID + " is not in ONOS" ) |
| 2301 | returnValue = main.FALSE |
You Wang | 58d0445 | 2016-09-21 15:13:05 -0700 | [diff] [blame] | 2302 | else: |
| 2303 | if intentDict[ intentID ] != intentDictONOS[ intentID ]: |
| 2304 | main.log.debug( self.name + ": intent ID - " + intentID + |
| 2305 | " expected state is " + intentDict[ intentID ] + |
| 2306 | " but actual state is " + intentDictONOS[ intentID ] ) |
| 2307 | returnValue = main.FALSE |
| 2308 | intentDictONOS.pop( intentID ) |
| 2309 | if len( intentDictONOS ) > 0: |
| 2310 | returnValue = main.FALSE |
| 2311 | for intentID in intentDictONOS.keys(): |
| 2312 | 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] | 2313 | if returnValue == main.TRUE: |
| 2314 | main.log.info( self.name + ": all intent IDs and states match that in ONOS" ) |
| 2315 | return returnValue |
You Wang | 1be9a51 | 2016-05-26 16:54:17 -0700 | [diff] [blame] | 2316 | except KeyError: |
| 2317 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2318 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2319 | except ( TypeError, ValueError ): |
| 2320 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intentsRaw ) ) |
You Wang | 8556037 | 2016-05-18 10:44:33 -0700 | [diff] [blame] | 2321 | return main.ERROR |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2322 | except pexpect.EOF: |
| 2323 | main.log.error( self.name + ": EOF exception found" ) |
| 2324 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2325 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2326 | except Exception: |
| 2327 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2328 | main.cleanAndExit() |
You Wang | 66518af | 2016-05-16 15:32:59 -0700 | [diff] [blame] | 2329 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2330 | def checkIntentSummary( self, timeout=60, noExit=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2331 | """ |
| 2332 | Description: |
| 2333 | Check the number of installed intents. |
| 2334 | Optional: |
| 2335 | timeout - the timeout for pexcept |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2336 | noExit - If noExit, TestON will not exit if any except. |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2337 | Return: |
| 2338 | Returns main.TRUE only if the number of all installed intents are the same as total intents number |
| 2339 | , otherwise, returns main.FALSE. |
| 2340 | """ |
| 2341 | |
| 2342 | try: |
| 2343 | cmd = "intents -s -j" |
| 2344 | |
| 2345 | # Check response if something wrong |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2346 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2347 | if response is None: |
YPZhang | 0584d43 | 2016-06-21 15:20:13 -0700 | [diff] [blame] | 2348 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2349 | response = json.loads( response ) |
| 2350 | |
| 2351 | # get total and installed number, see if they are match |
| 2352 | allState = response.get( 'all' ) |
| 2353 | if allState.get('total') == allState.get('installed'): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2354 | main.log.info( 'Total Intents: {} Installed Intents: {}'.format( allState.get('total'), allState.get('installed') ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2355 | return main.TRUE |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2356 | main.log.info( 'Verified Intents failed Excepte intetnes: {} installed intents: {}'.format( allState.get('total'), allState.get('installed') ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2357 | return main.FALSE |
| 2358 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2359 | except ( TypeError, ValueError ): |
| 2360 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2361 | return None |
| 2362 | except pexpect.EOF: |
| 2363 | main.log.error( self.name + ": EOF exception found" ) |
| 2364 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2365 | if noExit: |
| 2366 | return main.FALSE |
| 2367 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2368 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2369 | except pexpect.TIMEOUT: |
| 2370 | main.log.error( self.name + ": ONOS timeout" ) |
| 2371 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2372 | except Exception: |
| 2373 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2374 | if noExit: |
| 2375 | return main.FALSE |
| 2376 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2377 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2378 | |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2379 | def flows( self, state="", jsonFormat=True, timeout=60, noExit=False, noCore=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2380 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2381 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2382 | * jsonFormat: enable output formatting in json |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2383 | * noCore: suppress core flows |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2384 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2385 | Obtain flows currently installed |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2386 | """ |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2387 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2388 | cmdStr = "flows" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2389 | if jsonFormat: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2390 | cmdStr += " -j " |
Jeremy Songster | 306ed7a | 2016-07-19 10:59:07 -0700 | [diff] [blame] | 2391 | if noCore: |
| 2392 | cmdStr += " -n " |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2393 | cmdStr += state |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2394 | handle = self.sendline( cmdStr, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2395 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2396 | assert "Command not found:" not in handle, handle |
| 2397 | if re.search( "Error:", handle ): |
| 2398 | main.log.error( self.name + ": flows() response: " + |
| 2399 | str( handle ) ) |
| 2400 | return handle |
| 2401 | except AssertionError: |
| 2402 | main.log.exception( "" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2403 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2404 | except TypeError: |
| 2405 | main.log.exception( self.name + ": Object not as expected" ) |
| 2406 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2407 | except pexpect.TIMEOUT: |
| 2408 | main.log.error( self.name + ": ONOS timeout" ) |
| 2409 | return None |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2410 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2411 | main.log.error( self.name + ": EOF exception found" ) |
| 2412 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2413 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2414 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2415 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2416 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2417 | |
Flavio Castro | d2ffffa | 2016-04-26 15:56:56 -0700 | [diff] [blame] | 2418 | def checkFlowCount(self, min=0, timeout=60 ): |
Flavio Castro | a1286fe | 2016-07-25 14:48:51 -0700 | [diff] [blame] | 2419 | count = self.getTotalFlowsNum( timeout=timeout ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2420 | count = int( count ) if count else 0 |
| 2421 | return count if ( count > min ) else False |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2422 | |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2423 | def checkFlowsState( self, isPENDING=True, timeout=60, noExit=False ): |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2424 | """ |
| 2425 | Description: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2426 | Check the if all the current flows are in ADDED state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2427 | We check PENDING_ADD, PENDING_REMOVE, REMOVED, and FAILED flows, |
| 2428 | if the count of those states is 0, which means all current flows |
| 2429 | are in ADDED state, and return main.TRUE otherwise return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2430 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2431 | * isPENDING: whether the PENDING_ADD is also a correct status |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2432 | Return: |
| 2433 | returnValue - Returns main.TRUE only if all flows are in |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2434 | ADDED state or PENDING_ADD if the isPENDING |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2435 | parameter is set true, return main.FALSE otherwise. |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2436 | """ |
| 2437 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2438 | states = ["PENDING_ADD", "PENDING_REMOVE", "REMOVED", "FAILED"] |
| 2439 | checkedStates = [] |
| 2440 | statesCount = [0, 0, 0, 0] |
| 2441 | for s in states: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2442 | rawFlows = self.flows( state=s, timeout = timeout ) |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2443 | if rawFlows: |
| 2444 | # if we didn't get flows or flows function return None, we should return |
| 2445 | # main.Flase |
| 2446 | checkedStates.append( json.loads( rawFlows ) ) |
| 2447 | else: |
| 2448 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2449 | for i in range( len( states ) ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2450 | for c in checkedStates[i]: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2451 | try: |
| 2452 | statesCount[i] += int( c.get( "flowCount" ) ) |
| 2453 | except TypeError: |
| 2454 | main.log.exception( "Json object not as expected" ) |
| 2455 | main.log.info( states[i] + " flows: " + str( statesCount[i] ) ) |
kelvin-onlab | f2ec6e0 | 2015-05-27 14:15:28 -0700 | [diff] [blame] | 2456 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2457 | # We want to count PENDING_ADD if isPENDING is true |
| 2458 | if isPENDING: |
| 2459 | if statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2460 | return main.FALSE |
pingping-lin | bab7f8a | 2015-09-21 17:33:36 -0700 | [diff] [blame] | 2461 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2462 | if statesCount[0] + statesCount[1] + statesCount[2] + statesCount[3] > 0: |
| 2463 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2464 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2465 | except ( TypeError, ValueError ): |
| 2466 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawFlows ) ) |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2467 | return None |
Jeremy Songster | 9385d41 | 2016-06-02 17:57:36 -0700 | [diff] [blame] | 2468 | |
YPZhang | 240842b | 2016-05-17 12:00:50 -0700 | [diff] [blame] | 2469 | except AssertionError: |
| 2470 | main.log.exception( "" ) |
| 2471 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2472 | except pexpect.TIMEOUT: |
| 2473 | main.log.error( self.name + ": ONOS timeout" ) |
| 2474 | return None |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2475 | except pexpect.EOF: |
| 2476 | main.log.error( self.name + ": EOF exception found" ) |
| 2477 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2478 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2479 | except Exception: |
| 2480 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2481 | main.cleanAndExit() |
kelvin-onlab | 4df89f2 | 2015-04-13 18:10:23 -0700 | [diff] [blame] | 2482 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2483 | def pushTestIntents( self, ingress, egress, batchSize, offset="", |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2484 | options="", timeout=10, background = False, noExit=False, getResponse=False ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2485 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2486 | Description: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2487 | Push a number of intents in a batch format to |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2488 | a specific point-to-point intent definition |
| 2489 | Required: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2490 | * ingress: specify source dpid |
| 2491 | * egress: specify destination dpid |
| 2492 | * batchSize: specify number of intents to push |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2493 | Optional: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2494 | * offset: the keyOffset is where the next batch of intents |
| 2495 | will be installed |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2496 | * noExit: If set to True, TestON will not exit if any error when issus command |
| 2497 | * getResponse: If set to True, function will return ONOS response. |
| 2498 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2499 | Returns: If failed to push test intents, it will returen None, |
| 2500 | if successful, return true. |
| 2501 | Timeout expection will return None, |
| 2502 | TypeError will return false |
| 2503 | other expections will exit() |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2504 | """ |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2505 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2506 | if background: |
| 2507 | back = "&" |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2508 | else: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2509 | back = "" |
| 2510 | cmd = "push-test-intents {} {} {} {} {} {}".format( options, |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2511 | ingress, |
| 2512 | egress, |
| 2513 | batchSize, |
| 2514 | offset, |
| 2515 | back ) |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2516 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2517 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2518 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2519 | main.log.info( response ) |
YPZhang | b34b7e1 | 2016-06-14 14:28:19 -0700 | [diff] [blame] | 2520 | if getResponse: |
| 2521 | return response |
| 2522 | |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2523 | # TODO: We should handle if there is failure in installation |
| 2524 | return main.TRUE |
| 2525 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2526 | except AssertionError: |
| 2527 | main.log.exception( "" ) |
| 2528 | return None |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2529 | except pexpect.TIMEOUT: |
| 2530 | main.log.error( self.name + ": ONOS timeout" ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2531 | return None |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2532 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2533 | main.log.error( self.name + ": EOF exception found" ) |
| 2534 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2535 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 2536 | except TypeError: |
| 2537 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2538 | return None |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2539 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2540 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2541 | main.cleanAndExit() |
andrewonlab | 87852b0 | 2014-11-19 18:44:19 -0500 | [diff] [blame] | 2542 | |
YPZhang | ebf9eb5 | 2016-05-12 15:20:24 -0700 | [diff] [blame] | 2543 | def getTotalFlowsNum( self, timeout=60, noExit=False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2544 | """ |
| 2545 | Description: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2546 | Get the number of ADDED flows. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2547 | Return: |
YPZhang | f6f14a0 | 2016-01-28 15:17:31 -0800 | [diff] [blame] | 2548 | The number of ADDED flows |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2549 | Or return None if any exceptions |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2550 | """ |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2551 | |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2552 | try: |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2553 | # get total added flows number |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2554 | cmd = "flows -c added" |
| 2555 | rawFlows = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
| 2556 | if rawFlows: |
| 2557 | rawFlows = rawFlows.split("\n") |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2558 | totalFlows = 0 |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2559 | for l in rawFlows: |
| 2560 | totalFlows += int(l.split("Count=")[1]) |
| 2561 | else: |
| 2562 | main.log.error("Response not as expected!") |
| 2563 | return None |
| 2564 | return totalFlows |
YPZhang | e3109a7 | 2016-02-02 11:25:37 -0800 | [diff] [blame] | 2565 | |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2566 | except ( TypeError, ValueError ): |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2567 | main.log.exception( "{}: Object not as expected!".format( self.name ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2568 | return None |
| 2569 | except pexpect.EOF: |
| 2570 | main.log.error( self.name + ": EOF exception found" ) |
| 2571 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2572 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2573 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2574 | return None |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2575 | except pexpect.TIMEOUT: |
| 2576 | main.log.error( self.name + ": ONOS timeout" ) |
| 2577 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2578 | except Exception: |
| 2579 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2580 | if not noExit: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2581 | main.cleanAndExit() |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2582 | return None |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2583 | |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2584 | def getTotalIntentsNum( self, timeout=60, noExit = False ): |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2585 | """ |
| 2586 | Description: |
| 2587 | Get the total number of intents, include every states. |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2588 | Optional: |
| 2589 | noExit - If noExit, TestON will not exit if any except. |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2590 | Return: |
| 2591 | The number of intents |
| 2592 | """ |
| 2593 | try: |
| 2594 | cmd = "summary -j" |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2595 | response = self.sendline( cmd, timeout=timeout, noExit=noExit ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2596 | if response is None: |
| 2597 | return -1 |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2598 | response = json.loads( response ) |
| 2599 | return int( response.get("intents") ) |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2600 | except ( TypeError, ValueError ): |
| 2601 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, response ) ) |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2602 | return None |
| 2603 | except pexpect.EOF: |
| 2604 | main.log.error( self.name + ": EOF exception found" ) |
| 2605 | main.log.error( self.name + ": " + self.handle.before ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2606 | if noExit: |
| 2607 | return -1 |
| 2608 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2609 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2610 | except Exception: |
| 2611 | main.log.exception( self.name + ": Uncaught exception!" ) |
YPZhang | 14a4aa9 | 2016-07-15 13:37:15 -0700 | [diff] [blame] | 2612 | if noExit: |
| 2613 | return -1 |
| 2614 | else: |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2615 | main.cleanAndExit() |
YPZhang | b5d3f83 | 2016-01-23 22:54:26 -0800 | [diff] [blame] | 2616 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2617 | def intentsEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2618 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2619 | Description:Returns topology metrics |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2620 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2621 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2622 | """ |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2623 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2624 | cmdStr = "intents-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2625 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2626 | cmdStr += " -j" |
| 2627 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2628 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2629 | assert "Command not found:" not in handle, handle |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2630 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2631 | except AssertionError: |
| 2632 | main.log.exception( "" ) |
| 2633 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2634 | except TypeError: |
| 2635 | main.log.exception( self.name + ": Object not as expected" ) |
| 2636 | return None |
andrewonlab | 0dbb6ec | 2014-11-06 13:46:55 -0500 | [diff] [blame] | 2637 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2638 | main.log.error( self.name + ": EOF exception found" ) |
| 2639 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2640 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2641 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2642 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2643 | main.cleanAndExit() |
Shreya Shah | 0f01c81 | 2014-10-26 20:15:28 -0400 | [diff] [blame] | 2644 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2645 | def topologyEventsMetrics( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2646 | """ |
| 2647 | Description:Returns topology metrics |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2648 | Optional: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2649 | * jsonFormat: enable json formatting of output |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2650 | """ |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2651 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2652 | cmdStr = "topology-events-metrics" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2653 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2654 | cmdStr += " -j" |
| 2655 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2656 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2657 | assert "Command not found:" not in handle, handle |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2658 | if handle: |
| 2659 | return handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2660 | elif jsonFormat: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 2661 | # Return empty json |
jenkins | 7ead5a8 | 2015-03-13 10:28:21 -0700 | [diff] [blame] | 2662 | return '{}' |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2663 | else: |
| 2664 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2665 | except AssertionError: |
| 2666 | main.log.exception( "" ) |
| 2667 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2668 | except TypeError: |
| 2669 | main.log.exception( self.name + ": Object not as expected" ) |
| 2670 | return None |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2671 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2672 | main.log.error( self.name + ": EOF exception found" ) |
| 2673 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2674 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2675 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2676 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2677 | main.cleanAndExit() |
andrewonlab | 867212a | 2014-10-22 20:13:38 -0400 | [diff] [blame] | 2678 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2679 | # Wrapper functions **************** |
| 2680 | # Wrapper functions use existing driver |
| 2681 | # functions and extends their use case. |
| 2682 | # For example, we may use the output of |
| 2683 | # a normal driver function, and parse it |
| 2684 | # using a wrapper function |
andrewonlab | c2d05aa | 2014-10-13 16:51:10 -0400 | [diff] [blame] | 2685 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2686 | def getAllIntentsId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2687 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2688 | Description: |
| 2689 | Obtain all intent id's in a list |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2690 | """ |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2691 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2692 | # Obtain output of intents function |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2693 | intentsStr = self.intents(jsonFormat=True) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 2694 | if intentsStr is None: |
| 2695 | raise TypeError |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2696 | # Convert to a dictionary |
| 2697 | intents = json.loads( intentsStr ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2698 | intentIdList = [] |
Jon Hall | 6021e06 | 2017-01-30 11:10:06 -0800 | [diff] [blame] | 2699 | for intent in intents: |
| 2700 | intentIdList.append( intent[ 'id' ] ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2701 | return intentIdList |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2702 | except TypeError: |
| 2703 | main.log.exception( self.name + ": Object not as expected" ) |
| 2704 | return None |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2705 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2706 | main.log.error( self.name + ": EOF exception found" ) |
| 2707 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2708 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2709 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2710 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2711 | main.cleanAndExit() |
andrewonlab | 9a50dfe | 2014-10-17 17:22:31 -0400 | [diff] [blame] | 2712 | |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2713 | def flowAddedCount( self, deviceId, core=False ): |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2714 | """ |
| 2715 | Determine the number of flow rules for the given device id that are |
| 2716 | in the added state |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2717 | Params: |
| 2718 | core: if True, only return the number of core flows added |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2719 | """ |
| 2720 | try: |
You Wang | 3c27625 | 2016-09-21 15:21:36 -0700 | [diff] [blame] | 2721 | if core: |
| 2722 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2723 | "grep 'state=ADDED' | grep org.onosproject.core | wc -l" |
| 2724 | else: |
| 2725 | cmdStr = "flows any " + str( deviceId ) + " | " +\ |
| 2726 | "grep 'state=ADDED' | wc -l" |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2727 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2728 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2729 | assert "Command not found:" not in handle, handle |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2730 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2731 | except AssertionError: |
| 2732 | main.log.exception( "" ) |
| 2733 | return None |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2734 | except pexpect.EOF: |
| 2735 | main.log.error( self.name + ": EOF exception found" ) |
| 2736 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2737 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2738 | except Exception: |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 2739 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2740 | main.cleanAndExit() |
andrewonlab | 95ce832 | 2014-10-13 14:12:04 -0400 | [diff] [blame] | 2741 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2742 | def getAllDevicesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2743 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2744 | Use 'devices' function to obtain list of all devices |
| 2745 | and parse the result to obtain a list of all device |
| 2746 | id's. Returns this list. Returns empty list if no |
| 2747 | devices exist |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2748 | List is ordered sequentially |
| 2749 | |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2750 | This function may be useful if you are not sure of the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2751 | device id, and wish to execute other commands using |
andrewonlab | 3e15ead | 2014-10-15 14:21:34 -0400 | [diff] [blame] | 2752 | the ids. By obtaining the list of device ids on the fly, |
| 2753 | you can iterate through the list to get mastership, etc. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2754 | """ |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2755 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2756 | # Call devices and store result string |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2757 | devicesStr = self.devices( jsonFormat=False ) |
| 2758 | idList = [] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2759 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2760 | if not devicesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2761 | main.log.info( "There are no devices to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2762 | return idList |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2763 | |
| 2764 | # Split the string into list by comma |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2765 | deviceList = devicesStr.split( "," ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2766 | # Get temporary list of all arguments with string 'id=' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2767 | tempList = [ dev for dev in deviceList if "id=" in dev ] |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2768 | # Split list further into arguments before and after string |
| 2769 | # 'id='. Get the latter portion ( the actual device id ) and |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2770 | # append to idList |
| 2771 | for arg in tempList: |
| 2772 | idList.append( arg.split( "id=" )[ 1 ] ) |
| 2773 | return idList |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2774 | |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2775 | except TypeError: |
| 2776 | main.log.exception( self.name + ": Object not as expected" ) |
| 2777 | return None |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2778 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2779 | main.log.error( self.name + ": EOF exception found" ) |
| 2780 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2781 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2782 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2783 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2784 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2785 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2786 | def getAllNodesId( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2787 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2788 | Uses 'nodes' function to obtain list of all nodes |
| 2789 | and parse the result of nodes to obtain just the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2790 | node id's. |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2791 | Returns: |
| 2792 | list of node id's |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2793 | """ |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2794 | try: |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2795 | nodesStr = self.nodes( jsonFormat=True ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2796 | idList = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2797 | # Sample nodesStr output |
Jon Hall | bd18278 | 2016-03-28 16:42:22 -0700 | [diff] [blame] | 2798 | # id=local, address=127.0.0.1:9876, state=READY * |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2799 | if not nodesStr: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2800 | main.log.info( "There are no nodes to get id from" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2801 | return idList |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 2802 | nodesJson = json.loads( nodesStr ) |
| 2803 | idList = [ node.get('id') for node in nodesJson ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2804 | return idList |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2805 | except ( TypeError, ValueError ): |
| 2806 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, nodesStr ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2807 | return None |
andrewonlab | 7c21157 | 2014-10-15 16:45:20 -0400 | [diff] [blame] | 2808 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2809 | main.log.error( self.name + ": EOF exception found" ) |
| 2810 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2811 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2812 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2813 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2814 | main.cleanAndExit() |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 2815 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2816 | def getDevice( self, dpid=None ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2817 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2818 | Return the first device from the devices api whose 'id' contains 'dpid' |
| 2819 | Return None if there is no match |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2820 | """ |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2821 | try: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2822 | if dpid is None: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2823 | return None |
| 2824 | else: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2825 | dpid = dpid.replace( ':', '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2826 | rawDevices = self.devices() |
| 2827 | devicesJson = json.loads( rawDevices ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2828 | # search json for the device with dpid then return the device |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2829 | for device in devicesJson: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2830 | # print "%s in %s?" % ( dpid, device[ 'id' ] ) |
| 2831 | if dpid in device[ 'id' ]: |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2832 | return device |
| 2833 | return None |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2834 | except ( TypeError, ValueError ): |
| 2835 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawDevices ) ) |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2836 | return None |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2837 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2838 | main.log.error( self.name + ": EOF exception found" ) |
| 2839 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2840 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2841 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2842 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2843 | main.cleanAndExit() |
Jon Hall | a91c4dc | 2014-10-22 12:57:04 -0400 | [diff] [blame] | 2844 | |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2845 | def getTopology( self, topologyOutput ): |
| 2846 | """ |
| 2847 | Definition: |
| 2848 | Loads a json topology output |
| 2849 | Return: |
| 2850 | topology = current ONOS topology |
| 2851 | """ |
| 2852 | import json |
| 2853 | try: |
| 2854 | # either onos:topology or 'topology' will work in CLI |
| 2855 | topology = json.loads(topologyOutput) |
Jeremy Songster | bc2d8ac | 2016-05-04 11:25:42 -0700 | [diff] [blame] | 2856 | main.log.debug( topology ) |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2857 | return topology |
You Wang | d3cb2ce | 2016-05-16 14:01:24 -0700 | [diff] [blame] | 2858 | except ( TypeError, ValueError ): |
| 2859 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, topologyOutput ) ) |
| 2860 | return None |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2861 | except pexpect.EOF: |
| 2862 | main.log.error( self.name + ": EOF exception found" ) |
| 2863 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2864 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2865 | except Exception: |
| 2866 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2867 | main.cleanAndExit() |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2868 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2869 | def checkStatus(self, numoswitch, numolink, numoctrl = -1, logLevel="info"): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2870 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2871 | Checks the number of switches & links that ONOS sees against the |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2872 | supplied values. By default this will report to main.log, but the |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2873 | log level can be specific. |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2874 | |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2875 | Params: numoswitch = expected number of switches |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2876 | numolink = expected number of links |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2877 | numoctrl = expected number of controllers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2878 | logLevel = level to log to. |
| 2879 | Currently accepts 'info', 'warn' and 'report' |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2880 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2881 | Returns: main.TRUE if the number of switches and links are correct, |
| 2882 | main.FALSE if the number of switches and links is incorrect, |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2883 | and main.ERROR otherwise |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2884 | """ |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2885 | import json |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2886 | try: |
You Wang | 1331025 | 2016-07-31 10:56:14 -0700 | [diff] [blame] | 2887 | summary = self.summary() |
| 2888 | summary = json.loads( summary ) |
Flavio Castro | f5b3f87 | 2016-06-23 17:52:31 -0700 | [diff] [blame] | 2889 | except ( TypeError, ValueError ): |
| 2890 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, summary ) ) |
| 2891 | return main.ERROR |
| 2892 | try: |
| 2893 | topology = self.getTopology( self.topology() ) |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 2894 | if topology == {} or topology is None or summary == {} or summary is None: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2895 | return main.ERROR |
| 2896 | output = "" |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2897 | # Is the number of switches is what we expected |
| 2898 | devices = topology.get( 'devices', False ) |
| 2899 | links = topology.get( 'links', False ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2900 | nodes = summary.get( 'nodes', False ) |
| 2901 | if devices is False or links is False or nodes is False: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2902 | return main.ERROR |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2903 | switchCheck = ( int( devices ) == int( numoswitch ) ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2904 | # Is the number of links is what we expected |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2905 | linkCheck = ( int( links ) == int( numolink ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2906 | nodeCheck = ( int( nodes ) == int( numoctrl ) ) or int( numoctrl ) == -1 |
| 2907 | if switchCheck and linkCheck and nodeCheck: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2908 | # We expected the correct numbers |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2909 | output = output + "The number of links and switches match "\ |
| 2910 | + "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2911 | result = main.TRUE |
| 2912 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2913 | output = output + \ |
| 2914 | "The number of links and switches does not match " + \ |
| 2915 | "what was expected" |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2916 | result = main.FALSE |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2917 | output = output + "\n ONOS sees %i devices" % int( devices ) |
| 2918 | output = output + " (%i expected) " % int( numoswitch ) |
| 2919 | output = output + "and %i links " % int( links ) |
| 2920 | output = output + "(%i expected)" % int( numolink ) |
YPZhang | d7e4b6e | 2016-06-17 16:07:55 -0700 | [diff] [blame] | 2921 | if int( numoctrl ) > 0: |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 2922 | output = output + "and %i controllers " % int( nodes ) |
| 2923 | output = output + "(%i expected)" % int( numoctrl ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2924 | if logLevel == "report": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2925 | main.log.report( output ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2926 | elif logLevel == "warn": |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2927 | main.log.warn( output ) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2928 | else: |
You Wang | 2413987 | 2016-05-03 11:48:47 -0700 | [diff] [blame] | 2929 | main.log.info( output ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2930 | return result |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 2931 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2932 | main.log.error( self.name + ": EOF exception found" ) |
| 2933 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2934 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2935 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2936 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2937 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2938 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2939 | def deviceRole( self, deviceId, onosNode, role="master" ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2940 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2941 | Calls the device-role cli command. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2942 | deviceId must be the id of a device as seen in the onos devices command |
| 2943 | 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] | 2944 | role must be either master, standby, or none |
| 2945 | |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2946 | Returns: |
| 2947 | main.TRUE or main.FALSE based on argument verification and |
| 2948 | main.ERROR if command returns and error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2949 | """ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2950 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 2951 | if role.lower() == "master" or role.lower() == "standby" or\ |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2952 | role.lower() == "none": |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2953 | cmdStr = "device-role " +\ |
| 2954 | str( deviceId ) + " " +\ |
| 2955 | str( onosNode ) + " " +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2956 | str( role ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2957 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2958 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2959 | assert "Command not found:" not in handle, handle |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2960 | if re.search( "Error", handle ): |
| 2961 | # end color output to escape any colours |
| 2962 | # from the cli |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2963 | main.log.error( self.name + ": " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2964 | handle + '\033[0m' ) |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2965 | return main.ERROR |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2966 | return main.TRUE |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2967 | else: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 2968 | main.log.error( "Invalid 'role' given to device_role(). " + |
| 2969 | "Value was '" + str(role) + "'." ) |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2970 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2971 | except AssertionError: |
| 2972 | main.log.exception( "" ) |
| 2973 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2974 | except TypeError: |
| 2975 | main.log.exception( self.name + ": Object not as expected" ) |
| 2976 | return None |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2977 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2978 | main.log.error( self.name + ": EOF exception found" ) |
| 2979 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2980 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2981 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 2982 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 2983 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 2984 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2985 | def clusters( self, jsonFormat=True ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2986 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2987 | Lists all clusters |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2988 | Optional argument: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2989 | * jsonFormat - boolean indicating if you want output in json |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 2990 | """ |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 2991 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2992 | cmdStr = "clusters" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2993 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2994 | cmdStr += " -j" |
| 2995 | handle = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 2996 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2997 | assert "Command not found:" not in handle, handle |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 2998 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 2999 | except AssertionError: |
| 3000 | main.log.exception( "" ) |
| 3001 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3002 | except TypeError: |
| 3003 | main.log.exception( self.name + ": Object not as expected" ) |
| 3004 | return None |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3005 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3006 | main.log.error( self.name + ": EOF exception found" ) |
| 3007 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3008 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3009 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3010 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3011 | main.cleanAndExit() |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 3012 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3013 | def electionTestLeader( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3014 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3015 | CLI command to get the current leader for the Election test application |
| 3016 | NOTE: Requires installation of the onos-app-election feature |
| 3017 | Returns: Node IP of the leader if one exists |
| 3018 | None if none exists |
| 3019 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3020 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3021 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3022 | cmdStr = "election-test-leader" |
| 3023 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3024 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3025 | assert "Command not found:" not in response, response |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3026 | # Leader |
| 3027 | leaderPattern = "The\scurrent\sleader\sfor\sthe\sElection\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3028 | "app\sis\s(?P<node>.+)\." |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3029 | nodeSearch = re.search( leaderPattern, response ) |
| 3030 | if nodeSearch: |
| 3031 | node = nodeSearch.group( 'node' ) |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3032 | main.log.info( "Election-test-leader on " + str( self.name ) + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3033 | " found " + node + " as the leader" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3034 | return node |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3035 | # no leader |
| 3036 | nullPattern = "There\sis\scurrently\sno\sleader\selected\sfor\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3037 | "the\sElection\sapp" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3038 | nullSearch = re.search( nullPattern, response ) |
| 3039 | if nullSearch: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3040 | main.log.info( "Election-test-leader found no leader on " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3041 | self.name ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3042 | return None |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3043 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3044 | main.log.error( "Error in electionTestLeader on " + self.name + |
| 3045 | ": " + "unexpected response" ) |
| 3046 | main.log.error( repr( response ) ) |
| 3047 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3048 | except AssertionError: |
| 3049 | main.log.exception( "" ) |
| 3050 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3051 | except TypeError: |
| 3052 | main.log.exception( self.name + ": Object not as expected" ) |
| 3053 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3054 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3055 | main.log.error( self.name + ": EOF exception found" ) |
| 3056 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3057 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3058 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3059 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3060 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3061 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3062 | def electionTestRun( self ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3063 | """ |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3064 | CLI command to run for leadership of the Election test application. |
| 3065 | NOTE: Requires installation of the onos-app-election feature |
| 3066 | Returns: Main.TRUE on success |
| 3067 | Main.FALSE on error |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3068 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3069 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3070 | cmdStr = "election-test-run" |
| 3071 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3072 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3073 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3074 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3075 | successPattern = "Entering\sleadership\selections\sfor\sthe\s" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3076 | "Election\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3077 | search = re.search( successPattern, response ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3078 | if search: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3079 | main.log.info( self.name + " entering leadership elections " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3080 | "for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3081 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3082 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3083 | main.log.error( "Error in electionTestRun on " + self.name + |
| 3084 | ": " + "unexpected response" ) |
| 3085 | main.log.error( repr( response ) ) |
| 3086 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3087 | except AssertionError: |
| 3088 | main.log.exception( "" ) |
| 3089 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3090 | except TypeError: |
| 3091 | main.log.exception( self.name + ": Object not as expected" ) |
| 3092 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3093 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3094 | main.log.error( self.name + ": EOF exception found" ) |
| 3095 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3096 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3097 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3098 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3099 | main.cleanAndExit() |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3100 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3101 | def electionTestWithdraw( self ): |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3102 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3103 | * CLI command to withdraw the local node from leadership election for |
| 3104 | * the Election test application. |
| 3105 | #NOTE: Requires installation of the onos-app-election feature |
| 3106 | Returns: Main.TRUE on success |
| 3107 | Main.FALSE on error |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3108 | """ |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3109 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3110 | cmdStr = "election-test-withdraw" |
| 3111 | response = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3112 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3113 | assert "Command not found:" not in response, response |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3114 | # success |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3115 | successPattern = "Withdrawing\sfrom\sleadership\selections\sfor" +\ |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3116 | "\sthe\sElection\sapp." |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3117 | if re.search( successPattern, response ): |
| 3118 | main.log.info( self.name + " withdrawing from leadership " + |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3119 | "elections for the Election app." ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3120 | return main.TRUE |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3121 | # error |
Jon Hall | 97cf84a | 2016-06-20 13:35:58 -0700 | [diff] [blame] | 3122 | main.log.error( "Error in electionTestWithdraw on " + |
| 3123 | self.name + ": " + "unexpected response" ) |
| 3124 | main.log.error( repr( response ) ) |
| 3125 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3126 | except AssertionError: |
| 3127 | main.log.exception( "" ) |
| 3128 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3129 | except TypeError: |
| 3130 | main.log.exception( self.name + ": Object not as expected" ) |
| 3131 | return main.FALSE |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 3132 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3133 | main.log.error( self.name + ": EOF exception found" ) |
| 3134 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3135 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3136 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3137 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3138 | main.cleanAndExit() |
Jon Hall | 1c9e873 | 2014-10-27 19:29:27 -0400 | [diff] [blame] | 3139 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3140 | def getDevicePortsEnabledCount( self, dpid ): |
| 3141 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3142 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3143 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3144 | try: |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3145 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3146 | cmdStr = "onos:ports -e " + dpid + " | wc -l" |
| 3147 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3148 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3149 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3150 | if re.search( "No such device", output ): |
| 3151 | main.log.error( "Error in getting ports" ) |
| 3152 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3153 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3154 | except AssertionError: |
| 3155 | main.log.exception( "" ) |
| 3156 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3157 | except TypeError: |
| 3158 | main.log.exception( self.name + ": Object not as expected" ) |
| 3159 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3160 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3161 | main.log.error( self.name + ": EOF exception found" ) |
| 3162 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3163 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3164 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3165 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3166 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3167 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3168 | def getDeviceLinksActiveCount( self, dpid ): |
| 3169 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3170 | Get the count of all enabled ports on a particular device/switch |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3171 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3172 | try: |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3173 | dpid = str( dpid ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3174 | cmdStr = "onos:links " + dpid + " | grep ACTIVE | wc -l" |
| 3175 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3176 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3177 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3178 | if re.search( "No such device", output ): |
kelvin-onlab | 898a6c6 | 2015-01-16 14:13:53 -0800 | [diff] [blame] | 3179 | main.log.error( "Error in getting ports " ) |
| 3180 | return ( output, "Error " ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3181 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3182 | except AssertionError: |
| 3183 | main.log.exception( "" ) |
| 3184 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3185 | except TypeError: |
| 3186 | main.log.exception( self.name + ": Object not as expected" ) |
| 3187 | return ( output, "Error " ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3188 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3189 | main.log.error( self.name + ": EOF exception found" ) |
| 3190 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3191 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3192 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3193 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3194 | main.cleanAndExit() |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3195 | |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3196 | def getAllIntentIds( self ): |
| 3197 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3198 | Return a list of all Intent IDs |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3199 | """ |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3200 | try: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3201 | cmdStr = "onos:intents | grep id=" |
| 3202 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3203 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3204 | assert "Command not found:" not in output, output |
Jon Hall | e3f39ff | 2015-01-13 11:50:53 -0800 | [diff] [blame] | 3205 | if re.search( "Error", output ): |
| 3206 | main.log.error( "Error in getting ports" ) |
| 3207 | return ( output, "Error" ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3208 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3209 | except AssertionError: |
| 3210 | main.log.exception( "" ) |
| 3211 | return None |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3212 | except TypeError: |
| 3213 | main.log.exception( self.name + ": Object not as expected" ) |
| 3214 | return ( output, "Error" ) |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3215 | except pexpect.EOF: |
kelvin | 8ec7144 | 2015-01-15 16:57:00 -0800 | [diff] [blame] | 3216 | main.log.error( self.name + ": EOF exception found" ) |
| 3217 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3218 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3219 | except Exception: |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3220 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3221 | main.cleanAndExit() |
Jon Hall | d4d4b37 | 2015-01-28 16:02:41 -0800 | [diff] [blame] | 3222 | |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3223 | def intentSummary( self ): |
| 3224 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3225 | Returns a dictionary containing the current intent states and the count |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3226 | """ |
| 3227 | try: |
| 3228 | intents = self.intents( ) |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3229 | states = [] |
Jon Hall | 5aa168b | 2015-03-23 14:23:09 -0700 | [diff] [blame] | 3230 | for intent in json.loads( intents ): |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3231 | states.append( intent.get( 'state', None ) ) |
| 3232 | out = [ ( i, states.count( i ) ) for i in set( states ) ] |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3233 | main.log.info( dict( out ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3234 | return dict( out ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3235 | except ( TypeError, ValueError ): |
| 3236 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, intents ) ) |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3237 | return None |
| 3238 | except pexpect.EOF: |
| 3239 | main.log.error( self.name + ": EOF exception found" ) |
| 3240 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3241 | main.cleanAndExit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 3242 | except Exception: |
Jon Hall | 7350995 | 2015-02-24 16:42:56 -0800 | [diff] [blame] | 3243 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3244 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3245 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3246 | def leaders( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3247 | """ |
| 3248 | Returns the output of the leaders command. |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3249 | Optional argument: |
| 3250 | * jsonFormat - boolean indicating if you want output in json |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3251 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3252 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3253 | cmdStr = "onos:leaders" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3254 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3255 | cmdStr += " -j" |
| 3256 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3257 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3258 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3259 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3260 | except AssertionError: |
| 3261 | main.log.exception( "" ) |
| 3262 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3263 | except TypeError: |
| 3264 | main.log.exception( self.name + ": Object not as expected" ) |
| 3265 | return None |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 3266 | except pexpect.EOF: |
| 3267 | main.log.error( self.name + ": EOF exception found" ) |
| 3268 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3269 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3270 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3271 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3272 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3273 | |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3274 | def leaderCandidates( self, jsonFormat=True ): |
| 3275 | """ |
| 3276 | Returns the output of the leaders -c command. |
| 3277 | Optional argument: |
| 3278 | * jsonFormat - boolean indicating if you want output in json |
| 3279 | """ |
| 3280 | try: |
| 3281 | cmdStr = "onos:leaders -c" |
| 3282 | if jsonFormat: |
| 3283 | cmdStr += " -j" |
| 3284 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3285 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3286 | assert "Command not found:" not in output, output |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3287 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3288 | except AssertionError: |
| 3289 | main.log.exception( "" ) |
| 3290 | return None |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3291 | except TypeError: |
| 3292 | main.log.exception( self.name + ": Object not as expected" ) |
| 3293 | return None |
| 3294 | except pexpect.EOF: |
| 3295 | main.log.error( self.name + ": EOF exception found" ) |
| 3296 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3297 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3298 | except Exception: |
| 3299 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3300 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3301 | |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3302 | def specificLeaderCandidate( self, topic ): |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3303 | """ |
| 3304 | Returns a list in format [leader,candidate1,candidate2,...] for a given |
| 3305 | topic parameter and an empty list if the topic doesn't exist |
| 3306 | If no leader is elected leader in the returned list will be "none" |
| 3307 | Returns None if there is a type error processing the json object |
| 3308 | """ |
| 3309 | try: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 3310 | cmdStr = "onos:leaders -j" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3311 | rawOutput = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3312 | assert rawOutput is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3313 | assert "Command not found:" not in rawOutput, rawOutput |
| 3314 | output = json.loads( rawOutput ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3315 | results = [] |
| 3316 | for dict in output: |
| 3317 | if dict["topic"] == topic: |
| 3318 | leader = dict["leader"] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3319 | candidates = re.split( ", ", dict["candidates"][1:-1] ) |
| 3320 | results.append( leader ) |
| 3321 | results.extend( candidates ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3322 | return results |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3323 | except AssertionError: |
| 3324 | main.log.exception( "" ) |
| 3325 | return None |
| 3326 | except ( TypeError, ValueError ): |
| 3327 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawOutput ) ) |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3328 | return None |
| 3329 | except pexpect.EOF: |
| 3330 | main.log.error( self.name + ": EOF exception found" ) |
| 3331 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3332 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3333 | except Exception: |
| 3334 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3335 | main.cleanAndExit() |
acsmars | a4a4d1e | 2015-07-10 16:01:24 -0700 | [diff] [blame] | 3336 | |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3337 | def pendingMap( self, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3338 | """ |
| 3339 | Returns the output of the intent Pending map. |
| 3340 | """ |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3341 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3342 | cmdStr = "onos:intents -p" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3343 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3344 | cmdStr += " -j" |
| 3345 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3346 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3347 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3348 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3349 | except AssertionError: |
| 3350 | main.log.exception( "" ) |
| 3351 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3352 | except TypeError: |
| 3353 | main.log.exception( self.name + ": Object not as expected" ) |
| 3354 | return None |
| 3355 | except pexpect.EOF: |
| 3356 | main.log.error( self.name + ": EOF exception found" ) |
| 3357 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3358 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3359 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3360 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3361 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3362 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3363 | def partitions( self, candidates=False, jsonFormat=True ): |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3364 | """ |
| 3365 | Returns the output of the raft partitions command for ONOS. |
| 3366 | """ |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3367 | # Sample JSON |
| 3368 | # { |
| 3369 | # "leader": "tcp://10.128.30.11:7238", |
| 3370 | # "members": [ |
| 3371 | # "tcp://10.128.30.11:7238", |
| 3372 | # "tcp://10.128.30.17:7238", |
| 3373 | # "tcp://10.128.30.13:7238", |
| 3374 | # ], |
| 3375 | # "name": "p1", |
| 3376 | # "term": 3 |
| 3377 | # }, |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3378 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3379 | cmdStr = "onos:partitions" |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 3380 | if candidates: |
| 3381 | cmdStr += " -c" |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 3382 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3383 | cmdStr += " -j" |
| 3384 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3385 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3386 | assert "Command not found:" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3387 | return output |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3388 | except AssertionError: |
| 3389 | main.log.exception( "" ) |
| 3390 | return None |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3391 | except TypeError: |
| 3392 | main.log.exception( self.name + ": Object not as expected" ) |
| 3393 | return None |
| 3394 | except pexpect.EOF: |
| 3395 | main.log.error( self.name + ": EOF exception found" ) |
| 3396 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3397 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3398 | except Exception: |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3399 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3400 | main.cleanAndExit() |
Jon Hall | 6360493 | 2015-02-26 17:09:50 -0800 | [diff] [blame] | 3401 | |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3402 | def apps( self, summary=False, active=False, jsonFormat=True ): |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3403 | """ |
| 3404 | Returns the output of the apps command for ONOS. This command lists |
| 3405 | information about installed ONOS applications |
| 3406 | """ |
| 3407 | # Sample JSON object |
| 3408 | # [{"name":"org.onosproject.openflow","id":0,"version":"1.2.0", |
| 3409 | # "description":"ONOS OpenFlow protocol southbound providers", |
| 3410 | # "origin":"ON.Lab","permissions":"[]","featuresRepo":"", |
| 3411 | # "features":"[onos-openflow]","state":"ACTIVE"}] |
| 3412 | try: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3413 | cmdStr = "onos:apps" |
Jon Hall | e9f909e | 2016-09-23 10:43:12 -0700 | [diff] [blame] | 3414 | if summary: |
| 3415 | cmdStr += " -s" |
| 3416 | if active: |
| 3417 | cmdStr += " -a" |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3418 | if jsonFormat: |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3419 | cmdStr += " -j" |
| 3420 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3421 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3422 | assert "Command not found:" not in output, output |
| 3423 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3424 | return output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3425 | # FIXME: look at specific exceptions/Errors |
| 3426 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3427 | main.log.exception( "Error in processing onos:app command." ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3428 | return None |
| 3429 | except TypeError: |
| 3430 | main.log.exception( self.name + ": Object not as expected" ) |
| 3431 | return None |
| 3432 | except pexpect.EOF: |
| 3433 | main.log.error( self.name + ": EOF exception found" ) |
| 3434 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3435 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3436 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3437 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3438 | main.cleanAndExit() |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3439 | |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3440 | def appStatus( self, appName ): |
| 3441 | """ |
| 3442 | Uses the onos:apps cli command to return the status of an application. |
| 3443 | Returns: |
| 3444 | "ACTIVE" - If app is installed and activated |
| 3445 | "INSTALLED" - If app is installed and deactivated |
| 3446 | "UNINSTALLED" - If app is not installed |
| 3447 | None - on error |
| 3448 | """ |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3449 | try: |
| 3450 | if not isinstance( appName, types.StringType ): |
| 3451 | main.log.error( self.name + ".appStatus(): appName must be" + |
| 3452 | " a string" ) |
| 3453 | return None |
| 3454 | output = self.apps( jsonFormat=True ) |
| 3455 | appsJson = json.loads( output ) |
| 3456 | state = None |
| 3457 | for app in appsJson: |
| 3458 | if appName == app.get('name'): |
| 3459 | state = app.get('state') |
| 3460 | break |
| 3461 | if state == "ACTIVE" or state == "INSTALLED": |
| 3462 | return state |
| 3463 | elif state is None: |
Jon Hall | 8bafdc0 | 2017-09-05 11:36:26 -0700 | [diff] [blame] | 3464 | main.log.warn( "{} app not found", appName ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3465 | return "UNINSTALLED" |
| 3466 | elif state: |
| 3467 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3468 | str( state ) ) |
| 3469 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3470 | except ( TypeError, ValueError ): |
| 3471 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3472 | return None |
| 3473 | except pexpect.EOF: |
| 3474 | main.log.error( self.name + ": EOF exception found" ) |
| 3475 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3476 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3477 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3478 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3479 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3480 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3481 | def app( self, appName, option ): |
| 3482 | """ |
| 3483 | Interacts with the app command for ONOS. This command manages |
| 3484 | application inventory. |
| 3485 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3486 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3487 | # Validate argument types |
| 3488 | valid = True |
| 3489 | if not isinstance( appName, types.StringType ): |
| 3490 | main.log.error( self.name + ".app(): appName must be a " + |
| 3491 | "string" ) |
| 3492 | valid = False |
| 3493 | if not isinstance( option, types.StringType ): |
| 3494 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3495 | valid = False |
| 3496 | if not valid: |
| 3497 | return main.FALSE |
| 3498 | # Validate Option |
| 3499 | option = option.lower() |
| 3500 | # NOTE: Install may become a valid option |
| 3501 | if option == "activate": |
| 3502 | pass |
| 3503 | elif option == "deactivate": |
| 3504 | pass |
| 3505 | elif option == "uninstall": |
| 3506 | pass |
| 3507 | else: |
| 3508 | # Invalid option |
| 3509 | main.log.error( "The ONOS app command argument only takes " + |
| 3510 | "the values: (activate|deactivate|uninstall)" + |
| 3511 | "; was given '" + option + "'") |
| 3512 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3513 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3514 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3515 | assert output is not None, "Error in sendline" |
| 3516 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3517 | if "Error executing command" in output: |
| 3518 | main.log.error( "Error in processing onos:app command: " + |
| 3519 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3520 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3521 | elif "No such application" in output: |
| 3522 | main.log.error( "The application '" + appName + |
| 3523 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3524 | return main.FALSE |
| 3525 | elif "Command not found:" in output: |
| 3526 | main.log.error( "Error in processing onos:app command: " + |
| 3527 | str( output ) ) |
| 3528 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3529 | elif "Unsupported command:" in output: |
| 3530 | main.log.error( "Incorrect command given to 'app': " + |
| 3531 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3532 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3533 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3534 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3535 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3536 | except AssertionError: |
| 3537 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3538 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3539 | except TypeError: |
| 3540 | main.log.exception( self.name + ": Object not as expected" ) |
| 3541 | return main.ERROR |
| 3542 | except pexpect.EOF: |
| 3543 | main.log.error( self.name + ": EOF exception found" ) |
| 3544 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3545 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3546 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3547 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3548 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3549 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3550 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3551 | """ |
| 3552 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3553 | appName is the hierarchical app name, not the feature name |
| 3554 | If check is True, method will check the status of the app after the |
| 3555 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3556 | Returns main.TRUE if the command was successfully sent |
| 3557 | main.FALSE if the cli responded with an error or given |
| 3558 | incorrect input |
| 3559 | """ |
| 3560 | try: |
| 3561 | if not isinstance( appName, types.StringType ): |
| 3562 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3563 | " a string" ) |
| 3564 | return main.FALSE |
| 3565 | status = self.appStatus( appName ) |
| 3566 | if status == "INSTALLED": |
| 3567 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3568 | if check and response == main.TRUE: |
| 3569 | for i in range(10): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3570 | status = self.appStatus( appName ) |
| 3571 | if status == "ACTIVE": |
| 3572 | return main.TRUE |
| 3573 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3574 | main.log.debug( "The state of application " + |
| 3575 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3576 | time.sleep( 1 ) |
| 3577 | return main.FALSE |
| 3578 | else: # not 'check' or command didn't succeed |
| 3579 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3580 | elif status == "ACTIVE": |
| 3581 | return main.TRUE |
| 3582 | elif status == "UNINSTALLED": |
| 3583 | main.log.error( self.name + ": Tried to activate the " + |
| 3584 | "application '" + appName + "' which is not " + |
| 3585 | "installed." ) |
| 3586 | else: |
| 3587 | main.log.error( "Unexpected return value from appStatus: " + |
| 3588 | str( status ) ) |
| 3589 | return main.ERROR |
| 3590 | except TypeError: |
| 3591 | main.log.exception( self.name + ": Object not as expected" ) |
| 3592 | return main.ERROR |
| 3593 | except pexpect.EOF: |
| 3594 | main.log.error( self.name + ": EOF exception found" ) |
| 3595 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3596 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3597 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3598 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3599 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3600 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3601 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3602 | """ |
| 3603 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3604 | appName is the hierarchical app name, not the feature name |
| 3605 | If check is True, method will check the status of the app after the |
| 3606 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3607 | Returns main.TRUE if the command was successfully sent |
| 3608 | main.FALSE if the cli responded with an error or given |
| 3609 | incorrect input |
| 3610 | """ |
| 3611 | try: |
| 3612 | if not isinstance( appName, types.StringType ): |
| 3613 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3614 | "be a string" ) |
| 3615 | return main.FALSE |
| 3616 | status = self.appStatus( appName ) |
| 3617 | if status == "INSTALLED": |
| 3618 | return main.TRUE |
| 3619 | elif status == "ACTIVE": |
| 3620 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3621 | if check and response == main.TRUE: |
| 3622 | for i in range(10): # try 10 times then give up |
| 3623 | status = self.appStatus( appName ) |
| 3624 | if status == "INSTALLED": |
| 3625 | return main.TRUE |
| 3626 | else: |
| 3627 | time.sleep( 1 ) |
| 3628 | return main.FALSE |
| 3629 | else: # not check or command didn't succeed |
| 3630 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3631 | elif status == "UNINSTALLED": |
| 3632 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3633 | "application '" + appName + "' which is not " + |
| 3634 | "installed." ) |
| 3635 | return main.TRUE |
| 3636 | else: |
| 3637 | main.log.error( "Unexpected return value from appStatus: " + |
| 3638 | str( status ) ) |
| 3639 | return main.ERROR |
| 3640 | except TypeError: |
| 3641 | main.log.exception( self.name + ": Object not as expected" ) |
| 3642 | return main.ERROR |
| 3643 | except pexpect.EOF: |
| 3644 | main.log.error( self.name + ": EOF exception found" ) |
| 3645 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3646 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3647 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3648 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3649 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3650 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3651 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3652 | """ |
| 3653 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3654 | appName is the hierarchical app name, not the feature name |
| 3655 | If check is True, method will check the status of the app after the |
| 3656 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3657 | Returns main.TRUE if the command was successfully sent |
| 3658 | main.FALSE if the cli responded with an error or given |
| 3659 | incorrect input |
| 3660 | """ |
| 3661 | # TODO: check with Thomas about the state machine for apps |
| 3662 | try: |
| 3663 | if not isinstance( appName, types.StringType ): |
| 3664 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3665 | "be a string" ) |
| 3666 | return main.FALSE |
| 3667 | status = self.appStatus( appName ) |
| 3668 | if status == "INSTALLED": |
| 3669 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3670 | if check and response == main.TRUE: |
| 3671 | for i in range(10): # try 10 times then give up |
| 3672 | status = self.appStatus( appName ) |
| 3673 | if status == "UNINSTALLED": |
| 3674 | return main.TRUE |
| 3675 | else: |
| 3676 | time.sleep( 1 ) |
| 3677 | return main.FALSE |
| 3678 | else: # not check or command didn't succeed |
| 3679 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3680 | elif status == "ACTIVE": |
| 3681 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3682 | "application '" + appName + "' which is " + |
| 3683 | "currently active." ) |
| 3684 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3685 | if check and response == main.TRUE: |
| 3686 | for i in range(10): # try 10 times then give up |
| 3687 | status = self.appStatus( appName ) |
| 3688 | if status == "UNINSTALLED": |
| 3689 | return main.TRUE |
| 3690 | else: |
| 3691 | time.sleep( 1 ) |
| 3692 | return main.FALSE |
| 3693 | else: # not check or command didn't succeed |
| 3694 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3695 | elif status == "UNINSTALLED": |
| 3696 | return main.TRUE |
| 3697 | else: |
| 3698 | main.log.error( "Unexpected return value from appStatus: " + |
| 3699 | str( status ) ) |
| 3700 | return main.ERROR |
| 3701 | except TypeError: |
| 3702 | main.log.exception( self.name + ": Object not as expected" ) |
| 3703 | return main.ERROR |
| 3704 | except pexpect.EOF: |
| 3705 | main.log.error( self.name + ": EOF exception found" ) |
| 3706 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3707 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3708 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3709 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3710 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3711 | |
| 3712 | def appIDs( self, jsonFormat=True ): |
| 3713 | """ |
| 3714 | Show the mappings between app id and app names given by the 'app-ids' |
| 3715 | cli command |
| 3716 | """ |
| 3717 | try: |
| 3718 | cmdStr = "app-ids" |
| 3719 | if jsonFormat: |
| 3720 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3721 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3722 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3723 | assert "Command not found:" not in output, output |
| 3724 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3725 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3726 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3727 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3728 | return None |
| 3729 | except TypeError: |
| 3730 | main.log.exception( self.name + ": Object not as expected" ) |
| 3731 | return None |
| 3732 | except pexpect.EOF: |
| 3733 | main.log.error( self.name + ": EOF exception found" ) |
| 3734 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3735 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3736 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3737 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3738 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3739 | |
| 3740 | def appToIDCheck( self ): |
| 3741 | """ |
| 3742 | This method will check that each application's ID listed in 'apps' is |
| 3743 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3744 | there are no duplicate IDs issued. Note that an app ID should be |
| 3745 | a globaly unique numerical identifier for app/app-like features. Once |
| 3746 | an ID is registered, the ID is never freed up so that if an app is |
| 3747 | reinstalled it will have the same ID. |
| 3748 | |
| 3749 | Returns: main.TRUE if the check passes and |
| 3750 | main.FALSE if the check fails or |
| 3751 | main.ERROR if there is some error in processing the test |
| 3752 | """ |
| 3753 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3754 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3755 | rawJson = self.appIDs( jsonFormat=True ) |
| 3756 | if rawJson: |
| 3757 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3758 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3759 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3760 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3761 | rawJson = self.apps( jsonFormat=True ) |
| 3762 | if rawJson: |
| 3763 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3764 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3765 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3766 | bail = True |
| 3767 | if bail: |
| 3768 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3769 | result = main.TRUE |
| 3770 | for app in apps: |
| 3771 | appID = app.get( 'id' ) |
| 3772 | if appID is None: |
| 3773 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3774 | result = main.FALSE |
| 3775 | appName = app.get( 'name' ) |
| 3776 | if appName is None: |
| 3777 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3778 | result = main.FALSE |
| 3779 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3780 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3781 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3782 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3783 | if not current: # if ids doesn't have this id |
| 3784 | result = main.FALSE |
| 3785 | main.log.error( "'app-ids' does not have the ID for " + |
| 3786 | str( appName ) + " that apps does." ) |
| 3787 | elif len( current ) > 1: |
| 3788 | # there is more than one app with this ID |
| 3789 | result = main.FALSE |
| 3790 | # We will log this later in the method |
| 3791 | elif not current[0][ 'name' ] == appName: |
| 3792 | currentName = current[0][ 'name' ] |
| 3793 | result = main.FALSE |
| 3794 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3795 | " registered under id:" + str( appID ) + |
| 3796 | " but 'apps' has " + str( appName ) ) |
| 3797 | else: |
| 3798 | pass # id and name match! |
| 3799 | # now make sure that app-ids has no duplicates |
| 3800 | idsList = [] |
| 3801 | namesList = [] |
| 3802 | for item in ids: |
| 3803 | idsList.append( item[ 'id' ] ) |
| 3804 | namesList.append( item[ 'name' ] ) |
| 3805 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3806 | len( namesList ) != len( set( namesList ) ): |
| 3807 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3808 | + json.dumps( ids, |
| 3809 | sort_keys=True, |
| 3810 | indent=4, |
| 3811 | separators=( ',', ': ' ) ) ) |
| 3812 | result = main.FALSE |
| 3813 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3814 | except ( TypeError, ValueError ): |
| 3815 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3816 | return main.ERROR |
| 3817 | except pexpect.EOF: |
| 3818 | main.log.error( self.name + ": EOF exception found" ) |
| 3819 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3820 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3821 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3822 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3823 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3824 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3825 | def getCfg( self, component=None, propName=None, short=False, |
| 3826 | jsonFormat=True ): |
| 3827 | """ |
| 3828 | Get configuration settings from onos cli |
| 3829 | Optional arguments: |
| 3830 | component - Optionally only list configurations for a specific |
| 3831 | component. If None, all components with configurations |
| 3832 | are displayed. Case Sensitive string. |
| 3833 | propName - If component is specified, propName option will show |
| 3834 | only this specific configuration from that component. |
| 3835 | Case Sensitive string. |
| 3836 | jsonFormat - Returns output as json. Note that this will override |
| 3837 | the short option |
| 3838 | short - Short, less verbose, version of configurations. |
| 3839 | This is overridden by the json option |
| 3840 | returns: |
| 3841 | Output from cli as a string or None on error |
| 3842 | """ |
| 3843 | try: |
| 3844 | baseStr = "cfg" |
| 3845 | cmdStr = " get" |
| 3846 | componentStr = "" |
| 3847 | if component: |
| 3848 | componentStr += " " + component |
| 3849 | if propName: |
| 3850 | componentStr += " " + propName |
| 3851 | if jsonFormat: |
| 3852 | baseStr += " -j" |
| 3853 | elif short: |
| 3854 | baseStr += " -s" |
| 3855 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3856 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3857 | assert "Command not found:" not in output, output |
| 3858 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3859 | return output |
| 3860 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3861 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3862 | return None |
| 3863 | except TypeError: |
| 3864 | main.log.exception( self.name + ": Object not as expected" ) |
| 3865 | return None |
| 3866 | except pexpect.EOF: |
| 3867 | main.log.error( self.name + ": EOF exception found" ) |
| 3868 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3869 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3870 | except Exception: |
| 3871 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3872 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3873 | |
| 3874 | def setCfg( self, component, propName, value=None, check=True ): |
| 3875 | """ |
| 3876 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3877 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3878 | component - The case sensitive name of the component whose |
| 3879 | property is to be set |
| 3880 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3881 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3882 | value - The value to set the property to. If None, will unset the |
| 3883 | property and revert it to it's default value(if applicable) |
| 3884 | check - Boolean, Check whether the option was successfully set this |
| 3885 | only applies when a value is given. |
| 3886 | returns: |
| 3887 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3888 | will return main.TRUE unless there is an error |
| 3889 | """ |
| 3890 | try: |
| 3891 | baseStr = "cfg" |
| 3892 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3893 | if value is not None: |
| 3894 | cmdStr += " " + str( value ) |
| 3895 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3896 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3897 | assert "Command not found:" not in output, output |
| 3898 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3899 | if value and check: |
| 3900 | results = self.getCfg( component=str( component ), |
| 3901 | propName=str( propName ), |
| 3902 | jsonFormat=True ) |
| 3903 | # Check if current value is what we just set |
| 3904 | try: |
| 3905 | jsonOutput = json.loads( results ) |
| 3906 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3907 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3908 | main.log.exception( "Error parsing cfg output" ) |
| 3909 | main.log.error( "output:" + repr( results ) ) |
| 3910 | return main.FALSE |
| 3911 | if current == str( value ): |
| 3912 | return main.TRUE |
| 3913 | return main.FALSE |
| 3914 | return main.TRUE |
| 3915 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3916 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3917 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3918 | except ( TypeError, ValueError ): |
| 3919 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3920 | return main.FALSE |
| 3921 | except pexpect.EOF: |
| 3922 | main.log.error( self.name + ": EOF exception found" ) |
| 3923 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3924 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3925 | except Exception: |
| 3926 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3927 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3928 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3929 | def distPrimitivesSend( self, cmd ): |
| 3930 | """ |
| 3931 | Function to handle sending cli commands for the distributed primitives test app |
| 3932 | |
| 3933 | This command will catch some exceptions and retry the command on some |
| 3934 | specific store exceptions. |
| 3935 | |
| 3936 | Required arguments: |
| 3937 | cmd - The command to send to the cli |
| 3938 | returns: |
| 3939 | string containing the cli output |
| 3940 | None on Error |
| 3941 | """ |
| 3942 | try: |
| 3943 | output = self.sendline( cmd ) |
| 3944 | try: |
| 3945 | assert output is not None, "Error in sendline" |
| 3946 | # TODO: Maybe make this less hardcoded |
| 3947 | # ConsistentMap Exceptions |
| 3948 | assert "org.onosproject.store.service" not in output |
| 3949 | # Node not leader |
| 3950 | assert "java.lang.IllegalStateException" not in output |
| 3951 | except AssertionError: |
| 3952 | main.log.error( "Error in processing '" + cmd + "' " + |
| 3953 | "command: " + str( output ) ) |
| 3954 | retryTime = 30 # Conservative time, given by Madan |
| 3955 | main.log.info( "Waiting " + str( retryTime ) + |
| 3956 | "seconds before retrying." ) |
| 3957 | time.sleep( retryTime ) # Due to change in mastership |
| 3958 | output = self.sendline( cmd ) |
| 3959 | assert output is not None, "Error in sendline" |
| 3960 | assert "Command not found:" not in output, output |
| 3961 | assert "Error executing command" not in output, output |
| 3962 | main.log.info( self.name + ": " + output ) |
| 3963 | return output |
| 3964 | except AssertionError: |
| 3965 | main.log.exception( "Error in processing '" + cmd + "' command." ) |
| 3966 | return None |
| 3967 | except TypeError: |
| 3968 | main.log.exception( self.name + ": Object not as expected" ) |
| 3969 | return None |
| 3970 | except pexpect.EOF: |
| 3971 | main.log.error( self.name + ": EOF exception found" ) |
| 3972 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3973 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3974 | except Exception: |
| 3975 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3976 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3977 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3978 | def setTestAdd( self, setName, values ): |
| 3979 | """ |
| 3980 | CLI command to add elements to a distributed set. |
| 3981 | Arguments: |
| 3982 | setName - The name of the set to add to. |
| 3983 | values - The value(s) to add to the set, space seperated. |
| 3984 | Example usages: |
| 3985 | setTestAdd( "set1", "a b c" ) |
| 3986 | setTestAdd( "set2", "1" ) |
| 3987 | returns: |
| 3988 | main.TRUE on success OR |
| 3989 | main.FALSE if elements were already in the set OR |
| 3990 | main.ERROR on error |
| 3991 | """ |
| 3992 | try: |
| 3993 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3994 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3995 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3996 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3997 | if re.search( positiveMatch, output): |
| 3998 | return main.TRUE |
| 3999 | elif re.search( negativeMatch, output): |
| 4000 | return main.FALSE |
| 4001 | else: |
| 4002 | main.log.error( self.name + ": setTestAdd did not" + |
| 4003 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4004 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4005 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4006 | except TypeError: |
| 4007 | main.log.exception( self.name + ": Object not as expected" ) |
| 4008 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4009 | except Exception: |
| 4010 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4011 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4012 | |
| 4013 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4014 | """ |
| 4015 | CLI command to remove elements from a distributed set. |
| 4016 | Required arguments: |
| 4017 | setName - The name of the set to remove from. |
| 4018 | values - The value(s) to remove from the set, space seperated. |
| 4019 | Optional arguments: |
| 4020 | clear - Clear all elements from the set |
| 4021 | retain - Retain only the given values. (intersection of the |
| 4022 | original set and the given set) |
| 4023 | returns: |
| 4024 | main.TRUE on success OR |
| 4025 | main.FALSE if the set was not changed OR |
| 4026 | main.ERROR on error |
| 4027 | """ |
| 4028 | try: |
| 4029 | cmdStr = "set-test-remove " |
| 4030 | if clear: |
| 4031 | cmdStr += "-c " + str( setName ) |
| 4032 | elif retain: |
| 4033 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4034 | else: |
| 4035 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4036 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4037 | if clear: |
| 4038 | pattern = "Set " + str( setName ) + " cleared" |
| 4039 | if re.search( pattern, output ): |
| 4040 | return main.TRUE |
| 4041 | elif retain: |
| 4042 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4043 | "only elements of set \[(.*)\]" |
| 4044 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4045 | "retaining only elements of the set " +\ |
| 4046 | "\[(.*)\]" |
| 4047 | if re.search( positivePattern, output ): |
| 4048 | return main.TRUE |
| 4049 | elif re.search( negativePattern, output ): |
| 4050 | return main.FALSE |
| 4051 | else: |
| 4052 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4053 | str( setName ) |
| 4054 | if ( len( values.split() ) == 1 ): |
| 4055 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4056 | str( setName ) |
| 4057 | else: |
| 4058 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4059 | str( setName ) |
| 4060 | if re.search( positivePattern, output ): |
| 4061 | return main.TRUE |
| 4062 | elif re.search( negativePattern, output ): |
| 4063 | return main.FALSE |
| 4064 | main.log.error( self.name + ": setTestRemove did not" + |
| 4065 | " match expected output" ) |
| 4066 | main.log.debug( self.name + " expected: " + pattern ) |
| 4067 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4068 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4069 | except TypeError: |
| 4070 | main.log.exception( self.name + ": Object not as expected" ) |
| 4071 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4072 | except Exception: |
| 4073 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4074 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4075 | |
| 4076 | def setTestGet( self, setName, values="" ): |
| 4077 | """ |
| 4078 | CLI command to get the elements in a distributed set. |
| 4079 | Required arguments: |
| 4080 | setName - The name of the set to remove from. |
| 4081 | Optional arguments: |
| 4082 | values - The value(s) to check if in the set, space seperated. |
| 4083 | returns: |
| 4084 | main.ERROR on error OR |
| 4085 | A list of elements in the set if no optional arguments are |
| 4086 | supplied OR |
| 4087 | A tuple containing the list then: |
| 4088 | main.FALSE if the given values are not in the set OR |
| 4089 | main.TRUE if the given values are in the set OR |
| 4090 | """ |
| 4091 | try: |
| 4092 | values = str( values ).strip() |
| 4093 | setName = str( setName ).strip() |
| 4094 | length = len( values.split() ) |
| 4095 | containsCheck = None |
| 4096 | # Patterns to match |
| 4097 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4098 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4099 | containsTrue = "Set " + setName + " contains the value " + values |
| 4100 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4101 | values |
| 4102 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4103 | setPattern |
| 4104 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4105 | " subset " + setPattern |
| 4106 | |
| 4107 | cmdStr = "set-test-get " |
| 4108 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4109 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4110 | if length == 0: |
| 4111 | match = re.search( pattern, output ) |
| 4112 | else: # if given values |
| 4113 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4114 | patternTrue = pattern + "\r\n" + containsTrue |
| 4115 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4116 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4117 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4118 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4119 | matchTrue = re.search( patternTrue, output ) |
| 4120 | matchFalse = re.search( patternFalse, output ) |
| 4121 | if matchTrue: |
| 4122 | containsCheck = main.TRUE |
| 4123 | match = matchTrue |
| 4124 | elif matchFalse: |
| 4125 | containsCheck = main.FALSE |
| 4126 | match = matchFalse |
| 4127 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4128 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4129 | "expected output" ) |
| 4130 | main.log.debug( self.name + " expected: " + pattern ) |
| 4131 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4132 | match = None |
| 4133 | if match: |
| 4134 | setMatch = match.group( 1 ) |
| 4135 | if setMatch == '': |
| 4136 | setList = [] |
| 4137 | else: |
| 4138 | setList = setMatch.split( ", " ) |
| 4139 | if length > 0: |
| 4140 | return ( setList, containsCheck ) |
| 4141 | else: |
| 4142 | return setList |
| 4143 | else: # no match |
| 4144 | main.log.error( self.name + ": setTestGet did not" + |
| 4145 | " match expected output" ) |
| 4146 | main.log.debug( self.name + " expected: " + pattern ) |
| 4147 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4148 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4149 | except TypeError: |
| 4150 | main.log.exception( self.name + ": Object not as expected" ) |
| 4151 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4152 | except Exception: |
| 4153 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4154 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4155 | |
| 4156 | def setTestSize( self, setName ): |
| 4157 | """ |
| 4158 | CLI command to get the elements in a distributed set. |
| 4159 | Required arguments: |
| 4160 | setName - The name of the set to remove from. |
| 4161 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4162 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4163 | None on error |
| 4164 | """ |
| 4165 | try: |
| 4166 | # TODO: Should this check against the number of elements returned |
| 4167 | # and then return true/false based on that? |
| 4168 | setName = str( setName ).strip() |
| 4169 | # Patterns to match |
| 4170 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4171 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4172 | setPattern |
| 4173 | cmdStr = "set-test-get -s " |
| 4174 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4175 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4176 | match = re.search( pattern, output ) |
| 4177 | if match: |
| 4178 | setSize = int( match.group( 1 ) ) |
| 4179 | setMatch = match.group( 2 ) |
| 4180 | if len( setMatch.split() ) == setSize: |
| 4181 | main.log.info( "The size returned by " + self.name + |
| 4182 | " matches the number of elements in " + |
| 4183 | "the returned set" ) |
| 4184 | else: |
| 4185 | main.log.error( "The size returned by " + self.name + |
| 4186 | " does not match the number of " + |
| 4187 | "elements in the returned set." ) |
| 4188 | return setSize |
| 4189 | else: # no match |
| 4190 | main.log.error( self.name + ": setTestGet did not" + |
| 4191 | " match expected output" ) |
| 4192 | main.log.debug( self.name + " expected: " + pattern ) |
| 4193 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4194 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4195 | except TypeError: |
| 4196 | main.log.exception( self.name + ": Object not as expected" ) |
| 4197 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4198 | except Exception: |
| 4199 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4200 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4201 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4202 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4203 | """ |
| 4204 | Command to list the various counters in the system. |
| 4205 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4206 | if jsonFormat, a string of the json object returned by the cli |
| 4207 | command |
| 4208 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4209 | None on error |
| 4210 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4211 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4212 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4213 | if jsonFormat: |
| 4214 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4215 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4216 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4217 | assert "Command not found:" not in output, output |
| 4218 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4219 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4220 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4221 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4222 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4223 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4224 | except TypeError: |
| 4225 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4226 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4227 | except pexpect.EOF: |
| 4228 | main.log.error( self.name + ": EOF exception found" ) |
| 4229 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4230 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4231 | except Exception: |
| 4232 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4233 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4234 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4235 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4236 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4237 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4238 | Required arguments: |
| 4239 | counter - The name of the counter to increment. |
| 4240 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4241 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4242 | returns: |
| 4243 | integer value of the counter or |
| 4244 | None on Error |
| 4245 | """ |
| 4246 | try: |
| 4247 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4248 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4249 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4250 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4251 | if delta != 1: |
| 4252 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4253 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4254 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4255 | match = re.search( pattern, output ) |
| 4256 | if match: |
| 4257 | return int( match.group( 1 ) ) |
| 4258 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4259 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4260 | " match expected output." ) |
| 4261 | main.log.debug( self.name + " expected: " + pattern ) |
| 4262 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4263 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4264 | except TypeError: |
| 4265 | main.log.exception( self.name + ": Object not as expected" ) |
| 4266 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4267 | except Exception: |
| 4268 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4269 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4270 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4271 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4272 | """ |
| 4273 | CLI command to get a distributed counter then add a delta to it. |
| 4274 | Required arguments: |
| 4275 | counter - The name of the counter to increment. |
| 4276 | Optional arguments: |
| 4277 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4278 | returns: |
| 4279 | integer value of the counter or |
| 4280 | None on Error |
| 4281 | """ |
| 4282 | try: |
| 4283 | counter = str( counter ) |
| 4284 | delta = int( delta ) |
| 4285 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4286 | cmdStr += counter |
| 4287 | if delta != 1: |
| 4288 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4289 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4290 | pattern = counter + " was updated to (-?\d+)" |
| 4291 | match = re.search( pattern, output ) |
| 4292 | if match: |
| 4293 | return int( match.group( 1 ) ) |
| 4294 | else: |
| 4295 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4296 | " match expected output." ) |
| 4297 | main.log.debug( self.name + " expected: " + pattern ) |
| 4298 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4299 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4300 | except TypeError: |
| 4301 | main.log.exception( self.name + ": Object not as expected" ) |
| 4302 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4303 | except Exception: |
| 4304 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4305 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4306 | |
| 4307 | def valueTestGet( self, valueName ): |
| 4308 | """ |
| 4309 | CLI command to get the value of an atomic value. |
| 4310 | Required arguments: |
| 4311 | valueName - The name of the value to get. |
| 4312 | returns: |
| 4313 | string value of the value or |
| 4314 | None on Error |
| 4315 | """ |
| 4316 | try: |
| 4317 | valueName = str( valueName ) |
| 4318 | cmdStr = "value-test " |
| 4319 | operation = "get" |
| 4320 | cmdStr = "value-test {} {}".format( valueName, |
| 4321 | operation ) |
| 4322 | output = self.distPrimitivesSend( cmdStr ) |
| 4323 | pattern = "(\w+)" |
| 4324 | match = re.search( pattern, output ) |
| 4325 | if match: |
| 4326 | return match.group( 1 ) |
| 4327 | else: |
| 4328 | main.log.error( self.name + ": valueTestGet did not" + |
| 4329 | " match expected output." ) |
| 4330 | main.log.debug( self.name + " expected: " + pattern ) |
| 4331 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4332 | return None |
| 4333 | except TypeError: |
| 4334 | main.log.exception( self.name + ": Object not as expected" ) |
| 4335 | return None |
| 4336 | except Exception: |
| 4337 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4338 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4339 | |
| 4340 | def valueTestSet( self, valueName, newValue ): |
| 4341 | """ |
| 4342 | CLI command to set the value of an atomic value. |
| 4343 | Required arguments: |
| 4344 | valueName - The name of the value to set. |
| 4345 | newValue - The value to assign to the given value. |
| 4346 | returns: |
| 4347 | main.TRUE on success or |
| 4348 | main.ERROR on Error |
| 4349 | """ |
| 4350 | try: |
| 4351 | valueName = str( valueName ) |
| 4352 | newValue = str( newValue ) |
| 4353 | operation = "set" |
| 4354 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4355 | operation, |
| 4356 | newValue ) |
| 4357 | output = self.distPrimitivesSend( cmdStr ) |
| 4358 | if output is not None: |
| 4359 | return main.TRUE |
| 4360 | else: |
| 4361 | return main.ERROR |
| 4362 | except TypeError: |
| 4363 | main.log.exception( self.name + ": Object not as expected" ) |
| 4364 | return main.ERROR |
| 4365 | except Exception: |
| 4366 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4367 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4368 | |
| 4369 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4370 | """ |
| 4371 | CLI command to compareAndSet the value of an atomic value. |
| 4372 | Required arguments: |
| 4373 | valueName - The name of the value. |
| 4374 | oldValue - Compare the current value of the atomic value to this |
| 4375 | newValue - If the value equals oldValue, set the value to newValue |
| 4376 | returns: |
| 4377 | main.TRUE on success or |
| 4378 | main.FALSE on failure or |
| 4379 | main.ERROR on Error |
| 4380 | """ |
| 4381 | try: |
| 4382 | valueName = str( valueName ) |
| 4383 | oldValue = str( oldValue ) |
| 4384 | newValue = str( newValue ) |
| 4385 | operation = "compareAndSet" |
| 4386 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4387 | operation, |
| 4388 | oldValue, |
| 4389 | newValue ) |
| 4390 | output = self.distPrimitivesSend( cmdStr ) |
| 4391 | pattern = "(\w+)" |
| 4392 | match = re.search( pattern, output ) |
| 4393 | if match: |
| 4394 | result = match.group( 1 ) |
| 4395 | if result == "true": |
| 4396 | return main.TRUE |
| 4397 | elif result == "false": |
| 4398 | return main.FALSE |
| 4399 | else: |
| 4400 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4401 | " match expected output." ) |
| 4402 | main.log.debug( self.name + " expected: " + pattern ) |
| 4403 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4404 | return main.ERROR |
| 4405 | except TypeError: |
| 4406 | main.log.exception( self.name + ": Object not as expected" ) |
| 4407 | return main.ERROR |
| 4408 | except Exception: |
| 4409 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4410 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4411 | |
| 4412 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4413 | """ |
| 4414 | CLI command to getAndSet the value of an atomic value. |
| 4415 | Required arguments: |
| 4416 | valueName - The name of the value to get. |
| 4417 | newValue - The value to assign to the given value |
| 4418 | returns: |
| 4419 | string value of the value or |
| 4420 | None on Error |
| 4421 | """ |
| 4422 | try: |
| 4423 | valueName = str( valueName ) |
| 4424 | cmdStr = "value-test " |
| 4425 | operation = "getAndSet" |
| 4426 | cmdStr += valueName + " " + operation |
| 4427 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4428 | operation, |
| 4429 | newValue ) |
| 4430 | output = self.distPrimitivesSend( cmdStr ) |
| 4431 | pattern = "(\w+)" |
| 4432 | match = re.search( pattern, output ) |
| 4433 | if match: |
| 4434 | return match.group( 1 ) |
| 4435 | else: |
| 4436 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4437 | " match expected output." ) |
| 4438 | main.log.debug( self.name + " expected: " + pattern ) |
| 4439 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4440 | return None |
| 4441 | except TypeError: |
| 4442 | main.log.exception( self.name + ": Object not as expected" ) |
| 4443 | return None |
| 4444 | except Exception: |
| 4445 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4446 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4447 | |
| 4448 | def valueTestDestroy( self, valueName ): |
| 4449 | """ |
| 4450 | CLI command to destroy an atomic value. |
| 4451 | Required arguments: |
| 4452 | valueName - The name of the value to destroy. |
| 4453 | returns: |
| 4454 | main.TRUE on success or |
| 4455 | main.ERROR on Error |
| 4456 | """ |
| 4457 | try: |
| 4458 | valueName = str( valueName ) |
| 4459 | cmdStr = "value-test " |
| 4460 | operation = "destroy" |
| 4461 | cmdStr += valueName + " " + operation |
| 4462 | output = self.distPrimitivesSend( cmdStr ) |
| 4463 | if output is not None: |
| 4464 | return main.TRUE |
| 4465 | else: |
| 4466 | return main.ERROR |
| 4467 | except TypeError: |
| 4468 | main.log.exception( self.name + ": Object not as expected" ) |
| 4469 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4470 | except Exception: |
| 4471 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4472 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4473 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4474 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4475 | """ |
| 4476 | Description: Execute summary command in onos |
| 4477 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4478 | no output |
| 4479 | |
| 4480 | """ |
| 4481 | try: |
| 4482 | cmdStr = "summary" |
| 4483 | if jsonFormat: |
| 4484 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4485 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4486 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4487 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4488 | assert "Error:" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4489 | if not handle: |
| 4490 | main.log.error( self.name + ": There is no output in " + |
| 4491 | "summary command" ) |
| 4492 | return main.FALSE |
| 4493 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4494 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4495 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4496 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4497 | except TypeError: |
| 4498 | main.log.exception( self.name + ": Object not as expected" ) |
| 4499 | return None |
| 4500 | except pexpect.EOF: |
| 4501 | main.log.error( self.name + ": EOF exception found" ) |
| 4502 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4503 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4504 | except Exception: |
| 4505 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4506 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4507 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4508 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4509 | """ |
| 4510 | CLI command to get the value of a key in a consistent map using |
| 4511 | transactions. This a test function and can only get keys from the |
| 4512 | test map hard coded into the cli command |
| 4513 | Required arguments: |
| 4514 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4515 | returns: |
| 4516 | The string value of the key or |
| 4517 | None on Error |
| 4518 | """ |
| 4519 | try: |
| 4520 | keyName = str( keyName ) |
| 4521 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4522 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4523 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4524 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4525 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4526 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4527 | return None |
| 4528 | else: |
| 4529 | match = re.search( pattern, output ) |
| 4530 | if match: |
| 4531 | return match.groupdict()[ 'value' ] |
| 4532 | else: |
| 4533 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4534 | " match expected output." ) |
| 4535 | main.log.debug( self.name + " expected: " + pattern ) |
| 4536 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4537 | return None |
| 4538 | except TypeError: |
| 4539 | main.log.exception( self.name + ": Object not as expected" ) |
| 4540 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4541 | except Exception: |
| 4542 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4543 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4544 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4545 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4546 | """ |
| 4547 | CLI command to put a value into 'numKeys' number of keys in a |
| 4548 | consistent map using transactions. This a test function and can only |
| 4549 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4550 | Required arguments: |
| 4551 | numKeys - Number of keys to add the value to |
| 4552 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4553 | returns: |
| 4554 | A dictionary whose keys are the name of the keys put into the map |
| 4555 | and the values of the keys are dictionaries whose key-values are |
| 4556 | 'value': value put into map and optionaly |
| 4557 | 'oldValue': Previous value in the key or |
| 4558 | None on Error |
| 4559 | |
| 4560 | Example output |
| 4561 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4562 | 'Key2': {'value': 'Testing'} } |
| 4563 | """ |
| 4564 | try: |
| 4565 | numKeys = str( numKeys ) |
| 4566 | value = str( value ) |
| 4567 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4568 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4569 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4570 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4571 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4572 | results = {} |
| 4573 | for line in output.splitlines(): |
| 4574 | new = re.search( newPattern, line ) |
| 4575 | updated = re.search( updatedPattern, line ) |
| 4576 | if new: |
| 4577 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4578 | elif updated: |
| 4579 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4580 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4581 | else: |
| 4582 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4583 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4584 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4585 | newPattern, |
| 4586 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4587 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4588 | return results |
| 4589 | except TypeError: |
| 4590 | main.log.exception( self.name + ": Object not as expected" ) |
| 4591 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4592 | except Exception: |
| 4593 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4594 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4595 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4596 | def maps( self, jsonFormat=True ): |
| 4597 | """ |
| 4598 | Description: Returns result of onos:maps |
| 4599 | Optional: |
| 4600 | * jsonFormat: enable json formatting of output |
| 4601 | """ |
| 4602 | try: |
| 4603 | cmdStr = "maps" |
| 4604 | if jsonFormat: |
| 4605 | cmdStr += " -j" |
| 4606 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4607 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4608 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4609 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4610 | except AssertionError: |
| 4611 | main.log.exception( "" ) |
| 4612 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4613 | except TypeError: |
| 4614 | main.log.exception( self.name + ": Object not as expected" ) |
| 4615 | return None |
| 4616 | except pexpect.EOF: |
| 4617 | main.log.error( self.name + ": EOF exception found" ) |
| 4618 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4619 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4620 | except Exception: |
| 4621 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4622 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4623 | |
| 4624 | def getSwController( self, uri, jsonFormat=True ): |
| 4625 | """ |
| 4626 | Descrition: Gets the controller information from the device |
| 4627 | """ |
| 4628 | try: |
| 4629 | cmd = "device-controllers " |
| 4630 | if jsonFormat: |
| 4631 | cmd += "-j " |
| 4632 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4633 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4634 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4635 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4636 | except AssertionError: |
| 4637 | main.log.exception( "" ) |
| 4638 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4639 | except TypeError: |
| 4640 | main.log.exception( self.name + ": Object not as expected" ) |
| 4641 | return None |
| 4642 | except pexpect.EOF: |
| 4643 | main.log.error( self.name + ": EOF exception found" ) |
| 4644 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4645 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4646 | except Exception: |
| 4647 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4648 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4649 | |
| 4650 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4651 | """ |
| 4652 | Descrition: sets the controller(s) for the specified device |
| 4653 | |
| 4654 | Parameters: |
| 4655 | Required: uri - String: The uri of the device(switch). |
| 4656 | ip - String or List: The ip address of the controller. |
| 4657 | This parameter can be formed in a couple of different ways. |
| 4658 | VALID: |
| 4659 | 10.0.0.1 - just the ip address |
| 4660 | tcp:10.0.0.1 - the protocol and the ip address |
| 4661 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4662 | so that you can add controllers with different |
| 4663 | protocols and ports |
| 4664 | INVALID: |
| 4665 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4666 | |
| 4667 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4668 | port - The port number. |
| 4669 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4670 | |
| 4671 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4672 | """ |
| 4673 | try: |
| 4674 | cmd = "device-setcontrollers" |
| 4675 | |
| 4676 | if jsonFormat: |
| 4677 | cmd += " -j" |
| 4678 | cmd += " " + uri |
| 4679 | if isinstance( ip, str ): |
| 4680 | ip = [ip] |
| 4681 | for item in ip: |
| 4682 | if ":" in item: |
| 4683 | sitem = item.split( ":" ) |
| 4684 | if len(sitem) == 3: |
| 4685 | cmd += " " + item |
| 4686 | elif "." in sitem[1]: |
| 4687 | cmd += " {}:{}".format(item, port) |
| 4688 | else: |
| 4689 | main.log.error( "Malformed entry: " + item ) |
| 4690 | raise TypeError |
| 4691 | else: |
| 4692 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4693 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4694 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4695 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4696 | if "Error" in response: |
| 4697 | main.log.error( response ) |
| 4698 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4699 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4700 | except AssertionError: |
| 4701 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4702 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4703 | except TypeError: |
| 4704 | main.log.exception( self.name + ": Object not as expected" ) |
| 4705 | return main.FALSE |
| 4706 | except pexpect.EOF: |
| 4707 | main.log.error( self.name + ": EOF exception found" ) |
| 4708 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4709 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4710 | except Exception: |
| 4711 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4712 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4713 | |
| 4714 | def removeDevice( self, device ): |
| 4715 | ''' |
| 4716 | Description: |
| 4717 | Remove a device from ONOS by passing the uri of the device(s). |
| 4718 | Parameters: |
| 4719 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
| 4720 | Returns: |
| 4721 | Returns main.FALSE if an exception is thrown or an error is present |
| 4722 | in the response. Otherwise, returns main.TRUE. |
| 4723 | NOTE: |
| 4724 | If a host cannot be removed, then this function will return main.FALSE |
| 4725 | ''' |
| 4726 | try: |
| 4727 | if type( device ) is str: |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 4728 | deviceStr = device |
| 4729 | device = [] |
| 4730 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4731 | |
| 4732 | for d in device: |
| 4733 | time.sleep( 1 ) |
| 4734 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4735 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4736 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4737 | if "Error" in response: |
| 4738 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4739 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4740 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4741 | except AssertionError: |
| 4742 | main.log.exception( "" ) |
| 4743 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4744 | except TypeError: |
| 4745 | main.log.exception( self.name + ": Object not as expected" ) |
| 4746 | return main.FALSE |
| 4747 | except pexpect.EOF: |
| 4748 | main.log.error( self.name + ": EOF exception found" ) |
| 4749 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4750 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4751 | except Exception: |
| 4752 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4753 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4754 | |
| 4755 | def removeHost( self, host ): |
| 4756 | ''' |
| 4757 | Description: |
| 4758 | Remove a host from ONOS by passing the id of the host(s) |
| 4759 | Parameters: |
| 4760 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
| 4761 | Returns: |
| 4762 | Returns main.FALSE if an exception is thrown or an error is present |
| 4763 | in the response. Otherwise, returns main.TRUE. |
| 4764 | NOTE: |
| 4765 | If a host cannot be removed, then this function will return main.FALSE |
| 4766 | ''' |
| 4767 | try: |
| 4768 | if type( host ) is str: |
| 4769 | host = list( host ) |
| 4770 | |
| 4771 | for h in host: |
| 4772 | time.sleep( 1 ) |
| 4773 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4774 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4775 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4776 | if "Error" in response: |
| 4777 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 4778 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4779 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4780 | except AssertionError: |
| 4781 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4782 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4783 | except TypeError: |
| 4784 | main.log.exception( self.name + ": Object not as expected" ) |
| 4785 | return main.FALSE |
| 4786 | except pexpect.EOF: |
| 4787 | main.log.error( self.name + ": EOF exception found" ) |
| 4788 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4789 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4790 | except Exception: |
| 4791 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4792 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4793 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4794 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4795 | ''' |
| 4796 | Description: |
| 4797 | Bring link down or up in the null-provider. |
| 4798 | params: |
| 4799 | begin - (string) One end of a device or switch. |
| 4800 | end - (string) the other end of the device or switch |
| 4801 | returns: |
| 4802 | main.TRUE if no exceptions were thrown and no Errors are |
| 4803 | present in the resoponse. Otherwise, returns main.FALSE |
| 4804 | ''' |
| 4805 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4806 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4807 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4808 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4809 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4810 | if "Error" in response or "Failure" in response: |
| 4811 | main.log.error( response ) |
| 4812 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4813 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4814 | except AssertionError: |
| 4815 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4816 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4817 | except TypeError: |
| 4818 | main.log.exception( self.name + ": Object not as expected" ) |
| 4819 | return main.FALSE |
| 4820 | except pexpect.EOF: |
| 4821 | main.log.error( self.name + ": EOF exception found" ) |
| 4822 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4823 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4824 | except Exception: |
| 4825 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4826 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4827 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4828 | def portstate( self, dpid, port, state ): |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4829 | ''' |
| 4830 | Description: |
| 4831 | Changes the state of port in an OF switch by means of the |
| 4832 | PORTSTATUS OF messages. |
| 4833 | params: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4834 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 4835 | port - (string) target port in the device. Ex: '2' |
| 4836 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4837 | returns: |
| 4838 | main.TRUE if no exceptions were thrown and no Errors are |
| 4839 | present in the resoponse. Otherwise, returns main.FALSE |
| 4840 | ''' |
| 4841 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4842 | state = state.lower() |
| 4843 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4844 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4845 | response = self.sendline( cmd, showResponse=True ) |
| 4846 | assert response is not None, "Error in sendline" |
| 4847 | assert "Command not found:" not in response, response |
| 4848 | if "Error" in response or "Failure" in response: |
| 4849 | main.log.error( response ) |
| 4850 | return main.FALSE |
| 4851 | return main.TRUE |
| 4852 | except AssertionError: |
| 4853 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4854 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4855 | except TypeError: |
| 4856 | main.log.exception( self.name + ": Object not as expected" ) |
| 4857 | return main.FALSE |
| 4858 | except pexpect.EOF: |
| 4859 | main.log.error( self.name + ": EOF exception found" ) |
| 4860 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4861 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4862 | except Exception: |
| 4863 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4864 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4865 | |
| 4866 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 4867 | """ |
| 4868 | Set the logging level to lvl for a specific app |
| 4869 | returns main.TRUE on success |
| 4870 | returns main.FALSE if Error occurred |
| 4871 | if noExit is True, TestON will not exit, but clean up |
| 4872 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 4873 | Level defaults to INFO |
| 4874 | """ |
| 4875 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4876 | self.handle.sendline( "log:set %s %s" % ( level, app ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4877 | self.handle.expect( "onos>" ) |
| 4878 | |
| 4879 | response = self.handle.before |
| 4880 | if re.search( "Error", response ): |
| 4881 | return main.FALSE |
| 4882 | return main.TRUE |
| 4883 | except pexpect.TIMEOUT: |
| 4884 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4885 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4886 | except pexpect.EOF: |
| 4887 | main.log.error( self.name + ": EOF exception found" ) |
| 4888 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4889 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4890 | except Exception: |
| 4891 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4892 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4893 | |
| 4894 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 4895 | """ |
| 4896 | Return a dictionary which describes the latest network topology data as a |
| 4897 | graph. |
| 4898 | An example of the dictionary: |
| 4899 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 4900 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 4901 | Each vertex should at least have an 'edges' attribute which describes the |
| 4902 | adjacency information. The value of 'edges' attribute is also represented by |
| 4903 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 4904 | list of attributes. |
| 4905 | An example of the edges dictionary: |
| 4906 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 4907 | vertex3: { 'port': ..., 'weight': ... } } |
| 4908 | If includeHost == True, all hosts (and host-switch links) will be included |
| 4909 | in topology data. |
| 4910 | """ |
| 4911 | graphDict = {} |
| 4912 | try: |
| 4913 | links = self.links() |
| 4914 | links = json.loads( links ) |
| 4915 | devices = self.devices() |
| 4916 | devices = json.loads( devices ) |
| 4917 | idToDevice = {} |
| 4918 | for device in devices: |
| 4919 | idToDevice[ device[ 'id' ] ] = device |
| 4920 | if includeHost: |
| 4921 | hosts = self.hosts() |
| 4922 | # FIXME: support 'includeHost' argument |
| 4923 | for link in links: |
| 4924 | nodeA = link[ 'src' ][ 'device' ] |
| 4925 | nodeB = link[ 'dst' ][ 'device' ] |
| 4926 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4927 | if nodeA not in graphDict.keys(): |
| 4928 | graphDict[ nodeA ] = { 'edges': {}, |
| 4929 | 'dpid': idToDevice[ nodeA ][ 'id' ][3:], |
| 4930 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 4931 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 4932 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 4933 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 4934 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 4935 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 4936 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 4937 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
| 4938 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4939 | else: |
| 4940 | # Assert nodeB is not connected to any current links of nodeA |
| 4941 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4942 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 4943 | 'type': link[ 'type' ], |
| 4944 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4945 | return graphDict |
| 4946 | except ( TypeError, ValueError ): |
| 4947 | main.log.exception( self.name + ": Object not as expected" ) |
| 4948 | return None |
| 4949 | except KeyError: |
| 4950 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4951 | return None |
| 4952 | except AssertionError: |
| 4953 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4954 | return None |
| 4955 | except pexpect.EOF: |
| 4956 | main.log.error( self.name + ": EOF exception found" ) |
| 4957 | main.log.error( self.name + ": " + self.handle.before ) |
| 4958 | return None |
| 4959 | except Exception: |
| 4960 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4961 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 4962 | |
| 4963 | def getIntentPerfSummary( self ): |
| 4964 | ''' |
| 4965 | Send command to check intent-perf summary |
| 4966 | Returns: dictionary for intent-perf summary |
| 4967 | if something wrong, function will return None |
| 4968 | ''' |
| 4969 | cmd = "intent-perf -s" |
| 4970 | respDic = {} |
| 4971 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 4972 | assert resp is not None, "Error in sendline" |
| 4973 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 4974 | try: |
| 4975 | # Generate the dictionary to return |
| 4976 | for l in resp.split( "\n" ): |
| 4977 | # Delete any white space in line |
| 4978 | temp = re.sub( r'\s+', '', l ) |
| 4979 | temp = temp.split( ":" ) |
| 4980 | respDic[ temp[0] ] = temp[ 1 ] |
| 4981 | |
| 4982 | except (TypeError, ValueError): |
| 4983 | main.log.exception( self.name + ": Object not as expected" ) |
| 4984 | return None |
| 4985 | except KeyError: |
| 4986 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4987 | return None |
| 4988 | except AssertionError: |
| 4989 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4990 | return None |
| 4991 | except pexpect.EOF: |
| 4992 | main.log.error( self.name + ": EOF exception found" ) |
| 4993 | main.log.error( self.name + ": " + self.handle.before ) |
| 4994 | return None |
| 4995 | except Exception: |
| 4996 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4997 | return None |
| 4998 | return respDic |
| 4999 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5000 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5001 | """ |
| 5002 | Searches the latest ONOS log file for the given search term and |
| 5003 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5004 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5005 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5006 | searchTerm: |
| 5007 | The string to grep from the ONOS log. |
| 5008 | startLine: |
| 5009 | The term that decides which line is the start to search the searchTerm in |
| 5010 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5011 | logNum: |
| 5012 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5013 | information.Because of this, search mutiply logs is necessary to capture |
| 5014 | the right result. logNum is the number of karaf logs that we need to search |
| 5015 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5016 | mode: |
| 5017 | all: return all the strings that contain the search term |
| 5018 | last: return the last string that contains the search term |
| 5019 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5020 | num: return the number of times that the searchTerm appears in the log |
| 5021 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5022 | """ |
| 5023 | try: |
| 5024 | assert type( searchTerm ) is str |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5025 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5026 | logPath = '/opt/onos/log/karaf.log.' |
| 5027 | logPaths = '/opt/onos/log/karaf.log' |
| 5028 | for i in range( 1, logNum ): |
| 5029 | logPaths = logPath + str( i ) + " " + logPaths |
| 5030 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5031 | if startLine: |
| 5032 | # 100000000 is just a extreme large number to make sure this function can grep all the lines after startLine |
| 5033 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5034 | if mode == 'all': |
| 5035 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5036 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5037 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5038 | elif mode == 'first': |
| 5039 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5040 | elif mode == 'num': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5041 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
You Wang | 118ba58 | 2017-01-02 17:14:43 -0800 | [diff] [blame] | 5042 | num = self.sendline( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5043 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5044 | elif mode == 'total': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5045 | totalLines = self.sendline( "cat /opt/onos/log/karaf.log | wc -l" ) |
| 5046 | return int(totalLines) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5047 | else: |
| 5048 | main.log.error( self.name + " unsupported mode" ) |
| 5049 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5050 | before = self.sendline( cmd ) |
| 5051 | before = before.splitlines() |
| 5052 | # make sure the returned list only contains the search term |
| 5053 | returnLines = [line for line in before if searchTerm in line] |
| 5054 | return returnLines |
| 5055 | except AssertionError: |
| 5056 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5057 | return None |
| 5058 | except pexpect.EOF: |
| 5059 | main.log.error( self.name + ": EOF exception found" ) |
| 5060 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5061 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5062 | except pexpect.TIMEOUT: |
| 5063 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5064 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5065 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5066 | except Exception: |
| 5067 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5068 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5069 | |
| 5070 | def vplsShow( self, jsonFormat=True ): |
| 5071 | """ |
| 5072 | Description: Returns result of onos:vpls show, which should list the |
| 5073 | configured VPLS networks and the assigned interfaces. |
| 5074 | Optional: |
| 5075 | * jsonFormat: enable json formatting of output |
| 5076 | Returns: |
| 5077 | The output of the command or None on error. |
| 5078 | """ |
| 5079 | try: |
| 5080 | cmdStr = "vpls show" |
| 5081 | if jsonFormat: |
| 5082 | raise NotImplementedError |
| 5083 | cmdStr += " -j" |
| 5084 | handle = self.sendline( cmdStr ) |
| 5085 | assert handle is not None, "Error in sendline" |
| 5086 | assert "Command not found:" not in handle, handle |
| 5087 | return handle |
| 5088 | except AssertionError: |
| 5089 | main.log.exception( "" ) |
| 5090 | return None |
| 5091 | except TypeError: |
| 5092 | main.log.exception( self.name + ": Object not as expected" ) |
| 5093 | return None |
| 5094 | except pexpect.EOF: |
| 5095 | main.log.error( self.name + ": EOF exception found" ) |
| 5096 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5097 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5098 | except NotImplementedError: |
| 5099 | main.log.exception( self.name + ": Json output not supported") |
| 5100 | return None |
| 5101 | except Exception: |
| 5102 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5103 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5104 | |
| 5105 | def parseVplsShow( self ): |
| 5106 | """ |
| 5107 | Parse the cli output of 'vpls show' into json output. This is required |
| 5108 | as there is currently no json output available. |
| 5109 | """ |
| 5110 | try: |
| 5111 | output = [] |
| 5112 | raw = self.vplsShow( jsonFormat=False ) |
| 5113 | namePat = "VPLS name: (?P<name>\w+)" |
| 5114 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5115 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5116 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5117 | mIter = re.finditer( pattern, raw ) |
| 5118 | for match in mIter: |
| 5119 | item = {} |
| 5120 | item[ 'name' ] = match.group( 'name' ) |
| 5121 | ifaces = match.group( 'interfaces' ).split( ', ') |
| 5122 | if ifaces == [ "" ]: |
| 5123 | ifaces = [] |
| 5124 | item[ 'interfaces' ] = ifaces |
| 5125 | encap = match.group( 'encap' ) |
| 5126 | if encap != 'NONE': |
| 5127 | item[ 'encapsulation' ] = encap.lower() |
| 5128 | output.append( item ) |
| 5129 | return output |
| 5130 | except Exception: |
| 5131 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5132 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5133 | |
| 5134 | def vplsList( self, jsonFormat=True ): |
| 5135 | """ |
| 5136 | Description: Returns result of onos:vpls list, which should list the |
| 5137 | configured VPLS networks. |
| 5138 | Optional: |
| 5139 | * jsonFormat: enable json formatting of output |
| 5140 | """ |
| 5141 | try: |
| 5142 | cmdStr = "vpls list" |
| 5143 | if jsonFormat: |
| 5144 | raise NotImplementedError |
| 5145 | cmdStr += " -j" |
| 5146 | handle = self.sendline( cmdStr ) |
| 5147 | assert handle is not None, "Error in sendline" |
| 5148 | assert "Command not found:" not in handle, handle |
| 5149 | return handle |
| 5150 | except AssertionError: |
| 5151 | main.log.exception( "" ) |
| 5152 | return None |
| 5153 | except TypeError: |
| 5154 | main.log.exception( self.name + ": Object not as expected" ) |
| 5155 | return None |
| 5156 | except pexpect.EOF: |
| 5157 | main.log.error( self.name + ": EOF exception found" ) |
| 5158 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5159 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5160 | except NotImplementedError: |
| 5161 | main.log.exception( self.name + ": Json output not supported") |
| 5162 | return None |
| 5163 | except Exception: |
| 5164 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5165 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5166 | |
| 5167 | def vplsCreate( self, network ): |
| 5168 | """ |
| 5169 | CLI command to create a new VPLS network. |
| 5170 | Required arguments: |
| 5171 | network - String name of the network to create. |
| 5172 | returns: |
| 5173 | main.TRUE on success and main.FALSE on failure |
| 5174 | """ |
| 5175 | try: |
| 5176 | network = str( network ) |
| 5177 | cmdStr = "vpls create " |
| 5178 | cmdStr += network |
| 5179 | output = self.sendline( cmdStr ) |
| 5180 | assert output is not None, "Error in sendline" |
| 5181 | assert "Command not found:" not in output, output |
| 5182 | assert "Error executing command" not in output, output |
| 5183 | assert "VPLS already exists:" not in output, output |
| 5184 | return main.TRUE |
| 5185 | except AssertionError: |
| 5186 | main.log.exception( "" ) |
| 5187 | return main.FALSE |
| 5188 | except TypeError: |
| 5189 | main.log.exception( self.name + ": Object not as expected" ) |
| 5190 | return main.FALSE |
| 5191 | except pexpect.EOF: |
| 5192 | main.log.error( self.name + ": EOF exception found" ) |
| 5193 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5194 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5195 | except Exception: |
| 5196 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5197 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5198 | |
| 5199 | def vplsDelete( self, network ): |
| 5200 | """ |
| 5201 | CLI command to delete a VPLS network. |
| 5202 | Required arguments: |
| 5203 | network - Name of the network to delete. |
| 5204 | returns: |
| 5205 | main.TRUE on success and main.FALSE on failure |
| 5206 | """ |
| 5207 | try: |
| 5208 | network = str( network ) |
| 5209 | cmdStr = "vpls delete " |
| 5210 | cmdStr += network |
| 5211 | output = self.sendline( cmdStr ) |
| 5212 | assert output is not None, "Error in sendline" |
| 5213 | assert "Command not found:" not in output, output |
| 5214 | assert "Error executing command" not in output, output |
| 5215 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5216 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5217 | return main.TRUE |
| 5218 | except AssertionError: |
| 5219 | main.log.exception( "" ) |
| 5220 | return main.FALSE |
| 5221 | except TypeError: |
| 5222 | main.log.exception( self.name + ": Object not as expected" ) |
| 5223 | return main.FALSE |
| 5224 | except pexpect.EOF: |
| 5225 | main.log.error( self.name + ": EOF exception found" ) |
| 5226 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5227 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5228 | except Exception: |
| 5229 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5230 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5231 | |
| 5232 | def vplsAddIface( self, network, iface ): |
| 5233 | """ |
| 5234 | CLI command to add an interface to a VPLS network. |
| 5235 | Required arguments: |
| 5236 | network - Name of the network to add the interface to. |
| 5237 | iface - The ONOS name for an interface. |
| 5238 | returns: |
| 5239 | main.TRUE on success and main.FALSE on failure |
| 5240 | """ |
| 5241 | try: |
| 5242 | network = str( network ) |
| 5243 | iface = str( iface ) |
| 5244 | cmdStr = "vpls add-if " |
| 5245 | cmdStr += network + " " + iface |
| 5246 | output = self.sendline( cmdStr ) |
| 5247 | assert output is not None, "Error in sendline" |
| 5248 | assert "Command not found:" not in output, output |
| 5249 | assert "Error executing command" not in output, output |
| 5250 | assert "already associated to network" not in output, output |
| 5251 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5252 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5253 | return main.TRUE |
| 5254 | except AssertionError: |
| 5255 | main.log.exception( "" ) |
| 5256 | return main.FALSE |
| 5257 | except TypeError: |
| 5258 | main.log.exception( self.name + ": Object not as expected" ) |
| 5259 | return main.FALSE |
| 5260 | except pexpect.EOF: |
| 5261 | main.log.error( self.name + ": EOF exception found" ) |
| 5262 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5263 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5264 | except Exception: |
| 5265 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5266 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5267 | |
| 5268 | def vplsRemIface( self, network, iface ): |
| 5269 | """ |
| 5270 | CLI command to remove an interface from a VPLS network. |
| 5271 | Required arguments: |
| 5272 | network - Name of the network to remove the interface from. |
| 5273 | iface - Name of the interface to remove. |
| 5274 | returns: |
| 5275 | main.TRUE on success and main.FALSE on failure |
| 5276 | """ |
| 5277 | try: |
| 5278 | iface = str( iface ) |
| 5279 | cmdStr = "vpls rem-if " |
| 5280 | cmdStr += network + " " + iface |
| 5281 | output = self.sendline( cmdStr ) |
| 5282 | assert output is not None, "Error in sendline" |
| 5283 | assert "Command not found:" not in output, output |
| 5284 | assert "Error executing command" not in output, output |
| 5285 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5286 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5287 | return main.TRUE |
| 5288 | except AssertionError: |
| 5289 | main.log.exception( "" ) |
| 5290 | return main.FALSE |
| 5291 | except TypeError: |
| 5292 | main.log.exception( self.name + ": Object not as expected" ) |
| 5293 | return main.FALSE |
| 5294 | except pexpect.EOF: |
| 5295 | main.log.error( self.name + ": EOF exception found" ) |
| 5296 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5297 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5298 | except Exception: |
| 5299 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5300 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5301 | |
| 5302 | def vplsClean( self ): |
| 5303 | """ |
| 5304 | Description: Clears the VPLS app configuration. |
| 5305 | Returns: main.TRUE on success and main.FALSE on failure |
| 5306 | """ |
| 5307 | try: |
| 5308 | cmdStr = "vpls clean" |
| 5309 | handle = self.sendline( cmdStr ) |
| 5310 | assert handle is not None, "Error in sendline" |
| 5311 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5312 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5313 | return handle |
| 5314 | except AssertionError: |
| 5315 | main.log.exception( "" ) |
| 5316 | return main.FALSE |
| 5317 | except TypeError: |
| 5318 | main.log.exception( self.name + ": Object not as expected" ) |
| 5319 | return main.FALSE |
| 5320 | except pexpect.EOF: |
| 5321 | main.log.error( self.name + ": EOF exception found" ) |
| 5322 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5323 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5324 | except Exception: |
| 5325 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5326 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5327 | |
| 5328 | def vplsSetEncap( self, network, encapType ): |
| 5329 | """ |
| 5330 | CLI command to add an interface to a VPLS network. |
| 5331 | Required arguments: |
| 5332 | network - Name of the network to create. |
| 5333 | encapType - Type of encapsulation. |
| 5334 | returns: |
| 5335 | main.TRUE on success and main.FALSE on failure |
| 5336 | """ |
| 5337 | try: |
| 5338 | network = str( network ) |
| 5339 | encapType = str( encapType ).upper() |
| 5340 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5341 | cmdStr = "vpls set-encap " |
| 5342 | cmdStr += network + " " + encapType |
| 5343 | output = self.sendline( cmdStr ) |
| 5344 | assert output is not None, "Error in sendline" |
| 5345 | assert "Command not found:" not in output, output |
| 5346 | assert "Error executing command" not in output, output |
| 5347 | assert "already associated to network" not in output, output |
| 5348 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5349 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5350 | return main.TRUE |
| 5351 | except AssertionError: |
| 5352 | main.log.exception( "" ) |
| 5353 | return main.FALSE |
| 5354 | except TypeError: |
| 5355 | main.log.exception( self.name + ": Object not as expected" ) |
| 5356 | return main.FALSE |
| 5357 | except pexpect.EOF: |
| 5358 | main.log.error( self.name + ": EOF exception found" ) |
| 5359 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5360 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5361 | except Exception: |
| 5362 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5363 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5364 | |
| 5365 | def interfaces( self, jsonFormat=True ): |
| 5366 | """ |
| 5367 | Description: Returns result of interfaces command. |
| 5368 | Optional: |
| 5369 | * jsonFormat: enable json formatting of output |
| 5370 | Returns: |
| 5371 | The output of the command or None on error. |
| 5372 | """ |
| 5373 | try: |
| 5374 | cmdStr = "interfaces" |
| 5375 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5376 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5377 | cmdStr += " -j" |
| 5378 | handle = self.sendline( cmdStr ) |
| 5379 | assert handle is not None, "Error in sendline" |
| 5380 | assert "Command not found:" not in handle, handle |
| 5381 | return handle |
| 5382 | except AssertionError: |
| 5383 | main.log.exception( "" ) |
| 5384 | return None |
| 5385 | except TypeError: |
| 5386 | main.log.exception( self.name + ": Object not as expected" ) |
| 5387 | return None |
| 5388 | except pexpect.EOF: |
| 5389 | main.log.error( self.name + ": EOF exception found" ) |
| 5390 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5391 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5392 | except NotImplementedError: |
| 5393 | main.log.exception( self.name + ": Json output not supported") |
| 5394 | return None |
| 5395 | except Exception: |
| 5396 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5397 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5398 | |
| 5399 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
| 5400 | ''' |
| 5401 | Get the timestamp of searchTerm from karaf log. |
| 5402 | |
| 5403 | Arguments: |
| 5404 | splitTerm_before and splitTerm_after: |
| 5405 | |
| 5406 | The terms that split the string that contains the timeStamp of |
| 5407 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5408 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5409 | and the splitTerm_after is "x" |
| 5410 | |
| 5411 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5412 | Please look at the "logsearch" Function in onosclidriver.py |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5413 | ''' |
| 5414 | if logNum < 0: |
| 5415 | main.log.error("Get wrong log number ") |
| 5416 | return main.ERROR |
| 5417 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
| 5418 | if len(lines) == 0: |
| 5419 | main.log.warn( "Captured timestamp string is empty" ) |
| 5420 | return main.ERROR |
| 5421 | lines = lines[ 0 ] |
| 5422 | try: |
| 5423 | assert type(lines) is str |
| 5424 | # get the target value |
| 5425 | line = lines.split( splitTerm_before ) |
| 5426 | key = line[ 1 ].split( splitTerm_after ) |
| 5427 | return int( key[ 0 ] ) |
| 5428 | except IndexError: |
| 5429 | main.log.warn( "Index Error!" ) |
| 5430 | return main.ERROR |
| 5431 | except AssertionError: |
| 5432 | main.log.warn( "Search Term Not Found " ) |
| 5433 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5434 | |
| 5435 | def workQueueAdd( self, queueName, value ): |
| 5436 | """ |
| 5437 | CLI command to add a string to the specified Work Queue. |
| 5438 | This function uses the distributed primitives test app, which |
| 5439 | gives some cli access to distributed primitives for testing |
| 5440 | purposes only. |
| 5441 | |
| 5442 | Required arguments: |
| 5443 | queueName - The name of the queue to add to |
| 5444 | value - The value to add to the queue |
| 5445 | returns: |
| 5446 | main.TRUE on success, main.FALSE on failure and |
| 5447 | main.ERROR on error. |
| 5448 | """ |
| 5449 | try: |
| 5450 | queueName = str( queueName ) |
| 5451 | value = str( value ) |
| 5452 | prefix = "work-queue-test" |
| 5453 | operation = "add" |
| 5454 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5455 | output = self.distPrimitivesSend( cmdStr ) |
| 5456 | if "Invalid operation name" in output: |
| 5457 | main.log.warn( output ) |
| 5458 | return main.ERROR |
| 5459 | elif "Done" in output: |
| 5460 | return main.TRUE |
| 5461 | except TypeError: |
| 5462 | main.log.exception( self.name + ": Object not as expected" ) |
| 5463 | return main.ERROR |
| 5464 | except Exception: |
| 5465 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5466 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5467 | |
| 5468 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5469 | """ |
| 5470 | CLI command to add two strings to the specified Work Queue. |
| 5471 | This function uses the distributed primitives test app, which |
| 5472 | gives some cli access to distributed primitives for testing |
| 5473 | purposes only. |
| 5474 | |
| 5475 | Required arguments: |
| 5476 | queueName - The name of the queue to add to |
| 5477 | value1 - The first value to add to the queue |
| 5478 | value2 - The second value to add to the queue |
| 5479 | returns: |
| 5480 | main.TRUE on success, main.FALSE on failure and |
| 5481 | main.ERROR on error. |
| 5482 | """ |
| 5483 | try: |
| 5484 | queueName = str( queueName ) |
| 5485 | value1 = str( value1 ) |
| 5486 | value2 = str( value2 ) |
| 5487 | prefix = "work-queue-test" |
| 5488 | operation = "addMultiple" |
| 5489 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 5490 | output = self.distPrimitivesSend( cmdStr ) |
| 5491 | if "Invalid operation name" in output: |
| 5492 | main.log.warn( output ) |
| 5493 | return main.ERROR |
| 5494 | elif "Done" in output: |
| 5495 | return main.TRUE |
| 5496 | except TypeError: |
| 5497 | main.log.exception( self.name + ": Object not as expected" ) |
| 5498 | return main.ERROR |
| 5499 | except Exception: |
| 5500 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5501 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5502 | |
| 5503 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 5504 | """ |
| 5505 | CLI command to take a value from the specified Work Queue and compelte it. |
| 5506 | This function uses the distributed primitives test app, which |
| 5507 | gives some cli access to distributed primitives for testing |
| 5508 | purposes only. |
| 5509 | |
| 5510 | Required arguments: |
| 5511 | queueName - The name of the queue to add to |
| 5512 | number - The number of items to take and complete |
| 5513 | returns: |
| 5514 | main.TRUE on success, main.FALSE on failure and |
| 5515 | main.ERROR on error. |
| 5516 | """ |
| 5517 | try: |
| 5518 | queueName = str( queueName ) |
| 5519 | number = str( int( number ) ) |
| 5520 | prefix = "work-queue-test" |
| 5521 | operation = "takeAndComplete" |
| 5522 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 5523 | output = self.distPrimitivesSend( cmdStr ) |
| 5524 | if "Invalid operation name" in output: |
| 5525 | main.log.warn( output ) |
| 5526 | return main.ERROR |
| 5527 | elif "Done" in output: |
| 5528 | return main.TRUE |
| 5529 | except TypeError: |
| 5530 | main.log.exception( self.name + ": Object not as expected" ) |
| 5531 | return main.ERROR |
| 5532 | except Exception: |
| 5533 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5534 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5535 | |
| 5536 | def workQueueDestroy( self, queueName ): |
| 5537 | """ |
| 5538 | CLI command to destroy the specified Work Queue. |
| 5539 | This function uses the distributed primitives test app, which |
| 5540 | gives some cli access to distributed primitives for testing |
| 5541 | purposes only. |
| 5542 | |
| 5543 | Required arguments: |
| 5544 | queueName - The name of the queue to add to |
| 5545 | returns: |
| 5546 | main.TRUE on success, main.FALSE on failure and |
| 5547 | main.ERROR on error. |
| 5548 | """ |
| 5549 | try: |
| 5550 | queueName = str( queueName ) |
| 5551 | prefix = "work-queue-test" |
| 5552 | operation = "destroy" |
| 5553 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5554 | output = self.distPrimitivesSend( cmdStr ) |
| 5555 | if "Invalid operation name" in output: |
| 5556 | main.log.warn( output ) |
| 5557 | return main.ERROR |
| 5558 | return main.TRUE |
| 5559 | except TypeError: |
| 5560 | main.log.exception( self.name + ": Object not as expected" ) |
| 5561 | return main.ERROR |
| 5562 | except Exception: |
| 5563 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5564 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5565 | |
| 5566 | def workQueueTotalPending( self, queueName ): |
| 5567 | """ |
| 5568 | CLI command to get the Total Pending items of the specified Work Queue. |
| 5569 | This function uses the distributed primitives test app, which |
| 5570 | gives some cli access to distributed primitives for testing |
| 5571 | purposes only. |
| 5572 | |
| 5573 | Required arguments: |
| 5574 | queueName - The name of the queue to add to |
| 5575 | returns: |
| 5576 | The number of Pending items in the specified work queue or |
| 5577 | None on error |
| 5578 | """ |
| 5579 | try: |
| 5580 | queueName = str( queueName ) |
| 5581 | prefix = "work-queue-test" |
| 5582 | operation = "totalPending" |
| 5583 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5584 | output = self.distPrimitivesSend( cmdStr ) |
| 5585 | pattern = r'\d+' |
| 5586 | if "Invalid operation name" in output: |
| 5587 | main.log.warn( output ) |
| 5588 | return None |
| 5589 | else: |
| 5590 | match = re.search( pattern, output ) |
| 5591 | return match.group(0) |
| 5592 | except ( AttributeError, TypeError ): |
| 5593 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5594 | return None |
| 5595 | except Exception: |
| 5596 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5597 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5598 | |
| 5599 | def workQueueTotalCompleted( self, queueName ): |
| 5600 | """ |
| 5601 | CLI command to get the Total Completed items of the specified Work Queue. |
| 5602 | This function uses the distributed primitives test app, which |
| 5603 | gives some cli access to distributed primitives for testing |
| 5604 | purposes only. |
| 5605 | |
| 5606 | Required arguments: |
| 5607 | queueName - The name of the queue to add to |
| 5608 | returns: |
| 5609 | The number of complete items in the specified work queue or |
| 5610 | None on error |
| 5611 | """ |
| 5612 | try: |
| 5613 | queueName = str( queueName ) |
| 5614 | prefix = "work-queue-test" |
| 5615 | operation = "totalCompleted" |
| 5616 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5617 | output = self.distPrimitivesSend( cmdStr ) |
| 5618 | pattern = r'\d+' |
| 5619 | if "Invalid operation name" in output: |
| 5620 | main.log.warn( output ) |
| 5621 | return None |
| 5622 | else: |
| 5623 | match = re.search( pattern, output ) |
| 5624 | return match.group(0) |
| 5625 | except ( AttributeError, TypeError ): |
| 5626 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5627 | return None |
| 5628 | except Exception: |
| 5629 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5630 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5631 | |
| 5632 | def workQueueTotalInProgress( self, queueName ): |
| 5633 | """ |
| 5634 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 5635 | This function uses the distributed primitives test app, which |
| 5636 | gives some cli access to distributed primitives for testing |
| 5637 | purposes only. |
| 5638 | |
| 5639 | Required arguments: |
| 5640 | queueName - The name of the queue to add to |
| 5641 | returns: |
| 5642 | The number of In Progress items in the specified work queue or |
| 5643 | None on error |
| 5644 | """ |
| 5645 | try: |
| 5646 | queueName = str( queueName ) |
| 5647 | prefix = "work-queue-test" |
| 5648 | operation = "totalInProgress" |
| 5649 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5650 | output = self.distPrimitivesSend( cmdStr ) |
| 5651 | pattern = r'\d+' |
| 5652 | if "Invalid operation name" in output: |
| 5653 | main.log.warn( output ) |
| 5654 | return None |
| 5655 | else: |
| 5656 | match = re.search( pattern, output ) |
| 5657 | return match.group(0) |
| 5658 | except ( AttributeError, TypeError ): |
| 5659 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5660 | return None |
| 5661 | except Exception: |
| 5662 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5663 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 5664 | |
| 5665 | def events( self, args='-a' ): |
| 5666 | """ |
| 5667 | Description: Returns events -a command output |
| 5668 | Optional: |
| 5669 | add other arguments |
| 5670 | """ |
| 5671 | try: |
| 5672 | cmdStr = "events" |
| 5673 | if args: |
| 5674 | cmdStr += " " + args |
| 5675 | handle = self.sendline( cmdStr ) |
| 5676 | assert handle is not None, "Error in sendline" |
| 5677 | assert "Command not found:" not in handle, handle |
| 5678 | return handle |
| 5679 | except AssertionError: |
| 5680 | main.log.exception( "" ) |
| 5681 | return None |
| 5682 | except TypeError: |
| 5683 | main.log.exception( self.name + ": Object not as expected" ) |
| 5684 | return None |
| 5685 | except pexpect.EOF: |
| 5686 | main.log.error( self.name + ": EOF exception found" ) |
| 5687 | main.log.error( self.name + ": " + self.handle.before ) |
| 5688 | main.cleanAndExit() |
| 5689 | except Exception: |
| 5690 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5691 | main.cleanAndExit() |
| 5692 | |
| 5693 | def getMaster( self, deviceID ): |
| 5694 | """ |
| 5695 | Description: Obtains current master using "roles" command for a specific deviceID |
| 5696 | """ |
| 5697 | try: |
| 5698 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 5699 | except AssertionError: |
| 5700 | main.log.exception( "" ) |
| 5701 | return None |
| 5702 | except TypeError: |
| 5703 | main.log.exception( self.name + ": Object not as expected" ) |
| 5704 | return None |
| 5705 | except pexpect.EOF: |
| 5706 | main.log.error( self.name + ": EOF exception found" ) |
| 5707 | main.log.error( self.name + ": " + self.handle.before ) |
| 5708 | main.cleanAndExit() |
| 5709 | except Exception: |
| 5710 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5711 | main.cleanAndExit() |