blob: 99ead73f39633359f1bb4635563421156a1ec608 [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
13class EventScheduleMethod:
14 def __init__( self ):
15 self.map = {}
16 self.RUN_NON_BLOCK = 1
17 self.map[ 1 ] = 'RUN_NON_BLOCK'
18 self.RUN_BLOCK = -1
19 self.map[ -1 ] = 'RUN_BLOCK'
20
21class EventTuple:
22 def __init__( self, id, className, typeString, typeIndex, scheduleMethod, args, rerunInterval, maxRerunNum ):
23 self.default = ''
24 self.id = 0
25 self.className = className
26 self.typeString = typeString
27 self.typeIndex = typeIndex
28 self.scheduleMethod = scheduleMethod
29 self.args = args
30 self.rerunInterval = rerunInterval
31 self.maxRerunNum = maxRerunNum
32
33 def startEvent( self ):
34 assert self.className in globals().keys()
35 event = globals()[ self.className ]
36 return event().startEvent( self.args )
37
38class EventScheduler:
39 def __init__( self ):
40 self.default = ''
41 self.pendingEvents = []
42 self.pendingEventsCondition = Condition()
43 self.runningEvents = []
44 self.runningEventsCondition = Condition()
45 self.isRunning = True
46 self.idleCondition = Condition()
47 self.pendingEventsCapacity = int( main.params[ 'SCHEDULER' ][ 'pendingEventsCapacity' ] )
48 self.runningEventsCapacity = int( main.params[ 'SCHEDULER' ][ 'runningEventsCapacity' ] )
49 self.scheduleLoopSleep = float( main.params[ 'SCHEDULER' ][ 'scheduleLoopSleep' ] )
50
51 def scheduleEvent( self, typeIndex, scheduleMethod, args=None, index=-1 ):
52 """
53 Insert an event to pendingEvents
54 param:
55 index: the position to insert into pendingEvents, default value -1 implies the tail of pendingEvents
56 """
57 if not typeIndex in main.enabledEvents.keys():
58 main.log.warn( "Event Scheduler - event type %s not enabled" % ( typeIndex ) )
59 return
60 if main.enabledEvents[ typeIndex ] in main.params[ 'EVENT' ].keys():
61 if 'rerunInterval' in main.params[ 'EVENT' ][ main.enabledEvents[ typeIndex ] ].keys():
62 rerunInterval = int( main.params[ 'EVENT' ][ main.enabledEvents[ typeIndex ] ][ 'rerunInterval' ] )
63 maxRerunNum = int( main.params[ 'EVENT' ][ main.enabledEvents[ typeIndex ] ][ 'maxRerunNum' ] )
64 else:
65 rerunInterval = int( main.params[ 'EVENT' ][ 'Event' ][ 'rerunInterval' ] )
66 maxRerunNum = int( main.params[ 'EVENT' ][ 'Event' ][ 'maxRerunNum' ] )
67 eventTuple = EventTuple( main.eventID, main.enabledEvents[ typeIndex ], EventType().map[ typeIndex ], typeIndex, scheduleMethod, args, rerunInterval, maxRerunNum )
68 with main.variableLock:
69 main.eventID += 1
70 main.log.debug( "Event Scheduler - Event added: %s, %s, %s" % ( typeIndex,
71 scheduleMethod,
72 args ) )
73 with self.pendingEventsCondition:
74 if index == -1:
75 self.pendingEvents.append( eventTuple )
76 elif index > -1 and index <= len( self.pendingEvents ):
77 self.pendingEvents.insert( index, eventTuple )
78 else:
79 main.log.warn( "Event Scheduler - invalid index when isnerting event: %s" % ( index ) )
80 self.pendingEventsCondition.notify()
81 self.printEvents()
82
83 def startScheduler( self ):
84 """
85 Start the loop which schedules the events in pendingEvents
86 """
87 import time
88
89 while 1:
90 with self.pendingEventsCondition:
91 while len( self.pendingEvents ) == 0:
92 self.pendingEventsCondition.wait()
93 eventTuple = self.pendingEvents[ 0 ]
94 main.log.debug( "Event Scheduler - Scheduling event: %s, %s, %s" % ( eventTuple.typeIndex,
95 eventTuple.scheduleMethod,
96 eventTuple.args ) )
97 if eventTuple.scheduleMethod == EventScheduleMethod().RUN_NON_BLOCK:
98 # Run NON_BLOCK events using threads
99 with self.pendingEventsCondition:
100 self.pendingEvents.remove( eventTuple )
101 t = main.Thread( target=self.startEvent,
102 threadID=main.threadID,
103 name="startEvent",
104 args=[ eventTuple ])
105 t.start()
106 with main.variableLock:
107 main.threadID += 1
108 elif eventTuple.scheduleMethod == EventScheduleMethod().RUN_BLOCK:
109 # Wait for all other events before start
110 with self.runningEventsCondition:
111 while not len( self.runningEvents ) == 0:
112 self.runningEventsCondition.wait()
113 # BLOCK events will temporarily block the following events until finish running
114 with self.pendingEventsCondition:
115 self.pendingEvents.remove( eventTuple )
116 self.startEvent( eventTuple )
117 else:
118 with self.pendingEventsCondition:
119 self.pendingEvents.remove( eventTuple )
120 time.sleep( self.scheduleLoopSleep )
121
122 def startEvent( self, eventTuple ):
123 """
124 Start a network/ONOS/application event
125 """
126 import time
127
128 with self.runningEventsCondition:
129 self.runningEvents.append( eventTuple )
130 self.printEvents()
131 rerunNum = 0
132 result = eventTuple.startEvent()
133 while result == EventStates().FAIL and rerunNum < eventTuple.maxRerunNum:
134 time.sleep( eventTuple.rerunInterval )
135 rerunNum += 1
136 main.log.debug( eventTuple.typeString + ": retry number " + str( rerunNum ) )
137 result = eventTuple.startEvent()
138 if result == EventStates().FAIL:
139 main.log.error( eventTuple.typeString + " failed" )
140 main.caseResult = main.FALSE
141 if main.params[ 'TEST' ][ 'pauseTest' ] == 'on':
142 #self.isRunning = False
143 #main.log.error( "Event Scheduler - Test paused. To resume test, run \'resume-test\' command in CLI debugging mode" )
144 main.stop()
145 with self.runningEventsCondition:
146 self.runningEvents.remove( eventTuple )
147 if len( self.runningEvents ) == 0:
148 self.runningEventsCondition.notify()
149 with self.pendingEventsCondition:
150 if len( self.pendingEvents ) == 0:
151 with self.idleCondition:
152 self.idleCondition.notify()
153 self.printEvents()
154
155 def printEvents( self ):
156 """
157 Print all the events in pendingEvents and runningEvents
158 """
159 events = " ["
160 with self.runningEventsCondition:
161 for index in range( 0, len( self.runningEvents ) - 1 ):
162 events += str( self.runningEvents[ index ].typeIndex )
163 events += ", "
164 if len( self.runningEvents ) > 0:
165 events += str( self.runningEvents[ -1 ].typeIndex )
166 events += "]"
167 events += " ["
168 with self.pendingEventsCondition:
169 for index in range( 0, len( self.pendingEvents ) - 1 ):
170 events += str( self.pendingEvents[ index ].typeIndex )
171 events += ", "
172 if len( self.pendingEvents ) > 0:
173 events += str( self.pendingEvents[ -1 ].typeIndex )
174 events += "]"
You Wang221db322016-06-03 15:45:52 -0700175 #main.log.debug( "Event Scheduler - Events: " + events )
You Wangdb927a52016-02-26 11:03:28 -0800176
177 def isAvailable( self ):
178 with self.pendingEventsCondition:
179 with self.runningEventsCondition:
180 return len( self.pendingEvents ) < self.pendingEventsCapacity and\
181 len( self.runningEvents ) < self.runningEventsCapacity and\
182 self.isRunning
183
184 def isIdle( self ):
185 with self.pendingEventsCondition:
186 with self.runningEventsCondition:
187 return len( self.pendingEvents ) == 0 and\
188 len( self.runningEvents ) == 0 and\
189 self.isRunning
190
191 def setPendingEventsCapacity( self, capacity ):
192 self.pendingEventsCapacity = capacity
193
194 def setRunningState( self, state ):
195 assert state == True or state == False
196 self.isRunning = state
197