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