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.controller.sal.binding.api.RpcProviderRegistry;
36 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
37 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
38 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
39 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
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 RpcProviderRegistry rpcRegistry;
55 private NioDatagramChannel channel;
56 private volatile String bindingAddress = "0.0.0.0";
57 private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
58 private volatile boolean listenOnXtrPort = false;
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;
70 LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
71 final LispSouthboundRPC sbRpcHandler = new LispSouthboundRPC(this);
73 sbRpcRegistration = rpcRegistry.addRpcImplementation(OdlLispSbService.class, sbRpcHandler);
75 synchronized (startLock) {
76 lispSouthboundHandler = new LispSouthboundHandler(this);
77 lispSouthboundHandler.setDataBroker(dataBroker);
78 lispSouthboundHandler.setNotificationProvider(this.notificationPublishService);
79 lispSouthboundHandler.init();
81 lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
82 lispXtrSouthboundHandler.setNotificationProvider(this.notificationPublishService);
84 bootstrap.group(eventLoopGroup);
85 bootstrap.channel(NioDatagramChannel.class);
86 bootstrap.handler(lispSouthboundHandler);
88 xtrBootstrap.group(eventLoopGroup);
89 xtrBootstrap.channel(NioDatagramChannel.class);
90 xtrBootstrap.handler(lispXtrSouthboundHandler);
95 LOG.info("LISP (RFC6830) Southbound Plugin is up!");
99 private void start() {
101 channel = (NioDatagramChannel) bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
102 LOG.debug("Binding LISP UDP listening socket to {}:{}", bindingAddress, LispMessage.PORT_NUM);
103 } catch (Exception e) {
104 LOG.error("Failed to open main socket ", e);
108 private void startXtr() {
109 if (listenOnXtrPort) {
111 xtrChannel = (NioDatagramChannel) xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
112 LOG.debug("Binding LISP xTR UDP listening socket to {}:{}", bindingAddress, xtrPort);
113 } catch (Exception e) {
114 LOG.error("Failed to open xTR socket ", e);
119 private void stop() {
121 channel.close().sync();
123 } catch (Exception e) {
124 LOG.error("Failed to close main socket ", e);
128 private void stopXtr() {
129 if (listenOnXtrPort) {
131 xtrChannel.close().sync();
133 } catch (Exception e) {
134 LOG.error("Failed to close xTR socket ", e);
139 private void restart() {
140 LOG.info("Reloading");
145 private void restartXtr() {
146 LOG.info("Reloading xTR");
151 public void setNotificationPublishService(NotificationPublishService notificationService) {
152 this.notificationPublishService = notificationService;
155 public void setRpcRegistryDependency(RpcProviderRegistry rpcRegistry) {
156 this.rpcRegistry = rpcRegistry;
159 private void unloadActions() {
160 lispSouthboundHandler = null;
161 lispXtrSouthboundHandler = null;
166 LOG.info("LISP (RFC6830) Southbound Plugin is down!");
169 public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
170 final MessageType packetType) {
171 InetAddress ip = getInetAddress(address);
172 handleSerializedLispBuffer(ip, outBuffer, packetType, address.getPort().getValue());
175 public void handleSerializedLispBuffer(InetAddress address, ByteBuffer outBuffer,
176 final MessageType packetType, final int portNumber) {
177 InetSocketAddress recipient = new InetSocketAddress(address, portNumber);
178 outBuffer.position(0);
179 ByteBuf data = wrappedBuffer(outBuffer);
180 DatagramPacket packet = new DatagramPacket(data, recipient);
181 LOG.debug("Sending {} on port {} to address: {}", packetType, portNumber, address);
182 if (LOG.isTraceEnabled()) {
183 LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
185 channel.write(packet).addListener(new ChannelFutureListener() {
187 public void operationComplete(ChannelFuture future) {
188 if (future.isSuccess()) {
189 LOG.trace("Success");
190 statistics.incrementTx(packetType.getIntValue());
192 LOG.warn("Failed to send packet");
193 statistics.incrementTxErrors();
200 private InetAddress getInetAddress(TransportAddress address) {
201 Preconditions.checkNotNull(address, "TransportAddress must not be null");
202 IpAddressBinary ip = address.getIpAddress();
204 if (ip.getIpv4AddressBinary() != null) {
205 return InetAddress.getByAddress(ip.getIpv4AddressBinary().getValue());
206 } else if (ip.getIpv6AddressBinary() != null) {
207 return InetAddress.getByAddress(ip.getIpv6AddressBinary().getValue());
209 } catch (UnknownHostException e) {
210 LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
215 public LispSouthboundStats getStats() {
220 public void setLispAddress(String address) {
221 synchronized (startLock) {
222 if (bindingAddress.equals(address)) {
223 LOG.debug("Configured LISP binding address didn't change.");
225 LOG.debug("Setting LISP binding address to {}", address);
226 bindingAddress = address;
227 if (channel != null) {
231 } catch (Exception e) {
232 LOG.error("Failed to set LISP binding address: ", e);
240 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
241 listenOnXtrPort = shouldListenOnXtrPort;
242 if (listenOnXtrPort) {
245 LOG.info("Shutting down xTR");
251 public void setXtrPort(int port) {
253 if (listenOnXtrPort) {
258 public void setDataBroker(final DataBroker dataBroker) {
259 this.dataBroker = dataBroker;
263 public void close() throws Exception {
265 eventLoopGroup.shutdownGracefully();
266 sbRpcRegistration.close();
267 lispSouthboundHandler.close();