Prepare for odlparent 3.0.0 checkstyle rules
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / MappingSystem.java
1 /*
2  * Copyright (c) 2015, 2017 Cisco Systems, Inc.  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 com.google.common.collect.Sets;
12 import java.util.ArrayList;
13 import java.util.Date;
14 import java.util.EnumMap;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.lispflowmapping.config.ConfigIni;
24 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
25 import org.opendaylight.lispflowmapping.implementation.timebucket.implementation.TimeBucketMappingTimeoutService;
26 import org.opendaylight.lispflowmapping.implementation.timebucket.interfaces.ISouthBoundMappingTimeoutService;
27 import org.opendaylight.lispflowmapping.implementation.util.DSBEInputUtil;
28 import org.opendaylight.lispflowmapping.implementation.util.LoggingUtil;
29 import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil;
30 import org.opendaylight.lispflowmapping.implementation.util.MappingMergeUtil;
31 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
32 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
33 import org.opendaylight.lispflowmapping.interfaces.dao.Subscriber;
34 import org.opendaylight.lispflowmapping.interfaces.mapcache.IAuthKeyDb;
35 import org.opendaylight.lispflowmapping.interfaces.mapcache.ILispMapCache;
36 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
37 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
38 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
39 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
40 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
41 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
42 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
43 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
44 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
45 import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
46 import org.opendaylight.lispflowmapping.mapcache.MultiTableMapCache;
47 import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
48 import org.opendaylight.lispflowmapping.mapcache.lisp.LispMapCacheStringifier;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6PrefixBinaryAfi;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinary;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinary;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76
77 /**
78  * The Mapping System coordinates caching of md-sal stored mappings and if so configured enables longest prefix match
79  * mapping lookups.
80  *
81  * @author Florin Coras
82  * @author Lorand Jakab
83  *
84  */
85 public class MappingSystem implements IMappingSystem {
86     private static final Logger LOG = LoggerFactory.getLogger(MappingSystem.class);
87     private static final String AUTH_KEY_TABLE = "authentication";
88     //private static final int TTL_RLOC_TIMED_OUT = 1;
89     private static final int TTL_NO_RLOC_KNOWN = ConfigIni.getInstance().getNegativeMappingTTL();
90     private NotificationPublishService notificationPublishService;
91     private boolean mappingMerge;
92     private ILispDAO dao;
93     private ILispDAO sdao;
94     private ILispMapCache smc;
95     private IMapCache pmc;
96     private ConcurrentHashMap<Eid, Set<Subscriber>> subscriberdb = new ConcurrentHashMap<>();
97     private IAuthKeyDb akdb;
98     private final EnumMap<MappingOrigin, IMapCache> tableMap = new EnumMap<>(MappingOrigin.class);
99     private DataStoreBackEnd dsbe;
100     private boolean isMaster = false;
101
102     private ISouthBoundMappingTimeoutService sbMappingTimeoutService;
103
104     public MappingSystem(ILispDAO dao, boolean iterateMask, NotificationPublishService nps, boolean mappingMerge) {
105         this.dao = dao;
106         this.notificationPublishService = nps;
107         this.mappingMerge = mappingMerge;
108         buildMapCaches();
109
110         sbMappingTimeoutService = new TimeBucketMappingTimeoutService(ConfigIni.getInstance()
111                 .getNumberOfBucketsInTimeBucketWheel(), ConfigIni.getInstance().getRegistrationValiditySb(),
112                 this);
113     }
114
115     public void setDataStoreBackEnd(DataStoreBackEnd dataStoreBackEnd) {
116         this.dsbe = dataStoreBackEnd;
117     }
118
119     @Override
120     public void setMappingMerge(boolean mappingMerge) {
121         this.mappingMerge = mappingMerge;
122     }
123
124     @Override
125     public void setIterateMask(boolean iterate) {
126         LOG.error("Non-longest prefix match lookups are not properly supported, variable is set to true");
127     }
128
129     public void initialize() {
130         restoreDaoFromDatastore();
131     }
132
133     private void buildMapCaches() {
134         /*
135          * There exists a direct relationship between MappingOrigins and the tables that are part of the MappingSystem.
136          * Therefore, if a new origin is added, probably a new table should be instantiated here as well. Here we
137          * instantiate a SimpleMapCache for southbound originated LISP mappings and a MultiTableMapCache for northbound
138          * originated mappings. Use of FlatMapCache would be possible when no longest prefix match is needed at all,
139          * but that option is no longer supported in the code, since it was never tested and may lead to unexpected
140          * results.
141          */
142         sdao = dao.putTable(MappingOrigin.Southbound.toString());
143         pmc = new MultiTableMapCache(dao.putTable(MappingOrigin.Northbound.toString()));
144         smc = new SimpleMapCache(sdao);
145         akdb = new AuthKeyDb(dao.putTable(AUTH_KEY_TABLE));
146         tableMap.put(MappingOrigin.Northbound, pmc);
147         tableMap.put(MappingOrigin.Southbound, smc);
148     }
149
150     public void addMapping(MappingOrigin origin, Eid key, MappingData mappingData) {
151
152         sbMappingTimeoutService.removeExpiredMappings();
153
154         if (mappingData == null) {
155             LOG.warn("addMapping() called with null mapping, ignoring");
156             return;
157         }
158
159         if (origin == MappingOrigin.Southbound) {
160             XtrId xtrId = mappingData.getXtrId();
161             if (xtrId == null && mappingMerge && mappingData.isMergeEnabled()) {
162                 LOG.warn("addMapping() called will null xTR-ID in MappingRecord, while merge is set, ignoring");
163                 return;
164             }
165             if (xtrId != null && mappingMerge) {
166                 if (mappingData.isMergeEnabled()) {
167                     smc.addMapping(key, xtrId, mappingData);
168                     handleMergedMapping(key);
169                     return;
170                 } else {
171                     clearPresentXtrIdMappings(key);
172                     smc.addMapping(key, xtrId, mappingData);
173                 }
174             }
175             addOrRefreshMappingInTimeoutService(key, mappingData);
176         }
177
178         tableMap.get(origin).addMapping(key, mappingData);
179
180         // We store explicit negative mappings in SB. That may cause issues, see bug 9037 for details
181         if (origin != MappingOrigin.Southbound) {
182             handleSbNegativeMappings(key);
183         }
184     }
185
186     @SuppressWarnings("unchecked")
187     private void clearPresentXtrIdMappings(Eid key) {
188         List<MappingData> allXtrMappingList = (List<MappingData>) (List<?>) smc.getAllXtrIdMappings(key);
189
190         if (((MappingData) smc.getMapping(key, (XtrId) null)).isMergeEnabled()) {
191             LOG.trace("Different xTRs have different merge configuration!");
192         }
193
194         for (MappingData mappingData : allXtrMappingList) {
195             removeSbXtrIdSpecificMapping(key, mappingData.getXtrId(), mappingData);
196         }
197     }
198
199     private void addOrRefreshMappingInTimeoutService(Eid key, MappingData mappingData) {
200         Integer oldBucketId = (Integer) smc.getData(key, SubKeys.TIME_BUCKET_ID);
201         Integer updatedBucketId;
202
203         if (oldBucketId != null) {
204             //refresh mapping
205             updatedBucketId = sbMappingTimeoutService.refreshMapping(key, mappingData, oldBucketId);
206         } else {
207             updatedBucketId = sbMappingTimeoutService.addMapping(key, mappingData);
208         }
209
210         smc.addData(key, SubKeys.TIME_BUCKET_ID, updatedBucketId);
211     }
212
213     private void handleSbNegativeMappings(Eid key) {
214         MappingData mappingData = getSbMappingWithExpiration(null, key, null);
215         if (mappingData != null && mappingData.isNegative().or(false)) {
216             removeSbMapping(mappingData.getRecord().getEid(), mappingData);
217         }
218     }
219
220     @Override
221     public MappingData addNegativeMapping(Eid key) {
222         MappingRecord mapping = buildNegativeMapping(key);
223         MappingData mappingData = new MappingData(mapping);
224         LOG.debug("Adding negative mapping for EID {}", LispAddressStringifier.getString(key));
225         LOG.trace(mappingData.getString());
226         smc.addMapping(mapping.getEid(), mappingData);
227         dsbe.addMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, mapping.getEid(), null, mappingData));
228         return mappingData;
229     }
230
231     private MappingRecord buildNegativeMapping(Eid eid) {
232         MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
233         recordBuilder.setAuthoritative(false);
234         recordBuilder.setMapVersion((short) 0);
235         recordBuilder.setEid(eid);
236         if (eid.getAddressType().equals(Ipv4PrefixBinaryAfi.class)
237                 || eid.getAddressType().equals(Ipv6PrefixBinaryAfi.class)) {
238             Eid widestNegativePrefix = getWidestNegativePrefix(eid);
239             if (widestNegativePrefix != null) {
240                 recordBuilder.setEid(widestNegativePrefix);
241             }
242         }
243         recordBuilder.setAction(LispMessage.NEGATIVE_MAPPING_ACTION);
244         //if (getAuthenticationKey(eid) != null) {
245         //    recordBuilder.setRecordTtl(TTL_RLOC_TIMED_OUT);
246         //} else {
247         recordBuilder.setRecordTtl(TTL_NO_RLOC_KNOWN);
248         //}
249         return recordBuilder.build();
250     }
251
252     /*
253      * Since this method is only called when there is a hit in the southbound Map-Register cache, and that cache is
254      * not used when merge is on, it's OK to ignore the effects of timestamp changes on merging for now.
255      */
256     public void refreshMappingRegistration(Eid key, XtrId xtrId, Long timestamp) {
257
258         sbMappingTimeoutService.removeExpiredMappings();
259
260         if (timestamp == null) {
261             timestamp = System.currentTimeMillis();
262         }
263         MappingData mappingData = (MappingData) smc.getMapping(null, key);
264         if (mappingData != null) {
265             mappingData.setTimestamp(new Date(timestamp));
266             addOrRefreshMappingInTimeoutService(key, mappingData);
267         } else {
268             LOG.warn("Could not update timestamp for EID {}, no mapping found", LispAddressStringifier.getString(key));
269         }
270         if (mappingMerge && xtrId != null) {
271             MappingData xtrIdMappingData = (MappingData) smc.getMapping(key, xtrId);
272             if (xtrIdMappingData != null) {
273                 xtrIdMappingData.setTimestamp(new Date(timestamp));
274             } else {
275                 LOG.warn("Could not update timestamp for EID {} xTR-ID {}, no mapping found",
276                         LispAddressStringifier.getString(key), LispAddressStringifier.getString(xtrId));
277             }
278         }
279     }
280
281     private MappingData updateServicePathMappingRecord(MappingData mappingData, Eid eid) {
282         // keep properties of original record
283         MappingRecordBuilder recordBuilder = new MappingRecordBuilder(mappingData.getRecord());
284         recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
285
286         // there should only be one locator record
287         if (mappingData.getRecord().getLocatorRecord().size() != 1) {
288             LOG.warn("MappingRecord associated to ServicePath EID has more than one locator!");
289             return mappingData;
290         }
291
292         LocatorRecord locatorRecord = mappingData.getRecord().getLocatorRecord().get(0);
293         long serviceIndex = ((ServicePath) eid.getAddress()).getServicePath().getServiceIndex();
294         int index = LispAddressUtil.STARTING_SERVICE_INDEX - (int) serviceIndex;
295         Rloc rloc = locatorRecord.getRloc();
296         if (rloc.getAddress() instanceof Ipv4 || rloc.getAddress() instanceof Ipv6) {
297             if (index != 0) {
298                 LOG.warn("Service Index should be 255 for simple IP RLOCs!");
299             }
300             return mappingData;
301         } else if (rloc.getAddress() instanceof ExplicitLocatorPath) {
302             ExplicitLocatorPath elp = (ExplicitLocatorPath) rloc.getAddress();
303             List<Hop> hops = elp.getExplicitLocatorPath().getHop();
304
305             if (index < 0 || index > hops.size())  {
306                 LOG.warn("Service Index out of bounds!");
307                 return mappingData;
308             }
309
310             SimpleAddress nextHop = hops.get(index).getAddress();
311             LocatorRecordBuilder lrb = new LocatorRecordBuilder(locatorRecord);
312             lrb.setRloc(LispAddressUtil.toRloc(nextHop));
313             recordBuilder.getLocatorRecord().add(lrb.build());
314             return new MappingData(recordBuilder.build());
315         } else {
316             LOG.warn("Nothing to do with ServicePath mapping record");
317             return mappingData;
318         }
319     }
320
321     private MappingData handleMergedMapping(Eid key) {
322         List<MappingData> expiredMappingDataList = new ArrayList<>();
323         Set<IpAddressBinary> sourceRlocs = new HashSet<>();
324
325         MappingData mergedMappingData = MappingMergeUtil.mergeXtrIdMappings(smc.getAllXtrIdMappings(key),
326                 expiredMappingDataList, sourceRlocs);
327
328         for (MappingData mappingData : expiredMappingDataList) {
329             removeSbXtrIdSpecificMapping(key, mappingData.getXtrId(), mappingData);
330         }
331
332         if (mergedMappingData != null) {
333             smc.addMapping(key, mergedMappingData, sourceRlocs);
334             dsbe.addMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, key, mergedMappingData));
335             addOrRefreshMappingInTimeoutService(key, mergedMappingData);
336         } else {
337             removeSbMapping(key, mergedMappingData);
338         }
339         return mergedMappingData;
340     }
341
342     @Override
343     public MappingData getMapping(Eid src, Eid dst) {
344         // NOTE: Currently we have two lookup algorithms implemented, which are configurable
345
346         if (ConfigIni.getInstance().getLookupPolicy() == IMappingService.LookupPolicy.NB_AND_SB) {
347             return getMappingNbSbIntersection(src, dst);
348         } else {
349             return getMappingNbFirst(src, dst);
350         }
351     }
352
353     @Override
354     public MappingData getMapping(Eid dst) {
355         return getMapping((Eid) null, dst);
356     }
357
358     @Override
359     public MappingData getMapping(Eid src, Eid dst, XtrId xtrId) {
360         // Note: If xtrId is null, we need to go through regular policy checking else Policy doesn't matter
361
362         if (xtrId == null) {
363             return getMapping(src, dst);
364         }
365
366         return getSbMappingWithExpiration(src, dst, xtrId);
367     }
368
369     @Override
370     public MappingData getMapping(MappingOrigin origin, Eid key) {
371         if (origin.equals(MappingOrigin.Southbound)) {
372             return getSbMappingWithExpiration(null, key, null);
373         }
374         return (MappingData) tableMap.get(origin).getMapping(null, key);
375     }
376
377     private MappingData getMappingNbFirst(Eid src, Eid dst) {
378
379         // Default lookup policy is northboundFirst
380         //lookupPolicy == NB_FIRST
381
382         MappingData nbMappingData = (MappingData) pmc.getMapping(src, dst);
383
384         if (nbMappingData == null) {
385             return getSbMappingWithExpiration(src, dst, null);
386         }
387         if (dst.getAddress() instanceof ServicePath) {
388             return updateServicePathMappingRecord(nbMappingData, dst);
389         }
390         return nbMappingData;
391     }
392
393     private MappingData getMappingNbSbIntersection(Eid src, Eid dst) {
394         //lookupPolicy == NB_AND_SB, we return intersection
395         //of NB and SB mappings, or NB mapping if intersection is empty.
396
397         MappingData nbMappingData = (MappingData) pmc.getMapping(src, dst);
398         if (nbMappingData == null) {
399             return nbMappingData;
400         }
401         // no intersection for Service Path mappings
402         if (dst.getAddress() instanceof ServicePath) {
403             return updateServicePathMappingRecord(nbMappingData, dst);
404         }
405         MappingData sbMappingData = getSbMappingWithExpiration(src, dst, null);
406         if (sbMappingData == null) {
407             return nbMappingData;
408         }
409         // both NB and SB mappings exist. Compute intersection of the mappings
410         return MappingMergeUtil.computeNbSbIntersection(nbMappingData, sbMappingData);
411     }
412
413     private MappingData getSbMappingWithExpiration(Eid src, Eid dst, XtrId xtrId) {
414         MappingData mappingData = (MappingData) smc.getMapping(dst, xtrId);
415         if (mappingData != null && MappingMergeUtil.mappingIsExpired(mappingData)) {
416             return handleSbExpiredMapping(dst, xtrId, mappingData);
417         } else {
418             return mappingData;
419         }
420     }
421
422     public MappingData handleSbExpiredMapping(Eid key, XtrId xtrId, MappingData mappingData) {
423         if (mappingMerge && mappingData.isMergeEnabled()) {
424             return handleMergedMapping(key);
425         }
426
427         if (xtrId != null) {
428             removeSbXtrIdSpecificMapping(key, xtrId, mappingData);
429         } else {
430             removeSbMapping(key, mappingData);
431         }
432         return null;
433     }
434
435     private void removeSbXtrIdSpecificMapping(Eid key, XtrId xtrId, MappingData mappingData) {
436         smc.removeMapping(key, xtrId);
437         dsbe.removeXtrIdMapping(DSBEInputUtil.toXtrIdMapping(mappingData));
438     }
439
440     private void removeSbMapping(Eid key, MappingData mappingData) {
441         if (mappingData != null && mappingData.getXtrId() != null) {
442             removeSbXtrIdSpecificMapping(key, mappingData.getXtrId(), mappingData);
443         }
444
445         removeFromSbTimeoutService(key);
446         Set<Subscriber> subscribers = getSubscribers(key);
447         smc.removeMapping(key);
448         dsbe.removeMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, key, mappingData));
449         notifyChange(mappingData, subscribers, null, MappingChange.Removed);
450         removeSubscribers(key);
451     }
452
453     private void removeFromSbTimeoutService(Eid key) {
454         Integer bucketId = (Integer) smc.getData(key, SubKeys.TIME_BUCKET_ID);
455         if (bucketId != null) {
456             sbMappingTimeoutService.removeMappingFromTimeoutService(key, bucketId);
457         }
458     }
459
460     @Override
461     public Eid getWidestNegativePrefix(Eid key) {
462         if (!MaskUtil.isMaskable(key.getAddress())) {
463             LOG.warn("Widest negative prefix only makes sense for maskable addresses!");
464             return null;
465         }
466
467         // We assume that ILispMapCache#getWidestNegativeMapping() returns null for positive mappings, and 0/0
468         // for empty cache.
469         Eid nbPrefix = pmc.getWidestNegativeMapping(key);
470         if (nbPrefix == null) {
471             LOG.trace("getWidestNegativePrefix NB: positive mapping, returning null");
472             return null;
473         }
474         if (LOG.isTraceEnabled()) {
475             LOG.trace("getWidestNegativePrefix NB: {}", LispAddressStringifier.getString(nbPrefix));
476         }
477
478         Eid sbPrefix = smc.getWidestNegativeMapping(key);
479         if (sbPrefix == null) {
480             LOG.trace("getWidestNegativePrefix SB: positive mapping, returning null");
481             return null;
482         }
483         if (LOG.isTraceEnabled()) {
484             LOG.trace("getWidestNegativePrefix SB: {}", LispAddressStringifier.getString(sbPrefix));
485         }
486
487         // since prefixes overlap, just return the more specific (larger mask)
488         if (LispAddressUtil.getIpPrefixMask(nbPrefix) < LispAddressUtil.getIpPrefixMask(sbPrefix)) {
489             return sbPrefix;
490         } else {
491             return nbPrefix;
492         }
493     }
494
495     @Override
496     public void removeMapping(MappingOrigin origin, Eid key) {
497         Set<Subscriber> subscribers = null;
498         Set<Subscriber> dstSubscribers = null;
499         MappingData mapping = (MappingData) tableMap.get(origin).getMapping(null, key);
500
501         if (LOG.isDebugEnabled()) {
502             LOG.debug("Removing mapping for EID {} from {}",
503                     LispAddressStringifier.getString(key), origin);
504         }
505         if (LOG.isTraceEnabled()) {
506             LOG.trace(mapping.getString());
507         }
508
509         MappingData notificationMapping = mapping;
510
511         if (mapping != null) {
512             subscribers = getSubscribers(key);
513             // For SrcDst LCAF also send SMRs to Dst prefix
514             if (key.getAddress() instanceof SourceDestKey) {
515                 Eid dstAddr = SourceDestKeyHelper.getDstBinary(key);
516                 dstSubscribers = getSubscribers(dstAddr);
517                 if (!(mapping.getRecord().getEid().getAddress() instanceof SourceDestKey)) {
518                     notificationMapping = new MappingData(new MappingRecordBuilder().setEid(key).build());
519                 }
520             }
521         }
522
523         removeSubscribers(key);
524
525         if (origin == MappingOrigin.Southbound) {
526             removeFromSbTimeoutService(key);
527         }
528
529         if (origin == MappingOrigin.Southbound && mapping != null && mapping.isPositive().or(false)) {
530             mergeNegativePrefixes(key);
531         } else {
532             // mergeNegativePrefixes() above removes the mapping, so addNegativeMapping() will work correctly
533             tableMap.get(origin).removeMapping(key);
534         }
535
536         if (notificationMapping != null) {
537             notifyChange(notificationMapping, subscribers, dstSubscribers, MappingChange.Removed);
538         }
539     }
540
541     private void notifyChange(MappingData mapping, Set<Subscriber> subscribers, Set<Subscriber> dstSubscribers,
542             MappingChange mappingChange) {
543         MappingChanged notification = MSNotificationInputUtil.toMappingChanged(mapping, subscribers, dstSubscribers,
544                 mappingChange);
545         try {
546             notificationPublishService.putNotification(notification);
547         } catch (InterruptedException e) {
548             LOG.warn("Notification publication interrupted!");
549         }
550     }
551
552
553     /*
554      * Merges adjacent negative prefixes and notifies their subscribers.
555      */
556     private void mergeNegativePrefixes(Eid eid) {
557         LOG.debug("Merging negative prefixes starting from EID {}", LispAddressStringifier.getString(eid));
558
559         // If we delete nodes while we walk up the radix trie the algorithm will give incorrect results, because
560         // removals rearrange relationships in the trie. So we save prefixes to be removed into a HashMap.
561         Map<Eid, MappingData> mergedMappings = new HashMap<>();
562
563         Eid currentNode = smc.getSiblingPrefix(eid);
564         MappingData mapping = (MappingData) smc.getMapping(null, currentNode);
565         if (mapping != null && mapping.isNegative().or(false)) {
566             mergedMappings.put(currentNode, mapping);
567         } else {
568             return;
569         }
570
571         Eid previousNode = currentNode;
572         currentNode = smc.getVirtualParentSiblingPrefix(currentNode);
573         while (currentNode != null) {
574             mapping = (MappingData) smc.getMapping(null, currentNode);
575             if (mapping != null && mapping.isNegative().or(false)) {
576                 mergedMappings.put(currentNode, mapping);
577             } else {
578                 break;
579             }
580             previousNode = currentNode;
581             currentNode = smc.getVirtualParentSiblingPrefix(previousNode);
582         }
583
584         for (Eid key : mergedMappings.keySet()) {
585             removeSbMapping(key, mergedMappings.get(key));
586         }
587         smc.removeMapping(eid);
588
589         addNegativeMapping(getVirtualParent(previousNode));
590     }
591
592     private static Eid getVirtualParent(Eid eid) {
593         if (eid.getAddress() instanceof Ipv4PrefixBinary) {
594             Ipv4PrefixBinary prefix = (Ipv4PrefixBinary) eid.getAddress();
595             short parentPrefixLength = (short) (prefix.getIpv4MaskLength() - 1);
596             byte[] parentPrefix = MaskUtil.normalizeByteArray(prefix.getIpv4AddressBinary().getValue(),
597                     parentPrefixLength);
598             return LispAddressUtil.asIpv4PrefixBinaryEid(eid, parentPrefix, parentPrefixLength);
599         } else if (eid.getAddress() instanceof Ipv6PrefixBinary) {
600             Ipv6PrefixBinary prefix = (Ipv6PrefixBinary) eid.getAddress();
601             short parentPrefixLength = (short) (prefix.getIpv6MaskLength() - 1);
602             byte[] parentPrefix = MaskUtil.normalizeByteArray(prefix.getIpv6AddressBinary().getValue(),
603                     parentPrefixLength);
604             return LispAddressUtil.asIpv6PrefixBinaryEid(eid, parentPrefix, parentPrefixLength);
605         }
606         return null;
607     }
608
609     @Override
610     public synchronized void subscribe(Subscriber subscriber, Eid subscribedEid) {
611         Set<Subscriber> subscribers = getSubscribers(subscribedEid);
612         if (subscribers == null) {
613             subscribers = Sets.newConcurrentHashSet();
614         } else if (subscribers.contains(subscriber)) {
615             // If there is an entry already for this subscriber, remove it, so that it gets the new timestamp
616             subscribers.remove(subscriber);
617         }
618         if (LOG.isDebugEnabled()) {
619             LOG.debug("Adding new subscriber {} for EID {}", subscriber.getString(),
620                     LispAddressStringifier.getString(subscribedEid));
621         }
622         subscribers.add(subscriber);
623         addSubscribers(subscribedEid, subscribers);
624     }
625
626     private void addSubscribers(Eid address, Set<Subscriber> subscribers) {
627         LoggingUtil.logSubscribers(LOG, address, subscribers);
628         subscriberdb.put(address, subscribers);
629     }
630
631     @Override
632     public Set<Subscriber> getSubscribers(Eid address) {
633         if (LOG.isDebugEnabled()) {
634             LOG.debug("Retrieving subscribers for EID {}", LispAddressStringifier.getString(address));
635         }
636
637         Set<Subscriber> subscribers = subscriberdb.get(address);
638         LoggingUtil.logSubscribers(LOG, address, subscribers);
639         return subscribers;
640     }
641
642     private void removeSubscribers(Eid address) {
643         if (LOG.isDebugEnabled()) {
644             LOG.debug("Removing subscribers for EID {}", LispAddressStringifier.getString(address));
645         }
646         subscriberdb.remove(address);
647     }
648
649     @Override
650     public void addAuthenticationKey(Eid key, MappingAuthkey authKey) {
651         LOG.debug("Adding authentication key '{}' with key-ID {} for {}", authKey.getKeyString(), authKey.getKeyType(),
652                 LispAddressStringifier.getString(key));
653         akdb.addAuthenticationKey(key, authKey);
654     }
655
656     @Override
657     public MappingAuthkey getAuthenticationKey(Eid key) {
658         if (LOG.isDebugEnabled()) {
659             LOG.debug("Retrieving authentication key for {}", LispAddressStringifier.getString(key));
660         }
661         return akdb.getAuthenticationKey(key);
662     }
663
664     @Override
665     public void removeAuthenticationKey(Eid key) {
666         if (LOG.isDebugEnabled()) {
667             LOG.debug("Removing authentication key for {}", LispAddressStringifier.getString(key));
668         }
669         akdb.removeAuthenticationKey(key);
670     }
671
672     @Override
673     public void addData(MappingOrigin origin, Eid key, String subKey, Object data) {
674         if (LOG.isDebugEnabled()) {
675             LOG.debug("Add data of {} for key {} and subkey {}", data.getClass(),
676                     LispAddressStringifier.getString(key), subKey);
677         }
678         tableMap.get(origin).addData(key, subKey, data);
679     }
680
681     @Override
682     public Object getData(MappingOrigin origin, Eid key, String subKey) {
683         if (LOG.isDebugEnabled()) {
684             LOG.debug("Retrieving data for key {} and subkey {}", LispAddressStringifier.getString(key), subKey);
685         }
686         return tableMap.get(origin).getData(key, subKey);
687     }
688
689     @Override
690     public void removeData(MappingOrigin origin, Eid key, String subKey) {
691         if (LOG.isDebugEnabled()) {
692             LOG.debug("Removing data for key {} and subkey {}", LispAddressStringifier.getString(key), subKey);
693         }
694         tableMap.get(origin).removeData(key, subKey);
695     }
696
697     @Override
698     public Eid getParentPrefix(Eid key) {
699         return smc.getParentPrefix(key);
700     }
701
702
703     /**
704      * Restore all mappings and keys from mdsal datastore.
705      */
706     private void restoreDaoFromDatastore() {
707         List<AuthenticationKey> authKeys = dsbe.getAllAuthenticationKeys();
708         List<Mapping> mappings = dsbe.getAllMappings(LogicalDatastoreType.CONFIGURATION);
709
710         /*
711          * XXX By default, the operational datastore is not persisted to disk, either at run-time, or on shutdown,
712          * so the following will have no effect (getLastUpdateTimestamp() will fail, since it's reading from
713          * the operational datastore, and even if it didn't getAllMappings() will fail anyway). According to rovarga it
714          * should be possible to turn on persistence for the operational datastore editing
715          * etc/opendaylight/karaf/05-clustering.xml, by setting <persistence>true</persistence>. At the time of writing
716          * the below code block that didn't seem to work though.
717          */
718         Long lastUpdateTimestamp = dsbe.getLastUpdateTimestamp();
719         if (lastUpdateTimestamp != null && System.currentTimeMillis() - lastUpdateTimestamp
720                 > ConfigIni.getInstance().getRegistrationValiditySb()) {
721             LOG.warn("Restore threshold passed, not restoring operational datastore into DAO");
722         } else {
723             mappings.addAll(dsbe.getAllMappings(LogicalDatastoreType.OPERATIONAL));
724         }
725         dsbe.removeLastUpdateTimestamp();
726
727         LOG.info("Restoring {} mappings and {} keys from datastore into DAO", mappings.size(), authKeys.size());
728
729         for (Mapping mapping : mappings) {
730             addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(),
731                     new MappingData(mapping.getMappingRecord()));
732         }
733
734         for (AuthenticationKey authKey : authKeys) {
735             addAuthenticationKey(authKey.getEid(), authKey.getMappingAuthkey());
736         }
737     }
738
739     public void destroy() {
740         LOG.info("Mapping System is being destroyed!");
741         dsbe.saveLastUpdateTimestamp();
742     }
743
744     @Override
745     public String printMappings() {
746         sbMappingTimeoutService.removeExpiredMappings();
747
748         final StringBuilder sb = new StringBuilder();
749         sb.append("Policy map-cache\n----------------\n");
750         sb.append(pmc.printMappings());
751         sb.append("\nSouthbound map-cache\n--------------------\n");
752         sb.append(smc.printMappings());
753         return sb.toString();
754     }
755
756     @Override
757     public String prettyPrintMappings() {
758         sbMappingTimeoutService.removeExpiredMappings();
759
760         final StringBuilder sb = new StringBuilder();
761         sb.append("Policy map-cache\n----------------\n");
762         sb.append(pmc.prettyPrintMappings());
763         sb.append("\nSouthbound map-cache\n--------------------\n");
764         sb.append(smc.prettyPrintMappings());
765         sb.append("\nSubscribers\n-----------\n");
766         sb.append(prettyPrintSubscribers(subscriberdb));
767         return sb.toString();
768     }
769
770     private static String prettyPrintSubscribers(Map<Eid, Set<Subscriber>> subscribers) {
771         final StringBuilder sb = new StringBuilder();
772         for (Eid eid: subscribers.keySet()) {
773             sb.append("\n  ");
774             sb.append(LispAddressStringifier.getString(eid));
775             sb.append("\n");
776             sb.append(LispMapCacheStringifier.prettyPrintSubscriberSet(subscribers.get(eid), 4));
777             sb.append("\n");
778         }
779         return sb.toString();
780     }
781
782     @Override
783     public String printKeys() {
784         return akdb.printKeys();
785     }
786
787     @Override
788     public String prettyPrintKeys() {
789         return akdb.prettyPrintKeys();
790     }
791
792     public void cleanCaches() {
793         dao.removeAll();
794         subscriberdb.clear();
795         buildMapCaches();
796     }
797
798     public void cleanSBMappings() {
799         smc = new SimpleMapCache(sdao);
800     }
801
802     @Override
803     public void setIsMaster(boolean isMaster) {
804         this.isMaster = isMaster;
805     }
806
807     @Override
808     public boolean isMaster() {
809         return isMaster;
810     }
811 }