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.InetSocketAddress;
15 import java.net.SocketException;
16 import java.net.SocketTimeoutException;
18 import org.eclipse.osgi.framework.console.CommandProvider;
19 import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
20 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
21 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundService;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.FrameworkUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class LispSouthboundPlugin implements ILispSouthboundPlugin {
28 protected static final Logger logger = LoggerFactory.getLogger(LispSouthboundPlugin.class);
30 private LispIoThread thread;
31 private LispSouthboundService service;
33 void setFlowMappingService(IFlowMapping mappingService) {
34 logger.debug("FlowMapping set in LispSouthbound");
35 service = new LispSouthboundService(mappingService, mappingService);
36 logger.debug("Registering LispIpv4Address");
37 logger.debug("Registering LispIpv6Address");
40 void unsetFlowMappingService(IFlowMapping mappingService) {
41 logger.debug("LispDAO was unset in LispMappingService");
46 logger.debug("LISP (RFC6830) Mapping Service is initialized!");
47 thread = new LispIoThread();
51 logger.info("LISP (RFC6830) Mapping Service is up!");
55 registerWithOSGIConsole();
58 private void registerWithOSGIConsole() {
59 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
60 bundleContext.registerService(CommandProvider.class.getName(), this, null);
65 logger.info("LISP (RFC6830) Mapping Service is down!");
68 } catch (InterruptedException e) {
72 public void destroy() {
73 logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
82 private class LispIoThread extends Thread {
83 private volatile boolean running;
85 public LispIoThread() {
92 String lispBindAddress = "0.0.0.0";
93 String lispIp = System.getProperty("lispip");
95 lispBindAddress = lispIp;
97 DatagramSocket socket;
98 int lispPortNumber = LispMessage.PORT_NUM;
99 int lispReceiveTimeout = 1000;
101 logger.info("LISP (RFC6830) Mapping Service is running and listening on " + lispBindAddress);
103 socket = new DatagramSocket(new InetSocketAddress(lispBindAddress, lispPortNumber));
104 socket.setSoTimeout(lispReceiveTimeout);
105 } catch (SocketException e) {
106 logger.warn("Cannot open socket on UDP port " + lispPortNumber, e);
111 byte[] buffer = new byte[4096];
112 DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
114 socket.receive(packet);
115 logger.debug("Received a packet!");
116 } catch (SocketTimeoutException ste) {
118 } catch (IOException e) {
119 logger.error("IO Exception while trying to recieve packet", e);
121 logger.debug("Handling packet from {}:{} (len={})", packet.getAddress().getHostAddress(), packet.getPort(), packet.getLength());
123 DatagramPacket reply = service.handlePacket(packet);
126 logger.debug("Reply was null!");
129 if (reply.getAddress() == null) {
130 reply.setAddress(packet.getAddress());
133 logger.debug("sending reply to {}:{} (len={})", reply.getAddress().getHostAddress(), reply.getPort(), reply.getLength());
135 } catch (IOException e) {
136 logger.error("IO Excpetion while sending: ", e);
138 } catch (RuntimeException e) {
144 logger.info("Socket closed");
147 public void stopRunning() {
152 public static String intToIpv4(int address) {
153 return ((address >> 24) & 0xff) + "." + //
154 ((address >> 16) & 0xff) + "." + //
155 ((address >> 8) & 0xff) + "." + //
156 ((address >> 0) & 0xff);
159 public String getHelp() {
160 StringBuffer help = new StringBuffer();
161 help.append("---LISP Southbound Plugin---\n");
162 return help.toString();