Merge remote branch 'origin/release-1.0.X' into mer
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LispMappingService.java
1 /*
2  * Copyright (c) 2014 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.implementation;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13
14 import org.apache.commons.lang3.tuple.MutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.eclipse.osgi.framework.console.CommandInterpreter;
17 import org.eclipse.osgi.framework.console.CommandProvider;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
21 import org.opendaylight.controller.sal.binding.api.NotificationListener;
22 import org.opendaylight.controller.sal.binding.api.NotificationService;
23 import org.opendaylight.lispflowmapping.implementation.dao.InMemoryDAO;
24 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKey;
25 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceNoMaskKey;
26 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
27 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
28 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
29 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
30 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
31 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
32 import org.opendaylight.lispflowmapping.interfaces.dao.IQueryAll;
33 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
34 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
35 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
36 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
37 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
38 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
39 import org.opendaylight.lispflowmapping.type.sbplugin.ILispSouthboundPlugin;
40 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.AddMapping;
41 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
42 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
43 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
44 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
45 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.RequestMapping;
46 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
47 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
48 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.Address;
49 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
52 import org.osgi.framework.BundleContext;
53 import org.osgi.framework.FrameworkUtil;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 import com.google.common.net.InetAddresses;
58
59 public class LispMappingService implements CommandProvider, IFlowMapping, BindingAwareConsumer, //
60         IMapRequestResultHandler, IMapNotifyHandler {
61     protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
62
63     private ILispDAO lispDao = null;
64     private IMapResolverAsync mapResolver;
65     private IMapServerAsync mapServer;
66     private volatile boolean shouldIterateMask;
67     private volatile boolean shouldAuthenticate;
68     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
69     private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
70     private ThreadLocal<Pair<MapRequest, InetAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, InetAddress>>();
71
72     private ILispSouthboundPlugin lispSB = null;
73
74     private ConsumerContext session;
75
76     public static void main(String[] args) throws Exception {
77         LispMappingService serv = new LispMappingService();
78         serv.setLispDao(new InMemoryDAO());
79         serv.init();
80     }
81
82     class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
83     }
84
85     class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
86     }
87
88     class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
89     }
90
91     class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
92     }
93
94     void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
95         logger.trace("BindingAwareBroker set!");
96         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
97         bindingAwareBroker.registerConsumer(this, bundleContext);
98     }
99
100     void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
101         logger.debug("BindingAwareBroker was unset in LispMappingService");
102     }
103
104     public void basicInit(ILispDAO dao) {
105         lispDao = dao;
106         mapResolver = new MapResolver(dao);
107         mapServer = new MapServer(dao);
108     }
109
110     void setLispDao(ILispDAO dao) {
111         logger.trace("LispDAO set in LispMappingService");
112         basicInit(dao);
113         logger.trace("Registering LispIpv4Address");
114         lispDao.register(LispIpv4AddressInMemoryConverter.class);
115         logger.trace("Registering LispIpv6Address");
116         lispDao.register(LispIpv6AddressInMemoryConverter.class);
117         logger.trace("Registering MappingServiceKey");
118         lispDao.register(MappingServiceKeyConvertor.class);
119         logger.trace("Registering MappingServiceNoMaskKey");
120         lispDao.register(MappingServiceNoMaskKeyConvertor.class);
121     }
122
123     void unsetLispDao(ILispDAO dao) {
124         logger.trace("LispDAO was unset in LispMappingService");
125         mapServer = null;
126         mapResolver = null;
127         lispDao = null;
128     }
129
130     public void init() {
131         try {
132             registerWithOSGIConsole();
133             logger.info("LISP (RFC6830) Mapping Service init finished");
134         } catch (Throwable t) {
135             logger.error(t.getStackTrace().toString());
136         }
137     }
138
139     private void registerWithOSGIConsole() {
140         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
141         bundleContext.registerService(CommandProvider.class.getName(), this, null);
142     }
143
144     public void destroy() {
145         logger.info("LISP (RFC6830) Mapping Service is destroyed!");
146         mapResolver = null;
147         mapServer = null;
148     }
149
150     public void _removeEid(final CommandInterpreter ci) {
151         lispDao.remove(LispAFIConvertor.asIPAfiAddress(ci.nextArgument()));
152     }
153
154     public void _dumpAll(final CommandInterpreter ci) {
155         ci.println("EID\tRLOCs");
156         if (lispDao instanceof IQueryAll) {
157             ((IQueryAll) lispDao).getAll(new IRowVisitor() {
158                 String lastKey = "";
159
160                 public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
161                     String key = keyType.getSimpleName() + "#" + keyId;
162                     if (!lastKey.equals(key)) {
163                         ci.println();
164                         ci.print(key + "\t");
165                     }
166                     ci.print(valueKey + "=" + value + "\t");
167                     lastKey = key;
168                 }
169             });
170             ci.println();
171         } else {
172             ci.println("Not implemented by this DAO");
173         }
174         return;
175     }
176
177     public void _addDefaultPassword(final CommandInterpreter ci) {
178         LispAddressContainerBuilder builder = new LispAddressContainerBuilder();
179         builder.setAddress((Address) (new Ipv4Builder().setIpv4Address(new Ipv4Address("0.0.0.0")).build()));
180         addAuthenticationKey(builder.build(), 0, "password");
181     }
182
183     public String getHelp() {
184         StringBuffer help = new StringBuffer();
185         help.append("---LISP Mapping Service---\n");
186         help.append("\t dumpAll        - Dump all current EID -> RLOC mapping\n");
187         help.append("\t removeEid      - Remove a single LispIPv4Address Eid\n");
188         return help.toString();
189     }
190
191     public MapReply handleMapRequest(MapRequest request) {
192         tlsMapReply.set(null);
193         tlsMapRequest.set(null);
194         mapResolver.handleMapRequest(request, this);
195         // After this invocation we assume that the thread local is filled with
196         // the reply
197         if (tlsMapRequest.get() != null) {
198             getLispSB().handleMapRequest(tlsMapRequest.get().getLeft(), tlsMapRequest.get().getRight());
199             return null;
200         } else {
201             return tlsMapReply.get();
202         }
203
204     }
205
206     public MapNotify handleMapRegister(MapRegister mapRegister) {
207         tlsMapNotify.set(null);
208         mapServer.handleMapRegister(mapRegister, this);
209         // After this invocation we assume that the thread local is filled with
210         // the reply
211         return tlsMapNotify.get();
212     }
213
214     public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
215         return mapServer.getAuthenticationKey(address, maskLen);
216     }
217
218     public boolean removeAuthenticationKey(LispAddressContainer address, int maskLen) {
219         return mapServer.removeAuthenticationKey(address, maskLen);
220     }
221
222     public boolean addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
223         return mapServer.addAuthenticationKey(address, maskLen, key);
224     }
225
226     public boolean shouldIterateMask() {
227         return this.shouldIterateMask;
228     }
229
230     public void setShouldIterateMask(boolean shouldIterateMask) {
231         this.shouldIterateMask = shouldIterateMask;
232         this.mapResolver.setShouldIterateMask(shouldIterateMask);
233         this.mapServer.setShouldIterateMask(shouldIterateMask);
234     }
235
236     public void setShouldAuthenticate(boolean shouldAuthenticate) {
237         this.shouldAuthenticate = shouldAuthenticate;
238         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
239         this.mapServer.setShouldAuthenticate(shouldAuthenticate);
240     }
241
242     public boolean shouldAuthenticate() {
243         return shouldAuthenticate;
244     }
245
246     public void onSessionInitialized(ConsumerContext session) {
247         logger.info("Lisp Consumer session initialized!");
248         NotificationService notificationService = session.getSALService(NotificationService.class);
249         // notificationService.registerNotificationListener(LispNotification.class,
250         // this);
251         notificationService.registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
252         notificationService.registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
253         this.session = session;
254     }
255
256     private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
257
258         @Override
259         public void onNotification(AddMapping mapRegisterNotification) {
260             MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
261             getLispSB().handleMapNotify(mapNotify,
262                     LispNotificationHelper.getInetAddressFromIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress()));
263
264         }
265     }
266
267     private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
268
269         @Override
270         public void onNotification(RequestMapping mapRequestNotification) {
271             MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
272             getLispSB().handleMapReply(mapReply,
273                     LispNotificationHelper.getInetAddressFromIpAddress(mapRequestNotification.getTransportAddress().getIpAddress()));
274         }
275
276     }
277
278     private ILispSouthboundPlugin getLispSB() {
279         if (lispSB == null) {
280             lispSB = session.getRpcService(ILispSouthboundPlugin.class);
281         }
282         return lispSB;
283     }
284
285     public void handleMapReply(MapReply reply) {
286         tlsMapReply.set(reply);
287     }
288
289     public void handleMapNotify(MapNotify notify) {
290         tlsMapNotify.set(notify);
291     }
292
293     @Override
294     public void handleNonProxyMapRequest(MapRequest mapRequest, InetAddress targetAddress) {
295         tlsMapRequest.set(new MutablePair<MapRequest, InetAddress>(mapRequest, targetAddress));
296     }
297
298 }