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;
29 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
30 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
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.lisp.proto.rev151105.MessageType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.common.net.InetAddresses;
44 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable, BindingAwareProvider {
45 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
47 private static Object startLock = new Object();
48 private LispSouthboundHandler lispSouthboundHandler;
49 private LispXtrSouthboundHandler lispXtrSouthboundHandler;
50 private NotificationPublishService notificationPublishService;
51 private RpcProviderRegistry rpcRegistry;
52 private BindingAwareBroker broker;
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 BindingAwareBroker.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);
69 broker.registerProvider(this);
71 synchronized (startLock) {
72 lispSouthboundHandler = new LispSouthboundHandler(this);
73 lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
74 lispSouthboundHandler.setNotificationProvider(this.notificationPublishService);
75 lispXtrSouthboundHandler.setNotificationProvider(this.notificationPublishService);
80 LOG.info("LISP (RFC6830) Southbound Plugin is up!");
84 private void start() {
86 Bootstrap bootstrap = new Bootstrap();
87 bootstrap.group(eventLoopGroup);
88 bootstrap.channel(NioDatagramChannel.class);
89 bootstrap.handler(lispSouthboundHandler);
90 channel = (NioDatagramChannel) bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
91 } catch (Exception e) {
92 LOG.error("Failed to open main socket ", e);
96 private void startXtr() {
97 if (listenOnXtrPort) {
99 Bootstrap xtrBootstrap = new Bootstrap();
100 xtrBootstrap.group(eventLoopGroup);
101 xtrBootstrap.channel(NioDatagramChannel.class);
102 xtrBootstrap.handler(lispXtrSouthboundHandler);
103 xtrChannel = (NioDatagramChannel) xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
104 } catch (Exception e) {
105 LOG.error("Failed to open xTR socket ", e);
110 private void stop() {
112 channel.close().sync();
114 } catch (Exception e) {
115 LOG.error("Failed to close main socket ", e);
119 private void stopXtr() {
120 if (listenOnXtrPort) {
122 xtrChannel.close().sync();
124 } catch (Exception e) {
125 LOG.error("Failed to close xTR socket ", e);
130 private void restart() {
131 LOG.info("Reloading");
136 private void restartXtr() {
137 LOG.info("Reloading xTR");
142 public void setNotificationPublishService(NotificationPublishService notificationService) {
143 this.notificationPublishService = notificationService;
146 public void setRpcRegistryDependency(RpcProviderRegistry rpcRegistry) {
147 this.rpcRegistry = rpcRegistry;
150 public void setBindingAwareBroker(BindingAwareBroker broker) {
151 this.broker = broker;
154 private void unloadActions() {
155 lispSouthboundHandler = null;
156 lispXtrSouthboundHandler = null;
157 bindingAddress = "0.0.0.0";
162 LOG.info("LISP (RFC6830) Southbound Plugin is down!");
165 public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
166 final MessageType packetType) {
167 InetAddress ip = InetAddresses.forString(new String(address.getIpAddress().getValue()));
168 InetSocketAddress recipient = new InetSocketAddress(ip, address.getPort().getValue());
169 // the wrappedBuffer() method doesn't copy data, so this conversion shouldn't hurt performance
170 ByteBuf data = wrappedBuffer(outBuffer.array());
171 DatagramPacket packet = new DatagramPacket(data, recipient);
172 LOG.debug("Sending {} on port {} to address: {}", packetType, address.getPort().getValue(), ip);
173 if (LOG.isTraceEnabled()) {
174 LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
176 channel.write(packet).addListener(new ChannelFutureListener() {
178 public void operationComplete(ChannelFuture future) {
179 if (future.isSuccess()) {
180 LOG.trace("Success");
181 statistics.incrementTx(packetType.getIntValue());
183 LOG.warn("Failed to send packet");
184 statistics.incrementTxErrors();
191 public LispSouthboundStats getStats() {
196 public void setLispAddress(String address) {
197 synchronized (startLock) {
198 if (bindingAddress.equals(address)) {
199 LOG.debug("Configured LISP binding address didn't change.");
201 LOG.debug("Setting LISP binding address to {}", address);
202 bindingAddress = address;
206 } catch (Exception e) {
207 LOG.error("Failed to set LISP binding address: ", e);
214 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
215 listenOnXtrPort = shouldListenOnXtrPort;
216 if (listenOnXtrPort) {
219 LOG.info("Shutting down xTR");
225 public void setXtrPort(int port) {
227 if (listenOnXtrPort) {
233 public void close() throws Exception {
235 eventLoopGroup.shutdownGracefully();
236 sbRpcRegistration.close();
240 public void onSessionInitiated(ProviderContext session) {
241 LOG.debug("LispSouthboundPlugin Provider Session Initiated");