2 * Copyright (c) 2013 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;
19 import java.util.concurrent.Future;
21 import org.eclipse.osgi.framework.console.CommandProvider;
22 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
24 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
25 import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
26 import org.opendaylight.lispflowmapping.implementation.serializer.MapNotifySerializer;
27 import org.opendaylight.lispflowmapping.implementation.serializer.MapReplySerializer;
28 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundService;
29 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
30 import org.opendaylight.lispflowmapping.type.lisp.MapReply;
31 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispPlugin;
32 import org.opendaylight.lispflowmapping.type.sbplugin.ILispSouthboundPlugin;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class LispSouthboundPlugin extends AbstractBindingAwareProvider implements ILispSouthboundPlugin, IConfigLispPlugin, CommandProvider {
40 protected static final Logger logger = LoggerFactory.getLogger(LispSouthboundPlugin.class);
42 private static Object startLock = new Object();
43 private LispIoThread thread;
44 private LispSouthboundService lispSouthboundService;
45 private volatile DatagramSocket socket = null;
46 private final String MAP_NOTIFY = "MapNotify";
47 private final String MAP_REPlY = "MapReply";
48 private volatile String bindingAddress = null;
49 private volatile boolean stillRunning = false;
50 private volatile boolean alreadyInit = false;
52 private void registerWithOSGIConsole() {
53 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
54 bundleContext.registerService(CommandProvider.class.getName(), this, null);
55 bundleContext.registerService(IConfigLispPlugin.class.getName(), this, null);
58 protected void stopImpl(BundleContext context) {
62 private void unloadActions() {
66 lispSouthboundService = null;
68 logger.info("LISP (RFC6830) Mapping Service is down!");
71 } catch (InterruptedException e) {
75 public void destroy() {
79 private class LispIoThread extends Thread {
80 private volatile boolean running;
82 public LispIoThread() {
91 int lispPortNumber = LispMessage.PORT_NUM;
92 int lispReceiveTimeout = 1000;
94 logger.info("LISP (RFC6830) Mapping Service is running and listening on " + bindingAddress);
96 socket = new DatagramSocket(new InetSocketAddress(bindingAddress, lispPortNumber));
97 socket.setSoTimeout(lispReceiveTimeout);
98 } catch (SocketException e) {
99 logger.warn("Cannot open socket on UDP port " + lispPortNumber, e);
104 byte[] buffer = new byte[4096];
105 DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
107 socket.receive(packet);
108 logger.debug("Received a packet!");
109 } catch (SocketTimeoutException ste) {
111 } catch (IOException e) {
112 logger.error("IO Exception while trying to recieve packet", e);
114 logger.debug("Handling packet from {}:{} (len={})", packet.getAddress().getHostAddress(), packet.getPort(), packet.getLength());
117 lispSouthboundService.handlePacket(packet);
118 } catch (Throwable t) {
119 logger.error("Error while handling packet", t);
124 logger.info("Socket closed");
125 stillRunning = false;
128 public void stopRunning() {
133 public static String intToIpv4(int address) {
134 return ((address >> 24) & 0xff) + "." + //
135 ((address >> 16) & 0xff) + "." + //
136 ((address >> 8) & 0xff) + "." + //
137 ((address >> 0) & 0xff);
140 public String getHelp() {
141 StringBuffer help = new StringBuffer();
142 help.append("---LISP Southbound Plugin---\n");
143 return help.toString();
146 private void startIOThread() {
147 thread = new LispIoThread();
148 logger.info("LISP (RFC6830) Mapping Service Southbound Plugin is up!");
152 public void onSessionInitiated(ProviderContext session) {
153 synchronized (startLock) {
156 lispSouthboundService = new LispSouthboundService();
157 registerWithOSGIConsole();
158 registerRPCs(session);
159 logger.debug("Provider Session initialized");
160 if (bindingAddress == null) {
161 setLispAddress("0.0.0.0");
168 private void registerRPCs(ProviderContext session) {
170 lispSouthboundService.setNotificationProvider(session.getSALService(NotificationProviderService.class));
171 session.addRpcImplementation(ILispSouthboundPlugin.class, this);
172 } catch (Throwable t) {
173 logger.error(t.getMessage(), t);
177 public Future<RpcResult<Void>> handleMapNotify(MapNotify mapNotify, InetAddress address) {
178 logger.trace("handleMapNotify called!!");
179 if (mapNotify != null) {
180 ByteBuffer outBuffer = MapNotifySerializer.getInstance().serialize(mapNotify);
181 handleSerializedLispBuffer(address, outBuffer, MAP_NOTIFY);
183 logger.debug("MapNotify was null");
188 private void handleSerializedLispBuffer(InetAddress address, ByteBuffer outBuffer, String packetType) {
189 DatagramPacket packet = new DatagramPacket(outBuffer.array(), outBuffer.limit());
190 packet.setPort(LispMessage.PORT_NUM);
191 packet.setAddress(address);
193 if (logger.isDebugEnabled()) {
194 logger.debug("Sending " + packetType + " on port " + LispMessage.PORT_NUM + " to address: " + address);
197 } catch (IOException e) {
198 logger.error("Failed to send " + packetType, e);
202 public Future<RpcResult<Void>> handleMapReply(MapReply mapReply, InetAddress address) {
203 logger.trace("handleMapReply called!!");
204 if (mapReply != null) {
205 ByteBuffer outBuffer = MapReplySerializer.getInstance().serialize(mapReply);
206 handleSerializedLispBuffer(address, outBuffer, MAP_REPlY);
208 logger.debug("MapReply was null");
213 public void setLispAddress(String address) {
214 synchronized (startLock) {
215 if (bindingAddress != null && bindingAddress.equals(address)) {
216 logger.debug("configured lisp binding address didn't change.");
218 String action = (bindingAddress == null ? "Setting" : "Resetting");
219 logger.info(action + " lisp binding address to: " + address);
220 bindingAddress = address;
221 if (thread != null) {
222 thread.stopRunning();
223 while (stillRunning) {
226 } catch (InterruptedException e) {