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;
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufUtil;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.ChannelFutureListener;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.channel.socket.DatagramPacket;
20 import io.netty.channel.socket.nio.NioDatagramChannel;
21 import io.netty.util.concurrent.DefaultThreadFactory;
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.nio.ByteBuffer;
26 import java.util.concurrent.ThreadFactory;
27 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
28 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
29 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
30 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
31 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
32 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
33 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.net.InetAddresses;
42 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable {
43 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
45 private static Object startLock = new Object();
46 private LispSouthboundHandler lispSouthboundHandler;
47 private LispXtrSouthboundHandler lispXtrSouthboundHandler;
48 private NotificationPublishService notificationPublishService;
49 private RpcProviderRegistry rpcRegistry;
50 private NioDatagramChannel channel;
51 private volatile String bindingAddress = "0.0.0.0";
52 private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
53 private volatile boolean listenOnXtrPort = false;
54 private RpcRegistration<OdlLispSbService> sbRpcRegistration;
55 private NioDatagramChannel xtrChannel;
56 private LispSouthboundStats statistics = new LispSouthboundStats();
57 private ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
58 private EventLoopGroup eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
62 LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
63 final LispSouthboundRPC sbRpcHandler = new LispSouthboundRPC(this);
65 sbRpcRegistration = rpcRegistry.addRpcImplementation(OdlLispSbService.class, sbRpcHandler);
67 synchronized (startLock) {
68 lispSouthboundHandler = new LispSouthboundHandler(this);
69 lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
70 lispSouthboundHandler.setNotificationProvider(this.notificationPublishService);
71 lispXtrSouthboundHandler.setNotificationProvider(this.notificationPublishService);
76 LOG.info("LISP (RFC6830) Southbound Plugin is up!");
80 private void start() {
82 Bootstrap bootstrap = new Bootstrap();
83 bootstrap.group(eventLoopGroup);
84 bootstrap.channel(NioDatagramChannel.class);
85 bootstrap.handler(lispSouthboundHandler);
86 channel = (NioDatagramChannel) bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
87 } catch (Exception e) {
88 LOG.error("Failed to open main socket ", e);
92 private void startXtr() {
93 if (listenOnXtrPort) {
95 Bootstrap xtrBootstrap = new Bootstrap();
96 xtrBootstrap.group(eventLoopGroup);
97 xtrBootstrap.channel(NioDatagramChannel.class);
98 xtrBootstrap.handler(lispXtrSouthboundHandler);
99 xtrChannel = (NioDatagramChannel) xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
100 } catch (Exception e) {
101 LOG.error("Failed to open xTR socket ", e);
106 private void stop() {
108 channel.close().sync();
110 } catch (Exception e) {
111 LOG.error("Failed to close main socket ", e);
115 private void stopXtr() {
116 if (listenOnXtrPort) {
118 xtrChannel.close().sync();
120 } catch (Exception e) {
121 LOG.error("Failed to close xTR socket ", e);
126 private void restart() {
127 LOG.info("Reloading");
132 private void restartXtr() {
133 LOG.info("Reloading xTR");
138 public void setNotificationPublishService(NotificationPublishService notificationService) {
139 this.notificationPublishService = notificationService;
142 public void setRpcRegistryDependency(RpcProviderRegistry rpcRegistry) {
143 this.rpcRegistry = rpcRegistry;
146 private void unloadActions() {
147 lispSouthboundHandler = null;
148 lispXtrSouthboundHandler = null;
149 bindingAddress = "0.0.0.0";
154 LOG.info("LISP (RFC6830) Southbound Plugin is down!");
157 public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
158 final MessageType packetType) {
159 InetAddress ip = InetAddresses.forString(new String(address.getIpAddress().getValue()));
160 InetSocketAddress recipient = new InetSocketAddress(ip, address.getPort().getValue());
161 // the wrappedBuffer() method doesn't copy data, so this conversion shouldn't hurt performance
162 ByteBuf data = wrappedBuffer(outBuffer.array());
163 DatagramPacket packet = new DatagramPacket(data, recipient);
164 LOG.debug("Sending {} on port {} to address: {}", packetType, address.getPort().getValue(), ip);
165 if (LOG.isTraceEnabled()) {
166 LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
168 channel.write(packet).addListener(new ChannelFutureListener() {
170 public void operationComplete(ChannelFuture future) {
171 if (future.isSuccess()) {
172 LOG.trace("Success");
173 statistics.incrementTx(packetType.getIntValue());
175 LOG.warn("Failed to send packet");
176 statistics.incrementTxErrors();
183 public LispSouthboundStats getStats() {
188 public void setLispAddress(String address) {
189 synchronized (startLock) {
190 if (bindingAddress.equals(address)) {
191 LOG.debug("Configured LISP binding address didn't change.");
193 LOG.debug("Setting LISP binding address to {}", address);
194 bindingAddress = address;
198 } catch (Exception e) {
199 LOG.error("Failed to set LISP binding address: ", e);
206 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
207 listenOnXtrPort = shouldListenOnXtrPort;
208 if (listenOnXtrPort) {
211 LOG.info("Shutting down xTR");
217 public void setXtrPort(int port) {
219 if (listenOnXtrPort) {
225 public void close() throws Exception {
227 eventLoopGroup.shutdownGracefully();
228 sbRpcRegistration.close();