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.net.UnknownHostException;
26 import java.nio.ByteBuffer;
27 import java.util.concurrent.ThreadFactory;
29 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
30 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
31 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
32 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
33 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
34 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
35 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.base.Preconditions;
45 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable {
46 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
48 private static Object startLock = new Object();
49 private LispSouthboundHandler lispSouthboundHandler;
50 private LispXtrSouthboundHandler lispXtrSouthboundHandler;
51 private NotificationPublishService notificationPublishService;
52 private RpcProviderRegistry rpcRegistry;
53 private NioDatagramChannel channel;
54 private volatile String bindingAddress = "0.0.0.0";
55 private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
56 private volatile boolean listenOnXtrPort = false;
57 private RpcRegistration<OdlLispSbService> sbRpcRegistration;
58 private NioDatagramChannel xtrChannel;
59 private LispSouthboundStats statistics = new LispSouthboundStats();
60 private ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
61 private EventLoopGroup eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
65 LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
66 final LispSouthboundRPC sbRpcHandler = new LispSouthboundRPC(this);
68 sbRpcRegistration = rpcRegistry.addRpcImplementation(OdlLispSbService.class, sbRpcHandler);
70 synchronized (startLock) {
71 lispSouthboundHandler = new LispSouthboundHandler(this);
72 lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
73 lispSouthboundHandler.setNotificationProvider(this.notificationPublishService);
74 lispXtrSouthboundHandler.setNotificationProvider(this.notificationPublishService);
79 LOG.info("LISP (RFC6830) Southbound Plugin is up!");
83 private void start() {
85 Bootstrap bootstrap = new Bootstrap();
86 bootstrap.group(eventLoopGroup);
87 bootstrap.channel(NioDatagramChannel.class);
88 bootstrap.handler(lispSouthboundHandler);
89 channel = (NioDatagramChannel) bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
90 } catch (Exception e) {
91 LOG.error("Failed to open main socket ", e);
95 private void startXtr() {
96 if (listenOnXtrPort) {
98 Bootstrap xtrBootstrap = new Bootstrap();
99 xtrBootstrap.group(eventLoopGroup);
100 xtrBootstrap.channel(NioDatagramChannel.class);
101 xtrBootstrap.handler(lispXtrSouthboundHandler);
102 xtrChannel = (NioDatagramChannel) xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
103 } catch (Exception e) {
104 LOG.error("Failed to open xTR socket ", e);
109 private void stop() {
111 channel.close().sync();
113 } catch (Exception e) {
114 LOG.error("Failed to close main socket ", e);
118 private void stopXtr() {
119 if (listenOnXtrPort) {
121 xtrChannel.close().sync();
123 } catch (Exception e) {
124 LOG.error("Failed to close xTR socket ", e);
129 private void restart() {
130 LOG.info("Reloading");
135 private void restartXtr() {
136 LOG.info("Reloading xTR");
141 public void setNotificationPublishService(NotificationPublishService notificationService) {
142 this.notificationPublishService = notificationService;
145 public void setRpcRegistryDependency(RpcProviderRegistry rpcRegistry) {
146 this.rpcRegistry = rpcRegistry;
149 private void unloadActions() {
150 lispSouthboundHandler = null;
151 lispXtrSouthboundHandler = null;
152 bindingAddress = "0.0.0.0";
157 LOG.info("LISP (RFC6830) Southbound Plugin is down!");
160 public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
161 final MessageType packetType) {
162 InetAddress ip = getInetAddress(address);
163 InetSocketAddress recipient = new InetSocketAddress(ip, address.getPort().getValue());
164 // the wrappedBuffer() method doesn't copy data, so this conversion shouldn't hurt performance
165 ByteBuf data = wrappedBuffer(outBuffer.array());
166 DatagramPacket packet = new DatagramPacket(data, recipient);
167 LOG.debug("Sending {} on port {} to address: {}", packetType, address.getPort().getValue(), ip);
168 if (LOG.isTraceEnabled()) {
169 LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
171 channel.write(packet).addListener(new ChannelFutureListener() {
173 public void operationComplete(ChannelFuture future) {
174 if (future.isSuccess()) {
175 LOG.trace("Success");
176 statistics.incrementTx(packetType.getIntValue());
178 LOG.warn("Failed to send packet");
179 statistics.incrementTxErrors();
186 private InetAddress getInetAddress(TransportAddress address) {
187 Preconditions.checkNotNull(address, "TransportAddress must not be null");
188 IpAddressBinary ip = address.getIpAddress();
190 if (ip.getIpv4AddressBinary() != null) {
191 return InetAddress.getByAddress(ip.getIpv4AddressBinary().getValue());
192 } else if (ip.getIpv6AddressBinary() != null) {
193 return InetAddress.getByAddress(ip.getIpv6AddressBinary().getValue());
195 } catch (UnknownHostException e) {
196 LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
201 public LispSouthboundStats getStats() {
206 public void setLispAddress(String address) {
207 synchronized (startLock) {
208 if (bindingAddress.equals(address)) {
209 LOG.debug("Configured LISP binding address didn't change.");
211 LOG.debug("Setting LISP binding address to {}", address);
212 bindingAddress = address;
216 } catch (Exception e) {
217 LOG.error("Failed to set LISP binding address: ", e);
224 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
225 listenOnXtrPort = shouldListenOnXtrPort;
226 if (listenOnXtrPort) {
229 LOG.info("Shutting down xTR");
235 public void setXtrPort(int port) {
237 if (listenOnXtrPort) {
243 public void close() throws Exception {
245 eventLoopGroup.shutdownGracefully();
246 sbRpcRegistration.close();