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.md.sal.common.api.clustering.EntityOwnershipService;
35 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
36 import org.opendaylight.lispflowmapping.clustering.ClusterNodeModulSwitcherImpl;
37 import org.opendaylight.lispflowmapping.clustering.api.ClusterNodeModuleSwitcher;
38 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
39 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
40 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
41 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.lisp.sb.config.rev150517.LispSbConfig;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable, ClusterNodeModuleSwitcher {
51 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
53 private static Object startLock = new Object();
54 private final ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher;
55 private LispSouthboundHandler lispSouthboundHandler;
56 private LispXtrSouthboundHandler lispXtrSouthboundHandler;
57 private NotificationPublishService notificationPublishService;
58 private NioDatagramChannel channel;
59 private volatile String bindingAddress = "0.0.0.0";
60 private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
61 private volatile boolean listenOnXtrPort = false;
62 private boolean mapRegisterCacheEnabled = true;
63 private RpcRegistration<OdlLispSbService> sbRpcRegistration;
64 private NioDatagramChannel xtrChannel;
65 private LispSouthboundStats statistics = new LispSouthboundStats();
66 private Bootstrap bootstrap = new Bootstrap();
67 private Bootstrap xtrBootstrap = new Bootstrap();
68 private ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
69 private EventLoopGroup eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
70 private DataBroker dataBroker;
72 public LispSouthboundPlugin(final DataBroker dataBroker,
73 final NotificationPublishService notificationPublishService,
74 final LispSbConfig lispSbConfig, final EntityOwnershipService entityOwnershipService) {
75 this.dataBroker = dataBroker;
76 this.notificationPublishService = notificationPublishService;
77 this.bindingAddress = lispSbConfig.getBindAddress();
78 this.mapRegisterCacheEnabled = lispSbConfig.isMapRegisterCache();
79 clusterNodeModulSwitcher = new ClusterNodeModulSwitcherImpl(entityOwnershipService);
80 clusterNodeModulSwitcher.setModule(this);
84 LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
85 synchronized (startLock) {
86 lispSouthboundHandler = new LispSouthboundHandler(this);
87 lispSouthboundHandler.setDataBroker(dataBroker);
88 lispSouthboundHandler.setNotificationProvider(this.notificationPublishService);
89 lispSouthboundHandler.setMapRegisterCacheEnabled(mapRegisterCacheEnabled);
90 lispSouthboundHandler.init();
91 lispSouthboundHandler.restoreDaoFromDatastore();
93 lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
94 lispXtrSouthboundHandler.setNotificationProvider(this.notificationPublishService);
96 bootstrap.group(eventLoopGroup);
97 bootstrap.channel(NioDatagramChannel.class);
98 bootstrap.handler(lispSouthboundHandler);
100 xtrBootstrap.group(eventLoopGroup);
101 xtrBootstrap.channel(NioDatagramChannel.class);
102 xtrBootstrap.handler(lispXtrSouthboundHandler);
107 LOG.info("LISP (RFC6830) Southbound Plugin is up!");
109 clusterNodeModulSwitcher.switchModuleByEntityOwnership();
112 private void start() {
114 channel = (NioDatagramChannel) bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
115 LOG.debug("Binding LISP UDP listening socket to {}:{}", bindingAddress, LispMessage.PORT_NUM);
116 } catch (Exception e) {
117 LOG.error("Failed to open main socket ", e);
121 private void startXtr() {
122 if (listenOnXtrPort) {
124 xtrChannel = (NioDatagramChannel) xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
125 LOG.debug("Binding LISP xTR UDP listening socket to {}:{}", bindingAddress, xtrPort);
126 } catch (Exception e) {
127 LOG.error("Failed to open xTR socket ", e);
132 private void stop() {
134 channel.close().sync();
136 } catch (Exception e) {
137 LOG.error("Failed to close main socket ", e);
141 private void stopXtr() {
142 if (listenOnXtrPort) {
144 xtrChannel.close().sync();
146 } catch (Exception e) {
147 LOG.error("Failed to close xTR socket ", e);
152 private void restart() {
153 LOG.info("Reloading");
158 private void restartXtr() {
159 LOG.info("Reloading xTR");
164 private void unloadActions() {
165 lispSouthboundHandler = null;
166 lispXtrSouthboundHandler = null;
171 LOG.info("LISP (RFC6830) Southbound Plugin is down!");
174 public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer,
175 final MessageType packetType) {
176 InetAddress ip = getInetAddress(address);
177 handleSerializedLispBuffer(ip, outBuffer, packetType, address.getPort().getValue());
180 public void handleSerializedLispBuffer(InetAddress address, ByteBuffer outBuffer,
181 final MessageType packetType, final int portNumber) {
182 InetSocketAddress recipient = new InetSocketAddress(address, portNumber);
183 outBuffer.position(0);
184 ByteBuf data = wrappedBuffer(outBuffer);
185 DatagramPacket packet = new DatagramPacket(data, recipient);
186 LOG.debug("Sending {} on port {} to address: {}", packetType, portNumber, address);
187 if (LOG.isTraceEnabled()) {
188 LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
190 channel.write(packet).addListener(new ChannelFutureListener() {
192 public void operationComplete(ChannelFuture future) {
193 if (future.isSuccess()) {
194 LOG.trace("Success");
195 statistics.incrementTx(packetType.getIntValue());
197 LOG.warn("Failed to send packet");
198 statistics.incrementTxErrors();
205 private InetAddress getInetAddress(TransportAddress address) {
206 Preconditions.checkNotNull(address, "TransportAddress must not be null");
207 IpAddressBinary ip = address.getIpAddress();
209 if (ip.getIpv4AddressBinary() != null) {
210 return InetAddress.getByAddress(ip.getIpv4AddressBinary().getValue());
211 } else if (ip.getIpv6AddressBinary() != null) {
212 return InetAddress.getByAddress(ip.getIpv6AddressBinary().getValue());
214 } catch (UnknownHostException e) {
215 LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
220 public LispSouthboundStats getStats() {
225 public void setLispAddress(String address) {
226 synchronized (startLock) {
227 if (bindingAddress.equals(address)) {
228 LOG.debug("Configured LISP binding address didn't change.");
230 LOG.debug("Setting LISP binding address to {}", address);
231 bindingAddress = address;
232 if (channel != null) {
236 } catch (Exception e) {
237 LOG.error("Failed to set LISP binding address: ", e);
245 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
246 listenOnXtrPort = shouldListenOnXtrPort;
247 if (listenOnXtrPort) {
250 LOG.info("Shutting down xTR");
256 public void setXtrPort(int port) {
258 if (listenOnXtrPort) {
263 public void setMapRegisterCacheEnabled(final boolean mapRegisterCacheEnabled) {
264 this.mapRegisterCacheEnabled = mapRegisterCacheEnabled;
265 if (mapRegisterCacheEnabled) {
266 LOG.info("Enabling Map-Register cache");
268 LOG.info("Disabling Map-Register cache");
273 public void close() throws Exception {
274 eventLoopGroup.shutdownGracefully();
275 sbRpcRegistration.close();
276 lispSouthboundHandler.close();
281 public void stopModule() {
282 if (lispSouthboundHandler != null) {
283 lispSouthboundHandler.setNotificationProvider(null);
284 lispSouthboundHandler.setIsReadFromChannelEnabled(false);
286 if (lispXtrSouthboundHandler != null) {
287 lispXtrSouthboundHandler.setNotificationProvider(null);
292 public void startModule() {
293 if (lispSouthboundHandler != null) {
294 lispSouthboundHandler.setNotificationProvider(notificationPublishService);
295 lispSouthboundHandler.restoreDaoFromDatastore();
296 lispSouthboundHandler.setIsReadFromChannelEnabled(true);
298 if (lispXtrSouthboundHandler != null) {
299 lispXtrSouthboundHandler.setNotificationProvider(notificationPublishService);