Jonathan Hart
Committed by Gerrit Code Review

Clear FPM routes when the FPM connection goes down.

Change-Id: I20b7eca27150aad9958cf872edbbfa973a003af9
...@@ -19,6 +19,10 @@ import com.google.common.collect.ImmutableMap; ...@@ -19,6 +19,10 @@ import com.google.common.collect.ImmutableMap;
19 import org.apache.felix.scr.annotations.Activate; 19 import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Component; 20 import org.apache.felix.scr.annotations.Component;
21 import org.apache.felix.scr.annotations.Deactivate; 21 import org.apache.felix.scr.annotations.Deactivate;
22 +import org.apache.felix.scr.annotations.Modified;
23 +import org.apache.felix.scr.annotations.Property;
24 +import org.apache.felix.scr.annotations.Reference;
25 +import org.apache.felix.scr.annotations.ReferenceCardinality;
22 import org.apache.felix.scr.annotations.Service; 26 import org.apache.felix.scr.annotations.Service;
23 import org.jboss.netty.bootstrap.ServerBootstrap; 27 import org.jboss.netty.bootstrap.ServerBootstrap;
24 import org.jboss.netty.channel.Channel; 28 import org.jboss.netty.channel.Channel;
...@@ -32,6 +36,8 @@ import org.jboss.netty.channel.group.DefaultChannelGroup; ...@@ -32,6 +36,8 @@ import org.jboss.netty.channel.group.DefaultChannelGroup;
32 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; 36 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
33 import org.onlab.packet.IpAddress; 37 import org.onlab.packet.IpAddress;
34 import org.onlab.packet.IpPrefix; 38 import org.onlab.packet.IpPrefix;
39 +import org.onlab.util.Tools;
40 +import org.onosproject.cfg.ComponentConfigService;
35 import org.onosproject.routing.RouteEntry; 41 import org.onosproject.routing.RouteEntry;
36 import org.onosproject.routing.RouteListener; 42 import org.onosproject.routing.RouteListener;
37 import org.onosproject.routing.RouteSourceService; 43 import org.onosproject.routing.RouteSourceService;
...@@ -43,14 +49,18 @@ import org.onosproject.routing.fpm.protocol.RouteAttributeDst; ...@@ -43,14 +49,18 @@ import org.onosproject.routing.fpm.protocol.RouteAttributeDst;
43 import org.onosproject.routing.fpm.protocol.RouteAttributeGateway; 49 import org.onosproject.routing.fpm.protocol.RouteAttributeGateway;
44 import org.onosproject.routing.fpm.protocol.RtNetlink; 50 import org.onosproject.routing.fpm.protocol.RtNetlink;
45 import org.onosproject.routing.fpm.protocol.RtProtocol; 51 import org.onosproject.routing.fpm.protocol.RtProtocol;
52 +import org.osgi.service.component.ComponentContext;
46 import org.slf4j.Logger; 53 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory; 54 import org.slf4j.LoggerFactory;
48 55
49 import java.net.InetSocketAddress; 56 import java.net.InetSocketAddress;
50 import java.net.SocketAddress; 57 import java.net.SocketAddress;
51 import java.util.Collections; 58 import java.util.Collections;
59 +import java.util.Dictionary;
60 +import java.util.List;
52 import java.util.Map; 61 import java.util.Map;
53 import java.util.concurrent.ConcurrentHashMap; 62 import java.util.concurrent.ConcurrentHashMap;
63 +import java.util.stream.Collectors;
54 64
55 import static java.util.concurrent.Executors.newCachedThreadPool; 65 import static java.util.concurrent.Executors.newCachedThreadPool;
56 import static org.onlab.util.Tools.groupedThreads; 66 import static org.onlab.util.Tools.groupedThreads;
...@@ -63,6 +73,11 @@ import static org.onlab.util.Tools.groupedThreads; ...@@ -63,6 +73,11 @@ import static org.onlab.util.Tools.groupedThreads;
63 public class FpmManager implements RouteSourceService, FpmInfoService { 73 public class FpmManager implements RouteSourceService, FpmInfoService {
64 private final Logger log = LoggerFactory.getLogger(getClass()); 74 private final Logger log = LoggerFactory.getLogger(getClass());
65 75
76 + private static final int FPM_PORT = 2620;
77 +
78 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 + protected ComponentConfigService componentConfigService;
80 +
66 private ServerBootstrap serverBootstrap; 81 private ServerBootstrap serverBootstrap;
67 private Channel serverChannel; 82 private Channel serverChannel;
68 private ChannelGroup allChannels = new DefaultChannelGroup(); 83 private ChannelGroup allChannels = new DefaultChannelGroup();
...@@ -73,19 +88,36 @@ public class FpmManager implements RouteSourceService, FpmInfoService { ...@@ -73,19 +88,36 @@ public class FpmManager implements RouteSourceService, FpmInfoService {
73 88
74 private RouteListener routeListener; 89 private RouteListener routeListener;
75 90
76 - private static final int FPM_PORT = 2620; 91 + @Property(name = "clearRoutes", boolValue = true,
92 + label = "Whether to clear routes when the FPM connection goes down")
93 + private boolean clearRoutes = true;
77 94
78 @Activate 95 @Activate
79 - protected void activate() { 96 + protected void activate(ComponentContext context) {
97 + componentConfigService.registerProperties(getClass());
98 + modified(context);
80 log.info("Started"); 99 log.info("Started");
81 } 100 }
82 101
83 @Deactivate 102 @Deactivate
84 protected void deactivate() { 103 protected void deactivate() {
85 stopServer(); 104 stopServer();
105 + componentConfigService.unregisterProperties(getClass(), false);
86 log.info("Stopped"); 106 log.info("Stopped");
87 } 107 }
88 108
109 + @Modified
110 + protected void modified(ComponentContext context) {
111 + Dictionary<?, ?> properties = context.getProperties();
112 + if (properties == null) {
113 + return;
114 + }
115 + String strClearRoutes = Tools.get(properties, "clearRoutes");
116 + clearRoutes = Boolean.parseBoolean(strClearRoutes);
117 +
118 + log.info("clearRoutes set to {}", clearRoutes);
119 + }
120 +
89 private void startServer() { 121 private void startServer() {
90 ChannelFactory channelFactory = new NioServerSocketChannelFactory( 122 ChannelFactory channelFactory = new NioServerSocketChannelFactory(
91 newCachedThreadPool(groupedThreads("onos/fpm", "sm-boss-%d")), 123 newCachedThreadPool(groupedThreads("onos/fpm", "sm-boss-%d")),
...@@ -127,6 +159,10 @@ public class FpmManager implements RouteSourceService, FpmInfoService { ...@@ -127,6 +159,10 @@ public class FpmManager implements RouteSourceService, FpmInfoService {
127 if (serverBootstrap != null) { 159 if (serverBootstrap != null) {
128 serverBootstrap.releaseExternalResources(); 160 serverBootstrap.releaseExternalResources();
129 } 161 }
162 +
163 + if (clearRoutes) {
164 + clearRoutes();
165 + }
130 } 166 }
131 167
132 @Override 168 @Override
...@@ -213,6 +249,15 @@ public class FpmManager implements RouteSourceService, FpmInfoService { ...@@ -213,6 +249,15 @@ public class FpmManager implements RouteSourceService, FpmInfoService {
213 routeListener.update(Collections.singletonList(routeUpdate)); 249 routeListener.update(Collections.singletonList(routeUpdate));
214 } 250 }
215 251
252 +
253 + private void clearRoutes() {
254 + log.info("Clearing all routes");
255 + List<RouteUpdate> routeUpdates = fpmRoutes.values().stream()
256 + .map(routeEntry -> new RouteUpdate(RouteUpdate.Type.DELETE, routeEntry))
257 + .collect(Collectors.toList());
258 + routeListener.update(routeUpdates);
259 + }
260 +
216 @Override 261 @Override
217 public Map<SocketAddress, Long> peers() { 262 public Map<SocketAddress, Long> peers() {
218 return ImmutableMap.copyOf(peers); 263 return ImmutableMap.copyOf(peers);
...@@ -236,6 +281,12 @@ public class FpmManager implements RouteSourceService, FpmInfoService { ...@@ -236,6 +281,12 @@ public class FpmManager implements RouteSourceService, FpmInfoService {
236 281
237 @Override 282 @Override
238 public void peerDisconnected(SocketAddress address) { 283 public void peerDisconnected(SocketAddress address) {
284 + log.info("FPM connection to {} went down", address);
285 +
286 + if (clearRoutes) {
287 + clearRoutes();
288 + }
289 +
239 peers.remove(address); 290 peers.remove(address);
240 } 291 }
241 } 292 }
......
...@@ -91,7 +91,6 @@ public class FpmSessionHandler extends SimpleChannelHandler { ...@@ -91,7 +91,6 @@ public class FpmSessionHandler extends SimpleChannelHandler {
91 @Override 91 @Override
92 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) 92 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
93 throws Exception { 93 throws Exception {
94 - handleDisconnect();
95 } 94 }
96 95
97 private void handleDisconnect() { 96 private void handleDisconnect() {
......