added authentication to map notify and map reply, and moved the serialization process...
[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
34     void setFlowMappingService(IFlowMapping mappingService) {
35         logger.debug("FlowMapping set in LispSouthbound");
36         service = new LispSouthboundService(mappingService, mappingService);
37         logger.debug("Registering LispIpv4Address");
38         logger.debug("Registering LispIpv6Address");
39     }
40
41     void unsetFlowMappingService(IFlowMapping mappingService) {
42         logger.debug("LispDAO was unset in LispMappingService");
43         service = null;
44     }
45
46     public void init() {
47         logger.debug("LISP (RFC6830) Mapping Service is initialized!");
48         thread = new LispIoThread();
49     }
50
51     public void start() {
52         logger.info("LISP (RFC6830) Mapping Service is up!");
53         thread.start();
54
55         // OSGI console
56         registerWithOSGIConsole();
57     }
58
59     private void registerWithOSGIConsole() {
60         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
61         bundleContext.registerService(CommandProvider.class.getName(), this, null);
62     }
63
64     public void stop() {
65         thread.stopRunning();
66         logger.info("LISP (RFC6830) Mapping Service is down!");
67     }
68
69     public void destroy() {
70         logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
71         thread = null;
72         service = null;
73     }
74
75     private class LispIoThread extends Thread {
76         private boolean running;
77
78         public LispIoThread() {
79             super("Lisp Thread");
80             running = true;
81         }
82
83         @Override
84         public void run() {
85             DatagramSocket socket;
86             int lispPortNumber = LispMessage.PORT_NUM;
87             int lispReceiveTimeout = 1000;
88             String lispBindAddress = "0.0.0.0";
89             logger.info("LISP (RFC6830) Mapping Service is running and listening on " + lispBindAddress);
90             try {
91                 socket = new DatagramSocket(new InetSocketAddress(lispBindAddress, lispPortNumber));
92                 socket.setSoTimeout(lispReceiveTimeout);
93             } catch (SocketException e) {
94                 logger.warn("Cannot open socket on UDP port " + lispPortNumber, e);
95                 return;
96             }
97
98             while (running) {
99                 byte[] buffer = new byte[4096];
100                 DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
101                 try {
102                     socket.receive(packet);
103                 } catch (SocketTimeoutException ste) {
104                     continue;
105                 } catch (IOException e) {
106                     // TODO: log
107                 }
108                 logger.debug("Handling packet from {}:{} (len={})", packet.getAddress().getHostAddress(), packet.getPort(), packet.getLength());
109                 try {
110                     DatagramPacket reply = service.handlePacket(packet);
111
112                     if (reply == null) {
113                         continue;
114                     }
115
116                     reply.setAddress(packet.getAddress());
117                     try {
118                         logger.debug("sending reply to {}:{} (len={})", reply.getAddress().getHostAddress(), reply.getPort(), reply.getLength());
119                         socket.send(reply);
120                     } catch (IOException e) {
121                         // TODO: log
122                     }
123                 } catch (RuntimeException e) {
124                     logger.warn("", e);
125                 }
126             }
127
128             socket.close();
129         }
130
131         public void stopRunning() {
132             running = false;
133         }
134     }
135
136
137     public static String intToIpv4(int address) {
138         return ((address >> 24) & 0xff) + "." + //
139                 ((address >> 16) & 0xff) + "." + //
140                 ((address >> 8) & 0xff) + "." + //
141                 ((address >> 0) & 0xff);
142     }
143
144     public String getHelp() {
145         StringBuffer help = new StringBuffer();
146         help.append("---LISP Southbound Plugin---\n");
147         return help.toString();
148     }
149
150 }