blob: f4e2a8969616505f05bf9e26badcff546165ac3b [file] [log] [blame]
You Wangdb927a52016-02-26 11:03:28 -08001"""
2This file contains classes for CHOTestMonkey that are related to application event
3Author: you@onlab.us
4"""
5from tests.CHOTestMonkey.dependencies.events.Event import EventType, EventStates, Event
6
7class ONOSEvent( Event ):
8 def __init__( self ):
9 Event.__init__( self )
10 self.ONOSIndex = -1
11
12 def startEvent( self, args ):
13 with self.eventLock:
14 main.log.info( "%s - starting event" % ( self.typeString ) )
15 result = EventStates().PASS
16 if self.typeIndex == EventType().ONOS_ONOS_DOWN or self.typeIndex == EventType().ONOS_ONOS_UP:
17 if len( args ) < 1:
18 main.log.warn( "%s - Not enough arguments: %s" % ( self.typeString, args ) )
19 result = EventStates().ABORT
20 elif len( args ) > 1:
21 main.log.warn( "%s - Too many arguments: %s" % ( self.typeString, args ) )
22 result = EventStates().ABORT
23 else:
24 index = int( args[ 0 ] )
25 if index < 1 or index > int( main.numCtrls ):
26 main.log.warn( "%s - invalid argument: %s" % ( self.typeString, index ) )
27 result = EventStates().ABORT
28 else:
29 self.ONOSIndex = index
30 result = self.startONOSEvent()
31 return result
32
33class ONOSDown( ONOSEvent ):
34 def __init__( self ):
35 ONOSEvent.__init__( self )
36 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
37 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
38
39 def startONOSEvent( self ):
40 assert self.ONOSIndex != -1
41 with main.variableLock:
42 if not main.controllers[ self.ONOSIndex - 1 ].isUp():
43 main.log.warn( "ONOS Down - ONOS already down" )
44 return EventStates().ABORT
45 with main.ONOSbenchLock:
46 result = main.ONOSbench.onosStop( main.controllers[ self.ONOSIndex - 1 ].ip )
47 if not result:
48 main.log.warn( "%s - failed to bring down ONOS" % ( self.typeString ) )
49 return EventStates().FAIL
50 with main.variableLock:
51 main.controllers[ self.ONOSIndex - 1 ].bringDown()
52 return EventStates().PASS
53
54class ONOSUp( ONOSEvent ):
55 def __init__( self ):
56 ONOSEvent.__init__( self )
57 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
58 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
59
60 def startONOSEvent( self ):
61 assert self.ONOSIndex != -1
62 with main.variableLock:
63 if main.controllers[ self.ONOSIndex - 1 ].isUp():
64 main.log.warn( "ONOS Up - ONOS already up" )
65 return EventStates().ABORT
66 with main.ONOSbenchLock:
67 startResult = main.ONOSbench.onosStart( main.controllers[ self.ONOSIndex - 1 ].ip )
68 if not startResult:
69 main.log.warn( "%s - failed to bring up ONOS" % ( self.typeString ) )
70 return EventStates().FAIL
71 else:
72 ONOSState = main.ONOSbench.isup( main.controllers[ self.ONOSIndex - 1 ].ip )
73 if not ONOSState:
74 main.log.warn( "%s - ONOS is not up" % ( self.typeString ) )
75 return EventStates().FAIL
76 else:
77 cliResult = main.controllers[ self.ONOSIndex - 1 ].startCLI()
78 if not cliResult:
79 main.log.warn( "%s - failed to start ONOS cli" % ( self.typeString ) )
80 return EventStates().FAIL
81 else:
82 with main.variableLock:
83 main.controllers[ self.ONOSIndex - 1 ].bringUp()
84 return EventStates().PASS
85
86class CfgEvent( Event ):
87 def __init__( self ):
88 Event.__init__( self )
89 self.component = ''
90 self.propName = ''
91 self.value = ''
92
93 def startEvent( self, args ):
94 with self.eventLock:
95 main.log.info( "%s - starting event" % ( self.typeString ) )
96 result = self.startCfgEvent( args )
97 return result
98
99class SetCfg( CfgEvent ):
100 def __init__( self ):
101 CfgEvent.__init__( self )
102 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
103 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
104
105 def startCfgEvent( self, args ):
106 if len( args ) < 3:
107 main.log.warn( "%s - Not enough arguments: %s" % ( self.typeString, args ) )
108 return EventStates().ABORT
109 elif len( args ) > 3:
110 main.log.warn( "%s - Too many arguments: %s" % ( self.typeString, args ) )
111 return EventStates().ABORT
112 else:
113 self.component = str( args[ 0 ] )
114 self.propName = str( args[ 1 ] )
115 self.value = str( args[ 2 ] )
116 assert self.component != '' and self.propName != '' and self.value != ''
117 index = -1
118 for controller in main.controllers:
119 if controller.isUp():
120 index = controller.index
121 if index == -1:
122 main.log.warn( "%s - No available controllers" %s ( self.typeString ) )
123 return EventStates().ABORT
124 controller = main.controllers[ index - 1 ]
125 with controller.CLILock:
126 result = controller.CLI.setCfg( component=self.component,
127 propName=self.propName,
128 value=self.value )
129 if not result:
130 main.log.warn( "%s - failed to set configuration" % ( self.typeString ) )
131 return EventStates().FAIL
132 return EventStates().PASS
133
134class SetFlowObj( CfgEvent ):
135 def __init__( self ):
136 CfgEvent.__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 startCfgEvent( self, args ):
141 if len( args ) < 1:
142 main.log.warn( "%s - Not enough arguments: %s" % ( self.typeString, args ) )
143 return EventStates().ABORT
144 elif len( args ) > 1:
145 main.log.warn( "%s - Too many arguments: %s" % ( self.typeString, args ) )
146 return EventStates().ABORT
147 elif args[ 0 ] != 'true' and args[ 0 ] != 'false':
148 main.log.warn( "%s - Invalid arguments: %s" % ( self.typeString, args ) )
149 return EventStates().ABORT
150 else:
151 self.component = 'org.onosproject.net.intent.impl.compiler.IntentConfigurableRegistrator'
152 self.propName = 'useFlowObjectives'
153 self.value = str( args[ 0 ] )
154 index = -1
155 for controller in main.controllers:
156 if controller.isUp():
157 index = controller.index
158 if index == -1:
159 main.log.warn( "%s - No available controllers" %s ( self.typeString ) )
160 return EventStates().ABORT
161 controller = main.controllers[ index - 1 ]
162 with controller.CLILock:
163 result = controller.CLI.setCfg( component=self.component,
164 propName=self.propName,
165 value=self.value )
166 if not result:
167 main.log.warn( "%s - failed to set configuration" % ( self.typeString ) )
168 return EventStates().FAIL
169 return EventStates().PASS
170
171class BalanceMasters( Event ):
172 def __init__( self ):
173 Event.__init__( self )
174 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
175 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
176
177 def startEvent( self, args=None ):
178 with self.eventLock:
179 main.log.info( "%s - starting event" % ( self.typeString ) )
180 index = -1
181 for controller in main.controllers:
182 if controller.isUp():
183 index = controller.index
184 if index == -1:
185 main.log.warn( "%s - No available controllers" %s ( self.typeString ) )
186 return EventStates().ABORT
187 controller = main.controllers[ index - 1 ]
188 with controller.CLILock:
189 result = controller.CLI.balanceMasters()
190 if not result:
191 main.log.warn( "%s - failed to balance masters" % ( self.typeString ) )
192 return EventStates().FAIL
193 return EventStates().PASS
194