blob: b22e65cc1f106d0b38289013047d00c08c31bf41 [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
28 intentIDs = []
29 for intent in main.intents:
30 if intent.isHostIntent():
31 deviceA = intent.hostA.device
32 deviceB = intent.hostB.device
33 elif intent.isPointIntent():
34 deviceA = intent.deviceA
35 deviceB = intent.deviceB
36 # Exclude the intents that are to or from removed devices/hosts
37 if not deviceA.isRemoved() and not deviceB.isRemoved():
38 intentIDs.append( intent.id )
39 for controller in main.controllers:
40 if controller.isUp():
41 with controller.CLILock:
42 intentState = controller.CLI.checkIntentState( intentsId=intentIDs )
43 if not intentState:
44 main.log.warn( "Intent Check - Not all intents are in INSTALLED state on ONOS%s" % ( controller.index ) )
45 checkResult = EventStates().FAIL
46 #TODO: check flows?
47 return checkResult
48
49class TopoCheck( CheckEvent ):
50 def __init__( self ):
51 CheckEvent.__init__( self )
52 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
53 self.typeIndex = int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
54
55 def startCheckEvent( self, args=None ):
56 import json
57 checkResult = EventStates().PASS
58 upLinkNum = 0
59 upDeviceNum = 0
60 upHostNum = 0
61 with main.variableLock:
62 for link in main.links:
63 if not link.isDown() and not link.isRemoved():
64 upLinkNum += 1
65 for device in main.devices:
66 if not device.isDown() and not device.isRemoved():
67 upDeviceNum += 1
68 for host in main.hosts:
69 if not host.isDown() and not host.isRemoved():
70 upHostNum += 1
71 clusterNum = 1
72 for controller in main.controllers:
73 if controller.isUp():
74 with controller.CLILock:
75 topologyOutput = controller.CLI.topology()
76 topoState = controller.CLI.checkStatus( topologyOutput, upDeviceNum, upLinkNum )
77 #if not topoState:
78 # main.log.warn( "Topo Check - link or device number discoverd by ONOS%s is incorrect" % ( controller.index ) )
79 # checkResult = EventStates().FAIL
80 # Check links
81 links = controller.CLI.links()
82 links = json.loads( links )
83 if not len( links ) == upLinkNum:
84 checkResult = EventStates().FAIL
85 main.log.warn( "Topo Check - link number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, upLinkNum, len( links ) ) )
86 # Check devices
87 devices = controller.CLI.devices()
88 devices = json.loads( devices )
89 availableDeviceNum = 0
90 for device in devices:
91 if device[ 'available' ] == True:
92 availableDeviceNum += 1
93 if not availableDeviceNum == upDeviceNum:
94 checkResult = EventStates().FAIL
95 main.log.warn( "Topo Check - device number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, upDeviceNum, availableDeviceNum ) )
96 # Check hosts
97 hosts = controller.CLI.hosts()
98 hosts = json.loads( hosts )
99 if not len( hosts ) == upHostNum:
100 checkResult = EventStates().FAIL
101 main.log.warn( "Topo Check - host number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, upHostNum, len( hosts ) ) )
102 # Check clusters
103 clusters = controller.CLI.clusters()
104 clusters = json.loads( clusters )
105 if not len( clusters ) == clusterNum:
106 checkResult = EventStates().FAIL
107 main.log.warn( "Topo Check - cluster number discoverd by ONOS%s is incorrect: %s expected and %s actual" % ( controller.index, clusterNum, len( clusters ) ) )
108 return checkResult
109
110class ONOSCheck( CheckEvent ):
111 def __init__( self ):
112 CheckEvent.__init__( self )
113 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
114 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
115
116 def startCheckEvent( self, args=None ):
117 import json
118 checkResult = EventStates().PASS
119 topics = []
120 # TODO: Other topics?
121 for i in range( 14 ):
122 topics.append( "intent-partition-" + str( i ) )
123 dpidToAvailability = {}
124 dpidToMaster = {}
125 for device in main.devices:
126 if device.isDown() or device.isRemoved():
127 dpidToAvailability[ device.dpid ] = False
128 else:
129 dpidToAvailability[ device.dpid ] = True
130 dpidToMaster[ device.dpid ] = 'unknown'
131 # Check mastership, leaders and node states on each controller node
132 for controller in main.controllers:
133 if controller.isUp():
134 # Check mastership
135 with controller.CLILock:
136 roles = controller.CLI.roles()
137 roles = json.loads( roles )
138 for device in roles:
139 dpid = device[ 'id' ]
140 if dpidToMaster[ dpid ] == 'unknown':
141 dpidToMaster[ dpid ] = device[ 'master' ]
142 elif dpidToMaster[ dpid ] != device[ 'master' ]:
143 checkResult = EventStates().FAIL
You Wang82e04e12016-05-12 17:10:27 -0700144 main.log.warn( "ONOS Check - Mastership of %s on ONOS%s is inconsistent with that on ONOS1" % ( dpid, controller.index ) )
You Wangdb927a52016-02-26 11:03:28 -0800145 if dpidToAvailability[ dpid ] and device[ 'master' ] == "none":
146 checkResult = EventStates().FAIL
You Wang82e04e12016-05-12 17:10:27 -0700147 main.log.warn( "ONOS Check - Device %s has no master on ONOS%s" % ( dpid, controller.index ) )
You Wangdb927a52016-02-26 11:03:28 -0800148 # Check leaders
149 with controller.CLILock:
150 leaders = controller.CLI.leaders()
151 leaders = json.loads( leaders )
152 ONOSTopics = [ j['topic'] for j in leaders ]
153 for topic in topics:
154 if topic not in ONOSTopics:
155 checkResult = EventStates().FAIL
156 main.log.warn( "ONOS Check - Topic %s not in leaders on ONOS%s" % ( topic, controller.index ) )
157 # Check node state
158 with controller.CLILock:
159 nodes = controller.CLI.nodes()
160 nodes = json.loads( nodes )
161 ipToState = {}
162 for node in nodes:
163 ipToState[ node[ 'ip' ] ] = node[ 'state' ]
164 for c in main.controllers:
165 if c.isUp() and ipToState[ c.ip ] == 'READY':
166 pass
167 elif not c.isUp() and ipToState[ c.ip ] == 'INACTIVE':
168 pass
169 else:
170 checkResult = EventStates().FAIL
171 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 ] ) )
172 # TODO: check partitions?
173 return checkResult
174
175class TrafficCheck( CheckEvent ):
176 def __init__( self ):
177 CheckEvent.__init__( self )
178 self.typeString = main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeString' ]
179 self.typeIndex= int( main.params[ 'EVENT' ][ self.__class__.__name__ ][ 'typeIndex' ] )
180
181 def startCheckEvent( self, args=None ):
182 checkResult = EventStates().PASS
183 pool = []
184 wait = int( main.params[ 'EVENT' ][ 'TrafficCheck' ][ 'pingWait' ] )
185 timeout = int( main.params[ 'EVENT' ][ 'TrafficCheck' ][ 'pingTimeout' ] )
186 dstIPv4List = {}
187 dstIPv6List = {}
188 upHosts = []
189 for host in main.hosts:
190 if host.isUp():
191 upHosts.append( host )
192 for host in upHosts:
193 dstIPv4List[ host.index ] = []
194 dstIPv6List[ host.index ] = []
195 for correspondent in host.correspondents:
196 if not correspondent in upHosts:
197 continue
198 for ipAddress in correspondent.ipAddresses:
199 if ipAddress.startswith( str( main.params[ 'TEST' ][ 'ipv6Prefix' ] ) ):
200 dstIPv6List[ host.index ].append( ipAddress )
201 elif ipAddress.startswith( str( main.params[ 'TEST' ][ 'ipv4Prefix' ] ) ):
202 dstIPv4List[ host.index ].append( ipAddress )
203 thread = main.Thread( target=host.handle.pingHostSetAlternative,
204 threadID=main.threadID,
205 name="pingHostSetAlternative",
206 args=[ dstIPv4List[ host.index ], 1 ] )
207 pool.append( thread )
208 thread.start()
209 with main.variableLock:
210 main.threadID += 1
211 for thread in pool:
212 thread.join( 10 )
213 if not thread.result:
214 checkResult = EventStates().FAIL
215 main.log.warn( "Traffic Check - ping failed" )
216
217 if not main.enableIPv6:
218 return checkResult
219 # Check ipv6 ping
220 for host in upHosts:
221 thread = main.Thread( target=host.handle.pingHostSetAlternative,
222 threadID=main.threadID,
223 name="pingHostSetAlternative",
224 args=[ dstIPv6List[ host.index ], 1, True ] )
225 pool.append( thread )
226 thread.start()
227 with main.variableLock:
228 main.threadID += 1
229 for thread in pool:
230 thread.join( 10 )
231 if not thread.result:
232 checkResult = EventStates().FAIL
233 main.log.warn( "Traffic Check - ping6 failed" )
234 return checkResult
235