Make Netty multi-threaded in southbound
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / LispSouthboundPlugin.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.lispflowmapping.southbound;
10
11 import static io.netty.buffer.Unpooled.wrappedBuffer;
12
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import io.netty.bootstrap.Bootstrap;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.ByteBufUtil;
19 import io.netty.buffer.PooledByteBufAllocator;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelFuture;
22 import io.netty.channel.ChannelFutureListener;
23 import io.netty.channel.ChannelOption;
24 import io.netty.channel.EventLoopGroup;
25 import io.netty.channel.epoll.Epoll;
26 import io.netty.channel.epoll.EpollChannelOption;
27 import io.netty.channel.epoll.EpollDatagramChannel;
28 import io.netty.channel.epoll.EpollEventLoopGroup;
29 import io.netty.channel.nio.NioEventLoopGroup;
30 import io.netty.channel.socket.DatagramPacket;
31 import io.netty.channel.socket.nio.NioDatagramChannel;
32 import io.netty.util.concurrent.DefaultThreadFactory;
33 import java.net.InetAddress;
34 import java.net.InetSocketAddress;
35 import java.net.UnknownHostException;
36 import java.nio.ByteBuffer;
37 import java.util.concurrent.ThreadFactory;
38 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
39 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
40 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
41 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
42 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
43 import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
44 import org.opendaylight.lispflowmapping.southbound.lisp.AuthenticationKeyDataListener;
45 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
46 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
47 import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterCache;
48 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
49 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
50 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
51 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57
58 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable, ClusterSingletonService {
59     protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
60     public static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
61     public static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
62             LISPFLOWMAPPING_ENTITY_NAME);
63
64     private volatile String bindingAddress;
65     private SimpleMapCache smc;
66     private MapRegisterCache mapRegisterCache = new MapRegisterCache();
67     private boolean mapRegisterCacheEnabled;
68     private long mapRegisterCacheTimeout;
69
70     private static Object startLock = new Object();
71     private final ClusterSingletonServiceProvider clusterSingletonService;
72     private LispSouthboundHandler lispSouthboundHandler;
73     private LispXtrSouthboundHandler lispXtrSouthboundHandler;
74     private NotificationPublishService notificationPublishService;
75     private int numChannels = 1;
76     private Channel[] channel;
77     private Channel xtrChannel;
78     private Class channelType;
79     private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
80     private volatile boolean listenOnXtrPort = false;
81     private ConcurrentLispSouthboundStats statistics = new ConcurrentLispSouthboundStats();
82     private Bootstrap bootstrap = new Bootstrap();
83     private Bootstrap xtrBootstrap = new Bootstrap();
84     private ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
85     private EventLoopGroup eventLoopGroup;
86     private DataBroker dataBroker;
87     private AuthenticationKeyDataListener authenticationKeyDataListener;
88     private DataStoreBackEnd dsbe;
89
90     public LispSouthboundPlugin(final DataBroker dataBroker,
91             final NotificationPublishService notificationPublishService,
92             final ClusterSingletonServiceProvider clusterSingletonService) {
93         this.dataBroker = dataBroker;
94         this.notificationPublishService = notificationPublishService;
95         this.clusterSingletonService = clusterSingletonService;
96         this.clusterSingletonService.registerClusterSingletonService(this);
97         if (Epoll.isAvailable()) {
98             numChannels = 5;
99         }
100         channel = new Channel[numChannels];
101     }
102
103     public void init() {
104         LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
105         synchronized (startLock) {
106             this.smc = new SimpleMapCache(new HashMapDb());
107             this.authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBroker, smc);
108             this.dsbe = new DataStoreBackEnd(dataBroker);
109
110             lispSouthboundHandler = new LispSouthboundHandler(this);
111             lispSouthboundHandler.setDataBroker(dataBroker);
112             lispSouthboundHandler.setNotificationProvider(notificationPublishService);
113             lispSouthboundHandler.setSimpleMapCache(smc);
114             lispSouthboundHandler.setMapRegisterCache(mapRegisterCache);
115             lispSouthboundHandler.setMapRegisterCacheTimeout(mapRegisterCacheTimeout);
116             lispSouthboundHandler.setAuthenticationKeyDataListener(authenticationKeyDataListener);
117             lispSouthboundHandler.setDataStoreBackEnd(dsbe);
118             lispSouthboundHandler.setStats(statistics);
119             lispSouthboundHandler.restoreDaoFromDatastore();
120
121             lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
122             lispXtrSouthboundHandler.setNotificationProvider(notificationPublishService);
123
124             if (Epoll.isAvailable()) {
125                 eventLoopGroup = new EpollEventLoopGroup(numChannels, threadFactory);
126                 channelType = EpollDatagramChannel.class;
127                 bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
128                 bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
129                 LOG.debug("Using Netty Epoll for UDP sockets");
130             } else {
131                 eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
132                 channelType = NioDatagramChannel.class;
133                 LOG.debug("Using Netty I/O (non-Epoll) for UDP sockets");
134             }
135
136             bootstrap.group(eventLoopGroup);
137             bootstrap.channel(channelType);
138             bootstrap.handler(lispSouthboundHandler);
139
140             xtrBootstrap.group(eventLoopGroup);
141             xtrBootstrap.channel(channelType);
142             xtrBootstrap.handler(lispXtrSouthboundHandler);
143
144             start();
145             startXtr();
146
147             LOG.info("LISP (RFC6830) Southbound Plugin is up!");
148         }
149     }
150
151     @SuppressWarnings("checkstyle:IllegalCatch")
152     private void start() {
153         try {
154             for (int i = 0; i < numChannels; ++i) {
155                 channel[i] = bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
156             }
157             LOG.debug("Binding LISP UDP listening socket to {}:{}", bindingAddress, LispMessage.PORT_NUM);
158         } catch (Exception e) {
159             LOG.error("Failed to open main socket ", e);
160         }
161     }
162
163     @SuppressWarnings("checkstyle:IllegalCatch")
164     private void startXtr() {
165         if (listenOnXtrPort) {
166             try {
167                 xtrChannel = xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
168                 LOG.debug("Binding LISP xTR UDP listening socket to {}:{}", bindingAddress, xtrPort);
169             } catch (Exception e) {
170                 LOG.error("Failed to open xTR socket ", e);
171             }
172         }
173     }
174
175     @SuppressWarnings("checkstyle:IllegalCatch")
176     private void stop() {
177         try {
178             for (int i = 0; i < numChannels; ++i) {
179                 channel[i].close().sync();
180                 channel[i] = null;
181             }
182         } catch (Exception e) {
183             LOG.error("Failed to close main socket ", e);
184         }
185     }
186
187     @SuppressWarnings("checkstyle:IllegalCatch")
188     private void stopXtr() {
189         if (listenOnXtrPort) {
190             try {
191                 xtrChannel.close().sync();
192                 xtrChannel = null;
193             } catch (Exception e) {
194                 LOG.error("Failed to close xTR socket ", e);
195             }
196         }
197     }
198
199     private void restart() {
200         LOG.info("Reloading");
201         stop();
202         start();
203     }
204
205     private void restartXtr() {
206         LOG.info("Reloading xTR");
207         stopXtr();
208         startXtr();
209     }
210
211     private void unloadActions() {
212         lispSouthboundHandler = null;
213         lispXtrSouthboundHandler = null;
214
215         stop();
216         stopXtr();
217
218         LOG.info("LISP (RFC6830) Southbound Plugin is down!");
219     }
220
221     public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
222                                            final MessageType packetType) {
223         InetAddress ip = getInetAddress(address);
224         handleSerializedLispBuffer(ip, outBuffer, packetType, address.getPort().getValue(), null);
225     }
226
227     public void handleSerializedLispBuffer(InetAddress address, ByteBuffer outBuffer,
228             final MessageType packetType, final int portNumber, Channel senderChannel) {
229         if (senderChannel == null) {
230             senderChannel = this.channel[0];
231         }
232         InetSocketAddress recipient = new InetSocketAddress(address, portNumber);
233         outBuffer.position(0);
234         ByteBuf data = wrappedBuffer(outBuffer);
235         DatagramPacket packet = new DatagramPacket(data, recipient);
236         LOG.debug("Sending {} on port {} to address: {}", packetType, portNumber, address);
237         if (LOG.isTraceEnabled()) {
238             LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
239         }
240         senderChannel.write(packet).addListener(new ChannelFutureListener() {
241             @Override
242             public void operationComplete(ChannelFuture future) {
243                 if (future.isSuccess()) {
244                     LOG.trace("Success");
245                     statistics.incrementTx(packetType.getIntValue());
246                 } else {
247                     LOG.warn("Failed to send packet");
248                     statistics.incrementTxErrors();
249                 }
250             }
251         });
252         senderChannel.flush();
253     }
254
255     private InetAddress getInetAddress(TransportAddress address) {
256         Preconditions.checkNotNull(address, "TransportAddress must not be null");
257         IpAddressBinary ip = address.getIpAddress();
258         try {
259             if (ip.getIpv4AddressBinary() != null) {
260                 return InetAddress.getByAddress(ip.getIpv4AddressBinary().getValue());
261             } else if (ip.getIpv6AddressBinary() != null) {
262                 return InetAddress.getByAddress(ip.getIpv6AddressBinary().getValue());
263             }
264         } catch (UnknownHostException e) {
265             LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
266         }
267         return null;
268     }
269
270     public ConcurrentLispSouthboundStats getStats() {
271         return statistics;
272     }
273
274     @Override
275     @SuppressWarnings("checkstyle:IllegalCatch")
276     public void setLispAddress(String address) {
277         synchronized (startLock) {
278             if (bindingAddress.equals(address)) {
279                 LOG.debug("Configured LISP binding address didn't change.");
280             } else {
281                 LOG.debug("Setting LISP binding address to {}", address);
282                 bindingAddress = address;
283                 if (channel != null) {
284                     try {
285                         restart();
286                         restartXtr();
287                     } catch (Exception e) {
288                         LOG.error("Failed to set LISP binding address: ", e);
289                     }
290                 }
291             }
292         }
293     }
294
295     @Override
296     public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
297         listenOnXtrPort = shouldListenOnXtrPort;
298         if (listenOnXtrPort) {
299             restartXtr();
300         } else {
301             LOG.info("Shutting down xTR");
302             stopXtr();
303         }
304     }
305
306     @Override
307     public void setXtrPort(int port) {
308         this.xtrPort = port;
309         if (listenOnXtrPort) {
310             restartXtr();
311         }
312     }
313
314     public void setMapRegisterCacheEnabled(final boolean mapRegisterCacheEnabled) {
315         this.mapRegisterCacheEnabled = mapRegisterCacheEnabled;
316         if (mapRegisterCacheEnabled) {
317             LOG.info("Enabling Map-Register cache");
318         } else {
319             LOG.info("Disabling Map-Register cache");
320         }
321     }
322
323     public void setMapRegisterCacheTimeout(long mapRegisterCacheTimeout) {
324         this.mapRegisterCacheTimeout = mapRegisterCacheTimeout;
325     }
326
327     public void setBindingAddress(String bindingAddress) {
328         this.bindingAddress = bindingAddress;
329     }
330
331     @Override
332     public void close() throws Exception {
333         eventLoopGroup.shutdownGracefully();
334         lispSouthboundHandler.close();
335         unloadActions();
336         clusterSingletonService.close();
337     }
338
339     @Override
340     public void instantiateServiceInstance() {
341         if (lispSouthboundHandler != null) {
342             lispSouthboundHandler.setNotificationProvider(notificationPublishService);
343             lispSouthboundHandler.restoreDaoFromDatastore();
344             lispSouthboundHandler.setIsMaster(true);
345         }
346         if (lispXtrSouthboundHandler != null) {
347             lispXtrSouthboundHandler.setNotificationProvider(notificationPublishService);
348         }
349     }
350
351     @Override
352     public ListenableFuture<Void> closeServiceInstance() {
353         if (lispSouthboundHandler != null) {
354             lispSouthboundHandler.setNotificationProvider(null);
355             lispSouthboundHandler.setIsMaster(false);
356         }
357         if (lispXtrSouthboundHandler != null) {
358             lispXtrSouthboundHandler.setNotificationProvider(null);
359         }
360         return Futures.<Void>immediateFuture(null);
361     }
362
363     @Override
364     public ServiceGroupIdentifier getIdentifier() {
365         return SERVICE_GROUP_IDENTIFIER;
366     }
367
368     public MapRegisterCache getMapRegisterCache() {
369         return mapRegisterCache;
370     }
371
372     public boolean isMapRegisterCacheEnabled() {
373         return mapRegisterCacheEnabled;
374     }
375
376     public long getMapRegisterCacheTimeout() {
377         return mapRegisterCacheTimeout;
378     }
379 }