Migrate to MD-SAL APIs
[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.ChannelOption;
22 import io.netty.channel.EventLoopGroup;
23 import io.netty.channel.epoll.Epoll;
24 import io.netty.channel.epoll.EpollChannelOption;
25 import io.netty.channel.epoll.EpollDatagramChannel;
26 import io.netty.channel.epoll.EpollEventLoopGroup;
27 import io.netty.channel.nio.NioEventLoopGroup;
28 import io.netty.channel.socket.DatagramPacket;
29 import io.netty.channel.socket.nio.NioDatagramChannel;
30 import io.netty.util.concurrent.DefaultThreadFactory;
31 import java.net.InetAddress;
32 import java.net.InetSocketAddress;
33 import java.net.UnknownHostException;
34 import java.nio.ByteBuffer;
35 import java.util.List;
36 import java.util.concurrent.ThreadFactory;
37 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
38 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
39 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
40 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
41 import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
42 import org.opendaylight.lispflowmapping.southbound.lisp.AuthenticationKeyDataListener;
43 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
44 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
45 import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterCache;
46 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
47 import org.opendaylight.mdsal.binding.api.DataBroker;
48 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
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.eid.container.Eid;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
58 import org.opendaylight.yangtools.yang.binding.Notification;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61
62 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable, ClusterSingletonService {
63     protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
64     public static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
65     public static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
66             LISPFLOWMAPPING_ENTITY_NAME);
67
68     private volatile boolean isMaster = false;
69     private volatile String bindingAddress;
70     private AuthKeyDb akdb;
71     private final MapRegisterCache mapRegisterCache = new MapRegisterCache();
72     private boolean mapRegisterCacheEnabled;
73     private long mapRegisterCacheTimeout;
74
75     private static Object startLock = new Object();
76     private final ClusterSingletonServiceProvider clusterSingletonService;
77     private LispSouthboundHandler lispSouthboundHandler;
78     private LispXtrSouthboundHandler lispXtrSouthboundHandler;
79     private final NotificationPublishService notificationPublishService;
80     private int numChannels = 1;
81     private final Channel[] channel;
82     private Channel xtrChannel;
83     private Class channelType;
84     private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
85     private volatile boolean listenOnXtrPort = false;
86     private final ConcurrentLispSouthboundStats statistics = new ConcurrentLispSouthboundStats();
87     private final Bootstrap bootstrap = new Bootstrap();
88     private final Bootstrap xtrBootstrap = new Bootstrap();
89     private final ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
90     private EventLoopGroup eventLoopGroup;
91     private final DataBroker dataBroker;
92     private AuthenticationKeyDataListener authenticationKeyDataListener;
93     private DataStoreBackEnd dsbe;
94
95     public LispSouthboundPlugin(final DataBroker dataBroker,
96             final NotificationPublishService notificationPublishService,
97             final ClusterSingletonServiceProvider clusterSingletonService) {
98         this.dataBroker = dataBroker;
99         this.notificationPublishService = notificationPublishService;
100         this.clusterSingletonService = clusterSingletonService;
101         if (Epoll.isAvailable()) {
102             // When lispflowmapping is under heavy load, there are usually two threads nearing 100% CPU core
103             // utilization. In order to have some headroom, we reserve 3 cores for "other" tasks, and allow the
104             // rest to be used for southbound packet processing, which is the most CPU intensive work done in lfm
105             numChannels = Math.max(1, Runtime.getRuntime().availableProcessors() - 3);
106         }
107         channel = new Channel[numChannels];
108     }
109
110     public void init() {
111         LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
112         synchronized (startLock) {
113             this.akdb = new AuthKeyDb(new HashMapDb());
114             this.authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBroker, akdb);
115             this.dsbe = new DataStoreBackEnd(dataBroker);
116             restoreDaoFromDatastore();
117
118             LispSouthboundHandler lsbh = new LispSouthboundHandler(this);
119             this.lispSouthboundHandler = lsbh;
120
121             LispXtrSouthboundHandler lxsbh = new LispXtrSouthboundHandler(this);
122             this.lispXtrSouthboundHandler = lxsbh;
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(lsbh);
139
140             xtrBootstrap.group(eventLoopGroup);
141             xtrBootstrap.channel(channelType);
142             xtrBootstrap.handler(lxsbh);
143
144             start();
145             startXtr();
146
147             clusterSingletonService.registerClusterSingletonService(this);
148             LOG.info("LISP (RFC6830) Southbound Plugin is up!");
149         }
150     }
151
152     @SuppressWarnings("checkstyle:IllegalCatch")
153     private void start() {
154         try {
155             for (int i = 0; i < numChannels; ++i) {
156                 channel[i] = bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
157             }
158             LOG.debug("Binding LISP UDP listening socket to {}:{}", bindingAddress, LispMessage.PORT_NUM);
159         } catch (Exception e) {
160             LOG.error("Failed to open main socket ", e);
161         }
162     }
163
164     @SuppressWarnings("checkstyle:IllegalCatch")
165     private void startXtr() {
166         if (listenOnXtrPort) {
167             try {
168                 xtrChannel = xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
169                 LOG.debug("Binding LISP xTR UDP listening socket to {}:{}", bindingAddress, xtrPort);
170             } catch (Exception e) {
171                 LOG.error("Failed to open xTR socket ", e);
172             }
173         }
174     }
175
176     @SuppressWarnings("checkstyle:IllegalCatch")
177     private void stop() {
178         try {
179             for (int i = 0; i < numChannels; ++i) {
180                 channel[i].close().sync();
181                 channel[i] = null;
182             }
183         } catch (Exception e) {
184             LOG.error("Failed to close main socket ", e);
185         }
186     }
187
188     @SuppressWarnings("checkstyle:IllegalCatch")
189     private void stopXtr() {
190         if (listenOnXtrPort) {
191             try {
192                 xtrChannel.close().sync();
193                 xtrChannel = null;
194             } catch (Exception e) {
195                 LOG.error("Failed to close xTR socket ", e);
196             }
197         }
198     }
199
200     private void restart() {
201         LOG.info("Reloading");
202         stop();
203         start();
204     }
205
206     private void restartXtr() {
207         LOG.info("Reloading xTR");
208         stopXtr();
209         startXtr();
210     }
211
212     private void unloadActions() {
213         lispSouthboundHandler = null;
214         lispXtrSouthboundHandler = null;
215
216         stop();
217         stopXtr();
218
219         LOG.info("LISP (RFC6830) Southbound Plugin is down!");
220     }
221
222     /**
223      * Restore all keys from MDSAL datastore.
224      */
225     public void restoreDaoFromDatastore() {
226         final List<AuthenticationKey> authKeys = dsbe.getAllAuthenticationKeys();
227         LOG.info("Restoring {} keys from datastore into southbound DAO", authKeys.size());
228
229         for (AuthenticationKey authKey : authKeys) {
230             final Eid key = authKey.getEid();
231             final MappingAuthkey mappingAuthkey = authKey.getMappingAuthkey();
232             LOG.debug("Adding authentication key '{}' with key-ID {} for {}", mappingAuthkey.getKeyString(),
233                     mappingAuthkey.getKeyType(),
234                     LispAddressStringifier.getString(key));
235             akdb.addAuthenticationKey(key, mappingAuthkey);
236         }
237     }
238
239     public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
240                                            final MessageType packetType) {
241         InetAddress ip = getInetAddress(address);
242         handleSerializedLispBuffer(ip, outBuffer, packetType, address.getPort().getValue(), null);
243     }
244
245     public void handleSerializedLispBuffer(InetAddress address, ByteBuffer outBuffer,
246             final MessageType packetType, final int portNumber, Channel senderChannel) {
247         if (senderChannel == null) {
248             senderChannel = this.channel[0];
249         }
250         InetSocketAddress recipient = new InetSocketAddress(address, portNumber);
251         outBuffer.position(0);
252         ByteBuf data = wrappedBuffer(outBuffer);
253         DatagramPacket packet = new DatagramPacket(data, recipient);
254         LOG.debug("Sending {} on port {} to address: {}", packetType, portNumber, address);
255         if (LOG.isTraceEnabled()) {
256             LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
257         }
258         senderChannel.write(packet).addListener(future -> {
259             if (future.isSuccess()) {
260                 LOG.trace("Success");
261                 statistics.incrementTx(packetType.getIntValue());
262             } else {
263                 LOG.warn("Failed to send packet");
264                 statistics.incrementTxErrors();
265             }
266         });
267         senderChannel.flush();
268     }
269
270     private InetAddress getInetAddress(TransportAddress address) {
271         Preconditions.checkNotNull(address, "TransportAddress must not be null");
272         IpAddressBinary ip = address.getIpAddress();
273         try {
274             if (ip.getIpv4AddressBinary() != null) {
275                 return InetAddress.getByAddress(ip.getIpv4AddressBinary().getValue());
276             } else if (ip.getIpv6AddressBinary() != null) {
277                 return InetAddress.getByAddress(ip.getIpv6AddressBinary().getValue());
278             }
279         } catch (UnknownHostException e) {
280             LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
281         }
282         return null;
283     }
284
285     @Override
286     @SuppressWarnings("checkstyle:IllegalCatch")
287     public void setLispAddress(String address) {
288         synchronized (startLock) {
289             if (bindingAddress.equals(address)) {
290                 LOG.debug("Configured LISP binding address didn't change.");
291             } else {
292                 LOG.debug("Setting LISP binding address to {}", address);
293                 bindingAddress = address;
294                 if (channel != null) {
295                     try {
296                         restart();
297                         restartXtr();
298                     } catch (Exception e) {
299                         LOG.error("Failed to set LISP binding address: ", e);
300                     }
301                 }
302             }
303         }
304     }
305
306     @Override
307     public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
308         listenOnXtrPort = shouldListenOnXtrPort;
309         if (listenOnXtrPort) {
310             restartXtr();
311         } else {
312             LOG.info("Shutting down xTR");
313             stopXtr();
314         }
315     }
316
317     @Override
318     public void setXtrPort(int port) {
319         this.xtrPort = port;
320         if (listenOnXtrPort) {
321             restartXtr();
322         }
323     }
324
325     public void setMapRegisterCacheEnabled(final boolean mapRegisterCacheEnabled) {
326         this.mapRegisterCacheEnabled = mapRegisterCacheEnabled;
327         if (mapRegisterCacheEnabled) {
328             LOG.info("Enabling Map-Register cache");
329         } else {
330             LOG.info("Disabling Map-Register cache");
331         }
332     }
333
334     public void setMapRegisterCacheTimeout(long mapRegisterCacheTimeout) {
335         this.mapRegisterCacheTimeout = mapRegisterCacheTimeout;
336     }
337
338     public void setBindingAddress(String bindingAddress) {
339         this.bindingAddress = bindingAddress;
340     }
341
342     @Override
343     public void close() throws Exception {
344         eventLoopGroup.shutdownGracefully();
345         lispSouthboundHandler.close();
346         unloadActions();
347         clusterSingletonService.close();
348         dsbe.closeTransactionChain();
349     }
350
351     @Override
352     public void instantiateServiceInstance() {
353         this.isMaster = true;
354     }
355
356     @Override
357     public ListenableFuture<Void> closeServiceInstance() {
358         this.isMaster = false;
359         return Futures.<Void>immediateFuture(null);
360     }
361
362     @Override
363     public ServiceGroupIdentifier getIdentifier() {
364         return SERVICE_GROUP_IDENTIFIER;
365     }
366
367     public synchronized void sendNotificationIfPossible(final Notification notification) throws InterruptedException {
368         if (isMaster && notificationPublishService != null) {
369             notificationPublishService.putNotification(notification);
370             LOG.trace("Publishing notification: {}", notification);
371         } else if (notificationPublishService == null) {
372             LOG.warn("Can't publish notification because no reference to publication service exists!");
373         }
374     }
375
376     public AuthKeyDb getAkdb() {
377         return akdb;
378     }
379
380     public ConcurrentLispSouthboundStats getStats() {
381         return statistics;
382     }
383
384     public DataBroker getDataBroker() {
385         return dataBroker;
386     }
387
388     public AuthenticationKeyDataListener getAuthenticationKeyDataListener() {
389         return authenticationKeyDataListener;
390     }
391
392     public MapRegisterCache getMapRegisterCache() {
393         return mapRegisterCache;
394     }
395
396     public boolean isMapRegisterCacheEnabled() {
397         return mapRegisterCacheEnabled;
398     }
399
400     public long getMapRegisterCacheTimeout() {
401         return mapRegisterCacheTimeout;
402     }
403 }