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;
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.implementation.serializer.MapRequestSerializer;
29 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundService;
30 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispPlugin;
31 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispflowmappingService;
32 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SendMapNotifyInput;
33 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SendMapReplyInput;
34 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SendMapRequestInput;
35 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.transportaddress.TransportAddress;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.common.net.InetAddresses;
44 public class LispSouthboundPlugin extends AbstractBindingAwareProvider implements IConfigLispPlugin, CommandProvider, LispflowmappingService {
45 protected static final Logger logger = LoggerFactory.getLogger(LispSouthboundPlugin.class);
47 private static Object startLock = new Object();
48 private LispIoThread thread;
49 private LispSouthboundService lispSouthboundService;
50 private volatile DatagramSocket socket = null;
51 private final String MAP_NOTIFY = "MapNotify";
52 private final String MAP_REPlY = "MapReply";
53 private final String MAP_REQUEST = "MapRequest";
54 private volatile String bindingAddress = null;
55 private volatile boolean stillRunning = false;
56 private volatile boolean alreadyInit = false;
58 private void registerWithOSGIConsole() {
59 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
60 bundleContext.registerService(CommandProvider.class.getName(), this, null);
61 bundleContext.registerService(IConfigLispPlugin.class.getName(), this, null);
64 protected void stopImpl(BundleContext context) {
68 private void unloadActions() {
72 lispSouthboundService = null;
74 logger.info("LISP (RFC6830) Mapping Service is down!");
77 } catch (InterruptedException e) {
81 public void destroy() {
85 private class LispIoThread extends Thread {
86 private volatile boolean running;
88 public LispIoThread() {
97 int lispPortNumber = LispMessage.PORT_NUM;
98 int lispReceiveTimeout = 1000;
100 logger.info("LISP (RFC6830) Mapping Service is running and listening on " + bindingAddress);
102 socket = new DatagramSocket(new InetSocketAddress(bindingAddress, lispPortNumber));
103 socket.setSoTimeout(lispReceiveTimeout);
104 } catch (SocketException e) {
105 logger.error("Cannot open socket on UDP port " + lispPortNumber, e);
110 byte[] buffer = new byte[4096];
111 DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
113 socket.receive(packet);
114 logger.trace("Received a packet!");
115 } catch (SocketTimeoutException ste) {
117 } catch (IOException e) {
118 logger.warn("IO Exception while trying to recieve packet", e);
120 logger.trace(String.format("Handling packet from {%s}:{%d} (len={%d})", packet.getAddress().getHostAddress(), packet.getPort(),
121 packet.getLength()));
124 lispSouthboundService.handlePacket(packet);
125 } catch (Throwable t) {
126 logger.warn("Error while handling packet", t);
131 logger.trace("Socket closed");
132 stillRunning = false;
135 public void stopRunning() {
140 public static String intToIpv4(int address) {
141 return ((address >> 24) & 0xff) + "." + //
142 ((address >> 16) & 0xff) + "." + //
143 ((address >> 8) & 0xff) + "." + //
144 ((address >> 0) & 0xff);
147 public String getHelp() {
148 StringBuffer help = new StringBuffer();
149 help.append("---LISP Southbound Plugin---\n");
150 return help.toString();
153 private void startIOThread() {
154 thread = new LispIoThread();
155 logger.info("LISP (RFC6830) Mapping Service Southbound Plugin is up!");
159 public void onSessionInitiated(ProviderContext session) {
160 logger.info("LISP (RFC6830) Mapping Service is up!");
161 synchronized (startLock) {
164 lispSouthboundService = new LispSouthboundService();
165 registerWithOSGIConsole();
166 registerRPCs(session);
167 logger.trace("Provider Session initialized");
168 if (bindingAddress == null) {
169 setLispAddress("0.0.0.0");
176 private void registerRPCs(ProviderContext session) {
178 lispSouthboundService.setNotificationProvider(session.getSALService(NotificationProviderService.class));
179 session.addRpcImplementation(LispflowmappingService.class, this);
180 } catch (Throwable t) {
181 logger.error(t.getMessage(), t);
185 private void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer, String packetType) {
186 DatagramPacket packet = new DatagramPacket(outBuffer.array(), outBuffer.limit());
187 packet.setPort(address.getPort().getValue());
188 InetAddress ip = InetAddresses.forString(address.getIpAddress().getIpv4Address().getValue());
189 packet.setAddress(ip);
191 if (logger.isDebugEnabled()) {
192 logger.trace("Sending " + packetType + " on port " + address.getPort().getValue() + " to address: " + ip);
195 } catch (IOException e) {
196 logger.warn("Failed to send " + packetType, e);
200 public void setLispAddress(String address) {
201 synchronized (startLock) {
202 if (bindingAddress != null && bindingAddress.equals(address)) {
203 logger.trace("configured lisp binding address didn't change.");
205 String action = (bindingAddress == null ? "Setting" : "Resetting");
206 logger.trace(action + " lisp binding address to: " + address);
207 bindingAddress = address;
208 if (thread != null) {
209 thread.stopRunning();
210 while (stillRunning) {
213 } catch (InterruptedException e) {
223 public Future<RpcResult<Void>> sendMapNotify(SendMapNotifyInput mapNotifyInput) {
224 logger.trace("sendMapNotify called!!");
225 if (mapNotifyInput != null) {
226 ByteBuffer outBuffer = MapNotifySerializer.getInstance().serialize(mapNotifyInput.getMapNotify());
227 handleSerializedLispBuffer(mapNotifyInput.getTransportAddress(), outBuffer, MAP_NOTIFY);
229 logger.warn("MapNotify was null");
235 public Future<RpcResult<Void>> sendMapReply(SendMapReplyInput mapReplyInput) {
236 logger.trace("sendMapReply called!!");
237 if (mapReplyInput != null) {
238 ByteBuffer outBuffer = MapReplySerializer.getInstance().serialize(mapReplyInput.getMapReply());
239 handleSerializedLispBuffer(mapReplyInput.getTransportAddress(), outBuffer, MAP_REPlY);
241 logger.warn("MapReply was null");
247 public Future<RpcResult<Void>> sendMapRequest(SendMapRequestInput mapRequestInput) {
248 logger.trace("sendMapRequest called!!");
249 if (mapRequestInput != null) {
250 ByteBuffer outBuffer = MapRequestSerializer.getInstance().serialize(mapRequestInput.getMapRequest());
251 handleSerializedLispBuffer(mapRequestInput.getTransportAddress(), outBuffer, MAP_REQUEST);
253 logger.debug("MapRequest was null");