blob: 7819fecb27a8ac82b9055536ac06aa9958933d55 [file] [log] [blame]
Devin Lim58046fa2017-07-05 16:55:00 -07001import time
2import re
3import imp
4import json
5class Topology:
6
7 def __init__( self ):
8 self.default = ''
9 """
10 These functions can be used for topology comparisons
11 """
12 def getAllDevices( self, numNode, needRetry, kwargs={} ):
13 """
14 Return a list containing the devices output from each ONOS node
15 """
16 devices = []
17 threads = []
Jon Hallca319892017-06-15 15:25:22 -070018 for ctrl in main.Cluster.active():
19 t = main.Thread( target=utilities.retry if needRetry else ctrl.devices,
20 name="devices-" + str( ctrl ),
21 args=[ ctrl.devices, [ None ] ] if needRetry else [],
Devin Lim58046fa2017-07-05 16:55:00 -070022 kwargs=kwargs )
23 threads.append( t )
24 t.start()
25
26 for t in threads:
27 t.join()
28 devices.append( t.result )
29 return devices
30
31 def getAllHosts( self, numNode, needRetry, kwargs={}, inJson=False ):
32 """
33 Return a list containing the hosts output from each ONOS node
34 """
35 hosts = []
36 ipResult = main.TRUE
37 threads = []
Jon Hallca319892017-06-15 15:25:22 -070038 for ctrl in main.Cluster.active():
39 t = main.Thread( target=utilities.retry if needRetry else ctrl.hosts,
40 name="hosts-" + str( ctrl ),
41 args=[ ctrl.hosts, [ None ] ] if needRetry else [],
Devin Lim58046fa2017-07-05 16:55:00 -070042 kwargs=kwargs )
43 threads.append( t )
44 t.start()
45
46 for t in threads:
47 t.join()
48 if inJson:
49 try:
50 hosts.append( json.loads( t.result ) )
51 except ( ValueError, TypeError ):
52 main.log.exception( "Error parsing hosts results" )
53 main.log.error( repr( t.result ) )
54 hosts.append( None )
55 else:
56 hosts.append( t.result )
57 return hosts
58
59 def getAllPorts( self, numNode, needRetry, kwargs={} ):
60 """
61 Return a list containing the ports output from each ONOS node
62 """
63 ports = []
64 threads = []
Jon Hallca319892017-06-15 15:25:22 -070065 for ctrl in main.Cluster.active():
66 t = main.Thread( target=utilities.retry if needRetry else ctrl.ports,
67 name="ports-" + str( ctrl ),
68 args=[ ctrl.ports, [ None ] ] if needRetry else [],
Devin Lim58046fa2017-07-05 16:55:00 -070069 kwargs=kwargs )
70 threads.append( t )
71 t.start()
72
73 for t in threads:
74 t.join()
75 ports.append( t.result )
76 return ports
77
78 def getAllLinks( self, numNode, needRetry, kwargs={} ):
79 """
80 Return a list containing the links output from each ONOS node
81 """
82 links = []
83 threads = []
Jon Hallca319892017-06-15 15:25:22 -070084 for ctrl in main.Cluster.active():
85 t = main.Thread( target=utilities.retry if needRetry else ctrl.links,
86 name="links-" + str( ctrl ),
87 args=[ ctrl.links, [ None ] ] if needRetry else [],
Devin Lim58046fa2017-07-05 16:55:00 -070088 kwargs=kwargs )
89 threads.append( t )
90 t.start()
91
92 for t in threads:
93 t.join()
94 links.append( t.result )
95 print links
96 return links
97
98 def getAllClusters( self, numNode, needRetry, kwargs={} ):
99 """
100 Return a list containing the clusters output from each ONOS node
101 """
102 clusters = []
103 threads = []
Jon Hallca319892017-06-15 15:25:22 -0700104 for ctrl in main.Cluster.active():
105 t = main.Thread( target=utilities.retry if needRetry else ctrl.clusters,
106 name="clusters-" + str( ctrl ),
107 args=[ ctrl.clusters, [ None ] ] if needRetry else [],
Devin Lim58046fa2017-07-05 16:55:00 -0700108 kwargs=kwargs )
109 threads.append( t )
110 t.start()
111
112 for t in threads:
113 t.join()
114 clusters.append( t.result )
115 return clusters
116
117 def compareDevicePort( self, Mininet, controller, mnSwitches, devices, ports ):
118 if devices[ controller ] and ports[ controller ] and \
119 "Error" not in devices[ controller ] and \
120 "Error" not in ports[ controller ]:
121 try:
122 currentDevicesResult = Mininet.compareSwitches(
123 mnSwitches,
124 json.loads( devices[ controller ] ),
125 json.loads( ports[ controller ] ) )
Jon Hallca319892017-06-15 15:25:22 -0700126 except ( TypeError, ValueError ):
Devin Lim58046fa2017-07-05 16:55:00 -0700127 main.log.error(
Jon Hallca319892017-06-15 15:25:22 -0700128 "Could not load json: {0} or {1}".format( str( devices[ controller ] ),
129 str( ports[ controller ] ) ) )
Devin Lim58046fa2017-07-05 16:55:00 -0700130 currentDevicesResult = main.FALSE
131 else:
132 currentDevicesResult = main.FALSE
133 return currentDevicesResult
134
135 def compareBase( self, compareElem, controller, compareF, compareArg ):
136 if compareElem[ controller ] and "Error" not in compareElem[ controller ]:
137 try:
138 if isinstance( compareArg, list ):
139 compareArg.append( json.loads( compareElem[ controller ] ) )
140 else:
141 compareArg = [compareArg, json.loads( compareElem[ controller ] ) ]
142
143 currentCompareResult = compareF( *compareArg )
144 except(TypeError, ValueError):
145 main.log.error(
146 "Could not load json: {0} or {1}".format( str( compareElem[ controller ] ) ) )
147 currentCompareResult = main.FALSE
148 else:
149 currentCompareResult = main.FALSE
150
151 return currentCompareResult
152
153 def compareTopos( self, Mininet, attempts=1 ):
154
155 main.case( "Compare ONOS Topology view to Mininet topology" )
156 main.caseExplanation = "Compare topology elements between Mininet" +\
157 " and ONOS"
158 main.log.info( "Gathering topology information from Mininet" )
159 devicesResults = main.FALSE # Overall Boolean for device correctness
160 linksResults = main.FALSE # Overall Boolean for link correctness
161 hostsResults = main.FALSE # Overall Boolean for host correctness
162 deviceFails = [] # Nodes where devices are incorrect
163 linkFails = [] # Nodes where links are incorrect
164 hostFails = [] # Nodes where hosts are incorrect
165
166 mnSwitches = Mininet.getSwitches()
167 mnLinks = Mininet.getLinks()
168 mnHosts = Mininet.getHosts()
169
170 main.step( "Comparing Mininet topology to ONOS topology" )
171
172 while ( attempts >= 0 ) and\
173 ( not devicesResults or not linksResults or not hostsResults ):
174 main.log.info( "Sleeping {} seconds".format( 2 ) )
175 time.sleep( 2 )
176 if not devicesResults:
177 devices = self.getAllDevices( main.numCtrls, False )
178 ports = self.getAllPorts( main.numCtrls, False )
179 devicesResults = main.TRUE
180 deviceFails = [] # Reset for each failed attempt
181 if not linksResults:
182 links = self.getAllLinks( main.numCtrls, False )
183 linksResults = main.TRUE
184 linkFails = [] # Reset for each failed attempt
185 if not hostsResults:
186 hosts = self.getAllHosts( main.numCtrls, False )
187 hostsResults = main.TRUE
188 hostFails = [] # Reset for each failed attempt
189
190 # Check for matching topology on each node
191 for controller in range( main.numCtrls ):
192 controllerStr = str( controller + 1 ) # ONOS node number
193 # Compare Devices
194 currentDevicesResult = self.compareDevicePort( Mininet, controller,
Jon Hallca319892017-06-15 15:25:22 -0700195 mnSwitches,
196 devices, ports )
Devin Lim58046fa2017-07-05 16:55:00 -0700197 if not currentDevicesResult:
198 deviceFails.append( controllerStr )
199 devicesResults = devicesResults and currentDevicesResult
200 # Compare Links
201 currentLinksResult = self.compareBase( links, controller,
Jon Hallca319892017-06-15 15:25:22 -0700202 Mininet.compareLinks,
203 [ mnSwitches, mnLinks ] )
Devin Lim58046fa2017-07-05 16:55:00 -0700204 if not currentLinksResult:
205 linkFails.append( controllerStr )
206 linksResults = linksResults and currentLinksResult
207 # Compare Hosts
208 currentHostsResult = self.compareBase( hosts, controller,
209 Mininet.compareHosts,
210 mnHosts )
211 if not currentHostsResult:
212 hostFails.append( controllerStr )
213 hostsResults = hostsResults and currentHostsResult
214 # Decrement Attempts Remaining
215 attempts -= 1
216
217 utilities.assert_equals( expect=[],
218 actual=deviceFails,
219 onpass="ONOS correctly discovered all devices",
220 onfail="ONOS incorrectly discovered devices on nodes: " +
221 str( deviceFails ) )
222 utilities.assert_equals( expect=[],
223 actual=linkFails,
224 onpass="ONOS correctly discovered all links",
225 onfail="ONOS incorrectly discovered links on nodes: " +
226 str( linkFails ) )
227 utilities.assert_equals( expect=[],
228 actual=hostFails,
229 onpass="ONOS correctly discovered all hosts",
230 onfail="ONOS incorrectly discovered hosts on nodes: " +
231 str( hostFails ) )
232 topoResults = hostsResults and linksResults and devicesResults
233 utilities.assert_equals( expect=main.TRUE,
234 actual=topoResults,
235 onpass="ONOS correctly discovered the topology",
236 onfail="ONOS incorrectly discovered the topology" )