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