You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 1 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 2 | Copyright 2016 Open Networking Foundation ( ONF ) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 3 | |
| 4 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 5 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 6 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 11 | ( at your option ) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 20 | """ |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 21 | """ |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 22 | This file contains classes for CHOTestMonkey that are related to network event |
| 23 | Author: you@onlab.us |
| 24 | """ |
| 25 | from tests.CHOTestMonkey.dependencies.events.Event import EventType, EventStates, Event |
| 26 | from tests.CHOTestMonkey.dependencies.elements.NetworkElement import NetworkElement, Device, Host, Link |
You Wang | ad483c8 | 2019-03-13 15:25:44 -0700 | [diff] [blame^] | 27 | import time |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 28 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 29 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 30 | class LinkEvent( Event ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 31 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 32 | def __init__( self ): |
| 33 | Event.__init__( self ) |
| 34 | self.linkA = None |
| 35 | self.linkB = None |
| 36 | |
| 37 | def startLinkEvent( self ): |
| 38 | return EventStates().PASS |
| 39 | |
| 40 | def startEvent( self, args ): |
| 41 | """ |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 42 | args are the names of the two link ends, e.g. [ 's1', 's2' ] |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 43 | """ |
| 44 | with self.eventLock: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 45 | # main.log.info( "%s - starting event" % ( self.typeString ) ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 46 | if len( args ) < 2: |
| 47 | main.log.warn( "%s - Not enough arguments: %s" % ( self.typeString, args ) ) |
| 48 | return EventStates().ABORT |
| 49 | elif len( args ) > 2: |
| 50 | main.log.warn( "%s - Too many arguments: %s" % ( self.typeString, args ) ) |
| 51 | return EventStates().ABORT |
| 52 | if args[ 0 ] == 'random' or args[ 1 ] == 'random': |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 53 | if self.typeIndex == EventType().NETWORK_LINK_DOWN: |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 54 | with main.networkLock: |
| 55 | linkRandom = main.Network.getLinkRandom( excludeNodes=main.excludeNodes, |
| 56 | skipLinks=main.skipLinks ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 57 | if linkRandom is None: |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 58 | main.log.warn( "No link available, aborting event" ) |
| 59 | return EventStates().ABORT |
| 60 | args[ 0 ] = linkRandom[ 0 ] |
| 61 | args[ 1 ] = linkRandom[ 1 ] |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 62 | elif self.typeIndex == EventType().NETWORK_LINK_UP: |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 63 | import random |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 64 | with main.variableLock: |
| 65 | downLinks = [] |
| 66 | for link in main.links: |
| 67 | if link.isDown(): |
| 68 | downLinks.append( link ) |
| 69 | if len( downLinks ) == 0: |
| 70 | main.log.warn( "None of the links are in 'down' state, aborting event" ) |
| 71 | return EventStates().ABORT |
| 72 | linkList = random.sample( downLinks, 1 ) |
| 73 | self.linkA = linkList[ 0 ] |
| 74 | self.linkB = linkList[ 0 ].backwardLink |
| 75 | elif args[ 0 ] == args[ 1 ]: |
| 76 | main.log.warn( "%s - invalid arguments: %s" % ( self.typeString, args ) ) |
| 77 | return EventStates().ABORT |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 78 | if self.linkA is None or self.linkB is None: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 79 | for link in main.links: |
| 80 | if link.deviceA.name == args[ 0 ] and link.deviceB.name == args[ 1 ]: |
| 81 | self.linkA = link |
| 82 | elif link.deviceA.name == args[ 1 ] and link.deviceB.name == args[ 0 ]: |
| 83 | self.linkB = link |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 84 | if self.linkA is not None and self.linkB is not None: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 85 | break |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 86 | if self.linkA is None or self.linkB is None: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 87 | main.log.warn( "Bidirectional link %s - %s does not exist: " % ( args[ 0 ], args[ 1 ] ) ) |
| 88 | return EventStates().ABORT |
| 89 | main.log.debug( "%s - %s" % ( self.typeString, self.linkA ) ) |
| 90 | return self.startLinkEvent() |
| 91 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 92 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 93 | class LinkDown( LinkEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 94 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 95 | """ |
| 96 | Generate a link down event giving the two ends of the link |
| 97 | """ |
| 98 | def __init__( self ): |
| 99 | LinkEvent.__init__( self ) |
| 100 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
| 101 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
| 102 | |
| 103 | def startLinkEvent( self ): |
| 104 | # TODO: do we need to handle a unidirectional link? |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 105 | assert self.linkA is not None and self.linkB is not None |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 106 | with main.variableLock: |
| 107 | if self.linkA.isDown() or self.linkB.isDown(): |
| 108 | main.log.warn( "Link Down - link already down" ) |
| 109 | return EventStates().ABORT |
| 110 | elif self.linkA.isRemoved() or self.linkB.isRemoved(): |
| 111 | main.log.warn( "Link Down - link has been removed" ) |
| 112 | return EventStates().ABORT |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 113 | main.log.info( "Event recorded: {} {} {} {}".format( self.typeIndex, self.typeString, self.linkA.deviceA.name, self.linkA.deviceB.name ) ) |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 114 | with main.networkLock: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 115 | """ |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 116 | result = main.Network.link( END1=self.linkA.deviceA.name, |
| 117 | END2=self.linkA.deviceB.name, |
| 118 | OPTION="down" ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 119 | """ |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 120 | result = main.Network.delLink( self.linkA.deviceA.name, |
| 121 | self.linkA.deviceB.name ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 122 | if not result: |
| 123 | main.log.warn( "%s - failed to bring down link" % ( self.typeString ) ) |
| 124 | return EventStates().FAIL |
| 125 | with main.variableLock: |
| 126 | self.linkA.bringDown() |
| 127 | self.linkB.bringDown() |
| 128 | return EventStates().PASS |
| 129 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 130 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 131 | class LinkUp( LinkEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 132 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 133 | """ |
| 134 | Generate a link up event giving the two ends of the link |
| 135 | """ |
| 136 | def __init__( self ): |
| 137 | LinkEvent.__init__( self ) |
| 138 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
| 139 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
| 140 | |
| 141 | def startLinkEvent( self ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 142 | assert self.linkA is not None and self.linkB is not None |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 143 | with main.variableLock: |
| 144 | if self.linkA.isUp() or self.linkB.isUp(): |
| 145 | main.log.warn( "Link Up - link already up" ) |
| 146 | return EventStates().ABORT |
| 147 | if self.linkA.isRemoved() or self.linkB.isRemoved(): |
| 148 | main.log.warn( "Link Up - link has been removed" ) |
| 149 | return EventStates().ABORT |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 150 | main.log.info( "Event recorded: {} {} {} {}".format( self.typeIndex, self.typeString, self.linkA.deviceA.name, self.linkA.deviceB.name ) ) |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 151 | with main.networkLock: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 152 | """ |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 153 | result = main.Network.link( END1=self.linkA.deviceA.name, |
| 154 | END2=self.linkA.deviceB.name, |
| 155 | OPTION="up" ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 156 | """ |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 157 | result = main.Network.addLink( self.linkA.deviceA.name, |
| 158 | self.linkA.deviceB.name ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 159 | if not result: |
| 160 | main.log.warn( "%s - failed to bring up link" % ( self.typeString ) ) |
| 161 | return EventStates().FAIL |
| 162 | with main.variableLock: |
| 163 | self.linkA.bringUp() |
| 164 | self.linkB.bringUp() |
| 165 | return EventStates().PASS |
| 166 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 167 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 168 | class DeviceEvent( Event ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 169 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 170 | def __init__( self ): |
| 171 | Event.__init__( self ) |
| 172 | self.device = None |
| 173 | |
| 174 | def startDeviceEvent( self ): |
| 175 | return EventStates().PASS |
| 176 | |
| 177 | def startEvent( self, args ): |
| 178 | """ |
| 179 | args are the names of the device, e.g. 's1' |
| 180 | """ |
| 181 | with self.eventLock: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 182 | # main.log.info( "%s - starting event" % ( self.typeString ) ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 183 | if len( args ) < 1: |
| 184 | main.log.warn( "%s - Not enough arguments: %s" % ( self.typeString, args ) ) |
| 185 | return EventStates().ABORT |
| 186 | elif len( args ) > 1: |
| 187 | main.log.warn( "%s - Too many arguments: %s" % ( self.typeString, args ) ) |
| 188 | return EventStates().ABORT |
| 189 | if args[ 0 ] == 'random': |
| 190 | import random |
| 191 | if self.typeIndex == EventType().NETWORK_DEVICE_DOWN: |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 192 | with main.networkLock: |
| 193 | switchRandom = main.Network.getSwitchRandom( excludeNodes=main.excludeNodes, |
| 194 | skipSwitches=main.skipSwitches ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 195 | if switchRandom is None: |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 196 | main.log.warn( "No switch available, aborting event" ) |
| 197 | return EventStates().ABORT |
| 198 | args[ 0 ] = switchRandom |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 199 | elif self.typeIndex == EventType().NETWORK_DEVICE_UP: |
| 200 | with main.variableLock: |
| 201 | removedDevices = [] |
| 202 | for device in main.devices: |
| 203 | if device.isRemoved(): |
| 204 | removedDevices.append( device ) |
| 205 | if len( removedDevices ) == 0: |
| 206 | main.log.warn( "None of the devices are removed, aborting event" ) |
| 207 | return EventStates().ABORT |
| 208 | deviceList = random.sample( removedDevices, 1 ) |
| 209 | self.device = deviceList[ 0 ] |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 210 | if self.device is None: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 211 | for device in main.devices: |
| 212 | if device.name == args[ 0 ]: |
| 213 | self.device = device |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 214 | if self.device is None: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 215 | main.log.warn( "Device %s does not exist: " % ( args[ 0 ] ) ) |
| 216 | return EventStates().ABORT |
| 217 | main.log.debug( "%s - %s" % ( self.typeString, self.device ) ) |
| 218 | return self.startDeviceEvent() |
| 219 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 220 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 221 | class DeviceDown( DeviceEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 222 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 223 | """ |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 224 | Generate a device down event ( which actually removes this device for now ) giving its name |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 225 | """ |
| 226 | def __init__( self ): |
| 227 | DeviceEvent.__init__( self ) |
| 228 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
| 229 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
| 230 | |
| 231 | def startDeviceEvent( self ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 232 | assert self.device is not None |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 233 | with main.variableLock: |
| 234 | if self.device.isRemoved(): |
| 235 | main.log.warn( "Device Down - device has been removed" ) |
| 236 | return EventStates().ABORT |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 237 | main.log.info( "Event recorded: {} {} {}".format( self.typeIndex, self.typeString, self.device.name ) ) |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 238 | result = main.TRUE |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 239 | with main.networkLock: |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 240 | # Disable ports toward dual-homed hosts |
| 241 | for host, port in self.device.hosts.items(): |
| 242 | if host.isDualHomed: |
| 243 | main.log.info( "Disable port {}/{} which connects to a dual-homed host before bringing down this device".format( self.device.dpid, port ) ) |
| 244 | result = result and main.Cluster.active( 0 ).CLI.portstate( dpid=self.device.dpid, port=port, state="disable" ) |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 245 | # result = main.Network.delSwitch( self.device.name ) |
| 246 | result = result and main.Network.switch( SW=self.device.name, OPTION="stop" ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 247 | if not result: |
| 248 | main.log.warn( "%s - failed to bring down device" % ( self.typeString ) ) |
| 249 | return EventStates().FAIL |
| 250 | with main.variableLock: |
| 251 | self.device.setRemoved() |
| 252 | for link in self.device.outgoingLinks: |
| 253 | link.setRemoved() |
| 254 | link.backwardLink.setRemoved() |
| 255 | for host in self.device.hosts: |
| 256 | host.setRemoved() |
You Wang | 2b687c0 | 2016-05-13 17:01:31 -0700 | [diff] [blame] | 257 | for intent in main.intents: |
| 258 | if intent.deviceA == self.device or intent.deviceB == self.device: |
| 259 | intent.setFailed() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 260 | return EventStates().PASS |
| 261 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 262 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 263 | class DeviceUp( DeviceEvent ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 264 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 265 | """ |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 266 | Generate a device up event ( which re-adds this device in case the device is removed ) giving its name |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 267 | """ |
| 268 | def __init__( self ): |
| 269 | DeviceEvent.__init__( self ) |
| 270 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
| 271 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
| 272 | |
| 273 | def startDeviceEvent( self ): |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 274 | assert self.device is not None |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 275 | with main.variableLock: |
| 276 | if self.device.isUp(): |
| 277 | main.log.warn( "Device Up - device already up" ) |
| 278 | return EventStates().ABORT |
| 279 | # Re-add the device |
You Wang | 5216320 | 2016-07-14 16:37:15 -0700 | [diff] [blame] | 280 | main.log.info( "Event recorded: {} {} {}".format( self.typeIndex, self.typeString, self.device.name ) ) |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 281 | if hasattr( main, 'Mininet1' ): |
| 282 | with main.networkLock: |
| 283 | # result = main.Network.addSwitch( self.device.name, dpid=self.device.dpid[ 3: ] ) |
| 284 | result = main.Network.switch( SW=self.device.name, OPTION='start' ) |
| 285 | if not result: |
| 286 | main.log.warn( "%s - failed to re-add device" % ( self.typeString ) ) |
| 287 | return EventStates().FAIL |
| 288 | # Re-assign mastership for the device |
| 289 | with main.networkLock: |
| 290 | result = main.Network.assignSwController( sw=self.device.name, ip=main.Cluster.getIps() ) |
| 291 | if not result: |
| 292 | main.log.warn( "%s - failed to assign device to controller" % ( self.typeString ) ) |
| 293 | return EventStates().FAIL |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 294 | with main.variableLock: |
| 295 | self.device.bringUp() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 296 | for link in self.device.outgoingLinks: |
| 297 | neighbor = link.deviceB |
| 298 | # Skip bringing up any link that connecting this device to a removed neighbor |
| 299 | if neighbor.isRemoved(): |
| 300 | continue |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 301 | # FIXME: remove this temporary hack for CORD-3240 |
| 302 | if neighbor.name == 's225': |
You Wang | ad483c8 | 2019-03-13 15:25:44 -0700 | [diff] [blame^] | 303 | time.sleep( 5 ) |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 304 | main.NetworkBench.switches[ 's225' ].setPortSpeed( index=link.portB ) |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 305 | # Bring down again any link that was brought down before the device was down |
| 306 | if int( link.portB ) in link.deviceB.downPorts: |
| 307 | with main.variableLock: |
| 308 | link.bringDown() |
| 309 | link.backwardLink.bringDown() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 310 | else: |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 311 | with main.variableLock: |
| 312 | link.bringUp() |
| 313 | link.backwardLink.bringUp() |
| 314 | # Re-discover hosts |
| 315 | if self.device.hosts: |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 316 | main.Network.discoverHosts( hostList=[ host.name for host in self.device.hosts ] ) |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 317 | for host in self.device.hosts: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 318 | with main.variableLock: |
| 319 | host.bringUp() |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 320 | self.device.downPorts = [] |
| 321 | return EventStates().PASS |
| 322 | |
| 323 | |
| 324 | class PortEvent( Event ): |
| 325 | |
| 326 | def __init__( self ): |
| 327 | Event.__init__( self ) |
| 328 | self.device = None |
| 329 | self.port = None |
| 330 | self.link = None |
| 331 | |
| 332 | def startPortEvent( self ): |
| 333 | return EventStates().PASS |
| 334 | |
| 335 | def startEvent( self, args ): |
| 336 | """ |
| 337 | args are the device name and port number, e.g. [ 's1', '5' ] |
| 338 | """ |
| 339 | with self.eventLock: |
| 340 | # main.log.info( "%s - starting event" % ( self.typeString ) ) |
| 341 | if len( args ) < 2: |
| 342 | main.log.warn( "%s - Not enough arguments: %s" % ( self.typeString, args ) ) |
| 343 | return EventStates().ABORT |
| 344 | elif len( args ) > 2: |
| 345 | main.log.warn( "%s - Too many arguments: %s" % ( self.typeString, args ) ) |
| 346 | return EventStates().ABORT |
| 347 | if args[ 0 ] == 'random' or args[ 1 ] == 'random': |
| 348 | if self.typeIndex == EventType().NETWORK_PORT_DOWN: |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 349 | with main.networkLock: |
| 350 | linkRandom = main.Network.getLinkRandom( excludeNodes=main.excludeNodes, |
| 351 | skipLinks=main.skipLinks ) |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 352 | if linkRandom is None: |
| 353 | main.log.warn( "No link available, aborting event" ) |
| 354 | return EventStates().ABORT |
| 355 | for link in main.links: |
| 356 | if link.deviceA.name == linkRandom[ 0 ] and link.deviceB.name == linkRandom[ 1 ]: |
| 357 | self.device = link.deviceA |
| 358 | self.port = int( link.portA ) |
| 359 | if not self.device: |
| 360 | main.log.warn( "Failed to get a radnom device port, aborting event" ) |
| 361 | return EventStates().ABORT |
| 362 | elif self.typeIndex == EventType().NETWORK_PORT_UP: |
| 363 | import random |
| 364 | with main.variableLock: |
| 365 | downPorts = {} |
| 366 | for link in main.links: |
| 367 | if link.isDown(): |
| 368 | if int( link.portA ) in link.deviceA.downPorts: |
| 369 | downPorts[ link.deviceA ] = link.portA |
| 370 | if len( downPorts ) == 0: |
| 371 | main.log.warn( "None of the links are in 'down' state, aborting event" ) |
| 372 | return EventStates().ABORT |
| 373 | deviceList = random.sample( downPorts, 1 ) |
| 374 | self.device = deviceList[ 0 ] |
| 375 | self.port = int( downPorts[ self.device ] ) |
| 376 | if self.device is None: |
| 377 | for device in main.devices: |
| 378 | if device.name == args[ 0 ]: |
| 379 | self.device = device |
| 380 | if self.device is None: |
| 381 | main.log.warn( "Device %s does not exist: " % ( args[ 0 ] ) ) |
| 382 | return EventStates().ABORT |
| 383 | if self.port is None: |
| 384 | try: |
| 385 | self.port = int( args[ 1 ] ) |
| 386 | except Exception: |
| 387 | main.log.warn( "Device port is not a number: {}".format( args[ 1 ] ) ) |
| 388 | return EventStates().ABORT |
| 389 | if self.link is None: |
| 390 | for link in main.links: |
| 391 | if link.deviceA.name == self.device.name and int( link.portA ) == self.port: |
| 392 | self.link = link |
| 393 | if self.link is None: |
| 394 | main.log.warn( "There's no link on device {} port {}".format( self.device.name, self.port ) ) |
| 395 | return EventStates().ABORT |
| 396 | main.log.debug( "%s - %s:%s" % ( self.typeString, self.device, self.port ) ) |
| 397 | return self.startPortEvent() |
| 398 | |
| 399 | |
| 400 | class PortDown( PortEvent ): |
| 401 | |
| 402 | """ |
| 403 | Generate a port down event giving the device name and port number |
| 404 | """ |
| 405 | def __init__( self ): |
| 406 | PortEvent.__init__( self ) |
| 407 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
| 408 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
| 409 | |
| 410 | def startPortEvent( self ): |
| 411 | assert self.device is not None and self.port is not None and self.link is not None |
| 412 | if self.link.isDown(): |
| 413 | main.log.warn( "port Down - link already down" ) |
| 414 | return EventStates().ABORT |
| 415 | elif self.link.isRemoved(): |
| 416 | main.log.warn( "port Down - link has been removed" ) |
| 417 | return EventStates().ABORT |
| 418 | main.log.info( "Event recorded: {} {} {} {}".format( self.typeIndex, self.typeString, self.device.name, self.port ) ) |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 419 | with main.networkLock: |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 420 | result = main.Cluster.active( 0 ).CLI.portstate( dpid=self.device.dpid, port=self.port, state="disable" ) |
| 421 | if not result: |
| 422 | main.log.warn( "%s - failed to bring down port" % ( self.typeString ) ) |
| 423 | return EventStates().FAIL |
| 424 | with main.variableLock: |
| 425 | self.device.downPorts.append( self.port ) |
| 426 | self.link.bringDown() |
| 427 | self.link.backwardLink.bringDown() |
| 428 | return EventStates().PASS |
| 429 | |
| 430 | |
| 431 | class PortUp( PortEvent ): |
| 432 | |
| 433 | """ |
| 434 | Generate a port up event giving the device name and port number |
| 435 | """ |
| 436 | def __init__( self ): |
| 437 | PortEvent.__init__( self ) |
| 438 | self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ] |
| 439 | self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] ) |
| 440 | |
| 441 | def startPortEvent( self ): |
| 442 | assert self.device is not None and self.port is not None and self.link is not None |
| 443 | if self.link.isUp(): |
| 444 | main.log.warn( "port up - link already up" ) |
| 445 | return EventStates().ABORT |
| 446 | elif self.link.isRemoved(): |
| 447 | main.log.warn( "port up - link has been removed" ) |
| 448 | return EventStates().ABORT |
| 449 | main.log.info( "Event recorded: {} {} {} {}".format( self.typeIndex, self.typeString, self.device.name, self.port ) ) |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 450 | with main.networkLock: |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 451 | result = main.Cluster.active( 0 ).CLI.portstate( dpid=self.device.dpid, port=self.port, state="enable" ) |
| 452 | if not result: |
| 453 | main.log.warn( "%s - failed to bring up port " % ( self.typeString ) ) |
| 454 | return EventStates().FAIL |
You Wang | c61aaa2 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 455 | # FIXME: remove this temporary hack for CORD-3240 |
| 456 | if self.link.deviceB.name == 's225': |
| 457 | main.NetworkBench.switches[ 's225' ].setPortSpeed( index=self.link.portB ) |
You Wang | 9fc5ce4 | 2019-01-23 15:10:08 -0800 | [diff] [blame] | 458 | with main.variableLock: |
| 459 | self.device.downPorts.remove( self.port ) |
| 460 | self.link.bringUp() |
| 461 | self.link.backwardLink.bringUp() |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 462 | return EventStates().PASS |