blob: d0408f9408d65f8e27f7cb9a3e02b77524a41a5f [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:
105 topologyOutput = controller.CLI.topology()
106 topoState = controller.CLI.checkStatus( topologyOutput, upDeviceNum, upLinkNum )
107 #if not topoState:
108 # main.log.warn( "Topo Check - link or device number discoverd by ONOS%s is incorrect" % ( controller.index ) )
109 # checkResult = EventStates().FAIL
110 # Check links
You Wang58aa11e2016-05-17 10:35:44 -0700111 try:
112 links = controller.CLI.links()
113 links = json.loads( links )
114 if not len( links ) == upLinkNum:
115 checkResult = EventStates().FAIL
116 main.log.warn( "Topo Check - link number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, upLinkNum, len( links ) ) )
117 # Check devices
118 devices = controller.CLI.devices()
119 devices = json.loads( devices )
120 availableDeviceNum = 0
121 for device in devices:
122 if device[ 'available' ] == True:
123 availableDeviceNum += 1
124 if not availableDeviceNum == upDeviceNum:
125 checkResult = EventStates().FAIL
126 main.log.warn( "Topo Check - device number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, upDeviceNum, availableDeviceNum ) )
127 # Check hosts
128 hosts = controller.CLI.hosts()
129 hosts = json.loads( hosts )
130 if not len( hosts ) == upHostNum:
131 checkResult = EventStates().FAIL
132 main.log.warn( "Topo Check - host number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, upHostNum, len( hosts ) ) )
133 # Check clusters
134 clusters = controller.CLI.clusters()
135 clusters = json.loads( clusters )
136 if not len( clusters ) == clusterNum:
137 checkResult = EventStates().FAIL
138 main.log.warn( "Topo Check - cluster number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, clusterNum, len( clusters ) ) )
139 except ( TypeError, ValueError ):
140 main.log.exception( "Flow Check - Object not as expected" )
141 return EventStates().FAIL
You Wangdb927a52016-02-26 11:03:28 -0800142 return checkResult
143
144class ONOSCheck( CheckEvent ):
145 def __init__( self ):
146 CheckEvent.__init__( self )
147 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
148 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
149
150 def startCheckEvent( self, args=None ):
151 import json
152 checkResult = EventStates().PASS
153 topics = []
154 # TODO: Other topics?
155 for i in range( 14 ):
156 topics.append( "intent-partition-" + str( i ) )
157 dpidToAvailability = {}
158 dpidToMaster = {}
159 for device in main.devices:
160 if device.isDown() or device.isRemoved():
161 dpidToAvailability[ device.dpid ] = False
162 else:
163 dpidToAvailability[ device.dpid ] = True
164 dpidToMaster[ device.dpid ] = 'unknown'
165 # Check mastership, leaders and node states on each controller node
166 for controller in main.controllers:
167 if controller.isUp():
168 # Check mastership
You Wang58aa11e2016-05-17 10:35:44 -0700169 try:
170 with controller.CLILock:
171 roles = controller.CLI.roles()
172 roles = json.loads( roles )
173 for device in roles:
174 dpid = device[ 'id' ]
175 if dpidToMaster[ dpid ] == 'unknown':
176 dpidToMaster[ dpid ] = device[ 'master' ]
177 elif dpidToMaster[ dpid ] != device[ 'master' ]:
178 checkResult = EventStates().FAIL
179 main.log.warn( "ONOS Check - Mastership of %s on ONOS%s is inconsistent with that on ONOS1" % ( dpid, controller.index ) )
180 if dpidToAvailability[ dpid ] and device[ 'master' ] == "none":
181 checkResult = EventStates().FAIL
182 main.log.warn( "ONOS Check - Device %s has no master on ONOS%s" % ( dpid, controller.index ) )
183 # Check leaders
184 with controller.CLILock:
185 leaders = controller.CLI.leaders()
186 leaders = json.loads( leaders )
187 ONOSTopics = [ j['topic'] for j in leaders ]
188 for topic in topics:
189 if topic not in ONOSTopics:
190 checkResult = EventStates().FAIL
191 main.log.warn( "ONOS Check - Topic %s not in leaders on ONOS%s" % ( topic, controller.index ) )
192 # Check node state
193 with controller.CLILock:
194 nodes = controller.CLI.nodes()
195 nodes = json.loads( nodes )
196 ipToState = {}
197 for node in nodes:
198 ipToState[ node[ 'ip' ] ] = node[ 'state' ]
199 for c in main.controllers:
200 if c.isUp() and ipToState[ c.ip ] == 'READY':
201 pass
202 elif not c.isUp() and ipToState[ c.ip ] == 'INACTIVE':
203 pass
204 else:
205 checkResult = EventStates().FAIL
206 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 ] ) )
207 # TODO: check partitions?
208 except ( TypeError, ValueError ):
209 main.log.exception( "ONOS Check - Object not as expected" )
210 return EventStates().FAIL
You Wangdb927a52016-02-26 11:03:28 -0800211 return checkResult
212
213class TrafficCheck( CheckEvent ):
214 def __init__( self ):
215 CheckEvent.__init__( self )
216 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
217 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
218
219 def startCheckEvent( self, args=None ):
220 checkResult = EventStates().PASS
221 pool = []
222 wait = int( main.params[ 'EVENT' ][ 'TrafficCheck' ][ 'pingWait' ] )
223 timeout = int( main.params[ 'EVENT' ][ 'TrafficCheck' ][ 'pingTimeout' ] )
224 dstIPv4List = {}
225 dstIPv6List = {}
226 upHosts = []
227 for host in main.hosts:
228 if host.isUp():
229 upHosts.append( host )
230 for host in upHosts:
231 dstIPv4List[ host.index ] = []
232 dstIPv6List[ host.index ] = []
233 for correspondent in host.correspondents:
234 if not correspondent in upHosts:
235 continue
236 for ipAddress in correspondent.ipAddresses:
237 if ipAddress.startswith( str( main.params[ 'TEST' ][ 'ipv6Prefix' ] ) ):
238 dstIPv6List[ host.index ].append( ipAddress )
239 elif ipAddress.startswith( str( main.params[ 'TEST' ][ 'ipv4Prefix' ] ) ):
240 dstIPv4List[ host.index ].append( ipAddress )
241 thread = main.Thread( target=host.handle.pingHostSetAlternative,
242 threadID=main.threadID,
243 name="pingHostSetAlternative",
244 args=[ dstIPv4List[ host.index ], 1 ] )
245 pool.append( thread )
246 thread.start()
247 with main.variableLock:
248 main.threadID += 1
249 for thread in pool:
250 thread.join( 10 )
251 if not thread.result:
252 checkResult = EventStates().FAIL
253 main.log.warn( "Traffic Check - ping failed" )
254
255 if not main.enableIPv6:
256 return checkResult
257 # Check ipv6 ping
258 for host in upHosts:
259 thread = main.Thread( target=host.handle.pingHostSetAlternative,
260 threadID=main.threadID,
261 name="pingHostSetAlternative",
262 args=[ dstIPv6List[ host.index ], 1, True ] )
263 pool.append( thread )
264 thread.start()
265 with main.variableLock:
266 main.threadID += 1
267 for thread in pool:
268 thread.join( 10 )
269 if not thread.result:
270 checkResult = EventStates().FAIL
271 main.log.warn( "Traffic Check - ping6 failed" )
272 return checkResult
273