2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
9 package org.opendaylight.lispflowmapping.southbound;
11 import static io.netty.buffer.Unpooled.wrappedBuffer;
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;
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);
64 private volatile String bindingAddress;
65 private SimpleMapCache smc;
66 private MapRegisterCache mapRegisterCache = new MapRegisterCache();
67 private boolean mapRegisterCacheEnabled;
68 private long mapRegisterCacheTimeout;
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;
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()) {
100 channel = new Channel[numChannels];
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);
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();
121 lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
122 lispXtrSouthboundHandler.setNotificationProvider(notificationPublishService);
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");
131 eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
132 channelType = NioDatagramChannel.class;
133 LOG.debug("Using Netty I/O (non-Epoll) for UDP sockets");
136 bootstrap.group(eventLoopGroup);
137 bootstrap.channel(channelType);
138 bootstrap.handler(lispSouthboundHandler);
140 xtrBootstrap.group(eventLoopGroup);
141 xtrBootstrap.channel(channelType);
142 xtrBootstrap.handler(lispXtrSouthboundHandler);
147 LOG.info("LISP (RFC6830) Southbound Plugin is up!");
151 @SuppressWarnings("checkstyle:IllegalCatch")
152 private void start() {
154 for (int i = 0; i < numChannels; ++i) {
155 channel[i] = bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
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);
163 @SuppressWarnings("checkstyle:IllegalCatch")
164 private void startXtr() {
165 if (listenOnXtrPort) {
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);
175 @SuppressWarnings("checkstyle:IllegalCatch")
176 private void stop() {
178 for (int i = 0; i < numChannels; ++i) {
179 channel[i].close().sync();
182 } catch (Exception e) {
183 LOG.error("Failed to close main socket ", e);
187 @SuppressWarnings("checkstyle:IllegalCatch")
188 private void stopXtr() {
189 if (listenOnXtrPort) {
191 xtrChannel.close().sync();
193 } catch (Exception e) {
194 LOG.error("Failed to close xTR socket ", e);
199 private void restart() {
200 LOG.info("Reloading");
205 private void restartXtr() {
206 LOG.info("Reloading xTR");
211 private void unloadActions() {
212 lispSouthboundHandler = null;
213 lispXtrSouthboundHandler = null;
218 LOG.info("LISP (RFC6830) Southbound Plugin is down!");
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);
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];
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));
240 senderChannel.write(packet).addListener(new ChannelFutureListener() {
242 public void operationComplete(ChannelFuture future) {
243 if (future.isSuccess()) {
244 LOG.trace("Success");
245 statistics.incrementTx(packetType.getIntValue());
247 LOG.warn("Failed to send packet");
248 statistics.incrementTxErrors();
252 senderChannel.flush();
255 private InetAddress getInetAddress(TransportAddress address) {
256 Preconditions.checkNotNull(address, "TransportAddress must not be null");
257 IpAddressBinary ip = address.getIpAddress();
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());
264 } catch (UnknownHostException e) {
265 LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
270 public ConcurrentLispSouthboundStats getStats() {
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.");
281 LOG.debug("Setting LISP binding address to {}", address);
282 bindingAddress = address;
283 if (channel != null) {
287 } catch (Exception e) {
288 LOG.error("Failed to set LISP binding address: ", e);
296 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
297 listenOnXtrPort = shouldListenOnXtrPort;
298 if (listenOnXtrPort) {
301 LOG.info("Shutting down xTR");
307 public void setXtrPort(int port) {
309 if (listenOnXtrPort) {
314 public void setMapRegisterCacheEnabled(final boolean mapRegisterCacheEnabled) {
315 this.mapRegisterCacheEnabled = mapRegisterCacheEnabled;
316 if (mapRegisterCacheEnabled) {
317 LOG.info("Enabling Map-Register cache");
319 LOG.info("Disabling Map-Register cache");
323 public void setMapRegisterCacheTimeout(long mapRegisterCacheTimeout) {
324 this.mapRegisterCacheTimeout = mapRegisterCacheTimeout;
327 public void setBindingAddress(String bindingAddress) {
328 this.bindingAddress = bindingAddress;
332 public void close() throws Exception {
333 eventLoopGroup.shutdownGracefully();
334 lispSouthboundHandler.close();
336 clusterSingletonService.close();
340 public void instantiateServiceInstance() {
341 if (lispSouthboundHandler != null) {
342 lispSouthboundHandler.setNotificationProvider(notificationPublishService);
343 lispSouthboundHandler.restoreDaoFromDatastore();
344 lispSouthboundHandler.setIsMaster(true);
346 if (lispXtrSouthboundHandler != null) {
347 lispXtrSouthboundHandler.setNotificationProvider(notificationPublishService);
352 public ListenableFuture<Void> closeServiceInstance() {
353 if (lispSouthboundHandler != null) {
354 lispSouthboundHandler.setNotificationProvider(null);
355 lispSouthboundHandler.setIsMaster(false);
357 if (lispXtrSouthboundHandler != null) {
358 lispXtrSouthboundHandler.setNotificationProvider(null);
360 return Futures.<Void>immediateFuture(null);
364 public ServiceGroupIdentifier getIdentifier() {
365 return SERVICE_GROUP_IDENTIFIER;
368 public MapRegisterCache getMapRegisterCache() {
369 return mapRegisterCache;
372 public boolean isMapRegisterCacheEnabled() {
373 return mapRegisterCacheEnabled;
376 public long getMapRegisterCacheTimeout() {
377 return mapRegisterCacheTimeout;