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.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.lisp.sb.config.rev150517.LispSbConfig;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable {
48 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
50 private static Object startLock = new Object();
51 private LispSouthboundHandler lispSouthboundHandler;
52 private LispXtrSouthboundHandler lispXtrSouthboundHandler;
53 private NotificationPublishService notificationPublishService;
54 private NioDatagramChannel channel;
55 private volatile String bindingAddress = "0.0.0.0";
56 private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
57 private volatile boolean listenOnXtrPort = false;
58 private boolean mapRegisterCacheEnabled = true;
59 private RpcRegistration<OdlLispSbService> sbRpcRegistration;
60 private NioDatagramChannel xtrChannel;
61 private LispSouthboundStats statistics = new LispSouthboundStats();
62 private Bootstrap bootstrap = new Bootstrap();
63 private Bootstrap xtrBootstrap = new Bootstrap();
64 private ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
65 private EventLoopGroup eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
66 private DataBroker dataBroker;
68 public LispSouthboundPlugin(final DataBroker dataBroker,
69 final NotificationPublishService notificationPublishService,
70 final LispSbConfig lispSbConfig) {
71 this.dataBroker = dataBroker;
72 this.notificationPublishService = notificationPublishService;
73 this.bindingAddress = lispSbConfig.getBindAddress();
74 this.mapRegisterCacheEnabled = lispSbConfig.isMapRegisterCache();
78 LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
79 synchronized (startLock) {
80 lispSouthboundHandler = new LispSouthboundHandler(this);
81 lispSouthboundHandler.setDataBroker(dataBroker);
82 lispSouthboundHandler.setNotificationProvider(this.notificationPublishService);
83 lispSouthboundHandler.setMapRegisterCacheEnabled(mapRegisterCacheEnabled);
84 lispSouthboundHandler.init();
85 lispSouthboundHandler.restoreDaoFromDatastore();
87 lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
88 lispXtrSouthboundHandler.setNotificationProvider(this.notificationPublishService);
90 bootstrap.group(eventLoopGroup);
91 bootstrap.channel(NioDatagramChannel.class);
92 bootstrap.handler(lispSouthboundHandler);
94 xtrBootstrap.group(eventLoopGroup);
95 xtrBootstrap.channel(NioDatagramChannel.class);
96 xtrBootstrap.handler(lispXtrSouthboundHandler);
101 LOG.info("LISP (RFC6830) Southbound Plugin is up!");
105 private void start() {
107 channel = (NioDatagramChannel) bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
108 LOG.debug("Binding LISP UDP listening socket to {}:{}", bindingAddress, LispMessage.PORT_NUM);
109 } catch (Exception e) {
110 LOG.error("Failed to open main socket ", e);
114 private void startXtr() {
115 if (listenOnXtrPort) {
117 xtrChannel = (NioDatagramChannel) xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
118 LOG.debug("Binding LISP xTR UDP listening socket to {}:{}", bindingAddress, xtrPort);
119 } catch (Exception e) {
120 LOG.error("Failed to open xTR socket ", e);
125 private void stop() {
127 channel.close().sync();
129 } catch (Exception e) {
130 LOG.error("Failed to close main socket ", e);
134 private void stopXtr() {
135 if (listenOnXtrPort) {
137 xtrChannel.close().sync();
139 } catch (Exception e) {
140 LOG.error("Failed to close xTR socket ", e);
145 private void restart() {
146 LOG.info("Reloading");
151 private void restartXtr() {
152 LOG.info("Reloading xTR");
157 private void unloadActions() {
158 lispSouthboundHandler = null;
159 lispXtrSouthboundHandler = null;
164 LOG.info("LISP (RFC6830) Southbound Plugin is down!");
167 public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
168 final MessageType packetType) {
169 InetAddress ip = getInetAddress(address);
170 handleSerializedLispBuffer(ip, outBuffer, packetType, address.getPort().getValue());
173 public void handleSerializedLispBuffer(InetAddress address, ByteBuffer outBuffer,
174 final MessageType packetType, final int portNumber) {
175 InetSocketAddress recipient = new InetSocketAddress(address, portNumber);
176 outBuffer.position(0);
177 ByteBuf data = wrappedBuffer(outBuffer);
178 DatagramPacket packet = new DatagramPacket(data, recipient);
179 LOG.debug("Sending {} on port {} to address: {}", packetType, portNumber, address);
180 if (LOG.isTraceEnabled()) {
181 LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
183 channel.write(packet).addListener(new ChannelFutureListener() {
185 public void operationComplete(ChannelFuture future) {
186 if (future.isSuccess()) {
187 LOG.trace("Success");
188 statistics.incrementTx(packetType.getIntValue());
190 LOG.warn("Failed to send packet");
191 statistics.incrementTxErrors();
198 private InetAddress getInetAddress(TransportAddress address) {
199 Preconditions.checkNotNull(address, "TransportAddress must not be null");
200 IpAddressBinary ip = address.getIpAddress();
202 if (ip.getIpv4AddressBinary() != null) {
203 return InetAddress.getByAddress(ip.getIpv4AddressBinary().getValue());
204 } else if (ip.getIpv6AddressBinary() != null) {
205 return InetAddress.getByAddress(ip.getIpv6AddressBinary().getValue());
207 } catch (UnknownHostException e) {
208 LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
213 public LispSouthboundStats getStats() {
218 public void setLispAddress(String address) {
219 synchronized (startLock) {
220 if (bindingAddress.equals(address)) {
221 LOG.debug("Configured LISP binding address didn't change.");
223 LOG.debug("Setting LISP binding address to {}", address);
224 bindingAddress = address;
225 if (channel != null) {
229 } catch (Exception e) {
230 LOG.error("Failed to set LISP binding address: ", e);
238 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
239 listenOnXtrPort = shouldListenOnXtrPort;
240 if (listenOnXtrPort) {
243 LOG.info("Shutting down xTR");
249 public void setXtrPort(int port) {
251 if (listenOnXtrPort) {
256 public void setMapRegisterCacheEnabled(final boolean mapRegisterCacheEnabled) {
257 this.mapRegisterCacheEnabled = mapRegisterCacheEnabled;
258 if (mapRegisterCacheEnabled) {
259 LOG.info("Enabling Map-Register cache");
261 LOG.info("Disabling Map-Register cache");
266 public void close() throws Exception {
267 eventLoopGroup.shutdownGracefully();
268 sbRpcRegistration.close();
269 lispSouthboundHandler.close();