blob: 480fb9a2df960cc40edd105650c7961c7634e59b [file] [log] [blame]
You Wangdb927a52016-02-26 11:03:28 -08001"""
2This file contains the event scheduler 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.events.TestEvent import *
8from tests.CHOTestMonkey.dependencies.events.CheckEvent import *
9from tests.CHOTestMonkey.dependencies.events.NetworkEvent import *
10from tests.CHOTestMonkey.dependencies.events.AppEvent import *
11from tests.CHOTestMonkey.dependencies.events.ONOSEvent import *
12
Jon Hall2bb3e212017-05-24 17:07:25 -070013
You Wangdb927a52016-02-26 11:03:28 -080014class EventScheduleMethod:
Jon Hall2bb3e212017-05-24 17:07:25 -070015
You Wangdb927a52016-02-26 11:03:28 -080016 def __init__( self ):
17 self.map = {}
18 self.RUN_NON_BLOCK = 1
19 self.map[ 1 ] = 'RUN_NON_BLOCK'
20 self.RUN_BLOCK = -1
21 self.map[ -1 ] = 'RUN_BLOCK'
22
Jon Hall2bb3e212017-05-24 17:07:25 -070023
You Wangdb927a52016-02-26 11:03:28 -080024class EventTuple:
Jon Hall2bb3e212017-05-24 17:07:25 -070025
You Wangdb927a52016-02-26 11:03:28 -080026 def __init__( self, id, className, typeString, typeIndex, scheduleMethod, args, rerunInterval, maxRerunNum ):
27 self.default = ''
28 self.id = 0
29 self.className = className
30 self.typeString = typeString
31 self.typeIndex = typeIndex
32 self.scheduleMethod = scheduleMethod
33 self.args = args
34 self.rerunInterval = rerunInterval
35 self.maxRerunNum = maxRerunNum
36
37 def startEvent( self ):
38 assert self.className in globals().keys()
39 event = globals()[ self.className ]
40 return event().startEvent( self.args )
41
Jon Hall2bb3e212017-05-24 17:07:25 -070042
You Wangdb927a52016-02-26 11:03:28 -080043class EventScheduler:
Jon Hall2bb3e212017-05-24 17:07:25 -070044
You Wangdb927a52016-02-26 11:03:28 -080045 def __init__( self ):
46 self.default = ''
47 self.pendingEvents = []
48 self.pendingEventsCondition = Condition()
49 self.runningEvents = []
50 self.runningEventsCondition = Condition()
51 self.isRunning = True
52 self.idleCondition = Condition()
53 self.pendingEventsCapacity = int( main.params[ 'SCHEDULER' ][ 'pendingEventsCapacity' ] )
54 self.runningEventsCapacity = int( main.params[ 'SCHEDULER' ][ 'runningEventsCapacity' ] )
55 self.scheduleLoopSleep = float( main.params[ 'SCHEDULER' ][ 'scheduleLoopSleep' ] )
56
57 def scheduleEvent( self, typeIndex, scheduleMethod, args=None, index=-1 ):
58 """
59 Insert an event to pendingEvents
60 param:
61 index: the position to insert into pendingEvents, default value -1 implies the tail of pendingEvents
62 """
63 if not typeIndex in main.enabledEvents.keys():
64 main.log.warn( "Event Scheduler - event type %s not enabled" % ( typeIndex ) )
65 return
66 if main.enabledEvents[ typeIndex ] in main.params[ 'EVENT' ].keys():
67 if 'rerunInterval' in main.params[ 'EVENT' ][ main.enabledEvents[ typeIndex ] ].keys():
68 rerunInterval = int( main.params[ 'EVENT' ][ main.enabledEvents[ typeIndex ] ][ 'rerunInterval' ] )
69 maxRerunNum = int( main.params[ 'EVENT' ][ main.enabledEvents[ typeIndex ] ][ 'maxRerunNum' ] )
70 else:
71 rerunInterval = int( main.params[ 'EVENT' ][ 'Event' ][ 'rerunInterval' ] )
72 maxRerunNum = int( main.params[ 'EVENT' ][ 'Event' ][ 'maxRerunNum' ] )
73 eventTuple = EventTuple( main.eventID, main.enabledEvents[ typeIndex ], EventType().map[ typeIndex ], typeIndex, scheduleMethod, args, rerunInterval, maxRerunNum )
74 with main.variableLock:
75 main.eventID += 1
76 main.log.debug( "Event Scheduler - Event added: %s, %s, %s" % ( typeIndex,
Jon Hall2bb3e212017-05-24 17:07:25 -070077 scheduleMethod,
78 args ) )
You Wangdb927a52016-02-26 11:03:28 -080079 with self.pendingEventsCondition:
80 if index == -1:
81 self.pendingEvents.append( eventTuple )
82 elif index > -1 and index <= len( self.pendingEvents ):
83 self.pendingEvents.insert( index, eventTuple )
84 else:
85 main.log.warn( "Event Scheduler - invalid index when isnerting event: %s" % ( index ) )
86 self.pendingEventsCondition.notify()
You Wang3ce74fd2016-07-14 09:34:30 -070087 #self.printEvents()
You Wangdb927a52016-02-26 11:03:28 -080088
89 def startScheduler( self ):
90 """
91 Start the loop which schedules the events in pendingEvents
92 """
93 import time
94
Jon Hall2bb3e212017-05-24 17:07:25 -070095 while True:
You Wangdb927a52016-02-26 11:03:28 -080096 with self.pendingEventsCondition:
97 while len( self.pendingEvents ) == 0:
98 self.pendingEventsCondition.wait()
99 eventTuple = self.pendingEvents[ 0 ]
100 main.log.debug( "Event Scheduler - Scheduling event: %s, %s, %s" % ( eventTuple.typeIndex,
Jon Hall2bb3e212017-05-24 17:07:25 -0700101 eventTuple.scheduleMethod,
102 eventTuple.args ) )
You Wangdb927a52016-02-26 11:03:28 -0800103 if eventTuple.scheduleMethod == EventScheduleMethod().RUN_NON_BLOCK:
104 # Run NON_BLOCK events using threads
105 with self.pendingEventsCondition:
106 self.pendingEvents.remove( eventTuple )
107 t = main.Thread( target=self.startEvent,
108 threadID=main.threadID,
109 name="startEvent",
Jon Hall2bb3e212017-05-24 17:07:25 -0700110 args=[ eventTuple ] )
You Wangdb927a52016-02-26 11:03:28 -0800111 t.start()
112 with main.variableLock:
113 main.threadID += 1
114 elif eventTuple.scheduleMethod == EventScheduleMethod().RUN_BLOCK:
115 # Wait for all other events before start
116 with self.runningEventsCondition:
117 while not len( self.runningEvents ) == 0:
118 self.runningEventsCondition.wait()
119 # BLOCK events will temporarily block the following events until finish running
120 with self.pendingEventsCondition:
121 self.pendingEvents.remove( eventTuple )
122 self.startEvent( eventTuple )
123 else:
124 with self.pendingEventsCondition:
125 self.pendingEvents.remove( eventTuple )
126 time.sleep( self.scheduleLoopSleep )
127
128 def startEvent( self, eventTuple ):
129 """
130 Start a network/ONOS/application event
131 """
132 import time
133
134 with self.runningEventsCondition:
135 self.runningEvents.append( eventTuple )
You Wang3ce74fd2016-07-14 09:34:30 -0700136 #self.printEvents()
You Wangdb927a52016-02-26 11:03:28 -0800137 rerunNum = 0
138 result = eventTuple.startEvent()
139 while result == EventStates().FAIL and rerunNum < eventTuple.maxRerunNum:
140 time.sleep( eventTuple.rerunInterval )
141 rerunNum += 1
142 main.log.debug( eventTuple.typeString + ": retry number " + str( rerunNum ) )
143 result = eventTuple.startEvent()
144 if result == EventStates().FAIL:
145 main.log.error( eventTuple.typeString + " failed" )
146 main.caseResult = main.FALSE
147 if main.params[ 'TEST' ][ 'pauseTest' ] == 'on':
148 #self.isRunning = False
149 #main.log.error( "Event Scheduler - Test paused. To resume test, run \'resume-test\' command in CLI debugging mode" )
150 main.stop()
151 with self.runningEventsCondition:
152 self.runningEvents.remove( eventTuple )
153 if len( self.runningEvents ) == 0:
154 self.runningEventsCondition.notify()
155 with self.pendingEventsCondition:
156 if len( self.pendingEvents ) == 0:
157 with self.idleCondition:
158 self.idleCondition.notify()
You Wang3ce74fd2016-07-14 09:34:30 -0700159 #self.printEvents()
You Wangdb927a52016-02-26 11:03:28 -0800160
161 def printEvents( self ):
162 """
163 Print all the events in pendingEvents and runningEvents
164 """
165 events = " ["
166 with self.runningEventsCondition:
167 for index in range( 0, len( self.runningEvents ) - 1 ):
168 events += str( self.runningEvents[ index ].typeIndex )
169 events += ", "
170 if len( self.runningEvents ) > 0:
171 events += str( self.runningEvents[ -1 ].typeIndex )
172 events += "]"
173 events += " ["
174 with self.pendingEventsCondition:
175 for index in range( 0, len( self.pendingEvents ) - 1 ):
176 events += str( self.pendingEvents[ index ].typeIndex )
177 events += ", "
178 if len( self.pendingEvents ) > 0:
179 events += str( self.pendingEvents[ -1 ].typeIndex )
180 events += "]"
You Wang3ce74fd2016-07-14 09:34:30 -0700181 main.log.debug( "Event Scheduler - Events: " + events )
You Wangdb927a52016-02-26 11:03:28 -0800182
183 def isAvailable( self ):
You Wang3ce74fd2016-07-14 09:34:30 -0700184 with self.runningEventsCondition:
185 with self.pendingEventsCondition:
You Wangdb927a52016-02-26 11:03:28 -0800186 return len( self.pendingEvents ) < self.pendingEventsCapacity and\
187 len( self.runningEvents ) < self.runningEventsCapacity and\
188 self.isRunning
189
190 def isIdle( self ):
You Wang3ce74fd2016-07-14 09:34:30 -0700191 with self.runningEventsCondition:
192 with self.pendingEventsCondition:
You Wangdb927a52016-02-26 11:03:28 -0800193 return len( self.pendingEvents ) == 0 and\
194 len( self.runningEvents ) == 0 and\
195 self.isRunning
196
197 def setPendingEventsCapacity( self, capacity ):
198 self.pendingEventsCapacity = capacity
199
200 def setRunningState( self, state ):
Jon Hall2bb3e212017-05-24 17:07:25 -0700201 assert state or state == False
You Wangdb927a52016-02-26 11:03:28 -0800202 self.isRunning = state