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 the event generator class for CHOTestMonkey |
| 23 | Author: you@onlab.us |
| 24 | """ |
| 25 | from threading import Lock, Condition |
| 26 | from tests.CHOTestMonkey.dependencies.events.Event import EventType, EventStates, Event |
| 27 | from tests.CHOTestMonkey.dependencies.EventScheduler import EventScheduleMethod |
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 MessageType: |
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 | self.map = {} |
| 34 | # This message type is used for requesting an event injection from outside CHOTestMonkey |
| 35 | self.EVENT_REQUEST = 1 |
| 36 | self.map[ 1 ] = 'EVENT_REQUEST' |
| 37 | # This message tyoe will force the event generator to accept the event injection request for debugging purpose |
| 38 | self.EVENT_REQUEST_DEBUG = 2 |
| 39 | self.map[ 2 ] = 'EVENT_REQUEST_DEBUG' |
| 40 | # This message type implies the event generator has inserted the event |
| 41 | self.EVENT_INSERTED = 10 |
| 42 | self.map[ 10 ] = 'EVENT_INSERTED' |
| 43 | # This message type implies CHOTestMonkey has refused the event injection request |
| 44 | # due to, e.g. too many pending events in the scheduler |
| 45 | self.EVENT_DENIED = 11 |
| 46 | self.map[ 11 ] = 'EVENT_DENIED' |
| 47 | # The followings are error messages |
| 48 | self.UNKNOWN_MESSAGE = 20 |
| 49 | self.map[ 20 ] = 'UNKNOWN_MESSAGE' |
| 50 | self.UNKNOWN_EVENT_TYPE = 21 |
| 51 | self.map[ 21 ] = 'UNKNOWN_EVENT_TYPE' |
| 52 | self.UNKNOWN_SCHEDULE_METHOD = 22 |
| 53 | self.map[ 22 ] = 'UNKNOWN_SCHEDULE_METHOD' |
| 54 | self.NOT_ENOUGH_ARGUMENT = 23 |
| 55 | self.map[ 23 ] = 'NOT_ENOUGH_ARGUMENT' |
| 56 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 57 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 58 | class EventGenerator: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 59 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 60 | def __init__( self ): |
| 61 | self.default = '' |
| 62 | self.eventGeneratorLock = Lock() |
| 63 | |
| 64 | def startListener( self ): |
| 65 | """ |
| 66 | Listen to event triggers |
| 67 | """ |
| 68 | from multiprocessing.connection import Listener |
| 69 | import time |
| 70 | |
| 71 | host = "localhost" |
| 72 | port = int( main.params[ 'GENERATOR' ][ 'listenerPort' ] ) |
| 73 | address = ( host, port ) |
| 74 | listener = Listener( address ) |
| 75 | main.log.info( "Event Generator - Event listener start listening on %s:%s" % ( host, port ) ) |
| 76 | |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 77 | while True: |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 78 | conn = listener.accept() |
| 79 | t = main.Thread( target=self.handleConnection, |
| 80 | threadID=main.threadID, |
| 81 | name="handleConnection", |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 82 | args=[ conn ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 83 | t.start() |
| 84 | with main.variableLock: |
| 85 | main.threadID += 1 |
| 86 | listener.close() |
| 87 | |
| 88 | def handleConnection( self, conn ): |
| 89 | """ |
| 90 | Handle connections from event triggers |
| 91 | """ |
| 92 | request = conn.recv() |
| 93 | if isinstance( request, list ) and ( request[ 0 ] == MessageType().EVENT_REQUEST or request[ 0 ] == MessageType().EVENT_REQUEST_DEBUG ): |
| 94 | if len( request ) < 3: |
| 95 | response = MessageType().NOT_ENOUGH_ARGUMENT |
| 96 | elif request[ 0 ] == MessageType().EVENT_REQUEST and not main.eventScheduler.isAvailable(): |
| 97 | response = MessageType().EVENT_DENIED |
| 98 | else: |
| 99 | typeString = str( request[ 1 ] ) |
| 100 | scheduleMethodString = str( request[ 2 ] ) |
| 101 | if len( request ) > 3: |
| 102 | args = request[ 3: ] |
| 103 | else: |
| 104 | args = None |
| 105 | for key, value in EventType().map.items(): |
| 106 | if value == typeString: |
| 107 | typeIndex = key |
| 108 | break |
| 109 | if not value == typeString: |
| 110 | response = MessageType().UNKNOWN_EVENT_TYPE |
| 111 | else: |
| 112 | for key, value in EventScheduleMethod().map.items(): |
| 113 | if value == scheduleMethodString: |
| 114 | scheduleMethod = key |
| 115 | break |
| 116 | if not value == scheduleMethodString: |
| 117 | response = MessageType().UNKNOWN_SCHEDULE_METHOD |
| 118 | else: |
| 119 | self.insertEvent( typeIndex, scheduleMethod, args ) |
| 120 | response = MessageType().EVENT_INSERTED |
| 121 | else: |
| 122 | response = MessageType().UNKNOWN_MESSAGE |
| 123 | conn.send( response ) |
| 124 | conn.close() |
| 125 | |
| 126 | def triggerEvent( self, typeIndex, scheduleMethod, *args ): |
| 127 | """ |
| 128 | This function triggers an event from inside of CHOTestMonkey |
| 129 | """ |
| 130 | import time |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 131 | if typeIndex not in EventType().map.keys(): |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 132 | main.log.warn( "Event Generator - Unknown event type: " + str( typeIndex ) ) |
| 133 | return |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 134 | if scheduleMethod not in EventScheduleMethod().map.keys(): |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 135 | main.log.warn( "Event Generator - Unknown event schedule method: " + str( scheduleMethod ) ) |
| 136 | return |
| 137 | while not main.eventScheduler.isAvailable(): |
| 138 | time.sleep( int( main.params[ 'GENERATOR' ][ 'insertEventRetryInterval' ] ) ) |
| 139 | self.insertEvent( typeIndex, scheduleMethod, list( args ) ) |
| 140 | |
| 141 | def insertEvent( self, typeIndex, scheduleMethod, args=None ): |
| 142 | """ |
| 143 | This function inserts an event into the scheduler |
| 144 | """ |
| 145 | if typeIndex > 100: |
| 146 | # Handle group events |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 147 | if typeIndex not in main.enabledEvents.keys(): |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 148 | main.log.warn( "Event Generator - event type %s not enabled" % ( typeIndex ) ) |
| 149 | return |
| 150 | function = getattr( self, main.enabledEvents[ typeIndex ] ) |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 151 | assert function is not None, "Event Generator - funtion for group event " + typeIndex + " not found" |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 152 | function( scheduleMethod, args ) |
| 153 | else: |
| 154 | # Add individual events to the scheduler |
| 155 | main.eventScheduler.scheduleEvent( typeIndex, scheduleMethod, args ) |
| 156 | |
| 157 | def insertAllChecks( self, args=None ): |
| 158 | """ |
| 159 | Acquire eventGeneratorLock before calling this funtion |
| 160 | """ |
| 161 | for eventType in main.enabledEvents.keys(): |
| 162 | if eventType < 100 and EventType().map[ eventType ].startswith( 'CHECK' ): |
| 163 | main.eventScheduler.scheduleEvent( eventType, |
| 164 | EventScheduleMethod().RUN_NON_BLOCK, |
| 165 | args ) |
| 166 | |
| 167 | def addAllChecks( self, scheduleMethod, args=None ): |
| 168 | """ |
| 169 | The function adds all check events into the scheduler |
| 170 | """ |
| 171 | with self.eventGeneratorLock: |
| 172 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 173 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 174 | self.insertAllChecks( args ) |
| 175 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 176 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 177 | |
| 178 | def randomLinkToggle( self, scheduleMethod, args=[ 5 ], blocking=True ): |
| 179 | """ |
| 180 | The function randomly adds a link down-up event pair into the scheduler |
| 181 | After each individual link event, all checks are inserted into the scheduler |
| 182 | param: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 183 | args[ 0 ] is the average interval between link down and link up events |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 184 | blocking means blocking other events from being scheduled between link down and link up |
| 185 | """ |
| 186 | import random |
| 187 | import time |
| 188 | |
| 189 | if len( args ) < 1: |
| 190 | main.log.warn( "Event Generator - Not enough arguments for randomLinkToggle: %s" % ( args ) ) |
| 191 | elif len( args ) > 1: |
| 192 | main.log.warn( "Event Generator - Too many arguments for randomLinkToggle: %s" % ( args ) ) |
| 193 | else: |
| 194 | downUpAvgInterval = int( args[ 0 ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 195 | self.eventGeneratorLock.acquire() |
| 196 | main.eventScheduler.scheduleEvent( EventType().NETWORK_LINK_DOWN, |
| 197 | scheduleMethod, |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 198 | [ 'random', 'random' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 199 | sleepTime = int( main.params[ 'EVENT' ][ 'randomLinkToggle' ][ 'sleepBeforeCheck' ] ) |
| 200 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
| 201 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
| 202 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 203 | # Insert a NULL BLOCK event |
| 204 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 205 | downUpInterval = abs( random.gauss( downUpAvgInterval, 1 ) ) |
| 206 | if not blocking: |
| 207 | self.eventGeneratorLock.release() |
| 208 | time.sleep( downUpInterval ) |
| 209 | self.eventGeneratorLock.acquire() |
| 210 | else: |
| 211 | time.sleep( downUpInterval ) |
| 212 | main.eventScheduler.scheduleEvent( EventType().NETWORK_LINK_UP, |
| 213 | scheduleMethod, |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 214 | [ 'random', 'random' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 215 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
| 216 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
| 217 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 218 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 219 | self.eventGeneratorLock.release() |
| 220 | |
| 221 | def randomLinkGroupToggle( self, scheduleMethod, args=None, blocking=True ): |
| 222 | """ |
| 223 | The function randomly adds a group of link down-up events into the scheduler |
| 224 | After each link down or up, all checks are inserted into the scheduler |
| 225 | param: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 226 | args[ 0 ] is the number of links that are to be brought down |
| 227 | args[ 1 ] is the average interval between link down events |
| 228 | args[ 2 ] is the average interval between link group down and group up events |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 229 | blocking means blocking other events from being scheduled between link events |
| 230 | """ |
| 231 | import random |
| 232 | import time |
| 233 | |
| 234 | if len( args ) < 3: |
| 235 | main.log.warn( "Event Generator - Not enough arguments for randomLinkGroupToggle: %s" % ( args ) ) |
| 236 | elif len( args ) > 3: |
| 237 | main.log.warn( "Event Generator - Too many arguments for randomLinkGroupToggle: %s" % ( args ) ) |
| 238 | else: |
| 239 | linkGroupSize = int( args[ 0 ] ) |
| 240 | downDownAvgInterval = int( args[ 1 ] ) |
| 241 | downUpAvgInterval = int( args[ 2 ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 242 | for i in range( 0, linkGroupSize ): |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 243 | if i == 0: |
| 244 | self.eventGeneratorLock.acquire() |
| 245 | main.eventScheduler.scheduleEvent( EventType().NETWORK_LINK_DOWN, |
| 246 | scheduleMethod, |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 247 | [ 'random', 'random' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 248 | sleepTime = int( main.params[ 'EVENT' ][ 'randomLinkGroupToggle' ][ 'sleepBeforeCheck' ] ) |
| 249 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
| 250 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
| 251 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 252 | # Insert a NULL BLOCK event |
| 253 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 254 | downDownInterval = abs( random.gauss( downDownAvgInterval, 1 ) ) |
| 255 | if not blocking: |
| 256 | self.eventGeneratorLock.release() |
| 257 | time.sleep( downDownInterval ) |
| 258 | self.eventGeneratorLock.acquire() |
| 259 | else: |
| 260 | time.sleep( downDownInterval ) |
| 261 | |
| 262 | downUpInterval = abs( random.gauss( downUpAvgInterval, 1 ) ) |
| 263 | if not blocking: |
| 264 | self.eventGeneratorLock.release() |
| 265 | time.sleep( downUpInterval ) |
| 266 | self.eventGeneratorLock.acquire() |
| 267 | else: |
| 268 | time.sleep( downUpInterval ) |
| 269 | |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 270 | for i in range( 0, linkGroupSize ): |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 271 | main.eventScheduler.scheduleEvent( EventType().NETWORK_LINK_UP, |
| 272 | scheduleMethod, |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 273 | [ 'random', 'random' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 274 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
| 275 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
| 276 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 277 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 278 | upUpInterval = abs( random.gauss( downDownAvgInterval, 1 ) ) |
| 279 | if not blocking: |
| 280 | self.eventGeneratorLock.release() |
| 281 | time.sleep( upUpInterval ) |
| 282 | self.eventGeneratorLock.acquire() |
| 283 | else: |
| 284 | time.sleep( upUpInterval ) |
| 285 | self.eventGeneratorLock.release() |
| 286 | |
| 287 | def randomDeviceToggle( self, scheduleMethod, args=[ 5 ], blocking=True ): |
| 288 | """ |
| 289 | The function randomly removes a device and then adds it back |
| 290 | After each individual device event, all checks are inserted into the scheduler |
| 291 | param: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 292 | args[ 0 ] is the average interval between device down and device up events |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 293 | blocking means blocking other events from being scheduled between device down and device up |
| 294 | """ |
| 295 | import random |
| 296 | import time |
| 297 | |
| 298 | if len( args ) < 1: |
| 299 | main.log.warn( "Event Generator - Not enough arguments for randomDeviceToggle: %s" % ( args ) ) |
| 300 | elif len( args ) > 1: |
| 301 | main.log.warn( "Event Generator - Too many arguments for randomDeviceToggle: %s" % ( args ) ) |
| 302 | else: |
| 303 | downUpAvgInterval = int( args[ 0 ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 304 | self.eventGeneratorLock.acquire() |
| 305 | main.eventScheduler.scheduleEvent( EventType().NETWORK_DEVICE_DOWN, |
| 306 | scheduleMethod, |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 307 | [ 'random' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 308 | sleepTime = int( main.params[ 'EVENT' ][ 'randomLinkToggle' ][ 'sleepBeforeCheck' ] ) |
| 309 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
| 310 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
| 311 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 312 | # Insert a NULL BLOCK event |
| 313 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 314 | downUpInterval = abs( random.gauss( downUpAvgInterval, 1 ) ) |
| 315 | if not blocking: |
| 316 | self.eventGeneratorLock.release() |
| 317 | time.sleep( downUpInterval ) |
| 318 | self.eventGeneratorLock.acquire() |
| 319 | else: |
| 320 | time.sleep( downUpInterval ) |
| 321 | main.eventScheduler.scheduleEvent( EventType().NETWORK_DEVICE_UP, |
| 322 | scheduleMethod, |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 323 | [ 'random' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 324 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
| 325 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
| 326 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 327 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 328 | self.eventGeneratorLock.release() |
| 329 | |
| 330 | def randomDeviceGroupToggle( self, scheduleMethod, args=None, blocking=True ): |
| 331 | """ |
| 332 | The function randomly adds a group of device down-up events into the scheduler |
| 333 | After each device down or up, all checks are inserted into the scheduler |
| 334 | param: |
Jon Hall | 2bb3e21 | 2017-05-24 17:07:25 -0700 | [diff] [blame] | 335 | args[ 0 ] is the number of devices that are to be brought down |
| 336 | args[ 1 ] is the average interval between device down events |
| 337 | args[ 2 ] is the average interval between device group down and group up events |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 338 | blocking means blocking other events from being scheduled between device events |
| 339 | """ |
| 340 | import random |
| 341 | import time |
| 342 | |
| 343 | if len( args ) < 3: |
| 344 | main.log.warn( "Event Generator - Not enough arguments for randomDeviceGroupToggle: %s" % ( args ) ) |
| 345 | elif len( args ) > 3: |
| 346 | main.log.warn( "Event Generator - Too many arguments for randomDeviceGroupToggle: %s" % ( args ) ) |
| 347 | else: |
| 348 | deviceGroupSize = int( args[ 0 ] ) |
| 349 | downDownAvgInterval = int( args[ 1 ] ) |
| 350 | downUpAvgInterval = int( args[ 2 ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 351 | for i in range( 0, deviceGroupSize ): |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 352 | if i == 0: |
| 353 | self.eventGeneratorLock.acquire() |
| 354 | main.eventScheduler.scheduleEvent( EventType().NETWORK_DEVICE_DOWN, |
| 355 | scheduleMethod, |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 356 | [ 'random' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 357 | sleepTime = int( main.params[ 'EVENT' ][ 'randomLinkGroupToggle' ][ 'sleepBeforeCheck' ] ) |
| 358 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
| 359 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
| 360 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 361 | # Insert a NULL BLOCK event |
| 362 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 363 | downDownInterval = abs( random.gauss( downDownAvgInterval, 1 ) ) |
| 364 | if not blocking: |
| 365 | self.eventGeneratorLock.release() |
| 366 | time.sleep( downDownInterval ) |
| 367 | self.eventGeneratorLock.acquire() |
| 368 | else: |
| 369 | time.sleep( downDownInterval ) |
| 370 | |
| 371 | downUpInterval = abs( random.gauss( downUpAvgInterval, 1 ) ) |
| 372 | if not blocking: |
| 373 | self.eventGeneratorLock.release() |
| 374 | time.sleep( downUpInterval ) |
| 375 | self.eventGeneratorLock.acquire() |
| 376 | else: |
| 377 | time.sleep( downUpInterval ) |
| 378 | |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 379 | for i in range( 0, deviceGroupSize ): |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 380 | main.eventScheduler.scheduleEvent( EventType().NETWORK_DEVICE_UP, |
| 381 | scheduleMethod, |
You Wang | 221db32 | 2016-06-03 15:45:52 -0700 | [diff] [blame] | 382 | [ 'random' ] ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 383 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
| 384 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
| 385 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 386 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 387 | upUpInterval = abs( random.gauss( downDownAvgInterval, 1 ) ) |
| 388 | if not blocking: |
| 389 | self.eventGeneratorLock.release() |
| 390 | time.sleep( upUpInterval ) |
| 391 | self.eventGeneratorLock.acquire() |
| 392 | else: |
| 393 | time.sleep( upUpInterval ) |
| 394 | self.eventGeneratorLock.release() |
| 395 | |
| 396 | def installAllHostIntents( self, scheduleMethod, args=None ): |
| 397 | """ |
| 398 | This function installs host intents for all host pairs |
| 399 | After all intent events are inserted, this funciton also insert intent and traffic checks |
| 400 | """ |
| 401 | import itertools |
| 402 | |
| 403 | with self.eventGeneratorLock: |
| 404 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 405 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 406 | availableControllers = [] |
| 407 | for controller in main.controllers: |
| 408 | if controller.isUp(): |
| 409 | availableControllers.append( controller.index ) |
| 410 | if len( availableControllers ) == 0: |
| 411 | main.log.warn( "Event Generator - No available controllers" ) |
| 412 | return |
| 413 | hostCombos = list( itertools.combinations( main.hosts, 2 ) ) |
| 414 | for i in xrange( 0, len( hostCombos ), len( availableControllers ) ): |
| 415 | for CLIIndex in availableControllers: |
| 416 | if i >= len( hostCombos ): |
| 417 | break |
| 418 | main.eventScheduler.scheduleEvent( EventType().APP_INTENT_HOST_ADD, |
| 419 | EventScheduleMethod().RUN_NON_BLOCK, |
| 420 | [ hostCombos[ i ][ 0 ].name, hostCombos[ i ][ 1 ].name, CLIIndex ] ) |
| 421 | i += 1 |
| 422 | # Pending checks after installing all intents |
| 423 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 424 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 425 | sleepTime = int( main.params[ 'EVENT' ][ 'installAllHostIntents' ][ 'sleepBeforeCheck' ] ) |
| 426 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
You Wang | 55503a3 | 2016-06-27 15:11:40 -0700 | [diff] [blame] | 427 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 428 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 429 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 430 | |
| 431 | def removeAllHostIntents( self, scheduleMethod, args=None ): |
| 432 | """ |
| 433 | This function removes host intents for all host pairs |
| 434 | After all intent events are inserted, this funciton also insert intent and traffic checks |
| 435 | """ |
| 436 | import itertools |
| 437 | |
| 438 | with self.eventGeneratorLock: |
| 439 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 440 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 441 | availableControllers = [] |
| 442 | for controller in main.controllers: |
| 443 | if controller.isUp(): |
| 444 | availableControllers.append( controller.index ) |
| 445 | if len( availableControllers ) == 0: |
| 446 | main.log.warn( "Event Generator - No available controllers" ) |
| 447 | return |
| 448 | hostCombos = list( itertools.combinations( main.hosts, 2 ) ) |
| 449 | for i in xrange( 0, len( hostCombos ), len( availableControllers ) ): |
| 450 | for CLIIndex in availableControllers: |
| 451 | if i >= len( hostCombos ): |
| 452 | break |
| 453 | main.eventScheduler.scheduleEvent( EventType().APP_INTENT_HOST_DEL, |
| 454 | EventScheduleMethod().RUN_NON_BLOCK, |
| 455 | [ hostCombos[ i ][ 0 ].name, hostCombos[ i ][ 1 ].name, CLIIndex ] ) |
| 456 | i += 1 |
| 457 | # Pending checks after removing all intents |
| 458 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 459 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 460 | sleepTime = int( main.params[ 'EVENT' ][ 'removeAllHostIntents' ][ 'sleepBeforeCheck' ] ) |
| 461 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
You Wang | 55503a3 | 2016-06-27 15:11:40 -0700 | [diff] [blame] | 462 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 463 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 464 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 465 | |
| 466 | def installAllPointIntents( self, scheduleMethod, args=None ): |
| 467 | """ |
| 468 | This function installs point intents for all device pairs |
| 469 | After all intent events are inserted, this funciton also insert intent and traffic checks |
| 470 | """ |
| 471 | import itertools |
| 472 | |
| 473 | with self.eventGeneratorLock: |
| 474 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 475 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 476 | availableControllers = [] |
| 477 | for controller in main.controllers: |
| 478 | if controller.isUp(): |
| 479 | availableControllers.append( controller.index ) |
| 480 | if len( availableControllers ) == 0: |
| 481 | main.log.warn( "Event Generator - No available controllers" ) |
| 482 | return |
| 483 | deviceCombos = list( itertools.permutations( main.devices, 2 ) ) |
| 484 | for i in xrange( 0, len( deviceCombos ), len( availableControllers ) ): |
| 485 | for CLIIndex in availableControllers: |
| 486 | if i >= len( deviceCombos ): |
| 487 | break |
| 488 | main.eventScheduler.scheduleEvent( EventType().APP_INTENT_POINT_ADD, |
| 489 | EventScheduleMethod().RUN_NON_BLOCK, |
| 490 | [ deviceCombos[ i ][ 0 ].name, deviceCombos[ i ][ 1 ].name, CLIIndex ] ) |
| 491 | i += 1 |
| 492 | # Pending checks after installing all intents |
| 493 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 494 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 495 | sleepTime = int( main.params[ 'EVENT' ][ 'installAllPointIntents' ][ 'sleepBeforeCheck' ] ) |
| 496 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
You Wang | 55503a3 | 2016-06-27 15:11:40 -0700 | [diff] [blame] | 497 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 498 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 499 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 500 | |
| 501 | def removeAllPointIntents( self, scheduleMethod, args=None ): |
| 502 | """ |
| 503 | This function removes point intents for all device pairs |
| 504 | After all intent events are inserted, this funciton also insert intent and traffic checks |
| 505 | """ |
| 506 | import itertools |
| 507 | |
| 508 | with self.eventGeneratorLock: |
| 509 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 510 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 511 | availableControllers = [] |
| 512 | for controller in main.controllers: |
| 513 | if controller.isUp(): |
| 514 | availableControllers.append( controller.index ) |
| 515 | if len( availableControllers ) == 0: |
| 516 | main.log.warn( "Event Generator - No available controllers" ) |
| 517 | return |
| 518 | deviceCombos = list( itertools.permutations( main.devices, 2 ) ) |
| 519 | for i in xrange( 0, len( deviceCombos ), len( availableControllers ) ): |
| 520 | for CLIIndex in availableControllers: |
| 521 | if i >= len( deviceCombos ): |
| 522 | break |
| 523 | main.eventScheduler.scheduleEvent( EventType().APP_INTENT_POINT_DEL, |
| 524 | EventScheduleMethod().RUN_NON_BLOCK, |
| 525 | [ deviceCombos[ i ][ 0 ].name, deviceCombos[ i ][ 1 ].name, CLIIndex ] ) |
| 526 | i += 1 |
| 527 | # Pending checks after removing all intents |
| 528 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 529 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |
| 530 | sleepTime = int( main.params[ 'EVENT' ][ 'removeAllPointIntents' ][ 'sleepBeforeCheck' ] ) |
| 531 | main.eventScheduler.scheduleEvent( EventType().TEST_SLEEP, EventScheduleMethod().RUN_BLOCK, [ sleepTime ] ) |
You Wang | 55503a3 | 2016-06-27 15:11:40 -0700 | [diff] [blame] | 532 | self.insertAllChecks( EventScheduleMethod().RUN_NON_BLOCK ) |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 533 | if scheduleMethod == EventScheduleMethod().RUN_BLOCK: |
| 534 | main.eventScheduler.scheduleEvent( EventType().NULL, EventScheduleMethod().RUN_BLOCK ) |