Merge "Enforce basic checkstyle on mappingservice.*"
[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.util.concurrent.Future;
12
13 import org.apache.commons.lang3.tuple.MutablePair;
14 import org.apache.commons.lang3.tuple.Pair;
15 import org.eclipse.osgi.framework.console.CommandInterpreter;
16 import org.eclipse.osgi.framework.console.CommandProvider;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
20 import org.opendaylight.controller.sal.binding.api.NotificationListener;
21 import org.opendaylight.controller.sal.binding.api.NotificationService;
22 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
23 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKey;
24 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
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.serializer.LispMessage;
29 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
30 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
31 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
32 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
33 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
34 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
35 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMappingShell;
36 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
37 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
38 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
39 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.AddMapping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LfmControlPlaneService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapNotify;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRegister;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapReply;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRequest;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.RequestMapping;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapNotifyInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapReplyInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapRequestInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv4.Ipv4AddressBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapnotifymessage.MapNotifyBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapreplymessage.MapReplyBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequestmessage.MapRequestBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddress;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddressBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddKeyInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddKeyOutput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingInput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingOutput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyOutput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyWithRefInput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyWithRefOutput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingInput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingOutput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingWithRefInput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingWithRefOutput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.LfmMappingDatabaseService;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyInput;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyWithRefInput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingInput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingWithRefInput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyInput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyWithRefInput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingInput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingWithRefInput;
78 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
79 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
80 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
81 import org.opendaylight.yangtools.yang.binding.Notification;
82 import org.opendaylight.yangtools.yang.common.RpcResult;
83 import org.osgi.framework.BundleContext;
84 import org.osgi.framework.FrameworkUtil;
85 import org.slf4j.Logger;
86 import org.slf4j.LoggerFactory;
87
88 public class LispMappingService implements CommandProvider, IFlowMapping, IFlowMappingShell, BindingAwareProvider,
89         IMapRequestResultHandler, IMapNotifyHandler, LfmMappingDatabaseService {
90     protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
91
92     private static final ConfigIni configIni = new ConfigIni();
93     private ILispDAO lispDao = null;
94     private IMapResolverAsync mapResolver;
95     private IMapServerAsync mapServer;
96     private volatile boolean shouldIterateMask;
97     private volatile boolean shouldAuthenticate;
98     private volatile boolean smr = configIni.smrIsSet();
99     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
100     private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
101     private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
102
103     private LfmControlPlaneService lispSB = null;
104
105     private ProviderContext session;
106
107     private NotificationService notificationService;
108
109     class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
110     }
111
112     class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
113     }
114
115     class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
116     }
117
118     class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
119     }
120
121     void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
122         LOG.trace("BindingAwareBroker set!");
123         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
124         bindingAwareBroker.registerProvider(this, bundleContext);
125     }
126
127     void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
128         LOG.debug("BindingAwareBroker was unset in LispMappingService");
129     }
130
131     public void basicInit(ILispDAO dao) {
132         lispDao = dao;
133         mapResolver = new MapResolver(dao);
134         mapServer = new MapServer(dao);
135     }
136
137     void setLispDao(ILispDAO dao) {
138         LOG.trace("LispDAO set in LispMappingService");
139         basicInit(dao);
140     }
141
142     void unsetLispDao(ILispDAO dao) {
143         LOG.trace("LispDAO was unset in LispMappingService");
144         mapServer = null;
145         mapResolver = null;
146         lispDao = null;
147     }
148
149     public void init() {
150         try {
151             registerWithOSGIConsole();
152             LOG.info("LISP (RFC6830) Mapping Service init finished");
153         } catch (Exception e) {
154             LOG.error(e.getStackTrace().toString());
155         }
156     }
157
158     private void registerWithOSGIConsole() {
159         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
160         bundleContext.registerService(CommandProvider.class.getName(), this, null);
161     }
162
163     public void destroy() {
164         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
165         mapResolver = null;
166         mapServer = null;
167     }
168
169     public void _removeEid(final CommandInterpreter ci) {
170         LispAddressContainer eid = LispAFIConvertor.getIPContainer(ci.nextArgument());
171         lispDao.remove(MappingServiceKeyUtil.generateMappingServiceKey(eid));
172     }
173
174     public void _dumpAll(final CommandInterpreter ci) {
175         ci.print(printMappings());
176     }
177
178     public String printMappings() {
179         final StringBuffer sb = new StringBuffer();
180         sb.append("EID\tRLOCs\n");
181         lispDao.getAll(new IRowVisitor() {
182             String lastKey = "";
183
184             public void visitRow(Object keyId, String valueKey, Object value) {
185                 String key = keyId.getClass().getSimpleName() + "#" + keyId;
186                 if (!lastKey.equals(key)) {
187                     sb.append("\n" + key + "\t");
188                 }
189                 sb.append(valueKey + "=" + value + "\t");
190                 lastKey = key;
191             }
192         });
193         sb.append("\n");
194         return sb.toString();
195     }
196
197     public void _setShouldOverwriteRlocs(final CommandInterpreter ci) {
198         try {
199             boolean shouldOverwriteRloc = Boolean.parseBoolean(ci.nextArgument());
200             setOverwrite(shouldOverwriteRloc);
201         } catch (Exception e) {
202             ci.println("Bad Usage!!");
203         }
204
205     }
206
207     public void _addDefaultPassword(final CommandInterpreter ci) {
208         addDefaultKeyIPv4();
209     }
210
211     public void addDefaultKeyIPv4() {
212         LispAddressContainer address = LispAFIConvertor.toContainer(
213                 new Ipv4AddressBuilder().setIpv4Address(new Ipv4Address("0.0.0.0")).build());
214         addAuthenticationKey(address, 0, "password");
215     }
216
217     public String getHelp() {
218         StringBuffer help = new StringBuffer();
219         help.append("---LISP Mapping Service---\n");
220         help.append("\t dumpAll                               - Dump all current EID -> RLOC mappings\n");
221         help.append("\t removeEid <EID>                       - Remove a single EID (/32 or /128)\n");
222         help.append("\t setShouldOverwriteRlocs <true|false>  - Set the map server's behavior regarding existing RLOCs\n");
223         help.append("\t addDefaultPassword                    - Add \"password\" as default password for IPv4 EIDs");
224         return help.toString();
225     }
226
227     public MapReply handleMapRequest(MapRequest request) {
228         return handleMapRequest(request, smr);
229     }
230
231     public MapReply handleMapRequest(MapRequest request, boolean smr) {
232         tlsMapReply.set(null);
233         tlsMapRequest.set(null);
234         mapResolver.handleMapRequest(request, smr, this);
235         // After this invocation we assume that the thread local is filled with
236         // the reply
237         if (tlsMapRequest.get() != null) {
238             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
239             new MapRequestBuilder(tlsMapRequest.get().getLeft());
240             smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
241             smrib.setTransportAddress(tlsMapRequest.get().getRight());
242             getLispSB().sendMapRequest(smrib.build());
243             return null;
244         } else {
245             return tlsMapReply.get();
246         }
247
248     }
249
250     public MapNotify handleMapRegister(MapRegister mapRegister) {
251         return handleMapRegister(mapRegister, smr);
252     }
253
254     public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
255         tlsMapNotify.set(null);
256         mapServer.handleMapRegister(mapRegister, smr, this);
257         // After this invocation we assume that the thread local is filled with
258         // the reply
259         return tlsMapNotify.get();
260     }
261
262     public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
263         return mapServer.getAuthenticationKey(address, maskLen);
264     }
265
266     public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
267         mapServer.removeAuthenticationKey(address, maskLen);
268     }
269
270     public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
271         mapServer.addAuthenticationKey(address, maskLen, key);
272     }
273
274     public void removeMapping(LispAddressContainer address, int maskLen) {
275         mapServer.removeMapping(address, maskLen, smr, this);
276     }
277
278     public boolean shouldIterateMask() {
279         return this.shouldIterateMask;
280     }
281
282     public boolean shouldUseSmr() {
283         return this.smr;
284     }
285
286     public void setShouldUseSmr(boolean smr) {
287         this.smr = smr;
288     }
289
290     public void setShouldIterateMask(boolean shouldIterateMask) {
291         this.shouldIterateMask = shouldIterateMask;
292         this.mapResolver.setShouldIterateMask(shouldIterateMask);
293         this.mapServer.setShouldIterateMask(shouldIterateMask);
294     }
295
296     public void setShouldAuthenticate(boolean shouldAuthenticate) {
297         this.shouldAuthenticate = shouldAuthenticate;
298         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
299         this.mapServer.setShouldAuthenticate(shouldAuthenticate);
300     }
301
302     public boolean shouldAuthenticate() {
303         return shouldAuthenticate;
304     }
305
306     @Override
307     public void onSessionInitiated(ProviderContext session) {
308         LOG.info("Lisp Consumer session initialized!");
309         notificationService = session.getSALService(NotificationService.class);
310         registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
311         registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
312         session.addRpcImplementation(LfmMappingDatabaseService.class, this);
313         this.session = session;
314     }
315
316     public <T extends Notification> void registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
317         notificationService.registerNotificationListener(notificationType, listener);
318     }
319
320     private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
321
322         @Override
323         public void onNotification(AddMapping mapRegisterNotification) {
324             MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister(), smr);
325             if (mapNotify != null) {
326                 TransportAddressBuilder tab = new TransportAddressBuilder();
327                 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
328                 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
329                 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
330                 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
331                 smnib.setTransportAddress(tab.build());
332                 getLispSB().sendMapNotify(smnib.build());
333             } else {
334                 LOG.warn("got null map notify");
335             }
336
337         }
338     }
339
340     private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
341
342         @Override
343         public void onNotification(RequestMapping mapRequestNotification) {
344             MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
345             if (mapReply != null) {
346                 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
347                 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
348                 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
349                 getLispSB().sendMapReply(smrib.build());
350             } else {
351                 LOG.warn("got null map reply");
352             }
353         }
354     }
355
356     private LfmControlPlaneService getLispSB() {
357         if (lispSB == null) {
358             lispSB = session.getRpcService(LfmControlPlaneService.class);
359         }
360         return lispSB;
361     }
362
363     public void handleMapReply(MapReply reply) {
364         tlsMapReply.set(reply);
365     }
366
367     public void handleMapNotify(MapNotify notify) {
368         tlsMapNotify.set(notify);
369     }
370
371     public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
372         LOG.debug("Sending SMR to " + subscriber.toString());
373         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
374         smrib.setMapRequest(new MapRequestBuilder(smr).build());
375         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
376         getLispSB().sendMapRequest(smrib.build());
377
378     }
379
380     @Override
381     public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
382         tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
383     }
384
385     @Override
386     public void clean() {
387         lispDao.removeAll();
388     }
389
390     @Override
391     public boolean shouldOverwrite() {
392         return mapServer.shouldOverwrite();
393     }
394
395     @Override
396     public void setOverwrite(boolean overwrite) {
397         mapServer.setOverwrite(overwrite);
398     }
399
400     @Override
401     public Future<RpcResult<AddKeyOutput>> addKey(AddKeyInput input) {
402         // TODO Auto-generated method stub
403         return null;
404     }
405
406     @Override
407     public Future<RpcResult<AddMappingOutput>> addMapping(AddMappingInput input) {
408         // TODO Auto-generated method stub
409         return null;
410     }
411
412     @Override
413     public Future<RpcResult<GetKeyOutput>> getKey(GetKeyInput input) {
414         // TODO Auto-generated method stub
415         return null;
416     }
417
418     @Override
419     public Future<RpcResult<GetKeyWithRefOutput>> getKeyWithRef(
420             GetKeyWithRefInput input) {
421         // TODO Auto-generated method stub
422         return null;
423     }
424
425     @Override
426     public Future<RpcResult<GetMappingOutput>> getMapping(GetMappingInput input) {
427         // TODO Auto-generated method stub
428         return null;
429     }
430
431     @Override
432     public Future<RpcResult<GetMappingWithRefOutput>> getMappingWithRef(
433             GetMappingWithRefInput input) {
434         // TODO Auto-generated method stub
435         return null;
436     }
437
438     @Override
439     public Future<RpcResult<Void>> removeKey(RemoveKeyInput input) {
440         // TODO Auto-generated method stub
441         return null;
442     }
443
444     @Override
445     public Future<RpcResult<Void>> removeKeyWithRef(RemoveKeyWithRefInput input) {
446         // TODO Auto-generated method stub
447         return null;
448     }
449
450     @Override
451     public Future<RpcResult<Void>> removeMapping(RemoveMappingInput input) {
452         // TODO Auto-generated method stub
453         return null;
454     }
455
456     @Override
457     public Future<RpcResult<Void>> removeMappingWithRef(
458             RemoveMappingWithRefInput input) {
459         // TODO Auto-generated method stub
460         return null;
461     }
462
463     @Override
464     public Future<RpcResult<Void>> updateKey(UpdateKeyInput input) {
465         // TODO Auto-generated method stub
466         return null;
467     }
468
469     @Override
470     public Future<RpcResult<Void>> updateKeyWithRef(UpdateKeyWithRefInput input) {
471         // TODO Auto-generated method stub
472         return null;
473     }
474
475     @Override
476     public Future<RpcResult<Void>> updateMapping(UpdateMappingInput input) {
477         // TODO Auto-generated method stub
478         return null;
479     }
480
481     @Override
482     public Future<RpcResult<Void>> updateMappingWithRef(
483             UpdateMappingWithRefInput input) {
484         // TODO Auto-generated method stub
485         return null;
486     }
487 }