Committed by
Gerrit Code Review
clean up the SingleSwitchFibInstaller, added the predeactivate hook and modified unit test CORD-663
Change-Id: I8bf9dd4c7a6e9c42833a6f631bfceb85b7f3ddef
Showing
2 changed files
with
42 additions
and
13 deletions
... | @@ -32,6 +32,7 @@ import org.onlab.packet.IpPrefix; | ... | @@ -32,6 +32,7 @@ import org.onlab.packet.IpPrefix; |
32 | import org.onlab.packet.MacAddress; | 32 | import org.onlab.packet.MacAddress; |
33 | import org.onlab.packet.VlanId; | 33 | import org.onlab.packet.VlanId; |
34 | import org.onlab.util.Tools; | 34 | import org.onlab.util.Tools; |
35 | +import org.onosproject.app.ApplicationService; | ||
35 | import org.onosproject.cfg.ComponentConfigService; | 36 | import org.onosproject.cfg.ComponentConfigService; |
36 | import org.onosproject.core.ApplicationId; | 37 | import org.onosproject.core.ApplicationId; |
37 | import org.onosproject.core.CoreService; | 38 | import org.onosproject.core.CoreService; |
... | @@ -88,6 +89,7 @@ import java.util.stream.Collectors; | ... | @@ -88,6 +89,7 @@ import java.util.stream.Collectors; |
88 | public class SingleSwitchFibInstaller { | 89 | public class SingleSwitchFibInstaller { |
89 | 90 | ||
90 | private final Logger log = LoggerFactory.getLogger(getClass()); | 91 | private final Logger log = LoggerFactory.getLogger(getClass()); |
92 | + private static final String APP_NAME = "org.onosproject.vrouter"; | ||
91 | 93 | ||
92 | private static final int PRIORITY_OFFSET = 100; | 94 | private static final int PRIORITY_OFFSET = 100; |
93 | private static final int PRIORITY_MULTIPLIER = 5; | 95 | private static final int PRIORITY_MULTIPLIER = 5; |
... | @@ -118,6 +120,9 @@ public class SingleSwitchFibInstaller { | ... | @@ -118,6 +120,9 @@ public class SingleSwitchFibInstaller { |
118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
119 | protected DeviceService deviceService; | 121 | protected DeviceService deviceService; |
120 | 122 | ||
123 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
124 | + protected ApplicationService applicationService; | ||
125 | + | ||
121 | @Property(name = "routeToNextHop", boolValue = false, | 126 | @Property(name = "routeToNextHop", boolValue = false, |
122 | label = "Install a /32 route to each next hop") | 127 | label = "Install a /32 route to each next hop") |
123 | private boolean routeToNextHop = false; | 128 | private boolean routeToNextHop = false; |
... | @@ -133,6 +138,7 @@ public class SingleSwitchFibInstaller { | ... | @@ -133,6 +138,7 @@ public class SingleSwitchFibInstaller { |
133 | 138 | ||
134 | private ApplicationId coreAppId; | 139 | private ApplicationId coreAppId; |
135 | private ApplicationId routerAppId; | 140 | private ApplicationId routerAppId; |
141 | + private ApplicationId vrouterAppId; | ||
136 | 142 | ||
137 | // Reference count for how many times a next hop is used by a route | 143 | // Reference count for how many times a next hop is used by a route |
138 | private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create(); | 144 | private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create(); |
... | @@ -163,6 +169,7 @@ public class SingleSwitchFibInstaller { | ... | @@ -163,6 +169,7 @@ public class SingleSwitchFibInstaller { |
163 | 169 | ||
164 | coreAppId = coreService.registerApplication(CoreService.CORE_APP_NAME); | 170 | coreAppId = coreService.registerApplication(CoreService.CORE_APP_NAME); |
165 | routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID); | 171 | routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID); |
172 | + vrouterAppId = coreService.registerApplication(APP_NAME); | ||
166 | 173 | ||
167 | networkConfigRegistry.registerConfigFactory(mcastConfigFactory); | 174 | networkConfigRegistry.registerConfigFactory(mcastConfigFactory); |
168 | 175 | ||
... | @@ -173,6 +180,10 @@ public class SingleSwitchFibInstaller { | ... | @@ -173,6 +180,10 @@ public class SingleSwitchFibInstaller { |
173 | 180 | ||
174 | updateConfig(); | 181 | updateConfig(); |
175 | 182 | ||
183 | + applicationService.registerDeactivateHook(vrouterAppId, () -> { | ||
184 | + this.cleanUp(); | ||
185 | + }); | ||
186 | + | ||
176 | log.info("Started"); | 187 | log.info("Started"); |
177 | } | 188 | } |
178 | 189 | ||
... | @@ -202,6 +213,17 @@ public class SingleSwitchFibInstaller { | ... | @@ -202,6 +213,17 @@ public class SingleSwitchFibInstaller { |
202 | log.info("routeToNextHop set to {}", routeToNextHop); | 213 | log.info("routeToNextHop set to {}", routeToNextHop); |
203 | } | 214 | } |
204 | 215 | ||
216 | + //remove filtering objectives and routes before deactivate. | ||
217 | + private void cleanUp() { | ||
218 | + //clean up the routes. | ||
219 | + for (Map.Entry<IpPrefix, IpAddress> routes: prefixToNextHop.entrySet()) { | ||
220 | + deleteRoute(new ResolvedRoute(routes.getKey(), null, null)); | ||
221 | + } | ||
222 | + //clean up the filtering objective for interfaces. | ||
223 | + Set<Interface> intfs = getInterfaces(); | ||
224 | + processIntfFilters(false, intfs); | ||
225 | + } | ||
226 | + | ||
205 | private void updateConfig() { | 227 | private void updateConfig() { |
206 | RouterConfig routerConfig = | 228 | RouterConfig routerConfig = |
207 | networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS); | 229 | networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS); |
... | @@ -226,22 +248,25 @@ public class SingleSwitchFibInstaller { | ... | @@ -226,22 +248,25 @@ public class SingleSwitchFibInstaller { |
226 | 248 | ||
227 | private void updateDevice() { | 249 | private void updateDevice() { |
228 | if (deviceId != null && deviceService.isAvailable(deviceId)) { | 250 | if (deviceId != null && deviceService.isAvailable(deviceId)) { |
229 | - | 251 | + Set<Interface> intfs = getInterfaces(); |
230 | - Set<Interface> intfs; | ||
231 | - if (interfaces.isEmpty()) { | ||
232 | - intfs = interfaceService.getInterfaces(); | ||
233 | - } else { | ||
234 | - // TODO need to fix by making interface names globally unique | ||
235 | - intfs = interfaceService.getInterfaces().stream() | ||
236 | - .filter(intf -> intf.connectPoint().deviceId().equals(deviceId)) | ||
237 | - .filter(intf -> interfaces.contains(intf.name())) | ||
238 | - .collect(Collectors.toSet()); | ||
239 | - } | ||
240 | - | ||
241 | processIntfFilters(true, intfs); | 252 | processIntfFilters(true, intfs); |
242 | } | 253 | } |
243 | } | 254 | } |
244 | 255 | ||
256 | + private Set<Interface> getInterfaces() { | ||
257 | + Set<Interface> intfs; | ||
258 | + if (interfaces.isEmpty()) { | ||
259 | + intfs = interfaceService.getInterfaces(); | ||
260 | + } else { | ||
261 | + // TODO need to fix by making interface names globally unique | ||
262 | + intfs = interfaceService.getInterfaces().stream() | ||
263 | + .filter(intf -> intf.connectPoint().deviceId().equals(deviceId)) | ||
264 | + .filter(intf -> interfaces.contains(intf.name())) | ||
265 | + .collect(Collectors.toSet()); | ||
266 | + } | ||
267 | + return intfs; | ||
268 | + } | ||
269 | + | ||
245 | private void updateRoute(ResolvedRoute route) { | 270 | private void updateRoute(ResolvedRoute route) { |
246 | addNextHop(route); | 271 | addNextHop(route); |
247 | 272 | ... | ... |
... | @@ -26,6 +26,7 @@ import org.onlab.packet.IpPrefix; | ... | @@ -26,6 +26,7 @@ import org.onlab.packet.IpPrefix; |
26 | import org.onlab.packet.MacAddress; | 26 | import org.onlab.packet.MacAddress; |
27 | import org.onlab.packet.VlanId; | 27 | import org.onlab.packet.VlanId; |
28 | import org.onosproject.TestApplicationId; | 28 | import org.onosproject.TestApplicationId; |
29 | +import org.onosproject.app.ApplicationService; | ||
29 | import org.onosproject.cfg.ComponentConfigService; | 30 | import org.onosproject.cfg.ComponentConfigService; |
30 | import org.onosproject.core.ApplicationId; | 31 | import org.onosproject.core.ApplicationId; |
31 | import org.onosproject.core.CoreService; | 32 | import org.onosproject.core.CoreService; |
... | @@ -102,12 +103,12 @@ public class SingleSwitchFibInstallerTest { | ... | @@ -102,12 +103,12 @@ public class SingleSwitchFibInstallerTest { |
102 | private static final InterfaceIpAddress INTF2 = | 103 | private static final InterfaceIpAddress INTF2 = |
103 | InterfaceIpAddress.valueOf("192.168.20.2/24"); | 104 | InterfaceIpAddress.valueOf("192.168.20.2/24"); |
104 | 105 | ||
105 | - | ||
106 | private final Set<Interface> interfaces = Sets.newHashSet(); | 106 | private final Set<Interface> interfaces = Sets.newHashSet(); |
107 | private InterfaceService interfaceService; | 107 | private InterfaceService interfaceService; |
108 | private NetworkConfigService networkConfigService; | 108 | private NetworkConfigService networkConfigService; |
109 | private NetworkConfigRegistry networkConfigRegistry; | 109 | private NetworkConfigRegistry networkConfigRegistry; |
110 | private FlowObjectiveService flowObjectiveService; | 110 | private FlowObjectiveService flowObjectiveService; |
111 | + private ApplicationService applicationService; | ||
111 | private DeviceService deviceService; | 112 | private DeviceService deviceService; |
112 | private static final ApplicationId APPID = TestApplicationId.create("foo"); | 113 | private static final ApplicationId APPID = TestApplicationId.create("foo"); |
113 | 114 | ||
... | @@ -132,6 +133,8 @@ public class SingleSwitchFibInstallerTest { | ... | @@ -132,6 +133,8 @@ public class SingleSwitchFibInstallerTest { |
132 | networkConfigService = createMock(NetworkConfigService.class); | 133 | networkConfigService = createMock(NetworkConfigService.class); |
133 | networkConfigRegistry = createMock(NetworkConfigRegistry.class); | 134 | networkConfigRegistry = createMock(NetworkConfigRegistry.class); |
134 | flowObjectiveService = createMock(FlowObjectiveService.class); | 135 | flowObjectiveService = createMock(FlowObjectiveService.class); |
136 | + applicationService = createNiceMock(ApplicationService.class); | ||
137 | + replay(applicationService); | ||
135 | deviceService = new TestDeviceService(); | 138 | deviceService = new TestDeviceService(); |
136 | CoreService coreService = createNiceMock(CoreService.class); | 139 | CoreService coreService = createNiceMock(CoreService.class); |
137 | expect(coreService.registerApplication(anyString())).andReturn(APPID).anyTimes(); | 140 | expect(coreService.registerApplication(anyString())).andReturn(APPID).anyTimes(); |
... | @@ -141,6 +144,7 @@ public class SingleSwitchFibInstallerTest { | ... | @@ -141,6 +144,7 @@ public class SingleSwitchFibInstallerTest { |
141 | sSfibInstaller.networkConfigRegistry = networkConfigRegistry; | 144 | sSfibInstaller.networkConfigRegistry = networkConfigRegistry; |
142 | sSfibInstaller.interfaceService = interfaceService; | 145 | sSfibInstaller.interfaceService = interfaceService; |
143 | sSfibInstaller.flowObjectiveService = flowObjectiveService; | 146 | sSfibInstaller.flowObjectiveService = flowObjectiveService; |
147 | + sSfibInstaller.applicationService = applicationService; | ||
144 | sSfibInstaller.coreService = coreService; | 148 | sSfibInstaller.coreService = coreService; |
145 | sSfibInstaller.routeService = new TestRouteService(); | 149 | sSfibInstaller.routeService = new TestRouteService(); |
146 | sSfibInstaller.deviceService = deviceService; | 150 | sSfibInstaller.deviceService = deviceService; | ... | ... |
-
Please register or login to post a comment