blob: 76722f0534ce043b8608ed6652960ef79ec29ded [file] [log] [blame]
You Wangdb927a52016-02-26 11:03:28 -08001"""
2This file contains classes for CHOTestMonkey that are related to check event
3Author: you@onlab.us
4"""
5from tests.CHOTestMonkey.dependencies.events.Event import EventType, EventStates, Event
6
7class CheckEvent( Event ):
8 def __init__( self ):
9 Event.__init__( self )
10
11 def startCheckEvent( self ):
12 return EventStates().PASS
13
14 def startEvent( self, args ):
15 with self.eventLock:
16 main.log.info( "%s - starting event" % ( self.typeString ) )
17 result = self.startCheckEvent()
18 return result
19
20class IntentCheck( CheckEvent ):
21 def __init__( self ):
22 CheckEvent.__init__( self )
23 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
24 self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
25
26 def startCheckEvent( self, args=None ):
27 checkResult = EventStates().PASS
You Wang58aa11e2016-05-17 10:35:44 -070028 intentDict = {}
You Wangdb927a52016-02-26 11:03:28 -080029 for intent in main.intents:
You Wang58aa11e2016-05-17 10:35:44 -070030 intentDict[ intent.id ] = intent.expectedState
You Wangdb927a52016-02-26 11:03:28 -080031 for controller in main.controllers:
32 if controller.isUp():
33 with controller.CLILock:
You Wang58aa11e2016-05-17 10:35:44 -070034 intentState = controller.CLI.compareIntent( intentDict )
You Wangdb927a52016-02-26 11:03:28 -080035 if not intentState:
You Wang58aa11e2016-05-17 10:35:44 -070036 main.log.warn( "Intent Check - not all intent ids and states match that on ONOS%s" % ( controller.index ) )
You Wangdb927a52016-02-26 11:03:28 -080037 checkResult = EventStates().FAIL
You Wang58aa11e2016-05-17 10:35:44 -070038 return checkResult
39
40class FlowCheck( CheckEvent ):
41 def __init__( self ):
42 CheckEvent.__init__( self )
43 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
44 self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
45
46 def startCheckEvent( self, args=None ):
47 import json
48 checkResult = EventStates().PASS
49 for controller in main.controllers:
50 if controller.isUp():
51 with controller.CLILock:
52 flows = controller.CLI.flows()
53 try:
54 flows = json.loads( flows )
55 except ( TypeError, ValueError ):
56 main.log.exception( "Flow Check - Object not as expected: {!r}".format( flows ) )
57 return EventStates().FAIL
58 # Compare flow IDs in ONOS and Mininet
59 flowIDList = []
60 for item in flows:
61 for flow in item[ "flows" ]:
62 flowIDList.append( hex( int( flow[ 'id' ] ) ) )
63 main.log.info( "Flow Check - current flow number on ONOS%s: %s" % ( controller.index, len( flowIDList ) ) )
64 switchList = []
65 for device in main.devices:
66 switchList.append( device.name )
67 with main.mininetLock:
68 flowCompareResult = main.Mininet1.checkFlowId( switchList, flowIDList, debug=False )
69 if not flowCompareResult:
70 main.log.warn( "Flow Check - flows on ONOS%s do not match that in Mininet" % ( controller.index ) )
71 checkResult = EventStates().FAIL
72 # Check flow state
73 flowState = controller.CLI.checkFlowsState( isPENDING=False )
74 if not flowState:
75 main.log.warn( "Flow Check - not all flows are in ADDED state on ONOS%s" % ( controller.index ) )
76 checkResult = EventStates().FAIL
You Wangdb927a52016-02-26 11:03:28 -080077 return checkResult
78
79class TopoCheck( CheckEvent ):
80 def __init__( self ):
81 CheckEvent.__init__( self )
82 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
83 self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
84
85 def startCheckEvent( self, args=None ):
86 import json
87 checkResult = EventStates().PASS
88 upLinkNum = 0
89 upDeviceNum = 0
90 upHostNum = 0
91 with main.variableLock:
92 for link in main.links:
93 if not link.isDown() and not link.isRemoved():
94 upLinkNum += 1
95 for device in main.devices:
96 if not device.isDown() and not device.isRemoved():
97 upDeviceNum += 1
98 for host in main.hosts:
99 if not host.isDown() and not host.isRemoved():
100 upHostNum += 1
101 clusterNum = 1
102 for controller in main.controllers:
103 if controller.isUp():
104 with controller.CLILock:
Flavio Castro82ee2f62016-06-07 15:04:12 -0700105 topoState = controller.CLI.checkStatus( upDeviceNum, upLinkNum )
You Wangdb927a52016-02-26 11:03:28 -0800106 #if not topoState:
107 # main.log.warn( "Topo Check - link or device number discoverd by ONOS%s is incorrect" % ( controller.index ) )
108 # checkResult = EventStates().FAIL
109 # Check links
You Wang58aa11e2016-05-17 10:35:44 -0700110 try:
111 links = controller.CLI.links()
112 links = json.loads( links )
113 if not len( links ) == upLinkNum:
114 checkResult = EventStates().FAIL
115 main.log.warn( "Topo Check - link number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, upLinkNum, len( links ) ) )
116 # Check devices
117 devices = controller.CLI.devices()
118 devices = json.loads( devices )
119 availableDeviceNum = 0
120 for device in devices:
121 if device[ 'available' ] == True:
122 availableDeviceNum += 1
123 if not availableDeviceNum == upDeviceNum:
124 checkResult = EventStates().FAIL
125 main.log.warn( "Topo Check - device number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, upDeviceNum, availableDeviceNum ) )
126 # Check hosts
127 hosts = controller.CLI.hosts()
128 hosts = json.loads( hosts )
129 if not len( hosts ) == upHostNum:
130 checkResult = EventStates().FAIL
131 main.log.warn( "Topo Check - host number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, upHostNum, len( hosts ) ) )
132 # Check clusters
133 clusters = controller.CLI.clusters()
134 clusters = json.loads( clusters )
135 if not len( clusters ) == clusterNum:
136 checkResult = EventStates().FAIL
137 main.log.warn( "Topo Check - cluster number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, clusterNum, len( clusters ) ) )
138 except ( TypeError, ValueError ):
139 main.log.exception( "Flow Check - Object not as expected" )
140 return EventStates().FAIL
You Wangdb927a52016-02-26 11:03:28 -0800141 return checkResult
142
143class ONOSCheck( CheckEvent ):
144 def __init__( self ):
145 CheckEvent.__init__( self )
146 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
147 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
148
149 def startCheckEvent( self, args=None ):
150 import json
151 checkResult = EventStates().PASS
152 topics = []
153 # TODO: Other topics?
154 for i in range( 14 ):
155 topics.append( "intent-partition-" + str( i ) )
156 dpidToAvailability = {}
157 dpidToMaster = {}
158 for device in main.devices:
159 if device.isDown() or device.isRemoved():
160 dpidToAvailability[ device.dpid ] = False
161 else:
162 dpidToAvailability[ device.dpid ] = True
163 dpidToMaster[ device.dpid ] = 'unknown'
164 # Check mastership, leaders and node states on each controller node
165 for controller in main.controllers:
166 if controller.isUp():
167 # Check mastership
You Wang58aa11e2016-05-17 10:35:44 -0700168 try:
169 with controller.CLILock:
170 roles = controller.CLI.roles()
171 roles = json.loads( roles )
172 for device in roles:
173 dpid = device[ 'id' ]
174 if dpidToMaster[ dpid ] == 'unknown':
175 dpidToMaster[ dpid ] = device[ 'master' ]
176 elif dpidToMaster[ dpid ] != device[ 'master' ]:
177 checkResult = EventStates().FAIL
178 main.log.warn( "ONOS Check - Mastership of %s on ONOS%s is inconsistent with that on ONOS1" % ( dpid, controller.index ) )
179 if dpidToAvailability[ dpid ] and device[ 'master' ] == "none":
180 checkResult = EventStates().FAIL
181 main.log.warn( "ONOS Check - Device %s has no master on ONOS%s" % ( dpid, controller.index ) )
182 # Check leaders
183 with controller.CLILock:
184 leaders = controller.CLI.leaders()
185 leaders = json.loads( leaders )
186 ONOSTopics = [ j['topic'] for j in leaders ]
187 for topic in topics:
188 if topic not in ONOSTopics:
189 checkResult = EventStates().FAIL
190 main.log.warn( "ONOS Check - Topic %s not in leaders on ONOS%s" % ( topic, controller.index ) )
191 # Check node state
192 with controller.CLILock:
193 nodes = controller.CLI.nodes()
194 nodes = json.loads( nodes )
195 ipToState = {}
196 for node in nodes:
197 ipToState[ node[ 'ip' ] ] = node[ 'state' ]
198 for c in main.controllers:
199 if c.isUp() and ipToState[ c.ip ] == 'READY':
200 pass
201 elif not c.isUp() and ipToState[ c.ip ] == 'INACTIVE':
202 pass
203 else:
204 checkResult = EventStates().FAIL
205 main.log.warn( "ONOS Check - ONOS%s shows wrong node state: ONOS%s is %s but state is %s" % ( controller.index, c.index, c.status, ipToState[ c.ip ] ) )
206 # TODO: check partitions?
207 except ( TypeError, ValueError ):
208 main.log.exception( "ONOS Check - Object not as expected" )
209 return EventStates().FAIL
You Wangdb927a52016-02-26 11:03:28 -0800210 return checkResult
211
212class TrafficCheck( CheckEvent ):
213 def __init__( self ):
214 CheckEvent.__init__( self )
215 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
216 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
217
218 def startCheckEvent( self, args=None ):
219 checkResult = EventStates().PASS
220 pool = []
221 wait = int( main.params[ 'EVENT' ][ 'TrafficCheck' ][ 'pingWait' ] )
222 timeout = int( main.params[ 'EVENT' ][ 'TrafficCheck' ][ 'pingTimeout' ] )
223 dstIPv4List = {}
224 dstIPv6List = {}
225 upHosts = []
226 for host in main.hosts:
227 if host.isUp():
228 upHosts.append( host )
229 for host in upHosts:
230 dstIPv4List[ host.index ] = []
231 dstIPv6List[ host.index ] = []
232 for correspondent in host.correspondents:
233 if not correspondent in upHosts:
234 continue
235 for ipAddress in correspondent.ipAddresses:
236 if ipAddress.startswith( str( main.params[ 'TEST' ][ 'ipv6Prefix' ] ) ):
237 dstIPv6List[ host.index ].append( ipAddress )
238 elif ipAddress.startswith( str( main.params[ 'TEST' ][ 'ipv4Prefix' ] ) ):
239 dstIPv4List[ host.index ].append( ipAddress )
240 thread = main.Thread( target=host.handle.pingHostSetAlternative,
241 threadID=main.threadID,
242 name="pingHostSetAlternative",
243 args=[ dstIPv4List[ host.index ], 1 ] )
244 pool.append( thread )
245 thread.start()
246 with main.variableLock:
247 main.threadID += 1
248 for thread in pool:
249 thread.join( 10 )
250 if not thread.result:
251 checkResult = EventStates().FAIL
252 main.log.warn( "Traffic Check - ping failed" )
253
254 if not main.enableIPv6:
255 return checkResult
256 # Check ipv6 ping
257 for host in upHosts:
258 thread = main.Thread( target=host.handle.pingHostSetAlternative,
259 threadID=main.threadID,
260 name="pingHostSetAlternative",
261 args=[ dstIPv6List[ host.index ], 1, True ] )
262 pool.append( thread )
263 thread.start()
264 with main.variableLock:
265 main.threadID += 1
266 for thread in pool:
267 thread.join( 10 )
268 if not thread.result:
269 checkResult = EventStates().FAIL
270 main.log.warn( "Traffic Check - ping6 failed" )
271 return checkResult
272