added basic map register and then request integration test TELSDN-375: #close
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / LispSouthboundPlugin.java
1 /*
2  * Copyright (c) 2013 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.lispflowmapping.southbound;
10
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;
17
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;
26
27 public class LispSouthboundPlugin implements ILispSouthboundPlugin {
28     protected static final Logger logger = LoggerFactory.getLogger(LispSouthboundPlugin.class);
29
30     private LispIoThread thread;
31     private LispSouthboundService service;
32
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");
38     }
39
40     void unsetFlowMappingService(IFlowMapping mappingService) {
41         logger.debug("LispDAO was unset in LispMappingService");
42         service = null;
43     }
44
45     public void init() {
46         logger.debug("LISP (RFC6830) Mapping Service is initialized!");
47         thread = new LispIoThread();
48     }
49
50     public void start() {
51         logger.info("LISP (RFC6830) Mapping Service is up!");
52         thread.start();
53
54         // OSGI console
55         registerWithOSGIConsole();
56     }
57
58     private void registerWithOSGIConsole() {
59         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
60         bundleContext.registerService(CommandProvider.class.getName(), this, null);
61     }
62
63     public void stop() {
64         thread.stopRunning();
65         logger.info("LISP (RFC6830) Mapping Service is down!");
66         try {
67             Thread.sleep(1100);
68         } catch (InterruptedException e) {
69         }
70     }
71
72     public void destroy() {
73         logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
74         if (thread != null) {
75             thread.stopRunning();
76         }
77         thread = null;
78         service = null;
79
80     }
81
82     private class LispIoThread extends Thread {
83         private volatile boolean running;
84
85         public LispIoThread() {
86             super("Lisp Thread");
87             running = true;
88         }
89
90         @Override
91         public void run() {
92             String lispBindAddress = "0.0.0.0";
93             String lispIp = System.getProperty("lispip");
94             if (lispIp != null) {
95                 lispBindAddress = lispIp;
96             }
97             DatagramSocket socket;
98             int lispPortNumber = LispMessage.PORT_NUM;
99             int lispReceiveTimeout = 1000;
100
101             logger.info("LISP (RFC6830) Mapping Service is running and listening on " + lispBindAddress);
102             try {
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);
107                 return;
108             }
109
110             while (running) {
111                 byte[] buffer = new byte[4096];
112                 DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
113                 try {
114                     socket.receive(packet);
115                     logger.debug("Received a packet!");
116                 } catch (SocketTimeoutException ste) {
117                     continue;
118                 } catch (IOException e) {
119                     logger.error("IO Exception while trying to recieve packet", e);
120                 }
121                 logger.debug("Handling packet from {}:{} (len={})", packet.getAddress().getHostAddress(), packet.getPort(), packet.getLength());
122                 try {
123                     DatagramPacket reply = service.handlePacket(packet);
124
125                     if (reply == null) {
126                         logger.debug("Reply was null!");
127                         continue;
128                     }
129                     if (reply.getAddress() == null) {
130                         reply.setAddress(packet.getAddress());
131                     }
132                     try {
133                         logger.debug("sending reply to {}:{} (len={})", reply.getAddress().getHostAddress(), reply.getPort(), reply.getLength());
134                         socket.send(reply);
135                     } catch (IOException e) {
136                         logger.error("IO Excpetion while sending: ", e);
137                     }
138                 } catch (RuntimeException e) {
139                     logger.warn("", e);
140                 }
141             }
142
143             socket.close();
144             logger.info("Socket closed");
145         }
146
147         public void stopRunning() {
148             running = false;
149         }
150     }
151
152     public static String intToIpv4(int address) {
153         return ((address >> 24) & 0xff) + "." + //
154                 ((address >> 16) & 0xff) + "." + //
155                 ((address >> 8) & 0xff) + "." + //
156                 ((address >> 0) & 0xff);
157     }
158
159     public String getHelp() {
160         StringBuffer help = new StringBuffer();
161         help.append("---LISP Southbound Plugin---\n");
162         return help.toString();
163     }
164
165 }