blob: 7a2e84f80f112b0649d9d657e2577d2923261c37 [file] [log] [blame]
You Wangdb927a52016-02-26 11:03:28 -08001"""
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002Copyright 2016 Open Networking Foundation ( ONF )
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07003
4Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
5the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
6or 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 Ronquillo23fb2162017-09-15 14:59:57 -070011 ( at your option ) any later version.
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -070012
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 Ronquillob27ce4c2017-07-17 12:41:28 -070021"""
You Wangdb927a52016-02-26 11:03:28 -080022This file contains classes for CHOTestMonkey that are related to network event
23Author: you@onlab.us
24"""
25from tests.CHOTestMonkey.dependencies.events.Event import EventType, EventStates, Event
26from tests.CHOTestMonkey.dependencies.elements.NetworkElement import NetworkElement, Device, Host, Link
You Wangdb927a52016-02-26 11:03:28 -080027
Jon Hall2bb3e212017-05-24 17:07:25 -070028
You Wangdb927a52016-02-26 11:03:28 -080029class LinkEvent( Event ):
Jon Hall2bb3e212017-05-24 17:07:25 -070030
You Wangdb927a52016-02-26 11:03:28 -080031 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 Hall2bb3e212017-05-24 17:07:25 -070041 args are the names of the two link ends, e.g. [ 's1', 's2' ]
You Wangdb927a52016-02-26 11:03:28 -080042 """
43 with self.eventLock:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070044 # main.log.info( "%s - starting event" % ( self.typeString ) )
You Wangdb927a52016-02-26 11:03:28 -080045 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 Wangdb927a52016-02-26 11:03:28 -080052 if self.typeIndex == EventType().NETWORK_LINK_DOWN:
You Wang221db322016-06-03 15:45:52 -070053 with main.mininetLock:
You Wang7d14d642019-01-23 15:10:08 -080054 linkRandom = main.Mininet1.getLinkRandom( switchClasses=r"(OVSSwitch)",
55 excludeNodes=[ 'bgp', 'cs', 'nat', 'dhcp', 'r' ] )
Jon Hall2bb3e212017-05-24 17:07:25 -070056 if linkRandom is None:
You Wang221db322016-06-03 15:45:52 -070057 main.log.warn( "No link available, aborting event" )
58 return EventStates().ABORT
59 args[ 0 ] = linkRandom[ 0 ]
60 args[ 1 ] = linkRandom[ 1 ]
You Wangdb927a52016-02-26 11:03:28 -080061 elif self.typeIndex == EventType().NETWORK_LINK_UP:
You Wang221db322016-06-03 15:45:52 -070062 import random
You Wangdb927a52016-02-26 11:03:28 -080063 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 Hall2bb3e212017-05-24 17:07:25 -070077 if self.linkA is None or self.linkB is None:
You Wangdb927a52016-02-26 11:03:28 -080078 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 Hall2bb3e212017-05-24 17:07:25 -070083 if self.linkA is not None and self.linkB is not None:
You Wangdb927a52016-02-26 11:03:28 -080084 break
Jon Hall2bb3e212017-05-24 17:07:25 -070085 if self.linkA is None or self.linkB is None:
You Wangdb927a52016-02-26 11:03:28 -080086 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 Hall2bb3e212017-05-24 17:07:25 -070091
You Wangdb927a52016-02-26 11:03:28 -080092class LinkDown( LinkEvent ):
Jon Hall2bb3e212017-05-24 17:07:25 -070093
You Wangdb927a52016-02-26 11:03:28 -080094 """
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 Hall2bb3e212017-05-24 17:07:25 -0700104 assert self.linkA is not None and self.linkB is not None
You Wangdb927a52016-02-26 11:03:28 -0800105 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 Wang52163202016-07-14 16:37:15 -0700112 main.log.info( "Event recorded: {} {} {} {}".format( self.typeIndex, self.typeString, self.linkA.deviceA.name, self.linkA.deviceB.name ) )
You Wangdb927a52016-02-26 11:03:28 -0800113 with main.mininetLock:
Jon Hall2bb3e212017-05-24 17:07:25 -0700114 """
You Wangdb927a52016-02-26 11:03:28 -0800115 result = main.Mininet1.link( END1=self.linkA.deviceA.name,
116 END2=self.linkA.deviceB.name,
Jon Hall2bb3e212017-05-24 17:07:25 -0700117 OPTION="down" )
118 """
You Wang221db322016-06-03 15:45:52 -0700119 result = main.Mininet1.delLink( self.linkA.deviceA.name,
120 self.linkA.deviceB.name )
You Wangdb927a52016-02-26 11:03:28 -0800121 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 Hall2bb3e212017-05-24 17:07:25 -0700129
You Wangdb927a52016-02-26 11:03:28 -0800130class LinkUp( LinkEvent ):
Jon Hall2bb3e212017-05-24 17:07:25 -0700131
You Wangdb927a52016-02-26 11:03:28 -0800132 """
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 Hall2bb3e212017-05-24 17:07:25 -0700141 assert self.linkA is not None and self.linkB is not None
You Wangdb927a52016-02-26 11:03:28 -0800142 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 Wang52163202016-07-14 16:37:15 -0700149 main.log.info( "Event recorded: {} {} {} {}".format( self.typeIndex, self.typeString, self.linkA.deviceA.name, self.linkA.deviceB.name ) )
You Wangdb927a52016-02-26 11:03:28 -0800150 with main.mininetLock:
Jon Hall2bb3e212017-05-24 17:07:25 -0700151 """
You Wangdb927a52016-02-26 11:03:28 -0800152 result = main.Mininet1.link( END1=self.linkA.deviceA.name,
153 END2=self.linkA.deviceB.name,
Jon Hall2bb3e212017-05-24 17:07:25 -0700154 OPTION="up" )
155 """
You Wang221db322016-06-03 15:45:52 -0700156 result = main.Mininet1.addLink( self.linkA.deviceA.name,
157 self.linkA.deviceB.name )
You Wangdb927a52016-02-26 11:03:28 -0800158 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 Hall2bb3e212017-05-24 17:07:25 -0700166
You Wangdb927a52016-02-26 11:03:28 -0800167class DeviceEvent( Event ):
Jon Hall2bb3e212017-05-24 17:07:25 -0700168
You Wangdb927a52016-02-26 11:03:28 -0800169 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 Ronquillo23fb2162017-09-15 14:59:57 -0700181 # main.log.info( "%s - starting event" % ( self.typeString ) )
You Wangdb927a52016-02-26 11:03:28 -0800182 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 Wanga9a5e002019-01-31 12:33:26 -0800191 if main.params[ 'TOPO' ][ 'excludeSwitches' ]:
192 excludeSwitches = main.params[ 'TOPO' ][ 'excludeSwitches' ].split( ',' )
193 else:
194 excludeSwitches = []
You Wang221db322016-06-03 15:45:52 -0700195 with main.mininetLock:
You Wang7d14d642019-01-23 15:10:08 -0800196 switchRandom = main.Mininet1.getSwitchRandom( switchClasses=r"(OVSSwitch)",
You Wanga9a5e002019-01-31 12:33:26 -0800197 excludeNodes=[ 'bgp', 'cs', 'nat', 'dhcp', 'r' ],
198 excludeSwitches=excludeSwitches )
Jon Hall2bb3e212017-05-24 17:07:25 -0700199 if switchRandom is None:
You Wang221db322016-06-03 15:45:52 -0700200 main.log.warn( "No switch available, aborting event" )
201 return EventStates().ABORT
202 args[ 0 ] = switchRandom
You Wangdb927a52016-02-26 11:03:28 -0800203 elif self.typeIndex == EventType().NETWORK_DEVICE_UP:
204 with main.variableLock:
205 removedDevices = []
206 for device in main.devices:
207 if device.isRemoved():
208 removedDevices.append( device )
209 if len( removedDevices ) == 0:
210 main.log.warn( "None of the devices are removed, aborting event" )
211 return EventStates().ABORT
212 deviceList = random.sample( removedDevices, 1 )
213 self.device = deviceList[ 0 ]
Jon Hall2bb3e212017-05-24 17:07:25 -0700214 if self.device is None:
You Wangdb927a52016-02-26 11:03:28 -0800215 for device in main.devices:
216 if device.name == args[ 0 ]:
217 self.device = device
Jon Hall2bb3e212017-05-24 17:07:25 -0700218 if self.device is None:
You Wangdb927a52016-02-26 11:03:28 -0800219 main.log.warn( "Device %s does not exist: " % ( args[ 0 ] ) )
220 return EventStates().ABORT
221 main.log.debug( "%s - %s" % ( self.typeString, self.device ) )
222 return self.startDeviceEvent()
223
Jon Hall2bb3e212017-05-24 17:07:25 -0700224
You Wangdb927a52016-02-26 11:03:28 -0800225class DeviceDown( DeviceEvent ):
Jon Hall2bb3e212017-05-24 17:07:25 -0700226
You Wangdb927a52016-02-26 11:03:28 -0800227 """
Jon Hall2bb3e212017-05-24 17:07:25 -0700228 Generate a device down event ( which actually removes this device for now ) giving its name
You Wangdb927a52016-02-26 11:03:28 -0800229 """
230 def __init__( self ):
231 DeviceEvent.__init__( self )
232 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
233 self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
234
235 def startDeviceEvent( self ):
Jon Hall2bb3e212017-05-24 17:07:25 -0700236 assert self.device is not None
You Wangdb927a52016-02-26 11:03:28 -0800237 with main.variableLock:
238 if self.device.isRemoved():
239 main.log.warn( "Device Down - device has been removed" )
240 return EventStates().ABORT
You Wang52163202016-07-14 16:37:15 -0700241 main.log.info( "Event recorded: {} {} {}".format( self.typeIndex, self.typeString, self.device.name ) )
You Wang7d14d642019-01-23 15:10:08 -0800242 result = main.TRUE
You Wangdb927a52016-02-26 11:03:28 -0800243 with main.mininetLock:
You Wang7d14d642019-01-23 15:10:08 -0800244 # Disable ports toward dual-homed hosts
245 for host, port in self.device.hosts.items():
246 if host.isDualHomed:
247 main.log.info( "Disable port {}/{} which connects to a dual-homed host before bringing down this device".format( self.device.dpid, port ) )
248 result = result and main.Cluster.active( 0 ).CLI.portstate( dpid=self.device.dpid, port=port, state="disable" )
249 # result = main.Mininet1.delSwitch( self.device.name )
250 result = result and main.Mininet1.switch( SW=self.device.name, OPTION="stop" )
You Wangdb927a52016-02-26 11:03:28 -0800251 if not result:
252 main.log.warn( "%s - failed to bring down device" % ( self.typeString ) )
253 return EventStates().FAIL
254 with main.variableLock:
255 self.device.setRemoved()
256 for link in self.device.outgoingLinks:
257 link.setRemoved()
258 link.backwardLink.setRemoved()
259 for host in self.device.hosts:
260 host.setRemoved()
You Wang2b687c02016-05-13 17:01:31 -0700261 for intent in main.intents:
262 if intent.deviceA == self.device or intent.deviceB == self.device:
263 intent.setFailed()
You Wangdb927a52016-02-26 11:03:28 -0800264 return EventStates().PASS
265
Jon Hall2bb3e212017-05-24 17:07:25 -0700266
You Wangdb927a52016-02-26 11:03:28 -0800267class DeviceUp( DeviceEvent ):
Jon Hall2bb3e212017-05-24 17:07:25 -0700268
You Wangdb927a52016-02-26 11:03:28 -0800269 """
Jon Hall2bb3e212017-05-24 17:07:25 -0700270 Generate a device up event ( which re-adds this device in case the device is removed ) giving its name
You Wangdb927a52016-02-26 11:03:28 -0800271 """
272 def __init__( self ):
273 DeviceEvent.__init__( self )
274 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
275 self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
276
277 def startDeviceEvent( self ):
Jon Hall2bb3e212017-05-24 17:07:25 -0700278 assert self.device is not None
You Wangdb927a52016-02-26 11:03:28 -0800279 with main.variableLock:
280 if self.device.isUp():
281 main.log.warn( "Device Up - device already up" )
282 return EventStates().ABORT
283 # Re-add the device
You Wang52163202016-07-14 16:37:15 -0700284 main.log.info( "Event recorded: {} {} {}".format( self.typeIndex, self.typeString, self.device.name ) )
You Wangdb927a52016-02-26 11:03:28 -0800285 with main.mininetLock:
You Wang7d14d642019-01-23 15:10:08 -0800286 # result = main.Mininet1.addSwitch( self.device.name, dpid=self.device.dpid[ 3: ] )
287 result = main.Mininet1.switch( SW=self.device.name, OPTION='start' )
You Wangdb927a52016-02-26 11:03:28 -0800288 if not result:
289 main.log.warn( "%s - failed to re-add device" % ( self.typeString ) )
290 return EventStates().FAIL
291 with main.variableLock:
292 self.device.bringUp()
You Wang7d14d642019-01-23 15:10:08 -0800293 '''
You Wangdb927a52016-02-26 11:03:28 -0800294 # Re-add links
295 # We add host-device links first since we did the same in mininet topology file
296 # TODO: a more rubust way is to add links according to the port info of the device
297 for host in self.device.hosts:
298 # Add host-device link
299 with main.mininetLock:
300 result = main.Mininet1.addLink( self.device.name, host.name )
301 if not result:
302 main.log.warn( "%s - failed to re-connect host %s to device" % ( self.typeString, host.name ) )
303 return EventStates().FAIL
304 for link in self.device.outgoingLinks:
305 neighbor = link.deviceB
306 # Skip bringing up any link that connecting this device to a removed neighbor
307 if neighbor.isRemoved():
308 continue
309 with main.mininetLock:
310 result = main.Mininet1.addLink( self.device.name, neighbor.name )
311 if not result:
312 main.log.warn( "%s - failed to re-add link to %s" % ( self.typeString, neighbor.name ) )
313 return EventStates().FAIL
314 with main.variableLock:
315 link.bringUp()
316 link.backwardLink.bringUp()
You Wang2b687c02016-05-13 17:01:31 -0700317 for intent in main.intents:
318 if intent.isFailed():
319 if intent.deviceA == self.device and intent.deviceB.isUp() or\
Jon Hall2bb3e212017-05-24 17:07:25 -0700320 intent.deviceB == self.device and intent.deviceA.isUp():
You Wang2b687c02016-05-13 17:01:31 -0700321 intent.setInstalled()
You Wang7d14d642019-01-23 15:10:08 -0800322 '''
You Wangdb927a52016-02-26 11:03:28 -0800323 # Re-assign mastership for the device
324 with main.mininetLock:
Devin Lim142b5342017-07-20 15:22:39 -0700325 ips = main.Cluster.getIps()
326 main.Mininet1.assignSwController( sw=self.device.name, ip=ips )
You Wang7d14d642019-01-23 15:10:08 -0800327 for link in self.device.outgoingLinks:
328 neighbor = link.deviceB
329 # Skip bringing up any link that connecting this device to a removed neighbor
330 if neighbor.isRemoved():
331 continue
332 # Bring down again any link that was brought down before the device was down
333 if int( link.portB ) in link.deviceB.downPorts:
334 with main.variableLock:
335 link.bringDown()
336 link.backwardLink.bringDown()
You Wangdb927a52016-02-26 11:03:28 -0800337 else:
You Wang7d14d642019-01-23 15:10:08 -0800338 with main.variableLock:
339 link.bringUp()
340 link.backwardLink.bringUp()
341 # Re-discover hosts
342 if self.device.hosts:
343 main.Mininet1.discoverHosts( hostList=[ host.name for host in self.device.hosts ] )
344 for host in self.device.hosts:
You Wangdb927a52016-02-26 11:03:28 -0800345 with main.variableLock:
346 host.bringUp()
You Wang7d14d642019-01-23 15:10:08 -0800347 self.device.downPorts = []
348 return EventStates().PASS
349
350
351class PortEvent( Event ):
352
353 def __init__( self ):
354 Event.__init__( self )
355 self.device = None
356 self.port = None
357 self.link = None
358
359 def startPortEvent( self ):
360 return EventStates().PASS
361
362 def startEvent( self, args ):
363 """
364 args are the device name and port number, e.g. [ 's1', '5' ]
365 """
366 with self.eventLock:
367 # main.log.info( "%s - starting event" % ( self.typeString ) )
368 if len( args ) < 2:
369 main.log.warn( "%s - Not enough arguments: %s" % ( self.typeString, args ) )
370 return EventStates().ABORT
371 elif len( args ) > 2:
372 main.log.warn( "%s - Too many arguments: %s" % ( self.typeString, args ) )
373 return EventStates().ABORT
374 if args[ 0 ] == 'random' or args[ 1 ] == 'random':
375 if self.typeIndex == EventType().NETWORK_PORT_DOWN:
376 with main.mininetLock:
377 linkRandom = main.Mininet1.getLinkRandom( switchClasses=r"(OVSSwitch)",
378 excludeNodes=[ 'bgp', 'cs', 'nat', 'dhcp', 'r' ])
379 if linkRandom is None:
380 main.log.warn( "No link available, aborting event" )
381 return EventStates().ABORT
382 for link in main.links:
383 if link.deviceA.name == linkRandom[ 0 ] and link.deviceB.name == linkRandom[ 1 ]:
384 self.device = link.deviceA
385 self.port = int( link.portA )
386 if not self.device:
387 main.log.warn( "Failed to get a radnom device port, aborting event" )
388 return EventStates().ABORT
389 elif self.typeIndex == EventType().NETWORK_PORT_UP:
390 import random
391 with main.variableLock:
392 downPorts = {}
393 for link in main.links:
394 if link.isDown():
395 if int( link.portA ) in link.deviceA.downPorts:
396 downPorts[ link.deviceA ] = link.portA
397 if len( downPorts ) == 0:
398 main.log.warn( "None of the links are in 'down' state, aborting event" )
399 return EventStates().ABORT
400 deviceList = random.sample( downPorts, 1 )
401 self.device = deviceList[ 0 ]
402 self.port = int( downPorts[ self.device ] )
403 if self.device is None:
404 for device in main.devices:
405 if device.name == args[ 0 ]:
406 self.device = device
407 if self.device is None:
408 main.log.warn( "Device %s does not exist: " % ( args[ 0 ] ) )
409 return EventStates().ABORT
410 if self.port is None:
411 try:
412 self.port = int( args[ 1 ] )
413 except Exception:
414 main.log.warn( "Device port is not a number: {}".format( args[ 1 ] ) )
415 return EventStates().ABORT
416 if self.link is None:
417 for link in main.links:
418 if link.deviceA.name == self.device.name and int( link.portA ) == self.port:
419 self.link = link
420 if self.link is None:
421 main.log.warn( "There's no link on device {} port {}".format( self.device.name, self.port ) )
422 return EventStates().ABORT
423 main.log.debug( "%s - %s:%s" % ( self.typeString, self.device, self.port ) )
424 return self.startPortEvent()
425
426
427class PortDown( PortEvent ):
428
429 """
430 Generate a port down event giving the device name and port number
431 """
432 def __init__( self ):
433 PortEvent.__init__( self )
434 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
435 self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
436
437 def startPortEvent( self ):
438 assert self.device is not None and self.port is not None and self.link is not None
439 if self.link.isDown():
440 main.log.warn( "port Down - link already down" )
441 return EventStates().ABORT
442 elif self.link.isRemoved():
443 main.log.warn( "port Down - link has been removed" )
444 return EventStates().ABORT
445 main.log.info( "Event recorded: {} {} {} {}".format( self.typeIndex, self.typeString, self.device.name, self.port ) )
446 with main.mininetLock:
447 result = main.Cluster.active( 0 ).CLI.portstate( dpid=self.device.dpid, port=self.port, state="disable" )
448 if not result:
449 main.log.warn( "%s - failed to bring down port" % ( self.typeString ) )
450 return EventStates().FAIL
451 with main.variableLock:
452 self.device.downPorts.append( self.port )
453 self.link.bringDown()
454 self.link.backwardLink.bringDown()
455 return EventStates().PASS
456
457
458class PortUp( PortEvent ):
459
460 """
461 Generate a port up event giving the device name and port number
462 """
463 def __init__( self ):
464 PortEvent.__init__( self )
465 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
466 self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
467
468 def startPortEvent( self ):
469 assert self.device is not None and self.port is not None and self.link is not None
470 if self.link.isUp():
471 main.log.warn( "port up - link already up" )
472 return EventStates().ABORT
473 elif self.link.isRemoved():
474 main.log.warn( "port up - link has been removed" )
475 return EventStates().ABORT
476 main.log.info( "Event recorded: {} {} {} {}".format( self.typeIndex, self.typeString, self.device.name, self.port ) )
477 with main.mininetLock:
478 result = main.Cluster.active( 0 ).CLI.portstate( dpid=self.device.dpid, port=self.port, state="enable" )
479 if not result:
480 main.log.warn( "%s - failed to bring up port " % ( self.typeString ) )
481 return EventStates().FAIL
482 with main.variableLock:
483 self.device.downPorts.remove( self.port )
484 self.link.bringUp()
485 self.link.backwardLink.bringUp()
You Wangdb927a52016-02-26 11:03:28 -0800486 return EventStates().PASS