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;
15 import io.netty.bootstrap.Bootstrap;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.ByteBufUtil;
18 import io.netty.channel.ChannelFuture;
19 import io.netty.channel.ChannelFutureListener;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.nio.NioEventLoopGroup;
22 import io.netty.channel.socket.DatagramPacket;
23 import io.netty.channel.socket.nio.NioDatagramChannel;
24 import io.netty.util.concurrent.DefaultThreadFactory;
26 import java.net.InetAddress;
27 import java.net.InetSocketAddress;
28 import java.net.UnknownHostException;
29 import java.nio.ByteBuffer;
30 import java.util.concurrent.ThreadFactory;
32 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
33 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
34 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
35 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
36 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
37 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
38 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable {
47 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
49 private volatile String bindingAddress;
50 private boolean mapRegisterCacheEnabled;
51 private long mapRegisterCacheTimeout;
53 private static Object startLock = new Object();
54 private LispSouthboundHandler lispSouthboundHandler;
55 private LispXtrSouthboundHandler lispXtrSouthboundHandler;
56 private NotificationPublishService notificationPublishService;
57 private NioDatagramChannel channel;
58 private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
59 private volatile boolean listenOnXtrPort = false;
60 private RpcRegistration<OdlLispSbService> sbRpcRegistration;
61 private NioDatagramChannel xtrChannel;
62 private LispSouthboundStats statistics = new LispSouthboundStats();
63 private Bootstrap bootstrap = new Bootstrap();
64 private Bootstrap xtrBootstrap = new Bootstrap();
65 private ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
66 private EventLoopGroup eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
67 private DataBroker dataBroker;
69 public LispSouthboundPlugin(final DataBroker dataBroker,
70 final NotificationPublishService notificationPublishService) {
71 this.dataBroker = dataBroker;
72 this.notificationPublishService = notificationPublishService;
76 LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
77 synchronized (startLock) {
78 lispSouthboundHandler = new LispSouthboundHandler(this);
79 lispSouthboundHandler.setDataBroker(dataBroker);
80 lispSouthboundHandler.setNotificationProvider(this.notificationPublishService);
81 lispSouthboundHandler.setMapRegisterCacheEnabled(mapRegisterCacheEnabled);
82 lispSouthboundHandler.setMapRegisterCacheTimeout(mapRegisterCacheTimeout);
83 lispSouthboundHandler.init();
84 lispSouthboundHandler.restoreDaoFromDatastore();
86 lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
87 lispXtrSouthboundHandler.setNotificationProvider(this.notificationPublishService);
89 bootstrap.group(eventLoopGroup);
90 bootstrap.channel(NioDatagramChannel.class);
91 bootstrap.handler(lispSouthboundHandler);
93 xtrBootstrap.group(eventLoopGroup);
94 xtrBootstrap.channel(NioDatagramChannel.class);
95 xtrBootstrap.handler(lispXtrSouthboundHandler);
100 LOG.info("LISP (RFC6830) Southbound Plugin is up!");
104 private void start() {
106 channel = (NioDatagramChannel) bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
107 LOG.debug("Binding LISP UDP listening socket to {}:{}", bindingAddress, LispMessage.PORT_NUM);
108 } catch (Exception e) {
109 LOG.error("Failed to open main socket ", e);
113 private void startXtr() {
114 if (listenOnXtrPort) {
116 xtrChannel = (NioDatagramChannel) xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
117 LOG.debug("Binding LISP xTR UDP listening socket to {}:{}", bindingAddress, xtrPort);
118 } catch (Exception e) {
119 LOG.error("Failed to open xTR socket ", e);
124 private void stop() {
126 channel.close().sync();
128 } catch (Exception e) {
129 LOG.error("Failed to close main socket ", e);
133 private void stopXtr() {
134 if (listenOnXtrPort) {
136 xtrChannel.close().sync();
138 } catch (Exception e) {
139 LOG.error("Failed to close xTR socket ", e);
144 private void restart() {
145 LOG.info("Reloading");
150 private void restartXtr() {
151 LOG.info("Reloading xTR");
156 private void unloadActions() {
157 lispSouthboundHandler = null;
158 lispXtrSouthboundHandler = null;
163 LOG.info("LISP (RFC6830) Southbound Plugin is down!");
166 public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
167 final MessageType packetType) {
168 InetAddress ip = getInetAddress(address);
169 handleSerializedLispBuffer(ip, outBuffer, packetType, address.getPort().getValue());
172 public void handleSerializedLispBuffer(InetAddress address, ByteBuffer outBuffer,
173 final MessageType packetType, final int portNumber) {
174 InetSocketAddress recipient = new InetSocketAddress(address, portNumber);
175 outBuffer.position(0);
176 ByteBuf data = wrappedBuffer(outBuffer);
177 DatagramPacket packet = new DatagramPacket(data, recipient);
178 LOG.debug("Sending {} on port {} to address: {}", packetType, portNumber, address);
179 if (LOG.isTraceEnabled()) {
180 LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
182 channel.write(packet).addListener(new ChannelFutureListener() {
184 public void operationComplete(ChannelFuture future) {
185 if (future.isSuccess()) {
186 LOG.trace("Success");
187 statistics.incrementTx(packetType.getIntValue());
189 LOG.warn("Failed to send packet");
190 statistics.incrementTxErrors();
197 private InetAddress getInetAddress(TransportAddress address) {
198 Preconditions.checkNotNull(address, "TransportAddress must not be null");
199 IpAddressBinary ip = address.getIpAddress();
201 if (ip.getIpv4AddressBinary() != null) {
202 return InetAddress.getByAddress(ip.getIpv4AddressBinary().getValue());
203 } else if (ip.getIpv6AddressBinary() != null) {
204 return InetAddress.getByAddress(ip.getIpv6AddressBinary().getValue());
206 } catch (UnknownHostException e) {
207 LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
212 public LispSouthboundStats getStats() {
217 public void setLispAddress(String address) {
218 synchronized (startLock) {
219 if (bindingAddress.equals(address)) {
220 LOG.debug("Configured LISP binding address didn't change.");
222 LOG.debug("Setting LISP binding address to {}", address);
223 bindingAddress = address;
224 if (channel != null) {
228 } catch (Exception e) {
229 LOG.error("Failed to set LISP binding address: ", e);
237 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
238 listenOnXtrPort = shouldListenOnXtrPort;
239 if (listenOnXtrPort) {
242 LOG.info("Shutting down xTR");
248 public void setXtrPort(int port) {
250 if (listenOnXtrPort) {
255 public void setMapRegisterCacheEnabled(final boolean mapRegisterCacheEnabled) {
256 this.mapRegisterCacheEnabled = mapRegisterCacheEnabled;
257 if (mapRegisterCacheEnabled) {
258 LOG.info("Enabling Map-Register cache");
260 LOG.info("Disabling Map-Register cache");
264 public void setMapRegisterCacheTimeout(long mapRegisterCacheTimeout) {
265 this.mapRegisterCacheTimeout = mapRegisterCacheTimeout;
268 public void setBindingAddress(String bindingAddress) {
269 this.bindingAddress = bindingAddress;
273 public void close() throws Exception {
274 eventLoopGroup.shutdownGracefully();
275 sbRpcRegistration.close();
276 lispSouthboundHandler.close();