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 java.io.IOException;
12 import java.net.DatagramPacket;
13 import java.net.DatagramSocket;
14 import java.net.InetAddress;
15 import java.net.InetSocketAddress;
16 import java.net.SocketException;
17 import java.net.SocketTimeoutException;
18 import java.nio.ByteBuffer;
20 import org.apache.commons.lang3.exception.ExceptionUtils;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
24 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
25 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
26 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
27 import org.opendaylight.lispflowmapping.southbound.lisp.ILispSouthboundService;
28 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundService;
29 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundService;
30 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MessageType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.transportaddress.TransportAddress;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.LispSbService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.net.InetAddresses;
39 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable, BindingAwareProvider {
40 protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
42 private static Object startLock = new Object();
43 private LispIoThread lispThread;
44 private LispIoThread xtrThread;
45 private LispSouthboundService lispSouthboundService;
46 private LispXtrSouthboundService lispXtrSouthboundService;
47 private NotificationProviderService notificationService;
48 private RpcProviderRegistry rpcRegistry;
49 private BindingAwareBroker broker;
50 private volatile DatagramSocket socket = null;
51 private volatile String bindingAddress = null;
52 private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
53 private volatile boolean listenOnXtrPort = false;
54 private BindingAwareBroker.RpcRegistration<LispSbService> sbRpcRegistration;
55 private DatagramSocket xtrSocket;
56 private LispSouthboundStats statistics = new LispSouthboundStats();
59 LOG.info("LISP (RFC6830) southbound plugin is initializing...");
60 final LispSouthboundRPC sbRpcHandler = new LispSouthboundRPC(this);
62 sbRpcRegistration = rpcRegistry.addRpcImplementation(LispSbService.class, sbRpcHandler);
63 broker.registerProvider(this);
65 synchronized (startLock) {
66 lispSouthboundService = new LispSouthboundService(this);
67 lispXtrSouthboundService = new LispXtrSouthboundService();
68 lispSouthboundService.setNotificationProvider(this.notificationService);
69 lispXtrSouthboundService.setNotificationProvider(this.notificationService);
70 if (bindingAddress == null) {
71 setLispAddress("0.0.0.0");
73 LOG.info("LISP (RFC6830) southbound plugin is up!");
77 public void setNotificationProviderService(NotificationProviderService notificationService) {
78 this.notificationService = notificationService;
81 public void setRpcRegistryDependency(RpcProviderRegistry rpcRegistry) {
82 this.rpcRegistry = rpcRegistry;
85 public void setBindingAwareBroker(BindingAwareBroker broker) {
89 private void unloadActions() {
90 if (lispThread != null) {
91 lispThread.stopRunning();
93 lispSouthboundService = null;
94 lispXtrSouthboundService = null;
97 bindingAddress = null;
98 LOG.info("LISP (RFC6830) southbound plugin is down!");
101 } catch (InterruptedException e) {
105 private class LispIoThread extends Thread {
106 private volatile boolean shouldRun;
107 private volatile DatagramSocket threadSocket = null;
108 private volatile ILispSouthboundService service;
109 private volatile boolean running;
111 public LispIoThread(DatagramSocket socket, ILispSouthboundService service) {
112 super("Lisp Thread");
113 this.threadSocket = socket;
114 this.service = service;
122 int lispReceiveTimeout = 1000;
124 LOG.info("LISP (RFC6830) southbound plugin is running and listening on address: " + bindingAddress
125 + " port: " + threadSocket.getLocalPort());
128 threadSocket.setSoTimeout(lispReceiveTimeout);
129 } catch (SocketException e) {
130 LOG.error("Cannot open socket on UDP port " + threadSocket.getLocalPort(), e);
135 byte[] buffer = new byte[4096];
136 DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
138 threadSocket.receive(packet);
139 LOG.trace("Received a packet!");
140 } catch (SocketTimeoutException ste) {
142 } catch (IOException e) {
143 LOG.warn("IO Exception while trying to recieve packet", e);
145 LOG.trace(String.format("Handling packet from {%s}:{%d} (len={%d})", packet.getAddress()
146 .getHostAddress(), packet.getPort(), packet.getLength()));
149 this.service.handlePacket(packet);
150 } catch (Exception e) {
151 LOG.warn("Error while handling packet", e);
155 threadSocket.close();
156 LOG.trace("Socket closed");
160 public void stopRunning() {
164 public boolean isRunning() {
169 public static String intToIpv4(int address) {
170 return ((address >> 24) & 0xff) + "." + //
171 ((address >> 16) & 0xff) + "." + //
172 ((address >> 8) & 0xff) + "." + //
173 ((address >> 0) & 0xff);
176 private void startIOThread() {
177 if (socket != null) {
178 while (!socket.isClosed()) {
181 } catch (InterruptedException e) {
186 socket = new DatagramSocket(new InetSocketAddress(bindingAddress, LispMessage.PORT_NUM));
187 lispThread = new LispIoThread(socket, lispSouthboundService);
189 LOG.info("LISP (RFC6830) southbound plugin is listening for control packets!");
190 if (listenOnXtrPort) {
193 } catch (SocketException e) {
194 LOG.error("couldn't start socket: {}", ExceptionUtils.getStackTrace(e));
198 private void restartXtrThread() {
201 xtrSocket = new DatagramSocket(new InetSocketAddress(bindingAddress, xtrPort));
202 xtrThread = new LispIoThread(xtrSocket, lispXtrSouthboundService);
204 LOG.info("xTR southbound plugin is up!");
205 } catch (SocketException e) {
206 LOG.warn("failed to start xtr thread: {}", ExceptionUtils.getStackTrace(e));
210 public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer, MessageType packetType) {
211 DatagramPacket packet = new DatagramPacket(outBuffer.array(), outBuffer.limit());
212 packet.setPort(address.getPort().getValue());
213 InetAddress ip = InetAddresses.forString(new String(address.getIpAddress().getValue()));
214 packet.setAddress(ip);
216 if (LOG.isDebugEnabled()) {
217 LOG.trace("Sending " + packetType + " on port " + address.getPort().getValue() + " to address: " + ip);
220 this.statistics.incrementTx(packetType.getIntValue());
221 } catch (IOException e) {
222 LOG.warn("Failed to send " + packetType, e);
223 this.statistics.incrementTxErrors();
227 public LispSouthboundStats getStats() {
231 public void setLispAddress(String address) {
232 synchronized (startLock) {
233 if (bindingAddress != null && bindingAddress.equals(address)) {
234 LOG.trace("configured lisp binding address didn't change.");
236 String action = (bindingAddress == null ? "Setting" : "Resetting");
237 LOG.trace(action + " lisp binding address to: " + address);
238 bindingAddress = address;
239 if (lispThread != null) {
240 lispThread.stopRunning();
241 while (lispThread.isRunning()) {
244 } catch (InterruptedException e) {
254 private void stopXtrThread() {
255 if (xtrThread != null) {
256 xtrThread.stopRunning();
257 while (xtrThread.isRunning()) {
260 } catch (InterruptedException e) {
267 public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
268 listenOnXtrPort = shouldListenOnXtrPort;
269 if (listenOnXtrPort) {
270 LOG.debug("restarting xtr thread");
273 LOG.debug("terminating thread");
279 public void setXtrPort(int port) {
281 if (listenOnXtrPort) {
287 public void close() throws Exception {
289 sbRpcRegistration.close();
293 public void onSessionInitiated(ProviderContext session) {
294 LOG.debug("LispSouthboundPlugin Provider Session Initiated");