Jonathan Hart

Change SingleSwitchFibInstaller to use new router config.

Also it now supports the config being added after the application starts.

Change-Id: I194a4482702c58d2e91be0436dc11ab42973df96
...@@ -35,6 +35,8 @@ import org.onosproject.core.CoreService; ...@@ -35,6 +35,8 @@ import org.onosproject.core.CoreService;
35 import org.onosproject.incubator.net.intf.Interface; 35 import org.onosproject.incubator.net.intf.Interface;
36 import org.onosproject.incubator.net.intf.InterfaceService; 36 import org.onosproject.incubator.net.intf.InterfaceService;
37 import org.onosproject.net.DeviceId; 37 import org.onosproject.net.DeviceId;
38 +import org.onosproject.net.config.NetworkConfigEvent;
39 +import org.onosproject.net.config.NetworkConfigListener;
38 import org.onosproject.net.config.NetworkConfigService; 40 import org.onosproject.net.config.NetworkConfigService;
39 import org.onosproject.net.device.DeviceEvent; 41 import org.onosproject.net.device.DeviceEvent;
40 import org.onosproject.net.device.DeviceListener; 42 import org.onosproject.net.device.DeviceListener;
...@@ -58,14 +60,13 @@ import org.onosproject.routing.FibEntry; ...@@ -58,14 +60,13 @@ import org.onosproject.routing.FibEntry;
58 import org.onosproject.routing.FibListener; 60 import org.onosproject.routing.FibListener;
59 import org.onosproject.routing.FibUpdate; 61 import org.onosproject.routing.FibUpdate;
60 import org.onosproject.routing.RoutingService; 62 import org.onosproject.routing.RoutingService;
61 -import org.onosproject.routing.config.BgpConfig; 63 +import org.onosproject.routing.config.RouterConfig;
62 import org.slf4j.Logger; 64 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory; 65 import org.slf4j.LoggerFactory;
64 66
65 import java.util.Collection; 67 import java.util.Collection;
66 import java.util.HashMap; 68 import java.util.HashMap;
67 import java.util.Map; 69 import java.util.Map;
68 -import java.util.Optional;
69 import java.util.Set; 70 import java.util.Set;
70 71
71 /** 72 /**
...@@ -97,12 +98,13 @@ public class SingleSwitchFibInstaller { ...@@ -97,12 +98,13 @@ public class SingleSwitchFibInstaller {
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected DeviceService deviceService; 99 protected DeviceService deviceService;
99 100
100 - private InnerDeviceListener deviceListener; 101 + private InternalDeviceListener deviceListener;
101 102
102 // Device id of data-plane switch - should be learned from config 103 // Device id of data-plane switch - should be learned from config
103 private DeviceId deviceId; 104 private DeviceId deviceId;
104 105
105 private ApplicationId appId; 106 private ApplicationId appId;
107 + private ApplicationId routerAppId;
106 108
107 // Reference count for how many times a next hop is used by a route 109 // Reference count for how many times a next hop is used by a route
108 private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create(); 110 private final Multiset<IpAddress> nextHopsCount = ConcurrentHashMultiset.create();
...@@ -119,29 +121,18 @@ public class SingleSwitchFibInstaller { ...@@ -119,29 +121,18 @@ public class SingleSwitchFibInstaller {
119 121
120 @Activate 122 @Activate
121 protected void activate() { 123 protected void activate() {
122 - ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); 124 + // TODO why are there two of the same app ID?
123 - BgpConfig bgpConfig = 125 + routerAppId = coreService.registerApplication(RoutingService.ROUTER_APP_ID);
124 - networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
125 -
126 - if (bgpConfig == null) {
127 - log.error("No BgpConfig found");
128 - return;
129 - }
130 -
131 - getDeviceConfiguration(bgpConfig);
132 126
133 appId = coreService.getAppId(RoutingService.ROUTER_APP_ID); 127 appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
134 128
135 - deviceListener = new InnerDeviceListener(); 129 + deviceListener = new InternalDeviceListener();
136 deviceService.addListener(deviceListener); 130 deviceService.addListener(deviceListener);
137 131
138 routingService.addFibListener(new InternalFibListener()); 132 routingService.addFibListener(new InternalFibListener());
139 routingService.start(); 133 routingService.start();
140 134
141 - // Initialize devices now if they are already connected 135 + updateConfig();
142 - if (deviceService.isAvailable(deviceId)) {
143 - processIntfFilters(true, interfaceService.getInterfaces());
144 - }
145 136
146 log.info("Started"); 137 log.info("Started");
147 } 138 }
...@@ -157,35 +148,26 @@ public class SingleSwitchFibInstaller { ...@@ -157,35 +148,26 @@ public class SingleSwitchFibInstaller {
157 log.info("Stopped"); 148 log.info("Stopped");
158 } 149 }
159 150
160 - private void getDeviceConfiguration(BgpConfig bgpConfig) { 151 + private void updateConfig() {
161 - Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker = 152 + RouterConfig routerConfig =
162 - bgpConfig.bgpSpeakers().stream().findAny(); 153 + networkConfigService.getConfig(routerAppId, RoutingService.ROUTER_CONFIG_CLASS);
163 154
164 - if (!bgpSpeaker.isPresent()) { 155 + if (routerConfig == null) {
165 - log.error("BGP speaker configuration not found"); 156 + log.info("Router config not available");
166 return; 157 return;
167 } 158 }
168 159
169 - Optional<IpAddress> peerAddress = 160 + deviceId = routerConfig.getControlPlaneConnectPoint().deviceId();
170 - bgpSpeaker.get().peers().stream().findAny();
171 161
172 - if (!peerAddress.isPresent()) { 162 + log.info("Router device ID is {}", deviceId);
173 - log.error("BGP speaker must have peers configured");
174 - return;
175 - }
176 163
177 - Interface intf = interfaceService.getMatchingInterface(peerAddress.get()); 164 + updateDevice();
165 + }
178 166
179 - if (intf == null) { 167 + private void updateDevice() {
180 - log.error("No interface found for peer"); 168 + if (deviceId != null && deviceService.isAvailable(deviceId)) {
181 - return; 169 + processIntfFilters(true, interfaceService.getInterfaces());
182 } 170 }
183 -
184 - // Assume all peers are configured on the same device - this is required
185 - // by the BGP router
186 - deviceId = intf.connectPoint().deviceId();
187 -
188 - log.info("Router dpid: {}", deviceId);
189 } 171 }
190 172
191 private void updateFibEntry(Collection<FibUpdate> updates) { 173 private void updateFibEntry(Collection<FibUpdate> updates) {
...@@ -384,9 +366,11 @@ public class SingleSwitchFibInstaller { ...@@ -384,9 +366,11 @@ public class SingleSwitchFibInstaller {
384 } 366 }
385 } 367 }
386 368
387 - 369 + /**
388 - // Triggers driver setup when a device is (re)detected. 370 + * Listener for device events used to trigger driver setup when a device is
389 - private class InnerDeviceListener implements DeviceListener { 371 + * (re)detected.
372 + */
373 + private class InternalDeviceListener implements DeviceListener {
390 @Override 374 @Override
391 public void event(DeviceEvent event) { 375 public void event(DeviceEvent event) {
392 switch (event.type()) { 376 switch (event.type()) {
...@@ -411,4 +395,26 @@ public class SingleSwitchFibInstaller { ...@@ -411,4 +395,26 @@ public class SingleSwitchFibInstaller {
411 } 395 }
412 } 396 }
413 } 397 }
398 +
399 + /**
400 + * Listener for network config events.
401 + */
402 + private class InternalNetworkConfigListener implements NetworkConfigListener {
403 + @Override
404 + public void event(NetworkConfigEvent event) {
405 + if (event.subject().equals(RoutingService.ROUTER_CONFIG_CLASS)) {
406 + switch (event.type()) {
407 + case CONFIG_ADDED:
408 + case CONFIG_UPDATED:
409 + updateConfig();
410 + break;
411 + case CONFIG_REGISTERED:
412 + case CONFIG_UNREGISTERED:
413 + case CONFIG_REMOVED:
414 + default:
415 + break;
416 + }
417 + }
418 + }
419 + }
414 } 420 }
......