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: |
| 3464 | return "UNINSTALLED" |
| 3465 | elif state: |
| 3466 | main.log.error( "Unexpected state from 'onos:apps': " + |
| 3467 | str( state ) ) |
| 3468 | return state |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3469 | except ( TypeError, ValueError ): |
| 3470 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3471 | return None |
| 3472 | except pexpect.EOF: |
| 3473 | main.log.error( self.name + ": EOF exception found" ) |
| 3474 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3475 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3476 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3477 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3478 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3479 | |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3480 | def app( self, appName, option ): |
| 3481 | """ |
| 3482 | Interacts with the app command for ONOS. This command manages |
| 3483 | application inventory. |
| 3484 | """ |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3485 | try: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3486 | # Validate argument types |
| 3487 | valid = True |
| 3488 | if not isinstance( appName, types.StringType ): |
| 3489 | main.log.error( self.name + ".app(): appName must be a " + |
| 3490 | "string" ) |
| 3491 | valid = False |
| 3492 | if not isinstance( option, types.StringType ): |
| 3493 | main.log.error( self.name + ".app(): option must be a string" ) |
| 3494 | valid = False |
| 3495 | if not valid: |
| 3496 | return main.FALSE |
| 3497 | # Validate Option |
| 3498 | option = option.lower() |
| 3499 | # NOTE: Install may become a valid option |
| 3500 | if option == "activate": |
| 3501 | pass |
| 3502 | elif option == "deactivate": |
| 3503 | pass |
| 3504 | elif option == "uninstall": |
| 3505 | pass |
| 3506 | else: |
| 3507 | # Invalid option |
| 3508 | main.log.error( "The ONOS app command argument only takes " + |
| 3509 | "the values: (activate|deactivate|uninstall)" + |
| 3510 | "; was given '" + option + "'") |
| 3511 | return main.FALSE |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3512 | cmdStr = "onos:app " + option + " " + appName |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3513 | output = self.sendline( cmdStr ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3514 | assert output is not None, "Error in sendline" |
| 3515 | assert "Command not found:" not in output, output |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3516 | if "Error executing command" in output: |
| 3517 | main.log.error( "Error in processing onos:app command: " + |
| 3518 | str( output ) ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3519 | return main.FALSE |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3520 | elif "No such application" in output: |
| 3521 | main.log.error( "The application '" + appName + |
| 3522 | "' is not installed in ONOS" ) |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3523 | return main.FALSE |
| 3524 | elif "Command not found:" in output: |
| 3525 | main.log.error( "Error in processing onos:app command: " + |
| 3526 | str( output ) ) |
| 3527 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3528 | elif "Unsupported command:" in output: |
| 3529 | main.log.error( "Incorrect command given to 'app': " + |
| 3530 | str( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3531 | # NOTE: we may need to add more checks here |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3532 | # else: Command was successful |
Jon Hall | 08f61bc | 2015-04-13 16:00:30 -0700 | [diff] [blame] | 3533 | # main.log.debug( "app response: " + repr( output ) ) |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3534 | return main.TRUE |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 3535 | except AssertionError: |
| 3536 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3537 | return main.ERROR |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3538 | except TypeError: |
| 3539 | main.log.exception( self.name + ": Object not as expected" ) |
| 3540 | return main.ERROR |
| 3541 | except pexpect.EOF: |
| 3542 | main.log.error( self.name + ": EOF exception found" ) |
| 3543 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3544 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3545 | except Exception: |
Jon Hall | be37960 | 2015-03-24 13:39:32 -0700 | [diff] [blame] | 3546 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3547 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3548 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3549 | def activateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3550 | """ |
| 3551 | Activate an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3552 | appName is the hierarchical app name, not the feature name |
| 3553 | If check is True, method will check the status of the app after the |
| 3554 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3555 | Returns main.TRUE if the command was successfully sent |
| 3556 | main.FALSE if the cli responded with an error or given |
| 3557 | incorrect input |
| 3558 | """ |
| 3559 | try: |
| 3560 | if not isinstance( appName, types.StringType ): |
| 3561 | main.log.error( self.name + ".activateApp(): appName must be" + |
| 3562 | " a string" ) |
| 3563 | return main.FALSE |
| 3564 | status = self.appStatus( appName ) |
| 3565 | if status == "INSTALLED": |
| 3566 | response = self.app( appName, "activate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3567 | if check and response == main.TRUE: |
| 3568 | for i in range(10): # try 10 times then give up |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3569 | status = self.appStatus( appName ) |
| 3570 | if status == "ACTIVE": |
| 3571 | return main.TRUE |
| 3572 | else: |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3573 | main.log.debug( "The state of application " + |
| 3574 | appName + " is " + status ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3575 | time.sleep( 1 ) |
| 3576 | return main.FALSE |
| 3577 | else: # not 'check' or command didn't succeed |
| 3578 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3579 | elif status == "ACTIVE": |
| 3580 | return main.TRUE |
| 3581 | elif status == "UNINSTALLED": |
| 3582 | main.log.error( self.name + ": Tried to activate the " + |
| 3583 | "application '" + appName + "' which is not " + |
| 3584 | "installed." ) |
| 3585 | else: |
| 3586 | main.log.error( "Unexpected return value from appStatus: " + |
| 3587 | str( status ) ) |
| 3588 | return main.ERROR |
| 3589 | except TypeError: |
| 3590 | main.log.exception( self.name + ": Object not as expected" ) |
| 3591 | return main.ERROR |
| 3592 | except pexpect.EOF: |
| 3593 | main.log.error( self.name + ": EOF exception found" ) |
| 3594 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3595 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3596 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3597 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3598 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3599 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3600 | def deactivateApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3601 | """ |
| 3602 | Deactivate an app that is already activated in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3603 | appName is the hierarchical app name, not the feature name |
| 3604 | If check is True, method will check the status of the app after the |
| 3605 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3606 | Returns main.TRUE if the command was successfully sent |
| 3607 | main.FALSE if the cli responded with an error or given |
| 3608 | incorrect input |
| 3609 | """ |
| 3610 | try: |
| 3611 | if not isinstance( appName, types.StringType ): |
| 3612 | main.log.error( self.name + ".deactivateApp(): appName must " + |
| 3613 | "be a string" ) |
| 3614 | return main.FALSE |
| 3615 | status = self.appStatus( appName ) |
| 3616 | if status == "INSTALLED": |
| 3617 | return main.TRUE |
| 3618 | elif status == "ACTIVE": |
| 3619 | response = self.app( appName, "deactivate" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3620 | if check and response == main.TRUE: |
| 3621 | for i in range(10): # try 10 times then give up |
| 3622 | status = self.appStatus( appName ) |
| 3623 | if status == "INSTALLED": |
| 3624 | return main.TRUE |
| 3625 | else: |
| 3626 | time.sleep( 1 ) |
| 3627 | return main.FALSE |
| 3628 | else: # not check or command didn't succeed |
| 3629 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3630 | elif status == "UNINSTALLED": |
| 3631 | main.log.warn( self.name + ": Tried to deactivate the " + |
| 3632 | "application '" + appName + "' which is not " + |
| 3633 | "installed." ) |
| 3634 | return main.TRUE |
| 3635 | else: |
| 3636 | main.log.error( "Unexpected return value from appStatus: " + |
| 3637 | str( status ) ) |
| 3638 | return main.ERROR |
| 3639 | except TypeError: |
| 3640 | main.log.exception( self.name + ": Object not as expected" ) |
| 3641 | return main.ERROR |
| 3642 | except pexpect.EOF: |
| 3643 | main.log.error( self.name + ": EOF exception found" ) |
| 3644 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3645 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3646 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3647 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3648 | main.cleanAndExit() |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3649 | |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3650 | def uninstallApp( self, appName, check=True ): |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3651 | """ |
| 3652 | Uninstall an app that is already installed in ONOS |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3653 | appName is the hierarchical app name, not the feature name |
| 3654 | If check is True, method will check the status of the app after the |
| 3655 | command is issued |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3656 | Returns main.TRUE if the command was successfully sent |
| 3657 | main.FALSE if the cli responded with an error or given |
| 3658 | incorrect input |
| 3659 | """ |
| 3660 | # TODO: check with Thomas about the state machine for apps |
| 3661 | try: |
| 3662 | if not isinstance( appName, types.StringType ): |
| 3663 | main.log.error( self.name + ".uninstallApp(): appName must " + |
| 3664 | "be a string" ) |
| 3665 | return main.FALSE |
| 3666 | status = self.appStatus( appName ) |
| 3667 | if status == "INSTALLED": |
| 3668 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3669 | if check and response == main.TRUE: |
| 3670 | for i in range(10): # try 10 times then give up |
| 3671 | status = self.appStatus( appName ) |
| 3672 | if status == "UNINSTALLED": |
| 3673 | return main.TRUE |
| 3674 | else: |
| 3675 | time.sleep( 1 ) |
| 3676 | return main.FALSE |
| 3677 | else: # not check or command didn't succeed |
| 3678 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3679 | elif status == "ACTIVE": |
| 3680 | main.log.warn( self.name + ": Tried to uninstall the " + |
| 3681 | "application '" + appName + "' which is " + |
| 3682 | "currently active." ) |
| 3683 | response = self.app( appName, "uninstall" ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3684 | if check and response == main.TRUE: |
| 3685 | for i in range(10): # try 10 times then give up |
| 3686 | status = self.appStatus( appName ) |
| 3687 | if status == "UNINSTALLED": |
| 3688 | return main.TRUE |
| 3689 | else: |
| 3690 | time.sleep( 1 ) |
| 3691 | return main.FALSE |
| 3692 | else: # not check or command didn't succeed |
| 3693 | return response |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3694 | elif status == "UNINSTALLED": |
| 3695 | return main.TRUE |
| 3696 | else: |
| 3697 | main.log.error( "Unexpected return value from appStatus: " + |
| 3698 | str( status ) ) |
| 3699 | return main.ERROR |
| 3700 | except TypeError: |
| 3701 | main.log.exception( self.name + ": Object not as expected" ) |
| 3702 | return main.ERROR |
| 3703 | except pexpect.EOF: |
| 3704 | main.log.error( self.name + ": EOF exception found" ) |
| 3705 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3706 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3707 | except Exception: |
Jon Hall | 146f152 | 2015-03-24 15:33:24 -0700 | [diff] [blame] | 3708 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3709 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3710 | |
| 3711 | def appIDs( self, jsonFormat=True ): |
| 3712 | """ |
| 3713 | Show the mappings between app id and app names given by the 'app-ids' |
| 3714 | cli command |
| 3715 | """ |
| 3716 | try: |
| 3717 | cmdStr = "app-ids" |
| 3718 | if jsonFormat: |
| 3719 | cmdStr += " -j" |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3720 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3721 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3722 | assert "Command not found:" not in output, output |
| 3723 | assert "Error executing command" not in output, output |
Jon Hall | c6358dd | 2015-04-10 12:44:28 -0700 | [diff] [blame] | 3724 | return output |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3725 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3726 | main.log.exception( "Error in processing onos:app-ids command." ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3727 | return None |
| 3728 | except TypeError: |
| 3729 | main.log.exception( self.name + ": Object not as expected" ) |
| 3730 | return None |
| 3731 | except pexpect.EOF: |
| 3732 | main.log.error( self.name + ": EOF exception found" ) |
| 3733 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3734 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3735 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3736 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3737 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3738 | |
| 3739 | def appToIDCheck( self ): |
| 3740 | """ |
| 3741 | This method will check that each application's ID listed in 'apps' is |
| 3742 | the same as the ID listed in 'app-ids'. The check will also check that |
| 3743 | there are no duplicate IDs issued. Note that an app ID should be |
| 3744 | a globaly unique numerical identifier for app/app-like features. Once |
| 3745 | an ID is registered, the ID is never freed up so that if an app is |
| 3746 | reinstalled it will have the same ID. |
| 3747 | |
| 3748 | Returns: main.TRUE if the check passes and |
| 3749 | main.FALSE if the check fails or |
| 3750 | main.ERROR if there is some error in processing the test |
| 3751 | """ |
| 3752 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3753 | bail = False |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3754 | rawJson = self.appIDs( jsonFormat=True ) |
| 3755 | if rawJson: |
| 3756 | ids = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3757 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3758 | main.log.error( "app-ids returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3759 | bail = True |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3760 | rawJson = self.apps( jsonFormat=True ) |
| 3761 | if rawJson: |
| 3762 | apps = json.loads( rawJson ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3763 | else: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3764 | main.log.error( "apps returned nothing:" + repr( rawJson ) ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3765 | bail = True |
| 3766 | if bail: |
| 3767 | return main.FALSE |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3768 | result = main.TRUE |
| 3769 | for app in apps: |
| 3770 | appID = app.get( 'id' ) |
| 3771 | if appID is None: |
| 3772 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3773 | result = main.FALSE |
| 3774 | appName = app.get( 'name' ) |
| 3775 | if appName is None: |
| 3776 | main.log.error( "Error parsing app: " + str( app ) ) |
| 3777 | result = main.FALSE |
| 3778 | # get the entry in ids that has the same appID |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3779 | current = filter( lambda item: item[ 'id' ] == appID, ids ) |
Jon Hall | 050e1bd | 2015-03-30 13:33:02 -0700 | [diff] [blame] | 3780 | # main.log.debug( "Comparing " + str( app ) + " to " + |
| 3781 | # str( current ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3782 | if not current: # if ids doesn't have this id |
| 3783 | result = main.FALSE |
| 3784 | main.log.error( "'app-ids' does not have the ID for " + |
| 3785 | str( appName ) + " that apps does." ) |
| 3786 | elif len( current ) > 1: |
| 3787 | # there is more than one app with this ID |
| 3788 | result = main.FALSE |
| 3789 | # We will log this later in the method |
| 3790 | elif not current[0][ 'name' ] == appName: |
| 3791 | currentName = current[0][ 'name' ] |
| 3792 | result = main.FALSE |
| 3793 | main.log.error( "'app-ids' has " + str( currentName ) + |
| 3794 | " registered under id:" + str( appID ) + |
| 3795 | " but 'apps' has " + str( appName ) ) |
| 3796 | else: |
| 3797 | pass # id and name match! |
| 3798 | # now make sure that app-ids has no duplicates |
| 3799 | idsList = [] |
| 3800 | namesList = [] |
| 3801 | for item in ids: |
| 3802 | idsList.append( item[ 'id' ] ) |
| 3803 | namesList.append( item[ 'name' ] ) |
| 3804 | if len( idsList ) != len( set( idsList ) ) or\ |
| 3805 | len( namesList ) != len( set( namesList ) ): |
| 3806 | main.log.error( "'app-ids' has some duplicate entries: \n" |
| 3807 | + json.dumps( ids, |
| 3808 | sort_keys=True, |
| 3809 | indent=4, |
| 3810 | separators=( ',', ': ' ) ) ) |
| 3811 | result = main.FALSE |
| 3812 | return result |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3813 | except ( TypeError, ValueError ): |
| 3814 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, rawJson ) ) |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3815 | return main.ERROR |
| 3816 | except pexpect.EOF: |
| 3817 | main.log.error( self.name + ": EOF exception found" ) |
| 3818 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3819 | main.cleanAndExit() |
Jon Hall | 77ba41c | 2015-04-06 10:25:40 -0700 | [diff] [blame] | 3820 | except Exception: |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3821 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3822 | main.cleanAndExit() |
Jon Hall | bd16b92 | 2015-03-26 17:53:15 -0700 | [diff] [blame] | 3823 | |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3824 | def getCfg( self, component=None, propName=None, short=False, |
| 3825 | jsonFormat=True ): |
| 3826 | """ |
| 3827 | Get configuration settings from onos cli |
| 3828 | Optional arguments: |
| 3829 | component - Optionally only list configurations for a specific |
| 3830 | component. If None, all components with configurations |
| 3831 | are displayed. Case Sensitive string. |
| 3832 | propName - If component is specified, propName option will show |
| 3833 | only this specific configuration from that component. |
| 3834 | Case Sensitive string. |
| 3835 | jsonFormat - Returns output as json. Note that this will override |
| 3836 | the short option |
| 3837 | short - Short, less verbose, version of configurations. |
| 3838 | This is overridden by the json option |
| 3839 | returns: |
| 3840 | Output from cli as a string or None on error |
| 3841 | """ |
| 3842 | try: |
| 3843 | baseStr = "cfg" |
| 3844 | cmdStr = " get" |
| 3845 | componentStr = "" |
| 3846 | if component: |
| 3847 | componentStr += " " + component |
| 3848 | if propName: |
| 3849 | componentStr += " " + propName |
| 3850 | if jsonFormat: |
| 3851 | baseStr += " -j" |
| 3852 | elif short: |
| 3853 | baseStr += " -s" |
| 3854 | output = self.sendline( baseStr + cmdStr + componentStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3855 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3856 | assert "Command not found:" not in output, output |
| 3857 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3858 | return output |
| 3859 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3860 | main.log.exception( "Error in processing 'cfg get' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3861 | return None |
| 3862 | except TypeError: |
| 3863 | main.log.exception( self.name + ": Object not as expected" ) |
| 3864 | return None |
| 3865 | except pexpect.EOF: |
| 3866 | main.log.error( self.name + ": EOF exception found" ) |
| 3867 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3868 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3869 | except Exception: |
| 3870 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3871 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3872 | |
| 3873 | def setCfg( self, component, propName, value=None, check=True ): |
| 3874 | """ |
| 3875 | Set/Unset configuration settings from ONOS cli |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3876 | Required arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3877 | component - The case sensitive name of the component whose |
| 3878 | property is to be set |
| 3879 | propName - The case sensitive name of the property to be set/unset |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3880 | Optional arguments: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3881 | value - The value to set the property to. If None, will unset the |
| 3882 | property and revert it to it's default value(if applicable) |
| 3883 | check - Boolean, Check whether the option was successfully set this |
| 3884 | only applies when a value is given. |
| 3885 | returns: |
| 3886 | main.TRUE on success or main.FALSE on failure. If check is False, |
| 3887 | will return main.TRUE unless there is an error |
| 3888 | """ |
| 3889 | try: |
| 3890 | baseStr = "cfg" |
| 3891 | cmdStr = " set " + str( component ) + " " + str( propName ) |
| 3892 | if value is not None: |
| 3893 | cmdStr += " " + str( value ) |
| 3894 | output = self.sendline( baseStr + cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 3895 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3896 | assert "Command not found:" not in output, output |
| 3897 | assert "Error executing command" not in output, output |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3898 | if value and check: |
| 3899 | results = self.getCfg( component=str( component ), |
| 3900 | propName=str( propName ), |
| 3901 | jsonFormat=True ) |
| 3902 | # Check if current value is what we just set |
| 3903 | try: |
| 3904 | jsonOutput = json.loads( results ) |
| 3905 | current = jsonOutput[ 'value' ] |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3906 | except ( TypeError, ValueError ): |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3907 | main.log.exception( "Error parsing cfg output" ) |
| 3908 | main.log.error( "output:" + repr( results ) ) |
| 3909 | return main.FALSE |
| 3910 | if current == str( value ): |
| 3911 | return main.TRUE |
| 3912 | return main.FALSE |
| 3913 | return main.TRUE |
| 3914 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3915 | main.log.exception( "Error in processing 'cfg set' command." ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3916 | return main.FALSE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 3917 | except ( TypeError, ValueError ): |
| 3918 | main.log.exception( "{}: Object not as expected: {!r}".format( self.name, results ) ) |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3919 | return main.FALSE |
| 3920 | except pexpect.EOF: |
| 3921 | main.log.error( self.name + ": EOF exception found" ) |
| 3922 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3923 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3924 | except Exception: |
| 3925 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3926 | main.cleanAndExit() |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 3927 | |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3928 | def distPrimitivesSend( self, cmd ): |
| 3929 | """ |
| 3930 | Function to handle sending cli commands for the distributed primitives test app |
| 3931 | |
| 3932 | This command will catch some exceptions and retry the command on some |
| 3933 | specific store exceptions. |
| 3934 | |
| 3935 | Required arguments: |
| 3936 | cmd - The command to send to the cli |
| 3937 | returns: |
| 3938 | string containing the cli output |
| 3939 | None on Error |
| 3940 | """ |
| 3941 | try: |
| 3942 | output = self.sendline( cmd ) |
| 3943 | try: |
| 3944 | assert output is not None, "Error in sendline" |
| 3945 | # TODO: Maybe make this less hardcoded |
| 3946 | # ConsistentMap Exceptions |
| 3947 | assert "org.onosproject.store.service" not in output |
| 3948 | # Node not leader |
| 3949 | assert "java.lang.IllegalStateException" not in output |
| 3950 | except AssertionError: |
| 3951 | main.log.error( "Error in processing '" + cmd + "' " + |
| 3952 | "command: " + str( output ) ) |
| 3953 | retryTime = 30 # Conservative time, given by Madan |
| 3954 | main.log.info( "Waiting " + str( retryTime ) + |
| 3955 | "seconds before retrying." ) |
| 3956 | time.sleep( retryTime ) # Due to change in mastership |
| 3957 | output = self.sendline( cmd ) |
| 3958 | assert output is not None, "Error in sendline" |
| 3959 | assert "Command not found:" not in output, output |
| 3960 | assert "Error executing command" not in output, output |
| 3961 | main.log.info( self.name + ": " + output ) |
| 3962 | return output |
| 3963 | except AssertionError: |
| 3964 | main.log.exception( "Error in processing '" + cmd + "' command." ) |
| 3965 | return None |
| 3966 | except TypeError: |
| 3967 | main.log.exception( self.name + ": Object not as expected" ) |
| 3968 | return None |
| 3969 | except pexpect.EOF: |
| 3970 | main.log.error( self.name + ": EOF exception found" ) |
| 3971 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3972 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3973 | except Exception: |
| 3974 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 3975 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3976 | |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3977 | def setTestAdd( self, setName, values ): |
| 3978 | """ |
| 3979 | CLI command to add elements to a distributed set. |
| 3980 | Arguments: |
| 3981 | setName - The name of the set to add to. |
| 3982 | values - The value(s) to add to the set, space seperated. |
| 3983 | Example usages: |
| 3984 | setTestAdd( "set1", "a b c" ) |
| 3985 | setTestAdd( "set2", "1" ) |
| 3986 | returns: |
| 3987 | main.TRUE on success OR |
| 3988 | main.FALSE if elements were already in the set OR |
| 3989 | main.ERROR on error |
| 3990 | """ |
| 3991 | try: |
| 3992 | cmdStr = "set-test-add " + str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 3993 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3994 | positiveMatch = "\[(.*)\] was added to the set " + str( setName ) |
| 3995 | negativeMatch = "\[(.*)\] was already in set " + str( setName ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 3996 | if re.search( positiveMatch, output): |
| 3997 | return main.TRUE |
| 3998 | elif re.search( negativeMatch, output): |
| 3999 | return main.FALSE |
| 4000 | else: |
| 4001 | main.log.error( self.name + ": setTestAdd did not" + |
| 4002 | " match expected output" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4003 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4004 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4005 | except TypeError: |
| 4006 | main.log.exception( self.name + ": Object not as expected" ) |
| 4007 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4008 | except Exception: |
| 4009 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4010 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4011 | |
| 4012 | def setTestRemove( self, setName, values, clear=False, retain=False ): |
| 4013 | """ |
| 4014 | CLI command to remove elements from a distributed set. |
| 4015 | Required arguments: |
| 4016 | setName - The name of the set to remove from. |
| 4017 | values - The value(s) to remove from the set, space seperated. |
| 4018 | Optional arguments: |
| 4019 | clear - Clear all elements from the set |
| 4020 | retain - Retain only the given values. (intersection of the |
| 4021 | original set and the given set) |
| 4022 | returns: |
| 4023 | main.TRUE on success OR |
| 4024 | main.FALSE if the set was not changed OR |
| 4025 | main.ERROR on error |
| 4026 | """ |
| 4027 | try: |
| 4028 | cmdStr = "set-test-remove " |
| 4029 | if clear: |
| 4030 | cmdStr += "-c " + str( setName ) |
| 4031 | elif retain: |
| 4032 | cmdStr += "-r " + str( setName ) + " " + str( values ) |
| 4033 | else: |
| 4034 | cmdStr += str( setName ) + " " + str( values ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4035 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4036 | if clear: |
| 4037 | pattern = "Set " + str( setName ) + " cleared" |
| 4038 | if re.search( pattern, output ): |
| 4039 | return main.TRUE |
| 4040 | elif retain: |
| 4041 | positivePattern = str( setName ) + " was pruned to contain " +\ |
| 4042 | "only elements of set \[(.*)\]" |
| 4043 | negativePattern = str( setName ) + " was not changed by " +\ |
| 4044 | "retaining only elements of the set " +\ |
| 4045 | "\[(.*)\]" |
| 4046 | if re.search( positivePattern, output ): |
| 4047 | return main.TRUE |
| 4048 | elif re.search( negativePattern, output ): |
| 4049 | return main.FALSE |
| 4050 | else: |
| 4051 | positivePattern = "\[(.*)\] was removed from the set " +\ |
| 4052 | str( setName ) |
| 4053 | if ( len( values.split() ) == 1 ): |
| 4054 | negativePattern = "\[(.*)\] was not in set " +\ |
| 4055 | str( setName ) |
| 4056 | else: |
| 4057 | negativePattern = "No element of \[(.*)\] was in set " +\ |
| 4058 | str( setName ) |
| 4059 | if re.search( positivePattern, output ): |
| 4060 | return main.TRUE |
| 4061 | elif re.search( negativePattern, output ): |
| 4062 | return main.FALSE |
| 4063 | main.log.error( self.name + ": setTestRemove did not" + |
| 4064 | " match expected output" ) |
| 4065 | main.log.debug( self.name + " expected: " + pattern ) |
| 4066 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4067 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4068 | except TypeError: |
| 4069 | main.log.exception( self.name + ": Object not as expected" ) |
| 4070 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4071 | except Exception: |
| 4072 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4073 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4074 | |
| 4075 | def setTestGet( self, setName, values="" ): |
| 4076 | """ |
| 4077 | CLI command to get the elements in a distributed set. |
| 4078 | Required arguments: |
| 4079 | setName - The name of the set to remove from. |
| 4080 | Optional arguments: |
| 4081 | values - The value(s) to check if in the set, space seperated. |
| 4082 | returns: |
| 4083 | main.ERROR on error OR |
| 4084 | A list of elements in the set if no optional arguments are |
| 4085 | supplied OR |
| 4086 | A tuple containing the list then: |
| 4087 | main.FALSE if the given values are not in the set OR |
| 4088 | main.TRUE if the given values are in the set OR |
| 4089 | """ |
| 4090 | try: |
| 4091 | values = str( values ).strip() |
| 4092 | setName = str( setName ).strip() |
| 4093 | length = len( values.split() ) |
| 4094 | containsCheck = None |
| 4095 | # Patterns to match |
| 4096 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4097 | pattern = "Items in set " + setName + ":\r\n" + setPattern |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4098 | containsTrue = "Set " + setName + " contains the value " + values |
| 4099 | containsFalse = "Set " + setName + " did not contain the value " +\ |
| 4100 | values |
| 4101 | containsAllTrue = "Set " + setName + " contains the the subset " +\ |
| 4102 | setPattern |
| 4103 | containsAllFalse = "Set " + setName + " did not contain the the" +\ |
| 4104 | " subset " + setPattern |
| 4105 | |
| 4106 | cmdStr = "set-test-get " |
| 4107 | cmdStr += setName + " " + values |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4108 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4109 | if length == 0: |
| 4110 | match = re.search( pattern, output ) |
| 4111 | else: # if given values |
| 4112 | if length == 1: # Contains output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4113 | patternTrue = pattern + "\r\n" + containsTrue |
| 4114 | patternFalse = pattern + "\r\n" + containsFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4115 | else: # ContainsAll output |
Jon Hall | 54b994f | 2016-12-05 10:48:59 -0800 | [diff] [blame] | 4116 | patternTrue = pattern + "\r\n" + containsAllTrue |
| 4117 | patternFalse = pattern + "\r\n" + containsAllFalse |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4118 | matchTrue = re.search( patternTrue, output ) |
| 4119 | matchFalse = re.search( patternFalse, output ) |
| 4120 | if matchTrue: |
| 4121 | containsCheck = main.TRUE |
| 4122 | match = matchTrue |
| 4123 | elif matchFalse: |
| 4124 | containsCheck = main.FALSE |
| 4125 | match = matchFalse |
| 4126 | else: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4127 | main.log.error( self.name + " setTestGet did not match " + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4128 | "expected output" ) |
| 4129 | main.log.debug( self.name + " expected: " + pattern ) |
| 4130 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4131 | match = None |
| 4132 | if match: |
| 4133 | setMatch = match.group( 1 ) |
| 4134 | if setMatch == '': |
| 4135 | setList = [] |
| 4136 | else: |
| 4137 | setList = setMatch.split( ", " ) |
| 4138 | if length > 0: |
| 4139 | return ( setList, containsCheck ) |
| 4140 | else: |
| 4141 | return setList |
| 4142 | else: # no match |
| 4143 | main.log.error( self.name + ": setTestGet did not" + |
| 4144 | " match expected output" ) |
| 4145 | main.log.debug( self.name + " expected: " + pattern ) |
| 4146 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4147 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4148 | except TypeError: |
| 4149 | main.log.exception( self.name + ": Object not as expected" ) |
| 4150 | return main.ERROR |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4151 | except Exception: |
| 4152 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4153 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4154 | |
| 4155 | def setTestSize( self, setName ): |
| 4156 | """ |
| 4157 | CLI command to get the elements in a distributed set. |
| 4158 | Required arguments: |
| 4159 | setName - The name of the set to remove from. |
| 4160 | returns: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 4161 | The integer value of the size returned or |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4162 | None on error |
| 4163 | """ |
| 4164 | try: |
| 4165 | # TODO: Should this check against the number of elements returned |
| 4166 | # and then return true/false based on that? |
| 4167 | setName = str( setName ).strip() |
| 4168 | # Patterns to match |
| 4169 | setPattern = "\[(.*)\]" |
Jon Hall | 6725383 | 2016-12-05 09:47:13 -0800 | [diff] [blame] | 4170 | pattern = "There are (\d+) items in set " + setName + ":\r\n" +\ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4171 | setPattern |
| 4172 | cmdStr = "set-test-get -s " |
| 4173 | cmdStr += setName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4174 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4175 | match = re.search( pattern, output ) |
| 4176 | if match: |
| 4177 | setSize = int( match.group( 1 ) ) |
| 4178 | setMatch = match.group( 2 ) |
| 4179 | if len( setMatch.split() ) == setSize: |
| 4180 | main.log.info( "The size returned by " + self.name + |
| 4181 | " matches the number of elements in " + |
| 4182 | "the returned set" ) |
| 4183 | else: |
| 4184 | main.log.error( "The size returned by " + self.name + |
| 4185 | " does not match the number of " + |
| 4186 | "elements in the returned set." ) |
| 4187 | return setSize |
| 4188 | else: # no match |
| 4189 | main.log.error( self.name + ": setTestGet did not" + |
| 4190 | " match expected output" ) |
| 4191 | main.log.debug( self.name + " expected: " + pattern ) |
| 4192 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4193 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4194 | except TypeError: |
| 4195 | main.log.exception( self.name + ": Object not as expected" ) |
| 4196 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4197 | except Exception: |
| 4198 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4199 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4200 | |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4201 | def counters( self, jsonFormat=True ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4202 | """ |
| 4203 | Command to list the various counters in the system. |
| 4204 | returns: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4205 | if jsonFormat, a string of the json object returned by the cli |
| 4206 | command |
| 4207 | if not jsonFormat, the normal string output of the cli command |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4208 | None on error |
| 4209 | """ |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4210 | try: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4211 | cmdStr = "counters" |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4212 | if jsonFormat: |
| 4213 | cmdStr += " -j" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4214 | output = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4215 | assert output is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4216 | assert "Command not found:" not in output, output |
| 4217 | assert "Error executing command" not in output, output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4218 | main.log.info( self.name + ": " + output ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4219 | return output |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4220 | except AssertionError: |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4221 | main.log.exception( "Error in processing 'counters' command." ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4222 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4223 | except TypeError: |
| 4224 | main.log.exception( self.name + ": Object not as expected" ) |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 4225 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4226 | except pexpect.EOF: |
| 4227 | main.log.error( self.name + ": EOF exception found" ) |
| 4228 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4229 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4230 | except Exception: |
| 4231 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4232 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4233 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4234 | def counterTestAddAndGet( self, counter, delta=1 ): |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4235 | """ |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4236 | CLI command to add a delta to then get a distributed counter. |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4237 | Required arguments: |
| 4238 | counter - The name of the counter to increment. |
| 4239 | Optional arguments: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4240 | delta - The long to add to the counter |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4241 | returns: |
| 4242 | integer value of the counter or |
| 4243 | None on Error |
| 4244 | """ |
| 4245 | try: |
| 4246 | counter = str( counter ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4247 | delta = int( delta ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4248 | cmdStr = "counter-test-increment " |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4249 | cmdStr += counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4250 | if delta != 1: |
| 4251 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4252 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4253 | pattern = counter + " was updated to (-?\d+)" |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4254 | match = re.search( pattern, output ) |
| 4255 | if match: |
| 4256 | return int( match.group( 1 ) ) |
| 4257 | else: |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4258 | main.log.error( self.name + ": counterTestAddAndGet did not" + |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4259 | " match expected output." ) |
| 4260 | main.log.debug( self.name + " expected: " + pattern ) |
| 4261 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4262 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4263 | except TypeError: |
| 4264 | main.log.exception( self.name + ": Object not as expected" ) |
| 4265 | return None |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4266 | except Exception: |
| 4267 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4268 | main.cleanAndExit() |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 4269 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4270 | def counterTestGetAndAdd( self, counter, delta=1 ): |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4271 | """ |
| 4272 | CLI command to get a distributed counter then add a delta to it. |
| 4273 | Required arguments: |
| 4274 | counter - The name of the counter to increment. |
| 4275 | Optional arguments: |
| 4276 | delta - The long to add to the counter |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4277 | returns: |
| 4278 | integer value of the counter or |
| 4279 | None on Error |
| 4280 | """ |
| 4281 | try: |
| 4282 | counter = str( counter ) |
| 4283 | delta = int( delta ) |
| 4284 | cmdStr = "counter-test-increment -g " |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4285 | cmdStr += counter |
| 4286 | if delta != 1: |
| 4287 | cmdStr += " " + str( delta ) |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4288 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4289 | pattern = counter + " was updated to (-?\d+)" |
| 4290 | match = re.search( pattern, output ) |
| 4291 | if match: |
| 4292 | return int( match.group( 1 ) ) |
| 4293 | else: |
| 4294 | main.log.error( self.name + ": counterTestGetAndAdd did not" + |
| 4295 | " match expected output." ) |
| 4296 | main.log.debug( self.name + " expected: " + pattern ) |
| 4297 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4298 | return None |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4299 | except TypeError: |
| 4300 | main.log.exception( self.name + ": Object not as expected" ) |
| 4301 | return None |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4302 | except Exception: |
| 4303 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4304 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4305 | |
| 4306 | def valueTestGet( self, valueName ): |
| 4307 | """ |
| 4308 | CLI command to get the value of an atomic value. |
| 4309 | Required arguments: |
| 4310 | valueName - The name of the value to get. |
| 4311 | returns: |
| 4312 | string value of the value or |
| 4313 | None on Error |
| 4314 | """ |
| 4315 | try: |
| 4316 | valueName = str( valueName ) |
| 4317 | cmdStr = "value-test " |
| 4318 | operation = "get" |
| 4319 | cmdStr = "value-test {} {}".format( valueName, |
| 4320 | operation ) |
| 4321 | output = self.distPrimitivesSend( cmdStr ) |
| 4322 | pattern = "(\w+)" |
| 4323 | match = re.search( pattern, output ) |
| 4324 | if match: |
| 4325 | return match.group( 1 ) |
| 4326 | else: |
| 4327 | main.log.error( self.name + ": valueTestGet did not" + |
| 4328 | " match expected output." ) |
| 4329 | main.log.debug( self.name + " expected: " + pattern ) |
| 4330 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4331 | return None |
| 4332 | except TypeError: |
| 4333 | main.log.exception( self.name + ": Object not as expected" ) |
| 4334 | return None |
| 4335 | except Exception: |
| 4336 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4337 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4338 | |
| 4339 | def valueTestSet( self, valueName, newValue ): |
| 4340 | """ |
| 4341 | CLI command to set the value of an atomic value. |
| 4342 | Required arguments: |
| 4343 | valueName - The name of the value to set. |
| 4344 | newValue - The value to assign to the given value. |
| 4345 | returns: |
| 4346 | main.TRUE on success or |
| 4347 | main.ERROR on Error |
| 4348 | """ |
| 4349 | try: |
| 4350 | valueName = str( valueName ) |
| 4351 | newValue = str( newValue ) |
| 4352 | operation = "set" |
| 4353 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4354 | operation, |
| 4355 | newValue ) |
| 4356 | output = self.distPrimitivesSend( cmdStr ) |
| 4357 | if output is not None: |
| 4358 | return main.TRUE |
| 4359 | else: |
| 4360 | return main.ERROR |
| 4361 | except TypeError: |
| 4362 | main.log.exception( self.name + ": Object not as expected" ) |
| 4363 | return main.ERROR |
| 4364 | except Exception: |
| 4365 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4366 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4367 | |
| 4368 | def valueTestCompareAndSet( self, valueName, oldValue, newValue ): |
| 4369 | """ |
| 4370 | CLI command to compareAndSet the value of an atomic value. |
| 4371 | Required arguments: |
| 4372 | valueName - The name of the value. |
| 4373 | oldValue - Compare the current value of the atomic value to this |
| 4374 | newValue - If the value equals oldValue, set the value to newValue |
| 4375 | returns: |
| 4376 | main.TRUE on success or |
| 4377 | main.FALSE on failure or |
| 4378 | main.ERROR on Error |
| 4379 | """ |
| 4380 | try: |
| 4381 | valueName = str( valueName ) |
| 4382 | oldValue = str( oldValue ) |
| 4383 | newValue = str( newValue ) |
| 4384 | operation = "compareAndSet" |
| 4385 | cmdStr = "value-test {} {} {} {}".format( valueName, |
| 4386 | operation, |
| 4387 | oldValue, |
| 4388 | newValue ) |
| 4389 | output = self.distPrimitivesSend( cmdStr ) |
| 4390 | pattern = "(\w+)" |
| 4391 | match = re.search( pattern, output ) |
| 4392 | if match: |
| 4393 | result = match.group( 1 ) |
| 4394 | if result == "true": |
| 4395 | return main.TRUE |
| 4396 | elif result == "false": |
| 4397 | return main.FALSE |
| 4398 | else: |
| 4399 | main.log.error( self.name + ": valueTestCompareAndSet did not" + |
| 4400 | " match expected output." ) |
| 4401 | main.log.debug( self.name + " expected: " + pattern ) |
| 4402 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4403 | return main.ERROR |
| 4404 | except TypeError: |
| 4405 | main.log.exception( self.name + ": Object not as expected" ) |
| 4406 | return main.ERROR |
| 4407 | except Exception: |
| 4408 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4409 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4410 | |
| 4411 | def valueTestGetAndSet( self, valueName, newValue ): |
| 4412 | """ |
| 4413 | CLI command to getAndSet the value of an atomic value. |
| 4414 | Required arguments: |
| 4415 | valueName - The name of the value to get. |
| 4416 | newValue - The value to assign to the given value |
| 4417 | returns: |
| 4418 | string value of the value or |
| 4419 | None on Error |
| 4420 | """ |
| 4421 | try: |
| 4422 | valueName = str( valueName ) |
| 4423 | cmdStr = "value-test " |
| 4424 | operation = "getAndSet" |
| 4425 | cmdStr += valueName + " " + operation |
| 4426 | cmdStr = "value-test {} {} {}".format( valueName, |
| 4427 | operation, |
| 4428 | newValue ) |
| 4429 | output = self.distPrimitivesSend( cmdStr ) |
| 4430 | pattern = "(\w+)" |
| 4431 | match = re.search( pattern, output ) |
| 4432 | if match: |
| 4433 | return match.group( 1 ) |
| 4434 | else: |
| 4435 | main.log.error( self.name + ": valueTestGetAndSet did not" + |
| 4436 | " match expected output." ) |
| 4437 | main.log.debug( self.name + " expected: " + pattern ) |
| 4438 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4439 | return None |
| 4440 | except TypeError: |
| 4441 | main.log.exception( self.name + ": Object not as expected" ) |
| 4442 | return None |
| 4443 | except Exception: |
| 4444 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4445 | main.cleanAndExit() |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4446 | |
| 4447 | def valueTestDestroy( self, valueName ): |
| 4448 | """ |
| 4449 | CLI command to destroy an atomic value. |
| 4450 | Required arguments: |
| 4451 | valueName - The name of the value to destroy. |
| 4452 | returns: |
| 4453 | main.TRUE on success or |
| 4454 | main.ERROR on Error |
| 4455 | """ |
| 4456 | try: |
| 4457 | valueName = str( valueName ) |
| 4458 | cmdStr = "value-test " |
| 4459 | operation = "destroy" |
| 4460 | cmdStr += valueName + " " + operation |
| 4461 | output = self.distPrimitivesSend( cmdStr ) |
| 4462 | if output is not None: |
| 4463 | return main.TRUE |
| 4464 | else: |
| 4465 | return main.ERROR |
| 4466 | except TypeError: |
| 4467 | main.log.exception( self.name + ": Object not as expected" ) |
| 4468 | return main.ERROR |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4469 | except Exception: |
| 4470 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4471 | main.cleanAndExit() |
Jon Hall | e1a3b75 | 2015-07-22 13:02:46 -0700 | [diff] [blame] | 4472 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4473 | def summary( self, jsonFormat=True, timeout=30 ): |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4474 | """ |
| 4475 | Description: Execute summary command in onos |
| 4476 | Returns: json object ( summary -j ), returns main.FALSE if there is |
| 4477 | no output |
| 4478 | |
| 4479 | """ |
| 4480 | try: |
| 4481 | cmdStr = "summary" |
| 4482 | if jsonFormat: |
| 4483 | cmdStr += " -j" |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4484 | handle = self.sendline( cmdStr, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4485 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4486 | assert "Command not found:" not in handle, handle |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4487 | assert "Error:" not in handle, handle |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4488 | if not handle: |
| 4489 | main.log.error( self.name + ": There is no output in " + |
| 4490 | "summary command" ) |
| 4491 | return main.FALSE |
| 4492 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4493 | except AssertionError: |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 4494 | main.log.exception( "{} Error in summary output:".format( self.name ) ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4495 | return None |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4496 | except TypeError: |
| 4497 | main.log.exception( self.name + ": Object not as expected" ) |
| 4498 | return None |
| 4499 | except pexpect.EOF: |
| 4500 | main.log.error( self.name + ": EOF exception found" ) |
| 4501 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4502 | main.cleanAndExit() |
kelvin-onlab | a297c4d | 2015-06-01 13:53:55 -0700 | [diff] [blame] | 4503 | except Exception: |
| 4504 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4505 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4506 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4507 | def transactionalMapGet( self, keyName ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4508 | """ |
| 4509 | CLI command to get the value of a key in a consistent map using |
| 4510 | transactions. This a test function and can only get keys from the |
| 4511 | test map hard coded into the cli command |
| 4512 | Required arguments: |
| 4513 | keyName - The name of the key to get |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4514 | returns: |
| 4515 | The string value of the key or |
| 4516 | None on Error |
| 4517 | """ |
| 4518 | try: |
| 4519 | keyName = str( keyName ) |
| 4520 | cmdStr = "transactional-map-test-get " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4521 | cmdStr += keyName |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4522 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4523 | pattern = "Key-value pair \(" + keyName + ", (?P<value>.+)\) found." |
| 4524 | if "Key " + keyName + " not found." in output: |
Jon Hall | 9bfadd2 | 2016-05-11 14:48:07 -0700 | [diff] [blame] | 4525 | main.log.warn( output ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4526 | return None |
| 4527 | else: |
| 4528 | match = re.search( pattern, output ) |
| 4529 | if match: |
| 4530 | return match.groupdict()[ 'value' ] |
| 4531 | else: |
| 4532 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4533 | " match expected output." ) |
| 4534 | main.log.debug( self.name + " expected: " + pattern ) |
| 4535 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4536 | return None |
| 4537 | except TypeError: |
| 4538 | main.log.exception( self.name + ": Object not as expected" ) |
| 4539 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4540 | except Exception: |
| 4541 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4542 | main.cleanAndExit() |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4543 | |
Jon Hall | 935db19 | 2016-04-19 00:22:04 -0700 | [diff] [blame] | 4544 | def transactionalMapPut( self, numKeys, value ): |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4545 | """ |
| 4546 | CLI command to put a value into 'numKeys' number of keys in a |
| 4547 | consistent map using transactions. This a test function and can only |
| 4548 | put into keys named 'Key#' of the test map hard coded into the cli command |
| 4549 | Required arguments: |
| 4550 | numKeys - Number of keys to add the value to |
| 4551 | value - The string value to put into the keys |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4552 | returns: |
| 4553 | A dictionary whose keys are the name of the keys put into the map |
| 4554 | and the values of the keys are dictionaries whose key-values are |
| 4555 | 'value': value put into map and optionaly |
| 4556 | 'oldValue': Previous value in the key or |
| 4557 | None on Error |
| 4558 | |
| 4559 | Example output |
| 4560 | { 'Key1': {'oldValue': 'oldTestValue', 'value': 'Testing'}, |
| 4561 | 'Key2': {'value': 'Testing'} } |
| 4562 | """ |
| 4563 | try: |
| 4564 | numKeys = str( numKeys ) |
| 4565 | value = str( value ) |
| 4566 | cmdStr = "transactional-map-test-put " |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4567 | cmdStr += numKeys + " " + value |
Jon Hall | 7a6ebfd | 2017-03-13 10:58:58 -0700 | [diff] [blame] | 4568 | output = self.distPrimitivesSend( cmdStr ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4569 | newPattern = 'Created Key (?P<key>(\w)+) with value (?P<value>(.)+)\.' |
| 4570 | updatedPattern = "Put (?P<value>(.)+) into key (?P<key>(\w)+)\. The old value was (?P<oldValue>(.)+)\." |
| 4571 | results = {} |
| 4572 | for line in output.splitlines(): |
| 4573 | new = re.search( newPattern, line ) |
| 4574 | updated = re.search( updatedPattern, line ) |
| 4575 | if new: |
| 4576 | results[ new.groupdict()[ 'key' ] ] = { 'value': new.groupdict()[ 'value' ] } |
| 4577 | elif updated: |
| 4578 | results[ updated.groupdict()[ 'key' ] ] = { 'value': updated.groupdict()[ 'value' ], |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4579 | 'oldValue': updated.groupdict()[ 'oldValue' ] } |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4580 | else: |
| 4581 | main.log.error( self.name + ": transactionlMapGet did not" + |
| 4582 | " match expected output." ) |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4583 | main.log.debug( "{} expected: {!r} or {!r}".format( self.name, |
| 4584 | newPattern, |
| 4585 | updatedPattern ) ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4586 | main.log.debug( self.name + " actual: " + repr( output ) ) |
| 4587 | return results |
| 4588 | except TypeError: |
| 4589 | main.log.exception( self.name + ": Object not as expected" ) |
| 4590 | return None |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 4591 | except Exception: |
| 4592 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4593 | main.cleanAndExit() |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4594 | |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4595 | def maps( self, jsonFormat=True ): |
| 4596 | """ |
| 4597 | Description: Returns result of onos:maps |
| 4598 | Optional: |
| 4599 | * jsonFormat: enable json formatting of output |
| 4600 | """ |
| 4601 | try: |
| 4602 | cmdStr = "maps" |
| 4603 | if jsonFormat: |
| 4604 | cmdStr += " -j" |
| 4605 | handle = self.sendline( cmdStr ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4606 | assert handle is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4607 | assert "Command not found:" not in handle, handle |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4608 | return handle |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4609 | except AssertionError: |
| 4610 | main.log.exception( "" ) |
| 4611 | return None |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4612 | except TypeError: |
| 4613 | main.log.exception( self.name + ": Object not as expected" ) |
| 4614 | return None |
| 4615 | except pexpect.EOF: |
| 4616 | main.log.error( self.name + ": EOF exception found" ) |
| 4617 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4618 | main.cleanAndExit() |
acsmars | daea66c | 2015-09-03 11:44:06 -0700 | [diff] [blame] | 4619 | except Exception: |
| 4620 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4621 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4622 | |
| 4623 | def getSwController( self, uri, jsonFormat=True ): |
| 4624 | """ |
| 4625 | Descrition: Gets the controller information from the device |
| 4626 | """ |
| 4627 | try: |
| 4628 | cmd = "device-controllers " |
| 4629 | if jsonFormat: |
| 4630 | cmd += "-j " |
| 4631 | response = self.sendline( cmd + uri ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4632 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4633 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4634 | return response |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4635 | except AssertionError: |
| 4636 | main.log.exception( "" ) |
| 4637 | return None |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4638 | except TypeError: |
| 4639 | main.log.exception( self.name + ": Object not as expected" ) |
| 4640 | return None |
| 4641 | except pexpect.EOF: |
| 4642 | main.log.error( self.name + ": EOF exception found" ) |
| 4643 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4644 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4645 | except Exception: |
| 4646 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4647 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4648 | |
| 4649 | def setSwController( self, uri, ip, proto="tcp", port="6653", jsonFormat=True ): |
| 4650 | """ |
| 4651 | Descrition: sets the controller(s) for the specified device |
| 4652 | |
| 4653 | Parameters: |
| 4654 | Required: uri - String: The uri of the device(switch). |
| 4655 | ip - String or List: The ip address of the controller. |
| 4656 | This parameter can be formed in a couple of different ways. |
| 4657 | VALID: |
| 4658 | 10.0.0.1 - just the ip address |
| 4659 | tcp:10.0.0.1 - the protocol and the ip address |
| 4660 | tcp:10.0.0.1:6653 - the protocol and port can be specified, |
| 4661 | so that you can add controllers with different |
| 4662 | protocols and ports |
| 4663 | INVALID: |
| 4664 | 10.0.0.1:6653 - this is not supported by ONOS |
| 4665 | |
| 4666 | Optional: proto - The type of connection e.g. tcp, ssl. If a list of ips are given |
| 4667 | port - The port number. |
| 4668 | jsonFormat - If set ONOS will output in json NOTE: This is currently not supported |
| 4669 | |
| 4670 | Returns: main.TRUE if ONOS returns without any errors, otherwise returns main.FALSE |
| 4671 | """ |
| 4672 | try: |
| 4673 | cmd = "device-setcontrollers" |
| 4674 | |
| 4675 | if jsonFormat: |
| 4676 | cmd += " -j" |
| 4677 | cmd += " " + uri |
| 4678 | if isinstance( ip, str ): |
| 4679 | ip = [ip] |
| 4680 | for item in ip: |
| 4681 | if ":" in item: |
| 4682 | sitem = item.split( ":" ) |
| 4683 | if len(sitem) == 3: |
| 4684 | cmd += " " + item |
| 4685 | elif "." in sitem[1]: |
| 4686 | cmd += " {}:{}".format(item, port) |
| 4687 | else: |
| 4688 | main.log.error( "Malformed entry: " + item ) |
| 4689 | raise TypeError |
| 4690 | else: |
| 4691 | cmd += " {}:{}:{}".format( proto, item, port ) |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4692 | response = self.sendline( cmd ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4693 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4694 | assert "Command not found:" not in response, response |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4695 | if "Error" in response: |
| 4696 | main.log.error( response ) |
| 4697 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4698 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4699 | except AssertionError: |
| 4700 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4701 | return main.FALSE |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4702 | except TypeError: |
| 4703 | main.log.exception( self.name + ": Object not as expected" ) |
| 4704 | return main.FALSE |
| 4705 | except pexpect.EOF: |
| 4706 | main.log.error( self.name + ": EOF exception found" ) |
| 4707 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4708 | main.cleanAndExit() |
GlennRC | 050596c | 2015-11-18 17:06:41 -0800 | [diff] [blame] | 4709 | except Exception: |
| 4710 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4711 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4712 | |
| 4713 | def removeDevice( self, device ): |
| 4714 | ''' |
| 4715 | Description: |
| 4716 | Remove a device from ONOS by passing the uri of the device(s). |
| 4717 | Parameters: |
| 4718 | device - (str or list) the id or uri of the device ex. "of:0000000000000001" |
| 4719 | Returns: |
| 4720 | Returns main.FALSE if an exception is thrown or an error is present |
| 4721 | in the response. Otherwise, returns main.TRUE. |
| 4722 | NOTE: |
| 4723 | If a host cannot be removed, then this function will return main.FALSE |
| 4724 | ''' |
| 4725 | try: |
| 4726 | if type( device ) is str: |
You Wang | 823f502 | 2016-08-18 15:24:41 -0700 | [diff] [blame] | 4727 | deviceStr = device |
| 4728 | device = [] |
| 4729 | device.append( deviceStr ) |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4730 | |
| 4731 | for d in device: |
| 4732 | time.sleep( 1 ) |
| 4733 | response = self.sendline( "device-remove {}".format( d ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4734 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4735 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4736 | if "Error" in response: |
| 4737 | main.log.warn( "Error for device: {}\nResponse: {}".format( d, response ) ) |
| 4738 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4739 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4740 | except AssertionError: |
| 4741 | main.log.exception( "" ) |
| 4742 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4743 | except TypeError: |
| 4744 | main.log.exception( self.name + ": Object not as expected" ) |
| 4745 | return main.FALSE |
| 4746 | except pexpect.EOF: |
| 4747 | main.log.error( self.name + ": EOF exception found" ) |
| 4748 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4749 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4750 | except Exception: |
| 4751 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4752 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4753 | |
| 4754 | def removeHost( self, host ): |
| 4755 | ''' |
| 4756 | Description: |
| 4757 | Remove a host from ONOS by passing the id of the host(s) |
| 4758 | Parameters: |
| 4759 | hostId - (str or list) the id or mac of the host ex. "00:00:00:00:00:01" |
| 4760 | Returns: |
| 4761 | Returns main.FALSE if an exception is thrown or an error is present |
| 4762 | in the response. Otherwise, returns main.TRUE. |
| 4763 | NOTE: |
| 4764 | If a host cannot be removed, then this function will return main.FALSE |
| 4765 | ''' |
| 4766 | try: |
| 4767 | if type( host ) is str: |
| 4768 | host = list( host ) |
| 4769 | |
| 4770 | for h in host: |
| 4771 | time.sleep( 1 ) |
| 4772 | response = self.sendline( "host-remove {}".format( h ) ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4773 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4774 | assert "Command not found:" not in response, response |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4775 | if "Error" in response: |
| 4776 | main.log.warn( "Error for host: {}\nResponse: {}".format( h, response ) ) |
| 4777 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4778 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4779 | except AssertionError: |
| 4780 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4781 | return main.FALSE |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4782 | except TypeError: |
| 4783 | main.log.exception( self.name + ": Object not as expected" ) |
| 4784 | return main.FALSE |
| 4785 | except pexpect.EOF: |
| 4786 | main.log.error( self.name + ": EOF exception found" ) |
| 4787 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4788 | main.cleanAndExit() |
GlennRC | 20fc652 | 2015-12-23 23:26:57 -0800 | [diff] [blame] | 4789 | except Exception: |
| 4790 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4791 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4792 | |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4793 | def link( self, begin, end, state, timeout=30, showResponse=True ): |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4794 | ''' |
| 4795 | Description: |
| 4796 | Bring link down or up in the null-provider. |
| 4797 | params: |
| 4798 | begin - (string) One end of a device or switch. |
| 4799 | end - (string) the other end of the device or switch |
| 4800 | returns: |
| 4801 | main.TRUE if no exceptions were thrown and no Errors are |
| 4802 | present in the resoponse. Otherwise, returns main.FALSE |
| 4803 | ''' |
| 4804 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4805 | cmd = "null-link null:{} null:{} {}".format( begin, end, state ) |
YPZhang | febf730 | 2016-05-24 16:45:56 -0700 | [diff] [blame] | 4806 | response = self.sendline( cmd, showResponse=showResponse, timeout=timeout ) |
Jon Hall | a495f56 | 2016-05-16 18:03:26 -0700 | [diff] [blame] | 4807 | assert response is not None, "Error in sendline" |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4808 | assert "Command not found:" not in response, response |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4809 | if "Error" in response or "Failure" in response: |
| 4810 | main.log.error( response ) |
| 4811 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4812 | return main.TRUE |
Jon Hall | c679355 | 2016-01-19 14:18:37 -0800 | [diff] [blame] | 4813 | except AssertionError: |
| 4814 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4815 | return main.FALSE |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4816 | except TypeError: |
| 4817 | main.log.exception( self.name + ": Object not as expected" ) |
| 4818 | return main.FALSE |
| 4819 | except pexpect.EOF: |
| 4820 | main.log.error( self.name + ": EOF exception found" ) |
| 4821 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4822 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4823 | except Exception: |
| 4824 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4825 | main.cleanAndExit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 4826 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4827 | def portstate( self, dpid, port, state ): |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4828 | ''' |
| 4829 | Description: |
| 4830 | Changes the state of port in an OF switch by means of the |
| 4831 | PORTSTATUS OF messages. |
| 4832 | params: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4833 | dpid - (string) Datapath ID of the device. Ex: 'of:0000000000000102' |
| 4834 | port - (string) target port in the device. Ex: '2' |
| 4835 | state - (string) target state (enable or disable) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4836 | returns: |
| 4837 | main.TRUE if no exceptions were thrown and no Errors are |
| 4838 | present in the resoponse. Otherwise, returns main.FALSE |
| 4839 | ''' |
| 4840 | try: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4841 | state = state.lower() |
| 4842 | assert state == 'enable' or state == 'disable', "Unknown state" |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4843 | cmd = "portstate {} {} {}".format( dpid, port, state ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4844 | response = self.sendline( cmd, showResponse=True ) |
| 4845 | assert response is not None, "Error in sendline" |
| 4846 | assert "Command not found:" not in response, response |
| 4847 | if "Error" in response or "Failure" in response: |
| 4848 | main.log.error( response ) |
| 4849 | return main.FALSE |
| 4850 | return main.TRUE |
| 4851 | except AssertionError: |
| 4852 | main.log.exception( "" ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 4853 | return main.FALSE |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4854 | except TypeError: |
| 4855 | main.log.exception( self.name + ": Object not as expected" ) |
| 4856 | return main.FALSE |
| 4857 | except pexpect.EOF: |
| 4858 | main.log.error( self.name + ": EOF exception found" ) |
| 4859 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4860 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4861 | except Exception: |
| 4862 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4863 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4864 | |
| 4865 | def logSet( self, level="INFO", app="org.onosproject" ): |
| 4866 | """ |
| 4867 | Set the logging level to lvl for a specific app |
| 4868 | returns main.TRUE on success |
| 4869 | returns main.FALSE if Error occurred |
| 4870 | if noExit is True, TestON will not exit, but clean up |
| 4871 | Available level: DEBUG, TRACE, INFO, WARN, ERROR |
| 4872 | Level defaults to INFO |
| 4873 | """ |
| 4874 | try: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4875 | self.handle.sendline( "log:set %s %s" % ( level, app ) ) |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4876 | self.handle.expect( "onos>" ) |
| 4877 | |
| 4878 | response = self.handle.before |
| 4879 | if re.search( "Error", response ): |
| 4880 | return main.FALSE |
| 4881 | return main.TRUE |
| 4882 | except pexpect.TIMEOUT: |
| 4883 | main.log.exception( self.name + ": TIMEOUT exception found" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4884 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4885 | except pexpect.EOF: |
| 4886 | main.log.error( self.name + ": EOF exception found" ) |
| 4887 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4888 | main.cleanAndExit() |
Flavio Castro | 82ee2f6 | 2016-06-07 15:04:12 -0700 | [diff] [blame] | 4889 | except Exception: |
| 4890 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 4891 | main.cleanAndExit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4892 | |
| 4893 | def getGraphDict( self, timeout=60, includeHost=False ): |
| 4894 | """ |
| 4895 | Return a dictionary which describes the latest network topology data as a |
| 4896 | graph. |
| 4897 | An example of the dictionary: |
| 4898 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 4899 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 4900 | Each vertex should at least have an 'edges' attribute which describes the |
| 4901 | adjacency information. The value of 'edges' attribute is also represented by |
| 4902 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 4903 | list of attributes. |
| 4904 | An example of the edges dictionary: |
| 4905 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 4906 | vertex3: { 'port': ..., 'weight': ... } } |
| 4907 | If includeHost == True, all hosts (and host-switch links) will be included |
| 4908 | in topology data. |
| 4909 | """ |
| 4910 | graphDict = {} |
| 4911 | try: |
| 4912 | links = self.links() |
| 4913 | links = json.loads( links ) |
| 4914 | devices = self.devices() |
| 4915 | devices = json.loads( devices ) |
| 4916 | idToDevice = {} |
| 4917 | for device in devices: |
| 4918 | idToDevice[ device[ 'id' ] ] = device |
| 4919 | if includeHost: |
| 4920 | hosts = self.hosts() |
| 4921 | # FIXME: support 'includeHost' argument |
| 4922 | for link in links: |
| 4923 | nodeA = link[ 'src' ][ 'device' ] |
| 4924 | nodeB = link[ 'dst' ][ 'device' ] |
| 4925 | assert idToDevice[ nodeA ][ 'available' ] and idToDevice[ nodeB ][ 'available' ] |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4926 | if nodeA not in graphDict.keys(): |
| 4927 | graphDict[ nodeA ] = { 'edges': {}, |
| 4928 | 'dpid': idToDevice[ nodeA ][ 'id' ][3:], |
| 4929 | 'type': idToDevice[ nodeA ][ 'type' ], |
| 4930 | 'available': idToDevice[ nodeA ][ 'available' ], |
| 4931 | 'role': idToDevice[ nodeA ][ 'role' ], |
| 4932 | 'mfr': idToDevice[ nodeA ][ 'mfr' ], |
| 4933 | 'hw': idToDevice[ nodeA ][ 'hw' ], |
| 4934 | 'sw': idToDevice[ nodeA ][ 'sw' ], |
| 4935 | 'serial': idToDevice[ nodeA ][ 'serial' ], |
| 4936 | 'chassisId': idToDevice[ nodeA ][ 'chassisId' ], |
| 4937 | 'annotations': idToDevice[ nodeA ][ 'annotations' ]} |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4938 | else: |
| 4939 | # Assert nodeB is not connected to any current links of nodeA |
| 4940 | assert nodeB not in graphDict[ nodeA ][ 'edges' ].keys() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 4941 | graphDict[ nodeA ][ 'edges' ][ nodeB ] = { 'port': link[ 'src' ][ 'port' ], |
| 4942 | 'type': link[ 'type' ], |
| 4943 | 'state': link[ 'state' ] } |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 4944 | return graphDict |
| 4945 | except ( TypeError, ValueError ): |
| 4946 | main.log.exception( self.name + ": Object not as expected" ) |
| 4947 | return None |
| 4948 | except KeyError: |
| 4949 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4950 | return None |
| 4951 | except AssertionError: |
| 4952 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4953 | return None |
| 4954 | except pexpect.EOF: |
| 4955 | main.log.error( self.name + ": EOF exception found" ) |
| 4956 | main.log.error( self.name + ": " + self.handle.before ) |
| 4957 | return None |
| 4958 | except Exception: |
| 4959 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4960 | return None |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 4961 | |
| 4962 | def getIntentPerfSummary( self ): |
| 4963 | ''' |
| 4964 | Send command to check intent-perf summary |
| 4965 | Returns: dictionary for intent-perf summary |
| 4966 | if something wrong, function will return None |
| 4967 | ''' |
| 4968 | cmd = "intent-perf -s" |
| 4969 | respDic = {} |
| 4970 | resp = self.sendline( cmd ) |
You Wang | b5a55f7 | 2017-03-03 12:51:05 -0800 | [diff] [blame] | 4971 | assert resp is not None, "Error in sendline" |
| 4972 | assert "Command not found:" not in resp, resp |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 4973 | try: |
| 4974 | # Generate the dictionary to return |
| 4975 | for l in resp.split( "\n" ): |
| 4976 | # Delete any white space in line |
| 4977 | temp = re.sub( r'\s+', '', l ) |
| 4978 | temp = temp.split( ":" ) |
| 4979 | respDic[ temp[0] ] = temp[ 1 ] |
| 4980 | |
| 4981 | except (TypeError, ValueError): |
| 4982 | main.log.exception( self.name + ": Object not as expected" ) |
| 4983 | return None |
| 4984 | except KeyError: |
| 4985 | main.log.exception( self.name + ": KeyError exception found" ) |
| 4986 | return None |
| 4987 | except AssertionError: |
| 4988 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 4989 | return None |
| 4990 | except pexpect.EOF: |
| 4991 | main.log.error( self.name + ": EOF exception found" ) |
| 4992 | main.log.error( self.name + ": " + self.handle.before ) |
| 4993 | return None |
| 4994 | except Exception: |
| 4995 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 4996 | return None |
| 4997 | return respDic |
| 4998 | |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 4999 | def logSearch( self, mode='all', searchTerm='', startLine='', logNum=1 ): |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5000 | """ |
| 5001 | Searches the latest ONOS log file for the given search term and |
| 5002 | return a list that contains all the lines that have the search term. |
YPZhang | cbc2a06 | 2016-07-11 10:55:44 -0700 | [diff] [blame] | 5003 | |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5004 | Arguments: |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5005 | searchTerm: |
| 5006 | The string to grep from the ONOS log. |
| 5007 | startLine: |
| 5008 | The term that decides which line is the start to search the searchTerm in |
| 5009 | the karaf log. For now, startTerm only works in 'first' mode. |
| 5010 | logNum: |
| 5011 | In some extreme cases, one karaf log is not big enough to contain all the |
| 5012 | information.Because of this, search mutiply logs is necessary to capture |
| 5013 | the right result. logNum is the number of karaf logs that we need to search |
| 5014 | the searchTerm. |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5015 | mode: |
| 5016 | all: return all the strings that contain the search term |
| 5017 | last: return the last string that contains the search term |
| 5018 | first: return the first string that contains the search term |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5019 | num: return the number of times that the searchTerm appears in the log |
| 5020 | total: return how many lines in karaf log |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5021 | """ |
| 5022 | try: |
| 5023 | assert type( searchTerm ) is str |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5024 | # Build the log paths string |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5025 | logPath = '/opt/onos/log/karaf.log.' |
| 5026 | logPaths = '/opt/onos/log/karaf.log' |
| 5027 | for i in range( 1, logNum ): |
| 5028 | logPaths = logPath + str( i ) + " " + logPaths |
| 5029 | cmd = "cat " + logPaths |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5030 | if startLine: |
| 5031 | # 100000000 is just a extreme large number to make sure this function can grep all the lines after startLine |
| 5032 | cmd = cmd + " | grep -A 100000000 \'" + startLine + "\'" |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5033 | if mode == 'all': |
| 5034 | cmd = cmd + " | grep \'" + searchTerm + "\'" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5035 | elif mode == 'last': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5036 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | tail -n 1" |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5037 | elif mode == 'first': |
| 5038 | cmd = cmd + " | grep \'" + searchTerm + "\'" + " | head -n 1" |
| 5039 | elif mode == 'num': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5040 | cmd = cmd + " | grep -c \'" + searchTerm + "\'" |
You Wang | 118ba58 | 2017-01-02 17:14:43 -0800 | [diff] [blame] | 5041 | num = self.sendline( cmd ) |
Chiyu Cheng | b8c2c84 | 2016-10-05 12:40:49 -0700 | [diff] [blame] | 5042 | return num |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5043 | elif mode == 'total': |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5044 | totalLines = self.sendline( "cat /opt/onos/log/karaf.log | wc -l" ) |
| 5045 | return int(totalLines) |
You Wang | 6d301d4 | 2017-04-21 10:49:33 -0700 | [diff] [blame] | 5046 | else: |
| 5047 | main.log.error( self.name + " unsupported mode" ) |
| 5048 | return main.ERROR |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5049 | before = self.sendline( cmd ) |
| 5050 | before = before.splitlines() |
| 5051 | # make sure the returned list only contains the search term |
| 5052 | returnLines = [line for line in before if searchTerm in line] |
| 5053 | return returnLines |
| 5054 | except AssertionError: |
| 5055 | main.log.error( self.name + " searchTerm is not string type" ) |
| 5056 | return None |
| 5057 | except pexpect.EOF: |
| 5058 | main.log.error( self.name + ": EOF exception found" ) |
| 5059 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5060 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5061 | except pexpect.TIMEOUT: |
| 5062 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 5063 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5064 | main.cleanAndExit() |
chengchiyu | 08303a0 | 2016-09-08 17:40:26 -0700 | [diff] [blame] | 5065 | except Exception: |
| 5066 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5067 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5068 | |
| 5069 | def vplsShow( self, jsonFormat=True ): |
| 5070 | """ |
| 5071 | Description: Returns result of onos:vpls show, which should list the |
| 5072 | configured VPLS networks and the assigned interfaces. |
| 5073 | Optional: |
| 5074 | * jsonFormat: enable json formatting of output |
| 5075 | Returns: |
| 5076 | The output of the command or None on error. |
| 5077 | """ |
| 5078 | try: |
| 5079 | cmdStr = "vpls show" |
| 5080 | if jsonFormat: |
| 5081 | raise NotImplementedError |
| 5082 | cmdStr += " -j" |
| 5083 | handle = self.sendline( cmdStr ) |
| 5084 | assert handle is not None, "Error in sendline" |
| 5085 | assert "Command not found:" not in handle, handle |
| 5086 | return handle |
| 5087 | except AssertionError: |
| 5088 | main.log.exception( "" ) |
| 5089 | return None |
| 5090 | except TypeError: |
| 5091 | main.log.exception( self.name + ": Object not as expected" ) |
| 5092 | return None |
| 5093 | except pexpect.EOF: |
| 5094 | main.log.error( self.name + ": EOF exception found" ) |
| 5095 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5096 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5097 | except NotImplementedError: |
| 5098 | main.log.exception( self.name + ": Json output not supported") |
| 5099 | return None |
| 5100 | except Exception: |
| 5101 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5102 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5103 | |
| 5104 | def parseVplsShow( self ): |
| 5105 | """ |
| 5106 | Parse the cli output of 'vpls show' into json output. This is required |
| 5107 | as there is currently no json output available. |
| 5108 | """ |
| 5109 | try: |
| 5110 | output = [] |
| 5111 | raw = self.vplsShow( jsonFormat=False ) |
| 5112 | namePat = "VPLS name: (?P<name>\w+)" |
| 5113 | interfacesPat = "Associated interfaces: \[(?P<interfaces>.*)\]" |
| 5114 | encapPat = "Encapsulation: (?P<encap>\w+)" |
| 5115 | pattern = "\s+".join( [ namePat, interfacesPat, encapPat ] ) |
| 5116 | mIter = re.finditer( pattern, raw ) |
| 5117 | for match in mIter: |
| 5118 | item = {} |
| 5119 | item[ 'name' ] = match.group( 'name' ) |
| 5120 | ifaces = match.group( 'interfaces' ).split( ', ') |
| 5121 | if ifaces == [ "" ]: |
| 5122 | ifaces = [] |
| 5123 | item[ 'interfaces' ] = ifaces |
| 5124 | encap = match.group( 'encap' ) |
| 5125 | if encap != 'NONE': |
| 5126 | item[ 'encapsulation' ] = encap.lower() |
| 5127 | output.append( item ) |
| 5128 | return output |
| 5129 | except Exception: |
| 5130 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5131 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5132 | |
| 5133 | def vplsList( self, jsonFormat=True ): |
| 5134 | """ |
| 5135 | Description: Returns result of onos:vpls list, which should list the |
| 5136 | configured VPLS networks. |
| 5137 | Optional: |
| 5138 | * jsonFormat: enable json formatting of output |
| 5139 | """ |
| 5140 | try: |
| 5141 | cmdStr = "vpls list" |
| 5142 | if jsonFormat: |
| 5143 | raise NotImplementedError |
| 5144 | cmdStr += " -j" |
| 5145 | handle = self.sendline( cmdStr ) |
| 5146 | assert handle is not None, "Error in sendline" |
| 5147 | assert "Command not found:" not in handle, handle |
| 5148 | return handle |
| 5149 | except AssertionError: |
| 5150 | main.log.exception( "" ) |
| 5151 | return None |
| 5152 | except TypeError: |
| 5153 | main.log.exception( self.name + ": Object not as expected" ) |
| 5154 | return None |
| 5155 | except pexpect.EOF: |
| 5156 | main.log.error( self.name + ": EOF exception found" ) |
| 5157 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5158 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5159 | except NotImplementedError: |
| 5160 | main.log.exception( self.name + ": Json output not supported") |
| 5161 | return None |
| 5162 | except Exception: |
| 5163 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5164 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5165 | |
| 5166 | def vplsCreate( self, network ): |
| 5167 | """ |
| 5168 | CLI command to create a new VPLS network. |
| 5169 | Required arguments: |
| 5170 | network - String name of the network to create. |
| 5171 | returns: |
| 5172 | main.TRUE on success and main.FALSE on failure |
| 5173 | """ |
| 5174 | try: |
| 5175 | network = str( network ) |
| 5176 | cmdStr = "vpls create " |
| 5177 | cmdStr += network |
| 5178 | output = self.sendline( cmdStr ) |
| 5179 | assert output is not None, "Error in sendline" |
| 5180 | assert "Command not found:" not in output, output |
| 5181 | assert "Error executing command" not in output, output |
| 5182 | assert "VPLS already exists:" not in output, output |
| 5183 | return main.TRUE |
| 5184 | except AssertionError: |
| 5185 | main.log.exception( "" ) |
| 5186 | return main.FALSE |
| 5187 | except TypeError: |
| 5188 | main.log.exception( self.name + ": Object not as expected" ) |
| 5189 | return main.FALSE |
| 5190 | except pexpect.EOF: |
| 5191 | main.log.error( self.name + ": EOF exception found" ) |
| 5192 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5193 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5194 | except Exception: |
| 5195 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5196 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5197 | |
| 5198 | def vplsDelete( self, network ): |
| 5199 | """ |
| 5200 | CLI command to delete a VPLS network. |
| 5201 | Required arguments: |
| 5202 | network - Name of the network to delete. |
| 5203 | returns: |
| 5204 | main.TRUE on success and main.FALSE on failure |
| 5205 | """ |
| 5206 | try: |
| 5207 | network = str( network ) |
| 5208 | cmdStr = "vpls delete " |
| 5209 | cmdStr += network |
| 5210 | output = self.sendline( cmdStr ) |
| 5211 | assert output is not None, "Error in sendline" |
| 5212 | assert "Command not found:" not in output, output |
| 5213 | assert "Error executing command" not in output, output |
| 5214 | assert " not found" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5215 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5216 | return main.TRUE |
| 5217 | except AssertionError: |
| 5218 | main.log.exception( "" ) |
| 5219 | return main.FALSE |
| 5220 | except TypeError: |
| 5221 | main.log.exception( self.name + ": Object not as expected" ) |
| 5222 | return main.FALSE |
| 5223 | except pexpect.EOF: |
| 5224 | main.log.error( self.name + ": EOF exception found" ) |
| 5225 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5226 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5227 | except Exception: |
| 5228 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5229 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5230 | |
| 5231 | def vplsAddIface( self, network, iface ): |
| 5232 | """ |
| 5233 | CLI command to add an interface to a VPLS network. |
| 5234 | Required arguments: |
| 5235 | network - Name of the network to add the interface to. |
| 5236 | iface - The ONOS name for an interface. |
| 5237 | returns: |
| 5238 | main.TRUE on success and main.FALSE on failure |
| 5239 | """ |
| 5240 | try: |
| 5241 | network = str( network ) |
| 5242 | iface = str( iface ) |
| 5243 | cmdStr = "vpls add-if " |
| 5244 | cmdStr += network + " " + iface |
| 5245 | output = self.sendline( cmdStr ) |
| 5246 | assert output is not None, "Error in sendline" |
| 5247 | assert "Command not found:" not in output, output |
| 5248 | assert "Error executing command" not in output, output |
| 5249 | assert "already associated to network" not in output, output |
| 5250 | assert "Interface cannot be added." not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5251 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5252 | return main.TRUE |
| 5253 | except AssertionError: |
| 5254 | main.log.exception( "" ) |
| 5255 | return main.FALSE |
| 5256 | except TypeError: |
| 5257 | main.log.exception( self.name + ": Object not as expected" ) |
| 5258 | return main.FALSE |
| 5259 | except pexpect.EOF: |
| 5260 | main.log.error( self.name + ": EOF exception found" ) |
| 5261 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5262 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5263 | except Exception: |
| 5264 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5265 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5266 | |
| 5267 | def vplsRemIface( self, network, iface ): |
| 5268 | """ |
| 5269 | CLI command to remove an interface from a VPLS network. |
| 5270 | Required arguments: |
| 5271 | network - Name of the network to remove the interface from. |
| 5272 | iface - Name of the interface to remove. |
| 5273 | returns: |
| 5274 | main.TRUE on success and main.FALSE on failure |
| 5275 | """ |
| 5276 | try: |
| 5277 | iface = str( iface ) |
| 5278 | cmdStr = "vpls rem-if " |
| 5279 | cmdStr += network + " " + iface |
| 5280 | output = self.sendline( cmdStr ) |
| 5281 | assert output is not None, "Error in sendline" |
| 5282 | assert "Command not found:" not in output, output |
| 5283 | assert "Error executing command" not in output, output |
| 5284 | assert "is not configured" not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5285 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5286 | return main.TRUE |
| 5287 | except AssertionError: |
| 5288 | main.log.exception( "" ) |
| 5289 | return main.FALSE |
| 5290 | except TypeError: |
| 5291 | main.log.exception( self.name + ": Object not as expected" ) |
| 5292 | return main.FALSE |
| 5293 | except pexpect.EOF: |
| 5294 | main.log.error( self.name + ": EOF exception found" ) |
| 5295 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5296 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5297 | except Exception: |
| 5298 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5299 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5300 | |
| 5301 | def vplsClean( self ): |
| 5302 | """ |
| 5303 | Description: Clears the VPLS app configuration. |
| 5304 | Returns: main.TRUE on success and main.FALSE on failure |
| 5305 | """ |
| 5306 | try: |
| 5307 | cmdStr = "vpls clean" |
| 5308 | handle = self.sendline( cmdStr ) |
| 5309 | assert handle is not None, "Error in sendline" |
| 5310 | assert "Command not found:" not in handle, handle |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5311 | assert "still updating" not in handle, handle |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5312 | return handle |
| 5313 | except AssertionError: |
| 5314 | main.log.exception( "" ) |
| 5315 | return main.FALSE |
| 5316 | except TypeError: |
| 5317 | main.log.exception( self.name + ": Object not as expected" ) |
| 5318 | return main.FALSE |
| 5319 | except pexpect.EOF: |
| 5320 | main.log.error( self.name + ": EOF exception found" ) |
| 5321 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5322 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5323 | except Exception: |
| 5324 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5325 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5326 | |
| 5327 | def vplsSetEncap( self, network, encapType ): |
| 5328 | """ |
| 5329 | CLI command to add an interface to a VPLS network. |
| 5330 | Required arguments: |
| 5331 | network - Name of the network to create. |
| 5332 | encapType - Type of encapsulation. |
| 5333 | returns: |
| 5334 | main.TRUE on success and main.FALSE on failure |
| 5335 | """ |
| 5336 | try: |
| 5337 | network = str( network ) |
| 5338 | encapType = str( encapType ).upper() |
| 5339 | assert encapType in [ "MPLS", "VLAN", "NONE" ], "Incorrect type" |
| 5340 | cmdStr = "vpls set-encap " |
| 5341 | cmdStr += network + " " + encapType |
| 5342 | output = self.sendline( cmdStr ) |
| 5343 | assert output is not None, "Error in sendline" |
| 5344 | assert "Command not found:" not in output, output |
| 5345 | assert "Error executing command" not in output, output |
| 5346 | assert "already associated to network" not in output, output |
| 5347 | assert "Encapsulation type " not in output, output |
Jon Hall | cf97cf1 | 2017-06-06 09:37:51 -0700 | [diff] [blame] | 5348 | assert "still updating" not in output, output |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5349 | return main.TRUE |
| 5350 | except AssertionError: |
| 5351 | main.log.exception( "" ) |
| 5352 | return main.FALSE |
| 5353 | except TypeError: |
| 5354 | main.log.exception( self.name + ": Object not as expected" ) |
| 5355 | return main.FALSE |
| 5356 | except pexpect.EOF: |
| 5357 | main.log.error( self.name + ": EOF exception found" ) |
| 5358 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5359 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5360 | except Exception: |
| 5361 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5362 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5363 | |
| 5364 | def interfaces( self, jsonFormat=True ): |
| 5365 | """ |
| 5366 | Description: Returns result of interfaces command. |
| 5367 | Optional: |
| 5368 | * jsonFormat: enable json formatting of output |
| 5369 | Returns: |
| 5370 | The output of the command or None on error. |
| 5371 | """ |
| 5372 | try: |
| 5373 | cmdStr = "interfaces" |
| 5374 | if jsonFormat: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5375 | raise NotImplementedError |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5376 | cmdStr += " -j" |
| 5377 | handle = self.sendline( cmdStr ) |
| 5378 | assert handle is not None, "Error in sendline" |
| 5379 | assert "Command not found:" not in handle, handle |
| 5380 | return handle |
| 5381 | except AssertionError: |
| 5382 | main.log.exception( "" ) |
| 5383 | return None |
| 5384 | except TypeError: |
| 5385 | main.log.exception( self.name + ": Object not as expected" ) |
| 5386 | return None |
| 5387 | except pexpect.EOF: |
| 5388 | main.log.error( self.name + ": EOF exception found" ) |
| 5389 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5390 | main.cleanAndExit() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 5391 | except NotImplementedError: |
| 5392 | main.log.exception( self.name + ": Json output not supported") |
| 5393 | return None |
| 5394 | except Exception: |
| 5395 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5396 | main.cleanAndExit() |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5397 | |
| 5398 | def getTimeStampFromLog( self, mode, searchTerm, splitTerm_before, splitTerm_after, startLine='', logNum=1 ): |
| 5399 | ''' |
| 5400 | Get the timestamp of searchTerm from karaf log. |
| 5401 | |
| 5402 | Arguments: |
| 5403 | splitTerm_before and splitTerm_after: |
| 5404 | |
| 5405 | The terms that split the string that contains the timeStamp of |
| 5406 | searchTerm. For example, if that string is "xxxxxxxcreationTime = |
| 5407 | 1419510501xxxxxx", then the splitTerm_before is "CreationTime = " |
| 5408 | and the splitTerm_after is "x" |
| 5409 | |
| 5410 | others: |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5411 | Please look at the "logsearch" Function in onosclidriver.py |
Chiyu Cheng | ec63bde | 2016-11-17 18:11:36 -0800 | [diff] [blame] | 5412 | ''' |
| 5413 | if logNum < 0: |
| 5414 | main.log.error("Get wrong log number ") |
| 5415 | return main.ERROR |
| 5416 | lines = self.logSearch( mode=mode, searchTerm=searchTerm, startLine=startLine, logNum=logNum ) |
| 5417 | if len(lines) == 0: |
| 5418 | main.log.warn( "Captured timestamp string is empty" ) |
| 5419 | return main.ERROR |
| 5420 | lines = lines[ 0 ] |
| 5421 | try: |
| 5422 | assert type(lines) is str |
| 5423 | # get the target value |
| 5424 | line = lines.split( splitTerm_before ) |
| 5425 | key = line[ 1 ].split( splitTerm_after ) |
| 5426 | return int( key[ 0 ] ) |
| 5427 | except IndexError: |
| 5428 | main.log.warn( "Index Error!" ) |
| 5429 | return main.ERROR |
| 5430 | except AssertionError: |
| 5431 | main.log.warn( "Search Term Not Found " ) |
| 5432 | return main.ERROR |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5433 | |
| 5434 | def workQueueAdd( self, queueName, value ): |
| 5435 | """ |
| 5436 | CLI command to add a string to the specified Work Queue. |
| 5437 | This function uses the distributed primitives test app, which |
| 5438 | gives some cli access to distributed primitives for testing |
| 5439 | purposes only. |
| 5440 | |
| 5441 | Required arguments: |
| 5442 | queueName - The name of the queue to add to |
| 5443 | value - The value to add to the queue |
| 5444 | returns: |
| 5445 | main.TRUE on success, main.FALSE on failure and |
| 5446 | main.ERROR on error. |
| 5447 | """ |
| 5448 | try: |
| 5449 | queueName = str( queueName ) |
| 5450 | value = str( value ) |
| 5451 | prefix = "work-queue-test" |
| 5452 | operation = "add" |
| 5453 | cmdStr = " ".join( [ prefix, queueName, operation, value ] ) |
| 5454 | output = self.distPrimitivesSend( cmdStr ) |
| 5455 | if "Invalid operation name" in output: |
| 5456 | main.log.warn( output ) |
| 5457 | return main.ERROR |
| 5458 | elif "Done" in output: |
| 5459 | return main.TRUE |
| 5460 | except TypeError: |
| 5461 | main.log.exception( self.name + ": Object not as expected" ) |
| 5462 | return main.ERROR |
| 5463 | except Exception: |
| 5464 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5465 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5466 | |
| 5467 | def workQueueAddMultiple( self, queueName, value1, value2 ): |
| 5468 | """ |
| 5469 | CLI command to add two strings to the specified Work Queue. |
| 5470 | This function uses the distributed primitives test app, which |
| 5471 | gives some cli access to distributed primitives for testing |
| 5472 | purposes only. |
| 5473 | |
| 5474 | Required arguments: |
| 5475 | queueName - The name of the queue to add to |
| 5476 | value1 - The first value to add to the queue |
| 5477 | value2 - The second value to add to the queue |
| 5478 | returns: |
| 5479 | main.TRUE on success, main.FALSE on failure and |
| 5480 | main.ERROR on error. |
| 5481 | """ |
| 5482 | try: |
| 5483 | queueName = str( queueName ) |
| 5484 | value1 = str( value1 ) |
| 5485 | value2 = str( value2 ) |
| 5486 | prefix = "work-queue-test" |
| 5487 | operation = "addMultiple" |
| 5488 | cmdStr = " ".join( [ prefix, queueName, operation, value1, value2 ] ) |
| 5489 | output = self.distPrimitivesSend( cmdStr ) |
| 5490 | if "Invalid operation name" in output: |
| 5491 | main.log.warn( output ) |
| 5492 | return main.ERROR |
| 5493 | elif "Done" in output: |
| 5494 | return main.TRUE |
| 5495 | except TypeError: |
| 5496 | main.log.exception( self.name + ": Object not as expected" ) |
| 5497 | return main.ERROR |
| 5498 | except Exception: |
| 5499 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5500 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5501 | |
| 5502 | def workQueueTakeAndComplete( self, queueName, number=1 ): |
| 5503 | """ |
| 5504 | CLI command to take a value from the specified Work Queue and compelte it. |
| 5505 | This function uses the distributed primitives test app, which |
| 5506 | gives some cli access to distributed primitives for testing |
| 5507 | purposes only. |
| 5508 | |
| 5509 | Required arguments: |
| 5510 | queueName - The name of the queue to add to |
| 5511 | number - The number of items to take and complete |
| 5512 | returns: |
| 5513 | main.TRUE on success, main.FALSE on failure and |
| 5514 | main.ERROR on error. |
| 5515 | """ |
| 5516 | try: |
| 5517 | queueName = str( queueName ) |
| 5518 | number = str( int( number ) ) |
| 5519 | prefix = "work-queue-test" |
| 5520 | operation = "takeAndComplete" |
| 5521 | cmdStr = " ".join( [ prefix, queueName, operation, number ] ) |
| 5522 | output = self.distPrimitivesSend( cmdStr ) |
| 5523 | if "Invalid operation name" in output: |
| 5524 | main.log.warn( output ) |
| 5525 | return main.ERROR |
| 5526 | elif "Done" in output: |
| 5527 | return main.TRUE |
| 5528 | except TypeError: |
| 5529 | main.log.exception( self.name + ": Object not as expected" ) |
| 5530 | return main.ERROR |
| 5531 | except Exception: |
| 5532 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5533 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5534 | |
| 5535 | def workQueueDestroy( self, queueName ): |
| 5536 | """ |
| 5537 | CLI command to destroy the specified Work Queue. |
| 5538 | This function uses the distributed primitives test app, which |
| 5539 | gives some cli access to distributed primitives for testing |
| 5540 | purposes only. |
| 5541 | |
| 5542 | Required arguments: |
| 5543 | queueName - The name of the queue to add to |
| 5544 | returns: |
| 5545 | main.TRUE on success, main.FALSE on failure and |
| 5546 | main.ERROR on error. |
| 5547 | """ |
| 5548 | try: |
| 5549 | queueName = str( queueName ) |
| 5550 | prefix = "work-queue-test" |
| 5551 | operation = "destroy" |
| 5552 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5553 | output = self.distPrimitivesSend( cmdStr ) |
| 5554 | if "Invalid operation name" in output: |
| 5555 | main.log.warn( output ) |
| 5556 | return main.ERROR |
| 5557 | return main.TRUE |
| 5558 | except TypeError: |
| 5559 | main.log.exception( self.name + ": Object not as expected" ) |
| 5560 | return main.ERROR |
| 5561 | except Exception: |
| 5562 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5563 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5564 | |
| 5565 | def workQueueTotalPending( self, queueName ): |
| 5566 | """ |
| 5567 | CLI command to get the Total Pending items of the specified Work Queue. |
| 5568 | This function uses the distributed primitives test app, which |
| 5569 | gives some cli access to distributed primitives for testing |
| 5570 | purposes only. |
| 5571 | |
| 5572 | Required arguments: |
| 5573 | queueName - The name of the queue to add to |
| 5574 | returns: |
| 5575 | The number of Pending items in the specified work queue or |
| 5576 | None on error |
| 5577 | """ |
| 5578 | try: |
| 5579 | queueName = str( queueName ) |
| 5580 | prefix = "work-queue-test" |
| 5581 | operation = "totalPending" |
| 5582 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5583 | output = self.distPrimitivesSend( cmdStr ) |
| 5584 | pattern = r'\d+' |
| 5585 | if "Invalid operation name" in output: |
| 5586 | main.log.warn( output ) |
| 5587 | return None |
| 5588 | else: |
| 5589 | match = re.search( pattern, output ) |
| 5590 | return match.group(0) |
| 5591 | except ( AttributeError, TypeError ): |
| 5592 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5593 | return None |
| 5594 | except Exception: |
| 5595 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5596 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5597 | |
| 5598 | def workQueueTotalCompleted( self, queueName ): |
| 5599 | """ |
| 5600 | CLI command to get the Total Completed items of the specified Work Queue. |
| 5601 | This function uses the distributed primitives test app, which |
| 5602 | gives some cli access to distributed primitives for testing |
| 5603 | purposes only. |
| 5604 | |
| 5605 | Required arguments: |
| 5606 | queueName - The name of the queue to add to |
| 5607 | returns: |
| 5608 | The number of complete items in the specified work queue or |
| 5609 | None on error |
| 5610 | """ |
| 5611 | try: |
| 5612 | queueName = str( queueName ) |
| 5613 | prefix = "work-queue-test" |
| 5614 | operation = "totalCompleted" |
| 5615 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5616 | output = self.distPrimitivesSend( cmdStr ) |
| 5617 | pattern = r'\d+' |
| 5618 | if "Invalid operation name" in output: |
| 5619 | main.log.warn( output ) |
| 5620 | return None |
| 5621 | else: |
| 5622 | match = re.search( pattern, output ) |
| 5623 | return match.group(0) |
| 5624 | except ( AttributeError, TypeError ): |
| 5625 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5626 | return None |
| 5627 | except Exception: |
| 5628 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5629 | main.cleanAndExit() |
Jon Hall | e0f0b34 | 2017-04-18 11:43:47 -0700 | [diff] [blame] | 5630 | |
| 5631 | def workQueueTotalInProgress( self, queueName ): |
| 5632 | """ |
| 5633 | CLI command to get the Total In Progress items of the specified Work Queue. |
| 5634 | This function uses the distributed primitives test app, which |
| 5635 | gives some cli access to distributed primitives for testing |
| 5636 | purposes only. |
| 5637 | |
| 5638 | Required arguments: |
| 5639 | queueName - The name of the queue to add to |
| 5640 | returns: |
| 5641 | The number of In Progress items in the specified work queue or |
| 5642 | None on error |
| 5643 | """ |
| 5644 | try: |
| 5645 | queueName = str( queueName ) |
| 5646 | prefix = "work-queue-test" |
| 5647 | operation = "totalInProgress" |
| 5648 | cmdStr = " ".join( [ prefix, queueName, operation ] ) |
| 5649 | output = self.distPrimitivesSend( cmdStr ) |
| 5650 | pattern = r'\d+' |
| 5651 | if "Invalid operation name" in output: |
| 5652 | main.log.warn( output ) |
| 5653 | return None |
| 5654 | else: |
| 5655 | match = re.search( pattern, output ) |
| 5656 | return match.group(0) |
| 5657 | except ( AttributeError, TypeError ): |
| 5658 | main.log.exception( self.name + ": Object not as expected; " + str( output ) ) |
| 5659 | return None |
| 5660 | except Exception: |
| 5661 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 5662 | main.cleanAndExit() |
Jeremy Ronquillo | 818bc7c | 2017-08-09 17:14:53 +0000 | [diff] [blame] | 5663 | |
| 5664 | def events( self, args='-a' ): |
| 5665 | """ |
| 5666 | Description: Returns events -a command output |
| 5667 | Optional: |
| 5668 | add other arguments |
| 5669 | """ |
| 5670 | try: |
| 5671 | cmdStr = "events" |
| 5672 | if args: |
| 5673 | cmdStr += " " + args |
| 5674 | handle = self.sendline( cmdStr ) |
| 5675 | assert handle is not None, "Error in sendline" |
| 5676 | assert "Command not found:" not in handle, handle |
| 5677 | return handle |
| 5678 | except AssertionError: |
| 5679 | main.log.exception( "" ) |
| 5680 | return None |
| 5681 | except TypeError: |
| 5682 | main.log.exception( self.name + ": Object not as expected" ) |
| 5683 | return None |
| 5684 | except pexpect.EOF: |
| 5685 | main.log.error( self.name + ": EOF exception found" ) |
| 5686 | main.log.error( self.name + ": " + self.handle.before ) |
| 5687 | main.cleanAndExit() |
| 5688 | except Exception: |
| 5689 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5690 | main.cleanAndExit() |
| 5691 | |
| 5692 | def getMaster( self, deviceID ): |
| 5693 | """ |
| 5694 | Description: Obtains current master using "roles" command for a specific deviceID |
| 5695 | """ |
| 5696 | try: |
| 5697 | return str( self.getRole( deviceID )[ 'master' ] ) |
| 5698 | except AssertionError: |
| 5699 | main.log.exception( "" ) |
| 5700 | return None |
| 5701 | except TypeError: |
| 5702 | main.log.exception( self.name + ": Object not as expected" ) |
| 5703 | return None |
| 5704 | except pexpect.EOF: |
| 5705 | main.log.error( self.name + ": EOF exception found" ) |
| 5706 | main.log.error( self.name + ": " + self.handle.before ) |
| 5707 | main.cleanAndExit() |
| 5708 | except Exception: |
| 5709 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 5710 | main.cleanAndExit() |