You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Copyright 2018 Open Networking Foundation (ONF) |
| 4 | |
| 5 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 6 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 7 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 8 | |
| 9 | TestON is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 2 of the License, or |
| 12 | ( at your option ) any later version. |
| 13 | |
| 14 | TestON is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 21 | """ |
| 22 | |
| 23 | import pexpect |
| 24 | import re |
| 25 | import sys |
| 26 | import types |
| 27 | import os |
| 28 | import time |
| 29 | from math import pow |
| 30 | from drivers.common.cli.emulatordriver import Emulator |
| 31 | from core.graph import Graph |
| 32 | |
| 33 | |
| 34 | class MininetSwitchDriver( Emulator ): |
| 35 | """ |
| 36 | This class is created as a standalone switch driver. Hoever actually the |
| 37 | switch is an ovs-switch created by Mininet. It could be used as driver |
| 38 | for a mock physical switch for proof-of-concept testing in physical |
| 39 | environment. |
| 40 | """ |
| 41 | def __init__( self ): |
| 42 | super( MininetSwitchDriver, self ).__init__() |
| 43 | self.handle = self |
| 44 | self.name = None |
| 45 | self.shortName = None |
| 46 | self.home = None |
| 47 | |
| 48 | def connect( self, **connectargs ): |
| 49 | """ |
| 50 | Creates ssh handle for the Mininet switch. |
| 51 | NOTE: |
| 52 | The ip_address would come from the topo file using the host tag, the |
| 53 | value can be an environment variable as well as a "localhost" to get |
| 54 | the ip address needed to ssh to the "bench" |
| 55 | """ |
| 56 | try: |
| 57 | for key in connectargs: |
| 58 | vars( self )[ key ] = connectargs[ key ] |
| 59 | self.home = "~/mininet" |
| 60 | self.name = self.options[ 'name' ] |
| 61 | self.shortName = self.options[ 'shortName' ] |
| 62 | for key in self.options: |
| 63 | if key == "home": |
| 64 | self.home = self.options[ 'home' ] |
| 65 | break |
| 66 | if self.home is None or self.home == "": |
| 67 | self.home = "~/mininet" |
| 68 | |
| 69 | try: |
| 70 | if os.getenv( str( self.ip_address ) ) is not None: |
| 71 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 72 | else: |
| 73 | main.log.info( self.name + |
| 74 | ": Trying to connect to " + |
| 75 | self.ip_address ) |
| 76 | |
| 77 | except KeyError: |
| 78 | main.log.info( "Invalid host name," + |
| 79 | " connecting to local host instead" ) |
| 80 | self.ip_address = 'localhost' |
| 81 | except Exception as inst: |
| 82 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 83 | |
| 84 | self.handle = super( |
| 85 | MininetSwitchDriver, |
| 86 | self ).connect( |
| 87 | user_name=self.user_name, |
| 88 | ip_address=self.ip_address, |
| 89 | port=None, |
| 90 | pwd=self.pwd ) |
| 91 | |
| 92 | if self.handle: |
| 93 | main.log.info( "Connection successful to the host " + |
| 94 | self.user_name + |
| 95 | "@" + |
| 96 | self.ip_address ) |
| 97 | return main.TRUE |
| 98 | else: |
| 99 | main.log.error( "Connection failed to the host " + |
| 100 | self.user_name + |
| 101 | "@" + |
| 102 | self.ip_address ) |
| 103 | main.log.error( "Failed to connect to the Mininet CLI" ) |
| 104 | return main.FALSE |
| 105 | except pexpect.EOF: |
| 106 | main.log.error( self.name + ": EOF exception found" ) |
| 107 | main.log.error( self.name + ": " + self.handle.before ) |
| 108 | main.cleanAndExit() |
| 109 | except Exception: |
| 110 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 111 | main.cleanAndExit() |
| 112 | |
| 113 | def disconnect( self ): |
| 114 | """ |
| 115 | Called when test is complete to disconnect the handle. |
| 116 | """ |
| 117 | try: |
| 118 | self.handle.sendline( '' ) |
| 119 | i = self.handle.expect( [ self.prompt, pexpect.EOF, pexpect.TIMEOUT ], |
| 120 | timeout=2 ) |
| 121 | if i == 0: |
| 122 | return main.TRUE |
| 123 | elif i == 1: |
| 124 | return main.TRUE |
| 125 | else: |
| 126 | main.log.error( "Connection failed to the host" ) |
| 127 | return main.ERROR |
| 128 | except pexpect.EOF: |
| 129 | main.log.error( self.name + ": EOF exception found" ) |
| 130 | main.log.error( self.name + ": " + self.handle.before ) |
| 131 | main.cleanAndExit() |
| 132 | except Exception: |
| 133 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 134 | main.cleanAndExit() |
| 135 | |
| 136 | def assignSwController( self, ip, port="6653", ptcp="" ): |
| 137 | """ |
| 138 | Description: |
| 139 | Assign the Mininet switch to the controllers |
| 140 | Required: |
| 141 | ip - Ip addresses of controllers. This can be a list or a string. |
| 142 | Optional: |
| 143 | port - ONOS use port 6653, if no list of ports is passed, then |
| 144 | the all the controller will use 6653 as their port number |
| 145 | ptcp - ptcp number. This needs to be a string. |
| 146 | Return: |
| 147 | Returns main.TRUE if the switch is correctly assigned to controllers, |
| 148 | otherwise it will return main.FALSE or an appropriate exception(s) |
| 149 | """ |
| 150 | assignResult = main.TRUE |
| 151 | # Initial ovs command |
| 152 | commandList = [] |
| 153 | command = "sudo ovs-vsctl set-controller " |
| 154 | onosIp = "" |
| 155 | try: |
| 156 | if isinstance( ip, types.StringType ): |
| 157 | onosIp = "tcp:" + str( ip ) + ":" |
| 158 | if isinstance( port, types.StringType ) or \ |
| 159 | isinstance( port, types.IntType ): |
| 160 | onosIp += str( port ) |
| 161 | elif isinstance( port, types.ListType ): |
| 162 | main.log.error( self.name + ": Only one controller " + |
| 163 | "assigned and a list of ports has" + |
| 164 | " been passed" ) |
| 165 | return main.FALSE |
| 166 | else: |
| 167 | main.log.error( self.name + ": Invalid controller port " + |
| 168 | "number. Please specify correct " + |
| 169 | "controller port" ) |
| 170 | return main.FALSE |
| 171 | elif isinstance( ip, types.ListType ): |
| 172 | if isinstance( port, types.StringType ) or \ |
| 173 | isinstance( port, types.IntType ): |
| 174 | for ipAddress in ip: |
| 175 | onosIp += "tcp:" + str( ipAddress ) + ":" + \ |
| 176 | str( port ) + " " |
| 177 | elif isinstance( port, types.ListType ): |
| 178 | if ( len( ip ) != len( port ) ): |
| 179 | main.log.error( self.name + ": Port list = " + |
| 180 | str( len( port ) ) + |
| 181 | "should be the same as controller" + |
| 182 | " ip list = " + str( len( ip ) ) ) |
| 183 | return main.FALSE |
| 184 | else: |
| 185 | onosIp = "" |
| 186 | for ipAddress, portNum in zip( ip, port ): |
| 187 | onosIp += "tcp:" + str( ipAddress ) + ":" + \ |
| 188 | str( portNum ) + " " |
| 189 | else: |
| 190 | main.log.error( self.name + ": Invalid controller port " + |
| 191 | "number. Please specify correct " + |
| 192 | "controller port" ) |
| 193 | return main.FALSE |
| 194 | else: |
| 195 | main.log.error( self.name + ": Invalid ip address" ) |
| 196 | return main.FALSE |
| 197 | command += self.shortName + " " |
| 198 | if ptcp: |
| 199 | if isinstance( ptcp, types.StringType ): |
| 200 | command += "ptcp:" + str( ptcp ) + " " |
| 201 | elif isinstance( ptcp, types.ListType ): |
| 202 | main.log.error( self.name + ": Only one switch is " + |
| 203 | "being set and multiple PTCP is " + |
| 204 | "being passed " ) |
| 205 | return main.FALSE |
| 206 | else: |
| 207 | main.log.error( self.name + ": Invalid PTCP" ) |
| 208 | return main.FALSE |
| 209 | command += onosIp |
| 210 | self.execute( cmd=command, prompt=self.prompt, timeout=5 ) |
| 211 | return main.TRUE |
| 212 | except pexpect.TIMEOUT: |
| 213 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 214 | return main.FALSE |
| 215 | except pexpect.EOF: |
| 216 | main.log.error( self.name + ": EOF exception found" ) |
| 217 | main.log.error( self.name + ": " + self.handle.before ) |
| 218 | main.cleanAndExit() |
| 219 | except Exception: |
| 220 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 221 | main.cleanAndExit() |