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