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